Skip to content

Commit 7770564

Browse files
authored
Merge pull request #27624 from CodaFi/in-locale-parentis
Downgrade The TypeLoc in VarDecl to a TypeRepr
2 parents adfd8b3 + 660f66d commit 7770564

27 files changed

+204
-223
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -369,21 +369,14 @@ class alignas(1 << DeclAlignInBits) Decl {
369369
IsPropertyWrapperBackingProperty : 1
370370
);
371371

372-
SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1+2+1+NumDefaultArgumentKindBits,
372+
SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1+2+NumDefaultArgumentKindBits,
373373
/// Whether we've computed the specifier yet.
374374
SpecifierComputed : 1,
375375

376376
/// The specifier associated with this parameter. This determines
377377
/// the storage semantics of the value e.g. mutability.
378378
Specifier : 2,
379379

380-
/// True if the type is implicitly specified in the source, but this has an
381-
/// apparently valid typeRepr. This is used in accessors, which look like:
382-
/// set (value) {
383-
/// but need to get the typeRepr from the property as a whole so Sema can
384-
/// resolve the type.
385-
IsTypeLocImplicit : 1,
386-
387380
/// Information about a symbolic default argument, like #file.
388381
defaultArgumentKind : NumDefaultArgumentKindBits
389382
);
@@ -4804,8 +4797,7 @@ class VarDecl : public AbstractStorageDecl {
48044797
bool issCaptureList, SourceLoc nameLoc, Identifier name,
48054798
DeclContext *dc, StorageIsMutable_t supportsMutation);
48064799

4807-
/// This is the type specified, including location information.
4808-
TypeLoc typeLoc;
4800+
TypeRepr *ParentRepr = nullptr;
48094801

48104802
Type typeInContext;
48114803

@@ -4825,8 +4817,10 @@ class VarDecl : public AbstractStorageDecl {
48254817
return hasName() ? getBaseName().getIdentifier().str() : "_";
48264818
}
48274819

4828-
TypeLoc &getTypeLoc() { return typeLoc; }
4829-
TypeLoc getTypeLoc() const { return typeLoc; }
4820+
/// Retrieve the TypeRepr corresponding to the parsed type of the parent
4821+
/// pattern, if it exists.
4822+
TypeRepr *getTypeRepr() const { return ParentRepr; }
4823+
void setTypeRepr(TypeRepr *repr) { ParentRepr = repr; }
48304824

48314825
bool hasType() const {
48324826
// We have a type if either the type has been computed already or if
@@ -5201,10 +5195,8 @@ class ParamDecl : public VarDecl {
52015195
Identifier argumentName, SourceLoc parameterNameLoc,
52025196
Identifier parameterName, DeclContext *dc);
52035197

5204-
/// Clone constructor, allocates a new ParamDecl identical to the first.
5205-
/// Intentionally not defined as a typical copy constructor to avoid
5206-
/// accidental copies.
5207-
ParamDecl(ParamDecl *PD, bool withTypes);
5198+
/// Create a new ParamDecl identical to the first except without the interface type.
5199+
static ParamDecl *cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD);
52085200

52095201
/// Retrieve the argument (API) name for this function parameter.
52105202
Identifier getArgumentName() const { return ArgumentName; }
@@ -5221,10 +5213,7 @@ class ParamDecl : public VarDecl {
52215213
SourceLoc getParameterNameLoc() const { return ParameterNameLoc; }
52225214

52235215
SourceLoc getSpecifierLoc() const { return SpecifierLoc; }
5224-
5225-
bool isTypeLocImplicit() const { return Bits.ParamDecl.IsTypeLocImplicit; }
5226-
void setIsTypeLocImplicit(bool val) { Bits.ParamDecl.IsTypeLocImplicit = val; }
5227-
5216+
52285217
DefaultArgumentKind getDefaultArgumentKind() const {
52295218
return static_cast<DefaultArgumentKind>(Bits.ParamDecl.defaultArgumentKind);
52305219
}

include/swift/AST/ParameterList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ class alignas(ParamDecl *) ParameterList final :
111111
/// The cloned pattern is for an inherited constructor; mark default
112112
/// arguments as inherited, and mark unnamed arguments as named.
113113
Inherited = 0x02,
114-
/// The cloned pattern will strip type information.
115-
WithoutTypes = 0x04,
116114
};
117115

