-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathPrintClangFunction.h
115 lines (95 loc) · 4.19 KB
/
PrintClangFunction.h
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
//===--- PrintClangFunction.h - Printer for C/C++ functions -----*- 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
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_PRINTASCLANG_PRINTCLANGFUNCTION_H
#define SWIFT_PRINTASCLANG_PRINTCLANGFUNCTION_H
#include "swift/AST/Type.h"
#include "swift/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
namespace swift {
class AbstractFunctionDecl;
class AccessorDecl;
class FuncDecl;
class NominalTypeDecl;
class ParamDecl;
class ParameterList;
class PrimitiveTypeMapping;
class SwiftToClangInteropContext;
/// Responsible for printing a Swift function decl or type in C or C++ mode, to
/// be included in a Swift module's generated clang header.
class DeclAndTypeClangFunctionPrinter {
public:
DeclAndTypeClangFunctionPrinter(raw_ostream &os, raw_ostream &cPrologueOS,
PrimitiveTypeMapping &typeMapping,
SwiftToClangInteropContext &interopContext)
: os(os), cPrologueOS(cPrologueOS), typeMapping(typeMapping),
interopContext(interopContext) {}
/// What kind of function signature should be emitted for the given Swift
/// function.
enum class FunctionSignatureKind {
/// Emit a signature for the C function prototype.
CFunctionProto,
/// Emit a signature for the inline C++ function thunk.
CxxInlineThunk
};
/// Information about any additional parameters.
struct AdditionalParam {
enum class Role { Self };
Role role;
Type type;
// Should self be passed indirectly?
bool isIndirect = false;
};
/// Optional modifiers that can be applied to function signature.
struct FunctionSignatureModifiers {
/// Additional qualifier to add before the function's name.
const NominalTypeDecl *qualifierContext;
FunctionSignatureModifiers() : qualifierContext(nullptr) {}
};
/// Print the C function declaration or the C++ function thunk that
/// corresponds to the given function declaration.
void printFunctionSignature(const AbstractFunctionDecl *FD, StringRef name,
Type resultTy, FunctionSignatureKind kind,
ArrayRef<AdditionalParam> additionalParams = {},
FunctionSignatureModifiers modifiers = {});
/// Print the use of the C++ function thunk parameter as it's passed to the C
/// function declaration.
void printCxxToCFunctionParameterUse(const ParamDecl *param, StringRef name);
/// Print the body of the inline C++ function thunk that calls the underlying
/// Swift function.
void printCxxThunkBody(StringRef swiftSymbolName, Type resultTy,
const ParameterList *params,
ArrayRef<AdditionalParam> additionalParams = {});
/// Print the Swift method as C++ method declaration/definition, including
/// constructors.
void printCxxMethod(const NominalTypeDecl *typeDeclContext,
const AbstractFunctionDecl *FD, StringRef swiftSymbolName,
Type resultTy, bool isDefinition);
/// Print the C++ getter/setter method signature.
void printCxxPropertyAccessorMethod(const NominalTypeDecl *typeDeclContext,
const AccessorDecl *accessor,
StringRef swiftSymbolName, Type resultTy,
bool isDefinition);
private:
void printCxxToCFunctionParameterUse(
Type type, StringRef name, bool isInOut, bool isIndirect = false,
llvm::Optional<AdditionalParam::Role> paramRole = None);
bool hasKnownOptionalNullableCxxMapping(Type type);
raw_ostream &os;
raw_ostream &cPrologueOS;
PrimitiveTypeMapping &typeMapping;
SwiftToClangInteropContext &interopContext;
};
} // end namespace swift
#endif