@@ -633,11 +633,47 @@ extension ASTGenVisitor {
633
633
// MARK: - AbstractFunctionDecl
634
634
635
635
extension ASTGenVisitor {
636
+ struct GeneratedFunctionSignature {
637
+ var parameterList : BridgedParameterList
638
+ var asyncLoc : BridgedSourceLoc
639
+ var isReasync : Bool
640
+ var throwsLoc : BridgedSourceLoc
641
+ var isRethrows : Bool
642
+ var thrownType : BridgedTypeRepr ?
643
+ var returnType : BridgedTypeRepr ?
644
+ }
645
+
646
+ func generate(
647
+ functionSignature node: FunctionSignatureSyntax ,
648
+ for context: ParameterContext
649
+ ) -> GeneratedFunctionSignature {
650
+ let parameterList = self . generate ( functionParameterClause: node. parameterClause, for: context)
651
+ let asyncLoc = self . generateSourceLoc ( node. effectSpecifiers? . asyncSpecifier)
652
+ let isReasync = node. effectSpecifiers? . asyncSpecifier? . rawText == " reasync "
653
+ let throwsLoc = self . generateSourceLoc ( node. effectSpecifiers? . throwsClause? . throwsSpecifier)
654
+ let isRethrows = node. effectSpecifiers? . throwsClause? . throwsSpecifier. rawText == " rethrows "
655
+ let thrownType = ( node. effectSpecifiers? . thrownError) . map ( self . generate ( type: ) )
656
+ let returnType = ( node. returnClause? . type) . map ( self . generate ( type: ) )
657
+ return GeneratedFunctionSignature (
658
+ parameterList: parameterList,
659
+ asyncLoc: asyncLoc,
660
+ isReasync: isReasync,
661
+ throwsLoc: throwsLoc,
662
+ isRethrows: isRethrows,
663
+ thrownType: thrownType,
664
+ returnType: returnType
665
+ )
666
+ }
667
+
636
668
func generate( functionDecl node: FunctionDeclSyntax ) -> BridgedFuncDecl ? {
637
- let attrs = self . generateDeclAttributes ( node, allowStatic: true )
669
+ var attrs = self . generateDeclAttributes ( node, allowStatic: true )
638
670
guard let ( name, nameLoc) = self . generateIdentifierDeclNameAndLoc ( node. name) else {
639
671
return nil
640
672
}
673
+ let signature = self . generate (
674
+ functionSignature: node. signature,
675
+ for: name. isOperator ? . operator : . function
676
+ )
641
677
642
678
let decl = BridgedFuncDecl . createParsed (
643
679
self . ctx,
@@ -648,13 +684,19 @@ extension ASTGenVisitor {
648
684
name: name,
649
685
nameLoc: nameLoc,
650
686
genericParamList: self . generate ( genericParameterClause: node. genericParameterClause) ,
651
- parameterList: self . generate ( functionParameterClause : node . signature. parameterClause , for : name . isOperator ? . operator : . function ) ,
652
- asyncSpecifierLoc: self . generateSourceLoc ( node . signature. effectSpecifiers ? . asyncSpecifier ) ,
653
- throwsSpecifierLoc: self . generateSourceLoc ( node . signature. effectSpecifiers ? . throwsClause ? . throwsSpecifier ) ,
654
- thrownType: self . generate ( type : node . signature. effectSpecifiers ? . thrownError ) ,
655
- returnType: self . generate ( type : node . signature. returnClause ? . type ) ,
687
+ parameterList: signature. parameterList ,
688
+ asyncSpecifierLoc: signature. asyncLoc ,
689
+ throwsSpecifierLoc: signature. throwsLoc ,
690
+ thrownType: signature. thrownType . asNullable ,
691
+ returnType: signature. returnType . asNullable ,
656
692
genericWhereClause: self . generate ( genericWhereClause: node. genericWhereClause)
657
693
)
694
+ if ( signature. isReasync) {
695
+ attrs. attributes. add ( BridgedDeclAttribute . createSimple ( self . ctx, kind: . reasync, atLoc: nil , nameLoc: signature. asyncLoc) )
696
+ }
697
+ if ( signature. isRethrows) {
698
+ attrs. attributes. add ( BridgedDeclAttribute . createSimple ( self . ctx, kind: . rethrows, atLoc: nil , nameLoc: signature. throwsLoc) )
699
+ }
658
700
decl. asDecl. attachParsedAttrs ( attrs. attributes)
659
701
660
702
if let body = node. body {
@@ -667,7 +709,11 @@ extension ASTGenVisitor {
667
709
}
668
710
669
711
func generate( initializerDecl node: InitializerDeclSyntax ) -> BridgedConstructorDecl {
670
- let attrs = self . generateDeclAttributes ( node, allowStatic: false )
712
+ var attrs = self . generateDeclAttributes ( node, allowStatic: false )
713
+ let signature = self . generate (
714
+ functionSignature: node. signature,
715
+ for: . initializer
716
+ )
671
717
672
718
let decl = BridgedConstructorDecl . createParsed (
673
719
self . ctx,
@@ -676,13 +722,24 @@ extension ASTGenVisitor {
676
722
failabilityMarkLoc: self . generateSourceLoc ( node. optionalMark) ,
677
723
isIUO: node. optionalMark? . rawTokenKind == . exclamationMark,
678
724
genericParamList: self . generate ( genericParameterClause: node. genericParameterClause) ,
679
- parameterList: self . generate ( functionParameterClause : node . signature. parameterClause , for : . initializer ) ,
680
- asyncSpecifierLoc: self . generateSourceLoc ( node . signature. effectSpecifiers ? . asyncSpecifier ) ,
681
- throwsSpecifierLoc: self . generateSourceLoc ( node . signature. effectSpecifiers ? . throwsClause ? . throwsSpecifier ) ,
682
- thrownType: self . generate ( type : node . signature. effectSpecifiers ? . thrownError ) ,
725
+ parameterList: signature. parameterList ,
726
+ asyncSpecifierLoc: signature. asyncLoc ,
727
+ throwsSpecifierLoc: signature. throwsLoc ,
728
+ thrownType: signature. thrownType . asNullable ,
683
729
genericWhereClause: self . generate ( genericWhereClause: node. genericWhereClause)
684
730
)
731
+ if ( signature. isReasync) {
732
+ attrs. attributes. add ( BridgedDeclAttribute . createSimple ( self . ctx, kind: . reasync, atLoc: nil , nameLoc: signature. asyncLoc) )
733
+ }
734
+ if ( signature. isRethrows) {
735
+ attrs. attributes. add ( BridgedDeclAttribute . createSimple ( self . ctx, kind: . rethrows, atLoc: nil , nameLoc: signature. throwsLoc) )
736
+ }
685
737
decl. asDecl. attachParsedAttrs ( attrs. attributes)
738
+
739
+ guard signature. returnType == nil else {
740
+ // TODO: Diagnose.
741
+ fatalError ( " unexpected return type in initializer decl " )
742
+ }
686
743
687
744
if let body = node. body {
688
745
self . withDeclContext ( decl. asDeclContext) {
0 commit comments