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_INTERFACEBLOCK
9#define SKSL_INTERFACEBLOCK
10
11#include "src/sksl/ir/SkSLProgramElement.h"
12#include "src/sksl/ir/SkSLSymbolTable.h"
13#include "src/sksl/ir/SkSLVarDeclarations.h"
14
15namespace SkSL {
16
17/**
18 * An interface block, as in:
19 *
20 * out sk_PerVertex {
21 * layout(builtin=0) float4 sk_Position;
22 * layout(builtin=1) float sk_PointSize;
23 * };
24 *
25 * At the IR level, this is represented by a single variable of struct type.
26 */
27struct InterfaceBlock : public ProgramElement {
28 InterfaceBlock(int offset, const Variable* var, String typeName, String instanceName,
29 std::vector<std::unique_ptr<Expression>> sizes,
30 std::shared_ptr<SymbolTable> typeOwner)
31 : INHERITED(offset, kInterfaceBlock_Kind)
32 , fVariable(*var)
33 , fTypeName(std::move(typeName))
34 , fInstanceName(std::move(instanceName))
35 , fSizes(std::move(sizes))
36 , fTypeOwner(typeOwner) {}
37
38 std::unique_ptr<ProgramElement> clone() const override {
39 std::vector<std::unique_ptr<Expression>> sizesClone;
40 for (const auto& s : fSizes) {
41 sizesClone.push_back(s->clone());
42 }
43 return std::unique_ptr<ProgramElement>(new InterfaceBlock(fOffset, &fVariable, fTypeName,
44 fInstanceName,
45 std::move(sizesClone),
46 fTypeOwner));
47 }
48
49 String description() const override {
50 String result = fVariable.fModifiers.description() + fTypeName + " {\n";
51 const Type* structType = &fVariable.fType;
52 while (structType->kind() == Type::kArray_Kind) {
53 structType = &structType->componentType();
54 }
55 for (const auto& f : structType->fields()) {
56 result += f.description() + "\n";
57 }
58 result += "}";
59 if (fInstanceName.size()) {
60 result += " " + fInstanceName;
61 for (const auto& size : fSizes) {
62 result += "[";
63 if (size) {
64 result += size->description();
65 }
66 result += "]";
67 }
68 }
69 return result + ";";
70 }
71
72 const Variable& fVariable;
73 const String fTypeName;
74 const String fInstanceName;
75 std::vector<std::unique_ptr<Expression>> fSizes;
76 const std::shared_ptr<SymbolTable> fTypeOwner;
77
78 typedef ProgramElement INHERITED;
79};
80
81} // namespace SkSL
82
83#endif
84