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 "src/sksl/SkSLContext.h" 12 #include "src/sksl/SkSLUtil.h" 13 #include "src/sksl/ir/SkSLExpression.h" 14 15 namespace SkSL { 16 17 /** 18 * An expression which extracts a value from an array or matrix, as in 'm[2]'. 19 */ 20 struct IndexExpression final : public Expression { 21 inline static constexpr Kind kExpressionKind = Kind::kIndex; 22 IndexExpressionfinal23 IndexExpression(const Context& context, std::unique_ptr<Expression> base, 24 std::unique_ptr<Expression> index) 25 : INHERITED(base->fLine, kExpressionKind, &IndexType(context, base->type())) 26 , fBase(std::move(base)) 27 , fIndex(std::move(index)) {} 28 29 // Returns a simplified index-expression; reports errors via the ErrorReporter. 30 static std::unique_ptr<Expression> Convert(const Context& context, 31 SymbolTable& symbolTable, 32 std::unique_ptr<Expression> base, 33 std::unique_ptr<Expression> index); 34 35 // Returns a simplified index-expression; reports errors via ASSERT. 36 static std::unique_ptr<Expression> Make(const Context& context, 37 std::unique_ptr<Expression> base, 38 std::unique_ptr<Expression> index); 39 40 /** 41 * Given a type, returns the type that will result from extracting an array value from it. 42 */ 43 static const Type& IndexType(const Context& context, const Type& type); 44 basefinal45 std::unique_ptr<Expression>& base() { 46 return fBase; 47 } 48 basefinal49 const std::unique_ptr<Expression>& base() const { 50 return fBase; 51 } 52 indexfinal53 std::unique_ptr<Expression>& index() { 54 return fIndex; 55 } 56 indexfinal57 const std::unique_ptr<Expression>& index() const { 58 return fIndex; 59 } 60 hasPropertyfinal61 bool hasProperty(Property property) const override { 62 return this->base()->hasProperty(property) || this->index()->hasProperty(property); 63 } 64 clonefinal65 std::unique_ptr<Expression> clone() const override { 66 return std::unique_ptr<Expression>(new IndexExpression(this->base()->clone(), 67 this->index()->clone(), 68 &this->type())); 69 } 70 descriptionfinal71 String description() const override { 72 return this->base()->description() + "[" + this->index()->description() + "]"; 73 } 74 75 using INHERITED = Expression; 76 77 private: IndexExpressionfinal78 IndexExpression(std::unique_ptr<Expression> base, std::unique_ptr<Expression> index, 79 const Type* type) 80 : INHERITED(base->fLine, Kind::kIndex, type) 81 , fBase(std::move(base)) 82 , fIndex(std::move(index)) {} 83 84 std::unique_ptr<Expression> fBase; 85 std::unique_ptr<Expression> fIndex; 86 }; 87 88 } // namespace SkSL 89 90 #endif 91