/src/skia/include/private/SkSLProgramElement.h
Line | Count | Source |
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 | | |
15 | | namespace SkSL { |
16 | | |
17 | | /** |
18 | | * Represents a top-level element (e.g. function or global variable) in a program. |
19 | | */ |
20 | | class ProgramElement : public IRNode { |
21 | | public: |
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 | 2.05M | : INHERITED(offset, (int) kind) { |
37 | 2.05M | SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast); |
38 | 2.05M | } SkSL::ProgramElement::ProgramElement(int, SkSL::ProgramElement::Kind) Line | Count | Source | 36 | 216 | : INHERITED(offset, (int) kind) { | 37 | 216 | SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast); | 38 | 216 | } |
SkSL::ProgramElement::ProgramElement(int, SkSL::ProgramElement::Kind) Line | Count | Source | 36 | 2.05M | : INHERITED(offset, (int) kind) { | 37 | 2.05M | SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast); | 38 | 2.05M | } |
|
39 | | |
40 | 31.0M | Kind kind() const { |
41 | 31.0M | return (Kind) fKind; |
42 | 31.0M | } |
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 | 19.1M | bool is() const { |
50 | 19.1M | return this->kind() == T::kProgramElementKind; |
51 | 19.1M | } bool SkSL::ProgramElement::is<SkSL::GlobalVarDeclaration>() const Line | Count | Source | 49 | 2.35M | bool is() const { | 50 | 2.35M | return this->kind() == T::kProgramElementKind; | 51 | 2.35M | } |
bool SkSL::ProgramElement::is<SkSL::FunctionDefinition>() const Line | Count | Source | 49 | 15.4M | bool is() const { | 50 | 15.4M | return this->kind() == T::kProgramElementKind; | 51 | 15.4M | } |
bool SkSL::ProgramElement::is<SkSL::InterfaceBlock>() const Line | Count | Source | 49 | 1.28M | bool is() const { | 50 | 1.28M | return this->kind() == T::kProgramElementKind; | 51 | 1.28M | } |
bool SkSL::ProgramElement::is<SkSL::FunctionPrototype>() const Line | Count | Source | 49 | 34.5k | bool is() const { | 50 | 34.5k | return this->kind() == T::kProgramElementKind; | 51 | 34.5k | } |
bool SkSL::ProgramElement::is<SkSL::StructDefinition>() const Line | Count | Source | 49 | 31.4k | bool is() const { | 50 | 31.4k | return this->kind() == T::kProgramElementKind; | 51 | 31.4k | } |
bool SkSL::ProgramElement::is<SkSL::ModifiersDeclaration>() const Line | Count | Source | 49 | 17.7k | bool is() const { | 50 | 17.7k | return this->kind() == T::kProgramElementKind; | 51 | 17.7k | } |
bool SkSL::ProgramElement::is<SkSL::Extension>() const Line | Count | Source | 49 | 9.94k | bool is() const { | 50 | 9.94k | return this->kind() == T::kProgramElementKind; | 51 | 9.94k | } |
|
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 | 8.47M | const T& as() const { |
59 | 8.47M | SkASSERT(this->is<T>()); |
60 | 8.47M | return static_cast<const T&>(*this); |
61 | 8.47M | } SkSL::GlobalVarDeclaration const& SkSL::ProgramElement::as<SkSL::GlobalVarDeclaration>() const Line | Count | Source | 58 | 131k | const T& as() const { | 59 | 131k | SkASSERT(this->is<T>()); | 60 | 131k | return static_cast<const T&>(*this); | 61 | 131k | } |
SkSL::FunctionDefinition const& SkSL::ProgramElement::as<SkSL::FunctionDefinition>() const Line | Count | Source | 58 | 690 | const T& as() const { | 59 | 690 | SkASSERT(this->is<T>()); | 60 | 690 | return static_cast<const T&>(*this); | 61 | 690 | } |
SkSL::GlobalVarDeclaration const& SkSL::ProgramElement::as<SkSL::GlobalVarDeclaration>() const Line | Count | Source | 58 | 914k | const T& as() const { | 59 | 914k | SkASSERT(this->is<T>()); | 60 | 914k | return static_cast<const T&>(*this); | 61 | 914k | } |
SkSL::InterfaceBlock const& SkSL::ProgramElement::as<SkSL::InterfaceBlock>() const Line | Count | Source | 58 | 23.3k | const T& as() const { | 59 | 23.3k | SkASSERT(this->is<T>()); | 60 | 23.3k | return static_cast<const T&>(*this); | 61 | 23.3k | } |
SkSL::FunctionDefinition const& SkSL::ProgramElement::as<SkSL::FunctionDefinition>() const Line | Count | Source | 58 | 7.34M | const T& as() const { | 59 | 7.34M | SkASSERT(this->is<T>()); | 60 | 7.34M | return static_cast<const T&>(*this); | 61 | 7.34M | } |
SkSL::FunctionPrototype const& SkSL::ProgramElement::as<SkSL::FunctionPrototype>() const Line | Count | Source | 58 | 34.5k | const T& as() const { | 59 | 34.5k | SkASSERT(this->is<T>()); | 60 | 34.5k | return static_cast<const T&>(*this); | 61 | 34.5k | } |
SkSL::Extension const& SkSL::ProgramElement::as<SkSL::Extension>() const Line | Count | Source | 58 | 45 | const T& as() const { | 59 | 45 | SkASSERT(this->is<T>()); | 60 | 45 | return static_cast<const T&>(*this); | 61 | 45 | } |
SkSL::ModifiersDeclaration const& SkSL::ProgramElement::as<SkSL::ModifiersDeclaration>() const Line | Count | Source | 58 | 17.7k | const T& as() const { | 59 | 17.7k | SkASSERT(this->is<T>()); | 60 | 17.7k | return static_cast<const T&>(*this); | 61 | 17.7k | } |
SkSL::StructDefinition const& SkSL::ProgramElement::as<SkSL::StructDefinition>() const Line | Count | Source | 58 | 1.57k | const T& as() const { | 59 | 1.57k | SkASSERT(this->is<T>()); | 60 | 1.57k | return static_cast<const T&>(*this); | 61 | 1.57k | } |
|
62 | | |
63 | | template <typename T> |
64 | 4.73M | T& as() { |
65 | 4.73M | SkASSERT(this->is<T>()); |
66 | 4.73M | return static_cast<T&>(*this); |
67 | 4.73M | } SkSL::FunctionDefinition& SkSL::ProgramElement::as<SkSL::FunctionDefinition>() Line | Count | Source | 64 | 82 | T& as() { | 65 | 82 | SkASSERT(this->is<T>()); | 66 | 82 | return static_cast<T&>(*this); | 67 | 82 | } |
SkSL::GlobalVarDeclaration& SkSL::ProgramElement::as<SkSL::GlobalVarDeclaration>() Line | Count | Source | 64 | 4 | T& as() { | 65 | 4 | SkASSERT(this->is<T>()); | 66 | 4 | return static_cast<T&>(*this); | 67 | 4 | } |
Unexecuted instantiation: SkSL::InterfaceBlock& SkSL::ProgramElement::as<SkSL::InterfaceBlock>() SkSL::FunctionDefinition& SkSL::ProgramElement::as<SkSL::FunctionDefinition>() Line | Count | Source | 64 | 4.55M | T& as() { | 65 | 4.55M | SkASSERT(this->is<T>()); | 66 | 4.55M | return static_cast<T&>(*this); | 67 | 4.55M | } |
SkSL::GlobalVarDeclaration& SkSL::ProgramElement::as<SkSL::GlobalVarDeclaration>() Line | Count | Source | 64 | 177k | T& as() { | 65 | 177k | SkASSERT(this->is<T>()); | 66 | 177k | return static_cast<T&>(*this); | 67 | 177k | } |
Unexecuted instantiation: SkSL::InterfaceBlock& SkSL::ProgramElement::as<SkSL::InterfaceBlock>() |
68 | | |
69 | | virtual std::unique_ptr<ProgramElement> clone() const = 0; |
70 | | |
71 | | private: |
72 | | using INHERITED = IRNode; |
73 | | }; |
74 | | |
75 | | } // namespace SkSL |
76 | | |
77 | | #endif |