| 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 |
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 |
FormatArgumentKind (Kind kind, tl::optional<Identifier> ident) |
| 81 |
: kind (kind), ident (ident) |
| 82 |
{} |
| 83 |
|
| 84 |
FormatArgumentKind (const FormatArgumentKind &other) |
| 85 |
{ |
| 86 |
kind = other.kind; |
| 87 |
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 |
static FormatArgument normal (std::unique_ptr<Expr> expr) |
| 106 |
{ |
| 107 |
return FormatArgument (FormatArgumentKind::Kind::Normal, tl::nullopt, |
| 108 |
std::move (expr)); |
| 109 |
} |
| 110 |
|
| 111 |
static FormatArgument named (Identifier ident, std::unique_ptr<Expr> expr) |
| 112 |
{ |
| 113 |
return FormatArgument (FormatArgumentKind::Kind::Named, ident, |
| 114 |
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 |
FormatArgument (const FormatArgument &other) |
| 124 |
: kind (other.kind), expr (other.expr->clone_expr ()) |
| 125 |
{} |
| 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 |
FormatArgumentKind get_kind () const { return kind; } |
| 136 |
const Expr &get_expr () const { return *expr; } |
| 137 |
|
| 138 |
private: |
| 139 |
FormatArgument (FormatArgumentKind::Kind kind, tl::optional<Identifier> ident, |
| 140 |
std::unique_ptr<Expr> expr) |
| 141 |
: kind (FormatArgumentKind (kind, ident)), expr (std::move (expr)) |
| 142 |
{} |
| 143 |
|
| 144 |
FormatArgumentKind kind; |
| 145 |
std::unique_ptr<Expr> expr; |
| 146 |
}; |
| 147 |
|
| 148 |
class FormatArguments |
| 149 |
{ |
| 150 |
public: |
| 151 |
FormatArguments () {} |
| 152 |
FormatArguments (FormatArguments &&) = default; |
| 153 |
FormatArguments (const FormatArguments &other) |
| 154 |
{ |
| 155 |
args = std::vector<FormatArgument> (); |
| 156 |
args.reserve (other.args.size ()); |
| 157 |
|
| 158 |
for (const auto &arg : other.args) |
| 159 |
args.emplace_back (arg); |
| 160 |
}; |
| 161 |
|
| 162 |
FormatArguments &operator= (const FormatArguments &other) = default; |
| 163 |
|
| 164 |
void push (FormatArgument &&elt) { args.emplace_back (std::move (elt)); } |
| 165 |
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 |
FormatArgs (location_t loc, Fmt::Pieces &&template_str, |
| 190 |
FormatArguments &&arguments) |
| 191 |
: loc (loc), template_pieces (std::move (template_str)), |
| 192 |
arguments (std::move (arguments)) |
| 193 |
{} |
| 194 |
|
| 195 |
FormatArgs (FormatArgs &&other) = default; |
| 196 |
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 |
const FormatArguments &get_arguments () const { return arguments; } |
| 203 |
virtual location_t get_locus () const override; |
| 204 |
|
| 205 |
Expr::Kind get_expr_kind () const override { return Expr::Kind::FormatArgs; } |
| 206 |
|
| 207 |
private: |
| 208 |
location_t loc; |
| 209 |
// FIXME: This probably needs to be a separate type - it is one in rustc's |
| 210 |
// expansion of format_args!(). There is extra handling associated with it. |
| 211 |
// we can maybe do that in rust-fmt.cc? in collect_pieces()? like do the |
| 212 |
// transformation into something we can handle better |
| 213 |
Fmt::Pieces template_pieces; |
| 214 |
FormatArguments arguments; |
| 215 |
|
| 216 |
bool marked_for_strip = false; |
| 217 |
|
| 218 |
protected: |
| 219 |
virtual std::string as_string () const override; |
| 220 |
virtual bool is_expr_without_block () const override; |
| 221 |
virtual void mark_for_strip () override; |
| 222 |
virtual bool is_marked_for_strip () const override; |
| 223 |
virtual std::vector<Attribute> &get_outer_attrs () override; |
| 224 |
virtual void set_outer_attrs (std::vector<Attribute>) override; |
| 225 |
virtual Expr *clone_expr_impl () const override; |
| 226 |
}; |
| 227 |
|
| 228 |
} // namespace AST |
| 229 |
} // namespace Rust |
| 230 |
|
| 231 |
#endif // ! RUST_AST_BUILTIN_NODES_H |