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/SkSLChildCall.h"
10
11 namespace SkSL {
12
hasProperty(Property property) const13 bool ChildCall::hasProperty(Property property) const {
14 for (const auto& arg : this->arguments()) {
15 if (arg->hasProperty(property)) {
16 return true;
17 }
18 }
19 return false;
20 }
21
clone() const22 std::unique_ptr<Expression> ChildCall::clone() const {
23 return std::make_unique<ChildCall>(fLine, &this->type(), &this->child(),
24 this->arguments().clone());
25 }
26
description() const27 std::string ChildCall::description() const {
28 std::string result = std::string(this->child().name()) + ".eval(";
29 std::string separator;
30 for (const std::unique_ptr<Expression>& arg : this->arguments()) {
31 result += separator;
32 result += arg->description();
33 separator = ", ";
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 = [&]() -> SkSTArray<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 (size_t 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,int line,const Type * returnType,const Variable & child,ExpressionArray arguments)66 std::unique_ptr<Expression> ChildCall::Make(const Context& context,
67 int line,
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>(line, returnType, &child, std::move(arguments));
73 }
74
75 } // namespace SkSL
76