1//===--- Builtins.cpp - Builtin function implementation -------------------===// 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// This file implements various things for builtin functions. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Basic/Builtins.h" 15#include "clang/Basic/IdentifierTable.h" 16#include "clang/Basic/LangOptions.h" 17#include "clang/Basic/TargetInfo.h" 18#include "llvm/ADT/SmallVector.h" 19#include "llvm/ADT/StringRef.h" 20using namespace clang; 21 22static const Builtin::Info BuiltinInfo[] = { 23 { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES}, 24#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES }, 25#define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) { #ID, TYPE, ATTRS, 0, BUILTIN_LANG }, 26#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\ 27 BUILTIN_LANG }, 28#include "clang/Basic/Builtins.def" 29}; 30 31const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const { 32 if (ID < Builtin::FirstTSBuiltin) 33 return BuiltinInfo[ID]; 34 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!"); 35 return TSRecords[ID - Builtin::FirstTSBuiltin]; 36} 37 38Builtin::Context::Context() { 39 // Get the target specific builtins from the target. 40 TSRecords = nullptr; 41 NumTSRecords = 0; 42} 43 44void Builtin::Context::InitializeTarget(const TargetInfo &Target) { 45 assert(NumTSRecords == 0 && "Already initialized target?"); 46 Target.getTargetBuiltins(TSRecords, NumTSRecords); 47} 48 49bool Builtin::Context::BuiltinIsSupported(const Builtin::Info &BuiltinInfo, 50 const LangOptions &LangOpts) { 51 bool BuiltinsUnsupported = LangOpts.NoBuiltin && 52 strchr(BuiltinInfo.Attributes, 'f'); 53 bool MathBuiltinsUnsupported = 54 LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName && 55 llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h"); 56 bool GnuModeUnsupported = !LangOpts.GNUMode && 57 (BuiltinInfo.builtin_lang & GNU_LANG); 58 bool MSModeUnsupported = !LangOpts.MicrosoftExt && 59 (BuiltinInfo.builtin_lang & MS_LANG); 60 bool ObjCUnsupported = !LangOpts.ObjC1 && 61 BuiltinInfo.builtin_lang == OBJC_LANG; 62 return !BuiltinsUnsupported && !MathBuiltinsUnsupported && 63 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported; 64} 65 66/// InitializeBuiltins - Mark the identifiers for all the builtins with their 67/// appropriate builtin ID # and mark any non-portable builtin identifiers as 68/// such. 69void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, 70 const LangOptions& LangOpts) { 71 // Step #1: mark all target-independent builtins with their ID's. 72 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) 73 if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) { 74 Table.get(BuiltinInfo[i].Name).setBuiltinID(i); 75 } 76 77 // Step #2: Register target-specific builtins. 78 for (unsigned i = 0, e = NumTSRecords; i != e; ++i) 79 if (BuiltinIsSupported(TSRecords[i], LangOpts)) 80 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); 81} 82 83void 84Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) { 85 // Final all target-independent names 86 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) 87 if (!strchr(BuiltinInfo[i].Attributes, 'f')) 88 Names.push_back(BuiltinInfo[i].Name); 89 90 // Find target-specific names. 91 for (unsigned i = 0, e = NumTSRecords; i != e; ++i) 92 if (!strchr(TSRecords[i].Attributes, 'f')) 93 Names.push_back(TSRecords[i].Name); 94} 95 96void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) { 97 Table.get(GetRecord(ID).Name).setBuiltinID(0); 98} 99 100bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, 101 bool &HasVAListArg, const char *Fmt) const { 102 assert(Fmt && "Not passed a format string"); 103 assert(::strlen(Fmt) == 2 && 104 "Format string needs to be two characters long"); 105 assert(::toupper(Fmt[0]) == Fmt[1] && 106 "Format string is not in the form \"xX\""); 107 108 const char *Like = ::strpbrk(GetRecord(ID).Attributes, Fmt); 109 if (!Like) 110 return false; 111 112 HasVAListArg = (*Like == Fmt[1]); 113 114 ++Like; 115 assert(*Like == ':' && "Format specifier must be followed by a ':'"); 116 ++Like; 117 118 assert(::strchr(Like, ':') && "Format specifier must end with a ':'"); 119 FormatIdx = ::strtol(Like, nullptr, 10); 120 return true; 121} 122 123bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, 124 bool &HasVAListArg) { 125 return isLike(ID, FormatIdx, HasVAListArg, "pP"); 126} 127 128bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, 129 bool &HasVAListArg) { 130 return isLike(ID, FormatIdx, HasVAListArg, "sS"); 131} 132