From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6074 invoked by alias); 22 Nov 2004 22:50:22 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 5720 invoked from network); 22 Nov 2004 22:50:08 -0000 Received: from unknown (HELO cnr.cs.columbia.edu) (128.59.19.133) by sourceware.org with SMTP; 22 Nov 2004 22:50:08 -0000 Received: from cnr.cs.columbia.edu (localhost [127.0.0.1]) by cnr.cs.columbia.edu (8.13.1/8.13.1) with ESMTP id iAMMnuhi067693; Mon, 22 Nov 2004 17:49:56 -0500 (EST) (envelope-from lennox@cnr.cs.columbia.edu) Received: (from lennox@localhost) by cnr.cs.columbia.edu (8.13.1/8.13.1/Submit) id iAMMnqn3067688; Mon, 22 Nov 2004 17:49:52 -0500 (EST) (envelope-from lennox) From: Jonathan Lennox MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="cTwGB7wEl1" Content-Transfer-Encoding: 7bit Message-ID: <16802.27920.13033.780299@cnr.cs.columbia.edu> Date: Tue, 23 Nov 2004 00:03:00 -0000 References: <4D2CF60C-3919-11D9-8BD2-000A95BCF344@apple.com> <20041117212847.A26376@synopsys.com> <6F5FC748-7BBD-44B9-8DDC-246949F16102@apple.com> <20041118102741.A8347@synopsys.com> <77E8D36A-C0C2-4B03-964C-BEE0FE7BBBC3@apple.com> <98C86CD4-39E2-11D9-B2D5-000A95BCF344@apple.com> <20041119170011.A30410@synopsys.com> <9E6AD708-3A93-11D9-9070-000D9330C50E@apple.com> <20041119174042.A1311@synopsys.com> <90DC5074-3A96-11D9-9070-000D9330C50E@apple.com> <9CD04F70-3CC6-11D9-B847-000D9330C50E@apple.com> In-Reply-To: <9CD04F70-3CC6-11D9-B847-000D9330C50E@apple.com> To: Ziemowit Laski , gcc mailing list Cc: Steve Naroff , Michael Matz , Matt Austern , Joe Buck , Andrew Pinski , Mike Stump Subject: Re: generalized lvalues -- patch outline X-SW-Source: 2004-11/txt/msg00767.txt.bz2 --cTwGB7wEl1 Content-Type: text/plain; charset=us-ascii Content-Description: message body text Content-Transfer-Encoding: 7bit Content-length: 763 Ziemowit Laski writes: > 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. It occurs to me -- can't "lvalue casts" be done in standard GNU C and C++? Source code changes would be required, but they can probably be done largely mechanically. Here are the examples I've got -- they can maybe be cleaned up a bit, but both of these compile your test cases -Wall-cleanly with GCC 3.4. (They take advantage of the GCC extension that type-punning through a union is defined behavior.) First, the C++ example, since it's cleaner code: --cTwGB7wEl1 Content-Type: text/plain Content-Transfer-Encoding: 7bit Content-length: 732 /* lvalue_cast<> code by Jonathan Lennox , * placed in the public domain. */ template Targ& lvalue_cast(Src& s) { union { Src * s; Targ * t; } u; u.s = &s; return *u.t; /* GNU Extension */ } /* Test case by Ziemowit Laski */ #include #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; lvalue_cast(p) = long_arr; lvalue_cast(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; } --cTwGB7wEl1 Content-Type: text/plain; charset=us-ascii Content-Description: message body text Content-Transfer-Encoding: 7bit Content-length: 26 And then the C example: --cTwGB7wEl1 Content-Type: text/plain Content-Transfer-Encoding: 7bit Content-length: 662 /* LVALUE_CAST code by Jonathan Lennox , * placed in the public domain. */ #define LVALUE_CAST(TYPE, OBJ) (*({ \ union { __typeof__(OBJ) * s; TYPE * t; } u; \ \ u.s = &OBJ; \ u.t; /* GNU Extension */\ })) /* Test case by Ziemowit Laski */ #include #define CHECK_IF(expr) if (!(expr)) abort () int main(void) { char *p; long l; short s; LVALUE_CAST(long *, p) = &l; /* ok */ LVALUE_CAST(long *, p)++; /* ok */ LVALUE_CAST(short, l) = 2; /* Result depends on endianness. */ LVALUE_CAST(long, s) = 3; /* Stack clobber; run-time undefined behavior. */ return 0; } --cTwGB7wEl1 Content-Type: text/plain; charset=us-ascii Content-Description: message body and .signature Content-Transfer-Encoding: 7bit Content-length: 232 You'd have to do a global replace of lvalue casts with the new construct, but I imagine that's a lot easier to explain to programmers than the strict details of the new rules. -- Jonathan Lennox lennox at cs dot columbia dot edu --cTwGB7wEl1--