Skip to content

Commit c1b8690

Browse files
committed
AST: Introduce special Builtin.TheTupleType singleton
1 parent 7b9d4e6 commit c1b8690

33 files changed

+307
-10
lines changed

include/swift/AST/ASTContext.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace swift {
6666
enum class Associativity : unsigned char;
6767
class AvailabilityContext;
6868
class BoundGenericType;
69+
class BuiltinTupleDecl;
6970
class ClangModuleLoader;
7071
class ClangNode;
7172
class ClangTypeConverter;
@@ -953,7 +954,7 @@ class ASTContext final {
953954
const CanType TheIEEE80Type; /// 80-bit IEEE floating point
954955
const CanType TheIEEE128Type; /// 128-bit IEEE floating point
955956
const CanType ThePPC128Type; /// 128-bit PowerPC 2xDouble
956-
957+
957958
/// Adds a search path to SearchPathOpts, unless it is already present.
958959
///
959960
/// Does any proper bookkeeping to keep all module loaders up to date as well.
@@ -1421,6 +1422,13 @@ class ASTContext final {
14211422
/// invocation decoder of the given distributed actor.
14221423
FuncDecl *getDistributedActorArgumentDecodingMethod(NominalTypeDecl *);
14231424

1425+
/// The special Builtin.TheTupleType, which parents tuple extensions and
1426+
/// conformances.
1427+
BuiltinTupleDecl *getBuiltinTupleDecl();
1428+
1429+
/// The declared interface type of Builtin.TheTupleType.
1430+
BuiltinTupleType *getBuiltinTupleType();
1431+
14241432
private:
14251433
friend Decl;
14261434

include/swift/AST/Decl.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4755,6 +4755,48 @@ class ProtocolDecl final : public NominalTypeDecl {
47554755
}
47564756
};
47574757

4758+
/// This is the special singleton Builtin.TheTupleType. It is not directly
4759+
/// visible in the source language, but we use it to attach extensions
4760+
/// and conformances for tuple types.
4761+
///
4762+
/// - The declared interface type is the special TheTupleType singleton.
4763+
/// - The generic parameter list has one pack generic parameter, <Elements...>
4764+
/// - The generic signature has no requirements, <Elements...>
4765+
/// - The self interface type is the tuple type containing a single pack
4766+
/// expansion, (Elements...).
4767+
class BuiltinTupleDecl final : public NominalTypeDecl {
4768+
TupleType *TupleSelfType = nullptr;
4769+
4770+
public:
4771+
BuiltinTupleDecl(Identifier Name, DeclContext *Parent);
4772+
4773+
SourceRange getSourceRange() const {
4774+
return SourceRange();
4775+
}
4776+
4777+
TupleType *getTupleSelfType() const;
4778+
4779+
// Implement isa/cast/dyncast/etc.
4780+
static bool classof(const Decl *D) {
4781+
return D->getKind() == DeclKind::BuiltinTuple;
4782+
}
4783+
static bool classof(const GenericTypeDecl *D) {
4784+
return D->getKind() == DeclKind::BuiltinTuple;
4785+
}
4786+
static bool classof(const NominalTypeDecl *D) {
4787+
return D->getKind() == DeclKind::BuiltinTuple;
4788+
}
4789+
static bool classof(const DeclContext *C) {
4790+
if (auto D = C->getAsDecl())
4791+
return classof(D);
4792+
return false;
4793+
}
4794+
static bool classof(const IterableDeclContext *C) {
4795+
auto NTD = dyn_cast<NominalTypeDecl>(C);
4796+
return NTD && classof(NTD);
4797+
}
4798+
};
4799+
47584800
/// AbstractStorageDecl - This is the common superclass for VarDecl and
47594801
/// SubscriptDecl, representing potentially settable memory locations.
47604802
class AbstractStorageDecl : public ValueDecl {

include/swift/AST/DeclNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ ABSTRACT_DECL(Value, Decl)
151151
NOMINAL_TYPE_DECL(Struct, NominalTypeDecl)
152152
NOMINAL_TYPE_DECL(Class, NominalTypeDecl)
153153
NOMINAL_TYPE_DECL(Protocol, NominalTypeDecl)
154-
DECL_RANGE(NominalType, Enum, Protocol)
154+
NOMINAL_TYPE_DECL(BuiltinTuple, NominalTypeDecl)
155+
DECL_RANGE(NominalType, Enum, BuiltinTuple)
155156
GENERIC_VALUE_DECL(OpaqueType, GenericTypeDecl)
156157
GENERIC_VALUE_DECL(TypeAlias, GenericTypeDecl)
157158
DECL_RANGE(GenericType, Enum, TypeAlias)

include/swift/AST/KnownIdentifiers.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ IDENTIFIER_WITH_NAME(PatternMatchVar, "$match")
7575
IDENTIFIER(dynamicallyCall)
7676
IDENTIFIER(dynamicMember)
7777
IDENTIFIER(Element)
78+
IDENTIFIER(Elements)
7879
IDENTIFIER_(enclosingInstance)
7980
IDENTIFIER(Encodable)
8081
IDENTIFIER(encode)
@@ -313,6 +314,9 @@ IDENTIFIER_WITH_NAME(TypeWrapperProperty, "$_storage")
313314
IDENTIFIER(storageKeyPath)
314315
IDENTIFIER(memberwise)
315316

317+
// The singleton instance of TupleTypeDecl in the Builtin module
318+
IDENTIFIER(TheTupleType)
319+
316320
#undef IDENTIFIER
317321
#undef IDENTIFIER_
318322
#undef IDENTIFIER_WITH_NAME

include/swift/AST/TypeNodes.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ ABSTRACT_TYPE(AnyGeneric, Type)
128128
TYPE(Struct, NominalType)
129129
TYPE(Class, NominalType)
130130
TYPE(Protocol, NominalType)
131-
TYPE_RANGE(Nominal, Enum, Protocol)
131+
TYPE(BuiltinTuple, NominalType)
132+
TYPE_RANGE(Nominal, Enum, BuiltinTuple)
132133
ABSTRACT_TYPE(BoundGeneric, Type)
133134
TYPE(BoundGenericClass, BoundGenericType)
134135
TYPE(BoundGenericEnum, BoundGenericType)

include/swift/AST/Types.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ArgumentList;
5858
class AssociatedTypeDecl;
5959
class ASTContext;
6060
enum BufferPointerTypeKind : unsigned;
61+
class BuiltinTupleDecl;
6162
class ClassDecl;
6263
class ClangModuleLoader;
6364
class DependentMemberType;
@@ -5489,6 +5490,23 @@ BEGIN_CAN_TYPE_WRAPPER(ExistentialType, Type)
54895490
PROXY_CAN_TYPE_SIMPLE_GETTER(getConstraintType)
54905491
END_CAN_TYPE_WRAPPER(ExistentialType, Type)
54915492

5493+
5494+
/// BuiltinTupleType - A singleton nominal type which serves as the declared
5495+
/// interface type of Builtin.TheTupleType.
5496+
class BuiltinTupleType : public NominalType {
5497+
public:
5498+
// Implement isa/cast/dyncast/etc.
5499+
static bool classof(const TypeBase *T) {
5500+
return T->getKind() == TypeKind::BuiltinTuple;
5501+
}
5502+
5503+
private:
5504+
friend class ASTContext;
5505+
BuiltinTupleType(BuiltinTupleDecl *TheDecl, const ASTContext &Ctx);
5506+
};
5507+
BEGIN_CAN_TYPE_WRAPPER(BuiltinTupleType, NominalType)
5508+
END_CAN_TYPE_WRAPPER(BuiltinTupleType, NominalType)
5509+
54925510
/// LValueType - An l-value is a handle to a physical object. The
54935511
/// type of that object uniquely determines the type of an l-value
54945512
/// for it.

lib/AST/ASTContext.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,12 @@ struct ASTContext::Implementation {
552552

553553
/// Memory allocation arena for the term rewriting system.
554554
std::unique_ptr<rewriting::RewriteContext> TheRewriteContext;
555+
556+
/// The singleton Builtin.TheTupleType.
557+
BuiltinTupleDecl *TheTupleTypeDecl = nullptr;
558+
559+
/// The declared interface type of Builtin.TheTupleType.
560+
BuiltinTupleType *TheTupleType = nullptr;
555561
};
556562

557563
ASTContext::Implementation::Implementation()
@@ -4511,6 +4517,11 @@ Type ExistentialType::get(Type constraint) {
45114517
properties);
45124518
}
45134519

4520+
BuiltinTupleType::BuiltinTupleType(BuiltinTupleDecl *TheDecl,
4521+
const ASTContext &Ctx)
4522+
: NominalType(TypeKind::BuiltinTuple, &Ctx, TheDecl, Type(),
4523+
RecursiveTypeProperties()) { }
4524+
45144525
LValueType *LValueType::get(Type objectTy) {
45154526
assert(!objectTy->is<LValueType>() && !objectTy->is<InOutType>() &&
45164527
"cannot have 'inout' or @lvalue wrapped inside an @lvalue");
@@ -5914,3 +5925,30 @@ bool ASTContext::isASCIIString(StringRef s) const {
59145925
}
59155926
return true;
59165927
}
5928+
5929+
/// The special Builtin.TheTupleType, which parents tuple extensions and
5930+
/// conformances.
5931+
BuiltinTupleDecl *ASTContext::getBuiltinTupleDecl() {
5932+
auto &result = getImpl().TheTupleTypeDecl;
5933+
5934+
if (result)
5935+
return result;
5936+
5937+
result = new (*this) BuiltinTupleDecl(Id_TheTupleType,
5938+
TheBuiltinModule->getFiles()[0]);
5939+
result->setAccess(AccessLevel::Public);
5940+
5941+
return result;
5942+
}
5943+
5944+
/// The declared interface type of Builtin.TheTupleType.
5945+
BuiltinTupleType *ASTContext::getBuiltinTupleType() {
5946+
auto &result = getImpl().TheTupleType;
5947+
5948+
if (result)
5949+
return result;
5950+
5951+
result = new (*this) BuiltinTupleType(getBuiltinTupleDecl(), *this);
5952+
5953+
return result;
5954+
}

lib/AST/ASTDumper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,11 @@ namespace {
847847
printCommonPost(CD);
848848
}
849849

850+
void visitBuiltinTupleDecl(BuiltinTupleDecl *BTD) {
851+
printCommon(BTD, "builtin_tuple_decl");
852+
printCommonPost(BTD);
853+
}
854+
850855
void visitPatternBindingDecl(PatternBindingDecl *PBD) {
851856
printCommon(PBD, "pattern_binding_decl");
852857

@@ -3800,6 +3805,12 @@ namespace {
38003805
PrintWithColorRAII(OS, ParenthesisColor) << ')';
38013806
}
38023807

3808+
void visitBuiltinTupleType(BuiltinTupleType *T, StringRef label) {
3809+
printCommon(label, "builtin_tuple_type");
3810+
printField("decl", T->getDecl()->printRef());
3811+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
3812+
}
3813+
38033814
void visitMetatypeType(MetatypeType *T, StringRef label) {
38043815
printCommon(label, "metatype_type");
38053816
if (T->hasRepresentation())

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13241324
return appendType(ET->getConstraintType(), sig, forDecl);
13251325
}
13261326

1327+
case TypeKind::BuiltinTuple:
1328+
llvm_unreachable("Not implemented");
1329+
13271330
case TypeKind::UnboundGeneric:
13281331
case TypeKind::Class:
13291332
case TypeKind::Enum:
@@ -2554,6 +2557,8 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
25542557
case DeclKind::Struct:
25552558
appendOperator("V");
25562559
break;
2560+
case DeclKind::BuiltinTuple:
2561+
llvm_unreachable("Not implemented");
25572562
}
25582563
}
25592564

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,11 @@ void PrintAST::visitProtocolDecl(ProtocolDecl *decl) {
36733673
Options.BracketOptions.shouldCloseNominal(decl));
36743674
}
36753675
}
3676+
3677+
void PrintAST::visitBuiltinTupleDecl(BuiltinTupleDecl *decl) {
3678+
llvm_unreachable("Not implemented");
3679+
}
3680+
36763681
static bool isStructOrClassContext(DeclContext *dc) {
36773682
auto *nominal = dc->getSelfNominalTypeDecl();
36783683
if (nominal == nullptr)
@@ -6334,6 +6339,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
63346339
}
63356340
}
63366341

6342+
void visitBuiltinTupleType(BuiltinTupleType *T) {
6343+
printQualifiedType(T);
6344+
}
6345+
63376346
void visitLValueType(LValueType *T) {
63386347
Printer << "@lvalue ";
63396348
visit(T->getObjectType());

0 commit comments

Comments
 (0)