@@ -61,22 +61,41 @@ extension EnumCaseParameterSyntax: ValueParameterSyntax {
61
61
}
62
62
63
63
extension 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)
66
67
}
67
68
68
69
func generate( enumCaseParameter node: EnumCaseParameterSyntax ) -> BridgedParamDecl {
69
- self . makeParamDecl ( node)
70
+ self . makeParamDecl ( node, argNameByDefault : true )
70
71
}
71
72
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 {
73
76
// FIXME: This location should be derived from the type repr.
74
77
let specifierLoc : BridgedSourceLoc = nil
75
78
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
+ }
80
99
81
100
var type = node. optionalType. map ( generate ( type: ) )
82
101
if let ellipsis = node. ellipsis, let base = type {
@@ -92,10 +111,10 @@ extension ASTGenVisitor {
92
111
self . ctx,
93
112
declContext: self . declContext,
94
113
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 ,
99
118
type: type. asNullable,
100
119
defaultValue: self . generate ( expr: node. defaultValue? . value)
101
120
)
@@ -105,11 +124,14 @@ extension ASTGenVisitor {
105
124
// MARK: - ParameterList
106
125
107
126
extension ASTGenVisitor {
108
- func generate( functionParameterClause node: FunctionParameterClauseSyntax ) -> BridgedParameterList {
127
+ func generate(
128
+ functionParameterClause node: FunctionParameterClauseSyntax ,
129
+ forSubscript: Bool
130
+ ) -> BridgedParameterList {
109
131
BridgedParameterList . createParsed (
110
132
self . ctx,
111
133
leftParenLoc: self . generateSourceLoc ( node. leftParen) ,
112
- parameters: self . generate ( functionParameterList: node. parameters) ,
134
+ parameters: self . generate ( functionParameterList: node. parameters, forSubscript : forSubscript ) ,
113
135
rightParenLoc: self . generateSourceLoc ( node. rightParen)
114
136
)
115
137
}
@@ -129,10 +151,10 @@ extension ASTGenVisitor {
129
151
self . ctx,
130
152
declContext: self . declContext,
131
153
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,
136
158
type: nil ,
137
159
defaultValue: nil
138
160
)
@@ -147,7 +169,7 @@ extension ASTGenVisitor {
147
169
148
170
extension ASTGenVisitor {
149
171
@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 )
152
174
}
153
175
}
0 commit comments