Home | History | Annotate | Download | only in ir
      1 /*
      2  * Copyright 2016 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SKSL_PREFIXEXPRESSION
      9 #define SKSL_PREFIXEXPRESSION
     10 
     11 #include "SkSLExpression.h"
     12 #include "SkSLFloatLiteral.h"
     13 #include "SkSLIRGenerator.h"
     14 #include "SkSLToken.h"
     15 
     16 namespace SkSL {
     17 
     18 /**
     19  * An expression modified by a unary operator appearing before it, such as '!flag'.
     20  */
     21 struct PrefixExpression : public Expression {
     22     PrefixExpression(Token::Kind op, std::unique_ptr<Expression> operand)
     23     : INHERITED(operand->fPosition, kPrefix_Kind, operand->fType)
     24     , fOperand(std::move(operand))
     25     , fOperator(op) {}
     26 
     27     bool isConstant() const override {
     28         return fOperator == Token::MINUS && fOperand->isConstant();
     29     }
     30 
     31     bool hasSideEffects() const override {
     32         return fOperator == Token::PLUSPLUS || fOperator == Token::MINUSMINUS ||
     33                fOperand->hasSideEffects();
     34     }
     35 
     36     std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
     37                                                   const DefinitionMap& definitions) override {
     38         if (fOperand->fKind == Expression::kFloatLiteral_Kind) {
     39             return std::unique_ptr<Expression>(new FloatLiteral(
     40                                                               irGenerator.fContext,
     41                                                               Position(),
     42                                                               -((FloatLiteral&) *fOperand).fValue));
     43 
     44         }
     45         return nullptr;
     46     }
     47 
     48     String description() const override {
     49         return Token::OperatorName(fOperator) + fOperand->description();
     50     }
     51 
     52     std::unique_ptr<Expression> fOperand;
     53     const Token::Kind fOperator;
     54 
     55     typedef Expression INHERITED;
     56 };
     57 
     58 } // namespace
     59 
     60 #endif
     61