blob: 066922fb852b7271d7c2bc8329b15c5367938f37 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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
ethannicholas14fe8cc2016-09-07 13:37:16 -07008#ifndef SKSL_ASTVARDECLARATIONS
9#define SKSL_ASTVARDECLARATIONS
ethannicholasb3058bd2016-07-01 08:22:01 -070010
11#include "SkSLASTDeclaration.h"
12#include "SkSLASTModifiers.h"
13#include "SkSLASTStatement.h"
14#include "SkSLASTType.h"
15#include "../SkSLUtil.h"
16
17namespace SkSL {
18
19/**
ethannicholas14fe8cc2016-09-07 13:37:16 -070020 * 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.
ethannicholasb3058bd2016-07-01 08:22:01 -070023 */
ethannicholas14fe8cc2016-09-07 13:37:16 -070024struct ASTVarDeclaration {
Greg Daniel792d0f12016-11-20 14:53:35 +000025 ASTVarDeclaration(const std::string name,
ethannicholas14fe8cc2016-09-07 13:37:16 -070026 std::vector<std::unique_ptr<ASTExpression>> sizes,
27 std::unique_ptr<ASTExpression> value)
28 : fName(name)
ethannicholasb3058bd2016-07-01 08:22:01 -070029 , fSizes(std::move(sizes))
ethannicholas14fe8cc2016-09-07 13:37:16 -070030 , fValue(std::move(value)) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070031
Greg Daniel792d0f12016-11-20 14:53:35 +000032 std::string description() const {
33 std::string result = fName;
ethannicholas14fe8cc2016-09-07 13:37:16 -070034 for (const auto& size : fSizes) {
35 if (size) {
36 result += "[" + size->description() + "]";
37 } else {
38 result += "[]";
ethannicholasb3058bd2016-07-01 08:22:01 -070039 }
ethannicholas14fe8cc2016-09-07 13:37:16 -070040 }
41 if (fValue) {
42 result += " = " + fValue->description();
ethannicholasb3058bd2016-07-01 08:22:01 -070043 }
44 return result;
45 }
46
Greg Daniel792d0f12016-11-20 14:53:35 +000047 std::string fName;
ethannicholas14fe8cc2016-09-07 13:37:16 -070048
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 */
59struct ASTVarDeclarations : public ASTDeclaration {
60 ASTVarDeclarations(ASTModifiers 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
Greg Daniel792d0f12016-11-20 14:53:35 +000068 std::string description() const override {
69 std::string result = fModifiers.description() + fType->description() + " ";
70 std::string separator = "";
ethannicholas14fe8cc2016-09-07 13:37:16 -070071 for (const auto& var : fVars) {
72 result += separator;
73 separator = ", ";
74 result += var.description();
75 }
76 return result;
77 }
78
ethannicholasb3058bd2016-07-01 08:22:01 -070079 const ASTModifiers fModifiers;
80 const std::unique_ptr<ASTType> fType;
ethannicholas14fe8cc2016-09-07 13:37:16 -070081 const std::vector<ASTVarDeclaration> fVars;
ethannicholasb3058bd2016-07-01 08:22:01 -070082
83 typedef ASTDeclaration INHERITED;
84};
85
86} // namespace
87
88#endif