From patchwork Tue Feb 21 12:01:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59993 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1832685wrn; Tue, 21 Feb 2023 04:27:21 -0800 (PST) X-Google-Smtp-Source: AK7set/9ad3nGnst8H3LaKQ3WDftFULLt/RmwNDqyijK2Qp+Yj9kpraq5RKuqTYMzJur4Gn1rP91 X-Received: by 2002:a17:907:76ad:b0:8b1:749f:b2bc with SMTP id jw13-20020a17090776ad00b008b1749fb2bcmr13488194ejc.11.1676982441898; Tue, 21 Feb 2023 04:27:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676982441; cv=none; d=google.com; s=arc-20160816; b=ExLYtXP2KSCCsUe4/hxhb1DdgD+vbW3mZX70QxQhLsQIRdP1SLnD1dImszP8aLgVzo Y/8PrwNNVr5yX+moghqopGQObguKrRVhzgAMa6TdYXSMJvNRZUv6kPVDlgExxYDee0ni U0VmrwgZ7VTWnrN8bpu3R1BXxeKqB0cXnm7eiuIq6CPwRZKLMm8rIfgpaCXJE03TCfB9 4X0yx+zCAQQ9Lxe3ZTIaOCERiJWnC2NrpihjED/OYZTN0Ax43hy5+TCRgvBGDivFhlxg nWdO03QTVL4NZUR4QImrBj2hSx3gPxo09Ez2qJuDTNI+T/FhuXJUJpuEC7J/pDTTYSXH JiPQ== 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=pf8ctTx7dyWfWEzGSCn3OZ8N1vvNrHRekpIg9uJGwGk=; b=GqtgywxCqyBEff06QU7EkLQ3+kzAwJRbifTaVv9Ij6bhrHp8DKI8Tmiiye1xnMkiPa 2XRrsDgiFgNZwP7whUxozcwgcNl2Jm5jU6Qj6W6gkmqsv+pH691+j4fRhN6wEgO9dxQY wwrCNLQT6E/smOtY6835HoEUvPtXaL6/7K8X4PT39XgUN+v2wzuxa/TGsfgsvulTJcsd G1gdRTtoho4FyyxIQQ1a9YGo+pghClqDS9LLi41InVmXldLec6rnimt3TS3s+yckgPn0 UYs0YmWxic0BoILoFgYLUVjpl8Ek4Sb7O3gjdkIq+uRbskK/WRc7pWhxcQYI4ne/M0q7 SwHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=Ir2S3svT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 20-20020a17090600d400b008dd491949c6si2940335eji.978.2023.02.21.04.27.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:27:21 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=Ir2S3svT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 90A7C383DB8B for ; Tue, 21 Feb 2023 12:10:43 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 76C02383E2A0 for ; Tue, 21 Feb 2023 12:03:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 76C02383E2A0 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-x42a.google.com with SMTP id c12so4162690wrw.1 for ; Tue, 21 Feb 2023 04:03:49 -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=pf8ctTx7dyWfWEzGSCn3OZ8N1vvNrHRekpIg9uJGwGk=; b=Ir2S3svTkhTFsRVC909/++CMp7PiAQ4VSJTPaan27/dcZYsBIXA7+gWRKr8j+9ongl iqOgJLYmAMoHQP+Ay5WaBICZs6Iz0EnozOk2zN3fPeVFPVl53SSJK3wDmyd1W9x/AY+n iix+ptXdaMNtfOWsd7K03ytRmwTPSyfPiYokN/orM5pF9G/gyLVrwuopCx/kCyP7UyO6 3ZAXGdRMv3MsAwUI95DwHfrFTQxI3WTRVuVYtjje+aD7zk+P5zhZcIcD5eimgXuqk3ta 4s/xugZnDnai4RVEzn4MF66vFWmXYJpfmdXLlArRaRZTt2jCWJFFs4JqL7UmjhreLOhB bDIA== 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=pf8ctTx7dyWfWEzGSCn3OZ8N1vvNrHRekpIg9uJGwGk=; b=wrg5sJwqmFuJSMDQLunil3FwyyMlCili1q819mnO3a/rv2yuPFFTEjXlYDG8y7sIsl ddKAEJeh/varkG8Qc1zH8tLbUCEhjKODYi0NBNY3w0nB4o2tFf+UYc4sh7a420Z7U6hN 0HmmGCgZFOpqppzWGVrGD1BdVm3w2v1ALt6ycWDKVgZS9pqv3kuaAts055sTVkMtpeYk aoCTrPx136lldIvxWfWakXrwHd4gLgAcmCswCZIH7oR37NsEKow2488S3+Qgr3Dl9O9t 4aXyUkOndFmd977jQS5a7VG3TrzUyfvjTHLcON3xH4vjIK5ULyJWUty+GF8R6bf10auL Z9Ng== X-Gm-Message-State: AO0yUKU8bXfr8cfdCXgK7y9EFBhcKQHgndPkwHGevfXvtSamxlRQJY0a u0Y15P4v3YfHDI5O4zk6kknoJAkBLzBJJD4nYw== X-Received: by 2002:adf:e308:0:b0:2c5:5687:5ed5 with SMTP id b8-20020adfe308000000b002c556875ed5mr4548967wrj.18.1676981028060; Tue, 21 Feb 2023 04:03:48 -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.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:47 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 031/103] gccrs: Refactor TraitResolver to not require a visitor Date: Tue, 21 Feb 2023 13:01:21 +0100 Message-Id: <20230221120230.596966-32-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=unavailable 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?1758443540565344082?= X-GMAIL-MSGID: =?utf-8?q?1758443540565344082?= From: Philip Herron gcc/rust/ChangeLog: * typecheck/rust-hir-trait-resolve.cc (TraitResolver::TraitResolver): Do not nullptr init `resolved_trait_reference` anymore. (TraitResolver::resolve_path): Simplify function and rename to... (TraitResolver::resolve_path_to_trait): ...this. (TraitResolver::lookup_path): Use new interface. * typecheck/rust-hir-trait-resolve.h (class TraitResolver): Do not inherit `HIRFullVisitor` class anymore. --- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 53 +++++++++----------- gcc/rust/typecheck/rust-hir-trait-resolve.h | 10 ++-- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index 22398b1fa8a..1b0bcaac79d 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -91,33 +91,42 @@ TraitResolver::Lookup (HIR::TypePath &path) return resolver.lookup_path (path); } -TraitResolver::TraitResolver () - : TypeCheckBase (), resolved_trait_reference (nullptr) -{} +TraitResolver::TraitResolver () : TypeCheckBase () {} -TraitReference * -TraitResolver::resolve_path (HIR::TypePath &path) +bool +TraitResolver::resolve_path_to_trait (const HIR::TypePath &path, + HIR::Trait **resolved) const { NodeId ref; if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (), &ref)) { rust_error_at (path.get_locus (), "Failed to resolve path to node-id"); - return &TraitReference::error_node (); + return false; } HirId hir_node = UNKNOWN_HIRID; if (!mappings->lookup_node_to_hir (ref, &hir_node)) { rust_error_at (path.get_locus (), "Failed to resolve path to hir-id"); - return &TraitReference::error_node (); + return false; } HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node); - rust_assert (resolved_item != nullptr); - resolved_item->accept_vis (*this); - rust_assert (resolved_trait_reference != nullptr); + rust_assert (resolved_item->get_item_kind () == HIR::Item::ItemKind::Trait); + *resolved = static_cast (resolved_item); + + return true; +} + +TraitReference * +TraitResolver::resolve_path (HIR::TypePath &path) +{ + HIR::Trait *resolved_trait_reference; + bool ok = resolve_path_to_trait (path, &resolved_trait_reference); + if (!ok) + return &TraitReference::error_node (); return resolve_trait (resolved_trait_reference); } @@ -237,26 +246,10 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) TraitReference * TraitResolver::lookup_path (HIR::TypePath &path) { - NodeId ref; - if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (), - &ref)) - { - rust_error_at (path.get_locus (), "Failed to resolve path to node-id"); - return &TraitReference::error_node (); - } - - HirId hir_node = UNKNOWN_HIRID; - if (!mappings->lookup_node_to_hir (ref, &hir_node)) - { - rust_error_at (path.get_locus (), "Failed to resolve path to hir-id"); - return &TraitReference::error_node (); - } - - HIR::Item *resolved_item = mappings->lookup_hir_item (hir_node); - - rust_assert (resolved_item != nullptr); - resolved_item->accept_vis (*this); - rust_assert (resolved_trait_reference != nullptr); + HIR::Trait *resolved_trait_reference; + bool ok = resolve_path_to_trait (path, &resolved_trait_reference); + if (!ok) + return &TraitReference::error_node (); TraitReference *tref = &TraitReference::error_node (); if (context->lookup_trait_reference ( diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.h b/gcc/rust/typecheck/rust-hir-trait-resolve.h index 6d4b932e468..ca23d48c3dd 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.h +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.h @@ -55,10 +55,8 @@ private: std::vector substitutions; }; -class TraitResolver : public TypeCheckBase, private HIR::HIRFullVisitorBase +class TraitResolver : public TypeCheckBase { - using HIR::HIRFullVisitorBase::visit; - public: static TraitReference *Resolve (HIR::TypePath &path); @@ -75,10 +73,8 @@ private: TraitReference *lookup_path (HIR::TypePath &path); - HIR::Trait *resolved_trait_reference; - -public: - void visit (HIR::Trait &trait) override { resolved_trait_reference = &trait; } + bool resolve_path_to_trait (const HIR::TypePath &path, + HIR::Trait **resolved) const; }; } // namespace Resolver