18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringRef.h"
24#define HEADER(ID, NAME) \
27#include "clang/Basic/BuiltinHeaders.def"
30 llvm_unreachable(
"Unknown HeaderDesc::HeaderID enum");
35#define GET_BUILTIN_STR_TABLE
36#include "clang/Basic/Builtins.inc"
37#undef GET_BUILTIN_STR_TABLE
41#define GET_BUILTIN_INFOS
42#include "clang/Basic/Builtins.inc"
43#undef GET_BUILTIN_INFOS
47std::pair<const Builtin::InfosShard &, const Builtin::Info &>
48Builtin::Context::getShardAndInfo(
unsigned ID)
const {
50 NumAuxTargetBuiltins)) &&
51 "Invalid builtin ID!");
53 ArrayRef<InfosShard> Shards = BuiltinShards;
55 Shards = AuxTargetShards;
58 Shards = TargetShards;
65 for (
const auto &Shard : Shards) {
66 if (
ID < Shard.Infos.size()) {
67 return {Shard, Shard.Infos[
ID]};
70 ID -= Shard.Infos.size();
72 llvm_unreachable(
"Invalid target builtin shard structure!");
83 Buf.append(Name.begin(), Name.end());
84 return StringRef(Buf.data(), Buf.size());
95 const auto &[Shard, I] = getShardAndInfo(
ID);
96 return I.getName(Shard);
100 const auto &[Shard, I] = getShardAndInfo(
ID);
101 return (Twine(
"'") + Shard.NamePrefix + (*Shard.Strings)[I.Offsets.Name] +
107 const auto &[Shard, I] = getShardAndInfo(
ID);
108 return (*Shard.Strings)[I.Offsets.Type].data();
112 const auto &[Shard, I] = getShardAndInfo(
ID);
113 return (*Shard.Strings)[I.Offsets.Attributes].data();
117 const auto &[Shard, I] = getShardAndInfo(
ID);
118 return (*Shard.Strings)[I.Offsets.Features].data();
125 assert(TargetShards.empty() &&
"Already initialized target?");
126 assert(NumTargetBuiltins == 0 &&
"Already initialized target?");
127 TargetShards =
Target.getTargetBuiltins();
128 for (
const auto &Shard : TargetShards)
129 NumTargetBuiltins += Shard.Infos.size();
132 for (
const auto &Shard : AuxTargetShards)
133 NumAuxTargetBuiltins += Shard.Infos.size();
138 bool InStdNamespace = FuncName.consume_front(
"std-");
140 if (llvm::StringRef FuncNameSuffix = FuncName;
141 FuncNameSuffix.consume_front(Shard.NamePrefix))
142 for (
const auto &I : Shard.Infos)
143 if (FuncNameSuffix == (*Shard.Strings)[I.Offsets.Name] &&
144 (
bool)strchr((*Shard.Strings)[I.Offsets.Attributes].data(),
'z') ==
146 return strchr((*Shard.Strings)[I.Offsets.Attributes].data(),
'f') !=
159 if (LangOpts.NoBuiltin && strchr(AttributesStr.data(),
'f') !=
nullptr)
165 if (LangOpts.NoMathBuiltin && BuiltinInfo.
Header.
ID == HeaderDesc::MATH_H)
171 if (!LangOpts.MicrosoftExt && (BuiltinInfo.
Langs &
MS_LANG))
183 if (!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.
Langs &
OCL_GAS))
206 if (!LangOpts.CPlusPlus20 && strchr(AttributesStr.data(),
'G') !=
nullptr)
224 case Builtin::BI__builtin_fma:
225 case Builtin::BI__builtin_fmaf:
226 case Builtin::BI__builtin_fmal:
227 case Builtin::BI__builtin_fmaf16:
229 case Builtin::BIfmaf:
230 case Builtin::BIfmal: {
231 if (Trip.isGNUEnvironment() || Trip.isOSMSVCRT())
243 unsigned BuiltinID, llvm::Triple Trip, std::optional<bool> ErrnoOverwritten,
244 bool MathErrnoEnabled,
bool HasOptNoneAttr,
245 bool IsOptimizationEnabled)
const {
250 bool ErrnoOverridenToFalseWithOpt = ErrnoOverwritten.has_value() &&
251 !ErrnoOverwritten.value() &&
252 !HasOptNoneAttr && IsOptimizationEnabled;
264 bool ConstWithoutErrnoAndExceptions =
283 bool ConstWithoutErrnoOrExceptions =
284 ConstWithoutErrnoAndExceptions || ConstWithoutExceptions;
285 bool GenerateIntrinsics =
286 (ConstAlways && !HasOptNoneAttr) ||
287 (!MathErrnoEnabled &&
288 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
290 if (!GenerateIntrinsics) {
292 ConstWithoutErrnoOrExceptions && !ConstWithoutErrnoAndExceptions;
293 if (!GenerateIntrinsics)
295 ConstWithoutErrnoOrExceptions &&
296 (!MathErrnoEnabled &&
297 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
299 if (!GenerateIntrinsics)
301 ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
304 return GenerateIntrinsics;
316 for (
const auto &Shard : BuiltinShards)
317 for (
const auto &I : Shard.Infos) {
326 for (
const auto &Shard : TargetShards)
327 for (
const auto &I : Shard.Infos) {
334 for (
const auto &Shard : AuxTargetShards)
335 for (
const auto &I : Shard.Infos) {
343 bool InStdNamespace = Name.consume_front(
"std-");
344 auto NameIt = Table.
find(Name);
345 if (NameIt != Table.
end()) {
346 unsigned ID = NameIt->second->getBuiltinID();
349 NameIt->second->clearBuiltinID();
361 assert(*WidthPos ==
':' &&
362 "Vector width specifier must be followed by a ':'");
366 unsigned Width = ::strtol(WidthPos, &EndPos, 10);
367 assert(*EndPos ==
':' &&
"Vector width specific must end with a ':'");
371bool Builtin::Context::isLike(
unsigned ID,
unsigned &FormatIdx,
372 bool &HasVAListArg,
const char *Fmt)
const {
373 assert(Fmt &&
"Not passed a format string");
374 assert(::strlen(Fmt) == 2 &&
375 "Format string needs to be two characters long");
376 assert(::toupper(Fmt[0]) == Fmt[1] &&
377 "Format string is not in the form \"xX\"");
379 const char *Like = ::strpbrk(getAttributesString(
ID), Fmt);
383 HasVAListArg = (*Like == Fmt[1]);
386 assert(*Like ==
':' &&
"Format specifier must be followed by a ':'");
389 assert(::strchr(Like,
':') &&
"Format specifier must end with a ':'");
390 FormatIdx = ::strtol(Like,
nullptr, 10);
395 bool &HasVAListArg) {
396 return isLike(
ID, FormatIdx, HasVAListArg,
"pP");
400 bool &HasVAListArg) {
401 return isLike(
ID, FormatIdx, HasVAListArg,
"sS");
406 assert(*CurrPos ==
'<' &&
"Expected '<' to start index list");
410 int PosIdx = ::strtol(CurrPos, &EndPos, 10);
411 assert(PosIdx >= 0 &&
"Index is supposed to be positive!");
412 Indxs.push_back(PosIdx);
414 while (*EndPos ==
',') {
415 const char *PayloadPos = EndPos + 1;
417 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
418 Indxs.push_back(PayloadIdx);
421 assert(*EndPos ==
'>' &&
"Index list must end with '>'");
432 assert(*AttrPos ==
':' &&
"Format specifier must be followed by a ':'");
436 else if (*AttrPos ==
'1')
439 llvm_unreachable(
"Unrecognized NonNull optimization mode");
441 assert(*AttrPos ==
':' &&
"Mode must be followed by a ':'");
463 ID == Builtin::BI__builtin_assume_aligned ||
469 StringRef RequiredFeatures,
const llvm::StringMap<bool> &TargetFetureMap) {
471 if (RequiredFeatures.empty())
473 assert(!RequiredFeatures.contains(
' ') &&
"Space in feature list");
476 return TF.hasRequiredFeatures(RequiredFeatures);
static constexpr llvm::StringTable BuiltinStrings
static StringRef getBuiltinNameInto(const Builtin::InfosShard &Shard, const Builtin::Info &BuiltinInfo, SmallVectorImpl< char > &Buf)
Return a non-owning StringRef of the builtin's name, reconstructed into Buf.
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 void parseCommaSeparatedIndices(const char *CurrPos, llvm::SmallVectorImpl< int > &Indxs)
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 isNonNull(unsigned ID, llvm::SmallVectorImpl< int > &Indxs, Info::NonNullMode &Mode) const
Return true if this builtin has parameters that must be non-null.
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 Name
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