Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLTernaryExpression.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_TERNARYEXPRESSION
9#define SKSL_TERNARYEXPRESSION
10
16
17#include <cstdint>
18#include <memory>
19#include <string>
20#include <utility>
21
22namespace SkSL {
23
24class Context;
25enum class OperatorPrecedence : uint8_t;
26
27/**
28 * A ternary expression (test ? ifTrue : ifFalse).
29 */
30class TernaryExpression final : public Expression {
31public:
32 inline static constexpr Kind kIRNodeKind = Kind::kTernary;
33
34 TernaryExpression(Position pos, std::unique_ptr<Expression> test,
35 std::unique_ptr<Expression> ifTrue, std::unique_ptr<Expression> ifFalse)
37 , fTest(std::move(test))
38 , fIfTrue(std::move(ifTrue))
39 , fIfFalse(std::move(ifFalse)) {
40 SkASSERT(this->ifTrue()->type().matches(this->ifFalse()->type()));
41 }
42
43 // Creates a potentially-simplified form of the ternary. Typechecks and coerces input
44 // expressions; reports errors via ErrorReporter.
45 static std::unique_ptr<Expression> Convert(const Context& context,
47 std::unique_ptr<Expression> test,
48 std::unique_ptr<Expression> ifTrue,
49 std::unique_ptr<Expression> ifFalse);
50
51 // Creates a potentially-simplified form of the ternary; reports errors via ASSERT.
52 static std::unique_ptr<Expression> Make(const Context& context,
54 std::unique_ptr<Expression> test,
55 std::unique_ptr<Expression> ifTrue,
56 std::unique_ptr<Expression> ifFalse);
57
58 std::unique_ptr<Expression>& test() {
59 return fTest;
60 }
61
62 const std::unique_ptr<Expression>& test() const {
63 return fTest;
64 }
65
66 std::unique_ptr<Expression>& ifTrue() {
67 return fIfTrue;
68 }
69
70 const std::unique_ptr<Expression>& ifTrue() const {
71 return fIfTrue;
72 }
73
74 std::unique_ptr<Expression>& ifFalse() {
75 return fIfFalse;
76 }
77
78 const std::unique_ptr<Expression>& ifFalse() const {
79 return fIfFalse;
80 }
81
82 std::unique_ptr<Expression> clone(Position pos) const override {
83 return std::make_unique<TernaryExpression>(pos, this->test()->clone(),
84 this->ifTrue()->clone(),
85 this->ifFalse()->clone());
86 }
87
88 std::string description(OperatorPrecedence parentPrecedence) const override;
89
90private:
91 std::unique_ptr<Expression> fTest;
92 std::unique_ptr<Expression> fIfTrue;
93 std::unique_ptr<Expression> fIfFalse;
94
95 using INHERITED = Expression;
96};
97
98} // namespace SkSL
99
100#endif
SkPoint pos
#define SkASSERT(cond)
Definition SkAssert.h:116
virtual const Type & type() const
std::unique_ptr< Expression > clone() const
std::string description() const final
const std::unique_ptr< Expression > & test() const
std::unique_ptr< Expression > clone(Position pos) const override
const std::unique_ptr< Expression > & ifTrue() const
const std::unique_ptr< Expression > & ifFalse() const
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > test, std::unique_ptr< Expression > ifTrue, std::unique_ptr< Expression > ifFalse)
std::unique_ptr< Expression > & ifTrue()
std::unique_ptr< Expression > & test()
TernaryExpression(Position pos, std::unique_ptr< Expression > test, std::unique_ptr< Expression > ifTrue, std::unique_ptr< Expression > ifFalse)
static std::unique_ptr< Expression > Convert(const Context &context, Position pos, std::unique_ptr< Expression > test, std::unique_ptr< Expression > ifTrue, std::unique_ptr< Expression > ifFalse)
std::unique_ptr< Expression > & ifFalse()
static constexpr Kind kIRNodeKind
OperatorPrecedence
ExpressionKind
Definition SkSLIRNode.h:62
Definition ref_ptr.h:256