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

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

ジェネリクス型の型解決ができない場合のエラーメッセージを実装した。

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