1 // Copyright (C) 2020-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/>.
19 #ifndef RUST_AST_COLLECTOR_H
20 #define RUST_AST_COLLECTOR_H
22 #include "rust-token.h"
23 #include "rust-ast-visitor.h"
25 #include "rust-ast-full.h"
41 CollectItem (TokenPtr token
) : token (token
), kind (Kind::Token
) {}
42 CollectItem (std::string comment
) : comment (comment
), kind (Kind::Comment
) {}
43 CollectItem (Kind kind
) : kind (kind
) { rust_assert (kind
!= Kind::Token
); }
44 CollectItem (size_t level
) : indent_level (level
), kind (Kind::Indentation
) {}
46 Kind
get_kind () { return kind
; }
50 rust_assert (kind
== Kind::Token
);
54 std::string
get_comment ()
56 rust_assert (kind
== Kind::Comment
);
60 size_t get_indent_level ()
62 rust_assert (kind
== Kind::Indentation
);
73 class TokenCollector
: public ASTVisitor
76 TokenCollector () : indent_level (0) {}
78 bool output_trailing_commas
= false;
80 void visit (AST::Crate
&crate
);
81 void visit (AST::Item
&item
);
83 std::vector
<TokenPtr
> collect_tokens () const;
84 std::vector
<CollectItem
> collect () const;
87 std::vector
<CollectItem
> tokens
;
90 void push (TokenPtr token
) { tokens
.push_back ({token
}); }
93 * Visit all items in given @collection, placing the separator in between but
97 void visit_items_joined_by_separator (T
&collection
,
98 TokenId separator
= COMMA
,
99 size_t start_offset
= 0,
100 size_t end_offset
= 0)
102 if (collection
.size () > start_offset
)
104 visit (collection
.at (start_offset
));
105 auto size
= collection
.size () - end_offset
;
106 for (size_t i
= start_offset
+ 1; i
< size
; i
++)
108 push (Rust::Token::make (separator
, UNDEF_LOCATION
));
109 visit (collection
.at (i
));
115 * Visit item placing end of line after.
117 template <typename T
>
118 void visit_as_line (T
&item
, std::vector
<TokenPtr
> trailing
= {})
122 for (auto &token
: trailing
)
128 * Visit each item in @collection "as line".
132 template <typename T
>
133 void visit_items_as_lines (T
&collection
, std::vector
<TokenPtr
> trailing
= {})
135 for (auto &item
: collection
)
136 visit_as_line (item
, trailing
);
140 * Visit each item in @collection as lines inside a block delimited by braces
141 * with increased indentation. Also includes special handling for empty
142 * collection to print only the delimiters with no new line inside.
144 template <typename T
>
145 void visit_items_as_block (T
&collection
, std::vector
<TokenPtr
> trailing
= {},
146 TokenId left_brace
= LEFT_CURLY
,
147 TokenId right_brace
= RIGHT_CURLY
)
149 push (Rust::Token::make (left_brace
, UNDEF_LOCATION
));
150 if (collection
.empty ())
152 push (Rust::Token::make (right_brace
, UNDEF_LOCATION
));
158 increment_indentation ();
159 visit_items_as_lines (collection
, trailing
);
160 decrement_indentation ();
162 push (Rust::Token::make (right_brace
, UNDEF_LOCATION
));
167 void trailing_comma ();
170 void increment_indentation ();
171 void decrement_indentation ();
172 void comment (std::string comment
);
174 * Visit common items of functions: Parameters, return type, block
176 void visit_function_common (std::unique_ptr
<Type
> &return_type
,
177 std::unique_ptr
<BlockExpr
> &block
);
179 void visit_closure_common (ClosureExpr
&expr
);
181 void visit_loop_common (BaseLoopExpr
&expr
);
185 * Compatibility layer for using the visitor pattern on polymorphic classes
186 * with a unified overload syntax. This allows us to call `visit` both on
187 * types implementing `accept_vis` method and for classes for which the
188 * `visit` method is directly implemented.
190 template <typename T
> void visit (std::unique_ptr
<T
> &node
)
192 node
->accept_vis (*this);
196 * @see visit<std::unique_ptr<T>>
198 template <typename T
> void visit (T
&node
) { node
.accept_vis (*this); }
200 void visit (Visitable
&v
);
201 void visit (LoopLabel
&label
);
203 void visit (Literal
&lit
, location_t locus
= UNDEF_LOCATION
);
205 void visit (FunctionParam
¶m
);
206 void visit (VariadicParam
¶m
);
207 void visit (Attribute
&attrib
);
208 void visit (Visibility
&vis
);
209 void visit (std::vector
<std::unique_ptr
<GenericParam
>> ¶ms
);
210 void visit (TupleField
&field
);
211 void visit (StructField
&field
);
212 void visit (SimplePathSegment
&segment
);
213 void visit (NamedFunctionParam
¶m
);
214 void visit (MacroRule
&rule
);
215 void visit (WhereClause
&rule
);
216 void visit (std::vector
<LifetimeParam
> &for_lifetimes
);
217 void visit (FunctionQualifiers
&qualifiers
);
218 void visit (MaybeNamedParam
¶m
);
219 void visit (TypePathFunction
&type_path_fn
);
220 void visit (GenericArgsBinding
&binding
);
221 void visit (GenericArg
&arg
);
224 void visit (Token
&tok
);
225 void visit (DelimTokenTree
&delim_tok_tree
);
226 void visit (AttrInputMetaItemContainer
&input
);
227 void visit (IdentifierExpr
&ident_expr
);
228 void visit (Lifetime
&lifetime
);
229 void visit (LifetimeParam
&lifetime_param
);
230 void visit (ConstGenericParam
&const_param
);
233 void visit (SimplePath
&path
);
234 void visit (PathExprSegment
&segment
);
235 void visit (PathIdentSegment
&segment
);
236 void visit (PathInExpression
&path
);
237 void visit (TypePathSegment
&segment
);
238 void visit (TypePathSegmentGeneric
&segment
);
239 void visit (TypePathSegmentFunction
&segment
);
240 void visit (TypePath
&path
);
241 void visit (QualifiedPathType
&path
);
242 void visit (QualifiedPathInExpression
&path
);
243 void visit (QualifiedPathInType
&path
);
246 void visit (LiteralExpr
&expr
);
247 void visit (AttrInputLiteral
&attr_input
);
248 void visit (AttrInputMacro
&attr_input
);
249 void visit (MetaItemLitExpr
&meta_item
);
250 void visit (MetaItemPathLit
&meta_item
);
251 void visit (BorrowExpr
&expr
);
252 void visit (DereferenceExpr
&expr
);
253 void visit (ErrorPropagationExpr
&expr
);
254 void visit (NegationExpr
&expr
);
255 void visit (ArithmeticOrLogicalExpr
&expr
);
256 void visit (ComparisonExpr
&expr
);
257 void visit (LazyBooleanExpr
&expr
);
258 void visit (TypeCastExpr
&expr
);
259 void visit (AssignmentExpr
&expr
);
260 void visit (CompoundAssignmentExpr
&expr
);
261 void visit (GroupedExpr
&expr
);
262 void visit (ArrayElemsValues
&elems
);
263 void visit (ArrayElemsCopied
&elems
);
264 void visit (ArrayExpr
&expr
);
265 void visit (ArrayIndexExpr
&expr
);
266 void visit (TupleExpr
&expr
);
267 void visit (TupleIndexExpr
&expr
);
268 void visit (StructExprStruct
&expr
);
269 void visit (StructExprFieldIdentifier
&field
);
270 void visit (StructExprFieldIdentifierValue
&field
);
271 void visit (StructExprFieldIndexValue
&field
);
272 void visit (StructBase
&base
);
273 void visit (StructExprStructFields
&expr
);
274 void visit (StructExprStructBase
&expr
);
275 void visit (CallExpr
&expr
);
276 void visit (MethodCallExpr
&expr
);
277 void visit (FieldAccessExpr
&expr
);
278 void visit (ClosureParam
¶m
);
279 void visit (ClosureExprInner
&expr
);
280 void visit (BlockExpr
&expr
);
281 void visit (ClosureExprInnerTyped
&expr
);
282 void visit (ContinueExpr
&expr
);
283 void visit (BreakExpr
&expr
);
284 void visit (RangeFromToExpr
&expr
);
285 void visit (RangeFromExpr
&expr
);
286 void visit (RangeToExpr
&expr
);
287 void visit (RangeFullExpr
&expr
);
288 void visit (RangeFromToInclExpr
&expr
);
289 void visit (RangeToInclExpr
&expr
);
290 void visit (ReturnExpr
&expr
);
291 void visit (UnsafeBlockExpr
&expr
);
292 void visit (LoopExpr
&expr
);
293 void visit (WhileLoopExpr
&expr
);
294 void visit (WhileLetLoopExpr
&expr
);
295 void visit (ForLoopExpr
&expr
);
296 void visit (IfExpr
&expr
);
297 void visit (IfExprConseqElse
&expr
);
298 void visit (IfLetExpr
&expr
);
299 void visit (IfLetExprConseqElse
&expr
);
300 void visit (MatchArm
&arm
);
301 void visit (MatchCase
&arm
);
302 void visit (MatchExpr
&expr
);
303 void visit (AwaitExpr
&expr
);
304 void visit (AsyncBlockExpr
&expr
);
307 void visit (TypeParam
¶m
);
308 void visit (LifetimeWhereClauseItem
&item
);
309 void visit (TypeBoundWhereClauseItem
&item
);
310 void visit (Module
&module
);
311 void visit (ExternCrate
&crate
);
312 void visit (UseTreeGlob
&use_tree
);
313 void visit (UseTreeList
&use_tree
);
314 void visit (UseTreeRebind
&use_tree
);
315 void visit (UseDeclaration
&use_decl
);
316 void visit (Function
&function
);
317 void visit (TypeAlias
&type_alias
);
318 void visit (StructStruct
&struct_item
);
319 void visit (TupleStruct
&tuple_struct
);
320 void visit (EnumItem
&item
);
321 void visit (EnumItemTuple
&item
);
322 void visit (EnumItemStruct
&item
);
323 void visit (EnumItemDiscriminant
&item
);
324 void visit (Enum
&enumeration
);
325 void visit (Union
&union_item
);
326 void visit (ConstantItem
&const_item
);
327 void visit (StaticItem
&static_item
);
328 void visit (SelfParam
¶m
);
329 void visit (TraitItemConst
&item
);
330 void visit (TraitItemType
&item
);
331 void visit (Trait
&trait
);
332 void visit (InherentImpl
&impl
);
333 void visit (TraitImpl
&impl
);
334 void visit (ExternalTypeItem
&item
);
335 void visit (ExternalStaticItem
&item
);
336 void visit (ExternBlock
&block
);
339 void visit (MacroMatchFragment
&match
);
340 void visit (MacroMatchRepetition
&match
);
341 void visit (MacroMatcher
&matcher
);
342 void visit (MacroRulesDefinition
&rules_def
);
343 void visit (MacroInvocData
&invoc_data
);
344 void visit (MacroInvocation
¯o_invoc
);
345 void visit (MetaItemPath
&meta_item
);
346 void visit (MetaItemSeq
&meta_item
);
347 void visit (MetaWord
&meta_item
);
348 void visit (MetaNameValueStr
&meta_item
);
349 void visit (MetaListPaths
&meta_item
);
350 void visit (MetaListNameValueStr
&meta_item
);
353 void visit (LiteralPattern
&pattern
);
354 void visit (IdentifierPattern
&pattern
);
355 void visit (WildcardPattern
&pattern
);
356 void visit (RestPattern
&pattern
);
357 // void visit(RangePatternBound& bound);
358 void visit (RangePatternBoundLiteral
&bound
);
359 void visit (RangePatternBoundPath
&bound
);
360 void visit (RangePatternBoundQualPath
&bound
);
361 void visit (RangePattern
&pattern
);
362 void visit (ReferencePattern
&pattern
);
363 // void visit(StructPatternField& field);
364 void visit (StructPatternFieldTuplePat
&field
);
365 void visit (StructPatternFieldIdentPat
&field
);
366 void visit (StructPatternFieldIdent
&field
);
367 void visit (StructPattern
&pattern
);
368 // void visit(TupleStructItems& tuple_items);
369 void visit (TupleStructItemsNoRange
&tuple_items
);
370 void visit (TupleStructItemsRange
&tuple_items
);
371 void visit (TupleStructPattern
&pattern
);
372 // void visit(TuplePatternItems& tuple_items);
373 void visit (TuplePatternItemsMultiple
&tuple_items
);
374 void visit (TuplePatternItemsRanged
&tuple_items
);
375 void visit (TuplePattern
&pattern
);
376 void visit (GroupedPattern
&pattern
);
377 void visit (SlicePattern
&pattern
);
378 void visit (AltPattern
&pattern
);
381 void visit (EmptyStmt
&stmt
);
382 void visit (LetStmt
&stmt
);
383 void visit (ExprStmt
&stmt
);
386 void visit (TraitBound
&bound
);
387 void visit (ImplTraitType
&type
);
388 void visit (TraitObjectType
&type
);
389 void visit (ParenthesisedType
&type
);
390 void visit (ImplTraitTypeOneBound
&type
);
391 void visit (TraitObjectTypeOneBound
&type
);
392 void visit (TupleType
&type
);
393 void visit (NeverType
&type
);
394 void visit (RawPointerType
&type
);
395 void visit (ReferenceType
&type
);
396 void visit (ArrayType
&type
);
397 void visit (SliceType
&type
);
398 void visit (InferredType
&type
);
399 void visit (BareFunctionType
&type
);
401 void visit (FormatArgs
&fmt
);
407 #endif // !RUST_AST_COLLECTOR_H