| [100] | 1 | #pragma once
|
|---|
| [10] | 2 |
|
|---|
| [100] | 3 | #include "Type.h"
|
|---|
| 4 |
|
|---|
| [75] | 5 | class Variable : public Type
|
|---|
| 6 | {
|
|---|
| [103] | 7 | const NamespaceScopes namespaceScopes;
|
|---|
| [75] | 8 | string name;
|
|---|
| 9 | bool isConst;
|
|---|
| 10 | bool isRef;
|
|---|
| 11 | bool isArray;
|
|---|
| 12 | int subScripts[MAX_ARRAYDIM];
|
|---|
| [10] | 13 |
|
|---|
| [75] | 14 | bool isParameter;
|
|---|
| [10] | 15 |
|
|---|
| [75] | 16 | public:
|
|---|
| [103] | 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)
|
|---|
| [75] | 24 | {
|
|---|
| 25 | subScripts[0] = -1;
|
|---|
| 26 | }
|
|---|
| [103] | 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)
|
|---|
| [75] | 35 | {
|
|---|
| 36 | subScripts[0] = -1;
|
|---|
| [103] | 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;
|
|---|
| [75] | 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 |
|
|---|
| [103] | 63 | bool IsEqualSymbol( const NamespaceScopes &namespaceScopes, const string &name ) const;
|
|---|
| 64 | bool IsEqualSymbol( const string &name ) const;
|
|---|
| 65 |
|
|---|
| [75] | 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 |
|
|---|
| 143 | class Variables : public vector<Variable *>
|
|---|
| 144 | {
|
|---|
| 145 | public:
|
|---|
| 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 | ){
|
|---|
| [103] | 167 | if( var.IsEqualSymbol( varName ) ){
|
|---|
| [75] | 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 | ){
|
|---|
| [103] | 183 | if( var.IsEqualSymbol( varName ) ){
|
|---|
| [75] | 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];
|
|---|
| [103] | 196 | if( pVar->IsEqualSymbol( varName ) ){
|
|---|
| [75] | 197 | return pVar;
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | return NULL;
|
|---|
| 201 | }
|
|---|
| 202 | };
|
|---|
| 203 |
|
|---|
| 204 | extern Variables globalVars;
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|