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