From: Ziemowit Laski <zlaski@apple.com>
To: Ziemowit Laski <zlaski@apple.com>, gcc mailing list <gcc@gcc.gnu.org>
Cc: Steve Naroff <snaroff@apple.com>, Michael Matz <matz@suse.de>,
Matt Austern <austern@apple.com>,
Joe Buck <Joe.Buck@synopsys.com>,
Andrew Pinski <pinskia@physics.uc.edu>,
Mike Stump <mrs@apple.com>
Subject: generalized lvalues -- patch outline
Date: Mon, 22 Nov 2004 20:54:00 -0000 [thread overview]
Message-ID: <9CD04F70-3CC6-11D9-B847-000D9330C50E@apple.com> (raw)
In-Reply-To: <90DC5074-3A96-11D9-9070-000D9330C50E@apple.com>
On 19 Nov 2004, at 17.50, Ziemowit Laski wrote:
> To put it another way, I'm only concerned with cases where the
> compiler currently complains
> about assigning to a non-lvalue, and the non-lvalue in question is a
> cast of an lvalue.
Indeed, I now appear to have a mainline mod for C and C++ which allows
assignment to lvalue casts for pointer types. What follows is a
high-level synopsis of what I did; if there is interest, I can whip up
a full-fledged patch, complete with docs. Please let me know.
Thanks,
--Zem
------
The lvalue cast assignment/increment/decrement can be enabled with the
-flvalue-cast-assign flag (which is off by default).
The active ingredient of the C patch is:
Index: gcc/c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.400
diff -u -3 -p -r1.400 c-typeck.c
--- gcc/c-typeck.c 20 Nov 2004 20:31:43 -0000 1.400
+++ gcc/c-typeck.c 22 Nov 2004 20:32:00 -0000
@@ -2724,6 +2724,16 @@ lvalue_or_else (tree ref, enum lvalue_us
{
int win = lvalue_p (ref);
+ /* If -flvalue-cast-assignment is specified, we shall allow
assignments
+ (including increment/decrement) to casts of lvalues, as long as
+ both the lvalue and the cast are pointers. */
+ if (!win && flag_lvalue_cast_assign
+ && TREE_CODE (ref) == NOP_EXPR
+ && (use == lv_assign || use == lv_increment || use ==
lv_decrement)
+ && TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE
+ && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == POINTER_TYPE)
+ win = lvalue_p (TREE_OPERAND (ref, 0));
+
if (!win)
{
switch (use)
and will allow code as follows:
Index: gcc/testsuite/gcc.dg/lvalue-cast-1.c
===================================================================
RCS file: gcc/testsuite/gcc.dg/lvalue-cast-1.c
diff -N gcc/testsuite/gcc.dg/lvalue-cast-1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/gcc.dg/lvalue-cast-1.c 22 Nov 2004 20:32:50
-0000
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-flvalue-cast-assign" } */
+
+int foo(void) {
+
+ char *p;
+ long l;
+ short s;
+
+ (long *)p = &l; /* ok */
+ ((long *)p)++; /* ok */
+ (short)l = 2; /* { dg-error "non-lvalue" } */
+ (long)s = 3; /* { dg-error "non-lvalue" } */
+
+ return 0;
+}
Note that we're still erroring out on the non-pointer types, though
that too can be changed. FWIW, Microsoft's C compiler also allows the
'(short)l = 2' (though not the '(long)s = 3').
The C++ side of things is very similar:
Index: gcc/cp/tree.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/tree.c,v
retrieving revision 1.419
diff -u -3 -p -r1.419 tree.c
--- gcc/cp/tree.c 12 Nov 2004 21:47:09 -0000 1.419
+++ gcc/cp/tree.c 22 Nov 2004 20:34:50 -0000
@@ -223,6 +223,17 @@ lvalue_or_else (tree ref, const char* st
{
if (!lvalue_p (ref))
{
+ /* If -flvalue-cast-assignment is specified, we shall allow
assignments
+ (including increment/decrement) to casts of lvalues, as long as
+ both the lvalue and the cast are pointers. */
+ if (flag_lvalue_cast_assign
+ && TREE_CODE (ref) == NOP_EXPR
+ && string && string[0] == 'a'
+ && TREE_CODE (TREE_TYPE (ref)) == POINTER_TYPE
+ && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) ==
POINTER_TYPE
+ && lvalue_p (TREE_OPERAND (ref, 0)))
+ return 1;
+
error ("non-lvalue in %s", string);
return 0;
}
although we clearly could use an 'enum lvalue_use' here as well. At
any rate, this patch causes the following to compile and run
successfully:
Index: gcc/testsuite/g++.dg/ext/lvalue-cast-1.cpp
===================================================================
RCS file: gcc/testsuite/g++.dg/ext/lvalue-cast-1.cpp
diff -N gcc/testsuite/g++.dg/ext/lvalue-cast-1.cpp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/g++.dg/ext/lvalue-cast-1.cpp 22 Nov 2004 20:36:43
-0000
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+/* { dg-options "-flvalue-cast-assign" } */
+
+#include <stdlib.h>
+#define CHECK_IF(expr) if (!(expr)) abort ()
+
+static int global;
+
+void f(int &) { global = 35; }
+void f(const int &) { global = 78; }
+
+long long_arr[2];
+
+int main(void) {
+
+ char *p;
+
+ (long *)p = long_arr;
+ ((long *)p)++;
+ *(long *)p = -1;
+ *p = -2;
+ CHECK_IF(p[-1] == 0 && p[0] == -2 && p[1] == -1);
+
+ long x = 0;
+ f((int)x);
+ CHECK_IF(global == 78);
+
+ return 0;
+}
The rest of the patch (not shown) deals with defining/handling of the
-flvalue-cast-assign flag.
next prev parent reply other threads:[~2004-11-22 20:40 UTC|newest]
Thread overview: 116+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-11-18 3:50 generalized lvalues Matt Austern
2004-11-18 3:52 ` Andrew Pinski
2004-11-18 4:01 ` Andrew Pinski
2004-11-18 4:18 ` Daniel Berlin
2004-11-18 4:21 ` Andrew Pinski
2004-11-18 4:27 ` Matt Austern
2004-11-18 7:15 ` Joe Buck
2004-11-18 7:37 ` Matt Austern
2004-11-18 13:17 ` Giovanni Bajo
2004-11-18 17:57 ` Joe Buck
2004-11-18 18:28 ` Mike Stump
2004-11-18 18:44 ` Joe Buck
2004-11-19 1:39 ` Mike Stump
2004-11-19 4:52 ` Matt Austern
2004-11-19 22:24 ` Michael Matz
2004-11-19 22:30 ` Robert McNulty Junior
2004-11-20 1:00 ` Ziemowit Laski
2004-11-20 1:20 ` Joe Buck
2004-11-20 1:51 ` Ziemowit Laski
2004-11-20 5:04 ` Joe Buck
2004-11-20 5:17 ` Ziemowit Laski
2004-11-22 20:54 ` Ziemowit Laski [this message]
2004-11-22 21:01 ` generalized lvalues -- patch outline Andrew Pinski
2004-11-22 21:11 ` Ziemowit Laski
2004-11-22 21:39 ` Matt Austern
2004-11-22 22:11 ` Joe Buck
2004-11-22 22:12 ` Matt Austern
2004-11-23 0:06 ` Gabriel Dos Reis
2004-11-23 0:04 ` Gabriel Dos Reis
2004-11-23 0:27 ` Mike Stump
2004-11-24 20:40 ` Kai Henningsen
2004-11-23 0:11 ` Mark Mitchell
2004-11-23 1:19 ` Matt Austern
2004-11-23 1:26 ` Mark Mitchell
2004-11-23 1:32 ` Dale Johannesen
2004-11-23 1:42 ` Mark Mitchell
2004-11-23 1:45 ` Andrew Pinski
2004-11-23 1:55 ` Dale Johannesen
2004-11-23 1:56 ` Andrew Pinski
2004-11-23 2:12 ` Dale Johannesen
2004-11-23 2:10 ` Marcus G. Daniels
2004-11-23 9:43 ` Eric Botcazou
2004-11-23 18:27 ` Aaron W. LaFramboise
2004-11-22 21:23 ` Nathan Sidwell
2004-11-22 22:33 ` Ziemowit Laski
2004-11-23 0:09 ` Gabriel Dos Reis
2004-11-23 9:54 ` Nathan Sidwell
2004-11-23 13:35 ` Michael Matz
2004-11-23 14:56 ` Daniel Berlin
2004-11-23 15:02 ` Michael Matz
2004-11-23 15:15 ` Andrew Pinski
2004-11-23 15:47 ` Michael Matz
2004-11-23 15:56 ` Gabriel Dos Reis
2004-11-23 15:50 ` Steve Naroff
2004-11-23 15:51 ` Gabriel Dos Reis
2004-11-23 16:50 ` Eric Botcazou
2004-11-23 17:01 ` Paul Koning
2004-11-23 17:16 ` Andreas Schwab
2004-11-23 18:03 ` Thomas Kunert
2004-11-23 18:30 ` Nathan Sidwell
2004-11-23 18:57 ` Thomas Kunert
2004-11-23 21:21 ` Paul Koning
2004-11-23 21:52 ` Thomas Kunert
2004-11-23 15:51 ` Gabriel Dos Reis
2004-11-23 16:12 ` Michael Matz
2004-11-23 16:31 ` Richard Guenther
2004-11-23 16:44 ` Andreas Schwab
2004-11-23 20:16 ` Gabriel Dos Reis
2004-11-23 15:01 ` Nathan Sidwell
2004-11-23 15:09 ` Michael Matz
2004-11-23 19:11 ` Matt Austern
2004-11-23 21:10 ` Eric Botcazou
2004-11-23 21:34 ` Ziemowit Laski
2004-11-23 22:09 ` Joseph S. Myers
2004-11-23 22:23 ` Eric Botcazou
2004-11-23 22:27 ` Joseph S. Myers
2004-11-24 0:05 ` Eric Botcazou
2004-11-24 0:07 ` Steven Bosscher
2004-11-24 0:32 ` Joseph S. Myers
2004-11-25 8:41 ` Ian Lance Taylor
2004-11-25 20:41 ` Gabriel Dos Reis
2004-11-25 21:54 ` Matt Austern
2004-11-25 23:27 ` Gabriel Dos Reis
2004-11-29 18:06 ` Joe Buck
2004-11-29 18:57 ` Matt Austern
2004-11-23 23:27 ` Gabriel Dos Reis
2004-11-23 22:24 ` Nathan Sidwell
2004-11-23 0:07 ` Gabriel Dos Reis
2004-11-24 20:43 ` Kai Henningsen
2004-11-24 23:09 ` Gabriel Dos Reis
2004-11-24 23:17 ` Zack Weinberg
2004-11-22 21:31 ` Richard Henderson
2004-11-23 0:11 ` Gabriel Dos Reis
2004-11-23 0:03 ` Jonathan Lennox
2004-11-20 1:40 ` generalized lvalues Joseph S. Myers
2004-11-18 19:11 ` Alex Rosenberg
2004-11-18 4:24 ` Matt Austern
2004-11-18 5:33 ` Dale Johannesen
2004-11-18 17:14 ` Fariborz Jahanian
2004-11-18 18:23 ` Joe Buck
2004-11-19 0:20 ` Ziemowit Laski
2004-11-19 0:40 ` Steven Bosscher
2004-11-19 1:20 ` Joe Buck
2004-11-19 2:00 ` Ziemowit Laski
2004-11-18 23:36 ` Ziemowit Laski
2004-11-18 23:37 ` Matt Austern
2004-11-18 23:49 ` Zack Weinberg
2004-11-19 1:38 ` Ziemowit Laski
2004-11-19 2:52 ` Zack Weinberg
2004-11-19 3:56 ` Ziemowit Laski
2004-11-19 4:22 ` Zack Weinberg
2004-11-19 13:10 ` Nathan Sidwell
2004-11-19 21:58 ` Ziemowit Laski
2004-11-18 23:44 ` Andrew Pinski
2004-11-18 23:45 ` Joe Buck
2004-11-25 4:42 ` Aaron W. LaFramboise
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=9CD04F70-3CC6-11D9-B847-000D9330C50E@apple.com \
--to=zlaski@apple.com \
--cc=Joe.Buck@synopsys.com \
--cc=austern@apple.com \
--cc=gcc@gcc.gnu.org \
--cc=matz@suse.de \
--cc=mrs@apple.com \
--cc=pinskia@physics.uc.edu \
--cc=snaroff@apple.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).