Skip to content

Commit d78d95e

Browse files
committed
[AST] Introduce PlaceholderType and ThePlaceholderType singleton
1 parent 580fd40 commit d78d95e

File tree

12 files changed

+56
-3
lines changed

12 files changed

+56
-3
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ class ASTContext final {
756756
// Builtin type and simple types that are used frequently.
757757
const CanType TheErrorType; /// This is the ErrorType singleton.
758758
const CanType TheUnresolvedType; /// This is the UnresolvedType singleton.
759+
const CanType ThePlaceholderType;
759760
const CanType TheEmptyTupleType; /// This is '()', aka Void
760761
const CanType TheAnyType; /// This is 'Any', the empty protocol composition
761762
#define SINGLETON_TYPE(SHORT_ID, ID) \

include/swift/AST/TypeNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797

9898
TYPE(Error, Type)
9999
UNCHECKED_TYPE(Unresolved, Type)
100+
UNCHECKED_TYPE(Placeholder, Type)
100101
UNCHECKED_TYPE(Hole, Type)
101102
ABSTRACT_TYPE(Builtin, Type)
102103
ABSTRACT_TYPE(AnyBuiltinInteger, BuiltinType)

include/swift/AST/Types.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ class RecursiveTypeProperties {
152152
/// This type contains a type hole.
153153
HasTypeHole = 0x800,
154154

155-
Last_Property = HasTypeHole
155+
/// This type contains a placeholder.
156+
HasPlaceholder = 0x1000,
157+
158+
Last_Property = HasPlaceholder
156159
};
157160
enum { BitWidth = countBitsUsed(Property::Last_Property) };
158161

@@ -211,6 +214,9 @@ class RecursiveTypeProperties {
211214
/// type hole?
212215
bool hasTypeHole() const { return Bits & HasTypeHole; }
213216

217+
/// Does a type with these properties structurally contain a placeholder?
218+
bool hasPlaceholder() const { return Bits & HasPlaceholder; }
219+
214220
/// Returns the set of properties present in either set.
215221
friend RecursiveTypeProperties operator|(Property lhs, Property rhs) {
216222
return RecursiveTypeProperties(unsigned(lhs) | unsigned(rhs));
@@ -580,6 +586,11 @@ class alignas(1 << TypeAlignInBits) TypeBase {
580586
return getRecursiveProperties().hasUnresolvedType();
581587
}
582588

589+
/// Determine whether this type involves a \c PlaceholderType.
590+
bool hasPlaceholder() const {
591+
return getRecursiveProperties().hasPlaceholder();
592+
}
593+
583594
/// Determine whether this type involves a hole.
584595
bool hasHole() const { return getRecursiveProperties().hasTypeHole(); }
585596

@@ -1351,6 +1362,20 @@ class UnresolvedType : public TypeBase {
13511362
};
13521363
DEFINE_EMPTY_CAN_TYPE_WRAPPER(UnresolvedType, Type)
13531364

1365+
class PlaceholderType : public TypeBase {
1366+
friend class ASTContext;
1367+
// The Placeholder type is always canonical.
1368+
PlaceholderType(ASTContext &C)
1369+
: TypeBase(TypeKind::Placeholder, &C,
1370+
RecursiveTypeProperties(RecursiveTypeProperties::HasPlaceholder)) { }
1371+
public:
1372+
// Implement isa/cast/dyncast/etc.
1373+
static bool classof(const TypeBase *T) {
1374+
return T->getKind() == TypeKind::Placeholder;
1375+
}
1376+
};
1377+
DEFINE_EMPTY_CAN_TYPE_WRAPPER(PlaceholderType, Type)
1378+
13541379

13551380
/// BuiltinType - An abstract class for all the builtin types.
13561381
class BuiltinType : public TypeBase {

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ ASTContext::ASTContext(LangOptions &langOpts, TypeCheckerOptions &typeckOpts,
595595
ErrorType(*this, Type(), RecursiveTypeProperties::HasError)),
596596
TheUnresolvedType(new (*this, AllocationArena::Permanent)
597597
UnresolvedType(*this)),
598+
ThePlaceholderType(new (*this, AllocationArena::Permanent)
599+
PlaceholderType(*this)),
598600
TheEmptyTupleType(TupleType::get(ArrayRef<TupleTypeElt>(), *this)),
599601
TheAnyType(ProtocolCompositionType::get(*this, ArrayRef<Type>(),
600602
/*HasExplicitAnyObject=*/false)),
@@ -3102,7 +3104,7 @@ isFunctionTypeCanonical(ArrayRef<AnyFunctionType::Param> params,
31023104
static RecursiveTypeProperties
31033105
getGenericFunctionRecursiveProperties(ArrayRef<AnyFunctionType::Param> params,
31043106
Type result) {
3105-
static_assert(RecursiveTypeProperties::BitWidth == 12,
3107+
static_assert(RecursiveTypeProperties::BitWidth == 13,
31063108
"revisit this if you add new recursive type properties");
31073109
RecursiveTypeProperties properties;
31083110

@@ -3676,7 +3678,7 @@ CanSILFunctionType SILFunctionType::get(
36763678
void *mem = ctx.Allocate(bytes, alignof(SILFunctionType));
36773679

36783680
RecursiveTypeProperties properties;
3679-
static_assert(RecursiveTypeProperties::BitWidth == 12,
3681+
static_assert(RecursiveTypeProperties::BitWidth == 13,
36803682
"revisit this if you add new recursive type properties");
36813683
for (auto &param : params)
36823684
properties |= param.getInterfaceType()->getRecursiveProperties();

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,6 +3535,8 @@ namespace {
35353535

35363536
TRIVIAL_TYPE_PRINTER(Unresolved, unresolved)
35373537

3538+
TRIVIAL_TYPE_PRINTER(Placeholder, placeholder)
3539+
35383540
void visitHoleType(HoleType *T, StringRef label) {
35393541
printCommon(label, "hole_type");
35403542
auto originator = T->getOriginator();

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
966966

967967
case TypeKind::Error:
968968
case TypeKind::Unresolved:
969+
case TypeKind::Placeholder:
969970
appendOperator("Xe");
970971
return;
971972

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
41684168
Printer << "_";
41694169
}
41704170

4171+
void visitPlaceholderType(PlaceholderType *T) {
4172+
if (Options.PrintTypesForDebugging)
4173+
Printer << "<<placeholdertype>>";
4174+
else
4175+
Printer << "_";
4176+
}
4177+
41714178
void visitHoleType(HoleType *T) {
41724179
if (Options.PrintTypesForDebugging) {
41734180
Printer << "<<hole for ";

lib/AST/Type.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ bool CanType::isReferenceTypeImpl(CanType type, const GenericSignatureImpl *sig,
204204
case TypeKind::SILBlockStorage:
205205
case TypeKind::Error:
206206
case TypeKind::Unresolved:
207+
case TypeKind::Placeholder:
207208
case TypeKind::BuiltinInteger:
208209
case TypeKind::BuiltinIntegerLiteral:
209210
case TypeKind::BuiltinFloat:
@@ -1153,6 +1154,7 @@ CanType TypeBase::computeCanonicalType() {
11531154
#include "swift/AST/TypeNodes.def"
11541155
case TypeKind::Error:
11551156
case TypeKind::Unresolved:
1157+
case TypeKind::Placeholder:
11561158
case TypeKind::TypeVariable:
11571159
case TypeKind::Hole:
11581160
llvm_unreachable("these types are always canonical");
@@ -4275,6 +4277,7 @@ case TypeKind::Id:
42754277
case TypeKind::OpenedArchetype:
42764278
case TypeKind::Error:
42774279
case TypeKind::Unresolved:
4280+
case TypeKind::Placeholder:
42784281
case TypeKind::TypeVariable:
42794282
case TypeKind::Hole:
42804283
case TypeKind::GenericTypeParam:
@@ -5013,6 +5016,7 @@ ReferenceCounting TypeBase::getReferenceCounting() {
50135016
case TypeKind::SILBlockStorage:
50145017
case TypeKind::Error:
50155018
case TypeKind::Unresolved:
5019+
case TypeKind::Placeholder:
50165020
case TypeKind::BuiltinInteger:
50175021
case TypeKind::BuiltinIntegerLiteral:
50185022
case TypeKind::BuiltinFloat:

lib/AST/TypeWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
3434

3535
bool visitErrorType(ErrorType *ty) { return false; }
3636
bool visitUnresolvedType(UnresolvedType *ty) { return false; }
37+
bool visitPlaceholderType(PlaceholderType *ty) { return false; }
3738
bool visitHoleType(HoleType *ty) { return false; }
3839
bool visitBuiltinType(BuiltinType *ty) { return false; }
3940
bool visitTypeAliasType(TypeAliasType *ty) {

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
16291629
// checker.
16301630
case TypeKind::Error:
16311631
case TypeKind::Unresolved:
1632+
case TypeKind::Placeholder:
16321633
case TypeKind::LValue:
16331634
case TypeKind::TypeVariable:
16341635
case TypeKind::Hole:

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4901,6 +4901,9 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
49014901
case TypeKind::Unresolved:
49024902
return getTypeMatchFailure(locator);
49034903

4904+
case TypeKind::Placeholder:
4905+
llvm_unreachable("placeholders should already be converted to type vars");
4906+
49044907
case TypeKind::Hole: {
49054908
// If it's allowed to attempt fixes, let's delegate
49064909
// decision to `repairFailures`, since depending on
@@ -5596,6 +5599,7 @@ ConstraintSystem::simplifyConstructionConstraint(
55965599
llvm_unreachable("artificial type in constraint");
55975600

55985601
case TypeKind::Unresolved:
5602+
case TypeKind::Placeholder:
55995603
case TypeKind::Error:
56005604
case TypeKind::Hole:
56015605
return SolutionKind::Error;

lib/Serialization/Serialization.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,6 +4109,10 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
41094109
llvm_unreachable("should not serialize an UnresolvedType");
41104110
}
41114111

4112+
void visitPlaceholderType(const PlaceholderType *) {
4113+
llvm_unreachable("should not serialize an invalid type");
4114+
}
4115+
41124116
void visitHoleType(const HoleType *) {
41134117
llvm_unreachable("should not serialize a HoleType");
41144118
}

0 commit comments

Comments
 (0)