OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SKSL_ASTVARDECLARATION | 8 #ifndef SKSL_ASTVARDECLARATIONS |
9 #define SKSL_ASTVARDECLARATION | 9 #define SKSL_ASTVARDECLARATIONS |
10 | 10 |
11 #include "SkSLASTDeclaration.h" | 11 #include "SkSLASTDeclaration.h" |
12 #include "SkSLASTModifiers.h" | 12 #include "SkSLASTModifiers.h" |
13 #include "SkSLASTStatement.h" | 13 #include "SkSLASTStatement.h" |
14 #include "SkSLASTType.h" | 14 #include "SkSLASTType.h" |
15 #include "../SkSLUtil.h" | 15 #include "../SkSLUtil.h" |
16 | 16 |
17 namespace SkSL { | 17 namespace SkSL { |
18 | 18 |
19 /** | 19 /** |
20 * A variable declaration, which may consist of multiple individual variables. F
or instance | 20 * A single variable declaration within a var declaration statement. For instanc
e, the statement |
21 * 'int x, y = 1, z[4][2]' is a single ASTVarDeclaration. This declaration would
have a type of | 21 * 'int x = 2, y[3];' is an ASTVarDeclarations statement containing two individu
al ASTVarDeclaration |
22 * 'int', names ['x', 'y', 'z'], sizes of [[], [], [4, 2]], and values of [null,
1, null]. | 22 * instances. |
23 */ | 23 */ |
24 struct ASTVarDeclaration : public ASTDeclaration { | 24 struct ASTVarDeclaration { |
25 ASTVarDeclaration(ASTModifiers modifiers, | 25 ASTVarDeclaration(const std::string name, |
26 std::unique_ptr<ASTType> type, | 26 std::vector<std::unique_ptr<ASTExpression>> sizes, |
27 std::vector<std::string> names, | 27 std::unique_ptr<ASTExpression> value) |
28 std::vector<std::vector<std::unique_ptr<ASTExpression>>> s
izes, | 28 : fName(name) |
29 std::vector<std::unique_ptr<ASTExpression>> values) | 29 , fSizes(std::move(sizes)) |
| 30 , fValue(std::move(value)) {} |
| 31 |
| 32 std::string description() const { |
| 33 std::string result = fName; |
| 34 for (const auto& size : fSizes) { |
| 35 if (size) { |
| 36 result += "[" + size->description() + "]"; |
| 37 } else { |
| 38 result += "[]"; |
| 39 } |
| 40 } |
| 41 if (fValue) { |
| 42 result += " = " + fValue->description(); |
| 43 } |
| 44 return result; |
| 45 } |
| 46 |
| 47 const std::string fName; |
| 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) |
30 : INHERITED(type->fPosition, kVar_Kind) | 63 : INHERITED(type->fPosition, kVar_Kind) |
31 , fModifiers(modifiers) | 64 , fModifiers(modifiers) |
32 , fType(std::move(type)) | 65 , fType(std::move(type)) |
33 , fNames(std::move(names)) | 66 , fVars(std::move(vars)) {} |
34 , fSizes(std::move(sizes)) | |
35 , fValues(std::move(values)) { | |
36 ASSERT(fNames.size() == fValues.size()); | |
37 } | |
38 | 67 |
39 std::string description() const override { | 68 std::string description() const override { |
40 std::string result = fModifiers.description() + fType->description() + "
"; | 69 std::string result = fModifiers.description() + fType->description() + "
"; |
41 std::string separator = ""; | 70 std::string separator = ""; |
42 for (size_t i = 0; i < fNames.size(); i++) { | 71 for (const auto& var : fVars) { |
43 result += separator; | 72 result += separator; |
44 separator = ", "; | 73 separator = ", "; |
45 result += fNames[i]; | 74 result += var.description(); |
46 for (size_t j = 0; j < fSizes[i].size(); j++) { | |
47 if (fSizes[i][j]) { | |
48 result += "[" + fSizes[i][j]->description() + "]"; | |
49 } else { | |
50 result += "[]"; | |
51 } | |
52 } | |
53 if (fValues[i]) { | |
54 result += " = " + fValues[i]->description(); | |
55 } | |
56 } | 75 } |
57 return result; | 76 return result; |
58 } | 77 } |
59 | 78 |
60 const ASTModifiers fModifiers; | 79 const ASTModifiers fModifiers; |
61 const std::unique_ptr<ASTType> fType; | 80 const std::unique_ptr<ASTType> fType; |
62 const std::vector<std::string> fNames; | 81 const std::vector<ASTVarDeclaration> fVars; |
63 const std::vector<std::vector<std::unique_ptr<ASTExpression>>> fSizes; | |
64 const std::vector<std::unique_ptr<ASTExpression>> fValues; | |
65 | 82 |
66 typedef ASTDeclaration INHERITED; | 83 typedef ASTDeclaration INHERITED; |
67 }; | 84 }; |
68 | 85 |
69 } // namespace | 86 } // namespace |
70 | 87 |
71 #endif | 88 #endif |
OLD | NEW |