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