Flutter Engine
The Flutter Engine
SkSLFieldAccess.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
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
9
10#include "include/core/SkSpan.h"
25#include "src/sksl/ir/SkSLSymbolTable.h" // IWYU pragma: keep
26
27#include <cstddef>
28
29namespace SkSL {
30
31std::unique_ptr<Expression> FieldAccess::Convert(const Context& context,
33 std::unique_ptr<Expression> base,
34 std::string_view field) {
35 const Type& baseType = base->type();
36 if (baseType.isEffectChild()) {
37 // Turn the field name into a free function name, prefixed with '$':
38 std::string methodName = "$" + std::string(field);
39 const Symbol* result = context.fSymbolTable->find(methodName);
40 if (result && result->is<FunctionDeclaration>()) {
41 return std::make_unique<MethodReference>(context, pos, std::move(base),
43 }
44 context.fErrors->error(pos, "type '" + baseType.displayName() + "' has no method named '" +
45 std::string(field) + "'");
46 return nullptr;
47 }
48 if (baseType.isStruct()) {
49 SkSpan<const Field> fields = baseType.fields();
50 for (size_t i = 0; i < fields.size(); i++) {
51 if (fields[i].fName == field) {
52 return FieldAccess::Make(context, pos, std::move(base), (int)i);
53 }
54 }
55 }
56 if (baseType.matches(*context.fTypes.fSkCaps)) {
57 return Setting::Convert(context, pos, field);
58 }
59
60 context.fErrors->error(pos, "type '" + baseType.displayName() +
61 "' does not have a field named '" + std::string(field) + "'");
62 return nullptr;
63}
64
65static std::unique_ptr<Expression> extract_field(Position pos,
66 const ConstructorStruct& ctor,
67 int fieldIndex) {
68 // Confirm that the fields that are being removed are side-effect free.
69 const ExpressionArray& args = ctor.arguments();
70 int numFields = args.size();
71 for (int index = 0; index < numFields; ++index) {
72 if (fieldIndex == index) {
73 continue;
74 }
75 if (Analysis::HasSideEffects(*args[index])) {
76 return nullptr;
77 }
78 }
79
80 // Return the desired field.
81 return args[fieldIndex]->clone(pos);
82}
83
84std::unique_ptr<Expression> FieldAccess::Make(const Context& context,
86 std::unique_ptr<Expression> base,
87 int fieldIndex,
88 OwnerKind ownerKind) {
89 SkASSERT(base->type().isStruct());
90 SkASSERT(fieldIndex >= 0);
91 SkASSERT(fieldIndex < (int)base->type().fields().size());
92
93 // Replace `knownStruct.field` with the field's value if there are no side-effects involved.
95 if (expr->is<ConstructorStruct>()) {
96 if (std::unique_ptr<Expression> field = extract_field(pos, expr->as<ConstructorStruct>(),
97 fieldIndex)) {
98 return field;
99 }
100 }
101
102 return std::make_unique<FieldAccess>(pos, std::move(base), fieldIndex, ownerKind);
103}
104
106 SkSpan<const Field> fields = this->base()->type().fields();
107 const int fieldIndex = this->fieldIndex();
108
109 size_t slot = 0;
110 for (int index = 0; index < fieldIndex; ++index) {
111 slot += fields[index].fType->slotCount();
112 }
113 return slot;
114}
115
117 std::string f = this->base()->description(OperatorPrecedence::kPostfix);
118 if (!f.empty()) {
119 f.push_back('.');
120 }
121 return f + std::string(this->base()->type().fields()[this->fieldIndex()].fName);
122}
123
124} // namespace SkSL
const char * fName
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
const std::unique_ptr< Type > fSkCaps
static const Expression * GetConstantValueForVariable(const Expression &value)
const BuiltinTypes & fTypes
Definition: SkSLContext.h:30
ErrorReporter * fErrors
Definition: SkSLContext.h:36
SymbolTable * fSymbolTable
Definition: SkSLContext.h:48
void error(Position position, std::string_view msg)
const Type & type() const
std::string description() const final
int fieldIndex() const
static std::unique_ptr< Expression > Convert(const Context &context, Position pos, std::unique_ptr< Expression > base, std::string_view field)
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > base, int fieldIndex, OwnerKind ownerKind=OwnerKind::kDefault)
std::unique_ptr< Expression > & base()
OwnerKind ownerKind() const
size_t initialSlot() const
bool is() const
Definition: SkSLIRNode.h:124
const T & as() const
Definition: SkSLIRNode.h:133
ExpressionArray & arguments()
static std::unique_ptr< Expression > Convert(const Context &context, Position pos, const std::string_view &name)
Definition: SkSLSetting.cpp:63
const Symbol * find(std::string_view name) const
bool isEffectChild() const
Definition: SkSLType.h:550
virtual SkSpan< const Field > fields() const
Definition: SkSLType.h:469
bool matches(const Type &other) const
Definition: SkSLType.h:269
std::string displayName() const
Definition: SkSLType.h:234
virtual bool isStruct() const
Definition: SkSLType.h:540
constexpr size_t size() const
Definition: SkSpan_impl.h:95
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
bool HasSideEffects(const Expression &expr)
static std::unique_ptr< Expression > extract_field(Position pos, const ConstructorStruct &ctor, int fieldIndex)
FieldAccessOwnerKind
OperatorPrecedence
Definition: SkSLOperator.h:57