118116
friend OptionSet<CloneFlags> operator|(CloneFlags flag1, CloneFlags flag2) {

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,8 +2523,10 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
25232523
});
25242524
if (decl->hasInterfaceType()) {
25252525
Printer << ": ";
2526-
auto tyLoc = decl->getTypeLoc();
2527-
if (!tyLoc.getTypeRepr())
2526+
TypeLoc tyLoc;
2527+
if (auto *repr = decl->getTypeRepr())
2528+
tyLoc = TypeLoc(repr, decl->getInterfaceType());
2529+
else
25282530
tyLoc = TypeLoc::withoutLoc(decl->getInterfaceType());
25292531

25302532
Printer.printDeclResultTypePre(decl, tyLoc);
@@ -2592,13 +2594,15 @@ void PrintAST::printOneParameter(const ParamDecl *param,
25922594
Printer << ": ";
25932595
};
25942596

2595-
auto TheTypeLoc = param->getTypeLoc();
2596-
25972597
printAttributes(param);
25982598
printArgName();
25992599

2600-
if (!TheTypeLoc.getTypeRepr() && param->hasInterfaceType())
2600+
TypeLoc TheTypeLoc;
2601+
if (auto *repr = param->getTypeRepr()) {
2602+
TheTypeLoc = TypeLoc(repr, param->getInterfaceType());
2603+
} else {
26012604
TheTypeLoc = TypeLoc::withoutLoc(param->getInterfaceType());
2605+
}
26022606

26032607
// If the parameter is variadic, we will print the "..." after it, but we have
26042608
// to strip off the added array type.

lib/AST/ASTWalker.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,13 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
11581158

11591159
// Don't walk into the type if the decl is implicit, or if the type is
11601160
// implicit.
1161-
if (!P->isImplicit() && !P->isTypeLocImplicit() &&
1162-
doIt(P->getTypeLoc()))
1163-
return true;
1161+
if (!P->isImplicit()) {
1162+
if (auto *repr = P->getTypeRepr()) {
1163+
if (doIt(repr)) {
1164+
return true;
1165+
}
1166+
}
1167+
}
11641168

11651169
if (auto *E = P->getDefaultValue()) {
11661170
auto res = doIt(E);

lib/AST/Decl.cpp

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,7 +2579,7 @@ void ValueDecl::setOverriddenDecls(ArrayRef<ValueDecl *> overridden) {
25792579
}
25802580

25812581
OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
2582-
TypeLoc returnLoc;
2582+
TypeRepr *returnRepr = nullptr;
25832583
if (auto *VD = dyn_cast<VarDecl>(this)) {
25842584
if (auto *P = VD->getParentPattern()) {
25852585
while (auto *PP = dyn_cast<ParenPattern>(P))
@@ -2591,19 +2591,19 @@ OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
25912591
assert(NP->getDecl() == VD);
25922592
(void) NP;
25932593

2594-
returnLoc = TP->getTypeLoc();
2594+
returnRepr = TP->getTypeLoc().getTypeRepr();
25952595
}
25962596
}
25972597
} else {
2598-
returnLoc = VD->getTypeLoc();
2598+
returnRepr = VD->getTypeRepr();
25992599
}
26002600
} else if (auto *FD = dyn_cast<FuncDecl>(this)) {
2601-
returnLoc = FD->getBodyResultTypeLoc();
2601+
returnRepr = FD->getBodyResultTypeLoc().getTypeRepr();
26022602
} else if (auto *SD = dyn_cast<SubscriptDecl>(this)) {
2603-
returnLoc = SD->getElementTypeLoc();
2603+
returnRepr = SD->getElementTypeLoc().getTypeRepr();
26042604
}
26052605

