Skip to content

Commit

Permalink
implement pragma(__ctfe [,bool])
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Sep 27, 2023
1 parent 5a15884 commit 331c61e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
16 changes: 14 additions & 2 deletions dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ version (IN_LLVM)
}
return se;
}
void declarations()
void declarations(bool skipCodegen = false)
{
if (!pd.decl)
return;
Expand All @@ -1729,7 +1729,13 @@ version (IN_LLVM)
s.dsymbolSemantic(sc2);
continue;
}

if (pd.ident == Id.ctfe)
{
if (auto fd = s.isFuncDeclaration())
{
fd.skipCodegen = skipCodegen;
}
}
s.dsymbolSemantic(sc2);
if (pd.ident != Id.mangle)
{
Expand Down Expand Up @@ -1893,6 +1899,12 @@ else // !IN_LLVM

return declarations();
}
else if (pd.ident == Id.ctfe)
{
bool skipCodegen;
pragmaCtfeSemanitc(pd.loc, sc, pd.args, skipCodegen);
return declarations(skipCodegen);
}
else if (pd.ident == Id.mangle)
{
if (!pd.args)
Expand Down
53 changes: 53 additions & 0 deletions dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,21 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
return setError();
}
}
else if (ps.ident == Id.ctfe)
{
if (auto fd = sc.func)
{
bool skipCodegen;
if (!pragmaCtfeSemanitc(ps.loc, sc, ps.args, skipCodegen))
return setError();
fd.skipCodegen = skipCodegen;
}
else
{
ps.error("`pragma(__ctfe)` is not inside a function");
return setError();
}
}
else if (!global.params.ignoreUnsupportedPragmas)
{
ps.error("unrecognized `pragma(%s)`", ps.ident.toChars());
Expand Down Expand Up @@ -5033,6 +5048,44 @@ private void debugThrowWalker(Statement s)
s.accept(walker);
}

bool pragmaCtfeSemanitc(Loc loc, Scope* sc, Expressions *args, out bool skipCodegen)
{
if (!args)
{
skipCodegen = true;
return true;
}
if (args.length != 1)
{
.error(loc, "one `bool` argument expected for `__ctfe`");
skipCodegen = true;
return false;
}
Expression e = (*args)[0];
sc = sc.startCTFE();
e = e.expressionSemantic(sc);
e = resolveProperties(sc, e);
sc = sc.endCTFE();

e = e.ctfeInterpret();
if (!e)
{
skipCodegen = true;
return false;
}
(*args)[0] = e;
auto opt = e.toBool();
if (opt.isEmpty())
{
.error(loc, "one `bool` argument expected for `__ctfe`");
skipCodegen = true;
return false;
}
skipCodegen = opt.get();
return true;

}

/***********************************************************
* Evaluate and print a `pragma(msg, args)`
*
Expand Down

0 comments on commit 331c61e

Please sign in to comment.