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