1use rustc_data_structures::fx::FxHashMap;
2use rustc_macros::{Decodable, Encodable};
3use rustc_span::{Ident, Span, Symbol};
4
5use crate::Expr;
6use crate::ptr::P;
7use crate::token::LitKind;
8
9#[derive(Clone, Encodable, Decodable, Debug)]
45pub struct FormatArgs {
46 pub span: Span,
47 pub template: Vec<FormatArgsPiece>,
48 pub arguments: FormatArguments,
49 pub uncooked_fmt_str: (LitKind, Symbol),
53 pub is_source_literal: bool,
61}
62
63#[derive(Clone, Encodable, Decodable, Debug)]
67pub enum FormatArgsPiece {
68 Literal(Symbol),
69 Placeholder(FormatPlaceholder),
70}
71
72#[derive(Clone, Encodable, Decodable, Debug)]
77pub struct FormatArguments {
78 arguments: Vec<FormatArgument>,
79 num_unnamed_args: usize,
80 num_explicit_args: usize,
81 names: FxHashMap<Symbol, usize>,
82}
83
84impl FormatArguments {
85 pub fn new() -> Self {
86 Self {
87 arguments: Vec::new(),
88 names: FxHashMap::default(),
89 num_unnamed_args: 0,
90 num_explicit_args: 0,
91 }
92 }
93
94 pub fn add(&mut self, arg: FormatArgument) -> usize {
95 let index = self.arguments.len();
96 if let Some(name) = arg.kind.ident() {
97 self.names.insert(name.name, index);
98 } else if self.names.is_empty() {
99 self.num_unnamed_args += 1;
102 }
103 if !matches!(arg.kind, FormatArgumentKind::Captured(..)) {
104 assert_eq!(
107 self.num_explicit_args,
108 self.arguments.len(),
109 "captured arguments must be added last"
110 );
111 self.num_explicit_args += 1;
112 }
113 self.arguments.push(arg);
114 index
115 }
116
117 pub fn by_name(&self, name: Symbol) -> Option<(usize, &FormatArgument)> {
118 let i = *self.names.get(&name)?;
119 Some((i, &self.arguments[i]))
120 }
121
122 pub fn by_index(&self, i: usize) -> Option<&FormatArgument> {
123 (i < self.num_explicit_args).then(|| &self.arguments[i])
124 }
125
126 pub fn unnamed_args(&self) -> &[FormatArgument] {
127 &self.arguments[..self.num_unnamed_args]
128 }
129
130 pub fn named_args(&self) -> &[FormatArgument] {
131 &self.arguments[self.num_unnamed_args..self.num_explicit_args]
132 }
133
134 pub fn explicit_args(&self) -> &[FormatArgument] {
135 &self.arguments[..self.num_explicit_args]
136 }
137
138 pub fn all_args(&self) -> &[FormatArgument] {
139 &self.arguments[..]
140 }
141
142 pub fn all_args_mut(&mut self) -> &mut Vec<FormatArgument> {
143 &mut self.arguments
144 }
145}
146
147#[derive(Clone, Encodable, Decodable, Debug)]
148pub struct FormatArgument {
149 pub kind: FormatArgumentKind,
150 pub expr: P<Expr>,
151}
152
153#[derive(Clone, Encodable, Decodable, Debug)]
154pub enum FormatArgumentKind {
155 Normal,
157 Named(Ident),
159 Captured(Ident),
161}
162
163impl FormatArgumentKind {
164 pub fn ident(&self) -> Option<Ident> {
165 match self {
166 &Self::Normal => None,
167 &Self::Named(id) => Some(id),
168 &Self::Captured(id) => Some(id),
169 }
170 }
171}
172
173#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
174pub struct FormatPlaceholder {
175 pub argument: FormatArgPosition,
177 pub span: Option<Span>,
179 pub format_trait: FormatTrait,
181 pub format_options: FormatOptions,
183}
184
185#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
186pub struct FormatArgPosition {
187 pub index: Result<usize, usize>,
190 pub kind: FormatArgPositionKind,
192 pub span: Option<Span>,
194}
195
196#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
197pub enum FormatArgPositionKind {
198 Implicit,
200 Number,
202 Named,
204}
205
206#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq, Hash)]
207pub enum FormatTrait {
208 Display,
210 Debug,
212 LowerExp,
214 UpperExp,
216 Octal,
218 Pointer,
220 Binary,
222 LowerHex,
224 UpperHex,
226}
227
228#[derive(Clone, Encodable, Decodable, Default, Debug, PartialEq, Eq)]
229pub struct FormatOptions {
230 pub width: Option<FormatCount>,
232 pub precision: Option<FormatCount>,
234 pub alignment: Option<FormatAlignment>,
236 pub fill: Option<char>,
238 pub sign: Option<FormatSign>,
240 pub alternate: bool,
242 pub zero_pad: bool,
244 pub debug_hex: Option<FormatDebugHex>,
246}
247
248#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
249pub enum FormatSign {
250 Plus,
252 Minus,
254}
255
256#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
257pub enum FormatDebugHex {
258 Lower,
260 Upper,
262}
263
264#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
265pub enum FormatAlignment {
266 Left,
268 Right,
270 Center,
272}
273
274#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
275pub enum FormatCount {
276 Literal(u16),
278 Argument(FormatArgPosition),
280}