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