]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: v0-mangle closures
authorRaiki Tamura <tamaron1203@gmail.com>
Thu, 12 Oct 2023 08:24:46 +0000 (17:24 +0900)
committerCohenArthur <arthur.cohen@embecosm.com>
Wed, 15 Nov 2023 09:44:25 +0000 (09:44 +0000)
gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::generate_closure_function):
Fix reference to node.
* backend/rust-mangle.cc (struct V0Path): Modified to accept closures.
(v0_crate_path): Modified to accept closures.
(v0_closure): New function to mangle closures.
(v0_path): Modified to accept closures
* util/rust-mapping-common.h (UNKNOWN_NODEID): Change to UINT32_MAX.
(UNKNOWN_HIRID): Change to UINT32_MAX.

gcc/testsuite/ChangeLog:

* rust/compile/v0-mangle2.rs: New test.

Signed-off-by: Raiki Tamura <tamaron1203@gmail.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-mangle.cc
gcc/rust/util/rust-mapping-common.h
gcc/testsuite/rust/compile/v0-mangle2.rs [new file with mode: 0644]

index 71eeb076672a4483f0f5b38c866ba53cdeec34ed..a5c853f17f5069c83126ec4bf46927a56fc95f7b 100644 (file)
@@ -2219,8 +2219,12 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr,
 
   const Resolver::CanonicalPath &parent_canonical_path
     = closure_tyty.get_ident ().path;
+  NodeId node_id;
+  bool ok = ctx->get_mappings ()->lookup_hir_to_node (
+    expr.get_mappings ().get_hirid (), &node_id);
+  rust_assert (ok);
   Resolver::CanonicalPath path = parent_canonical_path.append (
-    Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "{{closure}}"));
+    Resolver::CanonicalPath::new_seg (node_id, "{{closure}}"));
 
   std::string ir_symbol_name = path.get ();
   std::string asm_name = ctx->mangle_item (&closure_tyty, path);
index 8d104846b8470b102b06648022047b17096f4795..e1c9e9427c58b8f7c044ec23875815b56a7274e6 100644 (file)
@@ -45,8 +45,7 @@ struct V0Path
   std::string path = "";
   // Used for "N" and "C"
   std::string ident = "";
-  // Used for "C"
-  std::string crate_disambiguator = "";
+  std::string disambiguator = "";
   // Used for "M" and "X"
   std::string impl_path = "";
   std::string impl_type = "";
@@ -58,13 +57,14 @@ struct V0Path
   std::string as_string () const
   {
     if (prefix == "N")
-      return generic_prefix + prefix + ns + path + ident + generic_postfix;
+      return generic_prefix + prefix + ns + path + disambiguator + ident
+            + generic_postfix;
     else if (prefix == "M")
       return prefix + impl_path + impl_type;
     else if (prefix == "X")
       return prefix + impl_type + trait_type;
     else if (prefix == "C")
-      return prefix + crate_disambiguator + ident;
+      return prefix + disambiguator + ident;
     else
       rust_unreachable ();
   }
@@ -427,7 +427,7 @@ v0_crate_path (CrateNum crate_num, std::string ident)
 {
   V0Path v0path;
   v0path.prefix = "C";
-  v0path.crate_disambiguator = v0_disambiguator (crate_num);
+  v0path.disambiguator = v0_disambiguator (crate_num);
   v0path.ident = ident;
   return v0path;
 }
@@ -468,6 +468,18 @@ v0_inherent_or_trait_impl_path (Rust::Compile::Context *ctx,
   return v0path;
 }
 
+static V0Path
+v0_closure (V0Path path, HirId closure)
+{
+  V0Path v0path;
+  v0path.prefix = "N";
+  v0path.ns = "C";
+  v0path.disambiguator = v0_disambiguator (closure);
+  v0path.path = path.as_string ();
+  v0path.ident = "0";
+  return v0path;
+}
+
 static std::string
 v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
         const Resolver::CanonicalPath &cpath)
@@ -490,6 +502,7 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
       = mappings->lookup_hir_implitem (hir_id, &parent_impl_id);
     HIR::TraitItem *trait_item = mappings->lookup_hir_trait_item (hir_id);
     HIR::Item *item = mappings->lookup_hir_item (hir_id);
+    HIR::Expr *expr = mappings->lookup_hir_expr (hir_id);
 
     if (impl_item != nullptr)
       {
@@ -567,10 +580,16 @@ v0_path (Rust::Compile::Context *ctx, const TyTy::BaseType *ty,
                                  cpath.get ().c_str ());
          break;
        }
+    else if (expr != nullptr)
+      {
+       rust_assert (expr->get_expression_type ()
+                    == HIR::Expr::ExprType::Closure);
+       // Use HIR ID as disambiguator.
+       v0path = v0_closure (v0path, hir_id);
+      }
     else
       {
-       // Not HIR item, impl item, nor trait impl item. Assume a crate.
-       // FIXME: Do closures get here?
+       // Not HIR item, impl item, trait impl item, nor expr. Assume a crate.
 
        // std::string crate_name;
        // bool ok = mappings->get_crate_name (path.get_crate_num (),
index 93df863bf673eb36a8db3505c399b13e336ccbba..725eae2a51f39cafa688481a35caa424bd52d979 100644 (file)
@@ -62,8 +62,8 @@ struct DefId
 };
 
 #define UNKNOWN_CRATENUM ((uint32_t) (UINT32_MAX))
-#define UNKNOWN_NODEID ((uint32_t) (0))
-#define UNKNOWN_HIRID ((uint32_t) (0))
+#define UNKNOWN_NODEID ((uint32_t) (UINT32_MAX))
+#define UNKNOWN_HIRID ((uint32_t) (UINT32_MAX))
 #define UNKNOWN_LOCAL_DEFID ((uint32_t) (0))
 #define UNKNOWN_DEFID (DefId{0, 0})
 
diff --git a/gcc/testsuite/rust/compile/v0-mangle2.rs b/gcc/testsuite/rust/compile/v0-mangle2.rs
new file mode 100644 (file)
index 0000000..d092dcc
--- /dev/null
@@ -0,0 +1,17 @@
+// { dg-additional-options -frust-mangling=v0 }
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "fn_once"]
+pub trait FnOnce<Args> {
+    #[lang = "fn_once_output"]
+    type Output;
+
+    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
+}
+
+fn main() {
+    // { dg-final { scan-assembler "_R.*NC.*NvC.*10v0_mangle24main.*0" } }
+    let closure_annotated = |i: i32| -> i32 { i + 1 };
+    let _ = closure_annotated(0) - 1;
+}
This page took 0.080294 seconds and 5 git commands to generate.