Skip to content

Commit 16d9766

Browse files
committed
Clang importer: stop importing types to map to their names for omit-needless-words.
1 parent 3ec6239 commit 16d9766

File tree

2 files changed

+110
-15
lines changed

2 files changed

+110
-15
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,18 +1669,113 @@ OmissionTypeName ClangImporter::Implementation::getClangTypeNameForOmission(
16691669

16701670
// Handle builtin types by importing them and getting the Swift name.
16711671
if (auto builtinTy = type->getAs<clang::BuiltinType>()) {
1672-
if (Type type = importType(clang::QualType(builtinTy, 0),
1673-
ImportTypeKind::Abstract,
1674-
/*allowNSUIntegerAsInt=*/true,
1675-
/*canFullyBridgeTypes=*/true,
1676-
OTK_None)) {
1677-
if (auto nominal = type->getAnyNominal()) {
1678-
OmissionTypeOptions options;
1679-
if (nominal->getName().str() == "Bool")
1680-
options |= OmissionTypeFlags::Boolean;
1681-
1682-
return OmissionTypeName(nominal->getName().str(), options);
1672+
// Names of integer types.
1673+
static const char *intTypeNames[] = {
1674+
"UInt8",
1675+
"UInt16",
1676+
"UInt32",
1677+
"UInt64",
1678+
"UInt128"
1679+
};
1680+
1681+
/// Retrieve the name for an integer type based on its size.
1682+
auto getIntTypeName = [&](bool isSigned) -> StringRef {
1683+
switch (ctx.getTypeSize(builtinTy)) {
1684+
case 8: return StringRef(intTypeNames[0]).substr(isSigned ? 1 : 0);
1685+
case 16: return StringRef(intTypeNames[1]).substr(isSigned ? 1 : 0);
1686+
case 32: return StringRef(intTypeNames[2]).substr(isSigned ? 1 : 0);
1687+
case 64: return StringRef(intTypeNames[3]).substr(isSigned ? 1 : 0);
1688+
case 128: return StringRef(intTypeNames[4]).substr(isSigned ? 1 : 0);
1689+
default: llvm_unreachable("bad integer type size");
16831690
}
1691+
};
1692+
1693+
switch (builtinTy->getKind()) {
1694+
case clang::BuiltinType::Void:
1695+
return "Void";
1696+
1697+
case clang::BuiltinType::Bool:
1698+
return OmissionTypeName("Bool", OmissionTypeFlags::Boolean);
1699+
1700+
case clang::BuiltinType::Float:
1701+
return "Float";
1702+
1703+
case clang::BuiltinType::Double:
1704+
return "Double";
1705+
1706+
case clang::BuiltinType::Char16:
1707+
return "UInt16";
1708+
1709+
case clang::BuiltinType::Char32:
1710+
return "UnicodeScalar";
1711+
1712+
case clang::BuiltinType::Char_U:
1713+
case clang::BuiltinType::UChar:
1714+
case clang::BuiltinType::UShort:
1715+
case clang::BuiltinType::UInt:
1716+
case clang::BuiltinType::ULong:
1717+
case clang::BuiltinType::ULongLong:
1718+
case clang::BuiltinType::UInt128:
1719+
case clang::BuiltinType::WChar_U:
1720+
return getIntTypeName(false);
1721+
1722+
case clang::BuiltinType::Char_S:
1723+
case clang::BuiltinType::SChar:
1724+
case clang::BuiltinType::Short:
1725+
case clang::BuiltinType::Int:
1726+
case clang::BuiltinType::Long:
1727+
case clang::BuiltinType::LongLong:
1728+
case clang::BuiltinType::Int128:
1729+
case clang::BuiltinType::WChar_S:
1730+
return getIntTypeName(true);
1731+
1732+
// Types that cannot be mapped into Swift, and probably won't ever be.
1733+
case clang::BuiltinType::Dependent:
1734+
case clang::BuiltinType::ARCUnbridgedCast:
1735+
case clang::BuiltinType::BoundMember:
1736+
case clang::BuiltinType::BuiltinFn:
1737+
case clang::BuiltinType::Overload:
1738+
case clang::BuiltinType::PseudoObject:
1739+
case clang::BuiltinType::UnknownAny:
1740+
return OmissionTypeName();
1741+
1742+
// FIXME: Types that can be mapped, but aren't yet.
1743+
case clang::BuiltinType::Half:
1744+
case clang::BuiltinType::LongDouble:
1745+
case clang::BuiltinType::NullPtr:
1746+
return OmissionTypeName();
1747+
1748+
// Objective-C types that aren't mapped directly; rather, pointers to
1749+
// these types will be mapped.
1750+
case clang::BuiltinType::ObjCClass:
1751+
case clang::BuiltinType::ObjCId:
1752+
case clang::BuiltinType::ObjCSel:
1753+
return OmissionTypeName();
1754+
1755+
// OpenCL types that don't have Swift equivalents.
1756+
case clang::BuiltinType::OCLImage1d:
1757+
case clang::BuiltinType::OCLImage1dArray:
1758+
case clang::BuiltinType::OCLImage1dBuffer:
1759+
case clang::BuiltinType::OCLImage2d:
1760+
case clang::BuiltinType::OCLImage2dArray:
1761+
case clang::BuiltinType::OCLImage2dDepth:
1762+
case clang::BuiltinType::OCLImage2dArrayDepth:
1763+
case clang::BuiltinType::OCLImage2dMSAA:
1764+
case clang::BuiltinType::OCLImage2dArrayMSAA:
1765+
case clang::BuiltinType::OCLImage2dMSAADepth:
1766+
case clang::BuiltinType::OCLImage2dArrayMSAADepth:
1767+
case clang::BuiltinType::OCLImage3d:
1768+
case clang::BuiltinType::OCLSampler:
1769+
case clang::BuiltinType::OCLEvent:
1770+
case clang::BuiltinType::OCLClkEvent:
1771+
case clang::BuiltinType::OCLQueue:
1772+
case clang::BuiltinType::OCLNDRange:
1773+
case clang::BuiltinType::OCLReserveID:
1774+
return OmissionTypeName();
1775+
1776+
// OpenMP types that don't have Swift equivalents.
1777+
case clang::BuiltinType::OMPArraySection:
1778+
return OmissionTypeName();
16841779
}
16851780
}
16861781

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,13 +750,13 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
750750

751751
/// Determine the imported CF type for the given typedef-name, or the empty
752752
/// string if this is not an imported CF type name.
753-
StringRef getCFTypeName(const clang::TypedefNameDecl *decl,
754-
StringRef *secondaryName = nullptr);
753+
static StringRef getCFTypeName(const clang::TypedefNameDecl *decl,
754+
StringRef *secondaryName = nullptr);
755755

756756
/// Retrieve the type name of a Clang type for the purposes of
757757
/// omitting unneeded words.
758-
OmissionTypeName getClangTypeNameForOmission(clang::ASTContext &ctx,
759-
clang::QualType type);
758+
static OmissionTypeName getClangTypeNameForOmission(clang::ASTContext &ctx,
759+
clang::QualType type);
760760

761761
/// Omit needless words in a function name.
762762
bool omitNeedlessWordsInFunctionName(

0 commit comments

Comments
 (0)