diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-05-26 13:37:13 +0200 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2023-06-02 13:32:29 +0000 |
commit | 2946327ff94f1653dd57f7362b6402e9b8353643 (patch) | |
tree | 340b9301f27f1e2fe27e282ab57587e8987213d2 | |
parent | 2e49323c7593404ce536040b65bed37d4048eb95 (diff) | |
download | gcc-2946327ff94f1653dd57f7362b6402e9b8353643.zip gcc-2946327ff94f1653dd57f7362b6402e9b8353643.tar.gz gcc-2946327ff94f1653dd57f7362b6402e9b8353643.tar.bz2 |
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.
-rw-r--r-- | gcc/rust/Make-lang.in | 1 | ||||
-rw-r--r-- | gcc/rust/expand/rust-derive-copy.cc | 80 | ||||
-rw-r--r-- | gcc/rust/expand/rust-derive-copy.h | 56 | ||||
-rw-r--r-- | gcc/rust/expand/rust-derive.cc | 2 |
4 files changed, 139 insertions, 0 deletions
diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 0e8cd02..8dcfa4f 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 0000000..d578849 --- /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 0000000..dce1f5e --- /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 cf51266..8f9b206 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: |