Skip to content

Commit 8770c7f

Browse files
authored
Rework ASTDumper (#68438)
This PR refactors the ASTDumper to make it more structured, less mistake-prone, and more amenable to future changes. For example: ```cpp // Before: void visitUnresolvedDotExpr(UnresolvedDotExpr *E) { printCommon(E, "unresolved_dot_expr") << " field '" << E->getName() << "'"; PrintWithColorRAII(OS, ExprModifierColor) << " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind()); if (E->getBase()) { OS << '\n'; printRec(E->getBase()); } PrintWithColorRAII(OS, ParenthesisColor) << ')'; } // After: void visitUnresolvedDotExpr(UnresolvedDotExpr *E, StringRef label) { printCommon(E, "unresolved_dot_expr", label); printFieldQuoted(E->getName(), "field"); printField(E->getFunctionRefKind(), "function_ref", ExprModifierColor); if (E->getBase()) { printRec(E->getBase()); } printFoot(); } ``` * Values are printed through calls to base class methods, rather than direct access to the underlying `raw_ostream`. * These methods tend to reduce the chances of bugs like missing/extra spaces or newlines, too much/too little indentation, etc. * More values are quoted, and unprintable/non-ASCII characters in quoted values are escaped before printing. * Infrastructure to label child nodes now exists. * Some weird breaks from the normal "style", like `PatternBindingDecl`'s original and processed initializers, have been brought into line. * Some types that previously used ad-hoc dumping functions, like conformances and substitution maps, are now structured similarly to the dumper classes. * I've fixed the odd dumping bug along the way. For example, distributed actors were only marked `actor`, not `distributed actor`. This PR doesn't change the overall style of AST dumps; they're still pseudo-S-expressions. But the logic that implements this style is now isolated into a relatively small base class, making it feasible to introduce e.g. JSON dumping in the future.
1 parent 51c676c commit 8770c7f

File tree

50 files changed

+3071
-3069
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3071
-3069
lines changed

include/swift/AST/Expr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,9 @@ class ObjectLiteralExpr final : public LiteralExpr {
11181118
#include "swift/AST/TokenKinds.def"
11191119
};
11201120

1121+
static StringRef
1122+
getLiteralKindPlainName(ObjectLiteralExpr::LiteralKind kind);
1123+
11211124
private:
11221125
ArgumentList *ArgList;
11231126
SourceLoc PoundLoc;

include/swift/AST/StorageImpl.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ enum class ReadImplKind {
203203
};
204204
enum { NumReadImplKindBits = 4 };
205205

206-
StringRef getReadImplKindName(ReadImplKind kind);
207-
208206
/// How are simple write accesses implemented?
209207
enum class WriteImplKind {
210208
/// It's immutable.
@@ -231,8 +229,6 @@ enum class WriteImplKind {
231229
};
232230
enum { NumWriteImplKindBits = 4 };
233231

234-
StringRef getWriteImplKindName(WriteImplKind kind);
235-
236232
/// How are read-write accesses implemented?
237233
enum class ReadWriteImplKind {
238234
/// It's immutable.
@@ -258,8 +254,6 @@ enum class ReadWriteImplKind {
258254
};
259255
enum { NumReadWriteImplKindBits = 4 };
260256

261-
StringRef getReadWriteImplKindName(ReadWriteImplKind kind);
262-
263257
class StorageImplInfo {
264258
using IntType = uint16_t;
265259
static_assert(NumReadImplKindBits + NumWriteImplKindBits

include/swift/AST/TypeRepr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
189189
void print(raw_ostream &OS, const PrintOptions &Opts = PrintOptions()) const;
190190
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
191191
SWIFT_DEBUG_DUMP;
192+
void dump(raw_ostream &OS, unsigned indent = 0) const;
192193
};
193194

194195
/// A TypeRepr for a type with a syntax error. Can be used both as a

0 commit comments

Comments
 (0)