Skip to content

Commit 4b258e8

Browse files
committed
AST: Stop setting contextual types on ParamDecls
VarDecl::getType() lazily maps the interface type into context if needed.
1 parent f11b620 commit 4b258e8

28 files changed

+99
-174
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,7 +4463,7 @@ class VarDecl : public AbstractStorageDecl {
44634463
llvm::PointerUnion<PatternBindingDecl*, Stmt*> ParentPattern;
44644464

44654465
VarDecl(DeclKind Kind, bool IsStatic, Specifier Sp, bool IsCaptureList,
4466-
SourceLoc NameLoc, Identifier Name, Type Ty, DeclContext *DC)
4466+
SourceLoc NameLoc, Identifier Name, DeclContext *DC)
44674467
: AbstractStorageDecl(Kind, DC, Name, NameLoc, !isImmutableSpecifier(Sp))
44684468
{
44694469
Bits.VarDecl.IsStatic = IsStatic;
@@ -4472,7 +4472,6 @@ class VarDecl : public AbstractStorageDecl {
44724472
Bits.VarDecl.IsDebuggerVar = false;
44734473
Bits.VarDecl.IsREPLVar = false;
44744474
Bits.VarDecl.HasNonPatternBindingInit = false;
4475-
setType(Ty);
44764475
}
44774476

44784477
/// This is the type specified, including location information.
@@ -4482,9 +4481,8 @@ class VarDecl : public AbstractStorageDecl {
44824481

44834482
public:
44844483
VarDecl(bool IsStatic, Specifier Sp, bool IsCaptureList, SourceLoc NameLoc,
4485-
Identifier Name, Type Ty, DeclContext *DC)
4486-
: VarDecl(DeclKind::Var, IsStatic, Sp, IsCaptureList, NameLoc, Name, Ty,
4487-
DC) {}
4484+
Identifier Name, DeclContext *DC)
4485+
: VarDecl(DeclKind::Var, IsStatic, Sp, IsCaptureList, NameLoc, Name, DC) {}
44884486

44894487
SourceRange getSourceRange() const;
44904488

@@ -4712,7 +4710,7 @@ class ParamDecl : public VarDecl {
47124710
ParamDecl(VarDecl::Specifier specifier,
47134711
SourceLoc specifierLoc, SourceLoc argumentNameLoc,
47144712
Identifier argumentName, SourceLoc parameterNameLoc,
4715-
Identifier parameterName, Type ty, DeclContext *dc);
4713+
Identifier parameterName, DeclContext *dc);
47164714

47174715
/// Clone constructor, allocates a new ParamDecl identical to the first.
47184716
/// Intentionally not defined as a typical copy constructor to avoid

lib/AST/Builtins.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
155155
for (Type argType : argTypes) {
156156
auto PD = new (Context)
157157
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
158-
Identifier(), SourceLoc(), Identifier(), argType, DC);
158+
Identifier(), SourceLoc(), Identifier(), DC);
159159
PD->setInterfaceType(argType);
160160
PD->setValidationToChecked();
161161
PD->setImplicit();
@@ -203,8 +203,7 @@ getBuiltinGenericFunction(Identifier Id,
203203
auto PD = new (Context) ParamDecl(specifier,
204204
SourceLoc(), SourceLoc(),
205205
Identifier(), SourceLoc(),
206-
Identifier(),
207-
Type(), DC);
206+
Identifier(), DC);
208207
PD->setInterfaceType(paramIfaceType);
209208
PD->setValidationToChecked();
210209
PD->setImplicit();

