Debian Codegraph @

gcc/rust/expand/rust-derive-copy.cc

raw
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 DeriveCopy::DeriveCopy (location_t loc)
27 : DeriveVisitor (loc), expanded (nullptr)
28 {}
29
30 std::unique_ptr<AST::Item>
31 DeriveCopy::go (Item &item)
32 {
33 item.accept_vis (*this);
34
35 rust_assert (expanded);
36
37 return std::move (expanded);
38 }
39
40 std::unique_ptr<Item>
41 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 auto copy_bound = builder.type_path (LangItem::Kind::COPY);
48 auto copy_trait_path = builder.type_path (LangItem::Kind::COPY);
49
50 auto generics = setup_impl_generics (name, type_generics,
51 builder.trait_bound (copy_bound));
52
53 return builder.trait_impl (copy_trait_path, std::move (generics.self_type),
54 {}, std::move (generics.impl));
55 }
56
57 void
58 DeriveCopy::visit_struct (StructStruct &item)
59 {
60 expanded = copy_impl (item.get_struct_name ().as_string (),
61 item.get_generic_params ());
62 }
63
64 void
65 DeriveCopy::visit_tuple (TupleStruct &item)
66 {
67 expanded = copy_impl (item.get_struct_name ().as_string (),
68 item.get_generic_params ());
69 }
70
71 void
72 DeriveCopy::visit_enum (Enum &item)
73 {
74 expanded = copy_impl (item.get_identifier ().as_string (),
75 item.get_generic_params ());
76 }
77
78 void
79 DeriveCopy::visit_union (Union &item)
80 {
81 expanded = copy_impl (item.get_identifier ().as_string (),
82 item.get_generic_params ());
83 }
84
85 } // namespace AST
86 } // namespace Rust