17330f729Sjoerg //===--- Builtins.cpp - Builtin function implementation -------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file implements various things for builtin functions.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/Basic/Builtins.h"
147330f729Sjoerg #include "clang/Basic/IdentifierTable.h"
157330f729Sjoerg #include "clang/Basic/LangOptions.h"
167330f729Sjoerg #include "clang/Basic/TargetInfo.h"
177330f729Sjoerg #include "llvm/ADT/StringRef.h"
187330f729Sjoerg using namespace clang;
197330f729Sjoerg
207330f729Sjoerg static const Builtin::Info BuiltinInfo[] = {
217330f729Sjoerg { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
227330f729Sjoerg #define BUILTIN(ID, TYPE, ATTRS) \
237330f729Sjoerg { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
247330f729Sjoerg #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
257330f729Sjoerg { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
267330f729Sjoerg #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
277330f729Sjoerg { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
287330f729Sjoerg #include "clang/Basic/Builtins.def"
297330f729Sjoerg };
307330f729Sjoerg
getRecord(unsigned ID) const317330f729Sjoerg const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
327330f729Sjoerg if (ID < Builtin::FirstTSBuiltin)
337330f729Sjoerg return BuiltinInfo[ID];
347330f729Sjoerg assert(((ID - Builtin::FirstTSBuiltin) <
357330f729Sjoerg (TSRecords.size() + AuxTSRecords.size())) &&
367330f729Sjoerg "Invalid builtin ID!");
377330f729Sjoerg if (isAuxBuiltinID(ID))
387330f729Sjoerg return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
397330f729Sjoerg return TSRecords[ID - Builtin::FirstTSBuiltin];
407330f729Sjoerg }
417330f729Sjoerg
InitializeTarget(const TargetInfo & Target,const TargetInfo * AuxTarget)427330f729Sjoerg void Builtin::Context::InitializeTarget(const TargetInfo &Target,
437330f729Sjoerg const TargetInfo *AuxTarget) {
447330f729Sjoerg assert(TSRecords.empty() && "Already initialized target?");
457330f729Sjoerg TSRecords = Target.getTargetBuiltins();
467330f729Sjoerg if (AuxTarget)
477330f729Sjoerg AuxTSRecords = AuxTarget->getTargetBuiltins();
487330f729Sjoerg }
497330f729Sjoerg
isBuiltinFunc(llvm::StringRef FuncName)50*e038c9c4Sjoerg bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
517330f729Sjoerg for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
527330f729Sjoerg if (FuncName.equals(BuiltinInfo[i].Name))
537330f729Sjoerg return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
547330f729Sjoerg
557330f729Sjoerg return false;
567330f729Sjoerg }
577330f729Sjoerg
builtinIsSupported(const Builtin::Info & BuiltinInfo,const LangOptions & LangOpts)587330f729Sjoerg bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
597330f729Sjoerg const LangOptions &LangOpts) {
607330f729Sjoerg bool BuiltinsUnsupported =
617330f729Sjoerg (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
627330f729Sjoerg strchr(BuiltinInfo.Attributes, 'f');
637330f729Sjoerg bool MathBuiltinsUnsupported =
647330f729Sjoerg LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
657330f729Sjoerg llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
667330f729Sjoerg bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
677330f729Sjoerg bool MSModeUnsupported =
687330f729Sjoerg !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
697330f729Sjoerg bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
707330f729Sjoerg bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
717330f729Sjoerg (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
727330f729Sjoerg bool OclC2Unsupported =
737330f729Sjoerg (LangOpts.OpenCLVersion != 200 && !LangOpts.OpenCLCPlusPlus) &&
747330f729Sjoerg (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
757330f729Sjoerg bool OclCUnsupported = !LangOpts.OpenCL &&
767330f729Sjoerg (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
777330f729Sjoerg bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
78*e038c9c4Sjoerg bool CUDAUnsupported = !LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG;
797330f729Sjoerg bool CPlusPlusUnsupported =
807330f729Sjoerg !LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
817330f729Sjoerg return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
827330f729Sjoerg !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported &&
837330f729Sjoerg !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
84*e038c9c4Sjoerg !CPlusPlusUnsupported && !CUDAUnsupported;
857330f729Sjoerg }
867330f729Sjoerg
877330f729Sjoerg /// initializeBuiltins - Mark the identifiers for all the builtins with their
887330f729Sjoerg /// appropriate builtin ID # and mark any non-portable builtin identifiers as
897330f729Sjoerg /// such.
initializeBuiltins(IdentifierTable & Table,const LangOptions & LangOpts)907330f729Sjoerg void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
917330f729Sjoerg const LangOptions& LangOpts) {
927330f729Sjoerg // Step #1: mark all target-independent builtins with their ID's.
937330f729Sjoerg for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
947330f729Sjoerg if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
957330f729Sjoerg Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
967330f729Sjoerg }
977330f729Sjoerg
987330f729Sjoerg // Step #2: Register target-specific builtins.
997330f729Sjoerg for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
1007330f729Sjoerg if (builtinIsSupported(TSRecords[i], LangOpts))
1017330f729Sjoerg Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
1027330f729Sjoerg
1037330f729Sjoerg // Step #3: Register target-specific builtins for AuxTarget.
1047330f729Sjoerg for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
1057330f729Sjoerg Table.get(AuxTSRecords[i].Name)
1067330f729Sjoerg .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
1077330f729Sjoerg }
1087330f729Sjoerg
getRequiredVectorWidth(unsigned ID) const1097330f729Sjoerg unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
1107330f729Sjoerg const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
1117330f729Sjoerg if (!WidthPos)
1127330f729Sjoerg return 0;
1137330f729Sjoerg
1147330f729Sjoerg ++WidthPos;
1157330f729Sjoerg assert(*WidthPos == ':' &&
1167330f729Sjoerg "Vector width specifier must be followed by a ':'");
1177330f729Sjoerg ++WidthPos;
1187330f729Sjoerg
1197330f729Sjoerg char *EndPos;
1207330f729Sjoerg unsigned Width = ::strtol(WidthPos, &EndPos, 10);
1217330f729Sjoerg assert(*EndPos == ':' && "Vector width specific must end with a ':'");
1227330f729Sjoerg return Width;
1237330f729Sjoerg }
1247330f729Sjoerg
isLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg,const char * Fmt) const1257330f729Sjoerg bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
1267330f729Sjoerg bool &HasVAListArg, const char *Fmt) const {
1277330f729Sjoerg assert(Fmt && "Not passed a format string");
1287330f729Sjoerg assert(::strlen(Fmt) == 2 &&
1297330f729Sjoerg "Format string needs to be two characters long");
1307330f729Sjoerg assert(::toupper(Fmt[0]) == Fmt[1] &&
1317330f729Sjoerg "Format string is not in the form \"xX\"");
1327330f729Sjoerg
1337330f729Sjoerg const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
1347330f729Sjoerg if (!Like)
1357330f729Sjoerg return false;
1367330f729Sjoerg
1377330f729Sjoerg HasVAListArg = (*Like == Fmt[1]);
1387330f729Sjoerg
1397330f729Sjoerg ++Like;
1407330f729Sjoerg assert(*Like == ':' && "Format specifier must be followed by a ':'");
1417330f729Sjoerg ++Like;
1427330f729Sjoerg
1437330f729Sjoerg assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
1447330f729Sjoerg FormatIdx = ::strtol(Like, nullptr, 10);
1457330f729Sjoerg return true;
1467330f729Sjoerg }
1477330f729Sjoerg
isPrintfLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg)1487330f729Sjoerg bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
1497330f729Sjoerg bool &HasVAListArg) {
1507330f729Sjoerg return isLike(ID, FormatIdx, HasVAListArg, "pP");
1517330f729Sjoerg }
1527330f729Sjoerg
isScanfLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg)1537330f729Sjoerg bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
1547330f729Sjoerg bool &HasVAListArg) {
1557330f729Sjoerg return isLike(ID, FormatIdx, HasVAListArg, "sS");
1567330f729Sjoerg }
1577330f729Sjoerg
performsCallback(unsigned ID,SmallVectorImpl<int> & Encoding) const1587330f729Sjoerg bool Builtin::Context::performsCallback(unsigned ID,
1597330f729Sjoerg SmallVectorImpl<int> &Encoding) const {
1607330f729Sjoerg const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
1617330f729Sjoerg if (!CalleePos)
1627330f729Sjoerg return false;
1637330f729Sjoerg
1647330f729Sjoerg ++CalleePos;
1657330f729Sjoerg assert(*CalleePos == '<' &&
1667330f729Sjoerg "Callback callee specifier must be followed by a '<'");
1677330f729Sjoerg ++CalleePos;
1687330f729Sjoerg
1697330f729Sjoerg char *EndPos;
1707330f729Sjoerg int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
1717330f729Sjoerg assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
1727330f729Sjoerg Encoding.push_back(CalleeIdx);
1737330f729Sjoerg
1747330f729Sjoerg while (*EndPos == ',') {
1757330f729Sjoerg const char *PayloadPos = EndPos + 1;
1767330f729Sjoerg
1777330f729Sjoerg int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
1787330f729Sjoerg Encoding.push_back(PayloadIdx);
1797330f729Sjoerg }
1807330f729Sjoerg
1817330f729Sjoerg assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
1827330f729Sjoerg return true;
1837330f729Sjoerg }
1847330f729Sjoerg
canBeRedeclared(unsigned ID) const1857330f729Sjoerg bool Builtin::Context::canBeRedeclared(unsigned ID) const {
1867330f729Sjoerg return ID == Builtin::NotBuiltin ||
1877330f729Sjoerg ID == Builtin::BI__va_start ||
1887330f729Sjoerg (!hasReferenceArgsOrResult(ID) &&
1897330f729Sjoerg !hasCustomTypechecking(ID));
1907330f729Sjoerg }
191