[gcc/devel/rust/master] Add support for external functions

Thomas Schwinge tschwinge@gcc.gnu.org
Tue May 7 16:16:31 GMT 2024


https://gcc.gnu.org/g:48add29a63c464ca27623b41a5d2841ca384e775

commit 48add29a63c464ca27623b41a5d2841ca384e775
Author: 0xn4utilus <gyanendrabanjare8@gmail.com>
Date:   Sun Feb 25 05:32:26 2024 +0530

    Add support for external functions
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast.cc (Function::Function): Add `is_external_function` field.
            (Function::operator=): Likewise.
            * ast/rust-ast.h: New constructor for ExternalItem.
            * ast/rust-item.h (class Function): Add `is_external_function`
            field. Update `get_node_id`.
            * ast/rust-macro.h: Update copy constructor.
    
    Signed-off-by: 0xn4utilus <gyanendrabanjare8@gmail.com>

Diff:
---
 gcc/rust/ast/rust-ast.cc  |  9 ++++++---
 gcc/rust/ast/rust-ast.h   |  4 +++-
 gcc/rust/ast/rust-item.h  | 41 +++++++++++++++++++++++++++++------------
 gcc/rust/ast/rust-macro.h |  9 +++++----
 4 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index fbd795f67180..5d661989904b 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -1061,9 +1061,11 @@ Union::as_string () const
 }
 
 Function::Function (Function const &other)
-  : VisItem (other), qualifiers (other.qualifiers),
-    function_name (other.function_name), where_clause (other.where_clause),
-    locus (other.locus), is_default (other.is_default)
+  : VisItem (other), ExternalItem (other.get_node_id ()),
+    qualifiers (other.qualifiers), function_name (other.function_name),
+    where_clause (other.where_clause), locus (other.locus),
+    is_default (other.is_default),
+    is_external_function (other.is_external_function)
 {
   // guard to prevent null dereference (always required)
   if (other.return_type != nullptr)
@@ -1095,6 +1097,7 @@ Function::operator= (Function const &other)
   // outer_attrs = other.outer_attrs;
   locus = other.locus;
   is_default = other.is_default;
+  is_external_function = other.is_external_function;
 
   // guard to prevent null dereference (always required)
   if (other.return_type != nullptr)
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index c4d5858dd636..92faaf297f9d 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1695,6 +1695,8 @@ class ExternalItem : public Visitable
 public:
   ExternalItem () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {}
 
+  ExternalItem (NodeId node_id) : node_id (node_id) {}
+
   virtual ~ExternalItem () {}
 
   // Unique pointer custom clone function
@@ -1708,7 +1710,7 @@ public:
   virtual void mark_for_strip () = 0;
   virtual bool is_marked_for_strip () const = 0;
 
-  NodeId get_node_id () const { return node_id; }
+  virtual NodeId get_node_id () const { return node_id; }
 
 protected:
   // Clone function implementation as pure virtual method
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 44963ba386e7..573888bea5aa 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -1289,7 +1289,7 @@ protected:
 class LetStmt;
 
 // Rust function declaration AST node
-class Function : public VisItem, public AssociatedItem
+class Function : public VisItem, public AssociatedItem, public ExternalItem
 {
   FunctionQualifiers qualifiers;
   Identifier function_name;
@@ -1300,6 +1300,7 @@ class Function : public VisItem, public AssociatedItem
   tl::optional<std::unique_ptr<BlockExpr>> function_body;
   location_t locus;
   bool is_default;
+  bool is_external_function;
 
 public:
   std::string as_string () const override;
@@ -1330,16 +1331,17 @@ public:
 	    std::unique_ptr<Type> return_type, WhereClause where_clause,
 	    tl::optional<std::unique_ptr<BlockExpr>> function_body,
 	    Visibility vis, std::vector<Attribute> outer_attrs,
-	    location_t locus, bool is_default = false)
+	    location_t locus, bool is_default = false,
+	    bool is_external_function = false)
     : VisItem (std::move (vis), std::move (outer_attrs)),
