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