Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 07, 2006 typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
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 07, 2006 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gregor Richards | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno Medeiros | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gregor Richards | 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 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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 08, 2006 Re: typeinfo comparison - 'is' or '=='? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | What's this? Burton Radons appearing out of the blue again? Good to see you around, Burton! :D -JJR |
Copyright © 1999-2021 by the D Language Foundation