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 "SkSLLexer.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 {
PrefixExpressionPrefixExpression22     PrefixExpression(Token::Kind op, std::unique_ptr<Expression> operand)
23     : INHERITED(operand->fOffset, kPrefix_Kind, operand->fType)
24     , fOperand(std::move(operand))
25     , fOperator(op) {}
26 
isConstantPrefixExpression27     bool isConstant() const override {
28         return fOperator == Token::MINUS && fOperand->isConstant();
29     }
30 
hasSideEffectsPrefixExpression31     bool hasSideEffects() const override {
32         return fOperator == Token::PLUSPLUS || fOperator == Token::MINUSMINUS ||
33                fOperand->hasSideEffects();
34     }
35 
constantPropagatePrefixExpression36     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                                                               fOffset,
42                                                               -((FloatLiteral&) *fOperand).fValue));
43 
44         }
45         return nullptr;
46     }
47 
clonePrefixExpression48     std::unique_ptr<Expression> clone() const override {
49         return std::unique_ptr<Expression>(new PrefixExpression(fOperator, fOperand->clone()));
50     }
51 
descriptionPrefixExpression52     String description() const override {
53         return Compiler::OperatorName(fOperator) + fOperand->description();
54     }
55 
56     std::unique_ptr<Expression> fOperand;
57     const Token::Kind fOperator;
58 
59     typedef Expression INHERITED;
60 };
61 
62 } // namespace
63 
64 #endif
65