From patchwork Mon Sep 23 23:43:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1988722 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=JFDlTPRS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XCKMs3HvPz1xsw for ; Tue, 24 Sep 2024 09:44:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C806385DDCF for ; Mon, 23 Sep 2024 23:44:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 1797C385DC1B for ; Mon, 23 Sep 2024 23:44:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1797C385DC1B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1797C385DC1B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727135043; cv=none; b=wdPmzPlDZm1ugjoVwHgoEIhfZMQvfr8hiJE6oVXzb4Qi1d16xAiofTksC1XUq9ohY/Q2hGjn6s4kKavmTPhAfcRG6Jtqakzw1EtX/AlinrRiPCb9eN25xOpenHlnk8djnoOA8maUNjSv/96tDC97HYM8i4XjZ1D7hIum1OrhoQE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727135043; c=relaxed/simple; bh=sLu/38Q3Mw+sOWnu2deFw5BL+30CdDgLM/3djfHN0Yw=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=ZSHd0+Ge7UNN9EzE4NlClRWegZpZWu1T23wSdTJ6mqJthGOSbwnHwoidAqhcIz8/VkMKiIpyX2RS0PI0kW68OnMofL6bIqGV6Knd6nhyqnfKkFraMBICfASX2/waVDg0NEf4eH2UuvOfOGowjRXtfsMXjA5yy5D10PczScnsAsg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-718e55bca81so607857b3a.2 for ; Mon, 23 Sep 2024 16:44:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727135040; x=1727739840; darn=gcc.gnu.org; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=rdbwhn2e4KGMo48efTOohHjOEdOlaF9vBNxqHr+rkXI=; b=JFDlTPRSGdXne76B4zjAxYApBzVzGCRCaqAQT4dyPJkGxsdOQPchM+ekWbRmspou6F x0+rdxQ8gl2zWwmyufm4g4lpsSrq/fRoXyc38F4GEXjTffN3R3SDutLoW+iFZR4hAvBZ Ef+I5/17qqexmuRbsVHpcN0kmG913E3JXtU+9Iszj2FiWDZvfEjHi7hBwq6MO5Lx9ry9 dpZ07ecJiR/4AhMvefx0JPBk1LKUTr1rNAxthxv02mwjobd5V5CZunkmYtxHDAmAj123 /OFfmvxvJ1/jQ2SRd6hMMNNlKdfoHbPVvgerjK/De4o/Yxzq8o/lRfVieCnld2mb8y8K jLyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727135040; x=1727739840; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=rdbwhn2e4KGMo48efTOohHjOEdOlaF9vBNxqHr+rkXI=; b=PWiKWoKs2Q4LC25eUQ5AH2OI4tl+Uca0fwJVCpgOcOnQYeQ+3Wl+fBeiLnm837FVbR F6j6TsYAC+7C4OsXFXKOPRL8jTl2/pLXuIkt3/GKecXe2KLgcOl4/WAOR0ShSFWKy4cr pScGnPVdli5IBZ1ydmUuk8sonpxGbPbGKG67Rg6NkP8IzFXgo5pvD/h0YLbQoZo3Jut5 hPToC72j4dThfMJMFRIuWBuAZaqTkX2E6GqJ8akJN4vqjh60eSelEk5W5i8McBl/WhlH q+3jcpjEQ5TBTjNnCe8Bkj8p0HGc6wybYooNc7EPGSKFLV8NH0cBxNKfTCrhjsRAdtot dGVw== X-Gm-Message-State: AOJu0Yytk0qRZhFXgazwoPbjuyN3lcBW+1l5Mcu9sL1gB+qPLrNPACXt S0HRRbmWgti3k1HtZ1zkwTVq/sJme7nv0BF5tUSGEyL88Xx2A9vDdiQN8Q== X-Google-Smtp-Source: AGHT+IHOVA6aSTsLxafJ619xE6rFhvdBIIBDnuzhKYXRrKWG6fvOBYFqwwGKmyiPCQcCfrgDaTSEjQ== X-Received: by 2002:a05:6a00:2d02:b0:70d:2289:4c55 with SMTP id d2e1a72fcca58-7199ca343a1mr7688572b3a.5.1727135039811; Mon, 23 Sep 2024 16:43:59 -0700 (PDT) Received: from Thaum. (163-47-68-2.ipv4.originbroadband.com.au. [163.47.68.2]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71afc8345b4sm151813b3a.29.2024.09.23.16.43.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 16:43:59 -0700 (PDT) Message-ID: <66f1fd3f.a70a0220.10bbdd.083b@mx.google.com> X-Google-Original-Message-ID: Date: Tue, 24 Sep 2024 09:43:55 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH 02/10] c++: Update decl_linkage for C++11 References: <66f1fce5.170a0220.150b6f.03f5@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <66f1fce5.170a0220.150b6f.03f5@mx.google.com> X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.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 This patch intends no change in functionality apart from the mangling difference noted; more tests are in patch 4 of this series, which adds a way to actually check what the linkage of decl_linkage provides more directly. Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? -- >8 -- Currently modules code uses a variety of ad-hoc methods to attempt to determine whether an entity has internal linkage, which leads to inconsistencies and some correctness issues as different edge cases are neglected. While investigating this I discovered 'decl_linkage', but it doesn't seem to have been updated to account for the C++11 clarification that all entities declared in an anonymous namespace are internal. I'm not convinced that even in C++98 it was intended that e.g. types in anonymous namespaces should be external, but some tests in the testsuite rely on this, so for compatibility I restricted those modifications to C++11 and later. This should have relatively minimal impact as not much seems to actually rely on decl_linkage, but does change the mangling of symbols in anonymous namespaces slightly. Previously, we had namespace { int x; // mangled as '_ZN12_GLOBAL__N_11xE' static int y; // mangled as '_ZN12_GLOBAL__N_1L1yE' } but with this patch the x is now mangled like y (with the extra 'L'). For contrast, Clang currently mangles neither x nor y with the 'L'. Since this only affects internal-linkage entities I don't believe this should break ABI in any observable fashion. gcc/cp/ChangeLog: * name-lookup.cc (do_namespace_alias): Propagate TREE_PUBLIC for namespace aliases. * tree.cc (decl_linkage): Update rules for C++11. gcc/testsuite/ChangeLog: * g++.dg/modules/mod-sym-4.C: Update test to account for non-static internal-linkage variables new mangling. Signed-off-by: Nathaniel Shead --- gcc/cp/name-lookup.cc | 1 + gcc/cp/tree.cc | 92 +++++++++++++++--------- gcc/testsuite/g++.dg/modules/mod-sym-4.C | 4 +- 3 files changed, 60 insertions(+), 37 deletions(-) diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index c7a693e02d5..50e169eca43 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -6610,6 +6610,7 @@ do_namespace_alias (tree alias, tree name_space) DECL_NAMESPACE_ALIAS (alias) = name_space; DECL_EXTERNAL (alias) = 1; DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); + TREE_PUBLIC (alias) = TREE_PUBLIC (DECL_CONTEXT (alias)); set_originating_module (alias); pushdecl (alias); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index f43febed124..28e14295de4 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -5840,7 +5840,7 @@ char_type_p (tree type) || same_type_p (type, wchar_type_node)); } -/* Returns the kind of linkage associated with the indicated DECL. Th +/* Returns the kind of linkage associated with the indicated DECL. The value returned is as specified by the language standard; it is independent of implementation details regarding template instantiation, etc. For example, it is possible that a declaration @@ -5857,53 +5857,75 @@ decl_linkage (tree decl) linkage first, and then transform that into a concrete implementation. */ - /* Things that don't have names have no linkage. */ - if (!DECL_NAME (decl)) - return lk_none; + /* An explicit type alias has no linkage. */ + if (TREE_CODE (decl) == TYPE_DECL + && !DECL_IMPLICIT_TYPEDEF_P (decl) + && !DECL_SELF_REFERENCE_P (decl)) + { + /* But this could be a typedef name for linkage purposes, in which + case we're interested in the linkage of the main decl. */ + if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))) + decl = TYPE_MAIN_DECL (TREE_TYPE (decl)); + else + return lk_none; + } - /* Fields have no linkage. */ - if (TREE_CODE (decl) == FIELD_DECL) + /* Namespace-scope entities with no name usually have no linkage. */ + if (NAMESPACE_SCOPE_P (decl) + && (!DECL_NAME (decl) || IDENTIFIER_ANON_P (DECL_NAME (decl)))) + { + if (TREE_CODE (decl) == TYPE_DECL && !TYPE_ANON_P (TREE_TYPE (decl))) + /* This entity has a typedef name for linkage purposes. */; + else if (TREE_CODE (decl) == NAMESPACE_DECL && cxx_dialect >= cxx11) + /* An anonymous namespace has internal linkage since C++11. */ + return lk_internal; + else + return lk_none; + } + + /* Fields and parameters have no linkage. */ + if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == PARM_DECL) return lk_none; - /* Things in local scope do not have linkage. */ + /* Things in block scope do not have linkage. */ if (decl_function_context (decl)) return lk_none; + /* Things in class scope have the linkage of their owning class. */ + if (tree ctype = DECL_CLASS_CONTEXT (decl)) + return decl_linkage (TYPE_NAME (ctype)); + + /* Anonymous namespaces don't provide internal linkage in C++98, + but otherwise consider such declarations to be internal. */ + if (cxx_dialect >= cxx11 && decl_internal_context_p (decl)) + return lk_internal; + + /* Templates don't properly propagate TREE_PUBLIC, consider the + template result instead. Any template that isn't a variable + or function must be external linkage by this point. */ + if (TREE_CODE (decl) == TEMPLATE_DECL) + { + decl = DECL_TEMPLATE_RESULT (decl); + if (!decl || !VAR_OR_FUNCTION_DECL_P (decl)) + return lk_external; + } + /* Things that are TREE_PUBLIC have external linkage. */ if (TREE_PUBLIC (decl)) return lk_external; - /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants, - check one of the "clones" for the real linkage. */ - if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl) - && DECL_CHAIN (decl) - && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl))) - return decl_linkage (DECL_CHAIN (decl)); - - if (TREE_CODE (decl) == NAMESPACE_DECL) + /* All types have external linkage in C++98, since anonymous namespaces + didn't explicitly confer internal linkage. */ + if (TREE_CODE (decl) == TYPE_DECL && cxx_dialect < cxx11) return lk_external; - /* Linkage of a CONST_DECL depends on the linkage of the enumeration - type. */ - if (TREE_CODE (decl) == CONST_DECL) - return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl))); - - /* Members of the anonymous namespace also have TREE_PUBLIC unset, but - are considered to have external linkage for language purposes, as do - template instantiations on targets without weak symbols. DECLs really - meant to have internal linkage have DECL_THIS_STATIC set. */ - if (TREE_CODE (decl) == TYPE_DECL) + /* Variables or function decls not marked as TREE_PUBLIC might still + be external linkage, such as for template instantiations on targets + without weak symbols, decls referring to internal-linkage entities, + or compiler-generated entities; in such cases, decls really meant to + have internal linkage will have DECL_THIS_STATIC set. */ + if (VAR_OR_FUNCTION_DECL_P (decl) && !DECL_THIS_STATIC (decl)) return lk_external; - if (VAR_OR_FUNCTION_DECL_P (decl)) - { - if (!DECL_THIS_STATIC (decl)) - return lk_external; - - /* Static data members and static member functions from classes - in anonymous namespace also don't have TREE_PUBLIC set. */ - if (DECL_CLASS_CONTEXT (decl)) - return lk_external; - } /* Everything else has internal linkage. */ return lk_internal; diff --git a/gcc/testsuite/g++.dg/modules/mod-sym-4.C b/gcc/testsuite/g++.dg/modules/mod-sym-4.C index fbf54d00171..14fef4fe253 100644 --- a/gcc/testsuite/g++.dg/modules/mod-sym-4.C +++ b/gcc/testsuite/g++.dg/modules/mod-sym-4.C @@ -12,9 +12,9 @@ static void addone () {} static int x = 5; namespace { -// { dg-final { scan-assembler {_ZN12_GLOBAL__N_14frobEv:} } } +// { dg-final { scan-assembler {_ZN12_GLOBAL__N_1L4frobEv:} } } void frob () {} -// { dg-final { scan-assembler {_ZN12_GLOBAL__N_11yE:} } } +// { dg-final { scan-assembler {_ZN12_GLOBAL__N_1L1yE:} } } int y = 2; struct Bill {