1 /*
2  *  VHDL abstract syntax elements.
3  *
4  *  Copyright (C) 2008  Nick Gasson (nick@nickg.me.uk)
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 along
17  *  with this program; if not, write to the Free Software Foundation, Inc.,
18  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef INC_VHDL_ELEMENT_HH
22 #define INC_VHDL_ELEMENT_HH
23 
24 #include <iosfwd>
25 #include <list>
26 #include <string>
27 #include <new>
28 #include <vector>
29 
30 typedef std::list<std::string> string_list_t;
31 
32 // Any VHDL syntax element. Each element can also contain a comment.
33 //
34 // Memory management is handled specially for vhdl_element subclasses:
35 // The vast majority of vhdl_elements will be created during code generation
36 // and persist until after they have been printed, at which point *all*
37 // vhdl_element objects should be destroyed. To support this all allocations
38 // of vhdl_element subclasses call a special operator new which records
39 // the pointer allocated so we can ensure that it is disposed of when
40 // the code generator completes -- by free_all_objects.
41 //
42 // The two big advantages of this are that we don't have to worry about
43 // memory leaks of vhdl_element objects, and we can freely share pointers
44 // between different parts of the AST.
45 class vhdl_element {
46 public:
~vhdl_element()47    virtual ~vhdl_element() {}
48 
49    void* operator new(size_t size);
50    void operator delete(void* ptr);
51 
52    virtual void emit(std::ostream &of, int level=0) const = 0;
53    void print() const;
54 
55    void set_comment(std::string comment);
56 
57    static int free_all_objects();
58    static size_t total_allocated();
59 protected:
60    void emit_comment(std::ostream &of, int level,
61                      bool end_of_line=false) const;
62 private:
63    std::string comment_;
64 
65    static std::vector<vhdl_element*> allocated_;
66    static size_t total_alloc_;
67 };
68 
69 typedef std::list<vhdl_element*> element_list_t;
70 
71 int indent(int level);
72 void newline(std::ostream &of, int level);
73 std::string nl_string(int level);
74 void blank_line(std::ostream &of, int level);
75 
76 #endif
77