Skip to content

Commit 6805e15

Browse files
authored
Merge pull request #79503 from rintaro/astgen-paramdecl-typerepr
[AST] Sink ParamDecl flag collection logic from Parse to AST
2 parents 3115f38 + 8833ff5 commit 6805e15

File tree

10 files changed

+73
-96
lines changed

10 files changed

+73
-96
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,16 +1317,19 @@ BridgedPatternBindingDecl BridgedPatternBindingDecl_createParsed(
13171317
BridgedVarDeclIntroducer cIntorducer, BridgedArrayRef cBindingEntries);
13181318

13191319
SWIFT_NAME("BridgedParamDecl.createParsed(_:declContext:specifierLoc:argName:"
1320-
"argNameLoc:paramName:paramNameLoc:type:defaultValue:"
1320+
"argNameLoc:paramName:paramNameLoc:defaultValue:"
13211321
"defaultValueInitContext:)")
13221322
BridgedParamDecl BridgedParamDecl_createParsed(
13231323
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
13241324
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cArgName,
13251325
BridgedSourceLoc cArgNameLoc, BridgedIdentifier cParamName,
1326-
BridgedSourceLoc cParamNameLoc, BridgedNullableTypeRepr type,
1327-
BridgedNullableExpr defaultValue,
1326+
BridgedSourceLoc cParamNameLoc, BridgedNullableExpr defaultValue,
13281327
BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext);
13291328

1329+
SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)")
1330+
BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
1331+
BridgedTypeRepr cType);
1332+
13301333
/// The various spellings of ownership modifier that can be used in source.
13311334
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
13321335
BridgedParamSpecifierDefault,

include/swift/AST/ASTBridgingImpl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ swift::ParamSpecifier unbridge(BridgedParamSpecifier specifier) {
310310
}
311311
}
312312

