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 "clang/Basic/LLVM.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     ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
     39     ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG   // builtin requires GNU mode.
     40   };
     41 
     42 namespace Builtin {
     43 enum ID {
     44   NotBuiltin  = 0,      // This is not a builtin function.
     45 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
     46 #include "clang/Basic/Builtins.def"
     47   FirstTSBuiltin
     48 };
     49 
     50 struct Info {
     51   const char *Name, *Type, *Attributes, *HeaderName;
     52   LanguageID builtin_lang;
     53 
     54   bool operator==(const Info &RHS) const {
     55     return !strcmp(Name, RHS.Name) &&
     56            !strcmp(Type, RHS.Type) &&
     57            !strcmp(Attributes, RHS.Attributes);
     58   }
     59   bool operator!=(const Info &RHS) const { return !(*this == RHS); }
     60 };
     61 
     62 /// \brief Holds information about both target-independent and
     63 /// target-specific builtins, allowing easy queries by clients.
     64 class Context {
     65   const Info *TSRecords;
     66   unsigned NumTSRecords;
     67 public:
     68   Context();
     69 
     70   /// \brief Perform target-specific initialization
     71   void InitializeTarget(const TargetInfo &Target);
     72 
     73   /// \brief Mark the identifiers for all the builtins with their
     74   /// appropriate builtin ID # and mark any non-portable builtin identifiers as
     75   /// such.
     76   void InitializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
     77 
     78   /// \brief Populate the vector with the names of all of the builtins.
     79   void GetBuiltinNames(SmallVectorImpl<const char *> &Names);
     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 has no side effects and doesn't
     93   /// read memory.
     94   bool isConst(unsigned ID) const {
     95     return strchr(GetRecord(ID).Attributes, 'c') != 0;
     96   }
     97 
     98   /// \brief Return true if we know this builtin never throws an exception.
     99   bool isNoThrow(unsigned ID) const {
    100     return strchr(GetRecord(ID).Attributes, 'n') != 0;
    101   }
    102 
    103   /// \brief Return true if we know this builtin never returns.
    104   bool isNoReturn(unsigned ID) const {
    105     return strchr(GetRecord(ID).Attributes, 'r') != 0;
    106   }
    107 
    108   /// \brief Return true if we know this builtin can return twice.
    109   bool isReturnsTwice(unsigned ID) const {
    110     return strchr(GetRecord(ID).Attributes, 'j') != 0;
    111   }
    112 
    113   /// \brief Returns true if this builtin does not perform the side-effects
    114   /// of its arguments.
    115   bool isUnevaluated(unsigned ID) const {
    116     return strchr(GetRecord(ID).Attributes, 'u') != 0;
    117   }
    118 
    119   /// \brief Return true if this is a builtin for a libc/libm function,
    120   /// with a "__builtin_" prefix (e.g. __builtin_abs).
    121   bool isLibFunction(unsigned ID) const {
    122     return strchr(GetRecord(ID).Attributes, 'F') != 0;
    123   }
    124 
    125   /// \brief Determines whether this builtin is a predefined libc/libm
    126   /// function, such as "malloc", where we know the signature a
    127   /// priori.
    128   bool isPredefinedLibFunction(unsigned ID) const {
    129     return strchr(GetRecord(ID).Attributes, 'f') != 0;
    130   }
    131 
    132   /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
    133   /// function, such as "__clear_cache", where we know the signature a
    134   /// priori.
    135   bool isPredefinedRuntimeFunction(unsigned ID) const {
    136     return strchr(GetRecord(ID).Attributes, 'i') != 0;
    137   }
    138 
    139   /// \brief Determines whether this builtin has custom typechecking.
    140   bool hasCustomTypechecking(unsigned ID) const {
    141     return strchr(GetRecord(ID).Attributes, 't') != 0;
    142   }
    143 
    144   /// \brief Completely forget that the given ID was ever considered a builtin,
    145   /// e.g., because the user provided a conflicting signature.
    146   void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
    147 
    148   /// \brief If this is a library function that comes from a specific
    149   /// header, retrieve that header name.
    150   const char *getHeaderName(unsigned ID) const {
    151     return GetRecord(ID).HeaderName;
    152   }
    153 
    154   /// \brief Determine whether this builtin is like printf in its
    155   /// formatting rules and, if so, set the index to the format string
    156   /// argument and whether this function as a va_list argument.
    157   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
    158 
    159   /// \brief Determine whether this builtin is like scanf in its
    160   /// formatting rules and, if so, set the index to the format string
    161   /// argument and whether this function as a va_list argument.
    162   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
    163 
    164   /// \brief Return true if this function has no side effects and doesn't
    165   /// read memory, except for possibly errno.
    166   ///
    167   /// Such functions can be const when the MathErrno lang option is disabled.
    168   bool isConstWithoutErrno(unsigned ID) const {
    169     return strchr(GetRecord(ID).Attributes, 'e') != 0;
    170   }
    171 
    172 private:
    173   const Info &GetRecord(unsigned ID) const;
    174 
    175   /// \brief Is this builtin supported according to the given language options?
    176   bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
    177                           const LangOptions &LangOpts);
    178 };
    179 
    180 }
    181 } // end namespace clang
    182 #endif
    183