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];
|
|
|
|
assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
|
|
|
|
return TSRecords[ID - Builtin::FirstTSBuiltin];
|
2007-01-28 16:20:04 +08:00
|
|
|
}
|
|
|
|
|
2011-09-02 07:39:15 +08:00
|
|
|
Builtin::Context::Context() {
|
2009-06-17 00:18:48 +08:00
|
|
|
// Get the target specific builtins from the target.
|
2014-05-08 14:41:40 +08:00
|
|
|
TSRecords = nullptr;
|
2009-06-17 01:27:50 +08:00
|
|
|
NumTSRecords = 0;
|
2011-09-02 07:39:15 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
void Builtin::Context::initializeTarget(const TargetInfo &Target) {
|
2011-09-02 07:39:15 +08:00
|
|
|
assert(NumTSRecords == 0 && "Already initialized target?");
|
|
|
|
Target.getTargetBuiltins(TSRecords, NumTSRecords);
|
2009-06-17 00:18:48 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
|
|
|
|
strchr(BuiltinInfo.Attributes, 'f');
|
|
|
|
bool MathBuiltinsUnsupported =
|
|
|
|
LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
|
|
|
|
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);
|
|
|
|
bool ObjCUnsupported = !LangOpts.ObjC1 && BuiltinInfo.Langs == OBJC_LANG;
|
2013-07-23 08:13:01 +08:00
|
|
|
return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
|
2013-11-14 06:47:22 +08:00
|
|
|
!GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
|
2013-07-23 08:13:01 +08:00
|
|
|
}
|
|
|
|
|
2007-01-28 16:20:04 +08:00
|
|
|
/// InitializeBuiltins - Mark the identifiers for all the builtins with their
|
|
|
|
/// 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.
|
2007-01-29 13:24:35 +08:00
|
|
|
for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
|
2015-08-06 09:01:12 +08:00
|
|
|
if (builtinIsSupported(TSRecords[i], LangOpts))
|
2009-02-15 04:49:29 +08:00
|
|
|
Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
|
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
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|