-      qualifiers (std::move (qualifiers)),
+      ExternalItem (Stmt::node_id), qualifiers (std::move (qualifiers)),
       function_name (std::move (function_name)),
       generic_params (std::move (generic_params)),
       function_params (std::move (function_params)),
       return_type (std::move (return_type)),
       where_clause (std::move (where_clause)),
       function_body (std::move (function_body)), locus (locus),
-      is_default (is_default)
+      is_default (is_default), is_external_function (is_external_function)
   {}
 
   // TODO: add constructor with less fields
@@ -1364,6 +1366,8 @@ public:
 	   && function_params.back ()->is_variadic ();
   }
 
+  bool is_external () const { return is_external_function; }
+
   // Invalid if block is null, so base stripping on that.
   void mark_for_strip () override { function_body = nullptr; }
   bool is_marked_for_strip () const override
@@ -1422,6 +1426,9 @@ public:
     return function_params[0];
   }
 
+  // ExternalItem::node_id is same as Stmt::node_id
+  NodeId get_node_id () const override { return Stmt::node_id; }
+
 protected:
   /* Use covariance to implement clone function as returning this object
    * rather than base */
@@ -1433,6 +1440,13 @@ protected:
   {
     return new Function (*this);
   }
+
+  /* Use covariance to implement clone function as returning this object
+   * rather than base */
+  Function *clone_external_item_impl () const override
+  {
+    return new Function (*this);
+  }
 };
 
 // Rust type alias (i.e. typedef) AST node
@@ -3257,10 +3271,11 @@ public:
       item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
   {}
 
+  // copy constructor
   ExternalTypeItem (ExternalTypeItem const &other)
-    : outer_attrs (other.outer_attrs), visibility (other.visibility),
-      item_name (other.item_name), locus (other.locus),
-      marked_for_strip (other.marked_for_strip)
+    : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
+      visibility (other.visibility), item_name (other.item_name),
+      locus (other.locus), marked_for_strip (other.marked_for_strip)
   {
     node_id = other.node_id;
   }
@@ -3340,8 +3355,9 @@ public:
 
   // Copy constructor
   ExternalStaticItem (ExternalStaticItem const &other)
-    : outer_attrs (other.outer_attrs), visibility (other.visibility),
-      item_name (other.item_name), locus (other.locus), has_mut (other.has_mut)
+    : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
+      visibility (other.visibility), item_name (other.item_name),
+      locus (other.locus), has_mut (other.has_mut)
   {
     node_id = other.node_id;
     // guard to prevent null dereference (only required if error state)
@@ -3607,9 +3623,10 @@ public:
 
   // Copy constructor with clone
   ExternalFunctionItem (ExternalFunctionItem const &other)
-    : outer_attrs (other.outer_attrs), visibility (other.visibility),
-      item_name (other.item_name), locus (other.locus),
-      where_clause (other.where_clause), function_params (other.function_params)
+    : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs),
+      visibility (other.visibility), item_name (other.item_name),
+      locus (other.locus), where_clause (other.where_clause),
+      function_params (other.function_params)
   {
     node_id = other.node_id;
     // guard to prevent null pointer dereference
diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index bcf5b0b5a994..bfdebfc42032 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -722,10 +722,11 @@ private:
   {}
 
   MacroInvocation (const MacroInvocation &other)
-    : TraitItem (other.locus), outer_attrs (other.outer_attrs),
-      locus (other.locus), node_id (other.node_id),
-      invoc_data (other.invoc_data), is_semi_coloned (other.is_semi_coloned),
-      kind (other.kind), builtin_kind (other.builtin_kind)
+    : TraitItem (other.locus), ExternalItem (Expr::node_id),
+      outer_attrs (other.outer_attrs), locus (other.locus),
+      node_id (other.node_id), invoc_data (other.invoc_data),
+      is_semi_coloned (other.is_semi_coloned), kind (other.kind),
+      builtin_kind (other.builtin_kind)
   {
     if (other.kind == InvocKind::Builtin)
       for (auto &pending : other.pending_eager_invocs)


More information about the Gcc-cvs mailing list