Skip to content

Commit b6b7224

Browse files
committed
[astgen] Correctly represent FuncDecl as a DeclContect.
1 parent 4892800 commit b6b7224

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
@@ -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,
163+
struct FuncDeclBridged FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
159164
BridgedIdentifier name, void *nameLoc, _Bool isAsync,
160165
void *_Nullable asyncLoc, _Bool throws,
161166
void *_Nullable throwsLoc, void *paramLLoc,
162167
BridgedArrayRef params, void *paramRLoc,
163-
void *_Nullable body, void *_Nullable returnType,
168+
void *_Nullable returnType,
164169
void *declContext);
170+
void FuncDecl_setBody(void *fn, void *body);
165171

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

lib/AST/CASTBridging.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ 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,
223+
struct FuncDeclBridged FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
224224
BridgedIdentifier name, void *nameLoc, bool isAsync,
225225
void *_Nullable asyncLoc, bool throws,
226226
void *_Nullable throwsLoc, void *paramLLoc,
227227
BridgedArrayRef params, void *paramRLoc,
228-
void *_Nullable body, void *_Nullable returnType,
228+
void *_Nullable returnType,
229229
void *declContext) {
230230
auto *paramList = ParameterList::create(
231231
*static_cast<ASTContext *>(ctx), getSourceLocFromPointer(paramLLoc),
@@ -240,9 +240,12 @@ 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), static_cast<Decl *>(out)};
245+
}
246+
247+
void FuncDecl_setBody(void *fn, void *body) {
248+
((FuncDecl *)fn)->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
246249
}
247250

248251
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
@@ -125,19 +125,13 @@ extension ASTGenVisitor {
125125
let rParamLoc = self.base.advanced(by: node.signature.input.leftParen.position.utf8Offset).raw
126126
let lParamLoc = self.base.advanced(by: node.signature.input.rightParen.position.utf8Offset).raw
127127

128+
128129

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

134-
let body: ASTNode?
135-
if let nodeBody = node.body {
136-
body = visit(nodeBody)
137-
} else {
138-
body = nil
139-
}
140-
141135
let returnType: ASTNode?
142136
if let output = node.signature.output {
143137
returnType = visit(output.returnType)
@@ -146,11 +140,27 @@ extension ASTGenVisitor {
146140
}
147141

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

0 commit comments

Comments
 (0)