Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 84 additions & 52 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,50 @@ IntrinsicInfo::getOrCreateAttributes(ASTContext &Ctx) const {
}

Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
if (Name == "FixedArray") {
return BuiltinUnboundGenericType::get(TypeKind::BuiltinFixedArray, Context);
// We first map to a kind using a StringSwitch so that we can perform an
// exhaustive switch making it easier to know to update this code.
auto kind =
llvm::StringSwitch<std::optional<BuiltinTypeKind>>(Name)
.Case("FixedArray", BuiltinTypeKind::BuiltinFixedArray)
.StartsWith("Vec", BuiltinTypeKind::BuiltinVector)
.Case("RawPointer", BuiltinTypeKind::BuiltinRawPointer)
.Case("RawUnsafeContinuation",
BuiltinTypeKind::BuiltinRawUnsafeContinuation)
.Case("Job", BuiltinTypeKind::BuiltinJob)
.Case("DefaultActorStorage",
BuiltinTypeKind::BuiltinDefaultActorStorage)
.Case("NonDefaultDistributedActorStorage",
BuiltinTypeKind::BuiltinNonDefaultDistributedActorStorage)
.Case("Executor", BuiltinTypeKind::BuiltinExecutor)
.Case("NativeObject", BuiltinTypeKind::BuiltinNativeObject)
.Case("BridgeObject", BuiltinTypeKind::BuiltinBridgeObject)
.Case("UnsafeValueBuffer", BuiltinTypeKind::BuiltinUnsafeValueBuffer)
.Case("PackIndex", BuiltinTypeKind::BuiltinPackIndex)
.StartsWith("FP", BuiltinTypeKind::BuiltinFloat)
.Case("Word", BuiltinTypeKind::BuiltinInteger)
.Case("IntLiteral", BuiltinTypeKind::BuiltinIntegerLiteral)
.StartsWith("Int", BuiltinTypeKind::BuiltinInteger)
.Default({});

// Handle types that are not BuiltinTypeKinds.
if (!kind) {
if (Name == "SILToken")
return Context.TheSILTokenType;

// AnyObject is the empty class-constrained existential.
if (Name == "AnyObject")
return CanType(ProtocolCompositionType::theAnyObjectType(Context));

return Type();
}

// Vectors are VecNxT, where "N" is the number of elements and
// T is the element type.
if (Name.starts_with("Vec")) {
switch (*kind) {
case BuiltinTypeKind::BuiltinFixedArray:
return BuiltinUnboundGenericType::get(TypeKind::BuiltinFixedArray, Context);

case BuiltinTypeKind::BuiltinVector: {
// Vectors are VecNxT, where "N" is the number of elements and
// T is the element type.
Name = Name.substr(3);
StringRef::size_type xPos = Name.find('x');
if (xPos == StringRef::npos)
Expand All @@ -82,67 +119,62 @@ Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {

return BuiltinVectorType::get(Context, elementType, numElements);
}

if (Name == "RawPointer")
case BuiltinTypeKind::BuiltinRawPointer:
return Context.TheRawPointerType;
if (Name == "RawUnsafeContinuation")
case BuiltinTypeKind::BuiltinRawUnsafeContinuation:
return Context.TheRawUnsafeContinuationType;
if (Name == "Job")
case BuiltinTypeKind::BuiltinJob:
return Context.TheJobType;
if (Name == "DefaultActorStorage")
case BuiltinTypeKind::BuiltinDefaultActorStorage:
return Context.TheDefaultActorStorageType;
if (Name == "NonDefaultDistributedActorStorage")
case BuiltinTypeKind::BuiltinNonDefaultDistributedActorStorage:
return Context.TheNonDefaultDistributedActorStorageType;
if (Name == "Executor")
case BuiltinTypeKind::BuiltinExecutor:
return Context.TheExecutorType;
if (Name == "NativeObject")
case BuiltinTypeKind::BuiltinNativeObject:
return Context.TheNativeObjectType;
if (Name == "BridgeObject")
case BuiltinTypeKind::BuiltinBridgeObject:
return Context.TheBridgeObjectType;
if (Name == "SILToken")
return Context.TheSILTokenType;
if (Name == "UnsafeValueBuffer")
case BuiltinTypeKind::BuiltinUnsafeValueBuffer:
return Context.TheUnsafeValueBufferType;
if (Name == "PackIndex")
case BuiltinTypeKind::BuiltinPackIndex:
return Context.ThePackIndexType;

if (Name == "FPIEEE32")
return Context.TheIEEE32Type;
if (Name == "FPIEEE64")
return Context.TheIEEE64Type;
case BuiltinTypeKind::BuiltinFloat:
// Target specific FP types.
if (Name == "FPIEEE32")
return Context.TheIEEE32Type;
if (Name == "FPIEEE64")
return Context.TheIEEE64Type;
if (Name == "FPIEEE16")
return Context.TheIEEE16Type;
if (Name == "FPIEEE80")
return Context.TheIEEE80Type;
if (Name == "FPIEEE128")
return Context.TheIEEE128Type;
if (Name == "FPPPC128")
return Context.ThePPC128Type;
return Type();
case BuiltinTypeKind::BuiltinInteger:
if (Name == "Word")
return BuiltinIntegerType::getWordType(Context);

if (Name == "Word")
return BuiltinIntegerType::getWordType(Context);
if (Name == "Int") {
return BuiltinUnboundGenericType::get(TypeKind::BuiltinInteger, Context);
}

if (Name == "IntLiteral")
// Handle 'int8' and friends.
if (Name.substr(0, 3) == "Int") {
unsigned BitWidth;
if (!Name.substr(3).getAsInteger(10, BitWidth) && BitWidth <= 2048 &&
BitWidth != 0) // Cap to prevent unsound things.
return BuiltinIntegerType::get(BitWidth, Context);
}
return Type();
case BuiltinTypeKind::BuiltinIntegerLiteral:
return Context.TheIntegerLiteralType;

if (Name == "Int") {
return BuiltinUnboundGenericType::get(TypeKind::BuiltinInteger, Context);
}

// Handle 'int8' and friends.
if (Name.substr(0, 3) == "Int") {
unsigned BitWidth;
if (!Name.substr(3).getAsInteger(10, BitWidth) &&
BitWidth <= 2048 && BitWidth != 0) // Cap to prevent unsound things.
return BuiltinIntegerType::get(BitWidth, Context);
case BuiltinTypeKind::BuiltinUnboundGeneric:
return Type();
}

// Target specific FP types.
if (Name == "FPIEEE16")
return Context.TheIEEE16Type;
if (Name == "FPIEEE80")
return Context.TheIEEE80Type;
if (Name == "FPIEEE128")
return Context.TheIEEE128Type;
if (Name == "FPPPC128")
return Context.ThePPC128Type;

// AnyObject is the empty class-constrained existential.
if (Name == "AnyObject")
return CanType(
ProtocolCompositionType::theAnyObjectType(Context));

return Type();
}
Expand Down