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

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