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

Last change on this file since 516 was 511, checked in by dai_9181, 17 years ago

幾つかの構文解析系の処理をLexicalAnalyzerに実装し直した

File size: 5.2 KB
Line 
1#pragma once
2
3#include <option.h>
4#include <Program.h>
5#include <Type.h>
6#include <Binary.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 bool hasInitData;
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 );
50 ar & BOOST_SERIALIZATION_NVP( hasInitData );
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:
59 Variable( const string &name, const Type &type, bool isConst, bool isRef, const std::string &paramStrForConstructor, bool hasInitData );
60 Variable( const NamespaceScopes &namespaceScopes, const string &name, const Type &type, bool isConst, bool isRef, const std::string &paramStrForConstructor, bool hasInitData );
61 Variable( const Variable &var );
62 Variable();
63
64 void SetArray( const Subscripts &subscripts ){
65 isArray = true;
66 this->subscripts = subscripts;
67 }
68
69 const Type &GetType() const
70 {
71 return type;
72 }
73 void ConstOff(){
74 isConst = false;
75 }
76 void ConstOn(){
77 isConst = true;
78 }
79 bool IsConst() const
80 {
81 return isConst;
82 }
83 bool IsRef() const
84 {
85 return isRef;
86 }
87 bool IsArray()const
88 {
89 return isArray;
90 }
91 const Subscripts &GetSubscripts() const
92 {
93 return subscripts;
94 }
95
96 void ThisIsParameter(){
97 isParameter = true;
98 }
99 bool IsParameter() const
100 {
101 return isParameter;
102 }
103 bool HasInitData() const
104 {
105 return hasInitData;
106 }
107
108
109 int GetMemorySize() const
110 {
111 if( isRef || isParameter ){
112 return PTR_SIZE;
113 }
114
115 int size = type.GetSize();
116
117 if( isArray ){
118 int num = 1;
119 for( int i=0; i<(int)subscripts.size(); i++){
120 num *= subscripts[i]+1;
121 }
122 size *= num;
123 }
124
125 if( size % PTR_SIZE ){
126 size += PTR_SIZE-(size%PTR_SIZE);
127 }
128
129 return size;
130 }
131
132 int GetOffsetAddress() const
133 {
134 return offset;
135 }
136 void SetOffsetAddress( int offset )
137 {
138 this->offset = offset;
139 }
140
141 const std::string &GetParamStrForConstructor() const
142 {
143 return paramStrForConstructor;
144 }
145
146 //レキシカルスコープ用
147 int GetScopeStartAddress() const
148 {
149 return scopeStartAddress;
150 }
151 void SetScopeStartAddress( int scopeStartAddress )
152 {
153 this->scopeStartAddress = scopeStartAddress;
154 }
155 int GetScopeEndAddress() const
156 {
157 return scopeEndAddress;
158 }
159 void SetScopeEndAddress( int scopeEndAddress )
160 {
161 this->scopeEndAddress = scopeEndAddress;
162 }
163 int GetScopeLevel() const
164 {
165 return scopeLevel;
166 }
167 void SetScopeLevel( int scopeLevel )
168 {
169 this->scopeLevel = scopeLevel;
170 }
171
172
173 bool isLiving;
174 int source_code_address;
175
176
177 static int GetSubScriptCounts( const Subscripts &subscripts ){
178 // 配列の要素数を取得
179 int i,i2;
180 for(i=0,i2=1;i<(int)subscripts.size();i++){
181 i2 *= subscripts[i]+1;
182 }
183 return i2;
184 }
185};
186
187class Variables : public vector<Variable *>
188{
189protected:
190 int allSize;
191
192 // XMLシリアライズ用
193private:
194 friend class boost::serialization::access;
195 template<class Archive> void serialize(Archive& ar, const unsigned int version)
196 {
197 trace_for_serialize( "serializing - Variables" );
198
199 ar & boost::serialization::make_nvp("vector_Variable", boost::serialization::base_object<vector<Variable *>>(*this));
200 }
201
202public:
203 Variables()
204 : allSize( 0 )
205 {
206 }
207 ~Variables(){
208 Clear();
209 }
210
211 void Clear(){
212 for( int i=0; i<(int)this->size(); i++ ){
213 delete (*this)[i];
214 }
215
216 allSize = 0;
217 clear();
218 }
219 void PullOutAll()
220 {
221 clear();
222 }
223
224 int GetAllSize() const
225 {
226 return allSize;
227 }
228
229 bool DuplicateCheck( const Symbol &symbol ) const;
230
231 const Variable *BackSearch( const Symbol &symbol ) const;
232 const Variable *Find( const Symbol &symbol )const;
233};
234
235class GlobalVars : public Variables
236{
237public:
238 Binary initAreaBuffer;
239
240 // XMLシリアライズ用
241private:
242 friend class boost::serialization::access;
243 template<class Archive> void serialize(Archive& ar, const unsigned int version)
244 {
245 trace_for_serialize( "serializing - GlobalVars" );
246
247 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP( Variables );
248 ar & BOOST_SERIALIZATION_NVP( initAreaBuffer );
249 }
250public:
251 GlobalVars()
252 {
253 }
254 ~GlobalVars()
255 {
256 }
257
258 void Add( Variable *pVar, bool isResetOffsetAddress = true );
259};
Note: See TracBrowser for help on using the repository browser.