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_SYMBOL
|
|
|
|
#define SKSL_SYMBOL
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/ir/SkSLIRNode.h"
|
2020-09-08 14:22:09 +00:00
|
|
|
#include "src/sksl/ir/SkSLProgramElement.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a symboltable entry.
|
|
|
|
*/
|
2020-10-05 15:49:11 +00:00
|
|
|
class Symbol : public IRNode {
|
|
|
|
public:
|
2020-09-08 14:22:09 +00:00
|
|
|
enum class Kind {
|
|
|
|
kExternal = (int) ProgramElement::Kind::kLast + 1,
|
|
|
|
kField,
|
|
|
|
kFunctionDeclaration,
|
2020-10-06 20:14:37 +00:00
|
|
|
kSymbolAlias,
|
2020-09-08 14:22:09 +00:00
|
|
|
kType,
|
|
|
|
kUnresolvedFunction,
|
|
|
|
kVariable,
|
|
|
|
|
|
|
|
kFirst = kExternal,
|
|
|
|
kLast = kVariable
|
2016-07-01 15:22:01 +00:00
|
|
|
};
|
|
|
|
|
2020-09-11 16:27:26 +00:00
|
|
|
Symbol(int offset, Kind kind, StringFragment name, const Type* type = nullptr)
|
2020-10-05 15:49:11 +00:00
|
|
|
: INHERITED(offset, (int) kind, SymbolData{name, type}) {
|
2020-09-08 14:22:09 +00:00
|
|
|
SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
|
|
|
|
}
|
2020-03-31 22:31:46 +00:00
|
|
|
|
2020-10-05 15:49:11 +00:00
|
|
|
Symbol(int offset, const FieldData& data)
|
|
|
|
: INHERITED(offset, (int) Kind::kField, data) {}
|
|
|
|
|
2020-10-06 20:14:37 +00:00
|
|
|
Symbol(int offset, const SymbolAliasData& data)
|
|
|
|
: INHERITED(offset, (int) Kind::kSymbolAlias, data) {}
|
|
|
|
|
2020-10-07 20:42:04 +00:00
|
|
|
Symbol(int offset, const VariableData& data)
|
|
|
|
: INHERITED(offset, (int) Kind::kVariable, data) {}
|
|
|
|
|
2020-09-18 19:27:35 +00:00
|
|
|
Symbol& operator=(const Symbol&) = default;
|
|
|
|
|
2020-08-14 02:58:04 +00:00
|
|
|
~Symbol() override {}
|
2018-03-01 20:05:17 +00:00
|
|
|
|
2020-09-08 14:22:09 +00:00
|
|
|
Kind kind() const {
|
|
|
|
return (Kind) fKind;
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:49:11 +00:00
|
|
|
virtual StringFragment name() const {
|
|
|
|
return this->symbolData().fName;
|
|
|
|
}
|
|
|
|
|
2020-09-03 14:50:21 +00:00
|
|
|
/**
|
|
|
|
* Use is<T> to check the type of a symbol.
|
2020-09-24 14:40:29 +00:00
|
|
|
* e.g. replace `sym.kind() == Symbol::Kind::kVariable` with `sym.is<Variable>()`.
|
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::kSymbolKind;
|
2020-09-03 14:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 14:40:03 +00:00
|
|
|
/**
|
|
|
|
* Use as<T> to downcast symbols. e.g. replace `(Variable&) sym` with `sym.as<Variable>()`.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
const T& as() const {
|
2020-09-03 14:50:21 +00:00
|
|
|
SkASSERT(this->is<T>());
|
2020-08-18 14:40:03 +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-18 14:40:03 +00:00
|
|
|
return static_cast<T&>(*this);
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:49:11 +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
|