1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
5     ---------------------------------------------------------------------
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 #pragma once
23 
24 #include <cstdlib>
25 #include <set>
26 #include "property.hh"
27 #include "sigtyperules.hh"
28 #include "tree.hh"
29 
30 //------------------------------------------------------------------------------
31 // TreeTraversal: Recursive transformation of a Tree with memoization
32 //------------------------------------------------------------------------------
33 // This is an abstract class. Derived class just have to implement the
34 // `transformation(t)` method. The `transformation(t)` method
35 // should not call itself recursively directly, but exclusively via `self(t)`
36 // (or `mapself(lt)` for a list).
37 //------------------------------------------------------------------------------
38 
39 class TreeTraversal {
40    protected:
41     // used when tracing
42     bool           fTraceFlag{false};  // trace transformations when true
43     int            fIndent{0};         // current indentation during trace
44     string         fMessage;           // trace message
45     std::set<Tree> fVisited;           // avoid visiting a tree twice
46 
47    public:
TreeTraversal(string msg="TreeTraversal")48     explicit TreeTraversal(string msg = "TreeTraversal") : fMessage(msg) {}
49     virtual ~TreeTraversal() = default;
50 
51     void self(Tree t);
52     void mapself(Tree lt);
53 
trace(bool b)54     void trace(bool b) { fTraceFlag = b; }
trace(bool b,const string & m)55     void trace(bool b, const string& m)
56     {
57         fTraceFlag = b;
58         fMessage   = m;
59     }
60 
61    protected:
62     virtual void visit(Tree) = 0;     // the visit to implement
63     void         traceEnter(Tree t);  // called when entering a visit
64     void         traceExit(Tree t);   // called when exiting a visit
65 };
66