Skip to content

Commit 86f421a

Browse files
committed
[AST] Sink the flag collection logic from Parse to AST
* Collect flag in `ParamDecl::setTypeRepr()`. * [ASTGen] Separate `BridgedParamDecl.setTypeRepr(_:)` from `BridgedParamDecl.createParsed(_:)` aligning with C++ API. The majority of the creations don't set the typerepr. * Update `ParamSpecifierRequest::evaluate` to handle non-implicit `ParamDecl` without `TypeRepr` (i.e. untyped closure parameter), instead of `setSpecifier(::Default)` manually in Parse.
1 parent 2ceb8f1 commit 86f421a

File tree

10 files changed

+71
-96
lines changed

10 files changed

+71
-96
lines changed

include/swift/AST/ASTBridging.h

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

13161316
SWIFT_NAME("BridgedParamDecl.createParsed(_:declContext:specifierLoc:argName:"
1317-
"argNameLoc:paramName:paramNameLoc:type:defaultValue:"
1317+
"argNameLoc:paramName:paramNameLoc:defaultValue:"
13181318
"defaultValueInitContext:)")
13191319
BridgedParamDecl BridgedParamDecl_createParsed(
13201320
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
13211321
BridgedSourceLoc cSpecifierLoc, BridgedIdentifier cArgName,
13221322
BridgedSourceLoc cArgNameLoc, BridgedIdentifier cParamName,
1323-
BridgedSourceLoc cParamNameLoc, BridgedNullableTypeRepr type,
1324-
BridgedNullableExpr defaultValue,
1323+
BridgedSourceLoc cParamNameLoc, BridgedNullableExpr defaultValue,
13251324
BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext);
13261325

1326+
SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)")
1327+
BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
1328+
BridgedTypeRepr cType);
1329+
13271330
/// The various spellings of ownership modifier that can be used in source.
13281331
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
13291332
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8712,6 +8712,45 @@ ParamDecl *ParamDecl::createParsed(
87128712
return decl;
87138713
}
87148714

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

607607
param->setTypeRepr(type);
608608

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

661623
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)