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
|
/*
*/
#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 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;
friend ::std::ostream& operator<<(::std::ostream& os, const PathNode& pn) {
os << pn.m_name;
if( pn.m_params.size() )
{
os << "<";
os << pn.m_params;
os << ">";
}
return os;
}
SERIALISABLE_PROTOTYPES();
};
class Path:
public ::Serialisable
{
public:
enum BindingType {
UNBOUND,
MODULE,
ALIAS,
ENUM,
STRUCT,
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 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; }
const Module& bound_module() const { assert(m_binding_type == MODULE); return *m_binding.module_; }
::std::vector<PathNode>& nodes() { return m_nodes; }
const ::std::vector<PathNode>& nodes() const { return m_nodes; }
PathNode& operator[](size_t idx) { return m_nodes[idx]; }
const PathNode& operator[](size_t idx) const { return m_nodes[idx]; }
SERIALISABLE_PROTOTYPES();
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
|