1 //===--- Builtins.cpp - Builtin function implementation -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements various things for builtin functions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/Builtins.h" 14 #include "clang/Basic/IdentifierTable.h" 15 #include "clang/Basic/LangOptions.h" 16 #include "clang/Basic/TargetInfo.h" 17 #include "llvm/ADT/StringRef.h" 18 using namespace clang; 19 20 static const Builtin::Info BuiltinInfo[] = { 21 { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr}, 22 #define BUILTIN(ID, TYPE, ATTRS) \ 23 { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr }, 24 #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \ 25 { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr }, 26 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \ 27 { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr }, 28 #include "clang/Basic/Builtins.def" 29 }; 30 31 const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { 32 if (ID < Builtin::FirstTSBuiltin) 33 return BuiltinInfo[ID]; 34 assert(((ID - Builtin::FirstTSBuiltin) < 35 (TSRecords.size() + AuxTSRecords.size())) && 36 "Invalid builtin ID!"); 37 if (isAuxBuiltinID(ID)) 38 return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin]; 39 return TSRecords[ID - Builtin::FirstTSBuiltin]; 40 } 41 42 void Builtin::Context::InitializeTarget(const TargetInfo &Target, 43 const TargetInfo *AuxTarget) { 44 assert(TSRecords.empty() && "Already initialized target?"); 45 TSRecords = Target.getTargetBuiltins(); 46 if (AuxTarget) 47 AuxTSRecords = AuxTarget->getTargetBuiltins(); 48 } 49 50 bool Builtin::Context::isBuiltinFunc(const char *Name) { 51 StringRef FuncName(Name); 52 for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i) 53 if (FuncName.equals(BuiltinInfo[i].Name)) 54 return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr; 55 56 return false; 57 } 58 59 bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo, 60 const LangOptions &LangOpts) { 61 bool BuiltinsUnsupported = 62 (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) && 63 strchr(BuiltinInfo.Attributes, 'f'); 64 bool MathBuiltinsUnsupported = 65 LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName && 66 llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h"); 67 bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG); 68 bool MSModeUnsupported = 69 !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG); 70 bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG; 71 bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 && 72 (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG; 73 bool OclC2Unsupported = 74 (LangOpts.OpenCLVersion != 200 && !LangOpts.OpenCLCPlusPlus) && 75 (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG; 76 bool OclCUnsupported = !LangOpts.OpenCL && 77 (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES); 78 bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG; 79 bool CPlusPlusUnsupported = 80 !LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG; 81 return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported && 82 !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported && 83 !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported && 84 !CPlusPlusUnsupported; 85 } 86 87 /// initializeBuiltins - Mark the identifiers for all the builtins with their 88 /// appropriate builtin ID # and mark any non-portable builtin identifiers as 89 /// such. 90 void Builtin::Context::initializeBuiltins(IdentifierTable &Table, 91 const LangOptions& LangOpts) { 92 // Step #1: mark all target-independent builtins with their ID's. 93 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) 94 if (builtinIsSupported(BuiltinInfo[i], LangOpts)) { 95 Table.get(BuiltinInfo[i].Name).setBuiltinID(i); 96 } 97 98 // Step #2: Register target-specific builtins. 99 for (unsigned i = 0, e = TSRecords.size(); i != e; ++i) 100 if (builtinIsSupported(TSRecords[i], LangOpts)) 101 Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin); 102 103 // Step #3: Register target-specific builtins for AuxTarget. 104 for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) 105 Table.get(AuxTSRecords[i].Name) 106 .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); 107 } 108 109 void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) { 110 Table.get(getRecord(ID).Name).setBuiltinID(0); 111 } 112 113 unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { 114 const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V'); 115 if (!WidthPos) 116 return 0; 117 118 ++WidthPos; 119 assert(*WidthPos == ':' && 120 "Vector width specifier must be followed by a ':'"); 121 ++WidthPos; 122 123 char *EndPos; 124 unsigned Width = ::strtol(WidthPos, &EndPos, 10); 125 assert(*EndPos == ':' && "Vector width specific must end with a ':'"); 126 return Width; 127 } 128 129 bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, 130 bool &HasVAListArg, const char *Fmt) const { 131 assert(Fmt && "Not passed a format string"); 132 assert(::strlen(Fmt) == 2 && 133 "Format string needs to be two characters long"); 134 assert(::toupper(Fmt[0]) == Fmt[1] && 135 "Format string is not in the form \"xX\""); 136 137 const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt); 138 if (!Like) 139 return false; 140 141 HasVAListArg = (*Like == Fmt[1]); 142 143 ++Like; 144 assert(*Like == ':' && "Format specifier must be followed by a ':'"); 145 ++Like; 146 147 assert(::strchr(Like, ':') && "Format specifier must end with a ':'"); 148 FormatIdx = ::strtol(Like, nullptr, 10); 149 return true; 150 } 151 152 bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, 153 bool &HasVAListArg) { 154 return isLike(ID, FormatIdx, HasVAListArg, "pP"); 155 } 156 157 bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, 158 bool &HasVAListArg) { 159 return isLike(ID, FormatIdx, HasVAListArg, "sS"); 160 } 161 162 bool Builtin::Context::performsCallback(unsigned ID, 163 SmallVectorImpl<int> &Encoding) const { 164 const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C'); 165 if (!CalleePos) 166 return false; 167 168 ++CalleePos; 169 assert(*CalleePos == '<' && 170 "Callback callee specifier must be followed by a '<'"); 171 ++CalleePos; 172 173 char *EndPos; 174 int CalleeIdx = ::strtol(CalleePos, &EndPos, 10); 175 assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!"); 176 Encoding.push_back(CalleeIdx); 177 178 while (*EndPos == ',') { 179 const char *PayloadPos = EndPos + 1; 180 181 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10); 182 Encoding.push_back(PayloadIdx); 183 } 184 185 assert(*EndPos == '>' && "Callback callee specifier must end with a '>'"); 186 return true; 187 } 188 189 bool Builtin::Context::canBeRedeclared(unsigned ID) const { 190 return ID == Builtin::NotBuiltin || 191 ID == Builtin::BI__va_start || 192 (!hasReferenceArgsOrResult(ID) && 193 !hasCustomTypechecking(ID)); 194 } 195