-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNamespace.cpp
More file actions
184 lines (165 loc) · 6.21 KB
/
Copy pathNamespace.cpp
File metadata and controls
184 lines (165 loc) · 6.21 KB
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
#include "Namespace.h"
Namespace::Namespace(Root) : parent(nullptr) { }
Namespace::Namespace(const UnqualifiedName &name, Namespace *parentNamespace) : name(name), parent(parentNamespace) { }
bool Namespace::inheritsRecursively(const Namespace *ns) const {
for (Namespace *baseNs : inheritedNamespaces) {
if (baseNs == ns || baseNs->inheritsRecursively(ns))
return true;
}
return false;
}
void Namespace::inheritFrom(Namespace *baseNamespace) {
if (baseNamespace && baseNamespace != this && !baseNamespace->inheritsRecursively(this)) {
for (Namespace *preexistingBase : inheritedNamespaces) {
if (preexistingBase == baseNamespace)
return;
}
inheritedNamespaces.push_back(baseNamespace);
}
}
void Namespace::usingNamespace(QualifiedName &&usingNamespaceName) {
if (usingNamespaceName) {
for (const QualifiedName &preexistingUsingNamespace : usingNamespaceNames) {
if (preexistingUsingNamespace == usingNamespaceName)
return;
}
usingNamespaceNames.push_back((QualifiedName &&) usingNamespaceName);
}
}
const std::vector<QualifiedName> &Namespace::usingNamespaces() const {
return usingNamespaceNames;
}
QualifiedName Namespace::fullName() const {
QualifiedName qualifiedName;
if (parent) {
qualifiedName = parent->fullName();
qualifiedName.append(name);
}
return qualifiedName;
}
Namespace *Namespace::parentNamespace() {
return parent;
}
bool Namespace::makeLocalAlias(const UnqualifiedName &name, const SymbolPtr &originalSymbol) {
if (originalSymbol) {
SymbolPtr &symbol = symbols[name];
if (symbol)
return symbol == originalSymbol;
symbol = originalSymbol;
}
return true;
}
SymbolPtr Namespace::findLocalSymbol(const UnqualifiedName &name) const {
std::map<std::string, SymbolPtr>::const_iterator it = symbols.find(name);
if (it != symbols.end())
return it->second;
return nullptr;
}
SymbolPtr Namespace::requireLocalSymbol(const UnqualifiedName &name) {
SymbolPtr &symbol = symbols[name];
if (!symbol)
symbol = SymbolPtr(new Symbol);
return symbol;
}
SymbolPtr Namespace::establishSymbol(QualifiedName::Ref name, bool establishNamespace) {
if (!name)
return nullptr;
if (name.isAbsolute()) {
if (parent)
return parent->establishSymbol(name, establishNamespace);
name = name.exceptAbsolute();
}
const UnqualifiedName &subname = name.prefix();
name = name.exceptPrefix();
SymbolPtr &symbol = symbols[subname];
if (!symbol)
symbol = SymbolPtr(new Symbol);
if (!name && !establishNamespace)
return symbol;
if (!symbol->ns)
symbol->ns = std::unique_ptr<Namespace>(new Namespace(subname, this));
if (!name)
return symbol;
return symbol->ns->establishSymbol(name, establishNamespace);
}
SymbolPtr Namespace::establishNamespace(QualifiedName::Ref name) {
return establishSymbol(name, true);
}
#define FALLBACK_FLAG_INHERITED 0x01
#define FALLBACK_FLAG_PARENT 0x02
#define FALLBACK_FLAG_USING 0x04
SymbolPtr Namespace::findSymbol(QualifiedName::Ref name, bool localOnly) const {
return findSymbol(name, localOnly ? FALLBACK_FLAG_INHERITED : FALLBACK_FLAG_INHERITED|FALLBACK_FLAG_PARENT|FALLBACK_FLAG_USING);
}
SymbolPtr Namespace::findSymbol(QualifiedName::Ref name, int fallbackFlags) const {
if (!name)
return nullptr;
if (name.isAbsolute()) {
if (parent)
return parent->findSymbol(name, fallbackFlags);
name = name.exceptAbsolute();
fallbackFlags &= FALLBACK_FLAG_INHERITED;
}
std::map<std::string, SymbolPtr>::const_iterator it = symbols.find(name.prefix());
if (it != symbols.end() && it->second) {
QualifiedName::Ref innerName = name.exceptPrefix();
if (!innerName)
return it->second;
else if (it->second->ns)
return it->second->ns->findSymbol(innerName, true);
} else {
if (fallbackFlags&FALLBACK_FLAG_INHERITED) {
for (Namespace *baseNamespace : inheritedNamespaces) {
if (SymbolPtr symbol = baseNamespace->findSymbol(name, FALLBACK_FLAG_INHERITED))
return symbol;
}
}
if ((fallbackFlags&FALLBACK_FLAG_PARENT) && parent) {
if (SymbolPtr symbol = parent->findSymbol(name, fallbackFlags|FALLBACK_FLAG_USING))
return symbol;
}
if (fallbackFlags&FALLBACK_FLAG_USING) {
for (const QualifiedName &usingNamespace : usingNamespaceNames) {
if (SymbolPtr symbol = findSymbol(usingNamespace+name, fallbackFlags&FALLBACK_FLAG_PARENT))
return symbol;
}
}
}
return nullptr;
}
int Namespace::compileTypes(TemplateInstanceCache *templateInstanceCache, int stopMask, const Type **stoppingType) {
int result = 0;
for (const std::map<std::string, SymbolPtr>::value_type &symbol : symbols) {
if (symbol.second) {
if (symbol.second->type && ((result |= symbol.second->type->compile(templateInstanceCache))&stopMask)) {
if (stoppingType)
*stoppingType = symbol.second->type.get();
break;
}
if (symbol.second->ns && ((result |= symbol.second->ns->compileTypes(templateInstanceCache, stopMask, stoppingType))&stopMask))
break;
}
}
return result;
}
std::string Namespace::dump(int indent) const {
std::string indentation;
for (int i = 0; i < indent; ++i)
indentation += " ";
std::string result;
for (const std::map<std::string, SymbolPtr>::value_type &symbol : symbols) {
if (symbol.second) {
result += "\n"+indentation+symbol.first;
if (symbol.second->type)
result += ": "+symbol.second->type->name().fullName();
if (symbol.second->ns) {
std::string subDump = symbol.second->ns->dump(indent+1);
if (subDump.empty())
result += " { }";
else
result += " {"+subDump+"\n"+indentation+"}";
}
}
}
return result;
}