• 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_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 struct ASTVarDeclaration {
ASTVarDeclarationASTVarDeclaration25     ASTVarDeclaration(const String name,
26                       std::vector<std::unique_ptr<ASTExpression>> sizes,
27                       std::unique_ptr<ASTExpression> value)
28     : fName(name)
29     , fSizes(std::move(sizes))
30     , fValue(std::move(value)) {}
31 
descriptionASTVarDeclaration32     String description() const {
33         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     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 {
ASTVarDeclarationsASTVarDeclarations60     ASTVarDeclarations(Modifiers modifiers,
61                        std::unique_ptr<ASTType> type,
62                        std::vector<ASTVarDeclaration> vars)
63     : INHERITED(type->fPosition, kVar_Kind)
64     , fModifiers(modifiers)
65     , fType(std::move(type))
66     , fVars(std::move(vars)) {}
67 
descriptionASTVarDeclarations68     String description() const override {
69         String result = fModifiers.description() + fType->description() + " ";
70         String separator;
71         for (const auto& var : fVars) {
72             result += separator;
73             separator = ", ";
74             result += var.description();
75         }
76         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
89