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

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

StartCompileメソッドを廃止し、Usingメソッドのオーバーロードに差し替えた。

File size: 6.7 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
[537]224void Compiler::SetCompilingUserProc( const UserProc *pUserProc )
[533]225{
[536]226 this->pCompilingUserProc = pUserProc;
227
228 this->SetCompilingClass( pUserProc->GetParentClassPtr() );
[537]229}
[533]230
[537]231void Compiler::StartGlobalAreaCompile()
232{
233 ClearCompilingUserProcAndClass();
234}
235
236void Compiler::StartProcedureCompile( const UserProc *pUserProc )
237{
238 //コンパイル中の関数
239 this->SetCompilingUserProc( pUserProc );
240
[540]241 if( pUserProc->HasParentClass() )
242 {
243 // クラスの使用チェック
244 pUserProc->GetParentClass().Using();
245 }
[533]246
247 // コンパイル中の関数が属する名前空間
248 this->GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
249
250 // コンパイル中の関数でImportsされている名前空間
251 this->GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
252
253 // コード生成対象を選択
254 this->codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
255}
256void Compiler::FinishProcedureCompile()
257{
[536]258 this->pCompilingUserProc = NULL;
259 this->pCompilingClass = NULL;
[533]260}
[536]261
262bool Compiler::IsGlobalAreaCompiling()
263{
264 return ( pCompilingUserProc == NULL );
265}
[537]266bool Compiler::IsLocalAreaCompiling()
267{
268 return ( pCompilingUserProc != NULL );
269}
[536]270const UserProc &Compiler::GetCompilingUserProc()
271{
272 if( !this->IsGlobalAreaCompiling() )
273 {
274 return *pCompilingUserProc;
275 }
276
277 throw;
278}
279
280bool Compiler::IsCompilingClass()
281{
282 return ( pCompilingClass != NULL );
283}
284const CClass &Compiler::GetCompilingClass()
285{
286 if( this->IsCompilingClass() )
287 {
288 return *pCompilingClass;
289 }
290
291 throw;
292}
Note: See TracBrowser for help on using the repository browser.