@@ -645,11 +645,47 @@ extension ASTGenVisitor {
645645// MARK: - AbstractFunctionDecl
646646
647647extension 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+
648680 func generate( functionDecl node: FunctionDeclSyntax ) -> BridgedFuncDecl ? {
649- let attrs = self . generateDeclAttributes ( node, allowStatic: true )
681+ var attrs = self . generateDeclAttributes ( node, allowStatic: true )
650682 guard let ( name, nameLoc) = self . generateIdentifierDeclNameAndLoc ( node. name) else {
651683 return nil
652684 }
685+ let signature = self . generate (
686+ functionSignature: node. signature,
687+ for: name. isOperator ? . operator : . function
688+ )
653689
654690 let decl = BridgedFuncDecl . createParsed (
655691 self . ctx,
@@ -660,13 +696,19 @@ extension ASTGenVisitor {
660696 name: name,
661697 nameLoc: nameLoc,
662698 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 ,
668704 genericWhereClause: self . generate ( genericWhereClause: node. genericWhereClause)
669705 )
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+ }
670712 decl. asDecl. attachParsedAttrs ( attrs. attributes)
671713
672714 if let body = node. body {
@@ -679,7 +721,11 @@ extension ASTGenVisitor {
679721 }
680722
681723 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+ )
683729
684730 let decl = BridgedConstructorDecl . createParsed (
685731 self . ctx,
@@ -688,13 +734,24 @@ extension ASTGenVisitor {
688734 failabilityMarkLoc: self . generateSourceLoc ( node. optionalMark) ,
689735 isIUO: node. optionalMark? . rawTokenKind == . exclamationMark,
690736 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 ,
695741 genericWhereClause: self . generate ( genericWhereClause: node. genericWhereClause)
696742 )
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+ }
697749 decl. asDecl. attachParsedAttrs ( attrs. attributes)
750+
751+ guard signature. returnType == nil else {
752+ // TODO: Diagnose.
753+ fatalError ( " unexpected return type in initializer decl " )
754+ }
698755
699756 if let body = node. body {
700757 self . withDeclContext ( decl. asDeclContext) {
0 commit comments