Skip to content

Commit 29788f6

Browse files
authored
Merge pull request #14963 from kitasuke/replace-of-builtin-string-with-constant
2 parents a5956a2 + d433428 commit 29788f6

File tree

8 files changed

+81
-40
lines changed

8 files changed

+81
-40
lines changed

include/swift/Strings.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,52 @@ namespace swift {
3535
static const char SWIFT_ONONE_SUPPORT[] = "SwiftOnoneSupport";
3636
/// The name of the SwiftShims module, which contains private stdlib decls.
3737
static const char SWIFT_SHIMS_NAME[] = "SwiftShims";
38+
/// The name of the Builtin module, which contains Builtin functions.
39+
static const char BUILTIN_NAME[] = "Builtin";
3840
/// The prefix of module names used by LLDB to capture Swift expressions
3941
static const char LLDB_EXPRESSIONS_MODULE_NAME_PREFIX[] = "__lldb_expr_";
4042

4143
/// The name of the fake module used to hold imported Objective-C things.
4244
static const char MANGLING_MODULE_OBJC[] = "__C";
4345
/// The name of the fake module used to hold synthesized ClangImporter things.
4446
static const char MANGLING_MODULE_CLANG_IMPORTER[] = "__C_Synthesized";
47+
48+
/// The name of the Builtin type prefix
49+
static const char BUILTIN_TYPE_NAME_PREFIX[] = "Builtin.";
50+
/// The name of the Builtin type for Int
51+
static const char BUILTIN_TYPE_NAME_INT[] = "Builtin.Int";
52+
/// The name of the Builtin type for Int8
53+
static const char BUILTIN_TYPE_NAME_INT8[] = "Builtin.Int8";
54+
/// The name of the Builtin type for Int16
55+
static const char BUILTIN_TYPE_NAME_INT16[] = "Builtin.Int16";
56+
/// The name of the Builtin type for Int32
57+
static const char BUILTIN_TYPE_NAME_INT32[] = "Builtin.Int32";
58+
/// The name of the Builtin type for Int64
59+
static const char BUILTIN_TYPE_NAME_INT64[] = "Builtin.Int64";
60+
/// The name of the Builtin type for Int128
61+
static const char BUILTIN_TYPE_NAME_INT128[] = "Builtin.Int128";
62+
/// The name of the Builtin type for Int256
63+
static const char BUILTIN_TYPE_NAME_INT256[] = "Builtin.Int256";
64+
/// The name of the Builtin type for Int512
65+
static const char BUILTIN_TYPE_NAME_INT512[] = "Builtin.Int512";
66+
/// The name of the Builtin type for Float
67+
static const char BUILTIN_TYPE_NAME_FLOAT[] = "Builtin.Float";
68+
/// The name of the Builtin type for NativeObject
69+
static const char BUILTIN_TYPE_NAME_NATIVEOBJECT[] = "Builtin.NativeObject";
70+
/// The name of the Builtin type for BridgeObject
71+
static const char BUILTIN_TYPE_NAME_BRIDGEOBJECT[] = "Builtin.BridgeObject";
72+
/// The name of the Builtin type for RawPointer
73+
static const char BUILTIN_TYPE_NAME_RAWPOINTER[] = "Builtin.RawPointer";
74+
/// The name of the Builtin type for UnsafeValueBuffer
75+
static const char BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER[] = "Builtin.UnsafeValueBuffer";
76+
/// The name of the Builtin type for UnknownObject
77+
static const char BUILTIN_TYPE_NAME_UNKNOWNOBJECT[] = "Builtin.UnknownObject";
78+
/// The name of the Builtin type for Vector
79+
static const char BUILTIN_TYPE_NAME_VEC[] = "Builtin.Vec";
80+
/// The name of the Builtin type for SILToken
81+
static const char BUILTIN_TYPE_NAME_SILTOKEN[] = "Builtin.SILToken";
82+
/// The name of the Builtin type for Word
83+
static const char BUILTIN_TYPE_NAME_WORD[] = "Builtin.Word";
4584
} // end namespace swift
4685

4786
#endif // SWIFT_STRINGS_H

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ ConstraintCheckerArenaRAII::~ConstraintCheckerArenaRAII() {
426426
}
427427

