Flutter Engine
The Flutter Engine
SkSLBinaryExpression.h
Go to the documentation of this file.
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_BINARYEXPRESSION
9#define SKSL_BINARYEXPRESSION
10
16
17#include <memory>
18#include <string>
19#include <utility>
20
21namespace SkSL {
22
23class Context;
24class Type;
25class VariableReference;
26
27/**
28 * A binary operation.
29 */
30class BinaryExpression final : public Expression {
31public:
32 inline static constexpr Kind kIRNodeKind = Kind::kBinary;
33
34 BinaryExpression(Position pos, std::unique_ptr<Expression> left, Operator op,
35 std::unique_ptr<Expression> right, const Type* type)
37 , fLeft(std::move(left))
38 , fOperator(op)
39 , fRight(std::move(right)) {
40 // If we are assigning to a VariableReference, ensure that it is set to Write or ReadWrite.
41 SkASSERT(!op.isAssignment() || CheckRef(*this->left()));
42 }
43
44 // Creates a potentially-simplified form of the expression. Determines the result type
45 // programmatically. Typechecks and coerces input expressions; reports errors via ErrorReporter.
46 static std::unique_ptr<Expression> Convert(const Context& context,
48 std::unique_ptr<Expression> left,
49 Operator op,
50 std::unique_ptr<Expression> right);
51
52 // Creates a potentially-simplified form of the expression. Determines the result type
53 // programmatically. Asserts if the expressions do not typecheck or are otherwise invalid.
54 static std::unique_ptr<Expression> Make(const Context& context,
56 std::unique_ptr<Expression> left,
57 Operator op,
58 std::unique_ptr<Expression> right);
59
60 // Creates a potentially-simplified form of the expression. Result type is passed in.
61 // Asserts if the expressions do not typecheck or are otherwise invalid.
62 static std::unique_ptr<Expression> Make(const Context& context,
64 std::unique_ptr<Expression> left,
65 Operator op,
66 std::unique_ptr<Expression> right,
67 const Type* resultType);
68
69 std::unique_ptr<Expression>& left() {
70 return fLeft;
71 }
72
73 const std::unique_ptr<Expression>& left() const {
74 return fLeft;
75 }
76
77 std::unique_ptr<Expression>& right() {
78 return fRight;
79 }
80
81 const std::unique_ptr<Expression>& right() const {
82 return fRight;
83 }
84
86 return fOperator;
87 }
88
89 std::unique_ptr<Expression> clone(Position pos) const override;
90
91 std::string description(OperatorPrecedence parentPrecedence) const override;
92
93 /**
94 * If the expression is an assignment like `a = 1` or `a += sin(b)`, returns the
95 * VariableReference that will be written to. For other types of expressions, returns null.
96 * Complex expressions that contain inner assignments, like `(a = b) * 2`, will return null.
97 */
99
100private:
101 static bool CheckRef(const Expression& expr);
102
103 std::unique_ptr<Expression> fLeft;
104 Operator fOperator;
105 std::unique_ptr<Expression> fRight;
106
107 using INHERITED = Expression;
108};
109
110} // namespace SkSL
111
112#endif
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
static std::unique_ptr< Expression > Convert(const Context &context, Position pos, std::unique_ptr< Expression > left, Operator op, std::unique_ptr< Expression > right)
const std::unique_ptr< Expression > & left() const
std::unique_ptr< Expression > & left()
BinaryExpression(Position pos, std::unique_ptr< Expression > left, Operator op, std::unique_ptr< Expression > right, const Type *type)
std::unique_ptr< Expression > & right()
const std::unique_ptr< Expression > & right() const
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > left, Operator op, std::unique_ptr< Expression > right)
VariableReference * isAssignmentIntoVariable()
static constexpr Kind kIRNodeKind
Operator getOperator() const
std::unique_ptr< Expression > clone() const
Expression(Position pos, Kind kind, const Type *type)
const Type & type() const
std::string description() const final
bool isAssignment() const
OperatorPrecedence
Definition: SkSLOperator.h:57
ExpressionKind
Definition: SkSLIRNode.h:62
Definition: ref_ptr.h:256