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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
#ifndef AST_HPP_INCLUDED
#define AST_HPP_INCLUDED
#include <string>
#include <vector>
#include <stdexcept>
#include "../coretypes.hpp"
#include <memory>
#include "../parse/tokentree.hpp"
#include "../types.hpp"
namespace AST {
using ::std::unique_ptr;
using ::std::move;
class Crate;
class MetaItem
{
::std::string m_name;
::std::vector<MetaItem> m_items;
::std::string m_str_val;
public:
MetaItem(::std::string name):
m_name(name)
{
}
MetaItem(::std::string name, ::std::vector<MetaItem> items):
m_name(name),
m_items(items)
{
}
const ::std::string& name() const { return m_name; }
};
class ExprNode;
class Pattern
{
public:
enum BindType {
MAYBE_BIND,
ANY,
VALUE,
TUPLE,
TUPLE_STRUCT,
};
private:
BindType m_class;
::std::string m_binding;
Path m_path;
unique_ptr<ExprNode> m_node;
::std::vector<Pattern> m_sub_patterns;
public:
Pattern():
m_class(ANY)
{}
struct TagBind {};
Pattern(TagBind, ::std::string name):
m_class(ANY),
m_binding(name)
{}
struct TagMaybeBind {};
Pattern(TagMaybeBind, ::std::string name):
m_class(MAYBE_BIND),
m_binding(name)
{}
struct TagValue {};
Pattern(TagValue, unique_ptr<ExprNode> node):
m_class(VALUE),
m_node( ::std::move(node) )
{}
struct TagTuple {};
Pattern(TagTuple, ::std::vector<Pattern> sub_patterns):
m_class(TUPLE),
m_sub_patterns( ::std::move(sub_patterns) )
{}
struct TagEnumVariant {};
Pattern(TagEnumVariant, Path path, ::std::vector<Pattern> sub_patterns):
m_class(TUPLE_STRUCT),
m_path( ::std::move(path) ),
m_sub_patterns( ::std::move(sub_patterns) )
{}
// Mutators
void set_bind(::std::string name) {
m_binding = name;
}
// Accessors
const ::std::string& binding() const { return m_binding; }
BindType type() const { return m_class; }
ExprNode& node() { return *m_node; }
const ExprNode& node() const { return *m_node; }
Path& path() { return m_path; }
const Path& path() const { return m_path; }
::std::vector<Pattern>& sub_patterns() { return m_sub_patterns; }
const ::std::vector<Pattern>& sub_patterns() const { return m_sub_patterns; }
friend ::std::ostream& operator<<(::std::ostream& os, const Pattern& pat);
};
#include "ast_expr.hpp"
class Expr
{
::std::shared_ptr<ExprNode> m_node;
public:
Expr(unique_ptr<ExprNode> node):
m_node(node.release())
{
}
Expr(ExprNode* node):
m_node(node)
{
}
void visit_nodes(NodeVisitor& v);
friend ::std::ostream& operator<<(::std::ostream& os, const Expr& pat);
};
class Function
{
public:
enum Class
{
CLASS_UNBOUND,
CLASS_REFMETHOD,
CLASS_MUTMETHOD,
CLASS_VALMETHOD,
};
typedef ::std::vector<StructItem> Arglist;
private:
::std::string m_name;
TypeParams m_generic_params;
Class m_fcn_class;
Expr m_code;
TypeRef m_rettype;
Arglist m_args;
public:
Function(::std::string name, TypeParams params, Class fcn_class, TypeRef ret_type, Arglist args, Expr code):
m_name(name),
m_generic_params(params),
m_fcn_class(fcn_class),
m_code(code),
m_rettype(ret_type),
m_args(args)
{
}
const ::std::string& name() const { return m_name; }
TypeParams& generic_params() { return m_generic_params; }
const TypeParams& generic_params() const { return m_generic_params; }
Expr& code() { return m_code; }
const Expr& code() const { return m_code; }
const TypeRef& rettype() const { return m_rettype; }
TypeRef& rettype() { return m_rettype; }
const Arglist& args() const { return m_args; }
Arglist& args() { return m_args; }
};
class Impl
{
public:
Impl(TypeRef impl_type, TypeRef trait_type);
void add_function(bool is_public, Function fcn);
};
class Crate;
class ExternCrate;
class Module;
typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn);
template <typename T>
struct Item
{
::std::string name;
T data;
bool is_pub;
Item(::std::string&& name, T&& data, bool is_pub):
name( move(name) ),
data( move(data) ),
is_pub( is_pub )
{
}
};
/// Representation of a parsed (and being converted) function
class Module
{
typedef ::std::vector< ::std::pair<Function, bool> > itemlist_fcn_t;
typedef ::std::vector< ::std::pair<Module, bool> > itemlist_mod_t;
typedef ::std::vector< Item<Path> > itemlist_use_t;
typedef ::std::vector< Item<ExternCrate> > itemlist_ext_t;
const Crate& m_crate;
::std::string m_name;
::std::vector<MetaItem> m_attrs;
itemlist_fcn_t m_functions;
itemlist_mod_t m_submods;
itemlist_use_t m_imports;
itemlist_ext_t m_extern_crates;
public:
Module(const Crate& crate, ::std::string name):
m_crate(crate),
m_name(name)
{
}
void add_ext_crate(::std::string ext_name, ::std::string imp_name);
void add_alias(bool is_public, Path path, ::std::string name) {
m_imports.push_back( Item<Path>( move(name), move(path), is_public) );
}
void add_constant(bool is_public, ::std::string name, TypeRef type, Expr val);
void add_global(bool is_public, bool is_mut, ::std::string name, TypeRef type, Expr val);
void add_struct(bool is_public, ::std::string name, TypeParams params, ::std::vector<StructItem> items);
void add_function(bool is_public, Function func);
void add_impl(Impl impl);
void add_attr(MetaItem item) {
m_attrs.push_back(item);
}
void iterate_functions(fcn_visitor_t* visitor, const Crate& crate);
const Crate& crate() const { return m_crate; }
const ::std::string& name() const { return m_name; }
::std::vector<MetaItem>& attrs() { return m_attrs; }
itemlist_fcn_t& functions() { return m_functions; }
itemlist_mod_t& submods() { return m_submods; }
itemlist_use_t& imports() { return m_imports; }
itemlist_ext_t& extern_crates() { return m_extern_crates; }
const ::std::vector<MetaItem>& attrs() const { return m_attrs; }
const itemlist_fcn_t& functions() const { return m_functions; }
const itemlist_mod_t& submods() const { return m_submods; }
const itemlist_use_t& imports() const { return m_imports; }
const itemlist_ext_t& extern_crates() const { return m_extern_crates; }
};
class Crate
{
public:
Module m_root_module;
bool m_load_std;
Crate();
Module& root_module() { return m_root_module; }
const Module& root_module() const { return m_root_module; }
void iterate_functions( fcn_visitor_t* visitor );
};
/// Representation of an imported crate
/// - Functions are stored as resolved+typechecked ASTs
class ExternCrate
{
Crate m_crate;
public:
ExternCrate();
ExternCrate(const char *path);
Module& root_module() { return m_crate.root_module(); }
const Module& root_module() const { return m_crate.root_module(); }
};
class CStruct
{
::std::vector<StructItem> m_fields;
public:
const char* name() const { return "TODO"; }
const char* mangled_name() const { return "TODO"; }
const ::std::vector<StructItem>& fields() const { return m_fields; }
};
class Flat
{
::std::vector<CStruct> m_structs;
::std::vector<Function> m_functions;
public:
const ::std::vector<Function>& functions() const { return m_functions; }
const ::std::vector<CStruct>& structs() const { return m_structs; }
};
}
#endif // AST_HPP_INCLUDED
|