[r368]: cmajor++ / Cm / Cm.Core / Namespace.cpp Maximize Restore History

Download this file

Namespace.cpp    265 lines (221 with data), 6.9 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
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
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Cm.Core/Namespace.hpp>
#include <Cm.Core/Scope.hpp>
#include <Cm.Core/Assert.hpp>
#include <Cm.Core/Error.hpp>
#include <Cm.Core/TypeChecker.hpp>
#include <Cm.Core/Clone.hpp>
namespace Cm { namespace Core {
Namespace::Namespace(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_):
Object(name_, pos_, enclosingScope_, fileScope_), scope(new Cm::Core::Scope(name_, pos_, enclosingScope_, fileScope_, NamespacePtr(), ScopeKind::namespace_))
{
}
ObjectPtr Namespace::Clone(CloneContext& context) const
{
CM_CORE_ASSERT(false, nullptr);
return ObjectPtr();
}
void Namespace::Dispose()
{
if (!Disposed())
{
Object::Dispose();
scope->Dispose();
scope.reset();
}
}
void Namespace::AddToScope(bool set)
{
NamespacePtr ns = GlobalScope()->GetNamespace(FullName(), Pos());
if (!ns)
{
ns = std::static_pointer_cast<Namespace>(shared_from_this());
EnclosingScope()->AddNamespace(ns);
}
GetScope()->SetNs(ns);
}
std::string Namespace::FullName() const
{
std::string fullName = scope->FullName();
SetFullNameCache(fullName);
return fullName;
}
void Namespace::TypeCheckSignature(TypeCheckContext& context)
{
context.AddNamespace(std::static_pointer_cast<Namespace>(shared_from_this()));
if (!context.DontTypeCheckNamespace())
{
Object::TypeCheckSignature(context);
scope->TypeCheckSignature(context);
}
}
void Namespace::TypeCheckBody(TypeCheckContext& context)
{
Object::TypeCheckBody(context);
scope->TypeCheck(context);
}
ScopePtr GlobalScope()
{
return GlobalNamespace::Instance()->GetScope();
}
GlobalNamespacePtr GlobalNamespace::instance;
std::stack<GlobalNamespacePtr> GlobalNamespace::instanceStack;
void GlobalNamespace::Init()
{
instanceStack.push(instance);
instance = GlobalNamespacePtr(new GlobalNamespace());
instance->GetScope()->SetNs(instance);
}
void GlobalNamespace::Done()
{
instance->Dispose();
instance = instanceStack.top();
instanceStack.pop();
}
GlobalNamespacePtr GlobalNamespace::Instance()
{
CM_CORE_ASSERT(instance, nullptr);
return instance;
}
GlobalNamespace::GlobalNamespace(): Namespace("", Position(), ScopePtr(), ScopePtr())
{
GetScope()->SetKind(ScopeKind::global);
}
SystemNamespacePtr SystemNamespace::instance;
std::stack<SystemNamespacePtr> SystemNamespace::instanceStack;
void SystemNamespace::Init()
{
instanceStack.push(instance);
instance = SystemNamespacePtr(new SystemNamespace());
instance->GetScope()->SetNs(instance);
GlobalScope()->AddNamespace(instance);
}
void SystemNamespace::Done()
{
instance->Dispose();
instance = instanceStack.top();
instanceStack.pop();
}
SystemNamespacePtr SystemNamespace::Instance()
{
CM_CORE_ASSERT(instance, nullptr);
return instance;
}
SystemNamespace::SystemNamespace(): Namespace("System", Position(), GlobalScope(), ScopePtr())
{
GetScope()->SetEnclosingScope(GlobalScope());
}
ScopePtr SystemScope()
{
return SystemNamespace::Instance()->GetScope();
}
NamespaceImport::NamespaceImport(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_):
Object(name_, pos_, enclosingScope_, fileScope_)
{
}
ObjectPtr NamespaceImport::Clone(CloneContext& context) const
{
return ObjectPtr(new NamespaceImport(Name(), Pos(), context.CurrentScope(), FileScope()));
}
void NamespaceImport::AddToScope(bool set)
{
#if !defined(NDEBUG) || defined(ASSERTIONS)
const char* fullName = GetFullNameFor(shared_from_this());
#endif
NamespaceImportPtr thisImport = std::static_pointer_cast<NamespaceImport>(shared_from_this());
CM_CORE_ASSERT(FileScope(), fullName);
if (!FileScope()->GetObject(Name(), Lookup::this_scope, Pos()))
{
FileScope()->AddObject(thisImport);
FileScope()->AddNamespaceImport(thisImport);
}
}
bool NamespaceImport::HasTypeCheckReplacement(TypeCheckContext& context) const
{
return true;
}
ObjectPtr NamespaceImport::GetTypeCheckReplacement(TypeCheckContext& context) const
{
return GlobalScope()->GetNamespace(Name(), Pos());
}
void NamespaceImport::TypeCheckSignature(TypeCheckContext& context)
{
#if !defined(NDEBUG) || defined(ASSERTIONS)
const char* fullName = GetFullNameFor(shared_from_this());
#endif
Object::TypeCheckSignature(context);
NamespacePtr ns = GlobalScope()->GetNamespace(Name(), Pos());
if (ns)
{
CM_CORE_ASSERT(FileScope(), fullName);
FileScope()->Import(ns->GetScope());
}
else
{
throw TypeCheckException(NAMESPACE_NOT_FOUND, "namespace '" + Name() + "' not found", Pos());
}
}
Alias::Alias(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, const ObjectPtr& referred_):
Object(name_, pos_, enclosingScope_, fileScope_), referred(referred_)
{
}
void Alias::Dispose()
{
if (!Disposed())
{
Object::Dispose();
if (referred)
{
referred->Dispose();
referred.reset();
}
}
}
ObjectPtr Alias::Clone(CloneContext& context) const
{
return ObjectPtr(new Alias(Name(), Pos(), EnclosingScope(), FileScope(), referred->Clone(context)));
}
void Alias::AddToScope(bool set)
{
#if !defined(NDEBUG) || defined(ASSERTIONS)
const char* fullName = GetFullNameFor(shared_from_this());
#endif
CM_CORE_ASSERT(FileScope(), fullName);
FileScope()->AddObject(shared_from_this());
}
bool Alias::HasTypeCheckReplacement(TypeCheckContext& context) const
{
return true;
}
ObjectPtr Alias::GetTypeCheckReplacement(TypeCheckContext& context) const
{
return referred;
}
void Alias::TypeCheckName(TypeCheckContext& context)
{
TypeChecker::TypeCheck(referred, context);
}
void Alias::TypeCheckSignature(TypeCheckContext& context)
{
TypeChecker::TypeCheck(referred, context);
}
TypePtr Alias::GetType() const
{
return referred->GetType();
}
void NamespaceInit()
{
GlobalNamespace::Init();
SystemNamespace::Init();
}
void NamespaceDone()
{
SystemNamespace::Done();
GlobalNamespace::Done();
}
} } // namespace Cm::Core