1f4a2713aSLionel Sambuc //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===// 2f4a2713aSLionel Sambuc // 3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4f4a2713aSLionel Sambuc // 5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7f4a2713aSLionel Sambuc // 8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9f4a2713aSLionel Sambuc /// 10f4a2713aSLionel Sambuc /// \file 11f4a2713aSLionel Sambuc /// \brief Defines enum values for all the target-independent builtin 12f4a2713aSLionel Sambuc /// functions. 13f4a2713aSLionel Sambuc /// 14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 15f4a2713aSLionel Sambuc 16f4a2713aSLionel Sambuc #ifndef LLVM_CLANG_BASIC_BUILTINS_H 17f4a2713aSLionel Sambuc #define LLVM_CLANG_BASIC_BUILTINS_H 18f4a2713aSLionel Sambuc 19f4a2713aSLionel Sambuc #include "clang/Basic/LLVM.h" 20f4a2713aSLionel Sambuc #include <cstring> 21f4a2713aSLionel Sambuc 22f4a2713aSLionel Sambuc // VC++ defines 'alloca' as an object-like macro, which interferes with our 23f4a2713aSLionel Sambuc // builtins. 24f4a2713aSLionel Sambuc #undef alloca 25f4a2713aSLionel Sambuc 26f4a2713aSLionel Sambuc namespace clang { 27f4a2713aSLionel Sambuc class TargetInfo; 28f4a2713aSLionel Sambuc class IdentifierTable; 29f4a2713aSLionel Sambuc class ASTContext; 30f4a2713aSLionel Sambuc class QualType; 31f4a2713aSLionel Sambuc class LangOptions; 32f4a2713aSLionel Sambuc 33f4a2713aSLionel Sambuc enum LanguageID { 34f4a2713aSLionel Sambuc GNU_LANG = 0x1, // builtin requires GNU mode. 35f4a2713aSLionel Sambuc C_LANG = 0x2, // builtin for c only. 36f4a2713aSLionel Sambuc CXX_LANG = 0x4, // builtin for cplusplus only. 37f4a2713aSLionel Sambuc OBJC_LANG = 0x8, // builtin for objective-c and objective-c++ 38f4a2713aSLionel Sambuc MS_LANG = 0x10, // builtin requires MS mode. 39f4a2713aSLionel Sambuc ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages. 40f4a2713aSLionel Sambuc ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode. 41f4a2713aSLionel Sambuc ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode. 42f4a2713aSLionel Sambuc }; 43f4a2713aSLionel Sambuc 44f4a2713aSLionel Sambuc namespace Builtin { 45f4a2713aSLionel Sambuc enum ID { 46f4a2713aSLionel Sambuc NotBuiltin = 0, // This is not a builtin function. 47f4a2713aSLionel Sambuc #define BUILTIN(ID, TYPE, ATTRS) BI##ID, 48f4a2713aSLionel Sambuc #include "clang/Basic/Builtins.def" 49f4a2713aSLionel Sambuc FirstTSBuiltin 50f4a2713aSLionel Sambuc }; 51f4a2713aSLionel Sambuc 52f4a2713aSLionel Sambuc struct Info { 53f4a2713aSLionel Sambuc const char *Name, *Type, *Attributes, *HeaderName; 54f4a2713aSLionel Sambuc LanguageID builtin_lang; 55f4a2713aSLionel Sambuc 56f4a2713aSLionel Sambuc bool operator==(const Info &RHS) const { 57f4a2713aSLionel Sambuc return !strcmp(Name, RHS.Name) && 58f4a2713aSLionel Sambuc !strcmp(Type, RHS.Type) && 59f4a2713aSLionel Sambuc !strcmp(Attributes, RHS.Attributes); 60f4a2713aSLionel Sambuc } 61f4a2713aSLionel Sambuc bool operator!=(const Info &RHS) const { return !(*this == RHS); } 62f4a2713aSLionel Sambuc }; 63f4a2713aSLionel Sambuc 64f4a2713aSLionel Sambuc /// \brief Holds information about both target-independent and 65f4a2713aSLionel Sambuc /// target-specific builtins, allowing easy queries by clients. 66f4a2713aSLionel Sambuc class Context { 67f4a2713aSLionel Sambuc const Info *TSRecords; 68f4a2713aSLionel Sambuc unsigned NumTSRecords; 69f4a2713aSLionel Sambuc public: 70f4a2713aSLionel Sambuc Context(); 71f4a2713aSLionel Sambuc 72f4a2713aSLionel Sambuc /// \brief Perform target-specific initialization 73f4a2713aSLionel Sambuc void InitializeTarget(const TargetInfo &Target); 74f4a2713aSLionel Sambuc 75f4a2713aSLionel Sambuc /// \brief Mark the identifiers for all the builtins with their 76f4a2713aSLionel Sambuc /// appropriate builtin ID # and mark any non-portable builtin identifiers as 77f4a2713aSLionel Sambuc /// such. 78f4a2713aSLionel Sambuc void InitializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts); 79f4a2713aSLionel Sambuc 80f4a2713aSLionel Sambuc /// \brief Populate the vector with the names of all of the builtins. 81f4a2713aSLionel Sambuc void GetBuiltinNames(SmallVectorImpl<const char *> &Names); 82f4a2713aSLionel Sambuc 83f4a2713aSLionel Sambuc /// \brief Return the identifier name for the specified builtin, 84f4a2713aSLionel Sambuc /// e.g. "__builtin_abs". GetName(unsigned ID)85f4a2713aSLionel Sambuc const char *GetName(unsigned ID) const { 86f4a2713aSLionel Sambuc return GetRecord(ID).Name; 87f4a2713aSLionel Sambuc } 88f4a2713aSLionel Sambuc 89f4a2713aSLionel Sambuc /// \brief Get the type descriptor string for the specified builtin. GetTypeString(unsigned ID)90f4a2713aSLionel Sambuc const char *GetTypeString(unsigned ID) const { 91f4a2713aSLionel Sambuc return GetRecord(ID).Type; 92f4a2713aSLionel Sambuc } 93f4a2713aSLionel Sambuc 94f4a2713aSLionel Sambuc /// \brief Return true if this function has no side effects and doesn't 95f4a2713aSLionel Sambuc /// read memory. isConst(unsigned ID)96f4a2713aSLionel Sambuc bool isConst(unsigned ID) const { 97*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'c') != nullptr; 98f4a2713aSLionel Sambuc } 99f4a2713aSLionel Sambuc 100f4a2713aSLionel Sambuc /// \brief Return true if we know this builtin never throws an exception. isNoThrow(unsigned ID)101f4a2713aSLionel Sambuc bool isNoThrow(unsigned ID) const { 102*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'n') != nullptr; 103f4a2713aSLionel Sambuc } 104f4a2713aSLionel Sambuc 105f4a2713aSLionel Sambuc /// \brief Return true if we know this builtin never returns. isNoReturn(unsigned ID)106f4a2713aSLionel Sambuc bool isNoReturn(unsigned ID) const { 107*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'r') != nullptr; 108f4a2713aSLionel Sambuc } 109f4a2713aSLionel Sambuc 110f4a2713aSLionel Sambuc /// \brief Return true if we know this builtin can return twice. isReturnsTwice(unsigned ID)111f4a2713aSLionel Sambuc bool isReturnsTwice(unsigned ID) const { 112*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'j') != nullptr; 113f4a2713aSLionel Sambuc } 114f4a2713aSLionel Sambuc 115f4a2713aSLionel Sambuc /// \brief Returns true if this builtin does not perform the side-effects 116f4a2713aSLionel Sambuc /// of its arguments. isUnevaluated(unsigned ID)117f4a2713aSLionel Sambuc bool isUnevaluated(unsigned ID) const { 118*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'u') != nullptr; 119f4a2713aSLionel Sambuc } 120f4a2713aSLionel Sambuc 121f4a2713aSLionel Sambuc /// \brief Return true if this is a builtin for a libc/libm function, 122f4a2713aSLionel Sambuc /// with a "__builtin_" prefix (e.g. __builtin_abs). isLibFunction(unsigned ID)123f4a2713aSLionel Sambuc bool isLibFunction(unsigned ID) const { 124*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'F') != nullptr; 125f4a2713aSLionel Sambuc } 126f4a2713aSLionel Sambuc 127f4a2713aSLionel Sambuc /// \brief Determines whether this builtin is a predefined libc/libm 128f4a2713aSLionel Sambuc /// function, such as "malloc", where we know the signature a 129f4a2713aSLionel Sambuc /// priori. isPredefinedLibFunction(unsigned ID)130f4a2713aSLionel Sambuc bool isPredefinedLibFunction(unsigned ID) const { 131*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'f') != nullptr; 132f4a2713aSLionel Sambuc } 133f4a2713aSLionel Sambuc 134f4a2713aSLionel Sambuc /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc 135f4a2713aSLionel Sambuc /// function, such as "__clear_cache", where we know the signature a 136f4a2713aSLionel Sambuc /// priori. isPredefinedRuntimeFunction(unsigned ID)137f4a2713aSLionel Sambuc bool isPredefinedRuntimeFunction(unsigned ID) const { 138*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'i') != nullptr; 139f4a2713aSLionel Sambuc } 140f4a2713aSLionel Sambuc 141f4a2713aSLionel Sambuc /// \brief Determines whether this builtin has custom typechecking. hasCustomTypechecking(unsigned ID)142f4a2713aSLionel Sambuc bool hasCustomTypechecking(unsigned ID) const { 143*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 't') != nullptr; 144f4a2713aSLionel Sambuc } 145f4a2713aSLionel Sambuc 146f4a2713aSLionel Sambuc /// \brief Completely forget that the given ID was ever considered a builtin, 147f4a2713aSLionel Sambuc /// e.g., because the user provided a conflicting signature. 148f4a2713aSLionel Sambuc void ForgetBuiltin(unsigned ID, IdentifierTable &Table); 149f4a2713aSLionel Sambuc 150f4a2713aSLionel Sambuc /// \brief If this is a library function that comes from a specific 151f4a2713aSLionel Sambuc /// header, retrieve that header name. getHeaderName(unsigned ID)152f4a2713aSLionel Sambuc const char *getHeaderName(unsigned ID) const { 153f4a2713aSLionel Sambuc return GetRecord(ID).HeaderName; 154f4a2713aSLionel Sambuc } 155f4a2713aSLionel Sambuc 156f4a2713aSLionel Sambuc /// \brief Determine whether this builtin is like printf in its 157f4a2713aSLionel Sambuc /// formatting rules and, if so, set the index to the format string 158f4a2713aSLionel Sambuc /// argument and whether this function as a va_list argument. 159f4a2713aSLionel Sambuc bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg); 160f4a2713aSLionel Sambuc 161f4a2713aSLionel Sambuc /// \brief Determine whether this builtin is like scanf in its 162f4a2713aSLionel Sambuc /// formatting rules and, if so, set the index to the format string 163f4a2713aSLionel Sambuc /// argument and whether this function as a va_list argument. 164f4a2713aSLionel Sambuc bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg); 165f4a2713aSLionel Sambuc 166f4a2713aSLionel Sambuc /// \brief Return true if this function has no side effects and doesn't 167f4a2713aSLionel Sambuc /// read memory, except for possibly errno. 168f4a2713aSLionel Sambuc /// 169f4a2713aSLionel Sambuc /// Such functions can be const when the MathErrno lang option is disabled. isConstWithoutErrno(unsigned ID)170f4a2713aSLionel Sambuc bool isConstWithoutErrno(unsigned ID) const { 171*0a6a1f1dSLionel Sambuc return strchr(GetRecord(ID).Attributes, 'e') != nullptr; 172f4a2713aSLionel Sambuc } 173f4a2713aSLionel Sambuc 174f4a2713aSLionel Sambuc private: 175f4a2713aSLionel Sambuc const Info &GetRecord(unsigned ID) const; 176f4a2713aSLionel Sambuc 177f4a2713aSLionel Sambuc /// \brief Is this builtin supported according to the given language options? 178f4a2713aSLionel Sambuc bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo, 179f4a2713aSLionel Sambuc const LangOptions &LangOpts); 180*0a6a1f1dSLionel Sambuc 181*0a6a1f1dSLionel Sambuc /// \brief Helper function for isPrintfLike and isScanfLike. 182*0a6a1f1dSLionel Sambuc bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg, 183*0a6a1f1dSLionel Sambuc const char *Fmt) const; 184f4a2713aSLionel Sambuc }; 185f4a2713aSLionel Sambuc 186f4a2713aSLionel Sambuc } 187f4a2713aSLionel Sambuc } // end namespace clang 188f4a2713aSLionel Sambuc #endif 189