lib/AST/Decl.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4696,9 +4696,9 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
46964696
ParamDecl::ParamDecl(Specifier specifier,
46974697
SourceLoc specifierLoc, SourceLoc argumentNameLoc,
46984698
Identifier argumentName, SourceLoc parameterNameLoc,
4699-
Identifier parameterName, Type ty, DeclContext *dc)
4699+
Identifier parameterName, DeclContext *dc)
47004700
: VarDecl(DeclKind::Param, /*IsStatic*/false, specifier,
4701-
/*IsCaptureList*/false, parameterNameLoc, parameterName, ty, dc),
4701+
/*IsCaptureList*/false, parameterNameLoc, parameterName, dc),
47024702
ArgumentName(argumentName), ArgumentNameLoc(argumentNameLoc),
47034703
SpecifierLoc(specifierLoc) {
47044704

@@ -4714,9 +4714,6 @@ ParamDecl::ParamDecl(Specifier specifier,
47144714
ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
47154715
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getSpecifier(),
47164716
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
4717-
PD->hasType() && withTypes
4718-
? PD->getType()
4719-
: Type(),
47204717
PD->getDeclContext()),
47214718
ArgumentName(PD->getArgumentName()),
47224719
ArgumentNameLoc(PD->getArgumentNameLoc()),
@@ -4759,7 +4756,6 @@ Type DeclContext::getSelfInterfaceType() const {
47594756
}
47604757

47614758
/// Create an implicit 'self' decl for a method in the specified decl context.
4762-
/// If 'static' is true, then this is self for a static method in the type.
47634759
///
47644760
/// Note that this decl is created, but it is returned with an incorrect
47654761
/// DeclContext that needs to be set correctly. This is automatically handled
@@ -4771,7 +4767,7 @@ ParamDecl *ParamDecl::createUnboundSelf(SourceLoc loc, DeclContext *DC) {
47714767
ASTContext &C = DC->getASTContext();
47724768
auto *selfDecl =
47734769
new (C) ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
4774-
Identifier(), loc, C.Id_self, Type(), DC);
4770+
Identifier(), loc, C.Id_self, DC);
47754771
selfDecl->setImplicit();
47764772
return selfDecl;
47774773
}
@@ -4801,7 +4797,7 @@ ParamDecl *ParamDecl::createSelf(SourceLoc loc, DeclContext *DC,
48014797
}
48024798

