xref: /freebsd-src/contrib/llvm-project/clang/include/clang/Basic/Builtins.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// Defines enum values for all the target-independent builtin
110b57cec5SDimitry Andric /// functions.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifndef LLVM_CLANG_BASIC_BUILTINS_H
160b57cec5SDimitry Andric #define LLVM_CLANG_BASIC_BUILTINS_H
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
1981ad6265SDimitry Andric #include "llvm/ADT/StringMap.h"
2081ad6265SDimitry Andric #include "llvm/ADT/StringRef.h"
210b57cec5SDimitry Andric #include <cstring>
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric // VC++ defines 'alloca' as an object-like macro, which interferes with our
240b57cec5SDimitry Andric // builtins.
250b57cec5SDimitry Andric #undef alloca
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric namespace clang {
280b57cec5SDimitry Andric class TargetInfo;
290b57cec5SDimitry Andric class IdentifierTable;
300b57cec5SDimitry Andric class LangOptions;
310b57cec5SDimitry Andric 
32bdd1243dSDimitry Andric enum LanguageID : uint16_t {
330b57cec5SDimitry Andric   GNU_LANG = 0x1,            // builtin requires GNU mode.
340b57cec5SDimitry Andric   C_LANG = 0x2,              // builtin for c only.
350b57cec5SDimitry Andric   CXX_LANG = 0x4,            // builtin for cplusplus only.
360b57cec5SDimitry Andric   OBJC_LANG = 0x8,           // builtin for objective-c and objective-c++
370b57cec5SDimitry Andric   MS_LANG = 0x10,            // builtin requires MS mode.
38d56accc7SDimitry Andric   OMP_LANG = 0x20,           // builtin requires OpenMP.
39d56accc7SDimitry Andric   CUDA_LANG = 0x40,          // builtin requires CUDA.
40d56accc7SDimitry Andric   COR_LANG = 0x80,           // builtin requires use of 'fcoroutine-ts' option.
41d56accc7SDimitry Andric   OCL_GAS = 0x100,           // builtin requires OpenCL generic address space.
42d56accc7SDimitry Andric   OCL_PIPE = 0x200,          // builtin requires OpenCL pipe.
43d56accc7SDimitry Andric   OCL_DSE = 0x400,           // builtin requires OpenCL device side enqueue.
44d56accc7SDimitry Andric   ALL_OCL_LANGUAGES = 0x800, // builtin for OCL languages.
4581ad6265SDimitry Andric   HLSL_LANG = 0x1000,        // builtin requires HLSL.
460b57cec5SDimitry Andric   ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
470b57cec5SDimitry Andric   ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
48d56accc7SDimitry Andric   ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG     // builtin requires MS mode.
490b57cec5SDimitry Andric };
500b57cec5SDimitry Andric 
51bdd1243dSDimitry Andric struct HeaderDesc {
52bdd1243dSDimitry Andric   enum HeaderID : uint16_t {
53bdd1243dSDimitry Andric #define HEADER(ID, NAME) ID,
54bdd1243dSDimitry Andric #include "clang/Basic/BuiltinHeaders.def"
55bdd1243dSDimitry Andric #undef HEADER
56bdd1243dSDimitry Andric   } ID;
57bdd1243dSDimitry Andric 
58bdd1243dSDimitry Andric   constexpr HeaderDesc(HeaderID ID) : ID(ID) {}
59bdd1243dSDimitry Andric 
60bdd1243dSDimitry Andric   const char *getName() const;
61bdd1243dSDimitry Andric };
62bdd1243dSDimitry Andric 
630b57cec5SDimitry Andric namespace Builtin {
640b57cec5SDimitry Andric enum ID {
650b57cec5SDimitry Andric   NotBuiltin  = 0,      // This is not a builtin function.
660b57cec5SDimitry Andric #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
67*0fca6ea1SDimitry Andric #include "clang/Basic/Builtins.inc"
680b57cec5SDimitry Andric   FirstTSBuiltin
690b57cec5SDimitry Andric };
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric struct Info {
72bdd1243dSDimitry Andric   llvm::StringLiteral Name;
73bdd1243dSDimitry Andric   const char *Type, *Attributes;
740b57cec5SDimitry Andric   const char *Features;
75bdd1243dSDimitry Andric   HeaderDesc Header;
76bdd1243dSDimitry Andric   LanguageID Langs;
770b57cec5SDimitry Andric };
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric /// Holds information about both target-independent and
800b57cec5SDimitry Andric /// target-specific builtins, allowing easy queries by clients.
810b57cec5SDimitry Andric ///
820b57cec5SDimitry Andric /// Builtins from an optional auxiliary target are stored in
830b57cec5SDimitry Andric /// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
840b57cec5SDimitry Andric /// be translated back with getAuxBuiltinID() before use.
850b57cec5SDimitry Andric class Context {
860b57cec5SDimitry Andric   llvm::ArrayRef<Info> TSRecords;
870b57cec5SDimitry Andric   llvm::ArrayRef<Info> AuxTSRecords;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric public:
9081ad6265SDimitry Andric   Context() = default;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   /// Perform target-specific initialization
930b57cec5SDimitry Andric   /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
940b57cec5SDimitry Andric   void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   /// Mark the identifiers for all the builtins with their
970b57cec5SDimitry Andric   /// appropriate builtin ID # and mark any non-portable builtin identifiers as
980b57cec5SDimitry Andric   /// such.
990b57cec5SDimitry Andric   void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   /// Return the identifier name for the specified builtin,
1020b57cec5SDimitry Andric   /// e.g. "__builtin_abs".
103bdd1243dSDimitry Andric   llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   /// Get the type descriptor string for the specified builtin.
1060b57cec5SDimitry Andric   const char *getTypeString(unsigned ID) const {
1070b57cec5SDimitry Andric     return getRecord(ID).Type;
1080b57cec5SDimitry Andric   }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   /// Return true if this function is a target-specific builtin.
1110b57cec5SDimitry Andric   bool isTSBuiltin(unsigned ID) const {
1120b57cec5SDimitry Andric     return ID >= Builtin::FirstTSBuiltin;
1130b57cec5SDimitry Andric   }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   /// Return true if this function has no side effects.
1160b57cec5SDimitry Andric   bool isPure(unsigned ID) const {
1170b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'U') != nullptr;
1180b57cec5SDimitry Andric   }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   /// Return true if this function has no side effects and doesn't
1210b57cec5SDimitry Andric   /// read memory.
1220b57cec5SDimitry Andric   bool isConst(unsigned ID) const {
1230b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'c') != nullptr;
1240b57cec5SDimitry Andric   }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   /// Return true if we know this builtin never throws an exception.
1270b57cec5SDimitry Andric   bool isNoThrow(unsigned ID) const {
1280b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'n') != nullptr;
1290b57cec5SDimitry Andric   }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   /// Return true if we know this builtin never returns.
1320b57cec5SDimitry Andric   bool isNoReturn(unsigned ID) const {
1330b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'r') != nullptr;
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   /// Return true if we know this builtin can return twice.
1370b57cec5SDimitry Andric   bool isReturnsTwice(unsigned ID) const {
1380b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'j') != nullptr;
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   /// Returns true if this builtin does not perform the side-effects
1420b57cec5SDimitry Andric   /// of its arguments.
1430b57cec5SDimitry Andric   bool isUnevaluated(unsigned ID) const {
1440b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'u') != nullptr;
1450b57cec5SDimitry Andric   }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   /// Return true if this is a builtin for a libc/libm function,
1480b57cec5SDimitry Andric   /// with a "__builtin_" prefix (e.g. __builtin_abs).
1490b57cec5SDimitry Andric   bool isLibFunction(unsigned ID) const {
1500b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'F') != nullptr;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   /// Determines whether this builtin is a predefined libc/libm
1540b57cec5SDimitry Andric   /// function, such as "malloc", where we know the signature a
1550b57cec5SDimitry Andric   /// priori.
15681ad6265SDimitry Andric   /// In C, such functions behave as if they are predeclared,
15781ad6265SDimitry Andric   /// possibly with a warning on first use. In Objective-C and C++,
15881ad6265SDimitry Andric   /// they do not, but they are recognized as builtins once we see
15981ad6265SDimitry Andric   /// a declaration.
1600b57cec5SDimitry Andric   bool isPredefinedLibFunction(unsigned ID) const {
1610b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'f') != nullptr;
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   /// Returns true if this builtin requires appropriate header in other
1650b57cec5SDimitry Andric   /// compilers. In Clang it will work even without including it, but we can emit
1660b57cec5SDimitry Andric   /// a warning about missing header.
1670b57cec5SDimitry Andric   bool isHeaderDependentFunction(unsigned ID) const {
1680b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'h') != nullptr;
1690b57cec5SDimitry Andric   }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   /// Determines whether this builtin is a predefined compiler-rt/libgcc
1720b57cec5SDimitry Andric   /// function, such as "__clear_cache", where we know the signature a
1730b57cec5SDimitry Andric   /// priori.
1740b57cec5SDimitry Andric   bool isPredefinedRuntimeFunction(unsigned ID) const {
1750b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'i') != nullptr;
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric 
17881ad6265SDimitry Andric   /// Determines whether this builtin is a C++ standard library function
17981ad6265SDimitry Andric   /// that lives in (possibly-versioned) namespace std, possibly a template
18081ad6265SDimitry Andric   /// specialization, where the signature is determined by the standard library
18181ad6265SDimitry Andric   /// declaration.
18281ad6265SDimitry Andric   bool isInStdNamespace(unsigned ID) const {
18381ad6265SDimitry Andric     return strchr(getRecord(ID).Attributes, 'z') != nullptr;
18481ad6265SDimitry Andric   }
18581ad6265SDimitry Andric 
18681ad6265SDimitry Andric   /// Determines whether this builtin can have its address taken with no
18781ad6265SDimitry Andric   /// special action required.
18881ad6265SDimitry Andric   bool isDirectlyAddressable(unsigned ID) const {
18981ad6265SDimitry Andric     // Most standard library functions can have their addresses taken. C++
19081ad6265SDimitry Andric     // standard library functions formally cannot in C++20 onwards, and when
19181ad6265SDimitry Andric     // we allow it, we need to ensure we instantiate a definition.
19281ad6265SDimitry Andric     return isPredefinedLibFunction(ID) && !isInStdNamespace(ID);
19381ad6265SDimitry Andric   }
19481ad6265SDimitry Andric 
1950b57cec5SDimitry Andric   /// Determines whether this builtin has custom typechecking.
1960b57cec5SDimitry Andric   bool hasCustomTypechecking(unsigned ID) const {
1970b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 't') != nullptr;
1980b57cec5SDimitry Andric   }
1990b57cec5SDimitry Andric 
200eaeb601bSDimitry Andric   /// Determines whether a declaration of this builtin should be recognized
201eaeb601bSDimitry Andric   /// even if the type doesn't match the specified signature.
202eaeb601bSDimitry Andric   bool allowTypeMismatch(unsigned ID) const {
203eaeb601bSDimitry Andric     return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
204eaeb601bSDimitry Andric            hasCustomTypechecking(ID);
205eaeb601bSDimitry Andric   }
206eaeb601bSDimitry Andric 
2070b57cec5SDimitry Andric   /// Determines whether this builtin has a result or any arguments which
2080b57cec5SDimitry Andric   /// are pointer types.
2090b57cec5SDimitry Andric   bool hasPtrArgsOrResult(unsigned ID) const {
2100b57cec5SDimitry Andric     return strchr(getRecord(ID).Type, '*') != nullptr;
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   /// Return true if this builtin has a result or any arguments which are
2140b57cec5SDimitry Andric   /// reference types.
2150b57cec5SDimitry Andric   bool hasReferenceArgsOrResult(unsigned ID) const {
2160b57cec5SDimitry Andric     return strchr(getRecord(ID).Type, '&') != nullptr ||
2170b57cec5SDimitry Andric            strchr(getRecord(ID).Type, 'A') != nullptr;
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   /// If this is a library function that comes from a specific
2210b57cec5SDimitry Andric   /// header, retrieve that header name.
2220b57cec5SDimitry Andric   const char *getHeaderName(unsigned ID) const {
223bdd1243dSDimitry Andric     return getRecord(ID).Header.getName();
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   /// Determine whether this builtin is like printf in its
2270b57cec5SDimitry Andric   /// formatting rules and, if so, set the index to the format string
2280b57cec5SDimitry Andric   /// argument and whether this function as a va_list argument.
2290b57cec5SDimitry Andric   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   /// Determine whether this builtin is like scanf in its
2320b57cec5SDimitry Andric   /// formatting rules and, if so, set the index to the format string
2330b57cec5SDimitry Andric   /// argument and whether this function as a va_list argument.
2340b57cec5SDimitry Andric   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   /// Determine whether this builtin has callback behavior (see
2370b57cec5SDimitry Andric   /// llvm::AbstractCallSites for details). If so, add the index to the
2380b57cec5SDimitry Andric   /// callback callee argument and the callback payload arguments.
2390b57cec5SDimitry Andric   bool performsCallback(unsigned ID,
2400b57cec5SDimitry Andric                         llvm::SmallVectorImpl<int> &Encoding) const;
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   /// Return true if this function has no side effects and doesn't
243bdd1243dSDimitry Andric   /// read memory, except for possibly errno or raising FP exceptions.
2440b57cec5SDimitry Andric   ///
245bdd1243dSDimitry Andric   /// Such functions can be const when the MathErrno lang option and FP
246bdd1243dSDimitry Andric   /// exceptions are disabled.
247bdd1243dSDimitry Andric   bool isConstWithoutErrnoAndExceptions(unsigned ID) const {
2480b57cec5SDimitry Andric     return strchr(getRecord(ID).Attributes, 'e') != nullptr;
2490b57cec5SDimitry Andric   }
2500b57cec5SDimitry Andric 
251bdd1243dSDimitry Andric   bool isConstWithoutExceptions(unsigned ID) const {
252bdd1243dSDimitry Andric     return strchr(getRecord(ID).Attributes, 'g') != nullptr;
253bdd1243dSDimitry Andric   }
254bdd1243dSDimitry Andric 
2550b57cec5SDimitry Andric   const char *getRequiredFeatures(unsigned ID) const {
2560b57cec5SDimitry Andric     return getRecord(ID).Features;
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   unsigned getRequiredVectorWidth(unsigned ID) const;
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   /// Return true if builtin ID belongs to AuxTarget.
2620b57cec5SDimitry Andric   bool isAuxBuiltinID(unsigned ID) const {
2630b57cec5SDimitry Andric     return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
2640b57cec5SDimitry Andric   }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   /// Return real builtin ID (i.e. ID it would have during compilation
2670b57cec5SDimitry Andric   /// for AuxTarget).
2680b57cec5SDimitry Andric   unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   /// Returns true if this is a libc/libm function without the '__builtin_'
2710b57cec5SDimitry Andric   /// prefix.
272480093f4SDimitry Andric   static bool isBuiltinFunc(llvm::StringRef Name);
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   /// Returns true if this is a builtin that can be redeclared.  Returns true
2750b57cec5SDimitry Andric   /// for non-builtins.
2760b57cec5SDimitry Andric   bool canBeRedeclared(unsigned ID) const;
2770b57cec5SDimitry Andric 
278bdd1243dSDimitry Andric   /// Return true if this function can be constant evaluated by Clang frontend.
279bdd1243dSDimitry Andric   bool isConstantEvaluated(unsigned ID) const {
280bdd1243dSDimitry Andric     return strchr(getRecord(ID).Attributes, 'E') != nullptr;
281bdd1243dSDimitry Andric   }
282bdd1243dSDimitry Andric 
283*0fca6ea1SDimitry Andric   /// Returns true if this is an immediate (consteval) function
284*0fca6ea1SDimitry Andric   bool isImmediate(unsigned ID) const {
285*0fca6ea1SDimitry Andric     return strchr(getRecord(ID).Attributes, 'G') != nullptr;
286*0fca6ea1SDimitry Andric   }
287*0fca6ea1SDimitry Andric 
2880b57cec5SDimitry Andric private:
2890b57cec5SDimitry Andric   const Info &getRecord(unsigned ID) const;
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   /// Helper function for isPrintfLike and isScanfLike.
2920b57cec5SDimitry Andric   bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
2930b57cec5SDimitry Andric               const char *Fmt) const;
2940b57cec5SDimitry Andric };
2950b57cec5SDimitry Andric 
29681ad6265SDimitry Andric /// Returns true if the required target features of a builtin function are
29781ad6265SDimitry Andric /// enabled.
29881ad6265SDimitry Andric /// \p TargetFeatureMap maps a target feature to true if it is enabled and
29981ad6265SDimitry Andric ///    false if it is disabled.
30081ad6265SDimitry Andric bool evaluateRequiredTargetFeatures(
30181ad6265SDimitry Andric     llvm::StringRef RequiredFatures,
30281ad6265SDimitry Andric     const llvm::StringMap<bool> &TargetFetureMap);
30381ad6265SDimitry Andric 
30481ad6265SDimitry Andric } // namespace Builtin
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric /// Kinds of BuiltinTemplateDecl.
3070b57cec5SDimitry Andric enum BuiltinTemplateKind : int {
3080b57cec5SDimitry Andric   /// This names the __make_integer_seq BuiltinTemplateDecl.
3090b57cec5SDimitry Andric   BTK__make_integer_seq,
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   /// This names the __type_pack_element BuiltinTemplateDecl.
3120b57cec5SDimitry Andric   BTK__type_pack_element
3130b57cec5SDimitry Andric };
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric } // end namespace clang
3160b57cec5SDimitry Andric #endif
317