Skip to content

Commit 19506da

Browse files
committed
Swift: statement visitor
This transfers the current state of `StmtVisitor` in the PoC, plus some changes required for the update to swift 5.6. Also `getLabel` in `SwiftDispatcher` got renamed to `createLabel`, and is now correctly outputting the label assignment to the trap file.
1 parent d6ced16 commit 19506da

38 files changed

+916
-75
lines changed

swift/codegen/schema.yml

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ AstNode:
5252

5353
ConditionElement:
5454
_extends: Locatable
55-
# boolean: Expr?
56-
# pattern: Pattern?
57-
# initializer: Expr?
55+
boolean: Expr?
56+
pattern: Pattern?
57+
initializer: Expr?
5858
_dir: stmt
5959

6060
AnyFunctionType:
@@ -534,56 +534,56 @@ TypedPattern:
534534

535535
BraceStmt:
536536
_extends: Stmt
537-
# elements: AstNode*
537+
elements: AstNode*
538538

539539
BreakStmt:
540540
_extends: Stmt
541-
# target_name: string?
542-
# target: Stmt?
541+
target_name: string?
542+
target: Stmt?
543543

544544
CaseStmt:
545545
_extends: Stmt
546-
# body: Stmt
547-
# labels: CaseLabelItem*
548-
# variables: VarDecl*
546+
body: Stmt
547+
labels: CaseLabelItem*
548+
variables: VarDecl*
549549

550550
CaseLabelItem:
551551
_extends: AstNode
552-
# pattern: Pattern
553-
# guard: Expr?
552+
pattern: Pattern
553+
guard: Expr?
554554
_dir: stmt
555555

556556
ContinueStmt:
557557
_extends: Stmt
558-
# target_name: string?
559-
# target: Stmt?
558+
target_name: string?
559+
target: Stmt?
560560

561561
DeferStmt:
562562
_extends: Stmt
563-
# body: BraceStmt
563+
body: BraceStmt
564564

565565
FailStmt:
566566
_extends: Stmt
567567

568568
FallthroughStmt:
569569
_extends: Stmt
570-
# fallthrough_source: CaseStmt
571-
# fallthrough_dest: CaseStmt
570+
fallthrough_source: CaseStmt
571+
fallthrough_dest: CaseStmt
572572

573573
LabeledStmt:
574574
_extends: Stmt
575-
# label: string?
575+
label: string?
576576

577577
PoundAssertStmt:
578578
_extends: Stmt
579579

580580
ReturnStmt:
581581
_extends: Stmt
582-
# result: Expr?
582+
result: Expr?
583583

584584
ThrowStmt:
585585
_extends: Stmt
586-
# sub_expr: Expr
586+
sub_expr: Expr
587587

588588
YieldStmt:
589589
_extends: Stmt
@@ -843,37 +843,38 @@ OverloadedDeclRefExpr:
843843

844844
DoCatchStmt:
845845
_extends: LabeledStmt
846-
# body: Stmt
847-
# catches: CaseStmt*
846+
body: Stmt
847+
catches: CaseStmt*
848848

849849
DoStmt:
850850
_extends: LabeledStmt
851-
# body: BraceStmt
851+
body: BraceStmt
852852

853853
ForEachStmt:
854854
_extends: LabeledStmt
855-
# body: BraceStmt
856-
# sequence: Expr
857-
# where: Expr?
855+
pattern: Pattern
856+
sequence: Expr
857+
where: Expr?
858+
body: BraceStmt
858859

859860
LabeledConditionalStmt:
860861
_extends: LabeledStmt
861-
# condition: StmtCondition
862+
condition: StmtCondition
862863

863864
StmtCondition:
864865
_extends: AstNode
865-
# elements: ConditionElement*
866+
elements: ConditionElement*
866867
_dir: stmt
867868

868869
RepeatWhileStmt:
869870
_extends: LabeledStmt
870-
# condition: Expr
871-
# body: Stmt
871+
condition: Expr
872+
body: Stmt
872873

873874
SwitchStmt:
874875
_extends: LabeledStmt
875-
# expr: Expr
876-
# cases: CaseStmt*
876+
expr: Expr
877+
cases: CaseStmt*
877878

878879
BoundGenericClassType:
879880
_extends: BoundGenericType
@@ -979,16 +980,16 @@ DynamicSubscriptExpr:
979980

