blob: d5d97b2517d9690865ffc6e7f0b346d986347693 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_PREFIXEXPRESSION
9#define SKSL_PREFIXEXPRESSION
10
11#include "SkSLExpression.h"
Hal Canaryce78bad2017-05-04 14:15:40 -040012#include "SkSLFloatLiteral.h"
13#include "SkSLIRGenerator.h"
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070014#include "SkSLLexer.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070015
16namespace SkSL {
17
18/**
19 * An expression modified by a unary operator appearing before it, such as '!flag'.
20 */
21struct PrefixExpression : public Expression {
22 PrefixExpression(Token::Kind op, std::unique_ptr<Expression> operand)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070023 : INHERITED(operand->fOffset, kPrefix_Kind, operand->fType)
ethannicholasb3058bd2016-07-01 08:22:01 -070024 , fOperand(std::move(operand))
25 , fOperator(op) {}
26
Ethan Nicholascb670962017-04-20 19:31:52 -040027 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
Ethan Nicholas762466e2017-06-29 10:03:38 -040036 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
37 const DefinitionMap& definitions) override {
Ethan Nicholascb670962017-04-20 19:31:52 -040038 if (fOperand->fKind == Expression::kFloatLiteral_Kind) {
39 return std::unique_ptr<Expression>(new FloatLiteral(
40 irGenerator.fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070041 fOffset,
Ethan Nicholascb670962017-04-20 19:31:52 -040042 -((FloatLiteral&) *fOperand).fValue));
43
44 }
45 return nullptr;
46 }
47
48 String description() const override {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070049 return Compiler::OperatorName(fOperator) + fOperand->description();
ethannicholasb3058bd2016-07-01 08:22:01 -070050 }
51
Ethan Nicholas86a43402017-01-19 13:32:00 -050052 std::unique_ptr<Expression> fOperand;
ethannicholasb3058bd2016-07-01 08:22:01 -070053 const Token::Kind fOperator;
54
55 typedef Expression INHERITED;
56};
57
58} // namespace
59
60#endif