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 ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
40 ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode.
41 ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode.
46 NotBuiltin = 0, // This is not a builtin function.
47 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
48 #include "clang/Basic/Builtins.def"
53 const char *Name, *Type, *Attributes, *HeaderName;
58 /// \brief Holds information about both target-independent and
59 /// target-specific builtins, allowing easy queries by clients.
61 /// Builtins from an optional auxiliary target are stored in
62 /// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
63 /// be translated back with getAuxBuiltinID() before use.
65 llvm::ArrayRef<Info> TSRecords;
66 llvm::ArrayRef<Info> AuxTSRecords;
71 /// \brief Perform target-specific initialization
72 /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
73 void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
75 /// \brief Mark the identifiers for all the builtins with their
76 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
78 void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
80 /// \brief Return the identifier name for the specified builtin,
81 /// e.g. "__builtin_abs".
82 const char *getName(unsigned ID) const {
83 return getRecord(ID).Name;
86 /// \brief Get the type descriptor string for the specified builtin.
87 const char *getTypeString(unsigned ID) const {
88 return getRecord(ID).Type;
91 /// \brief Return true if this function is a target-specific builtin
92 bool isTSBuiltin(unsigned ID) const {
93 return ID >= Builtin::FirstTSBuiltin;
96 /// \brief Return true if this function has no side effects and doesn't
98 bool isConst(unsigned ID) const {
99 return strchr(getRecord(ID).Attributes, 'c') != nullptr;
102 /// \brief Return true if we know this builtin never throws an exception.
103 bool isNoThrow(unsigned ID) const {
104 return strchr(getRecord(ID).Attributes, 'n') != nullptr;
107 /// \brief Return true if we know this builtin never returns.
108 bool isNoReturn(unsigned ID) const {
109 return strchr(getRecord(ID).Attributes, 'r') != nullptr;
112 /// \brief Return true if we know this builtin can return twice.
113 bool isReturnsTwice(unsigned ID) const {
114 return strchr(getRecord(ID).Attributes, 'j') != nullptr;
117 /// \brief Returns true if this builtin does not perform the side-effects
118 /// of its arguments.
119 bool isUnevaluated(unsigned ID) const {
120 return strchr(getRecord(ID).Attributes, 'u') != nullptr;
123 /// \brief Return true if this is a builtin for a libc/libm function,
124 /// with a "__builtin_" prefix (e.g. __builtin_abs).
125 bool isLibFunction(unsigned ID) const {
126 return strchr(getRecord(ID).Attributes, 'F') != nullptr;
129 /// \brief Determines whether this builtin is a predefined libc/libm
130 /// function, such as "malloc", where we know the signature a
132 bool isPredefinedLibFunction(unsigned ID) const {
133 return strchr(getRecord(ID).Attributes, 'f') != nullptr;
136 /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
137 /// function, such as "__clear_cache", where we know the signature a
139 bool isPredefinedRuntimeFunction(unsigned ID) const {
140 return strchr(getRecord(ID).Attributes, 'i') != nullptr;
143 /// \brief Determines whether this builtin has custom typechecking.
144 bool hasCustomTypechecking(unsigned ID) const {
145 return strchr(getRecord(ID).Attributes, 't') != nullptr;
148 /// \brief Determines whether this builtin has a result or any arguments which
149 /// are pointer types.
150 bool hasPtrArgsOrResult(unsigned ID) const {
151 return strchr(getRecord(ID).Type, '*') != nullptr;
154 /// \brief Completely forget that the given ID was ever considered a builtin,
155 /// e.g., because the user provided a conflicting signature.
156 void forgetBuiltin(unsigned ID, IdentifierTable &Table);
158 /// \brief If this is a library function that comes from a specific
159 /// header, retrieve that header name.
160 const char *getHeaderName(unsigned ID) const {
161 return getRecord(ID).HeaderName;
164 /// \brief Determine whether this builtin is like printf in its
165 /// formatting rules and, if so, set the index to the format string
166 /// argument and whether this function as a va_list argument.
167 bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
169 /// \brief Determine whether this builtin is like scanf in its
170 /// formatting rules and, if so, set the index to the format string
171 /// argument and whether this function as a va_list argument.
172 bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
174 /// \brief Return true if this function has no side effects and doesn't
175 /// read memory, except for possibly errno.
177 /// Such functions can be const when the MathErrno lang option is disabled.
178 bool isConstWithoutErrno(unsigned ID) const {
179 return strchr(getRecord(ID).Attributes, 'e') != nullptr;
182 const char *getRequiredFeatures(unsigned ID) const {
183 return getRecord(ID).Features;
186 /// \brief Return true if builtin ID belongs to AuxTarget.
187 bool isAuxBuiltinID(unsigned ID) const {
188 return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
191 /// Return real buitin ID (i.e. ID it would have furing compilation
193 unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
196 const Info &getRecord(unsigned ID) const;
198 /// \brief Is this builtin supported according to the given language options?
199 bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
200 const LangOptions &LangOpts);
202 /// \brief Helper function for isPrintfLike and isScanfLike.
203 bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
204 const char *Fmt) const;
209 /// \brief Kinds of BuiltinTemplateDecl.
210 enum BuiltinTemplateKind : int {
211 /// \brief This names the __make_integer_seq BuiltinTemplateDecl.
212 BTK__make_integer_seq
215 } // end namespace clang