Skip to content

Commit fd3f03f

Browse files
committed
Remove UnresolvedConstructorExpr.
UnresolvedConstructorExpr is not providing any value here; it's essentially just UnresolvedDotExpr where the name refers to an initializer, so use that instead. NFC
1 parent c9c1d13 commit fd3f03f

15 files changed

+176
-255
lines changed

include/swift/AST/Expr.h

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,40 +1045,6 @@ class OtherConstructorDeclRefExpr : public Expr {
10451045
}
10461046
};
10471047

1048-
/// An unresolved reference to a constructor member of a value. Resolves to a
1049-
/// DotSyntaxCall involving the value and the resolved constructor.
1050-
class UnresolvedConstructorExpr : public Expr {
1051-
Expr *SubExpr;
1052-
SourceLoc DotLoc;
1053-
SourceLoc ConstructorLoc;
1054-
DeclName Name;
1055-
1056-
public:
1057-
UnresolvedConstructorExpr(Expr *SubExpr, SourceLoc DotLoc,
1058-
SourceLoc ConstructorLoc, DeclName Name,
1059-
bool Implicit)
1060-
: Expr(ExprKind::UnresolvedConstructor, Implicit),
1061-
SubExpr(SubExpr), DotLoc(DotLoc), ConstructorLoc(ConstructorLoc),
1062-
Name(Name)
1063-
{}
1064-
1065-
Expr *getSubExpr() const { return SubExpr; }
1066-
void setSubExpr(Expr *e) { SubExpr = e; }
1067-
1068-
SourceLoc getLoc() const { return ConstructorLoc; }
1069-
SourceLoc getConstructorLoc() const { return ConstructorLoc; }
1070-
SourceLoc getDotLoc() const { return DotLoc; }
1071-
1072-
DeclName getName() const { return Name; }
1073-
1074-
SourceLoc getStartLoc() const { return SubExpr->getStartLoc(); }
1075-
SourceLoc getEndLoc() const { return ConstructorLoc; }
1076-
1077-
static bool classof(const Expr *E) {
1078-
return E->getKind() == ExprKind::UnresolvedConstructor;
1079-
}
1080-
};
1081-
10821048
/// OverloadSetRefExpr - A reference to an overloaded set of values with a
10831049
/// single name.
10841050
///

include/swift/AST/ExprNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ EXPR(DeclRef, Expr)
6464
EXPR(SuperRef, Expr)
6565
EXPR(Type, Expr)
6666
EXPR(OtherConstructorDeclRef, Expr)
67-
UNCHECKED_EXPR(UnresolvedConstructor, Expr)
6867
EXPR(DotSyntaxBaseIgnored, Expr)
6968
ABSTRACT_EXPR(OverloadSetRef, Expr)
7069
UNCHECKED_EXPR(OverloadedDeclRef, OverloadSetRefExpr)

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,12 +1649,6 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
16491649
E->getDeclRef().dump(OS);
16501650
OS << ')';
16511651
}
1652-
void visitUnresolvedConstructorExpr(UnresolvedConstructorExpr *E) {
1653-
printCommon(E, "unresolved_constructor")
1654-
<< " name=" << E->getName() << '\n';
1655-
printRec(E->getSubExpr());
1656-
OS << ')';
1657-
}
16581652
void visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) {
16591653
printCommon(E, "overloaded_decl_ref_expr")
16601654
<< " name=" << E->getDecls()[0]->getName()

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
346346
return E;
347347
}
348348

