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