From: Arthur Cohen Date: Fri, 26 May 2023 11:37:13 +0000 (+0200) Subject: gccrs: derive: Add #[derive(Copy)] builtin X-Git-Tag: basepoints/gcc-15~2500 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e16397e9963212d8c47fd0cd51544ba373e32172;p=thirdparty%2Fgcc.git 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 --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index e708ac30a10e..771c75f14324 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 000000000000..d578849c06b8 --- /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 +// . + +#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 +DeriveCopy::go (Item &item) +{ + item.accept_vis (*this); + + rust_assert (expanded); + + return std::move (expanded); +} + +std::unique_ptr +DeriveCopy::copy_impl (std::string name) +{ + // `$crate::core::marker::Copy` instead + auto segments = std::vector> (); + segments.emplace_back (builder.type_path_segment ("Copy")); + auto copy = TypePath (std::move (segments), loc); + + return std::unique_ptr ( + 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 000000000000..dce1f5e9a950 --- /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 +// . + +#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 go (Item &); + +private: + Location loc; + AstBuilder builder; + std::unique_ptr 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 {} + */ + std::unique_ptr 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 cf5126663f6a..8f9b206da4be 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: