|
| 1 | +//===--- PrintSwiftToClangCoreScaffold.cpp - Print core decls ---*- 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 "PrintSwiftToClangCoreScaffold.h" |
| 14 | +#include "ClangSyntaxPrinter.h" |
| 15 | +#include "PrimitiveTypeMapping.h" |
| 16 | +#include "SwiftToClangInteropContext.h" |
| 17 | +#include "swift/AST/Decl.h" |
| 18 | +#include "swift/AST/Type.h" |
| 19 | +#include "swift/IRGen/IRABIDetailsProvider.h" |
| 20 | + |
| 21 | +using namespace swift; |
| 22 | + |
| 23 | +static void printKnownCType(Type t, PrimitiveTypeMapping &typeMapping, |
| 24 | + raw_ostream &os) { |
| 25 | + auto info = |
| 26 | + typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal()); |
| 27 | + assert(info.hasValue() && "not a known type"); |
| 28 | + os << info->name; |
| 29 | + if (info->canBeNullable) |
| 30 | + os << " _Null_unspecified"; |
| 31 | +} |
| 32 | + |
| 33 | +static void printKnownStruct( |
| 34 | + PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name, |
| 35 | + const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) { |
| 36 | + assert(typeRecord.getMembers().size() > 1); |
| 37 | + os << "struct " << name << " {\n"; |
| 38 | + for (const auto &ty : llvm::enumerate(typeRecord.getMembers())) { |
| 39 | + os << " "; |
| 40 | + printKnownCType(ty.value(), typeMapping, os); |
| 41 | + os << " _" << ty.index() << ";\n"; |
| 42 | + } |
| 43 | + os << "};\n"; |
| 44 | +} |
| 45 | + |
| 46 | +static void printKnownTypedef( |
| 47 | + PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name, |
| 48 | + const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) { |
| 49 | + assert(typeRecord.getMembers().size() == 1); |
| 50 | + os << "typedef "; |
| 51 | + printKnownCType(typeRecord.getMembers()[0], typeMapping, os); |
| 52 | + os << " " << name << ";\n"; |
| 53 | +} |
| 54 | + |
| 55 | +static void printKnownType( |
| 56 | + PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name, |
| 57 | + const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) { |
| 58 | + if (typeRecord.getMembers().size() == 1) |
| 59 | + return printKnownTypedef(typeMapping, os, name, typeRecord); |
| 60 | + printKnownStruct(typeMapping, os, name, typeRecord); |
| 61 | +} |
| 62 | + |
| 63 | +static void printTypeMetadataResponseType(SwiftToClangInteropContext &ctx, |
| 64 | + PrimitiveTypeMapping &typeMapping, |
| 65 | + raw_ostream &os) { |
| 66 | + os << "// Swift type metadata response type.\n"; |
| 67 | + // Print out the type metadata structure. |
| 68 | + auto funcSig = ctx.getIrABIDetails().getTypeMetadataAccessFunctionSignature(); |
| 69 | + printKnownType(typeMapping, os, "MetadataResponseTy", funcSig.returnType); |
| 70 | + assert(funcSig.parameterTypes.size() == 1); |
| 71 | + os << "// Swift type metadata request type.\n"; |
| 72 | + printKnownType(typeMapping, os, "MetadataRequestTy", |
| 73 | + funcSig.parameterTypes[0]); |
| 74 | +} |
| 75 | + |
| 76 | +void swift::printSwiftToClangCoreScaffold(SwiftToClangInteropContext &ctx, |
| 77 | + PrimitiveTypeMapping &typeMapping, |
| 78 | + raw_ostream &os) { |
| 79 | + ClangSyntaxPrinter printer(os); |
| 80 | + printer.printNamespace("swift", [&](raw_ostream &) { |
| 81 | + printer.printNamespace( |
| 82 | + cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &) { |
| 83 | + printer.printExternC([&](raw_ostream &os) { |
| 84 | + printTypeMetadataResponseType(ctx, typeMapping, os); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
0 commit comments