www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dump mixins (preprocessed/generated) code

reply "Domingo Alvarez Duarte" <mingodad gmail.com> writes:
Hello !

I'm trying to make MiniD/Croc work with dmd2 and I'm having a 
problem with this code and I want to know how to dump the 
generated code to understand what's happening.

How that can be done ?

debugmixin.d-mixin-15(15): Error: no identifier for declarator 
char[]

-----debugmixin.d
/// Eheheh, I has a __FUNCTION__.
const char[] FuncNameMix = "static if(!is(typeof(__FUNCTION__))) 
{ struct __FUNCTION {} const char[] __FUNCTION__ = 
_getJustName(__FUNCTION.mangleof); }";

template apiCheckNumParams(const char[] numParams, const char[] t 
= "t")
{
	const char[] apiCheckNumParams =
	"debug assert(" ~ t ~ ".stackIndex > " ~ t ~ ".stackBase, 
(printStack(" ~ t ~ "), printCallStack(" ~ t ~ "), \"fail.\"));" ~
	FuncNameMix ~
	"if((stackSize(" ~ t ~ ") - 1) < " ~ numParams ~ ")"
		"throwStdException(" ~ t ~ ", \"ApiError\", __FUNCTION__ ~ \" - 
not enough parameters (expected {}, only have {} stack slots)\", 
" ~ numParams ~ ", stackSize(" ~ t ~ ") - 1);";
}

void setHookFunc(void* t, ubyte mask, uint hookDelay)
{
	mixin(apiCheckNumParams!("1"));
}

void main()
{
}
-----
Jul 18 2014
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 18/07/2014 11:39 p.m., Domingo Alvarez Duarte wrote:
 Hello !

 I'm trying to make MiniD/Croc work with dmd2 and I'm having a problem
 with this code and I want to know how to dump the generated code to
 understand what's happening.

 How that can be done ?

 debugmixin.d-mixin-15(15): Error: no identifier for declarator char[]

 -----debugmixin.d
 /// Eheheh, I has a __FUNCTION__.
 const char[] FuncNameMix = "static if(!is(typeof(__FUNCTION__))) {
 struct __FUNCTION {} const char[] __FUNCTION__ =
 _getJustName(__FUNCTION.mangleof); }";

 template apiCheckNumParams(const char[] numParams, const char[] t = "t")
 {
      const char[] apiCheckNumParams =
      "debug assert(" ~ t ~ ".stackIndex > " ~ t ~ ".stackBase,
 (printStack(" ~ t ~ "), printCallStack(" ~ t ~ "), \"fail.\"));" ~
      FuncNameMix ~
      "if((stackSize(" ~ t ~ ") - 1) < " ~ numParams ~ ")"
          "throwStdException(" ~ t ~ ", \"ApiError\", __FUNCTION__ ~ \" -
 not enough parameters (expected {}, only have {} stack slots)\", " ~
 numParams ~ ", stackSize(" ~ t ~ ") - 1);";
 }

 void setHookFunc(void* t, ubyte mask, uint hookDelay)
 {
      mixin(apiCheckNumParams!("1"));
 }

 void main()
 {
 }
 -----
You can use pragma(msg, ""); to output text at compile time if the value is known during compilation (aka can go into a string mixin). e.g. pragma(msg, apiCheckNumParams!("1"));
Jul 18 2014
parent reply "Domingo Alvarez Duarte" <mingodad gmail.com> writes:
Thank you so much !

That did exactly what I was looking for, but now why I'm getting 
that error message ?

Any help is welcome ! Cheers !

The generated code seems to fine:
----
debug assert(t.stackIndex > t.stackBase, (printStack(t), 
printCallStack(t), "fail."));
static if(!is(typeof(__FUNCTION__))) {
	struct __FUNCTION {} const char[] __FUNCTION__ = 
_getJustName(__FUNCTION.mangleof);
}
if((stackSize(t) - 1) < 1)
	throwStdException(t, "ApiError", __FUNCTION__ ~ " - not enough 
parameters (expected {}, only have {} stack slots)", 1, 
stackSize(t) - 1);

----
Jul 18 2014
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 19/07/2014 12:09 a.m., Domingo Alvarez Duarte wrote:
 Thank you so much !

 That did exactly what I was looking for, but now why I'm getting that
 error message ?

 Any help is welcome ! Cheers !

 The generated code seems to fine:
 ----
 debug assert(t.stackIndex > t.stackBase, (printStack(t),
 printCallStack(t), "fail."));
 static if(!is(typeof(__FUNCTION__))) {
      struct __FUNCTION {} const char[] __FUNCTION__ =
 _getJustName(__FUNCTION.mangleof);
 }
 if((stackSize(t) - 1) < 1)
      throwStdException(t, "ApiError", __FUNCTION__ ~ " - not enough
 parameters (expected {}, only have {} stack slots)", 1, stackSize(t) - 1);

 ----
I can't really tell at this point. Try removing the code one line at a time (from the generation). You'll eventually work out what is killing it.
Jul 18 2014
parent reply "Domingo Alvarez Duarte" <mingodad gmail.com> writes:
Thanks for all you answers, I think I found the problem.

The constant __FUNCTION__ was not a compiler reserved word at the 
time MiniD/Croc was created and it was introduced later on dmd2, 
it was clashing with it.

I renamed it to __MFUNCTUION__ and it compiles now, I'm getting 
more errors of other kinds and trying to fix then.

I plan to fork https://github.com/JarrettBillingsley/Croc and 
make it available.

Cheers !
Jul 18 2014
next sibling parent "Dicebot" <public dicebot.lv> writes:
On Friday, 18 July 2014 at 14:25:57 UTC, Domingo Alvarez Duarte 
wrote:
 Thanks for all you answers, I think I found the problem.

 The constant __FUNCTION__ was not a compiler reserved word at 
 the time MiniD/Croc was created and it was introduced later on 
 dmd2, it was clashing with it.

 I renamed it to __MFUNCTUION__ and it compiles now, I'm getting 
 more errors of other kinds and trying to fix then.

 I plan to fork https://github.com/JarrettBillingsley/Croc and 
 make it available.

 Cheers !
In general all identifiers starting with __ (double underscore) are considered reserved by compiler / standard library and should never be used anywhere else.
Jul 18 2014
prev sibling parent "Domingo Alvarez Duarte" <mingodad gmail.com> writes:
It seems to be a bug somewhere because tired of getting errors 
with dmd 2.065 I switched to dmd 2.066 trunk and the error 
vanished.
Jul 18 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
While `static if` false branch does not get compiled or 
semantically evaluated it still should have a valid syntax. 
__FUNCTION__ gets replaced with a fully qualified name of a 
function which has dot inside - illegal identifier for a 
variable. This causes parser to freak out.
Jul 18 2014