Appearance
| 1 | namespace Semantic.Types is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use Source.LOCATION | |
| 5 | ||
| 6 | use Logging | |
| 7 | ||
| 8 | use Ghul.Pipes | |
| 9 | ||
| 10 | enum TypeVariance is | |
| 11 | INVARIANT, | |
| 12 | COVARIANT, | |
| 13 | CONTRAVARIANT | |
| 14 | si | |
| 15 | ||
| 16 | class GENERIC: NAMED is | |
| 17 | _is_wild: byte | |
| 18 | ||
| 19 | arguments: Collections.List[Type] => symbol.arguments | |
| 20 | ||
| 21 | is_error: bool => arguments |> any(a => a.is_error) | |
| 22 | ||
| 23 | // should be | |
| 24 | // is_wild: bool => arguments |> any(a => a.is_wild) | |
| 25 | // but the following shaves about 10% off compiler build time | |
| 26 | is_wild: bool is | |
| 27 | if _is_wild == 0b then | |
| 28 | let a = symbol.arguments | |
| 29 | let c = a.count | |
| 30 | ||
| 31 | let i mut = 0 | |
| 32 | ||
| 33 | while i < c do | |
| 34 | if a[i].is_wild then | |
| 35 | _is_wild = 1b | |
| 36 | return true | |
| 37 | fi | |
| 38 | ||
| 39 | i = i + 1 | |
| 40 | od | |
| 41 | ||
| 42 | _is_wild = 2b | |
| 43 | return false | |
| 44 | fi | |
| 45 | ||
| 46 | return _is_wild == 1b | |
| 47 | si | |
| 48 | ||
| 49 | // Recurse into arguments — Function[placeholder, int] is | |
| 50 | // a GENERIC whose self.is_inferred is false but whose | |
| 51 | // arguments contain a placeholder. The iterative-inference | |
| 52 | // re-narrowing path uses this to decide whether a let-bound | |
| 53 | // symbol's type still needs updating after a body retry. | |
| 54 | contains_inferred: bool => arguments |> any(a => a.contains_inferred) | |
| 55 | ||
| 56 | contains_function_generic_argument: bool => | |
| 57 | arguments |> any(a => a.contains_function_generic_argument) | |
| 58 | ||
| 59 | short_description: string is | |
| 60 | if ITERABLE_SPELLING.applies_to(symbol, arguments.count) then | |
| 61 | return "{arguments[0].short_description}{{}}" | |
| 62 | fi | |
| 63 | ||
| 64 | let result = System.Text.StringBuilder() | |
| 65 | ||
| 66 | result | |
| 67 | .append(IoC.CONTAINER.instance.name_display.short_name_for(symbol)) | |
| 68 | .append('[') | |
| 69 | ||
| 70 | let seen_any mut = false | |
| 71 | ||
| 72 | for a in arguments do | |
| 73 | if seen_any then | |
| 74 | result.append(',') | |
| 75 | fi | |
| 76 | ||
| 77 | result.append(a.short_description) | |
| 78 | ||
| 79 | seen_any = true | |
| 80 | od | |
| 81 | ||
| 82 | result.append(']') | |
| 83 | ||
| 84 | return result.to_string() | |
| 85 | si | |
| 86 | ||
| 87 | is_function_with_any_implicit_argument_types: bool is | |
| 88 | if !is_function then | |
| 89 | return false | |
| 90 | fi | |
| 91 | ||
| 92 | // is_inferred (today only INFERRED_RETURN_TYPE) rather | |
| 93 | // than is_sentinel: an ERROR-typed argument is a real failure, | |
| 94 | // not a deferred-inference placeholder, and shouldn't | |
| 95 | // trigger overload-resolver's "needs second pass to infer | |
| 96 | // formal-derived arg types" path. | |
| 97 | // | |
| 98 | // `arguments` carries a trailing return-type slot only for a | |
| 99 | // non-void function type; an `is_action`-shaped one (void | |
| 100 | // return) stores parameter types alone, so excluding the | |
| 101 | // last entry unconditionally would skip its final - and for | |
| 102 | // arity-1, only - parameter as though it were a return slot | |
| 103 | // that isn't there. | |
| 104 | let param_count = arguments.count - (if is_action then 0 else 1 fi) | |
| 105 | ||
| 106 | for i in 0..param_count do | |
| 107 | if arguments[i].is_inferred then | |
| 108 | return true | |
| 109 | fi | |
| 110 | od | |
| 111 | ||
| 112 | // A function returning a function whose own parameter types are | |
| 113 | // still implicit - `k => (a, b) => ...` once `k` is typed - has | |
| 114 | // them too: the second pass pushes the formal's return spine | |
| 115 | // down into the returned literal just as it does its parameters. | |
| 116 | return !is_action /\ arguments.count > 0 /\ arguments[arguments.count - 1].is_function_with_any_implicit_argument_types | |
| 117 | si | |
| 118 | ||
| 119 | init( | |
| 120 | symbol: Symbols.GENERIC | |
| 121 | ) is | |
| 122 | super.init(symbol) | |
| 123 | si | |
| 124 | ||
| 125 | init( | |
| 126 | location: LOCATION, | |
| 127 | symbol: Symbols.Classy, | |
| 128 | arguments: Collections.List[Type] | |
| 129 | ) is | |
| 130 | super.init(Symbols.GENERIC(location, symbol, arguments)) | |
| 131 | si | |
| 132 | ||
| 133 | create( | |
| 134 | location: LOCATION, | |
| 135 | symbol: Symbols.Classy, | |
| 136 | arguments: Collections.List[Type] | |
| 137 | ) -> GENERIC => | |
| 138 | GENERIC(location, symbol, arguments) | |
| 139 | ||
| 140 | is_same_symbol(other: Type) -> bool is | |
| 141 | if !isa GENERIC(other) then | |
| 142 | return false | |
| 143 | fi | |
| 144 | ||
| 145 | let other_generic = other | |
| 146 | ||
| 147 | let generic_symbol = cast Symbols.GENERIC?(symbol)! | |
| 148 | let generic_other_symbol = cast Symbols.GENERIC?(other_generic.symbol)! | |
| 149 | ||
| 150 | return generic_symbol.symbol == generic_other_symbol.symbol | |
| 151 | si | |
| 152 | ||
| 153 | // The declared variance at this type-argument position: whether | |
| 154 | // this position may vary at all, and in which direction. This is | |
| 155 | // purely a property of the generic type's own shape — read from | |
| 156 | // reflected .NET metadata for imported types (Symbols.Classy. | |
| 157 | // argument_variances) or, for FUNCTION/ACTION/ARRAY, fixed by | |
| 158 | // the kind of type they are. It says nothing about whether a | |
| 159 | // *specific* instantiation may use that variance — see | |
| 160 | // effective_argument_variance for that. | |
| 161 | get_argument_type_variance(index: int) -> TypeVariance is | |
| 162 | let generic_symbol = cast Symbols.GENERIC?(symbol) | |
| 163 | if !generic_symbol? then | |
| 164 | return TypeVariance.INVARIANT | |
| 165 | fi | |
| 166 | return generic_symbol.symbol.get_argument_variance(index) | |
| 167 | si | |
| 168 | ||
| 169 | // The variance actually usable when converting from `other` to | |
| 170 | // self at this position. The CLR only allows a variant | |
| 171 | // conversion when the actual type argument at this position is | |
| 172 | // a reference type on both sides, so this downgrades a declared | |
| 173 | // covariant/contravariant position to invariant whenever either | |
| 174 | // side's actual argument is a value type. | |
| 175 | effective_argument_variance(other: GENERIC, index: int) -> TypeVariance is | |
| 176 | // One tuple converts to another element by element, repacked | |
| 177 | // where the value crosses into the other type, so each | |
| 178 | // position varies with its element, value type or not. | |
| 179 | if (is_value_tuple \/ isa TUPLE(self)) /\ (other.is_value_tuple \/ isa TUPLE(other)) then | |
| 180 | return TypeVariance.COVARIANT | |
| 181 | fi | |
| 182 | ||
| 183 | let declared = other.get_argument_type_variance(index) | |
| 184 | ||
| 185 | if declared == TypeVariance.INVARIANT then | |
| 186 | return TypeVariance.INVARIANT | |
| 187 | fi | |
| 188 | ||
| 189 | if index >= 0 /\ index < arguments.count /\ (arguments[index].is_value_type \/ other.arguments[index].is_value_type) then | |
| 190 | return TypeVariance.INVARIANT | |
| 191 | fi | |
| 192 | ||
| 193 | return declared | |
| 194 | si | |
| 195 | ||
| 196 | get_element_type() -> Type => arguments[0] | |
| 197 | ||
| 198 | specialize_generic(type_map: Collections.Map[Symbols.Symbol,Type]) -> Types.GENERIC is | |
| 199 | let context = IoC.CONTAINER.instance.symbol_table.current_instance_context | |
| 200 | ||
| 201 | let we_are_generic = context? /\ context.arguments.count > 0 | |
| 202 | ||
| 203 | let seen_any_new mut = false | |
| 204 | ||
| 205 | let generic_symbol = cast Symbols.GENERIC?(symbol)! | |
| 206 | ||
| 207 | let new_arguments = Collections.LIST[Type](arguments.count) | |
| 208 | ||
| 209 | for i in 0..arguments.count do | |
| 210 | let parameter | |
| 211 | = cast Semantic.Symbols.GenericArgument?( | |
| 212 | generic_symbol.symbol.type_parameter_at(i)) | |
| 213 | ||
| 214 | let mapped_type: Type mut | |
| 215 | ||
| 216 | if we_are_generic /\ parameter? /\ type_map.try_get_value(parameter, mapped_type ref) then | |
| 217 | new_arguments.add(mapped_type) | |
| 218 | ||
| 219 | seen_any_new = true | |
| 220 | else | |
| 221 | let oa = generic_symbol.arguments[i] | |
| 222 | let na = oa.specialize(type_map) | |
| 223 | ||
| 224 | new_arguments.add(na) | |
| 225 | ||
| 226 | if oa != na then | |
| 227 | seen_any_new = true | |
| 228 | fi | |
| 229 | fi | |
| 230 | od | |
| 231 | ||
| 232 | if seen_any_new then | |
| 233 | let result = create(symbol.location, generic_symbol.symbol, new_arguments) | |
| 234 | ||
| 235 | if !is_optional then | |
| 236 | return result | |
| 237 | fi | |
| 238 | ||
| 239 | if let optional: GENERIC = result.as_optional_unchecked() then | |
| 240 | return optional | |
| 241 | fi | |
| 242 | ||
| 243 | return result | |
| 244 | else | |
| 245 | return self | |
| 246 | fi | |
| 247 | si | |
| 248 | ||
| 249 | specialize(type_map: Collections.Map[Symbols.Symbol,Type]) -> Type => | |
| 250 | specialize_generic(type_map) | |
| 251 | ||
| 252 | matches(other: Type) -> bool is | |
| 253 | if other.is_sentinel then | |
| 254 | return true | |
| 255 | fi | |
| 256 | ||
| 257 | if !isa GENERIC(other) then | |
| 258 | return false | |
| 259 | fi | |
| 260 | ||
| 261 | let generic_other = other | |
| 262 | ||
| 263 | if symbol == generic_other.symbol then | |
| 264 | return true | |
| 265 | fi | |
| 266 | ||
| 267 | let generic_symbol = cast Symbols.GENERIC?(symbol)! | |
| 268 | let generic_other_symbol = cast Symbols.GENERIC?(generic_other.symbol)! | |
| 269 | ||
| 270 | if generic_symbol.symbol != generic_other_symbol.symbol then | |
| 271 | return false | |
| 272 | fi | |
| 273 | ||
| 274 | if generic_symbol.arguments.count != generic_other_symbol.arguments.count then | |
| 275 | return false | |
| 276 | fi | |
| 277 | ||
| 278 | for i in 0..generic_symbol.arguments.count do | |
| 279 | if !generic_symbol.arguments[i].matches(generic_other_symbol.arguments[i]) then | |
| 280 | return false | |
| 281 | fi | |
| 282 | od | |
| 283 | ||
| 284 | return true | |
| 285 | si | |
| 286 | ||
| 287 | is_equivalent_to(other: Type) -> bool is | |
| 288 | if other.is_sentinel then | |
| 289 | return true | |
| 290 | fi | |
| 291 | ||
| 292 | if !isa GENERIC(other) then | |
| 293 | return false | |
| 294 | fi | |
| 295 | ||
| 296 | let generic_other = other | |
| 297 | ||
| 298 | // Unlike `matches`, the outer optional flag participates: | |
| 299 | // LIST[cat]? is not equivalent to LIST[cat], whether or | |
| 300 | // not the two share a symbol instance. | |
| 301 | if is_optional != generic_other.is_optional then | |
| 302 | return false | |
| 303 | fi | |
| 304 | ||
| 305 | if symbol == generic_other.symbol then | |
| 306 | return true | |
| 307 | fi | |
| 308 | ||
| 309 | let generic_symbol = cast Symbols.GENERIC?(symbol)! | |
| 310 | let generic_other_symbol = cast Symbols.GENERIC?(generic_other.symbol)! | |
| 311 | ||
| 312 | if generic_symbol.symbol != generic_other_symbol.symbol then | |
| 313 | return false | |
| 314 | fi | |
| 315 | ||
| 316 | if generic_symbol.arguments.count != generic_other_symbol.arguments.count then | |
| 317 | return false | |
| 318 | fi | |
| 319 | ||
| 320 | for i in 0..generic_symbol.arguments.count do | |
| 321 | if !generic_symbol.arguments[i].is_equivalent_to(generic_other_symbol.arguments[i]) then | |
| 322 | return false | |
| 323 | fi | |
| 324 | od | |
| 325 | ||
| 326 | return true | |
| 327 | si | |
| 328 | ||
| 329 | compare_direct(other: Type) -> Types.MATCH is | |
| 330 | if other.is_sentinel then | |
| 331 | return Types.MATCH.SAME | |
| 332 | fi | |
| 333 | ||
| 334 | if !isa GENERIC(other) then | |
| 335 | return Types.MATCH.DIFFERENT | |
| 336 | fi | |
| 337 | ||
| 338 | let generic_other = other | |
| 339 | ||
| 340 | if symbol == generic_other.symbol then | |
| 341 | // Structurally identical; the outer optional flag | |
| 342 | // decides the direction. `List[T]?` accepts `List[T]` | |
| 343 | // (widening); `List[T]` rejects `List[T]?` (would | |
| 344 | // lose the discriminator). | |
| 345 | if is_optional == generic_other.is_optional then | |
| 346 | return Types.MATCH.SAME | |
| 347 | elif is_optional then | |
| 348 | return Types.MATCH.ASSIGNABLE | |
| 349 | else | |
| 350 | return Types.MATCH.DIFFERENT | |
| 351 | fi | |
| 352 | fi | |
| 353 | ||
| 354 | let generic_symbol = cast Symbols.GENERIC?(symbol)! | |
| 355 | let generic_other_symbol = cast Symbols.GENERIC?(generic_other.symbol)! | |
| 356 | ||
| 357 | if generic_symbol.symbol != generic_other_symbol.symbol then | |
| 358 | return Types.MATCH.DIFFERENT | |
| 359 | fi | |
| 360 | ||
| 361 | if generic_symbol.arguments.count != generic_other_symbol.arguments.count then | |
| 362 | return Types.MATCH.DIFFERENT | |
| 363 | fi | |
| 364 | ||
| 365 | // Same outer-flag rule as the shared-symbol fast path | |
| 366 | // above, for structurally-equal constructions that don't | |
| 367 | // share a symbol instance: a bare slot rejects an | |
| 368 | // optional value outright, and an optional slot accepting | |
| 369 | // a bare value is a widening, never an exact match. | |
| 370 | if !is_optional /\ generic_other.is_optional then | |
| 371 | return Types.MATCH.DIFFERENT | |
| 372 | fi | |
| 373 | ||
| 374 | let result mut = | |
| 375 | if is_optional == generic_other.is_optional then | |
| 376 | Types.MATCH.SAME | |
| 377 | else | |
| 378 | Types.MATCH.ASSIGNABLE | |
| 379 | fi | |
| 380 | ||
| 381 | for i in 0..generic_symbol.arguments.count do | |
| 382 | let variance = effective_argument_variance(generic_other, i) | |
| 383 | ||
| 384 | let argument_score: Types.MATCH mut | |
| 385 | ||
| 386 | if variance == TypeVariance.COVARIANT then | |
| 387 | argument_score = generic_symbol.arguments[i].compare(generic_other_symbol.arguments[i]) | |
| 388 | elif variance == TypeVariance.CONTRAVARIANT then | |
| 389 | argument_score = generic_other_symbol.arguments[i].compare(generic_symbol.arguments[i]) | |
| 390 | elif generic_symbol.arguments[i].is_equivalent_to(generic_other_symbol.arguments[i]) then | |
| 391 | argument_score = Types.MATCH.SAME | |
| 392 | else | |
| 393 | // `matches` would erase the optional flag here, and an | |
| 394 | // invariant position must not: reading from the | |
| 395 | // slot could surface a null its type denies, and | |
| 396 | // writing through it could store one. | |
| 397 | return Types.MATCH.DIFFERENT | |
| 398 | fi | |
| 399 | ||
| 400 | if cast int(argument_score) > cast int(result) then | |
| 401 | result = argument_score | |
| 402 | fi | |
| 403 | od | |
| 404 | ||
| 405 | return result | |
| 406 | si | |
| 407 | ||
| 408 | compare(other: Type) -> Types.MATCH is | |
| 409 | if isa ONE_OF(other) /\ !isa ONE_OF(self) then | |
| 410 | // ONE_OF assignability is "value of underlying union | |
| 411 | // restricted to a variant subset" — delegate to the | |
| 412 | // underlying type so the narrowing target's | |
| 413 | // is_assignable_from check sees the same identity it | |
| 414 | // would for the plain union. NAMED.compare already | |
| 415 | // does the right thing for ONE_OF via symbol-match. | |
| 416 | let one_of = other | |
| 417 | return self.compare(one_of.underlying_type) | |
| 418 | fi | |
| 419 | ||
| 420 | // An already-broken type, and one still being inferred, | |
| 421 | // both answer `is_null` as well - so this has to come | |
| 422 | // ahead of the null test, or a construction rejects them | |
| 423 | // and piles a second diagnostic onto the first. | |
| 424 | if other.is_sentinel then | |
| 425 | return Types.MATCH.ASSIGNABLE | |
| 426 | fi | |
| 427 | ||
| 428 | if other.is_null /\ accepts_null then | |
| 429 | return Types.MATCH.ASSIGNABLE | |
| 430 | elif other.is_named then | |
| 431 | // Strict non-nullable-by-default: a bare slot never | |
| 432 | // accepts a `T?` value, at every construction shape. | |
| 433 | // compare_direct already enforces this for the shared | |
| 434 | // symbol, but the ancestor walk below recurses on | |
| 435 | // `other.symbol.ancestors`, which are bare (see | |
| 436 | // `Symbol.ancestors`) — so without this guard an | |
| 437 | // optional generic silently widens to a non-optional | |
| 438 | // supertype and its null discriminator is lost. | |
| 439 | // | |
| 440 | // A construction's own optional flag is fixed even when | |
| 441 | // its arguments are still unbound, so unlike | |
| 442 | // NAMED.compare this does not stand aside for wild | |
| 443 | // placeholders: `Iterable[T]` is non-optional whatever | |
| 444 | // `T` turns out to be. | |
| 445 | if !is_optional /\ other.is_optional then | |
| 446 | return Types.MATCH.DIFFERENT | |
| 447 | fi | |
| 448 | ||
| 449 | let direct_score = compare_direct(other) | |
| 450 | ||
| 451 | if cast int(direct_score) <= cast int(Types.MATCH.ASSIGNABLE) then | |
| 452 | return direct_score | |
| 453 | fi | |
| 454 | ||
| 455 | for i in 0..other.symbol.ancestors.count do | |
| 456 | let a = other.symbol.get_ancestor(i) | |
| 457 | ||
| 458 | let match = self.compare(a) | |
| 459 | ||
| 460 | if cast int(match) <= cast int(Types.MATCH.ASSIGNABLE) then | |
| 461 | return Types.MATCH.ASSIGNABLE | |
| 462 | elif match == Types.MATCH.CONVERTABLE then | |
| 463 | return Types.MATCH.CONVERTABLE | |
| 464 | fi | |
| 465 | od | |
| 466 | fi | |
| 467 | ||
| 468 | if is_wild \/ other.is_wild then | |
| 469 | return Types.MATCH.WILD | |
| 470 | fi | |
| 471 | ||
| 472 | return Types.MATCH.DIFFERENT | |
| 473 | si | |
| 474 | ||
| 475 | // attempt to bind type variables in this generic against concrete types in | |
| 476 | // other by pattern matching | |
| 477 | bind_type_variables(other: Type, results: GENERIC_ARGUMENT_BIND_RESULTS) -> bool is | |
| 478 | if other.is_null then | |
| 479 | return true | |
| 480 | fi | |
| 481 | ||
| 482 | if let other_generic: GENERIC = other then | |
| 483 | let generic_symbol = cast Symbols.GENERIC?(symbol)! | |
| 484 | let generic_other_symbol = cast Symbols.GENERIC?(other_generic.symbol)! | |
| 485 | ||
| 486 | if generic_symbol.symbol == generic_other_symbol.symbol then | |
| 487 | let result mut = true | |
| 488 | ||
| 489 | for i in 0..arguments.count do | |
| 490 | // A nested type argument is taken literally by the | |
| 491 | // CLR: `Func[MAYBE[U]]` is not `Func[string?]` for | |
| 492 | // any U, so the slot-boundary coercion between the | |
| 493 | // optional carriers cannot apply here. A MAYBE | |
| 494 | // formal at this position binds only against a | |
| 495 | // MAYBE actual, or one still being inferred. | |
| 496 | if let maybe_formal: GENERIC = arguments[i] /\ maybe_formal.is_maybe /\ !_can_bind_maybe_argument(maybe_formal, other.arguments[i]) then | |
| 497 | return false | |
| 498 | fi | |
| 499 | ||
| 500 | result = arguments[i].bind_type_variables(other.arguments[i], results) /\ result | |
| 501 | od | |
| 502 | ||
| 503 | return result | |
| 504 | else | |
| 505 | for i in 0..other_generic.ancestors.count do | |
| 506 | let a = other_generic.symbol.get_ancestor(i) | |
| 507 | ||
| 508 | if bind_type_variables(a, results) then | |
| 509 | return true | |
| 510 | fi | |
| 511 | od | |
| 512 | ||
| 513 | return false | |
| 514 | fi | |
| 515 | else | |
| 516 | for a in other.ancestors do | |
| 517 | if bind_type_variables(a, results) then | |
| 518 | return true | |
| 519 | fi | |
| 520 | od | |
| 521 | ||
| 522 | return false | |
| 523 | fi | |
| 524 | si | |
| 525 | ||
| 526 | // A `MAYBE[T]` written out by name is a plain construction over the | |
| 527 | // same symbol - the same CLR type, however it was reached. | |
| 528 | _can_bind_maybe_argument(formal: GENERIC, actual: Type) -> bool static => | |
| 529 | actual.is_maybe \/ actual.is_null \/ actual.is_sentinel \/ formal.is_construction_of_same_symbol_as(actual) | |
| 530 | ||
| 531 | // Whether `other` is constructed over this construction's own | |
| 532 | // generic symbol, whatever its arguments and whichever | |
| 533 | // subclass either side is. | |
| 534 | is_construction_of_same_symbol_as(other: Type) -> bool is | |
| 535 | if let other_generic: GENERIC = other then | |
| 536 | let own_symbol = cast Symbols.GENERIC?(symbol) | |
| 537 | let other_symbol = cast Symbols.GENERIC?(other_generic.symbol) | |
| 538 | ||
| 539 | return own_symbol? /\ other_symbol? /\ own_symbol.symbol == other_symbol.symbol | |
| 540 | fi | |
| 541 | ||
| 542 | return false | |
| 543 | si | |
| 544 | ||
| 545 | ||
| 546 | get_type_arguments_into(results: Collections.LIST[GenericArgument]) is | |
| 547 | for a in arguments do | |
| 548 | a.get_type_arguments_into(results) | |
| 549 | od | |
| 550 | si | |
| 551 | ||
| 552 | walk(action: (Type) -> void) is | |
| 553 | for a in arguments do | |
| 554 | a.walk(action) | |
| 555 | od | |
| 556 | ||
| 557 | super.walk(action) | |
| 558 | si | |
| 559 | ||
| 560 | to_string() -> string => | |
| 561 | if is_optional then | |
| 562 | "{symbol.to_string()}?" | |
| 563 | else | |
| 564 | symbol.to_string() | |
| 565 | fi | |
| 566 | si | |
| 567 | si |