clang API Documentation

Builtins.h
Go to the documentation of this file.
00001 //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Defines enum values for all the target-independent builtin
00012 /// functions.
00013 ///
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CLANG_BASIC_BUILTINS_H
00017 #define LLVM_CLANG_BASIC_BUILTINS_H
00018 
00019 #include "clang/Basic/LLVM.h"
00020 #include <cstring>
00021 
00022 // VC++ defines 'alloca' as an object-like macro, which interferes with our
00023 // builtins.
00024 #undef alloca
00025 
00026 namespace clang {
00027   class TargetInfo;
00028   class IdentifierTable;
00029   class ASTContext;
00030   class QualType;
00031   class LangOptions;
00032   
00033   enum LanguageID {
00034     GNU_LANG = 0x1,  // builtin requires GNU mode.
00035     C_LANG = 0x2,    // builtin for c only.
00036     CXX_LANG = 0x4,  // builtin for cplusplus only.
00037     OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
00038     MS_LANG = 0x10,  // builtin requires MS mode.
00039     ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
00040     ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
00041     ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG     // builtin requires MS mode.
00042   };
00043   
00044 namespace Builtin {
00045 enum ID {
00046   NotBuiltin  = 0,      // This is not a builtin function.
00047 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
00048 #include "clang/Basic/Builtins.def"
00049   FirstTSBuiltin
00050 };
00051 
00052 struct Info {
00053   const char *Name, *Type, *Attributes, *HeaderName;
00054   LanguageID builtin_lang;
00055 
00056   bool operator==(const Info &RHS) const {
00057     return !strcmp(Name, RHS.Name) &&
00058            !strcmp(Type, RHS.Type) &&
00059            !strcmp(Attributes, RHS.Attributes);
00060   }
00061   bool operator!=(const Info &RHS) const { return !(*this == RHS); }
00062 };
00063 
00064 /// \brief Holds information about both target-independent and
00065 /// target-specific builtins, allowing easy queries by clients.
00066 class Context {
00067   const Info *TSRecords;
00068   unsigned NumTSRecords;
00069 public:
00070   Context();
00071 
00072   /// \brief Perform target-specific initialization
00073   void InitializeTarget(const TargetInfo &Target);
00074   
00075   /// \brief Mark the identifiers for all the builtins with their
00076   /// appropriate builtin ID # and mark any non-portable builtin identifiers as
00077   /// such.
00078   void InitializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
00079 
00080   /// \brief Populate the vector with the names of all of the builtins.
00081   void GetBuiltinNames(SmallVectorImpl<const char *> &Names);
00082 
00083   /// \brief Return the identifier name for the specified builtin,
00084   /// e.g. "__builtin_abs".
00085   const char *GetName(unsigned ID) const {
00086     return GetRecord(ID).Name;
00087   }
00088 
00089   /// \brief Get the type descriptor string for the specified builtin.
00090   const char *GetTypeString(unsigned ID) const {
00091     return GetRecord(ID).Type;
00092   }
00093 
00094   /// \brief Return true if this function has no side effects and doesn't
00095   /// read memory.
00096   bool isConst(unsigned ID) const {
00097     return strchr(GetRecord(ID).Attributes, 'c') != nullptr;
00098   }
00099 
00100   /// \brief Return true if we know this builtin never throws an exception.
00101   bool isNoThrow(unsigned ID) const {
00102     return strchr(GetRecord(ID).Attributes, 'n') != nullptr;
00103   }
00104 
00105   /// \brief Return true if we know this builtin never returns.
00106   bool isNoReturn(unsigned ID) const {
00107     return strchr(GetRecord(ID).Attributes, 'r') != nullptr;
00108   }
00109 
00110   /// \brief Return true if we know this builtin can return twice.
00111   bool isReturnsTwice(unsigned ID) const {
00112     return strchr(GetRecord(ID).Attributes, 'j') != nullptr;
00113   }
00114 
00115   /// \brief Returns true if this builtin does not perform the side-effects
00116   /// of its arguments.
00117   bool isUnevaluated(unsigned ID) const {
00118     return strchr(GetRecord(ID).Attributes, 'u') != nullptr;
00119   }
00120 
00121   /// \brief Return true if this is a builtin for a libc/libm function,
00122   /// with a "__builtin_" prefix (e.g. __builtin_abs).
00123   bool isLibFunction(unsigned ID) const {
00124     return strchr(GetRecord(ID).Attributes, 'F') != nullptr;
00125   }
00126 
00127   /// \brief Determines whether this builtin is a predefined libc/libm
00128   /// function, such as "malloc", where we know the signature a
00129   /// priori.
00130   bool isPredefinedLibFunction(unsigned ID) const {
00131     return strchr(GetRecord(ID).Attributes, 'f') != nullptr;
00132   }
00133 
00134   /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
00135   /// function, such as "__clear_cache", where we know the signature a
00136   /// priori.
00137   bool isPredefinedRuntimeFunction(unsigned ID) const {
00138     return strchr(GetRecord(ID).Attributes, 'i') != nullptr;
00139   }
00140 
00141   /// \brief Determines whether this builtin has custom typechecking.
00142   bool hasCustomTypechecking(unsigned ID) const {
00143     return strchr(GetRecord(ID).Attributes, 't') != nullptr;
00144   }
00145 
00146   /// \brief Completely forget that the given ID was ever considered a builtin,
00147   /// e.g., because the user provided a conflicting signature.
00148   void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
00149   
00150   /// \brief If this is a library function that comes from a specific
00151   /// header, retrieve that header name.
00152   const char *getHeaderName(unsigned ID) const {
00153     return GetRecord(ID).HeaderName;
00154   }
00155 
00156   /// \brief Determine whether this builtin is like printf in its
00157   /// formatting rules and, if so, set the index to the format string
00158   /// argument and whether this function as a va_list argument.
00159   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
00160 
00161   /// \brief Determine whether this builtin is like scanf in its
00162   /// formatting rules and, if so, set the index to the format string
00163   /// argument and whether this function as a va_list argument.
00164   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
00165 
00166   /// \brief Return true if this function has no side effects and doesn't
00167   /// read memory, except for possibly errno.
00168   ///
00169   /// Such functions can be const when the MathErrno lang option is disabled.
00170   bool isConstWithoutErrno(unsigned ID) const {
00171     return strchr(GetRecord(ID).Attributes, 'e') != nullptr;
00172   }
00173 
00174 private:
00175   const Info &GetRecord(unsigned ID) const;
00176 
00177   /// \brief Is this builtin supported according to the given language options?
00178   bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
00179                           const LangOptions &LangOpts);
00180 
00181   /// \brief Helper function for isPrintfLike and isScanfLike.
00182   bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
00183               const char *Fmt) const;
00184 };
00185 
00186 }
00187 } // end namespace clang
00188 #endif