Flutter Engine
The Flutter Engine
SkSLIndexExpression.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_INDEX
9#define SKSL_INDEX
10
14
15#include <cstdint>
16#include <memory>
17#include <string>
18#include <utility>
19
20namespace SkSL {
21
22class Context;
23class Type;
24enum class OperatorPrecedence : uint8_t;
25
26/**
27 * An expression which extracts a value from an array, vector or matrix, as in 'm[2]'.
28 */
29class IndexExpression final : public Expression {
30public:
31 inline static constexpr Kind kIRNodeKind = Kind::kIndex;
32
33 IndexExpression(const Context& context, Position pos, std::unique_ptr<Expression> base,
34 std::unique_ptr<Expression> index)
35 : INHERITED(pos, kIRNodeKind, &IndexType(context, base->type()))
36 , fBase(std::move(base))
37 , fIndex(std::move(index)) {}
38
39 // Returns a simplified index-expression; reports errors via the ErrorReporter.
40 static std::unique_ptr<Expression> Convert(const Context& context,
42 std::unique_ptr<Expression> base,
43 std::unique_ptr<Expression> index);
44
45 // Returns a simplified index-expression; reports errors via ASSERT.
46 static std::unique_ptr<Expression> Make(const Context& context,
48 std::unique_ptr<Expression> base,
49 std::unique_ptr<Expression> index);
50
51 /**
52 * Given a type, returns the type that will result from extracting an array value from it.
53 */
54 static const Type& IndexType(const Context& context, const Type& type);
55
56 std::unique_ptr<Expression>& base() {
57 return fBase;
58 }
59
60 const std::unique_ptr<Expression>& base() const {
61 return fBase;
62 }
63
64 std::unique_ptr<Expression>& index() {
65 return fIndex;
66 }
67
68 const std::unique_ptr<Expression>& index() const {
69 return fIndex;
70 }
71
72 std::unique_ptr<Expression> clone(Position pos) const override {
73 return std::unique_ptr<Expression>(new IndexExpression(pos, this->base()->clone(),
74 this->index()->clone(),
75 &this->type()));
76 }
77
78 std::string description(OperatorPrecedence) const override;
79
81
82private:
83 IndexExpression(Position pos, std::unique_ptr<Expression> base,
84 std::unique_ptr<Expression> index, const Type* type)
86 , fBase(std::move(base))
87 , fIndex(std::move(index)) {}
88
89 std::unique_ptr<Expression> fBase;
90 std::unique_ptr<Expression> fIndex;
91};
92
93} // namespace SkSL
94
95#endif
SkPoint pos
std::unique_ptr< Expression > clone() const
Expression(Position pos, Kind kind, const Type *type)
const Type & type() const
std::string description() const final
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > base, std::unique_ptr< Expression > index)
const std::unique_ptr< Expression > & base() const
std::unique_ptr< Expression > & base()
static constexpr Kind kIRNodeKind
static std::unique_ptr< Expression > Convert(const Context &context, Position pos, std::unique_ptr< Expression > base, std::unique_ptr< Expression > index)
static const Type & IndexType(const Context &context, const Type &type)
std::unique_ptr< Expression > & index()
IndexExpression(const Context &context, Position pos, std::unique_ptr< Expression > base, std::unique_ptr< Expression > index)
const std::unique_ptr< Expression > & index() const
std::unique_ptr< Expression > clone(Position pos) const override
OperatorPrecedence
Definition: SkSLOperator.h:57
ExpressionKind
Definition: SkSLIRNode.h:62
Definition: ref_ptr.h:256