|
| 1 | +//===--- ClangClassTemplateNamePrinter.cpp --------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | +#include "ClangClassTemplateNamePrinter.h" |
| 14 | +#include "ImporterImpl.h" |
| 15 | +#include "clang/AST/TypeVisitor.h" |
| 16 | + |
| 17 | +using namespace swift; |
| 18 | +using namespace swift::importer; |
| 19 | + |
| 20 | +struct TemplateInstantiationNamePrinter |
| 21 | + : clang::TypeVisitor<TemplateInstantiationNamePrinter, std::string> { |
| 22 | + ASTContext &swiftCtx; |
| 23 | + NameImporter *nameImporter; |
| 24 | + ImportNameVersion version; |
| 25 | + |
| 26 | + TemplateInstantiationNamePrinter(ASTContext &swiftCtx, |
| 27 | + NameImporter *nameImporter, |
| 28 | + ImportNameVersion version) |
| 29 | + : swiftCtx(swiftCtx), nameImporter(nameImporter), version(version) {} |
| 30 | + |
| 31 | + std::string VisitBuiltinType(const clang::BuiltinType *type) { |
| 32 | + Type swiftType = nullptr; |
| 33 | + switch (type->getKind()) { |
| 34 | + case clang::BuiltinType::Void: |
| 35 | + swiftType = |
| 36 | + swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), "Void"); |
| 37 | + break; |
| 38 | +#define MAP_BUILTIN_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \ |
| 39 | + case clang::BuiltinType::CLANG_BUILTIN_KIND: \ |
| 40 | + swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), \ |
| 41 | + #SWIFT_TYPE_NAME); \ |
| 42 | + break; |
| 43 | +#define MAP_BUILTIN_CCHAR_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \ |
| 44 | + case clang::BuiltinType::CLANG_BUILTIN_KIND: \ |
| 45 | + swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), \ |
| 46 | + #SWIFT_TYPE_NAME); \ |
| 47 | + break; |
| 48 | +#include "swift/ClangImporter/BuiltinMappedTypes.def" |
| 49 | + default: |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + if (swiftType) { |
| 54 | + if (swiftType->is<NominalType>()) { |
| 55 | + return swiftType->getStringAsComponent(); |
| 56 | + } |
| 57 | + } |
| 58 | + return "_"; |
| 59 | + } |
| 60 | + |
| 61 | + std::string VisitRecordType(const clang::RecordType *type) { |
| 62 | + auto tagDecl = type->getAsTagDecl(); |
| 63 | + if (auto namedArg = dyn_cast_or_null<clang::NamedDecl>(tagDecl)) { |
| 64 | + llvm::SmallString<128> storage; |
| 65 | + llvm::raw_svector_ostream buffer(storage); |
| 66 | + nameImporter->importName(namedArg, version, clang::DeclarationName()) |
| 67 | + .getDeclName() |
| 68 | + .print(buffer); |
| 69 | + return buffer.str().str(); |
| 70 | + } |
| 71 | + return "_"; |
| 72 | + } |
| 73 | + |
| 74 | + std::string VisitPointerType(const clang::PointerType *type) { |
| 75 | + std::string pointeeResult = Visit(type->getPointeeType().getTypePtr()); |
| 76 | + |
| 77 | + enum class TagTypeDecorator { None, UnsafePointer, UnsafeMutablePointer }; |
| 78 | + |
| 79 | + // If this is a pointer to foreign reference type, we should not wrap |
| 80 | + // it in Unsafe(Mutable)?Pointer, since it will be imported as a class |
| 81 | + // in Swift. |
| 82 | + bool isReferenceType = false; |
| 83 | + if (auto tagDecl = type->getPointeeType()->getAsTagDecl()) { |
| 84 | + if (auto *rd = dyn_cast<clang::RecordDecl>(tagDecl)) |
| 85 | + isReferenceType = |
| 86 | + ClangImporter::Implementation::recordHasReferenceSemantics( |
| 87 | + rd, swiftCtx); |
| 88 | + } |
| 89 | + |
| 90 | + TagTypeDecorator decorator; |
| 91 | + if (!isReferenceType) |
| 92 | + decorator = type->getPointeeType().isConstQualified() |
| 93 | + ? TagTypeDecorator::UnsafePointer |
| 94 | + : TagTypeDecorator::UnsafeMutablePointer; |
| 95 | + else |
| 96 | + decorator = TagTypeDecorator::None; |
| 97 | + |
| 98 | + llvm::SmallString<128> storage; |
| 99 | + llvm::raw_svector_ostream buffer(storage); |
| 100 | + if (decorator != TagTypeDecorator::None) |
| 101 | + buffer << (decorator == TagTypeDecorator::UnsafePointer |
| 102 | + ? "UnsafePointer" |
| 103 | + : "UnsafeMutablePointer") |
| 104 | + << '<'; |
| 105 | + buffer << pointeeResult; |
| 106 | + if (decorator != TagTypeDecorator::None) |
| 107 | + buffer << '>'; |
| 108 | + |
| 109 | + return buffer.str().str(); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +std::string swift::importer::printClassTemplateSpecializationName( |
| 114 | + const clang::ClassTemplateSpecializationDecl *decl, ASTContext &swiftCtx, |
| 115 | + NameImporter *nameImporter, ImportNameVersion version) { |
| 116 | + TemplateInstantiationNamePrinter templateNamePrinter(swiftCtx, nameImporter, |
| 117 | + version); |
| 118 | + |
| 119 | + // TODO: the following logic should probably be a ConstTemplateArgumentVisitor |
| 120 | + llvm::SmallString<128> storage; |
| 121 | + llvm::raw_svector_ostream buffer(storage); |
| 122 | + decl->printName(buffer); |
| 123 | + buffer << "<"; |
| 124 | + llvm::interleaveComma( |
| 125 | + decl->getTemplateArgs().asArray(), buffer, |
| 126 | + [&buffer, &templateNamePrinter](const clang::TemplateArgument &arg) { |
| 127 | + // Use import name here so builtin types such as "int" map to their |
| 128 | + // Swift equivalent ("CInt"). |
| 129 | + if (arg.getKind() == clang::TemplateArgument::Type) { |
| 130 | + auto ty = arg.getAsType().getTypePtr(); |
| 131 | + buffer << templateNamePrinter.Visit(ty); |
| 132 | + return; |
| 133 | + } else if (arg.getKind() == clang::TemplateArgument::Integral) { |
| 134 | + buffer << "_"; |
| 135 | + if (arg.getIntegralType()->isBuiltinType()) { |
| 136 | + buffer << templateNamePrinter.Visit( |
| 137 | + arg.getIntegralType().getTypePtr()) |
| 138 | + << "_"; |
| 139 | + } |
| 140 | + arg.getAsIntegral().print(buffer, true); |
| 141 | + return; |
| 142 | + } |
| 143 | + buffer << "_"; |
| 144 | + }); |
| 145 | + buffer << ">"; |
| 146 | + return buffer.str().str(); |
| 147 | +} |
0 commit comments