clang 24.0.0git
CIRGenFunctionInfo.h
Go to the documentation of this file.
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
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
30 if (!proto->hasExtParameterInfos())
31 return 0;
32 return llvm::count_if(
33 proto->getExtParameterInfos(),
35 return info.hasPassObjectSize();
36 });
37}
38
39/// A class for recording the number of arguments that a function signature
40/// requires.
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
62 unsigned additional) {
63 if (!prototype->isVariadic())
64 return All;
65
66 additional += getNumPassObjectSizeParams(prototype);
67
68 return RequiredArgs(prototype->getNumParams() + additional);
69 }
70
71 static RequiredArgs
73 unsigned additional) {
74 return getFromProtoWithExtraSlots(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.
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,
125 bool instanceMethod, CanQualType resultType,
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
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,
144 id.AddInteger(info.getCC());
145 id.AddBoolean(instanceMethod);
146 id.AddBoolean(info.getNoReturn());
147 id.AddInteger(required.getOpaqueData());
148 resultType.Profile(id);
149 for (const CanQualType &arg : argTypes)
150 arg.Profile(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, isInstanceMethod(), getExtInfo(), required, getReturnType(),
159 arguments());
160 }
161
165
169
170 CanQualType getReturnType() const { return getArgTypes()[0]; }
171
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
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
194
195 bool isVariadic() const { return required.allowsOptionalArgs(); }
196 RequiredArgs getRequiredArgs() const { return required; }
197 unsigned getNumRequiredArgs() const {
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
210 return static_cast<CallingConv>(astCallingConvention);
211 }
212};
213
214} // namespace clang::CIRGen
215
216#endif
static ABIArgInfo getDirect(mlir::Type ty=nullptr)
Definition ABIArgInfo.h:56
const_arg_iterator argTypesEnd() const
cir::ABIArgInfo getReturnInfo() const
llvm::ArrayRef< CanQualType > argTypes() const
void Profile(llvm::FoldingSetNodeID &id)
llvm::MutableArrayRef< CanQualType > argTypes()
llvm::ArrayRef< CanQualType > requiredArguments() const
static CIRGenFunctionInfo * create(cir::CallingConv cirCC, FunctionType::ExtInfo info, bool instanceMethod, CanQualType resultType, llvm::ArrayRef< CanQualType > argTypes, RequiredArgs required)
static void Profile(llvm::FoldingSetNodeID &id, bool instanceMethod, FunctionType::ExtInfo info, RequiredArgs required, CanQualType resultType, llvm::ArrayRef< CanQualType > argTypes)
llvm::ArrayRef< CanQualType > arguments() const
const_arg_iterator argTypesBegin() const
cir::CallingConv getCallingConvention() const
A class for recording the number of arguments that a function signature requires.
static RequiredArgs getFromProtoWithExtraSlots(clang::CanQual< clang::FunctionProtoType > prototype, unsigned additional)
static RequiredArgs getFromProtoWithExtraSlots(const clang::FunctionProtoType *prototype, unsigned additional)
Compute the arguments required by the given formal prototype, given that there may be some additional...
Represents a canonical, potentially-qualified type.
void Profile(llvm::FoldingSetNodeID &ID) const
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5421
unsigned getNumParams() const
Definition TypeBase.h:5699
bool isVariadic() const
Whether this function prototype is variadic.
Definition TypeBase.h:5825
ArrayRef< ExtParameterInfo > getExtParameterInfos() const
Definition TypeBase.h:5894
bool hasExtParameterInfos() const
Is there any interesting extra information for any of the parameters of this function type?
Definition TypeBase.h:5890
A class which abstracts out some details necessary for making a call.
Definition TypeBase.h:4728
Interesting information about a specific parameter that can't simply be reflected in parameter's type...
Definition TypeBase.h:4643
unsigned getNumPassObjectSizeParams(const clang::FunctionProtoType *proto)
Return the number of parameters with the pass_object_size attribute.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:279