blob: c7718faac3e6bab38fbf3f73f3e507a7130988c3 [file] [log] [blame]
//===--- DeclAndTypePrinter.h - Emit ObjC decls from Swift AST --*- C++ -*-===//
// This source file is part of the open source project
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
// See for license information
// See for the list of Swift project authors
#include "swift/AST/Type.h"
// for OptionalTypeKind
#include "swift/ClangImporter/ClangImporter.h"
namespace clang {
class NamedDecl;
} // end namespace clang
namespace swift {
/// Responsible for printing a Swift Decl or Type in Objective-C, to be
/// included in a Swift module's ObjC compatibility header.
class DeclAndTypePrinter {
using DelayedMemberSet = llvm::SmallSetVector<const ValueDecl *, 32>;
class Implementation;
friend class Implementation;
ModuleDecl &M;
raw_ostream &os;
const DelayedMemberSet &delayedMembers;
AccessLevel minRequiredAccess;
struct CTypeInfo {
StringRef name;
bool canBeNullable;
/// A map from {Module, TypeName} pairs to {C name, C nullability} pairs.
/// This is populated on first use with a list of known Swift types that are
/// translated directly by the ObjC printer instead of structurally, allowing
/// it to do things like map 'Int' to 'NSInteger' and 'Float' to 'float'.
/// In some sense it's the reverse of the ClangImporter's MappedTypes.def.
llvm::DenseMap<std::pair<Identifier, Identifier>, CTypeInfo> specialNames;
/// The name 'CFTypeRef'.
/// Cached for convenience.
Identifier ID_CFTypeRef;
Implementation getImpl();
DeclAndTypePrinter(ModuleDecl &mod, raw_ostream &out,
DelayedMemberSet &delayed, AccessLevel access)
: M(mod), os(out), delayedMembers(delayed), minRequiredAccess(access) {}
/// Returns true if \p VD should be included in a compatibility header for
/// the options the printer was constructed with.
bool shouldInclude(const ValueDecl *VD);
void print(const Decl *D);
void print(Type ty);
/// Is \p ED empty of members and protocol conformances to include?
bool isEmptyExtensionDecl(const ExtensionDecl *ED);
/// Returns the type that will be printed by PrintAsObjC for a parameter or
/// result type resolved to this declaration.
/// \warning This handles \c _ObjectiveCBridgeable types, but it doesn't
/// currently know about other aspects of PrintAsObjC behavior, like known
/// types.
const TypeDecl *getObjCTypeDecl(const TypeDecl* TD);
/// Prints a category declaring the given members.
/// All members must have the same parent type. The list must not be empty.
printAdHocCategory(iterator_range<const ValueDecl * const *> members);
/// Returns the name of an <os/object.h> type minus the leading "OS_",
/// or an empty string if \p decl is not an <os/object.h> type.
static StringRef maybeGetOSObjectBaseName(const clang::NamedDecl *decl);
} // end namespace swift