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