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!");
52 ArrayRef<InfosShard> Shards = BuiltinShards;
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)
207 case Builtin::BI__builtin_fma:
208 case Builtin::BI__builtin_fmaf:
209 case Builtin::BI__builtin_fmal:
210 case Builtin::BI__builtin_fmaf16:
212 case Builtin::BIfmaf:
213 case Builtin::BIfmal: {
214 if (Trip.isGNUEnvironment() || Trip.isOSMSVCRT())
226 unsigned BuiltinID, llvm::Triple Trip, std::optional<bool> ErrnoOverwritten,
227 bool MathErrnoEnabled,
bool HasOptNoneAttr,
228 bool IsOptimizationEnabled)
const {
233 bool ErrnoOverridenToFalseWithOpt = ErrnoOverwritten.has_value() &&
234 !ErrnoOverwritten.value() &&
235 !HasOptNoneAttr && IsOptimizationEnabled;
247 bool ConstWithoutErrnoAndExceptions =
266 bool ConstWithoutErrnoOrExceptions =
267 ConstWithoutErrnoAndExceptions || ConstWithoutExceptions;
268 bool GenerateIntrinsics =
269 (ConstAlways && !HasOptNoneAttr) ||
270 (!MathErrnoEnabled &&
271 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
273 if (!GenerateIntrinsics) {
275 ConstWithoutErrnoOrExceptions && !ConstWithoutErrnoAndExceptions;
276 if (!GenerateIntrinsics)
278 ConstWithoutErrnoOrExceptions &&
279 (!MathErrnoEnabled &&
280 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
282 if (!GenerateIntrinsics)
284 ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
287 return GenerateIntrinsics;
298 for (
const auto &Shard : BuiltinShards)
299 for (
const auto &I : Shard.Infos) {
308 for (
const auto &Shard : TargetShards)
309 for (
const auto &I : Shard.Infos) {
316 for (
const auto &Shard : AuxTargetShards)
317 for (
const auto &I : Shard.Infos) {
325 bool InStdNamespace = Name.consume_front(
"std-");
326 auto NameIt = Table.
find(Name);
327 if (NameIt != Table.
end()) {
328 unsigned ID = NameIt->second->getBuiltinID();
331 NameIt->second->clearBuiltinID();
343 assert(*WidthPos ==
':' &&
344 "Vector width specifier must be followed by a ':'");
348 unsigned Width = ::strtol(WidthPos, &EndPos, 10);
349 assert(*EndPos ==
':' &&
"Vector width specific must end with a ':'");
353bool Builtin::Context::isLike(
unsigned ID,
unsigned &FormatIdx,
354 bool &HasVAListArg,
const char *Fmt)
const {
355 assert(Fmt &&
"Not passed a format string");
356 assert(::strlen(Fmt) == 2 &&
357 "Format string needs to be two characters long");
358 assert(::toupper(Fmt[0]) == Fmt[1] &&
359 "Format string is not in the form \"xX\"");
361 const char *Like = ::strpbrk(getAttributesString(
ID), Fmt);
365 HasVAListArg = (*Like == Fmt[1]);
368 assert(*Like ==
':' &&
"Format specifier must be followed by a ':'");
371 assert(::strchr(Like,
':') &&
"Format specifier must end with a ':'");
372 FormatIdx = ::strtol(Like,
nullptr, 10);
377 bool &HasVAListArg) {
378 return isLike(
ID, FormatIdx, HasVAListArg,
"pP");
382 bool &HasVAListArg) {
383 return isLike(
ID, FormatIdx, HasVAListArg,
"sS");
393 assert(*CalleePos ==
'<' &&
394 "Callback callee specifier must be followed by a '<'");
398 int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
399 assert(CalleeIdx >= 0 &&
"Callee index is supposed to be positive!");
400 Encoding.push_back(CalleeIdx);
402 while (*EndPos ==
',') {
403 const char *PayloadPos = EndPos + 1;
405 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
406 Encoding.push_back(PayloadIdx);
409 assert(*EndPos ==
'>' &&
"Callback callee specifier must end with a '>'");
415 ID == Builtin::BI__builtin_assume_aligned ||
421 StringRef RequiredFeatures,
const llvm::StringMap<bool> &TargetFetureMap) {
423 if (RequiredFeatures.empty())
425 assert(!RequiredFeatures.contains(
' ') &&
"Space in feature list");
428 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
static bool isBuiltinConstForTriple(unsigned BuiltinID, llvm::Triple Trip)
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.
bool shouldGenerateFPMathIntrinsic(unsigned BuiltinID, llvm::Triple Trip, std::optional< bool > ErrnoOverwritten, bool MathErrnoEnabled, bool HasOptNoneAttr, bool IsOptimizationEnabled) const
Determine whether we can generate LLVM intrinsics for the given builtin ID, based on whether it has s...
std::string getQuotedName(unsigned ID) const
Return the identifier name for the specified builtin inside single quotes for a diagnostic,...
bool hasReferenceArgsOrResult(unsigned ID) const
Return true if this builtin has a result or any arguments which are reference types.
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...
bool isConstWithoutErrnoAndExceptions(unsigned ID) const
Return true if this function has no side effects and doesn't read memory, except for possibly errno o...
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.
bool hasCustomTypechecking(unsigned ID) const
Determines whether this builtin has custom typechecking.
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 isInStdNamespace(unsigned ID) const
Determines whether this builtin is a C++ standard library function that lives in (possibly-versioned)...
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 isPredefinedLibFunction(unsigned ID) const
Determines whether this builtin is a predefined libc/libm function, such as "malloc",...
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
bool isConstWithoutExceptions(unsigned ID) const
void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget)
Perform target-specific initialization.
bool isConst(unsigned ID) const
Return true if this function has no side effects and doesn't read memory.
TargetFeatures - This class is used to check whether the builtin function has the required target 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