llvm-journey

LLVM Journey
git clone git://0xff.ir/g/llvm-journey.git
Log | Files | Refs | README | LICENSE

commit df7a246c5f31680037e028b2d1ffa899c68a6a49
parent c2bf5312aab77745da9b24a4939bdd0ce5640766
Author: Mohammad-Reza Nabipoor <m.nabipoor@yahoo.com>
Date:   Fri, 28 Aug 2020 17:21:13 +0430

kaleidoscope_ast.hpp: Add cast func to ASTNode

By `cast` function the user can cast the polymorphic type `ASTNode`
to the concrete type inside the `ASTNode`.

Diffstat:
Mkaleidoscope_ast.hpp | 18++++++++++++++++++
1 file changed, 18 insertions(+), 0 deletions(-)

diff --git a/kaleidoscope_ast.hpp b/kaleidoscope_ast.hpp @@ -129,6 +129,20 @@ public: return n.self_->node_type(); } + template<typename T> + friend bool cast(const ASTNode& n, T& val) + { + if (!n.self_) + return false; + + auto p = dynamic_cast<const Model<T>*>(n.self_.get()); + + if (p) + val = p->value_; + + return p != nullptr; + } + friend bool operator==(const ASTNode& x, const ASTNode& y) { assert(x.self_ != nullptr && y.self_ != nullptr); @@ -184,6 +198,10 @@ private: std::shared_ptr<const Concept> self_; }; +template<typename T> +bool +cast(const ASTNode& n, T& val); + struct Number { double value;