1//==-- CIRGenFunctionInfo.h - Representation of fn argument/return types ---==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines CIRGenFunctionInfo and associated types used in representing the
10// CIR source types and ABI-coerced types for function arguments and
11// return values.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_CIR_CIRGENFUNCTIONINFO_H
16#define LLVM_CLANG_CIR_CIRGENFUNCTIONINFO_H
17
18#include "clang/AST/CanonicalType.h"
19#include "clang/CIR/ABIArgInfo.h"
20#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
21#include "clang/CIR/MissingFeatures.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/Support/TrailingObjects.h"
24
25namespace clang::CIRGen {
26
27/// Return the number of parameters with the pass_object_size attribute.
28inline unsigned
29getNumPassObjectSizeParams(const clang::FunctionProtoType *proto) {
30 if (!proto->hasExtParameterInfos())
31 return 0;
32 return llvm::count_if(
33 Range: proto->getExtParameterInfos(),
34 P: [](const clang::FunctionProtoType::ExtParameterInfo &info) {
35 return info.hasPassObjectSize();
36 });
37}
38
39/// A class for recording the number of arguments that a function signature
40/// requires.
41class RequiredArgs {
42 /// The number of required arguments, or ~0 if the signature does not permit
43 /// optional arguments.
44 unsigned numRequired;
45
46public:
47 enum All_t { All };
48
49 RequiredArgs(All_t _) : numRequired(~0U) {}
50 explicit RequiredArgs(unsigned n) : numRequired(n) { assert(n != ~0U); }
51
52 unsigned getOpaqueData() const { return numRequired; }
53
54 bool allowsOptionalArgs() const { return numRequired != ~0U; }
55
56 /// Compute the arguments required by the given formal prototype, given that
57 /// there may be some additional, non-formal arguments in play.
58 ///
59 /// If FD is not null, this will consider pass_object_size params in FD.
60 static RequiredArgs
61 getFromProtoWithExtraSlots(const clang::FunctionProtoType *prototype,
62 unsigned additional) {
63 if (!prototype->isVariadic())
64 return All;
65
66 additional += getNumPassObjectSizeParams(proto: prototype);
67
68 return RequiredArgs(prototype->getNumParams() + additional);
69 }
70
71 static RequiredArgs
72 getFromProtoWithExtraSlots(clang::CanQual<clang::FunctionProtoType> prototype,
73 unsigned additional) {
74 return getFromProtoWithExtraSlots(prototype: prototype.getTypePtr(), additional);
75 }
76
77 unsigned getNumRequiredArgs() const {
78 assert(allowsOptionalArgs());
79 return numRequired;
80 }
81};
82
83// The TrailingObjects for this class contain the function return type in the
84// first CanQualType slot, followed by the argument types.
85class CIRGenFunctionInfo final
86 : public llvm::FoldingSetNode,
87 private llvm::TrailingObjects<CIRGenFunctionInfo, CanQualType> {
88 /// The CIR-level calling convention to use for this function.
89 unsigned callingConvention : 8;
90
91 /// The AST-level calling convention this function was declared with.
92 unsigned astCallingConvention : 8;
93
94 // Whether this function has noreturn.
95 LLVM_PREFERRED_TYPE(bool)
96 unsigned noReturn : 1;
97
98 // Whether this is an instance method/non-static member function with implicit
99 // 'this' argument.
100 LLVM_PREFERRED_TYPE(bool)
101 unsigned instanceMethod : 1;
102
103 RequiredArgs required;
104
105 unsigned numArgs;
106
107 CanQualType *getArgTypes() { return getTrailingObjects(); }
108 const CanQualType *getArgTypes() const { return getTrailingObjects(); }
109
110 CIRGenFunctionInfo() : required(RequiredArgs::All) {}
111
112 FunctionType::ExtInfo getExtInfo() const {
113 // TODO(cir): as we add this information to this type, we need to add calls
114 // here instead of explicit false/0.
115 return FunctionType::ExtInfo(
116 isNoReturn(), /*getHasRegParm=*/false, /*getRegParm=*/false,
117 getASTCallingConvention(), /*isReturnsRetained=*/false,
118 /*isNoCallerSavedRegs=*/false, /*isNoCfCheck=*/false,
119 /*isCmseNSCall=*/false);
120 }
121
122public:
123 static CIRGenFunctionInfo *create(cir::CallingConv cirCC,
124 FunctionType::ExtInfo info,
125 bool instanceMethod, CanQualType resultType,
126 llvm::ArrayRef<CanQualType> argTypes,
127 RequiredArgs required);
128
129 void operator delete(void *p) { ::operator delete(p); }
130
131 // Friending class TrailingObjects is apparantly not good enough for MSVC, so
132 // these have to be public.
133 friend class TrailingObjects;
134
135 using const_arg_iterator = const CanQualType *;
136 using arg_iterator = CanQualType *;
137
138 // This function has to be CamelCase because llvm::FoldingSet requires so.
139 // NOLINTNEXTLINE(readability-identifier-naming)
140 static void Profile(llvm::FoldingSetNodeID &id, bool instanceMethod,
141 FunctionType::ExtInfo info, RequiredArgs required,
142 CanQualType resultType,
143 llvm::ArrayRef<CanQualType> argTypes) {
144 id.AddInteger(I: info.getCC());
145 id.AddBoolean(B: instanceMethod);
146 id.AddBoolean(B: info.getNoReturn());
147 id.AddInteger(I: required.getOpaqueData());
148 resultType.Profile(ID&: id);
149 for (const CanQualType &arg : argTypes)
150 arg.Profile(ID&: id);
151 }
152
153 // NOLINTNEXTLINE(readability-identifier-naming)
154 void Profile(llvm::FoldingSetNodeID &id) {
155 // If the Profile functions get out of sync, we can end up with incorrect
156 // function signatures, so we call the static Profile function here rather
157 // than duplicating the logic.
158 Profile(id, instanceMethod: isInstanceMethod(), info: getExtInfo(), required, resultType: getReturnType(),
159 argTypes: arguments());
160 }
161
162 llvm::ArrayRef<CanQualType> arguments() const {
163 return llvm::ArrayRef<CanQualType>(argTypesBegin(), numArgs);
164 }
165
166 llvm::ArrayRef<CanQualType> requiredArguments() const {
167 return llvm::ArrayRef<CanQualType>(argTypesBegin(), getNumRequiredArgs());
168 }
169
170 CanQualType getReturnType() const { return getArgTypes()[0]; }
171
172 cir::ABIArgInfo getReturnInfo() const {
173 assert(!cir::MissingFeatures::abiArgInfo());
174 // TODO(cir): we currently just 'fake' this, but should calculate
175 // this/figure out what it means when we get our ABI info set correctly.
176 // For now, we leave this as a direct return.
177
178 return cir::ABIArgInfo::getDirect();
179 }
180
181 const_arg_iterator argTypesBegin() const { return getArgTypes() + 1; }
182 const_arg_iterator argTypesEnd() const { return getArgTypes() + 1 + numArgs; }
183 arg_iterator argTypesBegin() { return getArgTypes() + 1; }
184 arg_iterator argTypesEnd() { return getArgTypes() + 1 + numArgs; }
185
186 unsigned argTypeSize() const { return numArgs; }
187
188 llvm::MutableArrayRef<CanQualType> argTypes() {
189 return llvm::MutableArrayRef<CanQualType>(argTypesBegin(), numArgs);
190 }
191 llvm::ArrayRef<CanQualType> argTypes() const {
192 return llvm::ArrayRef<CanQualType>(argTypesBegin(), numArgs);
193 }
194
195 bool isVariadic() const { return required.allowsOptionalArgs(); }
196 RequiredArgs getRequiredArgs() const { return required; }
197 unsigned getNumRequiredArgs() const {
198 return isVariadic() ? getRequiredArgs().getNumRequiredArgs()
199 : argTypeSize();
200 }
201
202 bool isNoReturn() const { return noReturn; }
203 bool isInstanceMethod() const { return instanceMethod; }
204
205 cir::CallingConv getCallingConvention() const {
206 return static_cast<cir::CallingConv>(callingConvention);
207 }
208
209 CallingConv getASTCallingConvention() const {
210 return static_cast<CallingConv>(astCallingConvention);
211 }
212};
213
214} // namespace clang::CIRGen
215
216#endif
217