xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLSymbol.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 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/SkSLSymbol.h"
9 
10 #include "src/sksl/ir/SkSLExpression.h"
11 #include "src/sksl/ir/SkSLFieldAccess.h"
12 #include "src/sksl/ir/SkSLFieldSymbol.h"
13 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
14 #include "src/sksl/ir/SkSLFunctionReference.h"
15 #include "src/sksl/ir/SkSLType.h"
16 #include "src/sksl/ir/SkSLTypeReference.h"
17 #include "src/sksl/ir/SkSLVariable.h"
18 #include "src/sksl/ir/SkSLVariableReference.h"
19 
20 #include <utility>
21 
22 namespace SkSL {
23 
instantiate(const Context & context,Position pos) const24 std::unique_ptr<Expression> Symbol::instantiate(const Context& context, Position pos) const {
25     switch (this->kind()) {
26         case Symbol::Kind::kFunctionDeclaration:
27             return std::make_unique<FunctionReference>(
28                     context, pos, &this->as<FunctionDeclaration>());
29 
30         case Symbol::Kind::kVariable: {
31             const Variable* var = &this->as<Variable>();
32             // default to kRead_RefKind; this will be corrected later if the variable is written to
33             return VariableReference::Make(pos, var, VariableReference::RefKind::kRead);
34         }
35         case Symbol::Kind::kField: {
36             const FieldSymbol* field = &this->as<FieldSymbol>();
37             auto base = VariableReference::Make(
38                     pos, &field->owner(), VariableReference::RefKind::kRead);
39             return FieldAccess::Make(context,
40                                      pos,
41                                      std::move(base),
42                                      field->fieldIndex(),
43                                      FieldAccess::OwnerKind::kAnonymousInterfaceBlock);
44         }
45         case Symbol::Kind::kType:
46             return TypeReference::Convert(context, pos, &this->as<Type>());
47 
48         default:
49             SkDEBUGFAILF("unsupported symbol type %d\n", fKind);
50             return nullptr;
51     }
52 }
53 
54 }  // namespace SkSL
55