1 | #include "stdafx.h" |
---|
2 | |
---|
3 | using namespace ActiveBasic::Compiler; |
---|
4 | |
---|
5 | Compiler compiler; |
---|
6 | |
---|
7 | Compiler::Compiler() |
---|
8 | : isBuildSuccessful( false ) |
---|
9 | , targetModuleType( ActiveBasic::Common::TargetModuleType::Exe ) |
---|
10 | , isDebug( false ) |
---|
11 | , isUnicode( false ) |
---|
12 | , isCore( false ) |
---|
13 | , currentRelationalObjectModuleIndexForSource( 0 ) |
---|
14 | { |
---|
15 | // 生成先のオブジェクトモジュールを登録 |
---|
16 | ObjectModule *pObjectModule = new ObjectModule(); |
---|
17 | staticLibraries.push_back( pObjectModule ); |
---|
18 | SelectObjectModule( pObjectModule ); |
---|
19 | |
---|
20 | namespaceSupporter.RegistAllNamespaceScopesCollection( &GetObjectModule().meta.GetNamespaces() ); |
---|
21 | |
---|
22 | Symbol::RegistNamespaceSupporter( &namespaceSupporter ); |
---|
23 | } |
---|
24 | Compiler::~Compiler() |
---|
25 | { |
---|
26 | BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries ) |
---|
27 | { |
---|
28 | delete pStaticLibrary; |
---|
29 | } |
---|
30 | staticLibraries.clear(); |
---|
31 | } |
---|
32 | |
---|
33 | void Compiler::PreStaticLink( const ObjectModules &staticLibraries ) |
---|
34 | { |
---|
35 | BOOST_FOREACH( const ObjectModule *pStaticLibrary, staticLibraries ) |
---|
36 | { |
---|
37 | // 関連オブジェクトモジュールの名前リスト |
---|
38 | this->GetObjectModule().relationalObjectModuleNames.push_back( pStaticLibrary->GetName() ); |
---|
39 | } |
---|
40 | } |
---|
41 | void Compiler::StaticLink( ObjectModules &staticLibraries ) |
---|
42 | { |
---|
43 | BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries ) |
---|
44 | { |
---|
45 | if( &this->GetObjectModule() == pStaticLibrary ) |
---|
46 | { |
---|
47 | // 自分自身の場合はリンクしない |
---|
48 | continue; |
---|
49 | } |
---|
50 | |
---|
51 | // メタ情報 |
---|
52 | this->GetObjectModule().StaticLink( *pStaticLibrary, this->IsSll() ); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | const std::string &Compiler::GetGlobalAreaProcName() const |
---|
57 | { |
---|
58 | static std::string globalAreaProcName; |
---|
59 | |
---|
60 | if( globalAreaProcName.empty() ) |
---|
61 | { |
---|
62 | // 初期化 |
---|
63 | _ASSERTE( !this->GetModuleName().empty() ); |
---|
64 | |
---|
65 | std::string originalName = this->GetModuleName(); |
---|
66 | |
---|
67 | // モジュール名がシンボル名として使えない場合があるので、16進数文字列に変換する |
---|
68 | char temporary[VN_SIZE*2] = ""; |
---|
69 | for( int i=0; i<static_cast<int>(originalName.size()); i++ ) |
---|
70 | { |
---|
71 | sprintf( temporary + strlen(temporary), "%2x", originalName[i] ); |
---|
72 | } |
---|
73 | |
---|
74 | globalAreaProcName = (std::string)"_GlobalArea_" + temporary; |
---|
75 | } |
---|
76 | |
---|
77 | return globalAreaProcName; |
---|
78 | } |
---|
79 | |
---|
80 | ActiveBasic::Compiler::Error::StringToTypeErrorCode::EnumType Compiler::StringToGenericTypeEx( const std::string &typeName, Type &type ) |
---|
81 | { |
---|
82 | // ジェネリッククラスをインスタンス化した型の場合 |
---|
83 | int i = 0; |
---|
84 | char className[VN_SIZE]; |
---|
85 | GetIdentifierToken( className, typeName.c_str(), i ); |
---|
86 | |
---|
87 | // ジェネリクスクラスを取得 |
---|
88 | const CClass *pGenericClass = this->GetObjectModule().meta.FindClassSupportedTypeDef( |
---|
89 | LexicalAnalyzer::FullNameToSymbol( className ) |
---|
90 | ); |
---|
91 | |
---|
92 | if( !pGenericClass ) |
---|
93 | { |
---|
94 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::NotfoundGenericClass; |
---|
95 | } |
---|
96 | |
---|
97 | if( typeName[i] != '<' ) |
---|
98 | { |
---|
99 | Jenga::Throw( "StringToType内でジェネリクス構文の解析に失敗" ); |
---|
100 | } |
---|
101 | |
---|
102 | GenericTypes genericTypes; |
---|
103 | bool isValueType = false; |
---|
104 | while( true ) |
---|
105 | { |
---|
106 | i++; |
---|
107 | |
---|
108 | char typeParameterStr[VN_SIZE]; |
---|
109 | GetIdentifierToken( typeParameterStr, typeName.c_str(), i ); |
---|
110 | |
---|
111 | // 型パラメータの型情報を取得 |
---|
112 | Type typeParameterType; |
---|
113 | StringToType( typeParameterStr, typeParameterType ); |
---|
114 | |
---|
115 | if( this->IsCompilingClass() ) |
---|
116 | { |
---|
117 | if( this->pCompilingClass->IsExpanded() && typeParameterType.IsTypeParameter() ) |
---|
118 | { |
---|
119 | // 現在コンパイル中のクラスがテンプレート展開済みのクラスで、 |
---|
120 | // 尚且つターゲットとなる型が型パラメータだったとき |
---|
121 | |
---|
122 | // テンプレート展開情報を用いて型解決を行う |
---|
123 | this->pCompilingClass->ResolveExpandedClassActualTypeParameter( typeParameterType ); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | genericTypes.push_back( GenericType( "(non support)", typeParameterType ) ); |
---|
128 | |
---|
129 | if( typeParameterType.IsValueType() ) |
---|
130 | { |
---|
131 | // 値型の場合 |
---|
132 | isValueType = true; |
---|
133 | } |
---|
134 | |
---|
135 | if( typeName[i] != ',' ) |
---|
136 | { |
---|
137 | break; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | // 基本型をセット |
---|
142 | type.SetBasicType( DEF_OBJECT ); |
---|
143 | |
---|
144 | if( isValueType ) |
---|
145 | { |
---|
146 | // 型パラメータに値型が指定された場合 |
---|
147 | |
---|
148 | // 仮型パラメータを実型パラメータに変換 |
---|
149 | Types actualTypes; |
---|
150 | BOOST_FOREACH( const GenericType &genericType, genericTypes ) |
---|
151 | { |
---|
152 | actualTypes.push_back( genericType.GetType() ); |
---|
153 | } |
---|
154 | |
---|
155 | // テンプレートとしてクラスを展開する |
---|
156 | const CClass *pExpandedClass = LexicalAnalyzer::TemplateExpand( *const_cast<CClass *>(pGenericClass), actualTypes ); |
---|
157 | |
---|
158 | if( pExpandedClass ) |
---|
159 | { |
---|
160 | // 拡張情報をセット |
---|
161 | type.SetClassPtr( pExpandedClass ); |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | // TODO: 消す |
---|
166 | goto Generic; |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | Generic: |
---|
172 | // 型パラメータにクラス型が指定された場合 |
---|
173 | |
---|
174 | // ジェネリック クラスとして利用する |
---|
175 | |
---|
176 | // 拡張情報をセット |
---|
177 | type.SetClassPtr( pGenericClass ); |
---|
178 | type.SetActualGenericTypes( genericTypes ); |
---|
179 | } |
---|
180 | |
---|
181 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
182 | } |
---|
183 | ActiveBasic::Compiler::Error::StringToTypeErrorCode::EnumType Compiler::StringToTypeEx( const std::string &typeName, Type &type, bool isResolveGenerics ) |
---|
184 | { |
---|
185 | type.SetIndex( -1 ); |
---|
186 | |
---|
187 | |
---|
188 | ///////////////////////////////////////////////////////// |
---|
189 | // ☆★☆ ジェネリクスサポート ☆★☆ |
---|
190 | |
---|
191 | if( strstr( typeName.c_str(), "<" ) ) |
---|
192 | { |
---|
193 | return StringToGenericTypeEx( typeName, type ); |
---|
194 | } |
---|
195 | |
---|
196 | // |
---|
197 | ///////////////////////////////////////////////////////// |
---|
198 | |
---|
199 | |
---|
200 | if( typeName[0] == '*' ){ |
---|
201 | if( typeName.size() >= 3 |
---|
202 | && typeName[1] == 1 && ( typeName[2] == ESC_FUNCTION || typeName[2] == ESC_SUB ) ) |
---|
203 | { |
---|
204 | // 関数ポインタを追加する |
---|
205 | { |
---|
206 | DWORD dwProcType = (DWORD)typeName[2]; |
---|
207 | const std::string ¶mStr = typeName.substr( 3 ); |
---|
208 | |
---|
209 | Procedure::Kind kind = ( dwProcType == ESC_FUNCTION ) |
---|
210 | ? Procedure::Function |
---|
211 | : Procedure::Sub; |
---|
212 | |
---|
213 | ProcPointer *pProcPointer = new ProcPointer( kind ); |
---|
214 | |
---|
215 | //buffer[0]は'('となっている |
---|
216 | extern int cp; |
---|
217 | ActiveBasic::Compiler::LexicalAnalyzer::SetParamsAndReturnType( pProcPointer, paramStr.c_str(), false, cp ); |
---|
218 | |
---|
219 | this->GetObjectModule().meta.GetProcPointers().push_back( pProcPointer ); |
---|
220 | } |
---|
221 | |
---|
222 | //関数ポインタ(*Function) |
---|
223 | type.SetBasicType( DEF_PTR_PROC ); |
---|
224 | type.SetIndex( this->GetObjectModule().meta.GetProcPointers().size() - 1 ); |
---|
225 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
226 | } |
---|
227 | |
---|
228 | const std::string &nextTypeName = typeName.substr( 1 ); |
---|
229 | |
---|
230 | ActiveBasic::Compiler::Error::StringToTypeErrorCode::EnumType result = StringToTypeEx( nextTypeName, type, isResolveGenerics ); |
---|
231 | if( result != ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful ) |
---|
232 | { |
---|
233 | return result; |
---|
234 | } |
---|
235 | |
---|
236 | type.PtrLevelUp(); |
---|
237 | |
---|
238 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
239 | } |
---|
240 | |
---|
241 | { |
---|
242 | int basicType; |
---|
243 | if( Type::StringToBasicType( typeName, basicType ) ) |
---|
244 | { |
---|
245 | // 基本型だったとき |
---|
246 | type.SetBasicType( basicType ); |
---|
247 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | // Object型だったとき |
---|
252 | if( typeName == "Object" ) |
---|
253 | { |
---|
254 | type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetObjectClassPtr() ); |
---|
255 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
256 | } |
---|
257 | |
---|
258 | // String型だったとき |
---|
259 | if( typeName == "String" ) |
---|
260 | { |
---|
261 | type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetStringClassPtr() ); |
---|
262 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | //////////////////// |
---|
267 | // TypeDefされた型 |
---|
268 | //////////////////// |
---|
269 | const TypeDef *pTypeDef = this->GetObjectModule().meta.GetTypeDefs().Find( LexicalAnalyzer::FullNameToSymbol( typeName ) ); |
---|
270 | if( pTypeDef ) |
---|
271 | { |
---|
272 | type = pTypeDef->GetBaseType(); |
---|
273 | |
---|
274 | if( type.IsObject() ) |
---|
275 | { |
---|
276 | if( isResolveGenerics && type.GetClass().IsGeneric() && !type.HasActualGenericType() ) |
---|
277 | { |
---|
278 | // ジェネリッククラスの場合 |
---|
279 | trace( "型解決されていない" ); |
---|
280 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::FailedResolveGenericType; |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
285 | } |
---|
286 | |
---|
287 | //クラス |
---|
288 | const CClass *pobj_c = this->GetObjectModule().meta.FindClassSupportedTypeDef( LexicalAnalyzer::FullNameToSymbol( typeName ) ); |
---|
289 | if(pobj_c) |
---|
290 | { |
---|
291 | if( isResolveGenerics && pobj_c->IsGeneric() ) |
---|
292 | { |
---|
293 | // ジェネリッククラスの場合 |
---|
294 | trace( "型解決されていない" ); |
---|
295 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::FailedResolveGenericType; |
---|
296 | } |
---|
297 | |
---|
298 | if( pobj_c->IsStructure() ) |
---|
299 | { |
---|
300 | type.SetBasicType( DEF_STRUCT ); |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | type.SetBasicType( DEF_OBJECT ); |
---|
305 | } |
---|
306 | type.SetClassPtr( pobj_c ); |
---|
307 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | ///////////////////////////////////////////////////////// |
---|
312 | // ☆★☆ ジェネリクスサポート ☆★☆ |
---|
313 | |
---|
314 | // 型パラメータ |
---|
315 | if( this->IsCompilingClass() ) |
---|
316 | { |
---|
317 | // クラスに属するメソッドをコンパイルしているとき |
---|
318 | int formalTypeIndex = this->GetCompilingClass().GetFormalGenericTypeParameterIndex( typeName ); |
---|
319 | if( formalTypeIndex != -1 ) |
---|
320 | { |
---|
321 | // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき |
---|
322 | type.SetBasicType( DEF_TYPE_PARAMETER ); |
---|
323 | type.SetClassPtr( &this->GetCompilingClass().GetFormalGenericTypes()[formalTypeIndex].GetType().GetClass() ); |
---|
324 | type.SetFormalTypeName( typeName ); |
---|
325 | type.SetFormalTypeIndex( formalTypeIndex ); |
---|
326 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | // |
---|
331 | ///////////////////////////////////////////////////////// |
---|
332 | |
---|
333 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::NotfoundType; |
---|
334 | } |
---|
335 | |
---|
336 | bool Compiler::StringToType( const std::string &typeName, Type &type ) |
---|
337 | { |
---|
338 | return ( StringToTypeEx( typeName, type, false ) == ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful ); |
---|
339 | } |
---|
340 | |
---|
341 | const std::string Compiler::TypeToString( const Type &type ) |
---|
342 | { |
---|
343 | if( PTR_LEVEL( type.GetBasicType() ) ){ |
---|
344 | //ポインタレベルが1以上の場合 |
---|
345 | Type tempType( type ); |
---|
346 | tempType.PtrLevelDown(); |
---|
347 | |
---|
348 | return '*' + TypeToString( tempType ); |
---|
349 | } |
---|
350 | else if( type.IsObject() || type.IsStruct() ) |
---|
351 | { |
---|
352 | //オブジェクトまたは構造体 |
---|
353 | |
---|
354 | if( !( type.GetIndex() == 0 || type.GetIndex() == -1 ) ) |
---|
355 | { |
---|
356 | std::string result = type.GetClass().GetName(); |
---|
357 | if( type.GetClass().GetNamespaceScopes().size() >= 1 ) |
---|
358 | { |
---|
359 | result = type.GetClass().GetNamespaceScopes().ToString() |
---|
360 | + "." |
---|
361 | + result; |
---|
362 | } |
---|
363 | |
---|
364 | if( type.GetClass().IsExpanded() ) |
---|
365 | { |
---|
366 | // テンプレート展開済みのクラスの場合 |
---|
367 | std::string actualGenericTypesName; |
---|
368 | BOOST_FOREACH( const Type &typeParameter, type.GetClass().expandedClassActualTypeParameters ) |
---|
369 | { |
---|
370 | if( actualGenericTypesName.size() ) |
---|
371 | { |
---|
372 | actualGenericTypesName += ','; |
---|
373 | } |
---|
374 | actualGenericTypesName += typeParameter.ToString(); |
---|
375 | } |
---|
376 | |
---|
377 | result += '<' + actualGenericTypesName + '>'; |
---|
378 | } |
---|
379 | |
---|
380 | return result; |
---|
381 | } |
---|
382 | } |
---|
383 | else if( type.IsProcPtr() ){ |
---|
384 | if( type.GetIndex() == 0 || type.GetIndex() == -1 ){ |
---|
385 | return "VoidPtr"; |
---|
386 | } |
---|
387 | else{ |
---|
388 | if( this->GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){ |
---|
389 | return "*Sub"; |
---|
390 | } |
---|
391 | return "*Function"; |
---|
392 | } |
---|
393 | } |
---|
394 | else{ |
---|
395 | // 基本型 |
---|
396 | const char *lpszTypeName = Type::BasicTypeToCharPtr( type ); |
---|
397 | if( lpszTypeName ) |
---|
398 | { |
---|
399 | return (const std::string)lpszTypeName; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | compiler.errorMessenger.Output(1, NULL, cp); |
---|
404 | |
---|
405 | return (std::string)"(null)"; |
---|
406 | } |
---|
407 | |
---|
408 | int Compiler::SizeOf( const Type &type ) |
---|
409 | { |
---|
410 | Type tempType( type ); |
---|
411 | if( this->IsCompilingClass() ) |
---|
412 | { |
---|
413 | if( this->pCompilingClass->IsExpanded() && tempType.IsTypeParameter() ) |
---|
414 | { |
---|
415 | // 現在コンパイル中のクラスがテンプレート展開済みのクラスで、 |
---|
416 | // 尚且つターゲットとなる型が型パラメータだったとき |
---|
417 | |
---|
418 | // テンプレート展開情報を用いて型解決を行う |
---|
419 | this->pCompilingClass->ResolveExpandedClassActualTypeParameter( tempType ); |
---|
420 | } |
---|
421 | } |
---|
422 | return tempType.GetSize(); |
---|
423 | } |
---|
424 | |
---|
425 | void Compiler::ClearCompilingUserProcAndClass() |
---|
426 | { |
---|
427 | this->pCompilingUserProc = NULL; |
---|
428 | this->pCompilingClass = NULL; |
---|
429 | } |
---|
430 | |
---|
431 | void Compiler::SetCompilingClass( const CClass *pClass ) |
---|
432 | { |
---|
433 | this->pCompilingClass = pClass; |
---|
434 | } |
---|
435 | |
---|
436 | void Compiler::SetCompilingUserProc( const UserProc *pUserProc ) |
---|
437 | { |
---|
438 | this->pCompilingUserProc = pUserProc; |
---|
439 | |
---|
440 | this->SetCompilingClass( pUserProc->GetParentClassPtr() ); |
---|
441 | } |
---|
442 | |
---|
443 | void Compiler::StartGlobalAreaCompile() |
---|
444 | { |
---|
445 | ClearCompilingUserProcAndClass(); |
---|
446 | } |
---|
447 | |
---|
448 | void Compiler::StartProcedureCompile( const UserProc *pUserProc ) |
---|
449 | { |
---|
450 | //コンパイル中の関数 |
---|
451 | this->SetCompilingUserProc( pUserProc ); |
---|
452 | |
---|
453 | if( pUserProc->HasParentClass() ) |
---|
454 | { |
---|
455 | // クラスの使用チェック |
---|
456 | pUserProc->GetParentClass().Using(); |
---|
457 | } |
---|
458 | |
---|
459 | // コンパイル中の関数が属する名前空間 |
---|
460 | this->GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() ); |
---|
461 | |
---|
462 | // コンパイル中の関数でImportsされている名前空間 |
---|
463 | this->GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() ); |
---|
464 | |
---|
465 | // コード生成対象を選択 |
---|
466 | this->codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() ); |
---|
467 | } |
---|
468 | void Compiler::FinishProcedureCompile() |
---|
469 | { |
---|
470 | this->pCompilingUserProc = NULL; |
---|
471 | this->pCompilingClass = NULL; |
---|
472 | } |
---|
473 | |
---|
474 | bool Compiler::IsGlobalAreaCompiling() |
---|
475 | { |
---|
476 | if( pCompilingUserProc == NULL ) |
---|
477 | { |
---|
478 | return true; |
---|
479 | } |
---|
480 | return ( pCompilingUserProc->GetName() == this->GetGlobalAreaProcName() ); |
---|
481 | } |
---|
482 | bool Compiler::IsLocalAreaCompiling() |
---|
483 | { |
---|
484 | return !IsGlobalAreaCompiling(); |
---|
485 | } |
---|
486 | const UserProc &Compiler::GetCompilingUserProc() |
---|
487 | { |
---|
488 | if( pCompilingUserProc == NULL ) |
---|
489 | { |
---|
490 | _ASSERTE( false ); |
---|
491 | throw; |
---|
492 | } |
---|
493 | return *pCompilingUserProc; |
---|
494 | } |
---|
495 | |
---|
496 | bool Compiler::IsCompilingClass() |
---|
497 | { |
---|
498 | return ( pCompilingClass != NULL ); |
---|
499 | } |
---|
500 | const CClass &Compiler::GetCompilingClass() |
---|
501 | { |
---|
502 | if( this->IsCompilingClass() ) |
---|
503 | { |
---|
504 | return *pCompilingClass; |
---|
505 | } |
---|
506 | |
---|
507 | throw; |
---|
508 | } |
---|