Skip to content

[Gardening] Use string constants for Builtin module and type names #14963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions include/swift/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,52 @@ namespace swift {
static const char SWIFT_ONONE_SUPPORT[] = "SwiftOnoneSupport";
/// The name of the SwiftShims module, which contains private stdlib decls.
static const char SWIFT_SHIMS_NAME[] = "SwiftShims";
/// The name of the Builtin module, which contains Builtin functions.
static const char BUILTIN_NAME[] = "Builtin";
/// The prefix of module names used by LLDB to capture Swift expressions
static const char LLDB_EXPRESSIONS_MODULE_NAME_PREFIX[] = "__lldb_expr_";

/// The name of the fake module used to hold imported Objective-C things.
static const char MANGLING_MODULE_OBJC[] = "__C";
/// The name of the fake module used to hold synthesized ClangImporter things.
static const char MANGLING_MODULE_CLANG_IMPORTER[] = "__C_Synthesized";

/// The name of the Builtin type prefix
static const char BUILTIN_TYPE_NAME_PREFIX[] = "Builtin.";
/// The name of the Builtin type for Int
static const char BUILTIN_TYPE_NAME_INT[] = "Builtin.Int";
/// The name of the Builtin type for Int8
static const char BUILTIN_TYPE_NAME_INT8[] = "Builtin.Int8";
/// The name of the Builtin type for Int16
static const char BUILTIN_TYPE_NAME_INT16[] = "Builtin.Int16";
/// The name of the Builtin type for Int32
static const char BUILTIN_TYPE_NAME_INT32[] = "Builtin.Int32";
/// The name of the Builtin type for Int64
static const char BUILTIN_TYPE_NAME_INT64[] = "Builtin.Int64";
/// The name of the Builtin type for Int128
static const char BUILTIN_TYPE_NAME_INT128[] = "Builtin.Int128";
/// The name of the Builtin type for Int256
static const char BUILTIN_TYPE_NAME_INT256[] = "Builtin.Int256";
/// The name of the Builtin type for Int512
static const char BUILTIN_TYPE_NAME_INT512[] = "Builtin.Int512";
/// The name of the Builtin type for Float
static const char BUILTIN_TYPE_NAME_FLOAT[] = "Builtin.Float";
/// The name of the Builtin type for NativeObject
static const char BUILTIN_TYPE_NAME_NATIVEOBJECT[] = "Builtin.NativeObject";
/// The name of the Builtin type for BridgeObject
static const char BUILTIN_TYPE_NAME_BRIDGEOBJECT[] = "Builtin.BridgeObject";
/// The name of the Builtin type for RawPointer
static const char BUILTIN_TYPE_NAME_RAWPOINTER[] = "Builtin.RawPointer";
/// The name of the Builtin type for UnsafeValueBuffer
static const char BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER[] = "Builtin.UnsafeValueBuffer";
/// The name of the Builtin type for UnknownObject
static const char BUILTIN_TYPE_NAME_UNKNOWNOBJECT[] = "Builtin.UnknownObject";
/// The name of the Builtin type for Vector
static const char BUILTIN_TYPE_NAME_VEC[] = "Builtin.Vec";
/// The name of the Builtin type for SILToken
static const char BUILTIN_TYPE_NAME_SILTOKEN[] = "Builtin.SILToken";
/// The name of the Builtin type for Word
static const char BUILTIN_TYPE_NAME_WORD[] = "Builtin.Word";
} // end namespace swift

#endif // SWIFT_STRINGS_H
2 changes: 1 addition & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ ConstraintCheckerArenaRAII::~ConstraintCheckerArenaRAII() {
}

static ModuleDecl *createBuiltinModule(ASTContext &ctx) {
auto M = ModuleDecl::create(ctx.getIdentifier("Builtin"), ctx);
auto M = ModuleDecl::create(ctx.getIdentifier(BUILTIN_NAME), ctx);
M->addFile(*new (ctx) BuiltinUnit(*M));
return M;
}
Expand Down
20 changes: 10 additions & 10 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3200,23 +3200,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

void visitBuiltinRawPointerType(BuiltinRawPointerType *T) {
Printer << "Builtin.RawPointer";
Printer << BUILTIN_TYPE_NAME_RAWPOINTER;
}

void visitBuiltinNativeObjectType(BuiltinNativeObjectType *T) {
Printer << "Builtin.NativeObject";
Printer << BUILTIN_TYPE_NAME_NATIVEOBJECT;
}

void visitBuiltinUnknownObjectType(BuiltinUnknownObjectType *T) {
Printer << "Builtin.UnknownObject";
Printer << BUILTIN_TYPE_NAME_UNKNOWNOBJECT;
}

void visitBuiltinBridgeObjectType(BuiltinBridgeObjectType *T) {
Printer << "Builtin.BridgeObject";
Printer << BUILTIN_TYPE_NAME_BRIDGEOBJECT;
}

