aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/ast/SkSLASTVarDeclaration.h
blob: 2dcb9787ead9ce25fa5aea16acbcf83c639987b9 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * 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_ASTVARDECLARATIONS
#define SKSL_ASTVARDECLARATIONS

#include "SkSLASTDeclaration.h"
#include "SkSLASTStatement.h"
#include "SkSLASTType.h"
#include "../SkSLUtil.h"
#include "../ir/SkSLModifiers.h"

namespace SkSL {

/**
 * A single variable declaration within a var declaration statement. For instance, the statement
 * 'int x = 2, y[3];' is an ASTVarDeclarations statement containing two individual ASTVarDeclaration
 * instances.
 */
struct ASTVarDeclaration {
    ASTVarDeclaration(const String name,
                      std::vector<std::unique_ptr<ASTExpression>> sizes,
                      std::unique_ptr<ASTExpression> value)
    : fName(name)
    , fSizes(std::move(sizes))
    , fValue(std::move(value)) {}

    String description() const {
        String result = fName;
        for (const auto& size : fSizes) {
            if (size) {
                result += "[" + size->description() + "]";
            } else {
                result += "[]";
            }
        }
        if (fValue) {
            result += " = " + fValue->description();
        }
        return result;
    }

    String fName;

    // array sizes, if any. e.g. 'foo[3][]' has sizes [3, null]
    std::vector<std::unique_ptr<ASTExpression>> fSizes;

    // initial value, may be null
    std::unique_ptr<ASTExpression> fValue;
};

/**
 * A variable declaration statement, which may consist of one or more individual variables.
 */
struct ASTVarDeclarations : public ASTDeclaration {
    ASTVarDeclarations(Modifiers modifiers,
                       std::unique_ptr<ASTType> type,
                       std::vector<ASTVarDeclaration> vars)
    : INHERITED(type->fPosition, kVar_Kind)
    , fModifiers(modifiers)
    , fType(std::move(type))
    , fVars(std::move(vars)) {}

    String description() const override {
        String result = fModifiers.description() + fType->description() + " ";
        String separator;
        for (const auto& var : fVars) {
            result += separator;
            separator = ", ";
            result += var.description();
        }
        return result;
    }

    const Modifiers fModifiers;
    const std::unique_ptr<ASTType> fType;
    const std::vector<ASTVarDeclaration> fVars;

    typedef ASTDeclaration INHERITED;
};

} // namespace

#endif