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
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* hir/hir.cpp
* - Processed module tree (High-level Intermediate Representation)
*
* HIR type helper code
*/
#include "hir.hpp"
#include <algorithm>
#include <hir_typeck/common.hpp>
#include <hir_typeck/expr_visit.hpp> // for invoking typecheck
#include "item_path.hpp"
#include "expr_state.hpp"
#include <hir_expand/main_bindings.hpp>
#include <mir/main_bindings.hpp>
namespace HIR {
::std::ostream& operator<<(::std::ostream& os, const ::HIR::Literal& v)
{
TU_MATCH(::HIR::Literal, (v), (e),
(Invalid,
os << "!";
),
(Defer,
os << "?";
),
(List,
os << "[";
for(const auto& val : e)
os << " " << val << ",";
os << " ]";
),
(Variant,
os << "#" << e.idx << ":" << *e.val;
),
(Integer,
os << e;
),
(Float,
os << e;
),
(BorrowPath,
os << "&" << e;
),
(BorrowData,
os << "&" << *e;
),
(String,
os << "\"" << e << "\"";
)
)
return os;
}
bool operator==(const Literal& l, const Literal& r)
{
if( l.tag() != r.tag() )
return false;
TU_MATCH(::HIR::Literal, (l,r), (le,re),
(Invalid,
),
(Defer,
),
(List,
if( le.size() != re.size() )
return false;
for(unsigned int i = 0; i < le.size(); i ++)
if( le[i] != re[i] )
return false;
),
(Variant,
if( le.idx != re.idx )
return false;
return *le.val == *re.val;
),
(Integer,
return le == re;
),
(Float,
return le == re;
),
(BorrowPath,
return le == re;
),
(BorrowData,
return *le == *re;
),
(String,
return le == re;
)
)
return true;
}
::std::ostream& operator<<(::std::ostream& os, const Struct::Repr& x) {
os << "repr(";
switch(x)
{
case Struct::Repr::Rust: os << "Rust"; break;
case Struct::Repr::C: os << "C"; break;
case Struct::Repr::Packed: os << "packed"; break;
case Struct::Repr::Simd: os << "simd"; break;
case Struct::Repr::Aligned: os << "align(?)"; break;
case Struct::Repr::Transparent: os << "transparent"; break;
}
os << ")";
return os;
}
}
size_t HIR::Enum::find_variant(const ::std::string& name) const
{
if( m_data.is_Value() )
{
const auto& e = m_data.as_Value();
auto it = ::std::find_if(e.variants.begin(), e.variants.end(), [&](const auto& x){ return x.name == name; });
if( it == e.variants.end() )
return SIZE_MAX;
return it - e.variants.begin();
}
else
{
const auto& e = m_data.as_Data();
auto it = ::std::find_if(e.begin(), e.end(), [&](const auto& x){ return x.name == name; });
if( it == e.end() )
return SIZE_MAX;
return it - e.begin();
}
}
bool HIR::Enum::is_value() const
{
return this->m_data.is_Value();
}
uint32_t HIR::Enum::get_value(size_t idx) const
{
if( m_data.is_Value() )
{
const auto& e = m_data.as_Value();
assert(idx < e.variants.size());
return e.variants[idx].val;
}
else
{
assert(!"TODO: Enum::get_value on non-value enum?");
throw "";
}
}
const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path(const Span& sp, const char* name) const
{
auto it = this->m_lang_items.find( name );
if( it == this->m_lang_items.end() ) {
ERROR(sp, E0000, "Undefined language item '" << name << "' required");
}
return it->second;
}
const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path_opt(const char* name) const
{
static ::HIR::SimplePath empty_path;
auto it = this->m_lang_items.find( name );
if( it == this->m_lang_items.end() ) {
return empty_path;
}
return it->second;
}
const ::HIR::TypeItem& ::HIR::Crate::get_typeitem_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_crate_name, bool ignore_last_node) const
{
ASSERT_BUG(sp, path.m_components.size() > 0u, "get_typeitem_by_path received invalid path - " << path);
ASSERT_BUG(sp, path.m_components.size() > (ignore_last_node ? 1u : 0u), "get_typeitem_by_path received invalid path - " << path);
const ::HIR::Module* mod;
if( !ignore_crate_name && path.m_crate_name != m_crate_name ) {
ASSERT_BUG(sp, m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded for " << path);
mod = &m_ext_crates.at(path.m_crate_name).m_data->m_root_module;
}
else {
mod = &this->m_root_module;
}
for( unsigned int i = 0; i < path.m_components.size() - (ignore_last_node ? 2 : 1); i ++ )
{
const auto& pc = path.m_components[i];
auto it = mod->m_mod_items.find( pc );
if( it == mod->m_mod_items.end() ) {
BUG(sp, "Couldn't find component " << i << " of " << path);
}
TU_IFLET(::HIR::TypeItem, it->second->ent, Module, e,
mod = &e;
)
else {
BUG(sp, "Node " << i << " of path " << path << " wasn't a module");
}
}
auto it = mod->m_mod_items.find( ignore_last_node ? path.m_components[path.m_components.size()-2] : path.m_components.back() );
if( it == mod->m_mod_items.end() ) {
BUG(sp, "Could not find type name in " << path);
}
return it->second->ent;
}
const ::HIR::Module& ::HIR::Crate::get_mod_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_last_node/*=false*/) const
{
if( ignore_last_node )
{
ASSERT_BUG(sp, path.m_components.size() > 0, "get_mod_by_path received invalid path with ignore_last_node=true - " << path);
}
if( path.m_components.size() == (ignore_last_node ? 1 : 0) )
{
if( path.m_crate_name != m_crate_name )
{
ASSERT_BUG(sp, m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded");
return m_ext_crates.at(path.m_crate_name).m_data->m_root_module;
}
else
{
return this->m_root_module;
}
}
else
{
const auto& ti = this->get_typeitem_by_path(sp, path, false, ignore_last_node);
TU_IFLET(::HIR::TypeItem, ti, Module, e,
return e;
)
else {
if( ignore_last_node )
BUG(sp, "Parent path of " << path << " didn't point to a module");
else
BUG(sp, "Module path " << path << " didn't point to a module");
}
}
}
const ::HIR::Trait& ::HIR::Crate::get_trait_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Trait, e,
return e;
)
else {
BUG(sp, "Trait path " << path << " didn't point to a trait");
}
}
const ::HIR::Struct& ::HIR::Crate::get_struct_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Struct, e,
return e;
)
else {
BUG(sp, "Struct path " << path << " didn't point to a struct");
}
}
const ::HIR::Union& ::HIR::Crate::get_union_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Union, e,
return e;
)
else {
BUG(sp, "Path " << path << " didn't point to a union");
}
}
const ::HIR::Enum& ::HIR::Crate::get_enum_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_typeitem_by_path(sp, path);
TU_IFLET(::HIR::TypeItem, ti, Enum, e,
return e;
)
else {
BUG(sp, "Enum path " << path << " didn't point to an enum");
}
}
const ::HIR::ValueItem& ::HIR::Crate::get_valitem_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_crate_name) const
{
if( path.m_components.size() == 0) {
BUG(sp, "get_valitem_by_path received invalid path");
}
const ::HIR::Module* mod;
if( !ignore_crate_name && path.m_crate_name != m_crate_name ) {
ASSERT_BUG(sp, m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded");
mod = &m_ext_crates.at(path.m_crate_name).m_data->m_root_module;
}
else {
mod = &this->m_root_module;
}
for( unsigned int i = 0; i < path.m_components.size() - 1; i ++ )
{
const auto& pc = path.m_components[i];
auto it = mod->m_mod_items.find( pc );
if( it == mod->m_mod_items.end() ) {
BUG(sp, "Couldn't find component " << i << " of " << path);
}
TU_IFLET(::HIR::TypeItem, it->second->ent, Module, e,
mod = &e;
)
else {
BUG(sp, "Node " << i << " of path " << path << " wasn't a module");
}
}
auto it = mod->m_value_items.find( path.m_components.back() );
if( it == mod->m_value_items.end() ) {
BUG(sp, "Could not find value name " << path);
}
return it->second->ent;
}
const ::HIR::Function& ::HIR::Crate::get_function_by_path(const Span& sp, const ::HIR::SimplePath& path) const
{
const auto& ti = this->get_valitem_by_path(sp, path);
TU_IFLET(::HIR::ValueItem, ti, Function, e,
return e;
)
else {
BUG(sp, "Function path " << path << " didn't point to an function");
}
}
|