blob: 02876f14291d126a9eef1405054ca48d1bcc7735 [file] [log] [blame]
Daniel Jasperd1122cb2012-12-20 20:25:19 +00001//===--- OperatorPrecedence.cpp ---------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Jasperd1122cb2012-12-20 20:25:19 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// Defines and computes precedence levels for binary/ternary operators.
Daniel Jasperd1122cb2012-12-20 20:25:19 +000011///
12//===----------------------------------------------------------------------===//
13#include "clang/Basic/OperatorPrecedence.h"
14
15namespace clang {
16
Daniel Jasper9dd7e0f2012-12-20 20:31:38 +000017prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator,
Richard Smith2bf7fdb2013-01-02 11:42:31 +000018 bool CPlusPlus11) {
Daniel Jasperd1122cb2012-12-20 20:25:19 +000019 switch (Kind) {
20 case tok::greater:
21 // C++ [temp.names]p3:
22 // [...] When parsing a template-argument-list, the first
23 // non-nested > is taken as the ending delimiter rather than a
24 // greater-than operator. [...]
25 if (GreaterThanIsOperator)
26 return prec::Relational;
27 return prec::Unknown;
28
29 case tok::greatergreater:
Craig Topper122ec3a2013-07-14 17:04:56 +000030 // C++11 [temp.names]p3:
Daniel Jasperd1122cb2012-12-20 20:25:19 +000031 //
32 // [...] Similarly, the first non-nested >> is treated as two
33 // consecutive but distinct > tokens, the first of which is
34 // taken as the end of the template-argument-list and completes
35 // the template-id. [...]
Richard Smith2bf7fdb2013-01-02 11:42:31 +000036 if (GreaterThanIsOperator || !CPlusPlus11)
Daniel Jasperd1122cb2012-12-20 20:25:19 +000037 return prec::Shift;
38 return prec::Unknown;
39
40 default: return prec::Unknown;
41 case tok::comma: return prec::Comma;
42 case tok::equal:
43 case tok::starequal:
44 case tok::slashequal:
45 case tok::percentequal:
46 case tok::plusequal:
47 case tok::minusequal:
48 case tok::lesslessequal:
49 case tok::greatergreaterequal:
50 case tok::ampequal:
51 case tok::caretequal:
52 case tok::pipeequal: return prec::Assignment;
53 case tok::question: return prec::Conditional;
54 case tok::pipepipe: return prec::LogicalOr;
Anastasia Stulova735c6cd2016-02-03 15:17:14 +000055 case tok::caretcaret:
Daniel Jasperd1122cb2012-12-20 20:25:19 +000056 case tok::ampamp: return prec::LogicalAnd;
57 case tok::pipe: return prec::InclusiveOr;
58 case tok::caret: return prec::ExclusiveOr;
59 case tok::amp: return prec::And;
60 case tok::exclaimequal:
61 case tok::equalequal: return prec::Equality;
62 case tok::lessequal:
63 case tok::less:
64 case tok::greaterequal: return prec::Relational;
Richard Smithc70f1d62017-12-14 15:16:18 +000065 case tok::spaceship: return prec::Spaceship;
Daniel Jasperd1122cb2012-12-20 20:25:19 +000066 case tok::lessless: return prec::Shift;
67 case tok::plus:
68 case tok::minus: return prec::Additive;
69 case tok::percent:
70 case tok::slash:
71 case tok::star: return prec::Multiplicative;
72 case tok::periodstar:
73 case tok::arrowstar: return prec::PointerToMember;
74 }
75}
76
77} // namespace clang