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 11 #include "include/private/SkSLIRNode.h" 12 #include "include/sksl/SkSLPosition.h" 13 #include "src/sksl/ir/SkSLExpression.h" 14 15 #include <cstdint> 16 #include <memory> 17 #include <string> 18 #include <utility> 19 20 namespace SkSL { 21 22 class Context; 23 class SymbolTable; 24 class Type; 25 enum class OperatorPrecedence : uint8_t; 26 27 /** 28 * An expression which extracts a value from an array or matrix, as in 'm[2]'. 29 */ 30 struct IndexExpression final : public Expression { 31 inline static constexpr Kind kIRNodeKind = Kind::kIndex; 32 IndexExpressionfinal33 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, 41 SymbolTable& symbolTable, 42 Position pos, 43 std::unique_ptr<Expression> base, 44 std::unique_ptr<Expression> index); 45 46 // Returns a simplified index-expression; reports errors via ASSERT. 47 static std::unique_ptr<Expression> Make(const Context& context, 48 Position pos, 49 std::unique_ptr<Expression> base, 50 std::unique_ptr<Expression> index); 51 52 /** 53 * Given a type, returns the type that will result from extracting an array value from it. 54 */ 55 static const Type& IndexType(const Context& context, const Type& type); 56 basefinal57 std::unique_ptr<Expression>& base() { 58 return fBase; 59 } 60 basefinal61 const std::unique_ptr<Expression>& base() const { 62 return fBase; 63 } 64 indexfinal65 std::unique_ptr<Expression>& index() { 66 return fIndex; 67 } 68 indexfinal69 const std::unique_ptr<Expression>& index() const { 70 return fIndex; 71 } 72 clonefinal73 std::unique_ptr<Expression> clone(Position pos) const override { 74 return std::unique_ptr<Expression>(new IndexExpression(pos, this->base()->clone(), 75 this->index()->clone(), 76 &this->type())); 77 } 78 79 std::string description(OperatorPrecedence) const override; 80 81 using INHERITED = Expression; 82 83 private: IndexExpressionfinal84 IndexExpression(Position pos, std::unique_ptr<Expression> base, 85 std::unique_ptr<Expression> index, const Type* type) 86 : INHERITED(pos, Kind::kIndex, type) 87 , fBase(std::move(base)) 88 , fIndex(std::move(index)) {} 89 90 std::unique_ptr<Expression> fBase; 91 std::unique_ptr<Expression> fIndex; 92 }; 93 94 } // namespace SkSL 95 96 #endif 97