2007-01-28 16:20:04 +08:00
|
|
|
//===--- Builtins.cpp - Builtin function implementation -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-28 16:20:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements various things for builtin functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-14 09:05:48 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2007-10-07 16:58:51 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2010-12-01 01:35:24 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2013-07-23 08:13:01 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2007-01-28 16:20:04 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static const Builtin::Info BuiltinInfo[] = {
|
2015-08-07 13:14:44 +08:00
|
|
|
{ "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
|
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) \
|
|
|
|
{ #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
|
2015-08-06 05:04:28 +08:00
|
|
|
#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
|
2015-08-07 13:14:44 +08:00
|
|
|
{ #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
|
2015-08-06 05:04:28 +08:00
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
|
2015-08-07 13:14:44 +08:00
|
|
|
{ #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
|
2009-06-14 09:05:48 +08:00
|
|
|
#include "clang/Basic/Builtins.def"
|
2007-01-28 16:20:04 +08:00
|
|
|
};
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
|
2007-01-29 13:24:35 +08:00
|
|
|
if (ID < Builtin::FirstTSBuiltin)
|
|
|
|
return BuiltinInfo[ID];
|
2015-10-19 12:51:35 +08:00
|
|
|
assert(((ID - Builtin::FirstTSBuiltin) <
|
|
|
|
(TSRecords.size() + AuxTSRecords.size())) &&
|
2015-09-23 01:23:22 +08:00
|
|
|
"Invalid builtin ID!");
|
|
|
|
if (isAuxBuiltinID(ID))
|
|
|
|
return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
|
2007-01-29 13:24:35 +08:00
|
|
|
return TSRecords[ID - Builtin::FirstTSBuiltin];
|
2007-01-28 16:20:04 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:23:22 +08:00
|
|
|
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
|
|
|
|
const TargetInfo *AuxTarget) {
|
2015-10-19 12:51:35 +08:00
|
|
|
assert(TSRecords.empty() && "Already initialized target?");
|
|
|
|
TSRecords = Target.getTargetBuiltins();
|
2015-09-23 01:23:22 +08:00
|
|
|
if (AuxTarget)
|
2015-10-19 12:51:35 +08:00
|
|
|
AuxTSRecords = AuxTarget->getTargetBuiltins();
|
2009-06-17 00:18:48 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 22:35:46 +08:00
|
|
|
bool Builtin::Context::isBuiltinFunc(const char *Name) {
|
|
|
|
StringRef FuncName(Name);
|
|
|
|
for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
|
|
|
|
if (FuncName.equals(BuiltinInfo[i].Name))
|
|
|
|
return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
|
2013-07-23 08:13:01 +08:00
|
|
|
const LangOptions &LangOpts) {
|
2016-01-06 22:35:46 +08:00
|
|
|
bool BuiltinsUnsupported =
|
|
|
|
(LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
|
|
|
|
strchr(BuiltinInfo.Attributes, 'f');
|
2013-07-23 08:13:01 +08:00
|
|
|
bool MathBuiltinsUnsupported =
|
2015-09-14 22:08:18 +08:00
|
|
|
LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
|
2013-07-23 08:13:01 +08:00
|
|
|
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
|
2015-08-06 05:04:28 +08:00
|
|
|
bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
|
|
|
|
bool MSModeUnsupported =
|
|
|
|
!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
|
2018-10-31 04:31:30 +08:00
|
|
|
bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
|
2017-09-08 03:39:10 +08:00
|
|
|
bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
|
|
|
|
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
|
|
|
|
bool OclC2Unsupported = LangOpts.OpenCLVersion != 200 &&
|
|
|
|
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
|
|
|
|
bool OclCUnsupported = !LangOpts.OpenCL &&
|
|
|
|
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
|
2017-10-17 22:28:14 +08:00
|
|
|
bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
|
2016-01-26 12:03:48 +08:00
|
|
|
return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
|
2017-10-17 22:28:14 +08:00
|
|
|
!OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported &&
|
2013-11-14 06:47:22 +08:00
|
|
|
!GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
|
2013-07-23 08:13:01 +08:00
|
|
|
}
|
|
|
|
|
2015-09-15 17:53:14 +08:00
|
|
|
/// initializeBuiltins - Mark the identifiers for all the builtins with their
|
2007-01-28 16:20:04 +08:00
|
|
|
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
|
|
|
|
/// such.
|
2015-08-06 09:01:12 +08:00
|
|
|
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
|
2010-12-01 01:35:24 +08:00
|
|
|
const LangOptions& LangOpts) {
|
2007-01-28 16:20:04 +08:00
|
|
|
// Step #1: mark all target-independent builtins with their ID's.
|
2007-01-29 13:24:35 +08:00
|
|
|
for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
|
2015-08-06 09:01:12 +08:00
|
|
|
if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
|
2013-06-01 00:29:28 +08:00
|
|
|
Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
|
2013-07-23 08:13:01 +08:00
|
|
|
}
|
2009-06-14 09:54:56 +08:00
|
|
|
|
2009-04-22 12:56:28 +08:00
|
|
|
// Step #2: Register target-specific builtins.
|
2015-10-19 12:51:35 +08:00
|
|
|
for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
|
2015-08-06 09:01:12 +08:00
|
|
|
if (builtinIsSupported(TSRecords[i], LangOpts))
|
2015-09-23 01:23:22 +08:00
|
|
|
Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
|
|
|
|
|
|
|
|
// Step #3: Register target-specific builtins for AuxTarget.
|
2015-10-19 12:51:35 +08:00
|
|
|
for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
|
2015-09-23 01:23:22 +08:00
|
|
|
Table.get(AuxTSRecords[i].Name)
|
2015-10-19 12:51:35 +08:00
|
|
|
.setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
|
2007-01-28 16:20:04 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
|
|
|
|
Table.get(getRecord(ID).Name).setBuiltinID(0);
|
2010-12-22 03:47:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 03:00:16 +08:00
|
|
|
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
|
|
|
|
const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
|
|
|
|
if (!WidthPos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
++WidthPos;
|
|
|
|
assert(*WidthPos == ':' &&
|
|
|
|
"Vector width specifier must be followed by a ':'");
|
|
|
|
++WidthPos;
|
|
|
|
|
|
|
|
char *EndPos;
|
|
|
|
unsigned Width = ::strtol(WidthPos, &EndPos, 10);
|
|
|
|
assert(*EndPos == ':' && "Vector width specific must end with a ':'");
|
|
|
|
return Width;
|
|
|
|
}
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg, const char *Fmt) const {
|
|
|
|
assert(Fmt && "Not passed a format string");
|
|
|
|
assert(::strlen(Fmt) == 2 &&
|
|
|
|
"Format string needs to be two characters long");
|
|
|
|
assert(::toupper(Fmt[0]) == Fmt[1] &&
|
|
|
|
"Format string is not in the form \"xX\"");
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
|
2014-01-04 04:10:54 +08:00
|
|
|
if (!Like)
|
2009-02-14 08:32:47 +08:00
|
|
|
return false;
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
HasVAListArg = (*Like == Fmt[1]);
|
2009-02-14 08:32:47 +08:00
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
++Like;
|
|
|
|
assert(*Like == ':' && "Format specifier must be followed by a ':'");
|
|
|
|
++Like;
|
2009-02-14 08:32:47 +08:00
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
|
2014-05-08 14:41:40 +08:00
|
|
|
FormatIdx = ::strtol(Like, nullptr, 10);
|
2009-02-14 08:32:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
|
|
|
return isLike(ID, FormatIdx, HasVAListArg, "pP");
|
2010-07-16 10:11:15 +08:00
|
|
|
}
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
|
|
|
return isLike(ID, FormatIdx, HasVAListArg, "sS");
|
|
|
|
}
|
2018-04-17 05:30:08 +08:00
|
|
|
|
|
|
|
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
|
|
|
|
return ID == Builtin::NotBuiltin ||
|
|
|
|
ID == Builtin::BI__va_start ||
|
|
|
|
(!hasReferenceArgsOrResult(ID) &&
|
|
|
|
!hasCustomTypechecking(ID));
|
|
|
|
}
|