Add is<T> to IRNode types.

This is a shorthand function for verifying the kind of IRNode you have.

Change-Id: Ifc53100b6d29d9fcd02e90e0658fda971f136210
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/315256
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
John Stiles 2020-09-03 10:50:21 -04:00 committed by Skia Commit-Bot
parent eadfc3bccd
commit 3454885c79
4 changed files with 44 additions and 8 deletions

View File

@ -57,18 +57,27 @@ struct Expression : public IRNode {
, fKind(kind)
, fType(std::move(type)) {}
/**
* Use is<T> to check the type of an expression.
* e.g. replace `e.fKind == Expression::kIntLiteral_Kind` with `e.is<IntLiteral>()`.
*/
template <typename T>
bool is() const {
return this->fKind == T::kExpressionKind;
}
/**
* Use as<T> to downcast expressions: e.g. replace `(IntLiteral&) i` with `i.as<IntLiteral>()`.
*/
template <typename T>
const T& as() const {
SkASSERT(this->fKind == T::kExpressionKind);
SkASSERT(this->is<T>());
return static_cast<const T&>(*this);
}
template <typename T>
T& as() {
SkASSERT(this->fKind == T::kExpressionKind);
SkASSERT(this->is<T>());
return static_cast<T&>(*this);
}

View File

@ -32,18 +32,27 @@ struct ProgramElement : public IRNode {
: INHERITED(offset)
, fKind(kind) {}
/**
* Use is<T> to check the type of a program element.
* e.g. replace `el.fKind == ProgramElement::kEnum_Kind` with `el.is<Enum>()`.
*/
template <typename T>
bool is() const {
return this->fKind == T::kProgramElementKind;
}
/**
* Use as<T> to downcast program elements. e.g. replace `(Enum&) el` with `el.as<Enum>()`.
*/
template <typename T>
const T& as() const {
SkASSERT(this->fKind == T::kProgramElementKind);
SkASSERT(this->is<T>());
return static_cast<const T&>(*this);
}
template <typename T>
T& as() {
SkASSERT(this->fKind == T::kProgramElementKind);
SkASSERT(this->is<T>());
return static_cast<T&>(*this);
}

View File

@ -38,19 +38,28 @@ struct Statement : public IRNode {
: INHERITED(offset)
, fKind(kind) {}
/**
* Use is<T> to check the type of a statement.
* e.g. replace `s.fKind == Statement::kReturn_Kind` with `s.is<ReturnStatement>()`.
*/
template <typename T>
bool is() const {
return this->fKind == T::kStatementKind;
}
/**
* Use as<T> to downcast statements.
* e.g. replace `(ReturnStatement&) s` with `s.as<ReturnStatement>()`.
*/
template <typename T>
const T& as() const {
SkASSERT(this->fKind == T::kStatementKind);
SkASSERT(this->is<T>());
return static_cast<const T&>(*this);
}
template <typename T>
T& as() {
SkASSERT(this->fKind == T::kStatementKind);
SkASSERT(this->is<T>());
return static_cast<T&>(*this);
}

View File

@ -32,18 +32,27 @@ struct Symbol : public IRNode {
~Symbol() override {}
/**
* Use is<T> to check the type of a symbol.
* e.g. replace `sym.fKind == Symbol::kVariable_Kind` with `sym.is<Variable>()`.
*/
template <typename T>
bool is() const {
return this->fKind == T::kSymbolKind;
}
/**
* Use as<T> to downcast symbols. e.g. replace `(Variable&) sym` with `sym.as<Variable>()`.
*/
template <typename T>
const T& as() const {
SkASSERT(this->fKind == T::kSymbolKind);
SkASSERT(this->is<T>());
return static_cast<const T&>(*this);
}
template <typename T>
T& as() {
SkASSERT(this->fKind == T::kSymbolKind);
SkASSERT(this->is<T>());
return static_cast<T&>(*this);
}