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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
*/
#include "path.hpp"
#include "ast.hpp"
#include "../types.hpp"
#include <iostream>
#include "../parse/parseerror.hpp"
#include <algorithm>
namespace AST {
// --- AST::PathNode
PathNode::PathNode(::std::string name, ::std::vector<TypeRef> args):
m_name(name),
m_params(args)
{
}
const ::std::string& PathNode::name() const
{
return m_name;
}
const ::std::vector<TypeRef>& PathNode::args() const
{
return m_params;
}
SERIALISE_TYPE(PathNode::, "PathNode", {
s << m_name;
s << m_params;
})
// --- AST::Path
template<typename T>
typename ::std::vector<Item<T> >::const_iterator find_named(const ::std::vector<Item<T> >& vec, const ::std::string& name)
{
return ::std::find_if(vec.begin(), vec.end(), [&name](const Item<T>& x) {
return x.name == name;
});
}
void Path::resolve(const Crate& root_crate)
{
DEBUG("*this = " << *this);
if(m_class != ABSOLUTE)
throw ParseError::BugCheck("Calling Path::resolve on non-absolute path");
DEBUG("m_crate = '" << m_crate << "'");
const Module* mod = &root_crate.get_root_module(m_crate);
for(unsigned int i = 0; i < m_nodes.size(); i ++ )
{
const bool is_last = (i+1 == m_nodes.size());
const bool is_sec_last = (i+2 == m_nodes.size());
const PathNode& node = m_nodes[i];
DEBUG("mod = " << mod << ", node = " << node);
// Sub-modules
{
auto& sms = mod->submods();
auto it = ::std::find_if(sms.begin(), sms.end(), [&node](const ::std::pair<Module,bool>& x) {
return x.first.name() == node.name();
});
if( it != sms.end() )
{
DEBUG("Sub-module '" << node.name() << "'");
if( node.args().size() )
throw ParseError::Generic("Generic params applied to module");
mod = &it->first;
continue;
}
}
// External crates
{
auto& crates = mod->extern_crates();
auto it = find_named(crates, node.name());
if( it != crates.end() )
{
DEBUG("Extern crate '" << node.name() << "' = '" << it->data << "'");
if( node.args().size() )
throw ParseError::Generic("Generic params applied to extern crate");
mod = &root_crate.get_root_module(it->data);
continue;
}
}
// Start searching for:
// - Re-exports
{
auto& imp = mod->imports();
auto it = find_named(imp, node.name());
if( it != imp.end() )
{
DEBUG("Re-exported path " << it->data);
throw ParseError::Todo("Path::resolve() re-export");
}
}
// - Functions
{
auto& items = mod->functions();
auto it = find_named(items, node.name());
if( it != items.end() )
{
DEBUG("Found function");
if( is_last ) {
throw ParseError::Todo("Path::resolve() bind to function");
}
else {
throw ParseError::Generic("Import of function, too many extra nodes");
}
}
}
// - Structs
{
auto& items = mod->structs();
auto it = find_named(items, node.name());
if( it != items.end() )
{
DEBUG("Found struct");
if( is_last ) {
bind_struct(it->data, node.args());
return;
}
else if( is_sec_last ) {
throw ParseError::Todo("Path::resolve() struct method");
}
else {
throw ParseError::Generic("Import of struct, too many extra nodes");
}
}
}
// - Enums (and variants)
{
auto& enums = mod->enums();
auto it = find_named(enums, node.name());
if( it != enums.end() )
{
DEBUG("Found enum");
if( is_last ) {
bind_enum(it->data, node.args());
return ;
}
else if( is_sec_last ) {
bind_enum_var(it->data, m_nodes[i+1].name(), node.args());
return ;
}
else {
throw ParseError::Generic("Binding path to enum, too many extra nodes");
}
}
}
// - Constants / statics
{
auto& items = mod->statics();
auto it = find_named(items, node.name());
if( it != items.end() )
{
DEBUG("Found static/const");
if( is_last ) {
if( node.args().size() )
throw ParseError::Generic("Unexpected generic params on static/const");
bind_static(it->data);
return ;
}
else {
throw ParseError::Generic("Binding path to static, trailing nodes");
}
}
}
throw ParseError::Generic("Unable to find component '" + node.name() + "'");
}
// We only reach here if the path points to a module
bind_module(*mod);
}
void Path::bind_module(const Module& mod)
{
m_binding_type = MODULE;
m_binding.module_ = &mod;
}
void Path::bind_enum(const Enum& ent, const ::std::vector<TypeRef>& args)
{
DEBUG("Bound to enum");
m_binding_type = ENUM;
m_binding.enum_ = &ent;
if( args.size() > 0 )
{
if( args.size() != ent.params().size() )
throw ParseError::Generic("Parameter count mismatch");
throw ParseError::Todo("Bind enum with params passed");
}
}
void Path::bind_enum_var(const Enum& ent, const ::std::string& name, const ::std::vector<TypeRef>& args)
{
unsigned int idx = 0;
for( idx = 0; idx < ent.variants().size(); idx ++ )
{
if( ent.variants()[idx].first == name ) {
break;
}
}
if( idx == ent.variants().size() )
throw ParseError::Generic("Enum variant not found");
if( args.size() > 0 )
{
if( args.size() != ent.params().size() )
throw ParseError::Generic("Parameter count mismatch");
throw ParseError::Todo("Bind enum variant with params passed");
}
DEBUG("Bound to enum variant '" << name << "' (#" << idx << ")");
m_binding_type = ENUM_VAR;
m_binding.enumvar = {&ent, idx};
}
void Path::bind_struct(const Struct& ent, const ::std::vector<TypeRef>& args)
{
throw ParseError::Todo("Path::resolve() bind to struct type");
}
void Path::bind_static(const Static& ent)
{
m_binding_type = STATIC;
m_binding.static_ = &ent;
}
Path& Path::operator+=(const Path& other)
{
for(auto& node : other.m_nodes)
append(node);
return *this;
}
::std::ostream& operator<<(::std::ostream& os, const Path& path)
{
switch(path.m_class)
{
case Path::RELATIVE:
os << "Path({" << path.m_nodes << "})";
break;
case Path::ABSOLUTE:
os << "Path(TagAbsolute, {" << path.m_nodes << "})";
break;
case Path::LOCAL:
os << "Path(TagLocal, " << path.m_nodes[0].name() << ")";
break;
}
return os;
}
::Serialiser& operator<<(Serialiser& s, Path::Class pc)
{
switch(pc)
{
case Path::RELATIVE: s << "RELATIVE"; break;
case Path::ABSOLUTE: s << "ABSOLUTE"; break;
case Path::LOCAL: s << "LOCAL"; break;
}
return s;
}
SERIALISE_TYPE(Path::, "AST_Path", {
s << m_class;
s << m_nodes;
})
}
|