428428
static ModuleDecl *createBuiltinModule(ASTContext &ctx) {
429-
auto M = ModuleDecl::create(ctx.getIdentifier("Builtin"), ctx);
429+
auto M = ModuleDecl::create(ctx.getIdentifier(BUILTIN_NAME), ctx);
430430
M->addFile(*new (ctx) BuiltinUnit(*M));
431431
return M;
432432
}

lib/AST/ASTPrinter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,23 +3200,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32003200
}
32013201

32023202
void visitBuiltinRawPointerType(BuiltinRawPointerType *T) {
3203-
Printer << "Builtin.RawPointer";
3203+
Printer << BUILTIN_TYPE_NAME_RAWPOINTER;
32043204
}
32053205

32063206
void visitBuiltinNativeObjectType(BuiltinNativeObjectType *T) {
3207-
Printer << "Builtin.NativeObject";
3207+
Printer << BUILTIN_TYPE_NAME_NATIVEOBJECT;
32083208
}
32093209

32103210
void visitBuiltinUnknownObjectType(BuiltinUnknownObjectType *T) {
3211-
Printer << "Builtin.UnknownObject";
3211+
Printer << BUILTIN_TYPE_NAME_UNKNOWNOBJECT;
32123212
}
32133213

32143214
void visitBuiltinBridgeObjectType(BuiltinBridgeObjectType *T) {
3215-
Printer << "Builtin.BridgeObject";
3215+
Printer << BUILTIN_TYPE_NAME_BRIDGEOBJECT;
32163216
}
32173217

32183218
void visitBuiltinUnsafeValueBufferType(BuiltinUnsafeValueBufferType *T) {
3219-
Printer << "Builtin.UnsafeValueBuffer";
3219+
Printer << BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER;
32203220
}
32213221

32223222
void visitBuiltinVectorType(BuiltinVectorType *T) {
@@ -3228,21 +3228,21 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32283228
llvm::raw_svector_ostream UnderlyingOS(UnderlyingStrVec);
32293229
T->getElementType().print(UnderlyingOS);
32303230
}
3231-
if (UnderlyingStrVec.startswith("Builtin."))
3231+
if (UnderlyingStrVec.startswith(BUILTIN_TYPE_NAME_PREFIX))
32323232
UnderlyingStr = UnderlyingStrVec.substr(8);
32333233
else
32343234
UnderlyingStr = UnderlyingStrVec;
32353235
}
32363236

3237-
Printer << "Builtin.Vec" << T->getNumElements() << "x" << UnderlyingStr;
3237+
Printer << BUILTIN_TYPE_NAME_VEC << T->getNumElements() << "x" << UnderlyingStr;
32383238
}
32393239

32403240
void visitBuiltinIntegerType(BuiltinIntegerType *T) {
32413241
auto width = T->getWidth();
32423242
if (width.isFixedWidth()) {
3243-
Printer << "Builtin.Int" << width.getFixedWidth();
3243+
Printer << BUILTIN_TYPE_NAME_INT << width.getFixedWidth();
32443244
} else if (width.isPointerWidth()) {
3245-
Printer << "Builtin.Word";
3245+
Printer << BUILTIN_TYPE_NAME_WORD;
32463246
} else {
32473247
llvm_unreachable("impossible bit width");
32483248
}
@@ -3260,7 +3260,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32603260
}
32613261

32623262
void visitSILTokenType(SILTokenType *T) {
3263-
Printer << "Builtin.SILToken";
3263+
Printer << BUILTIN_TYPE_NAME_SILTOKEN;
32643264
}
32653265

