blob: 295c0b6997ef97a2e2bf2b7ab33bbe0f90a8791e [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_VARDECLARATIONS
9#define SKSL_VARDECLARATIONS
ethannicholasb3058bd2016-07-01 08:22:01 -070010
11#include "SkSLExpression.h"
12#include "SkSLStatement.h"
13#include "SkSLVariable.h"
14
15namespace SkSL {
16
17/**
ethannicholas14fe8cc2016-09-07 13:37:16 -070018 * A single variable declaration within a var declaration statement. For instance, the statement
19 * 'int x = 2, y[3];' is a VarDeclarations statement containing two individual VarDeclaration
20 * instances.
ethannicholasb3058bd2016-07-01 08:22:01 -070021 */
ethannicholas14fe8cc2016-09-07 13:37:16 -070022struct VarDeclaration {
23 VarDeclaration(const Variable* var,
24 std::vector<std::unique_ptr<Expression>> sizes,
25 std::unique_ptr<Expression> value)
26 : fVar(var)
27 , fSizes(std::move(sizes))
28 , fValue(std::move(value)) {}
29
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050030 SkString description() const {
31 SkString result = fVar->fName;
ethannicholas14fe8cc2016-09-07 13:37:16 -070032 for (const auto& size : fSizes) {
33 if (size) {
34 result += "[" + size->description() + "]";
35 } else {
36 result += "[]";
37 }
38 }
39 if (fValue) {
40 result += " = " + fValue->description();
41 }
42 return result;
43 }
44
45 const Variable* fVar;
46 std::vector<std::unique_ptr<Expression>> fSizes;
47 std::unique_ptr<Expression> fValue;
48};
49
50/**
51 * A variable declaration statement, which may consist of one or more individual variables.
52 */
53struct VarDeclarations : public ProgramElement {
54 VarDeclarations(Position position, const Type* baseType,
55 std::vector<VarDeclaration> vars)
ethannicholasf789b382016-08-03 12:43:36 -070056 : INHERITED(position, kVar_Kind)
57 , fBaseType(*baseType)
ethannicholas14fe8cc2016-09-07 13:37:16 -070058 , fVars(std::move(vars)) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070059
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050060 SkString description() const override {
ethannicholas14fe8cc2016-09-07 13:37:16 -070061 if (!fVars.size()) {
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050062 return SkString();
ethannicholasb3058bd2016-07-01 08:22:01 -070063 }
Ethan Nicholasd8df21a2016-11-17 16:13:37 -050064 SkString result = fVars[0].fVar->fModifiers.description() + fBaseType.description() + " ";
65 SkString separator;
ethannicholas14fe8cc2016-09-07 13:37:16 -070066 for (const auto& var : fVars) {
ethannicholasb3058bd2016-07-01 08:22:01 -070067 result += separator;
68 separator = ", ";
ethannicholas14fe8cc2016-09-07 13:37:16 -070069 result += var.description();
ethannicholasb3058bd2016-07-01 08:22:01 -070070 }
ethannicholasb3058bd2016-07-01 08:22:01 -070071 return result;
72 }
73
ethannicholasf789b382016-08-03 12:43:36 -070074 const Type& fBaseType;
ethannicholas14fe8cc2016-09-07 13:37:16 -070075 const std::vector<VarDeclaration> fVars;
ethannicholasb3058bd2016-07-01 08:22:01 -070076
77 typedef ProgramElement INHERITED;
78};
79
80} // namespace
81
82#endif