Skip to content

Commit 1abd35a

Browse files
[NFC] Lift out common "canImportAsOptional" to a new header.
1 parent 99b7299 commit 1abd35a

File tree

6 files changed

+76
-18
lines changed

6 files changed

+76
-18
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//=- ClangSwiftTypeCorrespondence.h - Relations between Clang & Swift types -=//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file describes some common relations between Clang types and Swift
14+
// types that are need by the ClangTypeConverter and parts of ClangImporter.
15+
//
16+
// Since ClangTypeConverter is an implementation detail, ClangImporter should
17+
// not depend on ClangTypeConverter.h.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#ifndef SWIFT_AST_CLANG_SWIFT_TYPE_CORRESPONDENCE_H
22+
#define SWIFT_AST_CLANG_SWIFT_TYPE_CORRESPONDENCE_H
23+
24+
namespace clang {
25+
class Type;
26+
}
27+
28+
namespace swift {
29+
/// Checks whether a Clang type can be imported as a Swift Optional type.
30+
///
31+
/// For example, a `const uint8_t *` could be imported as
32+
/// `Optional<UnsafePointer<UInt8>>`.
33+
bool canImportAsOptional(const clang::Type *type);
34+
}
35+
36+
#endif /* SWIFT_AST_CLANG_SWIFT_TYPE_CORRESPONDENCE_H */

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_swift_host_library(swiftAST STATIC
3434
AvailabilitySpec.cpp
3535
Builtins.cpp
3636
CaptureInfo.cpp
37+
ClangSwiftTypeCorrespondence.cpp
3738
ClangTypeConverter.cpp
3839
ConcreteDeclRef.cpp
3940
ConformanceLookupTable.cpp
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//- ClangSwiftTypeCorrespondence.cpp - Relations between Clang & Swift types -//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// See description in ClangSwiftTypeCorrespondence.h.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "swift/AST/ClangSwiftTypeCorrespondence.h"
18+
#include "clang/AST/Type.h"
19+
20+
bool swift::canImportAsOptional(const clang::Type *type) {
21+
// Note: this mimics ImportHint::canImportAsOptional.
22+
23+
// Includes CoreFoundation types such as CFStringRef (== struct CFString *).
24+
return type && (type->isPointerType()
25+
|| type->isBlockPointerType()
26+
|| type->isObjCObjectPointerType());
27+
}

lib/AST/ClangTypeConverter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "swift/AST/ASTContext.h"
2828
#include "swift/AST/ClangModuleLoader.h"
29+
#include "swift/AST/ClangSwiftTypeCorrespondence.h"
2930
#include "swift/AST/ExistentialLayout.h"
3031
#include "swift/AST/Module.h"
3132
#include "swift/AST/Type.h"
@@ -460,7 +461,10 @@ ClangTypeConverter::visitBoundGenericType(BoundGenericType *type) {
460461
if (type->getDecl()->isOptionalDecl()) {
461462
auto args = type->getGenericArgs();
462463
assert((args.size() == 1) && "Optional should have 1 generic argument.");
463-
return convert(args[0]);
464+
clang::QualType innerTy = convert(args[0]);
465+
if (swift::canImportAsOptional(innerTy.getTypePtrOrNull()))
466+
return innerTy;
467+
return clang::QualType();
464468
}
465469

466470
auto swiftStructDecl = type->getDecl();

lib/ClangImporter/ImportName.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ClangDiagnosticConsumer.h"
2222
#include "swift/Subsystems.h"
2323
#include "swift/AST/ASTContext.h"
24+
#include "swift/AST/ClangSwiftTypeCorrespondence.h"
2425
#include "swift/AST/DiagnosticEngine.h"
2526
#include "swift/AST/DiagnosticsClangImporter.h"
2627
#include "swift/AST/Module.h"
@@ -170,19 +171,6 @@ static bool isIntegerType(clang::QualType clangType) {
170171
return false;
171172
}
172173

173-
/// Whether the given Objective-C type can be imported as an optional type.
174-
static bool canImportAsOptional(clang::ASTContext &ctx, clang::QualType type) {
175-
// Note: this mimics ImportHint::canImportAsOptional.
176-
177-
// Objective-C object pointers.
178-
if (type->getAs<clang::ObjCObjectPointerType>()) return true;
179-
180-
// Block and C pointers, including CF types.
181-
if (type->isBlockPointerType() || type->isPointerType()) return true;
182-
183-
return false;
184-
}
185-
186174
static Optional<ForeignErrorConvention::Kind>
187175
classifyMethodErrorHandling(const clang::ObjCMethodDecl *clangDecl,
188176
OptionalTypeKind resultOptionality) {
@@ -202,7 +190,8 @@ classifyMethodErrorHandling(const clang::ObjCMethodDecl *clangDecl,
202190
// non-optional type.
203191
case clang::SwiftErrorAttr::NullResult:
204192
if (resultOptionality != OTK_None &&
205-
canImportAsOptional(clangCtx, clangDecl->getReturnType()))
193+
swift::canImportAsOptional(
194+
clangDecl->getReturnType().getTypePtrOrNull()))
206195
return ForeignErrorConvention::NilResult;
207196
return None;
208197

@@ -235,7 +224,8 @@ classifyMethodErrorHandling(const clang::ObjCMethodDecl *clangDecl,
235224

236225
// For optional reference results, a nil value is normally an error.
237226
if (resultOptionality != OTK_None &&
238-
canImportAsOptional(clangCtx, clangDecl->getReturnType())) {
227+
swift::canImportAsOptional(
228+
clangDecl->getReturnType().getTypePtrOrNull())) {
239229
return ForeignErrorConvention::NilResult;
240230
}
241231

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "swift/AST/ASTContext.h"
1616
#include "swift/AST/ASTVisitor.h"
17+
#include "swift/AST/ClangSwiftTypeCorrespondence.h"
1718
#include "swift/AST/Comment.h"
1819
#include "swift/AST/Decl.h"
1920
#include "swift/AST/ExistentialLayout.h"
@@ -1559,8 +1560,7 @@ class DeclAndTypePrinter::Implementation
15591560
ASTContext &ctx = getASTContext();
15601561
auto &clangASTContext = ctx.getClangModuleLoader()->getClangASTContext();
15611562
clang::QualType clangTy = clangASTContext.getTypeDeclType(clangTypeDecl);
1562-
return clangTy->isPointerType() || clangTy->isBlockPointerType() ||
1563-
clangTy->isObjCObjectPointerType();
1563+
return swift::canImportAsOptional(clangTy.getTypePtr());
15641564
}
15651565

15661566
bool printImportedAlias(const TypeAliasDecl *alias,

0 commit comments

Comments
 (0)