source: dev/BasicCompiler_Common/Variable.h@ 133

Last change on this file since 133 was 103, checked in by dai_9181, 17 years ago

名前空間機能をグローバル変数、定数と列挙型に適用。
一部、クラスの静的メンバと名前空間の相性が悪いコードが潜んでいるため、要改修

File size: 4.0 KB
Line 
1#pragma once
2
3#include "Type.h"
4
5class Variable : public Type
6{
7 const NamespaceScopes namespaceScopes;
8 string name;
9 bool isConst;
10 bool isRef;
11 bool isArray;
12 int subScripts[MAX_ARRAYDIM];
13
14 bool isParameter;
15
16public:
17 Variable( const string &name, const Type &type, bool isConst = false, bool isRef = false )
18 : Type( type )
19 , name( name )
20 , isConst( isConst )
21 , isRef( isRef )
22 , isArray( false )
23 , isParameter( false)
24 {
25 subScripts[0] = -1;
26 }
27 Variable( const NamespaceScopes &namespaceScopes, const string &name, const Type &type, bool isConst = false, bool isRef = false )
28 : namespaceScopes( namespaceScopes )
29 , Type( type )
30 , name( name )
31 , isConst( isConst )
32 , isRef( isRef )
33 , isArray( false )
34 , isParameter( false)
35 {
36 subScripts[0] = -1;
37 }
38 Variable( const Variable &var )
39 : Type( var )
40 , name( var.name )
41 , isConst( var.isConst )
42 , isRef( var.isRef )
43 , isArray( false )
44 , isParameter( false )
45 {
46 subScripts[0] = -1;
47 if( var.isArray ){
48 SetArray( var.subScripts );
49 }
50 }
51 ~Variable(){}
52
53 void SetArray( const int *pSubScripts ){
54 isArray = true;
55 memcpy( this->subScripts, pSubScripts, sizeof(int) * MAX_ARRAYDIM );
56 }
57
58 const string &GetName() const
59 {
60 return name;
61 }
62
63 bool IsEqualSymbol( const NamespaceScopes &namespaceScopes, const string &name ) const;
64 bool IsEqualSymbol( const string &name ) const;
65
66 void ConstOff(){
67 isConst = false;
68 }
69 void ConstOn(){
70 isConst = true;
71 }
72 bool IsConst() const
73 {
74 return isConst;
75 }
76 bool IsRef() const
77 {
78 return isRef;
79 }
80 bool IsArray()const
81 {
82 return isArray;
83 }
84 const int *GetSubScriptsPtr() const
85 {
86 return subScripts;
87 }
88
89 void ThisIsParameter(){
90 isParameter = true;
91 }
92 bool IsParameter() const
93 {
94 return isParameter;
95 }
96
97
98 int GetMemorySize() const
99 {
100 if( isRef || isParameter ){
101 return PTR_SIZE;
102 }
103
104 int size = Type::GetSize();
105
106 if( isArray ){
107 int num = 1;
108 for( int i=0; i<MAX_ARRAYDIM; i++){
109 if(subScripts[i]==-1) break;
110 num *= subScripts[i]+1;
111 }
112 size *= num;
113 }
114
115 if( size % PTR_SIZE ){
116 size += PTR_SIZE-(size%PTR_SIZE);
117 }
118
119 return size;
120 }
121
122
123 /* --- オフセット ---
124
125 ※グローバル変数で初期バッファがない場合は最上位ビットに1がセットされ、
126 初期バッファの有無が識別される。
127 (その後、スケジュール実行により、実際の配置に並び替えられる)*/
128 int offset;
129
130 //コンストラクタ用パラメータ
131 string paramStrForConstructor;
132
133 //レキシカルスコープ用
134 int ScopeStartAddress;
135 int ScopeEndAddress;
136 int ScopeLevel;
137 BOOL bLiving;
138
139
140 int source_code_address;
141};
142
143class Variables : public vector<Variable *>
144{
145public:
146 Variables(){}
147 ~Variables(){
148 clear();
149 }
150
151 void clear(){
152 for( int i=0; i<(int)this->size(); i++ ){
153 delete (*this)[i];
154 }
155
156 vector<Variable *>::clear();
157 }
158
159 bool DuplicateCheck( const string &varName ) const
160 {
161 //レキシカルスコープを考慮して重複判定
162 for( int i=(int)this->size()-1; i>=0 ; i-- ){
163 Variable &var = *(*this)[i];
164 if( var.bLiving //現在のスコープで有効なもの
165 && var.ScopeLevel == obj_LexScopes.GetNowLevel() //現在のスコープと同一レベル
166 ){
167 if( var.IsEqualSymbol( varName ) ){
168 return true;
169 }
170 }
171 }
172 return false;
173 }
174
175 const Variable *BackSearch( const string &varName ) const
176 {
177 //レキシカルスコープを考慮してバックサーチ
178 for( int i=(int)this->size()-1; i>=0 ; i-- ){
179 Variable &var = *(*this)[i];
180 if( var.bLiving //現在のスコープで有効なもの
181 && var.ScopeLevel <= obj_LexScopes.GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)
182 ){
183 if( var.IsEqualSymbol( varName ) ){
184 return &var;
185 }
186 }
187 }
188 return NULL;
189 }
190
191 const Variable *Find( const string &varName )const
192 {
193 int max = (int)this->size();
194 for( int i=0; i<max; i++ ){
195 Variable *pVar = (*this)[i];
196 if( pVar->IsEqualSymbol( varName ) ){
197 return pVar;
198 }
199 }
200 return NULL;
201 }
202};
203
204extern Variables globalVars;
205
206
Note: See TracBrowser for help on using the repository browser.