From patchwork Thu Aug 1 14:57:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 95025 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DFC933860744 for ; Thu, 1 Aug 2024 15:26:40 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id 215583858289 for ; Thu, 1 Aug 2024 14:59:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 215583858289 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 215583858289 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::52f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524434; cv=none; b=eW4HPt1eEQI0cVqumx6/83STdRwl/CT6G0ghxtuq7SQuWhlBKCe942KVJ7JOQzxLSRSo1ORuOmFeqZ7ttb3h8mFHShmFYdDqG09xT3LPuvKiCA/7OfgswVM1XqDmABdGY2ytxJlyNCGCOZl/vLyW6WA+72pX20Ed1X+SHV54rvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524434; c=relaxed/simple; bh=+Nn9ng/QdUnLZvuLUqdLtFX/QJjvrcVEAdq/hHRqyxg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CO2v8N2ftWtda4dlop6wH2fudddLXpueBpj0CncZtguQOT+LMGR28/ARAV4J6QnhPiJc0tJW/GGF3mawzDXqGESypQIw6OT516ZzvFtK1YiiGd9ivso1bMOKcYUor4UK4QU7j1cyYQqnTHYJEP5L64j6g+kBoFlpVxaICfI5Rdo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-5b7b6a30454so853420a12.2 for ; Thu, 01 Aug 2024 07:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524381; x=1723129181; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ALrcuTydNQZtWXqvOQWVEfCq+UEQhrT4CP2SCUbYWu0=; b=AhoAeP3wPYRIo4UfnbM4WsjGW9iyY/PVhfoo0PuIaWf7rRwjClLQUH0HrJsoJwvBwj mJPYPQ79U5dGPHsgirVqjAJEZhnD+GmCJeE4XfxXud2rLw0LhBTQnU+oUgo2OqGhXm5B 0WFHcAokVcR3cuaBb7WJGGqC3Q7DGVtnNi8dTbrwW2ovnfoEUlEmNnrE8T93eluhOCws hr5LcA7Gmca28XlYEQvsRatJntjS5uv598oSMT7653ew5nT48OrIPG3UsKz9gkvHtEdH Y4+CMnSc0N7nlZkQmKN4t91eJU4yLJBilDecouCU0+g1Fs5IB/nGYok+axwkvrxSKIxx p70g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524381; x=1723129181; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ALrcuTydNQZtWXqvOQWVEfCq+UEQhrT4CP2SCUbYWu0=; b=OUX16aRU+7wTK1HLvAQD9ZfMqI6GTUp/UO6c7qen5NtOReNbUyYQ6J0hYsvwK1JLNv tNc6gCvn9JcKFwkd8ahTAf0keJQCs6cvBzfd4Y+LtzrxnXHAoHPkH7OYsr/q7BMyd5OT EeSgFha34fjYE8SdFmzTietCJ4vcV/veP+rTDBwWC8p0SAtHnji9bSg+++TVWnmFmgyw La5gFGdqcBlTn1HL72ZQwQz9FIVYks+A7IAm+zAfIPMk6SN/EN/Y3J2B6+Bj0OItH4oJ PFsLd/ozlFau2TShTHnarClaWn/zeVwDvw5Pue7OsJKySaLDWy1eiIBoNKTL9sDBBPX6 6Zzg== X-Gm-Message-State: AOJu0YwwK9DDcm2th3n6s5CZS92BJMvk0oxNLJLEmZKcTWZSvVlo+clh x/ut0l7vg9OPVI6be0QB5oWZoXL5+sCMROoHRDrMoVnCU0BysQ4uvEIV9NrDwikA6vjFqKsn2hA 4/2Le X-Google-Smtp-Source: AGHT+IFp4nPlewh4HpIlgMcGnDyHva4vPPDYVFsLZDCG0X3NUWQIN4UPC4PSOgsFzVVm3/0zvhDqRA== X-Received: by 2002:aa7:d70d:0:b0:5a2:d970:114c with SMTP id 4fb4d7f45d1cf-5b7f340d77emr463783a12.4.1722524381257; Thu, 01 Aug 2024 07:59:41 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.59.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:41 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [PATCH 077/125] gccrs: Make globbing definition shadowable by default Date: Thu, 1 Aug 2024 16:57:13 +0200 Message-ID: <20240801145809.366388-79-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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~patchwork=sourceware.org@gcc.gnu.org From: Pierre-Emmanuel Patry Elements from glob use declaration shall be shadowable by default. gcc/rust/ChangeLog: * resolve/rust-forever-stack.h: Add a new function prototype to insert a shadowable definition. * resolve/rust-forever-stack.hxx: Add the new insert_shadowable function to insert shadowable definition into the forever stack. * resolve/rust-name-resolution-context.cc (NameResolutionContext::insert_shadowable): Likewise with the name resolution context. * resolve/rust-name-resolution-context.h: Add name resolution context insert_shadowable member function prototype. * resolve/rust-toplevel-name-resolver-2.0.cc (GlobbingVisitor::visit): Insert shadowable definition into the forever stack for glob use declaration. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/resolve/rust-forever-stack.h | 15 ++++++++ gcc/rust/resolve/rust-forever-stack.hxx | 10 ++++++ .../resolve/rust-name-resolution-context.cc | 19 ++++++++++ .../resolve/rust-name-resolution-context.h | 3 ++ .../rust-toplevel-name-resolver-2.0.cc | 35 ++++++++++--------- 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h index 01371fc7bcd..bba5875d435 100644 --- a/gcc/rust/resolve/rust-forever-stack.h +++ b/gcc/rust/resolve/rust-forever-stack.h @@ -437,6 +437,21 @@ public: */ tl::expected insert (Identifier name, NodeId id); + /** + * Insert a new shadowable definition in the innermost `Rib` in this stack + * + * @param name The name of the definition + * @param id Its NodeId + * + * @return `DuplicateNameError` if that node was already present in the Rib, + * the node's `NodeId` otherwise. + * + * @aborts if there are no `Rib`s inserted in the current map, this function + * aborts the program. + */ + tl::expected insert_shadowable (Identifier name, + NodeId id); + /** * Insert a new definition at the root of this stack * diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx index a2fdce98362..008adff4676 100644 --- a/gcc/rust/resolve/rust-forever-stack.hxx +++ b/gcc/rust/resolve/rust-forever-stack.hxx @@ -119,6 +119,16 @@ ForeverStack::insert (Identifier name, NodeId node) Rib::Definition::NonShadowable (node)); } +template +tl::expected +ForeverStack::insert_shadowable (Identifier name, NodeId node) +{ + auto &innermost_rib = peek (); + + return insert_inner (innermost_rib, name.as_string (), + Rib::Definition::Shadowable (node)); +} + template tl::expected ForeverStack::insert_at_root (Identifier name, NodeId node) diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc b/gcc/rust/resolve/rust-name-resolution-context.cc index 0340d28f127..e8529b70bcb 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.cc +++ b/gcc/rust/resolve/rust-name-resolution-context.cc @@ -45,6 +45,25 @@ NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns) } } +tl::expected +NameResolutionContext::insert_shadowable (Identifier name, NodeId id, + Namespace ns) +{ + switch (ns) + { + case Namespace::Values: + return values.insert_shadowable (name, id); + case Namespace::Types: + return types.insert_shadowable (name, id); + case Namespace::Macros: + return macros.insert_shadowable (name, id); + case Namespace::Labels: + default: + // return labels.insert (name, id); + rust_unreachable (); + } +} + void NameResolutionContext::map_usage (Usage usage, Definition definition) { diff --git a/gcc/rust/resolve/rust-name-resolution-context.h b/gcc/rust/resolve/rust-name-resolution-context.h index 8702900d0f3..74f110d54de 100644 --- a/gcc/rust/resolve/rust-name-resolution-context.h +++ b/gcc/rust/resolve/rust-name-resolution-context.h @@ -171,6 +171,9 @@ public: tl::expected insert (Identifier name, NodeId id, Namespace ns); + tl::expected + insert_shadowable (Identifier name, NodeId id, Namespace ns); + /** * Run a lambda in a "scoped" context, meaning that a new `Rib` will be pushed * before executing the lambda and then popped. This is useful for all kinds diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc index 501204174f2..7f4169a4d8e 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -36,71 +36,72 @@ void GlobbingVisitor::visit (AST::Module &module) { if (module.get_visibility ().is_public ()) - ctx.insert (module.get_name (), module.get_node_id (), Namespace::Types); + ctx.insert_shadowable (module.get_name (), module.get_node_id (), + Namespace::Types); } void GlobbingVisitor::visit (AST::MacroRulesDefinition ¯o) { if (macro.get_visibility ().is_public ()) - ctx.insert (macro.get_rule_name (), macro.get_node_id (), - Namespace::Macros); + ctx.insert_shadowable (macro.get_rule_name (), macro.get_node_id (), + Namespace::Macros); } void GlobbingVisitor::visit (AST::Function &function) { if (function.get_visibility ().is_public ()) - ctx.insert (function.get_function_name (), function.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (function.get_function_name (), + function.get_node_id (), Namespace::Values); } void GlobbingVisitor::visit (AST::StaticItem &static_item) { if (static_item.get_visibility ().is_public ()) - ctx.insert (static_item.get_identifier (), static_item.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (static_item.get_identifier (), + static_item.get_node_id (), Namespace::Values); } void GlobbingVisitor::visit (AST::StructStruct &struct_item) { if (struct_item.get_visibility ().is_public ()) - ctx.insert (struct_item.get_identifier (), struct_item.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (struct_item.get_identifier (), + struct_item.get_node_id (), Namespace::Values); } void GlobbingVisitor::visit (AST::TupleStruct &tuple_struct) { if (tuple_struct.get_visibility ().is_public ()) - ctx.insert (tuple_struct.get_identifier (), tuple_struct.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (tuple_struct.get_identifier (), + tuple_struct.get_node_id (), Namespace::Values); } void GlobbingVisitor::visit (AST::Enum &enum_item) { if (enum_item.get_visibility ().is_public ()) - ctx.insert (enum_item.get_identifier (), enum_item.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (enum_item.get_identifier (), + enum_item.get_node_id (), Namespace::Values); } void GlobbingVisitor::visit (AST::Union &union_item) { if (union_item.get_visibility ().is_public ()) - ctx.insert (union_item.get_identifier (), union_item.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (union_item.get_identifier (), + union_item.get_node_id (), Namespace::Values); } void GlobbingVisitor::visit (AST::ConstantItem &const_item) { if (const_item.get_visibility ().is_public ()) - ctx.insert (const_item.get_identifier (), const_item.get_node_id (), - Namespace::Values); + ctx.insert_shadowable (const_item.get_identifier (), + const_item.get_node_id (), Namespace::Values); } void