digitalmars.D - assert(__ctfe) and betterC
- Steven Schveighoffer (13/13) Jan 27 2021 CTFE is the primary vehicle used to generate dynamic mixin code based on...
- Tim (13/28) Jan 27 2021 One way I have sometimes used is an alias to a function literal:
- Steven Schveighoffer (4/23) Jan 27 2021 Well, that works great! What a nice... well, I have no idea why this
- Tim (7/10) Jan 27 2021 As far as I understand, the function literal can't be compiled at
- Petar Kirov [ZombineDev] (15/40) Jan 27 2021 `enum` also works:
- Walter Bright (5/8) Jan 27 2021 In oper.d there are some lambdas which generate static information, and ...
CTFE is the primary vehicle used to generate dynamic mixin code based on template and regular parameters. Doing it with templates *only* is possible, but so so painful. But, the general mechanisms used in CTFE to generate string data (i.e. string concatenation) are not available for betterC runtime code. And CTFE must be compiled to as runtime code before it is interpreted. I remember something about assert(__ctfe) in your function telling the compiler to not emit the code to the object file. Did that get in? Is there a way we can use that to turn off betterC too? Otherwise, you get errors when building your CTFE function because "TypeInfo cannot be used with -betterC" Is there another way I can do this? -Steve
Jan 27 2021
On Wednesday, 27 January 2021 at 20:59:24 UTC, Steven Schveighoffer wrote:CTFE is the primary vehicle used to generate dynamic mixin code based on template and regular parameters. Doing it with templates *only* is possible, but so so painful. But, the general mechanisms used in CTFE to generate string data (i.e. string concatenation) are not available for betterC runtime code. And CTFE must be compiled to as runtime code before it is interpreted. I remember something about assert(__ctfe) in your function telling the compiler to not emit the code to the object file. Did that get in? Is there a way we can use that to turn off betterC too? Otherwise, you get errors when building your CTFE function because "TypeInfo cannot be used with -betterC" Is there another way I can do this? -SteveOne way I have sometimes used is an alias to a function literal: alias generateVar = function(string name) { return "int " ~ name ~ ";"; }; mixin(generateVar("i")); extern(C) int main() { i = 0; return i; } This works with betterC.
Jan 27 2021
On 1/27/21 4:05 PM, Tim wrote:On Wednesday, 27 January 2021 at 20:59:24 UTC, Steven Schveighoffer wrote:Well, that works great! What a nice... well, I have no idea why this works. But thank you! -SteveIs there another way I can do this?One way I have sometimes used is an alias to a function literal: alias generateVar = function(string name) { return "int " ~ name ~ ";"; }; mixin(generateVar("i")); extern(C) int main() { i = 0; return i; } This works with betterC.
Jan 27 2021
On Wednesday, 27 January 2021 at 21:27:45 UTC, Steven Schveighoffer wrote:Well, that works great! What a nice... well, I have no idea why this works. But thank you! -SteveAs far as I understand, the function literal can't be compiled at the definition, because attributes and parameter types may need to be inferred at the call site. Since the only call site is inside a mixin, it is never compiled for runtime and no checks for betterC need to be performed.
Jan 27 2021
On Wednesday, 27 January 2021 at 21:27:45 UTC, Steven Schveighoffer wrote:On 1/27/21 4:05 PM, Tim wrote:`enum` also works: enum generateVar = (string name) => "int " ~ name ~ ";"; The key difference between these ways of defining a function and the regular way, is that with them it's guaranteed that the compiler won't codegen them since simply it only does this for regular functions. assert(__ctfe) could be made to work, but it's wrong way to go about things. Instead the compiler should only code-gen what is actually necessary. In the posts below I have sketched out how a scheme for fully on-demand codegen might look like, based on `export` inference: https://forum.dlang.org/post/xbllqrpvflazfpowizwj forum.dlang.org https://forum.dlang.org/post/nvvgrdlucajshjngdjlj forum.dlang.orgOn Wednesday, 27 January 2021 at 20:59:24 UTC, Steven Schveighoffer wrote:Well, that works great! What a nice... well, I have no idea why this works. But thank you! -SteveIs there another way I can do this?One way I have sometimes used is an alias to a function literal: alias generateVar = function(string name) { return "int " ~ name ~ ";"; }; mixin(generateVar("i")); extern(C) int main() { i = 0; return i; } This works with betterC.
Jan 27 2021
On 1/27/2021 12:59 PM, Steven Schveighoffer wrote:I remember something about assert(__ctfe) in your function telling the compiler to not emit the code to the object file. Did that get in? Is there a way we can use that to turn off betterC too?In oper.d there are some lambdas which generate static information, and the lambdas are not emitted to the object file. https://github.com/dlang/dmd/blob/master/src/dmd/backend/oper.d#L387 This capability is what eliminated the need for the optabgen.d file.
Jan 27 2021