Skip to content

Commit 7055988

Browse files
committed
[AST] Simplify KeyPath::Component construction slightly
Introduce private constructors for specific kinds of components, leaving the subscript constructor to deal with only subscripts.
1 parent 72d4d9f commit 7055988

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed

include/swift/AST/Expr.h

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5609,7 +5609,8 @@ class KeyPathExpr : public Expr {
56095609
Kind KindValue;
56105610
Type ComponentType;
56115611
SourceLoc Loc;
5612-
5612+
5613+
// Private constructor for subscript component.
56135614
explicit Component(ASTContext *ctxForCopyingLabels,
56145615
DeclNameOrRef decl,
56155616
Expr *indexExpr,
@@ -5619,27 +5620,31 @@ class KeyPathExpr : public Expr {
56195620
Type type,
56205621
SourceLoc loc);
56215622

5622-
// Private constructor for tuple element kind
5623-
Component(unsigned tupleIndex, Type elementType, SourceLoc loc)
5624-
: Component(nullptr, {}, nullptr, {}, {}, Kind::TupleElement,
5625-
elementType, loc) {
5623+
// Private constructor for property or #keyPath dictionary key.
5624+
explicit Component(DeclNameOrRef decl, Kind kind, Type type, SourceLoc loc)
5625+
: Component(kind, type, loc) {
5626+
assert(kind == Kind::Property || kind == Kind::UnresolvedProperty ||
5627+
kind == Kind::DictionaryKey);
5628+
Decl = decl;
5629+
}
5630+
5631+
// Private constructor for tuple element kind.
5632+
explicit Component(unsigned tupleIndex, Type elementType, SourceLoc loc)
5633+
: Component(Kind::TupleElement, elementType, loc) {
56265634
TupleIndex = tupleIndex;
56275635
}
5628-
5636+
5637+
// Private constructor for basic components with no additional information.
5638+
explicit Component(Kind kind, Type type, SourceLoc loc)
5639+
: Decl(), KindValue(kind), ComponentType(type), Loc(loc) {}
5640+
56295641
public:
5630-
Component()
5631-
: Component(nullptr, {}, nullptr, {}, {}, Kind::Invalid,
5632-
Type(), SourceLoc())
5633-
{}
5634-
5642+
Component() : Component(Kind::Invalid, Type(), SourceLoc()) {}
5643+
56355644
/// Create an unresolved component for a property.
56365645
static Component forUnresolvedProperty(DeclNameRef UnresolvedName,
56375646
SourceLoc Loc) {
5638-
return Component(nullptr,
5639-
UnresolvedName, nullptr, {}, {},
5640-
Kind::UnresolvedProperty,
5641-
Type(),
5642-
Loc);
5647+
return Component(UnresolvedName, Kind::UnresolvedProperty, Type(), Loc);
56435648
}
56445649

56455650
/// Create an unresolved component for a subscript.
@@ -5669,38 +5674,26 @@ class KeyPathExpr : public Expr {
56695674

56705675
/// Create an unresolved optional force `!` component.
56715676
static Component forUnresolvedOptionalForce(SourceLoc BangLoc) {
5672-
return Component(nullptr, {}, nullptr, {}, {},
5673-
Kind::OptionalForce,
5674-
Type(),
5675-
BangLoc);
5677+
return Component(Kind::OptionalForce, Type(), BangLoc);
56765678
}
56775679

56785680
/// Create an unresolved optional chain `?` component.
56795681
static Component forUnresolvedOptionalChain(SourceLoc QuestionLoc) {
5680-
return Component(nullptr, {}, nullptr, {}, {},
5681-
Kind::OptionalChain,
5682-
Type(),
5683-
QuestionLoc);
5682+
return Component(Kind::OptionalChain, Type(), QuestionLoc);
56845683
}
56855684

56865685
/// Create a component for a property.
56875686
static Component forProperty(ConcreteDeclRef property,
56885687
Type propertyType,
56895688
SourceLoc loc) {
5690-
return Component(nullptr, property, nullptr, {}, {},
5691-
Kind::Property,
5692-
propertyType,
5693-
loc);
5689+
return Component(property, Kind::Property, propertyType, loc);
56945690
}
56955691

56965692
/// Create a component for a dictionary key (#keyPath only).
56975693
static Component forDictionaryKey(DeclNameRef UnresolvedName,
56985694
Type valueType,
56995695
SourceLoc loc) {
5700-
return Component(nullptr, UnresolvedName, nullptr, {}, {},
5701-
Kind::DictionaryKey,
5702-
valueType,
5703-
loc);
5696+
return Component(UnresolvedName, Kind::DictionaryKey, valueType, loc);
57045697
}
57055698

57065699
/// Create a component for a subscript.
@@ -5726,32 +5719,24 @@ class KeyPathExpr : public Expr {
57265719

57275720
/// Create an optional-forcing `!` component.
57285721
static Component forOptionalForce(Type forcedType, SourceLoc bangLoc) {
5729-
return Component(nullptr, {}, nullptr, {}, {},
5730-
Kind::OptionalForce, forcedType,
5731-
bangLoc);
5722+
return Component(Kind::OptionalForce, forcedType, bangLoc);
57325723
}
57335724

57345725
/// Create an optional-chaining `?` component.
57355726
static Component forOptionalChain(Type unwrappedType,
57365727
SourceLoc questionLoc) {
5737-
return Component(nullptr, {}, nullptr, {}, {},
5738-
Kind::OptionalChain, unwrappedType,
5739-
questionLoc);
5728+
return Component(Kind::OptionalChain, unwrappedType, questionLoc);
57405729
}
57415730

57425731
/// Create an optional-wrapping component. This doesn't have a surface
57435732
/// syntax but may appear when the non-optional result of an optional chain
57445733
/// is implicitly wrapped.
57455734
static Component forOptionalWrap(Type wrappedType) {
5746-
return Component(nullptr, {}, nullptr, {}, {},
5747-
Kind::OptionalWrap, wrappedType,
5748-
SourceLoc());
5735+
return Component(Kind::OptionalWrap, wrappedType, SourceLoc());
57495736
}
57505737

57515738
static Component forIdentity(SourceLoc selfLoc) {
5752-
return Component(nullptr, {}, nullptr, {}, {},
5753-
Kind::Identity, Type(),
5754-
selfLoc);
5739+
return Component(Kind::Identity, Type(), selfLoc);
57555740
}
57565741

57575742
static Component forTupleElement(unsigned fieldNumber,
@@ -5761,8 +5746,7 @@ class KeyPathExpr : public Expr {
57615746
}
57625747

57635748
static Component forCodeCompletion(SourceLoc Loc) {
5764-
return Component(nullptr, {}, nullptr, {}, {}, Kind::CodeCompletion,
5765-
Type(), Loc);
5749+
return Component(Kind::CodeCompletion, Type(), Loc);
57665750
}
57675751

57685752
SourceLoc getLoc() const {

lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ KeyPathExpr::Component::Component(ASTContext *ctxForCopyingLabels,
24022402
: Decl(decl), SubscriptIndexExpr(indexExpr), KindValue(kind),
24032403
ComponentType(type), Loc(loc)
24042404
{
2405-
assert(kind != Kind::TupleElement || subscriptLabels.empty());
2405+
assert(kind == Kind::Subscript || kind == Kind::UnresolvedSubscript);
24062406
assert(subscriptLabels.size() == indexHashables.size()
24072407
|| indexHashables.empty());
24082408
SubscriptLabelsData = subscriptLabels.data();

0 commit comments

Comments
 (0)