From 3c8b777b9b7e438e16a3cb60dece5af3753fc8d4 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 20 Dec 2017 13:47:51 +0100 Subject: References counter. --- libjulia/optimiser/ASTWalker.h | 4 ++-- libjulia/optimiser/NameCollector.cpp | 25 +++++++++++++++++++++++++ libjulia/optimiser/NameCollector.h | 20 +++++++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/libjulia/optimiser/ASTWalker.h b/libjulia/optimiser/ASTWalker.h index dbf8194b..97891381 100644 --- a/libjulia/optimiser/ASTWalker.h +++ b/libjulia/optimiser/ASTWalker.h @@ -71,8 +71,8 @@ protected: template void walkVector(T const& _statements) { - for (auto const& st: _statements) - visit(st); + for (auto const& statement: _statements) + visit(statement); } }; diff --git a/libjulia/optimiser/NameCollector.cpp b/libjulia/optimiser/NameCollector.cpp index 7b4c4793..f94104b7 100644 --- a/libjulia/optimiser/NameCollector.cpp +++ b/libjulia/optimiser/NameCollector.cpp @@ -42,3 +42,28 @@ void NameCollector::operator ()(FunctionDefinition const& _funDef) m_names.insert(ret.name); ASTWalker::operator ()(_funDef); } + +void ReferencesCounter::operator()(Identifier const& _identifier) +{ + ++m_references[_identifier.name]; +} + +void ReferencesCounter::operator()(FunctionCall const& _funCall) +{ + ++m_references[_funCall.functionName.name]; + ASTWalker::operator()(_funCall); +} + +map ReferencesCounter::countReferences(Block const& _block) +{ + ReferencesCounter counter; + counter(_block); + return counter.references(); +} + +map ReferencesCounter::countReferences(Expression const& _expression) +{ + ReferencesCounter counter; + counter.visit(_expression); + return counter.references(); +} diff --git a/libjulia/optimiser/NameCollector.h b/libjulia/optimiser/NameCollector.h index b7e38f46..7fe386f7 100644 --- a/libjulia/optimiser/NameCollector.h +++ b/libjulia/optimiser/NameCollector.h @@ -15,7 +15,7 @@ along with solidity. If not, see . */ /** - * Specific AST walker that collects all defined names. + * Specific AST walkers that collect facts about identifiers and definitions. */ #pragma once @@ -48,5 +48,23 @@ private: std::map m_functions; }; +/** + * Specific AST walker that counts all references to all declarations. + */ +class ReferencesCounter: public ASTWalker +{ +public: + using ASTWalker::operator (); + virtual void operator()(Identifier const& _identifier); + virtual void operator()(FunctionCall const& _funCall); + + static std::map countReferences(Block const& _block); + static std::map countReferences(Expression const& _expression); + + std::map const& references() const { return m_references; } +private: + std::map m_references; +}; + } } -- cgit