Builtins.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===--- Builtins.cpp - Builtin function implementation -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements various things for builtin functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/Builtins.h"
  14. #include "clang/Basic/IdentifierTable.h"
  15. #include "clang/Basic/LangOptions.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringRef.h"
  19. using namespace clang;
  20. static const Builtin::Info BuiltinInfo[] = {
  21. { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES},
  22. #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
  23. #define LANGBUILTIN(ID, TYPE, ATTRS, BUILTIN_LANG) { #ID, TYPE, ATTRS, 0, BUILTIN_LANG },
  24. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
  25. BUILTIN_LANG },
  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 < NumTSRecords && "Invalid builtin ID!");
  32. return TSRecords[ID - Builtin::FirstTSBuiltin];
  33. }
  34. Builtin::Context::Context() {
  35. // Get the target specific builtins from the target.
  36. TSRecords = nullptr;
  37. NumTSRecords = 0;
  38. }
  39. void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
  40. assert(NumTSRecords == 0 && "Already initialized target?");
  41. Target.getTargetBuiltins(TSRecords, NumTSRecords);
  42. }
  43. bool Builtin::Context::BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
  44. const LangOptions &LangOpts) {
  45. bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
  46. strchr(BuiltinInfo.Attributes, 'f');
  47. bool MathBuiltinsUnsupported =
  48. LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
  49. llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
  50. bool GnuModeUnsupported = !LangOpts.GNUMode &&
  51. (BuiltinInfo.builtin_lang & GNU_LANG);
  52. bool MSModeUnsupported = !LangOpts.MicrosoftExt &&
  53. (BuiltinInfo.builtin_lang & MS_LANG);
  54. bool ObjCUnsupported = !LangOpts.ObjC1 &&
  55. BuiltinInfo.builtin_lang == OBJC_LANG;
  56. return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
  57. !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
  58. }
  59. /// InitializeBuiltins - Mark the identifiers for all the builtins with their
  60. /// appropriate builtin ID # and mark any non-portable builtin identifiers as
  61. /// such.
  62. void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
  63. const LangOptions& LangOpts) {
  64. // Step #1: mark all target-independent builtins with their ID's.
  65. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  66. if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) {
  67. Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
  68. }
  69. // Step #2: Register target-specific builtins.
  70. for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
  71. if (BuiltinIsSupported(TSRecords[i], LangOpts))
  72. Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
  73. }
  74. void
  75. Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) {
  76. // Final all target-independent names
  77. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  78. if (!strchr(BuiltinInfo[i].Attributes, 'f'))
  79. Names.push_back(BuiltinInfo[i].Name);
  80. // Find target-specific names.
  81. for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
  82. if (!strchr(TSRecords[i].Attributes, 'f'))
  83. Names.push_back(TSRecords[i].Name);
  84. }
  85. void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
  86. Table.get(GetRecord(ID).Name).setBuiltinID(0);
  87. }
  88. bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
  89. bool &HasVAListArg, const char *Fmt) const {
  90. assert(Fmt && "Not passed a format string");
  91. assert(::strlen(Fmt) == 2 &&
  92. "Format string needs to be two characters long");
  93. assert(::toupper(Fmt[0]) == Fmt[1] &&
  94. "Format string is not in the form \"xX\"");
  95. const char *Like = ::strpbrk(GetRecord(ID).Attributes, Fmt);
  96. if (!Like)
  97. return false;
  98. HasVAListArg = (*Like == Fmt[1]);
  99. ++Like;
  100. assert(*Like == ':' && "Format specifier must be followed by a ':'");
  101. ++Like;
  102. assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
  103. FormatIdx = ::strtol(Like, nullptr, 10);
  104. return true;
  105. }
  106. bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
  107. bool &HasVAListArg) {
  108. return isLike(ID, FormatIdx, HasVAListArg, "pP");
  109. }
  110. bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
  111. bool &HasVAListArg) {
  112. return isLike(ID, FormatIdx, HasVAListArg, "sS");
  113. }