Home | History | Annotate | Download | only in Basic
      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 
     18 prec::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::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;
     65   case tok::lessless:             return prec::Shift;
     66   case tok::plus:
     67   case tok::minus:                return prec::Additive;
     68   case tok::percent:
     69   case tok::slash:
     70   case tok::star:                 return prec::Multiplicative;
     71   case tok::periodstar:
     72   case tok::arrowstar:            return prec::PointerToMember;
     73   }
     74 }
     75 
     76 }  // namespace clang
     77