Improve linkage specification
[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         il_size_t         size;           /**< The size of this type. */
63         type_qualifiers_t qualifiers;
64         type_modifiers_t  modifiers;
65         il_alignment_t    alignment;      /**< The extra alignment of the type, 0 for default. */
66
67         ir_type          *firm_type;
68 };
69
70 struct atomic_type_t {
71         type_base_t         base;
72         atomic_type_kind_t  akind;
73 };
74
75 struct complex_type_t {
76         type_base_t         base;
77         atomic_type_kind_t  akind;
78 };
79
80 struct imaginary_type_t {
81         type_base_t         base;
82         atomic_type_kind_t  akind;
83 };
84
85 struct builtin_type_t {
86         type_base_t  base;
87         symbol_t    *symbol;
88         type_t      *real_type;
89 };
90
91 struct pointer_type_t {
92         type_base_t  base;
93         type_t      *points_to;
94 };
95
96 struct array_type_t {
97         type_base_t   base;
98         type_t       *element_type;
99         expression_t *size_expression;
100         size_t        size;
101
102         ir_node      *size_node; /**< used by ast2firm phase */
103
104         bool          is_static         : 1; /**< a [static] type */
105         bool          is_variable       : 1; /**< a [*] type */
106         bool          has_implicit_size : 1;
107         bool          size_constant     : 1; /**< size expression is constant */
108         bool          is_vla            : 1; /**< it's a variable length array */
109 };
110
111 /**
112  * An entry in the parameter list of a function type.
113  */
114 struct function_parameter_t {
115         type_t               *type;  /**< The parameter type. */
116         function_parameter_t *next;  /**< Points to the next type inthe parameter list.*/
117 };
118
119 /** Linkage specifications. */
120 typedef enum linkage_kind_t {
121         LINKAGE_INVALID,
122         LINKAGE_C,       /**< C linkage. */
123         LINKAGE_CXX      /**< C++ linkage. */
124 } linkage_kind_t;
125
126 /** Calling conventions. */
127 typedef enum cc_kind_t {
128         CC_DEFAULT,      /**< default calling convention. */
129         CC_CDECL,        /**< cdecl calling convention. */
130         CC_STDCALL,      /**< stdcall calling convention. */
131         CC_FASTCALL,     /**< fastcall calling convention. */
132         CC_THISCALL      /**< thiscall calling convention. */
133 } cc_kind_t;
134
135 /**
136  * A function type.
137  */
138 struct function_type_t {
139         type_base_t           base;
140         type_t               *return_type;        /**< The return type. */
141         function_parameter_t *parameters;         /**< A list of the parameter types. */
142         linkage_kind_t        linkage;
143         cc_kind_t             calling_convention; /**< The specified calling convention. */
144         bool                  variadic : 1;
145         bool                  unspecified_parameters : 1;
146         bool                  kr_style_parameters : 1;
147 };
148
149 struct compound_type_t {
150         type_base_t  base;
151         bool         packed : 1; /**< Set if packed was specified. */
152         /** the declaration of the compound type, the scope of the declaration
153          *  contains the compound entries. */
154         compound_t  *compound;
155 };
156
157 struct enum_type_t {
158         type_base_t  base;
159         /** the enum entity. You can find the enum entries by walking the
160          *  enum->base.next list until you don't find ENTITY_ENUM_VALUE entities
161          *  anymore */
162         enum_t      *enume;
163 };
164
165 struct typedef_type_t {
166         type_base_t  base;
167         typedef_t   *typedefe;
168         type_t      *resolved_type;
169 };
170
171 struct typeof_type_t {
172         type_base_t   base;
173         expression_t *expression;
174         type_t       *typeof_type;
175         type_t       *resolved_type;
176 };
177
178 struct bitfield_type_t {
179         type_base_t   base;
180         type_t       *base_type;
181         expression_t *size_expression; /**< The expression for the bit size. */
182         il_size_t     bit_size;        /**< Size of this bitfield in bits. */
183 };
184
185 union type_t {
186         type_kind_t      kind;
187         type_base_t      base;
188         atomic_type_t    atomic;
189         complex_type_t   complex;
190         imaginary_type_t imaginary;
191         builtin_type_t   builtin;
192         pointer_type_t   pointer;
193         array_type_t     array;
194         function_type_t  function;
195         compound_type_t  compound;
196         enum_type_t      enumt;
197         typedef_type_t   typedeft;
198         bitfield_type_t  bitfield;
199         typeof_type_t    typeoft;
200 };
201
202 type_t *make_atomic_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
203 type_t *make_complex_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
204 type_t *make_imaginary_type(atomic_type_kind_t type, type_qualifiers_t qualifiers);
205 type_t *make_pointer_type(type_t *points_to, type_qualifiers_t qualifiers);
206 type_t *make_array_type(type_t *element_type, size_t size,
207                         type_qualifiers_t qualifiers);
208
209 type_t *duplicate_type(const type_t *type);
210
211 static inline bool is_typeref(const type_t *type)
212 {
213         return type->kind == TYPE_TYPEDEF || type->kind == TYPE_TYPEOF;
214 }
215
216 static inline bool is_type_atomic(const type_t *type, atomic_type_kind_t atype)
217 {
218         assert(!is_typeref(type));
219
220         if(type->kind != TYPE_ATOMIC)
221                 return false;
222         const atomic_type_t *atomic_type = &type->atomic;
223
224         return atomic_type->akind == atype;
225 }
226
227 static inline bool is_type_pointer(const type_t *type)
228 {
229         assert(!is_typeref(type));
230         return type->kind == TYPE_POINTER;
231 }
232
233 static inline bool is_type_array(const type_t *type)
234 {
235         assert(!is_typeref(type));
236         return type->kind == TYPE_ARRAY;
237 }
238
239 static inline bool is_type_function(const type_t *type)
240 {
241         assert(!is_typeref(type));
242         return type->kind == TYPE_FUNCTION;
243 }
244
245 static inline bool is_type_union(const type_t *type)
246 {
247         assert(!is_typeref(type));
248         return type->kind == TYPE_COMPOUND_UNION;
249 }
250
251 static inline bool is_type_struct(const type_t *type)
252 {
253         assert(!is_typeref(type));
254         return type->kind == TYPE_COMPOUND_STRUCT;
255 }
256
257 static inline bool is_type_builtin(const type_t *type)
258 {
259         assert(!is_typeref(type));
260         return type->kind == TYPE_BUILTIN;
261 }
262
263 static inline bool is_type_compound(const type_t *type)
264 {
265         assert(!is_typeref(type));
266         return type->kind == TYPE_COMPOUND_STRUCT
267                 || type->kind == TYPE_COMPOUND_UNION;
268 }
269
270 static inline bool is_type_valid(const type_t *type)
271 {
272         assert(!is_typeref(type));
273         return type->kind != TYPE_ERROR;
274 }
275
276 #endif