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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
//===--- ClangClassTemplateNamePrinter.cpp --------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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 "ClangClassTemplateNamePrinter.h"
#include "ImporterImpl.h"
#include "clang/AST/TypeVisitor.h"
using namespace swift;
using namespace swift::importer;
struct TemplateInstantiationNamePrinter
: clang::TypeVisitor<TemplateInstantiationNamePrinter, std::string> {
ASTContext &swiftCtx;
NameImporter *nameImporter;
ImportNameVersion version;
TemplateInstantiationNamePrinter(ASTContext &swiftCtx,
NameImporter *nameImporter,
ImportNameVersion version)
: swiftCtx(swiftCtx), nameImporter(nameImporter), version(version) {}
std::string VisitType(const clang::Type *type) {
// Print "_" as a fallback if we couldn't emit a more meaningful type name.
return "_";
}
std::string VisitBuiltinType(const clang::BuiltinType *type) {
Type swiftType = nullptr;
switch (type->getKind()) {
case clang::BuiltinType::Void:
swiftType =
swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), "Void");
break;
#define MAP_BUILTIN_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), \
#SWIFT_TYPE_NAME); \
break;
#define MAP_BUILTIN_CCHAR_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), \
#SWIFT_TYPE_NAME); \
break;
#include "swift/ClangImporter/BuiltinMappedTypes.def"
default:
break;
}
if (swiftType) {
if (swiftType->is<NominalType>() || swiftType->isVoid()) {
return swiftType->getStringAsComponent();
}
}
return "_";
}
std::string VisitRecordType(const clang::RecordType *type) {
auto tagDecl = type->getAsTagDecl();
if (auto namedArg = dyn_cast_or_null<clang::NamedDecl>(tagDecl)) {
llvm::SmallString<128> storage;
llvm::raw_svector_ostream buffer(storage);
nameImporter->importName(namedArg, version, clang::DeclarationName())
.getDeclName()
.print(buffer);
return buffer.str().str();
}
return "_";
}
std::string VisitPointerType(const clang::PointerType *type) {
std::string pointeeResult = Visit(type->getPointeeType().getTypePtr());
enum class TagTypeDecorator { None, UnsafePointer, UnsafeMutablePointer };
// If this is a pointer to foreign reference type, we should not wrap
// it in Unsafe(Mutable)?Pointer, since it will be imported as a class
// in Swift.
bool isReferenceType = false;
if (auto tagDecl = type->getPointeeType()->getAsTagDecl()) {
if (auto *rd = dyn_cast<clang::RecordDecl>(tagDecl))
isReferenceType =
ClangImporter::Implementation::recordHasReferenceSemantics(
rd, swiftCtx);
}
TagTypeDecorator decorator;
if (!isReferenceType)
decorator = type->getPointeeType().isConstQualified()
? TagTypeDecorator::UnsafePointer
: TagTypeDecorator::UnsafeMutablePointer;
else
decorator = TagTypeDecorator::None;
llvm::SmallString<128> storage;
llvm::raw_svector_ostream buffer(storage);
if (decorator != TagTypeDecorator::None)
buffer << (decorator == TagTypeDecorator::UnsafePointer
? "UnsafePointer"
: "UnsafeMutablePointer")
<< '<';
buffer << pointeeResult;
if (decorator != TagTypeDecorator::None)
buffer << '>';
return buffer.str().str();
}
std::string VisitFunctionProtoType(const clang::FunctionProtoType *type) {
llvm::SmallString<128> storage;
llvm::raw_svector_ostream buffer(storage);
buffer << "((";
llvm::interleaveComma(type->getParamTypes(), buffer,
[&](const clang::QualType ¶mType) {
buffer << Visit(paramType.getTypePtr());
});
buffer << ") -> ";
buffer << Visit(type->getReturnType().getTypePtr());
buffer << ")";
return buffer.str().str();
}
};
std::string swift::importer::printClassTemplateSpecializationName(
const clang::ClassTemplateSpecializationDecl *decl, ASTContext &swiftCtx,
NameImporter *nameImporter, ImportNameVersion version) {
TemplateInstantiationNamePrinter templateNamePrinter(swiftCtx, nameImporter,
version);
// TODO: the following logic should probably be a ConstTemplateArgumentVisitor
llvm::SmallString<128> storage;
llvm::raw_svector_ostream buffer(storage);
decl->printName(buffer);
buffer << "<";
llvm::interleaveComma(
decl->getTemplateArgs().asArray(), buffer,
[&buffer, &templateNamePrinter](const clang::TemplateArgument &arg) {
// Use import name here so builtin types such as "int" map to their
// Swift equivalent ("CInt").
if (arg.getKind() == clang::TemplateArgument::Type) {
auto ty = arg.getAsType().getTypePtr();
buffer << templateNamePrinter.Visit(ty);
return;
} else if (arg.getKind() == clang::TemplateArgument::Integral) {
buffer << "_";
if (arg.getIntegralType()->isBuiltinType()) {
buffer << templateNamePrinter.Visit(
arg.getIntegralType().getTypePtr())
<< "_";
}
arg.getAsIntegral().print(buffer, true);
return;
}
buffer << "_";
});
buffer << ">";
return buffer.str().str();
}
|