Home | History | Annotate | Download | only in Basic
      1 //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// \brief Defines enum values for all the target-independent builtin
     12 /// functions.
     13 ///
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_CLANG_BASIC_BUILTINS_H
     17 #define LLVM_CLANG_BASIC_BUILTINS_H
     18 
     19 #include "llvm/ADT/ArrayRef.h"
     20 #include <cstring>
     21 
     22 // VC++ defines 'alloca' as an object-like macro, which interferes with our
     23 // builtins.
     24 #undef alloca
     25 
     26 namespace clang {
     27 class TargetInfo;
     28 class IdentifierTable;
     29 class ASTContext;
     30 class QualType;
     31 class LangOptions;
     32 
     33 enum LanguageID {
     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 only.
     40   ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
     41   ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
     42   ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG     // builtin requires MS mode.
     43 };
     44 
     45 namespace Builtin {
     46 enum ID {
     47   NotBuiltin  = 0,      // This is not a builtin function.
     48 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
     49 #include "clang/Basic/Builtins.def"
     50   FirstTSBuiltin
     51 };
     52 
     53 struct Info {
     54   const char *Name, *Type, *Attributes, *HeaderName;
     55   LanguageID Langs;
     56   const char *Features;
     57 };
     58 
     59 /// \brief Holds information about both target-independent and
     60 /// target-specific builtins, allowing easy queries by clients.
     61 ///
     62 /// Builtins from an optional auxiliary target are stored in
     63 /// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
     64 /// be translated back with getAuxBuiltinID() before use.
     65 class Context {
     66   llvm::ArrayRef<Info> TSRecords;
     67   llvm::ArrayRef<Info> AuxTSRecords;
     68 
     69 public:
     70   Context() {}
     71 
     72   /// \brief Perform target-specific initialization
     73   /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
     74   void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
     75 
     76   /// \brief Mark the identifiers for all the builtins with their
     77   /// appropriate builtin ID # and mark any non-portable builtin identifiers as
     78   /// such.
     79   void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
     80 
     81   /// \brief Return the identifier name for the specified builtin,
     82   /// e.g. "__builtin_abs".
     83   const char *getName(unsigned ID) const {
     84     return getRecord(ID).Name;
     85   }
     86 
     87   /// \brief Get the type descriptor string for the specified builtin.
     88   const char *getTypeString(unsigned ID) const {
     89     return getRecord(ID).Type;
     90   }
     91 
     92   /// \brief Return true if this function is a target-specific builtin.
     93   bool isTSBuiltin(unsigned ID) const {
     94     return ID >= Builtin::FirstTSBuiltin;
     95   }
     96 
     97   /// \brief Return true if this function has no side effects.
     98   bool isPure(unsigned ID) const {
     99     return strchr(getRecord(ID).Attributes, 'U') != nullptr;
    100   }
    101 
    102   /// \brief Return true if this function has no side effects and doesn't
    103   /// read memory.
    104   bool isConst(unsigned ID) const {
    105     return strchr(getRecord(ID).Attributes, 'c') != nullptr;
    106   }
    107 
    108   /// \brief Return true if we know this builtin never throws an exception.
    109   bool isNoThrow(unsigned ID) const {
    110     return strchr(getRecord(ID).Attributes, 'n') != nullptr;
    111   }
    112 
    113   /// \brief Return true if we know this builtin never returns.
    114   bool isNoReturn(unsigned ID) const {
    115     return strchr(getRecord(ID).Attributes, 'r') != nullptr;
    116   }
    117 
    118   /// \brief Return true if we know this builtin can return twice.
    119   bool isReturnsTwice(unsigned ID) const {
    120     return strchr(getRecord(ID).Attributes, 'j') != nullptr;
    121   }
    122 
    123   /// \brief Returns true if this builtin does not perform the side-effects
    124   /// of its arguments.
    125   bool isUnevaluated(unsigned ID) const {
    126     return strchr(getRecord(ID).Attributes, 'u') != nullptr;
    127   }
    128 
    129   /// \brief Return true if this is a builtin for a libc/libm function,
    130   /// with a "__builtin_" prefix (e.g. __builtin_abs).
    131   bool isLibFunction(unsigned ID) const {
    132     return strchr(getRecord(ID).Attributes, 'F') != nullptr;
    133   }
    134 
    135   /// \brief Determines whether this builtin is a predefined libc/libm
    136   /// function, such as "malloc", where we know the signature a
    137   /// priori.
    138   bool isPredefinedLibFunction(unsigned ID) const {
    139     return strchr(getRecord(ID).Attributes, 'f') != nullptr;
    140   }
    141 
    142   /// \brief Returns true if this builtin requires appropriate header in other
    143   /// compilers. In Clang it will work even without including it, but we can emit
    144   /// a warning about missing header.
    145   bool isHeaderDependentFunction(unsigned ID) const {
    146     return strchr(getRecord(ID).Attributes, 'h') != nullptr;
    147   }
    148 
    149   /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
    150   /// function, such as "__clear_cache", where we know the signature a
    151   /// priori.
    152   bool isPredefinedRuntimeFunction(unsigned ID) const {
    153     return strchr(getRecord(ID).Attributes, 'i') != nullptr;
    154   }
    155 
    156   /// \brief Determines whether this builtin has custom typechecking.
    157   bool hasCustomTypechecking(unsigned ID) const {
    158     return strchr(getRecord(ID).Attributes, 't') != nullptr;
    159   }
    160 
    161   /// \brief Determines whether this builtin has a result or any arguments which
    162   /// are pointer types.
    163   bool hasPtrArgsOrResult(unsigned ID) const {
    164     return strchr(getRecord(ID).Type, '*') != nullptr;
    165   }
    166 
    167   /// \brief Completely forget that the given ID was ever considered a builtin,
    168   /// e.g., because the user provided a conflicting signature.
    169   void forgetBuiltin(unsigned ID, IdentifierTable &Table);
    170 
    171   /// \brief If this is a library function that comes from a specific
    172   /// header, retrieve that header name.
    173   const char *getHeaderName(unsigned ID) const {
    174     return getRecord(ID).HeaderName;
    175   }
    176 
    177   /// \brief Determine whether this builtin is like printf in its
    178   /// formatting rules and, if so, set the index to the format string
    179   /// argument and whether this function as a va_list argument.
    180   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
    181 
    182   /// \brief Determine whether this builtin is like scanf in its
    183   /// formatting rules and, if so, set the index to the format string
    184   /// argument and whether this function as a va_list argument.
    185   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
    186 
    187   /// \brief Return true if this function has no side effects and doesn't
    188   /// read memory, except for possibly errno.
    189   ///
    190   /// Such functions can be const when the MathErrno lang option is disabled.
    191   bool isConstWithoutErrno(unsigned ID) const {
    192     return strchr(getRecord(ID).Attributes, 'e') != nullptr;
    193   }
    194 
    195   const char *getRequiredFeatures(unsigned ID) const {
    196     return getRecord(ID).Features;
    197   }
    198 
    199   /// \brief Return true if builtin ID belongs to AuxTarget.
    200   bool isAuxBuiltinID(unsigned ID) const {
    201     return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
    202   }
    203 
    204   /// Return real buitin ID (i.e. ID it would have furing compilation
    205   /// for AuxTarget).
    206   unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
    207 
    208   /// Returns true if this is a libc/libm function without the '__builtin_'
    209   /// prefix.
    210   static bool isBuiltinFunc(const char *Name);
    211 
    212 private:
    213   const Info &getRecord(unsigned ID) const;
    214 
    215   /// \brief Is this builtin supported according to the given language options?
    216   bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
    217                           const LangOptions &LangOpts);
    218 
    219   /// \brief Helper function for isPrintfLike and isScanfLike.
    220   bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
    221               const char *Fmt) const;
    222 };
    223 
    224 }
    225 
    226 /// \brief Kinds of BuiltinTemplateDecl.
    227 enum BuiltinTemplateKind : int {
    228   /// \brief This names the __make_integer_seq BuiltinTemplateDecl.
    229   BTK__make_integer_seq,
    230 
    231   /// \brief This names the __type_pack_element BuiltinTemplateDecl.
    232   BTK__type_pack_element
    233 };
    234 
    235 } // end namespace clang
    236 #endif
    237