48034799
auto *selfDecl = new (C) ParamDecl(specifier, SourceLoc(),SourceLoc(),
4804-
Identifier(), loc, C.Id_self, Type(), DC);
4800+
Identifier(), loc, C.Id_self, DC);
48054801
selfDecl->setImplicit();
48064802
selfDecl->setInterfaceType(selfInterfaceType);
48074803
selfDecl->setValidationToChecked();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ createVarWithPattern(ASTContext &ctx, DeclContext *dc, Identifier name, Type ty,
136136
/*IsStatic*/false,
137137
specifier,
138138
/*IsCaptureList*/false,
139-
SourceLoc(), name, dc->mapTypeIntoContext(ty), dc);
139+
SourceLoc(), name, dc);
140140
if (isImplicit)
141141
var->setImplicit();
142142
var->setInterfaceType(ty);
@@ -426,7 +426,6 @@ makeEnumRawValueConstructor(ClangImporter::Implementation &Impl,
426426
auto param = new (C) ParamDecl(VarDecl::Specifier::Default, SourceLoc(),
427427
SourceLoc(), C.Id_rawValue,
428428
SourceLoc(), C.Id_rawValue,
429-
rawTy,
430429
enumDecl);
431430
param->setInterfaceType(rawTy);
432431
param->setValidationToChecked();
@@ -689,7 +688,6 @@ static AccessorDecl *makeFieldSetterDecl(ClangImporter::Implementation &Impl,
689688
auto newValueDecl = new (C) ParamDecl(VarDecl::Specifier::Default,
690689
SourceLoc(), SourceLoc(),
691690
Identifier(), SourceLoc(), C.Id_value,
692-
importedFieldDecl->getType(),
693691
importedDecl);
694692
newValueDecl->setInterfaceType(importedFieldDecl->getInterfaceType());
695693

@@ -1237,7 +1235,7 @@ createValueConstructor(ClangImporter::Implementation &Impl,
12371235
Identifier argName = generateParamName ? var->getName() : Identifier();
12381236
auto param = new (context)
12391237
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(), argName,
1240-
SourceLoc(), var->getName(), var->getType(), structDecl);
1238+
SourceLoc(), var->getName(), structDecl);
12411239
param->setInterfaceType(var->getInterfaceType());
12421240
param->setValidationToChecked();
12431241
Impl.recordImplicitUnwrapForDecl(
@@ -1516,7 +1514,7 @@ static void makeStructRawValuedWithBridge(
15161514
// Create a computed value variable.
15171515
auto computedVar = new (ctx) VarDecl(
15181516
/*IsStatic*/false, VarDecl::Specifier::Var, /*IsCaptureList*/false,
1519-
SourceLoc(), computedVarName, bridgedType, structDecl);
1517+
SourceLoc(), computedVarName, structDecl);
15201518
computedVar->setInterfaceType(bridgedType);
15211519
computedVar->setImplicit();
15221520
computedVar->setAccess(AccessLevel::Public);
@@ -1623,12 +1621,10 @@ buildSubscriptSetterDecl(ClangImporter::Implementation &Impl,
16231621

16241622
// 'self'
16251623
auto selfDecl = ParamDecl::createSelf(SourceLoc(), dc);
1626-
auto elementTy = dc->mapTypeIntoContext(elementInterfaceTy);
16271624

16281625
auto paramVarDecl =
16291626
new (C) ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
1630-
Identifier(), loc, valueIndex->get(0)->getName(),
1631-
elementTy, dc);
1627+
Identifier(), loc, valueIndex->get(0)->getName(), dc);
16321628
paramVarDecl->setInterfaceType(elementInterfaceTy);
16331629
paramVarDecl->setValidationToChecked();
16341630

@@ -1798,7 +1794,7 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,
17981794
// Make the property decl
17991795
auto errorDomainPropertyDecl = new (C) VarDecl(
18001796
/*IsStatic*/isStatic, VarDecl::Specifier::Var, /*IsCaptureList*/false,
1801-
SourceLoc(), C.Id_errorDomain, stringTy, swiftDecl);
1797+
SourceLoc(), C.Id_errorDomain, swiftDecl);
18021798
errorDomainPropertyDecl->setInterfaceType(stringTy);
18031799
errorDomainPropertyDecl->setValidationToChecked();
18041800
errorDomainPropertyDecl->setAccess(AccessLevel::Public);
@@ -2660,7 +2656,7 @@ namespace {
26602656
auto nsErrorProp = new (C) VarDecl(/*IsStatic*/false,
26612657
VarDecl::Specifier::Let,
26622658
/*IsCaptureList*/false,
2663-
loc, C.Id_nsError, nsErrorType,
2659+
loc, C.Id_nsError,
26642660
errorWrapper);
26652661
nsErrorProp->setImplicit();
26662662
nsErrorProp->setAccess(AccessLevel::Public);
@@ -2741,7 +2737,7 @@ namespace {
27412737
auto rawValue = new (C) VarDecl(/*IsStatic*/false,
27422738
VarDecl::Specifier::Var,
27432739
/*IsCaptureList*/false,
2744-
SourceLoc(), varName, underlyingType,
2740+
SourceLoc(), varName,
27452741
enumDecl);
27462742
rawValue->setImplicit();
27472743
rawValue->setAccess(AccessLevel::Public);
@@ -3386,7 +3382,7 @@ namespace {
33863382
VarDecl::Specifier::Var,
33873383
/*IsCaptureList*/false,
33883384
Impl.importSourceLoc(decl->getLocStart()),
3389-
name, dc->mapTypeIntoContext(type), dc);
3385+
name, dc);
33903386
result->setInterfaceType(type);
33913387
result->setIsObjC(false);
33923388
result->setIsDynamic(false);
@@ -3619,7 +3615,7 @@ namespace {
36193615
VarDecl::Specifier::Var,
36203616
/*IsCaptureList*/false,
36213617
Impl.importSourceLoc(decl->getLocation()),
3622-
name, dc->mapTypeIntoContext(type), dc);
3618+
name, dc);
36233619
result->setIsObjC(false);
36243620
result->setIsDynamic(false);
36253621
result->setInterfaceType(type);
@@ -3706,7 +3702,7 @@ namespace {
37063702
specifier,
37073703
/*IsCaptureList*/false,
37083704
Impl.importSourceLoc(decl->getLocation()),
3709-
name, dc->mapTypeIntoContext(type), dc);
3705+
name, dc);
37103706
result->setIsObjC(false);
37113707
result->setIsDynamic(false);
37123708
result->setInterfaceType(type);
@@ -4831,7 +4827,7 @@ namespace {
48314827
getOverridableAccessLevel(dc),
48324828
/*IsStatic*/decl->isClassProperty(), VarDecl::Specifier::Var,
48334829
/*IsCaptureList*/false, Impl.importSourceLoc(decl->getLocation()),
4834-
name, dc->mapTypeIntoContext(type), dc);
4830+
name, dc);
48354831
result->setInterfaceType(type);
48364832
Impl.recordImplicitUnwrapForDecl(result,
48374833
importedType.isImplicitlyUnwrapped());
@@ -5452,7 +5448,7 @@ SwiftDeclConverter::importOptionConstant(const clang::EnumConstantDecl *decl,
54525448
if (isa<EnumDecl>(theStruct))
54535449
convertKind = ConstantConvertKind::ConstructionWithUnwrap;
54545450
Decl *CD = Impl.createConstant(
5455-
name, theStruct, theStruct->getDeclaredTypeInContext(),
5451+
name, theStruct, theStruct->getDeclaredInterfaceType(),
54565452
clang::APValue(decl->getInitVal()), convertKind, /*isStatic*/ true, decl);
54575453
Impl.importAttributes(decl, CD);
54585454

@@ -5491,7 +5487,7 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
54915487
auto constantRef =
54925488
new (Impl.SwiftContext) DeclRefExpr(original, DeclNameLoc(),
54935489
/*implicit*/ true);
5494-
Type importedEnumTy = importedEnum->getDeclaredTypeInContext();
5490+
Type importedEnumTy = importedEnum->getDeclaredInterfaceType();
54955491
auto typeRef = TypeExpr::createImplicit(importedEnumTy, Impl.SwiftContext);
54965492
auto instantiate = new (Impl.SwiftContext)
54975493
DotSyntaxCallExpr(constantRef, SourceLoc(), typeRef);
@@ -5563,8 +5559,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
55635559
auto *paramDecl =
55645560
new (Impl.SwiftContext) ParamDecl(
55655561
VarDecl::Specifier::Default, SourceLoc(), SourceLoc(), argNames.front(),
5566-
SourceLoc(), argNames.front(), Impl.SwiftContext.TheEmptyTupleType,
5567-
dc);
5562+
SourceLoc(), argNames.front(), dc);
55685563
paramDecl->setInterfaceType(Impl.SwiftContext.TheEmptyTupleType);
55695564
paramDecl->setValidationToChecked();
55705565

