Appearance
| 1 | namespace Semantic.Symbols is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use System.Text.StringBuilder | |
| 5 | ||
| 6 | use Collections.List | |
| 7 | use Collections.Map | |
| 8 | ||
| 9 | use IoC | |
| 10 | use Logging | |
| 11 | use Source | |
| 12 | ||
| 13 | use IR.Values.Value | |
| 14 | use Types.Type | |
| 15 | ||
| 16 | use Ghul.Pipes | |
| 17 | ||
| 18 | // A GENERIC represents a particular specialization of a generic class, trait or struct - i.e. a version of that symbol | |
| 19 | // with actual type arguments specified for its formal type parameters, and all its member symbols' signatures rewritten | |
| 20 | // with all instances of each formal type parameter replaced with the corresponding actual type argument | |
| 21 | class GENERIC: Symbol, Types.Typed is | |
| 22 | _symbol: Classy | |
| 23 | _type_map: Collections.Map[Symbol,Type] | |
| 24 | ||
| 25 | type_map: Collections.Map[Symbol,Type] => _type_map | |
| 26 | arguments: Collections.List[Type] | |
| 27 | ancestors: Collections.List[Type] => symbol.ancestors | |
| 28 | ||
| 29 | implementors: Collections.Iterable[Symbol]? => symbol.implementors | |
| 30 | ||
| 31 | symbol: Classy => _symbol | |
| 32 | ||
| 33 | symbols: Collections.Iterable[Symbol] is | |
| 34 | let result = Collections.LIST[Symbol]() | |
| 35 | ||
| 36 | for s in symbol.symbols do | |
| 37 | if let specialized = _specialize(s) then | |
| 38 | result.add(specialized) | |
| 39 | fi | |
| 40 | od | |
| 41 | ||
| 42 | return result | |
| 43 | si | |
| 44 | ||
| 45 | owner: Scope? public => symbol.owner, = value is si | |
| 46 | ||
| 47 | unspecialized_symbol: Symbols.Symbol => symbol | |
| 48 | root_unspecialized_symbol: Symbols.Symbol => symbol.root_unspecialized_symbol | |
| 49 | ||
| 50 | location: LOCATION => symbol.location | |
| 51 | name: string => symbol.name | |
| 52 | ||
| 53 | access: ACCESS => symbol.access | |
| 54 | ||
| 55 | is_type: bool => symbol.is_type | |
| 56 | is_generic_type_specialization: bool => true | |
| 57 | is_value_type: bool => symbol.is_value_type | |
| 58 | is_inheritable: bool => symbol.is_inheritable | |
| 59 | is_class: bool => symbol.is_class | |
| 60 | is_trait: bool => symbol.is_trait | |
| 61 | is_union: bool => symbol.is_union | |
| 62 | is_variant: bool => symbol.is_variant | |
| 63 | is_unit_variant: bool => symbol.is_unit_variant | |
| 64 | is_closed_root: bool => symbol.is_closed_root | |
| 65 | is_specializable: bool => false // type parameters already applied | |
| 66 | ||
| 67 | qualified_name: string => "{symbol.qualified_name}[{arguments_string}]" | |
| 68 | ||
| 69 | // Only the head shortens; the type arguments stay. (Type rendering | |
| 70 | // reaches the head directly and adds the arguments itself, so this | |
| 71 | // is only used when a constructed generic is named on its own - as | |
| 72 | // the owner of a member, say.) | |
| 73 | render_name(scope: Scope?) -> string => | |
| 74 | if !scope? then | |
| 75 | qualified_name | |
| 76 | elif ITERABLE_SPELLING.applies_to(symbol, arguments.count) then | |
| 77 | "{arguments[0]}{{}}" | |
| 78 | else | |
| 79 | "{symbol._render_scope_relative_name(scope)}[{arguments_string}]" | |
| 80 | fi | |
| 81 | ||
| 82 | arguments_string: string is | |
| 83 | let result = System.Text.StringBuilder() | |
| 84 | ||
| 85 | let seen_any mut = false | |
| 86 | ||
| 87 | for a in arguments do | |
| 88 | if seen_any then | |
| 89 | result.append(',') | |
| 90 | fi | |
| 91 | ||
| 92 | result.append(a) | |
| 93 | ||
| 94 | seen_any = true | |
| 95 | od | |
| 96 | ||
| 97 | return result.to_string() | |
| 98 | si | |
| 99 | ||
| 100 | short_description: string is | |
| 101 | if ITERABLE_SPELLING.applies_to(symbol, arguments.count) then | |
| 102 | return "{arguments[0].short_description}{{}}" | |
| 103 | fi | |
| 104 | ||
| 105 | let result = System.Text.StringBuilder() | |
| 106 | ||
| 107 | result | |
| 108 | .append(name) | |
| 109 | .append('[') | |
| 110 | ||
| 111 | let seen_any mut = false | |
| 112 | ||
| 113 | for a in arguments do | |
| 114 | if seen_any then | |
| 115 | result.append(',') | |
| 116 | fi | |
| 117 | ||
| 118 | result.append(a.short_description) | |
| 119 | ||
| 120 | seen_any = true | |
| 121 | od | |
| 122 | ||
| 123 | result | |
| 124 | .append(']') | |
| 125 | ||
| 126 | return result.to_string() | |
| 127 | si | |
| 128 | ||
| 129 | symbol_kind: SymbolKind => symbol.symbol_kind | |
| 130 | completion_kind: CompletionKind => symbol.completion_kind | |
| 131 | ||
| 132 | // TODO we could probably cache this rather than creating | |
| 133 | // a new one every time | |
| 134 | type: Type => Types.GENERIC(location, symbol, arguments) | |
| 135 | ||
| 136 | depth: int => symbol.depth | |
| 137 | ||
| 138 | init(location: LOCATION, symbol: Classy, arguments: Collections.List[Type]) is | |
| 139 | assert arguments |> all(a => a?) else "type argument is null for {symbol.name}" | |
| 140 | ||
| 141 | super.init( | |
| 142 | symbol.location, | |
| 143 | symbol, | |
| 144 | symbol.name) | |
| 145 | ||
| 146 | _symbol = symbol | |
| 147 | ||
| 148 | assert arguments.count > 0 else "generic has 0 arguments" | |
| 149 | assert arguments |> all(a => a?) else "at least one null argument" | |
| 150 | assert symbol.is_generic else "symbol is not generic" | |
| 151 | ||
| 152 | let length mut = arguments.count | |
| 153 | ||
| 154 | if arguments.count != symbol.argument_names.count then | |
| 155 | if length > symbol.argument_names.count then | |
| 156 | length = symbol.argument_names.count | |
| 157 | fi | |
| 158 | ||
| 159 | IoC.CONTAINER.instance.logger.error(location, "expected {symbol.argument_names.count} type arguments") | |
| 160 | fi | |
| 161 | ||
| 162 | self.arguments = arguments | |
| 163 | let tm = Collections.MAP[Symbol,Type]() | |
| 164 | ||
| 165 | for i in 0..length do | |
| 166 | if let parameter = cast GenericArgument?(symbol.type_parameter_at(i)) then | |
| 167 | tm[parameter] = arguments[i] | |
| 168 | fi | |
| 169 | od | |
| 170 | ||
| 171 | _type_map = tm | |
| 172 | ||
| 173 | is_unsafe_constraints = symbol.is_unsafe_constraints | |
| 174 | si | |
| 175 | ||
| 176 | try_create_from(location: LOCATION, symbol: Classy, type_map: Collections.Map[Symbol,Type]) -> GENERIC? static is | |
| 177 | let arguments = Collections.LIST[Type]() | |
| 178 | ||
| 179 | for i in 0..symbol.argument_names.count do | |
| 180 | let parameter = cast GenericArgument?(symbol.type_parameter_at(i)) | |
| 181 | ||
| 182 | if !parameter? then | |
| 183 | return null | |
| 184 | fi | |
| 185 | ||
| 186 | let t: Type mut | |
| 187 | ||
| 188 | if !type_map.try_get_value(parameter, t ref) then | |
| 189 | return null | |
| 190 | fi | |
| 191 | ||
| 192 | arguments.add(t) | |
| 193 | od | |
| 194 | ||
| 195 | return GENERIC(location, symbol, arguments) | |
| 196 | si | |
| 197 | ||
| 198 | add_member(symbol: Symbol) -> bool is | |
| 199 | IoC.CONTAINER.instance.logger.warn(location, "inherit-into-specialized-generic", "cannot inherit {symbol} into specialized generic {self}") | |
| 200 | return true | |
| 201 | si | |
| 202 | ||
| 203 | add_implementor(implementor: Symbol) is | |
| 204 | symbol.add_implementor(implementor) | |
| 205 | si | |
| 206 | ||
| 207 | assert_symbols_pulled_down() is | |
| 208 | symbol.assert_symbols_pulled_down() | |
| 209 | si | |
| 210 | ||
| 211 | pull_down_super_symbols() is | |
| 212 | symbol.pull_down_super_symbols() | |
| 213 | si | |
| 214 | ||
| 215 | get_ancestor(i: int) -> Type | |
| 216 | => ancestors[i].specialize(type_map) | |
| 217 | ||
| 218 | // Declared: the loop over each argument's `matches` is | |
| 219 | // genuinely store-free, but the store-free walk disqualifies | |
| 220 | // any function containing a `for` loop unconditionally rather | |
| 221 | // than reasoning about the loop body. | |
| 222 | =~(other: Symbol) -> bool pure is | |
| 223 | if !isa GENERIC(other) then | |
| 224 | return false | |
| 225 | fi | |
| 226 | ||
| 227 | let other_generic = other | |
| 228 | ||
| 229 | if other_generic.symbol != symbol then | |
| 230 | return false | |
| 231 | fi | |
| 232 | ||
| 233 | assert | |
| 234 | other_generic.arguments.count == arguments.count | |
| 235 | else | |
| 236 | "generics with the same symbol should have same number of arguments" | |
| 237 | ||
| 238 | for i in 0..arguments.count do | |
| 239 | if !arguments[i].matches(other_generic.arguments[i]) then | |
| 240 | return false | |
| 241 | fi | |
| 242 | od | |
| 243 | ||
| 244 | return true | |
| 245 | si | |
| 246 | ||
| 247 | load(location: LOCATION, from: Value?, loader: SYMBOL_LOADER) -> Value => symbol.load(location, from, loader) | |
| 248 | store(location: LOCATION, from: Value?, value: Value, loader: SYMBOL_LOADER, is_initialize: bool) -> Value => symbol.store(location, from, value, loader, is_initialize) | |
| 249 | call(location: Source.LOCATION, from: Value?, arguments: Collections.List[Value], type: Type?, caller: FUNCTION_CALLER) -> Value => symbol.call(location, from, arguments, type, caller) | |
| 250 | // given a member of the class, trait or struct that this generic wraps, we want to get a copy of it | |
| 251 | // with all references to formal type parameters replaced with the corresponding actual type arguments | |
| 252 | _specialize(member: Symbols.Symbol?) -> Symbols.Symbol? => | |
| 253 | if !member? then | |
| 254 | null | |
| 255 | elif !member.is_specializable then | |
| 256 | member | |
| 257 | elif member.can_accept_actual_type_arguments then | |
| 258 | member.specialize(arguments) | |
| 259 | else | |
| 260 | member.specialize(type_map, self) | |
| 261 | fi | |
| 262 | ||
| 263 | find_direct(name: string) -> Symbol? is | |
| 264 | assert_symbols_pulled_down() | |
| 265 | ||
| 266 | return _specialize(symbol.find_direct(name)) | |
| 267 | si | |
| 268 | ||
| 269 | find_member(name: string) -> Symbol? => | |
| 270 | let result = symbol.find_member(name) in | |
| 271 | if result? then | |
| 272 | _specialize(result) | |
| 273 | else | |
| 274 | null | |
| 275 | fi | |
| 276 | ||
| 277 | find_specialized_function(function: Symbol) -> Function? is | |
| 278 | let result = find_member(function.name) | |
| 279 | ||
| 280 | if !result? then | |
| 281 | let results = Collections.MAP[string, Symbols.Symbol]() | |
| 282 | ||
| 283 | symbol.find_member_matches("", results) | |
| 284 | ||
| 285 | return null | |
| 286 | fi | |
| 287 | ||
| 288 | let function_result = cast Function?(result) | |
| 289 | ||
| 290 | if function_result? /\ function_result.specialized_from == function then | |
| 291 | return function_result | |
| 292 | fi | |
| 293 | ||
| 294 | let function_group_result = cast FUNCTION_GROUP?(result) | |
| 295 | ||
| 296 | if !function_group_result? then | |
| 297 | return null | |
| 298 | fi | |
| 299 | ||
| 300 | for f in function_group_result.functions do | |
| 301 | if f.specialized_from == function then | |
| 302 | return f | |
| 303 | fi | |
| 304 | od | |
| 305 | ||
| 306 | return null | |
| 307 | si | |
| 308 | ||
| 309 | get_destructure_member_name(index: int) -> string? => | |
| 310 | symbol.get_destructure_member_name(index) | |
| 311 | ||
| 312 | find_enclosing(name: string) -> Symbol? => | |
| 313 | let unspecialized = symbol.find_enclosing(name) in | |
| 314 | ||
| 315 | if unspecialized? /\ unspecialized.owner == symbol then | |
| 316 | _specialize(unspecialized) | |
| 317 | else | |
| 318 | unspecialized | |
| 319 | fi | |
| 320 | ||
| 321 | find_direct_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is | |
| 322 | let m = Collections.MAP[string, Symbols.Symbol]() | |
| 323 | ||
| 324 | symbol.find_direct_matches(prefix, m) | |
| 325 | ||
| 326 | for p in m.iterator do | |
| 327 | if !matches.contains_key(p.key) then | |
| 328 | if let specialized = _specialize(p.value) then | |
| 329 | matches[p.key] = specialized.collapse_group_if_single_member() | |
| 330 | fi | |
| 331 | fi | |
| 332 | od | |
| 333 | si | |
| 334 | ||
| 335 | find_member_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is | |
| 336 | find_direct_matches(prefix, matches) | |
| 337 | symbol.find_ancestor_matches(prefix, matches) | |
| 338 | si | |
| 339 | ||
| 340 | find_enclosing_matches(prefix: string, matches: Collections.MutableMap[string, Symbols.Symbol]) is | |
| 341 | si | |
| 342 | ||
| 343 | get_hash_code() -> int is | |
| 344 | let result mut = symbol.get_hash_code() | |
| 345 | ||
| 346 | for a in arguments do | |
| 347 | result = result + a.get_hash_code() | |
| 348 | od | |
| 349 | ||
| 350 | return result | |
| 351 | si | |
| 352 | ||
| 353 | to_string() -> string => | |
| 354 | if ITERABLE_SPELLING.applies_to(symbol, arguments.count) then | |
| 355 | "{arguments[0]}{{}}" | |
| 356 | else | |
| 357 | "{IoC.CONTAINER.instance.name_display.bare_name_for(symbol)}[{arguments_string}]" | |
| 358 | fi | |
| 359 | si | |
| 360 | si |