From 647473cf013f901bfa11f7327e9efce99abd3f22 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 16 Feb 2017 23:55:13 +0100 Subject: Generic visitor. --- libsolidity/inlineasm/AsmAnalysis.h | 65 ++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 37 deletions(-) (limited to 'libsolidity/inlineasm/AsmAnalysis.h') diff --git a/libsolidity/inlineasm/AsmAnalysis.h b/libsolidity/inlineasm/AsmAnalysis.h index 6b79ca67..f00ad3d8 100644 --- a/libsolidity/inlineasm/AsmAnalysis.h +++ b/libsolidity/inlineasm/AsmAnalysis.h @@ -44,6 +44,31 @@ struct Instruction; struct Identifier; struct Assignment; +template +struct GenericVisitor{}; + +template +struct GenericVisitor: public GenericVisitor +{ + using GenericVisitor::operator (); + explicit GenericVisitor( + std::function _visitor, + std::function... _otherVisitors + ): + GenericVisitor(_otherVisitors...), + m_visitor(_visitor) + {} + + void operator()(Visitable& _v) const { m_visitor(_v); } + + std::function m_visitor; +}; +template <> +struct GenericVisitor<>: public boost::static_visitor<> { + void operator()() const {} +}; + + struct Scope { struct Variable @@ -63,43 +88,11 @@ struct Scope }; using Identifier = boost::variant; - - struct Visitor: public boost::static_visitor<> - { - Visitor( - std::function _varVisitor, - std::function _labelVisitor - ): - m_varVisitor(std::move(_varVisitor)), - m_labelVisitor(std::move(_labelVisitor)) - {} - - void operator()(Variable const& _var) const { m_varVisitor(_var); } - void operator()(Label const& _label) const { m_labelVisitor(_label); } - - std::function m_varVisitor; - std::function m_labelVisitor; - }; - struct NonconstVisitor: public boost::static_visitor<> - { - NonconstVisitor( - std::function _varVisitor, - std::function _labelVisitor - ): - m_varVisitor(std::move(_varVisitor)), - m_labelVisitor(std::move(_labelVisitor)) - {} - - void operator()(Variable& _var) const { m_varVisitor(_var); } - void operator()(Label& _label) const { m_labelVisitor(_label); } - - std::function m_varVisitor; - std::function m_labelVisitor; - }; - - bool registerLabel(std::string const& _name, size_t _id); + using Visitor = GenericVisitor; + using NonconstVisitor = GenericVisitor; bool registerVariable(std::string const& _name); + bool registerLabel(std::string const& _name, size_t _id); /// Looks up the identifier in this or super scopes and returns a valid pointer if /// found or a nullptr if not found. @@ -118,8 +111,6 @@ struct Scope return false; } - - Scope* superScope = nullptr; std::map identifiers; }; -- cgit