From patchwork Fri Sep 6 11:15:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Martin X-Patchwork-Id: 1981772 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=nasilyan.com header.i=@nasilyan.com header.a=rsa-sha256 header.s=tey23rxsjton5kop5bydp3vc5ylkyjkg header.b=Vkfwcjr6; dkim=pass (1024-bit key; unprotected) header.d=amazonses.com header.i=@amazonses.com header.a=rsa-sha256 header.s=uku4taia5b5tsbglxyj6zym32efj7xqv header.b=Ecltnnoo; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X0YYf6bVmz1yh1 for ; Fri, 6 Sep 2024 21:15:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CAE6D384A449 for ; Fri, 6 Sep 2024 11:15:28 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from a2-92.smtp-out.eu-west-1.amazonses.com (a2-92.smtp-out.eu-west-1.amazonses.com [54.240.2.92]) by sourceware.org (Postfix) with ESMTPS id DB6FF3858CDA for ; Fri, 6 Sep 2024 11:15:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB6FF3858CDA Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=nasilyan.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eu-west-1.amazonses.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB6FF3858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=54.240.2.92 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725621310; cv=none; b=trwB/SoKfC+5w2ZVTWzuJabxGjcBwl2kToutbuZwtv3BahqUuA5I3U7q8p+ABMiw1bk1RIhhTbY2y73o3HcMJER8Wyn+se3u1FgipIiW91zIDHSd+1VYI9xhUS5gDlDXSRLrxsuxHX5isF1xkZg299PXWSZdHleblugndMJ7RHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725621310; c=relaxed/simple; bh=zdOkfxaUVmqqjKPorH5KT2liB6NT3wi74JLeh/jgmrk=; h=DKIM-Signature:DKIM-Signature:Subject:From:To:Date:Mime-Version: Message-ID; b=WzOZzOeRvCwuh3A6WPA8fOd4gQXteozTgg5ZalGbs34jGVWxoAMRo3Pzp9snAKq1puOOzSDtH3CvBooBtYlW32F1Iw5G7l4YU/fEz9hhgmYhFmyngFZ/4Gsskg+6Hd5WuaRu7886lkkY5TVTLDgKLJKDLB+NHNRfPqtgdo47N1E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=tey23rxsjton5kop5bydp3vc5ylkyjkg; d=nasilyan.com; t=1725621306; h=Subject:From:To:Date:Mime-Version:Content-Type:Content-Transfer-Encoding:References:Message-Id; bh=zdOkfxaUVmqqjKPorH5KT2liB6NT3wi74JLeh/jgmrk=; b=Vkfwcjr66hB79R7eelmM/xzBKZ6DhqZbK7IOL2vNq1dddQ89/ej5smFGwhLpcnrP ESpgDUuZur8IdqfRydBEzGB51zrC5MYtuMV7PexfDg3RfyXiVOJzQC2XnDfvXMx/9BA YcshCtxS/9isxUiqzDRId4rF70914KrqJxCaSepE= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=uku4taia5b5tsbglxyj6zym32efj7xqv; d=amazonses.com; t=1725621306; h=Subject:From:To:Date:Mime-Version:Content-Type:Content-Transfer-Encoding:References:Message-Id:Feedback-ID; bh=zdOkfxaUVmqqjKPorH5KT2liB6NT3wi74JLeh/jgmrk=; b=Ecltnnoo280LYhXNWbevkrEwrTXiDJ41eGNDBQtN0dOsHV/sowwqD+PmbNyN6ViR 1meFRFd2QdVYjGw/nzr3YT6acfdvvaICDFqC7MozHruMPkP5uI7dGk71tDwJHEzhf0y mKtTpUENA+117feayUcoY325BblTBY4qOXhJK/kc= Subject: [PATCH] c++: Properly mangle CONST_DECL without a INTEGER_CST value [PR116511] From: =?utf-8?q?Simon_Martin?= To: =?utf-8?q?gcc-patches=40gcc=2Egnu=2Eorg?= Date: Fri, 6 Sep 2024 11:15:06 +0000 Mime-Version: 1.0 References: <20240906111501.71213-1-simon@nasilyan.com> X-Mailer: Amazon WorkMail Thread-Index: AQHbAE4F4mWXKS3hQG24Rpqdve1PdQ== Thread-Topic: [PATCH] c++: Properly mangle CONST_DECL without a INTEGER_CST value [PR116511] X-Original-Mailer: git-send-email 2.44.0 X-Wm-Sent-Timestamp: 1725621304 Message-ID: <01020191c70b82f6-b6eab845-f923-4389-bef2-f189b8c4ca43-000000@eu-west-1.amazonses.com> Feedback-ID: ::1.eu-west-1.b24dn6frgCi6dh20skzbuMRr7UL8M6Soir/3ogtEjHQ=:AmazonSES X-SES-Outgoing: 2024.09.06-54.240.2.92 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org We ICE upon the following *valid* code when mangling the requires clause === cut here === template struct s1 { enum { e1 = 1 }; }; template struct s2 { enum { e1 = s1::e1 }; s2() requires(0 != e1) {} }; s2<8> a; === cut here === The problem is that the mangler wrongly assumes that the DECL_INITIAL of a CONST_DECL is always an INTEGER_CST, and blindly passes it to write_integer_cst. I assume we should be able to actually compute the value of e1 and use it when mangling, however from my investigation, it seems to be a pretty involved change. What's clear however is that we should not try to write a non-literal as a literal. This patch adds a utility function to determine whether a tree is a literal as per the definition in the ABI, and uses it to only call write_template_arg_literal when we actually have a literal in hand. Note that I had to change the expectation of an existing test, that was expecting "[...]void (AF::*)(){}[...]" and now gets an equivalent "[...](void (AF::*)())0[...]" (and FWIW is what clang and icx give; see https://godbolt.org/z/hnjdeKEhW). Successfully tested on x86_64-pc-linux-gnu. PR c++/116511 gcc/cp/ChangeLog: * mangle.cc (literal_p): New. (write_expression): Only call write_template_arg_literal for expressions with literal_p. (write_template_arg): Likewise. (write_template_arg_literal): Assert literal_p. gcc/testsuite/ChangeLog: * g++.dg/abi/mangle72.C: Adjust test expectation. * g++.dg/abi/mangle80.C: New test. --- gcc/cp/mangle.cc | 33 ++++++++++++++++++++++++----- gcc/testsuite/g++.dg/abi/mangle72.C | 2 +- gcc/testsuite/g++.dg/abi/mangle80.C | 13 ++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/abi/mangle80.C diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc index 46dc6923add..8279c3fe177 100644 --- a/gcc/cp/mangle.cc +++ b/gcc/cp/mangle.cc @@ -223,6 +223,7 @@ static void write_method_parms (tree, const int, const tree); static void write_class_enum_type (const tree); static void write_template_args (tree, tree = NULL_TREE); static void write_expression (tree); +static bool literal_p (const tree); static void write_template_arg_literal (const tree); static void write_template_arg (tree); static void write_template_template_arg (const tree); @@ -3397,8 +3398,7 @@ write_expression (tree expr) || code == TEMPLATE_PARM_INDEX) write_template_param (expr); /* Handle literals. */ - else if (TREE_CODE_CLASS (code) == tcc_constant - || code == CONST_DECL) + else if (literal_p (expr)) write_template_arg_literal (expr); else if (code == EXCESS_PRECISION_EXPR && TREE_CODE (TREE_OPERAND (expr, 0)) == REAL_CST) @@ -3946,6 +3946,29 @@ write_expression (tree expr) } } +/* Determine whether T is a literal per section 5.1.6.1 of the CXX ABI. */ + +static bool +literal_p (const tree t) +{ + if ((TREE_TYPE (t) && NULLPTR_TYPE_P (TREE_TYPE (t))) + || null_member_pointer_value_p (t)) + return true; + else + switch (TREE_CODE (t)) + { + case CONST_DECL: + return literal_p (DECL_INITIAL (t)); + case INTEGER_CST: + case REAL_CST: + case STRING_CST: + case COMPLEX_CST: + return true; + default: + return false; + } +} + /* Literal subcase of non-terminal . "Literal arguments, e.g. "A<42L>", are encoded with their type @@ -3956,6 +3979,8 @@ write_expression (tree expr) static void write_template_arg_literal (const tree value) { + gcc_assert (literal_p (value)); + if (TREE_CODE (value) == STRING_CST) /* Temporarily mangle strings as braced initializer lists. */ write_string ("tl"); @@ -4113,9 +4138,7 @@ write_template_arg (tree node) else if (code == TEMPLATE_DECL) /* A template appearing as a template arg is a template template arg. */ write_template_template_arg (node); - else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST) - || code == CONST_DECL - || null_member_pointer_value_p (node)) + else if (literal_p (node)) write_template_arg_literal (node); else if (code == EXCESS_PRECISION_EXPR && TREE_CODE (TREE_OPERAND (node, 0)) == REAL_CST) diff --git a/gcc/testsuite/g++.dg/abi/mangle72.C b/gcc/testsuite/g++.dg/abi/mangle72.C index 9581451c25d..fd7d6cb51ad 100644 --- a/gcc/testsuite/g++.dg/abi/mangle72.C +++ b/gcc/testsuite/g++.dg/abi/mangle72.C @@ -89,7 +89,7 @@ void k00 (F) { } // { dg-final { scan-assembler "_Z3k001FIXtl1DEEE" } } void k0x (F) { } -// { dg-final { scan-assembler "_Z3k0x1FIXtl1DtlA2_M2AFFvvEtlS3_EtlS3_adL_ZNS1_1fEvEEEEEE" } } +// { dg-final { scan-assembler "_Z3k0x1FIXtl1DtlA2_M2AFFvvELS3_0EtlS3_adL_ZNS1_1fEvEEEEEE" } } void kx_ (F) { } // { dg-final { scan-assembler "_Z3kx_1FIXtl1DtlA2_M2AFFvvEtlS3_adL_ZNS1_1fEvEEEEEE" } } diff --git a/gcc/testsuite/g++.dg/abi/mangle80.C b/gcc/testsuite/g++.dg/abi/mangle80.C new file mode 100644 index 00000000000..983f35cc440 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/mangle80.C @@ -0,0 +1,13 @@ +// PR c++/116511 +// { dg-do compile { target c++20 } } +// { dg-additional-options -fabi-compat-version=0 } +template struct s1 { + enum { e1 = 1 }; +}; +template struct s2 { + enum { e1 = s1::e1 }; + s2() requires(0 != e1) {} +}; + +// { dg-final { scan-assembler "_ZN2s2ILi8EEC1EvQneLi0EL_ZNS_IXT_EEUt_2e1EE" } } +s2<8> a;