Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : 21 : DeriveCopy::DeriveCopy (location_t loc)
27 : 21 : : DeriveVisitor (loc), expanded (nullptr)
28 : 21 : {}
29 : :
30 : : std::unique_ptr<AST::Item>
31 : 21 : DeriveCopy::go (Item &item)
32 : : {
33 : 21 : item.accept_vis (*this);
34 : :
35 : 21 : rust_assert (expanded);
36 : :
37 : 21 : return std::move (expanded);
38 : : }
39 : :
40 : : std::unique_ptr<Item>
41 : 21 : DeriveCopy::copy_impl (
42 : : std::string name,
43 : : const std::vector<std::unique_ptr<GenericParam>> &type_generics)
44 : : {
45 : : // we should have two of these, so we don't run into issues with
46 : : // two paths sharing a node id
47 : 21 : auto copy_bound = builder.type_path (LangItem::Kind::COPY);
48 : 21 : auto copy_trait_path = builder.type_path (LangItem::Kind::COPY);
49 : :
50 : 21 : auto generics = setup_impl_generics (name, type_generics,
51 : 42 : builder.trait_bound (copy_bound));
52 : :
53 : 42 : return builder.trait_impl (copy_trait_path, std::move (generics.self_type),
54 : 21 : {}, std::move (generics.impl));
55 : 21 : }
56 : :
57 : : void
58 : 21 : DeriveCopy::visit_struct (StructStruct &item)
59 : : {
60 : 63 : expanded = copy_impl (item.get_struct_name ().as_string (),
61 : 42 : item.get_generic_params ());
62 : 21 : }
63 : :
64 : : void
65 : 0 : DeriveCopy::visit_tuple (TupleStruct &item)
66 : : {
67 : 0 : expanded = copy_impl (item.get_struct_name ().as_string (),
68 : 0 : item.get_generic_params ());
69 : 0 : }
70 : :
71 : : void
72 : 0 : DeriveCopy::visit_enum (Enum &item)
73 : : {
74 : 0 : expanded = copy_impl (item.get_identifier ().as_string (),
75 : 0 : item.get_generic_params ());
76 : 0 : }
77 : :
78 : : void
79 : 0 : DeriveCopy::visit_union (Union &item)
80 : : {
81 : 0 : expanded = copy_impl (item.get_identifier ().as_string (),
82 : 0 : item.get_generic_params ());
83 : 0 : }
84 : :
85 : : } // namespace AST
86 : : } // namespace Rust
|