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"
|
2007-01-29 13:24:35 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2007-01-28 16:20:04 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static const Builtin::Info BuiltinInfo[] = {
|
2009-02-17 05:58:21 +08:00
|
|
|
{ "not a builtin function", 0, 0, 0, false },
|
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false },
|
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false },
|
2009-06-14 09:05:48 +08:00
|
|
|
#include "clang/Basic/Builtins.def"
|
2007-01-28 16:20:04 +08:00
|
|
|
};
|
|
|
|
|
2007-01-29 13:24:35 +08: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];
|
2007-01-28 16:20:04 +08:00
|
|
|
}
|
|
|
|
|
2009-06-17 00:18:48 +08:00
|
|
|
Builtin::Context::Context(const TargetInfo &Target) {
|
|
|
|
// Get the target specific builtins from the target.
|
2009-06-17 01:27:50 +08:00
|
|
|
TSRecords = 0;
|
|
|
|
NumTSRecords = 0;
|
2009-06-17 00:18:48 +08:00
|
|
|
Target.getTargetBuiltins(TSRecords, NumTSRecords);
|
|
|
|
}
|
|
|
|
|
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.
|
2007-01-29 13:24:35 +08:00
|
|
|
void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
|
2009-03-14 06:38:49 +08:00
|
|
|
bool NoBuiltins) {
|
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)
|
2009-02-15 04:49:29 +08:00
|
|
|
if (!BuiltinInfo[i].Suppressed &&
|
2009-03-14 06:38:49 +08:00
|
|
|
(!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
|
2009-02-15 04:49:29 +08:00
|
|
|
Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
|
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)
|
2009-02-15 04:49:29 +08:00
|
|
|
if (!TSRecords[i].Suppressed &&
|
2009-03-14 06:38:49 +08:00
|
|
|
(!NoBuiltins ||
|
2009-02-16 02:23:07 +08:00
|
|
|
(TSRecords[i].Attributes &&
|
|
|
|
!strchr(TSRecords[i].Attributes, 'f'))))
|
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
|
|
|
}
|
|
|
|
|
2009-04-23 02:49:13 +08:00
|
|
|
void
|
|
|
|
Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names,
|
|
|
|
bool NoBuiltins) {
|
|
|
|
// Final all target-independent names
|
|
|
|
for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
|
|
|
|
if (!BuiltinInfo[i].Suppressed &&
|
|
|
|
(!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
|
|
|
|
Names.push_back(BuiltinInfo[i].Name);
|
|
|
|
|
|
|
|
// Find target-specific names.
|
|
|
|
for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
|
|
|
|
if (!TSRecords[i].Suppressed &&
|
|
|
|
(!NoBuiltins ||
|
|
|
|
(TSRecords[i].Attributes &&
|
|
|
|
!strchr(TSRecords[i].Attributes, 'f'))))
|
|
|
|
Names.push_back(TSRecords[i].Name);
|
|
|
|
}
|
|
|
|
|
2009-02-14 08:32:47 +08:00
|
|
|
bool
|
|
|
|
Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
2009-02-15 00:15:20 +08:00
|
|
|
const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
|
2009-02-14 08:32:47 +08: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 14:41:13 +08:00
|
|
|
assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
|
2009-02-14 08:32:47 +08:00
|
|
|
FormatIdx = strtol(Printf, 0, 10);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|