Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace Semantic.DotNet is | |
| 4 | use TYPE = System.Type | |
| 5 | ||
| 6 | use System.Reflection.Assembly | |
| 7 | use System.Reflection.ConstructorInfo | |
| 8 | use System.Reflection.MethodInfo | |
| 9 | use System.Reflection.FieldInfo | |
| 10 | use System.Reflection.PropertyInfo | |
| 11 | use System.Reflection.MemberInfo | |
| 12 | use System.Reflection.ParameterInfo | |
| 13 | ||
| 14 | use System.Reflection.BindingFlags | |
| 15 | use System.Reflection.MemberTypes | |
| 16 | ||
| 17 | use System.Reflection.GenericParameterAttributes | |
| 18 | ||
| 19 | use Collections.List | |
| 20 | use Collections.MutableList | |
| 21 | use Collections.LIST | |
| 22 | use Collections.SET | |
| 23 | use Collections.MAP | |
| 24 | ||
| 25 | use Ghul.Pipes | |
| 26 | ||
| 27 | use Types.Type | |
| 28 | ||
| 29 | use Logging | |
| 30 | ||
| 31 | // The reflected types these hold cannot be looked up until the | |
| 32 | // type source has started, so start does it and the constructor | |
| 33 | // only registers the callback. | |
| 34 | @suppress("field-definite-assignment") | |
| 35 | class SYMBOL_FACTORY( | |
| 36 | _namespaces: NAMESPACES, | |
| 37 | _type_name_map: TYPE_NAME_MAP, | |
| 38 | _type_mapper: TYPE_MAPPER, | |
| 39 | _type_source: TypeSource, | |
| 40 | _type_details_lookup: TYPE_DETAILS_LOOKUP, | |
| 41 | logger: Logging.Logger private | |
| 42 | ) is | |
| 43 | _assembly_names: MAP[Assembly,string] | |
| 44 | ||
| 45 | _symbol_table: SYMBOL_TABLE | |
| 46 | ||
| 47 | _void_type: TYPE | |
| 48 | _object_type: TYPE | |
| 49 | _value_type: TYPE | |
| 50 | _array_type: TYPE | |
| 51 | _multicast_delegate_type: TYPE | |
| 52 | _bool_type: TYPE | |
| 53 | _int_type: TYPE | |
| 54 | _equatable_open_type: TYPE | |
| 55 | _comparable_open_type: TYPE | |
| 56 | _tuple_types: List[TYPE] | |
| 57 | ||
| 58 | // The types whose `<>` / `=~` is an intrinsic lowering to | |
| 59 | // opcodes (`compare.order` and friends in the runtime's | |
| 60 | // intrinsics). Their reflected interface members are hidden | |
| 61 | // from binary-operator resolution so `a < b` keeps the opcode | |
| 62 | // and its semantics — on `single`, `CompareTo` is a total | |
| 63 | // order that sorts NaN below everything, where the opcode is | |
| 64 | // the IEEE comparison. All are sealed, so the hidden member | |
| 65 | // can have no sibling overloads to shadow. | |
| 66 | // | |
| 67 | // `string` has an innate `=~` but no innate order, so it is in | |
| 68 | // the equality set only, and ordering one reaches | |
| 69 | // `System.String.CompareTo`. That comparison is culture- | |
| 70 | // sensitive: `"a" < "B"` answers true under a typical current | |
| 71 | // culture and false under invariant globalization, so a string | |
| 72 | // ordering is not machine-independent the way a scalar's is. | |
| 73 | _innate_order_type_names: SET[string] | |
| 74 | _innate_equality_type_names: SET[string] | |
| 75 | ||
| 76 | // The scalar types whose arithmetic, bitwise and shift operators | |
| 77 | // are the intrinsics. Held as the reflected types themselves, so | |
| 78 | // membership is identity rather than a rendered name. | |
| 79 | _intrinsic_operator_types: SET[TYPE] | |
| 80 | ||
| 81 | _ambiguous_method_checker: AMBIGUOUS_METHOD_CHECKER | |
| 82 | ||
| 83 | init(..) is | |
| 84 | _assembly_names = MAP[Assembly,string]() | |
| 85 | _ambiguous_method_checker = AMBIGUOUS_METHOD_CHECKER(logger) | |
| 86 | ||
| 87 | _type_source.on_start(() -> void is start(); si) | |
| 88 | si | |
| 89 | ||
| 90 | start() is | |
| 91 | _void_type = _type_source.get_type("System.Void") | |
| 92 | _object_type = _type_source.get_type("System.Object") | |
| 93 | _value_type = _type_source.get_type("System.ValueType") | |
| 94 | _array_type = _type_source.get_type("System.Array") | |
| 95 | _multicast_delegate_type = _type_source.get_type("System.MulticastDelegate") | |
| 96 | _bool_type = _type_source.get_type("System.Boolean") | |
| 97 | _int_type = _type_source.get_type("System.Int32") | |
| 98 | _equatable_open_type = _type_source.get_type("System.IEquatable`1") | |
| 99 | _comparable_open_type = _type_source.get_type("System.IComparable`1") | |
| 100 | ||
| 101 | _innate_order_type_names = SET([ | |
| 102 | "System.Char", | |
| 103 | "System.SByte", "System.Byte", | |
| 104 | "System.Int16", "System.UInt16", | |
| 105 | "System.Int32", "System.UInt32", | |
| 106 | "System.Int64", "System.UInt64", | |
| 107 | "System.IntPtr", "System.UIntPtr", | |
| 108 | "System.Single", "System.Double", | |
| 109 | "System.Decimal" | |
| 110 | ]) | |
| 111 | ||
| 112 | _innate_equality_type_names = SET([ | |
| 113 | "System.String" | |
| 114 | ]) | |
| 115 | ||
| 116 | _intrinsic_operator_types = SET(_type_source.get_types([ | |
| 117 | "System.Char", | |
| 118 | "System.SByte", "System.Byte", | |
| 119 | "System.Int16", "System.UInt16", | |
| 120 | "System.Int32", "System.UInt32", | |
| 121 | "System.Int64", "System.UInt64", | |
| 122 | "System.IntPtr", "System.UIntPtr", | |
| 123 | "System.Single", "System.Double", | |
| 124 | "System.Decimal" | |
| 125 | ])) | |
| 126 | ||
| 127 | _tuple_types = _type_source.get_types([ | |
| 128 | "System.ValueTuple`1", | |
| 129 | "System.ValueTuple`2", | |
| 130 | "System.ValueTuple`3", | |
| 131 | "System.ValueTuple`4", | |
| 132 | "System.ValueTuple`5", | |
| 133 | "System.ValueTuple`6", | |
| 134 | "System.ValueTuple`7" | |
| 135 | ]) | |
| 136 | ||
| 137 | // TODO handle more than 7-tuple types by | |
| 138 | // nesting them | |
| 139 | si | |
| 140 | ||
| 141 | set_symbol_table(symbol_table: SYMBOL_TABLE) is | |
| 142 | _symbol_table = symbol_table | |
| 143 | si | |
| 144 | ||
| 145 | // A reference set that cannot supply everything a type's members name | |
| 146 | // is a fact about the command line, so it is reported rather than | |
| 147 | // thrown: the caller keeps the partly-populated symbol, and the error | |
| 148 | // stops the build before generate-il can meet whatever the missing | |
| 149 | // members left behind. | |
| 150 | report_materialization_failure(dotnet_type: TYPE, ex: System.Exception) is | |
| 151 | _logger.environment_error( | |
| 152 | Source.LOCATION.reflected, | |
| 153 | "cannot load {dotnet_type}: {without_loader_advice(ex.message)}") | |
| 154 | si | |
| 155 | ||
| 156 | // The reflection layer's own remediation - load the assembly with | |
| 157 | // `LoadFromAssemblyPath`, or supply a resolver - addresses whoever | |
| 158 | // called it, which here is the compiler rather than the person reading | |
| 159 | // the diagnostic. The sentence before it names the assembly, which is | |
| 160 | // what they need. Dropping the advice is cosmetic on both sides: an | |
| 161 | // unrecognised message is reported whole. | |
| 162 | without_loader_advice(message: string) -> string is | |
| 163 | let advice = message.index_of("Either explicitly load") | |
| 164 | ||
| 165 | if advice < 0 then | |
| 166 | return message | |
| 167 | fi | |
| 168 | ||
| 169 | return message.substring(0, advice).trim_end() | |
| 170 | si | |
| 171 | ||
| 172 | create_symbol(type_details: TYPE_DETAILS) -> Symbols.Scoped? is | |
| 173 | try | |
| 174 | let dotnet_type = type_details.dotnet_type | |
| 175 | ||
| 176 | if dotnet_type.is_class \/ dotnet_type.is_value_type \/ dotnet_type.is_interface \/ dotnet_type.is_enum then | |
| 177 | return create_class(type_details) | |
| 178 | fi | |
| 179 | catch ex: System.Exception | |
| 180 | debug_always("create symbol failed: {type_details} exception: {ex}") | |
| 181 | ||
| 182 | throw ex | |
| 183 | yrt | |
| 184 | ||
| 185 | return null | |
| 186 | si | |
| 187 | ||
| 188 | create_class(type_details: TYPE_DETAILS) -> Symbols.Scoped is | |
| 189 | let dotnet_type = type_details.dotnet_type | |
| 190 | let ghul_type_name = type_details.ghul_type_name | |
| 191 | let assembly_name = type_details.assembly_name! | |
| 192 | let il_name mut = type_details.il_name | |
| 193 | ||
| 194 | let owner = EMPTY_SCOPE(type_details.ghul_namespace) | |
| 195 | ||
| 196 | let arguments = LIST() | |
| 197 | let argument_variances = LIST() | |
| 198 | let argument_constraint_kinds = LIST() | |
| 199 | let argument_has_constructor_constraint = LIST() | |
| 200 | let argument_type_bounds = Collections.LIST[Collections.List[Semantic.Types.Type]]() | |
| 201 | ||
| 202 | if dotnet_type.is_generic_type /\ dotnet_type.contains_generic_parameters then | |
| 203 | let argument_types = dotnet_type.get_generic_arguments() | |
| 204 | ||
| 205 | for a in argument_types do | |
| 206 | if a.is_generic_type_parameter then | |
| 207 | arguments.add(a.name) | |
| 208 | argument_variances.add(_type_mapper.map_type_argument_variance(a)) | |
| 209 | argument_constraint_kinds.add(_type_mapper.generic_parameter_constraint_kind(a)) | |
| 210 | argument_has_constructor_constraint.add(_type_mapper.generic_parameter_has_constructor_constraint(a)) | |
| 211 | argument_type_bounds.add(type_bounds(a)) | |
| 212 | fi | |
| 213 | od | |
| 214 | fi | |
| 215 | ||
| 216 | let result: Symbols.Classy mut | |
| 217 | ||
| 218 | if dotnet_type.is_enum then | |
| 219 | let enum_struct = | |
| 220 | Symbols.ENUM_STRUCT(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, owner) | |
| 221 | ||
| 222 | enum_struct.mark_overrides_resolved() | |
| 223 | ||
| 224 | enum_struct.underlying_type = _type_mapper.get_type(dotnet_type.get_enum_underlying_type()) | |
| 225 | ||
| 226 | Symbols.ENUM_ORDER_OPERATOR.register(enum_struct) | |
| 227 | Symbols.ENUM_EQUALITY_OPERATOR.register(enum_struct) | |
| 228 | ||
| 229 | if type_details.has_flags_attribute then | |
| 230 | enum_struct.is_flags = true | |
| 231 | Symbols.ENUM_AND_OPERATOR.register(enum_struct) | |
| 232 | Symbols.ENUM_OR_OPERATOR.register(enum_struct) | |
| 233 | Symbols.ENUM_XOR_OPERATOR.register(enum_struct) | |
| 234 | Symbols.ENUM_NOT_OPERATOR.register(enum_struct) | |
| 235 | fi | |
| 236 | ||
| 237 | result = enum_struct | |
| 238 | elif dotnet_type.is_class /\ has_union_attribute(dotnet_type) then | |
| 239 | result = Symbols.UNION(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, arguments, owner) | |
| 240 | result.mark_overrides_resolved() | |
| 241 | elif dotnet_type.is_class /\ has_variant_attribute(dotnet_type) then | |
| 242 | result = Symbols.VARIANT(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, arguments, owner) | |
| 243 | result.mark_overrides_resolved() | |
| 244 | elif dotnet_type.is_class then | |
| 245 | let result_class = Symbols.CLASS(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, arguments, owner) | |
| 246 | ||
| 247 | if dotnet_type == _object_type then | |
| 248 | result_class.mark_is_object() | |
| 249 | elif dotnet_type == _value_type then | |
| 250 | result_class.mark_is_root_value_type() | |
| 251 | elif dotnet_type == _array_type then | |
| 252 | result_class.mark_is_root_array_type() | |
| 253 | fi | |
| 254 | ||
| 255 | if has_closed_attribute(dotnet_type) then | |
| 256 | result_class.mark_closed() | |
| 257 | fi | |
| 258 | ||
| 259 | if _inherits_multicast_delegate(dotnet_type) then | |
| 260 | result_class.mark_delegate() | |
| 261 | fi | |
| 262 | ||
| 263 | if dotnet_type.is_abstract then | |
| 264 | result_class.mark_abstract() | |
| 265 | fi | |
| 266 | ||
| 267 | result = result_class | |
| 268 | result.mark_overrides_resolved() | |
| 269 | elif dotnet_type.is_value_type then | |
| 270 | if dotnet_type == _void_type then | |
| 271 | result = Symbols.VOID_STRUCT(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, arguments, owner) | |
| 272 | else | |
| 273 | result = Symbols.STRUCT(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, arguments, owner) | |
| 274 | fi | |
| 275 | ||
| 276 | result.built_in_type = type_details.built_in_type | |
| 277 | ||
| 278 | result.mark_overrides_resolved() | |
| 279 | elif dotnet_type.is_interface then | |
| 280 | let result_trait = Symbols.TRAIT(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, ghul_type_name, arguments, owner) | |
| 281 | ||
| 282 | if has_closed_attribute(dotnet_type) then | |
| 283 | result_trait.mark_closed() | |
| 284 | fi | |
| 285 | ||
| 286 | result = result_trait | |
| 287 | else | |
| 288 | throw System.Exception("unhandled reflected kind for {dotnet_type}") | |
| 289 | fi | |
| 290 | ||
| 291 | // The attribute check alone misses a type imported from an | |
| 292 | // assembly built before this change - ghul-runtime itself | |
| 293 | // included. A type name is never operator syntax, so a | |
| 294 | // leading `$` on a reflected type is an unambiguous legacy | |
| 295 | // signal, the same reasoning add_field's fallback uses. | |
| 296 | if | |
| 297 | type_details.is_globals_carrier \/ | |
| 298 | _has_compiler_generated_attribute(dotnet_type.get_custom_attributes_data()) \/ | |
| 299 | ghul_type_name.starts_with('$') | |
| 300 | then | |
| 301 | result.mark_synthesized() | |
| 302 | fi | |
| 303 | ||
| 304 | result.set_doc(_doc_attribute(dotnet_type.get_custom_attributes_data())) | |
| 305 | result.set_deprecation(_obsolete_attribute(dotnet_type.get_custom_attributes_data())) | |
| 306 | ||
| 307 | if argument_variances.count > 0 then | |
| 308 | result.argument_variances = argument_variances | |
| 309 | fi | |
| 310 | ||
| 311 | if argument_constraint_kinds.count > 0 then | |
| 312 | result.argument_constraint_kinds = argument_constraint_kinds | |
| 313 | fi | |
| 314 | ||
| 315 | if argument_has_constructor_constraint.count > 0 then | |
| 316 | result.argument_has_constructor_constraint = argument_has_constructor_constraint | |
| 317 | fi | |
| 318 | ||
| 319 | if argument_type_bounds.count > 0 /\ argument_type_bounds |> any(l => l.count > 0) then | |
| 320 | result.argument_type_bounds = argument_type_bounds | |
| 321 | fi | |
| 322 | ||
| 323 | result.il_assembly_name = assembly_name | |
| 324 | ||
| 325 | if let builder = type_details.async_builder_type then | |
| 326 | if let classy_result: Symbols.Classy = result then | |
| 327 | classy_result.async_builder = _type_mapper.get_type(builder) | |
| 328 | fi | |
| 329 | fi | |
| 330 | ||
| 331 | if !il_name? then | |
| 332 | il_name = get_il_name(dotnet_type) | |
| 333 | fi | |
| 334 | ||
| 335 | result.il_name_override = il_name | |
| 336 | result.dotnet_full_name = dotnet_type.full_name | |
| 337 | ||
| 338 | return result | |
| 339 | si | |
| 340 | ||
| 341 | add_ancestors(symbol: Symbols.Scoped, type: TYPE) is | |
| 342 | if !isa Symbols.Classy(symbol) then | |
| 343 | return | |
| 344 | fi | |
| 345 | ||
| 346 | let result = symbol | |
| 347 | ||
| 348 | if type.base_type? then | |
| 349 | result.add_ancestor(_type_mapper.get_type(type.base_type!)) | |
| 350 | elif type.is_interface then | |
| 351 | result.add_ancestor(_type_mapper.get_type(_type_source.get_type("System.Object"))) | |
| 352 | fi | |
| 353 | ||
| 354 | // A trait `type` implements directly can carry a reference-`?` | |
| 355 | // type argument that ordinary reflection has no way to | |
| 356 | // surface (see INTERFACE_NULLABILITY) — recover it from the | |
| 357 | // assembly's raw metadata and apply it to the mapped type | |
| 358 | // before it becomes an ancestor. Empty for a trait `type` | |
| 359 | // only inherits via a base class, or one with nothing | |
| 360 | // optional in its arguments; either way the mapped type is | |
| 361 | // used as-is. | |
| 362 | let nullable_by_key = _type_source.interface_nullability_bytes(type) | |
| 363 | ||
| 364 | for i in type.get_interfaces() do | |
| 365 | let mapped mut = _type_mapper.get_type(i) | |
| 366 | ||
| 367 | let bytes: LIST[int] mut | |
| 368 | ||
| 369 | if nullable_by_key.try_get_value(Semantic.DotNet.INTERFACE_NULLABILITY.key_for(i), bytes ref) then | |
| 370 | mapped = Semantic.DotNet.NULLABILITY.apply_bytes(mapped, bytes) ?? mapped | |
| 371 | fi | |
| 372 | ||
| 373 | result.add_ancestor(mapped) | |
| 374 | od | |
| 375 | si | |
| 376 | ||
| 377 | // Reflected interfaces are the one kind that isn't marked | |
| 378 | // overrides-resolved on creation, so this is where their | |
| 379 | // inherited members get pulled down. It has to run after | |
| 380 | // `add_members`: a .NET interface that redeclares an inherited | |
| 381 | // member covariantly (`new IDerived? BaseType { get; }`) only | |
| 382 | // has its own declaration to override with once that | |
| 383 | // declaration is in scope. Pull down first and the inherited | |
| 384 | // declaration wins, hiding the derived one and turning a | |
| 385 | // resolved redeclaration into an inheritance clash. | |
| 386 | resolve_overrides(symbol: Symbols.Scoped) is | |
| 387 | if !isa Symbols.Classy(symbol) then | |
| 388 | return | |
| 389 | fi | |
| 390 | ||
| 391 | let result = symbol | |
| 392 | ||
| 393 | IoC.CONTAINER.instance.symbol_table.enter_scope(result) | |
| 394 | result.pull_down_super_symbols() | |
| 395 | IoC.CONTAINER.instance.symbol_table.leave_scope(result) | |
| 396 | si | |
| 397 | ||
| 398 | mask_gpa(a: GenericParameterAttributes, b: GenericParameterAttributes) -> GenericParameterAttributes => | |
| 399 | cast GenericParameterAttributes(cast int(a) & cast int(b)) | |
| 400 | ||
| 401 | // Every ghūl-mapped .NET type bound on this parameter (`where T : | |
| 402 | // IA, IB`), empty when the parameter is unbounded or only carries | |
| 403 | // the redundant `System.ValueType` companion of a `struct` flag. | |
| 404 | type_bounds(t: TYPE) -> Collections.List[Semantic.Types.Type] is | |
| 405 | let result = Collections.LIST[Semantic.Types.Type]() | |
| 406 | let constraint_attributes = mask_gpa(t.generic_parameter_attributes, GenericParameterAttributes.SPECIAL_CONSTRAINT_MASK) | |
| 407 | let struct_constrained = | |
| 408 | constraint_attributes.has_flag(GenericParameterAttributes.NOT_NULLABLE_VALUE_TYPE_CONSTRAINT) | |
| 409 | ||
| 410 | for c in t.get_generic_parameter_constraints() do | |
| 411 | if !(struct_constrained /\ c.full_name? /\ c.full_name =~ "System.ValueType") then | |
| 412 | result.add(_type_mapper.get_type(c)) | |
| 413 | fi | |
| 414 | od | |
| 415 | ||
| 416 | return result | |
| 417 | si | |
| 418 | ||
| 419 | // Static members of the `$globals` synthetic class are global | |
| 420 | // functions / variables / properties in the source language. Detect | |
| 421 | // the host so the importer can produce GLOBAL_FUNCTION / | |
| 422 | // GLOBAL_VARIABLE / GLOBAL_PROPERTY symbols rather than the | |
| 423 | // static-on-a-class kinds. Their owner becomes the enclosing | |
| 424 | // NAMESPACE (mirroring source-side `declare_function`) so qualified | |
| 425 | // names skip the synthetic `$globals` segment and IL emission goes | |
| 426 | // through the existing `gen_globals_class_reference` path. | |
| 427 | _globals_owner_namespace(owner: Symbols.Symbol) -> Symbols.NAMESPACE? is | |
| 428 | if !(owner.name =~ "$globals") \/ !isa Symbols.Classy(owner) then | |
| 429 | return null | |
| 430 | fi | |
| 431 | ||
| 432 | let classy_owner = owner.owner | |
| 433 | ||
| 434 | if !classy_owner? then | |
| 435 | return null | |
| 436 | fi | |
| 437 | ||
| 438 | return _namespaces.try_find_namespace(".{classy_owner.name}") | |
| 439 | si | |
| 440 | ||
| 441 | add_members(result: Symbols.Scoped, type: TYPE) is | |
| 442 | let properties = MAP() | |
| 443 | let indexers = SET() | |
| 444 | let indexer_accessors = SET() | |
| 445 | let methods = LIST() | |
| 446 | ||
| 447 | for p in type.get_properties() do | |
| 448 | add_property(result, type, properties, indexers, indexer_accessors, p) | |
| 449 | od | |
| 450 | ||
| 451 | for t in type.get_members(cast BindingFlags(64 + 4 + 32 + 16 + 8)) do | |
| 452 | add_member(result, type, properties, indexers, indexer_accessors, methods, t) | |
| 453 | od | |
| 454 | ||
| 455 | if methods.count > 0 then | |
| 456 | _ambiguous_method_checker.check(result, methods) | |
| 457 | ||
| 458 | for m in methods do | |
| 459 | // An intrinsic operator is registered on the types of | |
| 460 | // its operands rather than on the carrier that declared | |
| 461 | // it. | |
| 462 | if let home: Symbols.Classy = m.function.owner /\ isa Symbols.INNATE_FUNCTION(m.function) then | |
| 463 | home.add_member(m.function) | |
| 464 | ||
| 465 | for other in Symbols.TYPE_HOMED_OPERATORS.homes(m.function) do | |
| 466 | if other != home then | |
| 467 | other.add_member(m.function) | |
| 468 | fi | |
| 469 | od | |
| 470 | else | |
| 471 | result.add_member(m.function) | |
| 472 | fi | |
| 473 | od | |
| 474 | fi | |
| 475 | ||
| 476 | inherit_default_trait_members(result, type) | |
| 477 | ||
| 478 | _add_constructor_intrinsics(result, type) | |
| 479 | _add_scalar_conversions(result, type) | |
| 480 | si | |
| 481 | ||
| 482 | // The language's scalar types: the value types it names in | |
| 483 | // Ghul.Intrinsics, other than bool and void, which convert between | |
| 484 | // one another with `cast`. | |
| 485 | _is_scalar(type: TYPE) -> bool => | |
| 486 | type.is_value_type /\ !type.is_enum /\ !type.is_generic_type /\ | |
| 487 | type != _bool_type /\ type != _void_type /\ | |
| 488 | (_type_details_lookup.get_type_details_by_dotnet_type(type)?.ghul_namespace =~ "Ghul.Intrinsics") | |
| 489 | ||
| 490 | _scalar_types() -> Collections.List[TYPE] is | |
| 491 | let result = LIST[TYPE]() | |
| 492 | ||
| 493 | if let details = _type_details_lookup.get_all_type_details_in_ghul_namespace("Ghul.Intrinsics") then | |
| 494 | for detail in details do | |
| 495 | if _is_scalar(detail.dotnet_type) then | |
| 496 | result.add(detail.dotnet_type) | |
| 497 | fi | |
| 498 | od | |
| 499 | fi | |
| 500 | ||
| 501 | return result | |
| 502 | si | |
| 503 | ||
| 504 | // A scalar type is constructed from any other scalar, converting the | |
| 505 | // way `cast` does: `double(n)` is `cast double(n)`. Where .NET | |
| 506 | // already gives the type a constructor from that source, as decimal | |
| 507 | // and BigInteger do for the wider numeric types, that constructor | |
| 508 | // stays and none is added beside it. | |
| 509 | _add_scalar_conversions(owner: Symbols.Scoped, type: TYPE) is | |
| 510 | if !_is_scalar(type) then | |
| 511 | return | |
| 512 | fi | |
| 513 | ||
| 514 | let scalars = _scalar_types() | |
| 515 | ||
| 516 | let location = Source.LOCATION.reflected | |
| 517 | ||
| 518 | for source in SCALAR_CONVERSION_SOURCES.needing_constructor(type, scalars) do | |
| 519 | let result = Symbols.STRUCT_METHOD(location, location, owner, "init", owner) | |
| 520 | ||
| 521 | result.argument_names = LIST[string](["value"]) | |
| 522 | result.arguments = LIST[Type]([_type_mapper.get_type(source)]) | |
| 523 | result.return_type = _type_mapper.get_type(_void_type) | |
| 524 | result.is_scalar_conversion = true | |
| 525 | result.mark_declared_pure() | |
| 526 | result.mark_synthesized() | |
| 527 | ||
| 528 | owner.add_member(result) | |
| 529 | od | |
| 530 | si | |
| 531 | ||
| 532 | // Copy any concrete (`is_default_trait_method`) method from | |
| 533 | // a trait the class implements into the class itself, unless | |
| 534 | // the class declares a method with the same signature. | |
| 535 | // | |
| 536 | // Why this is its own pass: `mark_overrides_resolved()` runs | |
| 537 | // on reflected concrete classes in `create_class` so the | |
| 538 | // full `SYMBOL_INHERITANCE_RESOLVER` skips them — its | |
| 539 | // override-clash and abstract-method checks would otherwise | |
| 540 | // fire on .NET-side diamonds (`IList` × `IReadOnlyList`, | |
| 541 | // `MemberInfo.Module`, …) that the assembly's IL has already | |
| 542 | // resolved via explicit interface implementation but that | |
| 543 | // ghul's reflection layer can't see. Pulling down just the | |
| 544 | // trait defaults keeps the assumption-of-trust for | |
| 545 | // overrides while still making `find_member("filter")` work | |
| 546 | // on a reflected class that inherits `filter` from `Pipe[T]`. | |
| 547 | inherit_default_trait_members(result: Symbols.Scoped, type: TYPE) is | |
| 548 | if !isa Symbols.Classy(result) then | |
| 549 | return | |
| 550 | fi | |
| 551 | ||
| 552 | let classy = result | |
| 553 | ||
| 554 | for i in 0..classy.ancestors.count do | |
| 555 | let scope = classy.get_ancestor(i)?.scope | |
| 556 | ||
| 557 | if !scope? then | |
| 558 | continue | |
| 559 | fi | |
| 560 | ||
| 561 | for member in scope.symbols do | |
| 562 | inherit_default_trait_member(classy, member) | |
| 563 | od | |
| 564 | od | |
| 565 | si | |
| 566 | ||
| 567 | inherit_default_trait_member(into: Symbols.Classy, member: Symbols.Symbol) is | |
| 568 | if isa Symbols.FUNCTION_GROUP(member) then | |
| 569 | let group = member | |
| 570 | ||
| 571 | for function in group.functions do | |
| 572 | if function.is_default_trait_method /\ !declares_override_of(into, function) then | |
| 573 | into.add_member(function) | |
| 574 | fi | |
| 575 | od | |
| 576 | elif isa Symbols.Function(member) then | |
| 577 | let function = member | |
| 578 | ||
| 579 | if function.is_default_trait_method /\ !declares_override_of(into, function) then | |
| 580 | into.add_member(function) | |
| 581 | fi | |
| 582 | elif let property: Symbols.Property = member then | |
| 583 | // A property with a default body is inherited the same way, | |
| 584 | // unless the class declares one of that name itself. | |
| 585 | if let getter = property.read_function /\ getter.is_default_trait_method /\ !into.find_direct(property.name)? then | |
| 586 | into.add_member(property) | |
| 587 | fi | |
| 588 | fi | |
| 589 | si | |
| 590 | ||
| 591 | // Whether the class itself declares a method with the default | |
| 592 | // method's signature: that method is the class's implementation | |
| 593 | // of it, so the default is not inherited beside it as a second | |
| 594 | // candidate for the same call. | |
| 595 | declares_override_of(into: Symbols.Classy, default_method: Symbols.Function) -> bool static is | |
| 596 | let existing = into.find_direct(default_method.name) | |
| 597 | ||
| 598 | if let group: Symbols.FUNCTION_GROUP = existing then | |
| 599 | return group.functions |> any(f => _overrides(into, f, default_method)) | |
| 600 | elif let single: Symbols.Function = existing then | |
| 601 | return _overrides(into, single, default_method) | |
| 602 | fi | |
| 603 | ||
| 604 | return false | |
| 605 | si | |
| 606 | ||
| 607 | _overrides(into: Symbols.Classy, candidate: Symbols.Function, default_method: Symbols.Function) -> bool static => | |
| 608 | candidate != default_method /\ | |
| 609 | candidate.owner == into /\ | |
| 610 | candidate.override_class =~ default_method.override_class | |
| 611 | ||
| 612 | add_member( | |
| 613 | owner: Symbols.Scoped, | |
| 614 | type: TYPE, | |
| 615 | properties: MAP[System.Reflection.MethodInfo,PROPERTY_DETAILS], | |
| 616 | indexers: SET[System.Reflection.MethodInfo], | |
| 617 | indexer_accessors: SET[System.Reflection.MethodInfo], | |
| 618 | methods: LIST[(function: Symbols.Function, method_info: MethodInfo)], | |
| 619 | member: MemberInfo | |
| 620 | ) -> Symbols.Symbol? | |
| 621 | is | |
| 622 | let member_type = member.member_type | |
| 623 | ||
| 624 | if member_type == MemberTypes.CONSTRUCTOR then | |
| 625 | add_constructor(owner, cast ConstructorInfo?(member)!) | |
| 626 | elif member_type == MemberTypes.FIELD then | |
| 627 | add_field(owner, type, cast FieldInfo?(member)!) | |
| 628 | elif member_type == MemberTypes.METHOD then | |
| 629 | add_method(owner, type, properties, indexers, indexer_accessors, methods, cast MethodInfo?(member)!) | |
| 630 | fi | |
| 631 | return null | |
| 632 | si | |
| 633 | ||
| 634 | add_constructor(owner: Symbols.Scoped, method: ConstructorInfo) is | |
| 635 | let result: Symbols.Function mut | |
| 636 | let location = Source.LOCATION.reflected | |
| 637 | ||
| 638 | if !method.is_public /\ !_is_session_member(method) then | |
| 639 | return | |
| 640 | fi | |
| 641 | ||
| 642 | if method.is_static then | |
| 643 | result = Symbols.STATIC_METHOD(location, location, owner, "init", owner) | |
| 644 | elif owner.is_value_type then | |
| 645 | result = Symbols.STRUCT_METHOD(location, location, owner, "init", owner) | |
| 646 | elif method.is_abstract then | |
| 647 | result = Symbols.ABSTRACT_METHOD(location, location, owner, "init", owner) | |
| 648 | else | |
| 649 | result = Symbols.INSTANCE_METHOD(location, location, owner, "init", owner) | |
| 650 | fi | |
| 651 | ||
| 652 | result.il_name_override = method.name | |
| 653 | ||
| 654 | let argument_names = LIST() | |
| 655 | let argument_types = LIST() | |
| 656 | let argument_defaults = Collections.LIST[string?]() | |
| 657 | let argument_reads = Collections.LIST[bool]() | |
| 658 | let argument_writes = Collections.LIST[bool]() | |
| 659 | let argument_is_pack = Collections.LIST[bool]() | |
| 660 | let argument_pack_depth = Collections.LIST[int]() | |
| 661 | let spread_argument_index mut = -1 | |
| 662 | let has_direction_override mut = false | |
| 663 | ||
| 664 | for a in method.get_parameters() do | |
| 665 | argument_names.add(a.name ?? "") | |
| 666 | let arg_attributes = a.get_custom_attributes_data() | |
| 667 | ||
| 668 | let pack_depth = _argument_pack_depth(arg_attributes) | |
| 669 | ||
| 670 | argument_is_pack.add(pack_depth >= 0) | |
| 671 | argument_pack_depth.add(if pack_depth >= 0 then pack_depth else 0 fi) | |
| 672 | ||
| 673 | if _has_attribute_named(arg_attributes, ARGUMENT_SPREAD_ATTRIBUTE_NAME) then | |
| 674 | spread_argument_index = argument_names.count - 1 | |
| 675 | fi | |
| 676 | ||
| 677 | argument_types.add( | |
| 678 | NULLABILITY.resolve( | |
| 679 | _with_tuple_element_names(_type_mapper.get_type(a.parameter_type), arg_attributes), | |
| 680 | arg_attributes, | |
| 681 | method, | |
| 682 | a.parameter_type | |
| 683 | )! | |
| 684 | ) | |
| 685 | argument_defaults.add(_default_for_parameter(a)) | |
| 686 | argument_reads.add(!(a.is_out /\ !a.is_in)) | |
| 687 | argument_writes.add(!(a.is_in /\ !a.is_out)) | |
| 688 | if a.is_out \/ a.is_in then | |
| 689 | has_direction_override = true | |
| 690 | fi | |
| 691 | od | |
| 692 | ||
| 693 | result.argument_is_pack = argument_is_pack | |
| 694 | result.argument_pack_depth = argument_pack_depth | |
| 695 | result.spread_argument_index = spread_argument_index | |
| 696 | ||
| 697 | let return_pack_depth = _pack_depth(method.get_custom_attributes_data(), RETURN_PACK_ATTRIBUTE_NAME) | |
| 698 | ||
| 699 | if return_pack_depth >= 0 then | |
| 700 | result.return_pack_depth = return_pack_depth | |
| 701 | fi | |
| 702 | result.argument_names = argument_names | |
| 703 | result.arguments = argument_types | |
| 704 | result.argument_defaults = argument_defaults | |
| 705 | ||
| 706 | if has_direction_override then | |
| 707 | result.set_argument_directions(argument_reads, argument_writes) | |
| 708 | fi | |
| 709 | ||
| 710 | result.return_type = _type_mapper.get_type(_void_type) | |
| 711 | result.mark_pack_type_parameters() | |
| 712 | ||
| 713 | // A public parameterless constructor — what a `new()` | |
| 714 | // type-parameter constraint requires of a type argument. | |
| 715 | if argument_types.count == 0 /\ isa Symbols.Classy(owner) then | |
| 716 | (cast Symbols.Classy(owner)).has_parameterless_constructor = true | |
| 717 | fi | |
| 718 | ||
| 719 | owner.add_member(result) | |
| 720 | si | |
| 721 | ||
| 722 | add_field(owner: Symbols.Scoped, type: TYPE, `field: FieldInfo) is | |
| 723 | if !`field.is_public /\ !_is_session_member(`field) then | |
| 724 | return | |
| 725 | fi | |
| 726 | ||
| 727 | if type.is_enum /\ !`field.is_special_name then | |
| 728 | let raw_value = `field.get_raw_constant_value() | |
| 729 | let enum_member = | |
| 730 | Symbols.ENUM_STRUCT_MEMBER( | |
| 731 | Source.LOCATION.reflected, | |
| 732 | cast Symbols.ENUM_STRUCT?(owner)!, | |
| 733 | _type_name_map.get_constant_name(owner.qualified_name, `field.name, false, `field.declaring_type ?? type), | |
| 734 | if raw_value? then raw_value.to_string() ?? "" else "" fi | |
| 735 | ) | |
| 736 | ||
| 737 | enum_member.set_doc(_doc_attribute(`field.get_custom_attributes_data())) | |
| 738 | enum_member.set_deprecation(_obsolete_attribute(`field.get_custom_attributes_data())) | |
| 739 | ||
| 740 | owner.add_member(enum_member) | |
| 741 | ||
| 742 | return | |
| 743 | fi | |
| 744 | ||
| 745 | let result: Symbols.Field mut | |
| 746 | let location = Source.LOCATION.reflected | |
| 747 | ||
| 748 | let field_name = `field.name | |
| 749 | ||
| 750 | let name: string? mut = null | |
| 751 | ||
| 752 | if field_name.length == 5 /\ `field.name.starts_with("Item") /\ type.is_generic_type then | |
| 753 | let unspecialized_type = type.get_generic_type_definition() | |
| 754 | ||
| 755 | let is_tuple mut = false | |
| 756 | ||
| 757 | for t in _tuple_types do | |
| 758 | if unspecialized_type == t then | |
| 759 | is_tuple = true | |
| 760 | break | |
| 761 | fi | |
| 762 | od | |
| 763 | ||
| 764 | if is_tuple then | |
| 765 | name = "{(cast int(field_name[field_name.length-1]) - 49)}" | |
| 766 | fi | |
| 767 | fi | |
| 768 | ||
| 769 | if !name? then | |
| 770 | name = _type_name_map.get_member_name(owner.qualified_name, `field.name, false, false, `field.declaring_type ?? type) | |
| 771 | fi | |
| 772 | ||
| 773 | // For inherited fields, the declaring symbol must point at the base | |
| 774 | // class, not the derived class — otherwise the IL fieldref says | |
| 775 | // "Foo::SubjectField" instead of "FooBase`2<...>::SubjectField" and | |
| 776 | // the runtime can't find the field. | |
| 777 | let declaring_symbol: Symbols.Symbol mut = owner | |
| 778 | let dt = `field.declaring_type | |
| 779 | if dt? /\ dt != type then | |
| 780 | declaring_symbol = _type_mapper.get_type(dt).symbol | |
| 781 | fi | |
| 782 | ||
| 783 | if `field.is_static then | |
| 784 | let globals_namespace = _globals_owner_namespace(owner) | |
| 785 | let constant = REFLECTED_CONSTANTS.value_of(`field) | |
| 786 | ||
| 787 | if globals_namespace? then | |
| 788 | let global = Symbols.GLOBAL_VARIABLE(location, globals_namespace, name) | |
| 789 | global.il_carrier = cast Symbols.Classy?(owner)! | |
| 790 | result = global | |
| 791 | elif isa ReflectedConstant.ABSENT(constant) then | |
| 792 | result = Symbols.CONSTANT_FIELD(location, declaring_symbol, name, null) | |
| 793 | elif let value: ReflectedConstant.VALUE = constant then | |
| 794 | result = Symbols.CONSTANT_FIELD(location, declaring_symbol, name, value.text) | |
| 795 | else | |
| 796 | result = Symbols.STATIC_FIELD(location, declaring_symbol, name) | |
| 797 | fi | |
| 798 | elif type.is_value_type then | |
| 799 | result = Symbols.STRUCT_FIELD(location, declaring_symbol, name) | |
| 800 | else | |
| 801 | result = Symbols.INSTANCE_FIELD(location, declaring_symbol, name) | |
| 802 | fi | |
| 803 | ||
| 804 | result.il_name_override = `field.name | |
| 805 | ||
| 806 | let field_attributes = `field.get_custom_attributes_data() | |
| 807 | ||
| 808 | // The attribute check alone misses a field imported from an | |
| 809 | // assembly built by a compiler that predates it. A field name | |
| 810 | // is never operator syntax - only a function can be named with | |
| 811 | // one - so a leading `$` on a field carries no ambiguity and is | |
| 812 | // a safe legacy fallback, the same shape as | |
| 813 | // GLOBALS_CARRIER.is_carrier's attribute-or-name check. | |
| 814 | if _has_compiler_generated_attribute(field_attributes) \/ name.starts_with('$') then | |
| 815 | result.mark_synthesized() | |
| 816 | fi | |
| 817 | ||
| 818 | result.set_doc(_doc_attribute(field_attributes)) | |
| 819 | result.set_deprecation(_obsolete_attribute(field_attributes)) | |
| 820 | ||
| 821 | result.set_type( | |
| 822 | NULLABILITY.resolve( | |
| 823 | _with_tuple_element_names(_type_mapper.get_type(`field.field_type), field_attributes), | |
| 824 | field_attributes, | |
| 825 | `field, | |
| 826 | `field.field_type | |
| 827 | )! | |
| 828 | ) | |
| 829 | ||
| 830 | // Same problem as add_method: inherited fields on a constructed | |
| 831 | // generic base have their declared type returned by reflection in | |
| 832 | // *substituted* form. Recover the open form via the open base type | |
| 833 | // and stash it as unspecialized_type; gen_reference prefers it. | |
| 834 | if dt? /\ dt != type /\ dt.is_generic_type /\ !dt.is_generic_type_definition then | |
| 835 | let open_type = dt.get_generic_type_definition() | |
| 836 | let open_field: FieldInfo? mut = null | |
| 837 | ||
| 838 | for m in open_type.get_members(cast BindingFlags(2 + 4 + 8 + 16 + 32)) do | |
| 839 | if m.member_type == MemberTypes.FIELD then | |
| 840 | let candidate = cast FieldInfo?(m)! | |
| 841 | if candidate.metadata_token == `field.metadata_token then | |
| 842 | open_field = candidate | |
| 843 | break | |
| 844 | fi | |
| 845 | fi | |
| 846 | od | |
| 847 | ||
| 848 | if open_field? then | |
| 849 | result.unspecialized_type = _type_mapper.get_type(open_field.field_type) | |
| 850 | fi | |
| 851 | fi | |
| 852 | ||
| 853 | owner.add_member(result) | |
| 854 | si | |
| 855 | ||
| 856 | // `family` and `famorassem` are deliberately absent: a subclass in | |
| 857 | // another assembly is entitled to a protected member. | |
| 858 | _is_reachable_from_other_assembly(method: MethodInfo) -> bool => | |
| 859 | !(method.is_private \/ method.is_assembly \/ method.is_family_and_assembly) \/ | |
| 860 | _is_session_member(method) | |
| 861 | ||
| 862 | // A member an earlier cell of the session declared, other than one | |
| 863 | // the compiler generated for it: the session is one assembly, so | |
| 864 | // its assembly-private members are as visible as they would be in | |
| 865 | // one file, and the ordinary access rules decide who may use them. | |
| 866 | _is_session_member(member: System.Reflection.MemberInfo) -> bool is | |
| 867 | let declaring = member.declaring_type | |
| 868 | ||
| 869 | return | |
| 870 | declaring? /\ !member.name.contains('$') /\ | |
| 871 | IoC.CONTAINER.instance.assemblies.can_reach_privates_of(declaring.assembly) | |
| 872 | si | |
| 873 | ||
| 874 | // The name of the type's indexer property, as named by | |
| 875 | // `DefaultMemberAttribute`. A type with no marker cannot have | |
| 876 | // an indexer, but the name an indexer would take is still the | |
| 877 | // right answer for the accessor-naming comparison. | |
| 878 | _default_member_name(type: TYPE) -> string is | |
| 879 | for attr in type.get_custom_attributes_data() do | |
| 880 | if attr.attribute_type.full_name =~ "System.Reflection.DefaultMemberAttribute" then | |
| 881 | for argument in attr.constructor_arguments do | |
| 882 | let name = cast string?(argument.value) | |
| 883 | ||
| 884 | if name? then | |
| 885 | return name | |
| 886 | fi | |
| 887 | od | |
| 888 | fi | |
| 889 | od | |
| 890 | ||
| 891 | return Symbols.INDEXER_NAMES.property_name | |
| 892 | si | |
| 893 | ||
| 894 | add_property( | |
| 895 | owner: Symbols.Scoped, | |
| 896 | type: TYPE, | |
| 897 | properties: MAP[System.Reflection.MethodInfo,PROPERTY_DETAILS], | |
| 898 | indexers: SET[System.Reflection.MethodInfo], | |
| 899 | indexer_accessors: SET[System.Reflection.MethodInfo], | |
| 900 | property: PropertyInfo | |
| 901 | ) is | |
| 902 | let index_parameters = property.get_index_parameters() | |
| 903 | ||
| 904 | if index_parameters.count > 0 then | |
| 905 | // An indexed property is *the* indexer when the declaring | |
| 906 | // type nominates it. A type can carry others that indexing | |
| 907 | // must not reach — but one already named `Item` was | |
| 908 | // reachable before the nomination was consulted, so it | |
| 909 | // stays reachable whether or not it is the nominated one. | |
| 910 | let is_indexer = | |
| 911 | property.name =~ _default_member_name(property.declaring_type ?? type) \/ | |
| 912 | property.name =~ Symbols.INDEXER_NAMES.property_name | |
| 913 | ||
| 914 | let getter = property.get_get_method() | |
| 915 | if getter? then | |
| 916 | indexers.add(getter) | |
| 917 | ||
| 918 | if is_indexer then | |
| 919 | indexer_accessors.add(getter) | |
| 920 | fi | |
| 921 | fi | |
| 922 | ||
| 923 | let setter = property.get_set_method() | |
| 924 | if setter? then | |
| 925 | indexers.add(setter) | |
| 926 | ||
| 927 | if is_indexer then | |
| 928 | indexer_accessors.add(setter) | |
| 929 | fi | |
| 930 | fi | |
| 931 | ||
| 932 | return | |
| 933 | fi | |
| 934 | ||
| 935 | if property.name =~ "Current" then | |
| 936 | if property.declaring_type == _type_source.get_type("System.Collections.IEnumerator") then | |
| 937 | return | |
| 938 | fi | |
| 939 | fi | |
| 940 | ||
| 941 | let result: Symbols.Property mut | |
| 942 | let name = | |
| 943 | _ghul_name_override(property) ?? | |
| 944 | _type_name_map.get_member_name(owner.qualified_name, property.name, false, property.is_special_name, property.declaring_type ?? type) | |
| 945 | ||
| 946 | let getter = property.get_method | |
| 947 | let setter = property.set_method | |
| 948 | ||
| 949 | if setter? /\ !getter? then | |
| 950 | return | |
| 951 | fi | |
| 952 | ||
| 953 | let is_static mut = false | |
| 954 | ||
| 955 | if getter? /\ getter.is_static then | |
| 956 | is_static = true | |
| 957 | elif setter? /\ setter.is_static then | |
| 958 | is_static = true | |
| 959 | fi | |
| 960 | ||
| 961 | if is_static then | |
| 962 | let globals_namespace = _globals_owner_namespace(owner) | |
| 963 | if globals_namespace? then | |
| 964 | result = Symbols.GLOBAL_PROPERTY(Source.LOCATION.reflected, Source.LOCATION.reflected, globals_namespace, name, setter?) | |
| 965 | else | |
| 966 | result = Symbols.STATIC_PROPERTY(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, name, setter?, false) | |
| 967 | fi | |
| 968 | else | |
| 969 | result = Symbols.INSTANCE_PROPERTY(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, name, setter?, false) | |
| 970 | fi | |
| 971 | ||
| 972 | result.set_type( | |
| 973 | _with_property_tuple_element_names( | |
| 974 | NULLABILITY.resolve( | |
| 975 | _type_mapper.get_type(property.property_type), | |
| 976 | property.get_custom_attributes_data(), | |
| 977 | property, | |
| 978 | property.property_type | |
| 979 | )!, | |
| 980 | property | |
| 981 | ) | |
| 982 | ) | |
| 983 | result.il_name_override = property.name | |
| 984 | ||
| 985 | result.set_doc(_doc_attribute(property.get_custom_attributes_data())) | |
| 986 | result.set_deprecation(_obsolete_attribute(property.get_custom_attributes_data())) | |
| 987 | ||
| 988 | // Not CompilerGeneratedAttribute here: the C# compiler puts it | |
| 989 | // on a record's public surface too (EqualityContract is | |
| 990 | // protected, but a member reachable through inheritance still | |
| 991 | // has to stay visible where it's reachable at all), so it | |
| 992 | // does not distinguish hidden from ordinary API the way it | |
| 993 | // does for a field or a type. is_special_name is the CLR's | |
| 994 | // own narrower signal for a property that is not ordinary | |
| 995 | // API. | |
| 996 | if property.is_special_name then | |
| 997 | result.mark_synthesized() | |
| 998 | fi | |
| 999 | ||
| 1000 | owner.add_member(result) | |
| 1001 | ||
| 1002 | let property_details = PROPERTY_DETAILS(result, property.get_get_method(), property.get_set_method()) | |
| 1003 | ||
| 1004 | if getter? then | |
| 1005 | properties[getter] = property_details | |
| 1006 | fi | |
| 1007 | ||
| 1008 | if setter? then | |
| 1009 | properties[setter] = property_details | |
| 1010 | fi | |
| 1011 | si | |
| 1012 | ||
| 1013 | add_method( | |
| 1014 | owner: Symbols.Scoped, | |
| 1015 | type: TYPE, | |
| 1016 | properties: MAP[System.Reflection.MethodInfo,PROPERTY_DETAILS], | |
| 1017 | indexers: SET[System.Reflection.MethodInfo], | |
| 1018 | indexer_accessors: SET[System.Reflection.MethodInfo], | |
| 1019 | methods: LIST[(function: Symbols.Function, method_info: MethodInfo)], | |
| 1020 | method: MethodInfo | |
| 1021 | ) is | |
| 1022 | add_method(owner, type, properties, indexers, indexer_accessors, methods, method, null) | |
| 1023 | si | |
| 1024 | ||
| 1025 | // `force_name`, when non-null, supplies the ghūl name instead of | |
| 1026 | // deriving one from the method. Used to surface a single | |
| 1027 | // reflected method under a second name: the derivation is | |
| 1028 | // skipped so the alias cannot re-trigger the side effects some | |
| 1029 | // of its branches carry. | |
| 1030 | // | |
| 1031 | // An alias of an abstract interface member is built as a | |
| 1032 | // default trait method rather than an abstract one. Only the | |
| 1033 | // primary name carries the obligation to implement, so a type | |
| 1034 | // satisfies the interface by supplying that one member, while | |
| 1035 | // both names remain callable. | |
| 1036 | add_method( | |
| 1037 | owner: Symbols.Scoped, | |
| 1038 | type: TYPE, | |
| 1039 | properties: MAP[System.Reflection.MethodInfo,PROPERTY_DETAILS], | |
| 1040 | indexers: SET[System.Reflection.MethodInfo], | |
| 1041 | indexer_accessors: SET[System.Reflection.MethodInfo], | |
| 1042 | methods: LIST[(function: Symbols.Function, method_info: MethodInfo)], | |
| 1043 | method: MethodInfo, | |
| 1044 | force_name: string? | |
| 1045 | ) is | |
| 1046 | if !_is_reachable_from_other_assembly(method) then | |
| 1047 | return | |
| 1048 | fi | |
| 1049 | ||
| 1050 | let result: Symbols.Function mut | |
| 1051 | let location = Source.LOCATION.reflected | |
| 1052 | ||
| 1053 | let name: string mut | |
| 1054 | let hide_from_operator_resolution mut = false | |
| 1055 | ||
| 1056 | // Set when this method is declared under a name the compiler | |
| 1057 | // itself picked ($get_iterator below) rather than one the | |
| 1058 | // reflected metadata names it, so it never appears as a | |
| 1059 | // second, ordinarily-callable member of the reflected type. | |
| 1060 | let is_synthesized_name mut = false | |
| 1061 | ||
| 1062 | // Set when the method is also to be reached under its | |
| 1063 | // ordinary de-camelled name, alongside the operator | |
| 1064 | // spelling picked below. | |
| 1065 | let alias_name: string? mut = null | |
| 1066 | ||
| 1067 | let property_details: PROPERTY_DETAILS mut | |
| 1068 | ||
| 1069 | if force_name? then | |
| 1070 | name = force_name | |
| 1071 | elif properties.try_get_value(method, property_details ref) then | |
| 1072 | name = | |
| 1073 | _ghul_name_override(method) ?? | |
| 1074 | _type_name_map.get_member_name(owner.qualified_name, method.name, method.is_private, true, method.declaring_type ?? type) | |
| 1075 | is_synthesized_name = true | |
| 1076 | elif indexer_accessors.contains(method) then | |
| 1077 | // Declared only under the indexer names, whatever | |
| 1078 | // metadata calls the accessor. An indexer is reached by | |
| 1079 | // indexing and by nothing else, so the metadata spelling | |
| 1080 | // is deliberately not surfaced as a second member. | |
| 1081 | name = Symbols.INDEXER_NAMES.accessor(method.name) | |
| 1082 | elif method.name =~ "Equals" /\ _is_self_relational_interface_target(type, method, _equatable_open_type, _bool_type) then | |
| 1083 | name = "=~" | |
| 1084 | alias_name = _plain_member_name(owner, type, indexers, method) | |
| 1085 | hide_from_operator_resolution = type.full_name? /\ _innate_equality_type_names.contains(type.full_name) | |
| 1086 | elif method.name =~ "CompareTo" /\ _is_self_relational_interface_target(type, method, _comparable_open_type, _int_type) then | |
| 1087 | name = "<>" | |
| 1088 | alias_name = _plain_member_name(owner, type, indexers, method) | |
| 1089 | hide_from_operator_resolution = type.full_name? /\ _innate_order_type_names.contains(type.full_name) | |
| 1090 | elif method.name =~ "get_Current" /\ method.declaring_type == _type_source.get_type("System.Collections.IEnumerator") then | |
| 1091 | return | |
| 1092 | elif method.name =~ "GetEnumerator" then | |
| 1093 | if method.declaring_type == _type_source.get_type("System.Collections.IEnumerable") then | |
| 1094 | return | |
| 1095 | fi | |
| 1096 | ||
| 1097 | name = "$get_iterator" | |
| 1098 | is_synthesized_name = true | |
| 1099 | ||
| 1100 | let iterator_property = Symbols.INSTANCE_PROPERTY(Source.LOCATION.reflected, Source.LOCATION.reflected, owner, "iterator", false, false) | |
| 1101 | iterator_property.set_type(_type_mapper.get_type(method.return_type)) | |
| 1102 | ||
| 1103 | owner.add_member(iterator_property) | |
| 1104 | ||
| 1105 | property_details = PROPERTY_DETAILS(iterator_property, method, null) | |
| 1106 | elif | |
| 1107 | method.is_static /\ | |
| 1108 | let mdt0 = method.declaring_type in mdt0? /\ | |
| 1109 | _generic_math_operator_name(method.name)? /\ | |
| 1110 | ( | |
| 1111 | (method.is_virtual /\ mdt0.is_interface) \/ | |
| 1112 | _declares_own_operators(mdt0) | |
| 1113 | ) | |
| 1114 | then | |
| 1115 | name = _generic_math_operator_name(method.name)! | |
| 1116 | else | |
| 1117 | name = _plain_member_name(owner, type, indexers, method) | |
| 1118 | ||
| 1119 | // A .NET special name - an operator, an event accessor - | |
| 1120 | // is reached through its dedicated syntax, never under | |
| 1121 | // any name, whatever assembly declared it. The flag on | |
| 1122 | // the row is the upstream fact; the $ spelling | |
| 1123 | // get_member_name derives from it is presentation. | |
| 1124 | // | |
| 1125 | // The test repeats the two carve-outs _plain_member_name | |
| 1126 | // applies, so the verdict and the name stay in step: an | |
| 1127 | // indexer accessor is named under its indexer names, and | |
| 1128 | // a ghūl name override replaces the derived spelling | |
| 1129 | // outright - either way there is no $ to hide. | |
| 1130 | if | |
| 1131 | method.is_special_name /\ | |
| 1132 | !indexers.contains(method) /\ | |
| 1133 | !_ghul_name_override(method)? | |
| 1134 | then | |
| 1135 | is_synthesized_name = true | |
| 1136 | fi | |
| 1137 | fi | |
| 1138 | ||
| 1139 | let declaring_symbol: Symbols.Symbol mut = owner | |
| 1140 | ||
| 1141 | let mdt = method.declaring_type | |
| 1142 | if mdt? /\ mdt != type then | |
| 1143 | declaring_symbol = _type_mapper.get_type(mdt).symbol | |
| 1144 | fi | |
| 1145 | ||
| 1146 | let intrinsic_operation = _intrinsic_operation(method.get_custom_attributes_data()) | |
| 1147 | ||
| 1148 | // A constructor intrinsic is an ordinary method: it is reached | |
| 1149 | // as a constructor of the type it returns, and called as itself. | |
| 1150 | if intrinsic_operation? /\ !intrinsic_operation.starts_with("constructor.") then | |
| 1151 | let globals_namespace = if method.is_static then _globals_owner_namespace(owner) else null fi | |
| 1152 | ||
| 1153 | if globals_namespace? then | |
| 1154 | // Innate calls lower to opcodes inline and never emit a | |
| 1155 | // methodref, so an innate function needs no il_assembly_name. | |
| 1156 | let home = _intrinsic_home(name, method) | |
| 1157 | ||
| 1158 | if home? then | |
| 1159 | result = Symbols.INNATE_FUNCTION(location, home, name, home, intrinsic_operation) | |
| 1160 | else | |
| 1161 | result = Symbols.INNATE_FUNCTION(location, globals_namespace, name, globals_namespace, intrinsic_operation) | |
| 1162 | fi | |
| 1163 | else | |
| 1164 | result = Symbols.INNATE_METHOD(location, declaring_symbol, name, owner, intrinsic_operation) | |
| 1165 | fi | |
| 1166 | elif method.is_static then | |
| 1167 | let globals_namespace = _globals_owner_namespace(owner) | |
| 1168 | if globals_namespace? then | |
| 1169 | let global = Symbols.GLOBAL_FUNCTION(location, location, globals_namespace, name, globals_namespace) | |
| 1170 | global.il_carrier = cast Symbols.Classy?(owner)! | |
| 1171 | result = global | |
| 1172 | else | |
| 1173 | let static_method = Symbols.STATIC_METHOD(location, location, declaring_symbol, name, owner) | |
| 1174 | ||
| 1175 | // A static virtual/abstract interface member - | |
| 1176 | // .NET's generic-math feature. Reachable through a | |
| 1177 | // bound type parameter, which needs the | |
| 1178 | // `constrained.` call shape; an ordinary reflected | |
| 1179 | // static method never is. | |
| 1180 | static_method.is_static_interface_virtual = method.is_virtual /\ mdt? /\ mdt.is_interface | |
| 1181 | ||
| 1182 | static_method.cannot_be_overridden = !static_method.is_static_interface_virtual | |
| 1183 | ||
| 1184 | result = static_method | |
| 1185 | fi | |
| 1186 | elif mdt? /\ mdt.is_value_type then | |
| 1187 | result = Symbols.STRUCT_METHOD(location, location, declaring_symbol, name, owner) | |
| 1188 | ||
| 1189 | result.cannot_be_overridden = true | |
| 1190 | elif method.is_abstract /\ !force_name? then | |
| 1191 | result = Symbols.ABSTRACT_METHOD(location, location, declaring_symbol, name, owner) | |
| 1192 | elif mdt? /\ mdt.is_interface then | |
| 1193 | result = Symbols.DEFAULT_TRAIT_METHOD(location, location, declaring_symbol, name, owner) | |
| 1194 | else | |
| 1195 | result = Symbols.INSTANCE_METHOD(location, location, declaring_symbol, name, owner) | |
| 1196 | ||
| 1197 | // A non-virtual or final CLR method binds to exactly | |
| 1198 | // one body, and so does any method of a sealed owner: | |
| 1199 | // no subclass exists to override it in. | |
| 1200 | result.cannot_be_overridden = | |
| 1201 | !method.is_virtual \/ method.is_final \/ (mdt? /\ mdt.is_sealed) | |
| 1202 | fi | |
| 1203 | ||
| 1204 | if method.is_generic_method then | |
| 1205 | result.is_generic = true | |
| 1206 | ||
| 1207 | let generic_argument_names = LIST() | |
| 1208 | let generic_arguments = LIST() | |
| 1209 | let generic_argument_constraint_kinds = Collections.LIST[Symbols.TypeParameterConstraintKind]() | |
| 1210 | let generic_argument_has_constructor_constraint = Collections.LIST[bool]() | |
| 1211 | let generic_argument_type_bounds = Collections.LIST[Collections.List[Semantic.Types.Type]]() | |
| 1212 | ||
| 1213 | let ga = method.get_generic_arguments() | |
| 1214 | ||
| 1215 | for p in ga do | |
| 1216 | generic_argument_names.add(p.name) | |
| 1217 | generic_arguments.add(_type_mapper.get_type(p)) | |
| 1218 | generic_argument_constraint_kinds.add(_type_mapper.generic_parameter_constraint_kind(p)) | |
| 1219 | generic_argument_has_constructor_constraint.add(_type_mapper.generic_parameter_has_constructor_constraint(p)) | |
| 1220 | generic_argument_type_bounds.add(type_bounds(p)) | |
| 1221 | od | |
| 1222 | ||
| 1223 | result.generic_argument_names = generic_argument_names | |
| 1224 | result.generic_arguments = generic_arguments | |
| 1225 | result.generic_argument_constraint_kinds = generic_argument_constraint_kinds | |
| 1226 | result.generic_argument_has_constructor_constraint = generic_argument_has_constructor_constraint | |
| 1227 | ||
| 1228 | if generic_argument_type_bounds.count > 0 /\ generic_argument_type_bounds |> any(l => l.count > 0) then | |
| 1229 | result.generic_argument_type_bounds = generic_argument_type_bounds | |
| 1230 | fi | |
| 1231 | fi | |
| 1232 | ||
| 1233 | result.il_name_override = method.name | |
| 1234 | result.is_hidden_from_operator_resolution = hide_from_operator_resolution | |
| 1235 | ||
| 1236 | // Not CompilerGeneratedAttribute on a foreign assembly: the C# | |
| 1237 | // compiler puts it on a record's ToString, Equals, GetHashCode, | |
| 1238 | // Deconstruct and equality operators too - ordinary callable | |
| 1239 | // API, not hidden members - so unlike a field or a type it does | |
| 1240 | // not distinguish the two there. On a ghūl-compiled assembly it | |
| 1241 | // is authoritative: the emitter marks what it synthesises - | |
| 1242 | // property accessors, lambda bodies compiled onto their | |
| 1243 | // enclosing type as $anon members, state-machine frame members, | |
| 1244 | // the Object.Equals bridge - and marks nothing a user wrote, | |
| 1245 | // since an authored member is either public and unmarked or | |
| 1246 | // invisible past the assembly boundary. Reading it recognises | |
| 1247 | // those shapes whatever name they travel under, and leaves an | |
| 1248 | // author's own `$` spelling alone. | |
| 1249 | if | |
| 1250 | is_synthesized_name \/ | |
| 1251 | ( | |
| 1252 | _type_name_map.is_ghul_compiled(method.declaring_type ?? type) /\ | |
| 1253 | _has_compiler_generated_attribute(method.get_custom_attributes_data()) | |
| 1254 | ) | |
| 1255 | then | |
| 1256 | result.mark_synthesized() | |
| 1257 | fi | |
| 1258 | ||
| 1259 | if method.is_family \/ method.is_family_or_assembly then | |
| 1260 | result.mark_imported_protected() | |
| 1261 | fi | |
| 1262 | ||
| 1263 | if _has_pure_attribute(method.get_custom_attributes_data()) then | |
| 1264 | // the marker carries the declared-pure contract: the | |
| 1265 | // function is trusted store-free, and local overrides | |
| 1266 | // are held to the pure-override contract here in the | |
| 1267 | // overriding assembly | |
| 1268 | result.mark_declared_pure() | |
| 1269 | fi | |
| 1270 | ||
| 1271 | // the marker carries a library member's @trusted_store_free(): | |
| 1272 | // its own body is trusted, and overriders are judged here | |
| 1273 | if _has_attribute(method.get_custom_attributes_data(), "Ghul.Internal.TRUSTED_STORE_FREE_ATTRIBUTE") then | |
| 1274 | result.mark_trusted_store_free() | |
| 1275 | fi | |
| 1276 | ||
| 1277 | if _has_stable_attribute(method.get_custom_attributes_data()) then | |
| 1278 | // the marker carries the declared-stable contract of | |
| 1279 | // the property this accessor reads: facts narrowed | |
| 1280 | // through it count as backed, and local overrides are | |
| 1281 | // held to the stable-override contract here in the | |
| 1282 | // overriding assembly | |
| 1283 | result.mark_declared_stable() | |
| 1284 | fi | |
| 1285 | ||
| 1286 | result.set_doc(_doc_attribute(method.get_custom_attributes_data())) | |
| 1287 | result.set_deprecation(_obsolete_attribute(method.get_custom_attributes_data())) | |
| 1288 | ||
| 1289 | let argument_names = LIST() | |
| 1290 | let argument_types = LIST() | |
| 1291 | let argument_defaults = Collections.LIST[string?]() | |
| 1292 | let argument_reads = Collections.LIST[bool]() | |
| 1293 | let argument_writes = Collections.LIST[bool]() | |
| 1294 | let argument_is_pack = Collections.LIST[bool]() | |
| 1295 | let argument_pack_depth = Collections.LIST[int]() | |
| 1296 | let spread_argument_index mut = -1 | |
| 1297 | let has_direction_override mut = false | |
| 1298 | ||
| 1299 | for a in method.get_parameters() do | |
| 1300 | argument_names.add(a.name ?? "") | |
| 1301 | let arg_attributes = a.get_custom_attributes_data() | |
| 1302 | ||
| 1303 | let pack_depth = _argument_pack_depth(arg_attributes) | |
| 1304 | ||
| 1305 | argument_is_pack.add(pack_depth >= 0) | |
| 1306 | argument_pack_depth.add(if pack_depth >= 0 then pack_depth else 0 fi) | |
| 1307 | ||
| 1308 | if _has_attribute_named(arg_attributes, ARGUMENT_SPREAD_ATTRIBUTE_NAME) then | |
| 1309 | spread_argument_index = argument_names.count - 1 | |
| 1310 | fi | |
| 1311 | ||
| 1312 | argument_types.add( | |
| 1313 | _with_pure_function( | |
| 1314 | NULLABILITY.resolve( | |
| 1315 | _with_tuple_element_names(_type_mapper.get_type(a.parameter_type), arg_attributes), | |
| 1316 | arg_attributes, | |
| 1317 | method, | |
| 1318 | a.parameter_type | |
| 1319 | )!, | |
| 1320 | arg_attributes | |
| 1321 | ) | |
| 1322 | ) | |
| 1323 | argument_defaults.add(_default_for_parameter(a)) | |
| 1324 | argument_reads.add(!(a.is_out /\ !a.is_in)) | |
| 1325 | argument_writes.add(!(a.is_in /\ !a.is_out)) | |
| 1326 | if a.is_out \/ a.is_in then | |
| 1327 | has_direction_override = true | |
| 1328 | fi | |
| 1329 | od | |
| 1330 | ||
| 1331 | result.argument_is_pack = argument_is_pack | |
| 1332 | result.argument_pack_depth = argument_pack_depth | |
| 1333 | result.spread_argument_index = spread_argument_index | |
| 1334 | ||
| 1335 | let return_pack_depth = _pack_depth(method.get_custom_attributes_data(), RETURN_PACK_ATTRIBUTE_NAME) | |
| 1336 | ||
| 1337 | if return_pack_depth >= 0 then | |
| 1338 | result.return_pack_depth = return_pack_depth | |
| 1339 | fi | |
| 1340 | result.argument_names = argument_names | |
| 1341 | result.arguments = argument_types | |
| 1342 | result.argument_defaults = argument_defaults | |
| 1343 | ||
| 1344 | if has_direction_override then | |
| 1345 | result.set_argument_directions(argument_reads, argument_writes) | |
| 1346 | fi | |
| 1347 | ||
| 1348 | let return_attributes = method.return_parameter.get_custom_attributes_data() | |
| 1349 | result.return_type = | |
| 1350 | _with_pure_function( | |
| 1351 | NULLABILITY.resolve( | |
| 1352 | _with_tuple_element_names( | |
| 1353 | _type_mapper.get_type(method.return_type), | |
| 1354 | return_attributes | |
| 1355 | ), | |
| 1356 | return_attributes, | |
| 1357 | method, | |
| 1358 | method.return_type | |
| 1359 | )!, | |
| 1360 | return_attributes | |
| 1361 | ) | |
| 1362 | ||
| 1363 | result.mark_pack_type_parameters() | |
| 1364 | ||
| 1365 | // When inheriting a method from a constructed-generic base in another | |
| 1366 | // assembly, reflection returns parameter and return types in their | |
| 1367 | // *substituted* form (e.g. !0 surfaces as System.Object when the base | |
| 1368 | // is FooBase<object,...>). The CLR requires the methodref signature | |
| 1369 | // to use the open-generic form (!0, !!0). Recover the open form via | |
| 1370 | // the open base type and stash it as unspecialized_*; the IL emitter | |
| 1371 | // already prefers unspecialized_* when present. | |
| 1372 | let dt = method.declaring_type | |
| 1373 | if dt? /\ dt != type /\ dt.is_generic_type /\ !dt.is_generic_type_definition then | |
| 1374 | let open_type = dt.get_generic_type_definition() | |
| 1375 | let open_method: MethodInfo? mut = null | |
| 1376 | ||
| 1377 | for m in open_type.get_members(cast BindingFlags(2 + 4 + 8 + 16 + 32)) do | |
| 1378 | if m.member_type == MemberTypes.METHOD then | |
| 1379 | let candidate = cast MethodInfo?(m)! | |
| 1380 | if candidate.metadata_token == method.metadata_token then | |
| 1381 | open_method = candidate | |
| 1382 | break | |
| 1383 | fi | |
| 1384 | fi | |
| 1385 | od | |
| 1386 | ||
| 1387 | if open_method? then | |
| 1388 | let unspec_arg_types = LIST() | |
| 1389 | for a in open_method.get_parameters() do | |
| 1390 | unspec_arg_types.add(_type_mapper.get_type(a.parameter_type)) | |
| 1391 | od | |
| 1392 | result.unspecialized_arguments = unspec_arg_types | |
| 1393 | result.unspecialized_return_type = _type_mapper.get_type(open_method.return_type) | |
| 1394 | fi | |
| 1395 | fi | |
| 1396 | ||
| 1397 | // property_details is only set for a property accessor method: | |
| 1398 | // most of the branches above never touch it, so it reaches here | |
| 1399 | // unassigned on every other path. | |
| 1400 | @suppress("presence-test-non-optional", "definite-assignment") | |
| 1401 | if property_details? then | |
| 1402 | let accessor_method_name = method.name | |
| 1403 | ||
| 1404 | if method.equals(property_details.dotnet_get_method) then | |
| 1405 | property_details.ghul_property.read_function = result | |
| 1406 | property_details.ghul_property.read_function_il_name_override = method.name | |
| 1407 | elif method.equals(property_details.dotnet_set_method) then | |
| 1408 | property_details.ghul_property.assign_function = result | |
| 1409 | property_details.ghul_property.assign_function_il_name_override = method.name | |
| 1410 | fi | |
| 1411 | fi | |
| 1412 | ||
| 1413 | methods.add((result, method)) | |
| 1414 | ||
| 1415 | // `IEquatable[T].Equals` and `IComparable[T].CompareTo` are | |
| 1416 | // reached as the operators `=~` and `<>`, and also under the | |
| 1417 | // ordinary de-camelled name, so a call spelled `equals` or | |
| 1418 | // `compare_to` resolves as well. Both symbols carry the same | |
| 1419 | // `il_name_override`, so either spelling emits a call to the | |
| 1420 | // one underlying method. | |
| 1421 | if alias_name? then | |
| 1422 | add_method(owner, type, properties, indexers, indexer_accessors, methods, method, alias_name) | |
| 1423 | fi | |
| 1424 | si | |
| 1425 | ||
| 1426 | // The type an intrinsic operator is registered on: the type of its | |
| 1427 | // first operand, for the operators a type declares for itself. | |
| 1428 | // Null for every other intrinsic, which stays global. | |
| 1429 | _intrinsic_home(name: string, method: MethodInfo) -> Symbols.Classy? is | |
| 1430 | if !Symbols.TYPE_HOMED_OPERATORS.contains(name) then | |
| 1431 | return null | |
| 1432 | fi | |
| 1433 | ||
| 1434 | let parameters = method.get_parameters() | |
| 1435 | ||
| 1436 | if parameters.count == 0 then | |
| 1437 | return null | |
| 1438 | fi | |
| 1439 | ||
| 1440 | if let first: Semantic.Types.NAMED = _type_mapper.get_type(parameters[0].parameter_type) then | |
| 1441 | return cast Symbols.Classy?(first.symbol) | |
| 1442 | fi | |
| 1443 | ||
| 1444 | return null | |
| 1445 | si | |
| 1446 | ||
| 1447 | // Whether a type's public static operator methods import under | |
| 1448 | // their operator spelling. The scalar types do not: their | |
| 1449 | // operators are the intrinsics, and a reflected `op_Addition` on | |
| 1450 | // `System.Decimal` or `System.IntPtr` would be a second candidate | |
| 1451 | // for the same operands. | |
| 1452 | _declares_own_operators(type: TYPE) -> bool => | |
| 1453 | !type.is_interface /\ !_intrinsic_operator_types.contains(type) | |
| 1454 | ||
| 1455 | // The ghūl spelling of a CLR operator-method name, or null for a | |
| 1456 | // method that is not one. Consulted for a static virtual interface | |
| 1457 | // member and for a public static operator on an ordinary type; a | |
| 1458 | // scalar type's reflected operator keeps its mangled name, since | |
| 1459 | // the intrinsic is that type's operator. | |
| 1460 | // | |
| 1461 | // The arithmetic, bitwise and shift operators. The comparison and | |
| 1462 | // equality operators are deliberately absent: ghūl derives those | |
| 1463 | // from `<>` and `=~`, and a bound that supplies these operators | |
| 1464 | // supplies those too, so importing `op_LessThan` as `<` would | |
| 1465 | // give the same expression a second, unrelated route to a | |
| 1466 | // different member. | |
| 1467 | _generic_math_operator_name(dotnet_name: string) -> string? => | |
| 1468 | case dotnet_name | |
| 1469 | when "op_Addition" then "+" | |
| 1470 | when "op_Subtraction" then "-" | |
| 1471 | when "op_Multiply" then "*" | |
| 1472 | when "op_Division" then "/" | |
| 1473 | when "op_Modulus" then "%" | |
| 1474 | when "op_UnaryNegation" then "-" | |
| 1475 | when "op_BitwiseAnd" then "&" | |
| 1476 | when "op_BitwiseOr" then "|" | |
| 1477 | when "op_ExclusiveOr" then "^" | |
| 1478 | when "op_OnesComplement" then "\\" | |
| 1479 | when "op_LeftShift" then "<<" | |
| 1480 | when "op_RightShift" then ">>" | |
| 1481 | when "op_UnsignedRightShift" then ">>>" | |
| 1482 | else null | |
| 1483 | esac | |
| 1484 | ||
| 1485 | _plain_member_name( | |
| 1486 | owner: Symbols.Scoped, | |
| 1487 | type: TYPE, | |
| 1488 | indexers: SET[System.Reflection.MethodInfo], | |
| 1489 | method: MethodInfo | |
| 1490 | ) -> string => | |
| 1491 | _ghul_name_override(method) ?? | |
| 1492 | _type_name_map.get_member_name( | |
| 1493 | owner.qualified_name, | |
| 1494 | method.name, | |
| 1495 | method.is_private, | |
| 1496 | method.is_special_name /\ !indexers.contains(method), | |
| 1497 | method.declaring_type ?? type | |
| 1498 | ) | |
| 1499 | ||
| 1500 | get_assembly_name(type: TYPE) -> string is | |
| 1501 | let assembly = type.assembly | |
| 1502 | ||
| 1503 | let result: string mut | |
| 1504 | ||
| 1505 | if _assembly_names.try_get_value(assembly, result ref) then | |
| 1506 | return result | |
| 1507 | fi | |
| 1508 | ||
| 1509 | result = assembly.get_name().name ?? "" | |
| 1510 | ||
| 1511 | _assembly_names.add(assembly, result) | |
| 1512 | ||
| 1513 | return result | |
| 1514 | si | |
| 1515 | ||
| 1516 | // C# records TupleElementNamesAttribute on the property itself; | |
| 1517 | // a ghūl assembly built before the property carried it holds | |
| 1518 | // the names only on the getter's `.param [0]`, which reflects | |
| 1519 | // as the return parameter rather than as a method attribute. | |
| 1520 | // Every reflected property reaches here, so neither carrier is | |
| 1521 | // read until the type is known to be a tuple. | |
| 1522 | _with_property_tuple_element_names( | |
| 1523 | type: Types.Type, | |
| 1524 | property: PropertyInfo | |
| 1525 | ) -> Types.Type is | |
| 1526 | let names = | |
| 1527 | TUPLE_ELEMENT_NAMES.read(property.get_custom_attributes_data()) ?? | |
| 1528 | (if let getter = property.get_get_method() then | |
| 1529 | TUPLE_ELEMENT_NAMES.read(getter.return_parameter.get_custom_attributes_data()) | |
| 1530 | else | |
| 1531 | null | |
| 1532 | fi) | |
| 1533 | ||
| 1534 | if let found = names then | |
| 1535 | return TUPLE_ELEMENT_NAMES.apply(type, found) | |
| 1536 | fi | |
| 1537 | ||
| 1538 | return type | |
| 1539 | si | |
| 1540 | ||
| 1541 | // If `attributes` carry a TupleElementNamesAttribute, return | |
| 1542 | // `type` with the recovered names put back on every tuple in it, | |
| 1543 | // however deep; otherwise return `type` unchanged. The reflected | |
| 1544 | // ValueTuple type itself has no names. | |
| 1545 | _with_tuple_element_names( | |
| 1546 | type: Types.Type, | |
| 1547 | attributes: Collections.Iterable[System.Reflection.CustomAttributeData] | |
| 1548 | ) -> Types.Type is | |
| 1549 | let names = TUPLE_ELEMENT_NAMES.read(attributes) | |
| 1550 | ||
| 1551 | if !names? then | |
| 1552 | return type | |
| 1553 | fi | |
| 1554 | ||
| 1555 | return TUPLE_ELEMENT_NAMES.apply(type, names) | |
| 1556 | si | |
| 1557 | ||
| 1558 | // The name `GHUL_NAME_ATTRIBUTE` carries, when a member is | |
| 1559 | // marked with one — an override or interface implementation | |
| 1560 | // whose IL name the base or interface fixed, so the IL name | |
| 1561 | // itself cannot be de-camelled back to the ghūl name the way | |
| 1562 | // an ordinary member's can. Read in preference to any name | |
| 1563 | // computed from the IL name. | |
| 1564 | _ghul_name_override(member: MemberInfo) -> string? is | |
| 1565 | try | |
| 1566 | for attr in member.get_custom_attributes_data() do | |
| 1567 | if attr.attribute_type.full_name =~ "Ghul.Internal.GHUL_NAME_ATTRIBUTE" then | |
| 1568 | return cast string?(attr.constructor_arguments[0].value) | |
| 1569 | fi | |
| 1570 | od | |
| 1571 | catch ex: System.Exception | |
| 1572 | yrt | |
| 1573 | ||
| 1574 | return null | |
| 1575 | si | |
| 1576 | ||
| 1577 | has_union_attribute(type: TYPE) -> bool is | |
| 1578 | try | |
| 1579 | for attr in type.get_custom_attributes_data() do | |
| 1580 | let attr_type = attr.attribute_type | |
| 1581 | ||
| 1582 | if attr_type.full_name =~ "Ghul.Internal.UNION_ATTRIBUTE" then | |
| 1583 | return true | |
| 1584 | fi | |
| 1585 | od | |
| 1586 | catch ex: System.Exception | |
| 1587 | yrt | |
| 1588 | ||
| 1589 | return false | |
| 1590 | si | |
| 1591 | ||
| 1592 | has_variant_attribute(type: TYPE) -> bool is | |
| 1593 | try | |
| 1594 | for attr in type.get_custom_attributes_data() do | |
| 1595 | let attr_type = attr.attribute_type | |
| 1596 | ||
| 1597 | if attr_type.full_name =~ "Ghul.Internal.VARIANT_ATTRIBUTE" then | |
| 1598 | return true | |
| 1599 | fi | |
| 1600 | od | |
| 1601 | catch ex: System.Exception | |
| 1602 | yrt | |
| 1603 | ||
| 1604 | return false | |
| 1605 | si | |
| 1606 | ||
| 1607 | has_default_variant_attribute(type: TYPE) -> bool is | |
| 1608 | try | |
| 1609 | for attr in type.get_custom_attributes_data() do | |
| 1610 | let attr_type = attr.attribute_type | |
| 1611 | ||
| 1612 | if attr_type.full_name =~ "Ghul.Internal.DEFAULT_VARIANT_ATTRIBUTE" then | |
| 1613 | return true | |
| 1614 | fi | |
| 1615 | od | |
| 1616 | catch ex: System.Exception | |
| 1617 | yrt | |
| 1618 | ||
| 1619 | return false | |
| 1620 | si | |
| 1621 | ||
| 1622 | // Resolving an attribute's type reaches for the assembly that | |
| 1623 | // declares it, so a marker this compiler emits against a runtime | |
| 1624 | // too old to declare it throws rather than answering. Treated as | |
| 1625 | // absent: the slot then reads the way it did before the marker | |
| 1626 | // existed. | |
| 1627 | ARGUMENT_PACK_ATTRIBUTE_NAME: string static => | |
| 1628 | "Ghul.Internal.ARGUMENT_PACK_ATTRIBUTE" | |
| 1629 | ||
| 1630 | RETURN_PACK_ATTRIBUTE_NAME: string static => | |
| 1631 | "Ghul.Internal.RETURN_PACK_ATTRIBUTE" | |
| 1632 | ||
| 1633 | ARGUMENT_SPREAD_ATTRIBUTE_NAME: string static => | |
| 1634 | "Ghul.Internal.ARGUMENT_SPREAD_ATTRIBUTE" | |
| 1635 | ||
| 1636 | // How deep in the parameter's own type the argument-pack marker | |
| 1637 | // sits, and negative when the parameter carries no marker at | |
| 1638 | // all. The argumentless form of the attribute is depth zero - | |
| 1639 | // the parameter's own function type - which is what an assembly | |
| 1640 | // built before the marker could be written deeper records. | |
| 1641 | _argument_pack_depth(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> int => | |
| 1642 | _pack_depth(attributes, ARGUMENT_PACK_ATTRIBUTE_NAME) | |
| 1643 | ||
| 1644 | _pack_depth( | |
| 1645 | attributes: Collections.Iterable[System.Reflection.CustomAttributeData], | |
| 1646 | attribute_name: string | |
| 1647 | ) -> int is | |
| 1648 | try | |
| 1649 | for attr in attributes do | |
| 1650 | if attr.attribute_type.full_name =~ attribute_name then | |
| 1651 | for argument in attr.constructor_arguments do | |
| 1652 | if let depth = cast int?(argument.value) then | |
| 1653 | return depth | |
| 1654 | fi | |
| 1655 | od | |
| 1656 | ||
| 1657 | return 0 | |
| 1658 | fi | |
| 1659 | od | |
| 1660 | catch ex: System.Exception | |
| 1661 | yrt | |
| 1662 | ||
| 1663 | return -1 | |
| 1664 | si | |
| 1665 | ||
| 1666 | _has_attribute_named(attributes: Collections.Iterable[System.Reflection.CustomAttributeData], name: string) -> bool is | |
| 1667 | try | |
| 1668 | for attr in attributes do | |
| 1669 | if attr.attribute_type.full_name =~ name then | |
| 1670 | return true | |
| 1671 | fi | |
| 1672 | od | |
| 1673 | catch ex: System.Exception | |
| 1674 | yrt | |
| 1675 | ||
| 1676 | return false | |
| 1677 | si | |
| 1678 | ||
| 1679 | _has_attribute(attributes: Collections.Iterable[System.Reflection.CustomAttributeData], full_name: string) -> bool is | |
| 1680 | try | |
| 1681 | for attr in attributes do | |
| 1682 | if attr.attribute_type.full_name =~ full_name then | |
| 1683 | return true | |
| 1684 | fi | |
| 1685 | od | |
| 1686 | catch ex: System.Exception | |
| 1687 | yrt | |
| 1688 | ||
| 1689 | return false | |
| 1690 | si | |
| 1691 | ||
| 1692 | _has_pure_attribute(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> bool is | |
| 1693 | try | |
| 1694 | for attr in attributes do | |
| 1695 | let attr_type = attr.attribute_type | |
| 1696 | ||
| 1697 | if attr_type.full_name =~ "Ghul.Internal.PURE_ATTRIBUTE" then | |
| 1698 | return true | |
| 1699 | fi | |
| 1700 | od | |
| 1701 | catch ex: System.Exception | |
| 1702 | yrt | |
| 1703 | ||
| 1704 | return false | |
| 1705 | si | |
| 1706 | ||
| 1707 | // What a System.ObsoleteAttribute on a member says: its message, if it | |
| 1708 | // has one, and whether a use is an error. Null when there is none. | |
| 1709 | _obsolete_attribute(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> Semantic.DEPRECATION? is | |
| 1710 | try | |
| 1711 | for attr in attributes do | |
| 1712 | if attr.attribute_type.full_name =~ "System.ObsoleteAttribute" then | |
| 1713 | let arguments = attr.constructor_arguments | |
| 1714 | ||
| 1715 | let message = | |
| 1716 | if arguments.count > 0 then cast string?(arguments[0].value) else null fi | |
| 1717 | ||
| 1718 | let is_error = | |
| 1719 | if arguments.count > 1 then | |
| 1720 | if let flag: bool = arguments[1].value then flag else false fi | |
| 1721 | else | |
| 1722 | false | |
| 1723 | fi | |
| 1724 | ||
| 1725 | return Semantic.DEPRECATION(message, is_error) | |
| 1726 | fi | |
| 1727 | od | |
| 1728 | catch ex: System.Exception | |
| 1729 | yrt | |
| 1730 | ||
| 1731 | return null | |
| 1732 | si | |
| 1733 | ||
| 1734 | // The text of a `///` doc comment the declaring assembly carried | |
| 1735 | // across, or null. | |
| 1736 | _doc_attribute(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> string? is | |
| 1737 | try | |
| 1738 | for attr in attributes do | |
| 1739 | if | |
| 1740 | attr.attribute_type.full_name =~ "Ghul.Internal.DOC_ATTRIBUTE" /\ | |
| 1741 | attr.constructor_arguments.count == 1 | |
| 1742 | then | |
| 1743 | return cast string?(attr.constructor_arguments[0].value) | |
| 1744 | fi | |
| 1745 | od | |
| 1746 | catch ex: System.Exception | |
| 1747 | yrt | |
| 1748 | ||
| 1749 | return null | |
| 1750 | si | |
| 1751 | ||
| 1752 | _has_stable_attribute(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> bool is | |
| 1753 | try | |
| 1754 | for attr in attributes do | |
| 1755 | let attr_type = attr.attribute_type | |
| 1756 | ||
| 1757 | if attr_type.full_name =~ "Ghul.Internal.STABLE_ATTRIBUTE" then | |
| 1758 | return true | |
| 1759 | fi | |
| 1760 | od | |
| 1761 | catch ex: System.Exception | |
| 1762 | yrt | |
| 1763 | ||
| 1764 | return false | |
| 1765 | si | |
| 1766 | ||
| 1767 | // The BCL's own marker for a synthesised member or type — see | |
| 1768 | // SRM_STRUCTURE_WALK.COMPILER_GENERATED_ATTRIBUTE_NAME for the | |
| 1769 | // emission side. Reading it back means an importing compilation | |
| 1770 | // recognises a synthesised symbol without testing its name, | |
| 1771 | // whether the assembly it came from is ghūl-compiled or not — | |
| 1772 | // a C#-compiled record's synthesised members carry it too. | |
| 1773 | _has_compiler_generated_attribute(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> bool is | |
| 1774 | try | |
| 1775 | for attr in attributes do | |
| 1776 | let attr_type = attr.attribute_type | |
| 1777 | ||
| 1778 | if attr_type.full_name =~ "System.Runtime.CompilerServices.CompilerGeneratedAttribute" then | |
| 1779 | return true | |
| 1780 | fi | |
| 1781 | od | |
| 1782 | catch ex: System.Exception | |
| 1783 | yrt | |
| 1784 | ||
| 1785 | return false | |
| 1786 | si | |
| 1787 | ||
| 1788 | // True when `method` is `type`'s implementation of the | |
| 1789 | // self-instantiated generic interface named by `open_interface_name` | |
| 1790 | // (`System.IEquatable\`1` or `System.IComparable\`1`) — i.e. `type` | |
| 1791 | // implements `IEquatable[type]`/`IComparable[type]`, not some other | |
| 1792 | // instantiation, and `method` is that interface member rather than | |
| 1793 | // an unrelated same-named overload (`object.Equals`, the | |
| 1794 | // non-generic `IComparable.CompareTo(object)`). | |
| 1795 | // | |
| 1796 | // `Type.GetInterfaceMap` would answer this directly but throws | |
| 1797 | // `NotSupportedException` under the `MetadataLoadContext` this | |
| 1798 | // reflection runs in (import assemblies are loaded for metadata | |
| 1799 | // only, never executed), so this matches by signature instead: | |
| 1800 | // `IEquatable[T].Equals`/`IComparable[T].CompareTo` are always | |
| 1801 | // exactly `(T) -> bool`/`(T) -> int`, and no other member can | |
| 1802 | // satisfy that interface with a different parameter type. The one | |
| 1803 | // gap this leaves is a type that implements the interface | |
| 1804 | // *explicitly* (`bool IEquatable<Box>.Equals(Box other)`) — that | |
| 1805 | // member doesn't surface under its simple name in `GetMethods()` | |
| 1806 | // at all, with or without `GetInterfaceMap`, so it's an existing | |
| 1807 | // reflection-layer limitation rather than one this check adds. | |
| 1808 | _is_self_relational_interface_target(type: TYPE, method: MethodInfo, open_interface: TYPE, expected_return_type: TYPE) -> bool is | |
| 1809 | if method.return_type != expected_return_type then | |
| 1810 | return false | |
| 1811 | fi | |
| 1812 | ||
| 1813 | let parameters = method.get_parameters() | |
| 1814 | ||
| 1815 | if parameters.count != 1 then | |
| 1816 | return false | |
| 1817 | fi | |
| 1818 | ||
| 1819 | // `IEquatable[T].=~`/`IComparable[T].<>` on the trait's own | |
| 1820 | // declaration: `type` is the open interface itself (reflecting | |
| 1821 | // `Ghul.Equatable[T]`'s member list, not a concrete | |
| 1822 | // implementer), and the parameter is its own type parameter. | |
| 1823 | if type == open_interface then | |
| 1824 | return parameters[0].parameter_type == open_interface.get_generic_arguments()[0] | |
| 1825 | fi | |
| 1826 | ||
| 1827 | // An implementer: `type` closed-implements `IEquatable[type]` | |
| 1828 | // and the parameter is `type` itself. | |
| 1829 | let closed_interface = open_interface.make_generic_type([type]) | |
| 1830 | ||
| 1831 | return type.get_interfaces() |> any(i => i == closed_interface) /\ | |
| 1832 | parameters[0].parameter_type == type | |
| 1833 | si | |
| 1834 | ||
| 1835 | // The intrinsic operation name (e.g. "arithmetic.add") carried by | |
| 1836 | // a method's INTRINSIC_ATTRIBUTE, or null if it carries none. An | |
| 1837 | // imported method bearing the marker is reconstructed as an innate | |
| 1838 | // function whose calls lower to IL opcodes, matching a source- | |
| 1839 | // declared intrinsic. | |
| 1840 | _intrinsic_operation(attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> string? => | |
| 1841 | INTRINSIC_OPERATION.of(attributes) | |
| 1842 | ||
| 1843 | // The constructors the runtime supplies for a type that has none of | |
| 1844 | // its own for the argument, as `init` overloads calling the runtime | |
| 1845 | // method in place of a newobj. Their parameters are the method's, | |
| 1846 | // with its type parameters read as the type's own where the type has | |
| 1847 | // them, and otherwise kept as the constructor's own, inferred from | |
| 1848 | // the arguments. | |
| 1849 | _add_constructor_intrinsics(owner: Symbols.Scoped, type: TYPE) is | |
| 1850 | let methods = | |
| 1851 | if _intrinsic_operation(type.get_custom_attributes_data()) =~ "type.array" then | |
| 1852 | _type_details_lookup.array_constructor_intrinsics | |
| 1853 | else | |
| 1854 | _type_details_lookup.constructor_intrinsics_for(type) | |
| 1855 | fi | |
| 1856 | ||
| 1857 | for method in methods do | |
| 1858 | let target = _find_reflected_function(method) | |
| 1859 | ||
| 1860 | if !target? then | |
| 1861 | continue | |
| 1862 | fi | |
| 1863 | ||
| 1864 | let shaped = | |
| 1865 | if method.is_generic_method_definition /\ type.is_generic_type_definition then | |
| 1866 | method.make_generic_method(type.get_generic_arguments()) | |
| 1867 | else | |
| 1868 | method | |
| 1869 | fi | |
| 1870 | ||
| 1871 | let location = Source.LOCATION.reflected | |
| 1872 | let result = Symbols.INSTANCE_METHOD(location, location, owner, "init", owner) | |
| 1873 | ||
| 1874 | let argument_names = LIST[string]() | |
| 1875 | let argument_types = LIST[Type]() | |
| 1876 | ||
| 1877 | for parameter in shaped.get_parameters() do | |
| 1878 | argument_names.add(parameter.name ?? "") | |
| 1879 | argument_types.add(_type_mapper.get_type(parameter.parameter_type)) | |
| 1880 | od | |
| 1881 | ||
| 1882 | result.argument_names = argument_names | |
| 1883 | result.arguments = argument_types | |
| 1884 | result.return_type = _type_mapper.get_type(_void_type) | |
| 1885 | result.constructor_target = target | |
| 1886 | ||
| 1887 | if target.is_declared_pure then | |
| 1888 | result.mark_declared_pure() | |
| 1889 | fi | |
| 1890 | ||
| 1891 | if shaped.is_generic_method_definition then | |
| 1892 | let generic_argument_names = LIST[string]() | |
| 1893 | let generic_arguments = LIST[Type]() | |
| 1894 | ||
| 1895 | for parameter in shaped.get_generic_arguments() do | |
| 1896 | generic_argument_names.add(parameter.name) | |
| 1897 | generic_arguments.add(_type_mapper.get_type(parameter)) | |
| 1898 | od | |
| 1899 | ||
| 1900 | result.is_generic = true | |
| 1901 | result.generic_argument_names = generic_argument_names | |
| 1902 | result.generic_arguments = generic_arguments | |
| 1903 | fi | |
| 1904 | result.mark_synthesized() | |
| 1905 | ||
| 1906 | owner.add_member(result) | |
| 1907 | od | |
| 1908 | si | |
| 1909 | ||
| 1910 | _find_reflected_function(method: System.Reflection.MethodInfo) -> Symbols.Function? is | |
| 1911 | let carrier_type = method.declaring_type | |
| 1912 | ||
| 1913 | if !carrier_type? then | |
| 1914 | return null | |
| 1915 | fi | |
| 1916 | ||
| 1917 | let carrier = cast Symbols.Scoped?(_symbol_table.get_symbol(carrier_type)) | |
| 1918 | ||
| 1919 | if !carrier? then | |
| 1920 | return null | |
| 1921 | fi | |
| 1922 | ||
| 1923 | let group = cast Symbols.FUNCTION_GROUP?(carrier.find_direct(method.name)) | |
| 1924 | ||
| 1925 | if !group? then | |
| 1926 | return null | |
| 1927 | fi | |
| 1928 | ||
| 1929 | let parameter_count = method.get_parameters().count | |
| 1930 | ||
| 1931 | for function in group.functions do | |
| 1932 | if function.arguments.count == parameter_count then | |
| 1933 | return function | |
| 1934 | fi | |
| 1935 | od | |
| 1936 | ||
| 1937 | return null | |
| 1938 | si | |
| 1939 | ||
| 1940 | // A parameter or return slot carrying the purity marker has a | |
| 1941 | // pure top-level function type: rebuild the mapped type in | |
| 1942 | // its pure shape. Slots whose mapped type is not a function | |
| 1943 | // type (or is the zero-argument void form, which has no pure | |
| 1944 | // shape) keep the plain type — conservative on both sides. | |
| 1945 | _with_pure_function(type: Types.Type, attributes: Collections.Iterable[System.Reflection.CustomAttributeData]) -> Types.Type is | |
| 1946 | if !type.is_function \/ !isa Types.GENERIC(type) \/ !_has_pure_attribute(attributes) then | |
| 1947 | return type | |
| 1948 | fi | |
| 1949 | ||
| 1950 | let lookup = IoC.CONTAINER.instance.innate_symbol_lookup | |
| 1951 | ||
| 1952 | let types = Collections.LIST[Types.Type]() | |
| 1953 | ||
| 1954 | for a in (cast Types.GENERIC(type)).arguments do | |
| 1955 | types.add(a) | |
| 1956 | od | |
| 1957 | ||
| 1958 | if type.is_action then | |
| 1959 | types.add(lookup.get_void_type()) | |
| 1960 | fi | |
| 1961 | ||
| 1962 | return lookup.get_function_type(types, true) | |
| 1963 | si | |
| 1964 | ||
| 1965 | // A delegate type, found by walking the base chain to | |
| 1966 | // System.MulticastDelegate and comparing reflected types by | |
| 1967 | // identity. What a delegate member does is whatever its target | |
| 1968 | // does, so nothing about its shape can vouch for it. | |
| 1969 | _inherits_multicast_delegate(type: TYPE) -> bool is | |
| 1970 | let current mut = type.base_type | |
| 1971 | ||
| 1972 | while current? do | |
| 1973 | if current == _multicast_delegate_type then | |
| 1974 | return true | |
| 1975 | fi | |
| 1976 | ||
| 1977 | current = current.base_type | |
| 1978 | od | |
| 1979 | ||
| 1980 | return false | |
| 1981 | si | |
| 1982 | ||
| 1983 | has_closed_attribute(type: TYPE) -> bool is | |
| 1984 | try | |
| 1985 | for attr in type.get_custom_attributes_data() do | |
| 1986 | let attr_type = attr.attribute_type | |
| 1987 | ||
| 1988 | if attr_type.full_name =~ "Ghul.Internal.CLOSED_ATTRIBUTE" then | |
| 1989 | return true | |
| 1990 | fi | |
| 1991 | od | |
| 1992 | catch ex: System.Exception | |
| 1993 | yrt | |
| 1994 | ||
| 1995 | return false | |
| 1996 | si | |
| 1997 | ||
| 1998 | // Cross-assembly variant materialization: when reflecting a | |
| 1999 | // union from another assembly, the variant classes are | |
| 2000 | // sibling top-level types whose `Namespace` equals the | |
| 2001 | // union's full name (assemblies.ghul: import_type queues | |
| 2002 | // them into TYPE_DETAILS_LOOKUP by union full name instead | |
| 2003 | // of registering them as top-level). When the union's | |
| 2004 | // symbol is created and its members populated, call this | |
| 2005 | // to also materialize the variants as members of the union. | |
| 2006 | materialize_variants(union_symbol: Symbols.UNION, union_type: TYPE) is | |
| 2007 | let union_full_name = union_type.full_name | |
| 2008 | ||
| 2009 | if !union_full_name? then | |
| 2010 | return | |
| 2011 | fi | |
| 2012 | ||
| 2013 | let variants = _type_details_lookup.get_variants_for_union(union_full_name) | |
| 2014 | ||
| 2015 | if !variants? then | |
| 2016 | return | |
| 2017 | fi | |
| 2018 | ||
| 2019 | for variant_type in variants do | |
| 2020 | materialize_variant(union_symbol, variant_type) | |
| 2021 | od | |
| 2022 | si | |
| 2023 | ||
| 2024 | materialize_variant(union_symbol: Symbols.UNION, variant_type: TYPE) is | |
| 2025 | let variant_name = variant_type.name | |
| 2026 | let assembly_name = get_assembly_name(variant_type) | |
| 2027 | ||
| 2028 | let arguments = LIST() | |
| 2029 | let argument_variances = LIST() | |
| 2030 | ||
| 2031 | if variant_type.is_generic_type /\ variant_type.contains_generic_parameters then | |
| 2032 | for a in variant_type.get_generic_arguments() do | |
| 2033 | if a.is_generic_type_parameter then | |
| 2034 | arguments.add(a.name) | |
| 2035 | argument_variances.add(_type_mapper.map_type_argument_variance(a)) | |
| 2036 | fi | |
| 2037 | od | |
| 2038 | fi | |
| 2039 | ||
| 2040 | let variant_symbol = | |
| 2041 | Symbols.VARIANT( | |
| 2042 | Source.LOCATION.reflected, | |
| 2043 | Source.LOCATION.reflected, | |
| 2044 | union_symbol, | |
| 2045 | variant_name, | |
| 2046 | arguments, | |
| 2047 | union_symbol | |
| 2048 | ) | |
| 2049 | ||
| 2050 | variant_symbol.mark_overrides_resolved() | |
| 2051 | ||
| 2052 | if argument_variances.count > 0 then | |
| 2053 | variant_symbol.argument_variances = argument_variances | |
| 2054 | fi | |
| 2055 | ||
| 2056 | variant_symbol.il_assembly_name = assembly_name | |
| 2057 | variant_symbol.il_name_override = get_il_name(variant_type) | |
| 2058 | variant_symbol.dotnet_full_name = variant_type.full_name | |
| 2059 | ||
| 2060 | // Register the variant in the lookup so subsequent | |
| 2061 | // dotnet-type-keyed lookups (`get_symbol(type)`) and | |
| 2062 | // resolution paths can find it. | |
| 2063 | let variant_td = | |
| 2064 | TYPE_DETAILS( | |
| 2065 | variant_type, | |
| 2066 | union_symbol.qualified_name, | |
| 2067 | variant_name, | |
| 2068 | variant_symbol.il_name_override, | |
| 2069 | assembly_name | |
| 2070 | ) | |
| 2071 | ||
| 2072 | union_symbol.add_member(variant_symbol) | |
| 2073 | ||
| 2074 | add_ancestors(variant_symbol, variant_type) | |
| 2075 | add_members(variant_symbol, variant_type) | |
| 2076 | resolve_overrides(variant_symbol) | |
| 2077 | ||
| 2078 | register_variant_field_names(variant_symbol, variant_type) | |
| 2079 | ||
| 2080 | // Wire the parent union's `default_variant` pointer to | |
| 2081 | // the marked variant so cross-asm `u?` / `u!` lowers to | |
| 2082 | // isa/cast against this variant — matching what source- | |
| 2083 | // defined unions get from declare_members. | |
| 2084 | if has_default_variant_attribute(variant_type) then | |
| 2085 | variant_symbol.is_default = true | |
| 2086 | union_symbol.default_variant = variant_symbol | |
| 2087 | fi | |
| 2088 | si | |
| 2089 | ||
| 2090 | // A variant's fields, in source order, are its constructor's | |
| 2091 | // parameters: a field spliced in from the union's header is | |
| 2092 | // stored on the union base rather than declared on the | |
| 2093 | // variant, so the variant's own public fields alone would make | |
| 2094 | // a variant carrying only header fields look like a unit | |
| 2095 | // variant. | |
| 2096 | register_variant_field_names(variant_symbol: Symbols.VARIANT, variant_type: TYPE) is | |
| 2097 | let own_fields = Collections.LIST[string]() | |
| 2098 | ||
| 2099 | for `field in variant_type.get_fields() do | |
| 2100 | if !`field.is_static /\ `field.is_public /\ `field.declaring_type == variant_type then | |
| 2101 | own_fields.add(`field.name) | |
| 2102 | fi | |
| 2103 | od | |
| 2104 | ||
| 2105 | let constructors = variant_type.get_constructors() | |
| 2106 | ||
| 2107 | if constructors.count != 1 then | |
| 2108 | for name in own_fields do | |
| 2109 | variant_symbol.register_reflected_field_name(name) | |
| 2110 | od | |
| 2111 | ||
| 2112 | return | |
| 2113 | fi | |
| 2114 | ||
| 2115 | for parameter in constructors[0].get_parameters() do | |
| 2116 | let name = parameter.name | |
| 2117 | ||
| 2118 | if !name? then | |
| 2119 | continue | |
| 2120 | fi | |
| 2121 | ||
| 2122 | if own_fields.contains(name) then | |
| 2123 | variant_symbol.register_reflected_field_name(name) | |
| 2124 | else | |
| 2125 | variant_symbol.register_inherited_primary_field_name(name) | |
| 2126 | fi | |
| 2127 | od | |
| 2128 | si | |
| 2129 | ||
| 2130 | // The CLR-side full name, with the nested-type separator the | |
| 2131 | // metadata reader expects: reflection spells a nested type | |
| 2132 | // `Outer+Inner`, and a type reference names it `Outer/Inner`. | |
| 2133 | get_il_name(type: TYPE) -> string is | |
| 2134 | let full_name: string? = type.full_name | |
| 2135 | ||
| 2136 | assert full_name? else "get il name type has no full name: {type}" | |
| 2137 | ||
| 2138 | return full_name.replace("+", "/") | |
| 2139 | si | |
| 2140 | ||
| 2141 | // Reflect the .NET-declared default value of a parameter into | |
| 2142 | // the string form that `Function.argument_defaults` expects. | |
| 2143 | // - null ⇒ no default ⇒ parameter is not omittable. | |
| 2144 | // - "default" ⇒ omittable, fill with `IR.Values.DEFAULT` (the | |
| 2145 | // .NET parameter is a reference type with `= null`, or a | |
| 2146 | // `Nullable<T>` with no constant value). | |
| 2147 | // - any other string ⇒ a constant literal, formatted with | |
| 2148 | // invariant culture so the emission side can read it back | |
| 2149 | // independent of host locale. | |
| 2150 | // | |
| 2151 | // Conservative gate: only primitive numeric types, bool, char, | |
| 2152 | // string, and enums are stored. Anything else (decimal, | |
| 2153 | // struct, …) returns null so the parameter is treated as not | |
| 2154 | // omittable rather than silently filled with a zero default. | |
| 2155 | _default_for_parameter(a: ParameterInfo) -> string? is | |
| 2156 | if !a.has_default_value then | |
| 2157 | return null | |
| 2158 | fi | |
| 2159 | ||
| 2160 | let raw = a.raw_default_value | |
| 2161 | ||
| 2162 | // This channel does spell an absent value as a word, because | |
| 2163 | // ghūl's own `= _` reaches it that way and a parameter | |
| 2164 | // default is read back by the same conversion. | |
| 2165 | if !raw? then | |
| 2166 | return "default" | |
| 2167 | fi | |
| 2168 | ||
| 2169 | return REFLECTED_CONSTANTS.text_of(raw, a.parameter_type) | |
| 2170 | si | |
| 2171 | si | |
| 2172 | si |