AndrC) Wagner <andre.nho@gmail.com> writes:> ...> What I'm trying to say is: the compiler yields different assembly code> for when 'x' is a lvalue and when 'x' is not a lvalue.

Of course.

> This gets more confusing when I have expressions such as 'x++'. This> is simple, since 'x' is obviously a lvalue in this case. In the case> of the compiler, I can parse 'x' and see that the lookahead points to> '++', so it's a lvalue.>> But what about '(x)++'? In this case, the compiler evaluates the> subexpression '(x)', and this expression results the value of 'x', not> the address. Now I have a '++' ahead, so how can I know the address of> 'x' since all that I have is a value?

In C, a parenthesized lvalue is an lvalue.

> All documentation that I found about lvalues were too vague, and> directed to the programmer, and not to the compiler writer. Are there> any specific rules for determining if the result of a expression is a> lvalue?

The definitive document is the C standard. You can get a copy of the
1999 ISO C standard by sending money to your national standard body;
see, for example, webstore.ansi.org. Or you can get a free copy of
the latest post-C99 draft, incorporating the C99 standard plus the
three Technical Corrigenda, at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>. The
Technical Corrigenda themselves are available at no charge.

I wouldn't even consider trying to implement a compiler without
having a copy of the standard for the language.

Some relevant passages:

C99 6.5.2.1:

Except when it is the operand of the sizeof operator, the unary &
operator, the ++ operator, the -- operator, or the left operand
of the . operator or an assignment operator, an lvalue that
does not have array type is converted to the value stored in
the designated object (and is no longer an lvalue).

C99 6.5.1p5:

A parenthesized expression is a primary expression. Its
type and value are identical to those of the unparenthesized
expression. It is an lvalue, a function designator, or a void
expression if the unparenthesized expression is, respectively,
an lvalue, a function designator, or a void expression.

Note that the definition of "lvalue" in C99 6.3.2.1p1 is flawed, or
at least incomplete. An lvalue is not merely "an expression with
an object type or an incomplete type other than void"; it's such
an expression that designates, or that could designate, an object.
For example, int is an object type, and 42 is an expression of
type int, but 42 is not an lvalue. On the other hand, if ptr is a
pointer-to-int, *ptr is an lvalue, even if ptr==NULL (but attempting
to use it invokes undefined behavior).