Line data Source code
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_ASTVARDECLARATIONS
9 : #define SKSL_ASTVARDECLARATIONS
10 :
11 : #include "SkSLASTDeclaration.h"
12 : #include "SkSLASTStatement.h"
13 : #include "SkSLASTType.h"
14 : #include "../SkSLUtil.h"
15 : #include "../ir/SkSLModifiers.h"
16 :
17 : namespace SkSL {
18 :
19 : /**
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.
23 : */
24 0 : struct ASTVarDeclaration {
25 0 : ASTVarDeclaration(const String name,
26 : std::vector<std::unique_ptr<ASTExpression>> sizes,
27 : std::unique_ptr<ASTExpression> value)
28 0 : : fName(name)
29 0 : , fSizes(std::move(sizes))
30 0 : , fValue(std::move(value)) {}
31 :
32 0 : String description() const {
33 0 : String result = fName;
34 0 : for (const auto& size : fSizes) {
35 0 : if (size) {
36 0 : result += "[" + size->description() + "]";
37 : } else {
38 0 : result += "[]";
39 : }
40 : }
41 0 : if (fValue) {
42 0 : result += " = " + fValue->description();
43 : }
44 0 : return result;
45 : }
46 :
47 : 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 0 : struct ASTVarDeclarations : public ASTDeclaration {
60 0 : ASTVarDeclarations(Modifiers modifiers,
61 : std::unique_ptr<ASTType> type,
62 : std::vector<ASTVarDeclaration> vars)
63 0 : : INHERITED(type->fPosition, kVar_Kind)
64 : , fModifiers(modifiers)
65 0 : , fType(std::move(type))
66 0 : , fVars(std::move(vars)) {}
67 :
68 0 : String description() const override {
69 0 : String result = fModifiers.description() + fType->description() + " ";
70 0 : String separator;
71 0 : for (const auto& var : fVars) {
72 0 : result += separator;
73 0 : separator = ", ";
74 0 : result += var.description();
75 : }
76 0 : return result;
77 : }
78 :
79 : const Modifiers fModifiers;
80 : const std::unique_ptr<ASTType> fType;
81 : const std::vector<ASTVarDeclaration> fVars;
82 :
83 : typedef ASTDeclaration INHERITED;
84 : };
85 :
86 : } // namespace
87 :
88 : #endif
|