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 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 8 | #ifndef SKSL_ASTVARDECLARATIONS |
| 9 | #define SKSL_ASTVARDECLARATIONS |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 10 | |
| 11 | #include "SkSLASTDeclaration.h" |
| 12 | #include "SkSLASTModifiers.h" |
| 13 | #include "SkSLASTStatement.h" |
| 14 | #include "SkSLASTType.h" |
| 15 | #include "../SkSLUtil.h" |
| 16 | |
| 17 | namespace SkSL { |
| 18 | |
| 19 | /** |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 20 | * A single variable declaration within a var declaration statement. For instance, the statement |
| 21 | * 'int x = 2, y[3];' is an ASTVarDeclarations statement containing two individual ASTVarDeclaration |
| 22 | * instances. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 23 | */ |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 24 | struct ASTVarDeclaration { |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 25 | ASTVarDeclaration(const std::string name, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 26 | std::vector<std::unique_ptr<ASTExpression>> sizes, |
| 27 | std::unique_ptr<ASTExpression> value) |
| 28 | : fName(name) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 29 | , fSizes(std::move(sizes)) |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 30 | , fValue(std::move(value)) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 31 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 32 | std::string description() const { |
| 33 | std::string result = fName; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 34 | for (const auto& size : fSizes) { |
| 35 | if (size) { |
| 36 | result += "[" + size->description() + "]"; |
| 37 | } else { |
| 38 | result += "[]"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 40 | } |
| 41 | if (fValue) { |
| 42 | result += " = " + fValue->description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | } |
| 44 | return result; |
| 45 | } |
| 46 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 47 | std::string fName; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 48 | |
| 49 | // array sizes, if any. e.g. 'foo[3][]' has sizes [3, null] |
| 50 | std::vector<std::unique_ptr<ASTExpression>> fSizes; |
| 51 | |
| 52 | // initial value, may be null |
| 53 | std::unique_ptr<ASTExpression> fValue; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * A variable declaration statement, which may consist of one or more individual variables. |
| 58 | */ |
| 59 | struct ASTVarDeclarations : public ASTDeclaration { |
| 60 | ASTVarDeclarations(ASTModifiers modifiers, |
| 61 | std::unique_ptr<ASTType> type, |
| 62 | std::vector<ASTVarDeclaration> vars) |
| 63 | : INHERITED(type->fPosition, kVar_Kind) |
| 64 | , fModifiers(modifiers) |
| 65 | , fType(std::move(type)) |
| 66 | , fVars(std::move(vars)) {} |
| 67 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 68 | std::string description() const override { |
| 69 | std::string result = fModifiers.description() + fType->description() + " "; |
| 70 | std::string separator = ""; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 71 | for (const auto& var : fVars) { |
| 72 | result += separator; |
| 73 | separator = ", "; |
| 74 | result += var.description(); |
| 75 | } |
| 76 | return result; |
| 77 | } |
| 78 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 79 | const ASTModifiers fModifiers; |
| 80 | const std::unique_ptr<ASTType> fType; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 81 | const std::vector<ASTVarDeclaration> fVars; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 82 | |
| 83 | typedef ASTDeclaration INHERITED; |
| 84 | }; |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | #endif |