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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* ast/ast.hpp
* - Core AST header
*/
#ifndef AST_HPP_INCLUDED
#define AST_HPP_INCLUDED
#include <string>
#include <vector>
#include <stdexcept>
#include "../coretypes.hpp"
#include <memory>
#include <map>
#include <unordered_map>
#include <algorithm>
#include "../parse/tokentree.hpp"
#include "types.hpp"
#include <serialise.hpp>
#include <ast/pattern.hpp>
#include <ast/attrs.hpp>
#include <ast/expr.hpp>
#include <ast/item.hpp>
#include <ast/macro.hpp>
#include "generics.hpp"
#include <macro_rules/macro_rules_ptr.hpp>
namespace AST {
class Crate;
class Module;
class Item;
using ::std::unique_ptr;
using ::std::move;
enum eItemType
{
ITEM_TRAIT,
ITEM_STRUCT,
ITEM_FN,
};
struct StructItem
{
::AST::MetaItems m_attrs;
bool m_is_public;
::std::string m_name;
TypeRef m_type;
StructItem()
{
}
StructItem(::AST::MetaItems attrs, bool is_pub, ::std::string name, TypeRef ty):
m_attrs( mv$(attrs) ),
m_is_public(is_pub),
m_name( mv$(name) ),
m_type( mv$(ty) )
{
}
friend ::std::ostream& operator<<(::std::ostream& os, const StructItem& x) {
return os << (x.m_is_public ? "pub " : "") << x.m_name << ": " << x.m_type;
}
};
struct TupleItem
{
::AST::MetaItems m_attrs;
bool m_is_public;
TypeRef m_type;
TupleItem()
{
}
TupleItem(::AST::MetaItems attrs, bool is_pub, TypeRef ty):
m_attrs( mv$(attrs) ),
m_is_public(is_pub),
m_type( mv$(ty) )
{
}
friend ::std::ostream& operator<<(::std::ostream& os, const TupleItem& x) {
return os << (x.m_is_public ? "pub " : "") << x.m_type;
}
};
class TypeAlias
{
GenericParams m_params;
TypeRef m_type;
public:
TypeAlias() {}
TypeAlias(GenericParams params, TypeRef type):
m_params( move(params) ),
m_type( move(type) )
{}
const GenericParams& params() const { return m_params; }
const TypeRef& type() const { return m_type; }
GenericParams& params() { return m_params; }
TypeRef& type() { return m_type; }
};
class Static
{
public:
enum Class
{
CONST,
STATIC,
MUT,
};
private:
Class m_class;
TypeRef m_type;
Expr m_value;
public:
Static():
m_class(CONST)
{}
Static(Class s_class, TypeRef type, Expr value):
m_class(s_class),
m_type( move(type) ),
m_value( move(value) )
{}
const Class& s_class() const { return m_class; }
const TypeRef& type() const { return m_type; }
const Expr& value() const { return m_value; }
TypeRef& type() { return m_type; }
Expr& value() { return m_value; }
};
class Function
{
public:
typedef ::std::vector< ::std::pair<AST::Pattern,TypeRef> > Arglist;
enum class Receiver {
Free,
Value,
BorrowOwned,
BorrowUnique,
BorrowShared,
//RawMut,
//RawConst,
//Box,
};
private:
Span m_span;
//::std::string m_lifetime;
Receiver m_receiver;
GenericParams m_params;
Expr m_code;
TypeRef m_rettype;
Arglist m_args;
public:
Function():
m_receiver(Receiver::Free)
{}
Function(const Function&) = delete;
Function& operator=(const Function&) = delete;
Function(Function&&) = default;
Function& operator=(Function&&) = default;
Function(Span sp, GenericParams params, TypeRef ret_type, Arglist args);
void set_code(Expr code) { m_code = ::std::move(code); }
const GenericParams& params() const { return m_params; }
GenericParams& params() { return m_params; }
const Expr& code() const { return m_code; }
Expr& code() { 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; }
Receiver receiver() const { return m_receiver; }
};
class Trait
{
GenericParams m_params;
::std::vector< Spanned<AST::Path> > m_supertraits;
bool m_is_marker;
NamedList<Item> m_items;
public:
Trait():
m_is_marker(false)
{}
Trait(GenericParams params, ::std::vector< Spanned<Path> > supertraits):
m_params( mv$(params) ),
m_supertraits( mv$(supertraits) ),
m_is_marker(false)
{
}
const GenericParams& params() const { return m_params; }
GenericParams& params() { return m_params; }
const ::std::vector<Spanned<Path> >& supertraits() const { return m_supertraits; }
::std::vector<Spanned<Path> >& supertraits() { return m_supertraits; }
const NamedList<Item>& items() const { return m_items; }
NamedList<Item>& items() { return m_items; }
void add_type(::std::string name, TypeRef type);
void add_function(::std::string name, Function fcn);
void add_static(::std::string name, Static v);
void set_is_marker();
bool is_marker() const;
bool has_named_item(const ::std::string& name, bool& out_is_fcn) const;
};
TAGGED_UNION_EX(EnumVariantData, (), Value,
(
(Value, struct {
::AST::Expr m_value;
}),
(Tuple, struct {
::std::vector<TypeRef> m_sub_types;
}),
(Struct, struct {
::std::vector<StructItem> m_fields;
})
),
(), (),
(
public:
)
);
struct EnumVariant
{
MetaItems m_attrs;
::std::string m_name;
EnumVariantData m_data;
EnumVariant()
{
}
EnumVariant(MetaItems attrs, ::std::string name, Expr&& value):
m_attrs( mv$(attrs) ),
m_name( mv$(name) ),
m_data( EnumVariantData::make_Value({mv$(value)}) )
{
}
EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<TypeRef> sub_types):
m_attrs( mv$(attrs) ),
m_name( ::std::move(name) ),
m_data( EnumVariantData::make_Tuple( {mv$(sub_types)} ) )
{
}
EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<StructItem> fields):
m_attrs( mv$(attrs) ),
m_name( ::std::move(name) ),
m_data( EnumVariantData::make_Struct( {mv$(fields)} ) )
{
}
friend ::std::ostream& operator<<(::std::ostream& os, const EnumVariant& x)
{
os << "EnumVariant(" << x.m_name;
TU_MATCH(EnumVariantData, (x.m_data), (e),
(Value,
os << " = " << e.m_value;
),
(Tuple,
os << "(" << e.m_sub_types << ")";
),
(Struct,
os << " { " << e.m_fields << " }";
)
)
return os << ")";
}
};
class Enum
{
GenericParams m_params;
::std::vector<EnumVariant> m_variants;
public:
Enum() {}
Enum( GenericParams params, ::std::vector<EnumVariant> variants ):
m_params( move(params) ),
m_variants( move(variants) )
{}
const GenericParams& params() const { return m_params; }
const ::std::vector<EnumVariant>& variants() const { return m_variants; }
GenericParams& params() { return m_params; }
::std::vector<EnumVariant>& variants() { return m_variants; }
};
TAGGED_UNION_EX(StructData, (), Struct,
(
(Tuple, struct {
::std::vector<TupleItem> ents;
}),
(Struct, struct {
::std::vector<StructItem> ents;
})
),
(),(),
(
public:
)
);
class Struct
{
GenericParams m_params;
public:
StructData m_data;
Struct() {}
Struct( GenericParams params, ::std::vector<StructItem> fields ):
m_params( move(params) ),
m_data( StructData::make_Struct({mv$(fields)}) )
{}
Struct( GenericParams params, ::std::vector<TupleItem> fields ):
m_params( move(params) ),
m_data( StructData::make_Tuple({mv$(fields)}) )
{}
const GenericParams& params() const { return m_params; }
GenericParams& params() { return m_params; }
TypeRef get_field_type(const char *name, const ::std::vector<TypeRef>& args);
};
class ImplDef
{
Span m_span;
MetaItems m_attrs;
GenericParams m_params;
Spanned<Path> m_trait;
TypeRef m_type;
public:
ImplDef() {}
ImplDef(ImplDef&&) /*noexcept*/ = default;
ImplDef(Span sp, MetaItems attrs, GenericParams params, Spanned<Path> trait_type, TypeRef impl_type):
m_span( mv$(sp) ),
m_attrs( mv$(attrs) ),
m_params( mv$(params) ),
m_trait( mv$(trait_type) ),
m_type( mv$(impl_type) )
{}
ImplDef& operator=(ImplDef&&) = default;
// Accessors
const Span& span() const { return m_span; }
const MetaItems& attrs() const { return m_attrs; }
const GenericParams& params() const { return m_params; }
GenericParams& params() { return m_params; }
const Spanned<Path>& trait() const { return m_trait; }
Spanned<Path>& trait() { return m_trait; }
const TypeRef& type() const { return m_type; }
TypeRef& type() { return m_type; }
friend ::std::ostream& operator<<(::std::ostream& os, const ImplDef& impl);
};
class Impl
{
public:
struct ImplItem {
bool is_pub; // Ignored for trait impls
bool is_specialisable;
::std::string name;
::std::unique_ptr<Item> data;
};
private:
ImplDef m_def;
Span m_span;
::std::vector< ImplItem > m_items;
//NamedList<TypeRef> m_types;
//NamedList<Function> m_functions;
//NamedList<Static> m_statics;
public:
::std::vector<MacroInvocation> m_macro_invocations;
Impl() {}
Impl(Impl&&) /*noexcept*/ = default;
Impl(ImplDef def):
m_def( mv$(def) )
{}
Impl& operator=(Impl&&) = default;
void add_function(bool is_public, bool is_specialisable, ::std::string name, Function fcn);
void add_type(bool is_public, bool is_specialisable, ::std::string name, TypeRef type);
void add_static(bool is_public, bool is_specialisable, ::std::string name, Static v);
void add_macro_invocation( MacroInvocation inv ) {
m_macro_invocations.push_back( mv$(inv) );
}
const ImplDef& def() const { return m_def; }
ImplDef& def() { return m_def; }
const ::std::vector<ImplItem>& items() const { return m_items; }
::std::vector<ImplItem>& items() { return m_items; }
bool has_named_item(const ::std::string& name) const;
friend ::std::ostream& operator<<(::std::ostream& os, const Impl& impl);
private:
};
struct UseStmt
{
Span sp;
::AST::Path path;
::AST::MetaItems attrs;
UseStmt(UseStmt&&) = default;
UseStmt(){}
UseStmt(Span sp, Path p):
sp(sp),
path(p)
{
}
friend ::std::ostream& operator<<(::std::ostream& os, const UseStmt& x);
};
/// Representation of a parsed (and being converted) function
class Module
{
public:
class ItemRef
{
public:
enum Type
{
TAG_None,
TAG_Module,
TAG_Crate,
TAG_TypeAlias,
TAG_Function,
TAG_Trait,
TAG_Struct,
TAG_Enum,
TAG_Static,
TAG_Use,
};
private:
Type m_type;
const void* m_ref;
public:
ItemRef(): m_type(TAG_None) {}
Type tag() const { return m_type; }
bool is_None() const { return m_type == TAG_None; }
const Type& as_None() const { return m_type; } // HACK: Returns &Type in place of &void
#define _(ty,ident) \
ItemRef(const ty& ref): m_type(TAG_##ident), m_ref(&ref) {} \
bool is_##ident() const { return m_type == TAG_##ident; } \
const ty& as_##ident() const { assert(m_type == TAG_##ident); return *(const ty*)m_ref; }
_(AST::Module, Module)
_(::std::string, Crate)
_(AST::TypeAlias, TypeAlias)
_(AST::Function, Function)
_(AST::Trait, Trait)
_(AST::Struct, Struct)
_(AST::Enum, Enum)
_(AST::Static, Static)
_(AST::Named<UseStmt>, Use)
#undef _
friend ::std::ostream& operator<<(::std::ostream& os, const ItemRef& x) {
switch(x.m_type)
{
#define _(ident) case TAG_##ident: return os << "ItemRef(" #ident ")";
_(None)
_(Module)
_(Crate)
_(TypeAlias)
_(Function)
_(Trait)
_(Struct)
_(Enum)
_(Static)
_(Use)
#undef _
}
throw "";
}
};
private:
typedef ::std::vector< Named<UseStmt> > itemlist_use_t;
::AST::Path m_my_path;
// Module-level items
/// General items
::std::vector<Named<Item>> m_items;
/// `use` imports (public and private)
itemlist_use_t m_imports;
/// Macro invocations
::std::vector<MacroInvocation> m_macro_invocations;
/// Impl blocks
::std::vector<Impl> m_impls;
/// Negative impl blocks
::std::vector<ImplDef> m_neg_impls;
// --- Runtime caches and state ---
::std::vector<Module*> m_anon_modules;
::std::vector< NamedNS<const MacroRules*> > m_macro_import_res; // Vec of imported macros (not serialised)
::std::vector< Named<MacroRulesPtr> > m_macros;
public:
char m_index_populated = 0; // 0 = no, 1 = partial, 2 = complete
// TODO: Add "namespace" list (separate to types)
struct IndexEnt {
bool is_pub; // Used as part of glob import checking
bool is_import; // Set if this item has a path that isn't `mod->path() + name`
::AST::Path path;
};
::std::unordered_map< ::std::string, IndexEnt > m_namespace_items;
::std::unordered_map< ::std::string, IndexEnt > m_type_items;
::std::unordered_map< ::std::string, IndexEnt > m_value_items;
public:
Module() {}
Module(::AST::Path path):
m_my_path( mv$(path) )
{
}
bool is_anon() const {
return m_my_path.nodes().back().name()[0] == '#';
}
// Called when module is loaded from a serialised format
void prescan();
/// Create an anon module (for use inside expressions)
::std::unique_ptr<AST::Module> add_anon();
void add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs);
void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs);
void add_alias(bool is_public, UseStmt path, ::std::string name, MetaItems attrs);
void add_typealias(bool is_public, ::std::string name, TypeAlias alias, MetaItems attrs);
void add_static(bool is_public, ::std::string name, Static item, MetaItems attrs);
void add_trait(bool is_public, ::std::string name, Trait item, MetaItems attrs);
void add_struct(bool is_public, ::std::string name, Struct item, MetaItems attrs);
void add_enum(bool is_public, ::std::string name, Enum inst, MetaItems attrs);
void add_function(bool is_public, ::std::string name, Function item, MetaItems attrs);
void add_submod(bool is_public, ::std::string name, Module mod, MetaItems attrs);
void add_impl(Impl impl) {
m_impls.emplace_back( mv$(impl) );
}
void add_neg_impl(ImplDef impl) {
m_neg_impls.emplace_back( mv$(impl) );
}
void add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro);
void add_macro_import(::std::string name, const MacroRules& mr) {
m_macro_import_res.push_back( NamedNS<const MacroRules*>( mv$(name), &mr, false ) );
}
void add_macro_invocation(MacroInvocation item) {
m_macro_invocations.push_back( mv$(item) );
}
unsigned int add_anon_module(Module* mod_ptr) {
auto it = ::std::find(m_anon_modules.begin(), m_anon_modules.end(), mod_ptr);
if( it != m_anon_modules.end() )
return it - m_anon_modules.begin();
m_anon_modules.push_back(mod_ptr);
return m_anon_modules.size()-1;
}
const ::AST::Path& path() const { return m_my_path; }
ItemRef find_item(const ::std::string& needle, bool allow_leaves = true, bool ignore_private_wildcard = true) const;
::std::vector<Named<Item>>& items() { return m_items; }
const ::std::vector<Named<Item>>& items() const { return m_items; }
itemlist_use_t& imports() { return m_imports; }
const itemlist_use_t& imports() const { return m_imports; }
::std::vector<Impl>& impls() { return m_impls; }
const ::std::vector<Impl>& impls() const { return m_impls; }
::std::vector<ImplDef>& neg_impls() { return m_neg_impls; }
const ::std::vector<ImplDef>& neg_impls() const { return m_neg_impls; }
::std::vector<Module*>& anon_mods() { return m_anon_modules; }
const ::std::vector<Module*>& anon_mods() const { return m_anon_modules; }
::std::vector<MacroInvocation>& macro_invs() { return m_macro_invocations; }
NamedList<MacroRulesPtr>& macros() { return m_macros; }
const NamedList<MacroRulesPtr>& macros() const { return m_macros; }
const ::std::vector<NamedNS<const MacroRules*> > macro_imports_res() const { return m_macro_import_res; }
private:
void resolve_macro_import(const Crate& crate, const ::std::string& modname, const ::std::string& macro_name);
};
TAGGED_UNION_EX(Item, (), None,
(
(None, struct {} ),
(MacroInv, MacroInvocation),
(Module, Module),
(Crate, struct {
::std::string name;
}),
(Type, TypeAlias),
(Struct, Struct),
(Enum, Enum),
(Trait, Trait),
(Function, Function),
(Static, Static)
),
(, attrs(mv$(x.attrs))), (attrs = mv$(x.attrs);),
(
public:
MetaItems attrs;
Span span;
)
);
struct ImplRef
{
const Impl& impl;
::std::vector<TypeRef> params;
ImplRef(const Impl& impl, ::std::vector<TypeRef> params):
impl(impl),
params(params)
{}
::rust::option<char> find_named_item(const ::std::string& name) const;
};
} // namespace AST
class GenericResolveClosure
{
const ::AST::GenericParams& m_params;
const ::std::vector<TypeRef>& m_args;
public:
GenericResolveClosure(const AST::GenericParams& params, const ::std::vector<TypeRef>& args):
m_params(params),
m_args(args)
{}
const TypeRef& operator()(const char *argname) {
for(unsigned int i = 0; i < m_params.ty_params().size(); i ++)
{
if( m_params.ty_params()[i].name() == argname ) {
return m_args.at(i);
}
}
throw ::std::runtime_error("BUGCHECK - Unknown arg in field type");
}
};
#endif // AST_HPP_INCLUDED
|