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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
//===--- ClangSyntaxPrinter.cpp - Printer for C and C++ code ----*- 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 "ClangSyntaxPrinter.h"
#include "PrimitiveTypeMapping.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
#include "swift/AST/SwiftNameTranslation.h"
#include "swift/AST/TypeCheckRequests.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/NestedNameSpecifier.h"
using namespace swift;
using namespace cxx_synthesis;
StringRef cxx_synthesis::getCxxSwiftNamespaceName() { return "swift"; }
StringRef cxx_synthesis::getCxxImplNamespaceName() { return "_impl"; }
StringRef cxx_synthesis::getCxxOpaqueStorageClassName() {
return "OpaqueStorage";
}
bool ClangSyntaxPrinter::isClangKeyword(StringRef name) {
static const llvm::DenseSet<StringRef> keywords = [] {
llvm::DenseSet<StringRef> set;
// FIXME: clang::IdentifierInfo /nearly/ has the API we need to do this
// in a more principled way, but not quite.
#define KEYWORD(SPELLING, FLAGS) set.insert(#SPELLING);
#define CXX_KEYWORD_OPERATOR(SPELLING, TOK) set.insert(#SPELLING);
#include "clang/Basic/TokenKinds.def"
return set;
}();
return keywords.contains(name);
}
bool ClangSyntaxPrinter::isClangKeyword(Identifier name) {
if (name.empty())
return false;
return ClangSyntaxPrinter::isClangKeyword(name.str());
}
void ClangSyntaxPrinter::printIdentifier(StringRef name) const {
os << name;
if (ClangSyntaxPrinter::isClangKeyword(name))
os << '_';
}
void ClangSyntaxPrinter::printBaseName(const ValueDecl *decl) const {
assert(decl->getName().isSimpleName());
printIdentifier(cxx_translation::getNameForCxx(decl));
}
void ClangSyntaxPrinter::printModuleNameCPrefix(const ModuleDecl &mod) {
os << mod.getName().str() << '_';
}
void ClangSyntaxPrinter::printModuleNamespaceQualifiersIfNeeded(
const ModuleDecl *referencedModule, const ModuleDecl *currentContext) {
if (referencedModule == currentContext)
return;
printBaseName(referencedModule);
os << "::";
}
bool ClangSyntaxPrinter::printNominalTypeOutsideMemberDeclTemplateSpecifiers(
const NominalTypeDecl *typeDecl) {
// FIXME: Full qualifiers for nested types?
if (!typeDecl->isGeneric())
return true;
printGenericSignature(
typeDecl->getGenericSignature().getCanonicalSignature());
return false;
}
bool ClangSyntaxPrinter::printNominalTypeOutsideMemberDeclInnerStaticAssert(
const NominalTypeDecl *typeDecl) {
if (!typeDecl->isGeneric())
return true;
printGenericSignatureInnerStaticAsserts(
typeDecl->getGenericSignature().getCanonicalSignature());
return false;
}
void ClangSyntaxPrinter::printClangTypeReference(const clang::Decl *typeDecl) {
if (cast<clang::NamedDecl>(typeDecl)->getDeclName().isEmpty() &&
isa<clang::TagDecl>(typeDecl)) {
if (auto *tnd =
cast<clang::TagDecl>(typeDecl)->getTypedefNameForAnonDecl()) {
printClangTypeReference(tnd);
return;
}
}
auto &clangCtx = typeDecl->getASTContext();
clang::PrintingPolicy pp(clangCtx.getLangOpts());
const auto *NS = clang::NestedNameSpecifier::getRequiredQualification(
clangCtx, clangCtx.getTranslationUnitDecl(),
typeDecl->getLexicalDeclContext());
if (NS)
NS->print(os, pp);
assert(cast<clang::NamedDecl>(typeDecl)->getDeclName().isIdentifier());
os << cast<clang::NamedDecl>(typeDecl)->getName();
if (auto *ctd = dyn_cast<clang::ClassTemplateSpecializationDecl>(typeDecl)) {
if (ctd->getTemplateArgs().size()) {
os << '<';
llvm::interleaveComma(ctd->getTemplateArgs().asArray(), os,
[&](const clang::TemplateArgument &arg) {
arg.print(pp, os, /*IncludeType=*/true);
});
os << '>';
}
}
}
void ClangSyntaxPrinter::printNominalTypeReference(
const NominalTypeDecl *typeDecl, const ModuleDecl *moduleContext) {
if (typeDecl->hasClangNode()) {
printClangTypeReference(typeDecl->getClangDecl());
return;
}
printModuleNamespaceQualifiersIfNeeded(typeDecl->getModuleContext(),
moduleContext);
// FIXME: Full qualifiers for nested types?
ClangSyntaxPrinter(os).printBaseName(typeDecl);
if (typeDecl->isGeneric())
printGenericSignatureParams(
typeDecl->getGenericSignature().getCanonicalSignature());
}
void ClangSyntaxPrinter::printNominalTypeQualifier(
const NominalTypeDecl *typeDecl, const ModuleDecl *moduleContext) {
printNominalTypeReference(typeDecl, moduleContext);
os << "::";
}
void ClangSyntaxPrinter::printModuleNamespaceStart(
const ModuleDecl &moduleContext) const {
os << "namespace ";
printBaseName(&moduleContext);
os << " SWIFT_PRIVATE_ATTR";
printSymbolUSRAttribute(&moduleContext);
os << " {\n";
}
/// Print a C++ namespace declaration with the give name and body.
void ClangSyntaxPrinter::printNamespace(
llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia, const ModuleDecl *moduleContext) const {
os << "namespace ";
namePrinter(os);
if (trivia == NamespaceTrivia::AttributeSwiftPrivate)
os << " SWIFT_PRIVATE_ATTR";
if (moduleContext)
printSymbolUSRAttribute(moduleContext);
os << " {\n\n";
bodyPrinter(os);
os << "\n} // namespace ";
namePrinter(os);
os << "\n\n";
}
void ClangSyntaxPrinter::printNamespace(
StringRef name, llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia) const {
printNamespace([&](raw_ostream &os) { os << name; }, bodyPrinter, trivia);
}
void ClangSyntaxPrinter::printExternC(
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
os << "#ifdef __cplusplus\n";
os << "extern \"C\" {\n";
os << "#endif\n\n";
bodyPrinter(os);
os << "\n#ifdef __cplusplus\n";
os << "}\n";
os << "#endif\n";
}
void ClangSyntaxPrinter::printObjCBlock(
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
os << "#if defined(__OBJC__)\n";
bodyPrinter(os);
os << "\n#endif\n";
}
void ClangSyntaxPrinter::printSwiftImplQualifier() const {
os << "swift::" << cxx_synthesis::getCxxImplNamespaceName() << "::";
}
void ClangSyntaxPrinter::printInlineForThunk() const {
os << "SWIFT_INLINE_THUNK ";
}
void ClangSyntaxPrinter::printInlineForHelperFunction() const {
os << "SWIFT_INLINE_PRIVATE_HELPER ";
}
void ClangSyntaxPrinter::printNullability(
std::optional<OptionalTypeKind> kind,
NullabilityPrintKind printKind) const {
if (!kind)
return;
switch (printKind) {
case NullabilityPrintKind::ContextSensitive:
switch (*kind) {
case OTK_None:
os << "nonnull";
break;
case OTK_Optional:
os << "nullable";
break;
case OTK_ImplicitlyUnwrappedOptional:
os << "null_unspecified";
break;
}
break;
case NullabilityPrintKind::After:
os << ' ';
LLVM_FALLTHROUGH;
case NullabilityPrintKind::Before:
switch (*kind) {
case OTK_None:
os << "_Nonnull";
break;
case OTK_Optional:
os << "_Nullable";
break;
case OTK_ImplicitlyUnwrappedOptional:
os << "_Null_unspecified";
break;
}
break;
}
if (printKind != NullabilityPrintKind::After)
os << ' ';
}
void ClangSyntaxPrinter::printSwiftTypeMetadataAccessFunctionCall(
StringRef name, ArrayRef<GenericRequirement> requirements) {
os << name << "(0";
printGenericRequirementsInstantiantions(requirements, LeadingTrivia::Comma);
os << ')';
}
void ClangSyntaxPrinter::printValueWitnessTableAccessSequenceFromTypeMetadata(
StringRef metadataVariable, StringRef vwTableVariable, int indent) {
os << std::string(indent, ' ');
os << "auto *vwTableAddr = ";
os << "reinterpret_cast<";
printSwiftImplQualifier();
os << "ValueWitnessTable **>(" << metadataVariable << "._0) - 1;\n";
os << "#ifdef __arm64e__\n";
os << std::string(indent, ' ');
os << "auto *" << vwTableVariable << " = ";
os << "reinterpret_cast<";
printSwiftImplQualifier();
os << "ValueWitnessTable *>(ptrauth_auth_data(";
os << "reinterpret_cast<void *>(*vwTableAddr), "
"ptrauth_key_process_independent_data, ";
os << "ptrauth_blend_discriminator(vwTableAddr, "
<< SpecialPointerAuthDiscriminators::ValueWitnessTable << ")));\n";
os << "#else\n";
os << std::string(indent, ' ');
os << "auto *" << vwTableVariable << " = *vwTableAddr;\n";
os << "#endif\n";
}
void ClangSyntaxPrinter::printCTypeMetadataTypeFunction(
const TypeDecl *typeDecl, StringRef typeMetadataFuncName,
llvm::ArrayRef<GenericRequirement> genericRequirements) {
// FIXME: Support generic requirements > 3.
if (!genericRequirements.empty())
os << "static_assert(" << genericRequirements.size()
<< " <= " << NumDirectGenericTypeMetadataAccessFunctionArgs
<< ", \"unsupported generic requirement list for metadata func\");\n";
os << "// Type metadata accessor for " << typeDecl->getNameStr() << "\n";
os << "SWIFT_EXTERN ";
printSwiftImplQualifier();
os << "MetadataResponseTy " << typeMetadataFuncName << '(';
printSwiftImplQualifier();
os << "MetadataRequestTy";
if (!genericRequirements.empty())
os << ", ";
llvm::interleaveComma(genericRequirements, os,
[&](const GenericRequirement &) {
// FIXME: Print parameter name.
os << "void * _Nonnull";
});
os << ')';
os << " SWIFT_NOEXCEPT SWIFT_CALL;\n\n";
}
void ClangSyntaxPrinter::printGenericTypeParamTypeName(
const GenericTypeParamType *gtpt) {
os << "T_" << gtpt->getDepth() << '_' << gtpt->getIndex();
}
void ClangSyntaxPrinter::printGenericSignature(
GenericSignature signature) {
os << "template<";
llvm::interleaveComma(signature.getInnermostGenericParams(), os,
[&](const GenericTypeParamType *genericParamType) {
os << "class ";
printGenericTypeParamTypeName(genericParamType);
});
os << ">\n";
os << "#ifdef __cpp_concepts\n";
os << "requires ";
llvm::interleave(
signature.getInnermostGenericParams(), os,
[&](const GenericTypeParamType *genericParamType) {
os << "swift::isUsableInGenericContext<";
printGenericTypeParamTypeName(genericParamType);
os << ">";
},
" && ");
os << "\n#endif // __cpp_concepts\n";
}
void ClangSyntaxPrinter::printGenericSignatureInnerStaticAsserts(
GenericSignature signature) {
os << "#ifndef __cpp_concepts\n";
llvm::interleave(
signature.getInnermostGenericParams(), os,
[&](const GenericTypeParamType *genericParamType) {
os << "static_assert(swift::isUsableInGenericContext<";
printGenericTypeParamTypeName(genericParamType);
os << ">, \"type cannot be used in a Swift generic context\");";
},
"\n");
os << "\n#endif // __cpp_concepts\n";
}
void ClangSyntaxPrinter::printGenericSignatureParams(
GenericSignature signature) {
os << '<';
llvm::interleaveComma(signature.getInnermostGenericParams(), os,
[&](const GenericTypeParamType *genericParamType) {
printGenericTypeParamTypeName(genericParamType);
});
os << '>';
}
void ClangSyntaxPrinter::printGenericRequirementInstantiantion(
const GenericRequirement &requirement) {
assert(requirement.isAnyMetadata() &&
"protocol requirements not supported yet!");
auto *gtpt = requirement.getTypeParameter()->getAs<GenericTypeParamType>();
assert(gtpt && "unexpected generic param type");
os << "swift::TypeMetadataTrait<";
printGenericTypeParamTypeName(gtpt);
os << ">::getTypeMetadata()";
}
void ClangSyntaxPrinter::printGenericRequirementsInstantiantions(
ArrayRef<GenericRequirement> requirements, LeadingTrivia leadingTrivia) {
if (leadingTrivia == LeadingTrivia::Comma && !requirements.empty())
os << ", ";
llvm::interleaveComma(requirements, os,
[&](const GenericRequirement &requirement) {
printGenericRequirementInstantiantion(requirement);
});
}
void ClangSyntaxPrinter::printPrimaryCxxTypeName(
const NominalTypeDecl *type, const ModuleDecl *moduleContext) {
printModuleNamespaceQualifiersIfNeeded(type->getModuleContext(),
moduleContext);
// FIXME: Print class qualifiers for nested class references.
printBaseName(type);
}
void ClangSyntaxPrinter::printIncludeForShimHeader(StringRef headerName) {
printIgnoredDiagnosticBlock("non-modular-include-in-framework-module", [&] {
os << "// Allow user to find the header using additional include paths\n";
os << "#if __has_include(<swiftToCxx/" << headerName << ">)\n";
os << "#include <swiftToCxx/" << headerName << ">\n";
os << "// Look for the C++ interop support header relative to clang's "
"resource dir:\n";
os << "// "
"'<toolchain>/usr/lib/clang/<version>/include/../../../swift/"
"swiftToCxx'.\n";
os << "#elif __has_include(<../../../swift/swiftToCxx/" << headerName
<< ">)\n";
os << "#include <../../../swift/swiftToCxx/" << headerName << ">\n";
os << "#elif __has_include(<../../../../../lib/swift/swiftToCxx/"
<< headerName << ">)\n";
os << "// "
"'<toolchain>/usr/local/lib/clang/<version>/include/../../../../../"
"lib/"
"swift/swiftToCxx'.\n";
os << "#include <../../../../../lib/swift/swiftToCxx/" << headerName
<< ">\n";
os << "#endif\n";
});
}
void ClangSyntaxPrinter::printDefine(StringRef macroName) {
os << "#define " << macroName << "\n";
}
void ClangSyntaxPrinter::printIgnoredDiagnosticBlock(
StringRef diagName, llvm::function_ref<void()> bodyPrinter) {
os << "#pragma clang diagnostic push\n";
os << "#pragma clang diagnostic ignored \"-W" << diagName << "\"\n";
bodyPrinter();
os << "#pragma clang diagnostic pop\n";
}
void ClangSyntaxPrinter::printIgnoredCxx17ExtensionDiagnosticBlock(
llvm::function_ref<void()> bodyPrinter) {
printIgnoredDiagnosticBlock("c++17-extensions", bodyPrinter);
}
void ClangSyntaxPrinter::printSymbolUSRAttribute(const ValueDecl *D) const {
if (isa<ModuleDecl>(D)) {
os << " SWIFT_SYMBOL_MODULE(\"";
printBaseName(D);
os << "\")";
return;
}
auto result = evaluateOrDefault(D->getASTContext().evaluator,
USRGenerationRequest{D}, std::string());
if (result.empty())
return;
os << " SWIFT_SYMBOL(\"" << result << "\")";
}
void ClangSyntaxPrinter::printKnownCType(
Type t, PrimitiveTypeMapping &typeMapping) const {
auto info =
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
assert(info.has_value() && "not a known type");
os << info->name;
if (info->canBeNullable)
os << " _Null_unspecified";
}
void ClangSyntaxPrinter::printSwiftMangledNameForDebugger(
const NominalTypeDecl *typeDecl) {
printIgnoredCxx17ExtensionDiagnosticBlock([&]() {
os << "#pragma clang diagnostic push\n";
os << "#pragma clang diagnostic ignored \"-Wreserved-identifier\"\n";
auto mangled_name = mangler.mangleTypeForDebugger(
typeDecl->getDeclaredInterfaceType(), nullptr);
if (!mangled_name.empty()) {
os << " typedef char " << mangled_name << ";\n";
os << " static inline constexpr " << mangled_name
<< " __swift_mangled_name = 0;\n";
}
});
os << "#pragma clang diagnostic pop\n";
}
|