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_VARDECLARATION |
| 9 | #define SKSL_VARDECLARATION |
| 10 | |
| 11 | #include "SkSLExpression.h" |
| 12 | #include "SkSLStatement.h" |
| 13 | #include "SkSLVariable.h" |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
| 17 | /** |
| 18 | * A variable declaration, which may consist of multiple individual variables. For instance |
| 19 | * 'int x, y = 1, z[4][2];' is a single VarDeclaration. This declaration would have a type of 'int', |
| 20 | * names ['x', 'y', 'z'], sizes of [[], [], [4, 2]], and values of [null, 1, null]. |
| 21 | */ |
| 22 | struct VarDeclaration : public ProgramElement { |
| 23 | VarDeclaration(Position position, std::vector<std::shared_ptr<Variable>> vars, |
| 24 | std::vector<std::vector<std::unique_ptr<Expression>>> sizes, |
| 25 | std::vector<std::unique_ptr<Expression>> values) |
| 26 | : INHERITED(position, kVar_Kind) |
| 27 | , fVars(std::move(vars)) |
| 28 | , fSizes(std::move(sizes)) |
| 29 | , fValues(std::move(values)) {} |
| 30 | |
| 31 | std::string description() const override { |
| 32 | std::string result = fVars[0]->fModifiers.description(); |
| 33 | std::shared_ptr<Type> baseType = fVars[0]->fType; |
| 34 | while (baseType->kind() == Type::kArray_Kind) { |
| 35 | baseType = baseType->componentType(); |
| 36 | } |
| 37 | result += baseType->description(); |
| 38 | std::string separator = " "; |
| 39 | for (size_t i = 0; i < fVars.size(); i++) { |
| 40 | result += separator; |
| 41 | separator = ", "; |
| 42 | result += fVars[i]->fName; |
| 43 | for (size_t j = 0; j < fSizes[i].size(); j++) { |
| 44 | if (fSizes[i][j]) { |
| 45 | result += "[" + fSizes[i][j]->description() + "]"; |
| 46 | } else { |
| 47 | result += "[]"; |
| 48 | } |
| 49 | } |
| 50 | if (fValues[i]) { |
| 51 | result += " = " + fValues[i]->description(); |
| 52 | } |
| 53 | } |
| 54 | result += ";"; |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | const std::vector<std::shared_ptr<Variable>> fVars; |
| 59 | const std::vector<std::vector<std::unique_ptr<Expression>>> fSizes; |
| 60 | const std::vector<std::unique_ptr<Expression>> fValues; |
| 61 | |
| 62 | typedef ProgramElement INHERITED; |
| 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | #endif |