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_FIELDACCESS 9 #define SKSL_FIELDACCESS 10 11 #include "src/sksl/SkSLUtil.h" 12 #include "src/sksl/ir/SkSLExpression.h" 13 14 namespace SkSL { 15 16 enum class FieldAccessOwnerKind : int8_t { 17 kDefault, 18 // this field access is to a field of an anonymous interface block (and thus, the field name 19 // is actually in global scope, so only the field name needs to be written in GLSL) 20 kAnonymousInterfaceBlock 21 }; 22 23 /** 24 * An expression which extracts a field from a struct, as in 'foo.bar'. 25 */ 26 class FieldAccess final : public Expression { 27 public: 28 using OwnerKind = FieldAccessOwnerKind; 29 30 inline static constexpr Kind kExpressionKind = Kind::kFieldAccess; 31 32 FieldAccess(std::unique_ptr<Expression> base, int fieldIndex, 33 OwnerKind ownerKind = OwnerKind::kDefault) 34 : INHERITED(base->fLine, kExpressionKind, base->type().fields()[fieldIndex].fType) 35 , fFieldIndex(fieldIndex) 36 , fOwnerKind(ownerKind) 37 , fBase(std::move(base)) {} 38 39 // Returns a field-access expression; reports errors via the ErrorReporter. 40 static std::unique_ptr<Expression> Convert(const Context& context, 41 SymbolTable& symbolTable, 42 std::unique_ptr<Expression> base, 43 skstd::string_view field); 44 45 // Returns a field-access expression; reports errors via ASSERT. 46 static std::unique_ptr<Expression> Make(const Context& context, 47 std::unique_ptr<Expression> base, 48 int fieldIndex, 49 OwnerKind ownerKind = OwnerKind::kDefault); 50 base()51 std::unique_ptr<Expression>& base() { 52 return fBase; 53 } 54 base()55 const std::unique_ptr<Expression>& base() const { 56 return fBase; 57 } 58 fieldIndex()59 int fieldIndex() const { 60 return fFieldIndex; 61 } 62 ownerKind()63 OwnerKind ownerKind() const { 64 return fOwnerKind; 65 } 66 hasProperty(Property property)67 bool hasProperty(Property property) const override { 68 return this->base()->hasProperty(property); 69 } 70 clone()71 std::unique_ptr<Expression> clone() const override { 72 return std::unique_ptr<Expression>(new FieldAccess(this->base()->clone(), 73 this->fieldIndex(), 74 this->ownerKind())); 75 } 76 description()77 String description() const override { 78 return this->base()->description() + "." + 79 this->base()->type().fields()[this->fieldIndex()].fName; 80 } 81 82 private: 83 int fFieldIndex; 84 FieldAccessOwnerKind fOwnerKind; 85 std::unique_ptr<Expression> fBase; 86 87 using INHERITED = Expression; 88 }; 89 90 } // namespace SkSL 91 92 #endif 93