@@ -61,22 +61,41 @@ extension EnumCaseParameterSyntax: ValueParameterSyntax {
6161}
6262
6363extension ASTGenVisitor {
64- func generate( functionParameter node: FunctionParameterSyntax ) -> BridgedParamDecl {
65- self . makeParamDecl ( node)
64+ func generate( functionParameter node: FunctionParameterSyntax , forSubscript: Bool ) -> BridgedParamDecl {
65+ // For non-subscripts, the argument name is defaulted to the parameter name.
66+ self . makeParamDecl ( node, argNameByDefault: !forSubscript)
6667 }
6768
6869 func generate( enumCaseParameter node: EnumCaseParameterSyntax ) -> BridgedParamDecl {
69- self . makeParamDecl ( node)
70+ self . makeParamDecl ( node, argNameByDefault : true )
7071 }
7172
72- private func makeParamDecl( _ node: some ValueParameterSyntax ) -> BridgedParamDecl {
73+ /// Generate a ParamDecl. If `argNameByDefault` is true, then the parameter's
74+ /// argument label is inferred from the first name if no second name is present.
75+ private func makeParamDecl( _ node: some ValueParameterSyntax , argNameByDefault: Bool ) -> BridgedParamDecl {
7376 // FIXME: This location should be derived from the type repr.
7477 let specifierLoc : BridgedSourceLoc = nil
7578
76- let ( firstName, firstNameLoc) =
77- self . generateIdentifierAndSourceLoc ( node. optionalFirstName)
78- let ( secondName, secondNameLoc) =
79- self . generateIdentifierAndSourceLoc ( node. secondName)
79+ let paramName : BridgedIdentifier
80+ let paramNameLoc : BridgedSourceLoc
81+ let argName : BridgedIdentifier
82+ let argNameLoc : BridgedSourceLoc
83+
84+ // Map the first name and second name to argument name and parameter name.
85+ // If we have both, use them. If we only have one, then use that as the
86+ // parameter name, inferring it as the argument name if we're allowed to.
87+ switch ( node. optionalFirstName, node. secondName) {
88+ case let ( argNameNode? , paramNameNode? ) :
89+ ( argName, argNameLoc) = self . generateIdentifierAndSourceLoc ( argNameNode)
90+ ( paramName, paramNameLoc) = self . generateIdentifierAndSourceLoc ( paramNameNode)
91+ case let ( nameNode? , nil ) :
92+ ( paramName, paramNameLoc) = self . generateIdentifierAndSourceLoc ( nameNode)
93+ ( argName, argNameLoc) = argNameByDefault ? ( paramName, paramNameLoc) : ( nil , nil )
94+ case ( nil , _? ) :
95+ preconditionFailure ( " If only one name is present, it should be the first " )
96+ default :
97+ ( argName, argNameLoc, paramName, paramNameLoc) = ( nil , nil , nil , nil )
98+ }
8099
81100 var type = node. optionalType. map ( generate ( type: ) )
82101 if let ellipsis = node. ellipsis, let base = type {
@@ -92,10 +111,10 @@ extension ASTGenVisitor {
92111 self . ctx,
93112 declContext: self . declContext,
94113 specifierLoc: specifierLoc,
95- firstName : firstName ,
96- firstNameLoc : firstNameLoc ,
97- secondName : secondName ,
98- secondNameLoc : secondNameLoc ,
114+ argName : argName ,
115+ argNameLoc : argNameLoc ,
116+ paramName : paramName ,
117+ paramNameLoc : paramNameLoc ,
99118 type: type. asNullable,
100119 defaultValue: self . generate ( expr: node. defaultValue? . value)
101120 )
@@ -105,11 +124,14 @@ extension ASTGenVisitor {
105124// MARK: - ParameterList
106125
107126extension ASTGenVisitor {
108- func generate( functionParameterClause node: FunctionParameterClauseSyntax ) -> BridgedParameterList {
127+ func generate(
128+ functionParameterClause node: FunctionParameterClauseSyntax ,
129+ forSubscript: Bool
130+ ) -> BridgedParameterList {
109131 BridgedParameterList . createParsed (
110132 self . ctx,
111133 leftParenLoc: self . generateSourceLoc ( node. leftParen) ,
112- parameters: self . generate ( functionParameterList: node. parameters) ,
134+ parameters: self . generate ( functionParameterList: node. parameters, forSubscript : forSubscript ) ,
113135 rightParenLoc: self . generateSourceLoc ( node. rightParen)
114136 )
115137 }
@@ -129,10 +151,10 @@ extension ASTGenVisitor {
129151 self . ctx,
130152 declContext: self . declContext,
131153 specifierLoc: nil ,
132- firstName : nil ,
133- firstNameLoc : nil ,
134- secondName : name,
135- secondNameLoc : nameLoc,
154+ argName : nil ,
155+ argNameLoc : nil ,
156+ paramName : name,
157+ paramNameLoc : nameLoc,
136158 type: nil ,
137159 defaultValue: nil
138160 )
@@ -147,7 +169,7 @@ extension ASTGenVisitor {
147169
148170extension ASTGenVisitor {
149171 @inline ( __always)
150- func generate( functionParameterList node: FunctionParameterListSyntax ) -> BridgedArrayRef {
151- node. lazy. map ( self . generate) . bridgedArray ( in: self )
172+ func generate( functionParameterList node: FunctionParameterListSyntax , forSubscript : Bool ) -> BridgedArrayRef {
173+ node. lazy. map ( { self . generate ( functionParameter : $0 , forSubscript : forSubscript ) } ) . bridgedArray ( in: self )
152174 }
153175}
0 commit comments