Branch data Line data Source code
1 : : // Copyright (C) 2024-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 : : #ifndef RUST_AST_BUILTIN_NODES_H
20 : : #define RUST_AST_BUILTIN_NODES_H
21 : :
22 : : #include "rust-system.h"
23 : : #include "line-map.h"
24 : : #include "optional.h"
25 : : #include "rust-ast.h"
26 : : #include "rust-fmt.h"
27 : :
28 : : namespace Rust {
29 : : namespace AST {
30 : :
31 : : // Definitions, from rustc's `FormatArgs` AST struct
32 : : // https://github.com/rust-lang/rust/blob/1be468815c/compiler/rustc_ast/src/format.rs
33 : : //
34 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
35 : : // └──────────────────────────────────────────────┘
36 : : // FormatArgs
37 : : //
38 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
39 : : // └─────────┘
40 : : // argument
41 : : //
42 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
43 : : // └───────────────────┘
44 : : // template
45 : : //
46 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
47 : : // └────┘└─────────┘└┘
48 : : // pieces
49 : : //
50 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
51 : : // └────┘ └┘
52 : : // literal pieces
53 : : //
54 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
55 : : // └─────────┘
56 : : // placeholder
57 : : //
58 : : // format_args!("hello {abc:.xyz$}!!", abc="world");
59 : : // └─┘ └─┘
60 : : // positions (could be names, numbers, empty, or `*`)
61 : :
62 : : // FIXME: Merge with the class below this one?
63 : 1 : class FormatArgumentKind
64 : : {
65 : : public:
66 : : enum class Kind
67 : : {
68 : : Normal,
69 : : Named,
70 : : Captured,
71 : : } kind;
72 : :
73 : : Identifier &get_ident ()
74 : : {
75 : : rust_assert (kind == Kind::Captured || kind == Kind::Named);
76 : :
77 : : return ident.value ();
78 : : }
79 : :
80 : 1 : FormatArgumentKind (Kind kind, tl::optional<Identifier> ident)
81 : 2 : : kind (kind), ident (ident)
82 : : {}
83 : :
84 : 3 : FormatArgumentKind (const FormatArgumentKind &other)
85 : 3 : {
86 : 3 : kind = other.kind;
87 : 3 : ident = other.ident;
88 : : }
89 : :
90 : : FormatArgumentKind operator= (const FormatArgumentKind &other)
91 : : {
92 : : kind = other.kind;
93 : : ident = other.ident;
94 : :
95 : : return *this;
96 : : }
97 : :
98 : : private:
99 : : tl::optional<Identifier> ident;
100 : : };
101 : :
102 : : class FormatArgument
103 : : {
104 : : public:
105 : 1 : static FormatArgument normal (std::unique_ptr<Expr> expr)
106 : : {
107 : 1 : return FormatArgument (FormatArgumentKind::Kind::Normal, tl::nullopt,
108 : 1 : std::move (expr));
109 : : }
110 : :
111 : 0 : static FormatArgument named (Identifier ident, std::unique_ptr<Expr> expr)
112 : : {
113 : 0 : return FormatArgument (FormatArgumentKind::Kind::Named, ident,
114 : 0 : std::move (expr));
115 : : }
116 : :
117 : : static FormatArgument captured (Identifier ident, std::unique_ptr<Expr> expr)
118 : : {
119 : : return FormatArgument (FormatArgumentKind::Kind::Captured, ident,
120 : : std::move (expr));
121 : : }
122 : :
123 : 2 : FormatArgument (const FormatArgument &other)
124 : 2 : : kind (other.kind), expr (other.expr->clone_expr ())
125 : 2 : {}
126 : :
127 : : FormatArgument operator= (const FormatArgument &other)
128 : : {
129 : : kind = other.kind;
130 : : expr = other.expr->clone_expr ();
131 : :
132 : : return *this;
133 : : }
134 : :
135 : 1 : FormatArgumentKind get_kind () const { return kind; }
136 : 1 : const Expr &get_expr () const { return *expr; }
137 : :
138 : : private:
139 : 1 : FormatArgument (FormatArgumentKind::Kind kind, tl::optional<Identifier> ident,
140 : : std::unique_ptr<Expr> expr)
141 : 1 : : kind (FormatArgumentKind (kind, ident)), expr (std::move (expr))
142 : 1 : {}
143 : :
144 : : FormatArgumentKind kind;
145 : : std::unique_ptr<Expr> expr;
146 : : };
147 : :
148 : 3 : class FormatArguments
149 : : {
150 : : public:
151 : 1 : FormatArguments () {}
152 : 1 : FormatArguments (FormatArguments &&) = default;
153 : 0 : FormatArguments (const FormatArguments &other)
154 : 0 : {
155 : 0 : args = std::vector<FormatArgument> ();
156 : 0 : args.reserve (other.args.size ());
157 : :
158 : 0 : for (const auto &arg : other.args)
159 : 0 : args.emplace_back (arg);
160 : 0 : };
161 : :
162 : : FormatArguments &operator= (const FormatArguments &other) = default;
163 : :
164 : 1 : void push (FormatArgument &&elt) { args.emplace_back (std::move (elt)); }
165 : 1 : const FormatArgument at (size_t idx) const { return args.at (idx); }
166 : :
167 : : private:
168 : : std::vector<FormatArgument> args;
169 : : };
170 : :
171 : : // TODO: Format documentation better
172 : : // Having a separate AST node for `format_args!()` expansion allows some
173 : : // important optimizations which help reduce generated code a lot. For example,
174 : : // turning `format_args!("a {} {} {}", 15, "hey", 'a')` directly into
175 : : // `format_args!("a 15 hey a")`, since all arguments are literals. Or,
176 : : // flattening imbricated `format_args!()` calls: `format_args!("heyo {}",
177 : : // format_args!("result: {}", some_result))` -> `format_args!("heyo result: {}",
178 : : // some_result)`
179 : : // FIXME: Move to rust-macro.h
180 : : class FormatArgs : public Expr
181 : : {
182 : : public:
183 : : enum class Newline
184 : : {
185 : : Yes,
186 : : No
187 : : };
188 : :
189 : 1 : FormatArgs (location_t loc, Fmt::Pieces &&template_str,
190 : : FormatArguments &&arguments)
191 : 2 : : loc (loc), template_pieces (std::move (template_str)),
192 : 1 : arguments (std::move (arguments))
193 : 1 : {}
194 : :
195 : : FormatArgs (FormatArgs &&other) = default;
196 : 0 : FormatArgs (const FormatArgs &other) = default;
197 : : FormatArgs &operator= (const FormatArgs &other) = default;
198 : :
199 : : void accept_vis (AST::ASTVisitor &vis) override;
200 : :
201 : : const Fmt::Pieces &get_template () const { return template_pieces; }
202 : 1 : const FormatArguments &get_arguments () const { return arguments; }
203 : : virtual location_t get_locus () const override;
204 : :
205 : : private:
206 : : location_t loc;
207 : : // FIXME: This probably needs to be a separate type - it is one in rustc's
208 : : // expansion of format_args!(). There is extra handling associated with it.
209 : : // we can maybe do that in rust-fmt.cc? in collect_pieces()? like do the
210 : : // transformation into something we can handle better
211 : : Fmt::Pieces template_pieces;
212 : : FormatArguments arguments;
213 : :
214 : : bool marked_for_strip = false;
215 : :
216 : : protected:
217 : : virtual std::string as_string () const override;
218 : : virtual bool is_expr_without_block () const override;
219 : : virtual void mark_for_strip () override;
220 : : virtual bool is_marked_for_strip () const override;
221 : : virtual std::vector<Attribute> &get_outer_attrs () override;
222 : : virtual void set_outer_attrs (std::vector<Attribute>) override;
223 : : virtual Expr *clone_expr_impl () const override;
224 : : };
225 : :
226 : : } // namespace AST
227 : : } // namespace Rust
228 : :
229 : : #endif // ! RUST_AST_BUILTIN_NODES_H
|