349-
Expr *visitUnresolvedConstructorExpr(UnresolvedConstructorExpr *E) {
350-
if (auto sub = doIt(E->getSubExpr())) {
351-
E->setSubExpr(sub);
352-
return E;
353-
}
354-
355-
return nullptr;
356-
}
357-
358349
Expr *visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) { return E; }
359350
Expr *visitOverloadedMemberRefExpr(OverloadedMemberRefExpr *E) {
360351
if (auto base = doIt(E->getBase())) {

lib/AST/Decl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,10 +4357,13 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
43574357

43584358
if (isa<OtherConstructorDeclRefExpr>(Callee)) {
43594359
arg = apply->getArg();
4360-
} else if (auto *UCE = dyn_cast<UnresolvedConstructorExpr>(Callee)) {
4361-
arg = UCE->getSubExpr();
43624360
} else if (auto *CRE = dyn_cast<ConstructorRefCallExpr>(Callee)) {
43634361
arg = CRE->getArg();
4362+
} else if (auto *dotExpr = dyn_cast<UnresolvedDotExpr>(Callee)) {
4363+
if (dotExpr->getName().getBaseName().str() != "init")
4364+
return { true, E };
4365+
4366+
arg = dotExpr->getBase();
43644367
} else {
43654368
// Not a constructor call.
43664369
return { true, E };

lib/AST/Expr.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ bool Expr::canAppendCallParentheses() const {
502502
case ExprKind::SuperRef:
503503
case ExprKind::Type:
504504
case ExprKind::OtherConstructorDeclRef:
505-
case ExprKind::UnresolvedConstructor:
506505
case ExprKind::DotSyntaxBaseIgnored:
507506
return true;
508507

lib/Parse/ParseExpr.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,6 @@ ParserResult<Expr> Parser::parseExprSuper() {
596596
if (!name)
597597
return nullptr;
598598

599-
if (name.getBaseName() == Context.Id_init) {
600-
Expr *result = new (Context) UnresolvedConstructorExpr(superRef,
601-
dotLoc, nameLoc, name,
602-
/*Implicit=*/false);
603-
return makeParserResult(result);
604-
}
605-
606599
return makeParserResult(
607600
new (Context) UnresolvedDotExpr(superRef, dotLoc, name, nameLoc,
608601
/*Implicit=*/false));
@@ -1090,15 +1083,6 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
10901083
diag::expected_member_name);
10911084
if (!Name) return nullptr;
10921085

1093-
// Handle initializers.
1094-
if (Name.getBaseName() == Context.Id_init) {
1095-
Expr *initRef = new (Context) UnresolvedConstructorExpr(
1096-
Result.get(), TokLoc, NameLoc, Name,
1097-
/*Implicit=*/false);
1098-
Result = makeParserResult(initRef);
1099-
continue;
1100-
}
1101-
11021086
Result = makeParserResult(
11031087
new (Context) UnresolvedDotExpr(Result.get(), TokLoc, Name,
11041088
NameLoc,

lib/Sema/CSApply.cpp

Lines changed: 82 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,104 +2109,6 @@ namespace {
21092109
return expr;
21102110
}
21112111

2112-
Expr *visitUnresolvedConstructorExpr(UnresolvedConstructorExpr *expr) {
2113-
// Resolve the callee to the constructor declaration selected.
2114-
auto ctorLocator = cs.getConstraintLocator(
2115-
expr,
2116-
ConstraintLocator::ConstructorMember);
2117-
2118-
auto selected = getOverloadChoiceIfAvailable(ctorLocator);
2119-
2120-
// If we didn't form a ConstructorMember constraint, then it
2121-
// acts like a normal member reference to '.init'.
2122-
if (!selected) {
2123-
return applyMemberRefExpr(expr, expr->getSubExpr(), expr->getDotLoc(),
2124-
expr->getConstructorLoc(), expr->isImplicit());
2125-
}
2126-
2127-
2128-
auto choice = selected->choice;
2129-
auto *ctor = cast<ConstructorDecl>(choice.getDecl());
2130-
2131-
auto arg = expr->getSubExpr()->getSemanticsProvidingExpr();
2132-
auto &tc = cs.getTypeChecker();
2133-
2134-
// If the subexpression is a metatype, build a direct reference to the
2135-
// constructor.
2136-
if (arg->getType()->is<AnyMetatypeType>()) {
2137-
return buildMemberRef(expr->getSubExpr(),
2138-
selected->openedFullType,
2139-
expr->getDotLoc(),
2140-
ctor,
2141-
expr->getConstructorLoc(),
2142-
expr->getType(),
2143-
ConstraintLocatorBuilder(
2144-
cs.getConstraintLocator(expr)),
2145-
ctorLocator,
2146-
expr->isImplicit(),
2147-
AccessSemantics::Ordinary,
2148-
/*isDynamic=*/false);
2149-
}
2150-
2151-
// The subexpression must be either 'self' or 'super'.
2152-
if (!arg->isSuperExpr()) {
2153-
// 'super' references have already been fully checked; handle the
2154-
// 'self' case below.
2155-
bool diagnoseBadInitRef = true;
2156-
if (auto dre = dyn_cast<DeclRefExpr>(arg)) {
2157-
if (dre->getDecl()->getName() == cs.getASTContext().Id_self) {
2158-
// We have a reference to 'self'.
2159-
diagnoseBadInitRef = false;
2160-
2161-
// Make sure the reference to 'self' occurs within an initializer.
2162-
if (!dyn_cast_or_null<ConstructorDecl>(
2163-
cs.DC->getInnermostMethodContext())) {
2164-
if (!SuppressDiagnostics)
2165-
tc.diagnose(expr->getDotLoc(),
2166-
diag::init_delegation_outside_initializer);
2167-
return nullptr;
2168-
}
2169-
}
2170-
}
2171-
2172-
// If we need to diagnose this as a bad reference to an initializer,
2173-
// do so now.
2174-
if (diagnoseBadInitRef) {
2175-
// Determine whether 'super' would have made sense as a base.
2176-
bool hasSuper = false;
2177-
if (auto func = cs.DC->getInnermostMethodContext()) {
2178-
if (auto nominalType
2179-
= func->getDeclContext()->getDeclaredTypeOfContext()) {
2180-
if (auto classDecl = nominalType->getClassOrBoundGenericClass()) {
2181-
hasSuper = classDecl->hasSuperclass();
2182-
}
2183-
}
2184-
}
2185-
2186-
if (SuppressDiagnostics)
2187-
return nullptr;
2188-
2189-
tc.diagnose(expr->getDotLoc(), diag::bad_init_ref_base, hasSuper);
2190-
}
2191-
}
2192-
2193-
// Build a partial application of the delegated initializer.
2194-
Expr *ctorRef = buildOtherConstructorRef(
2195-
selected->openedFullType,
2196-
ctor, expr->getConstructorLoc(),
2197-
cs.getConstraintLocator(
2198-
expr,
2199-
ConstraintLocator::ConstructorMember),
2200-
expr->isImplicit());
2201-
auto *call
2202-
= new (cs.getASTContext()) DotSyntaxCallExpr(ctorRef,
2203-
expr->getDotLoc(),
2204-
expr->getSubExpr());
2205-
return finishApply(call, expr->getType(),
2206-
ConstraintLocatorBuilder(
2207-
cs.getConstraintLocator(expr)));
2208-
}
2209-
22102112
Expr *visitDotSyntaxBaseIgnoredExpr(DotSyntaxBaseIgnoredExpr *expr) {
22112113
return simplifyExprType(expr);
22122114
}
@@ -2352,9 +2254,91 @@ namespace {
23522254
/// A list of optional injections that have been diagnosed.
23532255
llvm::SmallPtrSet<InjectIntoOptionalExpr *, 4> DiagnosedOptionalInjections;
23542256
private:
2257+
/// Create a member reference to the given constructor.
2258+
Expr *applyCtorRefExpr(Expr *expr, Expr *base, SourceLoc dotLoc,
2259+
SourceLoc nameLoc, bool implicit,
2260+
ConstraintLocator *ctorLocator,
2261+
ConstructorDecl *ctor,
2262+
Type openedType) {
2263+
// If the subexpression is a metatype, build a direct reference to the
2264+
// constructor.
2265+
if (base->getType()->is<AnyMetatypeType>()) {
2266+
return buildMemberRef(base, openedType, dotLoc, ctor, nameLoc,
2267+
expr->getType(),
2268+
ConstraintLocatorBuilder(
2269+
cs.getConstraintLocator(expr)),
2270+
ctorLocator,
2271+
implicit,
2272+
AccessSemantics::Ordinary,
2273+
/*isDynamic=*/false);
2274+
}
2275+
2276+
// The subexpression must be either 'self' or 'super'.
2277+
if (!base->isSuperExpr()) {
2278+
// 'super' references have already been fully checked; handle the
2279+
// 'self' case below.
2280+
auto &tc = cs.getTypeChecker();
2281+
bool diagnoseBadInitRef = true;
2282+
auto arg = base->getSemanticsProvidingExpr();
2283+
if (auto dre = dyn_cast<DeclRefExpr>(arg)) {
2284+
if (dre->getDecl()->getName() == cs.getASTContext().Id_self) {
2285+
// We have a reference to 'self'.
2286+
diagnoseBadInitRef = false;
2287+
2288+
// Make sure the reference to 'self' occurs within an initializer.
2289+
if (!dyn_cast_or_null<ConstructorDecl>(
2290+
cs.DC->getInnermostMethodContext())) {
2291+
if (!SuppressDiagnostics)
2292+
tc.diagnose(dotLoc, diag::init_delegation_outside_initializer);
2293+
return nullptr;
2294+
}
2295+
}
2296+
}
2297+
2298+
// If we need to diagnose this as a bad reference to an initializer,
2299+
// do so now.
2300+
if (diagnoseBadInitRef) {
2301+
// Determine whether 'super' would have made sense as a base.
2302+
bool hasSuper = false;
2303+
if (auto func = cs.DC->getInnermostMethodContext()) {
2304+
if (auto nominalType
2305+
= func->getDeclContext()->getDeclaredTypeOfContext()) {
2306+
if (auto classDecl = nominalType->getClassOrBoundGenericClass()) {
2307+
hasSuper = classDecl->hasSuperclass();
2308+
}
2309+
}
2310+
}
2311+
2312+
if (SuppressDiagnostics)
2313+
return nullptr;
2314+
2315+
tc.diagnose(dotLoc, diag::bad_init_ref_base, hasSuper);
2316+
}
2317+
}
2318+
2319+
// Build a partial application of the delegated initializer.
2320+
Expr *ctorRef = buildOtherConstructorRef(openedType, ctor, nameLoc,
2321+
ctorLocator, implicit);
2322+
auto *call = new (cs.getASTContext()) DotSyntaxCallExpr(ctorRef, dotLoc,
2323+
base);
2324+
return finishApply(call, expr->getType(),
2325+
ConstraintLocatorBuilder(
2326+
cs.getConstraintLocator(expr)));
2327+
}
23552328

23562329
Expr *applyMemberRefExpr(Expr *expr, Expr *base, SourceLoc dotLoc,
23572330
SourceLoc nameLoc, bool implicit) {
2331+
// If we have a constructor member, handle it as a constructor.
2332+
auto ctorLocator = cs.getConstraintLocator(
2333+
expr,
2334+
ConstraintLocator::ConstructorMember);
2335+
if (auto selected = getOverloadChoiceIfAvailable(ctorLocator)) {
2336+
auto choice = selected->choice;
2337+
auto *ctor = cast<ConstructorDecl>(choice.getDecl());
2338+
return applyCtorRefExpr(expr, base, dotLoc, nameLoc, implicit,
2339+
ctorLocator, ctor, selected->openedFullType);
2340+
}
2341+
23582342
// Determine the declaration selected for this overloaded reference.
23592343
auto memberLocator = cs.getConstraintLocator(expr,
23602344
ConstraintLocator::Member);

0 commit comments

Comments
 (0)