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_VARDECLARATIONS |
| 9 | #define SKSL_VARDECLARATIONS |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 10 | |
| 11 | #include "SkSLExpression.h" |
| 12 | #include "SkSLStatement.h" |
| 13 | #include "SkSLVariable.h" |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
| 17 | /** |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 18 | * A single variable declaration within a var declaration statement. For instance, the statement |
| 19 | * 'int x = 2, y[3];' is a VarDeclarations statement containing two individual VarDeclaration |
| 20 | * instances. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | */ |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 22 | struct VarDeclaration { |
| 23 | VarDeclaration(const Variable* var, |
| 24 | std::vector<std::unique_ptr<Expression>> sizes, |
| 25 | std::unique_ptr<Expression> value) |
| 26 | : fVar(var) |
| 27 | , fSizes(std::move(sizes)) |
| 28 | , fValue(std::move(value)) {} |
| 29 | |
| 30 | std::string description() const { |
| 31 | std::string result = fVar->fName; |
| 32 | for (const auto& size : fSizes) { |
| 33 | if (size) { |
| 34 | result += "[" + size->description() + "]"; |
| 35 | } else { |
| 36 | result += "[]"; |
| 37 | } |
| 38 | } |
| 39 | if (fValue) { |
| 40 | result += " = " + fValue->description(); |
| 41 | } |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | const Variable* fVar; |
| 46 | std::vector<std::unique_ptr<Expression>> fSizes; |
| 47 | std::unique_ptr<Expression> fValue; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * A variable declaration statement, which may consist of one or more individual variables. |
| 52 | */ |
| 53 | struct VarDeclarations : public ProgramElement { |
| 54 | VarDeclarations(Position position, const Type* baseType, |
| 55 | std::vector<VarDeclaration> vars) |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 56 | : INHERITED(position, kVar_Kind) |
| 57 | , fBaseType(*baseType) |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 58 | , fVars(std::move(vars)) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | |
| 60 | std::string description() const override { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 61 | if (!fVars.size()) { |
| 62 | return ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 63 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 64 | std::string result = fVars[0].fVar->fModifiers.description() + fBaseType.description() + |
| 65 | " "; |
| 66 | std::string separator = ""; |
| 67 | for (const auto& var : fVars) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 68 | result += separator; |
| 69 | separator = ", "; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 70 | result += var.description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 71 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 72 | return result; |
| 73 | } |
| 74 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 75 | const Type& fBaseType; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 76 | const std::vector<VarDeclaration> fVars; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 77 | |
| 78 | typedef ProgramElement INHERITED; |
| 79 | }; |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | #endif |