Skip to content

Commit

Permalink
Implement as_if functionality (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
HungMingWu authored Nov 9, 2022
1 parent 52a8f5e commit 211ccf5
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/slang/ast/Constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ class SLANG_EXPORT Constraint {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor& visitor, Args&&... args) const;

Expand Down
14 changes: 14 additions & 0 deletions include/slang/ast/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ class SLANG_EXPORT Expression {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor&& visitor, Args&&... args);

Expand Down
14 changes: 14 additions & 0 deletions include/slang/ast/Patterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ class SLANG_EXPORT Pattern {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor& visitor, Args&&... args) const;

Expand Down
14 changes: 14 additions & 0 deletions include/slang/ast/Statements.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ class SLANG_EXPORT Statement {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor&& visitor, Args&&... args) const;

Expand Down
17 changes: 17 additions & 0 deletions include/slang/ast/Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,28 @@ class SLANG_EXPORT Symbol {
}
}

template<typename T>
decltype(auto) as_if() {
if constexpr (std::is_same_v<T, Scope>) {
return scopeOrNull();
}
else {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}
}

template<typename T>
const T& as() const {
return const_cast<Symbol*>(this)->as<T>();
}

template<typename T>
const T* as_if() const {
return const_cast<Symbol*>(this)->as_if<T>();
}

/// Gets the index of the symbol within its parent scope, which can be used
/// to determine the relative ordering of scope members.
SymbolIndex getIndex() const { return indexInScope; }
Expand Down
14 changes: 14 additions & 0 deletions include/slang/ast/TimingControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ class SLANG_EXPORT TimingControl {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor& visitor, Args&&... args) const;

Expand Down
14 changes: 14 additions & 0 deletions include/slang/ast/expressions/AssertionExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ class SLANG_EXPORT AssertionExpr {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor& visitor, Args&&... args) const;

Expand Down
14 changes: 14 additions & 0 deletions include/slang/ast/symbols/CoverSymbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ class SLANG_EXPORT BinsSelectExpr {
return *static_cast<const T*>(this);
}

template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

template<typename TVisitor, typename... Args>
decltype(auto) visit(TVisitor& visitor, Args&&... args) const;

Expand Down
16 changes: 16 additions & 0 deletions include/slang/syntax/SyntaxNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ class SLANG_EXPORT SyntaxNode {
return *static_cast<const T*>(this);
}

// Reinterprets this node as being of type T, if it is not belong type T, return nullptr
template<typename T>
T* as_if() {
if (!T::isKind(kind))
return nullptr;
return static_cast<T*>(this);
}

// Reinterprets this node as being of type T, if it is not belong type T, return nullptr
template<typename T>
const T* as_if() const {
if (!T::isKind(kind))
return nullptr;
return static_cast<const T*>(this);
}

/// Applies a visitor object to this node by dispatching based on the
/// dynamic kind. The given @a args are forwarded to the visitor.
template<typename TVisitor, typename... Args>
Expand Down

0 comments on commit 211ccf5

Please sign in to comment.