Builtins.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //===--- Builtins.cpp - Builtin function implementation -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements various things for builtin functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/Builtins.h"
  13. #include "clang/Basic/IdentifierTable.h"
  14. #include "clang/Basic/LangOptions.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "llvm/ADT/StringRef.h"
  17. using namespace clang;
  18. static const Builtin::Info BuiltinInfo[] = {
  19. { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
  20. #define BUILTIN(ID, TYPE, ATTRS) \
  21. { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
  22. #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
  23. { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
  24. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
  25. { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
  26. #include "clang/Basic/Builtins.def"
  27. };
  28. const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
  29. if (ID < Builtin::FirstTSBuiltin)
  30. return BuiltinInfo[ID];
  31. assert(((ID - Builtin::FirstTSBuiltin) <
  32. (TSRecords.size() + AuxTSRecords.size())) &&
  33. "Invalid builtin ID!");
  34. if (isAuxBuiltinID(ID))
  35. return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
  36. return TSRecords[ID - Builtin::FirstTSBuiltin];
  37. }
  38. void Builtin::Context::InitializeTarget(const TargetInfo &Target,
  39. const TargetInfo *AuxTarget) {
  40. assert(TSRecords.empty() && "Already initialized target?");
  41. TSRecords = Target.getTargetBuiltins();
  42. if (AuxTarget)
  43. AuxTSRecords = AuxTarget->getTargetBuiltins();
  44. }
  45. bool Builtin::Context::isBuiltinFunc(const char *Name) {
  46. StringRef FuncName(Name);
  47. for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
  48. if (FuncName.equals(BuiltinInfo[i].Name))
  49. return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
  50. return false;
  51. }
  52. bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
  53. const LangOptions &LangOpts) {
  54. bool BuiltinsUnsupported =
  55. (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
  56. strchr(BuiltinInfo.Attributes, 'f');
  57. bool MathBuiltinsUnsupported =
  58. LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
  59. llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
  60. bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
  61. bool MSModeUnsupported =
  62. !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
  63. bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
  64. bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
  65. (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
  66. bool OclC2Unsupported =
  67. (LangOpts.OpenCLVersion != 200 && !LangOpts.OpenCLCPlusPlus) &&
  68. (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
  69. bool OclCUnsupported = !LangOpts.OpenCL &&
  70. (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
  71. bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
  72. bool CPlusPlusUnsupported =
  73. !LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
  74. return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
  75. !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported &&
  76. !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
  77. !CPlusPlusUnsupported;
  78. }
  79. /// initializeBuiltins - Mark the identifiers for all the builtins with their
  80. /// appropriate builtin ID # and mark any non-portable builtin identifiers as
  81. /// such.
  82. void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
  83. const LangOptions& LangOpts) {
  84. // Step #1: mark all target-independent builtins with their ID's.
  85. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  86. if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
  87. Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
  88. }
  89. // Step #2: Register target-specific builtins.
  90. for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
  91. if (builtinIsSupported(TSRecords[i], LangOpts))
  92. Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
  93. // Step #3: Register target-specific builtins for AuxTarget.
  94. for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
  95. Table.get(AuxTSRecords[i].Name)
  96. .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
  97. }
  98. void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
  99. Table.get(getRecord(ID).Name).setBuiltinID(0);
  100. }
  101. unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
  102. const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
  103. if (!WidthPos)
  104. return 0;
  105. ++WidthPos;
  106. assert(*WidthPos == ':' &&
  107. "Vector width specifier must be followed by a ':'");
  108. ++WidthPos;
  109. char *EndPos;
  110. unsigned Width = ::strtol(WidthPos, &EndPos, 10);
  111. assert(*EndPos == ':' && "Vector width specific must end with a ':'");
  112. return Width;
  113. }
  114. bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
  115. bool &HasVAListArg, const char *Fmt) const {
  116. assert(Fmt && "Not passed a format string");
  117. assert(::strlen(Fmt) == 2 &&
  118. "Format string needs to be two characters long");
  119. assert(::toupper(Fmt[0]) == Fmt[1] &&
  120. "Format string is not in the form \"xX\"");
  121. const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
  122. if (!Like)
  123. return false;
  124. HasVAListArg = (*Like == Fmt[1]);
  125. ++Like;
  126. assert(*Like == ':' && "Format specifier must be followed by a ':'");
  127. ++Like;
  128. assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
  129. FormatIdx = ::strtol(Like, nullptr, 10);
  130. return true;
  131. }
  132. bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
  133. bool &HasVAListArg) {
  134. return isLike(ID, FormatIdx, HasVAListArg, "pP");
  135. }
  136. bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
  137. bool &HasVAListArg) {
  138. return isLike(ID, FormatIdx, HasVAListArg, "sS");
  139. }
  140. bool Builtin::Context::performsCallback(unsigned ID,
  141. SmallVectorImpl<int> &Encoding) const {
  142. const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
  143. if (!CalleePos)
  144. return false;
  145. ++CalleePos;
  146. assert(*CalleePos == '<' &&
  147. "Callback callee specifier must be followed by a '<'");
  148. ++CalleePos;
  149. char *EndPos;
  150. int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
  151. assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
  152. Encoding.push_back(CalleeIdx);
  153. while (*EndPos == ',') {
  154. const char *PayloadPos = EndPos + 1;
  155. int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
  156. Encoding.push_back(PayloadIdx);
  157. }
  158. assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
  159. return true;
  160. }
  161. bool Builtin::Context::canBeRedeclared(unsigned ID) const {
  162. return ID == Builtin::NotBuiltin ||
  163. ID == Builtin::BI__va_start ||
  164. (!hasReferenceArgsOrResult(ID) &&
  165. !hasCustomTypechecking(ID));
  166. }