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-debug.h"
20 : #include "rust-ast.h"
21 : #include "rust-hir-map.h"
22 : #include "rust-system.h"
23 :
24 : namespace Rust {
25 : namespace AST {
26 :
27 175 : DeriveDebug::DeriveDebug (location_t loc, Builder::Source item_source)
28 175 : : DeriveVisitor (loc, item_source), expanded (nullptr)
29 175 : {}
30 :
31 : std::unique_ptr<Item>
32 175 : DeriveDebug::go (Item &item)
33 : {
34 175 : item.accept_vis (*this);
35 :
36 175 : rust_assert (expanded);
37 :
38 175 : return std::move (expanded);
39 : }
40 :
41 : std::unique_ptr<AssociatedItem>
42 175 : DeriveDebug::stub_debug_fn ()
43 : {
44 175 : auto unit_expr = builder.tuple ();
45 175 : auto ok_expr
46 175 : = ptrify (builder.path_in_expression (LangItem::Kind::RESULT_OK));
47 :
48 350 : auto stub_return = builder.call (std::move (ok_expr), std::move (unit_expr));
49 :
50 : // we can't use builder.block() here as it returns a unique_ptr<Expr> and
51 : // Function's constructor expects a unique_ptr<BlockExpr>
52 175 : auto block = std::unique_ptr<BlockExpr> (
53 175 : new BlockExpr ({}, std::move (stub_return), {}, {}, tl::nullopt, loc, loc));
54 :
55 175 : auto self = builder.self_ref_param ();
56 :
57 175 : auto return_type = ptrify (
58 700 : builder.type_path ({builder.get_path_start (), "fmt", "Result"}, true));
59 :
60 175 : auto mut_fmt_type_inner = ptrify (
61 700 : builder.type_path ({builder.get_path_start (), "fmt", "Formatter"}, true));
62 :
63 175 : auto mut_fmt_type
64 175 : = builder.reference_type (std::move (mut_fmt_type_inner), true);
65 :
66 350 : auto fmt = builder.function_param (builder.identifier_pattern ("_fmt"),
67 175 : std::move (mut_fmt_type));
68 :
69 175 : auto params = vec (std::move (self), std::move (fmt));
70 :
71 525 : auto function = builder.function ("fmt", std::move (params),
72 350 : std::move (return_type), std::move (block));
73 :
74 175 : return function;
75 175 : }
76 :
77 : std::unique_ptr<Item>
78 175 : DeriveDebug::stub_derive_impl (
79 : std::string name,
80 : const std::vector<std::unique_ptr<GenericParam>> &type_generics)
81 : {
82 175 : auto trait_items = vec (stub_debug_fn ());
83 :
84 403 : auto debug = [this] () {
85 1140 : return builder.type_path ({builder.get_path_start (), "fmt", "Debug"},
86 456 : true);
87 175 : };
88 403 : auto generics = setup_impl_generics (name, type_generics, [&, this] () {
89 53 : return builder.trait_bound (debug ());
90 175 : });
91 :
92 350 : return builder.trait_impl (debug (), std::move (generics.self_type),
93 : std::move (trait_items),
94 350 : std::move (generics.impl));
95 175 : }
96 :
97 : void
98 83 : DeriveDebug::visit_struct (StructStruct &struct_item)
99 : {
100 249 : expanded = stub_derive_impl (struct_item.get_identifier ().as_string (),
101 166 : struct_item.get_generic_params ());
102 83 : }
103 :
104 : void
105 68 : DeriveDebug::visit_tuple (TupleStruct &tuple_item)
106 : {
107 204 : expanded = stub_derive_impl (tuple_item.get_identifier ().as_string (),
108 136 : tuple_item.get_generic_params ());
109 68 : }
110 :
111 : void
112 24 : DeriveDebug::visit_enum (Enum &enum_item)
113 : {
114 72 : expanded = stub_derive_impl (enum_item.get_identifier ().as_string (),
115 48 : enum_item.get_generic_params ());
116 24 : }
117 :
118 : void
119 0 : DeriveDebug::visit_union (Union &enum_item)
120 : {
121 0 : rust_error_at (loc, "derive(Debug) cannot be derived for unions");
122 0 : }
123 :
124 : } // namespace AST
125 : } // namespace Rust
|