From patchwork Thu May 23 17:24:43 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Stump X-Patchwork-Id: 245985 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 8036E2C0342 for ; Fri, 24 May 2013 03:24:54 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :content-type:subject:date:message-id:cc:to:mime-version; q=dns; s=default; b=MEb8LqUX1wY9PdxXQT9mifDWKQRooNxZdosY1+32wUwJB5UFTx MQFDi9t/Vfire2hiGBorK40pX8/wtqRsPSGSOSZxlTgzPjDtPnlTSl3O8EbgYMtP B76PyrcLN64Ph+OR7DBPReMMp0hEpfFB+3PIElD7Rl/QTNMHlS1Sx6gQo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :content-type:subject:date:message-id:cc:to:mime-version; s= default; bh=dC0StYF0HpDTMGw+6GZxO+OqZnA=; b=sIlczLmBdnIOqS+U9ZHw fAPAf4RKJEFG+O7G1HF29KnxH2g3ghtpX5PXPLF79Cepa5HzFPdR5CVVQZ+mHC2n 5nxTdcG2mLequEj0Sz0dK8gwYRLgtWn4ZGPGU+1Ga6R0quxEKnOPA1h9I6Xh3VEO 0nMPQ773Sg2ISZHNFiq0l60= Received: (qmail 7926 invoked by alias); 23 May 2013 17:24:48 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 7915 invoked by uid 89); 23 May 2013 17:24:47 -0000 X-Spam-SWARE-Status: No, score=-3.0 required=5.0 tests=BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_HOSTKARMA_NO, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.1 Received: from qmta11.emeryville.ca.mail.comcast.net (HELO qmta11.emeryville.ca.mail.comcast.net) (76.96.27.211) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 23 May 2013 17:24:46 +0000 Received: from omta15.emeryville.ca.mail.comcast.net ([76.96.30.71]) by qmta11.emeryville.ca.mail.comcast.net with comcast id fSmy1l0031Y3wxoABVQlor; Thu, 23 May 2013 17:24:45 +0000 Received: from bag6-1-pt.tunnel.tserv3.fmt2.ipv6.he.net ([IPv6:2001:470:1f04:ae1::2]) by omta15.emeryville.ca.mail.comcast.net with comcast id fVQj1l00k0P3DwE8bVQkyP; Thu, 23 May 2013 17:24:45 +0000 From: Mike Stump Subject: fix memory spaces and references for C Date: Thu, 23 May 2013 10:24:43 -0700 Message-Id: Cc: Kenneth Zadeck To: " Patches" Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\)) X-Virus-Found: No So, memory spaces and references are interacting badly in C. The standard allows conversions during assignment that can change qualifiers. The good news, all that code is already written and appears to work just fine. The sad part, we don't use it. The code that needs fixing is in convert_for_assignment: /* A type converts to a reference to it. This code doesn't fully support references, it's just for the special case of va_start and va_copy. */ if (codel == REFERENCE_TYPE && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) This doesn't work, as the memory space qualifiers disqualify the two types from being compatible (this is correct and matches the standard). Instead, we expand the conditional to include all cases we are prepared to handle: if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) and then, in the body, we use convert_for_assignment to handle the conversion, as it already does everything. This is a smaller patch than maybe it should be. Arguably not recursing is a better approach, but then we need to split into two functions, so that I can add the REFERENCE_TYPE back to the top. Let me know if you prefer it split. A user actually hit this in rather trivial code with memory spaces. Tested on two platforms, one with memory spaces and one without. Ok? ------------------------------ diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index fe6d1f6..40ccf58 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -5294,11 +5294,9 @@ convert_for_assignment (location_t location, tree type, tree rhs, rhs = require_complete_type (rhs); if (rhs == error_mark_node) return error_mark_node; - /* A type converts to a reference to it. - This code doesn't fully support references, it's just for the - special case of va_start and va_copy. */ - if (codel == REFERENCE_TYPE - && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1) + /* A non-reference type can convert to a reference. This handles + va_start, va_copy and possibly port built-ins. */ + if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE) { if (!lvalue_p (rhs)) { @@ -5310,16 +5308,11 @@ convert_for_assignment (location_t location, tree type, tree rhs, rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs); SET_EXPR_LOCATION (rhs, location); - /* We already know that these two types are compatible, but they - may not be exactly identical. In fact, `TREE_TYPE (type)' is - likely to be __builtin_va_list and `TREE_TYPE (rhs)' is - likely to be va_list, a typedef to __builtin_va_list, which - is different enough that it will cause problems later. */ - if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type)) - { - rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs); - SET_EXPR_LOCATION (rhs, location); - } + rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)), + rhs, origtype, errtype, null_pointer_constant, + fundecl, function, parmnum); + if (rhs == error_mark_node) + return error_mark_node; rhs = build1 (NOP_EXPR, type, rhs); SET_EXPR_LOCATION (rhs, location);