Skip to content
← Back

src/syntax/process/register_source_intrinsics.ghul

1
namespace Syntax.Process is
2
use Logging
3
use Trees
4
use Source
5
6
// Registers an innate for each intrinsic the compilation declares in
7
// source, so the assembly that provides the intrinsics can be built by
8
// a compiler that otherwise reflects them.
9
//
10
// The declaration itself stays where it is written and is emitted
11
// normally — that emitted method, carrying its INTRINSIC_ATTRIBUTE, is
12
// what every other compilation reflects. What it cannot do is serve
13
// this compilation: a call has to lower to an opcode, and that is a
14
// property of the symbol's class, not a flag. So a matching innate is
15
// registered in `Ghul`, where the language looks for it.
16
//
17
// Runs once signatures are resolved, because the innate takes its own
18
// from the declaration.
19
class REGISTER_SOURCE_INTRINSICS: ScopedVisitor is
20
_namespaces: Semantic.NAMESPACES
21
_innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
22
23
init(
24
logger: Logger,
25
symbol_table: Semantic.SYMBOL_TABLE,
26
namespaces: Semantic.NAMESPACES,
27
innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
28
)
29
is
30
super.init(logger, symbol_table, namespaces)
31
32
_namespaces = namespaces
33
_innate_symbol_lookup = innate_symbol_lookup
34
si
35
36
_offers_constructors: bool
37
38
// Registers the intrinsics `root` declares. Where `offers_constructors`
39
// is set, each constructor intrinsic is also offered as a constructor
40
// of the type it returns.
41
apply(root: Trees.Node, offers_constructors: bool) is
42
_offers_constructors = offers_constructors
43
44
root.walk(self)
45
si
46
47
visit(function: Definitions.FUNCTION) is
48
super.visit(function)
49
50
let symbol = symbol_for(function)
51
52
if !symbol? \/ !isa Semantic.Symbols.Function(symbol) then
53
return
54
fi
55
56
let declaration = cast Semantic.Symbols.Function(symbol)
57
58
// A scalar type orders by an opcode, so an operator expression
59
// reaches the intrinsic rather than a `<>` member the type
60
// declares for its `Comparable` implementation, as it does for the
61
// member reflected from a .NET scalar.
62
if declaration.name =~ "<>" /\ _orders_by_opcode(declaration.owner) then
63
declaration.is_hidden_from_operator_resolution = true
64
fi
65
66
if !declaration.intrinsic_operation? \/ declaration.is_innate then
67
return
68
fi
69
70
// A constructor intrinsic stays an ordinary method. It is offered
71
// as a constructor to the assemblies that reflect it, and, from a
72
// source library, to the program compiled with it.
73
if declaration.intrinsic_operation.starts_with("constructor.") then
74
if _offers_constructors then
75
_offer_as_constructor(declaration)
76
fi
77
78
return
79
fi
80
81
let operation = declaration.intrinsic_operation
82
83
let intrinsic: Semantic.Symbols.Function mut
84
85
// An operator a type declares for itself goes onto the type of
86
// its first operand, as its reflected counterpart does. One
87
// declared as a member of that type is already there, and a copy
88
// beside it would make every use ambiguous between the two.
89
if let home = _operator_home(declaration) /\ declaration.owner == home then
90
intrinsic = declaration
91
elif let home = _operator_home(declaration) then
92
intrinsic = Semantic.Symbols.INNATE_FUNCTION(
93
declaration.location,
94
home,
95
declaration.name,
96
home,
97
operation
98
)
99
100
home.declare_function_group(declaration.location, intrinsic, null)
101
else
102
let target = _namespaces.try_find_namespace(".Ghul.Intrinsics")
103
104
if !target? then
105
return
106
fi
107
108
intrinsic =
109
cast Semantic.Symbols.Function?(
110
target.declare_innate(
111
declaration.location,
112
declaration.name,
113
operation,
114
target,
115
null
116
)
117
)!
118
fi
119
120
intrinsic.arguments = declaration.arguments
121
intrinsic.argument_names = declaration.argument_names
122
intrinsic.return_type = declaration.return_type
123
124
if declaration.is_declared_pure then
125
intrinsic.mark_declared_pure()
126
fi
127
128
// An operator's other operand types reach it too.
129
if let home = _operator_home(declaration) then
130
for other in Semantic.Symbols.TYPE_HOMED_OPERATORS.homes(declaration) do
131
if other != home then
132
other.declare_function_group(declaration.location, intrinsic, null)
133
fi
134
od
135
fi
136
137
if declaration.is_generic then
138
intrinsic.is_generic = true
139
intrinsic.generic_arguments = declaration.generic_arguments
140
intrinsic.generic_argument_names = declaration.generic_argument_names
141
intrinsic.generic_argument_constraint_kinds = declaration.generic_argument_constraint_kinds
142
intrinsic.generic_argument_has_constructor_constraint = declaration.generic_argument_has_constructor_constraint
143
fi
144
si
145
146
// Adds an `init` to the class `target` returns that calls `target`, as
147
// the reflected path does for a constructor intrinsic. Where `target`
148
// returns a generic instance, the class is the generic definition and
149
// the `init` belongs to it, so the target's type parameters must be
150
// the class's own, in order, as in `from_pairs[K, V]() -> MAP[K, V]`;
151
// a target that returns its parameters in any other shape is left as
152
// a method.
153
_offer_as_constructor(target: Semantic.Symbols.Function) is
154
if let generic: Semantic.Types.GENERIC = target.return_type then
155
_offer_as_generic_constructor(target, generic)
156
return
157
fi
158
159
let owner = cast Semantic.Symbols.Classy?(cast Semantic.Types.NAMED?(target.return_type)?.symbol)
160
161
if !owner? \/ owner.is_generic then
162
return
163
fi
164
165
_add_constructor(target, owner, target.arguments)
166
si
167
168
// The `init` is phrased in the class's own type parameters, so the
169
// arguments are the target's with its type parameters replaced by
170
// the class's. A call constructing the class specialises the target
171
// by the constructed type's arguments.
172
_offer_as_generic_constructor(target: Semantic.Symbols.Function, returned: Semantic.Types.GENERIC) is
173
let owner = cast Semantic.Symbols.GENERIC?(returned.symbol)?.symbol
174
175
if !owner? \/ !target.is_generic \/ target.generic_arguments.count != returned.arguments.count then
176
return
177
fi
178
179
let parameters = Collections.LIST[Semantic.Types.Type]()
180
181
for i in 0..returned.arguments.count do
182
let returned_parameter = cast Semantic.Symbols.GenericArgument?(cast Semantic.Types.NAMED?(returned.arguments[i])?.symbol)
183
let target_parameter = cast Semantic.Symbols.GenericArgument?(cast Semantic.Types.NAMED?(target.generic_arguments[i])?.symbol)
184
let owner_parameter = owner.type_parameter_at(i)?.type
185
186
if !returned_parameter? \/ returned_parameter != target_parameter \/ !owner_parameter? then
187
return
188
fi
189
190
parameters.add(owner_parameter)
191
od
192
193
let phrased = cast Semantic.Symbols.Function(target.specialize(parameters))
194
195
_add_constructor(target, owner, phrased.arguments)
196
si
197
198
_add_constructor(target: Semantic.Symbols.Function, owner: Semantic.Symbols.Classy, arguments: Collections.List[Semantic.Types.Type]) is
199
let result = Semantic.Symbols.INSTANCE_METHOD(target.location, target.location, owner, "init", owner)
200
201
result.arguments = arguments
202
result.argument_names = target.argument_names
203
result.return_type = _innate_symbol_lookup.get_void_type()
204
result.constructor_target = target
205
206
if target.is_declared_pure then
207
result.mark_declared_pure()
208
fi
209
210
if target.is_generic /\ !owner.is_generic then
211
result.is_generic = true
212
result.generic_arguments = target.generic_arguments
213
result.generic_argument_names = target.generic_argument_names
214
fi
215
216
result.mark_synthesized()
217
218
owner.add_member(result)
219
si
220
221
_orders_by_opcode(owner: Semantic.Scope?) -> bool is
222
if let classy = cast Semantic.Symbols.Classy?(owner) then
223
if let built_in = classy.built_in_type then
224
return built_in != Semantic.Symbols.BuiltInType.VOID /\ built_in != Semantic.Symbols.BuiltInType.BOOL
225
fi
226
227
return classy == _innate_symbol_lookup.get_decimal_type().symbol
228
fi
229
230
return false
231
si
232
233
_operator_home(declaration: Semantic.Symbols.Function) -> Semantic.Symbols.Classy? is
234
if !Semantic.Symbols.TYPE_HOMED_OPERATORS.contains(declaration.name) \/ declaration.arguments.count == 0 then
235
return null
236
fi
237
238
if let first: Semantic.Types.NAMED = declaration.arguments[0] then
239
return cast Semantic.Symbols.Classy?(first.symbol)
240
fi
241
242
return null
243
si
244
si
245
si