Skip to content

Commit 0ba92d8

Browse files
committed
[ASTGen] Generate closure signatures
Still TODO: * Capture list * Anonymous parameters
1 parent fa8852b commit 0ba92d8

File tree

10 files changed

+296
-44
lines changed

10 files changed

+296
-44
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class DiagnosticEngine;
4545
class Identifier;
4646
class IfConfigClauseRangeInfo;
4747
struct LabeledStmtInfo;
48+
enum class ParamSpecifier : uint8_t;
4849
class ProtocolConformanceRef;
4950
class RegexLiteralPatternFeature;
5051
class RegexLiteralPatternFeatureKind;
@@ -526,6 +527,10 @@ SWIFT_NAME("getter:BridgedPatternBindingInitializer.asDeclContext(self:)")
526527
BridgedDeclContext BridgedPatternBindingInitializer_asDeclContext(
527528
BridgedPatternBindingInitializer cInit);
528529

530+
SWIFT_NAME("getter:BridgedClosureExpr.asDeclContext(self:)")
531+
BridgedDeclContext
532+
BridgedClosureExpr_asDeclContext(BridgedClosureExpr cClosure);
533+
529534
//===----------------------------------------------------------------------===//
530535
// MARK: DeclAttributes
531536
//===----------------------------------------------------------------------===//
@@ -852,6 +857,24 @@ BridgedParamDecl BridgedParamDecl_createParsed(
852857
BridgedSourceLoc cParamNameLoc, BridgedNullableTypeRepr type,
853858
BridgedNullableExpr defaultValue);
854859

860+
/// The various spellings of ownership modifier that can be used in source.
861+
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
862+
BridgedParamSpecifierDefault,
863+
BridgedParamSpecifierInOut,
864+
BridgedParamSpecifierBorrowing,
865+
BridgedParamSpecifierConsuming,
866+
BridgedParamSpecifierLegacyShared,
867+
BridgedParamSpecifierLegacyOwned,
868+
BridgedParamSpecifierImplicitlyCopyableConsuming,
869+
};
870+
871+
BRIDGED_INLINE swift::ParamSpecifier unbridge(BridgedParamSpecifier kind);
872+
873+
SWIFT_NAME("BridgedParamDecl.setSpecifier(self:_:)")
874+
BRIDGED_INLINE void
875+
BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
876+
BridgedParamSpecifier cSpecifier);
877+
855878
SWIFT_NAME("BridgedConstructorDecl.setParsedBody(self:_:)")
856879
void BridgedConstructorDecl_setParsedBody(BridgedConstructorDecl decl,
857880
BridgedBraceStmt body);
@@ -1230,10 +1253,28 @@ BridgedCallExpr BridgedCallExpr_createParsed(BridgedASTContext cContext,
12301253
BridgedExpr fn,
12311254
BridgedArgumentList args);
12321255

1233-
SWIFT_NAME("BridgedClosureExpr.createParsed(_:declContext:parameterList:body:)")
1256+
SWIFT_NAME("BridgedClosureExpr.createParsed(_:declContext:attributes:"
1257+
"bracketRange:capturedSelfDecl:parameterList:asyncLoc:throwsLoc:"
1258+
"thrownType:arrowLoc:explicitResultType:inLoc:)")
12341259
BridgedClosureExpr BridgedClosureExpr_createParsed(
12351260
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1236-
BridgedParameterList cParamList, BridgedBraceStmt body);
1261+
BridgedDeclAttributes cAttributes, BridgedSourceRange cBracketRange,
1262+
BridgedNullableVarDecl cCapturedSelfDecl,
1263+
BridgedNullableParameterList cParameterList, BridgedSourceLoc cAsyncLoc,
1264+
BridgedSourceLoc cThrowsLoc, BridgedNullableTypeRepr cThrownType,
1265+
BridgedSourceLoc cArrowLoc, BridgedNullableTypeRepr cExplicitResultType,
1266+
BridgedSourceLoc cInLoc);
1267+
1268+
SWIFT_NAME("BridgedClosureExpr.setParameterList(self:_:)")
1269+
void BridgedClosureExpr_setParameterList(BridgedClosureExpr cClosure,
1270+
BridgedParameterList cParams);
1271+
1272+
SWIFT_NAME("BridgedClosureExpr.setHasAnonymousClosureVars(self:)")
1273+
void BridgedClosureExpr_setHasAnonymousClosureVars(BridgedClosureExpr cClosure);
1274+
1275+
SWIFT_NAME("BridgedClosureExpr.setBody(self:_:)")
1276+
void BridgedClosureExpr_setBody(BridgedClosureExpr cClosure,
1277+
BridgedBraceStmt cBody);
12371278

