Skip to content

Commit f2db96c

Browse files
committed
[ASTGen] Eliminate ASTGen.ASTNode
We can simply use `BridgedASTNode`, no need to have its own ASTNode
1 parent 484a4da commit f2db96c

File tree

6 files changed

+84
-73
lines changed

6 files changed

+84
-73
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,6 @@ bool BridgedASTContext_canImport(BridgedASTContext cContext,
335335
// MARK: AST nodes
336336
//===----------------------------------------------------------------------===//
337337

338-
enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : size_t {
339-
ASTNodeKindExpr,
340-
ASTNodeKindStmt,
341-
ASTNodeKindDecl
342-
};
343-
344338
void registerBridgedDecl(BridgedStringRef bridgedClassName, SwiftMetatype metatype);
345339

346340
struct OptionalBridgedDeclObj {
@@ -376,16 +370,53 @@ struct BridgedDeclObj {
376370
BRIDGED_INLINE bool Destructor_isIsolated() const;
377371
};
378372

379-
struct BridgedASTNode {
380-
SWIFT_NAME("raw")
381-
void *_Nonnull Raw;
373+
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedASTNodeKind : uint8_t {
374+
BridgedASTNodeKindExpr,
375+
BridgedASTNodeKindStmt,
376+
BridgedASTNodeKindDecl
377+
};
378+
379+
class BridgedASTNode {
380+
intptr_t opaque;
382381

383-
SWIFT_NAME("kind")
384-
ASTNodeKind Kind;
382+
BRIDGED_INLINE BridgedASTNode(void *_Nonnull pointer,
383+
BridgedASTNodeKind kind);
384+
385+
void *_Nonnull getPointer() const {
386+
return reinterpret_cast<void *>(opaque & ~0x7);
387+
}
388+
389+
public:
390+
SWIFT_NAME("decl(_:)")
391+
static BridgedASTNode createDecl(BridgedDecl d) {
392+
return BridgedASTNode(d.unbridged(), BridgedASTNodeKindDecl);
393+
}
394+
SWIFT_NAME("stmt(_:)")
395+
static BridgedASTNode createStmt(BridgedStmt s) {
396+
return BridgedASTNode(s.unbridged(), BridgedASTNodeKindStmt);
397+
}
398+
SWIFT_NAME("expr(_:)")
399+
static BridgedASTNode createExor(BridgedExpr e) {
400+
return BridgedASTNode(e.unbridged(), BridgedASTNodeKindExpr);
401+
}
402+
403+
SWIFT_UNAVAILABLE("use .kind")
404+
BridgedASTNodeKind getKind() const {
405+
return static_cast<BridgedASTNodeKind>(opaque & 0x7);
406+
}
407+
408+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedExpr castToExpr() const;
409+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStmt castToStmt() const;
410+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDecl castToDecl() const;
385411

386412
BRIDGED_INLINE swift::ASTNode unbridged() const;
387413
};
388414

415+
SWIFT_NAME("getter:BridgedASTNode.kind(self:)")
416+
inline BridgedASTNodeKind BridgedASTNode_getKind(BridgedASTNode node) {
417+
return node.getKind();
418+
}
419+
389420
// Declare `.asDecl` on each BridgedXXXDecl type, which upcasts a wrapper for
390421
// a Decl subclass to a BridgedDecl.
391422
#define DECL(Id, Parent) \

include/swift/AST/ASTBridgingImpl.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,32 @@ bool BridgedDeclObj::Destructor_isIsolated() const {
224224
// MARK: BridgedASTNode
225225
//===----------------------------------------------------------------------===//
226226

227+
BridgedASTNode::BridgedASTNode(void *_Nonnull pointer, BridgedASTNodeKind kind)
228+
: opaque(intptr_t(pointer) | kind) {
229+
assert(getPointer() == pointer && getKind() == kind);
230+
}
231+
232+
BridgedExpr BridgedASTNode::castToExpr() const {
233+
assert(getKind() == BridgedASTNodeKindExpr);
234+
return static_cast<swift::Expr *>(getPointer());
235+
}
236+
BridgedStmt BridgedASTNode::castToStmt() const {
237+
assert(getKind() == BridgedASTNodeKindStmt);
238+
return static_cast<swift::Stmt *>(getPointer());
239+
}
240+
BridgedDecl BridgedASTNode::castToDecl() const {
241+
assert(getKind() == BridgedASTNodeKindDecl);
242+
return static_cast<swift::Decl *>(getPointer());
243+
}
244+
227245
swift::ASTNode BridgedASTNode::unbridged() const {
228-
switch (Kind) {
229-
case ASTNodeKindExpr:
230-
return swift::ASTNode(static_cast<swift::Expr *>(Raw));
231-
case ASTNodeKindStmt:
232-
return swift::ASTNode(static_cast<swift::Stmt *>(Raw));
233-
case ASTNodeKindDecl:
234-
return swift::ASTNode(static_cast<swift::Decl *>(Raw));
246+
switch (getKind()) {
247+
case BridgedASTNodeKindExpr:
248+
return castToExpr().unbridged();
249+
case BridgedASTNodeKindStmt:
250+
return castToStmt().unbridged();
251+
case BridgedASTNodeKindDecl:
252+
return castToDecl().unbridged();
235253
}
236254
}
237255

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,6 @@ import SwiftIfConfig
1818

1919
import struct SwiftDiagnostics.Diagnostic
2020

21-
enum ASTNode {
22-
case decl(BridgedDecl)
23-
case stmt(BridgedStmt)
24-
case expr(BridgedExpr)
25-
26-
var castToExpr: BridgedExpr {
27-
guard case .expr(let bridged) = self else {
28-
fatalError("Expected an expr")
29-
}
30-
return bridged
31-
}
32-
33-
var castToStmt: BridgedStmt {
34-
guard case .stmt(let bridged) = self else {
35-
fatalError("Expected a stmt")
36-
}
37-
return bridged
38-
}
39-
40-
var castToDecl: BridgedDecl {
41-
guard case .decl(let bridged) = self else {
42-
fatalError("Expected a decl")
43-
}
44-
return bridged
45-
}
46-
47-
var bridged: BridgedASTNode {
48-
switch self {
49-
case .expr(let e):
50-
return BridgedASTNode(raw: e.raw, kind: .expr)
51-
case .stmt(let s):
52-
return BridgedASTNode(raw: s.raw, kind: .stmt)
53-
case .decl(let d):
54-
return BridgedASTNode(raw: d.raw, kind: .decl)
55-
}
56-
}
57-
}
58-
5921
/// Little utility wrapper that lets us have some mutable state within
6022
/// immutable structs, and is therefore pretty evil.
6123
@propertyWrapper
@@ -95,8 +57,8 @@ struct ASTGenVisitor {
9557
self.configuredRegions = configuredRegions
9658
}
9759

98-
func generate(sourceFile node: SourceFileSyntax) -> [ASTNode] {
99-
var out = [ASTNode]()
60+
func generate(sourceFile node: SourceFileSyntax) -> [BridgedASTNode] {
61+
var out = [BridgedASTNode]()
10062
let isTopLevel = self.declContext.isModuleScopeContext
10163

10264
visitIfConfigElements(
@@ -105,7 +67,7 @@ struct ASTGenVisitor {
10567
split: Self.splitCodeBlockItemIfConfig
10668
) { element in
10769

108-
func generateStmtOrExpr(_ body: () -> ASTNode) -> ASTNode {
70+
func generateStmtOrExpr(_ body: () -> BridgedASTNode) -> BridgedASTNode {
10971
if !isTopLevel {
11072
return body()
11173
}
@@ -117,7 +79,7 @@ struct ASTGenVisitor {
11779

11880
// Diagnose top level code in non-script file.
11981
if (!declContext.parentSourceFile.isScriptMode) {
120-
switch astNode {
82+
switch element.item {
12183
case .stmt:
12284
self.diagnose(.illegalTopLevelStmt(element))
12385
case .expr:
@@ -131,7 +93,7 @@ struct ASTGenVisitor {
13193
let body = BridgedBraceStmt.createImplicit(
13294
self.ctx,
13395
lBraceLoc: bodyRange.start,
134-
element: astNode.bridged,
96+
element: astNode,
13597
rBraceLoc: bodyRange.end
13698
)
13799
topLevelDecl.setBody(body: body)
@@ -147,7 +109,7 @@ struct ASTGenVisitor {
147109
// Hoist 'VarDecl' to the top-level.
148110
withBridgedSwiftClosure { ptr in
149111
let hoisted = ptr!.load(as: BridgedDecl.self)
150-
out.append(ASTNode.decl(hoisted))
112+
out.append(.decl(hoisted))
151113
} call: { handle in
152114
d.forEachDeclToHoist(handle)
153115
}
@@ -476,11 +438,11 @@ public func buildTopLevelASTNodes(
476438
switch sourceFile.pointee.syntax.as(SyntaxEnum.self) {
477439
case .sourceFile(let node):
478440
for elem in visitor.generate(sourceFile: node) {
479-
callback(elem.bridged, outputContext)
441+
callback(elem, outputContext)
480442
}
481443
case .memberBlockItemList(let node):
482444
for elem in visitor.generate(memberBlockItemList: node) {
483-
callback(ASTNode.decl(elem).bridged, outputContext)
445+
callback(.decl(elem), outputContext)
484446
}
485447
default:
486448
fatalError("invalid syntax for a source file")

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ extension ASTGenVisitor {
596596
let body = BridgedBraceStmt.createImplicit(
597597
self.ctx,
598598
lBraceLoc: range.start,
599-
element: ASTNode.decl(decl.asDecl).bridged,
599+
element: .decl(decl.asDecl),
600600
rBraceLoc: range.end
601601
)
602602
topLevelDecl.setBody(body: body);

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension ASTGenVisitor {
6767
}()
6868

6969
// 'stmts' is a list of body elements of 'TapExpr' aka "appendingExpr" for the 'InterpolatedStringLiteralExpr'.
70-
var stmts: [ASTNode] = []
70+
var stmts: [BridgedASTNode] = []
7171

7272
// The first element is a 'VarDecl'.
7373
let interpolationVar = BridgedVarDecl.createImplicitStringInterpolationVar(self.declContext)
@@ -199,7 +199,7 @@ extension ASTGenVisitor {
199199
let body = BridgedBraceStmt.createParsed(
200200
self.ctx,
201201
lBraceLoc: nil,
202-
elements: stmts.lazy.map({ $0.bridged }).bridgedArray(in: self),
202+
elements: stmts.lazy.bridgedArray(in: self),
203203
rBraceLoc: nil
204204
)
205205
let appendingExpr = BridgedTapExpr.create(

lib/ASTGen/Sources/ASTGen/Stmts.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension ASTGenVisitor {
6666
}
6767
}
6868

69-
func generate(codeBlockItem node: CodeBlockItemSyntax) -> ASTNode? {
69+
func generate(codeBlockItem node: CodeBlockItemSyntax) -> BridgedASTNode? {
7070
// TODO: Set semicolon loc.
7171
switch node.item {
7272
case .decl(let node):
@@ -89,15 +89,15 @@ extension ASTGenVisitor {
8989
guard let item = self.generate(codeBlockItem: codeBlockItem) else {
9090
return
9191
}
92-
allItems.append(item.bridged)
92+
allItems.append(item)
9393

9494
// Hoist 'VarDecl' to the block.
95-
if case .decl(let decl) = item {
95+
if item.kind == .decl {
9696
withBridgedSwiftClosure { ptr in
9797
let d = ptr!.load(as: BridgedDecl.self)
98-
allItems.append(ASTNode.decl(d).bridged)
98+
allItems.append(.decl(d))
9999
} call: { handle in
100-
decl.forEachDeclToHoist(handle)
100+
item.castToDecl().forEachDeclToHoist(handle)
101101
}
102102
}
103103
}
@@ -504,7 +504,7 @@ extension ASTGenVisitor {
504504
}
505505
} body: { caseNode in
506506
allBridgedCases.append(
507-
ASTNode.stmt(self.generate(switchCase: caseNode).asStmt).bridged
507+
.stmt(self.generate(switchCase: caseNode).asStmt)
508508
)
509509
}
510510

0 commit comments

Comments
 (0)