Skip to content
← Back

src/semantic/scope/block_scope.ghul

1
namespace Semantic is
2
use System.NotImplementedException
3
4
use IoC
5
use Logging
6
use Source
7
8
use Types.Type
9
use Symbols.Symbol
10
11
class BLOCK_SCOPE: Scope, DeclarationContext is
12
_enclosing: Scope?
13
14
/// The scope this block is nested in, or null at the top.
15
enclosing: Scope? => _enclosing
16
_symbols: Semantic.SYMBOL_STORE
17
18
name: string => "[block]"
19
qualified_name: string =>
20
if let self._enclosing? then
21
"{_enclosing.qualified_name}.[block]"
22
else
23
"[block]"
24
fi
25
26
symbols: Collections.Iterable[Symbols.Symbol] => _symbols.values
27
28
type: Type => Types.NONE.instance
29
30
unspecialized_symbol: Symbols.Symbol? => null
31
32
init() is
33
init(null)
34
si
35
36
init(enclosing: Scope?) is
37
_enclosing = enclosing
38
_symbols = Semantic.SYMBOL_STORE()
39
si
40
41
qualify(name: string) -> string =>
42
if let self._enclosing? then
43
_enclosing.qualify(name)
44
else
45
name
46
fi
47
48
find_direct(name: string) -> Symbols.Symbol? =>
49
_symbols[name]
50
51
find_direct_matches(prefix: string, matches: Collections.MutableMap[string,Semantic.Symbols.Symbol]) is
52
_symbols.find_matches(prefix, matches)
53
si
54
55
find_member(name: string) -> Symbols.Symbol? => throw NotImplementedException("cannot search for member {name} in block scope")
56
find_member_matches(prefix: string, matches: Collections.MutableMap[string,Semantic.Symbols.Symbol]) => throw NotImplementedException("cannot search for member matches {prefix} in block scope")
57
find_enclosing(name: string) -> Symbols.Symbol? is
58
let result = find_direct(name)
59
60
if result? then
61
return result
62
elif _enclosing? then
63
return _enclosing.find_enclosing(name)
64
else
65
return null
66
fi
67
si
68
69
find_enclosing_matches(prefix: string, matches: Collections.MutableMap[string,Semantic.Symbols.Symbol]) is
70
find_direct_matches(prefix, matches)
71
72
if let self._enclosing? then
73
_enclosing.find_enclosing_matches(prefix, matches)
74
fi
75
si
76
77
declare_undefined(location: LOCATION, kind: string, name: string) -> Semantic.Symbols.Symbol is
78
CONTAINER.instance.logger.error(location, "cannot declare {kind} here")
79
80
return Symbols.UNDEFINED(location, self, name)
81
si
82
83
declare_class(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
84
declare_undefined(location, "class", name)
85
86
declare_trait(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
87
declare_undefined(location, "trait", name)
88
89
declare_struct(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
90
declare_undefined(location, "struct", name)
91
92
declare_union(location: LOCATION, span: LOCATION, name: string, arguments: Collections.List[string], enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
93
declare_undefined(location, "union", name)
94
95
declare_variant(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
96
declare_undefined(location, "variant", name)
97
98
declare_type(location: LOCATION, name: string, index: int, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
99
declare_undefined(location, "type", name)
100
101
declare_enum(location: LOCATION, span: LOCATION, name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
102
declare_undefined(location, "enum", name)
103
104
declare_enum_member(location: LOCATION, name: string, value: string?, symbol_definition_listener: SymbolDefinitionListener?) is
105
declare_undefined(location, "enum member", name)
106
si
107
108
declare_innate(location: LOCATION, name: string, innate_name: string, enclosing: Scope, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
109
declare_undefined(location, "innate", name)
110
111
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 =>
112
declare_undefined(location, "function", name)
113
114
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 =>
115
declare_undefined(location, "generator function", name)
116
117
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 =>
118
declare_undefined(location, "async function", name)
119
120
declare_namespace(location: LOCATION, name: string, `namespace: Symbols.NAMESPACE, symbol_definition_listener: SymbolDefinitionListener?) is
121
declare_undefined(location, "namespace", name)
122
si
123
124
declare_label(location: LOCATION, name: string, symbol_definition_listener: SymbolDefinitionListener?) is
125
let label = Symbols.LABEL(location, self, name)
126
declare(location, label, symbol_definition_listener)
127
si
128
129
declare_variable(location: LOCATION, name: string, is_static: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol is
130
// FIXME: IoC
131
//
132
// A block scope reached with no enclosing capture context -
133
// a rejected `partial`/`impl` block at namespace scope
134
// routes its members here so each is rejected once rather
135
// than cascading - has nothing real to own this variable.
136
// Owning it here keeps a rejected function's own parameters
137
// resolvable within its body; nothing downstream reads the
138
// owner of a variable declared into a dead-end scope.
139
let capture_context = IoC.CONTAINER.instance.symbol_table.current_capture_context
140
let owner = if capture_context? then cast Scope(capture_context) else self fi
141
142
let variable = Symbols.LOCAL_VARIABLE(location, owner, name)
143
declare(location, variable, symbol_definition_listener)
144
return variable
145
si
146
147
declare_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol => declare_undefined(location, "anonymous function", name)
148
declare_async_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol => declare_undefined(location, "anonymous async function", name)
149
declare_generator_closure(location: LOCATION, name: string, owner: Scope, enclosing: Scope, is_recursive: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol => declare_undefined(location, "anonymous generator function", name)
150
declare_property(location: LOCATION, span: LOCATION, name: string, is_static: bool, is_private: bool, is_assignable: bool, symbol_definition_listener: SymbolDefinitionListener?) -> Symbol =>
151
declare_undefined(location, "property", name)
152
153
declare(location: LOCATION, symbol: Symbols.Symbol, symbol_definition_listener: SymbolDefinitionListener?) is
154
let name = symbol.name
155
let existing = find_direct(name)
156
157
if existing? then
158
CONTAINER.instance.logger.error(location, "redefining symbol {symbol.name}", existing.location, "symbol declared here")
159
CONTAINER.instance.logger.error(existing.location, "symbol {symbol.name} is redefined", location, "redefined here")
160
fi
161
162
if symbol_definition_listener? then
163
symbol_definition_listener.add_symbol_definition(location, symbol)
164
fi
165
166
_symbols[name] = symbol
167
si
168
169
to_string() -> string =>
170
"{get_type()} {_symbols}"
171
si
172
si