Skip to content

Commit 6c928c7

Browse files
authored
Merge pull request #2112 from aG0aep6G/ctfe
clarify CTFE spec a bit merged-on-behalf-of: Petar Kirov <ZombineDev@users.noreply.github.com>
2 parents 1d4b41f + c4cbe6a commit 6c928c7

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

spec/function.dd

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,15 +2199,50 @@ void main()
21992199
$(H2 $(LNAME2 interpretation, Compile Time Function Execution (CTFE)))
22002200

22012201
$(P Functions which are both portable and free of global side-effects can be
2202-
executed at compile time. This is useful when constant folding
2203-
algorithms need to include recursion and looping. Compile time function
2204-
execution is subject to the following restrictions:
2202+
executed at compile time. In certain contexts, such compile time execution
2203+
is guaranteed. It is called Compile Time Function Execution (CTFE) then.
2204+
The contexts that trigger CTFE are:)
2205+
2206+
$(UL
2207+
$(LI initialization of a static variable or a
2208+
$(DDSUBLINK spec/enum, manifest_constants, manifest constant))
2209+
$(LI static initializers of struct/class members)
2210+
$(LI dimension of a $(DDSUBLINK spec/arrays, static-arrays, static array))
2211+
$(LI argument for a $(DDSUBLINK spec/template, template_value_parameter,
2212+
template value parameter))
2213+
$(LI $(DDSUBLINK spec/version, staticif, `static if`))
2214+
$(LI $(DDSUBLINK spec/version, staticforeach, `static foreach`))
2215+
$(LI $(DDSUBLINK spec/version, static-assert, `static assert`))
2216+
$(LI $(DDSUBLINK spec/statement, mixin-statement,
2217+
`mixin` statement))
2218+
$(LI $(DDLINK spec/pragma, Pragmas, `pragma` argument))
22052219
)
22062220

2221+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
2222+
---
2223+
enum eval(Args...) = Args[0];
2224+
2225+
int square(int i)
2226+
{
2227+
return i * i;
2228+
}
2229+
2230+
void foo()
2231+
{
2232+
static j = square(3); // CTFE
2233+
writeln(j);
2234+
assert(square(4)); // run time
2235+
writeln(eval!(square(5))); // CTFE
2236+
}
2237+
---
2238+
)
2239+
2240+
$(P CTFE is subject to the following restrictions:)
2241+
22072242
$(OL
22082243
$(LI The function source code must be available to the compiler. Functions
22092244
which exist in the source code only as $(D_KEYWORD extern) declarations
2210-
cannot be executed at compile time.)
2245+
cannot be executed in CTFE.)
22112246

22122247
$(LI Executed expressions may not reference any global or local
22132248
static variables.)
@@ -2217,7 +2252,7 @@ $(H2 $(LNAME2 interpretation, Compile Time Function Execution (CTFE)))
22172252
$(LI Non-portable casts (eg, from $(D int[]) to $(D float[])), including
22182253
casts which depend on endianness, are not permitted.
22192254
Casts between signed and unsigned types are permitted)
2220-
$(LI Reinterpretation of overlapped fields in a Union.)
2255+
$(LI Reinterpretation of overlapped fields in a Union is not permitted.)
22212256
)
22222257

22232258
$(P Pointers are permitted in CTFE, provided they are used safely:)
@@ -2289,47 +2324,14 @@ static assert(countTen(6) == 6); // OK
22892324
static assert(countTen(12) == 12); // invalid, modifies y.
22902325
---
22912326
$(P The $(D __ctfe) boolean pseudo-variable, which evaluates to $(D_KEYWORD true)
2292-
at compile time, but $(D_KEYWORD false) at run time, can be used to provide
2327+
in CTFE, but $(D_KEYWORD false) otherwise, can be used to provide
22932328
an alternative execution path to avoid operations which are forbidden
2294-
at compile time. Every usage of $(D __ctfe) is evaluated before
2329+
in CTFE. Every usage of $(D __ctfe) is evaluated before
22952330
code generation and therefore has no run-time cost, even if no optimizer
22962331
is used.
22972332
)
22982333

2299-
2300-
$(P In order to be executed at compile time, the function
2301-
must appear in a context where it must be so executed, for
2302-
example:)
2303-
2304-
$(UL
2305-
$(LI initialization of a static variable or a manifest constant)
2306-
$(LI dimension of a static array)
2307-
$(LI argument for a template value parameter)
2308-
)
2309-
2310-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
2311-
---
2312-
template eval( A... )
2313-
{
2314-
const typeof(A[0]) eval = A[0];
2315-
}
2316-
2317-
int square(int i)
2318-
{
2319-
return i * i;
2320-
}
2321-
2322-
void foo()
2323-
{
2324-
static j = square(3); // compile time
2325-
writeln(j);
2326-
writeln(square(4)); // run time
2327-
writeln(eval!(square(5))); // compile time
2328-
}
2329-
---
2330-
)
2331-
2332-
$(P Executing functions at compile time can take considerably
2334+
$(P Executing functions via CTFE can take considerably
23332335
longer than executing it at run time.
23342336
If the function goes into an infinite loop, it will hang at
23352337
compile time (rather than hanging at run time).
@@ -2339,7 +2341,7 @@ void foo()
23392341
throw exceptions; instead, they end interpretation immediately.
23402342
)
23412343

2342-
$(P Functions executed at compile time can give different results
2344+
$(P Functions executed via CTFE can give different results
23432345
from run time in the following scenarios:
23442346
)
23452347

@@ -2357,7 +2359,7 @@ void foo()
23572359

23582360
$(H3 $(LNAME2 string-mixins, String Mixins and Compile Time Function Execution))
23592361

2360-
$(P Any functions that execute at compile time must also
2362+
$(P Any functions that execute in CTFE must also
23612363
be executable at run time. The compile time evaluation of
23622364
a function does the equivalent of running the function at
23632365
run time. This means that the semantics of a function cannot
@@ -2372,11 +2374,13 @@ int foo(char[] s)
23722374
const int x = foo("1");
23732375
---
23742376

2375-
$(P is illegal, because the runtime code for foo() cannot be
2377+
$(COMMENT Intentionally not a $(P ...) so that it doesn't get a
2378+
paragraph number, because this continues the paragraph above.)
2379+
is illegal, because the runtime code for `foo` cannot be
23762380
generated. A function template would be the appropriate
2377-
method to implement this sort of thing.)
2381+
method to implement this sort of thing.
23782382

2379-
$(H3 $(LNAME2 nogc-functions, No-GC Functions))
2383+
$(H2 $(LNAME2 nogc-functions, No-GC Functions))
23802384

23812385
$(P No-GC functions are functions marked with the $(D @nogc) attribute.
23822386
Those functions do not allocate memory on the GC heap,

0 commit comments

Comments
 (0)