-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathScope.cpp
More file actions
133 lines (103 loc) · 2.95 KB
/
Scope.cpp
File metadata and controls
133 lines (103 loc) · 2.95 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
#include "Scope.h"
#include "Struct.h"
#include "Output.h"
using namespace std;
Scope::Scope(Scope* parent, bool newContext)
{
m_parent = parent;
m_root = ((!parent) || newContext) ? this : parent->m_root;
}
Scope* Scope::Duplicate(DuplicateContext& dup)
{
Scope* scope = new Scope(NULL, true);
for (vector< Ref<Variable> >::iterator i = m_vars.begin(); i != m_vars.end(); i++)
scope->m_vars.push_back((*i)->Duplicate(dup));
for (map< string, Ref<Variable> >::iterator i = m_currentScopeVars.begin(); i != m_currentScopeVars.end(); i++)
scope->m_currentScopeVars[i->first] = i->second->Duplicate(dup);
return scope;
}
bool Scope::IsVariableDefined(const string& name) const
{
map< string, Ref<Variable> >::const_iterator i = m_currentScopeVars.find(name);
if (i != m_currentScopeVars.end())
return true;
if (!m_parent)
return false;
return m_parent->IsVariableDefined(name);
}
bool Scope::IsVariableDefinedInCurrentScope(const string& name) const
{
map< string, Ref<Variable> >::const_iterator i = m_currentScopeVars.find(name);
return i != m_currentScopeVars.end();
}
Variable* Scope::GetVariable(const string& name) const
{
map< string, Ref<Variable> >::const_iterator i = m_currentScopeVars.find(name);
if (i != m_currentScopeVars.end())
return i->second;
if (!m_parent)
return NULL;
return m_parent->GetVariable(name);
}
Variable* Scope::DefineVariable(VariableClass cls, Type* type, const string& name)
{
Variable* var = new Variable(cls, type, name);
if (cls == VAR_FILE_SCOPE)
{
// Static variable, always add to global list (but still in current scope for namespace reasons)
Scope* topmost = m_root;
while (topmost->m_parent)
topmost = topmost->m_parent;
topmost->m_vars.push_back(var);
}
else
{
// Normal variable, add to current context
m_root->m_vars.push_back(var);
}
m_currentScopeVars[name] = var;
return var;
}
void Scope::DefineVariable(Variable* var)
{
m_root->m_vars.push_back(var);
m_currentScopeVars[var->GetName()] = var;
}
void Scope::Serialize(OutputBlock* output)
{
output->WriteInteger(m_vars.size());
for (vector< Ref<Variable> >::iterator i = m_vars.begin(); i != m_vars.end(); i++)
(*i)->Serialize(output);
output->WriteInteger(m_currentScopeVars.size());
for (map< string, Ref<Variable> >::iterator i = m_currentScopeVars.begin(); i != m_currentScopeVars.end(); i++)
{
output->WriteString(i->first);
i->second->Serialize(output);
}
}
bool Scope::Deserialize(InputBlock* input)
{
size_t varCount;
if (!input->ReadNativeInteger(varCount))
return false;
for (size_t i = 0; i < varCount; i++)
{
Variable* var = Variable::Deserialize(input);
if (!var)
return false;
m_vars.push_back(var);
}
if (!input->ReadNativeInteger(varCount))
return false;
for (size_t i = 0; i < varCount; i++)
{
string name;
if (!input->ReadString(name))
return false;
Variable* var = Variable::Deserialize(input);
if (!var)
return false;
m_currentScopeVars[name] = var;
}
return true;
}