source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/include/Variable.h@ 580

Last change on this file since 580 was 580, checked in by dai_9181, 16 years ago

VariableクラスのLexicalAnalyzerクラスへの依存性をなくした。

File size: 5.2 KB
Line 
1#pragma once
2
3class Variable : public Symbol
4{
5 Type type;
6 bool isConst;
7 bool isRef;
8 bool isArray;
9 Subscripts subscripts;
10
11 bool isParameter;
12 bool hasInitData;
13
14 //コンストラクタ用パラメータ
15 std::string paramStrForConstructor;
16
17
18 /* --- オフセット ---
19 ※グローバル変数で初期バッファがない場合は最上位ビットに1がセットされ、
20 初期バッファの有無が識別される。
21 (その後、スケジュール実行により、実際の配置に並び替えられる)*/
22 int offset;
23
24
25 //レキシカルスコープ用
26 int scopeStartAddress;
27 int scopeEndAddress;
28 int scopeLevel;
29
30
31 // XMLシリアライズ用
32private:
33 friend class boost::serialization::access;
34 template<class Archive> void serialize(Archive& ar, const unsigned int version)
35 {
36 trace_for_serialize( "serializing - Variable" );
37
38 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Symbol );
39 ar & BOOST_SERIALIZATION_NVP( type );
40 ar & BOOST_SERIALIZATION_NVP( isConst );
41 ar & BOOST_SERIALIZATION_NVP( isRef );
42 ar & BOOST_SERIALIZATION_NVP( isArray );
43 ar & BOOST_SERIALIZATION_NVP( subscripts );
44 ar & BOOST_SERIALIZATION_NVP( isParameter );
45 ar & BOOST_SERIALIZATION_NVP( hasInitData );
46 ar & BOOST_SERIALIZATION_NVP( paramStrForConstructor );
47 ar & BOOST_SERIALIZATION_NVP( offset );
48 ar & BOOST_SERIALIZATION_NVP( scopeStartAddress );
49 ar & BOOST_SERIALIZATION_NVP( scopeEndAddress );
50 ar & BOOST_SERIALIZATION_NVP( scopeLevel );
51 }
52
53public:
54 Variable( const Symbol &symbol, const Type &type, bool isConst, bool isRef, const std::string &paramStrForConstructor, bool hasInitData );
55 Variable( const NamespaceScopes &namespaceScopes, const std::string &name, const Type &type, bool isConst, bool isRef, const std::string &paramStrForConstructor, bool hasInitData );
56 Variable( const Variable &var );
57 Variable();
58
59 void SetArray( const Subscripts &subscripts ){
60 isArray = true;
61 this->subscripts = subscripts;
62 }
63
64 const Type &GetType() const
65 {
66 return type;
67 }
68 void ConstOff(){
69 isConst = false;
70 }
71 void ConstOn(){
72 isConst = true;
73 }
74 bool IsConst() const
75 {
76 return isConst;
77 }
78 bool IsRef() const
79 {
80 return isRef;
81 }
82 bool IsArray()const
83 {
84 return isArray;
85 }
86 const Subscripts &GetSubscripts() const
87 {
88 return subscripts;
89 }
90
91 void ThisIsParameter(){
92 isParameter = true;
93 }
94 bool IsParameter() const
95 {
96 return isParameter;
97 }
98 bool HasInitData() const
99 {
100 return hasInitData;
101 }
102
103
104 int GetMemorySize() const
105 {
106 if( isRef || isParameter ){
107 return PTR_SIZE;
108 }
109
110 int size = type.GetSize();
111
112 if( isArray ){
113 int num = 1;
114 for( int i=0; i<(int)subscripts.size(); i++){
115 num *= subscripts[i]+1;
116 }
117 size *= num;
118 }
119
120 if( size % PTR_SIZE ){
121 size += PTR_SIZE-(size%PTR_SIZE);
122 }
123
124 return size;
125 }
126
127 int GetOffsetAddress() const
128 {
129 return offset;
130 }
131 void SetOffsetAddress( int offset )
132 {
133 this->offset = offset;
134 }
135
136 const std::string &GetParamStrForConstructor() const
137 {
138 return paramStrForConstructor;
139 }
140
141 //レキシカルスコープ用
142 int GetScopeStartAddress() const
143 {
144 return scopeStartAddress;
145 }
146 void SetScopeStartAddress( int scopeStartAddress )
147 {
148 this->scopeStartAddress = scopeStartAddress;
149 }
150 int GetScopeEndAddress() const
151 {
152 return scopeEndAddress;
153 }
154 void SetScopeEndAddress( int scopeEndAddress )
155 {
156 this->scopeEndAddress = scopeEndAddress;
157 }
158 int GetScopeLevel() const
159 {
160 return scopeLevel;
161 }
162 void SetScopeLevel( int scopeLevel )
163 {
164 this->scopeLevel = scopeLevel;
165 }
166
167
168 bool isLiving;
169 int source_code_address;
170
171
172 static int GetSubScriptCounts( const Subscripts &subscripts ){
173 // 配列の要素数を取得
174 int i,i2;
175 for(i=0,i2=1;i<(int)subscripts.size();i++){
176 i2 *= subscripts[i]+1;
177 }
178 return i2;
179 }
180};
181
182class Variables : public std::vector<Variable *>
183{
184protected:
185 int allSize;
186
187 // XMLシリアライズ用
188private:
189 friend class boost::serialization::access;
190 template<class Archive> void serialize(Archive& ar, const unsigned int version)
191 {
192 trace_for_serialize( "serializing - Variables" );
193
194 ar & boost::serialization::make_nvp("vector_Variable", boost::serialization::base_object<vector<Variable *>>(*this));
195 }
196
197public:
198 Variables()
199 : allSize( 0 )
200 {
201 }
202 ~Variables(){
203 Clear();
204 }
205
206 void Clear(){
207 for( int i=0; i<(int)this->size(); i++ ){
208 delete (*this)[i];
209 }
210
211 allSize = 0;
212 clear();
213 }
214 void PullOutAll()
215 {
216 clear();
217 }
218
219 int GetAllSize() const
220 {
221 return allSize;
222 }
223
224 bool DuplicateCheck( const Symbol &symbol, int nowScopeLevel ) const;
225
226 const Variable *BackSearch( const Symbol &symbol, int nowScopeLevel ) const;
227 const Variable *Find( const Symbol &symbol )const;
228};
229
230class GlobalVars : public Variables
231{
232public:
233 Jenga::Common::Binary initAreaBuffer;
234
235 // XMLシリアライズ用
236private:
237 friend class boost::serialization::access;
238 template<class Archive> void serialize(Archive& ar, const unsigned int version)
239 {
240 trace_for_serialize( "serializing - GlobalVars" );
241
242 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Variables );
243 ar & BOOST_SERIALIZATION_NVP( initAreaBuffer );
244 }
245public:
246 GlobalVars()
247 {
248 }
249 ~GlobalVars()
250 {
251 }
252
253 void Add( Variable *pVar, bool isResetOffsetAddress = true );
254};
Note: See TracBrowser for help on using the repository browser.