1194179Sed//===--- Builtins.h - Builtin function header -------------------*- C++ -*-===// 2194179Sed// 3194179Sed// The LLVM Compiler Infrastructure 4194179Sed// 5194179Sed// This file is distributed under the University of Illinois Open Source 6194179Sed// License. See LICENSE.TXT for details. 7194179Sed// 8194179Sed//===----------------------------------------------------------------------===// 9245431Sdim/// 10245431Sdim/// \file 11245431Sdim/// \brief Defines enum values for all the target-independent builtin 12245431Sdim/// functions. 13245431Sdim/// 14194179Sed//===----------------------------------------------------------------------===// 15194179Sed 16194179Sed#ifndef LLVM_CLANG_BASIC_BUILTINS_H 17194179Sed#define LLVM_CLANG_BASIC_BUILTINS_H 18194179Sed 19226890Sdim#include "clang/Basic/LLVM.h" 20194179Sed#include <cstring> 21194179Sed 22198092Srdivacky// VC++ defines 'alloca' as an object-like macro, which interferes with our 23198092Srdivacky// builtins. 24198092Srdivacky#undef alloca 25198092Srdivacky 26194179Sednamespace clang { 27194179Sed class TargetInfo; 28194179Sed class IdentifierTable; 29194179Sed class ASTContext; 30194179Sed class QualType; 31218893Sdim class LangOptions; 32218893Sdim 33218893Sdim enum LanguageID { 34263509Sdim GNU_LANG = 0x1, // builtin requires GNU mode. 35263509Sdim C_LANG = 0x2, // builtin for c only. 36263509Sdim CXX_LANG = 0x4, // builtin for cplusplus only. 37263509Sdim OBJC_LANG = 0x8, // builtin for objective-c and objective-c++ 38263509Sdim MS_LANG = 0x10, // builtin requires MS mode. 39263509Sdim ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages. 40263509Sdim ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode. 41263509Sdim ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode. 42218893Sdim }; 43218893Sdim 44194179Sednamespace Builtin { 45194179Sedenum ID { 46194179Sed NotBuiltin = 0, // This is not a builtin function. 47194179Sed#define BUILTIN(ID, TYPE, ATTRS) BI##ID, 48194179Sed#include "clang/Basic/Builtins.def" 49194179Sed FirstTSBuiltin 50194179Sed}; 51194179Sed 52194179Sedstruct Info { 53194179Sed const char *Name, *Type, *Attributes, *HeaderName; 54218893Sdim LanguageID builtin_lang; 55194179Sed 56194179Sed bool operator==(const Info &RHS) const { 57194179Sed return !strcmp(Name, RHS.Name) && 58194179Sed !strcmp(Type, RHS.Type) && 59194179Sed !strcmp(Attributes, RHS.Attributes); 60194179Sed } 61194179Sed bool operator!=(const Info &RHS) const { return !(*this == RHS); } 62194179Sed}; 63194179Sed 64245431Sdim/// \brief Holds information about both target-independent and 65194179Sed/// target-specific builtins, allowing easy queries by clients. 66194179Sedclass Context { 67194179Sed const Info *TSRecords; 68194179Sed unsigned NumTSRecords; 69194179Sedpublic: 70226890Sdim Context(); 71194179Sed 72226890Sdim /// \brief Perform target-specific initialization 73226890Sdim void InitializeTarget(const TargetInfo &Target); 74226890Sdim 75245431Sdim /// \brief Mark the identifiers for all the builtins with their 76194179Sed /// appropriate builtin ID # and mark any non-portable builtin identifiers as 77194179Sed /// such. 78218893Sdim void InitializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts); 79194179Sed 80263509Sdim /// \brief Populate the vector with the names of all of the builtins. 81263509Sdim void GetBuiltinNames(SmallVectorImpl<const char *> &Names); 82198092Srdivacky 83245431Sdim /// \brief Return the identifier name for the specified builtin, 84194179Sed /// e.g. "__builtin_abs". 85194179Sed const char *GetName(unsigned ID) const { 86194179Sed return GetRecord(ID).Name; 87194179Sed } 88198092Srdivacky 89245431Sdim /// \brief Get the type descriptor string for the specified builtin. 90194179Sed const char *GetTypeString(unsigned ID) const { 91194179Sed return GetRecord(ID).Type; 92194179Sed } 93198092Srdivacky 94245431Sdim /// \brief Return true if this function has no side effects and doesn't 95194179Sed /// read memory. 96194179Sed bool isConst(unsigned ID) const { 97194179Sed return strchr(GetRecord(ID).Attributes, 'c') != 0; 98194179Sed } 99198092Srdivacky 100245431Sdim /// \brief Return true if we know this builtin never throws an exception. 101194179Sed bool isNoThrow(unsigned ID) const { 102194179Sed return strchr(GetRecord(ID).Attributes, 'n') != 0; 103194179Sed } 104198092Srdivacky 105245431Sdim /// \brief Return true if we know this builtin never returns. 106198092Srdivacky bool isNoReturn(unsigned ID) const { 107198092Srdivacky return strchr(GetRecord(ID).Attributes, 'r') != 0; 108198092Srdivacky } 109198092Srdivacky 110245431Sdim /// \brief Return true if we know this builtin can return twice. 111226890Sdim bool isReturnsTwice(unsigned ID) const { 112226890Sdim return strchr(GetRecord(ID).Attributes, 'j') != 0; 113226890Sdim } 114226890Sdim 115252723Sdim /// \brief Returns true if this builtin does not perform the side-effects 116252723Sdim /// of its arguments. 117252723Sdim bool isUnevaluated(unsigned ID) const { 118252723Sdim return strchr(GetRecord(ID).Attributes, 'u') != 0; 119252723Sdim } 120252723Sdim 121245431Sdim /// \brief Return true if this is a builtin for a libc/libm function, 122194179Sed /// with a "__builtin_" prefix (e.g. __builtin_abs). 123194179Sed bool isLibFunction(unsigned ID) const { 124194179Sed return strchr(GetRecord(ID).Attributes, 'F') != 0; 125194179Sed } 126198092Srdivacky 127194179Sed /// \brief Determines whether this builtin is a predefined libc/libm 128194179Sed /// function, such as "malloc", where we know the signature a 129194179Sed /// priori. 130194179Sed bool isPredefinedLibFunction(unsigned ID) const { 131194179Sed return strchr(GetRecord(ID).Attributes, 'f') != 0; 132194179Sed } 133194179Sed 134252723Sdim /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc 135252723Sdim /// function, such as "__clear_cache", where we know the signature a 136252723Sdim /// priori. 137252723Sdim bool isPredefinedRuntimeFunction(unsigned ID) const { 138252723Sdim return strchr(GetRecord(ID).Attributes, 'i') != 0; 139252723Sdim } 140252723Sdim 141219077Sdim /// \brief Determines whether this builtin has custom typechecking. 142219077Sdim bool hasCustomTypechecking(unsigned ID) const { 143219077Sdim return strchr(GetRecord(ID).Attributes, 't') != 0; 144219077Sdim } 145219077Sdim 146218893Sdim /// \brief Completely forget that the given ID was ever considered a builtin, 147218893Sdim /// e.g., because the user provided a conflicting signature. 148218893Sdim void ForgetBuiltin(unsigned ID, IdentifierTable &Table); 149218893Sdim 150194179Sed /// \brief If this is a library function that comes from a specific 151194179Sed /// header, retrieve that header name. 152194179Sed const char *getHeaderName(unsigned ID) const { 153194179Sed return GetRecord(ID).HeaderName; 154194179Sed } 155194179Sed 156194179Sed /// \brief Determine whether this builtin is like printf in its 157194179Sed /// formatting rules and, if so, set the index to the format string 158194179Sed /// argument and whether this function as a va_list argument. 159194179Sed bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg); 160194179Sed 161212904Sdim /// \brief Determine whether this builtin is like scanf in its 162212904Sdim /// formatting rules and, if so, set the index to the format string 163212904Sdim /// argument and whether this function as a va_list argument. 164212904Sdim bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg); 165212904Sdim 166245431Sdim /// \brief Return true if this function has no side effects and doesn't 167245431Sdim /// read memory, except for possibly errno. 168245431Sdim /// 169245431Sdim /// Such functions can be const when the MathErrno lang option is disabled. 170194179Sed bool isConstWithoutErrno(unsigned ID) const { 171194179Sed return strchr(GetRecord(ID).Attributes, 'e') != 0; 172194179Sed } 173194179Sed 174194179Sedprivate: 175194179Sed const Info &GetRecord(unsigned ID) const; 176263509Sdim 177263509Sdim /// \brief Is this builtin supported according to the given language options? 178263509Sdim bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo, 179263509Sdim const LangOptions &LangOpts); 180194179Sed}; 181194179Sed 182194179Sed} 183194179Sed} // end namespace clang 184194179Sed#endif 185