1/*
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_ASTINTERFACEBLOCK
9#define SKSL_ASTINTERFACEBLOCK
10
11#include "SkSLASTVarDeclaration.h"
12#include "../ir/SkSLModifiers.h"
13
14namespace SkSL {
15
16/**
17 * An interface block, as in:
18 *
19 * out sk_PerVertex {
20 *   layout(builtin=0) float4 sk_Position;
21 *   layout(builtin=1) float sk_PointSize;
22 * };
23 */
24struct ASTInterfaceBlock : public ASTDeclaration {
25    // valueName is empty when it was not present in the source
26    ASTInterfaceBlock(int offset,
27                      Modifiers modifiers,
28                      StringFragment typeName,
29                      std::vector<std::unique_ptr<ASTVarDeclarations>> declarations,
30                      StringFragment instanceName,
31                      std::vector<std::unique_ptr<ASTExpression>> sizes)
32    : INHERITED(offset, kInterfaceBlock_Kind)
33    , fModifiers(modifiers)
34    , fTypeName(typeName)
35    , fDeclarations(std::move(declarations))
36    , fInstanceName(instanceName)
37    , fSizes(std::move(sizes)) {}
38
39    String description() const override {
40        String result = fModifiers.description() + fTypeName + " {\n";
41        for (size_t i = 0; i < fDeclarations.size(); i++) {
42            result += fDeclarations[i]->description() + "\n";
43        }
44        result += "}";
45        if (fInstanceName.fLength) {
46            result += " " + fInstanceName;
47            for (const auto& size : fSizes) {
48                result += "[";
49                if (size) {
50                    result += size->description();
51                }
52                result += "]";
53            }
54        }
55        return result + ";";
56    }
57
58    const Modifiers fModifiers;
59    const StringFragment fTypeName;
60    const std::vector<std::unique_ptr<ASTVarDeclarations>> fDeclarations;
61    const StringFragment fInstanceName;
62    const std::vector<std::unique_ptr<ASTExpression>> fSizes;
63
64    typedef ASTDeclaration INHERITED;
65};
66
67} // namespace
68
69#endif
70