980981
GuardStmt:
981982
_extends: LabeledConditionalStmt
982-
# body: BraceStmt
983+
body: BraceStmt
983984

984985
IfStmt:
985986
_extends: LabeledConditionalStmt
986-
# then: Stmt
987-
# else: Stmt?
987+
then: Stmt
988+
else: Stmt?
988989

989990
WhileStmt:
990991
_extends: LabeledConditionalStmt
991-
# body: Stmt
992+
body: Stmt
992993

993994
AccessorDecl:
994995
_extends: FuncDecl

swift/extractor/SwiftDispatcher.h

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,60 +67,104 @@ class SwiftDispatcher {
6767
// `swift::TypeBase*`)
6868
TrapLabel<TypeTag> fetchLabel(swift::Type t) { return fetchLabel(t.getPointer()); }
6969

70+
TrapLabel<AstNodeTag> fetchLabel(swift::ASTNode node) {
71+
return fetchLabelFromUnion<AstNodeTag>(node);
72+
}
73+
7074
// Due to the lazy emission approach, we must assign a label to a corresponding AST node before
7175
// it actually gets emitted to handle recursive cases such as recursive calls, or recursive type
7276
// declarations
7377
template <typename E>
7478
TrapLabelOf<E> assignNewLabel(E* e) {
7579
assert(waitingForNewLabel == Store::Handle{e} && "assignNewLabel called on wrong entity");
76-
auto label = getLabel<TrapTagOf<E>>();
77-
trap.assignStar(label);
80+
auto label = createLabel<TrapTagOf<E>>();
7881
store.insert(e, label);
7982
waitingForNewLabel = std::monostate{};
8083
return label;
8184
}
8285

8386
template <typename Tag>
84-
TrapLabel<Tag> getLabel() {
85-
return arena.allocateLabel<Tag>();
87+
TrapLabel<Tag> createLabel() {
88+
auto ret = arena.allocateLabel<Tag>();
89+
trap.assignStar(ret);
90+
return ret;
91+
}
92+
93+
template <typename Tag, typename... Args>
94+
TrapLabel<Tag> createLabel(Args&&... args) {
95+
auto ret = arena.allocateLabel<Tag>();
96+
trap.assignKey(ret, std::forward<Args>(args)...);
97+
return ret;
8698
}
8799

88100
template <typename Locatable>
89101
void attachLocation(Locatable locatable, TrapLabel<LocatableTag> locatableLabel) {
90102
attachLocation(&locatable, locatableLabel);
91103
}
92104

93-
// Emits a Location TRAP entry and attaches it to an AST node
105+
// Emits a Location TRAP entry and attaches it to a `Locatable` trap label
94106
template <typename Locatable>
95107
void attachLocation(Locatable* locatable, TrapLabel<LocatableTag> locatableLabel) {
96-
auto start = locatable->getStartLoc();
97-
auto end = locatable->getEndLoc();
108+
attachLocation(locatable->getStartLoc(), locatable->getEndLoc(), locatableLabel);
109+
}
110+
111+
// Emits a Location TRAP entry for a list of swift entities and attaches it to a `Locatable` trap
112+
// label
113+
template <typename Locatable>
114+
void attachLocation(llvm::MutableArrayRef<Locatable>* locatables,
115+
TrapLabel<LocatableTag> locatableLabel) {
116+
if (locatables->empty()) {
117+
return;
118+
}
119+
attachLocation(locatables->front().getStartLoc(), locatables->back().getEndLoc(),
120+
locatableLabel);
121+
}
122+
123+
private:
124+
// types to be supported by assignNewLabel/fetchLabel need to be listed here
125+
using Store = TrapLabelStore<swift::Decl,
126+
swift::Stmt,
127+
swift::StmtCondition,
128+
swift::CaseLabelItem,
129+
swift::Expr,
130+
swift::Pattern,
131+
swift::TypeRepr,
132+
swift::TypeBase>;
133+
134+
void attachLocation(swift::SourceLoc start,
135+
swift::SourceLoc end,
136+
TrapLabel<LocatableTag> locatableLabel) {
98137
if (!start.isValid() || !end.isValid()) {
99138
// invalid locations seem to come from entities synthesized by the compiler
100139
return;
101140
}
102141
std::string filepath = getFilepath(start);
103-
auto fileLabel = arena.allocateLabel<FileTag>();
104-
trap.assignKey(fileLabel, filepath);
142+
auto fileLabel = createLabel<FileTag>(filepath);
105143
// TODO: do not emit duplicate trap entries for Files
106144
trap.emit(FilesTrap{fileLabel, filepath});
107145
auto [startLine, startColumn] = sourceManager.getLineAndColumnInBuffer(start);
108146
auto [endLine, endColumn] = sourceManager.getLineAndColumnInBuffer(end);
109-
auto locLabel = arena.allocateLabel<LocationTag>();
110-
trap.assignKey(locLabel, '{', fileLabel, "}:", startLine, ':', startColumn, ':', endLine, ':',
111-
endColumn);
147+
auto locLabel = createLabel<LocationTag>('{', fileLabel, "}:", startLine, ':', startColumn, ':',
148+
endLine, ':', endColumn);
112149
trap.emit(LocationsTrap{locLabel, fileLabel, startLine, startColumn, endLine, endColumn});
113150
trap.emit(LocatablesTrap{locatableLabel, locLabel});
114151
}
115152

116-
private:
117-
// types to be supported by assignNewLabel/fetchLabel need to be listed here
118-
using Store = TrapLabelStore<swift::Decl,
119-
swift::Stmt,
120-
swift::Expr,
121-
swift::Pattern,
122-
swift::TypeRepr,
123-
swift::TypeBase>;
153+
template <typename Tag, typename... Ts>
154+
TrapLabel<Tag> fetchLabelFromUnion(const llvm::PointerUnion<Ts...> u) {
155+
TrapLabel<Tag> ret{};
156+
assert((... || fetchLabelFromUnionCase<Tag, Ts>(u, ret)) && "llvm::PointerUnion not set");
157+
return ret;
158+
}
159+
160+
template <typename Tag, typename T, typename... Ts>
161+
bool fetchLabelFromUnionCase(const llvm::PointerUnion<Ts...> u, TrapLabel<Tag>& output) {
162+
if (auto e = u.template dyn_cast<T>()) {
163+
output = fetchLabel(e);
164+
return true;
165+
}
166+
return false;
167+
}
124168

125169
std::string getFilepath(swift::SourceLoc loc) {
126170
// TODO: this needs more testing
@@ -139,6 +183,8 @@ class SwiftDispatcher {
139183
// which are to be introduced in follow-up PRs
140184
virtual void visit(swift::Decl* decl) = 0;
141185
virtual void visit(swift::Stmt* stmt) = 0;
186+
virtual void visit(swift::StmtCondition* cond) = 0;
187+
virtual void visit(swift::CaseLabelItem* item) = 0;
142188
virtual void visit(swift::Expr* expr) = 0;
143189
virtual void visit(swift::Pattern* pattern) = 0;
144190
virtual void visit(swift::TypeRepr* type) = 0;

swift/extractor/SwiftTagTraits.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ using SILBoxTypeReprTag = SilBoxTypeReprTag;
3535
static_assert(std::is_base_of_v<TYPE##Tag, TAG>, "override is not a subtag");
3636

3737
MAP_TAG(Stmt);
38+
MAP_TAG(StmtCondition);
39+
MAP_TAG(CaseLabelItem);
3840
#define ABSTRACT_STMT(CLASS, PARENT) MAP_SUBTAG(CLASS##Stmt, PARENT)
3941
#define STMT(CLASS, PARENT) ABSTRACT_STMT(CLASS, PARENT)
4042
#include "swift/AST/StmtNodes.def"

swift/extractor/SwiftVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class SwiftVisitor : private SwiftDispatcher {
2222
private:
2323
void visit(swift::Decl* decl) override { declVisitor.visit(decl); }
2424
void visit(swift::Stmt* stmt) override { stmtVisitor.visit(stmt); }
25+
void visit(swift::StmtCondition* cond) override { stmtVisitor.visitStmtCondition(cond); }
26+
void visit(swift::CaseLabelItem* item) override { stmtVisitor.visitCaseLabelItem(item); }
2527
void visit(swift::Expr* expr) override { exprVisitor.visit(expr); }
2628
void visit(swift::Pattern* pattern) override { patternVisitor.visit(pattern); }
2729
void visit(swift::TypeRepr* type) override { typeReprVisitor.visit(type); }

0 commit comments

Comments
 (0)