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
|
/*
*/
#include <ast/ast.hpp>
#include <ast/crate.hpp>
#include <main_bindings.hpp>
#include <synext.hpp>
#include <map>
::std::map< ::std::string, ::std::unique_ptr<ExpandDecorator> > g_decorators;
::std::map< ::std::string, ::std::unique_ptr<ExpandProcMacro> > g_macros;
void Register_Synext_Decorator(::std::string name, ::std::unique_ptr<ExpandDecorator> handler) {
g_decorators[name] = mv$(handler);
}
void Register_Synext_Macro(::std::string name, ::std::unique_ptr<ExpandProcMacro> handler) {
g_macros[name] = mv$(handler);
}
void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate, const ::AST::Path& path, ::AST::Module& mod, ::AST::Item& item)
{
for( auto& a : attrs.m_items )
{
for( auto& d : g_decorators ) {
if( d.first == a.name() && d.second->stage() == stage ) {
d.second->handle(a, crate, path, mod, item);
}
}
}
}
void Expand_Mod(bool is_early, ::AST::Crate& crate, ::AST::Path modpath, ::AST::Module& mod)
{
TRACE_FUNCTION_F("is_early = " << is_early << ", modpath = " << modpath);
// 1. Macros first
for( auto& mi : mod.macro_invs() )
{
if( mi.name() == "" )
continue ;
for( auto& m : g_macros ) {
if( mi.name() == m.first && m.second->expand_early() == is_early ) {
m.second->expand(mi.input_ident(), mi.input_tt(), mod, MacroPosition::Item);
mi.clear();
break;
}
}
if( ! is_early && mi.name() != "" ) {
// TODO: Error - Unknown macro name
throw ::std::runtime_error( FMT("Unknown macro '" << mi.name() << "'") );
}
}
// 2. General items
DEBUG("Items");
for( auto& i : mod.items() )
{
::AST::Path path = modpath + i.name;
Expand_Attrs(i.data.attrs, (is_early ? AttrStage::EarlyPre : AttrStage::LatePre), crate, path, mod, i.data);
TU_MATCH(::AST::Item, (i.data), (e),
(None,
// Skip, nothing
),
(Module,
Expand_Mod(is_early, crate, path, e.e);
),
(Crate,
// Skip, no recursion
),
(Struct,
// TODO: Struct items
),
(Enum,
),
(Trait,
),
(Type,
// TODO: Do type aliases require recursion?
),
(Function,
// TODO:
),
(Static,
// TODO:
)
)
Expand_Attrs(i.data.attrs, (is_early ? AttrStage::EarlyPost : AttrStage::LatePost), crate, path, mod, i.data);
}
// 3. Post-recurse macros (everything else)
}
void Expand(::AST::Crate& crate)
{
// 1. Crate attributes
for( auto& a : crate.m_attrs.m_items )
{
for( auto& d : g_decorators ) {
if( d.first == a.name() && d.second->stage() == AttrStage::EarlyPre ) {
d.second->handle(a, crate);
}
}
}
// 2. Module attributes
for( auto& a : crate.m_attrs.m_items )
{
for( auto& d : g_decorators ) {
if( d.first == a.name() && d.second->stage() == AttrStage::EarlyPre ) {
//d.second->handle(a, crate, ::AST::Path(), crate.m_root_module, crate.m_root_module);
}
}
}
// 3. Module tree
Expand_Mod(true , crate, ::AST::Path(), crate.m_root_module);
Expand_Mod(false, crate, ::AST::Path(), crate.m_root_module);
// Post-process
#if 0
for( auto& a : crate.m_attrs.m_items )
{
for( auto& d : g_decorators ) {
if( d.first == a.name() && d.second->expand_before_macros() == false ) {
//d.second->handle(a, crate, ::AST::Path(), crate.m_root_module, crate.m_root_module);
}
}
}
for( auto& a : crate.m_attrs.m_items )
{
for( auto& d : g_decorators ) {
if( d.first == a.name() && d.second->expand_before_macros() == false ) {
d.second->handle(a, crate);
}
}
}
#endif
}
|