type: Add missing space for printing complex types.
[cparser] / type.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #ifndef TYPE_H
21 #define TYPE_H
22
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include "ast.h"
26 #include "symbol.h"
27
28 /** Type used to express sizes. */
29 typedef unsigned long il_size_t;
30 typedef unsigned char il_alignment_t;
31
32 /* note that the constant values represent the rank of the types as defined
33  * in § 6.3.1 */
34 typedef enum atomic_type_kind_t {
35         ATOMIC_TYPE_VOID = 1,
36         ATOMIC_TYPE_BOOL,
37         ATOMIC_TYPE_WCHAR_T, /* only used in C++, in C code wchar_t is a pp-macro */
38         ATOMIC_TYPE_CHAR,
39         ATOMIC_TYPE_SCHAR,
40         ATOMIC_TYPE_UCHAR,
41         ATOMIC_TYPE_SHORT,
42         ATOMIC_TYPE_USHORT,
43         ATOMIC_TYPE_INT,
44         ATOMIC_TYPE_UINT,
45         ATOMIC_TYPE_LONG,
46         ATOMIC_TYPE_ULONG,
47         ATOMIC_TYPE_LONGLONG,
48         ATOMIC_TYPE_ULONGLONG,
49         ATOMIC_TYPE_FLOAT,
50         ATOMIC_TYPE_DOUBLE,
51         ATOMIC_TYPE_LONG_DOUBLE,
52
53         ATOMIC_TYPE_LAST = ATOMIC_TYPE_LONG_DOUBLE
54 } atomic_type_kind_t;
55
56 typedef enum atomic_type_flag_t {
57         ATOMIC_TYPE_FLAG_NONE       = 0,
58         ATOMIC_TYPE_FLAG_SIGNED     = 1 << 0,
59         ATOMIC_TYPE_FLAG_INTEGER    = 1 << 1,
60         ATOMIC_TYPE_FLAG_FLOAT      = 1 << 2,
61         ATOMIC_TYPE_FLAG_ARITHMETIC = 1 << 3,
62 } atomic_type_flag_t;
63
64 typedef enum type_qualifier_t {
65         TYPE_QUALIFIER_NONE     = 0,
66         TYPE_QUALIFIER_CONST    = 1 << 0,
67         TYPE_QUALIFIER_RESTRICT = 1 << 1,
68         TYPE_QUALIFIER_VOLATILE = 1 << 2,
69         /* microsoft extended qualifiers */
70         TYPE_QUALIFIER_W64      = 1 << 3,
71         TYPE_QUALIFIER_PTR32    = 1 << 4,
72         TYPE_QUALIFIER_PTR64    = 1 << 5,
73         TYPE_QUALIFIER_SPTR     = 1 << 6,
74         TYPE_QUALIFIER_UPTR     = 1 << 7,
75 } type_qualifier_t;
76 typedef unsigned short type_qualifiers_t;
77
78 typedef struct type_base_t           type_base_t;
79 typedef struct atomic_type_t         atomic_type_t;
80 typedef struct pointer_type_t        pointer_type_t;
81 typedef struct reference_type_t      reference_type_t;
82 typedef struct function_parameter_t  function_parameter_t;
83 typedef struct function_type_t       function_type_t;
84 typedef struct compound_type_t       compound_type_t;
85 typedef struct enum_type_t           enum_type_t;
86 typedef struct builtin_type_t        builtin_type_t;
87 typedef struct array_type_t          array_type_t;
88 typedef struct typedef_type_t        typedef_type_t;
89 typedef struct bitfield_type_t       bitfield_type_t;
90 typedef struct typeof_type_t         typeof_type_t;
91 typedef union  type_t                type_t;
92
93 /**
94  * Initializes the type system. Attempts to set some defaults on the atomic
95  * types based on a given machine size.
96  * These type properties are not final but you should adapt them to your system
97  * as your architecture and operating systems application binary interface (ABI)
98  * requires.
99  */
100 void init_types(unsigned machine_size);
101 void exit_types(void);
102
103 /**
104  * Prints a type.
105  *
106  * @param type   The type.
107  */
108 void print_type(const type_t *type);
109
110 /**
111  * prints a human readable form of @p type. prints an abstract typename
112  * if symbol is NULL
113  */
114 void print_type_ext(const type_t *type, const symbol_t *symbol,
115                     const scope_t *parameters);
116
117 typedef enum QualifierSeparators {
118         QUAL_SEP_NONE  = 0,
119         QUAL_SEP_START = 1U << 0,
120         QUAL_SEP_END   = 1U << 1
121 } QualifierSeparators;
122
123 void print_type_qualifiers(type_qualifiers_t qualifiers, QualifierSeparators);
124
125 /**
126  * Prints an enum definition.
127  *
128  * @param declaration  The enum's type declaration.
129  */
130 void print_enum_definition(const enum_t *enume);
131
132 /**
133  * Print the compound part of a compound type.
134  */
135 void print_compound_definition(const compound_t *compound);
136
137 void inc_type_visited(void);
138
139 /**
140  * Returns true if the given type is an integer type.
141  *
142  * @param type  The type to check.
143  * @return True if type is an integer type.
144  */
145 bool is_type_integer(const type_t *type);
146
147 /**
148  * Returns true if the given type is an enum type.
149  *
150  * @param type  The type to check.
151  * @return True if type is an enum type.
152  */
153 bool is_type_enum(const type_t *type);
154
155 /**
156  * Returns true if the given type is a signed type.
157  *
158  * @param type  The type to check.
159  * @return True if type is a signed type.
160  */
161 bool is_type_signed(const type_t *type);
162
163 /**
164  * Returns true if the given type is a floating point type.
165  *
166  * @param type  The type to check.
167  * @return True if type is a floating point type.
168  */
169 bool is_type_float(const type_t *type);
170
171 /**
172  * Returns true if the given type is a complex type.
173  *
174  * @param type  The type to check.
175  * @return True if type is a complex type.
176  */
177 bool is_type_complex(const type_t *type);
178
179 /**
180  * Returns true if the given type is an integer or float type.
181  *
182  * @param type  The type to check.
183  * @return True if type is an integer or float type.
184  */
185 bool is_type_real(const type_t *type);
186
187 /**
188  * Returns true if the type is an arithmetic type (§6.2.5 clause 18)
189  *
190  * @param type  The type to check.
191  * @return True if type represents an arithmetic type.
192  */
193 bool is_type_arithmetic(const type_t *type);
194
195 /**
196  * Returns true if the type is a scalar type (§6.2.5 clause 21)
197  *
198  * @param type  The type to check.
199  * @return True if type represents a scalar type.
200  */
201 bool is_type_scalar(const type_t *type);
202
203 /**
204  * Check if a given type is incomplete.
205  *
206  * @param type  The type to check.
207  * @return True if the given type is incomplete (i.e. just forward).
208  */
209 bool is_type_incomplete(const type_t *type);
210
211 bool is_type_object(const type_t *type);
212
213 /**
214  * Check if two types are compatible.
215  */
216 bool types_compatible(const type_t *type1, const type_t *type2);
217
218 /**
219  * Returns the unqualified type of a given type.
220  *
221  * @param type  The type.
222  * @returns The unqualified type.
223  */
224 type_t *get_unqualified_type(type_t *type);
225
226 type_t *get_qualified_type(type_t*, type_qualifiers_t);
227 type_t *skip_typeref(type_t *type);
228
229 /**
230  * Return the type qualifier set of a type. If skip_array_type
231  * is true, skip all array types.
232  */
233 type_qualifiers_t get_type_qualifier(const type_t *type, bool skip_array_type);
234
235 /**
236  * returns size of an atomic type kind in bytes
237  */
238 unsigned get_atomic_type_size(atomic_type_kind_t kind);
239
240 /**
241  * returns alignment of an atomic type kind in bytes
242  */
243 unsigned get_atomic_type_alignment(atomic_type_kind_t kind);
244
245 unsigned         get_type_alignment(type_t *type);
246 unsigned         get_type_size(type_t *type);
247 decl_modifiers_t get_type_modifiers(const type_t *type);
248
249 /**
250  * returns flags of an atomic type kind
251  */
252 unsigned get_atomic_type_flags(atomic_type_kind_t kind);
253
254 /**
255  * Find the atomic type kind representing a given size (signed).
256  */
257 atomic_type_kind_t find_signed_int_atomic_type_kind_for_size(unsigned size);
258
259 /**
260  * Find the atomic type kind representing a given size (unsigned).
261  */
262 atomic_type_kind_t find_unsigned_int_atomic_type_kind_for_size(unsigned size);
263
264 const char *get_atomic_kind_name(atomic_type_kind_t kind);
265
266 /**
267  * Finish the construction of a struct type by calculating its size, offsets,
268  * alignment.
269  */
270 void layout_struct_type(compound_type_t *type);
271
272 /**
273  * Finish the construction of an union type by calculating
274  * its size and alignment.
275  */
276 void layout_union_type(compound_type_t *type);
277
278 #endif