Repository navigation
Expand file tree
/
Copy pathSymbolResolver.h
More file actions
189 lines (153 loc) · 6.16 KB
/
Copy pathSymbolResolver.h
File metadata and controls
189 lines (153 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#pragma once
#include <ranges>
#include <utility>
#include "SemaVisitor.h"
namespace sema {
struct SymbolResolver : SemaVisitor<SymbolResolver> {
SymbolManager& SymMan;
ModuleManager& ModuleMap;
struct Data {
std::unordered_set<std::string_view> ignore_symbols{};
std::unordered_map<std::string_view, Ident*> generic_args;
};
explicit SymbolResolver(const SemaContext& context)
: SemaVisitor(context.module, context.error_callback)
, SymMan(context.module->symbol_table)
, ModuleMap(context.module->getModuleManager())
{}
void handle(const ImportNode* node, const Data&) {
if (!node->imported_symbols.empty()) {
for (auto& symbol : node->imported_symbols) {
IdentInfo* id = SymMan.getIdInfoFromModule(
node->mod_handle, std::string(symbol.actual_name));
if (!id) {
reportError(
ErrCode::SYMBOL_NOT_FOUND_IN_MOD,
{.path_1 = node->mod_handle->getPath(), .str_1 = symbol.actual_name}
);
continue;
}
if ((
(!m_Module->isBuiltin() && !BuiltinTypes.contains(id->toString()))) &&
!SymMan.lookupDecl(id).is_exported) {
reportError(
ErrCode::SYMBOL_NOT_EXPORTED,
{.str_1 = symbol.actual_name}
);
}
// make the symbol manager aware of the foreign symbol's `IdentInfo*`
SymMan.registerForeignID(
symbol.assigned_alias.empty() ?
std::string(symbol.actual_name)
: std::string(symbol.assigned_alias),
id, node->is_exported
);
}
}
}
void handle(const Function* node, Data data) {
// when not being instantiated, do not attempt to resolve symbols which use generics
if (!node->generic_params.empty() && data.generic_args.empty()) {
for (GenericParam* param : node->generic_params) {
data.ignore_symbols.insert(param->name);
visit(param, data);
}
}
for (Parameter* param : node->params) {
visit(param, data);
} visit(node->return_type, data);
visit(node->children, data);
}
void handle(const Protocol* node, Data data) {
// mandated associated types are placeholders, add them to the ignored-symbols set
for (TypeAlias* alias : node->type_aliases) {
if (!alias->alias_for) {
data.ignore_symbols.insert(alias->alias);
}
}
// add generic parameters to ignored-symbols set
for (GenericParam* param : node->generic_params) {
visit(param, data);
data.ignore_symbols.insert(param->name);
}
for (const auto& method : node->methods) {
for (TypeWrapper* ty : method.params) {
visit(ty, data);
}
visit(method.return_type, data);
}
for (TypeAlias* alias : node->type_aliases) {
visit(alias, data);
}
}
void handle(FuncCall* node, Data data) {
std::span<GenericParam*>* generic_params = nullptr;
if (node->ident->value) {
auto fn_node = SymMan.lookupDecl(node->ident->value).node_ptr->to<Function>();
generic_params = &fn_node->generic_params;
}
visit(node->ident, data);
for (const auto& [i, arg] : std::views::enumerate(node->generic_args)) {
// Id to node table required
if (!generic_params) {
reportError(ErrCode::NOT_A_GENERIC, {
.str_1 = node->ident->full_qualification.back().name});
break;
}
if (i >= generic_params->size()) {
reportError(ErrCode::TOO_MANY_GENERIC_ARGS, {});
break;
}
if (arg->isType()) {
data.generic_args.insert({generic_params->at(i)->name, arg->getType()->type_id}); // TODO
}
}
for (const auto& arg : node->args) {
visit(arg, data);
}
}
void handle(const Op* node, Data data) {
if (node->op_type == Op::DOT) {
// only visit LHS; RHS method-name Ident is handled by TypeResolver::evaluateType(Op*)
visit(node->operands.at(0), std::move(data));
} else {
for (auto& operand : node->operands) {
visit(operand, data);
}
}
}
void handle(Ident* node, const Data& data) {
assert(!node->full_qualification.empty());
// do not attempt resolution if the symbol is ignored
if (data.ignore_symbols.contains(node->full_qualification.front().name)) {
return;
}
// Type-qualified members (`Type::member`) may be provided by protocol
// impls, which are only registered by the TypeResolver, i.e. after this
// pass. Defer such lookups.
const bool defer_to_type_resolver = [this](const Ident& id) {
if (id.full_qualification.size() <= 1)
return false;
auto* first = SymMan.getIdInfoOfAGlobal(
std::string(id.full_qualification.front().name), false, false);
if (!first)
return false;
const auto& entry = SymMan.lookupDecl(first);
return entry.scope != nullptr && !entry.is_mod_namespace;
}(*node);
if (!node->value) {
node->value = SymMan.getIDInfoFor(*node,
defer_to_type_resolver
? std::nullopt
: std::optional<ErrorCallback_t>{[this](const ErrCode code, const ErrorContext& ctx) {
reportError(code, ctx);
}});
}
if (!node->value && !defer_to_type_resolver) {
reportError(ErrCode::UNDEFINED_IDENTIFIER, {
.str_1 = node->full_qualification.back().name
});
}
}
};
}