313+
void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
314+
BridgedTypeRepr cType) {
315+
cDecl.unbridged()->setTypeRepr(cType.unbridged());
316+
}
317+
313318
void BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
314319
BridgedParamSpecifier cSpecifier) {
315320
cDecl.unbridged()->setSpecifier(unbridge(cSpecifier));

include/swift/AST/Decl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7031,7 +7031,9 @@ class ParamDecl : public VarDecl {
70317031

70327032
/// Retrieve the TypeRepr corresponding to the parsed type of the parameter, if it exists.
70337033
TypeRepr *getTypeRepr() const { return TyReprAndFlags.getPointer(); }
7034-
void setTypeRepr(TypeRepr *repr) { TyReprAndFlags.setPointer(repr); }
7034+
7035+
/// Set the parsed TypeRepr on the parameter.
7036+
void setTypeRepr(TypeRepr *repr);
70357037

70367038
bool isDestructured() const {
70377039
auto flags = ArgumentNameAndFlags.getInt();

lib/AST/Bridging/DeclBridging.cpp

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -193,58 +193,13 @@ BridgedParamDecl BridgedParamDecl_createParsed(
193193
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
194194
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cArgName,
195195
BridgedSourceLoc cArgNameLoc, BridgedIdentifier cParamName,
196-
BridgedSourceLoc cParamNameLoc, BridgedNullableTypeRepr opaqueType,
197-
BridgedNullableExpr cDefaultArgument,
196+
BridgedSourceLoc cParamNameLoc, BridgedNullableExpr cDefaultArgument,
198197
BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext) {
199-
auto *paramDecl = ParamDecl::createParsed(
198+
return ParamDecl::createParsed(
200199
cContext.unbridged(), cSpecifierLoc.unbridged(), cArgNameLoc.unbridged(),
201200
cArgName.unbridged(), cParamNameLoc.unbridged(), cParamName.unbridged(),
202201
cDefaultArgument.unbridged(), cDefaultArgumentInitContext.unbridged(),
203202
cDeclContext.unbridged());
204-
205-
if (auto type = opaqueType.unbridged()) {
206-
paramDecl->setTypeRepr(type);
207-
208-
// FIXME: Copied from 'Parser::parsePattern()'. This should be in Sema.
209-
// Dig through the type to find any attributes or modifiers that are
210-
// associated with the type but should also be reflected on the
211-
// declaration.
212-
auto unwrappedType = type;
213-
while (true) {
214-
if (auto *ATR = dyn_cast<AttributedTypeRepr>(unwrappedType)) {
215-
auto attrs = ATR->getAttrs();
216-
// At this point we actually don't know if that's valid to mark
217-
// this parameter declaration as `autoclosure` because type has
218-
// not been resolved yet - it should either be a function type
219-
// or typealias with underlying function type.
220-
bool autoclosure = llvm::any_of(attrs, [](TypeOrCustomAttr attr) {
221-
if (auto typeAttr = attr.dyn_cast<TypeAttribute *>())
222-
return isa<AutoclosureTypeAttr>(typeAttr);
223-
return false;
224-
});
225-
paramDecl->setAutoClosure(autoclosure);
226-
227-
unwrappedType = ATR->getTypeRepr();
228-
continue;
229-
}
230-
231-
if (auto *STR = dyn_cast<SpecifierTypeRepr>(unwrappedType)) {
232-
if (isa<IsolatedTypeRepr>(STR))
233-
paramDecl->setIsolated(true);
234-
else if (isa<CompileTimeConstTypeRepr>(STR))
235-
paramDecl->setCompileTimeConst(true);
236-
else if (isa<SendingTypeRepr>(STR))
237-
paramDecl->setSending(true);
238-
239-
unwrappedType = STR->getBase();
240-
continue;
241-
}
242-
243-
break;
244-
}
245-
}
246-
247-
return paramDecl;
248203
}
249204

250205
void BridgedConstructorDecl_setParsedBody(BridgedConstructorDecl decl,

lib/AST/Decl.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8714,6 +8714,47 @@ ParamDecl *ParamDecl::createParsed(
87148714
return decl;
87158715
}
87168716

8717+
void ParamDecl::setTypeRepr(TypeRepr *repr) {
8718+
ASSERT(!getTypeRepr() && "TypeRepr already set");
8719+
8720+
TyReprAndFlags.setPointer(repr);
8721+
8722+
// Dig through the type to find any attributes or modifiers that are
8723+
// associated with the type but should also be reflected on the
8724+
// declaration.
8725+
{
8726+
auto unwrappedType = repr;
8727+
while (true) {
8728+
if (auto *ATR = dyn_cast<AttributedTypeRepr>(unwrappedType)) {
8729+
// At this point we actually don't know if that's valid to mark
8730+
// this parameter declaration as `autoclosure` because type has
8731+
// not been resolved yet - it should either be a function type
8732+
// or typealias with underlying function type.
8733+
if (ATR->has(TypeAttrKind::Autoclosure))
8734+
setAutoClosure(true);
8735+
if (ATR->has(TypeAttrKind::Addressable))
8736+
setAddressable(true);
8737+
8738+
unwrappedType = ATR->getTypeRepr();
8739+
continue;
8740+
}
8741+
8742+
if (auto *STR = dyn_cast<SpecifierTypeRepr>(unwrappedType)) {
8743+
if (isa<IsolatedTypeRepr>(STR))
8744+
setIsolated(true);
8745+
else if (isa<CompileTimeConstTypeRepr>(STR))
8746+
setCompileTimeConst(true);
8747+
else if (isa<SendingTypeRepr>(STR))
8748+
setSending(true);
8749+
unwrappedType = STR->getBase();
8750+
continue;
8751+
}
8752+
8753+
break;
8754+
}
8755+
}
8756+
}
8757+
87178758
void ParamDecl::setDefaultArgumentKind(DefaultArgumentKind K) {
87188759
assert(getDefaultArgumentKind() == DefaultArgumentKind::None &&
87198760
"Overwrite of default argument kind");

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ extension ASTGenVisitor {
365365
argNameLoc: nil,
366366
paramName: ctx.getDollarIdentifier(idx),
367367
paramNameLoc: loc,
368-
type: nil,
369368
defaultValue: nil,
370369
defaultValueInitContext: nil
371370
)

lib/ASTGen/Sources/ASTGen/ParameterClause.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@ extension ASTGenVisitor {
176176
argNameLoc: argNameLoc,
177177
paramName: paramName,
178178
paramNameLoc: paramNameLoc,
179-
type: type.asNullable,
180179
defaultValue: initExpr.asNullable,
181180
defaultValueInitContext: initContext.asNullable
182181
)
182+
if let type {
183+
param.setTypeRepr(type)
184+
}
183185
param.asDecl.attachParsedAttrs(attrs)
184186
return param
185187
}
@@ -194,11 +196,9 @@ extension ASTGenVisitor {
194196
argNameLoc: nil,
195197
paramName: name.identifier,
196198
paramNameLoc: name.sourceLoc,
197-
type: nil,
198199
defaultValue: nil,
199200
defaultValueInitContext: nil
200201
)
201-
param.setSpecifier(.default)
202202
return param
203203
}
204204
}
@@ -259,7 +259,6 @@ extension ASTGenVisitor {
259259
argNameLoc: nil,
260260
paramName: name,
261261
paramNameLoc: nameLoc,
262-
type: nil,
263262
defaultValue: nil,
264263
defaultValueInitContext: nil
265264
)
@@ -276,7 +275,6 @@ extension ASTGenVisitor {
276275
params.reserveCapacity(node.parameters.count)
277276
for (index, node) in node.parameters.enumerated() {
278277
let param = self.generate(closureParameter: node, at: index)
279-
param.setSpecifier(.default)
280278
params.append(param)
281279
}
282280

lib/Parse/ParsePattern.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -607,40 +607,6 @@ mapParsedParameters(Parser &parser,
607607

608608
param->setTypeRepr(type);
609609

610-
// Dig through the type to find any attributes or modifiers that are
611-
// associated with the type but should also be reflected on the
612-
// declaration.
613-
{
614-
auto unwrappedType = type;
615-
while (true) {
616-
if (auto *ATR = dyn_cast<AttributedTypeRepr>(unwrappedType)) {
617-
// At this point we actually don't know if that's valid to mark
618-
// this parameter declaration as `autoclosure` because type has
619-
// not been resolved yet - it should either be a function type
620-
// or typealias with underlying function type.
621-
if (ATR->has(TypeAttrKind::Autoclosure))
622-
param->setAutoClosure(true);
623-
if (ATR->has(TypeAttrKind::Addressable))
624-
param->setAddressable(true);
625-
626-
unwrappedType = ATR->getTypeRepr();
627-
continue;
628-
}
629-
630-
if (auto *STR = dyn_cast<SpecifierTypeRepr>(unwrappedType)) {
631-
if (isa<IsolatedTypeRepr>(STR))
632-
param->setIsolated(true);
633-
else if (isa<CompileTimeConstTypeRepr>(STR))
634-
param->setCompileTimeConst(true);
635-
else if (isa<SendingTypeRepr>(STR))
636-
param->setSending(true);
637-
unwrappedType = STR->getBase();
638-
continue;
639-
}
640-
641-
break;
642-
}
643-
}
644610
} else if (paramInfo.SpecifierLoc.isValid()) {
645611
llvm::SmallString<16> specifier;
646612
{
@@ -653,10 +619,6 @@ mapParsedParameters(Parser &parser,
653619
specifier);
654620
paramInfo.SpecifierLoc = SourceLoc();
655621
paramInfo.SpecifierKind = ParamDecl::Specifier::Default;
656-
657-
param->setSpecifier(ParamSpecifier::Default);
658-
} else {
659-
param->setSpecifier(ParamSpecifier::Default);
660622
}
661623

662624
return param;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,12 @@ ParamSpecifierRequest::evaluate(Evaluator &evaluator,
22342234
}
22352235

22362236
auto typeRepr = param->getTypeRepr();
2237+
2238+
if (!typeRepr && !param->isImplicit()) {
2239+
// Untyped closure parameter.
2240+
return ParamSpecifier::Default;
2241+
}
2242+
22372243
assert(typeRepr != nullptr && "Should call setSpecifier() on "
22382244
"synthesized parameter declarations");
22392245

test/ASTGen/decls.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ func test2(y: Int = 0, oi: Int? = nil) -> Int {
4949
}
5050

5151
func test3(_ b: inout Bool) {
52-
// b = true
52+
b = true
53+
}
54+
55+
func testInOutClosureParam() -> (inout (Int, String)) -> Void {
56+
return { (arg: inout (Int, String)) in
57+
arg.1 = "rewritten"
58+
}
5359
}
5460

5561
func test4(_ i: _const Int) {

0 commit comments

Comments
 (0)