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