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 |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame^] | 19 | * 'int x, y = 1, z[4][2];' is a single VarDeclaration. This declaration would have a base type of |
| 20 | * 'int', names ['x', 'y', 'z'], sizes of [[], [], [4, 2]], and values of [null, 1, null]. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | */ |
| 22 | struct VarDeclaration : public ProgramElement { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame^] | 23 | VarDeclaration(Position position, const Type* baseType, std::vector<const Variable*> vars, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | std::vector<std::vector<std::unique_ptr<Expression>>> sizes, |
| 25 | std::vector<std::unique_ptr<Expression>> values) |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame^] | 26 | : INHERITED(position, kVar_Kind) |
| 27 | , fBaseType(*baseType) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | , fVars(std::move(vars)) |
| 29 | , fSizes(std::move(sizes)) |
| 30 | , fValues(std::move(values)) {} |
| 31 | |
| 32 | std::string description() const override { |
| 33 | std::string result = fVars[0]->fModifiers.description(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 34 | const Type* baseType = &fVars[0]->fType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 35 | while (baseType->kind() == Type::kArray_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 36 | baseType = &baseType->componentType(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 37 | } |
| 38 | result += baseType->description(); |
| 39 | std::string separator = " "; |
| 40 | for (size_t i = 0; i < fVars.size(); i++) { |
| 41 | result += separator; |
| 42 | separator = ", "; |
| 43 | result += fVars[i]->fName; |
| 44 | for (size_t j = 0; j < fSizes[i].size(); j++) { |
| 45 | if (fSizes[i][j]) { |
| 46 | result += "[" + fSizes[i][j]->description() + "]"; |
| 47 | } else { |
| 48 | result += "[]"; |
| 49 | } |
| 50 | } |
| 51 | if (fValues[i]) { |
| 52 | result += " = " + fValues[i]->description(); |
| 53 | } |
| 54 | } |
| 55 | result += ";"; |
| 56 | return result; |
| 57 | } |
| 58 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame^] | 59 | const Type& fBaseType; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 60 | const std::vector<const Variable*> fVars; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 61 | const std::vector<std::vector<std::unique_ptr<Expression>>> fSizes; |
| 62 | const std::vector<std::unique_ptr<Expression>> fValues; |
| 63 | |
| 64 | typedef ProgramElement INHERITED; |
| 65 | }; |
| 66 | |
| 67 | } // namespace |
| 68 | |
| 69 | #endif |