Line data Source code
1 : // Copyright (C) 2024-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-expand-format-args.h"
20 : #include "rust-ast-fragment.h"
21 : #include "rust-ast.h"
22 : #include "rust-builtin-ast-nodes.h"
23 : #include "rust-ast-builder.h"
24 : #include "rust-diagnostics.h"
25 : #include "rust-expr.h"
26 : #include "rust-fmt.h"
27 : #include "rust-path.h"
28 : #include "rust-system.h"
29 : #include "rust-token.h"
30 :
31 : namespace Rust {
32 : namespace Fmt {
33 :
34 : static std::unique_ptr<AST::Expr>
35 924 : format_arg (const AST::Builder &builder, std::unique_ptr<AST::Expr> &&to_format,
36 : const std::string &trait)
37 : {
38 924 : auto formatter_fn = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
39 6468 : builder.path_in_expression ({builder.get_path_start (), "fmt", trait,
40 924 : "fmt"},
41 1848 : builder.has_opening_scope ())));
42 :
43 924 : auto path = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
44 5544 : builder.path_in_expression ({builder.get_path_start (), "fmt", "ArgumentV1",
45 924 : "new"},
46 1848 : builder.has_opening_scope ())));
47 :
48 924 : auto args = std::vector<std::unique_ptr<AST::Expr>> ();
49 924 : args.emplace_back (std::move (to_format));
50 924 : args.emplace_back (std::move (formatter_fn));
51 :
52 924 : return builder.call (std::move (path), std::move (args));
53 924 : }
54 :
55 : const std::string &
56 924 : get_trait_name (ffi::FormatSpec format_specifier)
57 : {
58 924 : static const std::unordered_map<std::string, std::string> spec_map = {
59 : {"", "Display"}, {"?", "Debug"}, {"e", "LowerExp"},
60 : {"E", "UpperExp"}, {"o", "Octal"}, {"p", "Pointer"},
61 : {"b", "Binary"}, {"x", "LowerHex"}, {"X", "UpperHex"},
62 1023 : };
63 :
64 924 : auto it = spec_map.find (format_specifier.ty.to_string ());
65 :
66 924 : if (it == spec_map.end ())
67 0 : rust_unreachable ();
68 :
69 924 : return it->second;
70 : }
71 :
72 : tl::optional<AST::Fragment>
73 587 : expand_format_args (AST::FormatArgs &fmt,
74 : std::vector<std::unique_ptr<AST::Token>> &&tokens)
75 : {
76 587 : auto loc = fmt.get_locus ();
77 : // FIXME: This needs to be aware of the crate in which we are expanding it.
78 : // Expanding format_args!() within core needs to use paths that start with
79 : // `crate::`, not `core::`
80 587 : auto builder = AST::Builder (loc, AST::Builder::Source::InCore);
81 587 : auto &arguments = fmt.get_arguments ();
82 :
83 587 : auto static_pieces = std::vector<std::unique_ptr<AST::Expr>> ();
84 587 : auto args
85 : = std::vector<std::pair<std::unique_ptr<AST::Expr>, ffi::FormatSpec>> ();
86 :
87 2691 : for (const auto &node : fmt.get_template ().get_pieces ())
88 : {
89 2104 : switch (node.tag)
90 : {
91 1180 : case ffi::Piece::Tag::String:
92 1180 : static_pieces.emplace_back (
93 2360 : builder.literal_string (node.string._0.to_string ()));
94 1180 : break;
95 924 : case ffi::Piece::Tag::NextArgument:
96 924 : {
97 924 : auto next_argument = node.next_argument._0;
98 924 : switch (node.next_argument._0.position.tag)
99 : {
100 924 : case ffi::Position::Tag::ArgumentImplicitlyIs:
101 924 : {
102 924 : auto idx = next_argument.position.argument_implicitly_is._0;
103 924 : auto trait = next_argument.format;
104 924 : auto arg = arguments.at (idx);
105 :
106 : // FIXME(Arthur): This API sucks
107 924 : rust_assert (arg.get_kind ().kind
108 : == AST::FormatArgumentKind::Kind::Normal);
109 :
110 924 : args.push_back ({arg.get_expr ().clone_expr (), trait});
111 924 : }
112 924 : break;
113 0 : case ffi::Position::Tag::ArgumentIs:
114 0 : case ffi::Position::Tag::ArgumentNamed:
115 0 : rust_sorry_at (loc, "unhandled argument position specifier");
116 0 : break;
117 : }
118 924 : }
119 924 : break;
120 : }
121 : }
122 :
123 587 : auto args_array = std::vector<std::unique_ptr<AST::Expr>> ();
124 1511 : for (auto &&arg : args)
125 1848 : args_array.emplace_back (format_arg (builder,
126 1848 : builder.ref (std::move (arg.first)),
127 1848 : get_trait_name (arg.second)));
128 :
129 587 : auto pieces = builder.ref (builder.array (std::move (static_pieces)));
130 587 : auto args_slice = builder.ref (builder.array (std::move (args_array)));
131 :
132 587 : auto final_path = std::make_unique<AST::PathInExpression> (
133 3522 : builder.path_in_expression ({builder.get_path_start (), "fmt", "Arguments",
134 587 : "new_v1"},
135 587 : builder.has_opening_scope ()));
136 587 : auto final_args = std::vector<std::unique_ptr<AST::Expr>> ();
137 587 : final_args.emplace_back (std::move (pieces));
138 587 : final_args.emplace_back (std::move (args_slice));
139 :
140 587 : auto final_call
141 587 : = builder.call (std::move (final_path), std::move (final_args));
142 :
143 587 : auto node = AST::SingleASTNode (std::move (final_call));
144 :
145 1761 : return AST::Fragment ({node}, std::move (tokens));
146 587 : }
147 :
148 : } // namespace Fmt
149 : } // namespace Rust
|