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 #include "type_t.h"
21 #include "types.h"
22 #include "lang_features.h"
23 #include "entity_t.h"
24 
25 /** The error type. */
26 type_t *type_error_type;
27 
28 type_t *type_bool;
29 type_t *type_char;
30 type_t *type_const_char;
31 type_t *type_double;
32 type_t *type_float;
33 type_t *type_int;
34 type_t *type_long_double;
35 type_t *type_long_long;
36 type_t *type_long;
37 type_t *type_short;
38 type_t *type_unsigned_short;
39 type_t *type_signed_char;
40 type_t *type_unsigned_char;
41 type_t *type_unsigned_int;
42 type_t *type_unsigned_long_long;
43 type_t *type_unsigned_long;
44 type_t *type_void;
45 
46 type_t *type_char_ptr;
47 type_t *type_char_ptr_restrict;
48 type_t *type_const_char_ptr;
49 type_t *type_const_char_ptr_restrict;
50 type_t *type_int_ptr;
51 type_t *type_long_long_ptr;
52 type_t *type_long_ptr;
53 type_t *type_unsigned_long_ptr;
54 type_t *type_short_ptr;
55 type_t *type_signed_char_ptr;
56 type_t *type_void_ptr;
57 type_t *type_const_void;
58 type_t *type_const_void_ptr;
59 type_t *type_void_ptr_restrict;
60 type_t *type_const_void_ptr_restrict;
61 
62 type_t *type_char_ptr_ptr;
63 
64 type_t *type_char16_t;
65 type_t *type_char32_t;
66 type_t *type_char16_t_const;
67 type_t *type_char32_t_const;
68 type_t *type_intmax_t;
69 type_t *type_ptrdiff_t;
70 type_t *type_size_t;
71 type_t *type_ssize_t;
72 type_t *type_uintmax_t;
73 type_t *type_uptrdiff_t;
74 type_t *type_wchar_t;
75 type_t *type_const_wchar_t;
76 type_t *type_wint_t;
77 type_t *type_int32_t;
78 type_t *type_int64_t;
79 
80 type_t *type_char16_t_ptr;
81 type_t *type_char32_t_ptr;
82 type_t *type_char16_t_const_ptr;
83 type_t *type_char32_t_const_ptr;
84 type_t *type_intmax_t_ptr;
85 type_t *type_ptrdiff_t_ptr;
86 type_t *type_ssize_t_ptr;
87 type_t *type_wchar_t_ptr;
88 type_t *type_const_wchar_t_ptr;
89 
90 type_t *type_valist;
91 
92 /* microsoft types */
93 atomic_type_kind_t int8_type_kind;
94 atomic_type_kind_t int16_type_kind;
95 atomic_type_kind_t int32_type_kind;
96 atomic_type_kind_t int64_type_kind;
97 atomic_type_kind_t int128_type_kind;
98 atomic_type_kind_t unsigned_int8_type_kind;
99 atomic_type_kind_t unsigned_int16_type_kind;
100 atomic_type_kind_t unsigned_int32_type_kind;
101 atomic_type_kind_t unsigned_int64_type_kind;
102 atomic_type_kind_t unsigned_int128_type_kind;
103 
104 type_t *type_int8;
105 type_t *type_int16;
106 type_t *type_int32;
107 type_t *type_int64;
108 type_t *type_unsigned_int8;
109 type_t *type_unsigned_int16;
110 type_t *type_unsigned_int32;
111 type_t *type_unsigned_int64;
112 type_t *type_int64_ptr;
113 
114 
init_basic_types(void)115 void init_basic_types(void)
116 {
117 	static const type_base_t error = { TYPE_ERROR, TYPE_QUALIFIER_NONE, NULL };
118 
119 	type_error_type         = (type_t*)&error;
120 	type_bool               = make_atomic_type(ATOMIC_TYPE_BOOL,        TYPE_QUALIFIER_NONE);
121 	type_signed_char        = make_atomic_type(ATOMIC_TYPE_SCHAR,       TYPE_QUALIFIER_NONE);
122 	type_unsigned_char      = make_atomic_type(ATOMIC_TYPE_UCHAR,       TYPE_QUALIFIER_NONE);
123 	type_short              = make_atomic_type(ATOMIC_TYPE_SHORT,       TYPE_QUALIFIER_NONE);
124 	type_unsigned_short     = make_atomic_type(ATOMIC_TYPE_USHORT,      TYPE_QUALIFIER_NONE);
125 	type_int                = make_atomic_type(ATOMIC_TYPE_INT,         TYPE_QUALIFIER_NONE);
126 	type_unsigned_int       = make_atomic_type(ATOMIC_TYPE_UINT,        TYPE_QUALIFIER_NONE);
127 	type_long               = make_atomic_type(ATOMIC_TYPE_LONG,        TYPE_QUALIFIER_NONE);
128 	type_unsigned_long      = make_atomic_type(ATOMIC_TYPE_ULONG,       TYPE_QUALIFIER_NONE);
129 	type_long_long          = make_atomic_type(ATOMIC_TYPE_LONGLONG,    TYPE_QUALIFIER_NONE);
130 	type_unsigned_long_long = make_atomic_type(ATOMIC_TYPE_ULONGLONG,   TYPE_QUALIFIER_NONE);
131 	type_long_double        = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
132 	type_double             = make_atomic_type(ATOMIC_TYPE_DOUBLE,      TYPE_QUALIFIER_NONE);
133 	type_float              = make_atomic_type(ATOMIC_TYPE_FLOAT,       TYPE_QUALIFIER_NONE);
134 	type_char               = make_atomic_type(ATOMIC_TYPE_CHAR,        TYPE_QUALIFIER_NONE);
135 	type_void               = make_atomic_type(ATOMIC_TYPE_VOID,        TYPE_QUALIFIER_NONE);
136 	type_const_void         = make_atomic_type(ATOMIC_TYPE_VOID,        TYPE_QUALIFIER_CONST);
137 
138 	int8_type_kind  = find_signed_int_atomic_type_kind_for_size(1);
139 	int16_type_kind = find_signed_int_atomic_type_kind_for_size(2);
140 	int32_type_kind = find_signed_int_atomic_type_kind_for_size(4);
141 	int64_type_kind = find_signed_int_atomic_type_kind_for_size(8);
142 
143 	type_int32_t = make_atomic_type(int32_type_kind, TYPE_QUALIFIER_NONE);
144 	type_int64_t = make_atomic_type(int64_type_kind, TYPE_QUALIFIER_NONE);
145 
146 	/* microsoft types */
147 	if (c_mode & _MS) {
148 		type_int8                = make_atomic_type(int8_type_kind, TYPE_QUALIFIER_NONE);
149 		type_int16               = make_atomic_type(int16_type_kind, TYPE_QUALIFIER_NONE);
150 		type_int32               = make_atomic_type(int32_type_kind, TYPE_QUALIFIER_NONE);
151 		type_int64               = make_atomic_type(int64_type_kind, TYPE_QUALIFIER_NONE);
152 		unsigned_int8_type_kind  = find_unsigned_int_atomic_type_kind_for_size(1);
153 		type_unsigned_int8       = make_atomic_type(unsigned_int8_type_kind, TYPE_QUALIFIER_NONE);
154 		unsigned_int16_type_kind = find_unsigned_int_atomic_type_kind_for_size(2);
155 		type_unsigned_int16      = make_atomic_type(unsigned_int16_type_kind, TYPE_QUALIFIER_NONE);
156 		unsigned_int32_type_kind = find_unsigned_int_atomic_type_kind_for_size(4);
157 		type_unsigned_int32      = make_atomic_type(unsigned_int32_type_kind, TYPE_QUALIFIER_NONE);
158 		unsigned_int64_type_kind = find_unsigned_int_atomic_type_kind_for_size(8);
159 		type_unsigned_int64      = make_atomic_type(unsigned_int64_type_kind, TYPE_QUALIFIER_NONE);
160 
161 		/* pointer types */
162 		type_int64_ptr           = make_pointer_type(type_int64,              TYPE_QUALIFIER_NONE);
163 	}
164 
165 	/* pointer types */
166 	type_void_ptr           = make_pointer_type(type_void,              TYPE_QUALIFIER_NONE);
167 	type_const_void_ptr     = make_pointer_type(type_const_void,        TYPE_QUALIFIER_NONE);
168 	type_void_ptr_restrict  = make_pointer_type(type_void,              TYPE_QUALIFIER_RESTRICT);
169 	type_const_void_ptr_restrict
170 	                        = make_pointer_type(type_const_void,        TYPE_QUALIFIER_RESTRICT);
171 	type_char_ptr           = make_pointer_type(type_char,              TYPE_QUALIFIER_NONE);
172 	type_char_ptr_restrict  = make_pointer_type(type_char,              TYPE_QUALIFIER_RESTRICT);
173 	type_signed_char_ptr    = make_pointer_type(type_signed_char,       TYPE_QUALIFIER_NONE);
174 	type_short_ptr          = make_pointer_type(type_short,             TYPE_QUALIFIER_NONE);
175 	type_int_ptr            = make_pointer_type(type_int,               TYPE_QUALIFIER_NONE);
176 	type_long_ptr           = make_pointer_type(type_long,              TYPE_QUALIFIER_NONE);
177 	type_unsigned_long_ptr  = make_pointer_type(type_unsigned_long,     TYPE_QUALIFIER_NONE);
178 	type_long_long_ptr      = make_pointer_type(type_long_long,         TYPE_QUALIFIER_NONE);
179 
180 	type_char_ptr_ptr       = make_pointer_type(type_char_ptr,          TYPE_QUALIFIER_NONE);
181 	type_valist             = type_void_ptr;
182 
183 	/* const character types */
184 	type_const_char         = make_atomic_type(ATOMIC_TYPE_CHAR,        TYPE_QUALIFIER_CONST);
185 	type_const_char_ptr     = make_pointer_type(type_const_char,        TYPE_QUALIFIER_NONE);
186 	type_const_char_ptr_restrict = make_pointer_type(type_const_char,        TYPE_QUALIFIER_RESTRICT);
187 
188 	/* other types */
189 	type_intmax_t    = type_long_long;
190 	type_size_t      = type_unsigned_long;
191 	type_ssize_t     = type_long;
192 	type_ptrdiff_t   = type_long;
193 	type_uintmax_t   = type_unsigned_long_long;
194 	type_uptrdiff_t  = type_unsigned_long;
195 	type_wint_t      = type_int;
196 
197 	type_intmax_t_ptr  = make_pointer_type(type_intmax_t,  TYPE_QUALIFIER_NONE);
198 	type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
199 	type_ssize_t_ptr   = make_pointer_type(type_ssize_t,   TYPE_QUALIFIER_NONE);
200 }
201 
init_wchar_types(atomic_type_kind_t akind)202 void init_wchar_types(atomic_type_kind_t akind)
203 {
204 	type_wchar_t       = make_atomic_type(akind, TYPE_QUALIFIER_NONE);
205 	type_const_wchar_t = make_atomic_type(akind, TYPE_QUALIFIER_CONST);
206 	type_wchar_t_ptr   = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
207 	type_const_wchar_t_ptr
208 		= make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
209 
210 	atomic_type_kind_t const u2 = find_unsigned_int_atomic_type_kind_for_size(2);
211 	type_char16_t           = make_atomic_type(u2, TYPE_QUALIFIER_NONE);
212 	type_char16_t_const     = make_atomic_type(u2, TYPE_QUALIFIER_CONST);
213 	type_char16_t_ptr       = make_pointer_type(type_char16_t,       TYPE_QUALIFIER_NONE);
214 	type_char16_t_const_ptr = make_pointer_type(type_char16_t_const, TYPE_QUALIFIER_NONE);
215 
216 	atomic_type_kind_t const u4 = find_unsigned_int_atomic_type_kind_for_size(4);
217 	type_char32_t           = make_atomic_type(u4, TYPE_QUALIFIER_NONE);
218 	type_char32_t_const     = make_atomic_type(u4, TYPE_QUALIFIER_CONST);
219 	type_char32_t_ptr       = make_pointer_type(type_char32_t,       TYPE_QUALIFIER_NONE);
220 	type_char32_t_const_ptr = make_pointer_type(type_char32_t_const, TYPE_QUALIFIER_NONE);
221 }
222