source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/Variable.cpp@ 570

Last change on this file since 570 was 570, checked in by dai_9181, 16 years ago

Variables::DuplicateCheck、Variables::BackSearchメソッドにnowScopeLevelパラメータを追加した。

File size: 3.3 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4#include <Variable.h>
5
6Variable::Variable( const std::string &name, const Type &type, bool isConst, bool isRef, const std::string &paramStrForConstructor, bool hasInitData )
7 : Symbol( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( name ) )
8 , type( type )
9 , isConst( isConst )
10 , isRef( isRef )
11 , isArray( false )
12 , isParameter( false)
13 , paramStrForConstructor( paramStrForConstructor )
14 , hasInitData( hasInitData )
15{
16}
17Variable::Variable( const NamespaceScopes &namespaceScopes, const std::string &name, const Type &type, bool isConst, bool isRef, const std::string &paramStrForConstructor, bool hasInitData )
18 : Symbol( namespaceScopes, name )
19 , type( type )
20 , isConst( isConst )
21 , isRef( isRef )
22 , isArray( false )
23 , isParameter( false)
24 , paramStrForConstructor( paramStrForConstructor )
25 , hasInitData( hasInitData )
26{
27}
28Variable::Variable( const Variable &var )
29 : Symbol( var )
30 , type( var.type )
31 , isConst( var.isConst )
32 , isRef( var.isRef )
33 , isArray( var.isArray )
34 , subscripts( var.subscripts )
35 , isParameter( false )
36 , paramStrForConstructor( var.paramStrForConstructor )
37 , hasInitData( var.hasInitData )
38{
39}
40Variable::Variable()
41{
42}
43
44
45bool Variables::DuplicateCheck( const Symbol &symbol, int nowScopeLevel ) const
46{
47 //レキシカルスコープを考慮して重複判定
48 for( int i=(int)this->size()-1; i>=0 ; i-- ){
49 const Variable *pVar = (*this)[i];
50 if( pVar->isLiving //現在のスコープで有効なもの
51 && pVar->GetScopeLevel() == nowScopeLevel //現在のスコープと同一レベル
52 )
53 {
54 if( pVar->IsEqualSymbol( symbol ) ){
55 return true;
56 }
57 }
58 }
59 return false;
60}
61
62const Variable *Variables::BackSearch( const Symbol &symbol, int nowScopeLevel ) const
63{
64 //レキシカルスコープを考慮してバックサーチ
65 for( int i=(int)this->size()-1; i>=0 ; i-- ){
66 const Variable *pVar = (*this)[i];
67 if( pVar->isLiving //現在のスコープで有効なもの
68 && pVar->GetScopeLevel() <= nowScopeLevel //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)
69 ){
70 if( pVar->IsEqualSymbol( symbol ) ){
71 return pVar;
72 }
73 }
74 }
75 return NULL;
76}
77
78const Variable *Variables::Find( const Symbol &symbol )const
79{
80 int max = (int)this->size();
81 for( int i=0; i<max; i++ ){
82 Variable *pVar = (*this)[i];
83 if( pVar->IsEqualSymbol( symbol ) ){
84 return pVar;
85 }
86 }
87 return NULL;
88}
89
90void GlobalVars::Add( Variable *pVar, bool isResetOffsetAddress )
91{
92 int alignment = 0;
93 if( pVar->GetType().IsStruct() ){
94 alignment = pVar->GetType().GetClass().GetFixedAlignment();
95 }
96
97 if( pVar->HasInitData() ){
98 //初期バッファがあるとき
99
100 if( isResetOffsetAddress )
101 {
102 if( alignment ){
103 if( initAreaBuffer.GetSize() % alignment ){
104 initAreaBuffer.Resize( initAreaBuffer.GetSize() + ( alignment - (initAreaBuffer.GetSize() % alignment) ) );
105 }
106 }
107
108 pVar->SetOffsetAddress( initAreaBuffer.GetSize() );
109
110 initAreaBuffer.Resize( initAreaBuffer.GetSize() + pVar->GetMemorySize() );
111 }
112 }
113 else{
114 //初期バッファがないとき
115
116 if( alignment ){
117 if( allSize % alignment ){
118 allSize += alignment - (allSize % alignment);
119 }
120 }
121
122 pVar->SetOffsetAddress( allSize | 0x80000000 );
123 allSize += pVar->GetMemorySize();
124 }
125
126 push_back( pVar );
127}
Note: See TracBrowser for help on using the repository browser.