]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Backend support for Fortran 95 basic types and derived types. |
818ab71a | 2 | Copyright (C) 2002-2016 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Paul Brook <paul@nowt.org> |
4 | and Steven Bosscher <s.bosscher@student.tudelft.nl> | |
5 | ||
9fc4d79b | 6 | This file is part of GCC. |
6de9cd9a | 7 | |
9fc4d79b TS |
8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
d234d788 | 10 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 11 | version. |
6de9cd9a | 12 | |
9fc4d79b TS |
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
6de9cd9a DN |
17 | |
18 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
21 | |
22 | /* trans-types.c -- gfortran backend types */ | |
23 | ||
24 | #include "config.h" | |
25 | #include "system.h" | |
26 | #include "coretypes.h" | |
2adfab87 | 27 | #include "target.h" |
6de9cd9a | 28 | #include "tree.h" |
2adfab87 AM |
29 | #include "gfortran.h" |
30 | #include "trans.h" | |
31 | #include "stringpool.h" | |
40e23961 | 32 | #include "fold-const.h" |
d8a2d370 | 33 | #include "stor-layout.h" |
a48ba7e1 | 34 | #include "langhooks.h" /* For iso-c-bindings.def. */ |
c829d016 | 35 | #include "toplev.h" /* For rest_of_decl_compilation. */ |
6de9cd9a DN |
36 | #include "trans-types.h" |
37 | #include "trans-const.h" | |
a48ba7e1 | 38 | #include "dwarf2out.h" /* For struct array_descr_info. */ |
6de9cd9a DN |
39 | \f |
40 | ||
41 | #if (GFC_MAX_DIMENSIONS < 10) | |
42 | #define GFC_RANK_DIGITS 1 | |
43 | #define GFC_RANK_PRINTF_FORMAT "%01d" | |
44 | #elif (GFC_MAX_DIMENSIONS < 100) | |
45 | #define GFC_RANK_DIGITS 2 | |
46 | #define GFC_RANK_PRINTF_FORMAT "%02d" | |
47 | #else | |
48 | #error If you really need >99 dimensions, continue the sequence above... | |
49 | #endif | |
50 | ||
a8b3b0b6 CR |
51 | /* array of structs so we don't have to worry about xmalloc or free */ |
52 | CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER]; | |
53 | ||
6de9cd9a | 54 | tree gfc_array_index_type; |
b4838d29 | 55 | tree gfc_array_range_type; |
7e6de2a5 | 56 | tree gfc_character1_type_node; |
6de9cd9a | 57 | tree pvoid_type_node; |
10174ddf | 58 | tree prvoid_type_node; |
6de9cd9a DN |
59 | tree ppvoid_type_node; |
60 | tree pchar_type_node; | |
089db47d | 61 | tree pfunc_type_node; |
7e6de2a5 | 62 | |
d7177ab2 | 63 | tree gfc_charlen_type_node; |
6de9cd9a | 64 | |
a3c85b74 FXC |
65 | tree float128_type_node = NULL_TREE; |
66 | tree complex_float128_type_node = NULL_TREE; | |
67 | ||
68 | bool gfc_real16_is_float128 = false; | |
69 | ||
e2cad04b | 70 | static GTY(()) tree gfc_desc_dim_type; |
6de9cd9a | 71 | static GTY(()) tree gfc_max_array_element_size; |
c62c6622 TB |
72 | static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)]; |
73 | static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)]; | |
6de9cd9a | 74 | |
5e8e542f RH |
75 | /* Arrays for all integral and real kinds. We'll fill this in at runtime |
76 | after the target has a chance to process command-line options. */ | |
77 | ||
78 | #define MAX_INT_KINDS 5 | |
79 | gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1]; | |
80 | gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1]; | |
e2cad04b RH |
81 | static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1]; |
82 | static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1]; | |
5e8e542f | 83 | |
e8f35d4d | 84 | #define MAX_REAL_KINDS 5 |
5e8e542f | 85 | gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1]; |
e2cad04b RH |
86 | static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1]; |
87 | static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1]; | |
5e8e542f | 88 | |
374929b2 FXC |
89 | #define MAX_CHARACTER_KINDS 2 |
90 | gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1]; | |
91 | static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1]; | |
92 | static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1]; | |
93 | ||
35151cd5 | 94 | static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **); |
66e4ab31 | 95 | |
5e8e542f RH |
96 | /* The integer kind to use for array indices. This will be set to the |
97 | proper value based on target information from the backend. */ | |
98 | ||
99 | int gfc_index_integer_kind; | |
100 | ||
101 | /* The default kinds of the various types. */ | |
102 | ||
9d64df18 | 103 | int gfc_default_integer_kind; |
f4e7375a | 104 | int gfc_max_integer_kind; |
9d64df18 TS |
105 | int gfc_default_real_kind; |
106 | int gfc_default_double_kind; | |
107 | int gfc_default_character_kind; | |
108 | int gfc_default_logical_kind; | |
109 | int gfc_default_complex_kind; | |
e8525382 | 110 | int gfc_c_int_kind; |
da661a58 TB |
111 | int gfc_atomic_int_kind; |
112 | int gfc_atomic_logical_kind; | |
5e8e542f | 113 | |
4fec64b0 JD |
114 | /* The kind size used for record offsets. If the target system supports |
115 | kind=8, this will be set to 8, otherwise it is set to 4. */ | |
f04986a9 | 116 | int gfc_intio_kind; |
4fec64b0 | 117 | |
f1412ca5 FXC |
118 | /* The integer kind used to store character lengths. */ |
119 | int gfc_charlen_int_kind; | |
120 | ||
39f87c03 FXC |
121 | /* The size of the numeric storage unit and character storage unit. */ |
122 | int gfc_numeric_storage_size; | |
123 | int gfc_character_storage_size; | |
124 | ||
a8b3b0b6 | 125 | |
524af0d6 | 126 | bool |
a8b3b0b6 CR |
127 | gfc_check_any_c_kind (gfc_typespec *ts) |
128 | { | |
129 | int i; | |
f04986a9 | 130 | |
a8b3b0b6 CR |
131 | for (i = 0; i < ISOCBINDING_NUMBER; i++) |
132 | { | |
133 | /* Check for any C interoperable kind for the given type/kind in ts. | |
134 | This can be used after verify_c_interop to make sure that the | |
135 | Fortran kind being used exists in at least some form for C. */ | |
136 | if (c_interop_kinds_table[i].f90_type == ts->type && | |
137 | c_interop_kinds_table[i].value == ts->kind) | |
524af0d6 | 138 | return true; |
a8b3b0b6 CR |
139 | } |
140 | ||
524af0d6 | 141 | return false; |
a8b3b0b6 CR |
142 | } |
143 | ||
144 | ||
145 | static int | |
146 | get_real_kind_from_node (tree type) | |
147 | { | |
148 | int i; | |
149 | ||
150 | for (i = 0; gfc_real_kinds[i].kind != 0; i++) | |
151 | if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type)) | |
152 | return gfc_real_kinds[i].kind; | |
153 | ||
154 | return -4; | |
155 | } | |
156 | ||
157 | static int | |
158 | get_int_kind_from_node (tree type) | |
159 | { | |
160 | int i; | |
161 | ||
162 | if (!type) | |
163 | return -2; | |
164 | ||
165 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
166 | if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type)) | |
167 | return gfc_integer_kinds[i].kind; | |
168 | ||
169 | return -1; | |
170 | } | |
171 | ||
e0a6661b FXC |
172 | /* Return a typenode for the "standard" C type with a given name. */ |
173 | static tree | |
174 | get_typenode_from_name (const char *name) | |
175 | { | |
176 | if (name == NULL || *name == '\0') | |
177 | return NULL_TREE; | |
178 | ||
179 | if (strcmp (name, "char") == 0) | |
180 | return char_type_node; | |
181 | if (strcmp (name, "unsigned char") == 0) | |
182 | return unsigned_char_type_node; | |
183 | if (strcmp (name, "signed char") == 0) | |
184 | return signed_char_type_node; | |
185 | ||
186 | if (strcmp (name, "short int") == 0) | |
187 | return short_integer_type_node; | |
188 | if (strcmp (name, "short unsigned int") == 0) | |
189 | return short_unsigned_type_node; | |
190 | ||
191 | if (strcmp (name, "int") == 0) | |
192 | return integer_type_node; | |
193 | if (strcmp (name, "unsigned int") == 0) | |
194 | return unsigned_type_node; | |
195 | ||
196 | if (strcmp (name, "long int") == 0) | |
197 | return long_integer_type_node; | |
198 | if (strcmp (name, "long unsigned int") == 0) | |
199 | return long_unsigned_type_node; | |
200 | ||
201 | if (strcmp (name, "long long int") == 0) | |
202 | return long_long_integer_type_node; | |
203 | if (strcmp (name, "long long unsigned int") == 0) | |
204 | return long_long_unsigned_type_node; | |
205 | ||
206 | gcc_unreachable (); | |
207 | } | |
208 | ||
209 | static int | |
210 | get_int_kind_from_name (const char *name) | |
211 | { | |
212 | return get_int_kind_from_node (get_typenode_from_name (name)); | |
213 | } | |
214 | ||
215 | ||
216 | /* Get the kind number corresponding to an integer of given size, | |
217 | following the required return values for ISO_FORTRAN_ENV INT* constants: | |
218 | -2 is returned if we support a kind of larger size, -1 otherwise. */ | |
219 | int | |
220 | gfc_get_int_kind_from_width_isofortranenv (int size) | |
221 | { | |
222 | int i; | |
223 | ||
224 | /* Look for a kind with matching storage size. */ | |
225 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
226 | if (gfc_integer_kinds[i].bit_size == size) | |
227 | return gfc_integer_kinds[i].kind; | |
228 | ||
229 | /* Look for a kind with larger storage size. */ | |
230 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
231 | if (gfc_integer_kinds[i].bit_size > size) | |
232 | return -2; | |
233 | ||
234 | return -1; | |
235 | } | |
236 | ||
237 | /* Get the kind number corresponding to a real of given storage size, | |
238 | following the required return values for ISO_FORTRAN_ENV REAL* constants: | |
239 | -2 is returned if we support a kind of larger size, -1 otherwise. */ | |
240 | int | |
241 | gfc_get_real_kind_from_width_isofortranenv (int size) | |
242 | { | |
243 | int i; | |
244 | ||
245 | size /= 8; | |
246 | ||
247 | /* Look for a kind with matching storage size. */ | |
248 | for (i = 0; gfc_real_kinds[i].kind != 0; i++) | |
249 | if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size) | |
250 | return gfc_real_kinds[i].kind; | |
251 | ||
252 | /* Look for a kind with larger storage size. */ | |
253 | for (i = 0; gfc_real_kinds[i].kind != 0; i++) | |
254 | if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size) | |
255 | return -2; | |
256 | ||
257 | return -1; | |
258 | } | |
259 | ||
260 | ||
261 | ||
a8b3b0b6 CR |
262 | static int |
263 | get_int_kind_from_width (int size) | |
264 | { | |
265 | int i; | |
266 | ||
267 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
268 | if (gfc_integer_kinds[i].bit_size == size) | |
269 | return gfc_integer_kinds[i].kind; | |
270 | ||
271 | return -2; | |
272 | } | |
273 | ||
274 | static int | |
275 | get_int_kind_from_minimal_width (int size) | |
276 | { | |
277 | int i; | |
278 | ||
279 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
280 | if (gfc_integer_kinds[i].bit_size >= size) | |
281 | return gfc_integer_kinds[i].kind; | |
282 | ||
283 | return -2; | |
284 | } | |
285 | ||
286 | ||
287 | /* Generate the CInteropKind_t objects for the C interoperable | |
288 | kinds. */ | |
289 | ||
28d0b595 TB |
290 | void |
291 | gfc_init_c_interop_kinds (void) | |
a8b3b0b6 CR |
292 | { |
293 | int i; | |
a8b3b0b6 CR |
294 | |
295 | /* init all pointers in the list to NULL */ | |
296 | for (i = 0; i < ISOCBINDING_NUMBER; i++) | |
297 | { | |
298 | /* Initialize the name and value fields. */ | |
299 | c_interop_kinds_table[i].name[0] = '\0'; | |
300 | c_interop_kinds_table[i].value = -100; | |
301 | c_interop_kinds_table[i].f90_type = BT_UNKNOWN; | |
302 | } | |
303 | ||
21684705 | 304 | #define NAMED_INTCST(a,b,c,d) \ |
a8b3b0b6 CR |
305 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ |
306 | c_interop_kinds_table[a].f90_type = BT_INTEGER; \ | |
307 | c_interop_kinds_table[a].value = c; | |
28d0b595 | 308 | #define NAMED_REALCST(a,b,c,d) \ |
a8b3b0b6 CR |
309 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ |
310 | c_interop_kinds_table[a].f90_type = BT_REAL; \ | |
311 | c_interop_kinds_table[a].value = c; | |
28d0b595 | 312 | #define NAMED_CMPXCST(a,b,c,d) \ |
a8b3b0b6 CR |
313 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ |
314 | c_interop_kinds_table[a].f90_type = BT_COMPLEX; \ | |
315 | c_interop_kinds_table[a].value = c; | |
316 | #define NAMED_LOGCST(a,b,c) \ | |
317 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
318 | c_interop_kinds_table[a].f90_type = BT_LOGICAL; \ | |
319 | c_interop_kinds_table[a].value = c; | |
320 | #define NAMED_CHARKNDCST(a,b,c) \ | |
321 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
322 | c_interop_kinds_table[a].f90_type = BT_CHARACTER; \ | |
323 | c_interop_kinds_table[a].value = c; | |
324 | #define NAMED_CHARCST(a,b,c) \ | |
325 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
326 | c_interop_kinds_table[a].f90_type = BT_CHARACTER; \ | |
327 | c_interop_kinds_table[a].value = c; | |
328 | #define DERIVED_TYPE(a,b,c) \ | |
329 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ | |
330 | c_interop_kinds_table[a].f90_type = BT_DERIVED; \ | |
331 | c_interop_kinds_table[a].value = c; | |
cadddfdd | 332 | #define NAMED_FUNCTION(a,b,c,d) \ |
a8b3b0b6 CR |
333 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ |
334 | c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \ | |
cadddfdd TB |
335 | c_interop_kinds_table[a].value = c; |
336 | #define NAMED_SUBROUTINE(a,b,c,d) \ | |
d000aa67 TB |
337 | strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \ |
338 | c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \ | |
339 | c_interop_kinds_table[a].value = c; | |
340 | #include "iso-c-binding.def" | |
a8b3b0b6 CR |
341 | } |
342 | ||
343 | ||
5e8e542f RH |
344 | /* Query the target to determine which machine modes are available for |
345 | computation. Choose KIND numbers for them. */ | |
346 | ||
347 | void | |
348 | gfc_init_kinds (void) | |
349 | { | |
09639a83 | 350 | unsigned int mode; |
374929b2 | 351 | int i_index, r_index, kind; |
5e8e542f | 352 | bool saw_i4 = false, saw_i8 = false; |
f4347334 | 353 | bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false; |
5e8e542f RH |
354 | |
355 | for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++) | |
356 | { | |
357 | int kind, bitsize; | |
358 | ||
ef4bddc2 | 359 | if (!targetm.scalar_mode_supported_p ((machine_mode) mode)) |
5e8e542f RH |
360 | continue; |
361 | ||
04204c2f RH |
362 | /* The middle end doesn't support constants larger than 2*HWI. |
363 | Perhaps the target hook shouldn't have accepted these either, | |
364 | but just to be safe... */ | |
ef4bddc2 | 365 | bitsize = GET_MODE_BITSIZE ((machine_mode) mode); |
04204c2f RH |
366 | if (bitsize > 2*HOST_BITS_PER_WIDE_INT) |
367 | continue; | |
368 | ||
6e45f57b | 369 | gcc_assert (i_index != MAX_INT_KINDS); |
5e8e542f RH |
370 | |
371 | /* Let the kind equal the bit size divided by 8. This insulates the | |
372 | programmer from the underlying byte size. */ | |
5e8e542f RH |
373 | kind = bitsize / 8; |
374 | ||
375 | if (kind == 4) | |
376 | saw_i4 = true; | |
377 | if (kind == 8) | |
378 | saw_i8 = true; | |
379 | ||
380 | gfc_integer_kinds[i_index].kind = kind; | |
381 | gfc_integer_kinds[i_index].radix = 2; | |
382 | gfc_integer_kinds[i_index].digits = bitsize - 1; | |
383 | gfc_integer_kinds[i_index].bit_size = bitsize; | |
384 | ||
385 | gfc_logical_kinds[i_index].kind = kind; | |
386 | gfc_logical_kinds[i_index].bit_size = bitsize; | |
387 | ||
388 | i_index += 1; | |
389 | } | |
390 | ||
f04986a9 | 391 | /* Set the kind used to match GFC_INT_IO in libgfortran. This is |
4fec64b0 JD |
392 | used for large file access. */ |
393 | ||
394 | if (saw_i8) | |
014ec6ee | 395 | gfc_intio_kind = 8; |
4fec64b0 | 396 | else |
014ec6ee | 397 | gfc_intio_kind = 4; |
4fec64b0 | 398 | |
f04986a9 PT |
399 | /* If we do not at least have kind = 4, everything is pointless. */ |
400 | gcc_assert(saw_i4); | |
4fec64b0 | 401 | |
f4e7375a SK |
402 | /* Set the maximum integer kind. Used with at least BOZ constants. */ |
403 | gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind; | |
404 | ||
5e8e542f RH |
405 | for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++) |
406 | { | |
09639a83 | 407 | const struct real_format *fmt = |
ef4bddc2 | 408 | REAL_MODE_FORMAT ((machine_mode) mode); |
5e8e542f RH |
409 | int kind; |
410 | ||
411 | if (fmt == NULL) | |
412 | continue; | |
ef4bddc2 | 413 | if (!targetm.scalar_mode_supported_p ((machine_mode) mode)) |
5e8e542f RH |
414 | continue; |
415 | ||
a3c85b74 FXC |
416 | /* Only let float, double, long double and __float128 go through. |
417 | Runtime support for others is not provided, so they would be | |
87e6d9dc | 418 | useless. */ |
21c0a521 | 419 | if (!targetm.libgcc_floating_mode_supported_p ((machine_mode) |
8cc4b7a2 | 420 | mode)) |
21c0a521 DM |
421 | continue; |
422 | if (mode != TYPE_MODE (float_type_node) | |
1ec601bf FXC |
423 | && (mode != TYPE_MODE (double_type_node)) |
424 | && (mode != TYPE_MODE (long_double_type_node)) | |
8cc4b7a2 | 425 | #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT) |
1ec601bf FXC |
426 | && (mode != TFmode) |
427 | #endif | |
428 | ) | |
216ac520 SE |
429 | continue; |
430 | ||
5e8e542f RH |
431 | /* Let the kind equal the precision divided by 8, rounding up. Again, |
432 | this insulates the programmer from the underlying byte size. | |
433 | ||
434 | Also, it effectively deals with IEEE extended formats. There, the | |
435 | total size of the type may equal 16, but it's got 6 bytes of padding | |
436 | and the increased size can get in the way of a real IEEE quad format | |
437 | which may also be supported by the target. | |
438 | ||
439 | We round up so as to handle IA-64 __floatreg (RFmode), which is an | |
440 | 82 bit type. Not to be confused with __float80 (XFmode), which is | |
441 | an 80 bit type also supported by IA-64. So XFmode should come out | |
442 | to be kind=10, and RFmode should come out to be kind=11. Egads. */ | |
443 | ||
444 | kind = (GET_MODE_PRECISION (mode) + 7) / 8; | |
445 | ||
446 | if (kind == 4) | |
447 | saw_r4 = true; | |
448 | if (kind == 8) | |
449 | saw_r8 = true; | |
f4347334 ZG |
450 | if (kind == 10) |
451 | saw_r10 = true; | |
5e8e542f RH |
452 | if (kind == 16) |
453 | saw_r16 = true; | |
454 | ||
df2fba9e | 455 | /* Careful we don't stumble a weird internal mode. */ |
6e45f57b | 456 | gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind); |
5e8e542f | 457 | /* Or have too many modes for the allocated space. */ |
6e45f57b | 458 | gcc_assert (r_index != MAX_REAL_KINDS); |
5e8e542f RH |
459 | |
460 | gfc_real_kinds[r_index].kind = kind; | |
461 | gfc_real_kinds[r_index].radix = fmt->b; | |
462 | gfc_real_kinds[r_index].digits = fmt->p; | |
463 | gfc_real_kinds[r_index].min_exponent = fmt->emin; | |
464 | gfc_real_kinds[r_index].max_exponent = fmt->emax; | |
c69cb92f AM |
465 | if (fmt->pnan < fmt->p) |
466 | /* This is an IBM extended double format (or the MIPS variant) | |
467 | made up of two IEEE doubles. The value of the long double is | |
468 | the sum of the values of the two parts. The most significant | |
469 | part is required to be the value of the long double rounded | |
470 | to the nearest double. If we use emax of 1024 then we can't | |
471 | represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because | |
472 | rounding will make the most significant part overflow. */ | |
473 | gfc_real_kinds[r_index].max_exponent = fmt->emax - 1; | |
e2cad04b | 474 | gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode); |
5e8e542f RH |
475 | r_index += 1; |
476 | } | |
477 | ||
f4347334 ZG |
478 | /* Choose the default integer kind. We choose 4 unless the user directs us |
479 | otherwise. Even if the user specified that the default integer kind is 8, | |
480 | the numeric storage size is not 64 bits. In this case, a warning will be | |
481 | issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */ | |
482 | ||
483 | gfc_numeric_storage_size = 4 * 8; | |
484 | ||
203c7ebf | 485 | if (flag_default_integer) |
5e8e542f RH |
486 | { |
487 | if (!saw_i8) | |
ddc05d11 TB |
488 | gfc_fatal_error ("INTEGER(KIND=8) is not available for " |
489 | "%<-fdefault-integer-8%> option"); | |
f4347334 | 490 | |
9d64df18 | 491 | gfc_default_integer_kind = 8; |
39f87c03 | 492 | |
f4347334 | 493 | } |
203c7ebf | 494 | else if (flag_integer4_kind == 8) |
f4347334 ZG |
495 | { |
496 | if (!saw_i8) | |
ddc05d11 TB |
497 | gfc_fatal_error ("INTEGER(KIND=8) is not available for " |
498 | "%<-finteger-4-integer-8%> option"); | |
f4347334 ZG |
499 | |
500 | gfc_default_integer_kind = 8; | |
5e8e542f RH |
501 | } |
502 | else if (saw_i4) | |
39f87c03 FXC |
503 | { |
504 | gfc_default_integer_kind = 4; | |
39f87c03 | 505 | } |
5e8e542f | 506 | else |
39f87c03 FXC |
507 | { |
508 | gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind; | |
509 | gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size; | |
510 | } | |
5e8e542f RH |
511 | |
512 | /* Choose the default real kind. Again, we choose 4 when possible. */ | |
203c7ebf | 513 | if (flag_default_real) |
5e8e542f RH |
514 | { |
515 | if (!saw_r8) | |
ddc05d11 TB |
516 | gfc_fatal_error ("REAL(KIND=8) is not available for " |
517 | "%<-fdefault-real-8%> option"); | |
f4347334 | 518 | |
9d64df18 | 519 | gfc_default_real_kind = 8; |
5e8e542f | 520 | } |
203c7ebf | 521 | else if (flag_real4_kind == 8) |
f4347334 ZG |
522 | { |
523 | if (!saw_r8) | |
ddc05d11 TB |
524 | gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> " |
525 | "option"); | |
f4347334 ZG |
526 | |
527 | gfc_default_real_kind = 8; | |
528 | } | |
203c7ebf | 529 | else if (flag_real4_kind == 10) |
f4347334 ZG |
530 | { |
531 | if (!saw_r10) | |
ddc05d11 TB |
532 | gfc_fatal_error ("REAL(KIND=10) is not available for " |
533 | "%<-freal-4-real-10%> option"); | |
f4347334 ZG |
534 | |
535 | gfc_default_real_kind = 10; | |
536 | } | |
203c7ebf | 537 | else if (flag_real4_kind == 16) |
f4347334 ZG |
538 | { |
539 | if (!saw_r16) | |
ddc05d11 TB |
540 | gfc_fatal_error ("REAL(KIND=16) is not available for " |
541 | "%<-freal-4-real-16%> option"); | |
f4347334 ZG |
542 | |
543 | gfc_default_real_kind = 16; | |
544 | } | |
5e8e542f | 545 | else if (saw_r4) |
9d64df18 | 546 | gfc_default_real_kind = 4; |
5e8e542f | 547 | else |
9d64df18 | 548 | gfc_default_real_kind = gfc_real_kinds[0].kind; |
5e8e542f | 549 | |
f04986a9 | 550 | /* Choose the default double kind. If -fdefault-real and -fdefault-double |
3ae9eb27 SK |
551 | are specified, we use kind=8, if it's available. If -fdefault-real is |
552 | specified without -fdefault-double, we use kind=16, if it's available. | |
553 | Otherwise we do not change anything. */ | |
203c7ebf | 554 | if (flag_default_double && !flag_default_real) |
ddc05d11 TB |
555 | gfc_fatal_error ("Use of %<-fdefault-double-8%> requires " |
556 | "%<-fdefault-real-8%>"); | |
3ae9eb27 | 557 | |
203c7ebf | 558 | if (flag_default_real && flag_default_double && saw_r8) |
3ae9eb27 | 559 | gfc_default_double_kind = 8; |
203c7ebf | 560 | else if (flag_default_real && saw_r16) |
9d64df18 | 561 | gfc_default_double_kind = 16; |
203c7ebf | 562 | else if (flag_real8_kind == 4) |
f4347334 ZG |
563 | { |
564 | if (!saw_r4) | |
ddc05d11 TB |
565 | gfc_fatal_error ("REAL(KIND=4) is not available for " |
566 | "%<-freal-8-real-4%> option"); | |
f4347334 | 567 | |
21c0a521 | 568 | gfc_default_double_kind = 4; |
f4347334 | 569 | } |
203c7ebf | 570 | else if (flag_real8_kind == 10 ) |
f4347334 ZG |
571 | { |
572 | if (!saw_r10) | |
ddc05d11 TB |
573 | gfc_fatal_error ("REAL(KIND=10) is not available for " |
574 | "%<-freal-8-real-10%> option"); | |
f4347334 | 575 | |
21c0a521 | 576 | gfc_default_double_kind = 10; |
f4347334 | 577 | } |
203c7ebf | 578 | else if (flag_real8_kind == 16 ) |
f4347334 ZG |
579 | { |
580 | if (!saw_r16) | |
ddc05d11 TB |
581 | gfc_fatal_error ("REAL(KIND=10) is not available for " |
582 | "%<-freal-8-real-16%> option"); | |
f4347334 | 583 | |
21c0a521 | 584 | gfc_default_double_kind = 16; |
f4347334 | 585 | } |
5e8e542f | 586 | else if (saw_r4 && saw_r8) |
9d64df18 | 587 | gfc_default_double_kind = 8; |
5e8e542f RH |
588 | else |
589 | { | |
590 | /* F95 14.6.3.1: A nonpointer scalar object of type double precision | |
591 | real ... occupies two contiguous numeric storage units. | |
592 | ||
593 | Therefore we must be supplied a kind twice as large as we chose | |
594 | for single precision. There are loopholes, in that double | |
595 | precision must *occupy* two storage units, though it doesn't have | |
596 | to *use* two storage units. Which means that you can make this | |
597 | kind artificially wide by padding it. But at present there are | |
598 | no GCC targets for which a two-word type does not exist, so we | |
599 | just let gfc_validate_kind abort and tell us if something breaks. */ | |
600 | ||
9d64df18 TS |
601 | gfc_default_double_kind |
602 | = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false); | |
5e8e542f RH |
603 | } |
604 | ||
605 | /* The default logical kind is constrained to be the same as the | |
606 | default integer kind. Similarly with complex and real. */ | |
9d64df18 TS |
607 | gfc_default_logical_kind = gfc_default_integer_kind; |
608 | gfc_default_complex_kind = gfc_default_real_kind; | |
5e8e542f | 609 | |
374929b2 FXC |
610 | /* We only have two character kinds: ASCII and UCS-4. |
611 | ASCII corresponds to a 8-bit integer type, if one is available. | |
1cc0e193 | 612 | UCS-4 corresponds to a 32-bit integer type, if one is available. */ |
374929b2 FXC |
613 | i_index = 0; |
614 | if ((kind = get_int_kind_from_width (8)) > 0) | |
615 | { | |
616 | gfc_character_kinds[i_index].kind = kind; | |
617 | gfc_character_kinds[i_index].bit_size = 8; | |
618 | gfc_character_kinds[i_index].name = "ascii"; | |
619 | i_index++; | |
620 | } | |
621 | if ((kind = get_int_kind_from_width (32)) > 0) | |
622 | { | |
623 | gfc_character_kinds[i_index].kind = kind; | |
624 | gfc_character_kinds[i_index].bit_size = 32; | |
625 | gfc_character_kinds[i_index].name = "iso_10646"; | |
626 | i_index++; | |
627 | } | |
628 | ||
5e8e542f | 629 | /* Choose the smallest integer kind for our default character. */ |
374929b2 | 630 | gfc_default_character_kind = gfc_character_kinds[0].kind; |
39f87c03 | 631 | gfc_character_storage_size = gfc_default_character_kind * 8; |
5e8e542f | 632 | |
8395e28b TB |
633 | gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE); |
634 | ||
e8525382 SK |
635 | /* Pick a kind the same size as the C "int" type. */ |
636 | gfc_c_int_kind = INT_TYPE_SIZE / 8; | |
a8b3b0b6 | 637 | |
da661a58 TB |
638 | /* Choose atomic kinds to match C's int. */ |
639 | gfc_atomic_int_kind = gfc_c_int_kind; | |
640 | gfc_atomic_logical_kind = gfc_c_int_kind; | |
5e8e542f RH |
641 | } |
642 | ||
28d0b595 | 643 | |
5e8e542f RH |
644 | /* Make sure that a valid kind is present. Returns an index into the |
645 | associated kinds array, -1 if the kind is not present. */ | |
646 | ||
647 | static int | |
648 | validate_integer (int kind) | |
649 | { | |
650 | int i; | |
651 | ||
652 | for (i = 0; gfc_integer_kinds[i].kind != 0; i++) | |
653 | if (gfc_integer_kinds[i].kind == kind) | |
654 | return i; | |
655 | ||
656 | return -1; | |
657 | } | |
658 | ||
659 | static int | |
660 | validate_real (int kind) | |
661 | { | |
662 | int i; | |
663 | ||
664 | for (i = 0; gfc_real_kinds[i].kind != 0; i++) | |
665 | if (gfc_real_kinds[i].kind == kind) | |
666 | return i; | |
667 | ||
668 | return -1; | |
669 | } | |
670 | ||
671 | static int | |
672 | validate_logical (int kind) | |
673 | { | |
674 | int i; | |
675 | ||
676 | for (i = 0; gfc_logical_kinds[i].kind; i++) | |
677 | if (gfc_logical_kinds[i].kind == kind) | |
678 | return i; | |
679 | ||
680 | return -1; | |
681 | } | |
682 | ||
683 | static int | |
684 | validate_character (int kind) | |
685 | { | |
374929b2 FXC |
686 | int i; |
687 | ||
688 | for (i = 0; gfc_character_kinds[i].kind; i++) | |
689 | if (gfc_character_kinds[i].kind == kind) | |
690 | return i; | |
691 | ||
692 | return -1; | |
5e8e542f RH |
693 | } |
694 | ||
695 | /* Validate a kind given a basic type. The return value is the same | |
696 | for the child functions, with -1 indicating nonexistence of the | |
697 | type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */ | |
698 | ||
699 | int | |
700 | gfc_validate_kind (bt type, int kind, bool may_fail) | |
701 | { | |
702 | int rc; | |
703 | ||
704 | switch (type) | |
705 | { | |
706 | case BT_REAL: /* Fall through */ | |
707 | case BT_COMPLEX: | |
708 | rc = validate_real (kind); | |
709 | break; | |
710 | case BT_INTEGER: | |
711 | rc = validate_integer (kind); | |
712 | break; | |
713 | case BT_LOGICAL: | |
714 | rc = validate_logical (kind); | |
715 | break; | |
716 | case BT_CHARACTER: | |
717 | rc = validate_character (kind); | |
718 | break; | |
719 | ||
720 | default: | |
721 | gfc_internal_error ("gfc_validate_kind(): Got bad type"); | |
722 | } | |
723 | ||
724 | if (rc < 0 && !may_fail) | |
725 | gfc_internal_error ("gfc_validate_kind(): Got bad kind"); | |
726 | ||
727 | return rc; | |
728 | } | |
729 | ||
730 | ||
e2cad04b RH |
731 | /* Four subroutines of gfc_init_types. Create type nodes for the given kind. |
732 | Reuse common type nodes where possible. Recognize if the kind matches up | |
733 | with a C type. This will be used later in determining which routines may | |
734 | be scarfed from libm. */ | |
735 | ||
736 | static tree | |
737 | gfc_build_int_type (gfc_integer_info *info) | |
738 | { | |
739 | int mode_precision = info->bit_size; | |
740 | ||
741 | if (mode_precision == CHAR_TYPE_SIZE) | |
742 | info->c_char = 1; | |
743 | if (mode_precision == SHORT_TYPE_SIZE) | |
744 | info->c_short = 1; | |
745 | if (mode_precision == INT_TYPE_SIZE) | |
746 | info->c_int = 1; | |
747 | if (mode_precision == LONG_TYPE_SIZE) | |
748 | info->c_long = 1; | |
749 | if (mode_precision == LONG_LONG_TYPE_SIZE) | |
750 | info->c_long_long = 1; | |
751 | ||
752 | if (TYPE_PRECISION (intQI_type_node) == mode_precision) | |
753 | return intQI_type_node; | |
754 | if (TYPE_PRECISION (intHI_type_node) == mode_precision) | |
755 | return intHI_type_node; | |
756 | if (TYPE_PRECISION (intSI_type_node) == mode_precision) | |
757 | return intSI_type_node; | |
758 | if (TYPE_PRECISION (intDI_type_node) == mode_precision) | |
759 | return intDI_type_node; | |
760 | if (TYPE_PRECISION (intTI_type_node) == mode_precision) | |
761 | return intTI_type_node; | |
762 | ||
763 | return make_signed_type (mode_precision); | |
764 | } | |
765 | ||
0a05c536 | 766 | tree |
374929b2 FXC |
767 | gfc_build_uint_type (int size) |
768 | { | |
769 | if (size == CHAR_TYPE_SIZE) | |
770 | return unsigned_char_type_node; | |
771 | if (size == SHORT_TYPE_SIZE) | |
772 | return short_unsigned_type_node; | |
773 | if (size == INT_TYPE_SIZE) | |
774 | return unsigned_type_node; | |
775 | if (size == LONG_TYPE_SIZE) | |
776 | return long_unsigned_type_node; | |
777 | if (size == LONG_LONG_TYPE_SIZE) | |
778 | return long_long_unsigned_type_node; | |
779 | ||
780 | return make_unsigned_type (size); | |
781 | } | |
782 | ||
783 | ||
e2cad04b RH |
784 | static tree |
785 | gfc_build_real_type (gfc_real_info *info) | |
786 | { | |
787 | int mode_precision = info->mode_precision; | |
788 | tree new_type; | |
789 | ||
790 | if (mode_precision == FLOAT_TYPE_SIZE) | |
791 | info->c_float = 1; | |
792 | if (mode_precision == DOUBLE_TYPE_SIZE) | |
793 | info->c_double = 1; | |
794 | if (mode_precision == LONG_DOUBLE_TYPE_SIZE) | |
795 | info->c_long_double = 1; | |
a3c85b74 FXC |
796 | if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128) |
797 | { | |
798 | info->c_float128 = 1; | |
799 | gfc_real16_is_float128 = true; | |
800 | } | |
e2cad04b RH |
801 | |
802 | if (TYPE_PRECISION (float_type_node) == mode_precision) | |
803 | return float_type_node; | |
804 | if (TYPE_PRECISION (double_type_node) == mode_precision) | |
805 | return double_type_node; | |
806 | if (TYPE_PRECISION (long_double_type_node) == mode_precision) | |
807 | return long_double_type_node; | |
808 | ||
809 | new_type = make_node (REAL_TYPE); | |
810 | TYPE_PRECISION (new_type) = mode_precision; | |
811 | layout_type (new_type); | |
812 | return new_type; | |
813 | } | |
814 | ||
815 | static tree | |
816 | gfc_build_complex_type (tree scalar_type) | |
817 | { | |
818 | tree new_type; | |
819 | ||
820 | if (scalar_type == NULL) | |
821 | return NULL; | |
822 | if (scalar_type == float_type_node) | |
823 | return complex_float_type_node; | |
824 | if (scalar_type == double_type_node) | |
825 | return complex_double_type_node; | |
826 | if (scalar_type == long_double_type_node) | |
827 | return complex_long_double_type_node; | |
828 | ||
829 | new_type = make_node (COMPLEX_TYPE); | |
830 | TREE_TYPE (new_type) = scalar_type; | |
831 | layout_type (new_type); | |
832 | return new_type; | |
833 | } | |
834 | ||
835 | static tree | |
836 | gfc_build_logical_type (gfc_logical_info *info) | |
837 | { | |
838 | int bit_size = info->bit_size; | |
839 | tree new_type; | |
840 | ||
841 | if (bit_size == BOOL_TYPE_SIZE) | |
842 | { | |
843 | info->c_bool = 1; | |
844 | return boolean_type_node; | |
845 | } | |
846 | ||
847 | new_type = make_unsigned_type (bit_size); | |
848 | TREE_SET_CODE (new_type, BOOLEAN_TYPE); | |
849 | TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1); | |
850 | TYPE_PRECISION (new_type) = 1; | |
851 | ||
852 | return new_type; | |
853 | } | |
854 | ||
e0a6661b | 855 | |
6de9cd9a DN |
856 | /* Create the backend type nodes. We map them to their |
857 | equivalent C type, at least for now. We also give | |
858 | names to the types here, and we push them in the | |
859 | global binding level context.*/ | |
c3e8c6b8 | 860 | |
6de9cd9a DN |
861 | void |
862 | gfc_init_types (void) | |
863 | { | |
e5008df7 | 864 | char name_buf[18]; |
e2cad04b RH |
865 | int index; |
866 | tree type; | |
6de9cd9a | 867 | unsigned n; |
6de9cd9a | 868 | |
e2cad04b | 869 | /* Create and name the types. */ |
6de9cd9a | 870 | #define PUSH_TYPE(name, node) \ |
c2255bc4 AH |
871 | pushdecl (build_decl (input_location, \ |
872 | TYPE_DECL, get_identifier (name), node)) | |
6de9cd9a | 873 | |
e2cad04b RH |
874 | for (index = 0; gfc_integer_kinds[index].kind != 0; ++index) |
875 | { | |
876 | type = gfc_build_int_type (&gfc_integer_kinds[index]); | |
dead0bae JJ |
877 | /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */ |
878 | if (TYPE_STRING_FLAG (type)) | |
879 | type = make_signed_type (gfc_integer_kinds[index].bit_size); | |
e2cad04b | 880 | gfc_integer_types[index] = type; |
40373aa6 | 881 | snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)", |
e2cad04b RH |
882 | gfc_integer_kinds[index].kind); |
883 | PUSH_TYPE (name_buf, type); | |
884 | } | |
6de9cd9a | 885 | |
e2cad04b RH |
886 | for (index = 0; gfc_logical_kinds[index].kind != 0; ++index) |
887 | { | |
888 | type = gfc_build_logical_type (&gfc_logical_kinds[index]); | |
889 | gfc_logical_types[index] = type; | |
40373aa6 | 890 | snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)", |
e2cad04b RH |
891 | gfc_logical_kinds[index].kind); |
892 | PUSH_TYPE (name_buf, type); | |
893 | } | |
6de9cd9a | 894 | |
e2cad04b RH |
895 | for (index = 0; gfc_real_kinds[index].kind != 0; index++) |
896 | { | |
897 | type = gfc_build_real_type (&gfc_real_kinds[index]); | |
898 | gfc_real_types[index] = type; | |
40373aa6 | 899 | snprintf (name_buf, sizeof(name_buf), "real(kind=%d)", |
e2cad04b RH |
900 | gfc_real_kinds[index].kind); |
901 | PUSH_TYPE (name_buf, type); | |
902 | ||
a3c85b74 FXC |
903 | if (gfc_real_kinds[index].c_float128) |
904 | float128_type_node = type; | |
905 | ||
e2cad04b RH |
906 | type = gfc_build_complex_type (type); |
907 | gfc_complex_types[index] = type; | |
40373aa6 | 908 | snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)", |
e2cad04b RH |
909 | gfc_real_kinds[index].kind); |
910 | PUSH_TYPE (name_buf, type); | |
a3c85b74 FXC |
911 | |
912 | if (gfc_real_kinds[index].c_float128) | |
913 | complex_float128_type_node = type; | |
e2cad04b | 914 | } |
6de9cd9a | 915 | |
374929b2 FXC |
916 | for (index = 0; gfc_character_kinds[index].kind != 0; ++index) |
917 | { | |
918 | type = gfc_build_uint_type (gfc_character_kinds[index].bit_size); | |
919 | type = build_qualified_type (type, TYPE_UNQUALIFIED); | |
920 | snprintf (name_buf, sizeof(name_buf), "character(kind=%d)", | |
921 | gfc_character_kinds[index].kind); | |
922 | PUSH_TYPE (name_buf, type); | |
923 | gfc_character_types[index] = type; | |
924 | gfc_pcharacter_types[index] = build_pointer_type (type); | |
925 | } | |
926 | gfc_character1_type_node = gfc_character_types[0]; | |
6de9cd9a DN |
927 | |
928 | PUSH_TYPE ("byte", unsigned_char_type_node); | |
929 | PUSH_TYPE ("void", void_type_node); | |
930 | ||
931 | /* DBX debugging output gets upset if these aren't set. */ | |
932 | if (!TYPE_NAME (integer_type_node)) | |
933 | PUSH_TYPE ("c_integer", integer_type_node); | |
934 | if (!TYPE_NAME (char_type_node)) | |
935 | PUSH_TYPE ("c_char", char_type_node); | |
e2cad04b | 936 | |
6de9cd9a DN |
937 | #undef PUSH_TYPE |
938 | ||
939 | pvoid_type_node = build_pointer_type (void_type_node); | |
10174ddf | 940 | prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT); |
6de9cd9a DN |
941 | ppvoid_type_node = build_pointer_type (pvoid_type_node); |
942 | pchar_type_node = build_pointer_type (gfc_character1_type_node); | |
089db47d | 943 | pfunc_type_node |
b64fca63 | 944 | = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE)); |
6de9cd9a | 945 | |
6de9cd9a | 946 | gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind); |
b4838d29 ZD |
947 | /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type, |
948 | since this function is called before gfc_init_constants. */ | |
949 | gfc_array_range_type | |
950 | = build_range_type (gfc_array_index_type, | |
951 | build_int_cst (gfc_array_index_type, 0), | |
952 | NULL_TREE); | |
6de9cd9a DN |
953 | |
954 | /* The maximum array element size that can be handled is determined | |
955 | by the number of bits available to store this field in the array | |
956 | descriptor. */ | |
957 | ||
e2cad04b | 958 | n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT; |
7d60be94 | 959 | gfc_max_array_element_size |
fae3018d | 960 | = wide_int_to_tree (size_type_node, |
807e902e | 961 | wi::mask (n, UNSIGNED, |
fae3018d | 962 | TYPE_PRECISION (size_type_node))); |
6de9cd9a | 963 | |
e2cad04b | 964 | boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind); |
7d60be94 NS |
965 | boolean_true_node = build_int_cst (boolean_type_node, 1); |
966 | boolean_false_node = build_int_cst (boolean_type_node, 0); | |
e2cad04b RH |
967 | |
968 | /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */ | |
f1412ca5 FXC |
969 | gfc_charlen_int_kind = 4; |
970 | gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind); | |
6de9cd9a DN |
971 | } |
972 | ||
e2cad04b | 973 | /* Get the type node for the given type and kind. */ |
c3e8c6b8 | 974 | |
6de9cd9a DN |
975 | tree |
976 | gfc_get_int_type (int kind) | |
977 | { | |
644cb69f FXC |
978 | int index = gfc_validate_kind (BT_INTEGER, kind, true); |
979 | return index < 0 ? 0 : gfc_integer_types[index]; | |
6de9cd9a DN |
980 | } |
981 | ||
6de9cd9a DN |
982 | tree |
983 | gfc_get_real_type (int kind) | |
984 | { | |
644cb69f FXC |
985 | int index = gfc_validate_kind (BT_REAL, kind, true); |
986 | return index < 0 ? 0 : gfc_real_types[index]; | |
6de9cd9a DN |
987 | } |
988 | ||
6de9cd9a DN |
989 | tree |
990 | gfc_get_complex_type (int kind) | |
991 | { | |
644cb69f FXC |
992 | int index = gfc_validate_kind (BT_COMPLEX, kind, true); |
993 | return index < 0 ? 0 : gfc_complex_types[index]; | |
6de9cd9a DN |
994 | } |
995 | ||
6de9cd9a DN |
996 | tree |
997 | gfc_get_logical_type (int kind) | |
998 | { | |
644cb69f FXC |
999 | int index = gfc_validate_kind (BT_LOGICAL, kind, true); |
1000 | return index < 0 ? 0 : gfc_logical_types[index]; | |
6de9cd9a | 1001 | } |
374929b2 FXC |
1002 | |
1003 | tree | |
1004 | gfc_get_char_type (int kind) | |
1005 | { | |
1006 | int index = gfc_validate_kind (BT_CHARACTER, kind, true); | |
1007 | return index < 0 ? 0 : gfc_character_types[index]; | |
1008 | } | |
1009 | ||
1010 | tree | |
1011 | gfc_get_pchar_type (int kind) | |
1012 | { | |
1013 | int index = gfc_validate_kind (BT_CHARACTER, kind, true); | |
1014 | return index < 0 ? 0 : gfc_pcharacter_types[index]; | |
1015 | } | |
1016 | ||
6de9cd9a | 1017 | \f |
40f20186 | 1018 | /* Create a character type with the given kind and length. */ |
c3e8c6b8 | 1019 | |
6de9cd9a | 1020 | tree |
d393bbd7 | 1021 | gfc_get_character_type_len_for_eltype (tree eltype, tree len) |
6de9cd9a | 1022 | { |
e2cad04b | 1023 | tree bounds, type; |
6de9cd9a | 1024 | |
5e3b8727 | 1025 | bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len); |
d393bbd7 | 1026 | type = build_array_type (eltype, bounds); |
6de9cd9a DN |
1027 | TYPE_STRING_FLAG (type) = 1; |
1028 | ||
1029 | return type; | |
1030 | } | |
40f20186 | 1031 | |
d393bbd7 FXC |
1032 | tree |
1033 | gfc_get_character_type_len (int kind, tree len) | |
1034 | { | |
1035 | gfc_validate_kind (BT_CHARACTER, kind, false); | |
1036 | return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len); | |
1037 | } | |
1038 | ||
40f20186 PB |
1039 | |
1040 | /* Get a type node for a character kind. */ | |
1041 | ||
1042 | tree | |
1043 | gfc_get_character_type (int kind, gfc_charlen * cl) | |
1044 | { | |
1045 | tree len; | |
1046 | ||
1047 | len = (cl == NULL) ? NULL_TREE : cl->backend_decl; | |
1048 | ||
1049 | return gfc_get_character_type_len (kind, len); | |
1050 | } | |
6de9cd9a DN |
1051 | \f |
1052 | /* Covert a basic type. This will be an array for character types. */ | |
c3e8c6b8 | 1053 | |
6de9cd9a DN |
1054 | tree |
1055 | gfc_typenode_for_spec (gfc_typespec * spec) | |
1056 | { | |
1057 | tree basetype; | |
1058 | ||
1059 | switch (spec->type) | |
1060 | { | |
1061 | case BT_UNKNOWN: | |
6e45f57b | 1062 | gcc_unreachable (); |
6de9cd9a DN |
1063 | |
1064 | case BT_INTEGER: | |
a8b3b0b6 CR |
1065 | /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol |
1066 | has been resolved. This is done so we can convert C_PTR and | |
1067 | C_FUNPTR to simple variables that get translated to (void *). */ | |
1068 | if (spec->f90_type == BT_VOID) | |
089db47d | 1069 | { |
bc21d315 JW |
1070 | if (spec->u.derived |
1071 | && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR) | |
089db47d CR |
1072 | basetype = ptr_type_node; |
1073 | else | |
1074 | basetype = pfunc_type_node; | |
1075 | } | |
a8b3b0b6 CR |
1076 | else |
1077 | basetype = gfc_get_int_type (spec->kind); | |
6de9cd9a DN |
1078 | break; |
1079 | ||
1080 | case BT_REAL: | |
1081 | basetype = gfc_get_real_type (spec->kind); | |
1082 | break; | |
1083 | ||
1084 | case BT_COMPLEX: | |
1085 | basetype = gfc_get_complex_type (spec->kind); | |
1086 | break; | |
1087 | ||
1088 | case BT_LOGICAL: | |
1089 | basetype = gfc_get_logical_type (spec->kind); | |
1090 | break; | |
1091 | ||
1092 | case BT_CHARACTER: | |
9b548517 | 1093 | basetype = gfc_get_character_type (spec->kind, spec->u.cl); |
6de9cd9a DN |
1094 | break; |
1095 | ||
25812571 PT |
1096 | case BT_HOLLERITH: |
1097 | /* Since this cannot be used, return a length one character. */ | |
1098 | basetype = gfc_get_character_type_len (gfc_default_character_kind, | |
1099 | gfc_index_one_node); | |
1100 | break; | |
1101 | ||
6de9cd9a | 1102 | case BT_DERIVED: |
cf2b3c22 | 1103 | case BT_CLASS: |
bc21d315 | 1104 | basetype = gfc_get_derived_type (spec->u.derived); |
6de9cd9a | 1105 | |
8f75db9f PT |
1106 | if (spec->type == BT_CLASS) |
1107 | GFC_CLASS_TYPE_P (basetype) = 1; | |
1108 | ||
a8b3b0b6 CR |
1109 | /* If we're dealing with either C_PTR or C_FUNPTR, we modified the |
1110 | type and kind to fit a (void *) and the basetype returned was a | |
1111 | ptr_type_node. We need to pass up this new information to the | |
1112 | symbol that was declared of type C_PTR or C_FUNPTR. */ | |
cadddfdd | 1113 | if (spec->u.derived->ts.f90_type == BT_VOID) |
a8b3b0b6 | 1114 | { |
cadddfdd TB |
1115 | spec->type = BT_INTEGER; |
1116 | spec->kind = gfc_index_integer_kind; | |
1117 | spec->f90_type = BT_VOID; | |
a8b3b0b6 CR |
1118 | } |
1119 | break; | |
1120 | case BT_VOID: | |
45a69325 | 1121 | case BT_ASSUMED: |
089db47d CR |
1122 | /* This is for the second arg to c_f_pointer and c_f_procpointer |
1123 | of the iso_c_binding module, to accept any ptr type. */ | |
1124 | basetype = ptr_type_node; | |
1125 | if (spec->f90_type == BT_VOID) | |
1126 | { | |
bc21d315 JW |
1127 | if (spec->u.derived |
1128 | && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR) | |
089db47d CR |
1129 | basetype = ptr_type_node; |
1130 | else | |
1131 | basetype = pfunc_type_node; | |
1132 | } | |
a8b3b0b6 | 1133 | break; |
6de9cd9a | 1134 | default: |
6e45f57b | 1135 | gcc_unreachable (); |
6de9cd9a DN |
1136 | } |
1137 | return basetype; | |
1138 | } | |
1139 | \f | |
1140 | /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */ | |
c3e8c6b8 | 1141 | |
6de9cd9a DN |
1142 | static tree |
1143 | gfc_conv_array_bound (gfc_expr * expr) | |
1144 | { | |
1145 | /* If expr is an integer constant, return that. */ | |
1146 | if (expr != NULL && expr->expr_type == EXPR_CONSTANT) | |
1147 | return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind); | |
1148 | ||
1149 | /* Otherwise return NULL. */ | |
1150 | return NULL_TREE; | |
1151 | } | |
1152 | \f | |
710700ab TB |
1153 | /* Return the type of an element of the array. Note that scalar coarrays |
1154 | are special. In particular, for GFC_ARRAY_TYPE_P, the original argument | |
1155 | (with POINTER_TYPE stripped) is returned. */ | |
1156 | ||
6de9cd9a DN |
1157 | tree |
1158 | gfc_get_element_type (tree type) | |
1159 | { | |
1160 | tree element; | |
1161 | ||
1162 | if (GFC_ARRAY_TYPE_P (type)) | |
1163 | { | |
1164 | if (TREE_CODE (type) == POINTER_TYPE) | |
1165 | type = TREE_TYPE (type); | |
4409de24 TB |
1166 | if (GFC_TYPE_ARRAY_RANK (type) == 0) |
1167 | { | |
1168 | gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0); | |
1169 | element = type; | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
1174 | element = TREE_TYPE (type); | |
1175 | } | |
6de9cd9a DN |
1176 | } |
1177 | else | |
1178 | { | |
6e45f57b | 1179 | gcc_assert (GFC_DESCRIPTOR_TYPE_P (type)); |
4c73896d | 1180 | element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type); |
6de9cd9a | 1181 | |
6e45f57b | 1182 | gcc_assert (TREE_CODE (element) == POINTER_TYPE); |
6de9cd9a DN |
1183 | element = TREE_TYPE (element); |
1184 | ||
badd9e69 | 1185 | /* For arrays, which are not scalar coarrays. */ |
5e67a369 | 1186 | if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element)) |
badd9e69 | 1187 | element = TREE_TYPE (element); |
6de9cd9a DN |
1188 | } |
1189 | ||
1190 | return element; | |
1191 | } | |
1192 | \f | |
eb28fb7d | 1193 | /* Build an array. This function is called from gfc_sym_type(). |
c3e8c6b8 | 1194 | Actually returns array descriptor type. |
6de9cd9a DN |
1195 | |
1196 | Format of array descriptors is as follows: | |
1197 | ||
1198 | struct gfc_array_descriptor | |
1199 | { | |
1200 | array *data | |
1201 | index offset; | |
1202 | index dtype; | |
1203 | struct descriptor_dimension dimension[N_DIM]; | |
1204 | } | |
1205 | ||
1206 | struct descriptor_dimension | |
1207 | { | |
1208 | index stride; | |
1209 | index lbound; | |
1210 | index ubound; | |
1211 | } | |
1212 | ||
eb28fb7d TS |
1213 | Translation code should use gfc_conv_descriptor_* rather than |
1214 | accessing the descriptor directly. Any changes to the array | |
1215 | descriptor type will require changes in gfc_conv_descriptor_* and | |
1216 | gfc_build_array_initializer. | |
6de9cd9a | 1217 | |
eb28fb7d TS |
1218 | This is represented internally as a RECORD_TYPE. The index nodes |
1219 | are gfc_array_index_type and the data node is a pointer to the | |
1220 | data. See below for the handling of character types. | |
6de9cd9a DN |
1221 | |
1222 | The dtype member is formatted as follows: | |
1223 | rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits | |
1224 | type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits | |
1225 | size = dtype >> GFC_DTYPE_SIZE_SHIFT | |
1226 | ||
eb28fb7d TS |
1227 | I originally used nested ARRAY_TYPE nodes to represent arrays, but |
1228 | this generated poor code for assumed/deferred size arrays. These | |
1229 | require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part | |
1230 | of the GENERIC grammar. Also, there is no way to explicitly set | |
1231 | the array stride, so all data must be packed(1). I've tried to | |
1232 | mark all the functions which would require modification with a GCC | |
1233 | ARRAYS comment. | |
6de9cd9a | 1234 | |
eb28fb7d | 1235 | The data component points to the first element in the array. The |
df2fba9e RW |
1236 | offset field is the position of the origin of the array (i.e. element |
1237 | (0, 0 ...)). This may be outside the bounds of the array. | |
6de9cd9a DN |
1238 | |
1239 | An element is accessed by | |
eb28fb7d | 1240 | data[offset + index0*stride0 + index1*stride1 + index2*stride2] |
c3e8c6b8 | 1241 | This gives good performance as the computation does not involve the |
eb28fb7d TS |
1242 | bounds of the array. For packed arrays, this is optimized further |
1243 | by substituting the known strides. | |
6de9cd9a | 1244 | |
eb28fb7d TS |
1245 | This system has one problem: all array bounds must be within 2^31 |
1246 | elements of the origin (2^63 on 64-bit machines). For example | |
1247 | integer, dimension (80000:90000, 80000:90000, 2) :: array | |
1248 | may not work properly on 32-bit machines because 80000*80000 > | |
df2fba9e | 1249 | 2^31, so the calculation for stride2 would overflow. This may |
eb28fb7d TS |
1250 | still work, but I haven't checked, and it relies on the overflow |
1251 | doing the right thing. | |
6de9cd9a | 1252 | |
1f2959f0 | 1253 | The way to fix this problem is to access elements as follows: |
eb28fb7d TS |
1254 | data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1] |
1255 | Obviously this is much slower. I will make this a compile time | |
1256 | option, something like -fsmall-array-offsets. Mixing code compiled | |
1257 | with and without this switch will work. | |
1258 | ||
1259 | (1) This can be worked around by modifying the upper bound of the | |
1260 | previous dimension. This requires extra fields in the descriptor | |
1261 | (both real_ubound and fake_ubound). */ | |
6de9cd9a DN |
1262 | |
1263 | ||
1264 | /* Returns true if the array sym does not require a descriptor. */ | |
1265 | ||
1266 | int | |
1267 | gfc_is_nodesc_array (gfc_symbol * sym) | |
1268 | { | |
f3b0bb7a AV |
1269 | symbol_attribute *array_attr; |
1270 | gfc_array_spec *as; | |
1271 | bool is_classarray = IS_CLASS_ARRAY (sym); | |
1272 | ||
1273 | array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr; | |
1274 | as = is_classarray ? CLASS_DATA (sym)->as : sym->as; | |
1275 | ||
1276 | gcc_assert (array_attr->dimension || array_attr->codimension); | |
6de9cd9a DN |
1277 | |
1278 | /* We only want local arrays. */ | |
f3b0bb7a AV |
1279 | if ((sym->ts.type != BT_CLASS && sym->attr.pointer) |
1280 | || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer) | |
1281 | || array_attr->allocatable) | |
6de9cd9a DN |
1282 | return 0; |
1283 | ||
571d54de | 1284 | /* We want a descriptor for associate-name arrays that do not have an |
f3b0bb7a AV |
1285 | explicitly known shape already. */ |
1286 | if (sym->assoc && as->type != AS_EXPLICIT) | |
571d54de DK |
1287 | return 0; |
1288 | ||
f3b0bb7a | 1289 | /* The dummy is stored in sym and not in the component. */ |
6de9cd9a | 1290 | if (sym->attr.dummy) |
f3b0bb7a AV |
1291 | return as->type != AS_ASSUMED_SHAPE |
1292 | && as->type != AS_ASSUMED_RANK; | |
6de9cd9a DN |
1293 | |
1294 | if (sym->attr.result || sym->attr.function) | |
1295 | return 0; | |
1296 | ||
f3b0bb7a | 1297 | gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed); |
6de9cd9a DN |
1298 | |
1299 | return 1; | |
1300 | } | |
1301 | ||
40f20186 PB |
1302 | |
1303 | /* Create an array descriptor type. */ | |
1304 | ||
6de9cd9a | 1305 | static tree |
fad0afd7 | 1306 | gfc_build_array_type (tree type, gfc_array_spec * as, |
fe4e525c TB |
1307 | enum gfc_array_kind akind, bool restricted, |
1308 | bool contiguous) | |
6de9cd9a DN |
1309 | { |
1310 | tree lbound[GFC_MAX_DIMENSIONS]; | |
1311 | tree ubound[GFC_MAX_DIMENSIONS]; | |
a8a5f4a9 TB |
1312 | int n, corank; |
1313 | ||
1314 | /* Assumed-shape arrays do not have codimension information stored in the | |
1315 | descriptor. */ | |
1316 | corank = as->corank; | |
1317 | if (as->type == AS_ASSUMED_SHAPE || | |
1318 | (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE)) | |
1319 | corank = 0; | |
6de9cd9a | 1320 | |
c62c6622 TB |
1321 | if (as->type == AS_ASSUMED_RANK) |
1322 | for (n = 0; n < GFC_MAX_DIMENSIONS; n++) | |
1323 | { | |
1324 | lbound[n] = NULL_TREE; | |
1325 | ubound[n] = NULL_TREE; | |
1326 | } | |
1327 | ||
6de9cd9a DN |
1328 | for (n = 0; n < as->rank; n++) |
1329 | { | |
1330 | /* Create expressions for the known bounds of the array. */ | |
1331 | if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL) | |
7ab92584 | 1332 | lbound[n] = gfc_index_one_node; |
6de9cd9a DN |
1333 | else |
1334 | lbound[n] = gfc_conv_array_bound (as->lower[n]); | |
1335 | ubound[n] = gfc_conv_array_bound (as->upper[n]); | |
1336 | } | |
1337 | ||
a8a5f4a9 | 1338 | for (n = as->rank; n < as->rank + corank; n++) |
a3935ffc | 1339 | { |
49b804d0 | 1340 | if (as->type != AS_DEFERRED && as->lower[n] == NULL) |
a3935ffc TB |
1341 | lbound[n] = gfc_index_one_node; |
1342 | else | |
1343 | lbound[n] = gfc_conv_array_bound (as->lower[n]); | |
1344 | ||
a8a5f4a9 | 1345 | if (n < as->rank + corank - 1) |
a3935ffc TB |
1346 | ubound[n] = gfc_conv_array_bound (as->upper[n]); |
1347 | } | |
1348 | ||
fad0afd7 | 1349 | if (as->type == AS_ASSUMED_SHAPE) |
fe4e525c TB |
1350 | akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT |
1351 | : GFC_ARRAY_ASSUMED_SHAPE; | |
c62c6622 TB |
1352 | else if (as->type == AS_ASSUMED_RANK) |
1353 | akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT | |
1354 | : GFC_ARRAY_ASSUMED_RANK; | |
1355 | return gfc_get_array_type_bounds (type, as->rank == -1 | |
1356 | ? GFC_MAX_DIMENSIONS : as->rank, | |
a8a5f4a9 | 1357 | corank, lbound, |
f33beee9 | 1358 | ubound, 0, akind, restricted); |
6de9cd9a DN |
1359 | } |
1360 | \f | |
1361 | /* Returns the struct descriptor_dimension type. */ | |
c3e8c6b8 | 1362 | |
6de9cd9a DN |
1363 | static tree |
1364 | gfc_get_desc_dim_type (void) | |
1365 | { | |
1366 | tree type; | |
35151cd5 | 1367 | tree decl, *chain = NULL; |
6de9cd9a DN |
1368 | |
1369 | if (gfc_desc_dim_type) | |
1370 | return gfc_desc_dim_type; | |
1371 | ||
1372 | /* Build the type node. */ | |
1373 | type = make_node (RECORD_TYPE); | |
1374 | ||
1375 | TYPE_NAME (type) = get_identifier ("descriptor_dimension"); | |
1376 | TYPE_PACKED (type) = 1; | |
1377 | ||
1378 | /* Consists of the stride, lbound and ubound members. */ | |
35151cd5 | 1379 | decl = gfc_add_field_to_struct_1 (type, |
dfd6ece2 NF |
1380 | get_identifier ("stride"), |
1381 | gfc_array_index_type, &chain); | |
d8eff1b8 | 1382 | TREE_NO_WARNING (decl) = 1; |
6de9cd9a | 1383 | |
35151cd5 | 1384 | decl = gfc_add_field_to_struct_1 (type, |
dfd6ece2 NF |
1385 | get_identifier ("lbound"), |
1386 | gfc_array_index_type, &chain); | |
d8eff1b8 | 1387 | TREE_NO_WARNING (decl) = 1; |
6de9cd9a | 1388 | |
35151cd5 | 1389 | decl = gfc_add_field_to_struct_1 (type, |
dfd6ece2 NF |
1390 | get_identifier ("ubound"), |
1391 | gfc_array_index_type, &chain); | |
d8eff1b8 | 1392 | TREE_NO_WARNING (decl) = 1; |
6de9cd9a DN |
1393 | |
1394 | /* Finish off the type. */ | |
6de9cd9a | 1395 | gfc_finish_type (type); |
dfcf0b12 | 1396 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1; |
6de9cd9a DN |
1397 | |
1398 | gfc_desc_dim_type = type; | |
1399 | return type; | |
1400 | } | |
1401 | ||
40b026d8 | 1402 | |
43a5ef69 | 1403 | /* Return the DTYPE for an array. This describes the type and type parameters |
40b026d8 PB |
1404 | of the array. */ |
1405 | /* TODO: Only call this when the value is actually used, and make all the | |
1406 | unknown cases abort. */ | |
1407 | ||
1408 | tree | |
d7463e5b | 1409 | gfc_get_dtype_rank_type (int rank, tree etype) |
6de9cd9a DN |
1410 | { |
1411 | tree size; | |
1412 | int n; | |
1413 | HOST_WIDE_INT i; | |
1414 | tree tmp; | |
1415 | tree dtype; | |
6de9cd9a | 1416 | |
40b026d8 | 1417 | switch (TREE_CODE (etype)) |
6de9cd9a DN |
1418 | { |
1419 | case INTEGER_TYPE: | |
a11930ba | 1420 | n = BT_INTEGER; |
6de9cd9a DN |
1421 | break; |
1422 | ||
1423 | case BOOLEAN_TYPE: | |
a11930ba | 1424 | n = BT_LOGICAL; |
6de9cd9a DN |
1425 | break; |
1426 | ||
1427 | case REAL_TYPE: | |
a11930ba | 1428 | n = BT_REAL; |
6de9cd9a DN |
1429 | break; |
1430 | ||
1431 | case COMPLEX_TYPE: | |
a11930ba | 1432 | n = BT_COMPLEX; |
6de9cd9a DN |
1433 | break; |
1434 | ||
40b026d8 | 1435 | /* We will never have arrays of arrays. */ |
6de9cd9a | 1436 | case RECORD_TYPE: |
a11930ba | 1437 | n = BT_DERIVED; |
6de9cd9a DN |
1438 | break; |
1439 | ||
1440 | case ARRAY_TYPE: | |
a11930ba | 1441 | n = BT_CHARACTER; |
6de9cd9a DN |
1442 | break; |
1443 | ||
45a69325 TB |
1444 | case POINTER_TYPE: |
1445 | n = BT_ASSUMED; | |
1446 | break; | |
1447 | ||
6de9cd9a | 1448 | default: |
40f20186 PB |
1449 | /* TODO: Don't do dtype for temporary descriptorless arrays. */ |
1450 | /* We can strange array types for temporary arrays. */ | |
1451 | return gfc_index_zero_node; | |
6de9cd9a DN |
1452 | } |
1453 | ||
6e45f57b | 1454 | gcc_assert (rank <= GFC_DTYPE_RANK_MASK); |
40b026d8 | 1455 | size = TYPE_SIZE_UNIT (etype); |
f676971a | 1456 | |
6de9cd9a DN |
1457 | i = rank | (n << GFC_DTYPE_TYPE_SHIFT); |
1458 | if (size && INTEGER_CST_P (size)) | |
1459 | { | |
1460 | if (tree_int_cst_lt (gfc_max_array_element_size, size)) | |
f354bf1d | 1461 | gfc_fatal_error ("Array element size too big at %C"); |
6de9cd9a DN |
1462 | |
1463 | i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT; | |
1464 | } | |
7d60be94 | 1465 | dtype = build_int_cst (gfc_array_index_type, i); |
6de9cd9a DN |
1466 | |
1467 | if (size && !INTEGER_CST_P (size)) | |
1468 | { | |
7d60be94 | 1469 | tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT); |
bc98ed60 TB |
1470 | tmp = fold_build2_loc (input_location, LSHIFT_EXPR, |
1471 | gfc_array_index_type, | |
1472 | fold_convert (gfc_array_index_type, size), tmp); | |
1473 | dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type, | |
1474 | tmp, dtype); | |
6de9cd9a DN |
1475 | } |
1476 | /* If we don't know the size we leave it as zero. This should never happen | |
1477 | for anything that is actually used. */ | |
1478 | /* TODO: Check this is actually true, particularly when repacking | |
1479 | assumed size parameters. */ | |
1480 | ||
d7463e5b TB |
1481 | return dtype; |
1482 | } | |
1483 | ||
1484 | ||
1485 | tree | |
1486 | gfc_get_dtype (tree type) | |
1487 | { | |
1488 | tree dtype; | |
1489 | tree etype; | |
1490 | int rank; | |
1491 | ||
1492 | gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type)); | |
1493 | ||
1494 | if (GFC_TYPE_ARRAY_DTYPE (type)) | |
1495 | return GFC_TYPE_ARRAY_DTYPE (type); | |
1496 | ||
1497 | rank = GFC_TYPE_ARRAY_RANK (type); | |
1498 | etype = gfc_get_element_type (type); | |
1499 | dtype = gfc_get_dtype_rank_type (rank, etype); | |
1500 | ||
40b026d8 | 1501 | GFC_TYPE_ARRAY_DTYPE (type) = dtype; |
6de9cd9a DN |
1502 | return dtype; |
1503 | } | |
1504 | ||
1505 | ||
dcfef7d4 TS |
1506 | /* Build an array type for use without a descriptor, packed according |
1507 | to the value of PACKED. */ | |
6de9cd9a DN |
1508 | |
1509 | tree | |
10174ddf MM |
1510 | gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed, |
1511 | bool restricted) | |
6de9cd9a DN |
1512 | { |
1513 | tree range; | |
1514 | tree type; | |
1515 | tree tmp; | |
1516 | int n; | |
1517 | int known_stride; | |
1518 | int known_offset; | |
1519 | mpz_t offset; | |
1520 | mpz_t stride; | |
1521 | mpz_t delta; | |
1522 | gfc_expr *expr; | |
1523 | ||
1524 | mpz_init_set_ui (offset, 0); | |
1525 | mpz_init_set_ui (stride, 1); | |
1526 | mpz_init (delta); | |
1527 | ||
1528 | /* We don't use build_array_type because this does not include include | |
13795658 | 1529 | lang-specific information (i.e. the bounds of the array) when checking |
6de9cd9a | 1530 | for duplicates. */ |
4409de24 TB |
1531 | if (as->rank) |
1532 | type = make_node (ARRAY_TYPE); | |
1533 | else | |
8a5c4899 | 1534 | type = build_variant_type_copy (etype); |
6de9cd9a DN |
1535 | |
1536 | GFC_ARRAY_TYPE_P (type) = 1; | |
766090c2 | 1537 | TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> (); |
6de9cd9a | 1538 | |
dcfef7d4 | 1539 | known_stride = (packed != PACKED_NO); |
6de9cd9a DN |
1540 | known_offset = 1; |
1541 | for (n = 0; n < as->rank; n++) | |
1542 | { | |
1543 | /* Fill in the stride and bound components of the type. */ | |
1544 | if (known_stride) | |
08789087 | 1545 | tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind); |
6de9cd9a DN |
1546 | else |
1547 | tmp = NULL_TREE; | |
1548 | GFC_TYPE_ARRAY_STRIDE (type, n) = tmp; | |
1549 | ||
1550 | expr = as->lower[n]; | |
1551 | if (expr->expr_type == EXPR_CONSTANT) | |
1552 | { | |
1553 | tmp = gfc_conv_mpz_to_tree (expr->value.integer, | |
fad0afd7 | 1554 | gfc_index_integer_kind); |
6de9cd9a DN |
1555 | } |
1556 | else | |
1557 | { | |
1558 | known_stride = 0; | |
1559 | tmp = NULL_TREE; | |
1560 | } | |
1561 | GFC_TYPE_ARRAY_LBOUND (type, n) = tmp; | |
1562 | ||
1563 | if (known_stride) | |
1564 | { | |
1565 | /* Calculate the offset. */ | |
1566 | mpz_mul (delta, stride, as->lower[n]->value.integer); | |
1567 | mpz_sub (offset, offset, delta); | |
1568 | } | |
1569 | else | |
1570 | known_offset = 0; | |
1571 | ||
1572 | expr = as->upper[n]; | |
1573 | if (expr && expr->expr_type == EXPR_CONSTANT) | |
1574 | { | |
1575 | tmp = gfc_conv_mpz_to_tree (expr->value.integer, | |
1576 | gfc_index_integer_kind); | |
1577 | } | |
1578 | else | |
1579 | { | |
1580 | tmp = NULL_TREE; | |
1581 | known_stride = 0; | |
1582 | } | |
1583 | GFC_TYPE_ARRAY_UBOUND (type, n) = tmp; | |
1584 | ||
1585 | if (known_stride) | |
1586 | { | |
1587 | /* Calculate the stride. */ | |
1588 | mpz_sub (delta, as->upper[n]->value.integer, | |
1589 | as->lower[n]->value.integer); | |
1590 | mpz_add_ui (delta, delta, 1); | |
1591 | mpz_mul (stride, stride, delta); | |
1592 | } | |
1593 | ||
1594 | /* Only the first stride is known for partial packed arrays. */ | |
dcfef7d4 | 1595 | if (packed == PACKED_NO || packed == PACKED_PARTIAL) |
6de9cd9a DN |
1596 | known_stride = 0; |
1597 | } | |
a3935ffc TB |
1598 | for (n = as->rank; n < as->rank + as->corank; n++) |
1599 | { | |
1600 | expr = as->lower[n]; | |
1601 | if (expr->expr_type == EXPR_CONSTANT) | |
1602 | tmp = gfc_conv_mpz_to_tree (expr->value.integer, | |
1603 | gfc_index_integer_kind); | |
1604 | else | |
1605 | tmp = NULL_TREE; | |
1606 | GFC_TYPE_ARRAY_LBOUND (type, n) = tmp; | |
1607 | ||
1608 | expr = as->upper[n]; | |
1609 | if (expr && expr->expr_type == EXPR_CONSTANT) | |
1610 | tmp = gfc_conv_mpz_to_tree (expr->value.integer, | |
1611 | gfc_index_integer_kind); | |
1612 | else | |
1613 | tmp = NULL_TREE; | |
1614 | if (n < as->rank + as->corank - 1) | |
1615 | GFC_TYPE_ARRAY_UBOUND (type, n) = tmp; | |
1616 | } | |
6de9cd9a DN |
1617 | |
1618 | if (known_offset) | |
1619 | { | |
1620 | GFC_TYPE_ARRAY_OFFSET (type) = | |
1621 | gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind); | |
1622 | } | |
1623 | else | |
1624 | GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE; | |
1625 | ||
1626 | if (known_stride) | |
1627 | { | |
1628 | GFC_TYPE_ARRAY_SIZE (type) = | |
1629 | gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind); | |
1630 | } | |
1631 | else | |
1632 | GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE; | |
1633 | ||
6de9cd9a | 1634 | GFC_TYPE_ARRAY_RANK (type) = as->rank; |
a3935ffc | 1635 | GFC_TYPE_ARRAY_CORANK (type) = as->corank; |
40b026d8 | 1636 | GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE; |
7ab92584 | 1637 | range = build_range_type (gfc_array_index_type, gfc_index_zero_node, |
6de9cd9a DN |
1638 | NULL_TREE); |
1639 | /* TODO: use main type if it is unbounded. */ | |
1640 | GFC_TYPE_ARRAY_DATAPTR_TYPE (type) = | |
1641 | build_pointer_type (build_array_type (etype, range)); | |
10174ddf MM |
1642 | if (restricted) |
1643 | GFC_TYPE_ARRAY_DATAPTR_TYPE (type) = | |
1644 | build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type), | |
1645 | TYPE_QUAL_RESTRICT); | |
6de9cd9a | 1646 | |
4409de24 TB |
1647 | if (as->rank == 0) |
1648 | { | |
f19626cf | 1649 | if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB) |
5aacb11e TB |
1650 | { |
1651 | type = build_pointer_type (type); | |
4409de24 | 1652 | |
5aacb11e | 1653 | if (restricted) |
f04986a9 | 1654 | type = build_qualified_type (type, TYPE_QUAL_RESTRICT); |
4409de24 | 1655 | |
4409de24 | 1656 | GFC_ARRAY_TYPE_P (type) = 1; |
f04986a9 | 1657 | TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type)); |
4409de24 TB |
1658 | } |
1659 | ||
1660 | return type; | |
1661 | } | |
1662 | ||
6de9cd9a DN |
1663 | if (known_stride) |
1664 | { | |
1665 | mpz_sub_ui (stride, stride, 1); | |
1666 | range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind); | |
1667 | } | |
1668 | else | |
1669 | range = NULL_TREE; | |
1670 | ||
7ab92584 | 1671 | range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range); |
6de9cd9a DN |
1672 | TYPE_DOMAIN (type) = range; |
1673 | ||
1674 | build_pointer_type (etype); | |
1675 | TREE_TYPE (type) = etype; | |
1676 | ||
1677 | layout_type (type); | |
1678 | ||
1679 | mpz_clear (offset); | |
1680 | mpz_clear (stride); | |
1681 | mpz_clear (delta); | |
1682 | ||
09775c40 AO |
1683 | /* Represent packed arrays as multi-dimensional if they have rank > |
1684 | 1 and with proper bounds, instead of flat arrays. This makes for | |
1685 | better debug info. */ | |
1686 | if (known_offset) | |
08789087 JJ |
1687 | { |
1688 | tree gtype = etype, rtype, type_decl; | |
1689 | ||
1690 | for (n = as->rank - 1; n >= 0; n--) | |
1691 | { | |
1692 | rtype = build_range_type (gfc_array_index_type, | |
1693 | GFC_TYPE_ARRAY_LBOUND (type, n), | |
1694 | GFC_TYPE_ARRAY_UBOUND (type, n)); | |
1695 | gtype = build_array_type (gtype, rtype); | |
1696 | } | |
c2255bc4 AH |
1697 | TYPE_NAME (type) = type_decl = build_decl (input_location, |
1698 | TYPE_DECL, NULL, gtype); | |
08789087 JJ |
1699 | DECL_ORIGINAL_TYPE (type_decl) = gtype; |
1700 | } | |
1701 | ||
b8ff4e88 | 1702 | if (packed != PACKED_STATIC || !known_stride |
f19626cf | 1703 | || (as->corank && flag_coarray == GFC_FCOARRAY_LIB)) |
6de9cd9a | 1704 | { |
841b0c1f PB |
1705 | /* For dummy arrays and automatic (heap allocated) arrays we |
1706 | want a pointer to the array. */ | |
6de9cd9a | 1707 | type = build_pointer_type (type); |
10174ddf MM |
1708 | if (restricted) |
1709 | type = build_qualified_type (type, TYPE_QUAL_RESTRICT); | |
6de9cd9a DN |
1710 | GFC_ARRAY_TYPE_P (type) = 1; |
1711 | TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type)); | |
1712 | } | |
1713 | return type; | |
1714 | } | |
1715 | ||
aa13dc3c | 1716 | |
4c73896d RH |
1717 | /* Return or create the base type for an array descriptor. */ |
1718 | ||
1719 | static tree | |
aa13dc3c TB |
1720 | gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted, |
1721 | enum gfc_array_kind akind) | |
4c73896d | 1722 | { |
35151cd5 | 1723 | tree fat_type, decl, arraytype, *chain = NULL; |
f33beee9 | 1724 | char name[16 + 2*GFC_RANK_DIGITS + 1 + 1]; |
c62c6622 TB |
1725 | int idx; |
1726 | ||
1727 | /* Assumed-rank array. */ | |
1728 | if (dimen == -1) | |
1729 | dimen = GFC_MAX_DIMENSIONS; | |
1730 | ||
1731 | idx = 2 * (codimen + dimen) + restricted; | |
4c73896d | 1732 | |
c62c6622 | 1733 | gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS); |
af232d48 | 1734 | |
f19626cf | 1735 | if (flag_coarray == GFC_FCOARRAY_LIB && codimen) |
af232d48 TB |
1736 | { |
1737 | if (gfc_array_descriptor_base_caf[idx]) | |
1738 | return gfc_array_descriptor_base_caf[idx]; | |
1739 | } | |
1740 | else if (gfc_array_descriptor_base[idx]) | |
10174ddf | 1741 | return gfc_array_descriptor_base[idx]; |
4c73896d RH |
1742 | |
1743 | /* Build the type node. */ | |
1744 | fat_type = make_node (RECORD_TYPE); | |
1745 | ||
bf65e4b1 | 1746 | sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen); |
4c73896d | 1747 | TYPE_NAME (fat_type) = get_identifier (name); |
cd3f04c8 | 1748 | TYPE_NAMELESS (fat_type) = 1; |
4c73896d RH |
1749 | |
1750 | /* Add the data member as the first element of the descriptor. */ | |
35151cd5 | 1751 | decl = gfc_add_field_to_struct_1 (fat_type, |
dfd6ece2 NF |
1752 | get_identifier ("data"), |
1753 | (restricted | |
1754 | ? prvoid_type_node | |
1755 | : ptr_type_node), &chain); | |
4c73896d RH |
1756 | |
1757 | /* Add the base component. */ | |
35151cd5 | 1758 | decl = gfc_add_field_to_struct_1 (fat_type, |
dfd6ece2 NF |
1759 | get_identifier ("offset"), |
1760 | gfc_array_index_type, &chain); | |
d8eff1b8 | 1761 | TREE_NO_WARNING (decl) = 1; |
4c73896d RH |
1762 | |
1763 | /* Add the dtype component. */ | |
35151cd5 | 1764 | decl = gfc_add_field_to_struct_1 (fat_type, |
dfd6ece2 NF |
1765 | get_identifier ("dtype"), |
1766 | gfc_array_index_type, &chain); | |
d8eff1b8 | 1767 | TREE_NO_WARNING (decl) = 1; |
4c73896d RH |
1768 | |
1769 | /* Build the array type for the stride and bound components. */ | |
c62c6622 TB |
1770 | if (dimen + codimen > 0) |
1771 | { | |
1772 | arraytype = | |
1773 | build_array_type (gfc_get_desc_dim_type (), | |
1774 | build_range_type (gfc_array_index_type, | |
1775 | gfc_index_zero_node, | |
1776 | gfc_rank_cst[codimen + dimen - 1])); | |
1777 | ||
1778 | decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"), | |
1779 | arraytype, &chain); | |
1780 | TREE_NO_WARNING (decl) = 1; | |
1781 | } | |
4c73896d | 1782 | |
f19626cf | 1783 | if (flag_coarray == GFC_FCOARRAY_LIB && codimen |
aa13dc3c | 1784 | && akind == GFC_ARRAY_ALLOCATABLE) |
af232d48 TB |
1785 | { |
1786 | decl = gfc_add_field_to_struct_1 (fat_type, | |
1787 | get_identifier ("token"), | |
1788 | prvoid_type_node, &chain); | |
1789 | TREE_NO_WARNING (decl) = 1; | |
1790 | } | |
1791 | ||
4c73896d | 1792 | /* Finish off the type. */ |
4c73896d | 1793 | gfc_finish_type (fat_type); |
dfcf0b12 | 1794 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1; |
4c73896d | 1795 | |
f19626cf | 1796 | if (flag_coarray == GFC_FCOARRAY_LIB && codimen |
aa13dc3c | 1797 | && akind == GFC_ARRAY_ALLOCATABLE) |
af232d48 TB |
1798 | gfc_array_descriptor_base_caf[idx] = fat_type; |
1799 | else | |
1800 | gfc_array_descriptor_base[idx] = fat_type; | |
1801 | ||
4c73896d RH |
1802 | return fat_type; |
1803 | } | |
6de9cd9a | 1804 | |
aa13dc3c | 1805 | |
6de9cd9a DN |
1806 | /* Build an array (descriptor) type with given bounds. */ |
1807 | ||
1808 | tree | |
f33beee9 | 1809 | gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound, |
fad0afd7 | 1810 | tree * ubound, int packed, |
10174ddf | 1811 | enum gfc_array_kind akind, bool restricted) |
6de9cd9a | 1812 | { |
f33beee9 | 1813 | char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN]; |
416a8af4 | 1814 | tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype; |
9aa433c2 | 1815 | const char *type_name; |
4c73896d | 1816 | int n; |
6de9cd9a | 1817 | |
aa13dc3c | 1818 | base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted, akind); |
9618fb3c | 1819 | fat_type = build_distinct_type_copy (base_type); |
10174ddf MM |
1820 | /* Make sure that nontarget and target array type have the same canonical |
1821 | type (and same stub decl for debug info). */ | |
aa13dc3c | 1822 | base_type = gfc_get_array_descriptor_base (dimen, codimen, false, akind); |
9618fb3c RG |
1823 | TYPE_CANONICAL (fat_type) = base_type; |
1824 | TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type); | |
6de9cd9a DN |
1825 | |
1826 | tmp = TYPE_NAME (etype); | |
1827 | if (tmp && TREE_CODE (tmp) == TYPE_DECL) | |
1828 | tmp = DECL_NAME (tmp); | |
1829 | if (tmp) | |
9aa433c2 | 1830 | type_name = IDENTIFIER_POINTER (tmp); |
6de9cd9a | 1831 | else |
9aa433c2 | 1832 | type_name = "unknown"; |
bf65e4b1 | 1833 | sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen, |
9aa433c2 | 1834 | GFC_MAX_SYMBOL_LEN, type_name); |
6de9cd9a | 1835 | TYPE_NAME (fat_type) = get_identifier (name); |
cd3f04c8 | 1836 | TYPE_NAMELESS (fat_type) = 1; |
6de9cd9a | 1837 | |
4c73896d | 1838 | GFC_DESCRIPTOR_TYPE_P (fat_type) = 1; |
766090c2 | 1839 | TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> (); |
4c73896d RH |
1840 | |
1841 | GFC_TYPE_ARRAY_RANK (fat_type) = dimen; | |
a3935ffc | 1842 | GFC_TYPE_ARRAY_CORANK (fat_type) = codimen; |
4c73896d | 1843 | GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE; |
fad0afd7 | 1844 | GFC_TYPE_ARRAY_AKIND (fat_type) = akind; |
6de9cd9a DN |
1845 | |
1846 | /* Build an array descriptor record type. */ | |
1847 | if (packed != 0) | |
7ab92584 | 1848 | stride = gfc_index_one_node; |
6de9cd9a DN |
1849 | else |
1850 | stride = NULL_TREE; | |
4ca9939b | 1851 | for (n = 0; n < dimen + codimen; n++) |
6de9cd9a | 1852 | { |
4ca9939b TB |
1853 | if (n < dimen) |
1854 | GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride; | |
6de9cd9a DN |
1855 | |
1856 | if (lbound) | |
1857 | lower = lbound[n]; | |
1858 | else | |
1859 | lower = NULL_TREE; | |
1860 | ||
1861 | if (lower != NULL_TREE) | |
1862 | { | |
1863 | if (INTEGER_CST_P (lower)) | |
1864 | GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower; | |
1865 | else | |
1866 | lower = NULL_TREE; | |
1867 | } | |
1868 | ||
4ca9939b TB |
1869 | if (codimen && n == dimen + codimen - 1) |
1870 | break; | |
1871 | ||
6de9cd9a DN |
1872 | upper = ubound[n]; |
1873 | if (upper != NULL_TREE) | |
1874 | { | |
1875 | if (INTEGER_CST_P (upper)) | |
1876 | GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper; | |
1877 | else | |
1878 | upper = NULL_TREE; | |
1879 | } | |
1880 | ||
4ca9939b TB |
1881 | if (n >= dimen) |
1882 | continue; | |
1883 | ||
6de9cd9a DN |
1884 | if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE) |
1885 | { | |
bc98ed60 TB |
1886 | tmp = fold_build2_loc (input_location, MINUS_EXPR, |
1887 | gfc_array_index_type, upper, lower); | |
1888 | tmp = fold_build2_loc (input_location, PLUS_EXPR, | |
1889 | gfc_array_index_type, tmp, | |
1890 | gfc_index_one_node); | |
1891 | stride = fold_build2_loc (input_location, MULT_EXPR, | |
1892 | gfc_array_index_type, tmp, stride); | |
6de9cd9a | 1893 | /* Check the folding worked. */ |
6e45f57b | 1894 | gcc_assert (INTEGER_CST_P (stride)); |
6de9cd9a DN |
1895 | } |
1896 | else | |
1897 | stride = NULL_TREE; | |
1898 | } | |
1899 | GFC_TYPE_ARRAY_SIZE (fat_type) = stride; | |
4c73896d | 1900 | |
6de9cd9a DN |
1901 | /* TODO: known offsets for descriptors. */ |
1902 | GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE; | |
1903 | ||
badd9e69 TB |
1904 | if (dimen == 0) |
1905 | { | |
1906 | arraytype = build_pointer_type (etype); | |
1907 | if (restricted) | |
1908 | arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT); | |
1909 | ||
1910 | GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype; | |
1911 | return fat_type; | |
1912 | } | |
1913 | ||
416a8af4 RG |
1914 | /* We define data as an array with the correct size if possible. |
1915 | Much better than doing pointer arithmetic. */ | |
1916 | if (stride) | |
1917 | rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node, | |
1918 | int_const_binop (MINUS_EXPR, stride, | |
807e902e | 1919 | build_int_cst (TREE_TYPE (stride), 1))); |
416a8af4 RG |
1920 | else |
1921 | rtype = gfc_array_range_type; | |
1922 | arraytype = build_array_type (etype, rtype); | |
6de9cd9a | 1923 | arraytype = build_pointer_type (arraytype); |
10174ddf MM |
1924 | if (restricted) |
1925 | arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT); | |
6de9cd9a DN |
1926 | GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype; |
1927 | ||
d560566a AO |
1928 | /* This will generate the base declarations we need to emit debug |
1929 | information for this type. FIXME: there must be a better way to | |
1930 | avoid divergence between compilations with and without debug | |
1931 | information. */ | |
1932 | { | |
1933 | struct array_descr_info info; | |
1934 | gfc_get_array_descr_info (fat_type, &info); | |
1935 | gfc_get_array_descr_info (build_pointer_type (fat_type), &info); | |
1936 | } | |
1937 | ||
6de9cd9a DN |
1938 | return fat_type; |
1939 | } | |
1940 | \f | |
1941 | /* Build a pointer type. This function is called from gfc_sym_type(). */ | |
c3e8c6b8 | 1942 | |
6de9cd9a DN |
1943 | static tree |
1944 | gfc_build_pointer_type (gfc_symbol * sym, tree type) | |
1945 | { | |
436529ea | 1946 | /* Array pointer types aren't actually pointers. */ |
6de9cd9a DN |
1947 | if (sym->attr.dimension) |
1948 | return type; | |
1949 | else | |
1950 | return build_pointer_type (type); | |
1951 | } | |
b3c1b8a1 MM |
1952 | |
1953 | static tree gfc_nonrestricted_type (tree t); | |
1954 | /* Given two record or union type nodes TO and FROM, ensure | |
1955 | that all fields in FROM have a corresponding field in TO, | |
1956 | their type being nonrestrict variants. This accepts a TO | |
1957 | node that already has a prefix of the fields in FROM. */ | |
1958 | static void | |
1959 | mirror_fields (tree to, tree from) | |
1960 | { | |
1961 | tree fto, ffrom; | |
1962 | tree *chain; | |
1963 | ||
1964 | /* Forward to the end of TOs fields. */ | |
1965 | fto = TYPE_FIELDS (to); | |
1966 | ffrom = TYPE_FIELDS (from); | |
1967 | chain = &TYPE_FIELDS (to); | |
1968 | while (fto) | |
1969 | { | |
1970 | gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom)); | |
1971 | chain = &DECL_CHAIN (fto); | |
1972 | fto = DECL_CHAIN (fto); | |
1973 | ffrom = DECL_CHAIN (ffrom); | |
1974 | } | |
1975 | ||
1976 | /* Now add all fields remaining in FROM (starting with ffrom). */ | |
1977 | for (; ffrom; ffrom = DECL_CHAIN (ffrom)) | |
1978 | { | |
1979 | tree newfield = copy_node (ffrom); | |
1980 | DECL_CONTEXT (newfield) = to; | |
1981 | /* The store to DECL_CHAIN might seem redundant with the | |
1982 | stores to *chain, but not clearing it here would mean | |
1983 | leaving a chain into the old fields. If ever | |
1984 | our called functions would look at them confusion | |
1985 | will arise. */ | |
1986 | DECL_CHAIN (newfield) = NULL_TREE; | |
1987 | *chain = newfield; | |
1988 | chain = &DECL_CHAIN (newfield); | |
1989 | ||
1990 | if (TREE_CODE (ffrom) == FIELD_DECL) | |
1991 | { | |
1992 | tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom)); | |
1993 | TREE_TYPE (newfield) = elemtype; | |
1994 | } | |
1995 | } | |
1996 | *chain = NULL_TREE; | |
1997 | } | |
1998 | ||
1999 | /* Given a type T, returns a different type of the same structure, | |
2000 | except that all types it refers to (recursively) are always | |
2001 | non-restrict qualified types. */ | |
2002 | static tree | |
2003 | gfc_nonrestricted_type (tree t) | |
2004 | { | |
2005 | tree ret = t; | |
2006 | ||
eea58adb | 2007 | /* If the type isn't laid out yet, don't copy it. If something |
b3c1b8a1 MM |
2008 | needs it for real it should wait until the type got finished. */ |
2009 | if (!TYPE_SIZE (t)) | |
2010 | return t; | |
2011 | ||
2012 | if (!TYPE_LANG_SPECIFIC (t)) | |
766090c2 | 2013 | TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> (); |
b3c1b8a1 MM |
2014 | /* If we're dealing with this very node already further up |
2015 | the call chain (recursion via pointers and struct members) | |
2016 | we haven't yet determined if we really need a new type node. | |
2017 | Assume we don't, return T itself. */ | |
2018 | if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node) | |
2019 | return t; | |
2020 | ||
2021 | /* If we have calculated this all already, just return it. */ | |
2022 | if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type) | |
2023 | return TYPE_LANG_SPECIFIC (t)->nonrestricted_type; | |
2024 | ||
2025 | /* Mark this type. */ | |
2026 | TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node; | |
2027 | ||
2028 | switch (TREE_CODE (t)) | |
2029 | { | |
2030 | default: | |
2031 | break; | |
2032 | ||
2033 | case POINTER_TYPE: | |
2034 | case REFERENCE_TYPE: | |
2035 | { | |
2036 | tree totype = gfc_nonrestricted_type (TREE_TYPE (t)); | |
2037 | if (totype == TREE_TYPE (t)) | |
2038 | ret = t; | |
2039 | else if (TREE_CODE (t) == POINTER_TYPE) | |
2040 | ret = build_pointer_type (totype); | |
2041 | else | |
2042 | ret = build_reference_type (totype); | |
2043 | ret = build_qualified_type (ret, | |
2044 | TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT); | |
2045 | } | |
2046 | break; | |
2047 | ||
2048 | case ARRAY_TYPE: | |
2049 | { | |
2050 | tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t)); | |
2051 | if (elemtype == TREE_TYPE (t)) | |
2052 | ret = t; | |
2053 | else | |
2054 | { | |
2055 | ret = build_variant_type_copy (t); | |
2056 | TREE_TYPE (ret) = elemtype; | |
2057 | if (TYPE_LANG_SPECIFIC (t) | |
2058 | && GFC_TYPE_ARRAY_DATAPTR_TYPE (t)) | |
2059 | { | |
2060 | tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t); | |
2061 | dataptr_type = gfc_nonrestricted_type (dataptr_type); | |
2062 | if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t)) | |
2063 | { | |
2064 | TYPE_LANG_SPECIFIC (ret) | |
766090c2 | 2065 | = ggc_cleared_alloc<struct lang_type> (); |
b3c1b8a1 MM |
2066 | *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t); |
2067 | GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type; | |
2068 | } | |
2069 | } | |
2070 | } | |
2071 | } | |
2072 | break; | |
2073 | ||
2074 | case RECORD_TYPE: | |
2075 | case UNION_TYPE: | |
2076 | case QUAL_UNION_TYPE: | |
2077 | { | |
2078 | tree field; | |
2079 | /* First determine if we need a new type at all. | |
2080 | Careful, the two calls to gfc_nonrestricted_type per field | |
2081 | might return different values. That happens exactly when | |
2082 | one of the fields reaches back to this very record type | |
2083 | (via pointers). The first calls will assume that we don't | |
2084 | need to copy T (see the error_mark_node marking). If there | |
2085 | are any reasons for copying T apart from having to copy T, | |
2086 | we'll indeed copy it, and the second calls to | |
2087 | gfc_nonrestricted_type will use that new node if they | |
2088 | reach back to T. */ | |
2089 | for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) | |
2090 | if (TREE_CODE (field) == FIELD_DECL) | |
2091 | { | |
2092 | tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field)); | |
2093 | if (elemtype != TREE_TYPE (field)) | |
2094 | break; | |
2095 | } | |
2096 | if (!field) | |
2097 | break; | |
2098 | ret = build_variant_type_copy (t); | |
2099 | TYPE_FIELDS (ret) = NULL_TREE; | |
2100 | ||
2101 | /* Here we make sure that as soon as we know we have to copy | |
2102 | T, that also fields reaching back to us will use the new | |
2103 | copy. It's okay if that copy still contains the old fields, | |
2104 | we won't look at them. */ | |
2105 | TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret; | |
2106 | mirror_fields (ret, t); | |
2107 | } | |
2108 | break; | |
2109 | } | |
2110 | ||
2111 | TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret; | |
2112 | return ret; | |
2113 | } | |
2114 | ||
6de9cd9a DN |
2115 | \f |
2116 | /* Return the type for a symbol. Special handling is required for character | |
2117 | types to get the correct level of indirection. | |
2118 | For functions return the return type. | |
ad6e2a18 TS |
2119 | For subroutines return void_type_node. |
2120 | Calling this multiple times for the same symbol should be avoided, | |
2121 | especially for character and array types. */ | |
c3e8c6b8 | 2122 | |
6de9cd9a DN |
2123 | tree |
2124 | gfc_sym_type (gfc_symbol * sym) | |
2125 | { | |
2126 | tree type; | |
2127 | int byref; | |
10174ddf | 2128 | bool restricted; |
6de9cd9a | 2129 | |
3070bab4 JW |
2130 | /* Procedure Pointers inside COMMON blocks. */ |
2131 | if (sym->attr.proc_pointer && sym->attr.in_common) | |
00625fae JW |
2132 | { |
2133 | /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */ | |
2134 | sym->attr.proc_pointer = 0; | |
2135 | type = build_pointer_type (gfc_get_function_type (sym)); | |
2136 | sym->attr.proc_pointer = 1; | |
2137 | return type; | |
2138 | } | |
2139 | ||
6de9cd9a DN |
2140 | if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function) |
2141 | return void_type_node; | |
2142 | ||
da4c6ed8 TS |
2143 | /* In the case of a function the fake result variable may have a |
2144 | type different from the function type, so don't return early in | |
2145 | that case. */ | |
2146 | if (sym->backend_decl && !sym->attr.function) | |
2147 | return TREE_TYPE (sym->backend_decl); | |
6de9cd9a | 2148 | |
665733c1 JJ |
2149 | if (sym->ts.type == BT_CHARACTER |
2150 | && ((sym->attr.function && sym->attr.is_bind_c) | |
2151 | || (sym->attr.result | |
2152 | && sym->ns->proc_name | |
9b548517 AV |
2153 | && sym->ns->proc_name->attr.is_bind_c) |
2154 | || (sym->ts.deferred && (!sym->ts.u.cl | |
2155 | || !sym->ts.u.cl->backend_decl)))) | |
06a54338 TB |
2156 | type = gfc_character1_type_node; |
2157 | else | |
2158 | type = gfc_typenode_for_spec (&sym->ts); | |
6de9cd9a | 2159 | |
06469efd | 2160 | if (sym->attr.dummy && !sym->attr.function && !sym->attr.value) |
6de9cd9a DN |
2161 | byref = 1; |
2162 | else | |
2163 | byref = 0; | |
2164 | ||
10174ddf | 2165 | restricted = !sym->attr.target && !sym->attr.pointer |
b3aefde2 | 2166 | && !sym->attr.proc_pointer && !sym->attr.cray_pointee; |
b3c1b8a1 MM |
2167 | if (!restricted) |
2168 | type = gfc_nonrestricted_type (type); | |
2169 | ||
c81e79b5 | 2170 | if (sym->attr.dimension || sym->attr.codimension) |
6de9cd9a DN |
2171 | { |
2172 | if (gfc_is_nodesc_array (sym)) | |
2173 | { | |
2174 | /* If this is a character argument of unknown length, just use the | |
2175 | base type. */ | |
2176 | if (sym->ts.type != BT_CHARACTER | |
b49a3de7 | 2177 | || !(sym->attr.dummy || sym->attr.function) |
bc21d315 | 2178 | || sym->ts.u.cl->backend_decl) |
6de9cd9a DN |
2179 | { |
2180 | type = gfc_get_nodesc_array_type (type, sym->as, | |
dcfef7d4 | 2181 | byref ? PACKED_FULL |
10174ddf MM |
2182 | : PACKED_STATIC, |
2183 | restricted); | |
6de9cd9a DN |
2184 | byref = 0; |
2185 | } | |
2186 | } | |
2187 | else | |
fad0afd7 JJ |
2188 | { |
2189 | enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN; | |
2190 | if (sym->attr.pointer) | |
fe4e525c TB |
2191 | akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT |
2192 | : GFC_ARRAY_POINTER; | |
fad0afd7 JJ |
2193 | else if (sym->attr.allocatable) |
2194 | akind = GFC_ARRAY_ALLOCATABLE; | |
fe4e525c TB |
2195 | type = gfc_build_array_type (type, sym->as, akind, restricted, |
2196 | sym->attr.contiguous); | |
fad0afd7 | 2197 | } |
a8b3b0b6 | 2198 | } |
6de9cd9a DN |
2199 | else |
2200 | { | |
571d54de DK |
2201 | if (sym->attr.allocatable || sym->attr.pointer |
2202 | || gfc_is_associate_pointer (sym)) | |
6de9cd9a DN |
2203 | type = gfc_build_pointer_type (sym, type); |
2204 | } | |
2205 | ||
2206 | /* We currently pass all parameters by reference. | |
2207 | See f95_get_function_decl. For dummy function parameters return the | |
2208 | function type. */ | |
2209 | if (byref) | |
1619aa6f PB |
2210 | { |
2211 | /* We must use pointer types for potentially absent variables. The | |
2212 | optimizers assume a reference type argument is never NULL. */ | |
30ff79fa JW |
2213 | if (sym->attr.optional |
2214 | || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master)) | |
1619aa6f PB |
2215 | type = build_pointer_type (type); |
2216 | else | |
10174ddf MM |
2217 | { |
2218 | type = build_reference_type (type); | |
2219 | if (restricted) | |
2220 | type = build_qualified_type (type, TYPE_QUAL_RESTRICT); | |
2221 | } | |
1619aa6f | 2222 | } |
6de9cd9a DN |
2223 | |
2224 | return (type); | |
2225 | } | |
2226 | \f | |
2227 | /* Layout and output debug info for a record type. */ | |
c3e8c6b8 | 2228 | |
6de9cd9a DN |
2229 | void |
2230 | gfc_finish_type (tree type) | |
2231 | { | |
2232 | tree decl; | |
2233 | ||
c2255bc4 AH |
2234 | decl = build_decl (input_location, |
2235 | TYPE_DECL, NULL_TREE, type); | |
6de9cd9a DN |
2236 | TYPE_STUB_DECL (type) = decl; |
2237 | layout_type (type); | |
2238 | rest_of_type_compilation (type, 1); | |
0e6df31e | 2239 | rest_of_decl_compilation (decl, 1, 0); |
6de9cd9a DN |
2240 | } |
2241 | \f | |
2242 | /* Add a field of given NAME and TYPE to the context of a UNION_TYPE | |
dfd6ece2 | 2243 | or RECORD_TYPE pointed to by CONTEXT. The new field is chained |
35151cd5 | 2244 | to the end of the field list pointed to by *CHAIN. |
6de9cd9a DN |
2245 | |
2246 | Returns a pointer to the new field. */ | |
c3e8c6b8 | 2247 | |
dfd6ece2 | 2248 | static tree |
35151cd5 | 2249 | gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain) |
dfd6ece2 NF |
2250 | { |
2251 | tree decl = build_decl (input_location, FIELD_DECL, name, type); | |
2252 | ||
2253 | DECL_CONTEXT (decl) = context; | |
910ad8de | 2254 | DECL_CHAIN (decl) = NULL_TREE; |
35151cd5 MM |
2255 | if (TYPE_FIELDS (context) == NULL_TREE) |
2256 | TYPE_FIELDS (context) = decl; | |
dfd6ece2 NF |
2257 | if (chain != NULL) |
2258 | { | |
2259 | if (*chain != NULL) | |
2260 | **chain = decl; | |
910ad8de | 2261 | *chain = &DECL_CHAIN (decl); |
dfd6ece2 NF |
2262 | } |
2263 | ||
2264 | return decl; | |
2265 | } | |
2266 | ||
2267 | /* Like `gfc_add_field_to_struct_1', but adds alignment | |
2268 | information. */ | |
2269 | ||
6de9cd9a | 2270 | tree |
35151cd5 | 2271 | gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain) |
6de9cd9a | 2272 | { |
35151cd5 | 2273 | tree decl = gfc_add_field_to_struct_1 (context, name, type, chain); |
6de9cd9a | 2274 | |
6de9cd9a DN |
2275 | DECL_INITIAL (decl) = 0; |
2276 | DECL_ALIGN (decl) = 0; | |
2277 | DECL_USER_ALIGN (decl) = 0; | |
6de9cd9a DN |
2278 | |
2279 | return decl; | |
2280 | } | |
2281 | ||
2282 | ||
6b887797 PT |
2283 | /* Copy the backend_decl and component backend_decls if |
2284 | the two derived type symbols are "equal", as described | |
2285 | in 4.4.2 and resolved by gfc_compare_derived_types. */ | |
2286 | ||
43afc047 TB |
2287 | int |
2288 | gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to, | |
0101807c | 2289 | bool from_gsym) |
6b887797 PT |
2290 | { |
2291 | gfc_component *to_cm; | |
2292 | gfc_component *from_cm; | |
2293 | ||
f29bda83 TB |
2294 | if (from == to) |
2295 | return 1; | |
2296 | ||
6b887797 PT |
2297 | if (from->backend_decl == NULL |
2298 | || !gfc_compare_derived_types (from, to)) | |
2299 | return 0; | |
2300 | ||
2301 | to->backend_decl = from->backend_decl; | |
2302 | ||
2303 | to_cm = to->components; | |
2304 | from_cm = from->components; | |
2305 | ||
2306 | /* Copy the component declarations. If a component is itself | |
2307 | a derived type, we need a copy of its component declarations. | |
2308 | This is done by recursing into gfc_get_derived_type and | |
2309 | ensures that the component's component declarations have | |
f04986a9 | 2310 | been built. If it is a character, we need the character |
6b887797 PT |
2311 | length, as well. */ |
2312 | for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next) | |
2313 | { | |
2314 | to_cm->backend_decl = from_cm->backend_decl; | |
78a1d149 JW |
2315 | if (from_cm->ts.type == BT_DERIVED |
2316 | && (!from_cm->attr.pointer || from_gsym)) | |
2317 | gfc_get_derived_type (to_cm->ts.u.derived); | |
2318 | else if (from_cm->ts.type == BT_CLASS | |
2319 | && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym)) | |
bc21d315 | 2320 | gfc_get_derived_type (to_cm->ts.u.derived); |
6b887797 | 2321 | else if (from_cm->ts.type == BT_CHARACTER) |
bc21d315 | 2322 | to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl; |
6b887797 PT |
2323 | } |
2324 | ||
2325 | return 1; | |
2326 | } | |
2327 | ||
2328 | ||
713485cc JW |
2329 | /* Build a tree node for a procedure pointer component. */ |
2330 | ||
2331 | tree | |
2332 | gfc_get_ppc_type (gfc_component* c) | |
2333 | { | |
2334 | tree t; | |
37513ce9 JW |
2335 | |
2336 | /* Explicit interface. */ | |
2337 | if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface) | |
2338 | return build_pointer_type (gfc_get_function_type (c->ts.interface)); | |
2339 | ||
2340 | /* Implicit interface (only return value may be known). */ | |
2341 | if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER) | |
2342 | t = gfc_typenode_for_spec (&c->ts); | |
713485cc JW |
2343 | else |
2344 | t = void_type_node; | |
37513ce9 | 2345 | |
b64fca63 | 2346 | return build_pointer_type (build_function_type_list (t, NULL_TREE)); |
713485cc JW |
2347 | } |
2348 | ||
2349 | ||
6b887797 PT |
2350 | /* Build a tree node for a derived type. If there are equal |
2351 | derived types, with different local names, these are built | |
2352 | at the same time. If an equal derived type has been built | |
2353 | in a parent namespace, this is used. */ | |
c3e8c6b8 | 2354 | |
7c1dab0d | 2355 | tree |
6de9cd9a DN |
2356 | gfc_get_derived_type (gfc_symbol * derived) |
2357 | { | |
35151cd5 | 2358 | tree typenode = NULL, field = NULL, field_type = NULL; |
3af8d8cb | 2359 | tree canonical = NULL_TREE; |
dfd6ece2 | 2360 | tree *chain = NULL; |
3af8d8cb | 2361 | bool got_canonical = false; |
8b704316 | 2362 | bool unlimited_entity = false; |
6de9cd9a | 2363 | gfc_component *c; |
6b887797 | 2364 | gfc_dt_list *dt; |
3af8d8cb | 2365 | gfc_namespace *ns; |
57905c2b | 2366 | tree tmp; |
6de9cd9a | 2367 | |
9f3880d1 TB |
2368 | if (derived->attr.unlimited_polymorphic |
2369 | || (flag_coarray == GFC_FCOARRAY_LIB | |
2370 | && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV | |
2371 | && derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)) | |
8b704316 PT |
2372 | return ptr_type_node; |
2373 | ||
5df445a2 TB |
2374 | if (flag_coarray != GFC_FCOARRAY_LIB |
2375 | && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV | |
2376 | && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE) | |
2377 | return gfc_get_int_type (gfc_default_integer_kind); | |
2378 | ||
c3f34952 TB |
2379 | if (derived && derived->attr.flavor == FL_PROCEDURE |
2380 | && derived->attr.generic) | |
2381 | derived = gfc_find_dt_in_generic (derived); | |
2382 | ||
a8b3b0b6 | 2383 | /* See if it's one of the iso_c_binding derived types. */ |
cadddfdd | 2384 | if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID) |
a8b3b0b6 | 2385 | { |
9dc35956 CR |
2386 | if (derived->backend_decl) |
2387 | return derived->backend_decl; | |
2388 | ||
089db47d CR |
2389 | if (derived->intmod_sym_id == ISOCBINDING_PTR) |
2390 | derived->backend_decl = ptr_type_node; | |
2391 | else | |
2392 | derived->backend_decl = pfunc_type_node; | |
9dc35956 | 2393 | |
a8b3b0b6 CR |
2394 | derived->ts.kind = gfc_index_integer_kind; |
2395 | derived->ts.type = BT_INTEGER; | |
2396 | /* Set the f90_type to BT_VOID as a way to recognize something of type | |
2397 | BT_INTEGER that needs to fit a void * for the purpose of the | |
2398 | iso_c_binding derived types. */ | |
2399 | derived->ts.f90_type = BT_VOID; | |
f04986a9 | 2400 | |
a8b3b0b6 CR |
2401 | return derived->backend_decl; |
2402 | } | |
3af8d8cb | 2403 | |
0101807c | 2404 | /* If use associated, use the module type for this one. */ |
9fa52231 TB |
2405 | if (derived->backend_decl == NULL |
2406 | && derived->attr.use_assoc | |
2407 | && derived->module | |
2408 | && gfc_get_module_backend_decl (derived)) | |
0101807c | 2409 | goto copy_derived_types; |
3af8d8cb | 2410 | |
9fa52231 TB |
2411 | /* The derived types from an earlier namespace can be used as the |
2412 | canonical type. */ | |
2413 | if (derived->backend_decl == NULL && !derived->attr.use_assoc | |
2414 | && gfc_global_ns_list) | |
3af8d8cb PT |
2415 | { |
2416 | for (ns = gfc_global_ns_list; | |
2417 | ns->translated && !got_canonical; | |
2418 | ns = ns->sibling) | |
2419 | { | |
2420 | dt = ns->derived_types; | |
2421 | for (; dt && !canonical; dt = dt->next) | |
2422 | { | |
43afc047 | 2423 | gfc_copy_dt_decls_ifequal (dt->derived, derived, true); |
3af8d8cb PT |
2424 | if (derived->backend_decl) |
2425 | got_canonical = true; | |
2426 | } | |
2427 | } | |
2428 | } | |
2429 | ||
2430 | /* Store up the canonical type to be added to this one. */ | |
2431 | if (got_canonical) | |
2432 | { | |
2433 | if (TYPE_CANONICAL (derived->backend_decl)) | |
2434 | canonical = TYPE_CANONICAL (derived->backend_decl); | |
2435 | else | |
2436 | canonical = derived->backend_decl; | |
2437 | ||
2438 | derived->backend_decl = NULL_TREE; | |
2439 | } | |
2440 | ||
6de9cd9a | 2441 | /* derived->backend_decl != 0 means we saw it before, but its |
436529ea | 2442 | components' backend_decl may have not been built. */ |
6de9cd9a | 2443 | if (derived->backend_decl) |
3e6d828d | 2444 | { |
37513ce9 JW |
2445 | /* Its components' backend_decl have been built or we are |
2446 | seeing recursion through the formal arglist of a procedure | |
2447 | pointer component. */ | |
ebd63afa | 2448 | if (TYPE_FIELDS (derived->backend_decl)) |
3e6d828d | 2449 | return derived->backend_decl; |
ebd63afa PT |
2450 | else if (derived->attr.abstract |
2451 | && derived->attr.proc_pointer_comp) | |
2452 | { | |
2453 | /* If an abstract derived type with procedure pointer | |
2454 | components has no other type of component, return the | |
2455 | backend_decl. Otherwise build the components if any of the | |
2456 | non-procedure pointer components have no backend_decl. */ | |
2457 | for (c = derived->components; c; c = c->next) | |
2458 | { | |
2459 | if (!c->attr.proc_pointer && c->backend_decl == NULL) | |
2460 | break; | |
2461 | else if (c->next == NULL) | |
2462 | return derived->backend_decl; | |
2463 | } | |
2464 | typenode = derived->backend_decl; | |
2465 | } | |
3e6d828d JW |
2466 | else |
2467 | typenode = derived->backend_decl; | |
2468 | } | |
6de9cd9a DN |
2469 | else |
2470 | { | |
2471 | /* We see this derived type first time, so build the type node. */ | |
2472 | typenode = make_node (RECORD_TYPE); | |
2473 | TYPE_NAME (typenode) = get_identifier (derived->name); | |
c61819ff | 2474 | TYPE_PACKED (typenode) = flag_pack_derived; |
6de9cd9a DN |
2475 | derived->backend_decl = typenode; |
2476 | } | |
2477 | ||
8b704316 PT |
2478 | if (derived->components |
2479 | && derived->components->ts.type == BT_DERIVED | |
2480 | && strcmp (derived->components->name, "_data") == 0 | |
2481 | && derived->components->ts.u.derived->attr.unlimited_polymorphic) | |
2482 | unlimited_entity = true; | |
2483 | ||
bce71376 PT |
2484 | /* Go through the derived type components, building them as |
2485 | necessary. The reason for doing this now is that it is | |
2486 | possible to recurse back to this derived type through a | |
2487 | pointer component (PR24092). If this happens, the fields | |
2488 | will be built and so we can return the type. */ | |
2489 | for (c = derived->components; c; c = c->next) | |
2490 | { | |
cf2b3c22 | 2491 | if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS) |
bce71376 PT |
2492 | continue; |
2493 | ||
c4984ab2 | 2494 | if ((!c->attr.pointer && !c->attr.proc_pointer) |
bc21d315 JW |
2495 | || c->ts.u.derived->backend_decl == NULL) |
2496 | c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived); | |
a8b3b0b6 | 2497 | |
efb63364 | 2498 | if (c->ts.u.derived->attr.is_iso_c) |
a8b3b0b6 CR |
2499 | { |
2500 | /* Need to copy the modified ts from the derived type. The | |
2501 | typespec was modified because C_PTR/C_FUNPTR are translated | |
2502 | into (void *) from derived types. */ | |
bc21d315 JW |
2503 | c->ts.type = c->ts.u.derived->ts.type; |
2504 | c->ts.kind = c->ts.u.derived->ts.kind; | |
2505 | c->ts.f90_type = c->ts.u.derived->ts.f90_type; | |
9dc35956 CR |
2506 | if (c->initializer) |
2507 | { | |
2508 | c->initializer->ts.type = c->ts.type; | |
2509 | c->initializer->ts.kind = c->ts.kind; | |
2510 | c->initializer->ts.f90_type = c->ts.f90_type; | |
2511 | c->initializer->expr_type = EXPR_NULL; | |
2512 | } | |
a8b3b0b6 | 2513 | } |
bce71376 PT |
2514 | } |
2515 | ||
2516 | if (TYPE_FIELDS (derived->backend_decl)) | |
2517 | return derived->backend_decl; | |
2518 | ||
6de9cd9a DN |
2519 | /* Build the type member list. Install the newly created RECORD_TYPE |
2520 | node as DECL_CONTEXT of each FIELD_DECL. */ | |
6de9cd9a DN |
2521 | for (c = derived->components; c; c = c->next) |
2522 | { | |
57905c2b PT |
2523 | /* Prevent infinite recursion, when the procedure pointer type is |
2524 | the same as derived, by forcing the procedure pointer component to | |
2525 | be built as if the explicit interface does not exist. */ | |
2526 | if (c->attr.proc_pointer | |
2527 | && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS) | |
2528 | || (c->ts.u.derived | |
2529 | && !gfc_compare_derived_types (derived, c->ts.u.derived)))) | |
713485cc | 2530 | field_type = gfc_get_ppc_type (c); |
57905c2b PT |
2531 | else if (c->attr.proc_pointer && derived->backend_decl) |
2532 | { | |
2533 | tmp = build_function_type_list (derived->backend_decl, NULL_TREE); | |
2534 | field_type = build_pointer_type (tmp); | |
2535 | } | |
cf2b3c22 | 2536 | else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS) |
bc21d315 | 2537 | field_type = c->ts.u.derived->backend_decl; |
6de9cd9a DN |
2538 | else |
2539 | { | |
2b3dc0db | 2540 | if (c->ts.type == BT_CHARACTER && !c->ts.deferred) |
6de9cd9a DN |
2541 | { |
2542 | /* Evaluate the string length. */ | |
bc21d315 JW |
2543 | gfc_conv_const_charlen (c->ts.u.cl); |
2544 | gcc_assert (c->ts.u.cl->backend_decl); | |
6de9cd9a | 2545 | } |
2b3dc0db PT |
2546 | else if (c->ts.type == BT_CHARACTER) |
2547 | c->ts.u.cl->backend_decl | |
2548 | = build_int_cst (gfc_charlen_type_node, 0); | |
6de9cd9a DN |
2549 | |
2550 | field_type = gfc_typenode_for_spec (&c->ts); | |
2551 | } | |
2552 | ||
1f2959f0 | 2553 | /* This returns an array descriptor type. Initialization may be |
6de9cd9a | 2554 | required. */ |
241e79cf | 2555 | if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer ) |
6de9cd9a | 2556 | { |
d4b7d0f0 | 2557 | if (c->attr.pointer || c->attr.allocatable) |
6de9cd9a | 2558 | { |
fad0afd7 | 2559 | enum gfc_array_kind akind; |
d4b7d0f0 | 2560 | if (c->attr.pointer) |
fe4e525c TB |
2561 | akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT |
2562 | : GFC_ARRAY_POINTER; | |
fad0afd7 JJ |
2563 | else |
2564 | akind = GFC_ARRAY_ALLOCATABLE; | |
1f2959f0 | 2565 | /* Pointers to arrays aren't actually pointer types. The |
e7dc5b4f | 2566 | descriptors are separate, but the data is common. */ |
10174ddf MM |
2567 | field_type = gfc_build_array_type (field_type, c->as, akind, |
2568 | !c->attr.target | |
fe4e525c TB |
2569 | && !c->attr.pointer, |
2570 | c->attr.contiguous); | |
6de9cd9a DN |
2571 | } |
2572 | else | |
dcfef7d4 | 2573 | field_type = gfc_get_nodesc_array_type (field_type, c->as, |
10174ddf MM |
2574 | PACKED_STATIC, |
2575 | !c->attr.target); | |
6de9cd9a | 2576 | } |
cf2b3c22 | 2577 | else if ((c->attr.pointer || c->attr.allocatable) |
8b704316 PT |
2578 | && !c->attr.proc_pointer |
2579 | && !(unlimited_entity && c == derived->components)) | |
6de9cd9a DN |
2580 | field_type = build_pointer_type (field_type); |
2581 | ||
4bfd470b TB |
2582 | if (c->attr.pointer) |
2583 | field_type = gfc_nonrestricted_type (field_type); | |
2584 | ||
c9dfc414 | 2585 | /* vtype fields can point to different types to the base type. */ |
8b704316 PT |
2586 | if (c->ts.type == BT_DERIVED |
2587 | && c->ts.u.derived && c->ts.u.derived->attr.vtype) | |
0a8c9a13 PT |
2588 | field_type = build_pointer_type_for_mode (TREE_TYPE (field_type), |
2589 | ptr_mode, true); | |
c9dfc414 | 2590 | |
f04986a9 PT |
2591 | /* Ensure that the CLASS language specific flag is set. */ |
2592 | if (c->ts.type == BT_CLASS) | |
2593 | { | |
2594 | if (POINTER_TYPE_P (field_type)) | |
2595 | GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1; | |
2596 | else | |
2597 | GFC_CLASS_TYPE_P (field_type) = 1; | |
2598 | } | |
2599 | ||
35151cd5 | 2600 | field = gfc_add_field_to_struct (typenode, |
dfd6ece2 NF |
2601 | get_identifier (c->name), |
2602 | field_type, &chain); | |
dfcf0b12 FXC |
2603 | if (c->loc.lb) |
2604 | gfc_set_decl_location (field, &c->loc); | |
2605 | else if (derived->declared_at.lb) | |
2606 | gfc_set_decl_location (field, &derived->declared_at); | |
6de9cd9a | 2607 | |
92d28cbb JJ |
2608 | gfc_finish_decl_attrs (field, &c->attr); |
2609 | ||
6de9cd9a DN |
2610 | DECL_PACKED (field) |= TYPE_PACKED (typenode); |
2611 | ||
bce71376 PT |
2612 | gcc_assert (field); |
2613 | if (!c->backend_decl) | |
2614 | c->backend_decl = field; | |
6de9cd9a DN |
2615 | } |
2616 | ||
35151cd5 | 2617 | /* Now lay out the derived type, including the fields. */ |
64754ed5 RG |
2618 | if (canonical) |
2619 | TYPE_CANONICAL (typenode) = canonical; | |
6de9cd9a DN |
2620 | |
2621 | gfc_finish_type (typenode); | |
dfcf0b12 | 2622 | gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at); |
96ffc6cd TK |
2623 | if (derived->module && derived->ns->proc_name |
2624 | && derived->ns->proc_name->attr.flavor == FL_MODULE) | |
a64f5186 JJ |
2625 | { |
2626 | if (derived->ns->proc_name->backend_decl | |
2627 | && TREE_CODE (derived->ns->proc_name->backend_decl) | |
2628 | == NAMESPACE_DECL) | |
2629 | { | |
2630 | TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl; | |
2631 | DECL_CONTEXT (TYPE_STUB_DECL (typenode)) | |
2632 | = derived->ns->proc_name->backend_decl; | |
2633 | } | |
2634 | } | |
6de9cd9a DN |
2635 | |
2636 | derived->backend_decl = typenode; | |
2637 | ||
3af8d8cb PT |
2638 | copy_derived_types: |
2639 | ||
a64f5186 | 2640 | for (dt = gfc_derived_types; dt; dt = dt->next) |
43afc047 | 2641 | gfc_copy_dt_decls_ifequal (derived, dt->derived, false); |
6b887797 | 2642 | |
e0e85e06 | 2643 | return derived->backend_decl; |
6de9cd9a | 2644 | } |
e0e85e06 PT |
2645 | |
2646 | ||
6de9cd9a DN |
2647 | int |
2648 | gfc_return_by_reference (gfc_symbol * sym) | |
2649 | { | |
2650 | if (!sym->attr.function) | |
2651 | return 0; | |
2652 | ||
b49a3de7 | 2653 | if (sym->attr.dimension) |
6de9cd9a DN |
2654 | return 1; |
2655 | ||
665733c1 JJ |
2656 | if (sym->ts.type == BT_CHARACTER |
2657 | && !sym->attr.is_bind_c | |
2658 | && (!sym->attr.result | |
2659 | || !sym->ns->proc_name | |
2660 | || !sym->ns->proc_name->attr.is_bind_c)) | |
6de9cd9a DN |
2661 | return 1; |
2662 | ||
973ff4c0 TS |
2663 | /* Possibly return complex numbers by reference for g77 compatibility. |
2664 | We don't do this for calls to intrinsics (as the library uses the | |
2665 | -fno-f2c calling convention), nor for calls to functions which always | |
2666 | require an explicit interface, as no compatibility problems can | |
2667 | arise there. */ | |
c61819ff | 2668 | if (flag_f2c && sym->ts.type == BT_COMPLEX |
973ff4c0 TS |
2669 | && !sym->attr.intrinsic && !sym->attr.always_explicit) |
2670 | return 1; | |
66e4ab31 | 2671 | |
6de9cd9a DN |
2672 | return 0; |
2673 | } | |
2674 | \f | |
d198b59a JJ |
2675 | static tree |
2676 | gfc_get_mixed_entry_union (gfc_namespace *ns) | |
2677 | { | |
2678 | tree type; | |
dfd6ece2 | 2679 | tree *chain = NULL; |
d198b59a JJ |
2680 | char name[GFC_MAX_SYMBOL_LEN + 1]; |
2681 | gfc_entry_list *el, *el2; | |
2682 | ||
2683 | gcc_assert (ns->proc_name->attr.mixed_entry_master); | |
2684 | gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0); | |
2685 | ||
2686 | snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7); | |
2687 | ||
2688 | /* Build the type node. */ | |
2689 | type = make_node (UNION_TYPE); | |
2690 | ||
2691 | TYPE_NAME (type) = get_identifier (name); | |
d198b59a JJ |
2692 | |
2693 | for (el = ns->entries; el; el = el->next) | |
2694 | { | |
2695 | /* Search for duplicates. */ | |
2696 | for (el2 = ns->entries; el2 != el; el2 = el2->next) | |
2697 | if (el2->sym->result == el->sym->result) | |
2698 | break; | |
2699 | ||
2700 | if (el == el2) | |
35151cd5 | 2701 | gfc_add_field_to_struct_1 (type, |
dfd6ece2 NF |
2702 | get_identifier (el->sym->result->name), |
2703 | gfc_sym_type (el->sym->result), &chain); | |
d198b59a JJ |
2704 | } |
2705 | ||
2706 | /* Finish off the type. */ | |
d198b59a | 2707 | gfc_finish_type (type); |
dfcf0b12 | 2708 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1; |
d198b59a JJ |
2709 | return type; |
2710 | } | |
2711 | \f | |
6dfd24f7 TB |
2712 | /* Create a "fn spec" based on the formal arguments; |
2713 | cf. create_function_arglist. */ | |
2714 | ||
2715 | static tree | |
2716 | create_fn_spec (gfc_symbol *sym, tree fntype) | |
2717 | { | |
2718 | char spec[150]; | |
2719 | size_t spec_len; | |
2720 | gfc_formal_arglist *f; | |
2721 | tree tmp; | |
2722 | ||
2723 | memset (&spec, 0, sizeof (spec)); | |
2724 | spec[0] = '.'; | |
2725 | spec_len = 1; | |
2726 | ||
2727 | if (sym->attr.entry_master) | |
2728 | spec[spec_len++] = 'R'; | |
2729 | if (gfc_return_by_reference (sym)) | |
2730 | { | |
2731 | gfc_symbol *result = sym->result ? sym->result : sym; | |
2732 | ||
2733 | if (result->attr.pointer || sym->attr.proc_pointer) | |
2734 | spec[spec_len++] = '.'; | |
2735 | else | |
2736 | spec[spec_len++] = 'w'; | |
2737 | if (sym->ts.type == BT_CHARACTER) | |
2738 | spec[spec_len++] = 'R'; | |
2739 | } | |
2740 | ||
4cbc9039 | 2741 | for (f = gfc_sym_get_dummy_args (sym); f; f = f->next) |
6dfd24f7 TB |
2742 | if (spec_len < sizeof (spec)) |
2743 | { | |
2744 | if (!f->sym || f->sym->attr.pointer || f->sym->attr.target | |
39752c6b TB |
2745 | || f->sym->attr.external || f->sym->attr.cray_pointer |
2746 | || (f->sym->ts.type == BT_DERIVED | |
2747 | && (f->sym->ts.u.derived->attr.proc_pointer_comp | |
2748 | || f->sym->ts.u.derived->attr.pointer_comp)) | |
2749 | || (f->sym->ts.type == BT_CLASS | |
2750 | && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp | |
2751 | || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))) | |
6dfd24f7 TB |
2752 | spec[spec_len++] = '.'; |
2753 | else if (f->sym->attr.intent == INTENT_IN) | |
2754 | spec[spec_len++] = 'r'; | |
2755 | else if (f->sym) | |
2756 | spec[spec_len++] = 'w'; | |
2757 | } | |
2758 | ||
2759 | tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec)); | |
2760 | tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype)); | |
2761 | return build_type_attribute_variant (fntype, tmp); | |
2762 | } | |
2763 | ||
2764 | ||
6de9cd9a DN |
2765 | tree |
2766 | gfc_get_function_type (gfc_symbol * sym) | |
2767 | { | |
2768 | tree type; | |
57ef133b | 2769 | vec<tree, va_gc> *typelist = NULL; |
6de9cd9a DN |
2770 | gfc_formal_arglist *f; |
2771 | gfc_symbol *arg; | |
57ef133b BE |
2772 | int alternate_return = 0; |
2773 | bool is_varargs = true; | |
6de9cd9a | 2774 | |
ecf24057 FXC |
2775 | /* Make sure this symbol is a function, a subroutine or the main |
2776 | program. */ | |
2777 | gcc_assert (sym->attr.flavor == FL_PROCEDURE | |
2778 | || sym->attr.flavor == FL_PROGRAM); | |
6de9cd9a | 2779 | |
3434c119 MM |
2780 | /* To avoid recursing infinitely on recursive types, we use error_mark_node |
2781 | so that they can be detected here and handled further down. */ | |
2782 | if (sym->backend_decl == NULL) | |
2783 | sym->backend_decl = error_mark_node; | |
2784 | else if (sym->backend_decl == error_mark_node) | |
57ef133b | 2785 | goto arg_type_list_done; |
3434c119 MM |
2786 | else if (sym->attr.proc_pointer) |
2787 | return TREE_TYPE (TREE_TYPE (sym->backend_decl)); | |
2788 | else | |
2789 | return TREE_TYPE (sym->backend_decl); | |
6de9cd9a | 2790 | |
3d79abbd | 2791 | if (sym->attr.entry_master) |
6c32445b | 2792 | /* Additional parameter for selecting an entry point. */ |
9771b263 | 2793 | vec_safe_push (typelist, gfc_array_index_type); |
3d79abbd | 2794 | |
bfd61955 TB |
2795 | if (sym->result) |
2796 | arg = sym->result; | |
2797 | else | |
2798 | arg = sym; | |
2799 | ||
2800 | if (arg->ts.type == BT_CHARACTER) | |
bc21d315 | 2801 | gfc_conv_const_charlen (arg->ts.u.cl); |
bfd61955 | 2802 | |
6de9cd9a DN |
2803 | /* Some functions we use an extra parameter for the return value. */ |
2804 | if (gfc_return_by_reference (sym)) | |
2805 | { | |
6de9cd9a | 2806 | type = gfc_sym_type (arg); |
973ff4c0 | 2807 | if (arg->ts.type == BT_COMPLEX |
6de9cd9a DN |
2808 | || arg->attr.dimension |
2809 | || arg->ts.type == BT_CHARACTER) | |
2810 | type = build_reference_type (type); | |
2811 | ||
9771b263 | 2812 | vec_safe_push (typelist, type); |
6de9cd9a | 2813 | if (arg->ts.type == BT_CHARACTER) |
8d51f26f PT |
2814 | { |
2815 | if (!arg->ts.deferred) | |
2816 | /* Transfer by value. */ | |
9771b263 | 2817 | vec_safe_push (typelist, gfc_charlen_type_node); |
8d51f26f PT |
2818 | else |
2819 | /* Deferred character lengths are transferred by reference | |
2820 | so that the value can be returned. */ | |
9771b263 | 2821 | vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node)); |
8d51f26f | 2822 | } |
6de9cd9a DN |
2823 | } |
2824 | ||
436529ea | 2825 | /* Build the argument types for the function. */ |
4cbc9039 | 2826 | for (f = gfc_sym_get_dummy_args (sym); f; f = f->next) |
6de9cd9a DN |
2827 | { |
2828 | arg = f->sym; | |
2829 | if (arg) | |
2830 | { | |
2831 | /* Evaluate constant character lengths here so that they can be | |
2832 | included in the type. */ | |
2833 | if (arg->ts.type == BT_CHARACTER) | |
bc21d315 | 2834 | gfc_conv_const_charlen (arg->ts.u.cl); |
6de9cd9a DN |
2835 | |
2836 | if (arg->attr.flavor == FL_PROCEDURE) | |
2837 | { | |
2838 | type = gfc_get_function_type (arg); | |
2839 | type = build_pointer_type (type); | |
2840 | } | |
2841 | else | |
2842 | type = gfc_sym_type (arg); | |
2843 | ||
2844 | /* Parameter Passing Convention | |
2845 | ||
2846 | We currently pass all parameters by reference. | |
2847 | Parameters with INTENT(IN) could be passed by value. | |
2848 | The problem arises if a function is called via an implicit | |
2849 | prototype. In this situation the INTENT is not known. | |
2850 | For this reason all parameters to global functions must be | |
aa9c57ec | 2851 | passed by reference. Passing by value would potentially |
6de9cd9a | 2852 | generate bad code. Worse there would be no way of telling that |
c3e8c6b8 | 2853 | this code was bad, except that it would give incorrect results. |
6de9cd9a DN |
2854 | |
2855 | Contained procedures could pass by value as these are never | |
e2ae1407 | 2856 | used without an explicit interface, and cannot be passed as |
c3e8c6b8 | 2857 | actual parameters for a dummy procedure. */ |
8d51f26f | 2858 | |
9771b263 | 2859 | vec_safe_push (typelist, type); |
6de9cd9a DN |
2860 | } |
2861 | else | |
2862 | { | |
2863 | if (sym->attr.subroutine) | |
2864 | alternate_return = 1; | |
2865 | } | |
2866 | } | |
2867 | ||
2868 | /* Add hidden string length parameters. */ | |
4cbc9039 | 2869 | for (f = gfc_sym_get_dummy_args (sym); f; f = f->next) |
8d51f26f PT |
2870 | { |
2871 | arg = f->sym; | |
2872 | if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c) | |
2873 | { | |
2874 | if (!arg->ts.deferred) | |
2875 | /* Transfer by value. */ | |
2876 | type = gfc_charlen_type_node; | |
2877 | else | |
2878 | /* Deferred character lengths are transferred by reference | |
2879 | so that the value can be returned. */ | |
2880 | type = build_pointer_type (gfc_charlen_type_node); | |
2881 | ||
9771b263 | 2882 | vec_safe_push (typelist, type); |
8d51f26f PT |
2883 | } |
2884 | } | |
6de9cd9a | 2885 | |
9771b263 | 2886 | if (!vec_safe_is_empty (typelist) |
6c32445b NF |
2887 | || sym->attr.is_main_program |
2888 | || sym->attr.if_source != IFSRC_UNKNOWN) | |
2889 | is_varargs = false; | |
6de9cd9a | 2890 | |
57ef133b | 2891 | if (sym->backend_decl == error_mark_node) |
3434c119 MM |
2892 | sym->backend_decl = NULL_TREE; |
2893 | ||
57ef133b BE |
2894 | arg_type_list_done: |
2895 | ||
6de9cd9a DN |
2896 | if (alternate_return) |
2897 | type = integer_type_node; | |
2898 | else if (!sym->attr.function || gfc_return_by_reference (sym)) | |
2899 | type = void_type_node; | |
d198b59a JJ |
2900 | else if (sym->attr.mixed_entry_master) |
2901 | type = gfc_get_mixed_entry_union (sym->ns); | |
c61819ff | 2902 | else if (flag_f2c && sym->ts.type == BT_REAL |
da4c6ed8 TS |
2903 | && sym->ts.kind == gfc_default_real_kind |
2904 | && !sym->attr.always_explicit) | |
2905 | { | |
f04986a9 | 2906 | /* Special case: f2c calling conventions require that (scalar) |
da4c6ed8 TS |
2907 | default REAL functions return the C type double instead. f2c |
2908 | compatibility is only an issue with functions that don't | |
2909 | require an explicit interface, as only these could be | |
2910 | implemented in Fortran 77. */ | |
2911 | sym->ts.kind = gfc_default_double_kind; | |
2912 | type = gfc_typenode_for_spec (&sym->ts); | |
2913 | sym->ts.kind = gfc_default_real_kind; | |
2914 | } | |
726d8566 JW |
2915 | else if (sym->result && sym->result->attr.proc_pointer) |
2916 | /* Procedure pointer return values. */ | |
3070bab4 JW |
2917 | { |
2918 | if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0) | |
2919 | { | |
2920 | /* Unset proc_pointer as gfc_get_function_type | |
2921 | is called recursively. */ | |
2922 | sym->result->attr.proc_pointer = 0; | |
2923 | type = build_pointer_type (gfc_get_function_type (sym->result)); | |
2924 | sym->result->attr.proc_pointer = 1; | |
2925 | } | |
2926 | else | |
2927 | type = gfc_sym_type (sym->result); | |
2928 | } | |
6de9cd9a DN |
2929 | else |
2930 | type = gfc_sym_type (sym); | |
2931 | ||
57ef133b | 2932 | if (is_varargs) |
6c32445b NF |
2933 | type = build_varargs_function_type_vec (type, typelist); |
2934 | else | |
2935 | type = build_function_type_vec (type, typelist); | |
6dfd24f7 | 2936 | type = create_fn_spec (sym, type); |
6de9cd9a DN |
2937 | |
2938 | return type; | |
2939 | } | |
2940 | \f | |
e2cad04b | 2941 | /* Language hooks for middle-end access to type nodes. */ |
6de9cd9a DN |
2942 | |
2943 | /* Return an integer type with BITS bits of precision, | |
2944 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
2945 | ||
2946 | tree | |
2947 | gfc_type_for_size (unsigned bits, int unsignedp) | |
2948 | { | |
e2cad04b RH |
2949 | if (!unsignedp) |
2950 | { | |
2951 | int i; | |
2952 | for (i = 0; i <= MAX_INT_KINDS; ++i) | |
2953 | { | |
2954 | tree type = gfc_integer_types[i]; | |
2955 | if (type && bits == TYPE_PRECISION (type)) | |
2956 | return type; | |
2957 | } | |
5218394a PB |
2958 | |
2959 | /* Handle TImode as a special case because it is used by some backends | |
df2fba9e | 2960 | (e.g. ARM) even though it is not available for normal use. */ |
5218394a PB |
2961 | #if HOST_BITS_PER_WIDE_INT >= 64 |
2962 | if (bits == TYPE_PRECISION (intTI_type_node)) | |
2963 | return intTI_type_node; | |
2964 | #endif | |
82aa8722 JJ |
2965 | |
2966 | if (bits <= TYPE_PRECISION (intQI_type_node)) | |
2967 | return intQI_type_node; | |
2968 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
2969 | return intHI_type_node; | |
2970 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
2971 | return intSI_type_node; | |
2972 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
2973 | return intDI_type_node; | |
2974 | if (bits <= TYPE_PRECISION (intTI_type_node)) | |
2975 | return intTI_type_node; | |
e2cad04b RH |
2976 | } |
2977 | else | |
2978 | { | |
82aa8722 | 2979 | if (bits <= TYPE_PRECISION (unsigned_intQI_type_node)) |
e2cad04b | 2980 | return unsigned_intQI_type_node; |
82aa8722 | 2981 | if (bits <= TYPE_PRECISION (unsigned_intHI_type_node)) |
e2cad04b | 2982 | return unsigned_intHI_type_node; |
82aa8722 | 2983 | if (bits <= TYPE_PRECISION (unsigned_intSI_type_node)) |
e2cad04b | 2984 | return unsigned_intSI_type_node; |
82aa8722 | 2985 | if (bits <= TYPE_PRECISION (unsigned_intDI_type_node)) |
e2cad04b | 2986 | return unsigned_intDI_type_node; |
82aa8722 | 2987 | if (bits <= TYPE_PRECISION (unsigned_intTI_type_node)) |
e2cad04b RH |
2988 | return unsigned_intTI_type_node; |
2989 | } | |
6de9cd9a | 2990 | |
e2cad04b | 2991 | return NULL_TREE; |
6de9cd9a DN |
2992 | } |
2993 | ||
e2cad04b RH |
2994 | /* Return a data type that has machine mode MODE. If the mode is an |
2995 | integer, then UNSIGNEDP selects between signed and unsigned types. */ | |
6de9cd9a DN |
2996 | |
2997 | tree | |
ef4bddc2 | 2998 | gfc_type_for_mode (machine_mode mode, int unsignedp) |
6de9cd9a | 2999 | { |
e2cad04b RH |
3000 | int i; |
3001 | tree *base; | |
3002 | ||
3003 | if (GET_MODE_CLASS (mode) == MODE_FLOAT) | |
3004 | base = gfc_real_types; | |
3005 | else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT) | |
3006 | base = gfc_complex_types; | |
3007 | else if (SCALAR_INT_MODE_P (mode)) | |
82aa8722 JJ |
3008 | { |
3009 | tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp); | |
3010 | return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE; | |
3011 | } | |
e2cad04b | 3012 | else if (VECTOR_MODE_P (mode)) |
6de9cd9a | 3013 | { |
ef4bddc2 | 3014 | machine_mode inner_mode = GET_MODE_INNER (mode); |
f676971a EC |
3015 | tree inner_type = gfc_type_for_mode (inner_mode, unsignedp); |
3016 | if (inner_type != NULL_TREE) | |
3017 | return build_vector_type_for_mode (inner_type, mode); | |
e2cad04b | 3018 | return NULL_TREE; |
6de9cd9a | 3019 | } |
e2cad04b | 3020 | else |
1a5ffec4 | 3021 | return NULL_TREE; |
6de9cd9a | 3022 | |
e2cad04b RH |
3023 | for (i = 0; i <= MAX_REAL_KINDS; ++i) |
3024 | { | |
3025 | tree type = base[i]; | |
3026 | if (type && mode == TYPE_MODE (type)) | |
3027 | return type; | |
3028 | } | |
3029 | ||
3030 | return NULL_TREE; | |
3031 | } | |
3032 | ||
fad0afd7 JJ |
3033 | /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO |
3034 | in that case. */ | |
3035 | ||
3036 | bool | |
3037 | gfc_get_array_descr_info (const_tree type, struct array_descr_info *info) | |
3038 | { | |
3039 | int rank, dim; | |
3040 | bool indirect = false; | |
3041 | tree etype, ptype, field, t, base_decl; | |
c4fae39e | 3042 | tree data_off, dim_off, dim_size, elem_size; |
fad0afd7 JJ |
3043 | tree lower_suboff, upper_suboff, stride_suboff; |
3044 | ||
3045 | if (! GFC_DESCRIPTOR_TYPE_P (type)) | |
3046 | { | |
3047 | if (! POINTER_TYPE_P (type)) | |
3048 | return false; | |
3049 | type = TREE_TYPE (type); | |
3050 | if (! GFC_DESCRIPTOR_TYPE_P (type)) | |
3051 | return false; | |
3052 | indirect = true; | |
3053 | } | |
3054 | ||
3055 | rank = GFC_TYPE_ARRAY_RANK (type); | |
3056 | if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0]))) | |
3057 | return false; | |
3058 | ||
3059 | etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type); | |
3060 | gcc_assert (POINTER_TYPE_P (etype)); | |
3061 | etype = TREE_TYPE (etype); | |
badd9e69 TB |
3062 | |
3063 | /* If the type is not a scalar coarray. */ | |
3064 | if (TREE_CODE (etype) == ARRAY_TYPE) | |
3065 | etype = TREE_TYPE (etype); | |
3066 | ||
fad0afd7 JJ |
3067 | /* Can't handle variable sized elements yet. */ |
3068 | if (int_size_in_bytes (etype) <= 0) | |
3069 | return false; | |
3070 | /* Nor non-constant lower bounds in assumed shape arrays. */ | |
fe4e525c TB |
3071 | if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE |
3072 | || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT) | |
fad0afd7 JJ |
3073 | { |
3074 | for (dim = 0; dim < rank; dim++) | |
3075 | if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE | |
3076 | || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST) | |
3077 | return false; | |
3078 | } | |
3079 | ||
3080 | memset (info, '\0', sizeof (*info)); | |
3081 | info->ndimensions = rank; | |
616743a8 | 3082 | info->ordering = array_descr_ordering_column_major; |
fad0afd7 JJ |
3083 | info->element_type = etype; |
3084 | ptype = build_pointer_type (gfc_array_index_type); | |
d560566a AO |
3085 | base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect); |
3086 | if (!base_decl) | |
fad0afd7 | 3087 | { |
54f9184b JJ |
3088 | base_decl = make_node (DEBUG_EXPR_DECL); |
3089 | DECL_ARTIFICIAL (base_decl) = 1; | |
3090 | TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype; | |
3091 | DECL_MODE (base_decl) = TYPE_MODE (TREE_TYPE (base_decl)); | |
d560566a | 3092 | GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl; |
fad0afd7 | 3093 | } |
d560566a AO |
3094 | info->base_decl = base_decl; |
3095 | if (indirect) | |
3096 | base_decl = build1 (INDIRECT_REF, ptype, base_decl); | |
fad0afd7 | 3097 | |
de870512 JJ |
3098 | if (GFC_TYPE_ARRAY_SPAN (type)) |
3099 | elem_size = GFC_TYPE_ARRAY_SPAN (type); | |
3100 | else | |
3101 | elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype)); | |
fad0afd7 JJ |
3102 | field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type)); |
3103 | data_off = byte_position (field); | |
910ad8de NF |
3104 | field = DECL_CHAIN (field); |
3105 | field = DECL_CHAIN (field); | |
3106 | field = DECL_CHAIN (field); | |
fad0afd7 JJ |
3107 | dim_off = byte_position (field); |
3108 | dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field))); | |
3109 | field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field))); | |
3110 | stride_suboff = byte_position (field); | |
910ad8de | 3111 | field = DECL_CHAIN (field); |
fad0afd7 | 3112 | lower_suboff = byte_position (field); |
910ad8de | 3113 | field = DECL_CHAIN (field); |
fad0afd7 JJ |
3114 | upper_suboff = byte_position (field); |
3115 | ||
3116 | t = base_decl; | |
3117 | if (!integer_zerop (data_off)) | |
5d49b6a7 | 3118 | t = fold_build_pointer_plus (t, data_off); |
fad0afd7 JJ |
3119 | t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t); |
3120 | info->data_location = build1 (INDIRECT_REF, ptr_type_node, t); | |
3121 | if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE) | |
3122 | info->allocated = build2 (NE_EXPR, boolean_type_node, | |
3123 | info->data_location, null_pointer_node); | |
fe4e525c TB |
3124 | else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER |
3125 | || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT) | |
fad0afd7 JJ |
3126 | info->associated = build2 (NE_EXPR, boolean_type_node, |
3127 | info->data_location, null_pointer_node); | |
3128 | ||
3129 | for (dim = 0; dim < rank; dim++) | |
3130 | { | |
5d49b6a7 RG |
3131 | t = fold_build_pointer_plus (base_decl, |
3132 | size_binop (PLUS_EXPR, | |
3133 | dim_off, lower_suboff)); | |
fad0afd7 JJ |
3134 | t = build1 (INDIRECT_REF, gfc_array_index_type, t); |
3135 | info->dimen[dim].lower_bound = t; | |
5d49b6a7 RG |
3136 | t = fold_build_pointer_plus (base_decl, |
3137 | size_binop (PLUS_EXPR, | |
3138 | dim_off, upper_suboff)); | |
fad0afd7 JJ |
3139 | t = build1 (INDIRECT_REF, gfc_array_index_type, t); |
3140 | info->dimen[dim].upper_bound = t; | |
fe4e525c TB |
3141 | if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE |
3142 | || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT) | |
fad0afd7 JJ |
3143 | { |
3144 | /* Assumed shape arrays have known lower bounds. */ | |
3145 | info->dimen[dim].upper_bound | |
3146 | = build2 (MINUS_EXPR, gfc_array_index_type, | |
3147 | info->dimen[dim].upper_bound, | |
3148 | info->dimen[dim].lower_bound); | |
3149 | info->dimen[dim].lower_bound | |
3150 | = fold_convert (gfc_array_index_type, | |
3151 | GFC_TYPE_ARRAY_LBOUND (type, dim)); | |
3152 | info->dimen[dim].upper_bound | |
3153 | = build2 (PLUS_EXPR, gfc_array_index_type, | |
3154 | info->dimen[dim].lower_bound, | |
3155 | info->dimen[dim].upper_bound); | |
3156 | } | |
5d49b6a7 RG |
3157 | t = fold_build_pointer_plus (base_decl, |
3158 | size_binop (PLUS_EXPR, | |
3159 | dim_off, stride_suboff)); | |
fad0afd7 JJ |
3160 | t = build1 (INDIRECT_REF, gfc_array_index_type, t); |
3161 | t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size); | |
3162 | info->dimen[dim].stride = t; | |
3163 | dim_off = size_binop (PLUS_EXPR, dim_off, dim_size); | |
3164 | } | |
3165 | ||
3166 | return true; | |
3167 | } | |
3168 | ||
b5116268 TB |
3169 | |
3170 | /* Create a type to handle vector subscripts for coarray library calls. It | |
3171 | has the form: | |
3172 | struct caf_vector_t { | |
3173 | size_t nvec; // size of the vector | |
3174 | union { | |
3175 | struct { | |
3176 | void *vector; | |
3177 | int kind; | |
3178 | } v; | |
3179 | struct { | |
3180 | ptrdiff_t lower_bound; | |
3181 | ptrdiff_t upper_bound; | |
3182 | ptrdiff_t stride; | |
3183 | } triplet; | |
3184 | } u; | |
3185 | } | |
3186 | where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector | |
3187 | size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */ | |
3188 | ||
3189 | tree | |
3190 | gfc_get_caf_vector_type (int dim) | |
3191 | { | |
3192 | static tree vector_types[GFC_MAX_DIMENSIONS]; | |
3193 | static tree vec_type = NULL_TREE; | |
3194 | tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain; | |
3195 | ||
3196 | if (vector_types[dim-1] != NULL_TREE) | |
3197 | return vector_types[dim-1]; | |
3198 | ||
3199 | if (vec_type == NULL_TREE) | |
3200 | { | |
3201 | chain = 0; | |
3202 | vect_struct_type = make_node (RECORD_TYPE); | |
3203 | tmp = gfc_add_field_to_struct_1 (vect_struct_type, | |
3204 | get_identifier ("vector"), | |
3205 | pvoid_type_node, &chain); | |
3206 | TREE_NO_WARNING (tmp) = 1; | |
3207 | tmp = gfc_add_field_to_struct_1 (vect_struct_type, | |
3208 | get_identifier ("kind"), | |
3209 | integer_type_node, &chain); | |
3210 | TREE_NO_WARNING (tmp) = 1; | |
3211 | gfc_finish_type (vect_struct_type); | |
3212 | ||
3213 | chain = 0; | |
3214 | triplet_struct_type = make_node (RECORD_TYPE); | |
3215 | tmp = gfc_add_field_to_struct_1 (triplet_struct_type, | |
3216 | get_identifier ("lower_bound"), | |
3217 | gfc_array_index_type, &chain); | |
3218 | TREE_NO_WARNING (tmp) = 1; | |
3219 | tmp = gfc_add_field_to_struct_1 (triplet_struct_type, | |
3220 | get_identifier ("upper_bound"), | |
3221 | gfc_array_index_type, &chain); | |
3222 | TREE_NO_WARNING (tmp) = 1; | |
3223 | tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"), | |
3224 | gfc_array_index_type, &chain); | |
3225 | TREE_NO_WARNING (tmp) = 1; | |
3226 | gfc_finish_type (triplet_struct_type); | |
3227 | ||
3228 | chain = 0; | |
3229 | union_type = make_node (UNION_TYPE); | |
3230 | tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"), | |
3231 | vect_struct_type, &chain); | |
3232 | TREE_NO_WARNING (tmp) = 1; | |
3233 | tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"), | |
3234 | triplet_struct_type, &chain); | |
3235 | TREE_NO_WARNING (tmp) = 1; | |
3236 | gfc_finish_type (union_type); | |
3237 | ||
3238 | chain = 0; | |
3239 | vec_type = make_node (RECORD_TYPE); | |
3240 | tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"), | |
3241 | size_type_node, &chain); | |
3242 | TREE_NO_WARNING (tmp) = 1; | |
3243 | tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"), | |
3244 | union_type, &chain); | |
3245 | TREE_NO_WARNING (tmp) = 1; | |
3246 | gfc_finish_type (vec_type); | |
3247 | TYPE_NAME (vec_type) = get_identifier ("caf_vector_t"); | |
3248 | } | |
3249 | ||
3250 | tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, | |
3251 | gfc_rank_cst[dim-1]); | |
3252 | vector_types[dim-1] = build_array_type (vec_type, tmp); | |
3253 | return vector_types[dim-1]; | |
3254 | } | |
3255 | ||
6de9cd9a | 3256 | #include "gt-fortran-trans-types.h" |