| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
July 25, 2017 Cast to subclass in the dmd compiler | ||||
|---|---|---|---|---|
| ||||
Hello!
I'm hacking dmd compiler and trying to look on members array just after parse module.
for(uint i = 0; i < members.dim; i++)
{
Dsymbol m = (*members)[i];
// It is good, but further:
Import imp = cast(Import) m;
if (imp !is null)
{
printf("++++ import %s.%s\n", imp.packages.toChars(), imp.id.toChars());
}
// ...
}
For really imports casting doing well. But for not imports it again casts, imp not is null and the compiler crashes.
What I'm doing wrong?
| ||||
July 25, 2017 Re: Cast to subclass in the dmd compiler | ||||
|---|---|---|---|---|
| ||||
Posted in reply to unDEFER | On 7/25/17 4:36 PM, unDEFER wrote:
> Hello!
> I'm hacking dmd compiler and trying to look on members array just after parse module.
>
> for(uint i = 0; i < members.dim; i++)
> {
> Dsymbol m = (*members)[i];
>
> // It is good, but further:
>
> Import imp = cast(Import) m;
> if (imp !is null)
> {
> printf("++++ import %s.%s\n", imp.packages.toChars(), imp.id.toChars());
> }
> // ...
> }
>
> For really imports casting doing well. But for not imports it again casts, imp not is null and the compiler crashes.
> What I'm doing wrong?
I think it's likely that these are actually C++ classes, and probably there is no RTTI, so this is a reinterpret cast. This is a wild guess, but much of the compiler still needs to interface with C++ backend.
I would look elsewhere in the compiler to see how they handle this type of thing. Not sure of dynamic_cast support?
-Steve
| |||
July 25, 2017 Re: Cast to subclass in the dmd compiler | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | I have found the answer in the code. Right code is: Import imp = m.isImport(); if (imp !is null) Thank you. | |||
July 25, 2017 Re: Cast to subclass in the dmd compiler | ||||
|---|---|---|---|---|
| ||||
Posted in reply to unDEFER | On Tuesday, 25 July 2017 at 21:06:25 UTC, unDEFER wrote:
> I have found the answer in the code.
> Right code is:
>
> Import imp = m.isImport();
> if (imp !is null)
>
> Thank you.
grep for kluge in code, you'll find all the places it does its own RTTI.
| |||
July 26, 2017 Re: Cast to subclass in the dmd compiler | ||||
|---|---|---|---|---|
| ||||
Posted in reply to unDEFER | On 2017-07-25 23:06, unDEFER wrote: > I have found the answer in the code. > Right code is: > > Import imp = m.isImport(); > if (imp !is null) > > Thank you. That's the correct solution. For Expression, there's a field called "op" that indicates what kind of expression it is, which can used in combination with a cast. -- /Jacob Carlborg | |||
July 26, 2017 Re: Cast to subclass in the dmd compiler | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 26 July 2017 at 06:50:21 UTC, Jacob Carlborg wrote:
> For Expression, there's a field called "op" that indicates what kind of expression it is, which can used in combination with a cast.
Thank you for hint!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply