|
| 1 | +//===--- PrintClangFunction.cpp - Printer for C/C++ functions ---*- 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 "PrintClangFunction.h" |
| 14 | +#include "DeclAndTypePrinter.h" |
| 15 | +#include "PrimitiveTypeMapping.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/ParameterList.h" |
| 18 | +#include "swift/AST/Type.h" |
| 19 | +#include "swift/AST/TypeVisitor.h" |
| 20 | +#include "swift/ClangImporter/ClangImporter.h" |
| 21 | +#include "llvm/ADT/STLExtras.h" |
| 22 | + |
| 23 | +using namespace swift; |
| 24 | + |
| 25 | +// Prints types in the C function signature that corresponds to the |
| 26 | +// native Swift function/method. |
| 27 | +class CFunctionSignatureTypePrinter |
| 28 | + : public TypeVisitor<CFunctionSignatureTypePrinter, void, |
| 29 | + Optional<OptionalTypeKind>> { |
| 30 | +public: |
| 31 | + CFunctionSignatureTypePrinter(raw_ostream &os, |
| 32 | + PrimitiveTypeMapping &typeMapping) |
| 33 | + : os(os), typeMapping(typeMapping) {} |
| 34 | + |
| 35 | + bool printIfKnownSimpleType(const TypeDecl *typeDecl, |
| 36 | + Optional<OptionalTypeKind> optionalKind) { |
| 37 | + auto knownTypeInfo = typeMapping.getKnownObjCTypeInfo(typeDecl); |
| 38 | + if (!knownTypeInfo) |
| 39 | + return false; |
| 40 | + os << knownTypeInfo->name; |
| 41 | + // FIXME: |
| 42 | + // if (knownTypeInfo->canBeNullable) |
| 43 | + // printNullability(optionalKind); |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + void visitType(TypeBase *Ty, Optional<OptionalTypeKind> optionalKind) { |
| 48 | + assert(Ty->getDesugaredType() == Ty && "unhandled sugared type"); |
| 49 | + os << "/* "; |
| 50 | + Ty->print(os); |
| 51 | + os << " */"; |
| 52 | + } |
| 53 | + |
| 54 | + void visitTupleType(TupleType *TT, Optional<OptionalTypeKind> optionalKind) { |
| 55 | + assert(TT->getNumElements() == 0); |
| 56 | + // FIXME: Handle non-void type. |
| 57 | + os << "void"; |
| 58 | + } |
| 59 | + |
| 60 | + void visitTypeAliasType(TypeAliasType *aliasTy, |
| 61 | + Optional<OptionalTypeKind> optionalKind) { |
| 62 | + const TypeAliasDecl *alias = aliasTy->getDecl(); |
| 63 | + if (printIfKnownSimpleType(alias, optionalKind)) |
| 64 | + return; |
| 65 | + |
| 66 | + visitPart(aliasTy->getSinglyDesugaredType(), optionalKind); |
| 67 | + } |
| 68 | + |
| 69 | + void visitStructType(StructType *ST, |
| 70 | + Optional<OptionalTypeKind> optionalKind) { |
| 71 | + const StructDecl *SD = ST->getStructOrBoundGenericStruct(); |
| 72 | + |
| 73 | + // Handle known type names. |
| 74 | + if (printIfKnownSimpleType(SD, optionalKind)) |
| 75 | + return; |
| 76 | + // FIXME: Handle struct types. |
| 77 | + } |
| 78 | + |
| 79 | + void visitPart(Type Ty, Optional<OptionalTypeKind> optionalKind) { |
| 80 | + TypeVisitor::visit(Ty, optionalKind); |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + raw_ostream &os; |
| 85 | + PrimitiveTypeMapping &typeMapping; |
| 86 | +}; |
| 87 | + |
| 88 | +void DeclAndTypeClangFunctionPrinter::printFunctionDeclAsCFunctionDecl( |
| 89 | + FuncDecl *FD, StringRef name, Type resultTy) { |
| 90 | + // FIXME: Might need a PrintMultiPartType here. |
| 91 | + auto print = [this](Type ty, Optional<OptionalTypeKind> optionalKind, |
| 92 | + StringRef name) { |
| 93 | + // FIXME: add support for noescape and PrintMultiPartType, |
| 94 | + // see DeclAndTypePrinter::print. |
| 95 | + CFunctionSignatureTypePrinter typePrinter(os, typeMapping); |
| 96 | + typePrinter.visit(ty, optionalKind); |
| 97 | + |
| 98 | + if (!name.empty()) { |
| 99 | + os << ' ' << name; |
| 100 | + if (DeclAndTypePrinter::isStrClangKeyword(name)) |
| 101 | + os << '_'; |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + // Print out the return type. |
| 106 | + OptionalTypeKind kind; |
| 107 | + Type objTy; |
| 108 | + std::tie(objTy, kind) = |
| 109 | + DeclAndTypePrinter::getObjectTypeAndOptionality(FD, resultTy); |
| 110 | + CFunctionSignatureTypePrinter typePrinter(os, typeMapping); |
| 111 | + typePrinter.visit(objTy, kind); |
| 112 | + |
| 113 | + os << ' ' << name << '('; |
| 114 | + |
| 115 | + // Print out the parameter types. |
| 116 | + auto params = FD->getParameters(); |
| 117 | + if (params->size()) { |
| 118 | + llvm::interleaveComma(*params, os, [&](const ParamDecl *param) { |
| 119 | + OptionalTypeKind kind; |
| 120 | + Type objTy; |
| 121 | + std::tie(objTy, kind) = DeclAndTypePrinter::getObjectTypeAndOptionality( |
| 122 | + param, param->getInterfaceType()); |
| 123 | + StringRef paramName = |
| 124 | + param->getName().empty() ? "" : param->getName().str(); |
| 125 | + print(objTy, kind, paramName); |
| 126 | + }); |
| 127 | + } else { |
| 128 | + os << "void"; |
| 129 | + } |
| 130 | + os << ')'; |
| 131 | +} |
0 commit comments