ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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_ASTFUNCTION |
| 9 | #define SKSL_ASTFUNCTION |
| 10 | |
| 11 | #include "SkSLASTBlock.h" |
| 12 | #include "SkSLASTDeclaration.h" |
| 13 | #include "SkSLASTParameter.h" |
| 14 | #include "SkSLASTType.h" |
| 15 | |
| 16 | namespace SkSL { |
| 17 | |
| 18 | /** |
| 19 | * A function declaration or definition. The fBody field will be null for declarations. |
| 20 | */ |
| 21 | struct ASTFunction : public ASTDeclaration { |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame] | 22 | ASTFunction(Position position, std::unique_ptr<ASTType> returnType, std::string name, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 23 | std::vector<std::unique_ptr<ASTParameter>> parameters, |
| 24 | std::unique_ptr<ASTBlock> body) |
| 25 | : INHERITED(position, kFunction_Kind) |
| 26 | , fReturnType(std::move(returnType)) |
| 27 | , fName(std::move(name)) |
| 28 | , fParameters(std::move(parameters)) |
| 29 | , fBody(std::move(body)) {} |
| 30 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame] | 31 | std::string description() const override { |
| 32 | std::string result = fReturnType->description() + " " + fName + "("; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 33 | for (size_t i = 0; i < fParameters.size(); i++) { |
| 34 | if (i > 0) { |
| 35 | result += ", "; |
| 36 | } |
| 37 | result += fParameters[i]->description(); |
| 38 | } |
| 39 | if (fBody) { |
| 40 | result += ") " + fBody->description(); |
| 41 | } else { |
| 42 | result += ");"; |
| 43 | } |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | const std::unique_ptr<ASTType> fReturnType; |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame] | 48 | const std::string fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 49 | const std::vector<std::unique_ptr<ASTParameter>> fParameters; |
| 50 | const std::unique_ptr<ASTBlock> fBody; |
| 51 | |
| 52 | typedef ASTDeclaration INHERITED; |
| 53 | }; |
| 54 | |
| 55 | } // namespace |
| 56 | |
| 57 | #endif |