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