Skip to content

Push subscript label allocation for KeyPathComponents into the root constructor. #10879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4612,24 +4612,24 @@ class KeyPathExpr : public Expr {
Type ComponentType;
SourceLoc Loc;

explicit Component(DeclNameOrRef decl,
explicit Component(ASTContext *ctxForCopyingLabels,
DeclNameOrRef decl,
Expr *indexExpr,
ArrayRef<Identifier> subscriptLabels,
Kind kind,
Type type,
SourceLoc loc)
: Decl(decl), SubscriptIndexExprAndKind(indexExpr, kind),
SubscriptLabels(subscriptLabels),
ComponentType(type), Loc(loc)
{}
SourceLoc loc);

public:
Component() : Component({}, nullptr, {}, Kind::Invalid, Type(), SourceLoc()) {}
Component()
: Component(nullptr, {}, nullptr, {}, Kind::Invalid, Type(), SourceLoc())
{}

/// Create an unresolved component for a property.
static Component forUnresolvedProperty(DeclName UnresolvedName,
SourceLoc Loc) {
return Component(UnresolvedName, nullptr, {},
return Component(nullptr,
UnresolvedName, nullptr, {},
Kind::UnresolvedProperty,
Type(),
Loc);
Expand All @@ -4648,25 +4648,28 @@ class KeyPathExpr : public Expr {
///
/// You shouldn't add new uses of this overload; use the one that takes a
/// list of index arguments.
static Component forUnresolvedSubscriptWithPrebuiltIndexExpr(Expr *index,
static Component forUnresolvedSubscriptWithPrebuiltIndexExpr(
ASTContext &context,
Expr *index,
ArrayRef<Identifier> subscriptLabels,
SourceLoc loc) {

return Component({}, index, subscriptLabels, Kind::UnresolvedSubscript,
return Component(&context,
{}, index, subscriptLabels, Kind::UnresolvedSubscript,
Type(), loc);
}

/// Create an unresolved optional force `!` component.
static Component forUnresolvedOptionalForce(SourceLoc BangLoc) {
return Component({}, nullptr, {},
return Component(nullptr, {}, nullptr, {},
Kind::OptionalForce,
Type(),
BangLoc);
}

/// Create an unresolved optional chain `?` component.
static Component forUnresolvedOptionalChain(SourceLoc QuestionLoc) {
return Component({}, nullptr, {},
return Component(nullptr, {}, nullptr, {},
Kind::OptionalChain,
Type(),
QuestionLoc);
Expand All @@ -4676,7 +4679,7 @@ class KeyPathExpr : public Expr {
static Component forProperty(ConcreteDeclRef property,
Type propertyType,
SourceLoc loc) {
return Component(property, nullptr, {},
return Component(nullptr, property, nullptr, {},
Kind::Property,
propertyType,
loc);
Expand All @@ -4699,21 +4702,19 @@ class KeyPathExpr : public Expr {
/// list of index arguments.
static Component forSubscriptWithPrebuiltIndexExpr(
ConcreteDeclRef subscript, Expr *index, ArrayRef<Identifier> labels,
Type elementType, SourceLoc loc) {
return Component(subscript, index, {}, Kind::Subscript, elementType, loc);
}
Type elementType, SourceLoc loc);

/// Create an optional-forcing `!` component.
static Component forOptionalForce(Type forcedType, SourceLoc bangLoc) {
return Component({}, nullptr, {},
return Component(nullptr, {}, nullptr, {},
Kind::OptionalForce, forcedType,
bangLoc);
}

/// Create an optional-chaining `?` component.
static Component forOptionalChain(Type unwrappedType,
SourceLoc questionLoc) {
return Component({}, nullptr, {},
return Component(nullptr, {}, nullptr, {},
Kind::OptionalChain, unwrappedType,
questionLoc);
}
Expand All @@ -4722,7 +4723,7 @@ class KeyPathExpr : public Expr {
/// syntax but may appear when the non-optional result of an optional chain
/// is implicitly wrapped.
static Component forOptionalWrap(Type wrappedType) {
return Component({}, nullptr, {},
return Component(nullptr, {}, nullptr, {},
Kind::OptionalWrap, wrappedType,
SourceLoc());
}
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
component.getLoc())
: KeyPathExpr::Component
::forUnresolvedSubscriptWithPrebuiltIndexExpr(
E->getType()->getASTContext(),
newIndex, component.getSubscriptLabels(), component.getLoc());
}
break;
Expand Down
28 changes: 25 additions & 3 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ KeyPathExpr::Component::forSubscript(ASTContext &ctx,
indexArgLabelsScratch,
indexArgLabelLocsScratch);
return forSubscriptWithPrebuiltIndexExpr(subscript, index,
ctx.AllocateCopy(indexArgLabels),
indexArgLabels,
elementType,
lSquareLoc);
}
Expand All @@ -2153,7 +2153,29 @@ KeyPathExpr::Component::forUnresolvedSubscript(ASTContext &ctx,
trailingClosure, /*implicit*/ false,
indexArgLabelsScratch,
indexArgLabelLocsScratch);
return forUnresolvedSubscriptWithPrebuiltIndexExpr(index,
ctx.AllocateCopy(indexArgLabels),
return forUnresolvedSubscriptWithPrebuiltIndexExpr(ctx, index,
indexArgLabels,
lSquareLoc);
}

KeyPathExpr::Component::Component(ASTContext *ctxForCopyingLabels,
DeclNameOrRef decl,
Expr *indexExpr,
ArrayRef<Identifier> subscriptLabels,
Kind kind,
Type type,
SourceLoc loc)
: Decl(decl), SubscriptIndexExprAndKind(indexExpr, kind),
SubscriptLabels(subscriptLabels.empty()
? subscriptLabels
: ctxForCopyingLabels->AllocateCopy(subscriptLabels)),
ComponentType(type), Loc(loc)
{}

KeyPathExpr::Component
KeyPathExpr::Component::forSubscriptWithPrebuiltIndexExpr(
ConcreteDeclRef subscript, Expr *index, ArrayRef<Identifier> labels,
Type elementType, SourceLoc loc) {
return Component(&elementType->getASTContext(),
subscript, index, {}, Kind::Subscript, elementType, loc);
}
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,7 @@ void PreCheckExpression::resolveKeyPathExpr(KeyPathExpr *KPE) {
// .[0] or just plain [0]
components.push_back(
KeyPathExpr::Component::forUnresolvedSubscriptWithPrebuiltIndexExpr(
TC.Context,
SE->getIndex(), SE->getArgumentLabels(), SE->getLoc()));

expr = SE->getBase();
Expand Down