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 "clang/Basic/LLVM.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;
54 LanguageID builtin_lang;
56 bool operator==(const Info &RHS) const {
57 return !strcmp(Name, RHS.Name) &&
58 !strcmp(Type, RHS.Type) &&
59 !strcmp(Attributes, RHS.Attributes);
61 bool operator!=(const Info &RHS) const { return !(*this == RHS); }
64 /// \brief Holds information about both target-independent and
65 /// target-specific builtins, allowing easy queries by clients.
67 const Info *TSRecords;
68 unsigned NumTSRecords;
72 /// \brief Perform target-specific initialization
73 void InitializeTarget(const TargetInfo &Target);
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 Populate the vector with the names of all of the builtins.
81 void GetBuiltinNames(SmallVectorImpl<const char *> &Names);
83 /// \brief Return the identifier name for the specified builtin,
84 /// e.g. "__builtin_abs".
85 const char *GetName(unsigned ID) const {
86 return GetRecord(ID).Name;
89 /// \brief Get the type descriptor string for the specified builtin.
90 const char *GetTypeString(unsigned ID) const {
91 return GetRecord(ID).Type;
94 /// \brief Return true if this function has no side effects and doesn't
96 bool isConst(unsigned ID) const {
97 return strchr(GetRecord(ID).Attributes, 'c') != nullptr;
100 /// \brief Return true if we know this builtin never throws an exception.
101 bool isNoThrow(unsigned ID) const {
102 return strchr(GetRecord(ID).Attributes, 'n') != nullptr;
105 /// \brief Return true if we know this builtin never returns.
106 bool isNoReturn(unsigned ID) const {
107 return strchr(GetRecord(ID).Attributes, 'r') != nullptr;
110 /// \brief Return true if we know this builtin can return twice.
111 bool isReturnsTwice(unsigned ID) const {
112 return strchr(GetRecord(ID).Attributes, 'j') != nullptr;
115 /// \brief Returns true if this builtin does not perform the side-effects
116 /// of its arguments.
117 bool isUnevaluated(unsigned ID) const {
118 return strchr(GetRecord(ID).Attributes, 'u') != nullptr;
121 /// \brief Return true if this is a builtin for a libc/libm function,
122 /// with a "__builtin_" prefix (e.g. __builtin_abs).
123 bool isLibFunction(unsigned ID) const {
124 return strchr(GetRecord(ID).Attributes, 'F') != nullptr;
127 /// \brief Determines whether this builtin is a predefined libc/libm
128 /// function, such as "malloc", where we know the signature a
130 bool isPredefinedLibFunction(unsigned ID) const {
131 return strchr(GetRecord(ID).Attributes, 'f') != nullptr;
134 /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
135 /// function, such as "__clear_cache", where we know the signature a
137 bool isPredefinedRuntimeFunction(unsigned ID) const {
138 return strchr(GetRecord(ID).Attributes, 'i') != nullptr;
141 /// \brief Determines whether this builtin has custom typechecking.
142 bool hasCustomTypechecking(unsigned ID) const {
143 return strchr(GetRecord(ID).Attributes, 't') != nullptr;
146 /// \brief Determines whether this builtin has a result or any arguments which
147 /// are pointer types.
148 bool hasPtrArgsOrResult(unsigned ID) const {
149 return strchr(GetRecord(ID).Type, '*') != nullptr;
152 /// \brief Completely forget that the given ID was ever considered a builtin,
153 /// e.g., because the user provided a conflicting signature.
154 void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
156 /// \brief If this is a library function that comes from a specific
157 /// header, retrieve that header name.
158 const char *getHeaderName(unsigned ID) const {
159 return GetRecord(ID).HeaderName;
162 /// \brief Determine whether this builtin is like printf in its
163 /// formatting rules and, if so, set the index to the format string
164 /// argument and whether this function as a va_list argument.
165 bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
167 /// \brief Determine whether this builtin is like scanf in its
168 /// formatting rules and, if so, set the index to the format string
169 /// argument and whether this function as a va_list argument.
170 bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
172 /// \brief Return true if this function has no side effects and doesn't
173 /// read memory, except for possibly errno.
175 /// Such functions can be const when the MathErrno lang option is disabled.
176 bool isConstWithoutErrno(unsigned ID) const {
177 return strchr(GetRecord(ID).Attributes, 'e') != nullptr;
181 const Info &GetRecord(unsigned ID) const;
183 /// \brief Is this builtin supported according to the given language options?
184 bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
185 const LangOptions &LangOpts);
187 /// \brief Helper function for isPrintfLike and isScanfLike.
188 bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
189 const char *Fmt) const;
193 } // end namespace clang