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_PROGRAMELEMENT
9#define SKSL_PROGRAMELEMENT
10
11#include "include/private/SkSLIRNode.h"
12
13#include <memory>
14
15namespace SkSL {
16
17/**
18 * Represents a top-level element (e.g. function or global variable) in a program.
19 */
20class ProgramElement : public IRNode {
21public:
22    enum class Kind {
23        kExtension = 0,
24        kFunction,
25        kFunctionPrototype,
26        kGlobalVar,
27        kInterfaceBlock,
28        kModifiers,
29        kStructDefinition,
30
31        kFirst = kExtension,
32        kLast = kStructDefinition
33    };
34
35    ProgramElement(int offset, Kind kind)
36        : INHERITED(offset, (int) kind) {
37        SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
38    }
39
40    Kind kind() const {
41        return (Kind) fKind;
42    }
43
44    /**
45     *  Use is<T> to check the type of a program element.
46     *  e.g. replace `el.kind() == ProgramElement::Kind::kExtension` with `el.is<Extension>()`.
47     */
48    template <typename T>
49    bool is() const {
50        return this->kind() == T::kProgramElementKind;
51    }
52
53    /**
54     *  Use as<T> to downcast program elements. e.g. replace `(Extension&) el` with
55     * `el.as<Extension>()`.
56     */
57    template <typename T>
58    const T& as() const {
59        SkASSERT(this->is<T>());
60        return static_cast<const T&>(*this);
61    }
62
63    template <typename T>
64    T& as() {
65        SkASSERT(this->is<T>());
66        return static_cast<T&>(*this);
67    }
68
69    virtual std::unique_ptr<ProgramElement> clone() const = 0;
70
71private:
72    using INHERITED = IRNode;
73};
74
75}  // namespace SkSL
76
77#endif
78