32663266
void visitNameAliasType(NameAliasType *T) {

lib/Demangling/Demangler.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -923,18 +923,18 @@ NodePointer Demangler::demangleBuiltinType() {
923923
switch (nextChar()) {
924924
case 'b':
925925
Ty = createNode(Node::Kind::BuiltinTypeName,
926-
"Builtin.BridgeObject");
926+
BUILTIN_TYPE_NAME_BRIDGEOBJECT);
927927
break;
928928
case 'B':
929929
Ty = createNode(Node::Kind::BuiltinTypeName,
930-
"Builtin.UnsafeValueBuffer");
930+
BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER);
931931
break;
932932
case 'f': {
933933
int size = demangleIndex() - 1;
934934
if (size <= 0)
935935
return nullptr;
936936
CharVector name;
937-
name.append("Builtin.Float", *this);
937+
name.append(BUILTIN_TYPE_NAME_FLOAT, *this);
938938
name.append(size, *this);
939939
Ty = createNode(Node::Kind::BuiltinTypeName, name);
940940
break;
@@ -944,7 +944,7 @@ NodePointer Demangler::demangleBuiltinType() {
944944
if (size <= 0)
945945
return nullptr;
946946
CharVector name;
947-
name.append("Builtin.Int", *this);
947+
name.append(BUILTIN_TYPE_NAME_INT, *this);
948948
name.append(size, *this);
949949
Ty = createNode(Node::Kind::BuiltinTypeName, name);
950950
break;
@@ -955,34 +955,34 @@ NodePointer Demangler::demangleBuiltinType() {
955955
return nullptr;
956956
NodePointer EltType = popTypeAndGetChild();
957957
if (!EltType || EltType->getKind() != Node::Kind::BuiltinTypeName ||
958-
!EltType->getText().startswith("Builtin."))
958+
!EltType->getText().startswith(BUILTIN_TYPE_NAME_PREFIX))
959959
return nullptr;
960960
CharVector name;
961-
name.append("Builtin.Vec", *this);
961+
name.append(BUILTIN_TYPE_NAME_VEC, *this);
962962
name.append(elts, *this);
963963
name.push_back('x', *this);
964-
name.append(EltType->getText().substr(sizeof("Builtin.") - 1), *this);
964+
name.append(EltType->getText().substr(strlen(BUILTIN_TYPE_NAME_PREFIX)), *this);
965965
Ty = createNode(Node::Kind::BuiltinTypeName, name);
966966
break;
967967
}
968968
case 'O':
969969
Ty = createNode(Node::Kind::BuiltinTypeName,
970-
"Builtin.UnknownObject");
970+
BUILTIN_TYPE_NAME_UNKNOWNOBJECT);
971971
break;
972972
case 'o':
973973
Ty = createNode(Node::Kind::BuiltinTypeName,
974-
"Builtin.NativeObject");
974+
BUILTIN_TYPE_NAME_NATIVEOBJECT);
975975
break;
976976
case 'p':
977977
Ty = createNode(Node::Kind::BuiltinTypeName,
978-
"Builtin.RawPointer");
978+
BUILTIN_TYPE_NAME_RAWPOINTER);
979979
break;
980980
case 't':
981-
Ty = createNode(Node::Kind::BuiltinTypeName, "Builtin.SILToken");
981+
Ty = createNode(Node::Kind::BuiltinTypeName, BUILTIN_TYPE_NAME_SILTOKEN);
982982
break;
983983
case 'w':
984984
Ty = createNode(Node::Kind::BuiltinTypeName,
985-
"Builtin.Word");
985+
BUILTIN_TYPE_NAME_WORD);
986986
break;
987987
default:
988988
return nullptr;