void visitBuiltinUnsafeValueBufferType(BuiltinUnsafeValueBufferType *T) {
Printer << "Builtin.UnsafeValueBuffer";
Printer << BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER;
}

void visitBuiltinVectorType(BuiltinVectorType *T) {
Expand All @@ -3228,21 +3228,21 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
llvm::raw_svector_ostream UnderlyingOS(UnderlyingStrVec);
T->getElementType().print(UnderlyingOS);
}
if (UnderlyingStrVec.startswith("Builtin."))
if (UnderlyingStrVec.startswith(BUILTIN_TYPE_NAME_PREFIX))
UnderlyingStr = UnderlyingStrVec.substr(8);
else
UnderlyingStr = UnderlyingStrVec;
}

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

void visitBuiltinIntegerType(BuiltinIntegerType *T) {
auto width = T->getWidth();
if (width.isFixedWidth()) {
Printer << "Builtin.Int" << width.getFixedWidth();
Printer << BUILTIN_TYPE_NAME_INT << width.getFixedWidth();
} else if (width.isPointerWidth()) {
Printer << "Builtin.Word";
Printer << BUILTIN_TYPE_NAME_WORD;
} else {
llvm_unreachable("impossible bit width");
}
Expand All @@ -3260,7 +3260,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

void visitSILTokenType(SILTokenType *T) {
Printer << "Builtin.SILToken";
Printer << BUILTIN_TYPE_NAME_SILTOKEN;
}

void visitNameAliasType(NameAliasType *T) {
Expand Down
24 changes: 12 additions & 12 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,18 +923,18 @@ NodePointer Demangler::demangleBuiltinType() {
switch (nextChar()) {
case 'b':
Ty = createNode(Node::Kind::BuiltinTypeName,
"Builtin.BridgeObject");
BUILTIN_TYPE_NAME_BRIDGEOBJECT);
break;
case 'B':
Ty = createNode(Node::Kind::BuiltinTypeName,
"Builtin.UnsafeValueBuffer");
BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER);
break;
case 'f': {
int size = demangleIndex() - 1;
if (size <= 0)
return nullptr;
CharVector name;
name.append("Builtin.Float", *this);
name.append(BUILTIN_TYPE_NAME_FLOAT, *this);
name.append(size, *this);
Ty = createNode(Node::Kind::BuiltinTypeName, name);
break;
Expand All @@ -944,7 +944,7 @@ NodePointer Demangler::demangleBuiltinType() {
if (size <= 0)
return nullptr;
CharVector name;
name.append("Builtin.Int", *this);
name.append(BUILTIN_TYPE_NAME_INT, *this);
name.append(size, *this);
Ty = createNode(Node::Kind::BuiltinTypeName, name);
break;
Expand All @@ -955,34 +955,34 @@ NodePointer Demangler::demangleBuiltinType() {
return nullptr;
NodePointer EltType = popTypeAndGetChild();
if (!EltType || EltType->getKind() != Node::Kind::BuiltinTypeName ||
!EltType->getText().startswith("Builtin."))
!EltType->getText().startswith(BUILTIN_TYPE_NAME_PREFIX))
return nullptr;
CharVector name;
name.append("Builtin.Vec", *this);
name.append(BUILTIN_TYPE_NAME_VEC, *this);
name.append(elts, *this);
name.push_back('x', *this);
name.append(EltType->getText().substr(sizeof("Builtin.") - 1), *this);
name.append(EltType->getText().substr(strlen(BUILTIN_TYPE_NAME_PREFIX)), *this);
Ty = createNode(Node::Kind::BuiltinTypeName, name);
break;
}
case 'O':
Ty = createNode(Node::Kind::BuiltinTypeName,
"Builtin.UnknownObject");
BUILTIN_TYPE_NAME_UNKNOWNOBJECT);
break;
case 'o':
Ty = createNode(Node::Kind::BuiltinTypeName,
"Builtin.NativeObject");
BUILTIN_TYPE_NAME_NATIVEOBJECT);
break;
case 'p':
Ty = createNode(Node::Kind::BuiltinTypeName,
"Builtin.RawPointer");
BUILTIN_TYPE_NAME_RAWPOINTER);
break;
case 't':
Ty = createNode(Node::Kind::BuiltinTypeName, "Builtin.SILToken");
Ty = createNode(Node::Kind::BuiltinTypeName, BUILTIN_TYPE_NAME_SILTOKEN);
break;
case 'w':
Ty = createNode(Node::Kind::BuiltinTypeName,
"Builtin.Word");
BUILTIN_TYPE_NAME_WORD);
break;
default:
return nullptr;
Expand Down
20 changes: 10 additions & 10 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,25 +598,25 @@ void Remangler::mangleBuiltinTypeName(Node *node) {
Buffer << 'B';
StringRef text = node->getText();

if (text == "Builtin.BridgeObject") {
if (text == BUILTIN_TYPE_NAME_BRIDGEOBJECT) {
Buffer << 'b';
} else if (text == "Builtin.UnsafeValueBuffer") {
} else if (text == BUILTIN_TYPE_NAME_UNSAFEVALUEBUFFER) {
Buffer << 'B';
} else if (text == "Builtin.UnknownObject") {
} else if (text == BUILTIN_TYPE_NAME_UNKNOWNOBJECT) {
Buffer << 'O';
} else if (text == "Builtin.NativeObject") {
} else if (text == BUILTIN_TYPE_NAME_NATIVEOBJECT) {
Buffer << 'o';
} else if (text == "Builtin.RawPointer") {
} else if (text == BUILTIN_TYPE_NAME_RAWPOINTER) {
Buffer << 'p';
} else if (text == "Builtin.SILToken") {
} else if (text == BUILTIN_TYPE_NAME_SILTOKEN) {
Buffer << 't';
} else if (text == "Builtin.Word") {
} else if (text == BUILTIN_TYPE_NAME_WORD) {
Buffer << 'w';
} else if (stripPrefix(text, "Builtin.Int")) {
} else if (stripPrefix(text, BUILTIN_TYPE_NAME_INT)) {
Buffer << 'i' << text << '_';
} else if (stripPrefix(text, "Builtin.Float")) {
} else if (stripPrefix(text, BUILTIN_TYPE_NAME_FLOAT)) {
Buffer << 'f' << text << '_';
} else if (stripPrefix(text, "Builtin.Vec")) {
} else if (stripPrefix(text, BUILTIN_TYPE_NAME_VEC)) {
auto split = text.split('x');
if (split.second == "RawPointer") {
Buffer << 'p';
Expand Down
10 changes: 5 additions & 5 deletions lib/IDE/TypeReconstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class DeclsLookupSource {
bool allow_clang_importer = true) {
assert(!module_name.empty());
static ConstString g_ObjectiveCModule(MANGLING_MODULE_OBJC);
static ConstString g_BuiltinModule("Builtin");
static ConstString g_BuiltinModule(BUILTIN_NAME);
static ConstString g_CModule(MANGLING_MODULE_CLANG_IMPORTER);
if (allow_clang_importer) {
if (module_name == g_ObjectiveCModule || module_name == g_CModule)
Expand Down Expand Up @@ -799,13 +799,13 @@ static void VisitNodeBuiltinTypeName(

StringRef builtin_name_ref(builtin_name);

if (builtin_name_ref.startswith("Builtin.")) {
if (builtin_name_ref.startswith(BUILTIN_TYPE_NAME_PREFIX)) {
StringRef stripped_name_ref =
builtin_name_ref.drop_front(strlen("Builtin."));
builtin_name_ref.drop_front(strlen(BUILTIN_TYPE_NAME_PREFIX));
SmallVector<ValueDecl *, 1> builtin_decls;

result._module =
DeclsLookupSource::GetDeclsLookupSource(*ast, ConstString("Builtin"));
DeclsLookupSource::GetDeclsLookupSource(*ast, ConstString(BUILTIN_NAME));

if (!FindNamedDecls(ast, ast->getIdentifier(stripped_name_ref), result)) {
result.Clear();
Expand All @@ -814,7 +814,7 @@ static void VisitNodeBuiltinTypeName(
}
} else {
result._error = stringWithFormat(
"BuiltinTypeName %s doesn't start with Builtin.", builtin_name.c_str());
"BuiltinTypeName %s doesn't start with %s", builtin_name.c_str(), BUILTIN_TYPE_NAME_PREFIX);
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,8 @@ void SILModule::print(SILPrintContext &PrintCtx, ModuleDecl *M,
break;
}

OS << "\n\nimport Builtin\nimport " << STDLIB_NAME
OS << "\n\nimport " << BUILTIN_NAME
<< "\nimport " << STDLIB_NAME
<< "\nimport " << SWIFT_SHIMS_NAME << "\n\n";

// Print the declarations and types from the associated context (origin module or
Expand Down
3 changes: 2 additions & 1 deletion lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Serialization/SerializationOptions.h"
#include "swift/Strings.h"

// FIXME: We're just using CompilerInstance::createOutputFile.
// This API should be sunk down to LLVM.
Expand Down Expand Up @@ -3551,7 +3552,7 @@ static TypeAliasDecl *findTypeAliasForBuiltin(ASTContext &Ctx, Type T) {
llvm::SmallString<32> FullName;
llvm::raw_svector_ostream OS(FullName);
T->print(OS);
assert(FullName.startswith("Builtin."));
assert(FullName.startswith(BUILTIN_TYPE_NAME_PREFIX));
StringRef TypeName = FullName.substr(8);

SmallVector<ValueDecl*, 4> CurModuleResults;
Expand Down