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)
210 case Builtin::BI__builtin_fma:
211 case Builtin::BI__builtin_fmaf:
212 case Builtin::BI__builtin_fmal:
213 case Builtin::BI__builtin_fmaf16:
215 case Builtin::BIfmaf:
216 case Builtin::BIfmal: {
217 if (Trip.isGNUEnvironment() || Trip.isOSMSVCRT())
229 unsigned BuiltinID, llvm::Triple Trip, std::optional<bool> ErrnoOverwritten,
230 bool MathErrnoEnabled,
bool HasOptNoneAttr,
231 bool IsOptimizationEnabled)
const {
236 bool ErrnoOverridenToFalseWithOpt = ErrnoOverwritten.has_value() &&
237 !ErrnoOverwritten.value() &&
238 !HasOptNoneAttr && IsOptimizationEnabled;
250 bool ConstWithoutErrnoAndExceptions =
269 bool ConstWithoutErrnoOrExceptions =
270 ConstWithoutErrnoAndExceptions || ConstWithoutExceptions;
271 bool GenerateIntrinsics =
272 (ConstAlways && !HasOptNoneAttr) ||
273 (!MathErrnoEnabled &&
274 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
276 if (!GenerateIntrinsics) {
278 ConstWithoutErrnoOrExceptions && !ConstWithoutErrnoAndExceptions;
279 if (!GenerateIntrinsics)
281 ConstWithoutErrnoOrExceptions &&
282 (!MathErrnoEnabled &&
283 !(ErrnoOverwritten.has_value() && ErrnoOverwritten.value()) &&
285 if (!GenerateIntrinsics)
287 ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
290 return GenerateIntrinsics;
301 for (
const auto &Shard : BuiltinShards)
302 for (
const auto &I : Shard.Infos) {
311 for (
const auto &Shard : TargetShards)
312 for (
const auto &I : Shard.Infos) {
319 for (
const auto &Shard : AuxTargetShards)
320 for (
const auto &I : Shard.Infos) {
328 bool InStdNamespace = Name.consume_front(
"std-");
329 auto NameIt = Table.
find(Name);
330 if (NameIt != Table.
end()) {
331 unsigned ID = NameIt->second->getBuiltinID();
334 NameIt->second->clearBuiltinID();
346 assert(*WidthPos ==
':' &&
347 "Vector width specifier must be followed by a ':'");
351 unsigned Width = ::strtol(WidthPos, &EndPos, 10);
352 assert(*EndPos ==
':' &&
"Vector width specific must end with a ':'");
356bool Builtin::Context::isLike(
unsigned ID,
unsigned &FormatIdx,
357 bool &HasVAListArg,
const char *Fmt)
const {
358 assert(Fmt &&
"Not passed a format string");
359 assert(::strlen(Fmt) == 2 &&
360 "Format string needs to be two characters long");
361 assert(::toupper(Fmt[0]) == Fmt[1] &&
362 "Format string is not in the form \"xX\"");
364 const char *Like = ::strpbrk(getAttributesString(
ID), Fmt);
368 HasVAListArg = (*Like == Fmt[1]);
371 assert(*Like ==
':' &&
"Format specifier must be followed by a ':'");
374 assert(::strchr(Like,
':') &&
"Format specifier must end with a ':'");
375 FormatIdx = ::strtol(Like,
nullptr, 10);
380 bool &HasVAListArg) {
381 return isLike(
ID, FormatIdx, HasVAListArg,
"pP");
385 bool &HasVAListArg) {
386 return isLike(
ID, FormatIdx, HasVAListArg,
"sS");
391 assert(*CurrPos ==
'<' &&
"Expected '<' to start index list");
395 int PosIdx = ::strtol(CurrPos, &EndPos, 10);
396 assert(PosIdx >= 0 &&
"Index is supposed to be positive!");
397 Indxs.push_back(PosIdx);
399 while (*EndPos ==
',') {
400 const char *PayloadPos = EndPos + 1;
402 int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
403 Indxs.push_back(PayloadIdx);
406 assert(*EndPos ==
'>' &&
"Index list must end with '>'");
417 assert(*AttrPos ==
':' &&
"Format specifier must be followed by a ':'");
421 else if (*AttrPos ==
'1')
424 llvm_unreachable(
"Unrecognized NonNull optimization mode");
426 assert(*AttrPos ==
':' &&
"Mode must be followed by a ':'");
448 ID == Builtin::BI__builtin_assume_aligned ||
454 StringRef RequiredFeatures,
const llvm::StringMap<bool> &TargetFetureMap) {
456 if (RequiredFeatures.empty())
458 assert(!RequiredFeatures.contains(
' ') &&
"Space in feature list");
461 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 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 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