SelectorLocationsKind.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===--- SelectorLocationsKind.cpp - Kind of selector locations -*- C++ -*-===//
  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. // Describes whether the identifier locations for a selector are "standard"
  11. // or not.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/SelectorLocationsKind.h"
  15. #include "clang/AST/Expr.h"
  16. using namespace clang;
  17. static SourceLocation getStandardSelLoc(unsigned Index,
  18. Selector Sel,
  19. bool WithArgSpace,
  20. SourceLocation ArgLoc,
  21. SourceLocation EndLoc) {
  22. unsigned NumSelArgs = Sel.getNumArgs();
  23. if (NumSelArgs == 0) {
  24. assert(Index == 0);
  25. if (EndLoc.isInvalid())
  26. return SourceLocation();
  27. IdentifierInfo *II = Sel.getIdentifierInfoForSlot(0);
  28. unsigned Len = II ? II->getLength() : 0;
  29. return EndLoc.getLocWithOffset(-Len);
  30. }
  31. assert(Index < NumSelArgs);
  32. if (ArgLoc.isInvalid())
  33. return SourceLocation();
  34. IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Index);
  35. unsigned Len = /* selector id */ (II ? II->getLength() : 0) + /* ':' */ 1;
  36. if (WithArgSpace)
  37. ++Len;
  38. return ArgLoc.getLocWithOffset(-Len);
  39. }
  40. namespace {
  41. template <typename T>
  42. SourceLocation getArgLoc(T* Arg);
  43. template <>
  44. SourceLocation getArgLoc<Expr>(Expr *Arg) {
  45. return Arg->getLocStart();
  46. }
  47. template <>
  48. SourceLocation getArgLoc<ParmVarDecl>(ParmVarDecl *Arg) {
  49. SourceLocation Loc = Arg->getLocStart();
  50. if (Loc.isInvalid())
  51. return Loc;
  52. // -1 to point to left paren of the method parameter's type.
  53. return Loc.getLocWithOffset(-1);
  54. }
  55. template <typename T>
  56. SourceLocation getArgLoc(unsigned Index, ArrayRef<T*> Args) {
  57. return Index < Args.size() ? getArgLoc(Args[Index]) : SourceLocation();
  58. }
  59. template <typename T>
  60. SelectorLocationsKind hasStandardSelLocs(Selector Sel,
  61. ArrayRef<SourceLocation> SelLocs,
  62. ArrayRef<T *> Args,
  63. SourceLocation EndLoc) {
  64. // Are selector locations in standard position with no space between args ?
  65. unsigned i;
  66. for (i = 0; i != SelLocs.size(); ++i) {
  67. if (SelLocs[i] != getStandardSelectorLoc(i, Sel, /*WithArgSpace=*/false,
  68. Args, EndLoc))
  69. break;
  70. }
  71. if (i == SelLocs.size())
  72. return SelLoc_StandardNoSpace;
  73. // Are selector locations in standard position with space between args ?
  74. for (i = 0; i != SelLocs.size(); ++i) {
  75. if (SelLocs[i] != getStandardSelectorLoc(i, Sel, /*WithArgSpace=*/true,
  76. Args, EndLoc))
  77. return SelLoc_NonStandard;
  78. }
  79. return SelLoc_StandardWithSpace;
  80. }
  81. } // anonymous namespace
  82. SelectorLocationsKind
  83. clang::hasStandardSelectorLocs(Selector Sel,
  84. ArrayRef<SourceLocation> SelLocs,
  85. ArrayRef<Expr *> Args,
  86. SourceLocation EndLoc) {
  87. return hasStandardSelLocs(Sel, SelLocs, Args, EndLoc);
  88. }
  89. SourceLocation clang::getStandardSelectorLoc(unsigned Index,
  90. Selector Sel,
  91. bool WithArgSpace,
  92. ArrayRef<Expr *> Args,
  93. SourceLocation EndLoc) {
  94. return getStandardSelLoc(Index, Sel, WithArgSpace,
  95. getArgLoc(Index, Args), EndLoc);
  96. }
  97. SelectorLocationsKind
  98. clang::hasStandardSelectorLocs(Selector Sel,
  99. ArrayRef<SourceLocation> SelLocs,
  100. ArrayRef<ParmVarDecl *> Args,
  101. SourceLocation EndLoc) {
  102. return hasStandardSelLocs(Sel, SelLocs, Args, EndLoc);
  103. }
  104. SourceLocation clang::getStandardSelectorLoc(unsigned Index,
  105. Selector Sel,
  106. bool WithArgSpace,
  107. ArrayRef<ParmVarDecl *> Args,
  108. SourceLocation EndLoc) {
  109. return getStandardSelLoc(Index, Sel, WithArgSpace,
  110. getArgLoc(Index, Args), EndLoc);
  111. }