Skip to content
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
8 changes: 8 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ class ASTContext final {
/// if applicable.
const Decl *getSwiftDeclForExportedClangDecl(const clang::Decl *decl);

/// General conversion method from Swift types -> Clang types.
///
/// HACK: This method is only intended to be called from a specific place in
/// IRGen. For converting function types, strongly prefer using one of the
/// other methods instead, instead of manually iterating over parameters
/// and results.
const clang::Type *getClangTypeForIRGen(Type ty);

/// Determine whether the given Swift type is representable in a
/// given foreign language.
ForeignRepresentationInfo
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "swift/Subsystems.h"
#include "swift/Syntax/References.h"
#include "swift/Syntax/SyntaxArena.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringMap.h"
Expand Down Expand Up @@ -4589,6 +4590,10 @@ ASTContext::getSwiftDeclForExportedClangDecl(const clang::Decl *decl) {
return impl.Converter->getSwiftDeclForExportedClangDecl(decl);
}

const clang::Type *
ASTContext::getClangTypeForIRGen(Type ty) {
return getClangTypeConverter().convert(ty).getTypePtrOrNull();
}

CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
if (auto theSig = getImpl().SingleGenericParameterSignature)
Expand Down
11 changes: 6 additions & 5 deletions lib/AST/ClangTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
//
// This file implements generation of Clang AST types from Swift AST types for
// types that are representable in Objective-C interfaces.
// Large chunks of the code are lightly modified versions of the code in
// IRGen/GenClangType.cpp (which should eventually go away), so make sure
// to keep the two in sync.
// The three major differences are that, in this file:
//
// The usage of ClangTypeConverter at the AST level means that we may
// encounter ill-formed types and/or sugared types. To avoid crashing and
// keeping sugar as much as possible (in case the generated Clang type needs
// to be surfaced to the user):
//
// 1. We fail gracefully instead of asserting/UB.
// 2. We try to keep clang sugar instead of discarding it.
// 3. We use getAs instead of cast as we handle Swift types with sugar.
//
//===----------------------------------------------------------------------===//

Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ClangTypeConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class ClangTypeConverter :
SmallVectorImpl<clang::TemplateArgument> &templateArgs);

private:
friend ASTContext; // HACK: expose `convert` method to ASTContext

clang::QualType convert(Type type);

clang::QualType convertMemberType(NominalTypeDecl *DC,
Expand Down
Loading