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