1 // Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
22 #include "rust-system.h"
25 // FIXME: How to encode Option?
37 std::string
to_string () const;
40 /// Enum of alignments which are supported.
43 /// The value will be aligned to the left.
45 /// The value will be aligned to the right.
47 /// The value will be aligned in the center.
49 /// The value will take on a default alignment.
53 /// Enum for the debug hex flags.
56 /// The `x` flag in `{:x?}`.
58 /// The `X` flag in `{:X?}`.
62 /// Enum for the sign flags.
71 /// Enum describing where an argument for a format can be located.
76 /// The argument is implied to be located at an index
78 /// The argument is located at a specific index given in the format,
80 /// The argument has a name.
84 struct ArgumentImplicitlyIs_Body
89 struct ArgumentIs_Body
94 struct ArgumentNamed_Body
102 ArgumentImplicitlyIs_Body argument_implicitly_is
;
103 ArgumentIs_Body argument_is
;
104 ArgumentNamed_Body argument_named
;
108 /// Range inside of a `Span` used for diagnostics when we only have access to
109 /// relative positions.
116 /// A count is used for the precision and width parameters of an integer, and
117 /// can reference either an argument or a literal integer.
122 /// The count is specified explicitly.
124 /// The count is specified by the argument with the given name.
126 /// The count is specified by the argument at the given index.
128 /// The count is specified by a star (like in `{:.*}`) that refers to the
129 /// argument at the given index.
131 /// The count is implied and cannot be explicitly specified.
140 struct CountIsName_Body
146 struct CountIsParam_Body
151 struct CountIsStar_Body
159 CountIs_Body count_is
;
160 CountIsName_Body count_is_name
;
161 CountIsParam_Body count_is_param
;
162 CountIsStar_Body count_is_star
;
166 /// Specification for the formatting of an argument in the format string.
169 /// Optionally specified character to fill alignment with.
170 const uint32_t *fill
;
171 /// Span of the optionally specified fill character.
172 const InnerSpan
*fill_span
;
173 /// Optionally specified alignment.
175 /// The `+` or `-` flag.
181 /// The `x` or `X` flag. (Only for `Debug`.)
182 const DebugHex
*debug_hex
;
183 /// The integer precision to use.
185 /// The span of the precision formatting flag (for diagnostics).
186 const InnerSpan
*precision_span
;
187 /// The string width requested for the resulting format.
189 /// The span of the width formatting flag (for diagnostics).
190 const InnerSpan
*width_span
;
191 /// The descriptor string representing the name of the format desired for
192 /// this argument, this can be empty or any number of characters, although
193 /// it is required to be one word.
195 /// The span of the descriptor string (for diagnostics).
196 const InnerSpan
*ty_span
;
199 /// Representation of an argument specification.
202 /// Where to find this argument
204 /// The span of the position indicator. Includes any whitespace in implicit
205 /// positions (`{ }`).
206 InnerSpan position_span
;
207 /// How to format the argument
211 /// A piece is a portion of the format string which represents the next part
212 /// to emit. These are emitted as a stream by the `Parser` class.
217 /// A literal string which should directly be emitted
219 /// This describes that formatting should process the next argument (as
220 /// specified inside) for emission.
229 struct NextArgument_Body
238 NextArgument_Body next_argument
;
244 const Piece
*base_ptr
;
251 const unsigned char *ptr
;
256 struct FormatArgsHandle
258 PieceSlice piece_slice
;
259 RustString rust_string
;
265 collect_pieces (const char *input
, bool append_newline
);
268 clone_pieces (const FormatArgsHandle
&);
270 void destroy_pieces (FormatArgsHandle
);
278 static Pieces
collect (const std::string
&to_parse
, bool append_newline
);
281 Pieces (const Pieces
&other
);
282 Pieces
&operator= (const Pieces
&other
);
284 Pieces (Pieces
&&other
);
286 const std::vector
<ffi::Piece
> &get_pieces () const { return pieces_vector
; }
289 // slice = clone_pieces (&other.slice);
290 // to_parse = other.to_parse;
296 Pieces (ffi::FormatArgsHandle handle
, std::vector
<ffi::Piece
> &&pieces_vector
)
297 : pieces_vector (std::move (pieces_vector
)), handle (handle
)
300 std::vector
<ffi::Piece
> pieces_vector
;
302 // this memory is held for FFI reasons - it needs to be released and cloned
303 // precisely, so try to not access it/modify it if possible. you should
304 // instead work with `pieces_vector`
305 ffi::FormatArgsHandle handle
;
311 #endif // !RUST_FMT_H