Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement __ctfeWrite builtin #16250

Merged
merged 5 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions changelog/dmd.ctfeWrite.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Added `__ctfeWrite` to write messages from CTFE

The special function `__ctfeWrite` can now be used to write custom messages to
`stderr` during CTFE. The function is available in `object.d` and accepts any
value implicitly convertible to `const char[]`.

For example:

```d
int greeting()
{
__ctfeWrite("Hello from CTFE. Today is ");
__ctfeWrite(__DATE__);
return 0;
JohanEngelen marked this conversation as resolved.
Show resolved Hide resolved
}

enum forceCTFE = greeting();
```

Compiling this program will generate the following output:

```
Hello from CTFE. Today is <current date>
```
26 changes: 25 additions & 1 deletion compiler/src/dmd/builtin.d
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

// Look for core.math, core.bitop, std.math, and std.math.<package>
const id2 = (md.packages.length == 2) ? md.packages[1] : md.id;
if (id2 != Id.math && id2 != Id.bitop)
if (id2 != Id.math && id2 != Id.bitop && id2 != Id.builtinsModuleName)
return BUILTIN.unimp;

if (md.packages.length != 1 && !(md.packages.length == 2 && id2 == Id.math))
Expand All @@ -108,6 +108,12 @@
return BUILTIN.unimp;
}

if (id1 == Id.core && id2 == Id.builtinsModuleName)
{
if (id3 == Id.ctfeWrite) return BUILTIN.ctfeWrite;
return BUILTIN.unimp;

Check warning on line 114 in compiler/src/dmd/builtin.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/builtin.d#L114

Added line #L114 was not covered by tests
}

// Math
if (id3 == Id.sin) return BUILTIN.sin;
if (id3 == Id.cos) return BUILTIN.cos;
Expand Down Expand Up @@ -167,6 +173,24 @@
return null;
}

Expression eval_ctfeWrite(Loc loc, FuncDeclaration fd, Expressions* arguments)
ibuclaw marked this conversation as resolved.
Show resolved Hide resolved
{
import core.stdc.stdio: fprintf, stderr;
import dmd.expression: CTFEExp;
import dmd.ctfeexpr: resolveSlice;

Expression e = (*arguments)[0];
if (auto se = resolveSlice(e).toStringExp())
{
const slice = se.peekString();
fprintf(stderr, "%.*s", cast(int)slice.length, slice.ptr);
}
else
fprintf(stderr, "%s", e.toChars());

Check warning on line 189 in compiler/src/dmd/builtin.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/builtin.d#L189

Added line #L189 was not covered by tests
JohanEngelen marked this conversation as resolved.
Show resolved Hide resolved

return CTFEExp.voidexp;
}

Expression eval_sin(Loc loc, FuncDeclaration fd, Expressions* arguments)
{
Expression arg0 = (*arguments)[0];
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ enum class BUILTIN : uint8_t
toPrecFloat = 33u,
toPrecDouble = 34u,
toPrecReal = 35u,
ctfeWrite = 36u,
};

enum class Include : uint8_t
Expand Down Expand Up @@ -8628,6 +8629,8 @@ struct Id final
static Identifier* outp;
static Identifier* outpl;
static Identifier* outpw;
static Identifier* builtinsModuleName;
static Identifier* ctfeWrite;
static Identifier* isAbstractClass;
static Identifier* isArithmetic;
static Identifier* isAssociativeArray;
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ enum BUILTIN : ubyte
yl2xp1,
toPrecFloat,
toPrecDouble,
toPrecReal
toPrecReal,
ctfeWrite,
}

/* Tweak all return statements and dtor call for nrvo_var, for correct NRVO.
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ immutable Msgtable[] msgtable =
{ "outp"},
{ "outpl"},
{ "outpw"},
{ "builtinsModuleName", "builtins" },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note the conflict with importC's builtins.d druntime file; that one has a very unfortunate (and confusing) name...

{ "ctfeWrite", "__ctfeWrite" },

// Traits
{ "isAbstractClass" },
Expand Down
47 changes: 47 additions & 0 deletions compiler/test/compilable/test__ctfeWrite.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
TEST_OUTPUT:
---
Hello
World
from
CTFE
Hello[1 .. 3] = el
abcdefghij[2 .. 6] = cdef
---
*/

int greeting(scope const char[][] values) pure nothrow @safe @nogc
{
const string newline = "\n";

foreach (const val; values)
{
__ctfeWrite(val);
__ctfeWrite(newline);
}

// Test slices
const val = values[0];
__ctfeWrite(val[]);
__ctfeWrite("[1 .. 3] = ");
__ctfeWrite(val[1 .. 3]);
__ctfeWrite(newline);

// Test mutable slices
char[10] buffer;
fill(buffer); // Avoid potential shortcuts for literals
__ctfeWrite(buffer[0 .. $]);
__ctfeWrite("[2 .. 6] = ");
__ctfeWrite(buffer[2 .. 6]);
__ctfeWrite(newline);

return 0;
}

void fill(ref char[10] buffer) pure nothrow @safe @nogc
{
foreach (const idx, ref ch; buffer)
ch = cast(char)('a' + idx);
}

enum forceCTFE = greeting(["Hello", "World", "from", "CTFE"]);