Skip to content

Commit e2c6c6c

Browse files
authored
Merge pull request #61836 from zoecarver/multi-param-fns
[astgen] A number of small fixes.
2 parents f184504 + 1e08317 commit e2c6c6c

File tree

5 files changed

+65
-37
lines changed

5 files changed

+65
-37
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,20 @@ void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
154154
void *_Nullable argName, void *_Nullable paramLoc,
155155
void *_Nullable paramName, void *_Nullable type,
156156
void *declContext);
157+
struct FuncDeclBridged {
158+
void *declContext;
159+
void *funcDecl;
160+
void *decl;
161+
};
157162

158-
void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
159-
BridgedIdentifier name, void *nameLoc, _Bool isAsync,
160-
void *_Nullable asyncLoc, _Bool throws,
161-
void *_Nullable throwsLoc, void *paramLLoc,
162-
BridgedArrayRef params, void *paramRLoc,
163-
void *_Nullable body, void *_Nullable returnType,
164-
void *declContext);
163+
struct FuncDeclBridged
164+
FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
165+
BridgedIdentifier name, void *nameLoc, _Bool isAsync,
166+
void *_Nullable asyncLoc, _Bool throws,
167+
void *_Nullable throwsLoc, void *paramLLoc,
168+
BridgedArrayRef params, void *paramRLoc,
169+
void *_Nullable returnType, void *declContext);
170+
void FuncDecl_setBody(void *fn, void *body);
165171

166172
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id);
167173

@@ -230,6 +236,7 @@ void TopLevelCodeDecl_dump(void *);
230236
void Expr_dump(void *);
231237
void Decl_dump(void *);
232238
void Stmt_dump(void *);
239+
void Type_dump(void *);
233240

234241
#ifdef __cplusplus
235242
}

lib/AST/CASTBridging.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
220220
return paramDecl;
221221
}
222222

223-
void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
224-
BridgedIdentifier name, void *nameLoc, bool isAsync,
225-
void *_Nullable asyncLoc, bool throws,
226-
void *_Nullable throwsLoc, void *paramLLoc,
227-
BridgedArrayRef params, void *paramRLoc,
228-
void *_Nullable body, void *_Nullable returnType,
229-
void *declContext) {
223+
struct FuncDeclBridged
224+
FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
225+
BridgedIdentifier name, void *nameLoc, bool isAsync,
226+
void *_Nullable asyncLoc, bool throws,
227+
void *_Nullable throwsLoc, void *paramLLoc,
228+
BridgedArrayRef params, void *paramRLoc,
229+
void *_Nullable returnType, void *declContext) {
230230
auto *paramList = ParameterList::create(
231231
*static_cast<ASTContext *>(ctx), getSourceLocFromPointer(paramLLoc),
232232
getArrayRef<ParamDecl *>(params), getSourceLocFromPointer(paramRLoc));
@@ -240,9 +240,13 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
240240
getSourceLocFromPointer(asyncLoc), throws,
241241
getSourceLocFromPointer(throwsLoc), nullptr, paramList,
242242
(TypeRepr *)returnType, (DeclContext *)declContext);
243-
out->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
244243

245-
return static_cast<Decl *>(out);
244+
return {static_cast<DeclContext *>(out), static_cast<FuncDecl *>(out),
245+
static_cast<Decl *>(out)};
246+
}
247+
248+
void FuncDecl_setBody(void *fn, void *body) {
249+
((FuncDecl *)fn)->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
246250
}
247251

