• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkSLContext.h"
9 #include "src/sksl/ir/SkSLFieldAccess.h"
10 #include "src/sksl/ir/SkSLMethodReference.h"
11 #include "src/sksl/ir/SkSLSetting.h"
12 #include "src/sksl/ir/SkSLSymbolTable.h"
13 #include "src/sksl/ir/SkSLUnresolvedFunction.h"
14 
15 namespace SkSL {
16 
Convert(const Context & context,SymbolTable & symbolTable,std::unique_ptr<Expression> base,skstd::string_view field)17 std::unique_ptr<Expression> FieldAccess::Convert(const Context& context,
18                                                  SymbolTable& symbolTable,
19                                                  std::unique_ptr<Expression> base,
20                                                  skstd::string_view field) {
21     const Type& baseType = base->type();
22     if (baseType.isEffectChild()) {
23         // Turn the field name into a free function name, prefixed with '$':
24         String methodName = String("$") + field;
25         const Symbol* result = symbolTable[methodName];
26         if (result) {
27             switch (result->kind()) {
28                 case Symbol::Kind::kFunctionDeclaration: {
29                     std::vector<const FunctionDeclaration*> f = {
30                             &result->as<FunctionDeclaration>()};
31                     return std::make_unique<MethodReference>(
32                             context, base->fLine, std::move(base), f);
33                 }
34                 case Symbol::Kind::kUnresolvedFunction: {
35                     const UnresolvedFunction& f = result->as<UnresolvedFunction>();
36                     return std::make_unique<MethodReference>(
37                             context, base->fLine, std::move(base), f.functions());
38                 }
39                 default:
40                     break;
41             }
42         }
43         context.fErrors->error(
44                 base->fLine,
45                 "type '" + baseType.displayName() + "' has no method named '" + field + "'");
46         return nullptr;
47     }
48     if (baseType.isStruct()) {
49         const std::vector<Type::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, std::move(base), (int) i);
53             }
54         }
55     }
56     if (baseType == *context.fTypes.fSkCaps) {
57         return Setting::Convert(context, base->fLine, field);
58     }
59 
60     context.fErrors->error(base->fLine, "type '" + baseType.displayName() +
61                                           "' does not have a field named '" + field + "'");
62     return nullptr;
63 }
64 
Make(const Context & context,std::unique_ptr<Expression> base,int fieldIndex,OwnerKind ownerKind)65 std::unique_ptr<Expression> FieldAccess::Make(const Context& context,
66                                               std::unique_ptr<Expression> base,
67                                               int fieldIndex,
68                                               OwnerKind ownerKind) {
69     SkASSERT(base->type().isStruct());
70     SkASSERT(fieldIndex >= 0);
71     SkASSERT(fieldIndex < (int) base->type().fields().size());
72     return std::make_unique<FieldAccess>(std::move(base), fieldIndex, ownerKind);
73 }
74 
75 }  // namespace SkSL
76