#include "stdafx.h" #include #include Compiler compiler; void Compiler::StaticLink( ObjectModules &staticLibraries ) { BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries ) { // メタ情報 pNowObjectModule->StaticLink( *pStaticLibrary ); } } bool Compiler::StringToType( const std::string &typeName, Type &type ){ type.SetIndex( -1 ); ///////////////////////////////////////////////////////// // ☆★☆ ジェネリクスサポート ☆★☆ if( strstr( typeName.c_str(), "<" ) ) { // ジェネリッククラスをインスタンス化した型の場合 int i = 0; char className[VN_SIZE]; GetIdentifierToken( className, typeName.c_str(), i ); // ジェネリクスクラスを取得 const CClass *pGenericClass = this->GetObjectModule().meta.GetClasses().Find( className ); if( !pGenericClass ) { return false; } if( typeName[i] != '<' ) { Jenga::Throw( "StringToType内でジェネリクス構文の解析に失敗" ); } GenericTypes genericTypes; while( true ) { i++; char typeParameter[VN_SIZE]; GetIdentifierToken( typeParameter, typeName.c_str(), i ); // 型パラメータの型情報を取得 Type baseType; StringToType( typeParameter, baseType ); genericTypes.push_back( GenericType( "(non support)", baseType ) ); if( typeName[i] != ',' ) { break; } } // 基本型をセット type.SetBasicType( DEF_OBJECT ); // 拡張情報をセット type.SetClassPtr( pGenericClass ); type.SetActualGenericTypes( genericTypes ); return true; } // ///////////////////////////////////////////////////////// if( typeName[0] == '*' ){ if( typeName.size() >= 3 && typeName[1] == 1 && ( typeName[2] == ESC_FUNCTION || typeName[2] == ESC_SUB ) ){ //関数ポインタ(*Function) type.SetBasicType( DEF_PTR_PROC ); type.SetIndex( this->GetObjectModule().meta.GetProcPointers().Add( typeName ) ); return true; } const std::string &nextTypeName = typeName.substr( 1 ); if( !StringToType( nextTypeName, type ) ){ return false; } type.PtrLevelUp(); return true; } { int basicType; if( Type::StringToBasicType( typeName, basicType ) ){ // 基本型だったとき type.SetBasicType( basicType ); return true; } } // Object型だったとき if( typeName == "Object" ){ type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetObjectClassPtr() ); return true; } // String型だったとき if( typeName == "String" ){ type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetStringClassPtr() ); return true; } //////////////////// // TypeDefされた型 //////////////////// int i=this->GetObjectModule().meta.GetTypeDefs().GetIndex( typeName ); if(i!=-1){ type = this->GetObjectModule().meta.GetTypeDefs()[i].GetBaseType(); return true; } //クラス const CClass *pobj_c = this->GetObjectModule().meta.GetClasses().Find( typeName ); if(pobj_c){ if( pobj_c->IsStructure() ){ type.SetBasicType( DEF_STRUCT ); } else{ type.SetBasicType( DEF_OBJECT ); } type.SetClassPtr( pobj_c ); return true; } ///////////////////////////////////////////////////////// // ☆★☆ ジェネリクスサポート ☆★☆ // 型パラメータ if( this->IsCompilingClass() ) { // クラスに属するメソッドをコンパイルしているとき int formalTypeIndex = this->GetCompilingClass().GetFormalGenericTypeParameterIndex( typeName ); if( formalTypeIndex != -1 ) { // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき type.SetBasicType( DEF_TYPE_PARAMETER ); type.SetClassPtr( &this->GetCompilingClass().GetFormalGenericTypes()[formalTypeIndex].GetType().GetClass() ); type.SetFormalTypeName( typeName ); type.SetFormalTypeIndex( formalTypeIndex ); return true; } } // ///////////////////////////////////////////////////////// return false; } const std::string Compiler::TypeToString( const Type &type ) { if( PTR_LEVEL( type.GetBasicType() ) ){ //ポインタレベルが1以上の場合 Type tempType( type ); tempType.PtrLevelDown(); return (std::string)"*" + TypeToString( tempType ); } else if( type.IsObject() || type.IsStruct() ){ //オブジェクトまたは構造体 if( !( type.GetIndex() == 0 || type.GetIndex() == -1 ) ){ if( type.GetClass().GetNamespaceScopes().size() >= 1 ) { return type.GetClass().GetNamespaceScopes().ToString() + "." + type.GetClass().GetName(); } return type.GetClass().GetName(); } } else if( type.IsProcPtr() ){ if( type.GetIndex() == 0 || type.GetIndex() == -1 ){ return "VoidPtr"; } else{ if( this->GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){ return "*Sub"; } return "*Function"; } } else{ // 基本型 const char *lpszTypeName = Type::BasicTypeToCharPtr( type ); if( lpszTypeName ) { return (const std::string)lpszTypeName; } } compiler.errorMessenger.Output(1, NULL, cp); return (std::string)"(null)"; } void Compiler::ClearCompilingUserProcAndClass() { this->pCompilingUserProc = NULL; this->pCompilingClass = NULL; } void Compiler::SetCompilingClass( const CClass *pClass ) { this->pCompilingClass = pClass; } void Compiler::StartProcedureCompile( const UserProc *pUserProc ) { //コンパイル中の関数 this->pCompilingUserProc = pUserProc; //コンパイル中の関数が属するクラス this->SetCompilingClass( pUserProc->GetParentClassPtr() ); //コンパイルスタートをクラス管理クラスに追加 this->GetObjectModule().meta.GetClasses().StartCompile( pUserProc ); //コンパイル中の関数 UserProc::CompileStartForUserProc( pUserProc ); // コンパイル中の関数が属する名前空間 this->GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() ); // コンパイル中の関数でImportsされている名前空間 this->GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() ); // コード生成対象を選択 this->codeGenerator.Select( (const_cast(pUserProc))->GetNativeCode() ); } void Compiler::FinishProcedureCompile() { this->pCompilingUserProc = NULL; this->pCompilingClass = NULL; } bool Compiler::IsGlobalAreaCompiling() { return ( pCompilingUserProc == NULL ); } const UserProc &Compiler::GetCompilingUserProc() { if( !this->IsGlobalAreaCompiling() ) { return *pCompilingUserProc; } throw; } bool Compiler::IsCompilingClass() { return ( pCompilingClass != NULL ); } const CClass &Compiler::GetCompilingClass() { if( this->IsCompilingClass() ) { return *pCompilingClass; } throw; }