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