Skip to content

Commit 87b8f79

Browse files
committed
[AST] Extend ASTNode to support Pattern/TypeLoc
1 parent f946392 commit 87b8f79

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

include/swift/AST/ASTNode.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ namespace swift {
2929
class Expr;
3030
class Stmt;
3131
class Decl;
32+
class Pattern;
33+
struct TypeLoc;
3234
class DeclContext;
3335
class SourceLoc;
3436
class SourceRange;
3537
class ASTWalker;
3638
enum class ExprKind : uint8_t;
3739
enum class DeclKind : uint8_t;
40+
enum class PatternKind : uint8_t;
3841
enum class StmtKind;
3942

40-
struct ASTNode : public llvm::PointerUnion<Expr*, Stmt*, Decl*> {
43+
struct ASTNode : public llvm::PointerUnion<Expr *, Stmt *, Decl *, Pattern *,
44+
TypeLoc *> {
4145
// Inherit the constructors from PointerUnion.
4246
using PointerUnion::PointerUnion;
4347

@@ -61,6 +65,7 @@ namespace swift {
6165
FUNC(Stmt)
6266
FUNC(Expr)
6367
FUNC(Decl)
68+
FUNC(Pattern)
6469
#undef FUNC
6570

6671
SWIFT_DEBUG_DUMP;

include/swift/AST/Pattern.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ class alignas(8) Pattern {
223223
void print(llvm::raw_ostream &OS,
224224
const PrintOptions &Options = PrintOptions()) const;
225225
SWIFT_DEBUG_DUMP;
226-
226+
void dump(raw_ostream &OS, unsigned Indent = 0) const;
227+
227228
/// walk - This recursively walks the AST rooted at this pattern.
228229
Pattern *walk(ASTWalker &walker);
229230
Pattern *walk(ASTWalker &&walker) { return walk(walker); }

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,8 +1426,12 @@ void SourceFile::dump(llvm::raw_ostream &OS) const {
14261426
}
14271427

14281428
void Pattern::dump() const {
1429-
PrintPattern(llvm::errs()).visit(const_cast<Pattern*>(this));
1430-
llvm::errs() << '\n';
1429+
dump(llvm::errs());
1430+
}
1431+
1432+
void Pattern::dump(raw_ostream &OS, unsigned Indent) const {
1433+
PrintPattern(OS, Indent).visit(const_cast<Pattern*>(this));
1434+
OS << '\n';
14311435
}
14321436

14331437
//===----------------------------------------------------------------------===//

lib/AST/ASTNode.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "swift/AST/Decl.h"
1919
#include "swift/AST/Expr.h"
2020
#include "swift/AST/Stmt.h"
21+
#include "swift/AST/Pattern.h"
22+
#include "swift/AST/TypeLoc.h"
2123
#include "swift/Basic/SourceLoc.h"
2224

2325
using namespace swift;
@@ -29,6 +31,10 @@ SourceRange ASTNode::getSourceRange() const {
2931
return S->getSourceRange();
3032
if (const auto *D = this->dyn_cast<Decl*>())
3133
return D->getSourceRange();
34+
if (const auto *P = this->dyn_cast<Pattern*>())
35+
return P->getSourceRange();
36+
if (const auto *L = this->dyn_cast<TypeLoc *>())
37+
return L->getSourceRange();
3238
llvm_unreachable("unsupported AST node");
3339
}
3440

@@ -63,6 +69,10 @@ bool ASTNode::isImplicit() const {
6369
return S->isImplicit();
6470
if (const auto *D = this->dyn_cast<Decl*>())
6571
return D->isImplicit();
72+
if (const auto *P = this->dyn_cast<Pattern*>())
73+
return P->isImplicit();
74+
if (const auto *L = this->dyn_cast<TypeLoc*>())
75+
return false;
6676
llvm_unreachable("unsupported AST node");
6777
}
6878

@@ -73,6 +83,8 @@ void ASTNode::walk(ASTWalker &Walker) {
7383
S->walk(Walker);
7484
else if (auto *D = this->dyn_cast<Decl*>())
7585
D->walk(Walker);
86+
else if (auto *P = this->dyn_cast<Pattern*>())
87+
P->walk(Walker);
7688
else
7789
llvm_unreachable("unsupported AST node");
7890
}
@@ -84,6 +96,8 @@ void ASTNode::dump(raw_ostream &OS, unsigned Indent) const {
8496
E->dump(OS, Indent);
8597
else if (auto D = dyn_cast<Decl*>())
8698
D->dump(OS, Indent);
99+
else if (auto P = dyn_cast<Pattern*>())
100+
P->dump(OS, Indent);
87101
else
88102
OS << "<null>";
89103
}
@@ -101,4 +115,5 @@ bool ASTNode::is##T(T##Kind Kind) const { \
101115
FUNC(Stmt)
102116
FUNC(Expr)
103117
FUNC(Decl)
118+
FUNC(Pattern)
104119
#undef FUNC

0 commit comments

Comments
 (0)