Appearance
| 1 | namespace IR.Program is | |
| 2 | use System.Text.StringBuilder | |
| 3 | ||
| 4 | use Semantic.Types.Type | |
| 5 | ||
| 6 | /// The names a whole-program target gives its types' descriptors, the | |
| 7 | /// full names .NET reports, so `get_type()` gives the same text on every | |
| 8 | /// target. | |
| 9 | class TYPE_NAMES is | |
| 10 | /// The name of `type`. | |
| 11 | /// | |
| 12 | /// - type: a class, array or box in `classes` | |
| 13 | /// - classes: the program's classes | |
| 14 | /// - string_type: the string type, or null where the program has none | |
| 15 | /// - scalar: the scalar kind a value of a type is, or null where it is not one | |
| 16 | /// - is_string: whether a type is `string` | |
| 17 | name_of( | |
| 18 | type: CLASS_INSTANCE, | |
| 19 | classes: CLASS_SET, | |
| 20 | string_type: CLASS_INSTANCE?, | |
| 21 | scalar: (Type?) -> Scalar?, | |
| 22 | is_string: (Type?) -> bool | |
| 23 | ) -> string static is | |
| 24 | if let boxed = classes.boxed_type(type) then | |
| 25 | return name_of(boxed, classes, string_type, scalar, is_string) | |
| 26 | elif type == string_type then | |
| 27 | return "System.String" | |
| 28 | elif type == classes.root then | |
| 29 | return "System.Object" | |
| 30 | elif classes.core.bigint? /\ type.definition == classes.core.bigint then | |
| 31 | return "System.Numerics.BigInteger" | |
| 32 | elif classes.is_array(type) then | |
| 33 | return "{name_of(classes.array_element(type), classes, string_type, scalar, is_string)}[]" | |
| 34 | elif type.arguments.count == 0 then | |
| 35 | return _emitted_name(type.definition) | |
| 36 | fi | |
| 37 | ||
| 38 | let result = StringBuilder() | |
| 39 | ||
| 40 | result.append(_emitted_name(type.definition)) | |
| 41 | result.append('[') | |
| 42 | ||
| 43 | for i in 0..type.arguments.count do | |
| 44 | if i > 0 then | |
| 45 | result.append(',') | |
| 46 | fi | |
| 47 | ||
| 48 | result.append(name_of(type.arguments[i], classes, string_type, scalar, is_string)) | |
| 49 | od | |
| 50 | ||
| 51 | result.append(']') | |
| 52 | ||
| 53 | return result.to_string() | |
| 54 | si | |
| 55 | ||
| 56 | // A type declared in a namespace is named as it is emitted on .NET, | |
| 57 | // which keeps a file-private namespace that a qualified name leaves | |
| 58 | // out, without the arity suffix a generic sharing its name with a | |
| 59 | // sibling carries: the type arguments follow in brackets instead. Any | |
| 60 | // other, such as a closure's frame, keeps its qualified name. | |
| 61 | _emitted_name(definition: Semantic.Symbols.Classy) -> string static is | |
| 62 | if !isa Semantic.Symbols.NAMESPACE(definition.owner) then | |
| 63 | return definition.qualified_name | |
| 64 | fi | |
| 65 | ||
| 66 | let name = definition.il_qualified_name | |
| 67 | let tick = name.last_index_of('`') | |
| 68 | ||
| 69 | return if tick > name.last_index_of('.') then name.substring(0, tick) else name fi | |
| 70 | si | |
| 71 | ||
| 72 | /// The name of `type`, any type a descriptor names. | |
| 73 | name_of( | |
| 74 | type: Type, | |
| 75 | classes: CLASS_SET, | |
| 76 | string_type: CLASS_INSTANCE?, | |
| 77 | scalar: (Type?) -> Scalar?, | |
| 78 | is_string: (Type?) -> bool | |
| 79 | ) -> string static is | |
| 80 | // An enum is held as its integer, but is named as itself. | |
| 81 | if let `enum: Semantic.Symbols.ENUM_STRUCT = type.symbol then | |
| 82 | return _emitted_name(`enum) | |
| 83 | fi | |
| 84 | ||
| 85 | if let kind = scalar(type) then | |
| 86 | return scalar_name(kind) | |
| 87 | elif is_string(type) then | |
| 88 | return "System.String" | |
| 89 | elif let held = classes.class_of(type) then | |
| 90 | return name_of(held, classes, string_type, scalar, is_string) | |
| 91 | fi | |
| 92 | ||
| 93 | return "{type}" | |
| 94 | si | |
| 95 | ||
| 96 | /// The .NET name of the scalar kind. | |
| 97 | scalar_name(scalar: Scalar) -> string static => | |
| 98 | case scalar | |
| 99 | when Scalar.BOOL then "System.Boolean" | |
| 100 | when Scalar.CHAR then "System.Char" | |
| 101 | when Scalar.INT8 then "System.SByte" | |
| 102 | when Scalar.UINT8 then "System.Byte" | |
| 103 | when Scalar.INT16 then "System.Int16" | |
| 104 | when Scalar.UINT16 then "System.UInt16" | |
| 105 | when Scalar.INT32 then "System.Int32" | |
| 106 | when Scalar.UINT32 then "System.UInt32" | |
| 107 | when Scalar.INT64 then "System.Int64" | |
| 108 | when Scalar.UINT64 then "System.UInt64" | |
| 109 | when Scalar.INT_NATIVE then "System.IntPtr" | |
| 110 | when Scalar.UINT_NATIVE then "System.UIntPtr" | |
| 111 | when Scalar.FLOAT32 then "System.Single" | |
| 112 | when Scalar.FLOAT64 then "System.Double" | |
| 113 | esac | |
| 114 | si | |
| 115 | si |