Skip to content

Commit f952ff0

Browse files
committed
[ast] Move printing code from ASTPrinter onto a helper on BuiltinType and have ASTPrinter call that instead.
1 parent 918a224 commit f952ff0

File tree

8 files changed

+263
-149
lines changed

8 files changed

+263
-149
lines changed

include/swift/AST/Types.h

+61-42
Original file line numberDiff line numberDiff line change
@@ -40,54 +40,60 @@
4040
#include "llvm/Support/TrailingObjects.h"
4141

4242
namespace llvm {
43-
struct fltSemantics;
44-
}
43+
struct fltSemantics;
44+
} // namespace llvm
45+
4546
namespace swift {
46-
enum class AllocationArena;
47-
class ArchetypeType;
48-
class AssociatedTypeDecl;
49-
class ASTContext;
50-
class ClassDecl;
51-
class DependentMemberType;
52-
class GenericTypeParamDecl;
53-
class GenericTypeParamType;
54-
class GenericParamList;
55-
class GenericSignature;
56-
class GenericSignatureBuilder;
57-
class Identifier;
58-
class InOutType;
59-
class OpaqueTypeDecl;
60-
class OpenedArchetypeType;
61-
enum class ReferenceCounting : uint8_t;
62-
enum class ResilienceExpansion : unsigned;
63-
class SILModule;
64-
class SILType;
65-
class TypeAliasDecl;
66-
class TypeDecl;
67-
class NominalTypeDecl;
68-
class GenericTypeDecl;
69-
class EnumDecl;
70-
class EnumElementDecl;
71-
class StructDecl;
72-
class ProtocolDecl;
73-
class TypeVariableType;
74-
class ValueDecl;
75-
class ModuleDecl;
76-
class ModuleType;
77-
class ProtocolConformance;
78-
enum PointerTypeKind : unsigned;
79-
struct ValueOwnershipKind;
80-
81-
enum class TypeKind : uint8_t {
47+
48+
enum class AllocationArena;
49+
class ArchetypeType;
50+
class AssociatedTypeDecl;
51+
class ASTContext;
52+
class ClassDecl;
53+
class DependentMemberType;
54+
class GenericTypeParamDecl;
55+
class GenericTypeParamType;
56+
class GenericParamList;
57+
class GenericSignature;
58+
class GenericSignatureBuilder;
59+
class Identifier;
60+
class InOutType;
61+
class OpaqueTypeDecl;
62+
class OpenedArchetypeType;
63+
enum class ReferenceCounting : uint8_t;
64+
enum class ResilienceExpansion : unsigned;
65+
class SILModule;
66+
class SILType;
67+
class TypeAliasDecl;
68+
class TypeDecl;
69+
class NominalTypeDecl;
70+
class GenericTypeDecl;
71+
class EnumDecl;
72+
class EnumElementDecl;
73+
class StructDecl;
74+
class ProtocolDecl;
75+
class TypeVariableType;
76+
class ValueDecl;
77+
class ModuleDecl;
78+
class ModuleType;
79+
class ProtocolConformance;
80+
enum PointerTypeKind : unsigned;
81+
struct ValueOwnershipKind;
82+
83+
enum class TypeKind : uint8_t {
8284
#define TYPE(id, parent) id,
8385
#define LAST_TYPE(id) Last_Type = id,
8486
#define TYPE_RANGE(Id, FirstId, LastId) \
8587
First_##Id##Type = FirstId, Last_##Id##Type = LastId,
8688
#include "swift/AST/TypeNodes.def"
87-
};
88-
enum : unsigned { NumTypeKindBits =
89-
countBitsUsed(static_cast<unsigned>(TypeKind::Last_Type)) };
90-
89+
};
90+
91+
enum : unsigned {
92+
NumTypeKindBits = countBitsUsed(static_cast<unsigned>(TypeKind::Last_Type))
93+
};
94+
95+
enum class BuiltinTypeKind : std::underlying_type<TypeKind>::type;
96+
9197
/// Various properties of types that are primarily defined recursively
9298
/// on structural types.
9399
class RecursiveTypeProperties {
@@ -1244,6 +1250,19 @@ class BuiltinType : public TypeBase {
12441250
return T->getKind() >= TypeKind::First_BuiltinType &&
12451251
T->getKind() <= TypeKind::Last_BuiltinType;
12461252
}
1253+
1254+
/// Return the "canonical" name for this builtin. E.x.:
1255+
///
1256+
/// BuiltinRawPointerType -> BUILTIN_TYPE_NAME_RAWPOINTER ->
1257+
/// Builtin.RawPointer.
1258+
///
1259+
/// If \p prependBuiltinNamespace is set to true, "Builtin." is left as a
1260+
/// prefix on the name. This is the default behavior. If the user asks, we
1261+
/// strip off the builtin prefix.
1262+
StringRef getTypeName(SmallVectorImpl<char> &result,
1263+
bool prependBuiltinNamespace = true) const;
1264+
1265+
BuiltinTypeKind getBuiltinTypeKind() const;
12471266
};
12481267
DEFINE_EMPTY_CAN_TYPE_WRAPPER(BuiltinType, Type)
12491268

include/swift/Strings.h

+70-35
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,104 @@
1919
namespace swift {
2020

2121
/// The name of the standard library, which is a reserved module name.
22-
constexpr static const char STDLIB_NAME[] = "Swift";
22+
constexpr static const StringLiteral STDLIB_NAME = "Swift";
2323
/// The name of the Onone support library, which is a reserved module name.
24-
constexpr static const char SWIFT_ONONE_SUPPORT[] = "SwiftOnoneSupport";
24+
constexpr static const StringLiteral SWIFT_ONONE_SUPPORT = "SwiftOnoneSupport";
2525
/// The name of the SwiftShims module, which contains private stdlib decls.
26-
constexpr static const char SWIFT_SHIMS_NAME[] = "SwiftShims";
26+
constexpr static const StringLiteral SWIFT_SHIMS_NAME = "SwiftShims";
2727
/// The name of the Builtin module, which contains Builtin functions.
28-
constexpr static const char BUILTIN_NAME[] = "Builtin";
28+
constexpr static const StringLiteral BUILTIN_NAME = "Builtin";
2929
/// The prefix of module names used by LLDB to capture Swift expressions
30-
constexpr static const char LLDB_EXPRESSIONS_MODULE_NAME_PREFIX[] =
30+
constexpr static const StringLiteral LLDB_EXPRESSIONS_MODULE_NAME_PREFIX =
3131
"__lldb_expr_";
3232

3333
/// The name of the fake module used to hold imported Objective-C things.
34-
constexpr static const char MANGLING_MODULE_OBJC[] = "__C";
34+
constexpr static const StringLiteral MANGLING_MODULE_OBJC = "__C";
3535
/// The name of the fake module used to hold synthesized ClangImporter things.
36-
constexpr static const char MANGLING_MODULE_CLANG_IMPORTER[] =
36+
constexpr static const StringLiteral MANGLING_MODULE_CLANG_IMPORTER =
3737
"__C_Synthesized";
3838

39+
constexpr static const StringLiteral SEMANTICS_PROGRAMTERMINATION_POINT =
40+
"programtermination_point";
41+
3942
/// The name of the Builtin type prefix
40-
constexpr static const char BUILTIN_TYPE_NAME_PREFIX[] = "Builtin.";
43+
constexpr static const StringLiteral BUILTIN_TYPE_NAME_PREFIX = "Builtin.";
44+
45+
/// A composition class containing a StringLiteral for the names of
46+
/// Swift builtins. The reason we use this is to ensure that we when
47+
/// necessary slice off the "Builtin." prefix from these names in a
48+
/// constexpr way from a global constant string.
49+
///
50+
/// NOTE: StringLiteral is a weird class to compose with. For this to
51+
/// work properly, one must always initialize these classes using an
52+
/// initializer list as shown below.
53+
struct BuiltinNameStringLiteral {
54+
const StringLiteral literal;
55+
56+
constexpr operator StringRef() const { return literal; }
57+
constexpr const StringRef getWithoutPrefix() const {
58+
return literal.drop_front(BUILTIN_TYPE_NAME_PREFIX.size());
59+
}
60+
};
61+
4162
/// The name of the Builtin type for Int
42-
constexpr static const char BUILTIN_TYPE_NAME_INT[] = "Builtin.Int";
63+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT = {
64+
"Builtin.Int"};
4365
/// The name of the Builtin type for Int8
44-
constexpr static const char BUILTIN_TYPE_NAME_INT8[] = "Builtin.Int8";
66+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT8 = {
67+
"Builtin.Int8"};
4568
/// The name of the Builtin type for Int16
46-
constexpr static const char BUILTIN_TYPE_NAME_INT16[] = "Builtin.Int16";
69+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT16 = {
70+
"Builtin.Int16"};
4771
/// The name of the Builtin type for Int32
48-
constexpr static const char BUILTIN_TYPE_NAME_INT32[] = "Builtin.Int32";
72+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT32 = {
73+
"Builtin.Int32"};
4974
/// The name of the Builtin type for Int64
50-
constexpr static const char BUILTIN_TYPE_NAME_INT64[] = "Builtin.Int64";
75+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT64 = {
76+
"Builtin.Int64"};
5177
/// The name of the Builtin type for Int128
52-
constexpr static const char BUILTIN_TYPE_NAME_INT128[] = "Builtin.Int128";
78+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT128 = {
79+
"Builtin.Int128"};
5380
/// The name of the Builtin type for Int256
54-
constexpr static const char BUILTIN_TYPE_NAME_INT256[] = "Builtin.Int256";
81+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT256 = {
82+
"Builtin.Int256"};
5583
/// The name of the Builtin type for Int512
56-
constexpr static const char BUILTIN_TYPE_NAME_INT512[] = "Builtin.Int512";
84+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INT512 = {
85+
"Builtin.Int512"};
5786
/// The name of the Builtin type for IntLiteral
58-
constexpr static const char BUILTIN_TYPE_NAME_INTLITERAL[] =
59-
"Builtin.IntLiteral";
60-
/// The name of the Builtin type for Float
61-
constexpr static const char BUILTIN_TYPE_NAME_FLOAT[] = "Builtin.FPIEEE";
87+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_INTLITERAL = {
88+
"Builtin.IntLiteral"};
89+
/// The name of the Builtin type for IEEE Floating point types.
90+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_FLOAT = {
91+
"Builtin.FPIEEE"};
92+
// The name of the builtin type for power pc specific floating point types.
93+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_FLOAT_PPC = {
94+
"Builtin.FPPPC"};
6295
/// The name of the Builtin type for NativeObject
63-
constexpr static const char BUILTIN_TYPE_NAME_NATIVEOBJECT[] =
64-
"Builtin.NativeObject";
96+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_NATIVEOBJECT = {
97+
"Builtin.NativeObject"};
6598
/// The name of the Builtin type for BridgeObject
66-
constexpr static const char BUILTIN_TYPE_NAME_BRIDGEOBJECT[] =
67-
"Builtin.BridgeObject";
99+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_BRIDGEOBJECT = {
100+
"Builtin.BridgeObject"};
68101
/// The name of the Builtin type for RawPointer
69-
constexpr static const char BUILTIN_TYPE_NAME_RAWPOINTER[] =
70-
"Builtin.RawPointer";
102+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_RAWPOINTER = {
103+
"Builtin.RawPointer"};
71104
/// The name of the Builtin type for UnsafeValueBuffer
72-
constexpr static const char BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER[] =
73-
"Builtin.UnsafeValueBuffer";
105+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER =
106+
{"Builtin.UnsafeValueBuffer"};
74107
/// The name of the Builtin type for UnknownObject
75-
constexpr static const char BUILTIN_TYPE_NAME_UNKNOWNOBJECT[] =
76-
"Builtin.UnknownObject";
108+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_UNKNOWNOBJECT = {
109+
"Builtin.UnknownObject"};
77110
/// The name of the Builtin type for Vector
78-
constexpr static const char BUILTIN_TYPE_NAME_VEC[] = "Builtin.Vec";
111+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_VEC = {
112+
"Builtin.Vec"};
79113
/// The name of the Builtin type for SILToken
80-
constexpr static const char BUILTIN_TYPE_NAME_SILTOKEN[] = "Builtin.SILToken";
114+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_SILTOKEN = {
115+
"Builtin.SILToken"};
81116
/// The name of the Builtin type for Word
82-
constexpr static const char BUILTIN_TYPE_NAME_WORD[] = "Builtin.Word";
83-
constexpr static StringLiteral SEMANTICS_PROGRAMTERMINATION_POINT =
84-
"programtermination_point";
117+
constexpr static BuiltinNameStringLiteral BUILTIN_TYPE_NAME_WORD = {
118+
"Builtin.Word"};
119+
85120
} // end namespace swift
86121

87122
#endif // SWIFT_STRINGS_H

lib/AST/ASTDemangler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ ASTBuilder::createBuiltinType(StringRef builtinName,
102102

103103
ModuleDecl::AccessPathTy accessPath;
104104
StringRef strippedName =
105-
builtinName.drop_front(strlen(BUILTIN_TYPE_NAME_PREFIX));
105+
builtinName.drop_front(BUILTIN_TYPE_NAME_PREFIX.size());
106106
Ctx.TheBuiltinModule->lookupValue(accessPath,
107107
Ctx.getIdentifier(strippedName),
108108
NLKind::QualifiedLookup,

lib/AST/ASTPrinter.cpp

+20-63
Original file line numberDiff line numberDiff line change
@@ -3576,69 +3576,26 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
35763576
Printer << "_";
35773577
}
35783578

3579-
void visitBuiltinRawPointerType(BuiltinRawPointerType *T) {
3580-
Printer << BUILTIN_TYPE_NAME_RAWPOINTER;
3581-
}
3582-
3583-
void visitBuiltinNativeObjectType(BuiltinNativeObjectType *T) {
3584-
Printer << BUILTIN_TYPE_NAME_NATIVEOBJECT;
3585-
}
3586-
3587-
void visitBuiltinUnknownObjectType(BuiltinUnknownObjectType *T) {
3588-
Printer << BUILTIN_TYPE_NAME_UNKNOWNOBJECT;
3589-
}
3590-
3591-
void visitBuiltinBridgeObjectType(BuiltinBridgeObjectType *T) {
3592-
Printer << BUILTIN_TYPE_NAME_BRIDGEOBJECT;
3593-
}
3594-
3595-
void visitBuiltinUnsafeValueBufferType(BuiltinUnsafeValueBufferType *T) {
3596-
Printer << BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER;
3597-
}
3598-
3599-
void visitBuiltinIntegerLiteralType(BuiltinIntegerLiteralType *T) {
3600-
Printer << BUILTIN_TYPE_NAME_INTLITERAL;
3601-
}
3602-
3603-
void visitBuiltinVectorType(BuiltinVectorType *T) {
3604-
llvm::SmallString<32> UnderlyingStrVec;
3605-
StringRef UnderlyingStr;
3606-
{
3607-
// FIXME: Ugly hack: remove the .Builtin from the element type.
3608-
{
3609-
llvm::raw_svector_ostream UnderlyingOS(UnderlyingStrVec);
3610-
T->getElementType().print(UnderlyingOS);
3611-
}
3612-
if (UnderlyingStrVec.startswith(BUILTIN_TYPE_NAME_PREFIX))
3613-
UnderlyingStr = UnderlyingStrVec.substr(8);
3614-
else
3615-
UnderlyingStr = UnderlyingStrVec;
3616-
}
3617-
3618-
Printer << BUILTIN_TYPE_NAME_VEC << T->getNumElements() << "x" << UnderlyingStr;
3619-
}
3620-
3621-
void visitBuiltinIntegerType(BuiltinIntegerType *T) {
3622-
auto width = T->getWidth();
3623-
if (width.isFixedWidth()) {
3624-
Printer << BUILTIN_TYPE_NAME_INT << width.getFixedWidth();
3625-
} else if (width.isPointerWidth()) {
3626-
Printer << BUILTIN_TYPE_NAME_WORD;
3627-
} else {
3628-
llvm_unreachable("impossible bit width");
3629-
}
3630-
}
3631-
3632-
void visitBuiltinFloatType(BuiltinFloatType *T) {
3633-
switch (T->getFPKind()) {
3634-
case BuiltinFloatType::IEEE16: Printer << "Builtin.FPIEEE16"; return;
3635-
case BuiltinFloatType::IEEE32: Printer << "Builtin.FPIEEE32"; return;
3636-
case BuiltinFloatType::IEEE64: Printer << "Builtin.FPIEEE64"; return;
3637-
case BuiltinFloatType::IEEE80: Printer << "Builtin.FPIEEE80"; return;
3638-
case BuiltinFloatType::IEEE128: Printer << "Builtin.FPIEEE128"; return;
3639-
case BuiltinFloatType::PPC128: Printer << "Builtin.FPPPC128"; return;
3640-
}
3641-
}
3579+
#ifdef ASTPRINTER_HANDLE_BUILTINTYPE
3580+
#error "ASTPRINTER_HANDLE_BUILTINTYPE should not be defined?!"
3581+
#endif
3582+
3583+
#define ASTPRINTER_PRINT_BUILTINTYPE(NAME) \
3584+
void visit##NAME(NAME *T) { \
3585+
SmallString<32> buffer; \
3586+
T->getTypeName(buffer); \
3587+
Printer << buffer; \
3588+
}
3589+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinRawPointerType)
3590+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinNativeObjectType)
3591+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinUnknownObjectType)
3592+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinBridgeObjectType)
3593+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinUnsafeValueBufferType)
3594+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinIntegerLiteralType)
3595+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinVectorType)
3596+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinIntegerType)
3597+
ASTPRINTER_PRINT_BUILTINTYPE(BuiltinFloatType)
3598+
#undef ASTPRINTER_PRINT_BUILTINTYPE
36423599

36433600
void visitSILTokenType(SILTokenType *T) {
36443601
Printer << BUILTIN_TYPE_NAME_SILTOKEN;

0 commit comments

Comments
 (0)