clang 20.0.0git
Builtins.h
Go to the documentation of this file.
1//===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
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/// \file
10/// Defines enum values for all the target-independent builtin
11/// functions.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_BUILTINS_H
16#define LLVM_CLANG_BASIC_BUILTINS_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include <cstring>
22
23// VC++ defines 'alloca' as an object-like macro, which interferes with our
24// builtins.
25#undef alloca
26
27namespace clang {
28class TargetInfo;
29class IdentifierTable;
30class LangOptions;
31
32enum LanguageID : uint16_t {
33 GNU_LANG = 0x1, // builtin requires GNU mode.
34 C_LANG = 0x2, // builtin for c only.
35 CXX_LANG = 0x4, // builtin for cplusplus only.
36 OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
37 MS_LANG = 0x10, // builtin requires MS mode.
38 OMP_LANG = 0x20, // builtin requires OpenMP.
39 CUDA_LANG = 0x40, // builtin requires CUDA.
40 COR_LANG = 0x80, // builtin requires use of 'fcoroutine-ts' option.
41 OCL_GAS = 0x100, // builtin requires OpenCL generic address space.
42 OCL_PIPE = 0x200, // builtin requires OpenCL pipe.
43 OCL_DSE = 0x400, // builtin requires OpenCL device side enqueue.
44 ALL_OCL_LANGUAGES = 0x800, // builtin for OCL languages.
45 HLSL_LANG = 0x1000, // builtin requires HLSL.
46 ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
47 ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode.
48 ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode.
49};
50
51struct HeaderDesc {
52 enum HeaderID : uint16_t {
53#define HEADER(ID, NAME) ID,
54#include "clang/Basic/BuiltinHeaders.def"
55#undef HEADER
56 } ID;
57
58 constexpr HeaderDesc(HeaderID ID) : ID(ID) {}
59
60 const char *getName() const;
61};
62
63namespace Builtin {
64enum ID {
65 NotBuiltin = 0, // This is not a builtin function.
66#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
67#include "clang/Basic/Builtins.inc"
69};
70
71struct Info {
72 llvm::StringLiteral Name;
73 const char *Type, *Attributes;
74 const char *Features;
77};
78
79/// Holds information about both target-independent and
80/// target-specific builtins, allowing easy queries by clients.
81///
82/// Builtins from an optional auxiliary target are stored in
83/// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
84/// be translated back with getAuxBuiltinID() before use.
85class Context {
86 llvm::ArrayRef<Info> TSRecords;
87 llvm::ArrayRef<Info> AuxTSRecords;
88
89public:
90 Context() = default;
91
92 /// Perform target-specific initialization
93 /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
94 void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
95
96 /// Mark the identifiers for all the builtins with their
97 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
98 /// such.
99 void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
100
101 /// Return the identifier name for the specified builtin,
102 /// e.g. "__builtin_abs".
103 llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }
104
105 /// Get the type descriptor string for the specified builtin.
106 const char *getTypeString(unsigned ID) const { return getRecord(ID).Type; }
107
108 /// Return true if this function is a target-specific builtin.
109 bool isTSBuiltin(unsigned ID) const {
110 return ID >= Builtin::FirstTSBuiltin;
111 }
112
113 /// Return true if this function has no side effects.
114 bool isPure(unsigned ID) const {
115 return strchr(getRecord(ID).Attributes, 'U') != nullptr;
116 }
117
118 /// Return true if this function has no side effects and doesn't
119 /// read memory.
120 bool isConst(unsigned ID) const {
121 return strchr(getRecord(ID).Attributes, 'c') != nullptr;
122 }
123
124 /// Return true if we know this builtin never throws an exception.
125 bool isNoThrow(unsigned ID) const {
126 return strchr(getRecord(ID).Attributes, 'n') != nullptr;
127 }
128
129 /// Return true if we know this builtin never returns.
130 bool isNoReturn(unsigned ID) const {
131 return strchr(getRecord(ID).Attributes, 'r') != nullptr;
132 }
133
134 /// Return true if we know this builtin can return twice.
135 bool isReturnsTwice(unsigned ID) const {
136 return strchr(getRecord(ID).Attributes, 'j') != nullptr;
137 }
138
139 /// Returns true if this builtin does not perform the side-effects
140 /// of its arguments.
141 bool isUnevaluated(unsigned ID) const {
142 return strchr(getRecord(ID).Attributes, 'u') != nullptr;
143 }
144
145 /// Return true if this is a builtin for a libc/libm function,
146 /// with a "__builtin_" prefix (e.g. __builtin_abs).
147 bool isLibFunction(unsigned ID) const {
148 return strchr(getRecord(ID).Attributes, 'F') != nullptr;
149 }
150
151 /// Determines whether this builtin is a predefined libc/libm
152 /// function, such as "malloc", where we know the signature a
153 /// priori.
154 /// In C, such functions behave as if they are predeclared,
155 /// possibly with a warning on first use. In Objective-C and C++,
156 /// they do not, but they are recognized as builtins once we see
157 /// a declaration.
158 bool isPredefinedLibFunction(unsigned ID) const {
159 return strchr(getRecord(ID).Attributes, 'f') != nullptr;
160 }
161
162 /// Returns true if this builtin requires appropriate header in other
163 /// compilers. In Clang it will work even without including it, but we can emit
164 /// a warning about missing header.
165 bool isHeaderDependentFunction(unsigned ID) const {
166 return strchr(getRecord(ID).Attributes, 'h') != nullptr;
167 }
168
169 /// Determines whether this builtin is a predefined compiler-rt/libgcc
170 /// function, such as "__clear_cache", where we know the signature a
171 /// priori.
172 bool isPredefinedRuntimeFunction(unsigned ID) const {
173 return strchr(getRecord(ID).Attributes, 'i') != nullptr;
174 }
175
176 /// Determines whether this builtin is a C++ standard library function
177 /// that lives in (possibly-versioned) namespace std, possibly a template
178 /// specialization, where the signature is determined by the standard library
179 /// declaration.
180 bool isInStdNamespace(unsigned ID) const {
181 return strchr(getRecord(ID).Attributes, 'z') != nullptr;
182 }
183
184 /// Determines whether this builtin can have its address taken with no
185 /// special action required.
186 bool isDirectlyAddressable(unsigned ID) const {
187 // Most standard library functions can have their addresses taken. C++
188 // standard library functions formally cannot in C++20 onwards, and when
189 // we allow it, we need to ensure we instantiate a definition.
191 }
192
193 /// Determines whether this builtin has custom typechecking.
194 bool hasCustomTypechecking(unsigned ID) const {
195 return strchr(getRecord(ID).Attributes, 't') != nullptr;
196 }
197
198 /// Determines whether a declaration of this builtin should be recognized
199 /// even if the type doesn't match the specified signature.
200 bool allowTypeMismatch(unsigned ID) const {
201 return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
203 }
204
205 /// Determines whether this builtin has a result or any arguments which
206 /// are pointer types.
207 bool hasPtrArgsOrResult(unsigned ID) const {
208 return strchr(getRecord(ID).Type, '*') != nullptr;
209 }
210
211 /// Return true if this builtin has a result or any arguments which are
212 /// reference types.
213 bool hasReferenceArgsOrResult(unsigned ID) const {
214 return strchr(getRecord(ID).Type, '&') != nullptr ||
215 strchr(getRecord(ID).Type, 'A') != nullptr;
216 }
217
218 /// If this is a library function that comes from a specific
219 /// header, retrieve that header name.
220 const char *getHeaderName(unsigned ID) const {
221 return getRecord(ID).Header.getName();
222 }
223
224 /// Determine whether this builtin is like printf in its
225 /// formatting rules and, if so, set the index to the format string
226 /// argument and whether this function as a va_list argument.
227 bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
228
229 /// Determine whether this builtin is like scanf in its
230 /// formatting rules and, if so, set the index to the format string
231 /// argument and whether this function as a va_list argument.
232 bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
233
234 /// Determine whether this builtin has callback behavior (see
235 /// llvm::AbstractCallSites for details). If so, add the index to the
236 /// callback callee argument and the callback payload arguments.
237 bool performsCallback(unsigned ID,
238 llvm::SmallVectorImpl<int> &Encoding) const;
239
240 /// Return true if this function has no side effects and doesn't
241 /// read memory, except for possibly errno or raising FP exceptions.
242 ///
243 /// Such functions can be const when the MathErrno lang option and FP
244 /// exceptions are disabled.
246 return strchr(getRecord(ID).Attributes, 'e') != nullptr;
247 }
248
249 bool isConstWithoutExceptions(unsigned ID) const {
250 return strchr(getRecord(ID).Attributes, 'g') != nullptr;
251 }
252
253 const char *getRequiredFeatures(unsigned ID) const {
254 return getRecord(ID).Features;
255 }
256
257 unsigned getRequiredVectorWidth(unsigned ID) const;
258
259 /// Return true if builtin ID belongs to AuxTarget.
260 bool isAuxBuiltinID(unsigned ID) const {
261 return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
262 }
263
264 /// Return real builtin ID (i.e. ID it would have during compilation
265 /// for AuxTarget).
266 unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
267
268 /// Returns true if this is a libc/libm function without the '__builtin_'
269 /// prefix.
270 static bool isBuiltinFunc(llvm::StringRef Name);
271
272 /// Returns true if this is a builtin that can be redeclared. Returns true
273 /// for non-builtins.
274 bool canBeRedeclared(unsigned ID) const;
275
276 /// Return true if this function can be constant evaluated by Clang frontend.
277 bool isConstantEvaluated(unsigned ID) const {
278 return strchr(getRecord(ID).Attributes, 'E') != nullptr;
279 }
280
281 /// Returns true if this is an immediate (consteval) function
282 bool isImmediate(unsigned ID) const {
283 return strchr(getRecord(ID).Attributes, 'G') != nullptr;
284 }
285
286private:
287 const Info &getRecord(unsigned ID) const;
288
289 /// Helper function for isPrintfLike and isScanfLike.
290 bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
291 const char *Fmt) const;
292};
293
294/// Returns true if the required target features of a builtin function are
295/// enabled.
296/// \p TargetFeatureMap maps a target feature to true if it is enabled and
297/// false if it is disabled.
299 llvm::StringRef RequiredFatures,
300 const llvm::StringMap<bool> &TargetFetureMap);
301
302} // namespace Builtin
303
304/// Kinds of BuiltinTemplateDecl.
306 /// This names the __make_integer_seq BuiltinTemplateDecl.
308
309 /// This names the __type_pack_element BuiltinTemplateDecl.
311
312 /// This names the __builtin_common_type BuiltinTemplateDecl.
314};
315
316} // end namespace clang
317#endif
llvm::MachO::Target Target
Definition: MachO.h:51
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition: Builtins.h:85
bool isUnevaluated(unsigned ID) const
Returns true if this builtin does not perform the side-effects of its arguments.
Definition: Builtins.h:141
bool hasReferenceArgsOrResult(unsigned ID) const
Return true if this builtin has a result or any arguments which are reference types.
Definition: Builtins.h:213
bool performsCallback(unsigned ID, llvm::SmallVectorImpl< int > &Encoding) const
Determine whether this builtin has callback behavior (see llvm::AbstractCallSites for details).
Definition: Builtins.cpp:215
bool isAuxBuiltinID(unsigned ID) const
Return true if builtin ID belongs to AuxTarget.
Definition: Builtins.h:260
bool isLibFunction(unsigned ID) const
Return true if this is a builtin for a libc/libm function, with a "__builtin_" prefix (e....
Definition: Builtins.h:147
const char * getHeaderName(unsigned ID) const
If this is a library function that comes from a specific header, retrieve that header name.
Definition: Builtins.h:220
bool hasPtrArgsOrResult(unsigned ID) const
Determines whether this builtin has a result or any arguments which are pointer types.
Definition: Builtins.h:207
llvm::StringRef getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.h:103
bool isReturnsTwice(unsigned ID) const
Return true if we know this builtin can return twice.
Definition: Builtins.h:135
bool isImmediate(unsigned ID) const
Returns true if this is an immediate (consteval) function.
Definition: Builtins.h:282
bool isConstWithoutErrnoAndExceptions(unsigned ID) const
Return true if this function has no side effects and doesn't read memory, except for possibly errno o...
Definition: Builtins.h:245
static bool isBuiltinFunc(llvm::StringRef Name)
Returns true if this is a libc/libm function without the '__builtin_' prefix.
Definition: Builtins.cpp:63
const char * getTypeString(unsigned ID) const
Get the type descriptor string for the specified builtin.
Definition: Builtins.h:106
unsigned getRequiredVectorWidth(unsigned ID) const
Definition: Builtins.cpp:166
unsigned getAuxBuiltinID(unsigned ID) const
Return real builtin ID (i.e.
Definition: Builtins.h:266
bool allowTypeMismatch(unsigned ID) const
Determines whether a declaration of this builtin should be recognized even if the type doesn't match ...
Definition: Builtins.h:200
bool isTSBuiltin(unsigned ID) const
Return true if this function is a target-specific builtin.
Definition: Builtins.h:109
bool hasCustomTypechecking(unsigned ID) const
Determines whether this builtin has custom typechecking.
Definition: Builtins.h:194
bool isHeaderDependentFunction(unsigned ID) const
Returns true if this builtin requires appropriate header in other compilers.
Definition: Builtins.h:165
void initializeBuiltins(IdentifierTable &Table, const LangOptions &LangOpts)
Mark the identifiers for all the builtins with their appropriate builtin ID # and mark any non-portab...
Definition: Builtins.cpp:134
bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like scanf in its formatting rules and, if so, set the index to the...
Definition: Builtins.cpp:210
bool isInStdNamespace(unsigned ID) const
Determines whether this builtin is a C++ standard library function that lives in (possibly-versioned)...
Definition: Builtins.h:180
bool canBeRedeclared(unsigned ID) const
Returns true if this is a builtin that can be redeclared.
Definition: Builtins.cpp:242
const char * getRequiredFeatures(unsigned ID) const
Definition: Builtins.h:253
bool isConstantEvaluated(unsigned ID) const
Return true if this function can be constant evaluated by Clang frontend.
Definition: Builtins.h:277
bool isPredefinedLibFunction(unsigned ID) const
Determines whether this builtin is a predefined libc/libm function, such as "malloc",...
Definition: Builtins.h:158
bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like printf in its formatting rules and, if so, set the index to th...
Definition: Builtins.cpp:205
bool isNoReturn(unsigned ID) const
Return true if we know this builtin never returns.
Definition: Builtins.h:130
bool isDirectlyAddressable(unsigned ID) const
Determines whether this builtin can have its address taken with no special action required.
Definition: Builtins.h:186
bool isConstWithoutExceptions(unsigned ID) const
Definition: Builtins.h:249
bool isPredefinedRuntimeFunction(unsigned ID) const
Determines whether this builtin is a predefined compiler-rt/libgcc function, such as "__clear_cache",...
Definition: Builtins.h:172
bool isPure(unsigned ID) const
Return true if this function has no side effects.
Definition: Builtins.h:114
bool isNoThrow(unsigned ID) const
Return true if we know this builtin never throws an exception.
Definition: Builtins.h:125
void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget)
Perform target-specific initialization.
Definition: Builtins.cpp:55
bool isConst(unsigned ID) const
Return true if this function has no side effects and doesn't read memory.
Definition: Builtins.h:120
Implements an efficient mapping from strings to IdentifierInfo nodes.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
Exposes information about the current target.
Definition: TargetInfo.h:220
The base class of the type hierarchy.
Definition: Type.h:1828
bool evaluateRequiredTargetFeatures(llvm::StringRef RequiredFatures, const llvm::StringMap< bool > &TargetFetureMap)
Returns true if the required target features of a builtin function are enabled.
The JSON file list parser is used to communicate input to InstallAPI.
LanguageID
Definition: Builtins.h:32
@ ALL_LANGUAGES
Definition: Builtins.h:46
@ MS_LANG
Definition: Builtins.h:37
@ CUDA_LANG
Definition: Builtins.h:39
@ OMP_LANG
Definition: Builtins.h:38
@ CXX_LANG
Definition: Builtins.h:35
@ OBJC_LANG
Definition: Builtins.h:36
@ OCL_DSE
Definition: Builtins.h:43
@ C_LANG
Definition: Builtins.h:34
@ ALL_OCL_LANGUAGES
Definition: Builtins.h:44
@ HLSL_LANG
Definition: Builtins.h:45
@ OCL_GAS
Definition: Builtins.h:41
@ ALL_GNU_LANGUAGES
Definition: Builtins.h:47
@ ALL_MS_LANGUAGES
Definition: Builtins.h:48
@ GNU_LANG
Definition: Builtins.h:33
@ COR_LANG
Definition: Builtins.h:40
@ OCL_PIPE
Definition: Builtins.h:42
BuiltinTemplateKind
Kinds of BuiltinTemplateDecl.
Definition: Builtins.h:305
@ BTK__type_pack_element
This names the __type_pack_element BuiltinTemplateDecl.
Definition: Builtins.h:310
@ BTK__builtin_common_type
This names the __builtin_common_type BuiltinTemplateDecl.
Definition: Builtins.h:313
@ BTK__make_integer_seq
This names the __make_integer_seq BuiltinTemplateDecl.
Definition: Builtins.h:307
llvm::StringLiteral Name
Definition: Builtins.h:72
const char * Features
Definition: Builtins.h:74
HeaderDesc Header
Definition: Builtins.h:75
LanguageID Langs
Definition: Builtins.h:76
const char * Type
Definition: Builtins.h:73
const char * Attributes
Definition: Builtins.h:73
const char * getName() const
Definition: Builtins.cpp:21
enum clang::HeaderDesc::HeaderID ID
constexpr HeaderDesc(HeaderID ID)
Definition: Builtins.h:58