xref: /aosp_15_r20/external/skia/src/sksl/ir/SkSLChildCall.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkSLChildCall.h"
9 
10 #include "include/core/SkTypes.h"
11 #include "include/private/base/SkTArray.h"
12 #include "src/sksl/SkSLBuiltinTypes.h"
13 #include "src/sksl/SkSLContext.h"
14 #include "src/sksl/SkSLOperator.h"
15 #include "src/sksl/SkSLString.h"
16 #include "src/sksl/ir/SkSLType.h"
17 #include "src/sksl/ir/SkSLVariable.h"
18 
19 using namespace skia_private;
20 
21 namespace SkSL {
22 
clone(Position pos) const23 std::unique_ptr<Expression> ChildCall::clone(Position pos) const {
24     return std::make_unique<ChildCall>(pos, &this->type(), &this->child(),
25                                        this->arguments().clone());
26 }
27 
description(OperatorPrecedence) const28 std::string ChildCall::description(OperatorPrecedence) const {
29     std::string result = std::string(this->child().name()) + ".eval(";
30     auto separator = SkSL::String::Separator();
31     for (const std::unique_ptr<Expression>& arg : this->arguments()) {
32         result += separator();
33         result += arg->description(OperatorPrecedence::kSequence);
34     }
35     result += ")";
36     return result;
37 }
38 
call_signature_is_valid(const Context & context,const Variable & child,const ExpressionArray & arguments)39 [[maybe_unused]] static bool call_signature_is_valid(const Context& context,
40                                                      const Variable& child,
41                                                      const ExpressionArray& arguments) {
42     const Type* half4 = context.fTypes.fHalf4.get();
43     const Type* float2 = context.fTypes.fFloat2.get();
44 
45     auto params = [&]() -> STArray<2, const Type*> {
46         switch (child.type().typeKind()) {
47             case Type::TypeKind::kBlender:     return { half4, half4 };
48             case Type::TypeKind::kColorFilter: return { half4 };
49             case Type::TypeKind::kShader:      return { float2 };
50             default:
51                 SkUNREACHABLE;
52         }
53     }();
54 
55     if (params.size() != arguments.size()) {
56         return false;
57     }
58     for (int i = 0; i < arguments.size(); i++) {
59         if (!arguments[i]->type().matches(*params[i])) {
60             return false;
61         }
62     }
63     return true;
64 }
65 
Make(const Context & context,Position pos,const Type * returnType,const Variable & child,ExpressionArray arguments)66 std::unique_ptr<Expression> ChildCall::Make(const Context& context,
67                                             Position pos,
68                                             const Type* returnType,
69                                             const Variable& child,
70                                             ExpressionArray arguments) {
71     SkASSERT(call_signature_is_valid(context, child, arguments));
72     return std::make_unique<ChildCall>(pos, returnType, &child, std::move(arguments));
73 }
74 
75 }  // namespace SkSL
76