mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 12:32:20 +02:00
Initializer expressions
This commit is contained in:
parent
f6e31e4976
commit
229a5aa5c5
17 changed files with 305 additions and 18 deletions
|
@ -196,6 +196,11 @@ void BfStructuralVisitor::Visit(BfSizedArrayCreateExpression* createExpr)
|
|||
Visit(createExpr->ToBase());
|
||||
}
|
||||
|
||||
void BfStructuralVisitor::Visit(BfInitializerExpression* initExpr)
|
||||
{
|
||||
Visit(initExpr->ToBase());
|
||||
}
|
||||
|
||||
void BfStructuralVisitor::Visit(BfCollectionInitializerExpression* collectionInitExpr)
|
||||
{
|
||||
Visit(collectionInitExpr->ToBase());
|
||||
|
|
|
@ -354,6 +354,7 @@ class BfStrideOfExpression;
|
|||
class BfDefaultExpression;
|
||||
class BfUninitializedExpression;
|
||||
class BfConditionalExpression;
|
||||
class BfInitializerExpression;
|
||||
class BfCollectionInitializerExpression;
|
||||
class BfSizedArrayCreateExpression;
|
||||
class BfEmptyStatement;
|
||||
|
@ -435,6 +436,7 @@ public:
|
|||
virtual void Visit(BfBaseExpression* baseExpr);
|
||||
virtual void Visit(BfMixinExpression* thisExpr);
|
||||
virtual void Visit(BfSizedArrayCreateExpression* createExpr);
|
||||
virtual void Visit(BfInitializerExpression* collectionInitExpr);
|
||||
virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr);
|
||||
virtual void Visit(BfTypeReference* typeRef);
|
||||
virtual void Visit(BfNamedTypeReference* typeRef);
|
||||
|
@ -2017,6 +2019,18 @@ public:
|
|||
BfVariant mValue;
|
||||
}; BF_AST_DECL(BfLiteralExpression, BfExpression);
|
||||
|
||||
class BfInitializerExpression : public BfExpression
|
||||
{
|
||||
public:
|
||||
BF_AST_TYPE(BfInitializerExpression, BfExpression);
|
||||
|
||||
BfExpression* mTarget;
|
||||
BfTokenNode* mOpenBrace;
|
||||
BfSizedArray<BfExpression*> mValues;
|
||||
BfSizedArray<BfTokenNode*> mCommas;
|
||||
BfTokenNode* mCloseBrace;
|
||||
}; BF_AST_DECL(BfInitializerExpression, BfExpression);
|
||||
|
||||
class BfCollectionInitializerExpression : public BfExpression
|
||||
{
|
||||
public:
|
||||
|
@ -2606,7 +2620,7 @@ public:
|
|||
BfTokenNode* mOpenToken;
|
||||
BfTokenNode* mCloseToken;
|
||||
BfSizedArray<BfExpression*> mArguments;
|
||||
BfSizedArray<BfTokenNode*> mCommas;
|
||||
BfSizedArray<BfTokenNode*> mCommas;
|
||||
}; BF_AST_DECL(BfObjectCreateExpression, BfMethodBoundExpression);
|
||||
|
||||
class BfBoxExpression : public BfExpression
|
||||
|
|
|
@ -1920,9 +1920,13 @@ void BfDefBuilder::FinishTypeDef(bool wantsToString)
|
|||
|
||||
if ((needsDefaultCtor) && (!hasDefaultCtor))
|
||||
{
|
||||
BfProtection prot = hasCtor ? BfProtection_Hidden : BfProtection_Public;
|
||||
if (mCurTypeDef->mName == mSystem->mEmptyAtom)
|
||||
prot = BfProtection_Hidden;
|
||||
|
||||
// Create default constructor. If it's the only constructor then make it public,
|
||||
// otherwise make it private so we can still internally use it but the user can't
|
||||
auto methodDef = AddMethod(mCurTypeDef, BfMethodType_Ctor, hasCtor ? BfProtection_Hidden : BfProtection_Public, false, "");
|
||||
auto methodDef = AddMethod(mCurTypeDef, BfMethodType_Ctor, prot, false, "");
|
||||
methodDef->mIsMutating = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -244,6 +244,19 @@ void BfElementVisitor::Visit(BfSizedArrayCreateExpression* createExpr)
|
|||
VisitChild(createExpr->mInitializer);
|
||||
}
|
||||
|
||||
void BfElementVisitor::Visit(BfInitializerExpression* initExpr)
|
||||
{
|
||||
Visit(initExpr->ToBase());
|
||||
|
||||
VisitChild(initExpr->mTarget);
|
||||
VisitChild(initExpr->mOpenBrace);
|
||||
for (auto& val : initExpr->mValues)
|
||||
VisitChild(val);
|
||||
for (auto& val : initExpr->mCommas)
|
||||
VisitChild(val);
|
||||
VisitChild(initExpr->mCloseBrace);
|
||||
}
|
||||
|
||||
void BfElementVisitor::Visit(BfCollectionInitializerExpression* collectionInitExpr)
|
||||
{
|
||||
Visit(collectionInitExpr->ToBase());
|
||||
|
|
|
@ -43,6 +43,7 @@ public:
|
|||
virtual void Visit(BfBaseExpression* baseExpr);
|
||||
virtual void Visit(BfMixinExpression* thisExpr);
|
||||
virtual void Visit(BfSizedArrayCreateExpression* createExpr);
|
||||
virtual void Visit(BfInitializerExpression* initExpr);
|
||||
virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr);
|
||||
virtual void Visit(BfTypeReference* typeRef);
|
||||
virtual void Visit(BfNamedTypeReference* typeRef);
|
||||
|
|
|
@ -8433,6 +8433,111 @@ void BfExprEvaluator::Visit(BfSizedArrayCreateExpression* createExpr)
|
|||
InitializedSizedArray(arrayType, createExpr->mInitializer->mOpenBrace, createExpr->mInitializer->mValues, createExpr->mInitializer->mCommas, createExpr->mInitializer->mCloseBrace);
|
||||
}
|
||||
|
||||
void BfExprEvaluator::Visit(BfInitializerExpression* initExpr)
|
||||
{
|
||||
VisitChild(initExpr->mTarget);
|
||||
if (!mResult)
|
||||
mResult = mModule->GetDefaultTypedValue(mModule->mContext->mBfObjectType);
|
||||
|
||||
BfTypedValue initValue = GetResult(true);
|
||||
bool isFirstAdd = true;
|
||||
BfFunctionBindResult addFunctionBindResult;
|
||||
addFunctionBindResult.mWantsArgs = true;
|
||||
|
||||
for (auto elementExpr : initExpr->mValues)
|
||||
{
|
||||
bool wasValidInitKind = false;
|
||||
|
||||
if (auto assignExpr = BfNodeDynCast<BfAssignmentExpression>(elementExpr))
|
||||
{
|
||||
BfTypedValue fieldResult;
|
||||
if (auto identifierNode = BfNodeDynCast<BfIdentifierNode>(assignExpr->mLeft))
|
||||
{
|
||||
StringT<128> findName;
|
||||
identifierNode->ToString(findName);
|
||||
fieldResult = LookupField(identifierNode, initValue, findName, BfLookupFieldFlag_IsImplicitThis);
|
||||
if (fieldResult.mKind == BfTypedValueKind_TempAddr)
|
||||
fieldResult.mKind = BfTypedValueKind_Addr;
|
||||
|
||||
wasValidInitKind = true;
|
||||
|
||||
if ((fieldResult) || (mPropDef != NULL))
|
||||
{
|
||||
mResult = fieldResult;
|
||||
PerformAssignment(assignExpr, true, BfTypedValue());
|
||||
mResult = BfTypedValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
mModule->Fail(StrFormat("'%s' does not contain a definition for '%s'", mModule->TypeToString(initValue.mType).c_str(),
|
||||
findName.c_str()), identifierNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto autoComplete = GetAutoComplete();
|
||||
if ((autoComplete != NULL) && (autoComplete->IsAutocompleteNode(elementExpr)))
|
||||
{
|
||||
if (auto identiferNode = BfNodeDynCast<BfIdentifierNode>(elementExpr))
|
||||
{
|
||||
auto typeInstance = initValue.mType->ToTypeInstance();
|
||||
if (typeInstance != NULL)
|
||||
{
|
||||
String filter;
|
||||
identiferNode->ToString(filter);
|
||||
autoComplete->AddTypeMembers(typeInstance, false, true, filter, typeInstance, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool wasFirstAdd = isFirstAdd;
|
||||
if (isFirstAdd)
|
||||
{
|
||||
BfExprEvaluator exprEvaluator(mModule);
|
||||
exprEvaluator.mFunctionBindResult = &addFunctionBindResult;
|
||||
SizedArray<BfExpression*, 2> argExprs;
|
||||
argExprs.push_back(elementExpr);
|
||||
BfSizedArray<BfExpression*> sizedArgExprs(argExprs);
|
||||
BfResolvedArgs argValues(&sizedArgExprs);
|
||||
exprEvaluator.ResolveArgValues(argValues);
|
||||
exprEvaluator.MatchMethod(elementExpr, NULL, initValue, false, false, "Add", argValues, NULL);
|
||||
|
||||
if (addFunctionBindResult.mMethodInstance != NULL)
|
||||
CreateCall(addFunctionBindResult.mMethodInstance, addFunctionBindResult.mFunc, true, addFunctionBindResult.mIRArgs);
|
||||
|
||||
isFirstAdd = false;
|
||||
}
|
||||
else if ((addFunctionBindResult.mMethodInstance == NULL) || (addFunctionBindResult.mMethodInstance->GetParamCount() == 0))
|
||||
{
|
||||
mModule->CreateValueFromExpression(elementExpr);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto argValue = mModule->CreateValueFromExpression(elementExpr, addFunctionBindResult.mMethodInstance->GetParamType(0));
|
||||
if ((argValue) && (!mModule->mBfIRBuilder->mIgnoreWrites))
|
||||
{
|
||||
SizedArray<BfIRValue, 2> irArgs;
|
||||
PushThis(elementExpr, initValue, addFunctionBindResult.mMethodInstance, irArgs);
|
||||
PushArg(argValue, irArgs);
|
||||
for (int argIdx = (int)irArgs.size(); argIdx < (int)addFunctionBindResult.mIRArgs.size(); argIdx++)
|
||||
irArgs.Add(addFunctionBindResult.mIRArgs[argIdx]);
|
||||
CreateCall(addFunctionBindResult.mMethodInstance, addFunctionBindResult.mFunc, true, irArgs);
|
||||
}
|
||||
}
|
||||
|
||||
wasValidInitKind = true;
|
||||
}
|
||||
|
||||
if (!wasValidInitKind)
|
||||
{
|
||||
mModule->Fail("Invalid initializer member declarator", initExpr);
|
||||
}
|
||||
}
|
||||
|
||||
mResult = initValue;
|
||||
}
|
||||
|
||||
void BfExprEvaluator::Visit(BfCollectionInitializerExpression* arrayInitExpr)
|
||||
{
|
||||
mModule->Fail("Collection initializer not usable here", arrayInitExpr);
|
||||
|
@ -13369,7 +13474,6 @@ void BfExprEvaluator::InjectMixin(BfAstNode* targetSrc, BfTypedValue target, boo
|
|||
}
|
||||
curMethodState->AddScope(&scopeData);
|
||||
curMethodState->mCurScope->mMixinDepth++;
|
||||
curMethodState->mIsEmbedded = false;
|
||||
// We can't flush scope state because we extend params in as arbitrary values
|
||||
mModule->NewScopeState(true, false);
|
||||
|
||||
|
|
|
@ -436,6 +436,7 @@ public:
|
|||
virtual void Visit(BfBaseExpression* baseExpr) override;
|
||||
virtual void Visit(BfMixinExpression* mixinExpr) override;
|
||||
virtual void Visit(BfSizedArrayCreateExpression* createExpr) override;
|
||||
virtual void Visit(BfInitializerExpression* initExpr) override;
|
||||
virtual void Visit(BfCollectionInitializerExpression* initExpr) override;
|
||||
virtual void Visit(BfTypeOfExpression* typeOfExpr) override;
|
||||
virtual void Visit(BfSizeOfExpression* sizeOfExpr) override;
|
||||
|
|
|
@ -1097,7 +1097,7 @@ void BfModule::EnsureIRBuilder(bool dbgVerifyCodeGen)
|
|||
// code as we walk the AST
|
||||
//mBfIRBuilder->mDbgVerifyCodeGen = true;
|
||||
if (
|
||||
(mModuleName == "-")
|
||||
(mModuleName == "-")
|
||||
//|| (mModuleName == "Tests_FuncRefs_Class")
|
||||
//|| (mModuleName == "Tests_FuncRefs")
|
||||
)
|
||||
|
|
|
@ -909,8 +909,7 @@ public:
|
|||
BfIRValue mDynStackRevIdx; // Increments when we restore the stack, which can invalidate dynSize for dynamic looped allocs
|
||||
BfIRBlock mIRExitBlock;
|
||||
BfBreakData* mBreakData;
|
||||
int mBlockNestLevel; // 0 = top level
|
||||
bool mIsEmbedded; // Is an embedded statement (ie: if () stmt) not wrapped in a block
|
||||
int mBlockNestLevel; // 0 = top level
|
||||
bool mIgnoreObjectAccessCheck;
|
||||
bool mDisableChecks;
|
||||
BfMixinState* mMixinState;
|
||||
|
@ -969,8 +968,7 @@ public:
|
|||
mBlockNestLevel = 0;
|
||||
mInPostReturn = false;
|
||||
mCrossingMixin = false;
|
||||
mNoBind = false;
|
||||
mIsEmbedded = false;
|
||||
mNoBind = false;
|
||||
mIgnoreObjectAccessCheck = false;
|
||||
mDisableChecks = false;
|
||||
mInConditionalBlock = false;
|
||||
|
|
|
@ -1206,6 +1206,25 @@ void BfPrinter::Visit(BfSizedArrayCreateExpression* createExpr)
|
|||
VisitChildWithPrecedingSpace(createExpr->mInitializer);
|
||||
}
|
||||
|
||||
void BfPrinter::Visit(BfInitializerExpression* initExpr)
|
||||
{
|
||||
Visit(initExpr->ToBase());
|
||||
|
||||
VisitChild(initExpr->mTarget);
|
||||
ExpectSpace();
|
||||
VisitChild(initExpr->mOpenBrace);
|
||||
for (int i = 0; i < (int)initExpr->mValues.size(); i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
VisitChild(initExpr->mCommas[i - 1]);
|
||||
ExpectSpace();
|
||||
}
|
||||
VisitChild(initExpr->mValues[i]);
|
||||
}
|
||||
VisitChild(initExpr->mCloseBrace);
|
||||
}
|
||||
|
||||
void BfPrinter::Visit(BfCollectionInitializerExpression* initExpr)
|
||||
{
|
||||
Visit(initExpr->ToBase());
|
||||
|
|
|
@ -130,6 +130,7 @@ public:
|
|||
virtual void Visit(BfBaseExpression* baseExpr) override;
|
||||
virtual void Visit(BfMixinExpression* mixinExpr) override;
|
||||
virtual void Visit(BfSizedArrayCreateExpression* createExpr) override;
|
||||
virtual void Visit(BfInitializerExpression* initExpr) override;
|
||||
virtual void Visit(BfCollectionInitializerExpression* initExpr) override;
|
||||
virtual void Visit(BfTypeReference* typeRef) override;
|
||||
virtual void Visit(BfNamedTypeReference* typeRef) override;
|
||||
|
|
|
@ -1750,7 +1750,11 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
|
|||
else if (isDelegateBind)
|
||||
exprLeft = CreateDelegateBindExpression(allocNode);
|
||||
else
|
||||
{
|
||||
exprLeft = CreateObjectCreateExpression(allocNode);
|
||||
if (auto initExpr = TryCreateInitializerExpression(exprLeft))
|
||||
exprLeft = initExpr;
|
||||
}
|
||||
|
||||
if (token == BfToken_Append)
|
||||
{
|
||||
|
@ -2642,6 +2646,8 @@ BfExpression* BfReducer::CreateExpression(BfAstNode* node, CreateExprFlags creat
|
|||
else if (token == BfToken_LParen)
|
||||
{
|
||||
exprLeft = CreateInvocationExpression(exprLeft, (CreateExprFlags)(createExprFlags & ~(CreateExprFlags_NoCast)));
|
||||
if (auto initExpr = TryCreateInitializerExpression(exprLeft))
|
||||
exprLeft = initExpr;
|
||||
}
|
||||
else if ((token == BfToken_LBracket) || (token == BfToken_QuestionLBracket))
|
||||
{
|
||||
|
@ -6900,6 +6906,60 @@ BfInvocationExpression* BfReducer::CreateInvocationExpression(BfAstNode* target,
|
|||
return invocationExpr;
|
||||
}
|
||||
|
||||
BfInitializerExpression * BfReducer::TryCreateInitializerExpression(BfExpression* target)
|
||||
{
|
||||
auto block = BfNodeDynCast<BfBlock>(mVisitorPos.GetNext());
|
||||
if (block == NULL)
|
||||
return NULL;
|
||||
|
||||
mVisitorPos.MoveNext();
|
||||
|
||||
auto initializerExpr = mAlloc->Alloc<BfInitializerExpression>();
|
||||
ReplaceNode(target, initializerExpr);
|
||||
initializerExpr->mTarget = target;
|
||||
MEMBER_SET(initializerExpr, mOpenBrace, block->mOpenBrace);
|
||||
|
||||
SetAndRestoreValue<BfVisitorPos> prevVisitorPos(mVisitorPos, BfVisitorPos(block));
|
||||
|
||||
bool isDone = !mVisitorPos.MoveNext();
|
||||
|
||||
BfDeferredAstSizedArray<BfExpression*> values(initializerExpr->mValues, mAlloc);
|
||||
BfDeferredAstSizedArray<BfTokenNode*> commas(initializerExpr->mCommas, mAlloc);
|
||||
|
||||
BfAstNode* nextNode = NULL;
|
||||
while (!isDone)
|
||||
{
|
||||
BfAstNode* node = mVisitorPos.GetCurrent();
|
||||
|
||||
auto expr = CreateExpression(node);
|
||||
isDone = !mVisitorPos.MoveNext();
|
||||
if (expr != NULL)
|
||||
values.Add(expr);
|
||||
|
||||
if (!isDone)
|
||||
{
|
||||
bool foundComma = false;
|
||||
|
||||
node = mVisitorPos.GetCurrent();
|
||||
if (auto tokenNode = BfNodeDynCast<BfTokenNode>(node))
|
||||
{
|
||||
if (tokenNode->mToken == BfToken_Comma)
|
||||
{
|
||||
foundComma = true;
|
||||
commas.Add(tokenNode);
|
||||
mVisitorPos.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mVisitorPos.Trim();
|
||||
|
||||
MEMBER_SET(initializerExpr, mCloseBrace, block->mCloseBrace);
|
||||
|
||||
return initializerExpr;
|
||||
}
|
||||
|
||||
BfDelegateBindExpression* BfReducer::CreateDelegateBindExpression(BfAstNode* allocNode)
|
||||
{
|
||||
auto delegateBindExpr = mAlloc->Alloc<BfDelegateBindExpression>();
|
||||
|
@ -7402,7 +7462,7 @@ BfObjectCreateExpression* BfReducer::CreateObjectCreateExpression(BfAstNode* all
|
|||
if (tokenNode == NULL)
|
||||
return objectCreateExpr;
|
||||
MEMBER_SET(objectCreateExpr, mCloseToken, tokenNode);
|
||||
|
||||
|
||||
return objectCreateExpr;
|
||||
}
|
||||
|
||||
|
@ -8606,15 +8666,16 @@ BfTokenNode* BfReducer::ParseMethodParams(BfAstNode* node, SizedArrayImpl<BfPara
|
|||
|
||||
if (typeRef == NULL)
|
||||
{
|
||||
auto typeIdentifierNode = ExpectIdentifierAfter(nameAfterNode, "parameter type");
|
||||
if (typeIdentifierNode == NULL)
|
||||
auto nextNode = mVisitorPos.GetNext();
|
||||
if (nextNode == NULL)
|
||||
{
|
||||
mVisitorPos.mReadPos = paramStartReadPos;
|
||||
FailAfter("Type expected", nameAfterNode);
|
||||
break;
|
||||
}
|
||||
|
||||
mVisitorPos.MoveNext();
|
||||
|
||||
typeRef = CreateTypeRef(typeIdentifierNode);
|
||||
typeRef = CreateTypeRef(nextNode);
|
||||
if (typeRef == NULL)
|
||||
{
|
||||
mVisitorPos.mReadPos = paramStartReadPos;
|
||||
|
|
|
@ -199,6 +199,7 @@ public:
|
|||
BfObjectCreateExpression* CreateObjectCreateExpression(BfAstNode* allocNode);
|
||||
BfScopedInvocationTarget* CreateScopedInvocationTarget(BfAstNode*& targetRef, BfTokenNode* colonToken);
|
||||
BfInvocationExpression* CreateInvocationExpression(BfAstNode* target, CreateExprFlags createExprFlags = CreateExprFlags_None);
|
||||
BfInitializerExpression* TryCreateInitializerExpression(BfExpression* target);
|
||||
BfExpression* CreateIndexerExpression(BfExpression* target);
|
||||
BfMemberReferenceExpression* CreateMemberReferenceExpression(BfAstNode* target);
|
||||
BfTupleExpression* CreateTupleExpression(BfTokenNode* newNode, BfExpression* innerExpr = NULL);
|
||||
|
|
|
@ -3091,8 +3091,7 @@ void BfModule::VisitEmbeddedStatement(BfAstNode* stmt, BfExprEvaluator* exprEval
|
|||
if (mCurMethodState != NULL)
|
||||
{
|
||||
bool isIgnore = mBfIRBuilder->mIgnoreWrites;
|
||||
|
||||
SetAndRestoreValue<bool> prevEmbedded(mCurMethodState->mIsEmbedded, block == NULL);
|
||||
|
||||
mCurMethodState->mInHeadScope = false;
|
||||
|
||||
BfScopeData scopeData;
|
||||
|
@ -3122,7 +3121,14 @@ void BfModule::VisitEmbeddedStatement(BfAstNode* stmt, BfExprEvaluator* exprEval
|
|||
VisitCodeBlock(block);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (auto varDecl = BfNodeDynCast<BfVariableDeclaration>(stmt))
|
||||
{
|
||||
Fail("Variable declarations must be wrapped in a block statement", varDecl);
|
||||
}
|
||||
|
||||
VisitChild(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
if ((block != NULL) && (closeBrace != NULL))
|
||||
|
@ -3720,9 +3726,6 @@ void BfModule::Visit(BfVariableDeclaration* varDecl)
|
|||
|
||||
UpdateSrcPos(varDecl);
|
||||
|
||||
if (mCurMethodState->mIsEmbedded)
|
||||
Fail("Variable declarations must be wrapped in a block statement", varDecl);
|
||||
|
||||
BfTupleExpression* tupleVariableDeclaration = BfNodeDynCast<BfTupleExpression>(varDecl->mNameNode);
|
||||
if (tupleVariableDeclaration != NULL)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue