|
| 1 | +//===--- PrintClangClassType.cpp - Print class types in C/C++ ---*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 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 "PrintClangClassType.h" |
| 14 | +#include "ClangSyntaxPrinter.h" |
| 15 | +#include "PrintClangValueType.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | + |
| 18 | +using namespace swift; |
| 19 | + |
| 20 | +void ClangClassTypePrinter::printClassTypeDecl( |
| 21 | + const ClassDecl *typeDecl, llvm::function_ref<void(void)> bodyPrinter) { |
| 22 | + auto printCxxImplClassName = ClangValueTypePrinter::printCxxImplClassName; |
| 23 | + |
| 24 | + ClangSyntaxPrinter printer(os); |
| 25 | + |
| 26 | + // Print out a forward declaration of the "hidden" _impl class. |
| 27 | + printer.printNamespace(cxx_synthesis::getCxxImplNamespaceName(), |
| 28 | + [&](raw_ostream &os) { |
| 29 | + os << "class "; |
| 30 | + printCxxImplClassName(os, typeDecl); |
| 31 | + os << ";\n"; |
| 32 | + }); |
| 33 | + |
| 34 | + os << "class "; |
| 35 | + printer.printBaseName(typeDecl); |
| 36 | + // FIXME: Add support for inherintance. |
| 37 | + os << " final"; |
| 38 | + os << " {\n"; |
| 39 | + os << "public:\n"; |
| 40 | + |
| 41 | + // Destructor releases the object. |
| 42 | + os << " inline ~"; |
| 43 | + printer.printBaseName(typeDecl); |
| 44 | + os << "() { swift::" << cxx_synthesis::getCxxImplNamespaceName() |
| 45 | + << "::swift_release(_opaquePointer); }\n"; |
| 46 | + |
| 47 | + // FIXME: move semantics should be restricted? |
| 48 | + os << " inline "; |
| 49 | + printer.printBaseName(typeDecl); |
| 50 | + os << "("; |
| 51 | + printer.printBaseName(typeDecl); |
| 52 | + os << "&&) noexcept = default;\n"; |
| 53 | + |
| 54 | + os << "private:\n"; |
| 55 | + os << " inline "; |
| 56 | + printer.printBaseName(typeDecl); |
| 57 | + os << "(void * _Nonnull ptr) noexcept : _opaquePointer(ptr) {}\n"; |
| 58 | + os << "\n void * _Nonnull _opaquePointer;\n"; |
| 59 | + os << " friend class " << cxx_synthesis::getCxxImplNamespaceName() << "::"; |
| 60 | + printCxxImplClassName(os, typeDecl); |
| 61 | + os << ";\n"; |
| 62 | + os << "};\n\n"; |
| 63 | + |
| 64 | + // Print out the "hidden" _impl class. |
| 65 | + printer.printNamespace( |
| 66 | + cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &os) { |
| 67 | + os << "class "; |
| 68 | + printCxxImplClassName(os, typeDecl); |
| 69 | + os << " {\n"; |
| 70 | + os << "public:\n"; |
| 71 | + os << "static inline "; |
| 72 | + printer.printBaseName(typeDecl); |
| 73 | + os << " fromUnretained(void * _Nonnull ptr) noexcept { return "; |
| 74 | + printer.printBaseName(typeDecl); |
| 75 | + os << "(ptr); }\n"; |
| 76 | + os << "};\n"; |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +void ClangClassTypePrinter::printClassTypeReturnScaffold( |
| 81 | + raw_ostream &os, const ClassDecl *type, const ModuleDecl *moduleContext, |
| 82 | + llvm::function_ref<void(void)> bodyPrinter) { |
| 83 | + os << " return "; |
| 84 | + ClangSyntaxPrinter(os).printModuleNamespaceQualifiersIfNeeded( |
| 85 | + type->getModuleContext(), moduleContext); |
| 86 | + os << cxx_synthesis::getCxxImplNamespaceName() << "::"; |
| 87 | + ClangValueTypePrinter::printCxxImplClassName(os, type); |
| 88 | + os << "::fromUnretained("; |
| 89 | + bodyPrinter(); |
| 90 | + os << ");\n"; |
| 91 | +} |
0 commit comments