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
|
/*
*/
#ifndef AST_PATH_HPP_INCLUDED
#define AST_PATH_HPP_INCLUDED
#include "../common.hpp"
#include <string>
#include <stdexcept>
#include <vector>
#include <initializer_list>
#include <cassert>
#include <serialise.hpp>
class TypeRef;
namespace AST {
class Crate;
class Module;
class TypeAlias;
class Enum;
class Struct;
class Trait;
class Static;
class Function;
class PathNode:
public ::Serialisable
{
::std::string m_name;
::std::vector<TypeRef> m_params;
public:
PathNode() {}
PathNode(::std::string name, ::std::vector<TypeRef> args = {});
const ::std::string& name() const;
::std::vector<TypeRef>& args() { return m_params; }
const ::std::vector<TypeRef>& args() const;
bool operator==(const PathNode& x) const;
void print_pretty(::std::ostream& os) const;
friend ::std::ostream& operator<<(::std::ostream& os, const PathNode& pn);
SERIALISABLE_PROTOTYPES();
};
class Path:
public ::Serialisable
{
public:
enum BindingType {
UNBOUND,
MODULE,
ALIAS,
ENUM,
STRUCT,
TRAIT,
STRUCT_METHOD,
ENUM_VAR,
FUNCTION,
STATIC,
};
private:
enum Class {
RELATIVE,
ABSOLUTE,
LOCAL,
};
/// The crate defining the root of this path (used for path resolution)
::std::string m_crate;
/// Path class (absolute, relative, local)
/// - Absolute is "relative" to the crate root
/// - Relative doesn't have a set crate (and can't be resolved)
/// - Local is a special case to handle possible use of local varaibles
Class m_class;
::std::vector<PathNode> m_nodes;
BindingType m_binding_type = UNBOUND;
union {
const Module* module_;
const Enum* enum_;
const Struct* struct_;
const Trait* trait_;
const Static* static_;
const Function* func_;
struct {
const Enum* enum_;
unsigned int idx;
} enumvar;
const TypeAlias* alias_;
} m_binding;
public:
Path():
m_class(RELATIVE)
{}
struct TagAbsolute {};
Path(TagAbsolute):
m_class(ABSOLUTE)
{}
struct TagLocal {};
Path(TagLocal, ::std::string name):
m_class(LOCAL),
m_nodes({PathNode(name, {})})
{}
Path(::std::initializer_list<PathNode> l):
m_class(ABSOLUTE),
m_nodes(l)
{}
Path(::std::string crate, ::std::vector<PathNode> nodes):
m_crate( ::std::move(crate) ),
m_class(ABSOLUTE),
m_nodes( ::std::move(nodes) )
{}
void set_crate(::std::string crate) {
if( m_crate == "" ) {
m_crate = crate;
DEBUG("crate set to " << m_crate);
}
}
static Path add_tailing(const Path& a, const Path& b) {
Path ret(a);
ret[ret.size()-1].args() = b[0].args();
for(unsigned int i = 1; i < b.m_nodes.size(); i ++)
ret.m_nodes.push_back(b.m_nodes[i]);
return ret;
}
Path operator+(PathNode&& pn) const {
Path tmp;
tmp.append( ::std::move(pn) );
return Path(*this) += tmp;
}
Path operator+(const ::std::string& s) const {
Path tmp;
tmp.append(PathNode(s, {}));
return Path(*this) += tmp;
}
Path operator+(const Path& x) const {
return Path(*this) += x;
}
Path& operator+=(const Path& x);
void append(PathNode node) {
m_nodes.push_back(node);
}
void resolve(const Crate& crate);
bool is_absolute() const { return m_class == ABSOLUTE; }
bool is_relative() const { return m_class == RELATIVE; }
size_t size() const { return m_nodes.size(); }
bool is_bound() const { return m_binding_type != UNBOUND; }
BindingType binding_type() const { return m_binding_type; }
#define _(t, n, v) const t& bound_##n() const { assert(m_binding_type == v); return *m_binding.n##_; }
_(Module, module, MODULE)
_(Trait, trait, TRAIT)
_(Struct, struct, STRUCT)
//_(Enum, enum, ENUM)
_(Function, func, FUNCTION)
_(Static, static, STATIC)
_(TypeAlias, alias, ALIAS)
#undef _
const Enum& bound_enum() const {
assert(m_binding_type == ENUM || m_binding_type == ENUM_VAR); // Kinda evil, given that it has its own union entry
return *m_binding.enum_;
}
const unsigned int bound_idx() const {
assert(m_binding_type == ENUM_VAR);
return m_binding.enumvar.idx;
}
::std::vector<PathNode>& nodes() { return m_nodes; }
const ::std::vector<PathNode>& nodes() const { return m_nodes; }
PathNode& operator[](int idx) { if(idx>=0) return m_nodes[idx]; else return m_nodes[size()+idx]; }
const PathNode& operator[](int idx) const { if(idx>=0) return m_nodes[idx]; else return m_nodes[size()+idx]; }
bool operator==(const Path& x) const;
SERIALISABLE_PROTOTYPES();
void print_pretty(::std::ostream& os) const;
friend ::std::ostream& operator<<(::std::ostream& os, const Path& path);
friend ::Serialiser& operator<<(Serialiser& s, Path::Class pc);
friend void operator>>(Deserialiser& s, Path::Class& pc);
private:
void bind_module(const Module& mod);
void bind_enum(const Enum& ent, const ::std::vector<TypeRef>& args);
void bind_enum_var(const Enum& ent, const ::std::string& name, const ::std::vector<TypeRef>& args);
void bind_struct(const Struct& ent, const ::std::vector<TypeRef>& args);
void bind_static(const Static& ent);
};
} // namespace AST
#endif
|