• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/sksl/ir/SkSLExpression.h"
12 #include "src/sksl/ir/SkSLProgramElement.h"
13 #include "src/sksl/ir/SkSLStatement.h"
14 #include "src/sksl/ir/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 struct VarDeclaration : public Statement {
VarDeclarationVarDeclaration24     VarDeclaration(const Variable* var,
25                    std::vector<std::unique_ptr<Expression>> sizes,
26                    std::unique_ptr<Expression> value)
27     : INHERITED(var->fOffset, Statement::kVarDeclaration_Kind)
28     , fVar(var)
29     , fSizes(std::move(sizes))
30     , fValue(std::move(value)) {}
31 
cloneVarDeclaration32     std::unique_ptr<Statement> clone() const override {
33         std::vector<std::unique_ptr<Expression>> sizesClone;
34         for (const auto& s : fSizes) {
35             if (s) {
36                 sizesClone.push_back(s->clone());
37             } else {
38                 sizesClone.push_back(nullptr);
39             }
40         }
41         return std::unique_ptr<Statement>(new VarDeclaration(fVar, std::move(sizesClone),
42                                                              fValue ? fValue->clone() : nullptr));
43     }
44 
descriptionVarDeclaration45     String description() const override {
46         String result = fVar->fName;
47         for (const auto& size : fSizes) {
48             if (size) {
49                 result += "[" + size->description() + "]";
50             } else {
51                 result += "[]";
52             }
53         }
54         if (fValue) {
55             result += " = " + fValue->description();
56         }
57         return result;
58     }
59 
60     const Variable* fVar;
61     std::vector<std::unique_ptr<Expression>> fSizes;
62     std::unique_ptr<Expression> fValue;
63 
64     typedef Statement INHERITED;
65 };
66 
67 /**
68  * A variable declaration statement, which may consist of one or more individual variables.
69  */
70 struct VarDeclarations : public ProgramElement {
VarDeclarationsVarDeclarations71     VarDeclarations(int offset, const Type* baseType,
72                     std::vector<std::unique_ptr<VarDeclaration>> vars)
73     : INHERITED(offset, kVar_Kind)
74     , fBaseType(*baseType) {
75         for (auto& var : vars) {
76             fVars.push_back(std::unique_ptr<Statement>(var.release()));
77         }
78     }
79 
cloneVarDeclarations80     std::unique_ptr<ProgramElement> clone() const override {
81         std::vector<std::unique_ptr<VarDeclaration>> cloned;
82         for (const auto& v : fVars) {
83             cloned.push_back(std::unique_ptr<VarDeclaration>(
84                                                            (VarDeclaration*) v->clone().release()));
85         }
86         return std::unique_ptr<ProgramElement>(new VarDeclarations(fOffset, &fBaseType,
87                                                                      std::move(cloned)));
88     }
89 
descriptionVarDeclarations90     String description() const override {
91         if (!fVars.size()) {
92             return String();
93         }
94         String result = ((VarDeclaration&) *fVars[0]).fVar->fModifiers.description() +
95                 fBaseType.description() + " ";
96         String separator;
97         for (const auto& var : fVars) {
98             result += separator;
99             separator = ", ";
100             result += var->description();
101         }
102         return result;
103     }
104 
105     const Type& fBaseType;
106     // this *should* be a vector of unique_ptr<VarDeclaration>, but it significantly simplifies the
107     // CFG to only have to worry about unique_ptr<Statement>
108     std::vector<std::unique_ptr<Statement>> fVars;
109 
110     typedef ProgramElement INHERITED;
111 };
112 
113 } // namespace
114 
115 #endif
116