| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <Compiler.h>
|
|---|
| 4 | #include <Type.h>
|
|---|
| 5 |
|
|---|
| 6 | using namespace ActiveBasic::Compiler;
|
|---|
| 7 |
|
|---|
| 8 | Compiler compiler;
|
|---|
| 9 |
|
|---|
| 10 | void Compiler::StaticLink( ObjectModules &staticLibraries )
|
|---|
| 11 | {
|
|---|
| 12 | BOOST_FOREACH( ObjectModule *pStaticLibrary, staticLibraries )
|
|---|
| 13 | {
|
|---|
| 14 | // メタ情報
|
|---|
| 15 | pNowObjectModule->StaticLink( *pStaticLibrary );
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | bool Compiler::StringToType( const std::string &typeName, Type &type ){
|
|---|
| 20 | type.SetIndex( -1 );
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | /////////////////////////////////////////////////////////
|
|---|
| 24 | // ☆★☆ ジェネリクスサポート ☆★☆
|
|---|
| 25 |
|
|---|
| 26 | if( strstr( typeName.c_str(), "<" ) )
|
|---|
| 27 | {
|
|---|
| 28 | // ジェネリッククラスをインスタンス化した型の場合
|
|---|
| 29 | int i = 0;
|
|---|
| 30 | char className[VN_SIZE];
|
|---|
| 31 | GetIdentifierToken( className, typeName.c_str(), i );
|
|---|
| 32 |
|
|---|
| 33 | // ジェネリクスクラスを取得
|
|---|
| 34 | const CClass *pGenericClass = this->GetObjectModule().meta.FindClassSupportedTypeDef(
|
|---|
| 35 | LexicalAnalyzer::FullNameToSymbol( className )
|
|---|
| 36 | );
|
|---|
| 37 |
|
|---|
| 38 | if( !pGenericClass )
|
|---|
| 39 | {
|
|---|
| 40 | return false;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | if( typeName[i] != '<' )
|
|---|
| 44 | {
|
|---|
| 45 | Jenga::Throw( "StringToType内でジェネリクス構文の解析に失敗" );
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | GenericTypes genericTypes;
|
|---|
| 49 | while( true )
|
|---|
| 50 | {
|
|---|
| 51 | i++;
|
|---|
| 52 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 82 | if( typeName[0] == '*' ){
|
|---|
| 83 | if( typeName.size() >= 3
|
|---|
| 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;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | const std::string &nextTypeName = typeName.substr( 1 );
|
|---|
| 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" ){
|
|---|
| 132 | type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetObjectClassPtr() );
|
|---|
| 133 | return true;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | // String型だったとき
|
|---|
| 137 | if( typeName == "String" ){
|
|---|
| 138 | type.SetType( DEF_OBJECT, this->GetObjectModule().meta.GetClasses().GetStringClassPtr() );
|
|---|
| 139 | return true;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | ////////////////////
|
|---|
| 144 | // TypeDefされた型
|
|---|
| 145 | ////////////////////
|
|---|
| 146 | int i=this->GetObjectModule().meta.GetTypeDefs().GetIndex( LexicalAnalyzer::FullNameToSymbol( typeName ) );
|
|---|
| 147 | if(i!=-1){
|
|---|
| 148 | type = this->GetObjectModule().meta.GetTypeDefs()[i].GetBaseType();
|
|---|
| 149 | return true;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | //クラス
|
|---|
| 153 | const CClass *pobj_c = this->GetObjectModule().meta.GetClasses().FindEx( LexicalAnalyzer::FullNameToSymbol( typeName ) );
|
|---|
| 154 | if(pobj_c){
|
|---|
| 155 | if( pobj_c->IsStructure() ){
|
|---|
| 156 | type.SetBasicType( DEF_STRUCT );
|
|---|
| 157 | }
|
|---|
| 158 | else{
|
|---|
| 159 | type.SetBasicType( DEF_OBJECT );
|
|---|
| 160 | }
|
|---|
| 161 | type.SetClassPtr( pobj_c );
|
|---|
| 162 | return true;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | /////////////////////////////////////////////////////////
|
|---|
| 167 | // ☆★☆ ジェネリクスサポート ☆★☆
|
|---|
| 168 |
|
|---|
| 169 | // 型パラメータ
|
|---|
| 170 | if( this->IsCompilingClass() )
|
|---|
| 171 | {
|
|---|
| 172 | // クラスに属するメソッドをコンパイルしているとき
|
|---|
| 173 | int formalTypeIndex = this->GetCompilingClass().GetFormalGenericTypeParameterIndex( typeName );
|
|---|
| 174 | if( formalTypeIndex != -1 )
|
|---|
| 175 | {
|
|---|
| 176 | // コンパイル中クラスにおけるジェネリクス用の型パラメータのとき
|
|---|
| 177 | type.SetBasicType( DEF_TYPE_PARAMETER );
|
|---|
| 178 | type.SetClassPtr( &this->GetCompilingClass().GetFormalGenericTypes()[formalTypeIndex].GetType().GetClass() );
|
|---|
| 179 | type.SetFormalTypeName( typeName );
|
|---|
| 180 | type.SetFormalTypeIndex( formalTypeIndex );
|
|---|
| 181 | return true;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | //
|
|---|
| 186 | /////////////////////////////////////////////////////////
|
|---|
| 187 |
|
|---|
| 188 | return false;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | const std::string Compiler::TypeToString( const Type &type )
|
|---|
| 192 | {
|
|---|
| 193 | if( PTR_LEVEL( type.GetBasicType() ) ){
|
|---|
| 194 | //ポインタレベルが1以上の場合
|
|---|
| 195 | Type tempType( type );
|
|---|
| 196 | tempType.PtrLevelDown();
|
|---|
| 197 |
|
|---|
| 198 | return (std::string)"*" + TypeToString( tempType );
|
|---|
| 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{
|
|---|
| 216 | if( this->GetObjectModule().meta.GetProcPointers()[type.GetIndex()]->ReturnType().IsNull() ){
|
|---|
| 217 | return "*Sub";
|
|---|
| 218 | }
|
|---|
| 219 | return "*Function";
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | else{
|
|---|
| 223 | // 基本型
|
|---|
| 224 | const char *lpszTypeName = Type::BasicTypeToCharPtr( type );
|
|---|
| 225 | if( lpszTypeName )
|
|---|
| 226 | {
|
|---|
| 227 | return (const std::string)lpszTypeName;
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | compiler.errorMessenger.Output(1, NULL, cp);
|
|---|
| 232 |
|
|---|
| 233 | return (std::string)"(null)";
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 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 |
|
|---|
| 247 | void Compiler::SetCompilingUserProc( const UserProc *pUserProc )
|
|---|
| 248 | {
|
|---|
| 249 | this->pCompilingUserProc = pUserProc;
|
|---|
| 250 |
|
|---|
| 251 | this->SetCompilingClass( pUserProc->GetParentClassPtr() );
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | void Compiler::StartGlobalAreaCompile()
|
|---|
| 255 | {
|
|---|
| 256 | ClearCompilingUserProcAndClass();
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | void Compiler::StartProcedureCompile( const UserProc *pUserProc )
|
|---|
| 260 | {
|
|---|
| 261 | //コンパイル中の関数
|
|---|
| 262 | this->SetCompilingUserProc( pUserProc );
|
|---|
| 263 |
|
|---|
| 264 | if( pUserProc->HasParentClass() )
|
|---|
| 265 | {
|
|---|
| 266 | // クラスの使用チェック
|
|---|
| 267 | pUserProc->GetParentClass().Using();
|
|---|
| 268 | }
|
|---|
| 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 | {
|
|---|
| 281 | this->pCompilingUserProc = NULL;
|
|---|
| 282 | this->pCompilingClass = NULL;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | bool Compiler::IsGlobalAreaCompiling()
|
|---|
| 286 | {
|
|---|
| 287 | return ( pCompilingUserProc == NULL );
|
|---|
| 288 | }
|
|---|
| 289 | bool Compiler::IsLocalAreaCompiling()
|
|---|
| 290 | {
|
|---|
| 291 | return ( pCompilingUserProc != NULL );
|
|---|
| 292 | }
|
|---|
| 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 | }
|
|---|