Appearance
| 1 | namespace Semantic is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use Symbols.Function | |
| 5 | use Symbols.FUNCTION_GROUP | |
| 6 | ||
| 7 | use Logging | |
| 8 | ||
| 9 | class NAMESPACE_SEARCH(owner: Symbols.Symbol init, name: string) is | |
| 10 | _logger: Logging.Logger | |
| 11 | ||
| 12 | owner: Scope | |
| 13 | functions: METHOD_OVERRIDE_MAP? | |
| 14 | other_symbol: Symbols.Symbol? | |
| 15 | ||
| 16 | init(..) is | |
| 17 | _logger = IoC.CONTAINER.instance.logger | |
| 18 | ||
| 19 | self.owner = owner | |
| 20 | si | |
| 21 | ||
| 22 | get_result() -> Symbols.Symbol? is | |
| 23 | if other_symbol? then | |
| 24 | return other_symbol | |
| 25 | fi | |
| 26 | ||
| 27 | if !functions? \/ !functions.contains_any_methods then | |
| 28 | return null | |
| 29 | fi | |
| 30 | ||
| 31 | let result = FUNCTION_GROUP(Source.LOCATION.unknown, owner, name) | |
| 32 | ||
| 33 | for s in functions! do | |
| 34 | let seen_any mut = false | |
| 35 | ||
| 36 | for f in s.iterable do | |
| 37 | assert !seen_any else "oops: multiple functions with same signature pulled into namespace scope: {s}" | |
| 38 | ||
| 39 | result.add(f) | |
| 40 | ||
| 41 | seen_any = true | |
| 42 | od | |
| 43 | od | |
| 44 | ||
| 45 | return result | |
| 46 | si | |
| 47 | ||
| 48 | add_function(function: Function) is | |
| 49 | if !function.are_arguments_declared then | |
| 50 | return | |
| 51 | fi | |
| 52 | ||
| 53 | let f mut = functions | |
| 54 | ||
| 55 | if !f? then | |
| 56 | f = METHOD_OVERRIDE_MAP(name) | |
| 57 | functions = f | |
| 58 | fi | |
| 59 | ||
| 60 | if !f.contains(function.override_class) then | |
| 61 | f.add(function) | |
| 62 | fi | |
| 63 | si | |
| 64 | ||
| 65 | add_function_group(group: FUNCTION_GROUP) is | |
| 66 | for f in group.functions do | |
| 67 | add_function(f) | |
| 68 | od | |
| 69 | si | |
| 70 | ||
| 71 | add_other(symbol: Symbols.Symbol) is | |
| 72 | assert !other_symbol? | |
| 73 | ||
| 74 | other_symbol = symbol | |
| 75 | si | |
| 76 | ||
| 77 | add(location: Source.LOCATION, symbol: Symbols.Symbol?, is_used_symbol: bool) -> bool is | |
| 78 | if !symbol? then | |
| 79 | return false | |
| 80 | fi | |
| 81 | ||
| 82 | if other_symbol? then | |
| 83 | // if we already found a non-function symbol, then it doesn't matter what kind of symbol | |
| 84 | // we've now found in an outer scope, it must be hidden by other_symbol: | |
| 85 | if is_used_symbol then | |
| 86 | let hiding = other_symbol | |
| 87 | ||
| 88 | _logger.warn(location, "hides-used-symbol", "used symbol {symbol} is hidden by {hiding}", hiding.location, "hiding declaration") | |
| 89 | fi | |
| 90 | ||
| 91 | // we should stop searching, as this non-function symbol hides all symbols with the same name | |
| 92 | // in any outer scopes | |
| 93 | return true | |
| 94 | elif isa Symbols.Function(symbol) then | |
| 95 | // we've not found any non-method symbol, yet, and we've just found a function. We need to | |
| 96 | // merge this function into the override map we're building: | |
| 97 | ||
| 98 | let function = symbol | |
| 99 | ||
| 100 | add_function(function) | |
| 101 | ||
| 102 | // continue searching, as there could be other overloads with the same name in outer scopes: | |
| 103 | return false | |
| 104 | elif isa Symbols.FUNCTION_GROUP(symbol) then | |
| 105 | // we've not found any non-method symbol, yet, and we've just found a function group. We need to | |
| 106 | // merge every function in this group into the override map we're building: | |
| 107 | ||
| 108 | let fg = symbol | |
| 109 | ||
| 110 | add_function_group(fg) | |
| 111 | ||
| 112 | // continue searching, as there could be other overloads with the same name in outer scopes: | |
| 113 | return false | |
| 114 | elif !functions? \/ !functions.contains_any_methods then | |
| 115 | // We've just found a non-function symbol, and we've not already accumulated at least one function for | |
| 116 | // our function group, so this symbol will become our result and all other matches should be ignored | |
| 117 | ||
| 118 | add_other(symbol) | |
| 119 | ||
| 120 | // we can stop searching, there can be no further matches | |
| 121 | return true | |
| 122 | fi | |
| 123 | ||
| 124 | // not a function symbol so stop search: | |
| 125 | return true | |
| 126 | si | |
| 127 | si | |
| 128 | si |