source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/Compiler.cpp@ 536

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

Compiler::pCompilingClassメンバをprivateにし、setter/getterにあたるメソッドを用意した。

File size: 6.6 KB
RevLine 
[206]1#include "stdafx.h"
2
[184]3#include <Compiler.h>
[206]4#include <Type.h>
[184]5
[195]6Compiler compiler;
[193]7
[270]8void Compiler::StaticLink( ObjectModules &staticLibraries )
9{
10 BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries )
11 {
12 // メタ情報
[273]13 pNowObjectModule->StaticLink( *pStaticLibrary );
[270]14 }
15}
16
[523]17bool Compiler::StringToType( const std::string &typeName, Type &type ){
[193]18 type.SetIndex( -1 );
19
[290]20
21 /////////////////////////////////////////////////////////
22 // ☆★☆ ジェネリクスサポート ☆★☆
23
24 if( strstr( typeName.c_str(), "<" ) )
25 {
26 // ジェネリッククラスをインスタンス化した型の場合
27 int i = 0;
[301]28 char className[VN_SIZE];
[290]29 GetIdentifierToken( className, typeName.c_str(), i );
30
31 // ジェネリクスクラスを取得
[299]32 const CClass *pGenericClass = this->GetObjectModule().meta.GetClasses().Find( className );
[290]33
[301]34 if( !pGenericClass )
35 {
[318]36 return false;
[301]37 }
[290]38
[301]39 if( typeName[i] != '<' )
40 {
41 Jenga::Throw( "StringToType内でジェネリクス構文の解析に失敗" );
42 }
43
[290]44 GenericTypes genericTypes;
[301]45 while( true )
46 {
47 i++;
[290]48
[301]49 char typeParameter[VN_SIZE];
50 GetIdentifierToken( typeParameter, typeName.c_str(), i );
51
52 // 型パラメータの型情報を取得
53 Type baseType;
54 StringToType( typeParameter, baseType );
55
56 genericTypes.push_back( GenericType( "(non support)", baseType ) );
57
58 if( typeName[i] != ',' )
59 {
60 break;
61 }
62 }
63
[290]64 // 基本型をセット
65 type.SetBasicType( DEF_OBJECT );
66
67 // 拡張情報をセット
68 type.SetClassPtr( pGenericClass );
69 type.SetActualGenericTypes( genericTypes );
70
71 return true;
72 }
73
74 //
75 /////////////////////////////////////////////////////////
76
77
[193]78 if( typeName[0] == '*' ){
79 if( typeName.size() >= 3
80 && typeName[1] == 1 && ( typeName[2] == ESC_FUNCTION || typeName[2] == ESC_SUB ) ){
81 //関数ポインタ(*Function)
82 type.SetBasicType( DEF_PTR_PROC );
[299]83 type.SetIndex( this->GetObjectModule().meta.GetProcPointers().Add( typeName ) );
[193]84 return true;
85 }
86
[523]87 const std::string &nextTypeName = typeName.substr( 1 );
[193]88
89 if( !StringToType( nextTypeName, type ) ){
90 return false;
91 }
92
93 type.PtrLevelUp();
94
95 return true;
96 }
97
98 {
99 int basicType;
100 if( Type::StringToBasicType( typeName, basicType ) ){
101 // 基本型だったとき
102 type.SetBasicType( basicType );
103 return true;
104 }
105 }
106
107 // Object型だったとき
108 if( typeName == "Object" ){
[299]109 type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetObjectClassPtr() );
[193]110 return true;
111 }
112
113 // String型だったとき
114 if( typeName == "String" ){
[299]115 type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetStringClassPtr() );
[193]116 return true;
117 }
118
119
120 ////////////////////
121 // TypeDefされた型
122 ////////////////////
[299]123 int i=this->GetObjectModule().meta.GetTypeDefs().GetIndex( typeName );
[193]124 if(i!=-1){
[299]125 type = this->GetObjectModule().meta.GetTypeDefs()[i].GetBaseType();
[193]126 return true;
127 }
128
129 //クラス
[299]130 const CClass *pobj_c = this->GetObjectModule().meta.GetClasses().Find( typeName );
[193]131 if(pobj_c){
132 if( pobj_c->IsStructure() ){
133 type.SetBasicType( DEF_STRUCT );
134 }
135 else{
136 type.SetBasicType( DEF_OBJECT );
137 }
[290]138 type.SetClassPtr( pobj_c );
[193]139 return true;
140 }
141
[290]142
143 /////////////////////////////////////////////////////////
144 // ☆★☆ ジェネリクスサポート ☆★☆
145
146 // 型パラメータ
[536]147 if( this->IsCompilingClass() )
[290]148 {
149 // クラスに属するメソッドをコンパイルしているとき
[536]150 int formalTypeIndex = this->GetCompilingClass().GetFormalGenericTypeParameterIndex( typeName );
[299]151 if( formalTypeIndex != -1 )
[290]152 {
153 // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき
154 type.SetBasicType( DEF_TYPE_PARAMETER );
[536]155 type.SetClassPtr( &this->GetCompilingClass().GetFormalGenericTypes()[formalTypeIndex].GetType().GetClass() );
[299]156 type.SetFormalTypeName( typeName );
157 type.SetFormalTypeIndex( formalTypeIndex );
[290]158 return true;
159 }
160 }
161
162 //
163 /////////////////////////////////////////////////////////
164
[193]165 return false;
166}
167
[523]168const std::string Compiler::TypeToString( const Type &type )
[193]169{
170 if( PTR_LEVEL( type.GetBasicType() ) ){
171 //ポインタレベルが1以上の場合
172 Type tempType( type );
173 tempType.PtrLevelDown();
174
[523]175 return (std::string)"*" + TypeToString( tempType );
[193]176 }
177 else if( type.IsObject() || type.IsStruct() ){
178 //オブジェクトまたは構造体
179
180 if( !( type.GetIndex() == 0 || type.GetIndex() == -1 ) ){
181 if( type.GetClass().GetNamespaceScopes().size() >= 1 )
182 {
183 return type.GetClass().GetNamespaceScopes().ToString() + "." + type.GetClass().GetName();
184 }
185 return type.GetClass().GetName();
186 }
187 }
188 else if( type.IsProcPtr() ){
189 if( type.GetIndex() == 0 || type.GetIndex() == -1 ){
190 return "VoidPtr";
191 }
192 else{
[299]193 if( this->GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){
[193]194 return "*Sub";
195 }
196 return "*Function";
197 }
198 }
199 else{
200 // 基本型
201 const char *lpszTypeName = Type::BasicTypeToCharPtr( type );
202 if( lpszTypeName )
203 {
[523]204 return (const std::string)lpszTypeName;
[193]205 }
206 }
207
[465]208 compiler.errorMessenger.Output(1, NULL, cp);
[193]209
[523]210 return (std::string)"(null)";
[193]211}
[533]212
[536]213void Compiler::ClearCompilingUserProcAndClass()
214{
215 this->pCompilingUserProc = NULL;
216 this->pCompilingClass = NULL;
217}
218
219void Compiler::SetCompilingClass( const CClass *pClass )
220{
221 this->pCompilingClass = pClass;
222}
223
[533]224void Compiler::StartProcedureCompile( const UserProc *pUserProc )
225{
[536]226 //コンパイル中の関数
227 this->pCompilingUserProc = pUserProc;
228
[533]229 //コンパイル中の関数が属するクラス
[536]230 this->SetCompilingClass( pUserProc->GetParentClassPtr() );
[533]231
232 //コンパイルスタートをクラス管理クラスに追加
233 this->GetObjectModule().meta.GetClasses().StartCompile( pUserProc );
234
235 //コンパイル中の関数
236 UserProc::CompileStartForUserProc( pUserProc );
237
238 // コンパイル中の関数が属する名前空間
239 this->GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
240
241 // コンパイル中の関数でImportsされている名前空間
242 this->GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
243
244 // コード生成対象を選択
245 this->codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
246}
247void Compiler::FinishProcedureCompile()
248{
[536]249 this->pCompilingUserProc = NULL;
250 this->pCompilingClass = NULL;
[533]251}
[536]252
253bool Compiler::IsGlobalAreaCompiling()
254{
255 return ( pCompilingUserProc == NULL );
256}
257const UserProc &Compiler::GetCompilingUserProc()
258{
259 if( !this->IsGlobalAreaCompiling() )
260 {
261 return *pCompilingUserProc;
262 }
263
264 throw;
265}
266
267bool Compiler::IsCompilingClass()
268{
269 return ( pCompilingClass != NULL );
270}
271const CClass &Compiler::GetCompilingClass()
272{
273 if( this->IsCompilingClass() )
274 {
275 return *pCompilingClass;
276 }
277
278 throw;
279}
Note: See TracBrowser for help on using the repository browser.