OperatorPrecedence.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===--- OperatorPrecedence.cpp ---------------------------------*- C++ -*-===//
  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. /// \file
  10. /// Defines and computes precedence levels for binary/ternary operators.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/OperatorPrecedence.h"
  14. namespace clang {
  15. prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator,
  16. bool CPlusPlus11) {
  17. switch (Kind) {
  18. case tok::greater:
  19. // C++ [temp.names]p3:
  20. // [...] When parsing a template-argument-list, the first
  21. // non-nested > is taken as the ending delimiter rather than a
  22. // greater-than operator. [...]
  23. if (GreaterThanIsOperator)
  24. return prec::Relational;
  25. return prec::Unknown;
  26. case tok::greatergreater:
  27. // C++11 [temp.names]p3:
  28. //
  29. // [...] Similarly, the first non-nested >> is treated as two
  30. // consecutive but distinct > tokens, the first of which is
  31. // taken as the end of the template-argument-list and completes
  32. // the template-id. [...]
  33. if (GreaterThanIsOperator || !CPlusPlus11)
  34. return prec::Shift;
  35. return prec::Unknown;
  36. default: return prec::Unknown;
  37. case tok::comma: return prec::Comma;
  38. case tok::equal:
  39. case tok::starequal:
  40. case tok::slashequal:
  41. case tok::percentequal:
  42. case tok::plusequal:
  43. case tok::minusequal:
  44. case tok::lesslessequal:
  45. case tok::greatergreaterequal:
  46. case tok::ampequal:
  47. case tok::caretequal:
  48. case tok::pipeequal: return prec::Assignment;
  49. case tok::question: return prec::Conditional;
  50. case tok::pipepipe: return prec::LogicalOr;
  51. case tok::caretcaret:
  52. case tok::ampamp: return prec::LogicalAnd;
  53. case tok::pipe: return prec::InclusiveOr;
  54. case tok::caret: return prec::ExclusiveOr;
  55. case tok::amp: return prec::And;
  56. case tok::exclaimequal:
  57. case tok::equalequal: return prec::Equality;
  58. case tok::lessequal:
  59. case tok::less:
  60. case tok::greaterequal: return prec::Relational;
  61. case tok::spaceship: return prec::Spaceship;
  62. case tok::lessless: return prec::Shift;
  63. case tok::plus:
  64. case tok::minus: return prec::Additive;
  65. case tok::percent:
  66. case tok::slash:
  67. case tok::star: return prec::Multiplicative;
  68. case tok::periodstar:
  69. case tok::arrowstar: return prec::PointerToMember;
  70. }
  71. }
  72. } // namespace clang