Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace Semantic.DotNet is | |
| 4 | use IO.Std | |
| 5 | ||
| 6 | use TYPE = System.Type | |
| 7 | use System.Reflection | |
| 8 | ||
| 9 | use Collections.LIST | |
| 10 | use Collections.MAP | |
| 11 | ||
| 12 | use Logging | |
| 13 | ||
| 14 | use Types.Type | |
| 15 | use Types.NAMED | |
| 16 | use Types.ERROR | |
| 17 | ||
| 18 | class TypeCreator(_mapper: TYPE_MAPPER) is | |
| 19 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type | |
| 20 | si | |
| 21 | ||
| 22 | class GENERIC_TYPE_CREATOR(mapper: TYPE_MAPPER): TypeCreator is | |
| 23 | super(mapper) | |
| 24 | ||
| 25 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type is | |
| 26 | let result: Type mut | |
| 27 | ||
| 28 | if type.is_generic_type_definition then | |
| 29 | result = GENERIC_TYPE_WRAPPER(symbol_table, _mapper, type) | |
| 30 | ||
| 31 | return result | |
| 32 | elif type.is_generic_type then | |
| 33 | result = GENERIC_TYPE_WRAPPER(symbol_table, _mapper, type) | |
| 34 | ||
| 35 | return result | |
| 36 | fi | |
| 37 | ||
| 38 | throw System.InvalidOperationException("don't know how to create type: {type}") | |
| 39 | si | |
| 40 | si | |
| 41 | ||
| 42 | class ACTION_0_TYPE_CREATOR: TypeCreator is | |
| 43 | init(mapper: TYPE_MAPPER) is | |
| 44 | super.init(mapper) | |
| 45 | si | |
| 46 | ||
| 47 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type => | |
| 48 | ACTION_0_TYPE_WRAPPER(symbol_table, type) | |
| 49 | si | |
| 50 | ||
| 51 | class ACTION_TYPE_CREATOR: TypeCreator is | |
| 52 | init(mapper: TYPE_MAPPER) is | |
| 53 | super.init(mapper) | |
| 54 | si | |
| 55 | ||
| 56 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type => | |
| 57 | ACTION_TYPE_WRAPPER(symbol_table, _mapper, type) | |
| 58 | si | |
| 59 | ||
| 60 | class FUNCTION_TYPE_CREATOR: TypeCreator is | |
| 61 | init(mapper: TYPE_MAPPER) is | |
| 62 | super.init(mapper) | |
| 63 | si | |
| 64 | ||
| 65 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type => | |
| 66 | FUNCTION_TYPE_WRAPPER(symbol_table, _mapper, type) | |
| 67 | si | |
| 68 | ||
| 69 | class TUPLE_TYPE_CREATOR: TypeCreator is | |
| 70 | init(mapper: TYPE_MAPPER) is | |
| 71 | super.init(mapper) | |
| 72 | si | |
| 73 | ||
| 74 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type => | |
| 75 | TUPLE_TYPE_WRAPPER(symbol_table, _mapper, type, null) | |
| 76 | si | |
| 77 | ||
| 78 | class NULLABLE_TYPE_CREATOR: TypeCreator is | |
| 79 | init(mapper: TYPE_MAPPER) is | |
| 80 | super.init(mapper) | |
| 81 | si | |
| 82 | ||
| 83 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type => | |
| 84 | NULLABLE_TYPE_WRAPPER(symbol_table, _mapper, type) | |
| 85 | si | |
| 86 | ||
| 87 | class MAYBE_TYPE_CREATOR: TypeCreator is | |
| 88 | init(mapper: TYPE_MAPPER) is | |
| 89 | super.init(mapper) | |
| 90 | si | |
| 91 | ||
| 92 | create(symbol_table: SYMBOL_TABLE, type: TYPE) -> Type => | |
| 93 | MAYBE_TYPE_WRAPPER(symbol_table, _mapper, type) | |
| 94 | si | |
| 95 | ||
| 96 | class TYPE_MAPPER is | |
| 97 | _symbol_table: System.Lazy[SYMBOL_TABLE] | |
| 98 | _innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 99 | _type_name_map: TYPE_NAME_MAP | |
| 100 | ||
| 101 | _generic_type_creator: TypeCreator | |
| 102 | _type_creators: MAP[TYPE,TypeCreator] | |
| 103 | ||
| 104 | _type_source: TypeSource | |
| 105 | ||
| 106 | // we don't need to intern types for correctness, but expecting to encounter a lot of references | |
| 107 | // to identical types, so interning them will result in lower memory usage and fewer calls into the | |
| 108 | // symbol table/symbol cache to materialize the associated ghul symbols | |
| 109 | _type_cache: MAP[TYPE,Type] | |
| 110 | ||
| 111 | init( | |
| 112 | symbol_table: System.Lazy[SYMBOL_TABLE], | |
| 113 | innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup, | |
| 114 | type_name_map: TYPE_NAME_MAP, | |
| 115 | type_source: TypeSource | |
| 116 | ) is | |
| 117 | _symbol_table = symbol_table | |
| 118 | _innate_symbol_lookup = innate_symbol_lookup | |
| 119 | _type_name_map = type_name_map | |
| 120 | ||
| 121 | _type_source = type_source | |
| 122 | ||
| 123 | _type_cache = MAP() | |
| 124 | ||
| 125 | _generic_type_creator = GENERIC_TYPE_CREATOR(self) | |
| 126 | ||
| 127 | _type_creators = MAP() | |
| 128 | ||
| 129 | _type_source.on_start(() -> void is start(); si) | |
| 130 | si | |
| 131 | ||
| 132 | start() is | |
| 133 | let action_0_type_creator = ACTION_0_TYPE_CREATOR(self) | |
| 134 | let action_type_creator = ACTION_TYPE_CREATOR(self) | |
| 135 | let function_type_creator = FUNCTION_TYPE_CREATOR(self) | |
| 136 | let tuple_type_creator = TUPLE_TYPE_CREATOR(self) | |
| 137 | ||
| 138 | _type_creators.add(_type_source.get_type("System.Nullable`1"), NULLABLE_TYPE_CREATOR(self)) | |
| 139 | ||
| 140 | // Registration is best-effort — when the compiler is | |
| 141 | // building ghul-runtime itself, the `ghul-runtime` | |
| 142 | // assembly is not in the loaded-assembly map, so the | |
| 143 | // lookup throws. In that case there are no reflected | |
| 144 | // MAYBE instances anyway, so the creator is never | |
| 145 | // consulted; leaving it unregistered is correct. | |
| 146 | try | |
| 147 | _type_creators.add(_type_source.get_type("ghul-runtime", "Ghul.MAYBE"), MAYBE_TYPE_CREATOR(self)) | |
| 148 | catch ex: System.Exception | |
| 149 | yrt | |
| 150 | ||
| 151 | _type_creators.add(_type_source.get_type("System.Action"), action_0_type_creator) | |
| 152 | ||
| 153 | for i in 1::16 do | |
| 154 | _type_creators.add(_type_source.get_type("System.Action`{i}"), action_type_creator) | |
| 155 | od | |
| 156 | ||
| 157 | for i in 1::17 do | |
| 158 | _type_creators.add(_type_source.get_type("System.Func`{i}"), function_type_creator) | |
| 159 | od | |
| 160 | ||
| 161 | // ghūl tuples are System.ValueTuple — the value type ghūl | |
| 162 | // emits and whose layout its tuple IL assumes. A reflected | |
| 163 | // System.Tuple (the legacy reference-type tuple) is left to | |
| 164 | // map as an ordinary generic class; treating it as a ghūl | |
| 165 | // tuple would emit value-tuple IL against a reference type. | |
| 166 | for i in 1::7 do | |
| 167 | _type_creators.add(_type_source.get_type("System.ValueTuple`{i}"), tuple_type_creator) | |
| 168 | od | |
| 169 | si | |
| 170 | ||
| 171 | get_type(type: TYPE?) -> Type is | |
| 172 | let result: Type mut | |
| 173 | let unsafe_constraints mut = false | |
| 174 | ||
| 175 | if !type? then | |
| 176 | Std.error.write_line("warning: materializing null type") | |
| 177 | return Types.NONE.instance | |
| 178 | fi | |
| 179 | ||
| 180 | if _type_cache.try_get_value(type, result ref) then | |
| 181 | return result | |
| 182 | fi | |
| 183 | ||
| 184 | if type.is_generic_method_parameter then | |
| 185 | // A reflected type parameter has no owning scope in the | |
| 186 | // symbol table: an empty block scope qualifies it to its | |
| 187 | // bare name, which is what it renders as either way. | |
| 188 | let symbol = Symbols.FUNCTION_GENERIC_ARGUMENT(Source.LOCATION.internal, Semantic.BLOCK_SCOPE(), type.name, type.generic_parameter_position) | |
| 189 | ||
| 190 | symbol.set_constraint_kind(generic_parameter_constraint_kind(type)) | |
| 191 | ||
| 192 | result = symbol.type! | |
| 193 | _type_cache.add(type, result) | |
| 194 | ||
| 195 | return result | |
| 196 | elif type.is_generic_type_parameter then | |
| 197 | // Prefer the declaring type's own cached parameter: an | |
| 198 | // imported member's formal and a substitution map built | |
| 199 | // from the owner must carry the same symbol, and type | |
| 200 | // substitution matches on symbol identity rather than | |
| 201 | // on the parameter's name. The fallback mints a | |
| 202 | // detached parameter when the declaring type is not | |
| 203 | // (yet) in the symbol table. | |
| 204 | if type.declaring_type? then | |
| 205 | let declaring = type.declaring_type! | |
| 206 | ||
| 207 | let owner_scoped = _symbol_table.value.get_symbol(declaring) | |
| 208 | ||
| 209 | if let owner_classy = cast Symbols.Classy?(owner_scoped) then | |
| 210 | let declared | |
| 211 | = cast Symbols.GenericArgument?( | |
| 212 | owner_classy.type_parameter_at(type.generic_parameter_position)) | |
| 213 | ||
| 214 | ||
| 215 | if declared? then | |
| 216 | declared.set_constraint_kind(generic_parameter_constraint_kind(type)) | |
| 217 | ||
| 218 | result = declared.type! | |
| 219 | _type_cache.add(type, result) | |
| 220 | ||
| 221 | return result | |
| 222 | fi | |
| 223 | fi | |
| 224 | fi | |
| 225 | ||
| 226 | let symbol = Symbols.CLASSY_GENERIC_ARGUMENT(Source.LOCATION.internal, Semantic.BLOCK_SCOPE(), type.name, type.generic_parameter_position) | |
| 227 | ||
| 228 | symbol.set_constraint_kind(generic_parameter_constraint_kind(type)) | |
| 229 | ||
| 230 | result = symbol.type! | |
| 231 | _type_cache.add(type, result) | |
| 232 | ||
| 233 | return result | |
| 234 | fi | |
| 235 | ||
| 236 | if type.is_generic_type_definition \/ type.is_generic_type then | |
| 237 | let b = type.get_generic_type_definition() | |
| 238 | ||
| 239 | let creator: TypeCreator mut | |
| 240 | ||
| 241 | if !_type_creators.try_get_value(b, creator ref) then | |
| 242 | creator = _generic_type_creator | |
| 243 | fi | |
| 244 | ||
| 245 | result = creator.create(_symbol_table.value, type) | |
| 246 | ||
| 247 | _type_cache.add(type, result) | |
| 248 | ||
| 249 | return result | |
| 250 | fi | |
| 251 | ||
| 252 | if type.is_array then | |
| 253 | let element_type = get_element_type(type) | |
| 254 | ||
| 255 | return _innate_symbol_lookup.get_array_type(element_type) | |
| 256 | elif type.is_by_ref then | |
| 257 | let element_type = get_element_type(type) | |
| 258 | ||
| 259 | return _innate_symbol_lookup.get_reference_type(element_type) | |
| 260 | elif type.is_pointer then | |
| 261 | let element_type = get_element_type(type) | |
| 262 | ||
| 263 | return _innate_symbol_lookup.get_pointer_type(element_type) | |
| 264 | elif type.is_by_ref_like then | |
| 265 | unsafe_constraints = true | |
| 266 | fi | |
| 267 | ||
| 268 | let creator: TypeCreator mut | |
| 269 | ||
| 270 | if _type_creators.try_get_value(type, creator ref) then | |
| 271 | return creator.create(_symbol_table.value, type) | |
| 272 | fi | |
| 273 | ||
| 274 | if !type.full_name? then | |
| 275 | Std.error.write_line("TM get type: type has no full name: {type}") | |
| 276 | return Types.NONE.instance | |
| 277 | fi | |
| 278 | ||
| 279 | result = TYPE_WRAPPER(_symbol_table.value, type) | |
| 280 | ||
| 281 | _type_cache.add(type, result) | |
| 282 | ||
| 283 | if unsafe_constraints then | |
| 284 | // not exactly but at least we'll get some kind of warning | |
| 285 | result.symbol.is_unsafe_constraints = true | |
| 286 | fi | |
| 287 | ||
| 288 | return result | |
| 289 | si | |
| 290 | ||
| 291 | map_type_argument_variance(type: TYPE) -> Types.TypeVariance => | |
| 292 | if type.generic_parameter_attributes.has_flag(System.Reflection.GenericParameterAttributes.COVARIANT) then | |
| 293 | Types.TypeVariance.COVARIANT | |
| 294 | elif type.generic_parameter_attributes.has_flag(System.Reflection.GenericParameterAttributes.CONTRAVARIANT) then | |
| 295 | Types.TypeVariance.CONTRAVARIANT | |
| 296 | else | |
| 297 | Types.TypeVariance.INVARIANT | |
| 298 | fi | |
| 299 | ||
| 300 | // Maps a .NET generic parameter's `class` / `struct` constraint | |
| 301 | // attributes to a ghūl kind constraint. | |
| 302 | generic_parameter_constraint_kind(type: TYPE) -> Symbols.TypeParameterConstraintKind is | |
| 303 | let attributes = type.generic_parameter_attributes | |
| 304 | ||
| 305 | if attributes.has_flag(System.Reflection.GenericParameterAttributes.REFERENCE_TYPE_CONSTRAINT) then | |
| 306 | return Symbols.TypeParameterConstraintKind.REFERENCE | |
| 307 | elif attributes.has_flag(System.Reflection.GenericParameterAttributes.NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) then | |
| 308 | return Symbols.TypeParameterConstraintKind.VALUE | |
| 309 | fi | |
| 310 | ||
| 311 | return Symbols.TypeParameterConstraintKind.NONE | |
| 312 | si | |
| 313 | ||
| 314 | // True when a .NET generic parameter declares a parameterless- | |
| 315 | // constructor (`new()`) constraint. | |
| 316 | generic_parameter_has_constructor_constraint(type: TYPE) -> bool => | |
| 317 | type.generic_parameter_attributes.has_flag( | |
| 318 | System.Reflection.GenericParameterAttributes.DEFAULT_CONSTRUCTOR_CONSTRAINT | |
| 319 | ) | |
| 320 | ||
| 321 | get_element_type(type: TYPE) -> Type => | |
| 322 | let element_type = type.get_element_type() in | |
| 323 | if !element_type? then | |
| 324 | Std.error.write_line("warning: structured .NET type {type} has null element type: treating as Types.NONE") | |
| 325 | Types.NONE.instance | |
| 326 | else | |
| 327 | get_type(element_type) | |
| 328 | fi | |
| 329 | si | |
| 330 | si |