2007-07-11 21:01:13 +04:00
|
|
|
//===--- Builtins.cpp - Builtin function implementation -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements various things for builtin functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-14 05:05:48 +04:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2007-10-07 12:58:51 +04:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2010-11-30 20:35:24 +03:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 13:13:33 +04:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2012-02-04 17:45:25 +04:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static const Builtin::Info BuiltinInfo[] = {
|
2011-07-06 01:53:01 +04:00
|
|
|
{ "not a builtin function", 0, 0, 0, ALL_LANGUAGES },
|
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
|
2010-11-30 20:35:24 +03:00
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
|
2011-07-06 01:53:01 +04:00
|
|
|
BUILTIN_LANG },
|
2009-06-14 05:05:48 +04:00
|
|
|
#include "clang/Basic/Builtins.def"
|
2007-07-11 21:01:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
|
|
|
|
if (ID < Builtin::FirstTSBuiltin)
|
|
|
|
return BuiltinInfo[ID];
|
|
|
|
assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
|
|
|
|
return TSRecords[ID - Builtin::FirstTSBuiltin];
|
|
|
|
}
|
|
|
|
|
2011-09-02 03:39:15 +04:00
|
|
|
Builtin::Context::Context() {
|
2009-06-16 20:18:48 +04:00
|
|
|
// Get the target specific builtins from the target.
|
2009-06-16 21:27:50 +04:00
|
|
|
TSRecords = 0;
|
|
|
|
NumTSRecords = 0;
|
2011-09-02 03:39:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
|
|
|
|
assert(NumTSRecords == 0 && "Already initialized target?");
|
|
|
|
Target.getTargetBuiltins(TSRecords, NumTSRecords);
|
2009-06-16 20:18:48 +04:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// InitializeBuiltins - Mark the identifiers for all the builtins with their
|
|
|
|
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
|
|
|
|
/// such.
|
|
|
|
void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
|
2010-11-30 20:35:24 +03:00
|
|
|
const LangOptions& LangOpts) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Step #1: mark all target-independent builtins with their ID's.
|
|
|
|
for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
|
2012-12-20 23:22:21 +04:00
|
|
|
if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) {
|
2010-11-30 20:35:24 +03:00
|
|
|
if (LangOpts.ObjC1 ||
|
|
|
|
BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
|
|
|
|
Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
|
|
|
|
}
|
2009-06-14 05:54:56 +04:00
|
|
|
|
2009-04-22 08:56:28 +04:00
|
|
|
// Step #2: Register target-specific builtins.
|
2007-07-11 21:01:13 +04:00
|
|
|
for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
|
2012-12-20 23:22:21 +04:00
|
|
|
if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
|
2009-02-14 23:49:29 +03:00
|
|
|
Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
void
|
2011-07-23 14:55:15 +04:00
|
|
|
Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
|
2009-04-22 22:49:13 +04:00
|
|
|
bool NoBuiltins) {
|
|
|
|
// Final all target-independent names
|
|
|
|
for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
|
2012-12-20 23:22:21 +04:00
|
|
|
if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))
|
2009-04-22 22:49:13 +04:00
|
|
|
Names.push_back(BuiltinInfo[i].Name);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-22 22:49:13 +04:00
|
|
|
// Find target-specific names.
|
|
|
|
for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
|
2012-12-20 23:22:21 +04:00
|
|
|
if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f'))
|
2009-04-22 22:49:13 +04:00
|
|
|
Names.push_back(TSRecords[i].Name);
|
|
|
|
}
|
|
|
|
|
2010-12-21 22:47:46 +03:00
|
|
|
void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
|
|
|
|
Table.get(GetRecord(ID).Name).setBuiltinID(0);
|
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
bool
|
|
|
|
Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
|
2009-02-14 03:32:47 +03:00
|
|
|
bool &HasVAListArg) {
|
2012-12-20 23:22:21 +04:00
|
|
|
const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
|
2009-02-14 03:32:47 +03:00
|
|
|
if (!Printf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
HasVAListArg = (*Printf == 'P');
|
|
|
|
|
|
|
|
++Printf;
|
|
|
|
assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
|
|
|
|
++Printf;
|
|
|
|
|
2009-02-19 09:41:13 +03:00
|
|
|
assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
|
2009-02-14 03:32:47 +03:00
|
|
|
FormatIdx = strtol(Printf, 0, 10);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-16 06:11:15 +04:00
|
|
|
// FIXME: Refactor with isPrintfLike.
|
|
|
|
bool
|
|
|
|
Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
2012-12-20 23:22:21 +04:00
|
|
|
const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
|
2010-07-16 06:11:15 +04:00
|
|
|
if (!Scanf)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
HasVAListArg = (*Scanf == 'S');
|
|
|
|
|
|
|
|
++Scanf;
|
|
|
|
assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
|
|
|
|
++Scanf;
|
|
|
|
|
|
|
|
assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
|
|
|
|
FormatIdx = strtol(Scanf, 0, 10);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|