blob: ca3c7f95b270465b661abf4962e8196b74d27e77 [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
8#ifndef SKSL_VARDECLARATION
9#define SKSL_VARDECLARATION
10
11#include "SkSLExpression.h"
12#include "SkSLStatement.h"
13#include "SkSLVariable.h"
14
15namespace SkSL {
16
17/**
18 * A variable declaration, which may consist of multiple individual variables. For instance
ethannicholasf789b382016-08-03 12:43:36 -070019 * 'int x, y = 1, z[4][2];' is a single VarDeclaration. This declaration would have a base type of
20 * 'int', names ['x', 'y', 'z'], sizes of [[], [], [4, 2]], and values of [null, 1, null].
ethannicholasb3058bd2016-07-01 08:22:01 -070021 */
22struct VarDeclaration : public ProgramElement {
ethannicholasf789b382016-08-03 12:43:36 -070023 VarDeclaration(Position position, const Type* baseType, std::vector<const Variable*> vars,
ethannicholasb3058bd2016-07-01 08:22:01 -070024 std::vector<std::vector<std::unique_ptr<Expression>>> sizes,
25 std::vector<std::unique_ptr<Expression>> values)
ethannicholasf789b382016-08-03 12:43:36 -070026 : INHERITED(position, kVar_Kind)
27 , fBaseType(*baseType)
ethannicholasb3058bd2016-07-01 08:22:01 -070028 , fVars(std::move(vars))
29 , fSizes(std::move(sizes))
30 , fValues(std::move(values)) {}
31
32 std::string description() const override {
33 std::string result = fVars[0]->fModifiers.description();
ethannicholasd598f792016-07-25 10:08:54 -070034 const Type* baseType = &fVars[0]->fType;
ethannicholasb3058bd2016-07-01 08:22:01 -070035 while (baseType->kind() == Type::kArray_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -070036 baseType = &baseType->componentType();
ethannicholasb3058bd2016-07-01 08:22:01 -070037 }
38 result += baseType->description();
39 std::string separator = " ";
40 for (size_t i = 0; i < fVars.size(); i++) {
41 result += separator;
42 separator = ", ";
43 result += fVars[i]->fName;
44 for (size_t j = 0; j < fSizes[i].size(); j++) {
45 if (fSizes[i][j]) {
46 result += "[" + fSizes[i][j]->description() + "]";
47 } else {
48 result += "[]";
49 }
50 }
51 if (fValues[i]) {
52 result += " = " + fValues[i]->description();
53 }
54 }
55 result += ";";
56 return result;
57 }
58
ethannicholasf789b382016-08-03 12:43:36 -070059 const Type& fBaseType;
ethannicholasd598f792016-07-25 10:08:54 -070060 const std::vector<const Variable*> fVars;
ethannicholasb3058bd2016-07-01 08:22:01 -070061 const std::vector<std::vector<std::unique_ptr<Expression>>> fSizes;
62 const std::vector<std::unique_ptr<Expression>> fValues;
63
64 typedef ProgramElement INHERITED;
65};
66
67} // namespace
68
69#endif