Skip to content

Commit a532a32

Browse files
committed
AST: Move a few methods from VarDecl down to ParamDecl
1 parent 87199d5 commit a532a32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+457
-431
lines changed

include/swift/AST/Decl.h

Lines changed: 97 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,9 @@ class alignas(1 << DeclAlignInBits) Decl {
364364
IsStatic : 1
365365
);
366366

367-
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 4+1+1+1+1+1,
368-
/// The specifier associated with this variable or parameter. This
369-
/// determines the storage semantics of the value e.g. mutability.
370-
Specifier : 4,
367+
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1,
368+
/// Encodes whether this is a 'let' binding.
369+
Introducer : 1,
371370

372371
/// Whether this declaration was an element of a capture list.
373372
IsCaptureList : 1,
@@ -388,7 +387,11 @@ class alignas(1 << DeclAlignInBits) Decl {
388387
IsPropertyWrapperBackingProperty : 1
389388
);
390389

391-
SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1 + NumDefaultArgumentKindBits,
390+
SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 2+1+NumDefaultArgumentKindBits,
391+
/// The specifier associated with this parameter. This determines
392+
/// the storage semantics of the value e.g. mutability.
393+
Specifier : 2,
394+
392395
/// True if the type is implicitly specified in the source, but this has an
393396
/// apparently valid typeRepr. This is used in accessors, which look like:
394397
/// set (value) {
@@ -4485,16 +4488,6 @@ class AbstractStorageDecl : public ValueDecl {
44854488
Bits.AbstractStorageDecl.IsStatic = IsStatic;
44864489
}
44874490

4488-
void setSupportsMutationIfStillStored(StorageIsMutable_t supportsMutation) {
4489-
if (auto ptr = Accessors.getPointer()) {
4490-
auto impl = ptr->getImplInfo();
4491-
if (!impl.isSimpleStored()) return;
4492-
impl = StorageImplInfo::getSimpleStored(supportsMutation);
4493-
ptr->overwriteImplInfo(impl);
4494-
}
4495-
Bits.AbstractStorageDecl.SupportsMutation = supportsMutation;
4496-
}
4497-
44984491
void computeIsValidKeyPathComponent();
44994492

