2016-07-01 15:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
#ifndef SKSL_PROGRAMELEMENT
|
|
|
|
#define SKSL_PROGRAMELEMENT
|
|
|
|
|
2021-03-09 18:10:59 +00:00
|
|
|
#include "include/private/SkSLIRNode.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2019-06-26 17:54:14 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a top-level element (e.g. function or global variable) in a program.
|
|
|
|
*/
|
2020-10-08 09:28:32 +00:00
|
|
|
class ProgramElement : public IRNode {
|
|
|
|
public:
|
2020-09-08 14:22:09 +00:00
|
|
|
enum class Kind {
|
2021-07-08 17:40:10 +00:00
|
|
|
kExtension = 0,
|
2020-09-08 14:22:09 +00:00
|
|
|
kFunction,
|
2020-11-03 17:18:22 +00:00
|
|
|
kFunctionPrototype,
|
2020-11-25 21:24:55 +00:00
|
|
|
kGlobalVar,
|
2020-09-08 14:22:09 +00:00
|
|
|
kInterfaceBlock,
|
|
|
|
kModifiers,
|
2020-11-25 21:24:55 +00:00
|
|
|
kStructDefinition,
|
2020-09-08 14:22:09 +00:00
|
|
|
|
2021-07-08 17:40:10 +00:00
|
|
|
kFirst = kExtension,
|
2020-11-25 21:24:55 +00:00
|
|
|
kLast = kStructDefinition
|
2016-07-01 15:22:01 +00:00
|
|
|
};
|
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
ProgramElement(int offset, Kind kind)
|
2020-10-30 14:29:12 +00:00
|
|
|
: INHERITED(offset, (int) kind) {
|
2020-09-30 14:17:00 +00:00
|
|
|
SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
|
|
|
|
}
|
|
|
|
|
2020-09-08 14:22:09 +00:00
|
|
|
Kind kind() const {
|
|
|
|
return (Kind) fKind;
|
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2020-09-03 14:50:21 +00:00
|
|
|
/**
|
|
|
|
* Use is<T> to check the type of a program element.
|
2021-07-08 17:40:10 +00:00
|
|
|
* e.g. replace `el.kind() == ProgramElement::Kind::kExtension` with `el.is<Extension>()`.
|
2020-09-03 14:50:21 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
bool is() const {
|
2020-09-08 14:22:09 +00:00
|
|
|
return this->kind() == T::kProgramElementKind;
|
2020-09-03 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 21:48:31 +00:00
|
|
|
/**
|
2021-07-08 17:40:10 +00:00
|
|
|
* Use as<T> to downcast program elements. e.g. replace `(Extension&) el` with
|
|
|
|
* `el.as<Extension>()`.
|
2020-08-19 21:48:31 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
const T& as() const {
|
2020-09-03 14:50:21 +00:00
|
|
|
SkASSERT(this->is<T>());
|
2020-08-19 21:48:31 +00:00
|
|
|
return static_cast<const T&>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T& as() {
|
2020-09-03 14:50:21 +00:00
|
|
|
SkASSERT(this->is<T>());
|
2020-08-19 21:48:31 +00:00
|
|
|
return static_cast<T&>(*this);
|
|
|
|
}
|
|
|
|
|
2018-07-31 13:44:36 +00:00
|
|
|
virtual std::unique_ptr<ProgramElement> clone() const = 0;
|
|
|
|
|
2020-10-08 09:28:32 +00:00
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = IRNode;
|
2016-07-01 15:22:01 +00:00
|
|
|
};
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace SkSL
|
2016-07-01 15:22:01 +00:00
|
|
|
|
|
|
|
#endif
|