1414#include " ClangSyntaxPrinter.h"
1515#include " PrimitiveTypeMapping.h"
1616#include " SwiftToClangInteropContext.h"
17+ #include " swift/ABI/MetadataValues.h"
1718#include " swift/AST/Decl.h"
1819#include " swift/AST/Type.h"
1920#include " swift/IRGen/IRABIDetailsProvider.h"
21+ #include " llvm/ADT/STLExtras.h"
2022
2123using namespace swift ;
2224
@@ -60,6 +62,49 @@ static void printKnownType(
6062 printKnownStruct (typeMapping, os, name, typeRecord);
6163}
6264
65+ static void printValueWitnessTableFunctionType (raw_ostream &os, StringRef name,
66+ StringRef returnType,
67+ std::string paramTypes,
68+ uint16_t ptrauthDisc) {
69+ os << " using ValueWitness" << name << " Ty = " << returnType << " (*)("
70+ << paramTypes << " ) __ptrauth_swift_value_witness_function_pointer("
71+ << ptrauthDisc << " );\n " ;
72+ }
73+
74+ static std::string makeParams (const char *arg) { return arg; }
75+
76+ template <class ... T>
77+ static std::string makeParams (const char *arg, const T... args) {
78+ return std::string (arg) + " , " + makeParams (args...);
79+ }
80+
81+ static void printValueWitnessTable (raw_ostream &os) {
82+ std::string members;
83+ llvm::raw_string_ostream membersOS (members);
84+
85+ #define WANT_ONLY_REQUIRED_VALUE_WITNESSES
86+ #define DATA_VALUE_WITNESS (lowerId, upperId, type ) \
87+ membersOS << " " << type << " " << #lowerId << " ;\n " ;
88+ #define FUNCTION_VALUE_WITNESS (lowerId, upperId, returnType, paramTypes ) \
89+ printValueWitnessTableFunctionType ( \
90+ os, #upperId, returnType, makeParams paramTypes, \
91+ SpecialPointerAuthDiscriminators::upperId); \
92+ membersOS << " ValueWitness" << #upperId << " Ty _Nonnull " << #lowerId \
93+ << " ;\n " ;
94+ #define MUTABLE_VALUE_TYPE " void * _Nonnull"
95+ #define IMMUTABLE_VALUE_TYPE " const void * _Nonnull"
96+ #define MUTABLE_BUFFER_TYPE " void * _Nonnull"
97+ #define IMMUTABLE_BUFFER_TYPE " const void * _Nonnull"
98+ #define TYPE_TYPE " void * _Nonnull"
99+ #define SIZE_TYPE " size_t"
100+ #define INT_TYPE " int"
101+ #define UINT_TYPE " unsigned"
102+ #define VOID_TYPE " void"
103+ #include " swift/ABI/ValueWitness.def"
104+
105+ os << " \n struct ValueWitnessTable {\n " << membersOS.str () << " };\n " ;
106+ }
107+
63108static void printTypeMetadataResponseType (SwiftToClangInteropContext &ctx,
64109 PrimitiveTypeMapping &typeMapping,
65110 raw_ostream &os) {
@@ -82,6 +127,8 @@ void swift::printSwiftToClangCoreScaffold(SwiftToClangInteropContext &ctx,
82127 cxx_synthesis::getCxxImplNamespaceName (), [&](raw_ostream &) {
83128 printer.printExternC ([&](raw_ostream &os) {
84129 printTypeMetadataResponseType (ctx, typeMapping, os);
130+ os << " \n " ;
131+ printValueWitnessTable (os);
85132 });
86133 });
87134 });
0 commit comments