From 8406e00a60fb2587de4a2a9b0bcf5673fd3a4665 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sun, 31 Jan 2021 10:23:39 -0800 Subject: [PATCH] Made Compiler 'Caller' values work inside mixins --- IDEHelper/Compiler/BfExprEvaluator.cpp | 7 ++++ IDEHelper/Compiler/BfModule.cpp | 44 ++++++++++++++++++++++++++ IDEHelper/Compiler/BfModule.h | 1 + 3 files changed, 52 insertions(+) diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index 25f0ba51..69c6d1d2 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -6889,6 +6889,12 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu argValue = BfTypedValue(mModule->GetStringObjectValue(projectName), mModule->ResolveTypeDef(mModule->mCompiler->mStringTypeDef)); } + else if (strcmp(globalVar->mName, "#ProjectName") == 0) + { + String projectName = methodInstance->mMethodDef->mDeclaringType->mProject->mName; + argValue = BfTypedValue(mModule->GetStringObjectValue(projectName), + mModule->ResolveTypeDef(mModule->mCompiler->mStringTypeDef)); + } else { argValue = mModule->GetCompilerFieldValue(globalVar->mName); @@ -15185,6 +15191,7 @@ void BfExprEvaluator::InjectMixin(BfAstNode* targetSrc, BfTypedValue target, boo auto rootMethodState = mModule->mCurMethodState->GetRootMethodState(); BfMixinState* mixinState = rootMethodState->mMixinStates.Alloc(); + mixinState->mInjectFilePosition = mModule->mCurFilePosition; mixinState->mPrevMixinState = curMethodState->mMixinState; mixinState->mLocalsStartIdx = (int)mModule->mCurMethodState->mLocals.size(); mixinState->mMixinMethodInstance = methodInstance; diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index 41687e09..fc0897d4 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -13521,6 +13521,50 @@ BfTypedValue BfModule::GetCompilerFieldValue(const StringImpl& str) if (mProject != NULL) return BfTypedValue(GetStringObjectValue(mProject->mName), ResolveTypeDef(mCompiler->mStringTypeDef)); } + + if (mCurMethodState->mMixinState != NULL) + { + if (str == "#CallerLineNum") + { + return BfTypedValue(GetConstValue(mCurMethodState->mMixinState->mInjectFilePosition.mCurLine + 1), GetPrimitiveType(BfTypeCode_Int32)); + } + else if (str == "#CallerFilePath") + { + String filePath = ""; + if (mCurMethodState->mMixinState->mInjectFilePosition.mFileInstance != NULL) + filePath = mCurMethodState->mMixinState->mInjectFilePosition.mFileInstance->mParser->mFileName; + return BfTypedValue(GetStringObjectValue(filePath), ResolveTypeDef(mCompiler->mStringTypeDef)); + } + else if (str == "#CallerFileName") + { + String filePath = ""; + if (mCurMethodState->mMixinState->mInjectFilePosition.mFileInstance != NULL) + filePath = mCurMethodState->mMixinState->mInjectFilePosition.mFileInstance->mParser->mFileName; + return BfTypedValue(GetStringObjectValue(GetFileName(filePath)), ResolveTypeDef(mCompiler->mStringTypeDef)); + } + else if (str == "#CallerFileDir") + { + String filePath = ""; + if (mCurMethodState->mMixinState->mInjectFilePosition.mFileInstance != NULL) + filePath = mCurMethodState->mMixinState->mInjectFilePosition.mFileInstance->mParser->mFileName; + return BfTypedValue(GetStringObjectValue(GetFileDir(filePath)), ResolveTypeDef(mCompiler->mStringTypeDef)); + } + else if (str == "#CallerMemberName") + { + String memberName = ""; + if (mCurMethodState->mMixinState->mMixinMethodInstance) + memberName = MethodToString(mCurMethodState->mMixinState->mMixinMethodInstance); + return BfTypedValue(GetStringObjectValue(memberName), ResolveTypeDef(mCompiler->mStringTypeDef)); + } + else if (str == "#CallerProject") + { + BfProject* project = NULL; + project = mCurMethodState->mMixinState->mMixinMethodInstance->mMethodDef->mDeclaringType->mProject; + if (project != NULL) + return BfTypedValue(GetStringObjectValue(mProject->mName), ResolveTypeDef(mCompiler->mStringTypeDef)); + } + } + if (str == "#TimeLocal") { time_t rawtime; diff --git a/IDEHelper/Compiler/BfModule.h b/IDEHelper/Compiler/BfModule.h index 29f46f56..fedc977d 100644 --- a/IDEHelper/Compiler/BfModule.h +++ b/IDEHelper/Compiler/BfModule.h @@ -767,6 +767,7 @@ public: BfAstNode* mSource; BfScopeData* mCallerScope; BfScopeData* mTargetScope; // Equals caller scope unless user explicitly calls specifies scope override + BfFilePosition mInjectFilePosition; BfMethodInstance* mMixinMethodInstance; BfAstNode* mResultExpr; int mLocalsStartIdx;