Appearance
| 1 | namespace Semantic.Lookups is | |
| 2 | use Source.LOCATION | |
| 3 | ||
| 4 | use Collections.LIST | |
| 5 | use Collections.List | |
| 6 | ||
| 7 | use Types | |
| 8 | ||
| 9 | // The built-in types as symbols constructed in code, for a target | |
| 10 | // that loads no assemblies. The scalars, `object`, `string` and the | |
| 11 | // generic families the language itself defines are declared into | |
| 12 | // `Ghul.Intrinsics` — the namespace every compilation imports — so | |
| 13 | // ordinary name resolution finds them with no resolver changes. | |
| 14 | // Their members are not constructed here: the core library supplies | |
| 15 | // those, and the find-only types (`Exception`, `Iterable`, the task | |
| 16 | // types) are looked up in what the compilation declares, answering | |
| 17 | // absent when the library has not declared them yet. | |
| 18 | class CONSTRUCTED_INNATE_SYMBOL_LOOKUP( | |
| 19 | _symbol_table: Semantic.SYMBOL_TABLE, | |
| 20 | _namespaces: Semantic.NAMESPACES, | |
| 21 | _source: GHUL_INNATE_TYPE_LOOKUP, | |
| 22 | _logger: Logging.Logger | |
| 23 | ): InnateSymbolLookup is | |
| 24 | _object_type: Type | |
| 25 | _string_type: Type | |
| 26 | _bigint_type: Type | |
| 27 | _value_type: Type | |
| 28 | _enum_type: Type | |
| 29 | _nullable: Semantic.Symbols.Classy | |
| 30 | _iterator: Semantic.Symbols.Classy | |
| 31 | _array: Semantic.Symbols.Classy | |
| 32 | _is_array_wired: bool | |
| 33 | _wired_list_ancestor: Type? | |
| 34 | _maybe_definition: Semantic.Symbols.Classy? | |
| 35 | ||
| 36 | // What construction declared into `Ghul.Intrinsics`, and the | |
| 37 | // members each of those started with, so that a rebuild can put | |
| 38 | // back exactly that. | |
| 39 | _built_ins: LIST[Semantic.Symbols.Symbol] | |
| 40 | _built_in_members: LIST[(scope: Semantic.Symbols.Scoped, names: Collections.SET[string])] | |
| 41 | _reported_missing: Collections.SET[string] | |
| 42 | ||
| 43 | init(..) is | |
| 44 | _reported_missing = Collections.SET[string]() | |
| 45 | ||
| 46 | let intrinsics = _intrinsics_namespace() | |
| 47 | ||
| 48 | // The two roots come first: every other built-in takes one of | |
| 49 | // them as its implicit ancestor, as a source-declared type of | |
| 50 | // the same kind does - a class or trait `object`, a struct | |
| 51 | // `ValueType` - and type parameters take `object`. | |
| 52 | let object_class = | |
| 53 | cast Semantic.Symbols.CLASS?( | |
| 54 | intrinsics.declare_class( | |
| 55 | LOCATION.internal, | |
| 56 | LOCATION.internal, | |
| 57 | "object", | |
| 58 | LIST[string](), | |
| 59 | intrinsics, | |
| 60 | null))! | |
| 61 | ||
| 62 | // The roots are open, as the reflected System.Object, | |
| 63 | // System.ValueType and System.Enum are: a type that derives from | |
| 64 | // one is not an alternative of a closed set, and inference never | |
| 65 | // widens a value to one. The value-type root is marked as the | |
| 66 | // reflected one is, so a join never settles on it either. | |
| 67 | object_class.mark_is_object() | |
| 68 | object_class.mark_open() | |
| 69 | _object_type = Types.NAMED(object_class) | |
| 70 | ||
| 71 | // The value-type base is constructed rather than declared: | |
| 72 | // no program names it, and the compiler needs it before any | |
| 73 | // source could have declared it. | |
| 74 | let value_type_class = | |
| 75 | Symbols.CLASS( | |
| 76 | LOCATION.internal, | |
| 77 | LOCATION.internal, | |
| 78 | intrinsics, | |
| 79 | "ValueType", | |
| 80 | LIST[string](), | |
| 81 | intrinsics) | |
| 82 | ||
| 83 | value_type_class.mark_is_root_value_type() | |
| 84 | value_type_class.mark_open() | |
| 85 | value_type_class.push_ancestor(_object_type) | |
| 86 | _value_type = Types.NAMED(value_type_class) | |
| 87 | ||
| 88 | // The enum base is constructed the same way, for the same | |
| 89 | // reasons: every enum takes it as an ancestor, and every cast | |
| 90 | // asks for it. | |
| 91 | let enum_class = | |
| 92 | Symbols.CLASS( | |
| 93 | LOCATION.internal, | |
| 94 | LOCATION.internal, | |
| 95 | intrinsics, | |
| 96 | "Enum", | |
| 97 | LIST[string](), | |
| 98 | intrinsics) | |
| 99 | ||
| 100 | enum_class.mark_open() | |
| 101 | enum_class.push_ancestor(_value_type) | |
| 102 | _enum_type = Types.NAMED(enum_class) | |
| 103 | ||
| 104 | let scalars = _declare_scalars(intrinsics) | |
| 105 | _declare_families(intrinsics) | |
| 106 | ||
| 107 | _string_type = Types.NAMED(_declare_class(intrinsics, "string")) | |
| 108 | let bigint_struct = _declare_struct(intrinsics, "bigint", null) | |
| 109 | ||
| 110 | _bigint_type = Types.NAMED(bigint_struct) | |
| 111 | ||
| 112 | // A scalar is constructed from a bigint too, converting as `cast` | |
| 113 | // does through the conversion the core library declares. | |
| 114 | _declare_scalar_conversions(scalars.structs, LIST[Semantic.Symbols.STRUCT]([bigint_struct]), scalars.void_type) | |
| 115 | ||
| 116 | // `T?` over a value type has no user-spellable name, so the | |
| 117 | // carrier is constructed against the namespace without being | |
| 118 | // declared into it: reachable from here, invisible to name | |
| 119 | // resolution. | |
| 120 | _nullable = | |
| 121 | cast Semantic.Symbols.Classy?( | |
| 122 | Symbols.STRUCT( | |
| 123 | LOCATION.internal, | |
| 124 | LOCATION.internal, | |
| 125 | intrinsics, | |
| 126 | "NULLABLE", | |
| 127 | LIST[string](["T"]), | |
| 128 | intrinsics))! | |
| 129 | ||
| 130 | _nullable.push_ancestor(_value_type) | |
| 131 | ||
| 132 | // The two members every value-type optional is read through: a | |
| 133 | // presence test reads `has_value`, and a narrowed read projects | |
| 134 | // the payload through `value`. Each lowers to an operation on | |
| 135 | // the carrier rather than to a call. | |
| 136 | _declare_innate_property(_nullable, "value", _nullable.type_parameter_at(0)!.type!, "nullable.value") | |
| 137 | _declare_innate_property(_nullable, "has_value", get_bool_type(), "nullable.has_value") | |
| 138 | ||
| 139 | // The iterator type, where the compilation declares no | |
| 140 | // `Collections.Iterator`: constructed, undeclared, and so on no | |
| 141 | // type's ancestry, so nothing implements it. | |
| 142 | _iterator = | |
| 143 | cast Semantic.Symbols.Classy?( | |
| 144 | Symbols.TRAIT( | |
| 145 | LOCATION.internal, | |
| 146 | LOCATION.internal, | |
| 147 | intrinsics, | |
| 148 | "Iterator", | |
| 149 | LIST[string](["T"]), | |
| 150 | intrinsics))! | |
| 151 | ||
| 152 | _built_ins = LIST[Semantic.Symbols.Symbol](intrinsics.symbols) | |
| 153 | _built_in_members = LIST[(scope: Semantic.Symbols.Scoped, names: Collections.SET[string])]() | |
| 154 | ||
| 155 | for symbol in _built_ins do | |
| 156 | if let scoped: Semantic.Symbols.Scoped = symbol then | |
| 157 | let names = Collections.SET[string]() | |
| 158 | ||
| 159 | for member in scoped.symbols do | |
| 160 | names.add(member.name) | |
| 161 | od | |
| 162 | ||
| 163 | _built_in_members.add((scoped, names)) | |
| 164 | fi | |
| 165 | od | |
| 166 | si | |
| 167 | ||
| 168 | // `Ghul.Intrinsics` is materialised before any pass runs on | |
| 169 | // every target, so it exists by the time the first lookup | |
| 170 | // constructs: the built-in types are declared into it, and the | |
| 171 | // ambient `use` every namespace block gets finds them. | |
| 172 | _intrinsics_namespace() -> Semantic.Symbols.NAMESPACE is | |
| 173 | let intrinsics = _namespaces.try_find_namespace(".Ghul.Intrinsics") | |
| 174 | ||
| 175 | assert intrinsics? | |
| 176 | else "Ghul.Intrinsics should be materialised before the constructed lookup builds" | |
| 177 | ||
| 178 | return intrinsics! | |
| 179 | si | |
| 180 | ||
| 181 | // Declares the scalar types, and answers them and void for the | |
| 182 | // conversions declared from types constructed later. | |
| 183 | _declare_scalars(intrinsics: Semantic.Symbols.NAMESPACE) -> (structs: List[Semantic.Symbols.STRUCT], void_type: Type) is | |
| 184 | // A VOID_STRUCT, as the reflected void is, since that is what | |
| 185 | // answers is_void. | |
| 186 | let void_struct = | |
| 187 | Symbols.VOID_STRUCT( | |
| 188 | LOCATION.internal, LOCATION.internal, intrinsics, "void", LIST[string](), intrinsics) | |
| 189 | ||
| 190 | void_struct.built_in_type = Symbols.BuiltInType.VOID | |
| 191 | void_struct.push_ancestor(_value_type) | |
| 192 | ||
| 193 | intrinsics.declare(LOCATION.internal, void_struct, null) | |
| 194 | ||
| 195 | _declare_struct(intrinsics, "bool", Symbols.BuiltInType.BOOL) | |
| 196 | ||
| 197 | let scalars = LIST[Semantic.Symbols.STRUCT]([ | |
| 198 | _declare_struct(intrinsics, "char", Symbols.BuiltInType.CHAR), | |
| 199 | _declare_struct(intrinsics, "byte", Symbols.BuiltInType.BYTE), | |
| 200 | _declare_struct(intrinsics, "ubyte", Symbols.BuiltInType.UBYTE), | |
| 201 | _declare_struct(intrinsics, "short", Symbols.BuiltInType.SHORT), | |
| 202 | _declare_struct(intrinsics, "ushort", Symbols.BuiltInType.USHORT), | |
| 203 | _declare_struct(intrinsics, "int", Symbols.BuiltInType.INT), | |
| 204 | _declare_struct(intrinsics, "uint", Symbols.BuiltInType.UINT), | |
| 205 | _declare_struct(intrinsics, "long", Symbols.BuiltInType.LONG), | |
| 206 | _declare_struct(intrinsics, "ulong", Symbols.BuiltInType.ULONG), | |
| 207 | _declare_struct(intrinsics, "word", Symbols.BuiltInType.WORD), | |
| 208 | _declare_struct(intrinsics, "uword", Symbols.BuiltInType.UWORD), | |
| 209 | _declare_struct(intrinsics, "single", Symbols.BuiltInType.SINGLE), | |
| 210 | _declare_struct(intrinsics, "double", Symbols.BuiltInType.DOUBLE) | |
| 211 | ]) | |
| 212 | ||
| 213 | _declare_struct(intrinsics, "decimal", null) | |
| 214 | ||
| 215 | let void_type = Types.NAMED(void_struct) | |
| 216 | ||
| 217 | _declare_scalar_conversions(scalars, scalars, void_type) | |
| 218 | ||
| 219 | return (structs = scalars, void_type = void_type) | |
| 220 | si | |
| 221 | ||
| 222 | // A scalar type is constructed from any other scalar, converting the | |
| 223 | // way `cast` does: `double(n)` is `cast double(n)`. The .NET target | |
| 224 | // gives the reflected scalar types the same constructors. | |
| 225 | _declare_scalar_conversions(owners: List[Semantic.Symbols.STRUCT], sources: List[Semantic.Symbols.STRUCT], void_type: Type) is | |
| 226 | for owner in owners do | |
| 227 | for source in sources do | |
| 228 | let result = Symbols.STRUCT_METHOD(LOCATION.internal, LOCATION.internal, owner, "init", owner) | |
| 229 | ||
| 230 | result.argument_names = LIST[string](["value"]) | |
| 231 | result.arguments = LIST[Type]([Types.NAMED(source)]) | |
| 232 | result.return_type = void_type | |
| 233 | result.is_scalar_conversion = true | |
| 234 | result.mark_declared_pure() | |
| 235 | result.mark_synthesized() | |
| 236 | ||
| 237 | owner.add_member(result) | |
| 238 | od | |
| 239 | od | |
| 240 | si | |
| 241 | ||
| 242 | _declare_families(intrinsics: Semantic.Symbols.NAMESPACE) is | |
| 243 | for i in 1::8 do | |
| 244 | _declare_with_arity(intrinsics, "TUPLE_{i}", i, true) | |
| 245 | od | |
| 246 | ||
| 247 | for i in 0::16 do | |
| 248 | _declare_with_arity(intrinsics, "FUNCTION_{i}", i + 1, false) | |
| 249 | od | |
| 250 | ||
| 251 | for i in 0::16 do | |
| 252 | _declare_with_arity(intrinsics, "ACTION_{i}", i, false) | |
| 253 | od | |
| 254 | ||
| 255 | // The three shapes the runtime's own intrinsics declare in | |
| 256 | // source; under this target nothing declares them, so the | |
| 257 | // carrier symbols are constructed here for the lookups | |
| 258 | // below to specialise. | |
| 259 | // The array family is a class here: the core library gives it | |
| 260 | // members with bodies, so a value of it is dispatched through | |
| 261 | // a trait it implements as any class's is. | |
| 262 | _array = | |
| 263 | cast Semantic.Symbols.Classy?( | |
| 264 | intrinsics.declare_class( | |
| 265 | LOCATION.internal, | |
| 266 | LOCATION.internal, | |
| 267 | "ARRAY", | |
| 268 | LIST[string](["E"]), | |
| 269 | intrinsics, | |
| 270 | null))! | |
| 271 | ||
| 272 | _array.push_ancestor(_object_type) | |
| 273 | _declare_type_arguments(_array, LIST[string](["E"])) | |
| 274 | ||
| 275 | _declare_with_arity(intrinsics, "REFERENCE", 1, true) | |
| 276 | _declare_with_arity(intrinsics, "POINTER", 1, true) | |
| 277 | si | |
| 278 | ||
| 279 | _type_argument_names(arity: int) -> List[string] is | |
| 280 | let result = LIST[string](arity) | |
| 281 | ||
| 282 | for i in 1::arity do | |
| 283 | result.add("T{i}") | |
| 284 | od | |
| 285 | ||
| 286 | return result | |
| 287 | si | |
| 288 | ||
| 289 | _declare_with_arity(intrinsics: Semantic.Symbols.NAMESPACE, name: string, arity: int, want_struct: bool) is | |
| 290 | let arguments = _type_argument_names(arity) | |
| 291 | ||
| 292 | let declared = | |
| 293 | if want_struct then | |
| 294 | intrinsics.declare_struct( | |
| 295 | LOCATION.internal, LOCATION.internal, name, arguments, intrinsics, null) | |
| 296 | else | |
| 297 | intrinsics.declare_class( | |
| 298 | LOCATION.internal, LOCATION.internal, name, arguments, intrinsics, null) | |
| 299 | fi | |
| 300 | ||
| 301 | let family = cast Semantic.Symbols.Classy?(declared)! | |
| 302 | ||
| 303 | family.push_ancestor(if want_struct then _value_type else _object_type fi) | |
| 304 | ||
| 305 | _declare_type_arguments(family, arguments) | |
| 306 | si | |
| 307 | ||
| 308 | // A source-declared type's parameters are declared into its own | |
| 309 | // scope, which is what makes them visible inside a `partial` | |
| 310 | // block that reopens it, and take `object` as their implicit | |
| 311 | // ancestor, which is what makes them assignable to it. The | |
| 312 | // constructed families are reopened the same way by the core | |
| 313 | // library, so their parameters need the same declaration. | |
| 314 | _declare_type_arguments(type: Semantic.Symbols.Classy?, arguments: List[string]) is | |
| 315 | if !type? then | |
| 316 | return | |
| 317 | fi | |
| 318 | ||
| 319 | let ancestors = LIST[Type]() | |
| 320 | ancestors.add(_object_type) | |
| 321 | let index mut = 0 | |
| 322 | ||
| 323 | for name in arguments do | |
| 324 | let argument = type.declare_type(LOCATION.internal, name, index, null) | |
| 325 | argument.set_ancestor_types(ancestors) | |
| 326 | index = index + 1 | |
| 327 | od | |
| 328 | si | |
| 329 | ||
| 330 | _declare_struct( | |
| 331 | intrinsics: Semantic.Symbols.NAMESPACE, | |
| 332 | name: string, | |
| 333 | built_in: Symbols.BuiltInType?) -> Semantic.Symbols.STRUCT is | |
| 334 | let result = | |
| 335 | cast Semantic.Symbols.STRUCT?( | |
| 336 | intrinsics.declare_struct( | |
| 337 | LOCATION.internal, | |
| 338 | LOCATION.internal, | |
| 339 | name, | |
| 340 | LIST[string](), | |
| 341 | intrinsics, | |
| 342 | null))! | |
| 343 | ||
| 344 | if let marker = built_in then | |
| 345 | result.built_in_type = marker | |
| 346 | fi | |
| 347 | ||
| 348 | result.push_ancestor(_value_type) | |
| 349 | ||
| 350 | return result | |
| 351 | si | |
| 352 | ||
| 353 | _declare_class(intrinsics: Semantic.Symbols.NAMESPACE, name: string) -> Semantic.Symbols.CLASS is | |
| 354 | let result = | |
| 355 | cast Semantic.Symbols.CLASS?( | |
| 356 | intrinsics.declare_class( | |
| 357 | LOCATION.internal, | |
| 358 | LOCATION.internal, | |
| 359 | name, | |
| 360 | LIST[string](), | |
| 361 | intrinsics, | |
| 362 | null))! | |
| 363 | ||
| 364 | result.push_ancestor(_object_type) | |
| 365 | ||
| 366 | return result | |
| 367 | si | |
| 368 | ||
| 369 | // The types this compilation declares, searched by qualified | |
| 370 | // name. A find-only type the library has not declared yet | |
| 371 | // answers absent, which is what lets the library start as | |
| 372 | // stubs. | |
| 373 | _find_declared(qualified_name: string) -> Type? is | |
| 374 | let parts = LIST[string]() | |
| 375 | ||
| 376 | for part in _split(qualified_name, '.') do | |
| 377 | parts.add(part) | |
| 378 | od | |
| 379 | ||
| 380 | let scope: Semantic.Scope? mut = _symbol_table.global_scope | |
| 381 | ||
| 382 | while parts.count > 0 /\ scope? do | |
| 383 | let name = parts[0] | |
| 384 | parts.remove_at(0) | |
| 385 | ||
| 386 | let found = scope.find_direct(name) | |
| 387 | ||
| 388 | if !found? then | |
| 389 | return null | |
| 390 | fi | |
| 391 | ||
| 392 | scope = cast Semantic.Scope?(found) | |
| 393 | od | |
| 394 | ||
| 395 | if let reached = scope then | |
| 396 | if let symbol = cast Semantic.Symbols.Classy?(reached) /\ _is_library_declared(symbol) then | |
| 397 | return Types.NAMED(symbol) | |
| 398 | fi | |
| 399 | fi | |
| 400 | ||
| 401 | return null | |
| 402 | si | |
| 403 | ||
| 404 | // Whether `symbol` can stand for a type the compiler gives special | |
| 405 | // meaning to: only one a library source declares can, so a type the | |
| 406 | // program declares under the same name is an ordinary type. | |
| 407 | _is_library_declared(symbol: Semantic.Symbols.Symbol) -> bool => | |
| 408 | IoC.CONTAINER.instance.build_flags.library_sources?.can_declare_core(symbol.location.file_name) ?? true | |
| 409 | ||
| 410 | // The class the core library declares as `qualified_name` taking | |
| 411 | // `arguments` type arguments, where two share the name, or null. | |
| 412 | _find_declared_class(qualified_name: string, arguments: int) -> Semantic.Symbols.Classy? is | |
| 413 | let dot = qualified_name.last_index_of('.') | |
| 414 | let container = cast Semantic.Scope?(_find_declared_scope(qualified_name.substring(0, dot))) | |
| 415 | let found = container?.find_direct(qualified_name.substring(dot + 1)) | |
| 416 | ||
| 417 | let classy = | |
| 418 | if let group: Semantic.Symbols.TYPE_GROUP = found then | |
| 419 | group.find_by_generic_arguments_count(arguments) | |
| 420 | elif let c: Semantic.Symbols.Classy = found /\ c.argument_names.count == arguments then | |
| 421 | c | |
| 422 | else | |
| 423 | null | |
| 424 | fi | |
| 425 | ||
| 426 | return if let c = classy /\ _is_library_declared(c) then c else null fi | |
| 427 | si | |
| 428 | ||
| 429 | // The namespace or type the core library declares as `qualified_name`. | |
| 430 | _find_declared_scope(qualified_name: string) -> Semantic.Symbols.Symbol? is | |
| 431 | let scope: Semantic.Scope? mut = _symbol_table.global_scope | |
| 432 | let found: Semantic.Symbols.Symbol? mut = null | |
| 433 | ||
| 434 | for part in _split(qualified_name, '.') do | |
| 435 | if !scope? then | |
| 436 | return null | |
| 437 | fi | |
| 438 | ||
| 439 | found = scope.find_direct(part) | |
| 440 | scope = cast Semantic.Scope?(found) | |
| 441 | od | |
| 442 | ||
| 443 | return found | |
| 444 | si | |
| 445 | ||
| 446 | _split(text: string, separator: char) -> List[string] is | |
| 447 | let result = LIST[string]() | |
| 448 | ||
| 449 | let current = System.Text.StringBuilder() | |
| 450 | ||
| 451 | for c in text do | |
| 452 | if c == separator then | |
| 453 | result.add("{current}") | |
| 454 | current.clear() | |
| 455 | else | |
| 456 | current.append(c) | |
| 457 | fi | |
| 458 | od | |
| 459 | ||
| 460 | result.add("{current}") | |
| 461 | ||
| 462 | return result | |
| 463 | si | |
| 464 | ||
| 465 | // A read-only instance property on `owner` whose getter lowers to | |
| 466 | // `operation` rather than to a call. | |
| 467 | _declare_innate_property(owner: Semantic.Symbols.Classy, name: string, type: Type, operation: string) is | |
| 468 | let property = cast Semantic.Symbols.Property( | |
| 469 | owner.declare_property(LOCATION.internal, LOCATION.internal, name, false, false, false, null)) | |
| 470 | ||
| 471 | let getter = Semantic.Symbols.INNATE_METHOD(LOCATION.internal, owner, name, owner, operation) | |
| 472 | ||
| 473 | getter.arguments = LIST[Type]() | |
| 474 | getter.argument_names = LIST[string]() | |
| 475 | getter.return_type = type | |
| 476 | getter.mark_declared_pure() | |
| 477 | ||
| 478 | property.set_type(type) | |
| 479 | property.read_function = getter | |
| 480 | si | |
| 481 | ||
| 482 | // A type the language needs and the core library has not declared. | |
| 483 | // The fault is in the library rather than the source being compiled, | |
| 484 | // so it is reported once and every use carries on with the error type. | |
| 485 | _required(what: string) -> Type is | |
| 486 | if _reported_missing.add(what) then | |
| 487 | _logger.environment_error(LOCATION.internal, "the core library does not declare {what}") | |
| 488 | fi | |
| 489 | ||
| 490 | return Types.ERROR() | |
| 491 | si | |
| 492 | ||
| 493 | get_enum_type() -> Type => _enum_type | |
| 494 | get_tuple_type(types: List[Type], names: List[string?]?) -> Type => _source.get_tuple_type(types, names) | |
| 495 | get_function_type(types: List[Type]) -> Type => _source.get_function_type(types) | |
| 496 | get_function_type(types: List[Type], is_pure: bool) -> Type => _source.get_function_type(types, is_pure) | |
| 497 | get_array_type(type: Type) -> Type => _source.get_array_type(type) | |
| 498 | get_optional_type(type: Type) -> Type => Types.NULLABLE(LOCATION.internal, _nullable, LIST[Type]([type])) | |
| 499 | ||
| 500 | get_maybe_type(type: Type) -> Type? is | |
| 501 | if let maybe = _find_declared("Ghul.MAYBE") then | |
| 502 | if let classy = cast Semantic.Symbols.Classy?(maybe.symbol) then | |
| 503 | return Types.MAYBE(LOCATION.internal, classy, LIST[Type]([type])) | |
| 504 | fi | |
| 505 | fi | |
| 506 | ||
| 507 | return null | |
| 508 | si | |
| 509 | ||
| 510 | // Found once the core library has been declared; until then a | |
| 511 | // definition is not known to be it, and the search runs again. | |
| 512 | is_maybe_definition(definition: Semantic.Symbols.Classy) -> bool is | |
| 513 | if !_maybe_definition? then | |
| 514 | _maybe_definition = cast Semantic.Symbols.Classy?(_find_declared("Ghul.MAYBE")?.symbol) | |
| 515 | fi | |
| 516 | ||
| 517 | return _maybe_definition? /\ definition == _maybe_definition | |
| 518 | si | |
| 519 | ||
| 520 | get_equality_comparer_type(type: Type) -> Type? => null | |
| 521 | ||
| 522 | compares_by_instantiation: bool => true | |
| 523 | get_list_class() -> Semantic.Symbols.Classy? => cast Semantic.Symbols.Classy?(_find_declared("Collections.LIST")?.symbol) | |
| 524 | ||
| 525 | get_map_class() -> Semantic.Symbols.Classy? => cast Semantic.Symbols.Classy?(_find_declared("Collections.MAP")?.symbol) | |
| 526 | ||
| 527 | get_set_class() -> Semantic.Symbols.Classy? => cast Semantic.Symbols.Classy?(_find_declared("Collections.SET")?.symbol) | |
| 528 | ||
| 529 | get_list_type(type: Type) -> Type? is | |
| 530 | if let list = _find_declared("Collections.LIST") then | |
| 531 | if let classy = cast Semantic.Symbols.Classy?(list.symbol) then | |
| 532 | return Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type])) | |
| 533 | fi | |
| 534 | fi | |
| 535 | ||
| 536 | return null | |
| 537 | si | |
| 538 | ||
| 539 | get_pointer_type(type: Type) -> Type => _source.get_pointer_type(type) | |
| 540 | get_reference_type(type: Type) -> Type => _source.get_reference_type(type) | |
| 541 | ||
| 542 | get_bool_type() -> Type => _source.get_bool_type() | |
| 543 | get_char_type() -> Type => _source.get_char_type() | |
| 544 | get_byte_type() -> Type => _source.get_byte_type() | |
| 545 | get_ubyte_type() -> Type => _source.get_ubyte_type() | |
| 546 | get_short_type() -> Type => _source.get_short_type() | |
| 547 | get_ushort_type() -> Type => _source.get_ushort_type() | |
| 548 | get_int_type() -> Type => _source.get_int_type() | |
| 549 | get_uint_type() -> Type => _source.get_uint_type() | |
| 550 | get_long_type() -> Type => _source.get_long_type() | |
| 551 | get_ulong_type() -> Type => _source.get_ulong_type() | |
| 552 | get_word_type() -> Type => _source.get_word_type() | |
| 553 | get_uword_type() -> Type => _source.get_uword_type() | |
| 554 | get_single_type() -> Type => _source.get_single_type() | |
| 555 | get_double_type() -> Type => _source.get_double_type() | |
| 556 | get_decimal_type() -> Type => _source.get_decimal_type() | |
| 557 | get_bigint_type() -> Type => _bigint_type | |
| 558 | get_void_type() -> Type => _source.get_void_type() | |
| 559 | get_object_type() -> Type => _object_type | |
| 560 | get_value_type() -> Type => _value_type | |
| 561 | get_string_type() -> Type => _string_type | |
| 562 | ||
| 563 | get_exception_type() -> Type is | |
| 564 | if let found = _find_declared("Ghul.Exception") then | |
| 565 | return found | |
| 566 | fi | |
| 567 | ||
| 568 | return _required("Ghul.Exception") | |
| 569 | si | |
| 570 | ||
| 571 | find_exception_type() -> Type? => _find_declared("Ghul.Exception") | |
| 572 | ||
| 573 | get_invalid_cast_exception_type() -> Type is | |
| 574 | if let found = _find_declared("Ghul.InvalidCastException") then | |
| 575 | return found | |
| 576 | fi | |
| 577 | ||
| 578 | return _required("Ghul.InvalidCastException") | |
| 579 | si | |
| 580 | ||
| 581 | get_overflow_exception_type() -> Type is | |
| 582 | if let found = _find_declared("Ghul.OverflowException") then | |
| 583 | return found | |
| 584 | fi | |
| 585 | ||
| 586 | return _required("Ghul.OverflowException") | |
| 587 | si | |
| 588 | ||
| 589 | find_type_type() -> Type? => _find_declared("Ghul.Type") | |
| 590 | find_environment_type() -> Type? => _find_declared("Ghul.ENVIRONMENT") | |
| 591 | ||
| 592 | find_runtime_exception_type(kind: RuntimeException) -> Type? => _find_declared("Ghul.{RUNTIME_EXCEPTIONS.name_of(kind)}") | |
| 593 | ||
| 594 | get_type_type() -> Type is | |
| 595 | if let found = _find_declared("Ghul.Type") then | |
| 596 | return found | |
| 597 | fi | |
| 598 | ||
| 599 | return _required("Ghul.Type") | |
| 600 | si | |
| 601 | ||
| 602 | get_unspecialized_iterable_type() -> Type is | |
| 603 | if let found = _find_declared("Collections.Iterable") then | |
| 604 | return found | |
| 605 | fi | |
| 606 | ||
| 607 | return _required("Collections.Iterable") | |
| 608 | si | |
| 609 | ||
| 610 | get_unspecialized_list_type() -> Type is | |
| 611 | if let found = _find_declared("Collections.List") then | |
| 612 | return found | |
| 613 | fi | |
| 614 | ||
| 615 | return _required("Collections.List") | |
| 616 | si | |
| 617 | ||
| 618 | get_unspecialized_mutable_list_type() -> Type is | |
| 619 | if let found = _find_declared("Collections.LIST") then | |
| 620 | return found | |
| 621 | fi | |
| 622 | ||
| 623 | return _required("Collections.LIST") | |
| 624 | si | |
| 625 | ||
| 626 | // The runtime declares the array family as | |
| 627 | // `trait ARRAY[E]: Collections.List[E]`, so an array is a List | |
| 628 | // and an Iterable of its element. Under this target the core | |
| 629 | // library declares List, so the constructed family takes that | |
| 630 | // ancestor from the compilation once its declarations exist. A | |
| 631 | // compilation that declares no List leaves arrays without one. | |
| 632 | wire_declared_ancestry() is | |
| 633 | if _is_array_wired then | |
| 634 | return | |
| 635 | fi | |
| 636 | ||
| 637 | let list = cast Semantic.Symbols.Classy?( | |
| 638 | cast Types.NAMED?(_find_declared("Collections.List"))?.symbol) | |
| 639 | ||
| 640 | if !list? then | |
| 641 | return | |
| 642 | fi | |
| 643 | ||
| 644 | let element = _array.type_parameter_at(0)?.type | |
| 645 | ||
| 646 | if !element? then | |
| 647 | return | |
| 648 | fi | |
| 649 | ||
| 650 | let arguments = LIST[Type]() | |
| 651 | arguments.add(element) | |
| 652 | ||
| 653 | let ancestor = Types.GENERIC(LOCATION.internal, list, arguments) | |
| 654 | ||
| 655 | _array.add_ancestor(ancestor) | |
| 656 | _wired_list_ancestor = ancestor | |
| 657 | _is_array_wired = true | |
| 658 | si | |
| 659 | ||
| 660 | // Puts the built-ins back as construction left them: what the | |
| 661 | // last build pulled down into them, what its partial blocks added | |
| 662 | // and the List it wired arrays to all belong to symbols the | |
| 663 | // rebuild has abandoned. | |
| 664 | redeclare_built_ins() is | |
| 665 | for (scope, names) in _built_in_members do | |
| 666 | if let classy: Semantic.Symbols.Classy = scope then | |
| 667 | classy.reset_pulled_down_symbols() | |
| 668 | fi | |
| 669 | ||
| 670 | for member in LIST[Semantic.Symbols.Symbol](scope.symbols) do | |
| 671 | if !names.contains(member.name) then | |
| 672 | scope.remove_direct(member.name) | |
| 673 | fi | |
| 674 | od | |
| 675 | od | |
| 676 | ||
| 677 | if let wired = _wired_list_ancestor then | |
| 678 | _array.remove_ancestor(wired) | |
| 679 | _wired_list_ancestor = null | |
| 680 | _is_array_wired = false | |
| 681 | fi | |
| 682 | ||
| 683 | let intrinsics = _intrinsics_namespace() | |
| 684 | ||
| 685 | for symbol in _built_ins do | |
| 686 | intrinsics.put_direct(symbol.name, symbol) | |
| 687 | od | |
| 688 | si | |
| 689 | ||
| 690 | get_int_range_types() -> (exclusive: Type, inclusive: Type)? is | |
| 691 | let exclusive = _find_declared("Ghul.INT_RANGE") | |
| 692 | let inclusive = _find_declared("Ghul.INT_RANGE_INCLUSIVE") | |
| 693 | ||
| 694 | if exclusive? /\ inclusive? then | |
| 695 | return (exclusive, inclusive) | |
| 696 | fi | |
| 697 | ||
| 698 | return null | |
| 699 | si | |
| 700 | ||
| 701 | get_unspecialized_iterator_type() -> Type => | |
| 702 | _find_declared("Collections.Iterator") ?? Types.NAMED(_iterator) | |
| 703 | get_unspecialized_pipe_type() -> Type? => _find_declared("Ghul.Pipes.Pipe") | |
| 704 | // The core library declares `Tasks.TASK` and `Tasks.TASK[T]` as two | |
| 705 | // types sharing a name, with their builders beside them. | |
| 706 | get_unspecialized_task_type() -> Type? => | |
| 707 | if let classy = _find_declared_class("Tasks.TASK", 1) then Types.NAMED(classy) else null fi | |
| 708 | ||
| 709 | // The core library declares the builder in `Text`, as .NET names | |
| 710 | // it, or in an earlier home where it predates the move. | |
| 711 | get_interpolated_string_handler_type() -> Type is | |
| 712 | for home in CORE_TYPE_ALIASES.homes("Text.STRING_BUILDER") do | |
| 713 | if let found = _find_declared(home) then | |
| 714 | return found | |
| 715 | fi | |
| 716 | od | |
| 717 | ||
| 718 | return _required("the interpolated string handler") | |
| 719 | si | |
| 720 | ||
| 721 | get_idisposable_type() -> Type is | |
| 722 | if let found = _find_declared("Ghul.Disposable") then | |
| 723 | return found | |
| 724 | fi | |
| 725 | ||
| 726 | return _required("Ghul.Disposable") | |
| 727 | si | |
| 728 | ||
| 729 | get_multicast_delegate_type() -> Type? => null | |
| 730 | ||
| 731 | get_box_type(type: Type) -> Type is | |
| 732 | if let box = _find_declared("Ghul.BOX") then | |
| 733 | if let classy = cast Semantic.Symbols.Classy?(box.symbol) then | |
| 734 | return Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type])) | |
| 735 | fi | |
| 736 | fi | |
| 737 | ||
| 738 | return _required("Ghul.BOX") | |
| 739 | si | |
| 740 | ||
| 741 | get_range_type() -> Type is | |
| 742 | if let found = _find_declared("System.Range") then | |
| 743 | return found | |
| 744 | fi | |
| 745 | ||
| 746 | return _required("System.Range") | |
| 747 | si | |
| 748 | ||
| 749 | get_int_range_type() -> Type is | |
| 750 | if let found = _find_declared("Ghul.INT_RANGE") then | |
| 751 | return found | |
| 752 | fi | |
| 753 | ||
| 754 | return _required("Ghul.INT_RANGE") | |
| 755 | si | |
| 756 | ||
| 757 | get_int_range_inclusive_type() -> Type is | |
| 758 | if let found = _find_declared("Ghul.INT_RANGE_INCLUSIVE") then | |
| 759 | return found | |
| 760 | fi | |
| 761 | ||
| 762 | return _required("Ghul.INT_RANGE_INCLUSIVE") | |
| 763 | si | |
| 764 | ||
| 765 | get_slice_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _source.slice_functions | |
| 766 | get_join_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _source.join_functions | |
| 767 | get_display_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _source.display_functions | |
| 768 | get_enum_text_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _source.enum_text_functions | |
| 769 | get_formattable_symbol() -> Semantic.Symbols.Symbol? => _source.formattable_symbol | |
| 770 | get_render_elements_functions() -> Semantic.Symbols.FUNCTION_GROUP? => _source.render_elements_functions | |
| 771 | get_pipes_fusion_functions() -> Collections.MAP[Semantic.Symbols.Symbol, string] => _source.fusion_functions | |
| 772 | get_pipes_unbounded_sources() -> Collections.SET[Semantic.Symbols.Symbol] => _source.unbounded_sources | |
| 773 | ||
| 774 | is_pipes_function(function: Semantic.Symbols.Function) -> bool => _source.is_pipes_function(function) | |
| 775 | stops_early(function: Semantic.Symbols.Function) -> bool => false | |
| 776 | reads_whole_source(function: Semantic.Symbols.Function) -> bool => false | |
| 777 | holds_resource(function: Semantic.Symbols.Function) -> bool => false | |
| 778 | ||
| 779 | get_task_type(type: Type) -> Type? => | |
| 780 | if let classy = _find_declared_class("Tasks.TASK", 1) then Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type])) else null fi | |
| 781 | ||
| 782 | get_void_task_type() -> Type? => | |
| 783 | if let classy = _find_declared_class("Tasks.TASK", 0) then Types.NAMED(classy) else null fi | |
| 784 | ||
| 785 | get_async_task_method_builder_type(type: Type) -> Type? => | |
| 786 | if let classy = _find_declared_class("Tasks.TASK_BUILDER", 1) then Types.GENERIC(LOCATION.internal, classy, LIST[Type]([type])) else null fi | |
| 787 | ||
| 788 | get_async_task_method_builder_void_type() -> Type? => | |
| 789 | if let classy = _find_declared_class("Tasks.TASK_BUILDER", 0) then Types.NAMED(classy) else null fi | |
| 790 | ||
| 791 | // The core library declares these under the names .NET gives them, | |
| 792 | // for an asynchronous function whose task-like type a library | |
| 793 | // declares. | |
| 794 | get_async_state_machine_interface_type() -> Type? => _find_declared("System.Runtime.CompilerServices.IAsyncStateMachine") | |
| 795 | get_notify_completion_type() -> Type? => _find_declared("System.Runtime.CompilerServices.INotifyCompletion") | |
| 796 | get_critical_notify_completion_type() -> Type? => _find_declared("System.Runtime.CompilerServices.ICriticalNotifyCompletion") | |
| 797 | si | |
| 798 | si |