12381279
SWIFT_NAME("BridgedCoerceExpr.createParsed(_:asLoc:type:)")
12391280
BridgedCoerceExpr BridgedCoerceExpr_createParsed(BridgedASTContext cContext,

include/swift/AST/ASTBridgingImpl.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,31 @@ swift::DeclAttributes BridgedDeclAttributes::unbridged() const {
213213
return attrs;
214214
}
215215

216+
//===----------------------------------------------------------------------===//
217+
// MARK: BridgedParamDecl
218+
//===----------------------------------------------------------------------===//
219+
220+
swift::ParamSpecifier unbridge(BridgedParamSpecifier specifier) {
221+
switch (specifier) {
222+
#define CASE(ID) \
223+
case BridgedParamSpecifier##ID: \
224+
return swift::ParamSpecifier::ID;
225+
CASE(Default)
226+
CASE(InOut)
227+
CASE(Borrowing)
228+
CASE(Consuming)
229+
CASE(LegacyShared)
230+
CASE(LegacyOwned)
231+
CASE(ImplicitlyCopyableConsuming)
232+
#undef CASE
233+
}
234+
}
235+
236+
void BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
237+
BridgedParamSpecifier cSpecifier) {
238+
cDecl.unbridged()->setSpecifier(unbridge(cSpecifier));
239+
}
240+
216241
//===----------------------------------------------------------------------===//
217242
// MARK: BridgedSubscriptDecl
218243
//===----------------------------------------------------------------------===//

lib/AST/Bridging/DeclContextBridging.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/AST/ASTBridging.h"
1414

1515
#include "swift/AST/DeclContext.h"
16+
#include "swift/AST/Expr.h"
1617

1718
using namespace swift;
1819

@@ -29,3 +30,8 @@ BridgedDeclContext BridgedPatternBindingInitializer_asDeclContext(
2930
BridgedPatternBindingInitializer cInit) {
3031
return cInit.unbridged();
3132
}
33+
34+
BridgedDeclContext
35+
BridgedClosureExpr_asDeclContext(BridgedClosureExpr cClosure) {
36+
return cClosure.unbridged();
37+
}

