Skip to content
← Back

src/syntax/process/declare_symbols.ghul

1
namespace Syntax.Process is
2
use Logging
3
use Source
4
use Trees
5
6
class DECLARE_SYMBOLS(
7
_logger: Logger,
8
symbol_table: Semantic.SYMBOL_TABLE,
9
namespaces: Semantic.NAMESPACES,
10
_symbol_definition_listener: Semantic.SymbolDefinitionListener
11
): ScopeVisitorBase is
12
_pragma_scope_stack: PRAGMA_SCOPE_STACK
13
14
_generic_argument_declarer: GENERIC_ARGUMENT_DECLARER
15
16
super(symbol_table, namespaces)
17
18
init(..) is
19
_pragma_scope_stack = PRAGMA_SCOPE_STACK()
20
_generic_argument_declarer = GENERIC_ARGUMENT_DECLARER(_logger, symbol_table, _symbol_definition_listener)
21
si
22
23
apply(node: Node) is
24
assert _pragma_scope_stack.is_balanced
25
26
node.walk(self)
27
28
assert _pragma_scope_stack.is_balanced
29
si
30
31
32
33
pre(pragma: Definitions.PRAGMA) -> bool is
34
_pragma_scope_stack.enter(pragma.pragma)
35
36
return false
37
si
38
39
visit(pragma: Definitions.PRAGMA) is
40
let p = pragma.pragma
41
42
let name = p.name.to_string()
43
44
if IL_PRAGMAS.is_name_pragma(name) then
45
if p.arguments.expressions.count != 1 then
46
_logger.error(p.arguments.location, "expected one argument")
47
return
48
fi
49
50
let argument = p.arguments.expressions[0]
51
52
if !isa Expressions.Literals.STRING(argument) then
53
_logger.error(p.arguments.location, "expected a string literal argument")
54
return
55
fi
56
57
let il_name = argument.value_string
58
59
// A name reaches metadata as written. Quoting was once
60
// how a name colliding with an IL keyword was spelled;
61
// now it would just become part of the name.
62
if il_name.index_of('\'') >= 0 then
63
_logger.error(
64
p.arguments.location, "an IL name cannot contain a quote")
65
66
return
67
fi
68
69
let definition mut = pragma.definition
70
71
while isa Definitions.PRAGMA(definition) do
72
definition = cast Definitions.PRAGMA(definition).definition
73
od
74
75
let symbol = symbol_for(definition)
76
77
if symbol? then
78
if isa Semantic.Symbols.Property(symbol) then
79
let property = symbol
80
81
if name =~ IL_PRAGMAS.NAME_READ then
82
property.read_function_il_name_override = il_name
83
elif name =~ IL_PRAGMAS.NAME_ASSIGN then
84
property.assign_function_il_name_override = il_name
85
elif name =~ IL_PRAGMAS.NAME then
86
property.il_name_override = il_name
87
88
if !property.read_function_il_name_override? then
89
property.read_function_il_name_override = "get_{il_name}"
90
fi
91
92
if !property.assign_function_il_name_override? then
93
property.assign_function_il_name_override = "set_{il_name}"
94
fi
95
fi
96
97
return
98
fi
99
100
if isa Semantic.Symbols.Classy(symbol) then
101
_logger.error(p.location, "pragma IL.name applies only to a function, method or property")
102
return
103
fi
104
105
symbol.il_name_override = il_name
106
fi
107
else
108
_pragma_scope_stack.leave(p)
109
fi
110
si
111
112
pre(`namespace: Definitions.NAMESPACE) -> bool is
113
let scope = declare_and_enter_namespace(`namespace, _symbol_definition_listener, `namespace.is_compiler_generated)
114
115
let identifier = `namespace.name
116
117
if !`namespace.is_compiler_generated /\ identifier.name =~ "llvm" /\ scope.containing_namespace == _namespaces.try_find_namespace(".llvm") then
118
IoC.CONTAINER.instance.capability_checker.check(Semantic.Capability.LLVM_NAMESPACE, identifier.location, "a namespace named llvm")
119
fi
120
121
return false
122
si
123
124
visit(`namespace: Definitions.NAMESPACE) is
125
leave_namespace(`namespace)
126
si
127
128
_pre(
129
classy: Definitions.Classy,
130
declare_symbol: (Semantic.DeclarationContext, LOCATION, LOCATION, string, Collections.List[string], Semantic.Scope, Semantic.SymbolDefinitionListener) -> Semantic.Symbols.Symbol
131
) -> bool is
132
let symbol: Semantic.Scope mut
133
134
let arguments = _generic_argument_declarer.get_generic_arguments(classy.arguments)
135
136
symbol = declare_symbol(
137
current_declaration_context,
138
classy.name.location,
139
classy.location,
140
classy.name.name,
141
arguments,
142
current_scope,
143
_symbol_definition_listener
144
)
145
146
if let declared: Semantic.Symbols.Symbol = symbol then
147
declared.set_doc(classy.doc)
148
fi
149
150
associate_and_enter_scope(
151
classy,
152
symbol
153
)
154
155
_generic_argument_declarer.declare_generic_arguments(classy.arguments)
156
return false
157
si
158
159
pre(`class: Definitions.CLASS) -> bool is
160
_pre(
161
`class,
162
(context, name_location, location, name, arguments, scope, listener) =>
163
context.declare_class(name_location, location, name, arguments, scope, listener)
164
)
165
166
let class_symbol = cast Semantic.Symbols.CLASS?(current_scope)
167
168
if class_symbol? then
169
if `class.modifiers.is_open then
170
class_symbol.mark_declared_open()
171
fi
172
173
if `class.modifiers.is_abstract then
174
class_symbol.mark_abstract()
175
fi
176
177
if `class.modifiers.is_pure then
178
class_symbol.mark_pure()
179
fi
180
fi
181
182
// The body holds only members - declared by DECLARE_MEMBERS.
183
return true
184
si
185
186
visit(`class: Definitions.CLASS) is
187
leave_scope(`class)
188
si
189
190
// impl and partial blocks and all other member-level
191
// declarations are handled by DECLARE_MEMBERS, which runs after
192
// resolve-uses; this pass declares only the type-level skeleton.
193
pre(`partial: Definitions.PARTIAL) -> bool => true
194
195
pre(`impl: Definitions.IMPL) -> bool => true
196
197
pre(`trait: Definitions.TRAIT) -> bool is
198
_pre(
199
`trait,
200
(context, name_location, location, name, arguments, scope, listener) =>
201
context.declare_trait(name_location, location, name, arguments, scope, listener)
202
)
203
204
let trait_symbol = cast Semantic.Symbols.TRAIT?(current_scope)
205
206
if trait_symbol? then
207
if `trait.modifiers.is_open then
208
trait_symbol.mark_declared_open()
209
fi
210
211
if `trait.modifiers.is_pure then
212
trait_symbol.mark_pure()
213
fi
214
fi
215
216
return true
217
si
218
219
visit(`trait: Definitions.TRAIT) is
220
leave_scope(`trait)
221
si
222
223
pre(`struct: Definitions.STRUCT) -> bool is
224
_pre(
225
`struct,
226
(context, name_location, location, name, arguments, scope, listener) =>
227
context.declare_struct(name_location, location, name, arguments, scope, listener)
228
)
229
230
let struct_symbol = cast Semantic.Symbols.STRUCT?(current_scope)
231
232
if struct_symbol? /\ `struct.modifiers.is_pure then
233
struct_symbol.mark_pure()
234
fi
235
236
return true
237
si
238
239
visit(`struct: Definitions.STRUCT) is
240
leave_scope(`struct)
241
si
242
243
pre(`union: Definitions.UNION) -> bool is
244
// The body holds the variants - types, declared here; their
245
// fields are members, declared by DECLARE_MEMBERS.
246
let result =
247
_pre(
248
`union,
249
(context, name_location, location, name, arguments, scope, listener) =>
250
context.declare_union(name_location, location, name, arguments, scope, listener)
251
)
252
253
if isa Semantic.Symbols.UNION(current_scope) /\ `union.modifiers.is_pure then
254
_logger.error(
255
`union.name.location,
256
"pure is not supported on a union"
257
)
258
fi
259
260
return result
261
si
262
263
visit(`union: Definitions.UNION) is
264
leave_scope(`union)
265
si
266
267
pre(variant: Definitions.VARIANT) -> bool is
268
let result =
269
_pre(
270
variant,
271
(context, name_location, location, name, arguments, scope, listener) =>
272
context.declare_variant(name_location, location, name, scope, listener)
273
)
274
275
if result then
276
return result
277
fi
278
279
// Propagate the AST's `default` modifier to the variant
280
// symbol so DECLARE_MEMBERS' union visit can pick the
281
// default variant once fields are declared.
282
if isa Semantic.Symbols.VARIANT(current_scope) then
283
let variant_symbol = cast Semantic.Symbols.VARIANT(current_scope)
284
variant_symbol.is_default = variant.is_default
285
fi
286
287
// Fields are members - declared by DECLARE_MEMBERS.
288
return true
289
si
290
291
292
293
pre(`enum: Definitions.ENUM) -> bool is
294
_pre(
295
`enum,
296
(context, name_location, location, name, arguments, scope, listener) =>
297
context.declare_enum(name_location, location, name, scope, listener)
298
)
299
300
return true
301
si
302
303
visit(`enum: Definitions.ENUM) is
304
leave_scope(`enum)
305
si
306
307
pre(enum_member: Definitions.ENUM_MEMBER) -> bool => true
308
309
pre(function: Definitions.FUNCTION) -> bool => true
310
311
pre(property: Definitions.PROPERTY) -> bool => true
312
313
pre(indexer: Definitions.INDEXER) -> bool => true
314
si
315
si