www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - typeinfo comparison - 'is' or '=='?

reply Gregor Richards <Richards codu.org> writes:
Partially to learn the DMD frontend and partially because it's cool and 
useful, I've been writing a D compiler targetting C ( 
http://www.dsource.org/projects/tdc/ )

I've ran into an issue with typeinfo objects.  It seems that for any 
type, there should be a static object containing the type info, and 
furthermore there should only be one in the output program.  My 
reasoning behind this is that several .d files in phobos/ use 'is' to 
compare typeinfo's, which implies to me that the 'typeid' operator will 
return a reference to a statically declared type (since otherwise it 
could return a different value and you'd get false negatives)

Unfortunately, this poses a bit of a problem when using .c as the 
output.  Anything I try to declare statically would end up declared into 
every .o file.  That's a (slightly) solvable problem with some linkers, 
but probably unsolvable with .so-linkage or .dll-linkage, unless I'm 
misunderstanding the situation.

So my question is: Am I wrong, or is the code in phobos/std/boxer.d and 
phobos/std/stream.d wrong?

  - Gregor Richards
May 06 2006
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Gregor Richards wrote:

 I've ran into an issue with typeinfo objects.  It seems that for any 
 type, there should be a static object containing the type info, and 
 furthermore there should only be one in the output program.  My 
 reasoning behind this is that several .d files in phobos/ use 'is' to 
 compare typeinfo's, which implies to me that the 'typeid' operator will 
 return a reference to a statically declared type (since otherwise it 
 could return a different value and you'd get false negatives)
This is a problem for GDC too, when running on old compilers (GCC 3.3) that doesn't support once-only linkage and therefore duplicates objects. http://www.digitalmars.com/d/archives/D/gnu/1594.html
 So my question is: Am I wrong, or is the code in phobos/std/boxer.d and 
 phobos/std/stream.d wrong?
Well, not "wrong" - it is just not portable ? But changing it from 'is' over to use '==' instead would make it work in more places than it does. So I think that Phobos should be changed. --anders
May 07 2006
next sibling parent reply Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Anders F Björklund wrote:
 So my question is: Am I wrong, or is the code in phobos/std/boxer.d 
 and phobos/std/stream.d wrong?
Well, not "wrong" - it is just not portable ? But changing it from 'is' over to use '==' instead would make it work in more places than it does. So I think that Phobos should be changed. --anders
Yet, that would make the comparison slower, just because of a compiler technology limitation, no? That doesn't sound good to me. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 07 2006
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Bruno Medeiros wrote:

 Yet, that would make the comparison slower, just because of a compiler 
 technology limitation, no? That doesn't sound good to me.
Sure, depends on whether you want it working on other platforms or not. Since DMD and Phobos only supports Windows and linux, I guess it is OK for them to do it in a way that is the fastest on those two platforms. But it still has to be patched, in the GDC / GPhobos versions of them ? --anders
May 07 2006
prev sibling next sibling parent reply Burton Radons <burton-radons smocky.com> writes:
Anders F Björklund wrote:
 Gregor Richards wrote:
 
 So my question is: Am I wrong, or is the code in phobos/std/boxer.d 
 and phobos/std/stream.d wrong?
Well, not "wrong" - it is just not portable ? But changing it from 'is' over to use '==' instead would make it work in more places than it does.
The opCmp method in almost all of the TypeInfo are completely inadequate for their task; for example, according to DMD "typeid (void *) == typeid (char *)"; any chained type (pointers, arrays, delegates, functions) has this problem, which would allow for data corruption in unboxing. That's why I used "is" instead: it's better to reject an unboxing than to do it wrong.
 So I think that Phobos should be changed.
std.boxer should be changed but only after TypeInfo has been fixed. I sent Walter a partial fix* for it (which was faster too) shortly after writing std.boxer; I don't know why he didn't incorporate it. Walter? * Delegates and functions cannot be truly fixed until the TypeInfo includes parameter information.
May 07 2006
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Burton Radons wrote:

 Well, not "wrong" - it is just not portable ? But changing it from 'is'
 over to use '==' instead would make it work in more places than it does.
The opCmp method in almost all of the TypeInfo are completely inadequate for their task; for example, according to DMD "typeid (void *) == typeid (char *)"; any chained type (pointers, arrays, delegates, functions) has this problem, which would allow for data corruption in unboxing. That's why I used "is" instead: it's better to reject an unboxing than to do it wrong.
That is a much more valid reason to use "is" than the performance one...
 So I think that Phobos should be changed.
std.boxer should be changed but only after TypeInfo has been fixed. I sent Walter a partial fix* for it (which was faster too) shortly after writing std.boxer; I don't know why he didn't incorporate it. Walter? * Delegates and functions cannot be truly fixed until the TypeInfo includes parameter information.
Yes, if the typeid for pointer types could be fixed, that would be good. (as I know that was one of the major problems for the "readf" methods) --anders
May 07 2006
prev sibling parent John Reimer <terminal.node gmail.com> writes:
What's this?  Burton Radons appearing out of the blue again?

Good to see you around, Burton!

:D

-JJR
May 08 2006
prev sibling parent reply Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Anders F Björklund wrote:
 Gregor Richards wrote:
 
 I've ran into an issue with typeinfo objects.  It seems that for any 
 type, there should be a static object containing the type info, and 
 furthermore there should only be one in the output program.  My 
 reasoning behind this is that several .d files in phobos/ use 'is' to 
 compare typeinfo's, which implies to me that the 'typeid' operator 
 will return a reference to a statically declared type (since otherwise 
 it could return a different value and you'd get false negatives)
This is a problem for GDC too, when running on old compilers (GCC 3.3) that doesn't support once-only linkage and therefore duplicates objects. http://www.digitalmars.com/d/archives/D/gnu/1594.html
 So my question is: Am I wrong, or is the code in phobos/std/boxer.d 
 and phobos/std/stream.d wrong?
Well, not "wrong" - it is just not portable ? But changing it from 'is' over to use '==' instead would make it work in more places than it does. So I think that Phobos should be changed. --anders
Whoa there, I just realized: This is a global language issue, it is not just a matter of Phobos. It has to be defined globally (by the language itself) what is the correct way to compare two types. I mean, what if in a D program one uses == / is with a TypeInfo? Wouldn't GDC support == ? -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 07 2006
parent Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
Bruno Medeiros wrote:
 Anders F Björklund wrote:
 Gregor Richards wrote:

 I've ran into an issue with typeinfo objects.  It seems that for any 
 type, there should be a static object containing the type info, and 
 furthermore there should only be one in the output program.  My 
 reasoning behind this is that several .d files in phobos/ use 'is' to 
 compare typeinfo's, which implies to me that the 'typeid' operator 
 will return a reference to a statically declared type (since 
 otherwise it could return a different value and you'd get false 
 negatives)
This is a problem for GDC too, when running on old compilers (GCC 3.3) that doesn't support once-only linkage and therefore duplicates objects. http://www.digitalmars.com/d/archives/D/gnu/1594.html
 So my question is: Am I wrong, or is the code in phobos/std/boxer.d 
 and phobos/std/stream.d wrong?
Well, not "wrong" - it is just not portable ? But changing it from 'is' over to use '==' instead would make it work in more places than it does. So I think that Phobos should be changed. --anders
Whoa there, I just realized: This is a global language issue, it is not just a matter of Phobos. It has to be defined globally (by the language itself) what is the correct way to compare two types. I mean, what if in a D program one uses == / is with a TypeInfo? Wouldn't GDC support == ?
Hum, actually, after some tests, and a very brief look in Phobos, it seems there are two kinds of comparisons available, each one corresponding to each operator. "is" tests for exact type equality, and "==" tests for meta-type equality (aka archetype, type type, super type?) Example: writefln(typeid(FooBar) is typeid(Foo)); //false writefln(typeid(FooBar) == typeid(int*)); //false writefln(typeid(FooBar) == typeid(Foo)); //true,same metatype(class) writefln(typeid(char*) == typeid(int*)); //true,same metatype(pointer) Then there's also the equals method of TypeInfo, and that I didn't quite understand what it does (or when). It seems to me redundant versus opEquals. In any case, it seems both operands must support their respective usage. We could use some official documentation on this too, as there's quite no mention of it anywhere, I think. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 08 2006
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
Gregor Richards wrote:
 Partially to learn the DMD frontend and partially because it's cool and 
 useful, I've been writing a D compiler targetting C ( 
 http://www.dsource.org/projects/tdc/ )
 
 I've ran into an issue with typeinfo objects.  It seems that for any 
 type, there should be a static object containing the type info, and 
 furthermore there should only be one in the output program.
That's right.
 My 
 reasoning behind this is that several .d files in phobos/ use 'is' to 
 compare typeinfo's, which implies to me that the 'typeid' operator will 
 return a reference to a statically declared type (since otherwise it 
 could return a different value and you'd get false negatives)
 
 Unfortunately, this poses a bit of a problem when using .c as the 
 output.  Anything I try to declare statically would end up declared into 
 every .o file.  That's a (slightly) solvable problem with some linkers, 
 but probably unsolvable with .so-linkage or .dll-linkage, unless I'm 
 misunderstanding the situation.
Why not create a source file with all the declarations and a header file, included by everything, that has an "extern" declaration for each one? Sean
May 07 2006
parent Gregor Richards <Richards codu.org> writes:
Sean Kelly wrote:
 Why not create a source file with all the declarations and a header 
 file, included by everything, that has an "extern" declaration for each 
 one?
There are infinite possibilities. void *, void **, void ***, void ****, void *[]*[], char (*)()[], etc, etc. - Gregor Richards
May 07 2006