From patchwork Tue Feb 21 12:01:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59996 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1833423wrn; Tue, 21 Feb 2023 04:29:27 -0800 (PST) X-Google-Smtp-Source: AK7set8TfNgbveaGUILhWqlKSUS1MgSsMeqvLqGbnkrBOqiqc30D5hDyeAk/RxQAZP1N2rRHHn0p X-Received: by 2002:a17:907:6d99:b0:8d7:153:1486 with SMTP id sb25-20020a1709076d9900b008d701531486mr5381026ejc.20.1676982567551; Tue, 21 Feb 2023 04:29:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676982567; cv=none; d=google.com; s=arc-20160816; b=ADHC4n237y1SOfRcqto/wxHQ69UaTymn1U8kgtiqBUN6QqOwusqG7SyBXHcXMgNHjJ 27aSgyllKQvvKe8z+Qesz/C+wsliTLtz/QynU8wvu1yoNcNsVk3HKCzeCicQXJnnadW/ 66QsrrWOGz3R9d2vGe+s13mJjDU3Og1mhcRCN+STC3573Qso16+EY8zluuS/4GKZOsOg RXykGIeVAyS3a9gIFxnWhMEh2unOqYUars+yVHHwoVy00KiAS0zkNrfLgkP7vU+h2L8z txwNcosMWJoPrAqDZYhyY+7qt3U5v0b/OI3xvItx2vEV7K7HpTNw/CALhJyICONpSuhj Rb4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=2kEV19E2Nn/fFi2EPMgG7Z9TklCsrbLhj0NdrpFT5u4=; b=QVBW+xxVXunL49TsSJ2AT1gky2BEXpcqASJ2feU9FfP9jZokIiOh+ustb4c4N0ucMJ aiiZMi23emtLnD04J0f3x5kEhYAj/VqiydLXcTDWC6hLRjCkRgGyZSXchWUED5ns3RfW aEmKHo9eQDiP9Chg9O/2BSzM3zm/8BNFVwTuW1XYXVpjjSf7w+y6Mqt3IKxFaCOpLviY xy8pRl1Xy9NDmOYdPIaMHVuzwJhm9Eh0pXfrkr8SJjjlaHMxxN9TAnYrmVUKEVVM98wz Pj0l8SyNcc2ro0H1JadndLYS6sXVmp/P4MB7ogpxOLEYKAlATWObzUUZO8/9e48qHos+ S+FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=E44wJ4qf; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ew6-20020a170907950600b008c9eb8758desi8636807ejc.915.2023.02.21.04.29.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:29:27 -0800 (PST) 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=@embecosm.com header.s=google header.b=E44wJ4qf; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C793385B536 for ; Tue, 21 Feb 2023 12:11:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 22BDD383E2AE for ; Tue, 21 Feb 2023 12:03:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22BDD383E2AE Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x42c.google.com with SMTP id r7so3991163wrz.6 for ; Tue, 21 Feb 2023 04:03:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=2kEV19E2Nn/fFi2EPMgG7Z9TklCsrbLhj0NdrpFT5u4=; b=E44wJ4qf5ZuXm7EHmlMS+iDPOwR/b137BG0a9lXt6jZucvULhJ3gbJnRe/WGWYehbm 49ON/WSUude38YInImUlTpdOwSsJJsSoAW2l6uWIxvb4Toms4geYWKBIA/KmjjwETBMU iKKC0yVa2cJlJ33oYQ4yqn/Bj29WWJkI8SJZejtKahZkQdPI86w/F9UflmgZLsEDy+kU TeZJ0B67XJwp+fxN9Ngw+YvXwcQ62B+EFKNCsENVuT+02H/ERS2K3ryntqfAHfw36FSq yTbN6+MEmLYCHaM8Texe2U6/MhHelNNKyaaVtvRjXDrEd/bsjmpeAfA/oq9F21R3meUF kENw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to: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=2kEV19E2Nn/fFi2EPMgG7Z9TklCsrbLhj0NdrpFT5u4=; b=BiECCGtEil1pR7ws9UZY1P/2hL1WL9K225gbCPCPsP67eUmPbQwf2X/E0N3Xp76agc ptiKakRjR4O6ybTQwOTzDT9x4flK14E63V15GGxNElvSjOCxuePhupm06p7OwIWLE1R3 Df/XbsprwJiYQoXQ/6V7SU0UO0WkRslgFEH23s+ahEwPky6TV4aaKOYqj3kNCosqVXT3 ymkMUCk3B1Way97QzKmDA9o0rnjdiJJvjjXF22r2KPRfxxJJrm0xysaYVCnF+L8Kfq/I BQxCNhaNlKL3T1CWJ+VS3Ot4L8sS+bwhdEMjqAA78AQYaBVpQQSU3ywMa0joJZQNvsRh +jAg== X-Gm-Message-State: AO0yUKXQu2KzoT/yUkuU6lV9CJBWU5DgYqHYXflrUhNHrAZDZyiwY63w bVumCWgOrsk9DR3yvEsIz5NfH2fmjpqaoHbUmA== X-Received: by 2002:a5d:47c9:0:b0:2bf:bf05:85ac with SMTP id o9-20020a5d47c9000000b002bfbf0585acmr2984720wrc.23.1676981029478; Tue, 21 Feb 2023 04:03:49 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.03.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:48 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 033/103] gccrs: Support outer attribute handling on trait items just like normal items Date: Tue, 21 Feb 2023 13:01:23 +0100 Message-Id: <20230221120230.596966-34-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com> References: <20230221120230.596966-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-15.0 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758443672838932658?= X-GMAIL-MSGID: =?utf-8?q?1758443672838932658?= From: Philip Herron gcc/rust/ChangeLog: * hir/rust-ast-lower-base.h (class ItemWrapper): New class. * hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_outer_attributes): Use `ItemWrapper` class. (ASTLoweringBase::handle_doc_item_attribute): Likewise. (ASTLoweringBase::handle_lang_item_attribute): Likewise. * hir/rust-ast-lower-implitem.h: Check outer attributes on items. * hir/tree/rust-hir-item.h: Add `get_trait_locus` methods. * hir/tree/rust-hir.h: Likewise. * util/rust-hir-map.h: Add defId mappings and associated functions. * util/rust-hir-map.cc (Mappings::insert_defid_mapping): Implement insertion to said mappings. (Mappings::lookup_trait_item_defid): And looking up said mappings. --- gcc/rust/hir/rust-ast-lower-base.cc | 6 +++--- gcc/rust/hir/rust-ast-lower-base.h | 30 +++++++++++++++++++++++--- gcc/rust/hir/rust-ast-lower-implitem.h | 14 ++++++------ gcc/rust/hir/tree/rust-hir-item.h | 6 ++++++ gcc/rust/hir/tree/rust-hir.h | 4 +++- gcc/rust/util/rust-hir-map.cc | 24 +++++++++++++++++++++ gcc/rust/util/rust-hir-map.h | 3 +++ 7 files changed, 72 insertions(+), 15 deletions(-) diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index a4bcfd6c4b4..098014ff92b 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -824,7 +824,7 @@ ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers) } void -ASTLoweringBase::handle_outer_attributes (const HIR::Item &item) +ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item) { for (const auto &attr : item.get_outer_attrs ()) { @@ -855,7 +855,7 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item) } void -ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item, +ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &item, const AST::Attribute &attr) { auto simple_doc_comment = attr.has_attr_input () @@ -878,7 +878,7 @@ ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item, } void -ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item, +ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item, const AST::Attribute &attr) { auto &literal = static_cast (attr.get_attr_input ()); diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index f08e3185451..adcbc5d2f69 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -29,6 +29,30 @@ namespace Rust { namespace HIR { +// proxy class so we can do attribute checking on items and trait items +class ItemWrapper +{ +public: + ItemWrapper (const HIR::Item &item) + : mappings (item.get_mappings ()), locus (item.get_locus ()), + outer_attrs (item.get_outer_attrs ()) + {} + + ItemWrapper (const HIR::TraitItem &item) + : mappings (item.get_mappings ()), locus (item.get_trait_locus ()), + outer_attrs (item.get_outer_attrs ()) + {} + + const Analysis::NodeMapping &get_mappings () const { return mappings; } + Location get_locus () const { return locus; } + const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } + +private: + const Analysis::NodeMapping &mappings; + Location locus; + const AST::AttrVec &outer_attrs; +}; + // base class to allow derivatives to overload as needed class ASTLoweringBase : public AST::ASTVisitor { @@ -264,12 +288,12 @@ protected: HIR::FunctionQualifiers lower_qualifiers (const AST::FunctionQualifiers &qualifiers); - void handle_outer_attributes (const HIR::Item &item); + void handle_outer_attributes (const ItemWrapper &item); - void handle_lang_item_attribute (const HIR::Item &item, + void handle_lang_item_attribute (const ItemWrapper &item, const AST::Attribute &attr); - void handle_doc_item_attribute (const HIR::Item &item, + void handle_doc_item_attribute (const ItemWrapper &item, const AST::Attribute &attr); bool is_known_attribute (const std::string &attribute_path) const; diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index 69436543825..709bb573046 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -305,16 +305,14 @@ public: if (resolver.translated != nullptr) { - // FIXME + auto id = resolver.translated->get_mappings ().get_hirid (); + auto defid = resolver.translated->get_mappings ().get_defid (); + auto locus = resolver.translated->get_trait_locus (); - // auto id = resolver.translated->get_mappings ().get_hirid (); - // auto defid = resolver.translated->get_mappings ().get_defid (); - // auto locus = resolver.translated->get_locus (); - - // resolver.handle_outer_attributes (*resolver.translated); + resolver.handle_outer_attributes (*resolver.translated); resolver.mappings->insert_hir_trait_item (resolver.translated); - // resolver.mappings->insert_location (id, locus); - // resolver.mappings->insert_defid_mapping (defid, resolver.item_cast); + resolver.mappings->insert_location (id, locus); + resolver.mappings->insert_defid_mapping (defid, resolver.translated); } return resolver.translated; diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 8d371e00b98..7f665159572 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -2394,6 +2394,8 @@ public: return outer_attrs; } + Location get_trait_locus () const override { return get_locus (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemFunc *clone_trait_item_impl () const override @@ -2480,6 +2482,8 @@ public: return outer_attrs; } + Location get_trait_locus () const override { return get_locus (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemConst *clone_trait_item_impl () const override @@ -2567,6 +2571,8 @@ public: return outer_attrs; } + Location get_trait_locus () const override { return get_locus (); } + protected: // Clone function implementation as (not pure) virtual method TraitItemType *clone_trait_item_impl () const override diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 2614ef85b29..aa305f1590c 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -796,7 +796,9 @@ public: virtual const std::string trait_identifier () const = 0; - const Analysis::NodeMapping get_mappings () const { return mappings; } + const Analysis::NodeMapping &get_mappings () const { return mappings; } + + virtual Location get_trait_locus () const = 0; virtual TraitItemKind get_item_kind () const = 0; diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 7ea782ed4c4..4aeb7f5ae18 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -316,6 +316,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item) rust_assert (lookup_defid (id) == nullptr); rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr); + rust_assert (lookup_trait_item_defid (id) == nullptr); defIdMappings[id] = item; insert_local_defid_mapping (crate_num, local_def_id, item); @@ -331,6 +332,29 @@ Mappings::lookup_defid (DefId id) return it->second; } +void +Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item) +{ + CrateNum crate_num = id.crateNum; + LocalDefId local_def_id = id.localDefId; + + rust_assert (lookup_defid (id) == nullptr); + rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr); + rust_assert (lookup_trait_item_defid (id) == nullptr); + + defIdTraitItemMappings[id] = item; +} + +HIR::TraitItem * +Mappings::lookup_trait_item_defid (DefId id) +{ + auto it = defIdTraitItemMappings.find (id); + if (it == defIdTraitItemMappings.end ()) + return nullptr; + + return it->second; +} + void Mappings::insert_hir_item (HIR::Item *item) { diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index addd9efc3b0..13cae717031 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -104,6 +104,8 @@ public: void insert_defid_mapping (DefId id, HIR::Item *item); HIR::Item *lookup_defid (DefId id); + void insert_defid_mapping (DefId id, HIR::TraitItem *item); + HIR::TraitItem *lookup_trait_item_defid (DefId id); void insert_local_defid_mapping (CrateNum crateNum, LocalDefId id, HIR::Item *item); @@ -307,6 +309,7 @@ private: std::map ast_crate_mappings; std::map hir_crate_mappings; std::map defIdMappings; + std::map defIdTraitItemMappings; std::map> localDefIdMappings; std::map hirModuleMappings;