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