-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathPrintSwiftToClangCoreScaffold.cpp
196 lines (180 loc) · 7.63 KB
/
PrintSwiftToClangCoreScaffold.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
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
//===--- PrintSwiftToClangCoreScaffold.cpp - Print core decls ---*- 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 "PrintSwiftToClangCoreScaffold.h"
#include "ClangSyntaxPrinter.h"
#include "PrimitiveTypeMapping.h"
#include "SwiftToClangInteropContext.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Type.h"
#include "swift/IRGen/IRABIDetailsProvider.h"
#include "llvm/ADT/STLExtras.h"
using namespace swift;
static void printKnownCType(Type t, PrimitiveTypeMapping &typeMapping,
raw_ostream &os) {
auto info =
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
assert(info.hasValue() && "not a known type");
os << info->name;
if (info->canBeNullable)
os << " _Null_unspecified";
}
static void printKnownStruct(
PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name,
const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) {
assert(typeRecord.getMembers().size() > 1);
os << "struct " << name << " {\n";
for (const auto &ty : llvm::enumerate(typeRecord.getMembers())) {
os << " ";
printKnownCType(ty.value(), typeMapping, os);
os << " _" << ty.index() << ";\n";
}
os << "};\n";
}
static void printKnownTypedef(
PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name,
const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) {
assert(typeRecord.getMembers().size() == 1);
os << "typedef ";
printKnownCType(typeRecord.getMembers()[0], typeMapping, os);
os << " " << name << ";\n";
}
static void printKnownType(
PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name,
const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) {
if (typeRecord.getMembers().size() == 1)
return printKnownTypedef(typeMapping, os, name, typeRecord);
printKnownStruct(typeMapping, os, name, typeRecord);
}
static void printValueWitnessTableFunctionType(raw_ostream &os, StringRef name,
StringRef returnType,
std::string paramTypes,
uint16_t ptrauthDisc) {
os << "using ValueWitness" << name << "Ty = " << returnType
<< "(* __ptrauth_swift_value_witness_function_pointer(" << ptrauthDisc
<< "))(" << paramTypes << ");\n";
}
static std::string makeParams(const char *arg) { return arg; }
template <class... T>
static std::string makeParams(const char *arg, const T... args) {
return std::string(arg) + ", " + makeParams(args...);
}
static void printValueWitnessTable(raw_ostream &os) {
std::string members;
llvm::raw_string_ostream membersOS(members);
#define WANT_ONLY_REQUIRED_VALUE_WITNESSES
#define DATA_VALUE_WITNESS(lowerId, upperId, type) \
membersOS << " " << type << " " << #lowerId << ";\n";
#define FUNCTION_VALUE_WITNESS(lowerId, upperId, returnType, paramTypes) \
printValueWitnessTableFunctionType( \
os, #upperId, returnType, makeParams paramTypes, \
SpecialPointerAuthDiscriminators::upperId); \
membersOS << " ValueWitness" << #upperId << "Ty _Nonnull " << #lowerId \
<< ";\n";
#define MUTABLE_VALUE_TYPE "void * _Nonnull"
#define IMMUTABLE_VALUE_TYPE "const void * _Nonnull"
#define MUTABLE_BUFFER_TYPE "void * _Nonnull"
#define IMMUTABLE_BUFFER_TYPE "const void * _Nonnull"
#define TYPE_TYPE "void * _Nonnull"
#define SIZE_TYPE "size_t"
#define INT_TYPE "int"
#define UINT_TYPE "unsigned"
#define VOID_TYPE "void"
#include "swift/ABI/ValueWitness.def"
os << "\nstruct ValueWitnessTable {\n" << membersOS.str() << "};\n";
}
static void printTypeMetadataResponseType(SwiftToClangInteropContext &ctx,
PrimitiveTypeMapping &typeMapping,
raw_ostream &os) {
os << "// Swift type metadata response type.\n";
// Print out the type metadata structure.
auto funcSig = ctx.getIrABIDetails().getTypeMetadataAccessFunctionSignature();
printKnownType(typeMapping, os, "MetadataResponseTy", funcSig.returnType);
assert(funcSig.parameterTypes.size() == 1);
os << "// Swift type metadata request type.\n";
printKnownType(typeMapping, os, "MetadataRequestTy",
funcSig.parameterTypes[0]);
}
static void printOpaqueAllocFee(raw_ostream &os) {
os << R"text(inline void * _Nonnull opaqueAlloc(size_t size, size_t align) {
#if defined(_WIN32)
void *r = _aligned_malloc(size, align);
#else
if (align < sizeof(void *)) align = sizeof(void *);
void *r = nullptr;
int res = posix_memalign(&r, align, size);
(void)res;
#endif
return r;
}
inline void opaqueFree(void * _Nonnull p) {
#if defined(_WIN32)
_aligned_free(p);
#else
free(p);
#endif
}
)text";
}
static void printSwiftResilientStorageClass(raw_ostream &os) {
auto name = cxx_synthesis::getCxxOpaqueStorageClassName();
static_assert(TargetValueWitnessFlags<uint64_t>::AlignmentMask ==
TargetValueWitnessFlags<uint32_t>::AlignmentMask,
"alignment mask doesn't match");
os << "/// Container for an opaque Swift value, like resilient struct.\n";
os << "class " << name << " {\n";
os << "public:\n";
os << " inline " << name << "() noexcept : storage(nullptr) { }\n";
os << " inline " << name
<< "(ValueWitnessTable * _Nonnull vwTable) noexcept : storage("
"reinterpret_cast<char *>(opaqueAlloc(vwTable->size, (vwTable->flags &"
<< TargetValueWitnessFlags<uint64_t>::AlignmentMask << ") + 1))) { }\n";
os << " inline " << name << "(" << name
<< "&& other) noexcept : storage(other.storage) { other.storage = "
"nullptr; }\n";
os << " inline " << name << "(const " << name << "&) noexcept = delete;\n";
os << " inline ~" << name
<< "() noexcept { if (storage) { opaqueFree(static_cast<char "
"* _Nonnull>(storage)); } }\n";
os << " void operator =(" << name
<< "&& other) noexcept { auto temp = storage; storage = other.storage; "
"other.storage = temp; }\n";
os << " void operator =(const " << name << "&) noexcept = delete;\n";
os << " inline char * _Nonnull getOpaquePointer() noexcept { return "
"static_cast<char "
"* _Nonnull>(storage); }\n";
os << " inline const char * _Nonnull getOpaquePointer() const noexcept { "
"return "
"static_cast<char * _Nonnull>(storage); }\n";
os << "private:\n";
os << " char * _Nullable storage;\n";
os << "};\n";
}
void swift::printSwiftToClangCoreScaffold(SwiftToClangInteropContext &ctx,
PrimitiveTypeMapping &typeMapping,
raw_ostream &os) {
ClangSyntaxPrinter printer(os);
printer.printNamespace("swift", [&](raw_ostream &) {
printer.printNamespace(
cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &) {
printer.printExternC([&](raw_ostream &os) {
printTypeMetadataResponseType(ctx, typeMapping, os);
os << "\n";
printValueWitnessTable(os);
});
os << "\n";
printOpaqueAllocFee(os);
os << "\n";
printSwiftResilientStorageClass(os);
});
});
}