]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Visit function and structure attributes
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 31 Jul 2023 13:29:36 +0000 (15:29 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:04:32 +0000 (19:04 +0100)
Add a simple attribute visit function and override StructStruct &
Function visit functions.

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc (Early::visit_attributes):
Add function to handle attributes.
(Early::visit): Override visitor functions.
* resolve/rust-early-name-resolver-2.0.h: Add prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/resolve/rust-early-name-resolver-2.0.cc
gcc/rust/resolve/rust-early-name-resolver-2.0.h

index e6603cf4fbd8adf3352f0d96f7ec094288bb31c1..65ec0d659167f669b9e7cbf01a26648cb2e247c4 100644 (file)
@@ -19,6 +19,7 @@
 #include "rust-early-name-resolver-2.0.h"
 #include "rust-ast-full.h"
 #include "rust-toplevel-name-resolver-2.0.h"
+#include "rust-attributes.h"
 
 namespace Rust {
 namespace Resolver2_0 {
@@ -173,5 +174,56 @@ void
 Early::visit (AST::UseTreeGlob &use)
 {}
 
+void
+Early::visit_attributes (std::vector<AST::Attribute> attrs)
+{
+  for (auto &attr : attrs)
+    {
+      auto name = attr.get_path ().get_segments ().at (0).get_segment_name ();
+
+      if (attr.is_derive ())
+       {
+         auto traits = attr.get_traits_to_derive ();
+         for (auto &trait : traits)
+           {
+             auto definition = ctx.macros.resolve_path (trait.get ());
+             if (!definition.has_value ())
+               {
+                 // FIXME: Change to proper error message
+                 rust_error_at (trait.get ().get_locus (),
+                                "could not resolve trait");
+               }
+           }
+       }
+      else if (Analysis::BuiltinAttributeMappings::get ()
+                ->lookup_builtin (name)
+                .is_error ()) // Do not resolve builtins
+       {
+         auto definition = ctx.macros.resolve_path (attr.get_path ());
+         if (!definition.has_value ())
+           {
+             // FIXME: Change to proper error message
+             rust_error_at (attr.get_locus (),
+                            "could not resolve attribute macro invocation");
+             return;
+           }
+       }
+    }
+}
+
+void
+Early::visit (AST::Function &fn)
+{
+  visit_attributes (fn.get_outer_attrs ());
+  DefaultResolver::visit (fn);
+}
+
+void
+Early::visit (AST::StructStruct &s)
+{
+  visit_attributes (s.get_outer_attrs ());
+  DefaultResolver::visit (s);
+}
+
 } // namespace Resolver2_0
 } // namespace Rust
index f2b63c9b6967843b8edc32f3a5cf49e8254eb6a7..fe1c1f6b11cb5ca63e830ae44075dfacd6505fb2 100644 (file)
@@ -54,8 +54,12 @@ public:
   void visit (AST::UseTreeRebind &) override;
   void visit (AST::UseTreeList &) override;
   void visit (AST::UseTreeGlob &) override;
+  void visit (AST::Function &) override;
+  void visit (AST::StructStruct &) override;
 
 private:
+  void visit_attributes (std::vector<AST::Attribute> attrs);
+
   /**
    * Macros can either be resolved through textual scoping or regular path
    * scoping - which this class represents. Textual scoping works similarly to a