18#include "llvm/ADT/StringRef.h"
23#define HEADER(ID, NAME) \
26#include "clang/Basic/BuiltinHeaders.def"
29 llvm_unreachable(
"Unknown HeaderDesc::HeaderID enum");
34#define GET_BUILTIN_STR_TABLE
35#include "clang/Basic/Builtins.inc"
36#undef GET_BUILTIN_STR_TABLE
40#define GET_BUILTIN_INFOS
41#include "clang/Basic/Builtins.inc"
42#undef GET_BUILTIN_INFOS
46std::pair<const Builtin::InfosShard &, const Builtin::Info &>
47Builtin::Context::getShardAndInfo(
unsigned ID)
const {
49 NumAuxTargetBuiltins)) &&
50 "Invalid builtin ID!");
54 Shards = AuxTargetShards;
57 Shards = TargetShards;
64 for (
const auto &Shard : Shards) {
65 if (
ID < Shard.Infos.size()) {
66 return {Shard, Shard.Infos[
ID]};
69 ID -= Shard.Infos.size();
71 llvm_unreachable(
"Invalid target builtin shard structure!");
81 const auto &[Shard, I] = getShardAndInfo(
ID);
82 return I.getName(Shard);
86 const auto &[Shard, I] = getShardAndInfo(
ID);
87 return (Twine(
"'") + Shard.NamePrefix + (*Shard.Strings)[I.Offsets.Name] +
93 const auto &[Shard, I] = getShardAndInfo(
ID);
94 return (*Shard.Strings)[I.Offsets.Type].data();
98 const auto &[Shard, I] = getShardAndInfo(
ID);
99 return (*Shard.Strings)[I.Offsets.Attributes].data();
103 const auto &[Shard, I] = getShardAndInfo(
ID);
104 return (*Shard.Strings)[I.Offsets.Features].data();
111 assert(TargetShards.empty() &&
"Already initialized target?");
112 assert(NumTargetBuiltins == 0 &&
"Already initialized target?");
113 TargetShards =
Target.getTargetBuiltins();
114 for (
const auto &Shard : TargetShards)
115 NumTargetBuiltins += Shard.Infos.size();
118 for (
const auto &Shard : AuxTargetShards)
119 NumAuxTargetBuiltins += Shard.Infos.size();
124 bool InStdNamespace = FuncName.consume_front(
"std-");
126 if (llvm::StringRef FuncNameSuffix = FuncName;
127 FuncNameSuffix.consume_front(Shard.NamePrefix))
128 for (
const auto &I : Shard.Infos)
129 if (FuncNameSuffix == (*Shard.Strings)[I.Offsets.Name] &&
130 (
bool)strchr((*Shard.Strings)[I.Offsets.Attributes].data(),
'z') ==
132 return strchr((*Shard.Strings)[I.Offsets.Attributes].data(),
'f') !=
145 if (LangOpts.NoBuiltin && strchr(AttributesStr.data(),
'f') !=
nullptr)
151 if (LangOpts.NoMathBuiltin && BuiltinInfo.
Header.
ID == HeaderDesc::MATH_H)
157 if (!LangOpts.MicrosoftExt && (BuiltinInfo.
Langs &
MS_LANG))
169 if (!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.
Langs &
OCL_GAS))
192 if (!LangOpts.CPlusPlus20 && strchr(AttributesStr.data(),
'G') !=
nullptr)
208 for (
const auto &Shard : BuiltinShards)
209 for (
const auto &I : Shard.Infos) {
218 for (
const auto &Shard : TargetShards)
219 for (
const auto &I : Shard.Infos) {
226 for (
const auto &Shard : AuxTargetShards)
227 for (
const auto &I : Shard.Infos) {
235 bool InStdNamespace = Name.consume_front(
"std-");
236 auto NameIt = Table.
find(Name);
237 if (NameIt != Table.
end()) {
238 unsigned ID = NameIt->second->getBuiltinID();
240 isInStdNamespace(
ID) == InStdNamespace) {
241 NameIt->second->clearBuiltinID();
248 const char *WidthPos = ::strchr(getAttributesString(
ID),
'V');
253 assert(*WidthPos ==
':' &&
254 "Vector width specifier must be followed by a ':'");
258 unsigned Width = ::strtol(WidthPos, &EndPos, 10);
259 assert(*EndPos ==
':' &&
"Vector width specific must end with a ':'");
263bool Builtin::Context::isLike(
unsigned ID,
unsigned &FormatIdx,
264 bool &HasVAListArg,
const char *Fmt)
const {
265 assert(Fmt &&
"Not passed a format string");
266 assert(::strlen(Fmt) == 2 &&
267 "Format string needs to be two characters long");
268 assert(::toupper(Fmt[0]) == Fmt[1] &&
269 "Format string is not in the form \"xX\"");
271 const char *Like = ::strpbrk(getAttributesString(
ID), Fmt);
275 HasVAListArg = (*Like == Fmt[1]);
278 assert(*Like ==
':' &&
"Format specifier must be followed by a ':'");
281 assert(::strchr(Like,
':') &&
"Format specifier must end with a ':'");
282 FormatIdx = ::strtol(Like,
nullptr, 10);
287 bool &HasVAListArg) {
288 return isLike(
ID, FormatIdx, HasVAListArg,
"pP");
292 bool &HasVAListArg) {
293 return isLike(
ID, FormatIdx, HasVAListArg,
"sS");
298 const char *CalleePos = ::strchr(getAttributesString(
ID),
'C');
303 assert(*CalleePos ==
'<' &&
304 "Callback callee specifier must be followed by a '<'");
308 int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
309 assert(CalleeIdx >= 0 &&
"Callee index is supposed to be positive!");
310 Encoding.push_back(CalleeIdx);
312 while (*EndPos ==
',') {
313 const char *PayloadPos = EndPos + 1;
315 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
316 Encoding.push_back(PayloadIdx);
319 assert(*EndPos ==
'>' &&
"Callback callee specifier must end with a '>'");
325 ID == Builtin::BI__builtin_assume_aligned ||
326 (!hasReferenceArgsOrResult(
ID) && !hasCustomTypechecking(
ID)) ||
327 isInStdNamespace(
ID);
331 StringRef RequiredFeatures,
const llvm::StringMap<bool> &TargetFetureMap) {
333 if (RequiredFeatures.empty())
335 assert(!RequiredFeatures.contains(
' ') &&
"Space in feature list");
338 return TF.hasRequiredFeatures(RequiredFeatures);
static constexpr llvm::StringTable BuiltinStrings
static constexpr Builtin::Info BuiltinInfos[]
static bool builtinIsSupported(const llvm::StringTable &Strings, const Builtin::Info &BuiltinInfo, const LangOptions &LangOpts)
Is this builtin supported according to the given language options?
static constexpr unsigned NumBuiltins
Defines enum values for all the target-independent builtin functions.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines the clang::LangOptions interface.
llvm::MachO::Target Target
std::string getQuotedName(unsigned ID) const
Return the identifier name for the specified builtin inside single quotes for a diagnostic,...
bool performsCallback(unsigned ID, llvm::SmallVectorImpl< int > &Encoding) const
Determine whether this builtin has callback behavior (see llvm::AbstractCallSites for details).
bool isAuxBuiltinID(unsigned ID) const
Return true if the builtin ID belongs exclusively to the AuxTarget, and false if it belongs to both p...
static bool isBuiltinFunc(llvm::StringRef Name)
Returns true if this is a libc/libm function without the '__builtin_' prefix.
unsigned getRequiredVectorWidth(unsigned ID) const
unsigned getAuxBuiltinID(unsigned ID) const
Return real builtin ID (i.e.
std::string getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
void initializeBuiltins(IdentifierTable &Table, const LangOptions &LangOpts)
Mark the identifiers for all the builtins with their appropriate builtin ID # and mark any non-portab...
bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like scanf in its formatting rules and, if so, set the index to the...
bool canBeRedeclared(unsigned ID) const
Returns true if this is a builtin that can be redeclared.
const char * getAttributesString(unsigned ID) const
Get the attributes descriptor string for the specified builtin.
bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like printf in its formatting rules and, if so, set the index to th...
const char * getTypeString(unsigned ID) const
Get the type descriptor string for the specified builtin.
const char * getRequiredFeatures(unsigned ID) const
void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget)
Perform target-specific initialization.
TargetFeatures - This class is used to check whether the builtin function has the required tagert spe...
void setBuiltinID(unsigned ID)
Implements an efficient mapping from strings to IdentifierInfo nodes.
iterator find(StringRef Name) const
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::vector< std::string > NoBuiltinFuncs
A list of all -fno-builtin-* function names (e.g., memset).
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
Exposes information about the current target.
virtual llvm::SmallVector< Builtin::InfosShard > getTargetBuiltins() const =0
Return information about target-specific builtins for the current primary target, and info about whic...
Defines the clang::TargetInfo interface.
bool evaluateRequiredTargetFeatures(llvm::StringRef RequiredFatures, const llvm::StringMap< bool > &TargetFetureMap)
Returns true if the required target features of a builtin function are enabled.
The JSON file list parser is used to communicate input to InstallAPI.
llvm::StringTable::Offset Attributes
The info used to represent each builtin.
struct clang::Builtin::Info::StrOffsets Offsets
std::string getName(const InfosShard &Shard) const
Get the name for the builtin represented by this Info object.
A shard of a target's builtins string table and info.
const llvm::StringTable * Strings
llvm::StringLiteral NamePrefix