From patchwork Tue Aug 22 08:12:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 136467 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3478307vqi; Tue, 22 Aug 2023 01:13:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJKfxwPoe5yIBX9oDyyloNtQaE8IuxOYdtbLaO0ig9/25znsDezcdEkutcx0JdiRwP5CKj X-Received: by 2002:a17:906:8a42:b0:99b:fdbb:31f1 with SMTP id gx2-20020a1709068a4200b0099bfdbb31f1mr7321212ejc.16.1692692022052; Tue, 22 Aug 2023 01:13:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692692022; cv=none; d=google.com; s=arc-20160816; b=DID6igJiEZCZ55/1WGBkc280/0hA3QIJ0ZuutMRF33wn+6iX/G69nqsSJdJrOd11lc IfTOgTDjDbAPhWLHSbuWnwXO5Q7qvHuJUh9krTUB0iwUo5/SCqrEUU4KqMiNxlEnpXGd 31/zB64E3ANuq3Pn/5tJIenX04gN0+22h/ONDfJJyon9EyGDphiApgvFqM41VLibKNwi LjWpAnhDzBU5Pn6Rd1CuiQBUkL+k6G9W3lkTeL/u3+zt2s84ZBfV9G6PbQrbOjYR0HuM Ij4q6xeOTgeSU0Bti84wzMpCqSQ50HAcPqfmMWFVXP2CX10YPJygO97ZwstJEtc3WWdM audQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=KXuSI3sjw4si2vVT5KCmKntVr3gtM1n5cIuezVpniPY=; fh=IMTvZBfL0G7+xziJdWPs4IsGafFJ6QqWQkMOdm9kPF0=; b=l6fh2vFExVgDFdadmAHSc36Uj6qYeLverSihVAfBF5rOJAYpvYxHDhgIiS7+LSI/2b m2/aC6Gk+ucrvjzpOCH8LW0VDLGFKQ9pmC6ATPmZNXYij54Pee02qv4++tjjq2HhUH2q FSH3+/XaMyQH6sqMBoVrtuGrJ8ZZkgZ+lRdRlODjX97NpMEM0KqrVLjR8RbdAVovGX2q RRFTte13RsbTFFaeOp/S6iBGQeOZL4u3s0/l7DoiGi94kfTYr7U0Q2ox/vz0MvWxlvqK U67hNbjceknMr0kV1XKxgnfvgHC3ohC39TBypXqSqO6T7hNlBNp9QF8AG67QN6bHg4xq zZEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S5npabSZ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w14-20020a170906480e00b0099207b3bc46si6826575ejq.322.2023.08.22.01.13.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 01:13:42 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=S5npabSZ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F05C38560B6 for ; Tue, 22 Aug 2023 08:13:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F05C38560B6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692692020; bh=KXuSI3sjw4si2vVT5KCmKntVr3gtM1n5cIuezVpniPY=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=S5npabSZT2BSjSXHUj1bfefHbhCvyals3Od0jyOJx5YqfeR8333h5XMyx4+mW9HS9 2+Vaevpw7/G+aQQx8ACkmdbsOXg3PDYLhnoblAWJfV3Yxi9NuhjUv9lz2P59hRfA47 VLXE+Ie972knfZ/pRlejZ7fVJ3/h/Smngdex6zhM= 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id BEBC03858D28 for ; Tue, 22 Aug 2023 08:12:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BEBC03858D28 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-526-FHxDdINXNJKBqzBrOdLIyQ-1; Tue, 22 Aug 2023 04:12:50 -0400 X-MC-Unique: FHxDdINXNJKBqzBrOdLIyQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6EE74299E747 for ; Tue, 22 Aug 2023 08:12:50 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.225.165]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1378440D2843; Tue, 22 Aug 2023 08:12:49 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 37M8Cm3a2416250 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 22 Aug 2023 10:12:48 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37M8CmUi2416249; Tue, 22 Aug 2023 10:12:48 +0200 Date: Tue, 22 Aug 2023 10:12:48 +0200 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix up mangling of function/block scope static structured bindings [PR111069] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.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 Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774916229889167262 X-GMAIL-MSGID: 1774916229889167262 Hi! As can be seen on the testcase, we weren't correctly mangling static/thread_local structured bindings (C++20 feature) at function/block scope. The following patch fixes that by using what write_local_name does for those cases (note, structured binding mandling doesn't use the standard path because it needs to pass a list of all the identifiers in the structured binding to the mangling). In addition to that it fixes mangling of various helpers which use write_guarded_name (_ZGV*, _ZTH*, _ZTW*) and kills find_decomp_unqualified_name which for the local names would be too hard to implement and uses write_guarded_name for structured binding related _ZGR* names as well. All the mangled names on the testcase match now clang++ and my expectations. Because the old mangled names were plain wrong (they mangled the same as structured binding at global scope and resulted in assembly errors if there was more than one static structured binding with the same identifiers in the same (or another) function, I think we don't need to play with another mangling ABI level which turns on/off the old broken way, unsure whether we should backport the patch to 13 branch though. BTW, I think we should also handle ABI tags in mangle_decomp which we currently don't do, but guess that should be subject to another mangling ABI version. On struct __attribute__((abi_tag ("foobar"))) S { int i; }; extern S a[2]; struct __attribute__((abi_tag ("qux"))) T { int i; S j; int k; }; extern T b[2]; namespace N { auto [i, j] = a; auto [k, l] = b; S c; T d; } inline void foo () { static auto [m, n] = a; static auto [o, p] = b; { static auto [m, n] = a; ++n.i; } ++m.i; ++o.i; } void (*p) () = &foo; clang++ uses _ZN1NDC1i1jEB6foobarE _ZN1NDC1k1lEB3quxE _ZZ3foovEDC1m1nEB6foobar _ZGVZ3foovEDC1m1nEB6foobar _ZZ3foovEDC1o1pEB3qux _ZGVZ3foovEDC1o1pEB3qux _ZZ3foovEDC1m1nEB6foobar_0 _ZGVZ3foovEDC1m1nEB6foobar_0 mangling while g++ (with the patch): _ZN1NDC1i1jEE _ZN1NDC1k1lEE _ZZ3foovEDC1m1nE _ZGVZ3foovEDC1m1nE _ZZ3foovEDC1o1pE _ZGVZ3foovEDC1o1pE _ZZ3foovEDC1m1nE_0 _ZGVZ3foovEDC1m1nE_0 Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-08-22 Jakub Jelinek PR c++/111069 * cp-tree.h (determine_local_discriminator): Add NAME argument with NULL_TREE default. * decl.cc (determine_local_discriminator): Add NAME argument, use it if non-NULL, otherwise compute it the old way. (cp_maybe_mangle_decomp): Call determine_local_discriminator. * mangle.cc (find_decomp_unqualified_name): Remove. (write_unqualified_name): Don't call find_decomp_unqualified_name. (mangle_decomp): Handle mangling of static function/block scope structured bindings. Don't call decl_mangling_context twice. (write_guarded_var_name): Handle structured bindings. (mangle_ref_init_variable): Use write_guarded_var_name for structured bindings. * g++.dg/cpp2a/decomp8.C: New test. Jakub --- gcc/cp/cp-tree.h.jj 2023-08-18 20:11:11.013692860 +0200 +++ gcc/cp/cp-tree.h 2023-08-19 10:45:00.320543681 +0200 @@ -6858,7 +6858,7 @@ extern void pop_switch (void); extern void note_break_stmt (void); extern bool note_iteration_stmt_body_start (void); extern void note_iteration_stmt_body_end (bool); -extern void determine_local_discriminator (tree); +extern void determine_local_discriminator (tree, tree = NULL_TREE); extern bool fns_correspond (tree, tree); extern int decls_match (tree, tree, bool = true); extern bool maybe_version_functions (tree, tree, bool); --- gcc/cp/decl.cc.jj 2023-08-18 09:49:38.899871662 +0200 +++ gcc/cp/decl.cc 2023-08-19 10:52:11.748888997 +0200 @@ -913,15 +913,16 @@ static GTY((deletable)) vec generally very few of these in any particular function. */ void -determine_local_discriminator (tree decl) +determine_local_discriminator (tree decl, tree name) { auto_cond_timevar tv (TV_NAME_LOOKUP); retrofit_lang_decl (decl); tree ctx = DECL_CONTEXT (decl); - tree name = (TREE_CODE (decl) == TYPE_DECL - && TYPE_UNNAMED_P (TREE_TYPE (decl)) - ? NULL_TREE : DECL_NAME (decl)); size_t nelts = vec_safe_length (local_entities); + if (name == NULL_TREE) + name = (TREE_CODE (decl) == TYPE_DECL + && TYPE_UNNAMED_P (TREE_TYPE (decl)) + ? NULL_TREE : DECL_NAME (decl)); for (size_t i = 0; i < nelts; i += 2) { tree *pair = &(*local_entities)[i]; @@ -9049,6 +9050,25 @@ cp_maybe_mangle_decomp (tree decl, tree tree d = first; for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d)) v[count - i - 1] = d; + if (DECL_FUNCTION_SCOPE_P (decl)) + { + size_t sz = 3; + for (unsigned int i = 0; i < count; ++i) + sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1; + char *name = XALLOCAVEC (char, sz); + name[0] = 'D'; + name[1] = 'C'; + char *p = name + 2; + for (unsigned int i = 0; i < count; ++i) + { + size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i])); + *p++ = ' '; + memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len); + p += len; + } + *p = '\0'; + determine_local_discriminator (decl, get_identifier (name)); + } SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v)); maybe_apply_pragma_weak (decl); } --- gcc/cp/mangle.cc.jj 2023-08-08 15:55:06.218168490 +0200 +++ gcc/cp/mangle.cc 2023-08-21 10:39:35.209155346 +0200 @@ -1344,51 +1344,6 @@ write_template_prefix (const tree node) add_substitution (substitution); } -/* As the list of identifiers for the structured binding declaration - DECL is likely gone, try to recover the DC + E portion - from its mangled name. Return pointer to the DC and set len to - the length up to and including the terminating E. On failure - return NULL. */ - -static const char * -find_decomp_unqualified_name (tree decl, size_t *len) -{ - const char *p = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); - const char *end = p + IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (decl)); - bool nested = false; - if (!startswith (p, "_Z")) - return NULL; - p += 2; - if (startswith (p, "St")) - p += 2; - else if (*p == 'N') - { - nested = true; - ++p; - while (ISDIGIT (p[0])) - { - char *e; - long num = strtol (p, &e, 10); - if (num >= 1 && num < end - e) - p = e + num; - else - break; - } - } - if (!startswith (p, "DC")) - return NULL; - if (nested) - { - if (end[-1] != 'E') - return NULL; - --end; - } - if (end[-1] != 'E') - return NULL; - *len = end - p; - return p; -} - /* "For the purposes of mangling, the name of an anonymous union is considered to be the name of the first named data member found by a pre-order, depth-first, declaration-order walk of the data members of the anonymous @@ -1461,17 +1416,7 @@ write_unqualified_name (tree decl) { found = true; gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); - const char *decomp_str = NULL; - size_t decomp_len = 0; - if (VAR_P (decl) - && DECL_DECOMPOSITION_P (decl) - && DECL_NAME (decl) == NULL_TREE - && DECL_NAMESPACE_SCOPE_P (decl)) - decomp_str = find_decomp_unqualified_name (decl, &decomp_len); - if (decomp_str) - write_chars (decomp_str, decomp_len); - else - write_source_name (DECL_ASSEMBLER_NAME (decl)); + write_source_name (DECL_ASSEMBLER_NAME (decl)); } else if (DECL_DECLARES_FUNCTION_P (decl)) { @@ -4370,13 +4315,21 @@ mangle_decomp (const tree decl, vec + inline void corge () + { + static auto [i, j] = a; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 } + static auto && [u, v, w] = Y{}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 } + ++u; + { + static auto && [u, v, w] = Y{}; // { dg-warning "structured bindings only available with" "" { target c++14_down } } + // { dg-warning "structured binding declaration can be 'static' only in" "" { target c++17_down } .-1 } + ++v; + } + } + } +} + +void (*p) () = &freddy; +void (*q) () = N::M::corge<3>; + +// { dg-final { scan-assembler "_ZZ6freddyvEDC1i1jE" } } +// { dg-final { scan-assembler "_ZZ6freddyvEDC1i1jE_0" } } +// { dg-final { scan-assembler "_ZZ6freddyvEDC1i1jE_1" } } +// { dg-final { scan-assembler "_ZZ6freddyvEDC1k1lE" } } +// { dg-final { scan-assembler "_ZZ6freddyvEDC1k1lE_0" } } +// { dg-final { scan-assembler "_ZZ6freddyvEDC1k1lE_1" } } +// { dg-final { scan-assembler "_ZZN1N1M5corgeILi3EEEvvEDC1i1jE" } } +// { dg-final { scan-assembler "_ZZN1N1M5corgeILi3EEEvvEDC1u1v1wE" } } +// { dg-final { scan-assembler "_ZZN1N1M5corgeILi3EEEvvEDC1u1v1wE_0" } } +// { dg-final { scan-assembler "_ZGVZ6freddyvEDC1i1jE" } } +// { dg-final { scan-assembler "_ZGVZ6freddyvEDC1i1jE_0" } } +// { dg-final { scan-assembler "_ZGVZ6freddyvEDC1i1jE_1" } } +// { dg-final { scan-assembler "_ZGVZ6freddyvEDC1k1lE" } } +// { dg-final { scan-assembler "_ZGVZ6freddyvEDC1k1lE_0" } } +// { dg-final { scan-assembler "_ZGVZ6freddyvEDC1k1lE_1" } } +// { dg-final { scan-assembler "_ZGVZN1N1M5corgeILi3EEEvvEDC1i1jE" } } +// { dg-final { scan-assembler "_ZGRZN1N1M5corgeILi3EEEvvEDC1u1v1wE_" } } +// { dg-final { scan-assembler "_ZGRZN1N1M5corgeILi3EEEvvEDC1u1v1wE_0_" } }