1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
//===--- PrimitiveTypeMapping.cpp - Mapping primitive types -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "PrimitiveTypeMapping.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
#include "swift/ClangImporter/ClangImporter.h"
using namespace swift;
void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
assert(mappedTypeNames.empty() && "expected empty type map");
#define MAP(SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY) \
mappedTypeNames[{ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME)}] = { \
CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY}
#define MAP_C(SWIFT_NAME, OBJC_REPR, C_REPR, NEEDS_NULLABILITY) \
mappedTypeNames[{ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME)}] = { \
OBJC_REPR, std::optional<StringRef>(C_REPR), \
std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY}
#define MAP_CXX(SWIFT_NAME, OBJC_REPR, C_REPR, CXX_REPR, NEEDS_NULLABILITY) \
mappedTypeNames[{ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME)}] = { \
OBJC_REPR, std::optional<StringRef>(C_REPR), \
std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY}
MAP(CBool, "bool", false);
MAP(CChar, "char", false);
MAP(CWideChar, "wchar_t", false);
MAP(CChar16, "char16_t", false);
MAP(CChar32, "char32_t", false);
MAP(CSignedChar, "signed char", false);
MAP(CShort, "short", false);
MAP(CInt, "int", false);
MAP(CLong, "long", false);
MAP(CLongLong, "long long", false);
MAP(CUnsignedChar, "unsigned char", false);
MAP(CUnsignedShort, "unsigned short", false);
MAP(CUnsignedInt, "unsigned int", false);
MAP(CUnsignedLong, "unsigned long", false);
MAP(CUnsignedLongLong, "unsigned long long", false);
MAP(CFloat, "float", false);
MAP(CDouble, "double", false);
MAP(Int8, "int8_t", false);
MAP(Int16, "int16_t", false);
MAP(Int32, "int32_t", false);
MAP(Int64, "int64_t", false);
MAP(UInt8, "uint8_t", false);
MAP(UInt16, "uint16_t", false);
MAP(UInt32, "uint32_t", false);
MAP(UInt64, "uint64_t", false);
MAP(Float, "float", false);
MAP(Double, "double", false);
MAP(Float32, "float", false);
MAP(Float64, "double", false);
MAP_CXX(Int, "NSInteger", "ptrdiff_t", "swift::Int", false);
MAP_CXX(UInt, "NSUInteger", "size_t", "swift::UInt", false);
MAP_C(Bool, "BOOL", "bool", false);
MAP(OpaquePointer, "void *", true);
MAP(UnsafeRawPointer, "void const *", true);
MAP(UnsafeMutableRawPointer, "void *", true);
Identifier ID_ObjectiveC = ctx.Id_ObjectiveC;
mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier("ObjCBool")}] = {
"BOOL", std::nullopt, std::nullopt, false};
mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier("Selector")}] = {
"SEL", std::nullopt, std::nullopt, true};
mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier(swift::getSwiftName(
KnownFoundationEntity::NSZone))}] = {
"struct _NSZone *", std::nullopt, std::nullopt, true};
mappedTypeNames[{ctx.Id_Darwin, ctx.getIdentifier("DarwinBoolean")}] = {
"Boolean", std::nullopt, std::nullopt, false};
mappedTypeNames[{ctx.Id_CoreGraphics, ctx.Id_CGFloat}] = {
"CGFloat", std::nullopt, std::nullopt, false};
mappedTypeNames[{ctx.Id_CoreFoundation, ctx.Id_CGFloat}] = {
"CGFloat", std::nullopt, std::nullopt, false};
// Use typedefs we set up for SIMD vector types.
#define MAP_SIMD_TYPE(BASENAME, _, __) \
StringRef simd2##BASENAME = "swift_" #BASENAME "2"; \
mappedTypeNames[{ctx.Id_simd, ctx.getIdentifier(#BASENAME "2")}] = { \
simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false}; \
StringRef simd3##BASENAME = "swift_" #BASENAME "3"; \
mappedTypeNames[{ctx.Id_simd, ctx.getIdentifier(#BASENAME "3")}] = { \
simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false}; \
StringRef simd4##BASENAME = "swift_" #BASENAME "4"; \
mappedTypeNames[{ctx.Id_simd, ctx.getIdentifier(#BASENAME "4")}] = { \
simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false};
#include "swift/ClangImporter/SIMDMappedTypes.def"
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
"must add or remove special name mappings if max number of "
"SIMD elements is changed");
}
PrimitiveTypeMapping::FullClangTypeInfo *
PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
if (mappedTypeNames.empty())
initialize(typeDecl->getASTContext());
Identifier moduleName = typeDecl->getModuleContext()->getName();
Identifier name = typeDecl->getName();
auto iter = mappedTypeNames.find({moduleName, name});
if (iter == mappedTypeNames.end())
return nullptr;
return &iter->second;
}
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
PrimitiveTypeMapping::getKnownObjCTypeInfo(const TypeDecl *typeDecl) {
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl))
return ClangTypeInfo{typeInfo->objcName, typeInfo->canBeNullable};
return std::nullopt;
}
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
PrimitiveTypeMapping::getKnownCTypeInfo(const TypeDecl *typeDecl) {
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl)) {
if (typeInfo->cName)
return ClangTypeInfo{*typeInfo->cName, typeInfo->canBeNullable};
}
return std::nullopt;
}
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
PrimitiveTypeMapping::getKnownCxxTypeInfo(const TypeDecl *typeDecl) {
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl)) {
if (typeInfo->cxxName)
return ClangTypeInfo{*typeInfo->cxxName, typeInfo->canBeNullable};
}
return std::nullopt;
}
|