@@ -5847,7 +5842,7 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
58475842
auto property = Impl.createDeclWithClangNode<VarDecl>(
58485843
getter, AccessLevel::Public, /*IsStatic*/isStatic,
58495844
VarDecl::Specifier::Var, /*IsCaptureList*/false, SourceLoc(),
5850-
propertyName, dc->mapTypeIntoContext(swiftPropertyType), dc);
5845+
propertyName, dc);
58515846
property->setInterfaceType(swiftPropertyType);
58525847
property->setIsObjC(false);
58535848
property->setIsDynamic(false);
@@ -8190,11 +8185,11 @@ ClangImporter::Implementation::createConstant(Identifier name, DeclContext *dc,
81908185
var = createDeclWithClangNode<VarDecl>(ClangN, AccessLevel::Public,
81918186
/*IsStatic*/isStatic, VarDecl::Specifier::Var,
81928187
/*IsCaptureList*/false, SourceLoc(),
8193-
name, dc->mapTypeIntoContext(type), dc);
8188+
name, dc);
81948189
} else {
81958190
var = new (SwiftContext)
81968191
VarDecl(/*IsStatic*/isStatic, VarDecl::Specifier::Var, /*IsCaptureList*/false,
8197-
SourceLoc(), name, dc->mapTypeIntoContext(type), dc);
8192+
SourceLoc(), name, dc);
81988193
var->setValidationToChecked();
81998194
}
82008195

@@ -8304,7 +8299,7 @@ createUnavailableDecl(Identifier name, DeclContext *dc, Type type,
83048299
/*IsStatic*/isStatic,
83058300
VarDecl::Specifier::Var,
83068301
/*IsCaptureList*/false,
8307-
SourceLoc(), name, type, dc);
8302+
SourceLoc(), name, dc);
83088303
var->setIsObjC(false);
83098304
var->setIsDynamic(false);
83108305
var->setInterfaceType(type);

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,6 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
16971697
param, AccessLevel::Private,
16981698
VarDecl::Specifier::Default, SourceLoc(), SourceLoc(), name,
16991699
importSourceLoc(param->getLocation()), bodyName,
1700-
dc->mapTypeIntoContext(swiftParamTy),
17011700
ImportedHeaderUnit);
17021701

17031702
paramInfo->setInterfaceType(swiftParamTy);
@@ -1716,7 +1715,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
17161715
auto param = new (SwiftContext) ParamDecl(VarDecl::Specifier::Default,
17171716
SourceLoc(), SourceLoc(),
17181717
Identifier(), SourceLoc(),
1719-
name, paramTy,
1718+
name,
17201719
ImportedHeaderUnit);
17211720
param->setInterfaceType(paramTy);
17221721