45004493
OpaqueTypeDecl *OpaqueReturn = nullptr;
@@ -4813,45 +4806,28 @@ enum class PropertyWrapperSynthesizedPropertyKind {
48134806
/// VarDecl - 'var' and 'let' declarations.
48144807
class VarDecl : public AbstractStorageDecl {
48154808
public:
4816-
enum class Specifier : uint8_t {
4817-
// For Var Decls
4818-
4809+
enum class Introducer : uint8_t {
48194810
Let = 0,
4820-
Var = 1,
4821-
4822-
// For Param Decls
4823-
4824-
Default = Let,
4825-
InOut = 2,
4826-
Shared = 3,
4827-
Owned = 4,
4811+
Var = 1
48284812
};
48294813

48304814
protected:
48314815
PointerUnion3<PatternBindingDecl *, Stmt *, VarDecl *> Parent;
48324816

4833-
VarDecl(DeclKind Kind, bool IsStatic, Specifier Sp, bool IsCaptureList,
4834-
SourceLoc NameLoc, Identifier Name, DeclContext *DC)
4835-
: AbstractStorageDecl(Kind, IsStatic, DC, Name, NameLoc,
4836-
StorageIsMutable_t(!isImmutableSpecifier(Sp)))
4837-
{
4838-
Bits.VarDecl.Specifier = static_cast<unsigned>(Sp);
4839-
Bits.VarDecl.IsCaptureList = IsCaptureList;
4840-
Bits.VarDecl.IsDebuggerVar = false;
4841-
Bits.VarDecl.IsLazyStorageProperty = false;
4842-
Bits.VarDecl.HasNonPatternBindingInit = false;
4843-
Bits.VarDecl.IsPropertyWrapperBackingProperty = false;
4844-
}
4817+
VarDecl(DeclKind kind, bool isStatic, Introducer introducer,
4818+
bool issCaptureList, SourceLoc nameLoc, Identifier name,
4819+
DeclContext *dc, StorageIsMutable_t supportsMutation);
48454820

48464821
/// This is the type specified, including location information.
48474822
TypeLoc typeLoc;
48484823

48494824
Type typeInContext;
48504825

48514826
public:
4852-
VarDecl(bool IsStatic, Specifier Sp, bool IsCaptureList, SourceLoc NameLoc,
4853-
Identifier Name, DeclContext *DC)
4854-
: VarDecl(DeclKind::Var, IsStatic, Sp, IsCaptureList, NameLoc, Name, DC) {}
4827+
VarDecl(bool isStatic, Introducer introducer, bool isCaptureList,
4828+
SourceLoc nameLoc, Identifier name, DeclContext *dc)
4829+
: VarDecl(DeclKind::Var, isStatic, introducer, isCaptureList, nameLoc,
4830+
name, dc, StorageIsMutable_t(introducer == Introducer::Var)) {}
48554831

48564832
SourceRange getSourceRange() const;
48574833

@@ -5025,81 +5001,19 @@ class VarDecl : public AbstractStorageDecl {
50255001
VarDecl *getOverriddenDecl() const {
50265002
return cast_or_null<VarDecl>(AbstractStorageDecl::getOverriddenDecl());
50275003
}
5028-
5029-
/// Determine whether this declaration is an anonymous closure parameter.
5030-
bool isAnonClosureParam() const;
5031-
5032-
/// Return the raw specifier value for this property or parameter.
5033-
Specifier getSpecifier() const {
5034-
return static_cast<Specifier>(Bits.VarDecl.Specifier);
5035-
}
5036-
void setSpecifier(Specifier Spec);
5037-
5038-
/// Is the type of this parameter 'inout'?
5039-
///
5040-
/// FIXME(Remove InOut): This is only valid on ParamDecls but multiple parts
5041-
/// of the compiler check ParamDecls and VarDecls along the same paths.
5042-
bool isInOut() const {
5043-
// FIXME: Re-enable this assertion and fix callers.
5044-
// assert((getKind() == DeclKind::Param) && "querying 'inout' on var decl?");
5045-
return getSpecifier() == Specifier::InOut;
5046-
}
50475004

5048-
bool isImmutable() const {
5049-
return isImmutableSpecifier(getSpecifier());
5050-
}
5051-
static bool isImmutableSpecifier(Specifier sp) {
5052-
switch (sp) {
5053-
case Specifier::Let:
5054-
case Specifier::Shared:
5055-
case Specifier::Owned:
5056-
return true;
5057-
case Specifier::Var:
5058-
case Specifier::InOut:
5059-
return false;
5060-
}
5061-
llvm_unreachable("unhandled specifier");
5062-
}
50635005
/// Is this an immutable 'let' property?
5064-
bool isLet() const { return getSpecifier() == Specifier::Let; }
5065-
/// Is this an immutable 'shared' property?
5066-
bool isShared() const { return getSpecifier() == Specifier::Shared; }
5067-
/// Is this an immutable 'owned' property?
5068-
bool isOwned() const { return getSpecifier() == Specifier::Owned; }
5006+
///
5007+
/// If this is a ParamDecl, isLet() is true iff
5008+
/// getSpecifier() == Specifier::Default.
5009+
bool isLet() const { return getIntroducer() == Introducer::Let; }
50695010

5070-
ValueOwnership getValueOwnership() const {
5071-
return getValueOwnershipForSpecifier(getSpecifier());
5011+
Introducer getIntroducer() const {
5012+
return Introducer(Bits.VarDecl.Introducer);
50725013
}
50735014

5074-
static ValueOwnership getValueOwnershipForSpecifier(Specifier specifier) {
5075-
switch (specifier) {
5076-
case Specifier::Let:
5077-
return ValueOwnership::Default;
5078-
case Specifier::Var:
5079-
return ValueOwnership::Default;
5080-
case Specifier::InOut:
5081-
return ValueOwnership::InOut;
5082-
case Specifier::Shared:
5083-
return ValueOwnership::Shared;
5084-
case Specifier::Owned:
5085-
return ValueOwnership::Owned;
5086-
}
5087-
llvm_unreachable("unhandled specifier");
5088-
}
5089-
5090-
static Specifier
5091-
getParameterSpecifierForValueOwnership(ValueOwnership ownership) {
5092-
switch (ownership) {
5093-
case ValueOwnership::Default:
5094-
return Specifier::Let;
5095-
case ValueOwnership::Shared:
5096-
return Specifier::Shared;
5097-
case ValueOwnership::InOut:
5098-
return Specifier::InOut;
5099-
case ValueOwnership::Owned:
5100-
return Specifier::Owned;
5101-
}
5102-
llvm_unreachable("unhandled ownership");
5015+
void setIntroducer(Introducer value) {
5016+
Bits.VarDecl.Introducer = uint8_t(value);
51035017
}
51045018

51055019
/// Is this an element in a capture list?
@@ -5281,7 +5195,14 @@ class ParamDecl : public VarDecl {
52815195
DefaultValueAndFlags;
52825196

52835197
public:
5284-
ParamDecl(VarDecl::Specifier specifier,
5198+
enum class Specifier : uint8_t {
5199+
Default = 0,
5200+
InOut = 1,
5201+
Shared = 2,
5202+
Owned = 3,
5203+
};
5204+
5205+
ParamDecl(Specifier specifier,
52855206
SourceLoc specifierLoc, SourceLoc argumentNameLoc,
52865207
Identifier argumentName, SourceLoc parameterNameLoc,
52875208
Identifier parameterName, DeclContext *dc);
@@ -5400,6 +5321,70 @@ class ParamDecl : public VarDecl {
54005321
return getVarargBaseTy(getInterfaceType());
54015322
}
54025323

5324+
/// Determine whether this declaration is an anonymous closure parameter.
5325+
bool isAnonClosureParam() const;
5326+
5327+
/// Return the raw specifier value for this parameter.
5328+
Specifier getSpecifier() const {
5329+
return static_cast<Specifier>(Bits.ParamDecl.Specifier);
5330+
}
5331+
void setSpecifier(Specifier Spec);
5332+
5333+
/// Is the type of this parameter 'inout'?
5334+
bool isInOut() const { return getSpecifier() == Specifier::InOut; }
5335+
/// Is this an immutable 'shared' property?
5336+
bool isShared() const { return getSpecifier() == Specifier::Shared; }
5337+
/// Is this an immutable 'owned' property?
5338+
bool isOwned() const { return getSpecifier() == Specifier::Owned; }
5339+
5340+
bool isImmutable() const {
5341+
return isImmutableSpecifier(getSpecifier());
5342+
}
5343+
static bool isImmutableSpecifier(Specifier sp) {
5344+
switch (sp) {
5345+
case Specifier::Default:
5346+
case Specifier::Shared:
5347+
case Specifier::Owned:
5348+
return true;
5349+
case Specifier::InOut:
5350+
return false;
5351+
}
5352+
llvm_unreachable("unhandled specifier");
5353+
}
5354+
5355+
ValueOwnership getValueOwnership() const {
5356+
return getValueOwnershipForSpecifier(getSpecifier());
5357+
}
5358+
5359+
static ValueOwnership getValueOwnershipForSpecifier(Specifier specifier) {
5360+
switch (specifier) {
5361+
case Specifier::Default:
5362+
return ValueOwnership::Default;
5363+
case Specifier::InOut:
5364+
return ValueOwnership::InOut;
5365+
case Specifier::Shared:
5366+
return ValueOwnership::Shared;
5367+
case Specifier::Owned:
5368+
return ValueOwnership::Owned;
5369+
}
5370+
llvm_unreachable("unhandled specifier");
5371+
}
5372+
5373+
static Specifier
5374+
getParameterSpecifierForValueOwnership(ValueOwnership ownership) {
5375+
switch (ownership) {
5376+
case ValueOwnership::Default:
5377+
return Specifier::Default;
5378+
case ValueOwnership::Shared:
5379+
return Specifier::Shared;
5380+
case ValueOwnership::InOut:
5381+
return Specifier::InOut;
5382+
case ValueOwnership::Owned:
5383+
return Specifier::Owned;
5384+
}
5385+
llvm_unreachable("unhandled ownership");
5386+
}
5387+
54035388
SourceRange getSourceRange() const;
54045389

54055390
AnyFunctionType::Param toFunctionParam(Type type = Type()) const;

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,16 +4367,15 @@ WARNING(variable_never_used, none,
43674367
"consider replacing with '_' or removing it",
43684368
(Identifier, unsigned))
43694369
WARNING(immutable_value_never_used_but_assigned, none,
4370-
"immutable value %0 was never used; "
4371-
"consider removing it",
4370+
"immutable value %0 was never used; consider removing it",
43724371
(Identifier))
43734372
WARNING(variable_never_mutated, none,
4374-
"%select{variable|parameter}1 %0 was never mutated; "
4375-
"consider %select{removing 'var' to make it|changing to 'let'}2 constant",
4376-
(Identifier, unsigned, bool))
4373+
"variable %0 was never mutated; "
4374+
"consider %select{removing 'var' to make it|changing to 'let'}1 constant",
4375+
(Identifier, bool))
43774376
WARNING(variable_never_read, none,
4378-
"%select{variable|parameter}1 %0 was written to, but never read",
4379-
(Identifier, unsigned))
4377+
"variable %0 was written to, but never read",
4378+
(Identifier))
43804379

43814380
//------------------------------------------------------------------------------
43824381
// MARK: Debug diagnostics

include/swift/Parse/Parser.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ class Parser {
951951
bool parseVersionTuple(llvm::VersionTuple &Version, SourceRange &Range,
952952
const Diagnostic &D);
953953

954-
bool parseTypeAttributeList(VarDecl::Specifier &Specifier,
954+
bool parseTypeAttributeList(ParamDecl::Specifier &Specifier,
955955
SourceLoc &SpecifierLoc,
956956
TypeAttributes &Attributes) {
957957
if (Tok.isAny(tok::at_sign, tok::kw_inout) ||
@@ -961,7 +961,7 @@ class Parser {
961961
return parseTypeAttributeListPresent(Specifier, SpecifierLoc, Attributes);
962962
return false;
963963
}
964-
bool parseTypeAttributeListPresent(VarDecl::Specifier &Specifier,
964+
bool parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier,
965965
SourceLoc &SpecifierLoc,
966966
TypeAttributes &Attributes);
967967
bool parseTypeAttribute(TypeAttributes &Attributes,
@@ -1103,7 +1103,7 @@ class Parser {
11031103
SourceLoc consumeImplicitlyUnwrappedOptionalToken();
11041104

11051105
TypeRepr *applyAttributeToType(TypeRepr *Ty, const TypeAttributes &Attr,
1106-
VarDecl::Specifier Specifier,
1106+
ParamDecl::Specifier Specifier,
11071107
SourceLoc SpecifierLoc);
11081108

11091109
//===--------------------------------------------------------------------===//
@@ -1142,7 +1142,7 @@ class Parser {
11421142
SourceLoc SpecifierLoc;
11431143

11441144
/// The parsed specifier kind, if present.
1145-
VarDecl::Specifier SpecifierKind = VarDecl::Specifier::Default;
1145+
ParamDecl::Specifier SpecifierKind = ParamDecl::Specifier::Default;
11461146

11471147
/// The location of the first name.
11481148
///
@@ -1260,7 +1260,7 @@ class Parser {
12601260

12611261

12621262
Pattern *createBindingFromPattern(SourceLoc loc, Identifier name,
1263-
VarDecl::Specifier specifier);
1263+
VarDecl::Introducer introducer);
12641264

12651265

12661266
/// Determine whether this token can only start a matching pattern

0 commit comments

Comments
 (0)