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