1 // Copyright (C) 2020-2023 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 #include "rust-ast-visitor.h"
20 #include "rust-ast.h"
21 #include "rust-ast-full.h"
22
23 #ifndef RUST_AST_DUMP_H
24 #define RUST_AST_DUMP_H
25
26 namespace Rust {
27 namespace AST {
28
29 // TODO: We might want to reuse this class somewhere else
30 class Indent
31 {
32 public:
33 Indent ();
34
35 friend std::ostream &operator<< (std::ostream &stream, const Indent &indent);
36
37 void increment ();
38 void decrement ();
39
40 private:
41 size_t tabs;
42 };
43
44 class Dump : public ASTVisitor
45 {
46 public:
47 Dump (std::ostream &stream);
48
49 /**
50 * Run the visitor on an entire crate and its items
51 */
52 void go (AST::Crate &crate);
53 void go (AST::Item &item);
54
55 /**
56 * Use the AST Dump as a debugging tool
57 */
58 template <typename T> static void debug (T &instance)
59 {
60 auto dump = Dump (std::cerr);
61
62 std::cerr << '\n';
63 instance.accept_vis (dump);
64 std::cerr << '\n';
65 }
66 template <typename T> static void debug (std::unique_ptr<T> &instance)
67 {
68 debug (*instance);
69 }
70
71 private:
72 std::ostream &stream;
73 Indent indentation;
74
75 /**
76 * Compatibility layer for using the visitor pattern on polymorphic classes
77 * with a unified overload syntax. This allows us to call `visit` both on
78 * types implementing `accept_vis` method and for classes for which the
79 * `visit` method is directly implemented.
80 */
81 template <typename T> void visit (std::unique_ptr<T> &node);
82
83 /**
84 * @see visit<std::unique_ptr<T>>
85 */
86 template <typename T> void visit (T &node);
87
88 /**
89 * Visit all items in given @collection, placing the separator in between but
90 * not at the end.
91 * Start and end offset allow to visit only a "slice" from the collection.
92 */
93 template <typename T>
94 void visit_items_joined_by_separator (T &collection,
95 const std::string &separator = "",
96 size_t start_offset = 0,
97 size_t end_offset = 0);
98
99 /**
100 * Visit item placing indentation before and trailing string + end of line
101 * after.
102 */
103 template <typename T>
104 void visit_as_line (T &item, const std::string &trailing = "");
105
106 /**
107 * Visit each item in @collection "as line".
108 *
109 * @see visit_as_line
110 */
111 template <typename T>
112 void visit_items_as_lines (T &collection, const std::string &trailing = "");
113
114 /**
115 * Visit each item in @collection as lines inside a block delimited by braces
116 * with increased indentation. Also includes special handling for empty
117 * collection to print only the delimiters with no new line inside.
118 */
119 template <typename T>
120 void visit_items_as_block (T &collection, const std::string &line_trailing,
121 char left_brace = '{', char right_brace = '}');
122
123 /**
124 * Visit common items of functions: Parameters, return type, block
125 */
126 void visit_function_common (std::unique_ptr<Type> &return_type,
127 std::unique_ptr<BlockExpr> &block);
128
129 void visit (FunctionParam ¶m);
130 void visit (Attribute &attrib);
131 void visit (Visibility &vis);
132 void visit (std::vector<std::unique_ptr<GenericParam>> ¶ms);
133 void visit (TupleField &field);
134 void visit (StructField &field);
135 void visit (SimplePathSegment &segment);
136 void visit (NamedFunctionParam ¶m);
137 void visit (MacroRule &rule);
138 void visit (WhereClause &rule);
139 void visit (std::vector<LifetimeParam> &for_lifetimes);
140 void visit (FunctionQualifiers &qualifiers);
141 void visit (MaybeNamedParam ¶m);
142 void visit (TypePathFunction &type_path_fn);
143 void visit (GenericArgsBinding &binding);
144 void visit (GenericArg &arg);
145
146 // rust-ast.h
147 void visit (Token &tok);
148 void visit (DelimTokenTree &delim_tok_tree);
149 void visit (AttrInputMetaItemContainer &input);
150 void visit (IdentifierExpr &ident_expr);
151 void visit (Lifetime &lifetime);
152 void visit (LifetimeParam &lifetime_param);
153 void visit (ConstGenericParam &const_param);
154
155 // rust-path.h
156 void visit (PathInExpression &path);
157 void visit (TypePathSegment &segment);
158 void visit (TypePathSegmentGeneric &segment);
159 void visit (TypePathSegmentFunction &segment);
160 void visit (TypePath &path);
161 void visit (QualifiedPathInExpression &path);
162 void visit (QualifiedPathInType &path);
163
164 // rust-expr.h
165 void visit (LiteralExpr &expr);
166 void visit (AttrInputLiteral &attr_input);
167 void visit (MetaItemLitExpr &meta_item);
168 void visit (MetaItemPathLit &meta_item);
169 void visit (BorrowExpr &expr);
170 void visit (DereferenceExpr &expr);
171 void visit (ErrorPropagationExpr &expr);
172 void visit (NegationExpr &expr);
173 void visit (ArithmeticOrLogicalExpr &expr);
174 void visit (ComparisonExpr &expr);
175 void visit (LazyBooleanExpr &expr);
176 void visit (TypeCastExpr &expr);
177 void visit (AssignmentExpr &expr);
178 void visit (CompoundAssignmentExpr &expr);
179 void visit (GroupedExpr &expr);
180 void visit (ArrayElemsValues &elems);
181 void visit (ArrayElemsCopied &elems);
182 void visit (ArrayExpr &expr);
183 void visit (ArrayIndexExpr &expr);
184 void visit (TupleExpr &expr);
185 void visit (TupleIndexExpr &expr);
186 void visit (StructExprStruct &expr);
187 void visit (StructExprFieldIdentifier &field);
188 void visit (StructExprFieldIdentifierValue &field);
189 void visit (StructExprFieldIndexValue &field);
190 void visit (StructExprStructFields &expr);
191 void visit (StructExprStructBase &expr);
192 void visit (CallExpr &expr);
193 void visit (MethodCallExpr &expr);
194 void visit (FieldAccessExpr &expr);
195 void visit (ClosureExprInner &expr);
196 void visit (BlockExpr &expr);
197 void visit (ClosureExprInnerTyped &expr);
198 void visit (ContinueExpr &expr);
199 void visit (BreakExpr &expr);
200 void visit (RangeFromToExpr &expr);
201 void visit (RangeFromExpr &expr);
202 void visit (RangeToExpr &expr);
203 void visit (RangeFullExpr &expr);
204 void visit (RangeFromToInclExpr &expr);
205 void visit (RangeToInclExpr &expr);
206 void visit (ReturnExpr &expr);
207 void visit (UnsafeBlockExpr &expr);
208 void visit (LoopExpr &expr);
209 void visit (WhileLoopExpr &expr);
210 void visit (WhileLetLoopExpr &expr);
211 void visit (ForLoopExpr &expr);
212 void visit (IfExpr &expr);
213 void visit (IfExprConseqElse &expr);
214 void visit (IfExprConseqIf &expr);
215 void visit (IfExprConseqIfLet &expr);
216 void visit (IfLetExpr &expr);
217 void visit (IfLetExprConseqElse &expr);
218 void visit (IfLetExprConseqIf &expr);
219 void visit (IfLetExprConseqIfLet &expr);
220 void visit (MatchExpr &expr);
221 void visit (AwaitExpr &expr);
222 void visit (AsyncBlockExpr &expr);
223
224 // rust-item.h
225 void visit (TypeParam ¶m);
226 void visit (LifetimeWhereClauseItem &item);
227 void visit (TypeBoundWhereClauseItem &item);
228 void visit (Method &method);
229 void visit (Module &module);
230 void visit (ExternCrate &crate);
231 void visit (UseTreeGlob &use_tree);
232 void visit (UseTreeList &use_tree);
233 void visit (UseTreeRebind &use_tree);
234 void visit (UseDeclaration &use_decl);
235 void visit (Function &function);
236 void visit (TypeAlias &type_alias);
237 void visit (StructStruct &struct_item);
238 void visit (TupleStruct &tuple_struct);
239 void visit (EnumItem &item);
240 void visit (EnumItemTuple &item);
241 void visit (EnumItemStruct &item);
242 void visit (EnumItemDiscriminant &item);
243 void visit (Enum &enum_item);
244 void visit (Union &union_item);
245 void visit (ConstantItem &const_item);
246 void visit (StaticItem &static_item);
247 void visit (TraitItemFunc &item);
248 void visit (TraitItemMethod &item);
249 void visit (TraitItemConst &item);
250 void visit (TraitItemType &item);
251 void visit (Trait &trait);
252 void visit (InherentImpl &impl);
253 void visit (TraitImpl &impl);
254 void visit (ExternalStaticItem &item);
255 void visit (ExternalFunctionItem &item);
256 void visit (ExternBlock &block);
257
258 // rust-macro.h
259 void visit (MacroMatchFragment &match);
260 void visit (MacroMatchRepetition &match);
261 void visit (MacroMatcher &matcher);
262 void visit (MacroRulesDefinition &rules_def);
263 void visit (MacroInvocation ¯o_invoc);
264 void visit (MetaItemPath &meta_item);
265 void visit (MetaItemSeq &meta_item);
266 void visit (MetaWord &meta_item);
267 void visit (MetaNameValueStr &meta_item);
268 void visit (MetaListPaths &meta_item);
269 void visit (MetaListNameValueStr &meta_item);
270
271 // rust-pattern.h
272 void visit (LiteralPattern &pattern);
273 void visit (IdentifierPattern &pattern);
274 void visit (WildcardPattern &pattern);
275 // void visit(RangePatternBound& bound);
276 void visit (RangePatternBoundLiteral &bound);
277 void visit (RangePatternBoundPath &bound);
278 void visit (RangePatternBoundQualPath &bound);
279 void visit (RangePattern &pattern);
280 void visit (ReferencePattern &pattern);
281 // void visit(StructPatternField& field);
282 void visit (StructPatternFieldTuplePat &field);
283 void visit (StructPatternFieldIdentPat &field);
284 void visit (StructPatternFieldIdent &field);
285 void visit (StructPattern &pattern);
286 // void visit(TupleStructItems& tuple_items);
287 void visit (TupleStructItemsNoRange &tuple_items);
288 void visit (TupleStructItemsRange &tuple_items);
289 void visit (TupleStructPattern &pattern);
290 // void visit(TuplePatternItems& tuple_items);
291 void visit (TuplePatternItemsMultiple &tuple_items);
292 void visit (TuplePatternItemsRanged &tuple_items);
293 void visit (TuplePattern &pattern);
294 void visit (GroupedPattern &pattern);
295 void visit (SlicePattern &pattern);
296 void visit (AltPattern &pattern);
297
298 // rust-stmt.h
299 void visit (EmptyStmt &stmt);
300 void visit (LetStmt &stmt);
301 void visit (ExprStmtWithoutBlock &stmt);
302 void visit (ExprStmtWithBlock &stmt);
303
304 // rust-type.h
305 void visit (TraitBound &bound);
306 void visit (ImplTraitType &type);
307 void visit (TraitObjectType &type);
308 void visit (ParenthesisedType &type);
309 void visit (ImplTraitTypeOneBound &type);
310 void visit (TraitObjectTypeOneBound &type);
311 void visit (TupleType &type);
312 void visit (NeverType &type);
313 void visit (RawPointerType &type);
314 void visit (ReferenceType &type);
315 void visit (ArrayType &type);
316 void visit (SliceType &type);
317 void visit (InferredType &type);
318 void visit (BareFunctionType &type);
319 };
320
321 } // namespace AST
322 } // namespace Rust
323
324 #endif // !RUST_AST_DUMP_H