- started implementation of size and alignment for types
[cparser] / type_t.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 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_T_H
21 #define TYPE_T_H
22
23 #include <stdbool.h>
24 #include <assert.h>
25
26 #include <libfirm/firm_types.h>
27
28 #include "type.h"
29 #include "symbol.h"
30 #include "token_t.h"
31 #include "ast_t.h"
32 #include "adt/obst.h"
33
34 extern struct obstack *type_obst;
35
36 typedef enum type_kind_t {
37         TYPE_INVALID,
38         TYPE_ERROR,
39         TYPE_ATOMIC,
40         TYPE_COMPLEX,
41         TYPE_IMAGINARY,
42         TYPE_COMPOUND_STRUCT,
43         TYPE_COMPOUND_UNION,
44         TYPE_ENUM,
45         TYPE_FUNCTION,
46         TYPE_POINTER,
47         TYPE_ARRAY,
48         TYPE_BITFIELD,
49         TYPE_BUILTIN,
50         TYPE_TYPEDEF,
51         TYPE_TYPEOF,
52 } type_kind_t;
53
54 typedef enum type_modifier_t {
55         TYPE_MODIFIER_NONE              = 0,
56         TYPE_MODIFIER_TRANSPARENT_UNION = 1 << 0,
57 } type_modifier_t;
58 typedef unsigned short type_modifiers_t;
59
60 struct type_base_t {
61         type_kind_t       kind;
62         source_position_t source_position;
63         il_size_t         size;           /**< The size of this type. */
64         type_qualifiers_t qualifiers;
65         type_modifiers_t  modifiers;
66         il_alignment_t    alignment;      /**< The extra alignment of the type, 0 for default. */
67
68         ir_type          *firm_type;
69 };
70
71 struct atomic_type_t {
72         type_base_t         base;
73         atomic_type_kind_t  akind;
74 };
75
76 struct complex_type_t {
77         type_base_t         base;
78         atomic_type_kind_t  akind;
79 };
80
81 struct imaginary_type_t {
82         type_base_t         base;
83         atomic_type_kind_t  akind;
84 };
85
86 struct builtin_type_t {
87         type_base_t  base;
88         symbol_t    *symbol;
89         type_t      *real_type;
90 };
91
92 struct pointer_type_t {
93         type_base_t  base;
94         type_t      *points_to;
95 };
96
97 struct array_type_t {
98         type_base_t   base;
99         type_t       *element_type;
100         expression_t *size_expression;
101         size_t        size;
102
103         ir_node      *size_node; /**< used by ast2firm phase */
104
105         unsigned      is_static         : 1; /**< a [static] type */
106         unsigned      is_variable       : 1; /**< a [*] type */
107         unsigned      has_implicit_size : 1;
108         unsigned      size_constant     : 1; /**< size expression is constant */
109         unsigned      is_vla            : 1; /**< it's a variable length array */
110 };
111
112 /**
113  * An entry in the parameter list of a function type.
114  */
115 struct function_parameter_t {
116         type_t               *type;  /**< The parameter type. */
117         function_parameter_t *next;  /**< Points to the next type inthe parameter list.*/
118 };
119
120 /** Calling conventions. */
121 typedef enum cc_kind_t {
122         CC_DEFAULT,      /**< default calling convention. */
123         CC_CDECL,        /**< cdecl calling convention. */
124         CC_STDCALL,      /**< stdcall calling convention. */
125         CC_FASTCALL,     /**< fastcall calling convention. */
126         CC_THISCALL      /**< thiscall calling convention. */
127 } cc_kind_t;
128
129 /**
130  * A function type.
131  */
132 struct function_type_t {
133         type_base_t           base;
134         type_t               *return_type;        /**< The return type. */
135         function_parameter_t *parameters;         /**< A list of the parameter types. */
136         cc_kind_t             calling_convention; /**< The specified calling convention. */
137         unsigned              variadic : 1;
138         unsigned              unspecified_parameters : 1;
139         unsigned              kr_style_parameters : 1;
140 };
141
142 struct compound_type_t {
143         type_base_t    base;
144         /** the declaration of the compound type, the scope of the declaration
145          *  contains the compound entries. */
146         declaration_t *declaration;
147 };
148
149 struct enum_type_t {
150         type_base_t    base;
151         /** the declaration of the enum type. You can find the enum entries by
152          *  walking the declaration->next list until you don't find
153          *  STORAGE_CLASS_ENUM_ENTRY declarations anymore */
154         declaration_t *declaration;
155 };
156
157 struct typedef_type_t {
158         type_base_t    base;
159         declaration_t *declaration;
160         type_t        *resolved_type;
161 };
162
163 struct typeof_type_t {
164         type_base_t   base;
165         expression_t *expression;
166         type_t       *typeof_type;
167         type_t       *resolved_type;
168 };
169
170 struct bitfield_type_t {
171         type_base_t   base;
172         type_t       *base_type;
173         expression_t *size;
174 };
175
176 union type_t {
177         type_kind_t      kind;
178         type_base_t      base;
179         atomic_type_t    atomic;
180         complex_type_t   complex;
181         imaginary_type_t imaginary;
182         builtin_type_t   builtin;
183         pointer_type_t   pointer;
184         array_type_t     array;
185         function_type_t  function;
186         compound_type_t  compound;
187         enum_type_t      enumt;
188         typedef_type_t   typedeft;
189         bitfield_type_t  bitfield;
190         typeof_type_t    typeoft;
191 };
192
193 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
194 type_t *make_complex_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
195 type_t *make_imaginary_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
196 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
197 type_t *make_array_type(type_t *element_type, size_t size,
198                         type_qualifiers_t qualifiers);
199
200 type_t *duplicate_type(const type_t *type);
201
202 static inline bool is_typeref(const type_t *type)
203 {
204         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
205 }
206
207 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
208 {
209         assert(!is_typeref(type));
210
211         if(type->kind != TYPE_ATOMIC)
212                 return false;
213         const atomic_type_t *atomic_type = &type->atomic;
214
215         return atomic_type->akind == atype;
216 }
217
218 static inline bool is_type_pointer(const type_t *type)
219 {
220         assert(!is_typeref(type));
221         return type->kind == TYPE_POINTER;
222 }
223
224 static inline bool is_type_array(const type_t *type)
225 {
226         assert(!is_typeref(type));
227         return type->kind == TYPE_ARRAY;
228 }
229
230 static inline bool is_type_function(const type_t *type)
231 {
232         assert(!is_typeref(type));
233         return type->kind == TYPE_FUNCTION;
234 }
235
236 static inline bool is_type_union(const type_t *type)
237 {
238         assert(!is_typeref(type));
239         return type->kind == TYPE_COMPOUND_UNION;
240 }
241
242 static inline bool is_type_struct(const type_t *type)
243 {
244         assert(!is_typeref(type));
245         return type->kind == TYPE_COMPOUND_STRUCT;
246 }
247
248 static inline bool is_type_builtin(const type_t *type)
249 {
250         assert(!is_typeref(type));
251         return type->kind == TYPE_BUILTIN;
252 }
253
254 static inline bool is_type_compound(const type_t *type)
255 {
256         assert(!is_typeref(type));
257         return type->kind == TYPE_COMPOUND_STRUCT
258                 || type->kind == TYPE_COMPOUND_UNION;
259 }
260
261 static inline bool is_type_valid(const type_t *type)
262 {
263         return type->kind != TYPE_ERROR;
264 }
265
266 #endif