-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPrintClangClassType.cpp
145 lines (129 loc) · 5.57 KB
/
PrintClangClassType.cpp
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
//===--- PrintClangClassType.cpp - Print class types in C/C++ ---*- 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 "PrintClangClassType.h"
#include "ClangSyntaxPrinter.h"
#include "DeclAndTypePrinter.h"
#include "PrintClangValueType.h"
#include "swift/AST/Decl.h"
#include "swift/IRGen/Linking.h"
using namespace swift;
void ClangClassTypePrinter::printClassTypeDecl(
const ClassDecl *typeDecl, llvm::function_ref<void(void)> bodyPrinter,
DeclAndTypePrinter &declAndTypePrinter) {
auto printCxxImplClassName = ClangValueTypePrinter::printCxxImplClassName;
ClangSyntaxPrinter printer(typeDecl->getASTContext(), os);
auto typeMetadataFunc = irgen::LinkEntity::forTypeMetadataAccessFunction(
typeDecl->getDeclaredType()->getCanonicalType());
std::string typeMetadataFuncName = typeMetadataFunc.mangleAsString(typeDecl->getASTContext());
printer.printParentNamespaceForNestedTypes(typeDecl, [&](raw_ostream &os) {
// Print out a forward declaration of the "hidden" _impl class.
printer.printNamespace(cxx_synthesis::getCxxImplNamespaceName(),
[&](raw_ostream &os) {
os << "class";
declAndTypePrinter.printAvailability(os, typeDecl);
os << ' ';
printCxxImplClassName(os, typeDecl);
os << ";\n";
// Print out special functions, like functions that
// access type metadata.
printer.printCTypeMetadataTypeFunction(
typeDecl, typeMetadataFuncName, {});
});
std::string baseClassName;
std::string baseClassQualifiedName;
if (auto *parentClass = typeDecl->getSuperclassDecl()) {
llvm::raw_string_ostream baseNameOS(baseClassName);
ClangSyntaxPrinter(typeDecl->getASTContext(), baseNameOS).printBaseName(parentClass);
llvm::raw_string_ostream baseQualNameOS(baseClassQualifiedName);
ClangSyntaxPrinter(typeDecl->getASTContext(), baseQualNameOS)
.printModuleNamespaceQualifiersIfNeeded(
parentClass->getModuleContext(), typeDecl->getModuleContext());
baseQualNameOS << baseNameOS.str();
} else {
baseClassName = "RefCountedClass";
baseClassQualifiedName = "swift::_impl::RefCountedClass";
}
os << "class";
declAndTypePrinter.printAvailability(os, typeDecl);
ClangSyntaxPrinter(typeDecl->getASTContext(), os).printSymbolUSRAttribute(typeDecl);
os << ' ';
printer.printBaseName(typeDecl);
if (typeDecl->isFinal())
os << " final";
os << " : public " << baseClassQualifiedName;
os << " {\n";
os << "public:\n";
os << " using " << baseClassName << "::" << baseClassName << ";\n";
os << " using " << baseClassName << "::operator=;\n";
bodyPrinter();
os << "protected:\n";
os << " ";
printer.printInlineForThunk();
printer.printBaseName(typeDecl);
os << "(void * _Nonnull ptr) noexcept : " << baseClassName << "(ptr) {}\n";
os << "private:\n";
os << " friend class " << cxx_synthesis::getCxxImplNamespaceName() << "::";
printCxxImplClassName(os, typeDecl);
os << ";\n";
printer.printSwiftMangledNameForDebugger(typeDecl);
os << "};\n\n";
// Print out the "hidden" _impl class.
printer.printNamespace(
cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &os) {
os << "class";
declAndTypePrinter.printAvailability(os, typeDecl);
os << ' ';
printCxxImplClassName(os, typeDecl);
os << " {\n";
os << "public:\n";
os << "static ";
printer.printInlineForThunk();
printer.printBaseName(typeDecl);
os << " makeRetained(void * _Nonnull ptr) noexcept { return ";
printer.printBaseName(typeDecl);
os << "(ptr); }\n";
os << "};\n";
});
});
ClangValueTypePrinter::printTypeGenericTraits(
os, typeDecl, typeMetadataFuncName, /*genericRequirements=*/{},
typeDecl->getModuleContext(), declAndTypePrinter);
}
void ClangClassTypePrinter::printClassTypeReturnScaffold(
raw_ostream &os, const ClassDecl *type, const ModuleDecl *moduleContext,
llvm::function_ref<void(void)> bodyPrinter) {
ClangSyntaxPrinter printer(type->getASTContext(), os);
os << " return ";
printer.printModuleNamespaceQualifiersIfNeeded(type->getModuleContext(),
moduleContext);
if (!printer.printNestedTypeNamespaceQualifiers(type))
os << "::";
os << cxx_synthesis::getCxxImplNamespaceName() << "::";
ClangValueTypePrinter::printCxxImplClassName(os, type);
os << "::makeRetained(";
bodyPrinter();
os << ");\n";
}
void ClangClassTypePrinter::printParameterCxxtoCUseScaffold(
raw_ostream &os, const ClassDecl *type, const ModuleDecl *moduleContext,
llvm::function_ref<void(void)> bodyPrinter, bool isInOut) {
if (isInOut)
os << '&';
os << "::swift::" << cxx_synthesis::getCxxImplNamespaceName()
<< "::_impl_RefCountedClass"
<< "::getOpaquePointer";
if (isInOut)
os << "Ref";
os << '(';
bodyPrinter();
os << ')';
}