lib/Demangling/Remangler.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -598,25 +598,25 @@ void Remangler::mangleBuiltinTypeName(Node *node) {
598598
Buffer << 'B';
599599
StringRef text = node->getText();
600600

601-
if (text == "Builtin.BridgeObject") {
601+
if (text == BUILTIN_TYPE_NAME_BRIDGEOBJECT) {
602602
Buffer << 'b';
603-
} else if (text == "Builtin.UnsafeValueBuffer") {
603+
} else if (text == BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER) {
604604
Buffer << 'B';
605-
} else if (text == "Builtin.UnknownObject") {
605+
} else if (text == BUILTIN_TYPE_NAME_UNKNOWNOBJECT) {
606606
Buffer << 'O';
607-
} else if (text == "Builtin.NativeObject") {
607+
} else if (text == BUILTIN_TYPE_NAME_NATIVEOBJECT) {
608608
Buffer << 'o';
609-
} else if (text == "Builtin.RawPointer") {
609+
} else if (text == BUILTIN_TYPE_NAME_RAWPOINTER) {
610610
Buffer << 'p';
611-
} else if (text == "Builtin.SILToken") {
611+
} else if (text == BUILTIN_TYPE_NAME_SILTOKEN) {
612612
Buffer << 't';
613-
} else if (text == "Builtin.Word") {
613+
} else if (text == BUILTIN_TYPE_NAME_WORD) {
614614
Buffer << 'w';
615-
} else if (stripPrefix(text, "Builtin.Int")) {
615+
} else if (stripPrefix(text, BUILTIN_TYPE_NAME_INT)) {
616616
Buffer << 'i' << text << '_';
617-
} else if (stripPrefix(text, "Builtin.Float")) {
617+
} else if (stripPrefix(text, BUILTIN_TYPE_NAME_FLOAT)) {
618618
Buffer << 'f' << text << '_';
619-
} else if (stripPrefix(text, "Builtin.Vec")) {
619+
} else if (stripPrefix(text, BUILTIN_TYPE_NAME_VEC)) {
620620
auto split = text.split('x');
621621
if (split.second == "RawPointer") {
622622
Buffer << 'p';

lib/IDE/TypeReconstruction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class DeclsLookupSource {
131131
bool allow_clang_importer = true) {
132132
assert(!module_name.empty());
133133
static ConstString g_ObjectiveCModule(MANGLING_MODULE_OBJC);
134-
static ConstString g_BuiltinModule("Builtin");
134+
static ConstString g_BuiltinModule(BUILTIN_NAME);
135135
static ConstString g_CModule(MANGLING_MODULE_CLANG_IMPORTER);
136136
if (allow_clang_importer) {
137137
if (module_name == g_ObjectiveCModule || module_name == g_CModule)
@@ -799,13 +799,13 @@ static void VisitNodeBuiltinTypeName(
799799

800800
StringRef builtin_name_ref(builtin_name);
801801

802-
if (builtin_name_ref.startswith("Builtin.")) {
802+
if (builtin_name_ref.startswith(BUILTIN_TYPE_NAME_PREFIX)) {
803803
StringRef stripped_name_ref =
804-
builtin_name_ref.drop_front(strlen("Builtin."));
804+
builtin_name_ref.drop_front(strlen(BUILTIN_TYPE_NAME_PREFIX));
805805
SmallVector<ValueDecl *, 1> builtin_decls;
806806

807807
result._module =
808-
DeclsLookupSource::GetDeclsLookupSource(*ast, ConstString("Builtin"));
808+
DeclsLookupSource::GetDeclsLookupSource(*ast, ConstString(BUILTIN_NAME));
809809

810810
if (!FindNamedDecls(ast, ast->getIdentifier(stripped_name_ref), result)) {
811811
result.Clear();
@@ -814,7 +814,7 @@ static void VisitNodeBuiltinTypeName(
814814
}
815815
} else {
816816
result._error = stringWithFormat(
817-
"BuiltinTypeName %s doesn't start with Builtin.", builtin_name.c_str());
817+
"BuiltinTypeName %s doesn't start with %s", builtin_name.c_str(), BUILTIN_TYPE_NAME_PREFIX);
818818
}
819819
}
820820

lib/SIL/SILPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2654,7 +2654,8 @@ void SILModule::print(SILPrintContext &PrintCtx, ModuleDecl *M,
26542654
break;
26552655
}
26562656

2657-
OS << "\n\nimport Builtin\nimport " << STDLIB_NAME
2657+
OS << "\n\nimport " << BUILTIN_NAME
2658+
<< "\nimport " << STDLIB_NAME
26582659
<< "\nimport " << SWIFT_SHIMS_NAME << "\n\n";
26592660

26602661
// Print the declarations and types from the associated context (origin module or

lib/Serialization/Serialization.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/ClangImporter/ClangImporter.h"
3737
#include "swift/ClangImporter/ClangModule.h"
3838
#include "swift/Serialization/SerializationOptions.h"
39+
#include "swift/Strings.h"
3940

4041
// FIXME: We're just using CompilerInstance::createOutputFile.
4142
// This API should be sunk down to LLVM.
@@ -3556,7 +3557,7 @@ static TypeAliasDecl *findTypeAliasForBuiltin(ASTContext &Ctx, Type T) {
35563557
llvm::SmallString<32> FullName;
35573558
llvm::raw_svector_ostream OS(FullName);
35583559
T->print(OS);
3559-
assert(FullName.startswith("Builtin."));
3560+
assert(FullName.startswith(BUILTIN_TYPE_NAME_PREFIX));
35603561
StringRef TypeName = FullName.substr(8);
35613562

35623563
SmallVector<ValueDecl*, 4> CurModuleResults;

0 commit comments

Comments
 (0)