aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/ir/SkSLVarDeclaration.h
blob: ca3c7f95b270465b661abf4962e8196b74d27e77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
 
#ifndef SKSL_VARDECLARATION
#define SKSL_VARDECLARATION

#include "SkSLExpression.h"
#include "SkSLStatement.h"
#include "SkSLVariable.h"

namespace SkSL {

/**
 * A variable declaration, which may consist of multiple individual variables. For instance
 * 'int x, y = 1, z[4][2];' is a single VarDeclaration. This declaration would have a base type of 
 * 'int', names ['x', 'y', 'z'], sizes of [[], [], [4, 2]], and values of [null, 1, null].
 */
struct VarDeclaration : public ProgramElement {
    VarDeclaration(Position position, const Type* baseType, std::vector<const Variable*> vars,
                   std::vector<std::vector<std::unique_ptr<Expression>>> sizes,
                   std::vector<std::unique_ptr<Expression>> values)
    : INHERITED(position, kVar_Kind)
    , fBaseType(*baseType)
    , fVars(std::move(vars))
    , fSizes(std::move(sizes))
    , fValues(std::move(values)) {}

    std::string description() const override {
        std::string result = fVars[0]->fModifiers.description();
        const Type* baseType = &fVars[0]->fType;
        while (baseType->kind() == Type::kArray_Kind) {
            baseType = &baseType->componentType();
        }
        result += baseType->description();
        std::string separator = " ";
        for (size_t i = 0; i < fVars.size(); i++) {
            result += separator;
            separator = ", ";
            result += fVars[i]->fName;
            for (size_t j = 0; j < fSizes[i].size(); j++) {
                if (fSizes[i][j]) {
                    result += "[" + fSizes[i][j]->description() + "]";
                } else {
                    result += "[]";
                }
            }
            if (fValues[i]) {
                result += " = " + fValues[i]->description();
            }
        }
        result += ";";
        return result;
    }

    const Type& fBaseType;
    const std::vector<const Variable*> fVars;
    const std::vector<std::vector<std::unique_ptr<Expression>>> fSizes;
    const std::vector<std::unique_ptr<Expression>> fValues;

    typedef ProgramElement INHERITED;
};

} // namespace

#endif