Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:48

0001 //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// Defines enum values for all the target-independent builtin
0011 /// functions.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_BASIC_BUILTINS_H
0016 #define LLVM_CLANG_BASIC_BUILTINS_H
0017 
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/StringMap.h"
0020 #include "llvm/ADT/StringRef.h"
0021 #include <cstring>
0022 
0023 // VC++ defines 'alloca' as an object-like macro, which interferes with our
0024 // builtins.
0025 #undef alloca
0026 
0027 namespace clang {
0028 class TargetInfo;
0029 class IdentifierTable;
0030 class LangOptions;
0031 
0032 enum LanguageID : uint16_t {
0033   GNU_LANG = 0x1,            // builtin requires GNU mode.
0034   C_LANG = 0x2,              // builtin for c only.
0035   CXX_LANG = 0x4,            // builtin for cplusplus only.
0036   OBJC_LANG = 0x8,           // builtin for objective-c and objective-c++
0037   MS_LANG = 0x10,            // builtin requires MS mode.
0038   OMP_LANG = 0x20,           // builtin requires OpenMP.
0039   CUDA_LANG = 0x40,          // builtin requires CUDA.
0040   COR_LANG = 0x80,           // builtin requires use of 'fcoroutine-ts' option.
0041   OCL_GAS = 0x100,           // builtin requires OpenCL generic address space.
0042   OCL_PIPE = 0x200,          // builtin requires OpenCL pipe.
0043   OCL_DSE = 0x400,           // builtin requires OpenCL device side enqueue.
0044   ALL_OCL_LANGUAGES = 0x800, // builtin for OCL languages.
0045   HLSL_LANG = 0x1000,        // builtin requires HLSL.
0046   ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
0047   ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
0048   ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG     // builtin requires MS mode.
0049 };
0050 
0051 struct HeaderDesc {
0052   enum HeaderID : uint16_t {
0053 #define HEADER(ID, NAME) ID,
0054 #include "clang/Basic/BuiltinHeaders.def"
0055 #undef HEADER
0056   } ID;
0057 
0058   constexpr HeaderDesc(HeaderID ID) : ID(ID) {}
0059 
0060   const char *getName() const;
0061 };
0062 
0063 namespace Builtin {
0064 enum ID {
0065   NotBuiltin  = 0,      // This is not a builtin function.
0066 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
0067 #include "clang/Basic/Builtins.inc"
0068   FirstTSBuiltin
0069 };
0070 
0071 struct Info {
0072   llvm::StringLiteral Name;
0073   const char *Type, *Attributes;
0074   const char *Features;
0075   HeaderDesc Header;
0076   LanguageID Langs;
0077 };
0078 
0079 /// Holds information about both target-independent and
0080 /// target-specific builtins, allowing easy queries by clients.
0081 ///
0082 /// Builtins from an optional auxiliary target are stored in
0083 /// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
0084 /// be translated back with getAuxBuiltinID() before use.
0085 class Context {
0086   llvm::ArrayRef<Info> TSRecords;
0087   llvm::ArrayRef<Info> AuxTSRecords;
0088 
0089 public:
0090   Context() = default;
0091 
0092   /// Perform target-specific initialization
0093   /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
0094   void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
0095 
0096   /// Mark the identifiers for all the builtins with their
0097   /// appropriate builtin ID # and mark any non-portable builtin identifiers as
0098   /// such.
0099   void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
0100 
0101   /// Return the identifier name for the specified builtin,
0102   /// e.g. "__builtin_abs".
0103   llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }
0104 
0105   /// Return a quoted name for the specified builtin for use in diagnostics.
0106   std::string getQuotedName(unsigned ID) const;
0107 
0108   /// Get the type descriptor string for the specified builtin.
0109   const char *getTypeString(unsigned ID) const { return getRecord(ID).Type; }
0110 
0111   /// Return true if this function is a target-specific builtin.
0112   bool isTSBuiltin(unsigned ID) const {
0113     return ID >= Builtin::FirstTSBuiltin;
0114   }
0115 
0116   /// Return true if this function has no side effects.
0117   bool isPure(unsigned ID) const {
0118     return strchr(getRecord(ID).Attributes, 'U') != nullptr;
0119   }
0120 
0121   /// Return true if this function has no side effects and doesn't
0122   /// read memory.
0123   bool isConst(unsigned ID) const {
0124     return strchr(getRecord(ID).Attributes, 'c') != nullptr;
0125   }
0126 
0127   /// Return true if we know this builtin never throws an exception.
0128   bool isNoThrow(unsigned ID) const {
0129     return strchr(getRecord(ID).Attributes, 'n') != nullptr;
0130   }
0131 
0132   /// Return true if we know this builtin never returns.
0133   bool isNoReturn(unsigned ID) const {
0134     return strchr(getRecord(ID).Attributes, 'r') != nullptr;
0135   }
0136 
0137   /// Return true if we know this builtin can return twice.
0138   bool isReturnsTwice(unsigned ID) const {
0139     return strchr(getRecord(ID).Attributes, 'j') != nullptr;
0140   }
0141 
0142   /// Returns true if this builtin does not perform the side-effects
0143   /// of its arguments.
0144   bool isUnevaluated(unsigned ID) const {
0145     return strchr(getRecord(ID).Attributes, 'u') != nullptr;
0146   }
0147 
0148   /// Return true if this is a builtin for a libc/libm function,
0149   /// with a "__builtin_" prefix (e.g. __builtin_abs).
0150   bool isLibFunction(unsigned ID) const {
0151     return strchr(getRecord(ID).Attributes, 'F') != nullptr;
0152   }
0153 
0154   /// Determines whether this builtin is a predefined libc/libm
0155   /// function, such as "malloc", where we know the signature a
0156   /// priori.
0157   /// In C, such functions behave as if they are predeclared,
0158   /// possibly with a warning on first use. In Objective-C and C++,
0159   /// they do not, but they are recognized as builtins once we see
0160   /// a declaration.
0161   bool isPredefinedLibFunction(unsigned ID) const {
0162     return strchr(getRecord(ID).Attributes, 'f') != nullptr;
0163   }
0164 
0165   /// Returns true if this builtin requires appropriate header in other
0166   /// compilers. In Clang it will work even without including it, but we can emit
0167   /// a warning about missing header.
0168   bool isHeaderDependentFunction(unsigned ID) const {
0169     return strchr(getRecord(ID).Attributes, 'h') != nullptr;
0170   }
0171 
0172   /// Determines whether this builtin is a predefined compiler-rt/libgcc
0173   /// function, such as "__clear_cache", where we know the signature a
0174   /// priori.
0175   bool isPredefinedRuntimeFunction(unsigned ID) const {
0176     return strchr(getRecord(ID).Attributes, 'i') != nullptr;
0177   }
0178 
0179   /// Determines whether this builtin is a C++ standard library function
0180   /// that lives in (possibly-versioned) namespace std, possibly a template
0181   /// specialization, where the signature is determined by the standard library
0182   /// declaration.
0183   bool isInStdNamespace(unsigned ID) const {
0184     return strchr(getRecord(ID).Attributes, 'z') != nullptr;
0185   }
0186 
0187   /// Determines whether this builtin can have its address taken with no
0188   /// special action required.
0189   bool isDirectlyAddressable(unsigned ID) const {
0190     // Most standard library functions can have their addresses taken. C++
0191     // standard library functions formally cannot in C++20 onwards, and when
0192     // we allow it, we need to ensure we instantiate a definition.
0193     return isPredefinedLibFunction(ID) && !isInStdNamespace(ID);
0194   }
0195 
0196   /// Determines whether this builtin has custom typechecking.
0197   bool hasCustomTypechecking(unsigned ID) const {
0198     return strchr(getRecord(ID).Attributes, 't') != nullptr;
0199   }
0200 
0201   /// Determines whether a declaration of this builtin should be recognized
0202   /// even if the type doesn't match the specified signature.
0203   bool allowTypeMismatch(unsigned ID) const {
0204     return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
0205            hasCustomTypechecking(ID);
0206   }
0207 
0208   /// Determines whether this builtin has a result or any arguments which
0209   /// are pointer types.
0210   bool hasPtrArgsOrResult(unsigned ID) const {
0211     return strchr(getRecord(ID).Type, '*') != nullptr;
0212   }
0213 
0214   /// Return true if this builtin has a result or any arguments which are
0215   /// reference types.
0216   bool hasReferenceArgsOrResult(unsigned ID) const {
0217     return strchr(getRecord(ID).Type, '&') != nullptr ||
0218            strchr(getRecord(ID).Type, 'A') != nullptr;
0219   }
0220 
0221   /// If this is a library function that comes from a specific
0222   /// header, retrieve that header name.
0223   const char *getHeaderName(unsigned ID) const {
0224     return getRecord(ID).Header.getName();
0225   }
0226 
0227   /// Determine whether this builtin is like printf in its
0228   /// formatting rules and, if so, set the index to the format string
0229   /// argument and whether this function as a va_list argument.
0230   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
0231 
0232   /// Determine whether this builtin is like scanf in its
0233   /// formatting rules and, if so, set the index to the format string
0234   /// argument and whether this function as a va_list argument.
0235   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
0236 
0237   /// Determine whether this builtin has callback behavior (see
0238   /// llvm::AbstractCallSites for details). If so, add the index to the
0239   /// callback callee argument and the callback payload arguments.
0240   bool performsCallback(unsigned ID,
0241                         llvm::SmallVectorImpl<int> &Encoding) const;
0242 
0243   /// Return true if this function has no side effects and doesn't
0244   /// read memory, except for possibly errno or raising FP exceptions.
0245   ///
0246   /// Such functions can be const when the MathErrno lang option and FP
0247   /// exceptions are disabled.
0248   bool isConstWithoutErrnoAndExceptions(unsigned ID) const {
0249     return strchr(getRecord(ID).Attributes, 'e') != nullptr;
0250   }
0251 
0252   bool isConstWithoutExceptions(unsigned ID) const {
0253     return strchr(getRecord(ID).Attributes, 'g') != nullptr;
0254   }
0255 
0256   const char *getRequiredFeatures(unsigned ID) const {
0257     return getRecord(ID).Features;
0258   }
0259 
0260   unsigned getRequiredVectorWidth(unsigned ID) const;
0261 
0262   /// Return true if builtin ID belongs to AuxTarget.
0263   bool isAuxBuiltinID(unsigned ID) const {
0264     return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
0265   }
0266 
0267   /// Return real builtin ID (i.e. ID it would have during compilation
0268   /// for AuxTarget).
0269   unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
0270 
0271   /// Returns true if this is a libc/libm function without the '__builtin_'
0272   /// prefix.
0273   static bool isBuiltinFunc(llvm::StringRef Name);
0274 
0275   /// Returns true if this is a builtin that can be redeclared.  Returns true
0276   /// for non-builtins.
0277   bool canBeRedeclared(unsigned ID) const;
0278 
0279   /// Return true if this function can be constant evaluated by Clang frontend.
0280   bool isConstantEvaluated(unsigned ID) const {
0281     return strchr(getRecord(ID).Attributes, 'E') != nullptr;
0282   }
0283 
0284   /// Returns true if this is an immediate (consteval) function
0285   bool isImmediate(unsigned ID) const {
0286     return strchr(getRecord(ID).Attributes, 'G') != nullptr;
0287   }
0288 
0289 private:
0290   const Info &getRecord(unsigned ID) const;
0291 
0292   /// Helper function for isPrintfLike and isScanfLike.
0293   bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
0294               const char *Fmt) const;
0295 };
0296 
0297 /// Returns true if the required target features of a builtin function are
0298 /// enabled.
0299 /// \p TargetFeatureMap maps a target feature to true if it is enabled and
0300 ///    false if it is disabled.
0301 bool evaluateRequiredTargetFeatures(
0302     llvm::StringRef RequiredFatures,
0303     const llvm::StringMap<bool> &TargetFetureMap);
0304 
0305 } // namespace Builtin
0306 
0307 /// Kinds of BuiltinTemplateDecl.
0308 enum BuiltinTemplateKind : int {
0309   /// This names the __make_integer_seq BuiltinTemplateDecl.
0310   BTK__make_integer_seq,
0311 
0312   /// This names the __type_pack_element BuiltinTemplateDecl.
0313   BTK__type_pack_element,
0314 
0315   /// This names the __builtin_common_type BuiltinTemplateDecl.
0316   BTK__builtin_common_type,
0317 };
0318 
0319 } // end namespace clang
0320 #endif