source: dev/BasicCompiler_Common/Variable.h@ 100

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

名前空間機能をグローバル関数に適用。

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