Skip to content

Commit

Permalink
Lambda in constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Sep 14, 2023
1 parent 0e6cb55 commit d9106be
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
3 changes: 2 additions & 1 deletion oscar64/Constexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ Expression* ConstexprInterpreter::EvalCall(Expression* exp)
else if (pex->mType == EX_VARIABLE && (pex->mDecValue->mFlags & DTF_CONST))
{
mParams[pos] = Value(pex->mLocation, pex->mDecValue->mBase);
mParams[pos].PutConst(0, pex->mDecValue->mValue->mDecValue);
if (pex->mDecValue->mSize > 0)
mParams[pos].PutConst(0, pex->mDecValue->mValue->mDecValue);
}
else
return exp;
Expand Down
20 changes: 18 additions & 2 deletions oscar64/Declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,9 +1095,14 @@ const Ident* Declaration::MangleIdent(void)
Declaration* dec = mParams;
while (dec)
{
const Ident* id = dec->mBase->MangleIdent();
const Ident* id;
if (dec->mBase)
id = dec->mBase->MangleIdent();
else
id = dec->MangleIdent();
if (id)
mMangleIdent = mMangleIdent->Mangle(id->mString);

dec = dec->mNext;
if (dec)
mMangleIdent = mMangleIdent->Mangle(",");
Expand Down Expand Up @@ -1178,6 +1183,15 @@ bool Declaration::ResolveTemplate(Expression* pexp, Declaration* tdec)
{
Declaration* pdec = tdec->mBase->mParams;

// Insert partially resolved templates
Declaration* ptdec = tdec->mTemplate->mParams;
while (ptdec)
{
if (ptdec->mBase)
mScope->Insert(ptdec->mIdent, ptdec->mBase);
ptdec = ptdec->mNext;
}

while (pexp)
{
Expression* ex = pexp;
Expand All @@ -1201,7 +1215,7 @@ bool Declaration::ResolveTemplate(Expression* pexp, Declaration* tdec)
}

Declaration* ppdec = nullptr;
Declaration* ptdec = tdec->mTemplate->mParams;
ptdec = tdec->mTemplate->mParams;
while (ptdec)
{
Declaration* pdec = mScope->Lookup(ptdec->mIdent);
Expand Down Expand Up @@ -1785,6 +1799,8 @@ bool Declaration::IsSameParams(const Declaration* dec) const
if (ld->mValue != rd->mValue)
return false;
}
else if (ld->mType == DT_TYPE_TEMPLATE || ld->mType == DT_CONST_TEMPLATE)
return false;
else if (!ld->mBase->IsSame(rd->mBase))
return false;
ld = ld->mNext;
Expand Down
45 changes: 38 additions & 7 deletions oscar64/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4836,7 +4836,7 @@ Expression* Parser::ParseLambdaExpression(void)
vdec->mBase = cdec;
vdec->mVarIndex = mLocalIndex++;
vdec->mSize = cdec->mSize;
vdec->mFlags |= DTF_DEFINED;
vdec->mFlags |= DTF_DEFINED | DTF_CONST;
vdec->mIdent = cdec->mIdent;

Expression* vexp = new Expression(mScanner->mLocation, EX_VARIABLE);
Expand Down Expand Up @@ -8365,6 +8365,25 @@ Declaration* Parser::ParseTemplateExpansion(Declaration* tmpld, Declaration* exp

Declaration* ppdec = nullptr;
Declaration* pdec = tmpld->mParams;

// Carry over already specialized parameters
while (pdec && (pdec->mType != DT_TYPE_TEMPLATE && pdec->mType != DT_CONST_TEMPLATE))
{
Declaration* epdec = pdec->Clone();

tdec->mScope->Insert(epdec->mIdent, epdec->mBase);
epdec->mFlags |= DTF_DEFINED;

if (ppdec)
ppdec->mNext = epdec;
else
tdec->mParams = epdec;
ppdec = epdec;

pdec = pdec->mNext;
}

// Now to the new parameters
while (pdec)
{
Declaration* epdec = pdec->Clone();
Expand All @@ -8382,7 +8401,7 @@ Declaration* Parser::ParseTemplateExpansion(Declaration* tmpld, Declaration* exp
epdec->mBase = TheVoidTypeDeclaration;
}
}
else
else if (epdec->mType == DT_CONST_TEMPLATE)
{
if (exp->mType == EX_CONSTANT && (exp->mDecValue->mType == DT_CONST_INTEGER || exp->mDecValue->mType == DT_CONST_TEMPLATE))
epdec->mBase = exp->mDecValue;
Expand All @@ -8400,13 +8419,25 @@ Declaration* Parser::ParseTemplateExpansion(Declaration* tmpld, Declaration* exp
ppdec = epdec;

pdec = pdec->mNext;
if (pdec)
ConsumeToken(TK_COMMA);
if (pdec && !ConsumeTokenIf(TK_COMMA))
break;
}

ConsumeToken(TK_GREATER_THAN);

// Partial template arguments given
if (pdec)
{
if (ppdec)
ppdec->mNext = pdec;
else
tdec->mParams = pdec;
}
}

while (!tmpld->mTokens)
tmpld = tmpld->mNext;

Declaration* etdec = tmpld->mNext;
while (etdec && !etdec->IsSameParams(tdec))
etdec = etdec->mNext;
Expand All @@ -8417,16 +8448,16 @@ Declaration* Parser::ParseTemplateExpansion(Declaration* tmpld, Declaration* exp
else
{
Declaration* epdec = tdec->mParams;
while (epdec && epdec->mBase->mType != DT_TYPE_TEMPLATE && epdec->mBase->mType != DT_CONST_TEMPLATE)
while (epdec && epdec->mBase && epdec->mBase->mType != DT_TYPE_TEMPLATE && epdec->mBase->mType != DT_CONST_TEMPLATE)
epdec = epdec->mNext;

if (epdec)
{
// Partial template specification
Declaration * bdec = new Declaration(mScanner->mLocation, DT_TYPE_STRUCT);
Declaration * bdec = new Declaration(mScanner->mLocation, tmpld->mBase->mType);
tdec->mBase = bdec;
bdec->mTemplate = tdec;
bdec->mBase = tmpld->mBase;
bdec->mBase = tmpld->mBase->mBase;
tdec->mNext = tmpld;
bdec->mIdent = tdec->MangleIdent();

Expand Down

0 comments on commit d9106be

Please sign in to comment.