Skip to content

Commit 2c1b76c

Browse files
committed
[astgen] Correctly represent FuncDecl as a DeclContect.
1 parent 1e8db6b commit 2c1b76c

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

include/swift/AST/CASTBridging.h

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

157-
void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
162+
struct FuncDeclBridged FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
158163
BridgedIdentifier name, void *nameLoc, _Bool isAsync,
159164
void *_Nullable asyncLoc, _Bool throws,
160165
void *_Nullable throwsLoc, void *paramLLoc,
161166
BridgedArrayRef params, void *paramRLoc,
162-
void *_Nullable body, void *_Nullable returnType,
167+
void *_Nullable returnType,
163168
void *declContext);
169+
void FuncDecl_setBody(void *fn, void *body);
164170

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

lib/AST/CASTBridging.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
210210
return paramDecl;
211211
}
212212

213-
void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
213+
struct FuncDeclBridged FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
214214
BridgedIdentifier name, void *nameLoc, bool isAsync,
215215
void *_Nullable asyncLoc, bool throws,
216216
void *_Nullable throwsLoc, void *paramLLoc,
217217
BridgedArrayRef params, void *paramRLoc,
218-
void *_Nullable body, void *_Nullable returnType,
218+
void *_Nullable returnType,
219219
void *declContext) {
220220
auto *paramList = ParameterList::create(
221221
*static_cast<ASTContext *>(ctx), getSourceLocFromPointer(paramLLoc),
@@ -230,9 +230,12 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
230230
getSourceLocFromPointer(asyncLoc), throws,
231231
getSourceLocFromPointer(throwsLoc), nullptr, paramList,
232232
(TypeRepr *)returnType, (DeclContext *)declContext);
233-
out->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
234233

235-
return static_cast<Decl *>(out);
234+
return {static_cast<DeclContext *>(out), static_cast<FuncDecl *>(out), static_cast<Decl *>(out)};
235+
}
236+
237+
void FuncDecl_setBody(void *fn, void *body) {
238+
((FuncDecl *)fn)->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
236239
}
237240

238241
void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id) {

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,13 @@ extension ASTGenVisitor {
122122
let rParamLoc = self.base.advanced(by: node.signature.input.leftParen.position.utf8Offset).raw
123123
let lParamLoc = self.base.advanced(by: node.signature.input.rightParen.position.utf8Offset).raw
124124

125+
125126

126127
var nameText = node.identifier.text
127128
let name = nameText.withUTF8 { buf in
128129
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
129130
}
130131

131-
let body: ASTNode?
132-
if let nodeBody = node.body {
133-
body = visit(nodeBody)
134-
} else {
135-
body = nil
136-
}
137-
138132
let returnType: ASTNode?
139133
if let output = node.signature.output {
140134
returnType = visit(output.returnType)
@@ -143,11 +137,27 @@ extension ASTGenVisitor {
143137
}
144138

145139
let params = node.signature.input.parameterList.map { visit($0).rawValue }
146-
return .decl(
147-
params.withBridgedArrayRef { ref in
148-
FuncDecl_create(
149-
ctx, staticLoc, false, funcLoc, name, nameLoc, false, nil, false, nil, rParamLoc, ref, lParamLoc, body?.rawValue,
150-
returnType?.rawValue, declContext)
151-
})
140+
let out = params.withBridgedArrayRef { ref in
141+
FuncDecl_create(
142+
ctx, staticLoc, false, funcLoc, name, nameLoc, false, nil, false, nil, rParamLoc, ref, lParamLoc,
143+
returnType?.rawValue, declContext)
144+
}
145+
146+
let oldDeclContext = declContext
147+
declContext = out.declContext
148+
defer { declContext = oldDeclContext }
149+
150+
let body: ASTNode?
151+
if let nodeBody = node.body {
152+
body = visit(nodeBody)
153+
} else {
154+
body = nil
155+
}
156+
157+
if let body = body {
158+
FuncDecl_setBody(out.funcDecl, body.rawValue)
159+
}
160+
161+
return .decl(out.decl)
152162
}
153163
}

0 commit comments

Comments
 (0)