1 /*
2  *  VHDL variable and signal types.
3  *
4  *  Copyright (C) 2008-2010  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_TYPE_HH
22 #define INC_VHDL_TYPE_HH
23 
24 #include "vhdl_element.hh"
25 
26 enum vhdl_type_name_t {
27    VHDL_TYPE_STD_LOGIC,
28    VHDL_TYPE_STD_ULOGIC,
29    VHDL_TYPE_STD_LOGIC_VECTOR,
30    VHDL_TYPE_STRING,
31    VHDL_TYPE_LINE,
32    VHDL_TYPE_FILE,
33    VHDL_TYPE_INTEGER,
34    VHDL_TYPE_BOOLEAN,
35    VHDL_TYPE_SIGNED,
36    VHDL_TYPE_UNSIGNED,
37    VHDL_TYPE_TIME,
38    VHDL_TYPE_ARRAY
39 };
40 
41 /*
42  * A type at the moment is just a name. It shouldn't get
43  * too much more complex, as Verilog's type system is much
44  * simpler than VHDL's.
45  */
46 class vhdl_type : public vhdl_element {
47 public:
48    // Scalar constructor
vhdl_type(vhdl_type_name_t name,int msb=0,int lsb=0)49    vhdl_type(vhdl_type_name_t name, int msb = 0, int lsb = 0)
50       : name_(name), msb_(msb), lsb_(lsb), base_(NULL) {}
51 
52    // Array constructor
vhdl_type(vhdl_type * base,const std::string & array_name,int msb,int lsb)53    vhdl_type(vhdl_type *base, const std::string &array_name,
54              int msb, int lsb)
55       : name_(VHDL_TYPE_ARRAY), msb_(msb), lsb_(lsb), base_(base),
56         array_name_(array_name) {}
57 
58    // Copy constructor
59    vhdl_type(const vhdl_type &other);
60 
61    virtual ~vhdl_type();
62 
63    void emit(std::ostream &of, int level) const;
get_name() const64    vhdl_type_name_t get_name() const { return name_; }
65    std::string get_string() const;
66    std::string get_decl_string() const;
67    std::string get_type_decl_string() const;
68    vhdl_type *get_base() const;
get_width() const69    int get_width() const { return msb_ - lsb_ + 1; }
get_msb() const70    int get_msb() const { return msb_; }
get_lsb() const71    int get_lsb() const { return lsb_; }
72 
73    // Common types
74    static vhdl_type *std_logic();
75    static vhdl_type *std_ulogic();
76    static vhdl_type *string();
77    static vhdl_type *line();
78    static vhdl_type *std_logic_vector(int msb, int lsb);
79    static vhdl_type *nunsigned(int width, int lsb=0);
80    static vhdl_type *nsigned(int width, int lsb=0);
81    static vhdl_type *integer();
82    static vhdl_type *boolean();
83    static vhdl_type *time();
84 
85    static vhdl_type *type_for(int width, bool issigned,
86                               int lsb=0, bool unresolved=false);
87    static vhdl_type *array_of(vhdl_type *b, std::string &n, int m, int l);
88 protected:
89    vhdl_type_name_t name_;
90    int msb_, lsb_;
91    vhdl_type *base_;   // Array base type for VHDL_TYPE_ARRAY
92    std::string array_name_; // Type name for the array `type array_name_ is ...'
93 };
94 
95 #endif
96