2606-
return dyn_cast_or_null<OpaqueReturnTypeRepr>(returnLoc.getTypeRepr());
2606+
return dyn_cast_or_null<OpaqueReturnTypeRepr>(returnRepr);
26072607
}
26082608

26092609
OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
@@ -5175,7 +5175,7 @@ SourceRange VarDecl::getSourceRange() const {
51755175
SourceRange VarDecl::getTypeSourceRangeForDiagnostics() const {
51765176
// For a parameter, map back to its parameter to get the TypeLoc.
51775177
if (auto *PD = dyn_cast<ParamDecl>(this)) {
5178-
if (auto typeRepr = PD->getTypeLoc().getTypeRepr())
5178+
if (auto typeRepr = PD->getTypeRepr())
51795179
return typeRepr->getSourceRange();
51805180
}
51815181

@@ -5726,36 +5726,26 @@ ParamDecl::ParamDecl(SourceLoc specifierLoc,
57265726
ArgumentName(argumentName), ParameterNameLoc(parameterNameLoc),
57275727
ArgumentNameLoc(argumentNameLoc), SpecifierLoc(specifierLoc) {
57285728
Bits.ParamDecl.SpecifierComputed = false;
5729-
Bits.ParamDecl.IsTypeLocImplicit = false;
57305729
Bits.ParamDecl.defaultArgumentKind =
57315730
static_cast<unsigned>(DefaultArgumentKind::None);
57325731
}
57335732

5734-
/// Clone constructor, allocates a new ParamDecl identical to the first.
5735-
/// Intentionally not defined as a copy constructor to avoid accidental copies.
5736-
ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
5737-
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getIntroducer(),
5738-
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
5739-
PD->getDeclContext(),
5740-
StorageIsMutable_t(!isImmutableSpecifier(PD->getSpecifier()))),
5741-
ArgumentName(PD->getArgumentName()),
5742-
ArgumentNameLoc(PD->getArgumentNameLoc()),
5743-
SpecifierLoc(PD->getSpecifierLoc()),
5744-
DefaultValueAndFlags(nullptr, PD->DefaultValueAndFlags.getInt()) {
5745-
Bits.ParamDecl.IsTypeLocImplicit = PD->Bits.ParamDecl.IsTypeLocImplicit;
5746-
Bits.ParamDecl.defaultArgumentKind = PD->Bits.ParamDecl.defaultArgumentKind;
5747-
typeLoc = PD->getTypeLoc().clone(PD->getASTContext());
5748-
if (!withTypes && typeLoc.getTypeRepr())
5749-
typeLoc.setType(Type());
5733+
ParamDecl *ParamDecl::cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD) {
5734+
auto *Clone = new (Ctx) ParamDecl(
5735+
PD->getSpecifierLoc(), PD->getArgumentNameLoc(), PD->getArgumentName(),
5736+
PD->getArgumentNameLoc(), PD->getParameterName(), PD->getDeclContext());
5737+
Clone->DefaultValueAndFlags.setPointerAndInt(
5738+
nullptr, PD->DefaultValueAndFlags.getInt());
5739+
Clone->Bits.ParamDecl.defaultArgumentKind =
5740+
PD->Bits.ParamDecl.defaultArgumentKind;
5741+
if (auto *repr = PD->getTypeRepr())
5742+
Clone->setTypeRepr(repr->clone(Ctx));
57505743

5751-
if (withTypes && PD->hasInterfaceType())
5752-
setInterfaceType(PD->getInterfaceType());
5753-
5754-
setSpecifier(PD->getSpecifier());
5755-
setImplicitlyUnwrappedOptional(PD->isImplicitlyUnwrappedOptional());
5744+
Clone->setSpecifier(PD->getSpecifier());
5745+
Clone->setImplicitlyUnwrappedOptional(PD->isImplicitlyUnwrappedOptional());
5746+
return Clone;
57565747
}
57575748

5758-
57595749
/// Retrieve the type of 'self' for the given context.
57605750
Type DeclContext::getSelfTypeInContext() const {
57615751
assert(isTypeContext());
@@ -5794,9 +5784,9 @@ SourceRange ParamDecl::getSourceRange() const {
57945784
startLoc = APINameLoc;
57955785
else if (nameLoc.isValid())
57965786
startLoc = nameLoc;
5797-
else {
5798-
startLoc = getTypeLoc().getSourceRange().Start;
5799-
}
5787+
else if (auto *repr = getTypeRepr())
5788+
startLoc = repr->getStartLoc();
5789+
58005790
if (startLoc.isInvalid())
58015791
return SourceRange();
58025792

@@ -5810,9 +5800,9 @@ SourceRange ParamDecl::getSourceRange() const {
58105800
}
58115801

58125802
// If the typeloc has a valid location, use it to end the range.
5813-
if (auto typeRepr = getTypeLoc().getTypeRepr()) {
5803+
if (auto typeRepr = getTypeRepr()) {
58145804
auto endLoc = typeRepr->getEndLoc();
5815-
if (endLoc.isValid() && !isTypeLocImplicit())
5805+
if (endLoc.isValid())
58165806
return SourceRange(startLoc, endLoc);
58175807
}
58185808

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5116,9 +5116,8 @@ void GenericSignatureBuilder::inferRequirements(
51165116
ModuleDecl &module,
51175117
ParameterList *params) {
51185118
for (auto P : *params) {
5119-
inferRequirements(module, P->getTypeLoc().getType(),
5120-
FloatingRequirementSource::forInferred(
5121-
P->getTypeLoc().getTypeRepr()));
5119+
inferRequirements(module, P->getInterfaceType(),
5120+
FloatingRequirementSource::forInferred(P->getTypeRepr()));
51225121
}
51235122
}
51245123

lib/AST/Parameter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ ParameterList *ParameterList::clone(const ASTContext &C,
5959
SmallVector<ParamDecl*, 8> params(begin(), end());
6060

6161
// Remap the ParamDecls inside of the ParameterList.
62-
bool withTypes = !options.contains(ParameterList::WithoutTypes);
6362
for (auto &decl : params) {
6463
bool hadDefaultArgument =
6564
decl->getDefaultArgumentKind() == DefaultArgumentKind::Normal;
6665

67-
decl = new (C) ParamDecl(decl, withTypes);
66+
decl = ParamDecl::cloneWithoutType(C, decl);
6867
if (options & Implicit)
6968
decl->setImplicit();
7069

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ static std::vector<CharSourceRange> getEnumParamListInfo(SourceManager &SM,
139139
for (ParamDecl *Param: *PL) {
140140
if (Param->isImplicit())
141141
continue;
142-
143-
SourceLoc LabelStart(Param->getTypeLoc().getLoc());
142+
143+
SourceLoc LabelStart;
144+
if (auto *repr = Param->getTypeRepr())
145+
LabelStart = repr->getLoc();
144146
SourceLoc LabelEnd(LabelStart);
145147

146148
if (Param->getNameLoc().isValid()) {

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
7373

7474
for (auto *Param: *Parent->getParameters()) {
7575
if (!--NextIndex) {
76-
return findChild(Param->getTypeLoc());
76+
return findChild(Param->getTypeRepr());
7777
}
7878
}
7979
llvm_unreachable("child index out of bounds");

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,9 +4310,6 @@ static ParamDecl *createSetterAccessorArgument(SourceLoc nameLoc,
43104310
if (isNameImplicit)
43114311
result->setImplicit();
43124312

4313-
// AST Walker shouldn't go into the type recursively.
4314-
result->setIsTypeLocImplicit(true);
4315-
43164313
return result;
43174314
}
43184315

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,8 +2676,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26762676
auto isTupleDestructuring = [](ParamDecl *param) -> bool {
26772677
if (!param->isInvalid())
26782678
return false;
2679-
auto &typeLoc = param->getTypeLoc();
2680-
if (auto typeRepr = typeLoc.getTypeRepr())
2679+
if (auto *typeRepr = param->getTypeRepr())
26812680
return !param->hasName() && isa<TupleTypeRepr>(typeRepr);
26822681
return false;
26832682
};
@@ -2688,7 +2687,6 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26882687
continue;
26892688

26902689
auto argName = "arg" + std::to_string(i);
2691-
auto typeLoc = param->getTypeLoc();
26922690

26932691
SmallString<64> fixIt;
26942692
llvm::raw_svector_ostream OS(fixIt);
@@ -2698,7 +2696,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26982696
OS << '\n' << indent;
26992697

27002698
OS << "let ";
2701-
printTupleNames(typeLoc.getTypeRepr(), OS);
2699+
printTupleNames(param->getTypeRepr(), OS);
27022700
OS << " = " << argName << (isMultiLine ? "\n" + indent : "; ");
27032701

27042702
diagnose(param->getStartLoc(), diag::anon_closure_tuple_param_destructuring)

lib/Parse/ParsePattern.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ mapParsedParameters(Parser &parser,
481481
auto setInvalid = [&]{
482482
if (param->isInvalid())
483483
return;
484-
param->getTypeLoc().setInvalidType(ctx);
485484
param->setInvalid();
486485
};
487486

@@ -512,7 +511,7 @@ mapParsedParameters(Parser &parser,
512511
"__owned",
513512
parsingEnumElt);
514513
}
515-
param->getTypeLoc() = TypeLoc(type);
514+
param->setTypeRepr(type);
516515

517516
// If there is `@autoclosure` attribute associated with the type
518517
// let's mark that in the declaration as well, because it
@@ -629,7 +628,7 @@ mapParsedParameters(Parser &parser,
629628
.fixItRemove(param.EllipsisLoc);
630629

631630
param.EllipsisLoc = SourceLoc();
632-
} else if (!result->getTypeLoc().getTypeRepr()) {
631+
} else if (!result->getTypeRepr()) {
633632
parser.diagnose(param.EllipsisLoc, diag::untyped_pattern_ellipsis)
634633
.highlight(result->getSourceRange());
635634

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,10 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const {
985985
emitDiagnostic(PD->getLoc(), diag::noescape_parameter, PD->getName());
986986

987987
if (!PD->isAutoClosure()) {
988-
note.fixItInsert(PD->getTypeLoc().getSourceRange().Start, "@escaping ");
988+
SourceLoc reprLoc;
989+
if (auto *repr = PD->getTypeRepr())
990+
reprLoc = repr->getStartLoc();
991+
note.fixItInsert(reprLoc, "@escaping ");
989992
} // TODO: add in a fixit for autoclosure
990993

991994
return true;
@@ -4131,9 +4134,8 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() {
41314134
// with parameters, if there are, we'll have to add
41324135
// type information to the replacement argument.
41334136
bool explicitTypes =
4134-
llvm::any_of(params->getArray(), [](const ParamDecl *param) {
4135-
return param->getTypeLoc().getTypeRepr();
4136-
});
4137+
llvm::any_of(params->getArray(),
4138+
[](const ParamDecl *param) { return param->getTypeRepr(); });
41374139

41384140
if (isMultiLineClosure)
41394141
OS << '\n' << indent;

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,8 +2027,8 @@ namespace {
20272027
Type paramType, internalType;
20282028

20292029
// If a type was explicitly specified, use its opened type.
2030-
if (auto type = param->getTypeLoc().getType()) {
2031-
paramType = closureExpr->mapTypeIntoContext(type);
2030+
if (param->getTypeRepr()) {
2031+
paramType = closureExpr->mapTypeIntoContext(param->getInterfaceType());
20322032
// FIXME: Need a better locator for a pattern as a base.
20332033
paramType = CS.openUnboundGenericType(paramType, locator);
20342034
internalType = paramType;

0 commit comments

Comments
 (0)