Appearance
| 1 | namespace IR.Program is | |
| 2 | use Collections.LIST | |
| 3 | use Collections.MAP | |
| 4 | ||
| 5 | use Semantic.Types.Type | |
| 6 | use Semantic.Symbols.Symbol | |
| 7 | use Semantic.Symbols.Classy | |
| 8 | use Semantic.Symbols.Function | |
| 9 | use Semantic.Symbols.GENERIC | |
| 10 | ||
| 11 | /// Applying and comparing the type maps instantiations carry. | |
| 12 | class TYPE_MAPS is | |
| 13 | /// `type` with every type parameter `map` names replaced, or `type` | |
| 14 | /// itself where the map is empty. | |
| 15 | specialize(type: Type?, map: Collections.Map[Symbol, Type]) -> Type? static => | |
| 16 | if !type? \/ map.count == 0 then type else type.specialize(_with_copies(type, map)) fi | |
| 17 | ||
| 18 | // A type parameter can be named by its declared symbol or by a | |
| 19 | // frozen copy of it, which a closure's frame holds, and the map is | |
| 20 | // keyed by the declared one. Each parameter in `type` the map does | |
| 21 | // not name directly takes the argument of the parameter it is a | |
| 22 | // copy of. | |
| 23 | _with_copies(type: Type, map: Collections.Map[Symbol, Type]) -> Collections.Map[Symbol, Type] static is | |
| 24 | let unmapped = LIST[Semantic.Symbols.GenericArgument]() | |
| 25 | ||
| 26 | _collect_unmapped(type, map, unmapped, 0) | |
| 27 | ||
| 28 | if unmapped.count == 0 then | |
| 29 | return map | |
| 30 | fi | |
| 31 | ||
| 32 | let result = MAP[Symbol, Type]() | |
| 33 | ||
| 34 | for (parameter, argument) in map do | |
| 35 | result[parameter] = argument | |
| 36 | od | |
| 37 | ||
| 38 | for copy in unmapped do | |
| 39 | let key = Semantic.Symbols.TYPE_PARAMETER_KEY.of(copy) | |
| 40 | ||
| 41 | for (parameter, argument) in map do | |
| 42 | if let declared: Semantic.Symbols.GenericArgument = parameter /\ Semantic.Symbols.TYPE_PARAMETER_KEY.of(declared) =~ key then | |
| 43 | result[copy] = argument | |
| 44 | fi | |
| 45 | od | |
| 46 | od | |
| 47 | ||
| 48 | return result | |
| 49 | si | |
| 50 | ||
| 51 | /// The argument `map` gives `parameter`, or the parameter it is a | |
| 52 | /// copy of, or null where it gives neither. | |
| 53 | argument_of(parameter: Symbol, map: Collections.Map[Symbol, Type]) -> Type? static is | |
| 54 | let argument: Type mut | |
| 55 | ||
| 56 | if map.try_get_value(parameter, argument ref) then | |
| 57 | return argument | |
| 58 | fi | |
| 59 | ||
| 60 | if let copy: Semantic.Symbols.GenericArgument = parameter then | |
| 61 | let key = Semantic.Symbols.TYPE_PARAMETER_KEY.of(copy) | |
| 62 | ||
| 63 | for (declared, value) in map do | |
| 64 | if let declared_argument: Semantic.Symbols.GenericArgument = declared /\ Semantic.Symbols.TYPE_PARAMETER_KEY.of(declared_argument) =~ key then | |
| 65 | return value | |
| 66 | fi | |
| 67 | od | |
| 68 | fi | |
| 69 | ||
| 70 | return null | |
| 71 | si | |
| 72 | ||
| 73 | _collect_unmapped(type: Type, map: Collections.Map[Symbol, Type], into: LIST[Semantic.Symbols.GenericArgument], depth: int) static is | |
| 74 | if depth > 16 then | |
| 75 | return | |
| 76 | fi | |
| 77 | ||
| 78 | if let parameter: Semantic.Symbols.GenericArgument = type.symbol then | |
| 79 | if !map.contains_key(parameter) /\ !into.contains(parameter) then | |
| 80 | into.add(parameter) | |
| 81 | fi | |
| 82 | fi | |
| 83 | ||
| 84 | for argument in type.arguments do | |
| 85 | _collect_unmapped(argument, map, into, depth + 1) | |
| 86 | od | |
| 87 | si | |
| 88 | ||
| 89 | /// Whether two type maps bind the same parameters to the same types. | |
| 90 | /// A type is the same as another only where each matches the other: | |
| 91 | /// the null type matches every optional type, so one direction alone | |
| 92 | /// would make two instantiations one. | |
| 93 | same(left: Collections.Map[Symbol, Type], right: Collections.Map[Symbol, Type]) -> bool static pure is | |
| 94 | if left.count != right.count then | |
| 95 | return false | |
| 96 | fi | |
| 97 | ||
| 98 | for (parameter, argument) in left do | |
| 99 | let other: Type mut | |
| 100 | ||
| 101 | if !right.try_get_value(parameter, other ref) \/ !argument.matches(other) \/ !other.matches(argument) then | |
| 102 | return false | |
| 103 | fi | |
| 104 | od | |
| 105 | ||
| 106 | return true | |
| 107 | si | |
| 108 | ||
| 109 | /// A hash of `map` that agrees with `same`: maps that are the same | |
| 110 | /// hash alike. Each binding contributes its parameter and its | |
| 111 | /// argument, and the contributions are summed, so the order the map | |
| 112 | /// yields its entries in makes no difference. An argument hashes by | |
| 113 | /// its symbol, and a generic one's symbol hashes by its definition | |
| 114 | /// and arguments, which is what `matches` compares. A sentinel type | |
| 115 | /// matches anything and so cannot be hashed to agree; a type map an | |
| 116 | /// instance is compiled under holds none. | |
| 117 | hash(map: Collections.Map[Symbol, Type]) -> int static pure is | |
| 118 | let result mut = 0 | |
| 119 | ||
| 120 | for (parameter, argument) in map do | |
| 121 | result = result + parameter.get_hash_code() * 31 + argument.get_hash_code() | |
| 122 | od | |
| 123 | ||
| 124 | return result | |
| 125 | si | |
| 126 | ||
| 127 | /// How deeply `types` nest generic type arguments: zero for types | |
| 128 | /// that take none, and one more than the deepest argument for one | |
| 129 | /// that does. | |
| 130 | depth(types: Collections.Iterable[Type]) -> int static is | |
| 131 | let result mut = 0 | |
| 132 | ||
| 133 | for type in types do | |
| 134 | if let generic: Semantic.Types.GENERIC = type then | |
| 135 | let nested = 1 + depth(generic.arguments) | |
| 136 | ||
| 137 | if nested > result then | |
| 138 | result = nested | |
| 139 | fi | |
| 140 | fi | |
| 141 | od | |
| 142 | ||
| 143 | return result | |
| 144 | si | |
| 145 | ||
| 146 | /// `map` with every value specialized by `context`. | |
| 147 | compose(map: Collections.Map[Symbol, Type], context: Collections.Map[Symbol, Type]) -> MAP[Symbol, Type] static is | |
| 148 | let result = MAP[Symbol, Type]() | |
| 149 | ||
| 150 | for (parameter, argument) in map do | |
| 151 | result[parameter] = specialize(argument, context)! | |
| 152 | od | |
| 153 | ||
| 154 | return result | |
| 155 | si | |
| 156 | ||
| 157 | /// The type arguments a reference to `function` binds, read in the | |
| 158 | /// context `context` of the body making it: the class's from the | |
| 159 | /// function's owner, and the function's own from its generic | |
| 160 | /// arguments. | |
| 161 | function_map(function: Function, context: Collections.Map[Symbol, Type]) -> MAP[Symbol, Type] static is | |
| 162 | let definition = cast Function?(function.root_specialized_from)! | |
| 163 | let result = MAP[Symbol, Type]() | |
| 164 | ||
| 165 | if let owner: GENERIC = function.owner then | |
| 166 | for (parameter, argument) in owner.type_map do | |
| 167 | result[parameter] = specialize(argument, context)! | |
| 168 | od | |
| 169 | elif let owner: Classy = definition.owner /\ owner.is_generic then | |
| 170 | // A call from inside a generic class to its own members | |
| 171 | // names the definition, and the class's arguments are the | |
| 172 | // ones the calling body was compiled with. | |
| 173 | for (parameter, argument) in context do | |
| 174 | if parameter.owner == owner then | |
| 175 | result[parameter] = argument | |
| 176 | fi | |
| 177 | od | |
| 178 | elif isa Semantic.Symbols.Closure(definition) then | |
| 179 | // A closure body can name every type parameter of the body | |
| 180 | // it was written in. | |
| 181 | for (parameter, argument) in context do | |
| 182 | result[parameter] = argument | |
| 183 | od | |
| 184 | fi | |
| 185 | ||
| 186 | let parameters = definition.generic_arguments | |
| 187 | let arguments = function.generic_arguments | |
| 188 | ||
| 189 | for i in 0..parameters.count do | |
| 190 | if i < arguments.count then | |
| 191 | result[parameters[i].symbol] = specialize(arguments[i], context)! | |
| 192 | fi | |
| 193 | od | |
| 194 | ||
| 195 | return result | |
| 196 | si | |
| 197 | ||
| 198 | /// The type map a body of `function` is lowered under: the instance's | |
| 199 | /// own, and, for a member of a generator's or asynchronous function's | |
| 200 | /// frame, the type parameters of the function the frame was made from, | |
| 201 | /// which its body still names, mapped through the frame's copies of | |
| 202 | /// them. | |
| 203 | /// | |
| 204 | /// - function: the instance whose body is lowered | |
| 205 | body_map(function: FUNCTION_INSTANCE) -> Collections.Map[Symbol, Type] static is | |
| 206 | let frame = cast Semantic.Symbols.STATE_MACHINE_FRAME_BASE?(function.definition.owner) | |
| 207 | ||
| 208 | if !frame? \/ frame.function_to_class_type_map.count == 0 then | |
| 209 | return function.type_map | |
| 210 | fi | |
| 211 | ||
| 212 | let result = MAP[Symbol, Type]() | |
| 213 | ||
| 214 | for (parameter, argument) in function.type_map do | |
| 215 | result[parameter] = argument | |
| 216 | od | |
| 217 | ||
| 218 | for (parameter, mirrored) in frame.function_to_class_type_map do | |
| 219 | if let argument = specialize(mirrored, function.type_map) then | |
| 220 | result[parameter] = argument | |
| 221 | fi | |
| 222 | od | |
| 223 | ||
| 224 | return result | |
| 225 | si | |
| 226 | si | |
| 227 | si |