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
8 #include "src/sksl/ir/SkSLFieldAccess.h"
9
10 #include "include/core/SkSpan.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/SkSLDefines.h"
13 #include "include/private/SkSLSymbol.h"
14 #include "include/private/base/SkTArray.h"
15 #include "include/sksl/SkSLErrorReporter.h"
16 #include "include/sksl/SkSLOperator.h"
17 #include "src/sksl/SkSLAnalysis.h"
18 #include "src/sksl/SkSLBuiltinTypes.h"
19 #include "src/sksl/SkSLConstantFolder.h"
20 #include "src/sksl/SkSLContext.h"
21 #include "src/sksl/ir/SkSLConstructorStruct.h"
22 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
23 #include "src/sksl/ir/SkSLMethodReference.h"
24 #include "src/sksl/ir/SkSLSetting.h"
25 #include "src/sksl/ir/SkSLSymbolTable.h"
26
27 #include <cstddef>
28
29 namespace SkSL {
30
Convert(const Context & context,Position pos,SymbolTable & symbolTable,std::unique_ptr<Expression> base,std::string_view field)31 std::unique_ptr<Expression> FieldAccess::Convert(const Context& context,
32 Position pos,
33 SymbolTable& symbolTable,
34 std::unique_ptr<Expression> base,
35 std::string_view field) {
36 const Type& baseType = base->type();
37 if (baseType.isEffectChild()) {
38 // Turn the field name into a free function name, prefixed with '$':
39 std::string methodName = "$" + std::string(field);
40 const Symbol* result = symbolTable.find(methodName);
41 if (result && result->is<FunctionDeclaration>()) {
42 return std::make_unique<MethodReference>(context, pos, std::move(base),
43 &result->as<FunctionDeclaration>());
44 }
45 context.fErrors->error(pos, "type '" + baseType.displayName() + "' has no method named '" +
46 std::string(field) + "'");
47 return nullptr;
48 }
49 if (baseType.isStruct()) {
50 const std::vector<Type::Field>& fields = baseType.fields();
51 for (size_t i = 0; i < fields.size(); i++) {
52 if (fields[i].fName == field) {
53 return FieldAccess::Make(context, pos, std::move(base), (int) i);
54 }
55 }
56 }
57 if (baseType.matches(*context.fTypes.fSkCaps)) {
58 return Setting::Convert(context, pos, field);
59 }
60
61 context.fErrors->error(pos, "type '" + baseType.displayName() +
62 "' does not have a field named '" + std::string(field) + "'");
63 return nullptr;
64 }
65
extract_field(Position pos,const ConstructorStruct & ctor,int fieldIndex)66 static std::unique_ptr<Expression> extract_field(Position pos,
67 const ConstructorStruct& ctor,
68 int fieldIndex) {
69 // Confirm that the fields that are being removed are side-effect free.
70 const ExpressionArray& args = ctor.arguments();
71 int numFields = args.size();
72 for (int index = 0; index < numFields; ++index) {
73 if (fieldIndex == index) {
74 continue;
75 }
76 if (Analysis::HasSideEffects(*args[index])) {
77 return nullptr;
78 }
79 }
80
81 // Return the desired field.
82 return args[fieldIndex]->clone(pos);
83 }
84
Make(const Context & context,Position pos,std::unique_ptr<Expression> base,int fieldIndex,OwnerKind ownerKind)85 std::unique_ptr<Expression> FieldAccess::Make(const Context& context,
86 Position pos,
87 std::unique_ptr<Expression> base,
88 int fieldIndex,
89 OwnerKind ownerKind) {
90 SkASSERT(base->type().isStruct());
91 SkASSERT(fieldIndex >= 0);
92 SkASSERT(fieldIndex < (int)base->type().fields().size());
93
94 // Replace `knownStruct.field` with the field's value if there are no side-effects involved.
95 const Expression* expr = ConstantFolder::GetConstantValueForVariable(*base);
96 if (expr->is<ConstructorStruct>()) {
97 if (std::unique_ptr<Expression> field = extract_field(pos, expr->as<ConstructorStruct>(),
98 fieldIndex)) {
99 return field;
100 }
101 }
102
103 return std::make_unique<FieldAccess>(pos, std::move(base), fieldIndex, ownerKind);
104 }
105
initialSlot() const106 size_t FieldAccess::initialSlot() const {
107 SkSpan<const Type::Field> fields = this->base()->type().fields();
108 const int fieldIndex = this->fieldIndex();
109
110 size_t slot = 0;
111 for (int index = 0; index < fieldIndex; ++index) {
112 slot += fields[index].fType->slotCount();
113 }
114 return slot;
115 }
116
description(OperatorPrecedence) const117 std::string FieldAccess::description(OperatorPrecedence) const {
118 std::string f = this->base()->description(OperatorPrecedence::kPostfix);
119 if (!f.empty()) {
120 f.push_back('.');
121 }
122 return f + std::string(this->base()->type().fields()[this->fieldIndex()].fName);
123 }
124
125 } // namespace SkSL
126