Debian Codegraph @

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

raw
1 // Copyright (C) 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-default.h"
20 #include "rust-ast.h"
21 #include "rust-diagnostics.h"
22 #include "rust-path.h"
23 #include "rust-system.h"
24
25 namespace Rust {
26 namespace AST {
27
28 DeriveDefault::DeriveDefault (location_t loc)
29 : DeriveVisitor (loc), expanded (nullptr)
30 {}
31
32 std::unique_ptr<Item>
33 DeriveDefault::go (Item &item)
34 {
35 item.accept_vis (*this);
36
37 rust_assert (expanded);
38
39 return std::move (expanded);
40 }
41
42 std::unique_ptr<Expr>
43 DeriveDefault::default_call (std::unique_ptr<Type> &&type)
44 {
45 auto default_trait = builder.type_path ({"core", "default", "Default"}, true);
46
47 auto default_fn
48 = builder.qualified_path_in_expression (std::move (type), default_trait,
49 builder.path_segment ("default"));
50
51 return builder.call (std::move (default_fn));
52 }
53
54 std::unique_ptr<AssociatedItem>
55 DeriveDefault::default_fn (std::unique_ptr<Expr> &&return_expr)
56 {
57 auto self_ty
58 = std::unique_ptr<Type> (new TypePath (builder.type_path ("Self")));
59
60 auto block = std::unique_ptr<BlockExpr> (
61 new BlockExpr ({}, std::move (return_expr), {}, {}, tl::nullopt, loc, loc));
62
63 return builder.function ("default", {}, std::move (self_ty),
64 std::move (block));
65 }
66
67 std::unique_ptr<Item>
68 DeriveDefault::default_impl (
69 std::unique_ptr<AssociatedItem> &&default_fn, std::string name,
70 const std::vector<std::unique_ptr<GenericParam>> &type_generics)
71 {
72 auto default_path = builder.type_path ({"core", "default", "Default"}, true);
73
74 auto trait_items = vec (std::move (default_fn));
75
76 auto generics = setup_impl_generics (name, type_generics,
77 builder.trait_bound (default_path));
78
79 return builder.trait_impl (default_path, std::move (generics.self_type),
80 std::move (trait_items),
81 std::move (generics.impl));
82 }
83
84 void
85 DeriveDefault::visit_struct (StructStruct &item)
86 {
87 if (item.is_unit_struct ())
88 {
89 auto unit_ctor
90 = builder.struct_expr_struct (item.get_struct_name ().as_string ());
91 expanded = default_impl (default_fn (std::move (unit_ctor)),
92 item.get_struct_name ().as_string (),
93 item.get_generic_params ());
94 return;
95 }
96
97 auto cloned_fields = std::vector<std::unique_ptr<StructExprField>> ();
98 for (auto &field : item.get_fields ())
99 {
100 auto name = field.get_field_name ().as_string ();
101 auto expr = default_call (field.get_field_type ().clone_type ());
102
103 cloned_fields.emplace_back (
104 builder.struct_expr_field (std::move (name), std::move (expr)));
105 }
106
107 auto ctor = builder.struct_expr (item.get_struct_name ().as_string (),
108 std::move (cloned_fields));
109
110 expanded = default_impl (default_fn (std::move (ctor)),
111 item.get_struct_name ().as_string (),
112 item.get_generic_params ());
113 }
114
115 void
116 DeriveDefault::visit_tuple (TupleStruct &tuple_item)
117 {
118 auto defaulted_fields = std::vector<std::unique_ptr<Expr>> ();
119
120 for (auto &field : tuple_item.get_fields ())
121 {
122 auto type = field.get_field_type ().clone_type ();
123
124 defaulted_fields.emplace_back (default_call (std::move (type)));
125 }
126
127 auto return_expr
128 = builder.call (builder.identifier (
129 tuple_item.get_struct_name ().as_string ()),
130 std::move (defaulted_fields));
131
132 expanded = default_impl (default_fn (std::move (return_expr)),
133 tuple_item.get_struct_name ().as_string (),
134 tuple_item.get_generic_params ());
135 }
136
137 void
138 DeriveDefault::visit_enum (Enum &enum_item)
139 {
140 // This is no longer the case in later Rust versions where you can choose a
141 // default variant to emit using the `#[default]` attribute:
142 //
143 // ```rust
144 // #[derive(Default)]
145 // enum Baz {
146 // #[default]
147 // A,
148 // B(i32),
149 // C { a: i32 }
150 // }
151 // ```
152 //
153 // will emit the following impl
154 //
155 // ```rust
156 // impl ::core::default::Default for Baz {
157 // #[inline]
158 // fn default() -> Baz { Self::A }
159 // }
160 // ```
161 rust_error_at (loc, ErrorCode::E0665,
162 "%<Default%> cannot be derived for enums, only structs");
163 }
164
165 void
166 DeriveDefault::visit_union (Union &enum_item)
167 {
168 rust_error_at (loc, "derive(Default) cannot be used on unions");
169 }
170
171 } // namespace AST
172 } // namespace Rust