lib/AST/Bridging/ExprBridging.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,42 @@ BridgedCallExpr BridgedCallExpr_createParsed(BridgedASTContext cContext,
123123

124124
BridgedClosureExpr BridgedClosureExpr_createParsed(
125125
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
126-
BridgedParameterList cParamList, BridgedBraceStmt body) {
127-
DeclAttributes attributes;
128-
SourceRange bracketRange;
129-
SourceLoc asyncLoc;
130-
SourceLoc throwsLoc;
131-
SourceLoc arrowLoc;
132-
SourceLoc inLoc;
133-
126+
BridgedDeclAttributes cAttributes, BridgedSourceRange cBracketRange,
127+
BridgedNullableVarDecl cCapturedSelfDecl,
128+
BridgedNullableParameterList cParameterList, BridgedSourceLoc cAsyncLoc,
129+
BridgedSourceLoc cThrowsLoc, BridgedNullableTypeRepr cThrownType,
130+
BridgedSourceLoc cArrowLoc, BridgedNullableTypeRepr cExplicitResultType,
131+
BridgedSourceLoc cInLoc) {
134132
ASTContext &context = cContext.unbridged();
135133
DeclContext *declContext = cDeclContext.unbridged();
134+
TypeExpr *throwsType = nullptr;
135+
if (auto *tyR = cThrownType.unbridged())
136+
throwsType = new (context) TypeExpr(tyR);
137+
TypeExpr *explicitResultType = nullptr;
138+
if (auto *tyR = cExplicitResultType.unbridged())
139+
explicitResultType = new (context) TypeExpr(tyR);
140+
141+
return new (context)
142+
ClosureExpr(cAttributes.unbridged(), cBracketRange.unbridged(),
143+
cCapturedSelfDecl.unbridged(), cParameterList.unbridged(),
144+
cAsyncLoc.unbridged(), cThrowsLoc.unbridged(), throwsType,
145+
cArrowLoc.unbridged(), cInLoc.unbridged(), explicitResultType,
146+
declContext);
147+
}
148+
149+
void BridgedClosureExpr_setParameterList(BridgedClosureExpr cClosure,
150+
BridgedParameterList cParams) {
151+
cClosure.unbridged()->setParameterList(cParams.unbridged());
152+
}
153+
154+
void BridgedClosureExpr_setHasAnonymousClosureVars(
155+
BridgedClosureExpr cClosure) {
156+
cClosure.unbridged()->setHasAnonymousClosureVars();
157+
}
136158

137-
auto *out = new (context) ClosureExpr(
138-
attributes, bracketRange, nullptr, cParamList.unbridged(), asyncLoc,
139-
throwsLoc,
140-
/*FIXME:thrownType=*/nullptr, arrowLoc, inLoc, nullptr, declContext);
141-
out->setBody(body.unbridged());
142-
return out;
159+
void BridgedClosureExpr_setBody(BridgedClosureExpr cClosure,
160+
BridgedBraceStmt cBody) {
161+
cClosure.unbridged()->setBody(cBody.unbridged());
143162
}
144163

145164
BridgedCoerceExpr BridgedCoerceExpr_createParsed(BridgedASTContext cContext,

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 120 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -278,36 +278,131 @@ extension ASTGenVisitor {
278278
return createOperatorRefExpr(token: node.operator, kind: .binaryOperator)
279279
}
280280

281-
func generate(closureExpr node: ClosureExprSyntax) -> BridgedClosureExpr {
282-
let params: BridgedParameterList
281+
func generate(closureSignature node: ClosureSignatureSyntax) -> GeneratedClosureSignature {
282+
var result = GeneratedClosureSignature()
283+
284+
// Attributes.
285+
visitIfConfigElements(node.attributes, of: AttributeSyntax.self) { element in
286+
switch element {
287+
case .ifConfigDecl(let ifConfigDecl):
288+
return .ifConfigDecl(ifConfigDecl)
289+
case .attribute(let attribute):
290+
return .underlying(attribute)
291+
}
292+
} body: { node in
293+
if let attr = self.generateDeclAttribute(attribute: node) {
294+
result.attributes.add(attr)
295+
}
296+
}
283297

284-
if let signature = node.signature {
285-
// FIXME: Translate the signature, capture list, 'in' location, etc.
286-
_ = signature
287-
fatalError("unimplmented")
288-
} else {
289-
let lBraceLoc = self.generateSourceLoc(node.leftBrace)
290-
params = BridgedParameterList.createParsed(
298+
if let node = node.capture {
299+
result.bracketRange = self.generateSourceRange(node)
300+
let captures = node.items.lazy.map { node in
301+
self.generate(closureCapture: node)
302+
}
303+
result.captureList = captures.bridgedArray(in: self)
304+
}
305+
306+
switch node.parameterClause {
307+
case .parameterClause(let node):
308+
result.params = self.generate(closureParameterClause: node)
309+
case .simpleInput(let node):
310+
result.params = self.generate(closureShorthandParameterList: node)
311+
case nil:
312+
result.params = .createParsed(
291313
self.ctx,
292-
leftParenLoc: lBraceLoc,
293-
parameters: .init(),
294-
rightParenLoc: lBraceLoc
314+
leftParenLoc: nil,
315+
parameters: BridgedArrayRef(),
316+
rightParenLoc: nil
295317
)
296318
}
297319

298-
let body = BridgedBraceStmt.createParsed(
299-
self.ctx,
300-
lBraceLoc: self.generateSourceLoc(node.leftBrace),
301-
elements: self.generate(codeBlockItemList: node.statements),
302-
rBraceLoc: self.generateSourceLoc(node.rightBrace)
303-
)
320+
if let effects = node.effectSpecifiers {
321+
result.asyncLoc = self.generateSourceLoc(effects.asyncSpecifier)
322+
result.throwsLoc = self.generateSourceLoc(effects.throwsClause)
323+
result.thrownType = effects.throwsClause?.type.map(generate(type:))
324+
}
304325

305-
return .createParsed(
326+
if let returnClause = node.returnClause {
327+
result.arrowLoc = self.generateSourceLoc(returnClause.arrow)
328+
result.explicitResultType = self.generate(type: returnClause.type)
329+
}
330+
331+
result.inLoc = self.generateSourceLoc(node.inKeyword)
332+
333+
return result
334+
}
335+
336+
func generate(closureCapture node: ClosureCaptureSyntax) {
337+
fatalError("unimplemented")
338+
}
339+
340+
struct GeneratedClosureSignature {
341+
var attributes: BridgedDeclAttributes = BridgedDeclAttributes()
342+
var bracketRange: BridgedSourceRange = BridgedSourceRange(start: nil, end: nil)
343+
var captureList: BridgedArrayRef = BridgedArrayRef()
344+
var capturedSelfDecl: BridgedVarDecl? = nil
345+
var params: BridgedParameterList? = nil
346+
var asyncLoc: BridgedSourceLoc = nil
347+
var throwsLoc: BridgedSourceLoc = nil
348+
var thrownType: BridgedTypeRepr? = nil
349+
var arrowLoc: BridgedSourceLoc = nil
350+
var explicitResultType: BridgedTypeRepr? = nil
351+
var inLoc: BridgedSourceLoc = nil
352+
}
353+
354+
func generate(closureExpr node: ClosureExprSyntax) -> BridgedClosureExpr {
355+
let signature: GeneratedClosureSignature
356+
if let node = node.signature {
357+
signature = self.generate(closureSignature: node)
358+
} else {
359+
signature = GeneratedClosureSignature()
360+
}
361+
362+
let expr = BridgedClosureExpr.createParsed(
306363
self.ctx,
307364
declContext: self.declContext,
308-
parameterList: params,
309-
body: body
365+
attributes: signature.attributes,
366+
bracketRange: signature.bracketRange,
367+
capturedSelfDecl: BridgedNullableVarDecl(raw: signature.capturedSelfDecl?.raw),
368+
parameterList: signature.params.asNullable,
369+
asyncLoc: signature.asyncLoc,
370+
throwsLoc: signature.throwsLoc,
371+
thrownType: signature.thrownType.asNullable,
372+
arrowLoc: signature.arrowLoc,
373+
explicitResultType: signature.explicitResultType.asNullable,
374+
inLoc: signature.inLoc
310375
)
376+
377+
let body = self.withDeclContext(expr.asDeclContext) {
378+
BridgedBraceStmt.createParsed(
379+
self.ctx,
380+
lBraceLoc: self.generateSourceLoc(node.leftBrace),
381+
elements: self.generate(codeBlockItemList: node.statements),
382+
rBraceLoc: self.generateSourceLoc(node.rightBrace)
383+
)
384+
}
385+
386+
if signature.params == nil {
387+
// TODO: Handle doller identifiers inside the closure.
388+
let loc = self.generateSourceLoc(node.leftBrace)
389+
let params = BridgedParameterList.createParsed(
390+
self.ctx,
391+
leftParenLoc: loc,
392+
parameters: .init(),
393+
rightParenLoc: loc
394+
)
395+
expr.setParameterList(params)
396+
expr.setHasAnonymousClosureVars()
397+
}
398+
399+
expr.setBody(body)
400+
401+
if signature.captureList.count > 0 {
402+
// TODO: CaptureListExpr.
403+
}
404+
405+
return expr
311406
}
312407

313408
func generate(consumeExpr node: ConsumeExprSyntax) -> BridgedConsumeExpr {
@@ -502,6 +597,10 @@ extension ASTGenVisitor {
502597
if node.baseName.isEditorPlaceholder {
503598
return generateEditorPlaceholderExpr(token: node.baseName).asExpr
504599
}
600+
if node.baseName.rawTokenKind == .dollarIdentifier {
601+
// TODO: Handle dollar identifier in closure decl context.
602+
// It's done in C++ Parser because it needs to handle inactive #if regions.
603+
}
505604
let nameAndLoc = generateDeclNameRef(declReferenceExpr: node)
506605
return BridgedUnresolvedDeclRefExpr.createParsed(
507606
self.ctx,

0 commit comments

Comments
 (0)