| 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 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 | #ifndef SKSL_FUNCTIONCALL |
| 9 | #define SKSL_FUNCTIONCALL |
| 10 | |
| 11 | #include "src/sksl/ir/SkSLExpression.h" |
| 12 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | /** |
| 17 | * A function invocation. |
| 18 | */ |
| 19 | struct FunctionCall : public Expression { |
| 20 | FunctionCall(int offset, const Type& type, const FunctionDeclaration& function, |
| 21 | std::vector<std::unique_ptr<Expression>> arguments) |
| 22 | : INHERITED(offset, kFunctionCall_Kind, type) |
| 23 | , fFunction(std::move(function)) |
| 24 | , fArguments(std::move(arguments)) { |
| 25 | ++fFunction.fCallCount; |
| 26 | } |
| 27 | |
| 28 | ~FunctionCall() override { |
| 29 | --fFunction.fCallCount; |
| 30 | } |
| 31 | |
| 32 | bool hasProperty(Property property) const override { |
| 33 | if (property == Property::kSideEffects && (fFunction.fModifiers.fFlags & |
| 34 | Modifiers::kHasSideEffects_Flag)) { |
| 35 | return true; |
| 36 | } |
| 37 | for (const auto& arg : fArguments) { |
| 38 | if (arg->hasProperty(property)) { |
| 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | int nodeCount() const override { |
| 46 | int result = 1; |
| 47 | for (const auto& a : fArguments) { |
| 48 | result += a->nodeCount(); |
| 49 | } |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | std::unique_ptr<Expression> clone() const override { |
| 54 | std::vector<std::unique_ptr<Expression>> cloned; |
| 55 | for (const auto& arg : fArguments) { |
| 56 | cloned.push_back(arg->clone()); |
| 57 | } |
| 58 | return std::unique_ptr<Expression>(new FunctionCall(fOffset, fType, fFunction, |
| 59 | std::move(cloned))); |
| 60 | } |
| 61 | |
| 62 | String description() const override { |
| 63 | String result = String(fFunction.fName) + "(" ; |
| 64 | String separator; |
| 65 | for (size_t i = 0; i < fArguments.size(); i++) { |
| 66 | result += separator; |
| 67 | result += fArguments[i]->description(); |
| 68 | separator = ", " ; |
| 69 | } |
| 70 | result += ")" ; |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | const FunctionDeclaration& fFunction; |
| 75 | std::vector<std::unique_ptr<Expression>> fArguments; |
| 76 | |
| 77 | typedef Expression INHERITED; |
| 78 | }; |
| 79 | |
| 80 | } // namespace SkSL |
| 81 | |
| 82 | #endif |
| 83 | |