From patchwork Thu Jan 7 16:30:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1423384 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=r2gkIfBJ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DBWth5qMNz9sWC for ; Fri, 8 Jan 2021 03:30:34 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D898C3971815; Thu, 7 Jan 2021 16:30:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D898C3971815 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1610037032; bh=ByyL8mm7Qw2rKzmzpFslX/RFajdGPDPJ6vRi3x/zCqw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=r2gkIfBJ27ekpJtkIVifc90JwUJHxcWpl2C5CmhnddZlAeizG1xlQHH7i+c74ecIU Pjh42yFdhzthE+V9fa5VWK6H2iAfPWXSjx5sznsD/ymSepR7zLeyl5DQaRGCrEkIDo 6nr0wvamNruH4jnEtJ36wBXAErq/FLu86yNmgqVM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 9B46F3971810 for ; Thu, 7 Jan 2021 16:30:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9B46F3971810 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-171-6KR0d2qANfuNt5HdJp65NQ-1; Thu, 07 Jan 2021 11:30:28 -0500 X-MC-Unique: 6KR0d2qANfuNt5HdJp65NQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8E8EF10054FF for ; Thu, 7 Jan 2021 16:30:27 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-11.ams2.redhat.com [10.36.112.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 237585D9D7; Thu, 7 Jan 2021 16:30:26 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 107GUPRi3254561 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 7 Jan 2021 17:30:25 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 107GUOld3254560; Thu, 7 Jan 2021 17:30:24 +0100 Date: Thu, 7 Jan 2021 17:30:24 +0100 To: Jason Merrill Subject: [PATCH] c++: Fix up tsubst of BIT_CAST_EXPR [PR98329] Message-ID: <20210107163024.GN725145@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi! As the testcase shows, calling cp_build_bit_cast in tsubst_copy doesn't seem to be a good idea, because tsubst_copy might not really make the operand non-dependent, but as processing_template_decl can be 0, type_dependent_expression_p will return false and then cp_build_bit_cast assumes the type is non-NULL and non-dependent. So, this patch just follows what is done e.g. for NOP_EXPR etc. and just builds some tree in tsubst_copy, and only calls the semantics.c function from tsubst_copy_and_build. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-01-07 Jakub Jelinek PR c++/98329 * pt.c (tsubst_copy) : Don't call cp_build_bit_cast here, instead just build_min a BIT_CAST_EXPR and set its location. (tsubst_copy_and_build): Handle BIT_CAST_EXPR. * g++.dg/cpp2a/bit-cast10.C: New test. Jakub --- gcc/cp/pt.c.jj 2021-01-05 22:33:41.635816799 +0100 +++ gcc/cp/pt.c 2021-01-07 11:45:40.935610649 +0100 @@ -16796,7 +16796,9 @@ tsubst_copy (tree t, tree args, tsubst_f { tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); - return cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain); + r = build_min (BIT_CAST_EXPR, type, op0); + SET_EXPR_LOCATION (r, EXPR_LOCATION (t)); + return r; } case SIZEOF_EXPR: @@ -19631,6 +19633,13 @@ tsubst_copy_and_build (tree t, RETURN (r); } + case BIT_CAST_EXPR: + { + tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); + tree op0 = RECUR (TREE_OPERAND (t, 0)); + RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain)); + } + case POSTDECREMENT_EXPR: case POSTINCREMENT_EXPR: op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), --- gcc/testsuite/g++.dg/cpp2a/bit-cast10.C.jj 2021-01-07 11:48:02.998020930 +0100 +++ gcc/testsuite/g++.dg/cpp2a/bit-cast10.C 2021-01-07 11:47:52.224141482 +0100 @@ -0,0 +1,42 @@ +// PR c++/98329 +// { dg-do compile { target c++20 } } + +template +constexpr To +foo (const From &from) +{ + return __builtin_bit_cast (To, &from); +} + +template +constexpr To +bar (const From &from) +{ + return __builtin_bit_cast (To, *from); +} + +template +constexpr To +baz (const From &from) +{ + return __builtin_bit_cast (To, **from); +} + +template +constexpr To +qux (const From &from) +{ + return __builtin_bit_cast (To, -from); +} + +void +test () +{ + int i = 0; + int *j = &i; + int **k = &j; + foo (i); + bar (j); + baz (k); + qux (i); +}