Appearance
| 1 | namespace Semantic.Symbols is | |
| 2 | use System.Text.StringBuilder | |
| 3 | ||
| 4 | use Logging | |
| 5 | use Source | |
| 6 | ||
| 7 | use IR.Values.Value | |
| 8 | ||
| 9 | use Types.Type | |
| 10 | ||
| 11 | // FIXME: doesn't really have a type, not sure how to represent this: | |
| 12 | class NAMESPACE: ScopedWithEnclosingScope, NamespaceContext, Types.Typed is | |
| 13 | qualified_name: string | |
| 14 | ||
| 15 | type: Type | |
| 16 | ||
| 17 | short_description: string => "namespace {name}" | |
| 18 | ||
| 19 | describe(context: DESCRIBE_CONTEXT) -> SignaturePart is | |
| 20 | let qn mut = qualified_name | |
| 21 | if qn.starts_with('.') then | |
| 22 | qn = qn.substring(1) | |
| 23 | fi | |
| 24 | return PARTS.literal("namespace {qn}") | |
| 25 | si | |
| 26 | ||
| 27 | symbol_kind: SymbolKind => SymbolKind.NAMESPACE | |
| 28 | completion_kind: CompletionKind => CompletionKind.MODULE | |
| 29 | ||
| 30 | is_namespace: bool => true | |
| 31 | is_classy: bool => false | |
| 32 | is_internal: bool => false | |
| 33 | is_compiler_generated: bool | |
| 34 | ||
| 35 | // Whether every compilation imports this namespace without asking, | |
| 36 | // which is true of `Ghul.Intrinsics` alone. A name it holds resolves | |
| 37 | // bare from anywhere, so qualifying one distinguishes nothing and | |
| 38 | // only costs the width a signature has to wrap at. Marked by | |
| 39 | // whatever resolves the namespace, so nothing has to recognise it | |
| 40 | // by what it is called. | |
| 41 | is_implicitly_imported: bool | |
| 42 | ||
| 43 | mark_implicitly_imported() is | |
| 44 | is_implicitly_imported = true | |
| 45 | si | |
| 46 | ||
| 47 | init( | |
| 48 | location: LOCATION, | |
| 49 | name: string, | |
| 50 | enclosing_scope: Scope?, | |
| 51 | qualified_name: string, | |
| 52 | is_compiler_generated: bool | |
| 53 | ) is | |
| 54 | super.init(location, self, name, enclosing_scope) | |
| 55 | ||
| 56 | self.qualified_name = qualified_name | |
| 57 | self.is_compiler_generated = is_compiler_generated | |
| 58 | ||
| 59 | type = Types.NAMED(self) | |
| 60 | si | |
| 61 | ||
| 62 | qualify(name: string) -> string is | |
| 63 | if is_compiler_generated then | |
| 64 | return name | |
| 65 | fi | |
| 66 | ||
| 67 | let q = qualified_name | |
| 68 | ||
| 69 | if q.length > 1 then | |
| 70 | return "{qualified_name.substring(1)}.{name}" | |
| 71 | fi | |
| 72 | ||
| 73 | return name | |
| 74 | si | |
| 75 | ||
| 76 | find_direct(name: string) -> Symbol? is | |
| 77 | let declared = super.find_direct(name) | |
| 78 | ||
| 79 | // A declaration of any other kind hides whatever an imported | |
| 80 | // assembly contributes under the name; a function joins the | |
| 81 | // imported overloads, as one declared in another file of this | |
| 82 | // compilation would. | |
| 83 | if declared? /\ !isa Function(declared) /\ !isa FUNCTION_GROUP(declared) then | |
| 84 | return declared | |
| 85 | fi | |
| 86 | ||
| 87 | let imported = _find_imported(name) | |
| 88 | ||
| 89 | if !declared? /\ !imported? then | |
| 90 | return _find_old_name(name) | |
| 91 | elif !declared? then | |
| 92 | return imported | |
| 93 | elif !imported? then | |
| 94 | return declared | |
| 95 | fi | |
| 96 | ||
| 97 | return DotNet.GLOBAL_OVERLOAD_MERGE.merge([declared, imported]) | |
| 98 | si | |
| 99 | ||
| 100 | // Where the core library declares the core types in source, an | |
| 101 | // old name such as `System.Exception` is the type it declares | |
| 102 | // under its ghūl name, and a namespace an old name lies in, such | |
| 103 | // as `System`, is a namespace, as the reflected ones are on .NET. | |
| 104 | _find_old_name(name: string) -> Symbol? is | |
| 105 | let container = IoC.CONTAINER.instance | |
| 106 | ||
| 107 | if !container.target_profile.has_source_core_library then | |
| 108 | return null | |
| 109 | fi | |
| 110 | ||
| 111 | let qn = qualified_name.trim_start(['.']) | |
| 112 | let full_name = if qn.length == 0 then name else "{qn}.{name}" fi | |
| 113 | ||
| 114 | if let renamed = CORE_TYPE_ALIASES.renamed_in_source(full_name) then | |
| 115 | for home in CORE_TYPE_ALIASES.homes(renamed) do | |
| 116 | let dot = home.last_index_of('.') | |
| 117 | ||
| 118 | if let owner = container.namespaces.try_find_namespace(".{home.substring(0, dot)}") then | |
| 119 | if let found = owner.find_direct(home.substring(dot + 1)) then | |
| 120 | return found | |
| 121 | fi | |
| 122 | fi | |
| 123 | od | |
| 124 | ||
| 125 | return null | |
| 126 | fi | |
| 127 | ||
| 128 | if CORE_TYPE_ALIASES.is_namespace_in_source(full_name) then | |
| 129 | return container.namespaces.find_or_add_namespace(name, ".{full_name}", false) | |
| 130 | fi | |
| 131 | ||
| 132 | return null | |
| 133 | si | |
| 134 | ||
| 135 | @IF.dotnet() | |
| 136 | _find_imported(name: string) -> Symbol? is | |
| 137 | let dotnet_symbol_table = IoC.CONTAINER.instance.dotnet_symbol_table.value | |
| 138 | ||
| 139 | let qn mut = qualified_name | |
| 140 | ||
| 141 | if qn =~ "." \/ qn.length == 0 then | |
| 142 | return null | |
| 143 | fi | |
| 144 | ||
| 145 | if qn.starts_with(".") then | |
| 146 | qn = qn.substring(1) | |
| 147 | fi | |
| 148 | ||
| 149 | if DotNet.HIDDEN_SYMBOLS.is_hidden_namespace(qn) then | |
| 150 | return null | |
| 151 | fi | |
| 152 | ||
| 153 | let direct = dotnet_symbol_table.get_symbol("{qn}.{name}") | |
| 154 | ||
| 155 | if direct? then | |
| 156 | return direct | |
| 157 | fi | |
| 158 | ||
| 159 | // Globals from a referenced assembly live as static members of | |
| 160 | // a synthetic `$globals` IL class. The .NET importer | |
| 161 | // materialises each one as a GLOBAL_FUNCTION / GLOBAL_VARIABLE / | |
| 162 | // GLOBAL_PROPERTY whose owner is *this* NAMESPACE - matching | |
| 163 | // how source-side globals are declared - but stashes them on | |
| 164 | // the Classy because that lookup-table outlives this | |
| 165 | // NAMESPACE's clear-between-compiles lifecycle. Resolving the | |
| 166 | // carrier name materialises every carrier the namespace has; | |
| 167 | // the member lookup then searches them all. | |
| 168 | let _ = dotnet_symbol_table.get_symbol("{qn}.{DotNet.GLOBALS_CARRIER.name}") | |
| 169 | ||
| 170 | return dotnet_symbol_table.find_global_member(qn, name) | |
| 171 | si | |
| 172 | ||
| 173 | @IF.not.dotnet() | |
| 174 | _find_imported(name: string) -> Symbol? => null | |
| 175 | ||
| 176 | // Mirrors `find_direct`: the declared symbols, and then the globals | |
| 177 | // an imported assembly contributes through this namespace's | |
| 178 | // `$globals` carriers. Without the second half a `use` of the | |
| 179 | // namespace makes every one of those names resolve unqualified | |
| 180 | // while offering none of them. | |
| 181 | find_direct_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is | |
| 182 | super.find_direct_matches(prefix, matches) | |
| 183 | ||
| 184 | let qn mut = qualified_name | |
| 185 | ||
| 186 | if qn =~ "." \/ qn.length == 0 then | |
| 187 | return | |
| 188 | fi | |
| 189 | ||
| 190 | if qn.starts_with(".") then | |
| 191 | qn = qn.substring(1) | |
| 192 | fi | |
| 193 | ||
| 194 | if DotNet.HIDDEN_SYMBOLS.is_hidden_namespace(qn) then | |
| 195 | return | |
| 196 | fi | |
| 197 | ||
| 198 | @IF.dotnet() | |
| 199 | IoC.CONTAINER.instance.dotnet_symbol_table.value.find_global_member_matches(qn, prefix, matches) | |
| 200 | si | |
| 201 | ||
| 202 | find_member(name: string) -> Symbol? | |
| 203 | => find_direct(name) | |
| 204 | ||
| 205 | find_member_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is | |
| 206 | find_direct_matches(prefix, matches) | |
| 207 | ||
| 208 | let qn mut = qualified_name | |
| 209 | ||
| 210 | let namespaces = IoC.CONTAINER.instance.namespaces | |
| 211 | @IF.dotnet() | |
| 212 | let dotnet_symbol_table = IoC.CONTAINER.instance.dotnet_symbol_table.value | |
| 213 | ||
| 214 | if qn =~ "." \/ qn.length == 0 then | |
| 215 | // FIXME: don't think this is ever hit: | |
| 216 | namespaces.find_root_matches(matches) | |
| 217 | @IF.dotnet() | |
| 218 | dotnet_symbol_table.find_root_matches(matches) | |
| 219 | ||
| 220 | return | |
| 221 | fi | |
| 222 | ||
| 223 | if qn.starts_with(".") then | |
| 224 | qn = qn.substring(1) | |
| 225 | fi | |
| 226 | ||
| 227 | namespaces.find_namespace_matches(qn, matches) | |
| 228 | ||
| 229 | @IF.dotnet() | |
| 230 | dotnet_symbol_table.find_member_matches(qn, matches) | |
| 231 | ||
| 232 | DotNet.HIDDEN_SYMBOLS.prune_hidden(qn, matches) | |
| 233 | si | |
| 234 | ||
| 235 | declare_namespace(location: LOCATION, name: string, `namespace: Symbols.NAMESPACE, symbol_definition_listener: SymbolDefinitionListener?) is | |
| 236 | declare(location, `namespace, symbol_definition_listener) | |
| 237 | si | |
| 238 | ||
| 239 | declare_class(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 240 | let result = Symbols.CLASS(location, span, self, name, arguments, enclosing) | |
| 241 | ||
| 242 | declare(location, result, symbol_definition_listener) | |
| 243 | ||
| 244 | return result | |
| 245 | si | |
| 246 | ||
| 247 | declare_trait(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 248 | let result = Symbols.TRAIT(location, span, self, name, arguments, enclosing) | |
| 249 | ||
| 250 | declare(location, result, symbol_definition_listener) | |
| 251 | ||
| 252 | return result | |
| 253 | si | |
| 254 | ||
| 255 | declare_struct(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 256 | let result = Symbols.STRUCT(location, span, self, name, arguments, enclosing) | |
| 257 | ||
| 258 | declare(location, result, symbol_definition_listener) | |
| 259 | ||
| 260 | return result | |
| 261 | si | |
| 262 | ||
| 263 | declare_union(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 264 | let result = Symbols.UNION(location, span, self, name, arguments, enclosing) | |
| 265 | ||
| 266 | declare(location, result, symbol_definition_listener) | |
| 267 | ||
| 268 | return result | |
| 269 | si | |
| 270 | ||
| 271 | declare_enum(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 272 | let result = Symbols.ENUM_STRUCT(location, span, self, name, self) | |
| 273 | ||
| 274 | declare(location, result, symbol_definition_listener) | |
| 275 | ||
| 276 | ENUM_ORDER_OPERATOR.register(result) | |
| 277 | ENUM_EQUALITY_OPERATOR.register(result) | |
| 278 | ||
| 279 | // The bitwise operators are registered later, once a | |
| 280 | // `@System.Flags()` pragma (if any) is known — see | |
| 281 | // RESOLVE_TYPE_EXPRESSIONS._maybe_register_enum_bitwise_operators. | |
| 282 | ||
| 283 | return result | |
| 284 | si | |
| 285 | ||
| 286 | declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 287 | let result = Symbols.INNATE_FUNCTION(location, self, name, enclosing, innate_name) | |
| 288 | ||
| 289 | declare_function_group(location, result, symbol_definition_listener) | |
| 290 | ||
| 291 | return result | |
| 292 | si | |
| 293 | ||
| 294 | declare_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, is_property_accessor: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 295 | let result = Symbols.GLOBAL_FUNCTION(location, span, self, name, enclosing) | |
| 296 | ||
| 297 | if is_private /\ IoC.CONTAINER.instance.build_flags.underscore_access != Compiler.UnderscoreAccess.LEGACY then | |
| 298 | result.emit_assembly = true | |
| 299 | fi | |
| 300 | ||
| 301 | declare_function_group(location, result, symbol_definition_listener) | |
| 302 | ||
| 303 | return result | |
| 304 | si | |
| 305 | ||
| 306 | declare_generator_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 307 | let result = Symbols.GLOBAL_GENERATOR_FUNCTION(location, span, self, name, enclosing) | |
| 308 | ||
| 309 | declare_function_group(location, result, symbol_definition_listener) | |
| 310 | ||
| 311 | return result | |
| 312 | si | |
| 313 | ||
| 314 | declare_async_function(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, has_body: bool, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 315 | let result = Symbols.GLOBAL_ASYNC_FUNCTION(location, span, self, name, enclosing) | |
| 316 | ||
| 317 | declare_function_group(location, result, symbol_definition_listener) | |
| 318 | ||
| 319 | return result | |
| 320 | si | |
| 321 | ||
| 322 | declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 323 | let result = Symbols.GLOBAL_VARIABLE(location, self, name) | |
| 324 | ||
| 325 | declare(location, result, symbol_definition_listener) | |
| 326 | ||
| 327 | return result | |
| 328 | si | |
| 329 | ||
| 330 | declare_top_level_variable(location: LOCATION, name: string, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 331 | let result = Symbols.TOP_LEVEL_VARIABLE(location, self, name) | |
| 332 | ||
| 333 | declare(location, result, symbol_definition_listener) | |
| 334 | ||
| 335 | return result | |
| 336 | si | |
| 337 | ||
| 338 | declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is | |
| 339 | let result = Symbols.GLOBAL_PROPERTY(location, span, self, name, is_assignable) | |
| 340 | ||
| 341 | declare(location, result, symbol_definition_listener) | |
| 342 | ||
| 343 | return result | |
| 344 | si | |
| 345 | ||
| 346 | load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value => loader.load_namespace(self) | |
| 347 | // Synthetic public sealed-abstract host for this namespace's globals. | |
| 348 | // Importer recognises by trailing simple name `$globals`. | |
| 349 | si | |
| 350 | si |