This is the mail archive of the gdb@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Cast to a struct in expressions




On Sat, 28 Apr 2001, Eli Zaretskii wrote:

> > Date: Fri, 27 Apr 2001 15:30:22 -0400
> > From: Daniel Berlin <dan@cgsoftware.com>
> > >
> > > I'm still missing something, because I don't see how "*(struct foo *)x"
> > > differs from "*(struct {long foo; unsigned bar;} *)x".  Both are valid
> > > C expression syntax, so the parser should be able to parse them both.
> >
> > No, they aren't both *expressions*, only one is.
> > One is a statement/declaration, and the other is an expression.
>
> I'm not a C language expert, but my references seem to disagree with
> you.  The "cast-expression" is defined as having the form
>
>    cast-expression:
>      unary-expression
>      ( type-name ) cast-expression
>
> and "type-name" is syntactically a declaration for a function or an
> object of that type with the object identifier omitted.

Errr, not possible.
You must be reading it wrong.
Try compiling
int main(void)
{
	int x;
	*((struct { long foo; unsigned bar;} *)x)->foo=5;
}

It won't work.
You can't define types in cast expressions, AFAIK.
What would their scope be?


>
> > No. It's done on purpose.  We aren't supposed to be handling statements and
> > declarations, only expressions.
> >
> > Doing more would require making the C parser into a mini-C compiler.
> > You'd have to start injecting types into the symbol table and whatnot.
>
> I think I understand the trade-offs and appreciate the problems.  I'm
> not saying GDB must support such expressions, but the language
> definition seems to say it's an expression, so users might expect them
> to be supported.  Perhaps something should be said in the docs.
>
Errr, i'm pretty sure you are the first user ever to try this, no offense
or anything.

The docs go over what is allowed to be used, and nowhere does it say
anything about being able to create types out of thin air in cast
expressions. Of cours,e it doesn't say you can't, either.
I have to modify that part of the docs since it says stabs is good for C++
(which hasn't been true for about 4 years now) anyway, so if you want me
to add smething about allowable cast expressions, i'll be happy to.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]