From 39bb7bfe440d8958ac5acb6667826c972ee36fd6 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Tue, 11 Apr 2023 16:23:20 -0600 Subject: [PATCH] [AST] Convert ASTContext::getSwiftName to a free function The only state `getSwiftName` uses is its argument, and can be made a free function. Of note the inverse operation, `getKnownFoundationEntity`, is also a free function. This removes the requirement that callers have an `ASTContext` instance. (cherry-picked from commit fb524c0b865659cd81af31241667abf808677006) --- include/swift/AST/ASTContext.h | 12 ++++++------ lib/AST/ASTContext.cpp | 2 +- lib/AST/ClangTypeConverter.cpp | 2 +- lib/ClangImporter/ImportType.cpp | 6 +++--- lib/ClangImporter/MappedTypes.def | 4 ++-- lib/PrintAsClang/PrimitiveTypeMapping.cpp | 2 +- lib/Sema/TypeCheckType.cpp | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/swift/AST/ASTContext.h b/include/swift/AST/ASTContext.h index 18db535a5c2de..dad55f234d337 100644 --- a/include/swift/AST/ASTContext.h +++ b/include/swift/AST/ASTContext.h @@ -160,7 +160,7 @@ namespace ide { /// While the names of Foundation types aren't likely to change in /// Objective-C, their mapping into Swift can. Therefore, when /// referring to names of Foundation entities in Swift, use this enum -/// and \c ASTContext::getSwiftName or \c ASTContext::getSwiftId. +/// and \c swift::getSwiftName or \c ASTContext::getSwiftId. enum class KnownFoundationEntity { #define FOUNDATION_ENTITY(Name) Name, #include "swift/AST/KnownFoundationEntities.def" @@ -170,6 +170,10 @@ enum class KnownFoundationEntity { /// entity name. Optional getKnownFoundationEntity(StringRef name); +/// Retrieve the Swift name for the given Foundation entity, where +/// "NS" prefix stripping will apply under omit-needless-words. +StringRef getSwiftName(KnownFoundationEntity kind); + /// Introduces a new constraint checker arena, whose lifetime is /// tied to the lifetime of this RAII object. class ConstraintCheckerArenaRAII { @@ -1344,14 +1348,10 @@ class ASTContext final { /// Returns memory used exclusively by constraint solver. size_t getSolverMemory() const; - /// Retrieve the Swift name for the given Foundation entity, where - /// "NS" prefix stripping will apply under omit-needless-words. - StringRef getSwiftName(KnownFoundationEntity kind); - /// Retrieve the Swift identifier for the given Foundation entity, where /// "NS" prefix stripping will apply under omit-needless-words. Identifier getSwiftId(KnownFoundationEntity kind) { - return getIdentifier(getSwiftName(kind)); + return getIdentifier(swift::getSwiftName(kind)); } /// Populate \p names with visible top level module names. diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index d31b2cf006ff8..395e42e953c5b 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -3046,7 +3046,7 @@ Optional swift::getKnownFoundationEntity(StringRef name){ .Default(None); } -StringRef ASTContext::getSwiftName(KnownFoundationEntity kind) { +StringRef swift::getSwiftName(KnownFoundationEntity kind) { StringRef objcName; switch (kind) { #define FOUNDATION_ENTITY(Name) case KnownFoundationEntity::Name: \ diff --git a/lib/AST/ClangTypeConverter.cpp b/lib/AST/ClangTypeConverter.cpp index acf3f43635094..1d8415d88e0a0 100644 --- a/lib/AST/ClangTypeConverter.cpp +++ b/lib/AST/ClangTypeConverter.cpp @@ -248,7 +248,7 @@ clang::QualType ClangTypeConverter::visitStructType(StructType *type) { CHECK_NAMED_TYPE("OpaquePointer", ctx.VoidPtrTy); CHECK_NAMED_TYPE("CVaListPointer", getClangDecayedVaListType(ctx)); CHECK_NAMED_TYPE("DarwinBoolean", ctx.UnsignedCharTy); - CHECK_NAMED_TYPE(swiftDecl->getASTContext().getSwiftName( + CHECK_NAMED_TYPE(swift::getSwiftName( KnownFoundationEntity::NSZone), ctx.VoidPtrTy); CHECK_NAMED_TYPE("WindowsBool", ctx.IntTy); diff --git a/lib/ClangImporter/ImportType.cpp b/lib/ClangImporter/ImportType.cpp index 3c355c8b2c4ed..98d833668d5e2 100644 --- a/lib/ClangImporter/ImportType.cpp +++ b/lib/ClangImporter/ImportType.cpp @@ -454,7 +454,7 @@ namespace { ModuleDecl *objCModule = Impl.SwiftContext.getLoadedModule(Id_ObjectiveC); Type wrapperTy = Impl.getNamedSwiftType( objCModule, - Impl.SwiftContext.getSwiftName( + swift::getSwiftName( KnownFoundationEntity::NSZone)); if (wrapperTy) return {wrapperTy, ImportHint::OtherPointer}; @@ -1358,7 +1358,7 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl, // FIXME: Avoid string comparison by caching this identifier. if (elementClass->getName().str() != - impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSError)) + swift::getSwiftName(KnownFoundationEntity::NSError)) return Type(); if (!impl.canImportFoundationModule() || @@ -1369,7 +1369,7 @@ static Type maybeImportNSErrorOutParameter(ClangImporter::Implementation &impl, if (resugarNSErrorPointer) return impl.getNamedSwiftType( foundationModule, - impl.SwiftContext.getSwiftName( + swift::getSwiftName( KnownFoundationEntity::NSErrorPointer)); // The imported type is AUMP, but the typealias is AUMP? diff --git a/lib/ClangImporter/MappedTypes.def b/lib/ClangImporter/MappedTypes.def index cb5694f5161ca..ea4610eb0e215 100644 --- a/lib/ClangImporter/MappedTypes.def +++ b/lib/ClangImporter/MappedTypes.def @@ -146,13 +146,13 @@ MAP_TYPE("__swift_shims_dispatch_data_t", ObjCId, 0, "Dispatch", "dispatch_data_ MAP_TYPE("SEL", ObjCSel, 0, "ObjectiveC", "Selector", false, DoNothing) MAP_STDLIB_TYPE("Class", ObjCClass, 0, "AnyClass", false, DoNothing) MAP_STDLIB_TYPE( - Impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSInteger), + swift::getSwiftName(KnownFoundationEntity::NSInteger), SignedWord, 0, "Int", false, DefineOnly) // Treat NSUInteger specially: exposing it as a typealias for "Int" would be // confusing. MAP_STDLIB_TYPE( - Impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSUInteger), + swift::getSwiftName(KnownFoundationEntity::NSUInteger), UnsignedWord, 0, "Int", false, DoNothing) // CGFloat. diff --git a/lib/PrintAsClang/PrimitiveTypeMapping.cpp b/lib/PrintAsClang/PrimitiveTypeMapping.cpp index d94aeb3951249..3c0f56bc545a8 100644 --- a/lib/PrintAsClang/PrimitiveTypeMapping.cpp +++ b/lib/PrintAsClang/PrimitiveTypeMapping.cpp @@ -88,7 +88,7 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) { "BOOL", None, None, false}; mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier("Selector")}] = { "SEL", None, None, true}; - mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier(ctx.getSwiftName( + mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier(swift::getSwiftName( KnownFoundationEntity::NSZone))}] = { "struct _NSZone *", None, None, true}; diff --git a/lib/Sema/TypeCheckType.cpp b/lib/Sema/TypeCheckType.cpp index 04f6a36415bbb..9ab034185e1cd 100644 --- a/lib/Sema/TypeCheckType.cpp +++ b/lib/Sema/TypeCheckType.cpp @@ -1374,7 +1374,7 @@ static Type diagnoseUnknownType(TypeResolution resolution, repr->overwriteNameRef(DeclNameRef(ctx.getIdentifier(RemappedTy))); // HACK: 'NSUInteger' suggests both 'UInt' and 'Int'. - if (TypeName == ctx.getSwiftName(KnownFoundationEntity::NSUInteger)) { + if (TypeName == swift::getSwiftName(KnownFoundationEntity::NSUInteger)) { diags.diagnose(L, diag::note_remapped_type, "UInt") .fixItReplace(R, "UInt"); }