public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7639] gccrs: derive: Add #[derive(Copy)] builtin
@ 2024-01-16 17:49 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 17:49 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e16397e9963212d8c47fd0cd51544ba373e32172

commit r14-7639-ge16397e9963212d8c47fd0cd51544ba373e32172
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Fri May 26 13:37:13 2023 +0200

    gccrs: derive: Add #[derive(Copy)] builtin
    
    gcc/rust/ChangeLog:
    
            * Make-lang.in: Add new files.
            * expand/rust-derive.cc (DeriveVisitor::derive): Call into DeriveCopy.
            * expand/rust-derive-copy.cc: New file.
            * expand/rust-derive-copy.h: New file.

Diff:
---
 gcc/rust/Make-lang.in               |  1 +
 gcc/rust/expand/rust-derive-copy.cc | 80 +++++++++++++++++++++++++++++++++++++
 gcc/rust/expand/rust-derive-copy.h  | 56 ++++++++++++++++++++++++++
 gcc/rust/expand/rust-derive.cc      |  2 +
 4 files changed, 139 insertions(+)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index e708ac30a10..771c75f1432 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -90,6 +90,7 @@ GRS_OBJS = \
 	rust/rust-ast-builder.o \
 	rust/rust-derive.o \
 	rust/rust-derive-clone.o \
+	rust/rust-derive-copy.o \
     rust/rust-macro-invoc-lexer.o \
     rust/rust-macro-substitute-ctx.o \
     rust/rust-macro-builtins.o \
diff --git a/gcc/rust/expand/rust-derive-copy.cc b/gcc/rust/expand/rust-derive-copy.cc
new file mode 100644
index 00000000000..d578849c06b
--- /dev/null
+++ b/gcc/rust/expand/rust-derive-copy.cc
@@ -0,0 +1,80 @@
+// Copyright (C) 2020-2023 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-derive-copy.h"
+#include "rust-ast-full.h"
+
+namespace Rust {
+namespace AST {
+
+DeriveCopy::DeriveCopy (Location loc)
+  : loc (loc), builder (AstBuilder (loc)), expanded (nullptr)
+{}
+
+std::unique_ptr<AST::Item>
+DeriveCopy::go (Item &item)
+{
+  item.accept_vis (*this);
+
+  rust_assert (expanded);
+
+  return std::move (expanded);
+}
+
+std::unique_ptr<Item>
+DeriveCopy::copy_impl (std::string name)
+{
+  // `$crate::core::marker::Copy` instead
+  auto segments = std::vector<std::unique_ptr<TypePathSegment>> ();
+  segments.emplace_back (builder.type_path_segment ("Copy"));
+  auto copy = TypePath (std::move (segments), loc);
+
+  return std::unique_ptr<Item> (
+    new TraitImpl (copy, /* unsafe */ false,
+		   /* exclam */ false, /* trait items */ {},
+		   /* generics */ {}, builder.single_type_path (name),
+		   WhereClause::create_empty (), Visibility::create_private (),
+		   {}, {}, loc));
+}
+
+void
+DeriveCopy::visit_struct (StructStruct &item)
+{
+  expanded = copy_impl (item.get_struct_name ());
+}
+
+void
+DeriveCopy::visit_tuple (TupleStruct &item)
+{
+  expanded = copy_impl (item.get_struct_name ());
+}
+
+void
+DeriveCopy::visit_enum (Enum &item)
+{
+  expanded = copy_impl (item.get_identifier ());
+}
+
+void
+DeriveCopy::visit_union (Union &item)
+{
+  expanded = copy_impl (item.get_identifier ());
+}
+
+} // namespace AST
+} // namespace Rust
diff --git a/gcc/rust/expand/rust-derive-copy.h b/gcc/rust/expand/rust-derive-copy.h
new file mode 100644
index 00000000000..dce1f5e9a95
--- /dev/null
+++ b/gcc/rust/expand/rust-derive-copy.h
@@ -0,0 +1,56 @@
+// Copyright (C) 2020-2023 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_DERIVE_COPY_H
+#define RUST_DERIVE_COPY_H
+
+#include "rust-derive.h"
+#include "rust-ast-builder.h"
+
+namespace Rust {
+namespace AST {
+class DeriveCopy : DeriveVisitor
+{
+public:
+  DeriveCopy (Location loc);
+
+  std::unique_ptr<Item> go (Item &);
+
+private:
+  Location loc;
+  AstBuilder builder;
+  std::unique_ptr<Item> expanded;
+
+  /**
+   * Create the Copy impl block for a type. These impl blocks are very simple as
+   * Copy is just a marker trait.
+   *
+   * impl Copy for <type> {}
+   */
+  std::unique_ptr<Item> copy_impl (std::string name);
+
+  virtual void visit_struct (StructStruct &item);
+  virtual void visit_tuple (TupleStruct &item);
+  virtual void visit_enum (Enum &item);
+  virtual void visit_union (Union &item);
+};
+
+} // namespace AST
+} // namespace Rust
+
+#endif // !RUST_DERIVE_COPY_H
diff --git a/gcc/rust/expand/rust-derive.cc b/gcc/rust/expand/rust-derive.cc
index cf5126663f6..8f9b206da4b 100644
--- a/gcc/rust/expand/rust-derive.cc
+++ b/gcc/rust/expand/rust-derive.cc
@@ -18,6 +18,7 @@
 
 #include "rust-derive.h"
 #include "rust-derive-clone.h"
+#include "rust-derive-copy.h"
 
 namespace Rust {
 namespace AST {
@@ -31,6 +32,7 @@ DeriveVisitor::derive (Item &item, const Attribute &attr,
     case BuiltinMacro::Clone:
       return DeriveClone (attr.get_locus ()).go (item);
     case BuiltinMacro::Copy:
+      return DeriveCopy (attr.get_locus ()).go (item);
     case BuiltinMacro::Debug:
     case BuiltinMacro::Default:
     case BuiltinMacro::Eq:

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 17:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 17:49 [gcc r14-7639] gccrs: derive: Add #[derive(Copy)] builtin Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).