Builtins.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/TargetInfo.h"
  16. #include "clang/Basic/LangOptions.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. using namespace clang;
  19. static const Builtin::Info BuiltinInfo[] = {
  20. { "not a builtin function", 0, 0, 0, ALL_LANGUAGES },
  21. #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
  22. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
  23. BUILTIN_LANG },
  24. #include "clang/Basic/Builtins.def"
  25. };
  26. const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
  27. if (ID < Builtin::FirstTSBuiltin)
  28. return BuiltinInfo[ID];
  29. assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
  30. return TSRecords[ID - Builtin::FirstTSBuiltin];
  31. }
  32. Builtin::Context::Context() {
  33. // Get the target specific builtins from the target.
  34. TSRecords = 0;
  35. NumTSRecords = 0;
  36. }
  37. void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
  38. assert(NumTSRecords == 0 && "Already initialized target?");
  39. Target.getTargetBuiltins(TSRecords, NumTSRecords);
  40. }
  41. /// InitializeBuiltins - Mark the identifiers for all the builtins with their
  42. /// appropriate builtin ID # and mark any non-portable builtin identifiers as
  43. /// such.
  44. void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
  45. const LangOptions& LangOpts) {
  46. // Step #1: mark all target-independent builtins with their ID's.
  47. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  48. if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) {
  49. if (LangOpts.ObjC1 ||
  50. BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
  51. Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
  52. }
  53. // Step #2: Register target-specific builtins.
  54. for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
  55. if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
  56. Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
  57. }
  58. void
  59. Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
  60. bool NoBuiltins) {
  61. // Final all target-independent names
  62. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  63. if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))
  64. Names.push_back(BuiltinInfo[i].Name);
  65. // Find target-specific names.
  66. for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
  67. if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f'))
  68. Names.push_back(TSRecords[i].Name);
  69. }
  70. void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
  71. Table.get(GetRecord(ID).Name).setBuiltinID(0);
  72. }
  73. bool
  74. Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
  75. bool &HasVAListArg) {
  76. const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
  77. if (!Printf)
  78. return false;
  79. HasVAListArg = (*Printf == 'P');
  80. ++Printf;
  81. assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
  82. ++Printf;
  83. assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
  84. FormatIdx = strtol(Printf, 0, 10);
  85. return true;
  86. }
  87. // FIXME: Refactor with isPrintfLike.
  88. bool
  89. Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
  90. bool &HasVAListArg) {
  91. const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
  92. if (!Scanf)
  93. return false;
  94. HasVAListArg = (*Scanf == 'S');
  95. ++Scanf;
  96. assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
  97. ++Scanf;
  98. assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
  99. FormatIdx = strtol(Scanf, 0, 10);
  100. return true;
  101. }