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