• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static constexpr Kind kExpressionKind = Kind::kFieldAccess;
31 
32     FieldAccess(std::unique_ptr<Expression> base, int fieldIndex,
33                 OwnerKind ownerKind = OwnerKind::kDefault)
34     : INHERITED(base->fOffset, 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                                                std::unique_ptr<Expression> base,
42                                                StringFragment field);
43 
44     // Returns a field-access expression; reports errors via ASSERT.
45     static std::unique_ptr<Expression> Make(const Context& context,
46                                             std::unique_ptr<Expression> base,
47                                             int fieldIndex,
48                                             OwnerKind ownerKind = OwnerKind::kDefault);
49 
base()50     std::unique_ptr<Expression>& base() {
51         return fBase;
52     }
53 
base()54     const std::unique_ptr<Expression>& base() const {
55         return fBase;
56     }
57 
fieldIndex()58     int fieldIndex() const {
59         return fFieldIndex;
60     }
61 
ownerKind()62     OwnerKind ownerKind() const {
63         return fOwnerKind;
64     }
65 
hasProperty(Property property)66     bool hasProperty(Property property) const override {
67         return this->base()->hasProperty(property);
68     }
69 
clone()70     std::unique_ptr<Expression> clone() const override {
71         return std::unique_ptr<Expression>(new FieldAccess(this->base()->clone(),
72                                                            this->fieldIndex(),
73                                                            this->ownerKind()));
74     }
75 
description()76     String description() const override {
77         return this->base()->description() + "." +
78                this->base()->type().fields()[this->fieldIndex()].fName;
79     }
80 
81 private:
82     int fFieldIndex;
83     FieldAccessOwnerKind fOwnerKind;
84     std::unique_ptr<Expression> fBase;
85 
86     using INHERITED = Expression;
87 };
88 
89 }  // namespace SkSL
90 
91 #endif
92