[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 |
|
---|
[628] | 16 | ActiveBasic::Compiler::Error::StringToTypeErrorCode::EnumType Compiler::StringToTypeEx( const std::string &typeName, Type &type, bool isResolveGenerics )
|
---|
| 17 | {
|
---|
[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 | // ジェネリクスクラスを取得
|
---|
[598] | 32 | const CClass *pGenericClass = this->GetObjectModule().meta.FindClassSupportedTypeDef(
|
---|
| 33 | LexicalAnalyzer::FullNameToSymbol( className )
|
---|
| 34 | );
|
---|
[290] | 35 |
|
---|
[301] | 36 | if( !pGenericClass )
|
---|
| 37 | {
|
---|
[628] | 38 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::NotfoundGenericClass;
|
---|
[301] | 39 | }
|
---|
[290] | 40 |
|
---|
[301] | 41 | if( typeName[i] != '<' )
|
---|
| 42 | {
|
---|
| 43 | Jenga::Throw( "StringToType内でジェネリクス構文の解析に失敗" );
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[290] | 46 | GenericTypes genericTypes;
|
---|
[301] | 47 | while( true )
|
---|
| 48 | {
|
---|
| 49 | i++;
|
---|
[290] | 50 |
|
---|
[301] | 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 |
|
---|
[290] | 66 | // 基本型をセット
|
---|
| 67 | type.SetBasicType( DEF_OBJECT );
|
---|
| 68 |
|
---|
| 69 | // 拡張情報をセット
|
---|
| 70 | type.SetClassPtr( pGenericClass );
|
---|
| 71 | type.SetActualGenericTypes( genericTypes );
|
---|
| 72 |
|
---|
[628] | 73 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[290] | 74 | }
|
---|
| 75 |
|
---|
| 76 | //
|
---|
| 77 | /////////////////////////////////////////////////////////
|
---|
| 78 |
|
---|
| 79 |
|
---|
[193] | 80 | if( typeName[0] == '*' ){
|
---|
| 81 | if( typeName.size() >= 3
|
---|
[575] | 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 ¶mStr = 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 );
|
---|
[628] | 105 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 106 | }
|
---|
| 107 |
|
---|
[523] | 108 | const std::string &nextTypeName = typeName.substr( 1 );
|
---|
[193] | 109 |
|
---|
[628] | 110 | ActiveBasic::Compiler::Error::StringToTypeErrorCode::EnumType result = StringToTypeEx( nextTypeName, type, isResolveGenerics );
|
---|
| 111 | if( result != ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful )
|
---|
| 112 | {
|
---|
| 113 | return result;
|
---|
[193] | 114 | }
|
---|
| 115 |
|
---|
| 116 | type.PtrLevelUp();
|
---|
| 117 |
|
---|
[628] | 118 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 119 | }
|
---|
| 120 |
|
---|
| 121 | {
|
---|
| 122 | int basicType;
|
---|
[628] | 123 | if( Type::StringToBasicType( typeName, basicType ) )
|
---|
| 124 | {
|
---|
[193] | 125 | // 基本型だったとき
|
---|
| 126 | type.SetBasicType( basicType );
|
---|
[628] | 127 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | // Object型だったとき
|
---|
[628] | 132 | if( typeName == "Object" )
|
---|
| 133 | {
|
---|
[299] | 134 | type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetObjectClassPtr() );
|
---|
[628] | 135 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 136 | }
|
---|
| 137 |
|
---|
| 138 | // String型だったとき
|
---|
[628] | 139 | if( typeName == "String" )
|
---|
| 140 | {
|
---|
[299] | 141 | type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetStringClassPtr() );
|
---|
[628] | 142 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | ////////////////////
|
---|
| 147 | // TypeDefされた型
|
---|
| 148 | ////////////////////
|
---|
[598] | 149 | int i=this->GetObjectModule().meta.GetTypeDefs().GetIndex( LexicalAnalyzer::FullNameToSymbol( typeName ) );
|
---|
[628] | 150 | if(i!=-1)
|
---|
| 151 | {
|
---|
[299] | 152 | type = this->GetObjectModule().meta.GetTypeDefs()[i].GetBaseType();
|
---|
[628] | 153 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 154 | }
|
---|
| 155 |
|
---|
| 156 | //クラス
|
---|
[598] | 157 | const CClass *pobj_c = this->GetObjectModule().meta.GetClasses().FindEx( LexicalAnalyzer::FullNameToSymbol( typeName ) );
|
---|
[628] | 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 | {
|
---|
[193] | 172 | type.SetBasicType( DEF_STRUCT );
|
---|
| 173 | }
|
---|
[628] | 174 | else
|
---|
| 175 | {
|
---|
[193] | 176 | type.SetBasicType( DEF_OBJECT );
|
---|
| 177 | }
|
---|
[290] | 178 | type.SetClassPtr( pobj_c );
|
---|
[628] | 179 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[193] | 180 | }
|
---|
| 181 |
|
---|
[290] | 182 |
|
---|
| 183 | /////////////////////////////////////////////////////////
|
---|
| 184 | // ☆★☆ ジェネリクスサポート ☆★☆
|
---|
| 185 |
|
---|
| 186 | // 型パラメータ
|
---|
[536] | 187 | if( this->IsCompilingClass() )
|
---|
[290] | 188 | {
|
---|
| 189 | // クラスに属するメソッドをコンパイルしているとき
|
---|
[536] | 190 | int formalTypeIndex = this->GetCompilingClass().GetFormalGenericTypeParameterIndex( typeName );
|
---|
[299] | 191 | if( formalTypeIndex != -1 )
|
---|
[290] | 192 | {
|
---|
| 193 | // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき
|
---|
| 194 | type.SetBasicType( DEF_TYPE_PARAMETER );
|
---|
[536] | 195 | type.SetClassPtr( &this->GetCompilingClass().GetFormalGenericTypes()[formalTypeIndex].GetType().GetClass() );
|
---|
[299] | 196 | type.SetFormalTypeName( typeName );
|
---|
| 197 | type.SetFormalTypeIndex( formalTypeIndex );
|
---|
[628] | 198 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful;
|
---|
[290] | 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | //
|
---|
| 203 | /////////////////////////////////////////////////////////
|
---|
| 204 |
|
---|
[628] | 205 | return ActiveBasic::Compiler::Error::StringToTypeErrorCode::NotfoundType;
|
---|
[193] | 206 | }
|
---|
| 207 |
|
---|
[628] | 208 | bool Compiler::StringToType( const std::string &typeName, Type &type )
|
---|
| 209 | {
|
---|
| 210 | return ( StringToTypeEx( typeName, type, false ) == ActiveBasic::Compiler::Error::StringToTypeErrorCode::Successful );
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[523] | 213 | const std::string Compiler::TypeToString( const Type &type )
|
---|
[193] | 214 | {
|
---|
| 215 | if( PTR_LEVEL( type.GetBasicType() ) ){
|
---|
| 216 | //ポインタレベルが1以上の場合
|
---|
| 217 | Type tempType( type );
|
---|
| 218 | tempType.PtrLevelDown();
|
---|
| 219 |
|
---|
[523] | 220 | return (std::string)"*" + TypeToString( tempType );
|
---|
[193] | 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{
|
---|
[299] | 238 | if( this->GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){
|
---|
[193] | 239 | return "*Sub";
|
---|
| 240 | }
|
---|
| 241 | return "*Function";
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | else{
|
---|
| 245 | // 基本型
|
---|
| 246 | const char *lpszTypeName = Type::BasicTypeToCharPtr( type );
|
---|
| 247 | if( lpszTypeName )
|
---|
| 248 | {
|
---|
[523] | 249 | return (const std::string)lpszTypeName;
|
---|
[193] | 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[465] | 253 | compiler.errorMessenger.Output(1, NULL, cp);
|
---|
[193] | 254 |
|
---|
[523] | 255 | return (std::string)"(null)";
|
---|
[193] | 256 | }
|
---|
[533] | 257 |
|
---|
[536] | 258 | void Compiler::ClearCompilingUserProcAndClass()
|
---|
| 259 | {
|
---|
| 260 | this->pCompilingUserProc = NULL;
|
---|
| 261 | this->pCompilingClass = NULL;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | void Compiler::SetCompilingClass( const CClass *pClass )
|
---|
| 265 | {
|
---|
| 266 | this->pCompilingClass = pClass;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[537] | 269 | void Compiler::SetCompilingUserProc( const UserProc *pUserProc )
|
---|
[533] | 270 | {
|
---|
[536] | 271 | this->pCompilingUserProc = pUserProc;
|
---|
| 272 |
|
---|
| 273 | this->SetCompilingClass( pUserProc->GetParentClassPtr() );
|
---|
[537] | 274 | }
|
---|
[533] | 275 |
|
---|
[537] | 276 | void Compiler::StartGlobalAreaCompile()
|
---|
| 277 | {
|
---|
| 278 | ClearCompilingUserProcAndClass();
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | void Compiler::StartProcedureCompile( const UserProc *pUserProc )
|
---|
| 282 | {
|
---|
| 283 | //コンパイル中の関数
|
---|
| 284 | this->SetCompilingUserProc( pUserProc );
|
---|
| 285 |
|
---|
[540] | 286 | if( pUserProc->HasParentClass() )
|
---|
| 287 | {
|
---|
| 288 | // クラスの使用チェック
|
---|
| 289 | pUserProc->GetParentClass().Using();
|
---|
| 290 | }
|
---|
[533] | 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 | }
|
---|
| 301 | void Compiler::FinishProcedureCompile()
|
---|
| 302 | {
|
---|
[536] | 303 | this->pCompilingUserProc = NULL;
|
---|
| 304 | this->pCompilingClass = NULL;
|
---|
[533] | 305 | }
|
---|
[536] | 306 |
|
---|
| 307 | bool Compiler::IsGlobalAreaCompiling()
|
---|
| 308 | {
|
---|
| 309 | return ( pCompilingUserProc == NULL );
|
---|
| 310 | }
|
---|
[537] | 311 | bool Compiler::IsLocalAreaCompiling()
|
---|
| 312 | {
|
---|
| 313 | return ( pCompilingUserProc != NULL );
|
---|
| 314 | }
|
---|
[536] | 315 | const UserProc &Compiler::GetCompilingUserProc()
|
---|
| 316 | {
|
---|
| 317 | if( !this->IsGlobalAreaCompiling() )
|
---|
| 318 | {
|
---|
| 319 | return *pCompilingUserProc;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | throw;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | bool Compiler::IsCompilingClass()
|
---|
| 326 | {
|
---|
| 327 | return ( pCompilingClass != NULL );
|
---|
| 328 | }
|
---|
| 329 | const CClass &Compiler::GetCompilingClass()
|
---|
| 330 | {
|
---|
| 331 | if( this->IsCompilingClass() )
|
---|
| 332 | {
|
---|
| 333 | return *pCompilingClass;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | throw;
|
---|
| 337 | }
|
---|