]> gcc.gnu.org Git - gcc.git/commitdiff
Add impl-trait path probe helper
authorPhilip Herron <philip.herron@embecosm.com>
Sun, 22 Aug 2021 12:44:46 +0000 (13:44 +0100)
committerPhilip Herron <philip.herron@embecosm.com>
Sun, 22 Aug 2021 13:56:26 +0000 (14:56 +0100)
This adds a probe to lookup candidates for a segment for any impl block
for this receiver and trait. This simplifies some query based compilation
code. When the item is resolved to a trait item but might be overriden by
a reciever impl block instead.

gcc/rust/typecheck/rust-hir-path-probe.h

index f737141d897ca53f50caf5459a0440e6a35ecf86..60cd98a5e5edde933ad860342057bf12ea708ea6 100644 (file)
@@ -114,6 +114,7 @@ struct PathProbeCandidate
 
 class PathProbeType : public TypeCheckBase
 {
+protected:
   using Rust::Resolver::TypeCheckBase::visit;
 
 public:
@@ -207,7 +208,7 @@ public:
       }
   }
 
-private:
+protected:
   void process_impl_items_for_candidates ()
   {
     mappings->iterate_impl_items ([&] (HirId id, HIR::ImplItem *item,
@@ -313,7 +314,7 @@ private:
       }
   }
 
-private:
+protected:
   PathProbeType (const TyTy::BaseType *receiver,
                 const HIR::PathIdentSegment &query)
     : TypeCheckBase (), receiver (receiver), search (query),
@@ -404,6 +405,33 @@ private:
   RichLocation &r;
 };
 
+class PathProbeImplTrait : public PathProbeType
+{
+public:
+  static std::vector<PathProbeCandidate>
+  Probe (const TyTy::BaseType *receiver,
+        const HIR::PathIdentSegment &segment_name,
+        const TraitReference *trait_reference)
+  {
+    PathProbeImplTrait probe (receiver, segment_name, trait_reference);
+    // iterate all impls for this trait and receiver
+    // then search for possible candidates using base class behaviours
+    probe.process_trait_impl_items_for_candidates ();
+    return probe.candidates;
+  }
+
+private:
+  void process_trait_impl_items_for_candidates ();
+
+  PathProbeImplTrait (const TyTy::BaseType *receiver,
+                     const HIR::PathIdentSegment &query,
+                     const TraitReference *trait_reference)
+    : PathProbeType (receiver, query), trait_reference (trait_reference)
+  {}
+
+  const TraitReference *trait_reference;
+};
+
 } // namespace Resolver
 } // namespace Rust
 
This page took 0.074726 seconds and 5 git commands to generate.