Skip to content

Commit e6dbfa3

Browse files
committed
Move Utilities back to TypeChecker
1 parent fb8b060 commit e6dbfa3

File tree

9 files changed

+88
-86
lines changed

9 files changed

+88
-86
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ namespace swift {
109109
class TypeAliasDecl;
110110
class VarDecl;
111111
class UnifiedStatsReporter;
112-
class UnresolvedDotExpr;
113112
class IndexSubset;
114113

115114
enum class KnownProtocolKind : uint8_t;
@@ -905,26 +904,6 @@ class ASTContext final {
905904
/// Each kind and SourceFile has its own cache for a Type.
906905
Type &getDefaultTypeRequestCache(SourceFile *, KnownProtocolKind);
907906

908-
public:
909-
/// Require that the library intrinsics for working with Optional<T>
910-
/// exist.
911-
bool requireOptionalIntrinsics(SourceLoc loc);
912-
913-
/// Require that the library intrinsics for working with
914-
/// UnsafeMutablePointer<T> exist.
915-
bool requirePointerArgumentIntrinsics(SourceLoc loc);
916-
917-
/// Require that the library intrinsics for creating
918-
/// array literals exist.
919-
bool requireArrayLiteralIntrinsics(SourceLoc loc);
920-
921-
public:
922-
/// If an expression references 'self.init' or 'super.init' in an
923-
/// initializer context, returns the implicit 'self' decl of the constructor.
924-
/// Otherwise, return nil.
925-
VarDecl *getSelfForInitDelegationInConstructor(DeclContext *DC,
926-
UnresolvedDotExpr *ctorRef);
927-
928907
private:
929908
friend Decl;
930909
Optional<RawComment> getRawComment(const Decl *D);

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,54 +4622,6 @@ void ASTContext::setSideCachedPropertyWrapperBackingPropertyType(
46224622
getImpl().PropertyWrapperBackingVarTypes[var] = type;
46234623
}
46244624

4625-
bool ASTContext::requireOptionalIntrinsics(SourceLoc loc) {
4626-
if (hasOptionalIntrinsics())
4627-
return false;
4628-
4629-
Diags.diagnose(loc, diag::optional_intrinsics_not_found);
4630-
return true;
4631-
}
4632-
4633-
bool ASTContext::requirePointerArgumentIntrinsics(SourceLoc loc) {
4634-
if (hasPointerArgumentIntrinsics())
4635-
return false;
4636-
4637-
Diags.diagnose(loc, diag::pointer_argument_intrinsics_not_found);
4638-
return true;
4639-
}
4640-
4641-
bool ASTContext::requireArrayLiteralIntrinsics(SourceLoc loc) {
4642-
if (hasArrayLiteralIntrinsics())
4643-
return false;
4644-
4645-
Diags.diagnose(loc, diag::array_literal_intrinsics_not_found);
4646-
return true;
4647-
}
4648-
4649-
/// If an expression references 'self.init' or 'super.init' in an
4650-
/// initializer context, returns the implicit 'self' decl of the constructor.
4651-
/// Otherwise, return nil.
4652-
VarDecl *
4653-
ASTContext::getSelfForInitDelegationInConstructor(DeclContext *DC,
4654-
UnresolvedDotExpr *ctorRef) {
4655-
// If the reference isn't to a constructor, we're done.
4656-
if (ctorRef->getName().getBaseName() != DeclBaseName::createConstructor())
4657-
return nullptr;
4658-
4659-
if (auto ctorContext
4660-
= dyn_cast_or_null<ConstructorDecl>(DC->getInnermostMethodContext())) {
4661-
auto nestedArg = ctorRef->getBase();
4662-
if (auto inout = dyn_cast<InOutExpr>(nestedArg))
4663-
nestedArg = inout->getSubExpr();
4664-
if (nestedArg->isSuperExpr())
4665-
return ctorContext->getImplicitSelfDecl();
4666-
if (auto declRef = dyn_cast<DeclRefExpr>(nestedArg))
4667-
if (declRef->getDecl()->getFullName() == Id_self)
4668-
return ctorContext->getImplicitSelfDecl();
4669-
}
4670-
return nullptr;
4671-
}
4672-
46734625
VarDecl *VarDecl::getOriginalWrappedProperty(
46744626
Optional<PropertyWrapperSynthesizedPropertyKind> kind) const {
46754627
if (!Bits.VarDecl.IsPropertyWrapperBackingProperty)

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,7 +3192,7 @@ namespace {
31923192
return nullptr;
31933193

31943194
// Extract a Bool from the resulting expression.
3195-
ctx.requireOptionalIntrinsics(expr->getLoc());
3195+
TypeChecker::requireOptionalIntrinsics(ctx, expr->getLoc());
31963196

31973197
// Match the optional value against its `Some` case.
31983198
auto *someDecl = ctx.getOptionalSomeDecl();
@@ -5069,7 +5069,7 @@ Expr *ExprRewriter::coerceOptionalToOptional(Expr *expr, Type toType,
50695069
auto &ctx = cs.getASTContext();
50705070
Type fromType = cs.getType(expr);
50715071

5072-
ctx.requireOptionalIntrinsics(expr->getLoc());
5072+
TypeChecker::requireOptionalIntrinsics(ctx, expr->getLoc());
50735073

50745074
SmallVector<Type, 4> fromOptionals;
50755075
(void)fromType->lookThroughAllOptionalTypes(fromOptionals);
@@ -6012,7 +6012,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
60126012
case ConversionRestrictionKind::ValueToOptional: {
60136013
auto toGenericType = toType->castTo<BoundGenericType>();
60146014
assert(toGenericType->getDecl()->isOptionalDecl());
6015-
cs.getASTContext().requireOptionalIntrinsics(expr->getLoc());
6015+
TypeChecker::requireOptionalIntrinsics(cs.getASTContext(),
6016+
expr->getLoc());
60166017

60176018
Type valueType = toGenericType->getGenericArgs()[0];
60186019
expr = coerceToType(expr, valueType, locator);
@@ -6072,7 +6073,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
60726073
auto toEltType = unwrappedTy->getAnyPointerElementType(pointerKind);
60736074
assert(toEltType && "not a pointer type?"); (void) toEltType;
60746075

6075-
ctx.requirePointerArgumentIntrinsics(expr->getLoc());
6076+
TypeChecker::requirePointerArgumentIntrinsics(ctx, expr->getLoc());
60766077
Expr *result =
60776078
cs.cacheType(new (ctx) InOutToPointerExpr(expr, unwrappedTy));
60786079
if (isOptional)
@@ -6088,7 +6089,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
60886089
unwrappedTy = unwrapped;
60896090
}
60906091

6091-
ctx.requirePointerArgumentIntrinsics(expr->getLoc());
6092+
TypeChecker::requirePointerArgumentIntrinsics(ctx, expr->getLoc());
60926093
Expr *result =
60936094
cs.cacheType(new (ctx) ArrayToPointerExpr(expr, unwrappedTy));
60946095
if (isOptional)
@@ -6104,7 +6105,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
61046105
unwrappedTy = unwrapped;
61056106
}
61066107

6107-
ctx.requirePointerArgumentIntrinsics(expr->getLoc());
6108+
TypeChecker::requirePointerArgumentIntrinsics(ctx, expr->getLoc());
61086109
Expr *result =
61096110
cs.cacheType(new (ctx) StringToPointerExpr(expr, unwrappedTy));
61106111
if (isOptional)
@@ -6113,7 +6114,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
61136114
}
61146115

61156116
case ConversionRestrictionKind::PointerToPointer: {
6116-
ctx.requirePointerArgumentIntrinsics(expr->getLoc());
6117+
TypeChecker::requirePointerArgumentIntrinsics(ctx, expr->getLoc());
61176118
Type unwrappedToTy = toType->getOptionalObjectType();
61186119

61196120
// Optional to optional.
@@ -6364,7 +6365,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
63646365
auto toGenericType = cast<BoundGenericEnumType>(desugaredToType);
63656366
if (!toGenericType->getDecl()->isOptionalDecl())
63666367
break;
6367-
ctx.requireOptionalIntrinsics(expr->getLoc());
6368+
TypeChecker::requireOptionalIntrinsics(ctx, expr->getLoc());
63686369

63696370
if (cs.getType(expr)->getOptionalObjectType())
63706371
return coerceOptionalToOptional(expr, toType, locator, typeFromPattern);

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ namespace {
12421242
case MagicIdentifierLiteralExpr::DSOHandle: {
12431243
// #dsohandle has type UnsafeMutableRawPointer.
12441244
auto &ctx = CS.getASTContext();
1245-
if (ctx.requirePointerArgumentIntrinsics(expr->getLoc()))
1245+
if (TypeChecker::requirePointerArgumentIntrinsics(ctx, expr->getLoc()))
12461246
return nullptr;
12471247

12481248
auto unsafeRawPointer = ctx.getUnsafeRawPointerDecl();
@@ -1578,7 +1578,7 @@ namespace {
15781578

15791579
// Open a member constraint for constructor delegations on the
15801580
// subexpr type.
1581-
if (ctx.getSelfForInitDelegationInConstructor(CS.DC, expr)) {
1581+
if (TypeChecker::getSelfForInitDelegationInConstructor(CS.DC, expr)) {
15821582
auto baseTy = CS.getType(expr->getBase())
15831583
->getWithoutSpecifierType();
15841584

@@ -2853,7 +2853,8 @@ namespace {
28532853
/// worth QoI efforts.
28542854
Type getOptionalType(SourceLoc optLoc, Type valueTy) {
28552855
auto optTy = CS.getTypeChecker().getOptionalType(optLoc, valueTy);
2856-
if (!optTy || CS.getASTContext().requireOptionalIntrinsics(optLoc))
2856+
if (!optTy ||
2857+
TypeChecker::requireOptionalIntrinsics(CS.getASTContext(), optLoc))
28572858
return Type();
28582859

28592860
return optTy;

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,9 @@ ConstraintSystem::getCalleeLocator(ConstraintLocator *locator,
483483

484484
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
485485
return getConstraintLocator(
486-
anchor, getASTContext().getSelfForInitDelegationInConstructor(DC, UDE)
487-
? ConstraintLocator::ConstructorMember
488-
: ConstraintLocator::Member);
486+
anchor, TypeChecker::getSelfForInitDelegationInConstructor(DC, UDE)
487+
? ConstraintLocator::ConstructorMember
488+
: ConstraintLocator::Member);
489489
}
490490

491491
if (isa<UnresolvedMemberExpr>(anchor))

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,30 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
802802
return new (Context) ErrorExpr(UDRE->getSourceRange());
803803
}
804804

805+
/// If an expression references 'self.init' or 'super.init' in an
806+
/// initializer context, returns the implicit 'self' decl of the constructor.
807+
/// Otherwise, return nil.
808+
VarDecl *
809+
TypeChecker::getSelfForInitDelegationInConstructor(DeclContext *DC,
810+
UnresolvedDotExpr *ctorRef) {
811+
// If the reference isn't to a constructor, we're done.
812+
if (ctorRef->getName().getBaseName() != DeclBaseName::createConstructor())
813+
return nullptr;
814+
815+
if (auto ctorContext =
816+
dyn_cast_or_null<ConstructorDecl>(DC->getInnermostMethodContext())) {
817+
auto nestedArg = ctorRef->getBase();
818+
if (auto inout = dyn_cast<InOutExpr>(nestedArg))
819+
nestedArg = inout->getSubExpr();
820+
if (nestedArg->isSuperExpr())
821+
return ctorContext->getImplicitSelfDecl();
822+
if (auto declRef = dyn_cast<DeclRefExpr>(nestedArg))
823+
if (declRef->getDecl()->getFullName() == DC->getASTContext().Id_self)
824+
return ctorContext->getImplicitSelfDecl();
825+
}
826+
return nullptr;
827+
}
828+
805829
namespace {
806830
/// Update the function reference kind based on adding a direct call to a
807831
/// callee with this kind.
@@ -1216,8 +1240,8 @@ namespace {
12161240
// RebindSelfInConstructorExpr::getCalledConstructor.
12171241
auto &ctx = getASTContext();
12181242
if (auto unresolvedDot = dyn_cast<UnresolvedDotExpr>(expr)) {
1219-
if (auto self
1220-
= ctx.getSelfForInitDelegationInConstructor(DC, unresolvedDot)) {
1243+
if (auto self = TypeChecker::getSelfForInitDelegationInConstructor(
1244+
DC, unresolvedDot)) {
12211245
// Walk our ancestor expressions looking for the appropriate place
12221246
// to insert the RebindSelfInConstructorExpr.
12231247
Expr *target = nullptr;

lib/Sema/TypeCheckExpr.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,32 @@ static Expr *foldSequence(DeclContext *DC,
563563
return makeBinOp(Ctx, op1.op, LHS, RHS, op1.precedence, S.empty());
564564
}
565565

566+
bool TypeChecker::requireOptionalIntrinsics(ASTContext &ctx, SourceLoc loc) {
567+
if (ctx.hasOptionalIntrinsics())
568+
return false;
569+
570+
ctx.Diags.diagnose(loc, diag::optional_intrinsics_not_found);
571+
return true;
572+
}
573+
574+
bool TypeChecker::requirePointerArgumentIntrinsics(ASTContext &ctx,
575+
SourceLoc loc) {
576+
if (ctx.hasPointerArgumentIntrinsics())
577+
return false;
578+
579+
ctx.Diags.diagnose(loc, diag::pointer_argument_intrinsics_not_found);
580+
return true;
581+
}
582+
583+
bool TypeChecker::requireArrayLiteralIntrinsics(ASTContext &ctx,
584+
SourceLoc loc) {
585+
if (ctx.hasArrayLiteralIntrinsics())
586+
return false;
587+
588+
ctx.Diags.diagnose(loc, diag::array_literal_intrinsics_not_found);
589+
return true;
590+
}
591+
566592
Expr *TypeChecker::buildCheckedRefExpr(VarDecl *value, DeclContext *UseDC,
567593
DeclNameLoc loc, bool Implicit) {
568594
auto type = TypeChecker::getUnopenedTypeOfReference(value, Type(), UseDC);

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
835835
}
836836

837837
// Working with iterators requires Optional.
838-
if (getASTContext().requireOptionalIntrinsics(S->getForLoc()))
838+
if (TypeChecker::requireOptionalIntrinsics(getASTContext(), S->getForLoc()))
839839
return nullptr;
840840

841841
// Gather the witnesses from the Iterator protocol conformance, which

lib/Sema/TypeChecker.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,12 @@ class TypeChecker final {
18411841
return DiagnosedExprs[E];
18421842
}
18431843

1844+
/// If an expression references 'self.init' or 'super.init' in an
1845+
/// initializer context, returns the implicit 'self' decl of the constructor.
1846+
/// Otherwise, return nil.
1847+
static VarDecl *getSelfForInitDelegationInConstructor(DeclContext *DC,
1848+
UnresolvedDotExpr *UDE);
1849+
18441850
/// Diagnose assigning variable to itself.
18451851
static bool diagnoseSelfAssignment(const Expr *E);
18461852

@@ -1894,6 +1900,19 @@ class TypeChecker final {
18941900
/// special case type-checking behavior.
18951901
static DeclTypeCheckingSemantics
18961902
getDeclTypeCheckingSemantics(ValueDecl *decl);
1903+
1904+
public:
1905+
/// Require that the library intrinsics for working with Optional<T>
1906+
/// exist.
1907+
static bool requireOptionalIntrinsics(ASTContext &ctx, SourceLoc loc);
1908+
1909+
/// Require that the library intrinsics for working with
1910+
/// UnsafeMutablePointer<T> exist.
1911+
static bool requirePointerArgumentIntrinsics(ASTContext &ctx, SourceLoc loc);
1912+
1913+
/// Require that the library intrinsics for creating
1914+
/// array literals exist.
1915+
static bool requireArrayLiteralIntrinsics(ASTContext &ctx, SourceLoc loc);
18971916
};
18981917

18991918
/// Temporary on-stack storage and unescaping for encoded diagnostic

0 commit comments

Comments
 (0)