source: dev/BasicCompiler_Common/Variable.h@ 140

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

traceログ機能を搭載
動的メンバをstl::vectorにまとめた
シンボルをクラス化した

File size: 4.0 KB
Line 
1#pragma once
2
3#include "Type.h"
4#include <Symbol.h>
5
6class Variable : public Type
7{
8 const NamespaceScopes namespaceScopes;
9 string name;
10 bool isConst;
11 bool isRef;
12 bool isArray;
13 int subScripts[MAX_ARRAYDIM];
14
15 bool isParameter;
16
17public:
18 Variable( const string &name, const Type &type, bool isConst = false, bool isRef = false )
19 : Type( type )
20 , name( name )
21 , isConst( isConst )
22 , isRef( isRef )
23 , isArray( false )
24 , isParameter( false)
25 {
26 subScripts[0] = -1;
27 }
28 Variable( const NamespaceScopes &namespaceScopes, const string &name, const Type &type, bool isConst = false, bool isRef = false )
29 : namespaceScopes( namespaceScopes )
30 , Type( type )
31 , name( name )
32 , isConst( isConst )
33 , isRef( isRef )
34 , isArray( false )
35 , isParameter( false)
36 {
37 subScripts[0] = -1;
38 }
39 Variable( const Variable &var )
40 : Type( var )
41 , name( var.name )
42 , isConst( var.isConst )
43 , isRef( var.isRef )
44 , isArray( false )
45 , isParameter( false )
46 {
47 subScripts[0] = -1;
48 if( var.isArray ){
49 SetArray( var.subScripts );
50 }
51 }
52 ~Variable(){}
53
54 void SetArray( const int *pSubScripts ){
55 isArray = true;
56 memcpy( this->subScripts, pSubScripts, sizeof(int) * MAX_ARRAYDIM );
57 }
58
59 const string &GetName() const
60 {
61 return name;
62 }
63
64 bool IsEqualSymbol( const Symbol &symbol, bool isSupportStaticMember = true ) 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 Symbol &symbol ) 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( symbol ) ){
168 return true;
169 }
170 }
171 }
172 return false;
173 }
174
175 const Variable *BackSearch( const Symbol &symbol ) 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( symbol ) ){
184 return &var;
185 }
186 }
187 }
188 return NULL;
189 }
190
191 const Variable *Find( const Symbol &symbol )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( symbol ) ){
197 return pVar;
198 }
199 }
200 return NULL;
201 }
202};
203
204extern Variables globalVars;
205
206
207
Note: See TracBrowser for help on using the repository browser.