Skip to content

Commit 0fff782

Browse files
authored
Merge pull request #61812 from zoecarver/smattering-of-astgen-fixes
[astgen] A smattering of ASTGen fixes.
2 parents 85399f5 + dd4a0ec commit 0fff782

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,21 @@ void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
125125
void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc);
126126

127127
void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name,
128-
void *loc, _Bool isStatic, _Bool isLet, void *dc);
128+
void *initExpr, void *loc, _Bool isStatic,
129+
_Bool isLet, void *dc);
129130

130131
void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then,
131132
void *_Nullable elseLoc, void *_Nullable elseStmt);
132133

134+
typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : long {
135+
ASTNodeKindExpr,
136+
ASTNodeKindStmt,
137+
ASTNodeKindDecl
138+
} ASTNodeKind;
139+
133140
struct ASTNodeBridged {
134141
void *ptr;
135-
_Bool isExpr; // Must be expr or stmt.
142+
ASTNodeKind kind;
136143
};
137144

138145
void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements,
@@ -144,7 +151,8 @@ void *BridgedSourceLoc_advanced(void *loc, long len);
144151

145152
void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
146153
void *_Nullable argName, void *_Nullable paramLoc,
147-
void *_Nullable paramName, void *declContext);
154+
void *_Nullable paramName, void *_Nullable type,
155+
void *declContext);
148156

149157
void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
150158
BridgedIdentifier name, void *nameLoc, _Bool isAsync,

lib/AST/CASTBridging.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "swift/AST/GenericParamList.h"
88
#include "swift/AST/Identifier.h"
99
#include "swift/AST/ParameterList.h"
10+
#include "swift/AST/Pattern.h"
1011
#include "swift/AST/Stmt.h"
1112
#include "swift/AST/TypeRepr.h"
1213

@@ -139,11 +140,20 @@ void *SwiftBooleanLiteralExpr_create(void *ctx, bool value, void *TokenLoc) {
139140
}
140141

141142
void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable nameId,
142-
void *loc, bool isStatic, bool isLet, void *dc) {
143+
void *initExpr, void *loc, bool isStatic, bool isLet,
144+
void *dc) {
143145
ASTContext &Context = *static_cast<ASTContext *>(ctx);
144-
return new (Context) VarDecl(
146+
auto name = (UnresolvedDeclRefExpr *)nameId;
147+
auto sourceLoc = getSourceLocFromPointer(loc);
148+
auto varDecl = new (Context) VarDecl(
145149
isStatic, isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var,
146-
getSourceLocFromPointer(loc), Identifier::getFromOpaquePointer(nameId),
150+
sourceLoc, name->getName().getBaseIdentifier(),
151+
reinterpret_cast<DeclContext *>(dc));
152+
auto pattern = NamedPattern::createImplicit(Context, varDecl);
153+
return PatternBindingDecl::create(
154+
Context, sourceLoc,
155+
isStatic ? StaticSpellingKind::KeywordStatic : StaticSpellingKind::None,
156+
sourceLoc, pattern, sourceLoc, (Expr *)initExpr,
147157
reinterpret_cast<DeclContext *>(dc));
148158
}
149159

@@ -164,12 +174,16 @@ void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements,
164174
void *rbloc) {
165175
llvm::SmallVector<ASTNode, 6> nodes;
166176
for (auto node : getArrayRef<ASTNodeBridged>(elements)) {
167-
if (node.isExpr) {
177+
if (node.kind == ASTNodeKindExpr) {
168178
auto expr = (Expr *)node.ptr;
169179
nodes.push_back(expr);
170-
} else {
180+
} else if (node.kind == ASTNodeKindStmt) {
171181
auto stmt = (Stmt *)node.ptr;
172182
nodes.push_back(stmt);
183+
} else {
184+
assert(node.kind == ASTNodeKindDecl);
185+
auto decl = (Decl *)node.ptr;
186+
nodes.push_back(decl);
173187
}
174188
}
175189

@@ -183,13 +197,17 @@ void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
183197
BridgedIdentifier _Nullable argName,
184198
void *_Nullable paramLoc,
185199
BridgedIdentifier _Nullable paramName,
186-
void *declContext) {
200+
void *_Nullable type, void *declContext) {
187201
ASTContext &Context = *static_cast<ASTContext *>(ctx);
188-
return new (Context) ParamDecl(
202+
if (!paramName)
203+
paramName = argName;
204+
auto paramDecl = new (Context) ParamDecl(
189205
getSourceLocFromPointer(loc), getSourceLocFromPointer(argLoc),
190206
Identifier::getFromOpaquePointer(argName),
191207
getSourceLocFromPointer(paramLoc),
192208
Identifier::getFromOpaquePointer(paramName), (DeclContext *)declContext);
209+
paramDecl->setTypeRepr((TypeRepr *)type);
210+
return paramDecl;
193211
}
194212

195213
void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ enum ASTNode {
3838
func bridged() -> ASTNodeBridged {
3939
switch self {
4040
case .expr(let e):
41-
return ASTNodeBridged(ptr: e, isExpr: true)
41+
return ASTNodeBridged(ptr: e, kind: .expr)
4242
case .stmt(let s):
43-
return ASTNodeBridged(ptr: s, isExpr: false)
43+
return ASTNodeBridged(ptr: s, kind: .stmt)
44+
case .decl(let d):
45+
return ASTNodeBridged(ptr: d, kind: .decl)
4446
default:
4547
fatalError("Must be expr or stmt.")
4648
}

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,18 @@ extension ASTGenVisitor {
7575
let isLet = node.letOrVarKeyword.tokenKind == .letKeyword
7676

7777
// TODO: don't drop "initializer" on the floor.
78-
return .decl(SwiftVarDecl_create(ctx, nil, loc, isStateic, isLet, declContext))
78+
return .decl(
79+
SwiftVarDecl_create(
80+
ctx, pattern, initializer, loc, isStateic,
81+
isLet, declContext))
7982
}
8083

8184
public func visit(_ node: FunctionParameterSyntax) -> ASTNode {
8285
let loc = self.base.advanced(by: node.position.utf8Offset).raw
8386

8487
let firstName: UnsafeMutableRawPointer?
8588
let secondName: UnsafeMutableRawPointer?
89+
let type: UnsafeMutableRawPointer?
8690

8791
if let nodeFirstName = node.firstName {
8892
var text = nodeFirstName.text
@@ -102,7 +106,13 @@ extension ASTGenVisitor {
102106
secondName = nil
103107
}
104108

105-
return .decl(ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, declContext))
109+
if let typeSyntax = node.type {
110+
type = visit(typeSyntax).rawValue
111+
} else {
112+
type = nil
113+
}
114+
115+
return .decl(ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, type, declContext))
106116
}
107117

108118
public func visit(_ node: FunctionDeclSyntax) -> ASTNode {

lib/ASTGen/Sources/ASTGen/Stmts.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import SwiftSyntax
55
extension ASTGenVisitor {
66
public func visit(_ node: CodeBlockSyntax) -> ASTNode {
77
let statements = node.statements.map(self.visit).map { $0.bridged() }
8-
let loc = self.base.advanced(by: node.position.utf8Offset).raw
8+
let startLoc = self.base.advanced(by: node.position.utf8Offset).raw
9+
let endLoc = self.base.advanced(by: node.endPosition.utf8Offset).raw
910

1011
return .stmt(
1112
statements.withBridgedArrayRef { ref in
12-
BraceStmt_create(ctx, loc, ref, loc)
13+
BraceStmt_create(ctx, startLoc, ref, endLoc)
1314
})
1415
}
1516

0 commit comments

Comments
 (0)