Skip to content

Commit 0b75992

Browse files
authored
Merge pull request swiftlang#83853 from gottesmm/pr-21e0361deb74f2e659fa9c4c8e4f4d001d80c141
[ast] Convert swift::getBuiltinType to use a covered switch for BuiltinTypeKind
2 parents 720406f + 05536d7 commit 0b75992

File tree

1 file changed

+84
-52
lines changed

1 file changed

+84
-52
lines changed

lib/AST/Builtins.cpp

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,50 @@ IntrinsicInfo::getOrCreateAttributes(ASTContext &Ctx) const {
5959
}
6060

6161
Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
62-
if (Name == "FixedArray") {
63-
return BuiltinUnboundGenericType::get(TypeKind::BuiltinFixedArray, Context);
62+
// We first map to a kind using a StringSwitch so that we can perform an
63+
// exhaustive switch making it easier to know to update this code.
64+
auto kind =
65+
llvm::StringSwitch<std::optional<BuiltinTypeKind>>(Name)
66+
.Case("FixedArray", BuiltinTypeKind::BuiltinFixedArray)
67+
.StartsWith("Vec", BuiltinTypeKind::BuiltinVector)
68+
.Case("RawPointer", BuiltinTypeKind::BuiltinRawPointer)
69+
.Case("RawUnsafeContinuation",
70+
BuiltinTypeKind::BuiltinRawUnsafeContinuation)
71+
.Case("Job", BuiltinTypeKind::BuiltinJob)
72+
.Case("DefaultActorStorage",
73+
BuiltinTypeKind::BuiltinDefaultActorStorage)
74+
.Case("NonDefaultDistributedActorStorage",
75+
BuiltinTypeKind::BuiltinNonDefaultDistributedActorStorage)
76+
.Case("Executor", BuiltinTypeKind::BuiltinExecutor)
77+
.Case("NativeObject", BuiltinTypeKind::BuiltinNativeObject)
78+
.Case("BridgeObject", BuiltinTypeKind::BuiltinBridgeObject)
79+
.Case("UnsafeValueBuffer", BuiltinTypeKind::BuiltinUnsafeValueBuffer)
80+
.Case("PackIndex", BuiltinTypeKind::BuiltinPackIndex)
81+
.StartsWith("FP", BuiltinTypeKind::BuiltinFloat)
82+
.Case("Word", BuiltinTypeKind::BuiltinInteger)
83+
.Case("IntLiteral", BuiltinTypeKind::BuiltinIntegerLiteral)
84+
.StartsWith("Int", BuiltinTypeKind::BuiltinInteger)
85+
.Default({});
86+
87+
// Handle types that are not BuiltinTypeKinds.
88+
if (!kind) {
89+
if (Name == "SILToken")
90+
return Context.TheSILTokenType;
91+
92+
// AnyObject is the empty class-constrained existential.
93+
if (Name == "AnyObject")
94+
return CanType(ProtocolCompositionType::theAnyObjectType(Context));
95+
96+
return Type();
6497
}
6598

66-
// Vectors are VecNxT, where "N" is the number of elements and
67-
// T is the element type.
68-
if (Name.starts_with("Vec")) {
99+
switch (*kind) {
100+
case BuiltinTypeKind::BuiltinFixedArray:
101+
return BuiltinUnboundGenericType::get(TypeKind::BuiltinFixedArray, Context);
102+
103+
case BuiltinTypeKind::BuiltinVector: {
104+
// Vectors are VecNxT, where "N" is the number of elements and
105+
// T is the element type.
69106
Name = Name.substr(3);
70107
StringRef::size_type xPos = Name.find('x');
71108
if (xPos == StringRef::npos)
@@ -82,67 +119,62 @@ Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
82119

83120
return BuiltinVectorType::get(Context, elementType, numElements);
84121
}
85-
86-
if (Name == "RawPointer")
122+
case BuiltinTypeKind::BuiltinRawPointer:
87123
return Context.TheRawPointerType;
88-
if (Name == "RawUnsafeContinuation")
124+
case BuiltinTypeKind::BuiltinRawUnsafeContinuation:
89125
return Context.TheRawUnsafeContinuationType;
90-
if (Name == "Job")
126+
case BuiltinTypeKind::BuiltinJob:
91127
return Context.TheJobType;
92-
if (Name == "DefaultActorStorage")
128+
case BuiltinTypeKind::BuiltinDefaultActorStorage:
93129
return Context.TheDefaultActorStorageType;
94-
if (Name == "NonDefaultDistributedActorStorage")
130+
case BuiltinTypeKind::BuiltinNonDefaultDistributedActorStorage:
95131
return Context.TheNonDefaultDistributedActorStorageType;
96-
if (Name == "Executor")
132+
case BuiltinTypeKind::BuiltinExecutor:
97133
return Context.TheExecutorType;
98-
if (Name == "NativeObject")
134+
case BuiltinTypeKind::BuiltinNativeObject:
99135
return Context.TheNativeObjectType;
100-
if (Name == "BridgeObject")
136+
case BuiltinTypeKind::BuiltinBridgeObject:
101137
return Context.TheBridgeObjectType;
102-
if (Name == "SILToken")
103-
return Context.TheSILTokenType;
104-
if (Name == "UnsafeValueBuffer")
138+
case BuiltinTypeKind::BuiltinUnsafeValueBuffer:
105139
return Context.TheUnsafeValueBufferType;
106-
if (Name == "PackIndex")
140+
case BuiltinTypeKind::BuiltinPackIndex:
107141
return Context.ThePackIndexType;
108-
109-
if (Name == "FPIEEE32")
110-
return Context.TheIEEE32Type;
111-
if (Name == "FPIEEE64")
112-
return Context.TheIEEE64Type;
142+
case BuiltinTypeKind::BuiltinFloat:
143+
// Target specific FP types.
144+
if (Name == "FPIEEE32")
145+
return Context.TheIEEE32Type;
146+
if (Name == "FPIEEE64")
147+
return Context.TheIEEE64Type;
148+
if (Name == "FPIEEE16")
149+
return Context.TheIEEE16Type;
150+
if (Name == "FPIEEE80")
151+
return Context.TheIEEE80Type;
152+
if (Name == "FPIEEE128")
153+
return Context.TheIEEE128Type;
154+
if (Name == "FPPPC128")
155+
return Context.ThePPC128Type;
156+
return Type();
157+
case BuiltinTypeKind::BuiltinInteger:
158+
if (Name == "Word")
159+
return BuiltinIntegerType::getWordType(Context);
113160

114-
if (Name == "Word")
115-
return BuiltinIntegerType::getWordType(Context);
161+
if (Name == "Int") {
162+
return BuiltinUnboundGenericType::get(TypeKind::BuiltinInteger, Context);
163+
}
116164

117-
if (Name == "IntLiteral")
165+
// Handle 'int8' and friends.
166+
if (Name.substr(0, 3) == "Int") {
167+
unsigned BitWidth;
168+
if (!Name.substr(3).getAsInteger(10, BitWidth) && BitWidth <= 2048 &&
169+
BitWidth != 0) // Cap to prevent unsound things.
170+
return BuiltinIntegerType::get(BitWidth, Context);
171+
}
172+
return Type();
173+
case BuiltinTypeKind::BuiltinIntegerLiteral:
118174
return Context.TheIntegerLiteralType;
119-
120-
if (Name == "Int") {
121-
return BuiltinUnboundGenericType::get(TypeKind::BuiltinInteger, Context);
122-
}
123-
124-
// Handle 'int8' and friends.
125-
if (Name.substr(0, 3) == "Int") {
126-
unsigned BitWidth;
127-
if (!Name.substr(3).getAsInteger(10, BitWidth) &&
128-
BitWidth <= 2048 && BitWidth != 0) // Cap to prevent unsound things.
129-
return BuiltinIntegerType::get(BitWidth, Context);
175+
case BuiltinTypeKind::BuiltinUnboundGeneric:
176+
return Type();
130177
}
131-
132-
// Target specific FP types.
133-
if (Name == "FPIEEE16")
134-
return Context.TheIEEE16Type;
135-
if (Name == "FPIEEE80")
136-
return Context.TheIEEE80Type;
137-
if (Name == "FPIEEE128")
138-
return Context.TheIEEE128Type;
139-
if (Name == "FPPPC128")
140-
return Context.ThePPC128Type;
141-
142-
// AnyObject is the empty class-constrained existential.
143-
if (Name == "AnyObject")
144-
return CanType(
145-
ProtocolCompositionType::theAnyObjectType(Context));
146178

147179
return Type();
148180
}

0 commit comments

Comments
 (0)