source: dev/trunk/abdev/BasicCompiler_Common/include/Variable.h@ 404

Last change on this file since 404 was 392, checked in by dai_9181, 17 years ago

BOOL bLiving → bool isLiving

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