1 //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines enum values for all the target-independent builtin
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_BASIC_BUILTINS_H
17 #define LLVM_CLANG_BASIC_BUILTINS_H
19 #include "llvm/ADT/ArrayRef.h"
22 // VC++ defines 'alloca' as an object-like macro, which interferes with our
28 class IdentifierTable;
34 GNU_LANG = 0x1, // builtin requires GNU mode.
35 C_LANG = 0x2, // builtin for c only.
36 CXX_LANG = 0x4, // builtin for cplusplus only.
37 OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
38 MS_LANG = 0x10, // builtin requires MS mode.
39 OCLC20_LANG = 0x20, // builtin for OpenCL C 2.0 only.
40 OCLC1X_LANG = 0x40, // builtin for OpenCL C 1.x only.
41 OMP_LANG = 0x80, // builtin requires OpenMP.
42 ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
43 ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode.
44 ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG, // builtin requires MS mode.
45 ALL_OCLC_LANGUAGES = OCLC1X_LANG | OCLC20_LANG // builtin for OCLC languages.
50 NotBuiltin = 0, // This is not a builtin function.
51 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
52 #include "clang/Basic/Builtins.def"
57 const char *Name, *Type, *Attributes, *HeaderName;
62 /// \brief Holds information about both target-independent and
63 /// target-specific builtins, allowing easy queries by clients.
65 /// Builtins from an optional auxiliary target are stored in
66 /// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
67 /// be translated back with getAuxBuiltinID() before use.
69 llvm::ArrayRef<Info> TSRecords;
70 llvm::ArrayRef<Info> AuxTSRecords;
75 /// \brief Perform target-specific initialization
76 /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
77 void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
79 /// \brief Mark the identifiers for all the builtins with their
80 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
82 void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
84 /// \brief Return the identifier name for the specified builtin,
85 /// e.g. "__builtin_abs".
86 const char *getName(unsigned ID) const {
87 return getRecord(ID).Name;
90 /// \brief Get the type descriptor string for the specified builtin.
91 const char *getTypeString(unsigned ID) const {
92 return getRecord(ID).Type;
95 /// \brief Return true if this function is a target-specific builtin.
96 bool isTSBuiltin(unsigned ID) const {
97 return ID >= Builtin::FirstTSBuiltin;
100 /// \brief Return true if this function has no side effects.
101 bool isPure(unsigned ID) const {
102 return strchr(getRecord(ID).Attributes, 'U') != nullptr;
105 /// \brief Return true if this function has no side effects and doesn't
107 bool isConst(unsigned ID) const {
108 return strchr(getRecord(ID).Attributes, 'c') != nullptr;
111 /// \brief Return true if we know this builtin never throws an exception.
112 bool isNoThrow(unsigned ID) const {
113 return strchr(getRecord(ID).Attributes, 'n') != nullptr;
116 /// \brief Return true if we know this builtin never returns.
117 bool isNoReturn(unsigned ID) const {
118 return strchr(getRecord(ID).Attributes, 'r') != nullptr;
121 /// \brief Return true if we know this builtin can return twice.
122 bool isReturnsTwice(unsigned ID) const {
123 return strchr(getRecord(ID).Attributes, 'j') != nullptr;
126 /// \brief Returns true if this builtin does not perform the side-effects
127 /// of its arguments.
128 bool isUnevaluated(unsigned ID) const {
129 return strchr(getRecord(ID).Attributes, 'u') != nullptr;
132 /// \brief Return true if this is a builtin for a libc/libm function,
133 /// with a "__builtin_" prefix (e.g. __builtin_abs).
134 bool isLibFunction(unsigned ID) const {
135 return strchr(getRecord(ID).Attributes, 'F') != nullptr;
138 /// \brief Determines whether this builtin is a predefined libc/libm
139 /// function, such as "malloc", where we know the signature a
141 bool isPredefinedLibFunction(unsigned ID) const {
142 return strchr(getRecord(ID).Attributes, 'f') != nullptr;
145 /// \brief Returns true if this builtin requires appropriate header in other
146 /// compilers. In Clang it will work even without including it, but we can emit
147 /// a warning about missing header.
148 bool isHeaderDependentFunction(unsigned ID) const {
149 return strchr(getRecord(ID).Attributes, 'h') != nullptr;
152 /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
153 /// function, such as "__clear_cache", where we know the signature a
155 bool isPredefinedRuntimeFunction(unsigned ID) const {
156 return strchr(getRecord(ID).Attributes, 'i') != nullptr;
159 /// \brief Determines whether this builtin has custom typechecking.
160 bool hasCustomTypechecking(unsigned ID) const {
161 return strchr(getRecord(ID).Attributes, 't') != nullptr;
164 /// \brief Determines whether this builtin has a result or any arguments which
165 /// are pointer types.
166 bool hasPtrArgsOrResult(unsigned ID) const {
167 return strchr(getRecord(ID).Type, '*') != nullptr;
170 /// \brief Completely forget that the given ID was ever considered a builtin,
171 /// e.g., because the user provided a conflicting signature.
172 void forgetBuiltin(unsigned ID, IdentifierTable &Table);
174 /// \brief If this is a library function that comes from a specific
175 /// header, retrieve that header name.
176 const char *getHeaderName(unsigned ID) const {
177 return getRecord(ID).HeaderName;
180 /// \brief Determine whether this builtin is like printf in its
181 /// formatting rules and, if so, set the index to the format string
182 /// argument and whether this function as a va_list argument.
183 bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
185 /// \brief Determine whether this builtin is like scanf in its
186 /// formatting rules and, if so, set the index to the format string
187 /// argument and whether this function as a va_list argument.
188 bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
190 /// \brief Return true if this function has no side effects and doesn't
191 /// read memory, except for possibly errno.
193 /// Such functions can be const when the MathErrno lang option is disabled.
194 bool isConstWithoutErrno(unsigned ID) const {
195 return strchr(getRecord(ID).Attributes, 'e') != nullptr;
198 const char *getRequiredFeatures(unsigned ID) const {
199 return getRecord(ID).Features;
202 /// \brief Return true if builtin ID belongs to AuxTarget.
203 bool isAuxBuiltinID(unsigned ID) const {
204 return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
207 /// Return real buitin ID (i.e. ID it would have furing compilation
209 unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
211 /// Returns true if this is a libc/libm function without the '__builtin_'
213 static bool isBuiltinFunc(const char *Name);
216 const Info &getRecord(unsigned ID) const;
218 /// \brief Is this builtin supported according to the given language options?
219 bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
220 const LangOptions &LangOpts);
222 /// \brief Helper function for isPrintfLike and isScanfLike.
223 bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
224 const char *Fmt) const;
229 /// \brief Kinds of BuiltinTemplateDecl.
230 enum BuiltinTemplateKind : int {
231 /// \brief This names the __make_integer_seq BuiltinTemplateDecl.
232 BTK__make_integer_seq,
234 /// \brief This names the __type_pack_element BuiltinTemplateDecl.
235 BTK__type_pack_element
238 } // end namespace clang