- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathASTNode.cpp
159 lines (146 loc) · 5.05 KB
/
ASTNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===--- ASTNode.cpp - Swift Language ASTs --------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the ASTNode, which is a union of Stmt, Expr, and Decl.
//
//===----------------------------------------------------------------------===//
#include"swift/AST/ASTNode.h"
#include"swift/AST/Decl.h"
#include"swift/AST/Expr.h"
#include"swift/AST/Stmt.h"
#include"swift/AST/Pattern.h"
#include"swift/AST/TypeRepr.h"
#include"swift/Basic/Assertions.h"
#include"swift/Basic/SourceLoc.h"
#include"swift/Parse/Token.h"
usingnamespaceswift;
SourceRange ASTNode::getSourceRange() const {
if (constauto *E = this->dyn_cast<Expr*>())
return E->getSourceRange();
if (constauto *S = this->dyn_cast<Stmt*>())
return S->getSourceRange();
if (constauto *D = this->dyn_cast<Decl*>())
return D->getSourceRange();
if (constauto *P = this->dyn_cast<Pattern*>())
return P->getSourceRange();
if (constauto *T = this->dyn_cast<TypeRepr *>())
return T->getSourceRange();
if (constauto *C = this->dyn_cast<StmtConditionElement *>())
return C->getSourceRange();
if (constauto *I = this->dyn_cast<CaseLabelItem *>()) {
return I->getSourceRange();
}
assert(!isNull() && "Null ASTNode doesn't have a source range");
llvm_unreachable("unsupported AST node");
}
/// Return the location of the start of the statement.
SourceLoc ASTNode::getStartLoc() const {
returngetSourceRange().Start;
}
/// Return the location of the end of the statement.
SourceLoc ASTNode::getEndLoc() const {
returngetSourceRange().End;
}
DeclContext *ASTNode::getAsDeclContext() const {
if (auto *E = this->dyn_cast<Expr*>()) {
if (isa<AbstractClosureExpr>(E))
returnstatic_cast<AbstractClosureExpr*>(E);
} elseif (is<Stmt*>()) {
returnnullptr;
} elseif (auto *D = this->dyn_cast<Decl*>()) {
if (isa<DeclContext>(D))
return cast<DeclContext>(D);
} elseif (getOpaqueValue())
llvm_unreachable("unsupported AST node");
returnnullptr;
}
boolASTNode::isImplicit() const {
if (constauto *E = this->dyn_cast<Expr*>())
return E->isImplicit();
if (constauto *S = this->dyn_cast<Stmt*>())
return S->isImplicit();
if (constauto *D = this->dyn_cast<Decl*>())
return D->isImplicit();
if (constauto *P = this->dyn_cast<Pattern*>())
return P->isImplicit();
if (this->is<TypeRepr *>())
returnfalse;
if (this->is<StmtConditionElement *>())
returnfalse;
if (this->is<CaseLabelItem *>())
returnfalse;
llvm_unreachable("unsupported AST node");
}
voidASTNode::walk(ASTWalker &Walker) {
if (auto *E = this->dyn_cast<Expr*>())
E->walk(Walker);
elseif (auto *S = this->dyn_cast<Stmt*>())
S->walk(Walker);
elseif (auto *D = this->dyn_cast<Decl*>())
D->walk(Walker);
elseif (auto *P = this->dyn_cast<Pattern*>())
P->walk(Walker);
elseif (auto *T = this->dyn_cast<TypeRepr*>())
T->walk(Walker);
elseif (auto *C = this->dyn_cast<StmtConditionElement *>())
C->walk(Walker);
elseif (auto *I = this->dyn_cast<CaseLabelItem *>()) {
if (auto *P = I->getPattern())
P->walk(Walker);
if (auto *G = I->getGuardExpr())
G->walk(Walker);
} else
llvm_unreachable("unsupported AST node");
}
voidASTNode::dump(raw_ostream &OS, unsigned Indent) const {
if (isNull())
OS << "(null)";
elseif (auto S = dyn_cast<Stmt*>())
S->dump(OS, /*context=*/nullptr, Indent);
elseif (auto E = dyn_cast<Expr*>())
E->dump(OS, Indent);
elseif (auto D = dyn_cast<Decl*>())
D->dump(OS, Indent);
elseif (auto P = dyn_cast<Pattern*>())
P->dump(OS, Indent);
elseif (auto T = dyn_cast<TypeRepr*>())
T->print(OS);
elseif (is<StmtConditionElement *>())
OS.indent(Indent) << "(statement condition)";
elseif (is<CaseLabelItem *>()) {
OS.indent(Indent) << "(case label item)";
} else
llvm_unreachable("unsupported AST node");
}
voidASTNode::dump() const {
dump(llvm::errs());
}
StringRef swift::getTokenText(tok kind) {
switch(kind) {
#defineKEYWORD(KW) case tok::kw_##KW: return #KW;
#definePOUND_KEYWORD(KW) case tok::pound_##KW: return"#"#KW;
#definePUNCTUATOR(PUN, TEXT) case tok::PUN: return TEXT;
#include"swift/AST/TokenKinds.def"
default: returnStringRef();
}
}
#defineFUNC(T) \
bool ASTNode::is##T(T##Kind Kind) const { \
if (!is<T*>()) \
returnfalse; \
return get<T*>()->getKind() == Kind; \
}
FUNC(Stmt)
FUNC(Expr)
FUNC(Decl)
FUNC(Pattern)
#undef FUNC