1 /*++ 2 Copyright (c) 2006 Microsoft Corporation 3 4 Module Name: 5 6 recurse_expr.h 7 8 Abstract: 9 10 Traverse an expression applying a visitor. 11 12 Author: 13 14 Leonardo de Moura (leonardo) 2008-01-11. 15 16 Revision History: 17 18 --*/ 19 #pragma once 20 21 #include "ast/ast.h" 22 #include "util/obj_hashtable.h" 23 24 template<typename T, typename Visitor, bool IgnorePatterns=false, bool CallDestructors=true> 25 class recurse_expr : public Visitor { 26 obj_map<expr, T> m_cache; 27 ptr_vector<expr> m_todo; 28 vector<T, CallDestructors> m_results1; 29 vector<T, CallDestructors> m_results2; 30 is_cached(expr * n)31 bool is_cached(expr * n) const { T c; return m_cache.find(n, c); } get_cached(expr * n)32 T get_cached(expr * n) const { return m_cache.find(n); } cache_result(expr * n,T c)33 void cache_result(expr * n, T c) { m_cache.insert(n, c); } 34 35 void visit(expr * n, bool & visited); 36 bool visit_children(expr * n); 37 void process(expr * n); 38 39 public: Visitor(v)40 recurse_expr(Visitor const & v = Visitor()):Visitor(v) {} 41 T operator()(expr * n); reset()42 void reset() { m_cache.reset(); m_todo.reset(); } finalize()43 void finalize() { m_cache.finalize(); m_todo.finalize(); } 44 }; 45 46