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 | _logger.warn(location, "hides-used-symbol", "used symbol {symbol} is hidden by {other_symbol}"); | |
| 87 | fi | |
| 88 | ||
| 89 | // we should stop searching, as this non-function symbol hides all symbols with the same name | |
| 90 | // in any outer scopes | |
| 91 | return true; | |
| 92 | elif isa Symbols.Function(symbol) then | |
| 93 | // we've not found any non-method symbol, yet, and we've just found a function. We need to | |
| 94 | // merge this function into the override map we're building: | |
| 95 | ||
| 96 | let function = symbol; | |
| 97 | ||
| 98 | add_function(function); | |
| 99 | ||
| 100 | // continue searching, as there could be other overloads with the same name in outer scopes: | |
| 101 | return false; | |
| 102 | elif isa Symbols.FUNCTION_GROUP(symbol) then | |
| 103 | // we've not found any non-method symbol, yet, and we've just found a function group. We need to | |
| 104 | // merge every function in this group into the override map we're building: | |
| 105 | ||
| 106 | let fg = symbol; | |
| 107 | ||
| 108 | add_function_group(fg); | |
| 109 | ||
| 110 | // continue searching, as there could be other overloads with the same name in outer scopes: | |
| 111 | return false; | |
| 112 | elif !functions? \/ !functions.contains_any_methods then | |
| 113 | // We've just found a non-function symbol, and we've not already accumulated at least one function for | |
| 114 | // our function group, so this symbol will become our result and all other matches should be ignored | |
| 115 | ||
| 116 | add_other(symbol); | |
| 117 | ||
| 118 | // we can stop searching, there can be no further matches | |
| 119 | return true; | |
| 120 | fi | |
| 121 | ||
| 122 | // not a function symbol so stop search: | |
| 123 | return true; | |
| 124 | si | |
| 125 | si | |
| 126 | si |