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

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