Jump to page: 1 2
Thread overview
compile time output
Jan 20, 2009
Trass3r
Jan 20, 2009
Bill Baxter
Jan 20, 2009
Trass3r
Jan 20, 2009
Ary Borenszweig
Jan 20, 2009
BCS
Jan 20, 2009
Trass3r
Jan 20, 2009
BCS
Jan 20, 2009
Trass3r
Jan 20, 2009
BCS
Jan 20, 2009
Trass3r
Jan 20, 2009
Trass3r
Jan 20, 2009
Bill Baxter
Jan 20, 2009
Sergey Gromov
Jan 20, 2009
Trass3r
Jan 20, 2009
BCS
Jan 20, 2009
Sergey Gromov
Jan 21, 2009
Christopher Wright
January 20, 2009
Is there any way to output information at compile time other than pragma(msg?
pragma is driving me crazy, the following doesn't work:

auto members = __traits(allMembers, typeof(this));
foreach(m; members)
{
	pragma(msg, m);
}

-> Error: string expected for message, not 'm'

Though the docs clearly state:
allMembers: "An array of string literals is returned"
Also checked that, m is of type invariant(char)[].


Any ideas? -.-
January 20, 2009
On Wed, Jan 21, 2009 at 12:36 AM, Trass3r <mrmocool@gmx.de> wrote:
> Is there any way to output information at compile time other than
> pragma(msg?
> pragma is driving me crazy, the following doesn't work:
>
> auto members = __traits(allMembers, typeof(this));
> foreach(m; members)
> {
>        pragma(msg, m);
> }
>
> -> Error: string expected for message, not 'm'
>
> Though the docs clearly state:
> allMembers: "An array of string literals is returned"
> Also checked that, m is of type invariant(char)[].
>
>
> Any ideas? -.-

try indexing explicitly or using ref:

foreach(i,m; members)
{
    pragma(msg, members[i]);
}

foreach(ref m; members)
{
    pragma(msg, m);
}

Latter one may not be useful.  I can't recall.
--bb
January 20, 2009
Trass3r wrote:
> Is there any way to output information at compile time other than pragma(msg?
> pragma is driving me crazy, the following doesn't work:
> 
> auto members = __traits(allMembers, typeof(this));

Kind of offtopic, but I tried this (with typeof(Foo) and Foo is defined in the same module) in Descent and I get a NullPointerException. :-(

I thought the port to Java was wrong so I compared to dmd's code.

In traits.c I can see:

---
else if (ident == Id::allMembers || ident == Id::derivedMembers)
    {
	if (dim != 1)
	    goto Ldimerror;
	Object *o = (Object *)args->data[0];
	Dsymbol *s = getDsymbol(o);
---

and in template.c getDsymbol says:

---
Dsymbol *getDsymbol(Object *oarg)
{
    Dsymbol *sa;
    Expression *ea = isExpression(oarg);
    if (ea)
    {
        // (snip)
    }
    else
    {   // Try to convert Type to symbol
	Type *ta = isType(oarg);
	if (ta)
	    sa = ta->toDsymbol(NULL);
	else
	    sa = isDsymbol(oarg);	// if already a symbol
    }
    return sa;
}
---

and of course oarg is a type, it's a TypeTypeof, so TypeTypeof::toDsymbol is invoked. Note that in this point sc is NULL.

---
Dsymbol *TypeTypeof::toDsymbol(Scope *sc)
{
    Type *t;

    t = semantic(0, sc);
    if (t == this)
	return NULL;
    return t->toDsymbol(sc);
}
---

and finally...

---
Type *TypeTypeof::semantic(Loc loc, Scope *sc)
{   Expression *e;
    Type *t;

	sc->intypeof++; // sc is NULL!!
	exp = exp->semantic(sc);
	sc->intypeof--;

   // (snip)
}
---

and that's why I get a NPE. But compiling with dmd works fine. Unfortunately I don't have with me the necessary stuff to debug dmd... does anyone know what's going on?

Thanks,
Ary
January 20, 2009
Bill Baxter schrieb:
> try indexing explicitly or using ref:
> 
> foreach(i,m; members)
> {
>     pragma(msg, members[i]);
> }
> 
> foreach(ref m; members)
> {
>     pragma(msg, m);
> }
> 
> Latter one may not be useful.  I can't recall.

Neither one works for me :(
January 20, 2009
Reply to Trass3r,

> Is there any way to output information at compile time other than
> pragma(msg?
> pragma is driving me crazy, the following doesn't work:
> auto members = __traits(allMembers, typeof(this));
> foreach(m; members)
> {
> pragma(msg, m);
> }
> -> Error: string expected for message, not 'm'
> 
> Though the docs clearly state:
> allMembers: "An array of string literals is returned"
> Also checked that, m is of type invariant(char)[].
> Any ideas? -.-
> 

I don't do 2.0 but it looks to me like your mixing runtime and compile time stuff. A foreach over an array is a runtime foreach.


January 20, 2009
BCS schrieb:
> I don't do 2.0 but it looks to me like your mixing runtime and compile time stuff. A foreach over an array is a runtime foreach.
> 

Do you know how I could make it run at compile-time?
January 20, 2009
Reply to Trass3r,

> BCS schrieb:
> 
>> I don't do 2.0 but it looks to me like your mixing runtime and
>> compile time stuff. A foreach over an array is a runtime foreach.
>> 
> Do you know how I could make it run at compile-time?
> 

template Tpl(T...)
{
 alias T Tpl;
}

template Range(int l, int u)
{
 static if(l<u)
   alias Tpl!(l, Range!(l+1,u)) Range;
 else
   alias Tpl!(l) Range;
}

const char[][] set = ["Hello"[], "world" ];

void main()
{
 foreach(str; Range!(0,set.length-1)) // compile time foreach over the numbers from 0 to set.length-1
   pragma(msg, set[str]);
}


January 20, 2009
Tue, 20 Jan 2009 16:36:09 +0100, Trass3r wrote:

> Is there any way to output information at compile time other than
> pragma(msg?
> pragma is driving me crazy, the following doesn't work:
> 
> auto members = __traits(allMembers, typeof(this));
> foreach(m; members)
> {
> 	pragma(msg, m);
> }
> 
> -> Error: string expected for message, not 'm'
> 
> Though the docs clearly state:
> allMembers: "An array of string literals is returned"
> Also checked that, m is of type invariant(char)[].
> 
> Any ideas? -.-

Weird.  The following code does not compile:

class Cls
{
  int bar;
  char[] baz;
}
string foo()
{
  auto members = __traits(allMembers, Cls);
  return "";
}
pragma(msg, foo());

>dmd -c test.d
test.d(11): Error: cannot evaluate foo() at compile time
test.d(11): pragma msg string expected for message, not 'foo()'

Comment out the traits and it compiles.  Traits are supposed to be compile-time.  How's that possible for them to prevent compile-time evaluation?
January 20, 2009
BCS schrieb:
> template Tpl(T...)
> {
>  alias T Tpl;
> }
> 
> template Range(int l, int u)
> {
>  static if(l<u)
>    alias Tpl!(l, Range!(l+1,u)) Range;
>  else
>    alias Tpl!(l) Range;
> }
> 
> const char[][] set = ["Hello"[], "world" ];
> 
> void main()
> {
>  foreach(str; Range!(0,set.length-1)) // compile time foreach over the numbers from 0 to set.length-1
>    pragma(msg, set[str]);
> }
> 

Still doesn't work for this code (used with a mixin):
template classMixin()
{
static this()
{
	auto members = __traits(allMembers, typeof(this));
	foreach(m; Range!(0, members.length-1))
	{
		pragma(msg, members[m]);
		static if (is (typeof(__traits(getMember, this, members[m])) F == function))
		{
			pragma(msg, "function");
		}
	}
}
}

-> Error: string expected for message, not 'members[0u]'

Leaving out the first pragma, compiles but doesn't output anything.
Replacing members[m] with a valid method name, such as "__ctor" it outputs function correctly :(
January 20, 2009
Sergey Gromov schrieb:
 > class Cls
> {
>   int bar;
>   char[] baz;
> }
> string foo()
> {
>   auto members = __traits(allMembers, Cls);
>   return "";
> }
> pragma(msg, foo());
> 
>> dmd -c test.d
> test.d(11): Error: cannot evaluate foo() at compile time
> test.d(11): pragma msg string expected for message, not 'foo()'
> 
> Comment out the traits and it compiles.  Traits are supposed to be
> compile-time.  How's that possible for them to prevent compile-time
> evaluation?

Seeing this really simple example crashing makes me think that this has to be a bug.
« First   ‹ Prev
1 2