@@ -2018,7 +2017,7 @@ ImportedType ClangImporter::Implementation::importMethodType(
20182017
auto type = TupleType::getEmpty(SwiftContext);
20192018
auto var = new (SwiftContext) ParamDecl(VarDecl::Specifier::Default, SourceLoc(),
20202019
SourceLoc(), argName,
2021-
SourceLoc(), argName, type,
2020+
SourceLoc(), argName,
20222021
ImportedHeaderUnit);
20232022
var->setInterfaceType(type);
20242023
swiftParams.push_back(var);
@@ -2143,7 +2142,6 @@ ImportedType ClangImporter::Implementation::importMethodType(
21432142
SourceLoc(), SourceLoc(), name,
21442143
importSourceLoc(param->getLocation()),
21452144
bodyName,
2146-
swiftParamTy,
21472145
ImportedHeaderUnit);
21482146
paramInfo->setInterfaceType(swiftParamTy->mapTypeOutOfContext());
21492147
recordImplicitUnwrapForDecl(paramInfo, paramIsIUO);
@@ -2263,7 +2261,6 @@ ImportedType ClangImporter::Implementation::importAccessorMethodType(
22632261
/*let loc*/SourceLoc(),
22642262
/*label loc*/SourceLoc(),
22652263
argLabel, nameLoc, bodyName,
2266-
propertyTy,
22672264
/*dummy DC*/ImportedHeaderUnit);
22682265
paramInfo->setInterfaceType(propertyTy->mapTypeOutOfContext());
22692266

lib/IRGen/LoadableByAddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ void LoadableStorageAllocation::insertIndirectReturnArgs() {
13041304
VarDecl::Specifier::InOut, SourceLoc(), SourceLoc(),
13051305
ctx.getIdentifier("$return_value"), SourceLoc(),
13061306
ctx.getIdentifier("$return_value"),
1307-
resultStorageType.getASTType(), pass.F->getDeclContext());
1307+
pass.F->getDeclContext());
13081308
pass.F->begin()->insertFunctionArgument(0, resultStorageType.getAddressType(),
13091309
ValueOwnershipKind::Trivial, var);
13101310
}

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,7 +3740,6 @@ static AccessorDecl *createAccessorFunc(SourceLoc DeclLoc,
37403740
storageParam->getArgumentName(),
37413741
storageParam->getNameLoc(),
37423742
storageParam->getName(),
3743-
Type(),
37443743
P->CurDeclContext);
37453744
accessorParam->setVariadic(storageParam->isVariadic());
37463745

@@ -3825,7 +3824,7 @@ static ParamDecl *createSetterAccessorArgument(SourceLoc nameLoc,
38253824

38263825
auto result = new (P.Context)
38273826
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
3828-
Identifier(), nameLoc, name, Type(), P.CurDeclContext);
3827+
Identifier(), nameLoc, name, P.CurDeclContext);
38293828
if (isNameImplicit)
38303829
result->setImplicit();
38313830

@@ -4457,7 +4456,7 @@ VarDecl *Parser::parseDeclVarGetSet(Pattern *pattern,
44574456
storage = new (Context) VarDecl(StaticLoc.isValid(),
44584457
VarDecl::Specifier::Var,
44594458
/*is capture list*/ false,
4460-
VarLoc, Identifier(), Type(),
4459+
VarLoc, Identifier(),
44614460
CurDeclContext);
44624461
storage->setImplicit(true);
44634462
storage->setInvalid(true);

lib/Parse/ParseExpr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
24702470
auto *VD = new (Context) VarDecl(/*isStatic*/false,
24712471
specifierKind,
24722472
/*isCaptureList*/true,
2473-
nameLoc, name, Type(), CurDeclContext);
2473+
nameLoc, name, CurDeclContext);
24742474

24752475
// Attributes.
24762476
if (ownershipKind != ReferenceOwnership::Strong)
@@ -2524,7 +2524,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
25242524
Context.getIdentifier(Tok.getText()) : Identifier();
25252525
auto var = new (Context)
25262526
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
2527-
Identifier(), Tok.getLoc(), name, Type(), nullptr);
2527+
Identifier(), Tok.getLoc(), name, nullptr);
25282528
elements.push_back(var);
25292529
consumeToken();
25302530

@@ -2826,7 +2826,7 @@ Expr *Parser::parseExprAnonClosureArg() {
28262826
SourceLoc varLoc = leftBraceLoc;
28272827
auto *var = new (Context)
28282828
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
2829-
Identifier(), varLoc, ident, Type(), closure);
2829+
Identifier(), varLoc, ident, closure);
28302830
var->setImplicit();
28312831
decls.push_back(var);
28322832
}

0 commit comments

Comments
 (0)