Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-derive-copy.h"
20 : #include "rust-hir-map.h"
21 : #include "rust-path.h"
22 :
23 : namespace Rust {
24 : namespace AST {
25 :
26 15 : DeriveCopy::DeriveCopy (location_t loc)
27 15 : : DeriveVisitor (loc), expanded (nullptr)
28 15 : {}
29 :
30 : std::unique_ptr<AST::Item>
31 15 : DeriveCopy::go (Item &item)
32 : {
33 15 : item.accept_vis (*this);
34 :
35 15 : rust_assert (expanded);
36 :
37 15 : return std::move (expanded);
38 : }
39 :
40 : std::unique_ptr<Item>
41 15 : DeriveCopy::copy_impl (
42 : std::string name,
43 : const std::vector<std::unique_ptr<GenericParam>> &type_generics)
44 : {
45 15 : auto copy_trait_path
46 15 : = [this] () { return builder.type_path (LangItem::Kind::COPY); };
47 :
48 31 : auto generics = setup_impl_generics (name, type_generics, [&, this] () {
49 1 : return builder.trait_bound (copy_trait_path ());
50 15 : });
51 :
52 30 : return builder.trait_impl (copy_trait_path (), std::move (generics.self_type),
53 30 : {}, std::move (generics.impl));
54 15 : }
55 :
56 : void
57 15 : DeriveCopy::visit_struct (StructStruct &item)
58 : {
59 45 : expanded = copy_impl (item.get_struct_name ().as_string (),
60 30 : item.get_generic_params ());
61 15 : }
62 :
63 : void
64 0 : DeriveCopy::visit_tuple (TupleStruct &item)
65 : {
66 0 : expanded = copy_impl (item.get_struct_name ().as_string (),
67 0 : item.get_generic_params ());
68 0 : }
69 :
70 : void
71 0 : DeriveCopy::visit_enum (Enum &item)
72 : {
73 0 : expanded = copy_impl (item.get_identifier ().as_string (),
74 0 : item.get_generic_params ());
75 0 : }
76 :
77 : void
78 0 : DeriveCopy::visit_union (Union &item)
79 : {
80 0 : expanded = copy_impl (item.get_identifier ().as_string (),
81 0 : item.get_generic_params ());
82 0 : }
83 :
84 : } // namespace AST
85 : } // namespace Rust
|