248252
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id) {
@@ -283,10 +287,13 @@ void *ClosureExpr_create(void *ctx, void *body, void *dc) {
283287
SourceLoc inLoc;
284288

285289
ASTContext &Context = *static_cast<ASTContext *>(ctx);
290+
auto params = ParameterList::create(Context, inLoc, {}, inLoc);
291+
286292
auto *out = new (Context)
287293
ClosureExpr(attributes, bracketRange, nullptr, nullptr, asyncLoc,
288294
throwsLoc, arrowLoc, inLoc, nullptr, 0, (DeclContext *)dc);
289295
out->setBody((BraceStmt *)body, true);
296+
out->setParameterList(params);
290297
return (Expr *)out;
291298
}
292299

@@ -505,3 +512,4 @@ void TopLevelCodeDecl_dump(void *decl) {
505512
void Expr_dump(void *expr) { ((Expr *)expr)->dump(llvm::errs()); }
506513
void Decl_dump(void *expr) { ((Decl *)expr)->dump(llvm::errs()); }
507514
void Stmt_dump(void *expr) { ((Stmt *)expr)->dump(llvm::errs()); }
515+
void Type_dump(void *expr) { ((TypeRepr *)expr)->dump(); }

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,47 @@ extension ASTGenVisitor {
119119
}
120120

121121
public func visit(_ node: FunctionDeclSyntax) -> ASTNode {
122-
let loc = self.base.advanced(by: node.position.utf8Offset).raw
122+
let staticLoc = self.base.advanced(by: node.position.utf8Offset).raw
123+
let funcLoc = self.base.advanced(by: node.funcKeyword.position.utf8Offset).raw
124+
let nameLoc = self.base.advanced(by: node.identifier.position.utf8Offset).raw
125+
let rParamLoc = self.base.advanced(by: node.signature.input.leftParen.position.utf8Offset).raw
126+
let lParamLoc = self.base.advanced(by: node.signature.input.rightParen.position.utf8Offset).raw
123127

124128
var nameText = node.identifier.text
125129
let name = nameText.withUTF8 { buf in
126130
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
127131
}
128132

133+
let returnType: ASTNode?
134+
if let output = node.signature.output {
135+
returnType = visit(output.returnType)
136+
} else {
137+
returnType = nil
138+
}
139+
140+
let params = node.signature.input.parameterList.map { visit($0).rawValue }
141+
let out = params.withBridgedArrayRef { ref in
142+
FuncDecl_create(
143+
ctx, staticLoc, false, funcLoc, name, nameLoc, false, nil, false, nil, rParamLoc, ref,
144+
lParamLoc,
145+
returnType?.rawValue, declContext)
146+
}
147+
148+
let oldDeclContext = declContext
149+
declContext = out.declContext
150+
defer { declContext = oldDeclContext }
151+
129152
let body: ASTNode?
130153
if let nodeBody = node.body {
131154
body = visit(nodeBody)
132155
} else {
133156
body = nil
134157
}
135158

136-
let returnType: ASTNode?
137-
if let output = node.signature.output {
138-
returnType = visit(output.returnType)
139-
} else {
140-
returnType = nil
159+
if let body = body {
160+
FuncDecl_setBody(out.funcDecl, body.rawValue)
141161
}
142162

143-
let params = node.signature.input.parameterList.map { visit($0) }
144-
return .decl(
145-
params.withBridgedArrayRef { ref in
146-
FuncDecl_create(
147-
ctx, loc, false, loc, name, loc, false, nil, false, nil, loc, ref, loc, body?.rawValue,
148-
returnType?.rawValue, declContext)
149-
})
163+
return .decl(out.decl)
150164
}
151165
}

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import SwiftSyntax
55
extension ASTGenVisitor {
66
public func visit(_ node: ClosureExprSyntax) -> ASTNode {
77
let statements = node.statements.map { self.visit($0).bridged() }
8-
let loc = self.base.advanced(by: node.position.utf8Offset).raw
9-
10-
let body = statements.withBridgedArrayRef { ref in
11-
BraceStmt_create(ctx, loc, ref, loc)
8+
let body: UnsafeMutableRawPointer = statements.withBridgedArrayRef { ref in
9+
let startLoc = self.base.advanced(by: node.leftBrace.position.utf8Offset).raw
10+
let endLoc = self.base.advanced(by: node.rightBrace.position.utf8Offset).raw
11+
return BraceStmt_create(ctx, startLoc, ref, endLoc)
1212
}
1313

1414
return .expr(ClosureExpr_create(ctx, body, declContext))
@@ -66,9 +66,8 @@ extension ASTGenVisitor {
6666
public func visit(_ node: TupleExprElementListSyntax) -> ASTNode {
6767
let elements = node.map { self.visit($0).rawValue }
6868

69-
// TODO: find correct paren locs.
7069
let lParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
71-
let rParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
70+
let rParenLoc = self.base.advanced(by: node.endPosition.utf8Offset).raw
7271

7372
return .expr(
7473
elements.withBridgedArrayRef { elementsRef in

lib/ASTGen/Sources/ASTGen/Stmts.swift

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

1111
return .stmt(
1212
statements.withBridgedArrayRef { ref in

0 commit comments

Comments
 (0)