Line data Source code
1 : /* Backend support for Fortran 95 basic types and derived types.
2 : Copyright (C) 2002-2026 Free Software Foundation, Inc.
3 : Contributed by Paul Brook <paul@nowt.org>
4 : and Steven Bosscher <s.bosscher@student.tudelft.nl>
5 :
6 : This file is part of GCC.
7 :
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
10 : Software Foundation; either version 3, or (at your option) any later
11 : version.
12 :
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.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : /* trans-types.cc -- gfortran backend types */
23 :
24 : #include "config.h"
25 : #include "system.h"
26 : #include "coretypes.h"
27 : #include "target.h"
28 : #include "tree.h"
29 : #include "gfortran.h"
30 : #include "trans.h"
31 : #include "stringpool.h"
32 : #include "fold-const.h"
33 : #include "stor-layout.h"
34 : #include "langhooks.h" /* For iso-c-bindings.def. */
35 : #include "toplev.h" /* For rest_of_decl_compilation. */
36 : #include "trans-types.h"
37 : #include "trans-const.h"
38 : #include "trans-array.h"
39 : #include "trans-descriptor.h"
40 : #include "dwarf2out.h" /* For struct array_descr_info. */
41 : #include "attribs.h"
42 : #include "alias.h"
43 :
44 :
45 : #if (GFC_MAX_DIMENSIONS < 10)
46 : #define GFC_RANK_DIGITS 1
47 : #define GFC_RANK_PRINTF_FORMAT "%01d"
48 : #elif (GFC_MAX_DIMENSIONS < 100)
49 : #define GFC_RANK_DIGITS 2
50 : #define GFC_RANK_PRINTF_FORMAT "%02d"
51 : #else
52 : #error If you really need >99 dimensions, continue the sequence above...
53 : #endif
54 :
55 : /* array of structs so we don't have to worry about xmalloc or free */
56 : CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
57 :
58 : tree gfc_array_dim_rank_type;
59 : tree gfc_array_index_type;
60 : tree gfc_array_range_type;
61 : tree gfc_character1_type_node;
62 : tree pvoid_type_node;
63 : tree prvoid_type_node;
64 : tree ppvoid_type_node;
65 : tree pchar_type_node;
66 : static tree pfunc_type_node;
67 :
68 : tree logical_type_node;
69 : tree logical_true_node;
70 : tree logical_false_node;
71 : tree gfc_charlen_type_node;
72 :
73 : tree gfc_float128_type_node = NULL_TREE;
74 : tree gfc_complex_float128_type_node = NULL_TREE;
75 :
76 : bool gfc_real16_is_float128 = false;
77 : bool gfc_real16_use_iec_60559 = false;
78 :
79 : static GTY(()) tree gfc_desc_dim_type;
80 : static GTY(()) tree gfc_max_array_element_size;
81 : static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
82 : static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
83 : static GTY(()) tree gfc_cfi_descriptor_base[2 * (CFI_MAX_RANK + 2)];
84 :
85 : /* Arrays for all integral and real kinds. We'll fill this in at runtime
86 : after the target has a chance to process command-line options. */
87 :
88 : #define MAX_INT_KINDS 5
89 : gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
90 : gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
91 : gfc_unsigned_info gfc_unsigned_kinds[MAX_INT_KINDS + 1];
92 : static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
93 : static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
94 : static GTY(()) tree gfc_unsigned_types[MAX_INT_KINDS + 1];
95 :
96 : #define MAX_REAL_KINDS 5
97 : gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
98 : static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
99 : static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
100 :
101 : #define MAX_CHARACTER_KINDS 2
102 : gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
103 : static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
104 : static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
105 :
106 : static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
107 :
108 : /* The integer kind to use for array indices. This will be set to the
109 : proper value based on target information from the backend. */
110 :
111 : int gfc_index_integer_kind;
112 :
113 : /* The default kinds of the various types. */
114 :
115 : int gfc_default_integer_kind;
116 : int gfc_default_unsigned_kind;
117 : int gfc_max_integer_kind;
118 : int gfc_default_real_kind;
119 : int gfc_default_double_kind;
120 : int gfc_default_character_kind;
121 : int gfc_default_logical_kind;
122 : int gfc_default_complex_kind;
123 : int gfc_c_int_kind;
124 : int gfc_c_uint_kind;
125 : int gfc_c_intptr_kind;
126 : int gfc_atomic_int_kind;
127 : int gfc_atomic_logical_kind;
128 :
129 : /* The kind size used for record offsets. If the target system supports
130 : kind=8, this will be set to 8, otherwise it is set to 4. */
131 : int gfc_intio_kind;
132 :
133 : /* The integer kind used to store character lengths. */
134 : int gfc_charlen_int_kind;
135 :
136 : /* Kind of internal integer for storing object sizes. */
137 : int gfc_size_kind;
138 :
139 : /* The size of the numeric storage unit and character storage unit. */
140 : int gfc_numeric_storage_size;
141 : int gfc_character_storage_size;
142 :
143 : static tree dtype_type_node = NULL_TREE;
144 :
145 :
146 : /* Build the dtype_type_node if necessary. */
147 440636 : tree get_dtype_type_node (void)
148 : {
149 440636 : tree field;
150 440636 : tree dtype_node;
151 440636 : tree *dtype_chain = NULL;
152 :
153 440636 : if (dtype_type_node == NULL_TREE)
154 : {
155 32079 : dtype_node = make_node (RECORD_TYPE);
156 32079 : TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
157 32079 : TYPE_NAMELESS (dtype_node) = 1;
158 32079 : field = gfc_add_field_to_struct_1 (dtype_node,
159 : get_identifier ("elem_len"),
160 : size_type_node, &dtype_chain);
161 32079 : suppress_warning (field);
162 32079 : field = gfc_add_field_to_struct_1 (dtype_node,
163 : get_identifier ("version"),
164 : integer_type_node, &dtype_chain);
165 32079 : suppress_warning (field);
166 32079 : field = gfc_add_field_to_struct_1 (dtype_node,
167 : get_identifier ("rank"),
168 : gfc_array_dim_rank_type, &dtype_chain);
169 32079 : suppress_warning (field);
170 32079 : field = gfc_add_field_to_struct_1 (dtype_node,
171 : get_identifier ("type"),
172 : signed_char_type_node, &dtype_chain);
173 32079 : suppress_warning (field);
174 32079 : field = gfc_add_field_to_struct_1 (dtype_node,
175 : get_identifier ("attribute"),
176 : short_integer_type_node, &dtype_chain);
177 32079 : suppress_warning (field);
178 32079 : gfc_finish_type (dtype_node);
179 32079 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
180 32079 : dtype_type_node = dtype_node;
181 : }
182 440636 : return dtype_type_node;
183 : }
184 :
185 : static int
186 256768 : get_real_kind_from_node (tree type)
187 : {
188 256768 : int i;
189 :
190 641920 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
191 641920 : if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
192 : {
193 : /* On Power, we have three 128-bit scalar floating-point modes
194 : and all of their types have 128 bit type precision, so we
195 : should check underlying real format details further. */
196 : #if defined(HAVE_TFmode) && defined(HAVE_IFmode) && defined(HAVE_KFmode)
197 : if (gfc_real_kinds[i].kind == 16)
198 : {
199 : machine_mode mode = TYPE_MODE (type);
200 : const struct real_format *fmt = REAL_MODE_FORMAT (mode);
201 : if (fmt->p != gfc_real_kinds[i].digits)
202 : continue;
203 : }
204 : #endif
205 : return gfc_real_kinds[i].kind;
206 : }
207 :
208 : return -4;
209 : }
210 :
211 : static int
212 834496 : get_int_kind_from_node (tree type)
213 : {
214 834496 : int i;
215 :
216 834496 : if (!type)
217 : return -2;
218 :
219 2691156 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
220 2691156 : if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
221 : return gfc_integer_kinds[i].kind;
222 :
223 : return -1;
224 : }
225 :
226 : static int
227 513536 : get_int_kind_from_name (const char *name)
228 : {
229 513536 : return get_int_kind_from_node (get_typenode_from_name (name));
230 : }
231 :
232 : static int
233 545632 : get_unsigned_kind_from_node (tree type)
234 : {
235 545632 : int i;
236 :
237 545632 : if (!type)
238 : return -2;
239 :
240 553227 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
241 11760 : if (gfc_unsigned_kinds[i].bit_size == TYPE_PRECISION (type))
242 : return gfc_unsigned_kinds[i].kind;
243 :
244 : return -1;
245 : }
246 :
247 : static int
248 417248 : get_uint_kind_from_name (const char *name)
249 : {
250 417248 : return get_unsigned_kind_from_node (get_typenode_from_name (name));
251 : }
252 :
253 : /* Get the kind number corresponding to an integer of given size,
254 : following the required return values for ISO_FORTRAN_ENV INT* constants:
255 : -2 is returned if we support a kind of larger size, -1 otherwise. */
256 : int
257 4912 : gfc_get_int_kind_from_width_isofortranenv (int size)
258 : {
259 4912 : int i;
260 :
261 : /* Look for a kind with matching storage size. */
262 12280 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
263 12280 : if (gfc_integer_kinds[i].bit_size == size)
264 : return gfc_integer_kinds[i].kind;
265 :
266 : /* Look for a kind with larger storage size. */
267 0 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
268 0 : if (gfc_integer_kinds[i].bit_size > size)
269 : return -2;
270 :
271 : return -1;
272 : }
273 :
274 : /* Same, but for unsigned. */
275 :
276 : int
277 2456 : gfc_get_uint_kind_from_width_isofortranenv (int size)
278 : {
279 2456 : int i;
280 :
281 : /* Look for a kind with matching storage size. */
282 2606 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
283 250 : if (gfc_unsigned_kinds[i].bit_size == size)
284 : return gfc_unsigned_kinds[i].kind;
285 :
286 : /* Look for a kind with larger storage size. */
287 2356 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
288 0 : if (gfc_unsigned_kinds[i].bit_size > size)
289 : return -2;
290 :
291 : return -1;
292 : }
293 :
294 :
295 : /* Get the kind number corresponding to a real of a given storage size.
296 : If two real's have the same storage size, then choose the real with
297 : the largest precision. If a kind type is unavailable and a real
298 : exists with wider storage, then return -2; otherwise, return -1. */
299 :
300 : int
301 2456 : gfc_get_real_kind_from_width_isofortranenv (int size)
302 : {
303 2456 : int digits, i, kind;
304 :
305 2456 : size /= 8;
306 :
307 2456 : kind = -1;
308 2456 : digits = 0;
309 :
310 : /* Look for a kind with matching storage size. */
311 12280 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
312 9824 : if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
313 : {
314 2452 : if (gfc_real_kinds[i].digits > digits)
315 : {
316 2452 : digits = gfc_real_kinds[i].digits;
317 2452 : kind = gfc_real_kinds[i].kind;
318 : }
319 : }
320 :
321 2456 : if (kind != -1)
322 : return kind;
323 :
324 : /* Look for a kind with larger storage size. */
325 3070 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
326 2456 : if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
327 2456 : kind = -2;
328 :
329 : return kind;
330 : }
331 :
332 :
333 :
334 : static int
335 160480 : get_int_kind_from_width (int size)
336 : {
337 160480 : int i;
338 :
339 481440 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
340 480622 : if (gfc_integer_kinds[i].bit_size == size)
341 : return gfc_integer_kinds[i].kind;
342 :
343 : return -2;
344 : }
345 :
346 : static int
347 32096 : get_int_kind_from_minimal_width (int size)
348 : {
349 32096 : int i;
350 :
351 160480 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
352 160071 : if (gfc_integer_kinds[i].bit_size >= size)
353 : return gfc_integer_kinds[i].kind;
354 :
355 : return -2;
356 : }
357 :
358 : static int
359 96288 : get_uint_kind_from_width (int size)
360 : {
361 96288 : int i;
362 :
363 99228 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
364 3675 : if (gfc_integer_kinds[i].bit_size == size)
365 735 : return gfc_integer_kinds[i].kind;
366 :
367 : return -2;
368 : }
369 :
370 :
371 : /* Generate the CInteropKind_t objects for the C interoperable
372 : kinds. */
373 :
374 : void
375 32096 : gfc_init_c_interop_kinds (void)
376 : {
377 32096 : int i;
378 :
379 : /* init all pointers in the list to NULL */
380 2439296 : for (i = 0; i < ISOCBINDING_NUMBER; i++)
381 : {
382 : /* Initialize the name and value fields. */
383 2407200 : c_interop_kinds_table[i].name[0] = '\0';
384 2407200 : c_interop_kinds_table[i].value = -100;
385 2407200 : c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
386 : }
387 :
388 : #define NAMED_INTCST(a,b,c,d) \
389 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
390 : c_interop_kinds_table[a].f90_type = BT_INTEGER; \
391 : c_interop_kinds_table[a].value = c;
392 : #define NAMED_UINTCST(a,b,c,d) \
393 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
394 : c_interop_kinds_table[a].f90_type = BT_UNSIGNED; \
395 : c_interop_kinds_table[a].value = c;
396 : #define NAMED_REALCST(a,b,c,d) \
397 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
398 : c_interop_kinds_table[a].f90_type = BT_REAL; \
399 : c_interop_kinds_table[a].value = c;
400 : #define NAMED_CMPXCST(a,b,c,d) \
401 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
402 : c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
403 : c_interop_kinds_table[a].value = c;
404 : #define NAMED_LOGCST(a,b,c) \
405 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
406 : c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
407 : c_interop_kinds_table[a].value = c;
408 : #define NAMED_CHARKNDCST(a,b,c) \
409 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
410 : c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
411 : c_interop_kinds_table[a].value = c;
412 : #define NAMED_CHARCST(a,b,c) \
413 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
414 : c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
415 : c_interop_kinds_table[a].value = c;
416 : #define DERIVED_TYPE(a,b,c) \
417 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
418 : c_interop_kinds_table[a].f90_type = BT_DERIVED; \
419 : c_interop_kinds_table[a].value = c;
420 : #define NAMED_FUNCTION(a,b,c,d) \
421 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
422 : c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
423 : c_interop_kinds_table[a].value = c;
424 : #define NAMED_SUBROUTINE(a,b,c,d) \
425 : strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
426 : c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
427 : c_interop_kinds_table[a].value = c;
428 : #include "iso-c-binding.def"
429 32096 : }
430 :
431 :
432 : /* Query the target to determine which machine modes are available for
433 : computation. Choose KIND numbers for them. */
434 :
435 : void
436 32096 : gfc_init_kinds (void)
437 : {
438 32096 : opt_scalar_int_mode int_mode_iter;
439 32096 : opt_scalar_float_mode float_mode_iter;
440 32096 : int i_index, r_index, kind;
441 32096 : bool saw_i4 = false, saw_i8 = false;
442 32096 : bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
443 32096 : scalar_mode r16_mode = QImode;
444 32096 : scalar_mode composite_mode = QImode;
445 :
446 32096 : i_index = 0;
447 256768 : FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
448 : {
449 224672 : scalar_int_mode mode = int_mode_iter.require ();
450 224672 : int kind, bitsize;
451 :
452 224672 : if (!targetm.scalar_mode_supported_p (mode))
453 224672 : continue;
454 :
455 : /* The middle end doesn't support constants larger than 2*HWI.
456 : Perhaps the target hook shouldn't have accepted these either,
457 : but just to be safe... */
458 160071 : bitsize = GET_MODE_BITSIZE (mode);
459 160071 : if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
460 0 : continue;
461 :
462 160071 : gcc_assert (i_index != MAX_INT_KINDS);
463 :
464 : /* Let the kind equal the bit size divided by 8. This insulates the
465 : programmer from the underlying byte size. */
466 160071 : kind = bitsize / 8;
467 :
468 160071 : if (kind == 4)
469 : saw_i4 = true;
470 127975 : if (kind == 8)
471 32096 : saw_i8 = true;
472 :
473 160071 : gfc_integer_kinds[i_index].kind = kind;
474 160071 : gfc_integer_kinds[i_index].radix = 2;
475 160071 : gfc_integer_kinds[i_index].digits = bitsize - 1;
476 160071 : gfc_integer_kinds[i_index].bit_size = bitsize;
477 :
478 160071 : if (flag_unsigned)
479 : {
480 1225 : gfc_unsigned_kinds[i_index].kind = kind;
481 1225 : gfc_unsigned_kinds[i_index].radix = 2;
482 1225 : gfc_unsigned_kinds[i_index].digits = bitsize;
483 1225 : gfc_unsigned_kinds[i_index].bit_size = bitsize;
484 : }
485 :
486 160071 : gfc_logical_kinds[i_index].kind = kind;
487 160071 : gfc_logical_kinds[i_index].bit_size = bitsize;
488 :
489 160071 : i_index += 1;
490 : }
491 :
492 : /* Set the kind used to match GFC_INT_IO in libgfortran. This is
493 : used for large file access. */
494 :
495 32096 : if (saw_i8)
496 : gfc_intio_kind = 8;
497 : else
498 0 : gfc_intio_kind = 4;
499 :
500 : /* If we do not at least have kind = 4, everything is pointless. */
501 32096 : gcc_assert(saw_i4);
502 :
503 : /* Set the maximum integer kind. Used with at least BOZ constants. */
504 32096 : gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
505 :
506 32096 : r_index = 0;
507 224672 : FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
508 : {
509 192576 : scalar_float_mode mode = float_mode_iter.require ();
510 192576 : const struct real_format *fmt = REAL_MODE_FORMAT (mode);
511 192576 : int kind;
512 :
513 192576 : if (fmt == NULL)
514 192576 : continue;
515 192576 : if (!targetm.scalar_mode_supported_p (mode))
516 0 : continue;
517 :
518 1348032 : if (MODE_COMPOSITE_P (mode)
519 0 : && (GET_MODE_PRECISION (mode) + 7) / 8 == 16)
520 0 : composite_mode = mode;
521 :
522 : /* Only let float, double, long double and TFmode go through.
523 : Runtime support for others is not provided, so they would be
524 : useless. */
525 192576 : if (!targetm.libgcc_floating_mode_supported_p (mode))
526 0 : continue;
527 192576 : if (mode != TYPE_MODE (float_type_node)
528 160480 : && (mode != TYPE_MODE (double_type_node))
529 128384 : && (mode != TYPE_MODE (long_double_type_node))
530 : #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
531 288864 : && (mode != TFmode)
532 : #endif
533 : )
534 64192 : continue;
535 :
536 : /* Let the kind equal the precision divided by 8, rounding up. Again,
537 : this insulates the programmer from the underlying byte size.
538 :
539 : Also, it effectively deals with IEEE extended formats. There, the
540 : total size of the type may equal 16, but it's got 6 bytes of padding
541 : and the increased size can get in the way of a real IEEE quad format
542 : which may also be supported by the target.
543 :
544 : We round up so as to handle IA-64 __floatreg (RFmode), which is an
545 : 82 bit type. Not to be confused with __float80 (XFmode), which is
546 : an 80 bit type also supported by IA-64. So XFmode should come out
547 : to be kind=10, and RFmode should come out to be kind=11. Egads.
548 :
549 : TODO: The kind calculation has to be modified to support all
550 : three 128-bit floating-point modes on PowerPC as IFmode, KFmode,
551 : and TFmode since the following line would all map to kind=16.
552 : However, currently only float, double, long double, and TFmode
553 : reach this code.
554 : */
555 :
556 128384 : kind = (GET_MODE_PRECISION (mode) + 7) / 8;
557 :
558 128384 : if (kind == 4)
559 : saw_r4 = true;
560 96288 : if (kind == 8)
561 : saw_r8 = true;
562 96288 : if (kind == 10)
563 : saw_r10 = true;
564 96288 : if (kind == 16)
565 : {
566 32096 : saw_r16 = true;
567 32096 : r16_mode = mode;
568 : }
569 :
570 : /* Careful we don't stumble a weird internal mode. */
571 128384 : gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
572 : /* Or have too many modes for the allocated space. */
573 96288 : gcc_assert (r_index != MAX_REAL_KINDS);
574 :
575 128384 : gfc_real_kinds[r_index].kind = kind;
576 128384 : gfc_real_kinds[r_index].abi_kind = kind;
577 128384 : gfc_real_kinds[r_index].radix = fmt->b;
578 128384 : gfc_real_kinds[r_index].digits = fmt->p;
579 128384 : gfc_real_kinds[r_index].min_exponent = fmt->emin;
580 128384 : gfc_real_kinds[r_index].max_exponent = fmt->emax;
581 128384 : if (fmt->pnan < fmt->p)
582 : /* This is an IBM extended double format (or the MIPS variant)
583 : made up of two IEEE doubles. The value of the long double is
584 : the sum of the values of the two parts. The most significant
585 : part is required to be the value of the long double rounded
586 : to the nearest double. If we use emax of 1024 then we can't
587 : represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
588 : rounding will make the most significant part overflow. */
589 0 : gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
590 128384 : gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
591 128384 : r_index += 1;
592 : }
593 :
594 : /* Detect the powerpc64le-linux case with -mabi=ieeelongdouble, where
595 : the long double type is non-MODE_COMPOSITE_P TFmode but one can use
596 : -mabi=ibmlongdouble too and get MODE_COMPOSITE_P TFmode with the same
597 : precision. For libgfortran calls pretend the IEEE 754 quad TFmode has
598 : kind 17 rather than 16 and use kind 16 for the IBM extended format
599 : TFmode. */
600 32096 : if (composite_mode != QImode && saw_r16 && !MODE_COMPOSITE_P (r16_mode))
601 : {
602 0 : for (int i = 0; i < r_index; ++i)
603 0 : if (gfc_real_kinds[i].kind == 16)
604 : {
605 0 : gfc_real_kinds[i].abi_kind = 17;
606 0 : if (flag_building_libgfortran
607 : && (TARGET_GLIBC_MAJOR < 2
608 : || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR < 32)))
609 : {
610 : if (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26)
611 : {
612 : gfc_real16_use_iec_60559 = true;
613 : gfc_real_kinds[i].use_iec_60559 = 1;
614 : }
615 : gfc_real16_is_float128 = true;
616 : gfc_real_kinds[i].c_float128 = 1;
617 : }
618 : }
619 : }
620 32096 : else if ((flag_convert & (GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM)) != 0)
621 0 : gfc_fatal_error ("%<-fconvert=r16_ieee%> or %<-fconvert=r16_ibm%> not "
622 : "supported on this architecture");
623 :
624 : /* Choose the default integer kind. We choose 4 unless the user directs us
625 : otherwise. Even if the user specified that the default integer kind is 8,
626 : the numeric storage size is not 64 bits. In this case, a warning will be
627 : issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
628 :
629 32096 : gfc_numeric_storage_size = 4 * 8;
630 :
631 32096 : if (flag_default_integer)
632 : {
633 91 : if (!saw_i8)
634 0 : gfc_fatal_error ("INTEGER(KIND=8) is not available for "
635 : "%<-fdefault-integer-8%> option");
636 :
637 91 : gfc_default_integer_kind = 8;
638 :
639 : }
640 32005 : else if (flag_integer4_kind == 8)
641 : {
642 0 : if (!saw_i8)
643 0 : gfc_fatal_error ("INTEGER(KIND=8) is not available for "
644 : "%<-finteger-4-integer-8%> option");
645 :
646 0 : gfc_default_integer_kind = 8;
647 : }
648 32005 : else if (saw_i4)
649 : {
650 32005 : gfc_default_integer_kind = 4;
651 : }
652 : else
653 : {
654 : gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
655 : gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
656 : }
657 :
658 32096 : gfc_default_unsigned_kind = gfc_default_integer_kind;
659 :
660 : /* Choose the default real kind. Again, we choose 4 when possible. */
661 32096 : if (flag_default_real_8)
662 : {
663 2 : if (!saw_r8)
664 0 : gfc_fatal_error ("REAL(KIND=8) is not available for "
665 : "%<-fdefault-real-8%> option");
666 :
667 2 : gfc_default_real_kind = 8;
668 : }
669 32094 : else if (flag_default_real_10)
670 : {
671 6 : if (!saw_r10)
672 0 : gfc_fatal_error ("REAL(KIND=10) is not available for "
673 : "%<-fdefault-real-10%> option");
674 :
675 6 : gfc_default_real_kind = 10;
676 : }
677 32088 : else if (flag_default_real_16)
678 : {
679 6 : if (!saw_r16)
680 0 : gfc_fatal_error ("REAL(KIND=16) is not available for "
681 : "%<-fdefault-real-16%> option");
682 :
683 6 : gfc_default_real_kind = 16;
684 : }
685 32082 : else if (flag_real4_kind == 8)
686 : {
687 24 : if (!saw_r8)
688 0 : gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
689 : "option");
690 :
691 24 : gfc_default_real_kind = 8;
692 : }
693 32058 : else if (flag_real4_kind == 10)
694 : {
695 24 : if (!saw_r10)
696 0 : gfc_fatal_error ("REAL(KIND=10) is not available for "
697 : "%<-freal-4-real-10%> option");
698 :
699 24 : gfc_default_real_kind = 10;
700 : }
701 32034 : else if (flag_real4_kind == 16)
702 : {
703 24 : if (!saw_r16)
704 0 : gfc_fatal_error ("REAL(KIND=16) is not available for "
705 : "%<-freal-4-real-16%> option");
706 :
707 24 : gfc_default_real_kind = 16;
708 : }
709 32010 : else if (saw_r4)
710 32010 : gfc_default_real_kind = 4;
711 : else
712 0 : gfc_default_real_kind = gfc_real_kinds[0].kind;
713 :
714 : /* Choose the default double kind. If -fdefault-real and -fdefault-double
715 : are specified, we use kind=8, if it's available. If -fdefault-real is
716 : specified without -fdefault-double, we use kind=16, if it's available.
717 : Otherwise we do not change anything. */
718 32096 : if (flag_default_double && saw_r8)
719 0 : gfc_default_double_kind = 8;
720 32096 : else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
721 : {
722 : /* Use largest available kind. */
723 14 : if (saw_r16)
724 14 : gfc_default_double_kind = 16;
725 0 : else if (saw_r10)
726 0 : gfc_default_double_kind = 10;
727 0 : else if (saw_r8)
728 0 : gfc_default_double_kind = 8;
729 : else
730 0 : gfc_default_double_kind = gfc_default_real_kind;
731 : }
732 32082 : else if (flag_real8_kind == 4)
733 : {
734 24 : if (!saw_r4)
735 0 : gfc_fatal_error ("REAL(KIND=4) is not available for "
736 : "%<-freal-8-real-4%> option");
737 :
738 24 : gfc_default_double_kind = 4;
739 : }
740 32058 : else if (flag_real8_kind == 10 )
741 : {
742 24 : if (!saw_r10)
743 0 : gfc_fatal_error ("REAL(KIND=10) is not available for "
744 : "%<-freal-8-real-10%> option");
745 :
746 24 : gfc_default_double_kind = 10;
747 : }
748 32034 : else if (flag_real8_kind == 16 )
749 : {
750 24 : if (!saw_r16)
751 0 : gfc_fatal_error ("REAL(KIND=10) is not available for "
752 : "%<-freal-8-real-16%> option");
753 :
754 24 : gfc_default_double_kind = 16;
755 : }
756 32010 : else if (saw_r4 && saw_r8)
757 32010 : gfc_default_double_kind = 8;
758 : else
759 : {
760 : /* F95 14.6.3.1: A nonpointer scalar object of type double precision
761 : real ... occupies two contiguous numeric storage units.
762 :
763 : Therefore we must be supplied a kind twice as large as we chose
764 : for single precision. There are loopholes, in that double
765 : precision must *occupy* two storage units, though it doesn't have
766 : to *use* two storage units. Which means that you can make this
767 : kind artificially wide by padding it. But at present there are
768 : no GCC targets for which a two-word type does not exist, so we
769 : just let gfc_validate_kind abort and tell us if something breaks. */
770 :
771 0 : gfc_default_double_kind
772 0 : = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
773 : }
774 :
775 : /* The default logical kind is constrained to be the same as the
776 : default integer kind. Similarly with complex and real. */
777 32096 : gfc_default_logical_kind = gfc_default_integer_kind;
778 32096 : gfc_default_complex_kind = gfc_default_real_kind;
779 :
780 : /* We only have two character kinds: ASCII and UCS-4.
781 : ASCII corresponds to a 8-bit integer type, if one is available.
782 : UCS-4 corresponds to a 32-bit integer type, if one is available. */
783 32096 : i_index = 0;
784 64192 : if ((kind = get_int_kind_from_width (8)) > 0)
785 : {
786 32096 : gfc_character_kinds[i_index].kind = kind;
787 32096 : gfc_character_kinds[i_index].bit_size = 8;
788 32096 : gfc_character_kinds[i_index].name = "ascii";
789 32096 : i_index++;
790 : }
791 64192 : if ((kind = get_int_kind_from_width (32)) > 0)
792 : {
793 32096 : gfc_character_kinds[i_index].kind = kind;
794 32096 : gfc_character_kinds[i_index].bit_size = 32;
795 32096 : gfc_character_kinds[i_index].name = "iso_10646";
796 32096 : i_index++;
797 : }
798 :
799 : /* Choose the smallest integer kind for our default character. */
800 32096 : gfc_default_character_kind = gfc_character_kinds[0].kind;
801 32096 : gfc_character_storage_size = gfc_default_character_kind * 8;
802 :
803 32505 : gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
804 :
805 32096 : if (flag_external_blas64 && gfc_index_integer_kind != gfc_integer_8_kind)
806 0 : gfc_fatal_error ("-fexternal-blas64 requires a 64-bit system");
807 :
808 : /* Pick a kind the same size as the C "int" type. */
809 32096 : gfc_c_int_kind = INT_TYPE_SIZE / 8;
810 :
811 : /* UNSIGNED has the same as INT. */
812 32096 : gfc_c_uint_kind = gfc_c_int_kind;
813 :
814 : /* Choose atomic kinds to match C's int. */
815 32096 : gfc_atomic_int_kind = gfc_c_int_kind;
816 32096 : gfc_atomic_logical_kind = gfc_c_int_kind;
817 :
818 32096 : gfc_c_intptr_kind = POINTER_SIZE / 8;
819 32096 : }
820 :
821 :
822 : /* Make sure that a valid kind is present. Returns an index into the
823 : associated kinds array, -1 if the kind is not present. */
824 :
825 : static int
826 0 : validate_integer (int kind)
827 : {
828 0 : int i;
829 :
830 82559492 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
831 82557425 : if (gfc_integer_kinds[i].kind == kind)
832 : return i;
833 :
834 : return -1;
835 : }
836 :
837 : static int
838 0 : validate_unsigned (int kind)
839 : {
840 0 : int i;
841 :
842 4501106 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
843 1636046 : if (gfc_unsigned_kinds[i].kind == kind)
844 : return i;
845 :
846 : return -1;
847 : }
848 :
849 : static int
850 0 : validate_real (int kind)
851 : {
852 0 : int i;
853 :
854 5373851 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
855 5373840 : if (gfc_real_kinds[i].kind == kind)
856 : return i;
857 :
858 : return -1;
859 : }
860 :
861 : static int
862 0 : validate_logical (int kind)
863 : {
864 0 : int i;
865 :
866 1975599 : for (i = 0; gfc_logical_kinds[i].kind; i++)
867 1975591 : if (gfc_logical_kinds[i].kind == kind)
868 : return i;
869 :
870 : return -1;
871 : }
872 :
873 : static int
874 0 : validate_character (int kind)
875 : {
876 0 : int i;
877 :
878 1411471 : for (i = 0; gfc_character_kinds[i].kind; i++)
879 1411457 : if (gfc_character_kinds[i].kind == kind)
880 : return i;
881 :
882 : return -1;
883 : }
884 :
885 : /* Validate a kind given a basic type. The return value is the same
886 : for the child functions, with -1 indicating nonexistence of the
887 : type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
888 :
889 : int
890 34694662 : gfc_validate_kind (bt type, int kind, bool may_fail)
891 : {
892 34694662 : int rc;
893 :
894 34694662 : switch (type)
895 : {
896 : case BT_REAL: /* Fall through */
897 : case BT_COMPLEX:
898 34694662 : rc = validate_real (kind);
899 : break;
900 : case BT_INTEGER:
901 34694662 : rc = validate_integer (kind);
902 : break;
903 : case BT_UNSIGNED:
904 34694662 : rc = validate_unsigned (kind);
905 : break;
906 : case BT_LOGICAL:
907 34694662 : rc = validate_logical (kind);
908 : break;
909 : case BT_CHARACTER:
910 34694662 : rc = validate_character (kind);
911 : break;
912 :
913 0 : default:
914 0 : gfc_internal_error ("gfc_validate_kind(): Got bad type");
915 : }
916 :
917 34694662 : if (rc < 0 && !may_fail)
918 0 : gfc_internal_error ("gfc_validate_kind(): Got bad kind");
919 :
920 34694662 : return rc;
921 : }
922 :
923 :
924 : /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
925 : Reuse common type nodes where possible. Recognize if the kind matches up
926 : with a C type. This will be used later in determining which routines may
927 : be scarfed from libm. */
928 :
929 : static tree
930 160071 : gfc_build_int_type (gfc_integer_info *info)
931 : {
932 160071 : int mode_precision = info->bit_size;
933 :
934 160071 : if (mode_precision == CHAR_TYPE_SIZE)
935 32096 : info->c_char = 1;
936 160071 : if (mode_precision == SHORT_TYPE_SIZE)
937 32096 : info->c_short = 1;
938 160071 : if (mode_precision == INT_TYPE_SIZE)
939 32096 : info->c_int = 1;
940 161707 : if (mode_precision == LONG_TYPE_SIZE)
941 32096 : info->c_long = 1;
942 160071 : if (mode_precision == LONG_LONG_TYPE_SIZE)
943 32096 : info->c_long_long = 1;
944 :
945 160071 : if (TYPE_PRECISION (intQI_type_node) == mode_precision)
946 : return intQI_type_node;
947 127975 : if (TYPE_PRECISION (intHI_type_node) == mode_precision)
948 : return intHI_type_node;
949 95879 : if (TYPE_PRECISION (intSI_type_node) == mode_precision)
950 : return intSI_type_node;
951 63783 : if (TYPE_PRECISION (intDI_type_node) == mode_precision)
952 : return intDI_type_node;
953 31687 : if (TYPE_PRECISION (intTI_type_node) == mode_precision)
954 : return intTI_type_node;
955 :
956 0 : return make_signed_type (mode_precision);
957 : }
958 :
959 : tree
960 65673 : gfc_build_uint_type (int size)
961 : {
962 65673 : if (size == CHAR_TYPE_SIZE)
963 32222 : return unsigned_char_type_node;
964 33451 : if (size == SHORT_TYPE_SIZE)
965 371 : return short_unsigned_type_node;
966 33080 : if (size == INT_TYPE_SIZE)
967 32330 : return unsigned_type_node;
968 750 : if (size == LONG_TYPE_SIZE)
969 371 : return long_unsigned_type_node;
970 379 : if (size == LONG_LONG_TYPE_SIZE)
971 0 : return long_long_unsigned_type_node;
972 :
973 379 : return make_unsigned_type (size);
974 : }
975 :
976 : static tree
977 735 : gfc_build_unsigned_type (gfc_unsigned_info *info)
978 : {
979 735 : int mode_precision = info->bit_size;
980 :
981 735 : if (mode_precision == CHAR_TYPE_SIZE)
982 0 : info->c_unsigned_char = 1;
983 735 : if (mode_precision == SHORT_TYPE_SIZE)
984 245 : info->c_unsigned_short = 1;
985 735 : if (mode_precision == INT_TYPE_SIZE)
986 0 : info->c_unsigned_int = 1;
987 735 : if (mode_precision == LONG_TYPE_SIZE)
988 245 : info->c_unsigned_long = 1;
989 735 : if (mode_precision == LONG_LONG_TYPE_SIZE)
990 245 : info->c_unsigned_long_long = 1;
991 :
992 735 : return gfc_build_uint_type (mode_precision);
993 : }
994 :
995 : static tree
996 128384 : gfc_build_real_type (gfc_real_info *info)
997 : {
998 128384 : int mode_precision = info->mode_precision;
999 128384 : tree new_type;
1000 :
1001 128384 : if (mode_precision == TYPE_PRECISION (float_type_node))
1002 32096 : info->c_float = 1;
1003 128384 : if (mode_precision == TYPE_PRECISION (double_type_node))
1004 32096 : info->c_double = 1;
1005 128384 : if (mode_precision == TYPE_PRECISION (long_double_type_node)
1006 128384 : && !info->c_float128)
1007 32096 : info->c_long_double = 1;
1008 128384 : if (mode_precision != TYPE_PRECISION (long_double_type_node)
1009 128384 : && mode_precision == 128)
1010 : {
1011 : /* TODO: see PR101835. */
1012 32096 : info->c_float128 = 1;
1013 32096 : gfc_real16_is_float128 = true;
1014 32096 : if (TARGET_GLIBC_MAJOR > 2
1015 : || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26))
1016 : {
1017 32096 : info->use_iec_60559 = 1;
1018 32096 : gfc_real16_use_iec_60559 = true;
1019 : }
1020 : }
1021 :
1022 128384 : if (TYPE_PRECISION (float_type_node) == mode_precision)
1023 : return float_type_node;
1024 96288 : if (TYPE_PRECISION (double_type_node) == mode_precision)
1025 : return double_type_node;
1026 64192 : if (TYPE_PRECISION (long_double_type_node) == mode_precision)
1027 : return long_double_type_node;
1028 :
1029 32096 : new_type = make_node (REAL_TYPE);
1030 32096 : TYPE_PRECISION (new_type) = mode_precision;
1031 32096 : layout_type (new_type);
1032 32096 : return new_type;
1033 : }
1034 :
1035 : static tree
1036 128384 : gfc_build_complex_type (tree scalar_type)
1037 : {
1038 128384 : tree new_type;
1039 :
1040 128384 : if (scalar_type == NULL)
1041 : return NULL;
1042 128384 : if (scalar_type == float_type_node)
1043 32096 : return complex_float_type_node;
1044 96288 : if (scalar_type == double_type_node)
1045 32096 : return complex_double_type_node;
1046 64192 : if (scalar_type == long_double_type_node)
1047 32096 : return complex_long_double_type_node;
1048 :
1049 32096 : new_type = make_node (COMPLEX_TYPE);
1050 32096 : TREE_TYPE (new_type) = scalar_type;
1051 32096 : layout_type (new_type);
1052 32096 : return new_type;
1053 : }
1054 :
1055 : static tree
1056 160071 : gfc_build_logical_type (gfc_logical_info *info)
1057 : {
1058 160071 : int bit_size = info->bit_size;
1059 160071 : tree new_type;
1060 :
1061 160071 : if (bit_size == BOOL_TYPE_SIZE)
1062 : {
1063 32096 : info->c_bool = 1;
1064 32096 : return boolean_type_node;
1065 : }
1066 :
1067 127975 : new_type = make_unsigned_type (bit_size);
1068 127975 : TREE_SET_CODE (new_type, BOOLEAN_TYPE);
1069 127975 : TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
1070 127975 : TYPE_PRECISION (new_type) = 1;
1071 :
1072 127975 : return new_type;
1073 : }
1074 :
1075 :
1076 : /* Create the backend type nodes. We map them to their
1077 : equivalent C type, at least for now. We also give
1078 : names to the types here, and we push them in the
1079 : global binding level context.*/
1080 :
1081 : void
1082 32096 : gfc_init_types (void)
1083 : {
1084 32096 : char name_buf[26];
1085 32096 : int index;
1086 32096 : tree type;
1087 32096 : unsigned n;
1088 :
1089 : /* Create and name the types. */
1090 : #define PUSH_TYPE(name, node) \
1091 : pushdecl (build_decl (input_location, \
1092 : TYPE_DECL, get_identifier (name), node))
1093 :
1094 192167 : for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
1095 : {
1096 160071 : type = gfc_build_int_type (&gfc_integer_kinds[index]);
1097 : /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
1098 160071 : if (TYPE_STRING_FLAG (type))
1099 32096 : type = make_signed_type (gfc_integer_kinds[index].bit_size);
1100 160071 : gfc_integer_types[index] = type;
1101 160071 : snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
1102 : gfc_integer_kinds[index].kind);
1103 160071 : PUSH_TYPE (name_buf, type);
1104 : }
1105 :
1106 192167 : for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
1107 : {
1108 160071 : type = gfc_build_logical_type (&gfc_logical_kinds[index]);
1109 160071 : gfc_logical_types[index] = type;
1110 160071 : snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
1111 : gfc_logical_kinds[index].kind);
1112 160071 : PUSH_TYPE (name_buf, type);
1113 : }
1114 :
1115 160480 : for (index = 0; gfc_real_kinds[index].kind != 0; index++)
1116 : {
1117 128384 : type = gfc_build_real_type (&gfc_real_kinds[index]);
1118 128384 : gfc_real_types[index] = type;
1119 128384 : snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
1120 : gfc_real_kinds[index].kind);
1121 128384 : PUSH_TYPE (name_buf, type);
1122 :
1123 128384 : if (gfc_real_kinds[index].c_float128)
1124 32096 : gfc_float128_type_node = type;
1125 :
1126 128384 : type = gfc_build_complex_type (type);
1127 128384 : gfc_complex_types[index] = type;
1128 128384 : snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
1129 : gfc_real_kinds[index].kind);
1130 128384 : PUSH_TYPE (name_buf, type);
1131 :
1132 128384 : if (gfc_real_kinds[index].c_float128)
1133 32096 : gfc_complex_float128_type_node = type;
1134 : }
1135 :
1136 96288 : for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
1137 : {
1138 64192 : type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
1139 64192 : type = build_qualified_type (type, TYPE_UNQUALIFIED);
1140 64192 : TYPE_STRING_FLAG (type) = 1;
1141 64192 : snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
1142 : gfc_character_kinds[index].kind);
1143 64192 : PUSH_TYPE (name_buf, type);
1144 64192 : gfc_character_types[index] = type;
1145 64192 : gfc_pcharacter_types[index] = build_pointer_type (type);
1146 : }
1147 32096 : gfc_character1_type_node = gfc_character_types[0];
1148 :
1149 32096 : if (flag_unsigned)
1150 : {
1151 1470 : for (index = 0; gfc_unsigned_kinds[index].kind != 0;++index)
1152 : {
1153 2940 : int index_char = -1;
1154 2940 : for (int i=0; gfc_character_kinds[i].kind != 0; i++)
1155 : {
1156 2205 : if (gfc_character_kinds[i].bit_size
1157 2205 : == gfc_unsigned_kinds[index].bit_size)
1158 : {
1159 : index_char = i;
1160 : break;
1161 : }
1162 : }
1163 1225 : if (index_char > -1)
1164 : {
1165 490 : type = gfc_character_types[index_char];
1166 490 : if (TYPE_STRING_FLAG (type))
1167 : {
1168 490 : type = build_distinct_type_copy (type);
1169 980 : TYPE_CANONICAL (type)
1170 490 : = TYPE_CANONICAL (gfc_character_types[index_char]);
1171 : }
1172 : else
1173 0 : type = build_variant_type_copy (type);
1174 490 : TYPE_NAME (type) = NULL_TREE;
1175 490 : TYPE_STRING_FLAG (type) = 0;
1176 : }
1177 : else
1178 735 : type = gfc_build_unsigned_type (&gfc_unsigned_kinds[index]);
1179 1225 : gfc_unsigned_types[index] = type;
1180 1225 : snprintf (name_buf, sizeof(name_buf), "unsigned(kind=%d)",
1181 : gfc_integer_kinds[index].kind);
1182 1225 : PUSH_TYPE (name_buf, type);
1183 : }
1184 : }
1185 :
1186 32096 : PUSH_TYPE ("byte", unsigned_char_type_node);
1187 32096 : PUSH_TYPE ("void", void_type_node);
1188 :
1189 : /* DBX debugging output gets upset if these aren't set. */
1190 32096 : if (!TYPE_NAME (integer_type_node))
1191 0 : PUSH_TYPE ("c_integer", integer_type_node);
1192 32096 : if (!TYPE_NAME (char_type_node))
1193 32096 : PUSH_TYPE ("c_char", char_type_node);
1194 :
1195 : #undef PUSH_TYPE
1196 :
1197 32096 : pvoid_type_node = build_pointer_type (void_type_node);
1198 32096 : prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
1199 32096 : ppvoid_type_node = build_pointer_type (pvoid_type_node);
1200 32096 : pchar_type_node = build_pointer_type (gfc_character1_type_node);
1201 32096 : pfunc_type_node
1202 32096 : = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
1203 :
1204 32096 : gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
1205 : /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1206 : since this function is called before gfc_init_constants. */
1207 32096 : gfc_array_range_type
1208 32096 : = build_range_type (gfc_array_index_type,
1209 : build_int_cst (gfc_array_index_type, 0),
1210 : NULL_TREE);
1211 :
1212 : /* The maximum array element size that can be handled is determined
1213 : by the number of bits available to store this field in the array
1214 : descriptor. */
1215 :
1216 32096 : n = TYPE_PRECISION (size_type_node);
1217 32096 : gfc_max_array_element_size
1218 32096 : = wide_int_to_tree (size_type_node,
1219 32096 : wi::mask (n, UNSIGNED,
1220 32096 : TYPE_PRECISION (size_type_node)));
1221 :
1222 32096 : logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1223 32096 : logical_true_node = build_int_cst (logical_type_node, 1);
1224 32096 : logical_false_node = build_int_cst (logical_type_node, 0);
1225 :
1226 : /* Character lengths are of type size_t, except signed. */
1227 32096 : gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1228 32096 : gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1229 :
1230 32096 : gfc_array_dim_rank_type
1231 32096 : = build_range_type (signed_char_type_node,
1232 : build_zero_cst (signed_char_type_node),
1233 : build_int_cst (signed_char_type_node,
1234 : GFC_MAX_DIMENSIONS));
1235 :
1236 : /* Fortran kind number of size_type_node (size_t). This is used for
1237 : the _size member in vtables. */
1238 32096 : gfc_size_kind = get_int_kind_from_node (size_type_node);
1239 32096 : }
1240 :
1241 : /* Get the type node for the given type and kind. */
1242 :
1243 : tree
1244 5846658 : gfc_get_int_type (int kind)
1245 : {
1246 5846658 : int index = gfc_validate_kind (BT_INTEGER, kind, true);
1247 5846658 : return index < 0 ? 0 : gfc_integer_types[index];
1248 : }
1249 :
1250 : tree
1251 3047554 : gfc_get_unsigned_type (int kind)
1252 : {
1253 3047554 : int index = gfc_validate_kind (BT_UNSIGNED, kind, true);
1254 3047554 : return index < 0 ? 0 : gfc_unsigned_types[index];
1255 : }
1256 :
1257 : tree
1258 781818 : gfc_get_real_type (int kind)
1259 : {
1260 781818 : int index = gfc_validate_kind (BT_REAL, kind, true);
1261 781818 : return index < 0 ? 0 : gfc_real_types[index];
1262 : }
1263 :
1264 : tree
1265 475515 : gfc_get_complex_type (int kind)
1266 : {
1267 475515 : int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1268 475515 : return index < 0 ? 0 : gfc_complex_types[index];
1269 : }
1270 :
1271 : tree
1272 592443 : gfc_get_logical_type (int kind)
1273 : {
1274 592443 : int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1275 592443 : return index < 0 ? 0 : gfc_logical_types[index];
1276 : }
1277 :
1278 : tree
1279 435356 : gfc_get_char_type (int kind)
1280 : {
1281 435356 : int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1282 435356 : return index < 0 ? 0 : gfc_character_types[index];
1283 : }
1284 :
1285 : tree
1286 166376 : gfc_get_pchar_type (int kind)
1287 : {
1288 166376 : int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1289 166376 : return index < 0 ? 0 : gfc_pcharacter_types[index];
1290 : }
1291 :
1292 :
1293 : /* Create a character type with the given kind and length. */
1294 :
1295 : tree
1296 93045 : gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1297 : {
1298 93045 : tree bounds, type;
1299 :
1300 93045 : bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1301 93045 : type = build_array_type (eltype, bounds);
1302 93045 : TYPE_STRING_FLAG (type) = 1;
1303 :
1304 93045 : return type;
1305 : }
1306 :
1307 : tree
1308 85202 : gfc_get_character_type_len (int kind, tree len)
1309 : {
1310 85202 : gfc_validate_kind (BT_CHARACTER, kind, false);
1311 85202 : return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1312 : }
1313 :
1314 :
1315 : /* Get a type node for a character kind. */
1316 :
1317 : tree
1318 74419 : gfc_get_character_type (int kind, gfc_charlen * cl)
1319 : {
1320 74419 : tree len;
1321 :
1322 74419 : len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1323 73242 : if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1324 0 : len = build_fold_indirect_ref (len);
1325 :
1326 74419 : return gfc_get_character_type_len (kind, len);
1327 : }
1328 :
1329 : /* Convert a basic type. This will be an array for character types. */
1330 :
1331 : tree
1332 1288968 : gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1333 : {
1334 1288968 : tree basetype;
1335 :
1336 1288968 : switch (spec->type)
1337 : {
1338 0 : case BT_UNKNOWN:
1339 0 : gcc_unreachable ();
1340 :
1341 471863 : case BT_INTEGER:
1342 : /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1343 : has been resolved. This is done so we can convert C_PTR and
1344 : C_FUNPTR to simple variables that get translated to (void *). */
1345 471863 : if (spec->f90_type == BT_VOID)
1346 : {
1347 343 : if (spec->u.derived
1348 343 : && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1349 258 : basetype = ptr_type_node;
1350 : else
1351 85 : basetype = pfunc_type_node;
1352 : }
1353 : else
1354 471520 : basetype = gfc_get_int_type (spec->kind);
1355 : break;
1356 :
1357 2778 : case BT_UNSIGNED:
1358 2778 : basetype = gfc_get_unsigned_type (spec->kind);
1359 2778 : break;
1360 :
1361 140279 : case BT_REAL:
1362 140279 : basetype = gfc_get_real_type (spec->kind);
1363 140279 : break;
1364 :
1365 25861 : case BT_COMPLEX:
1366 25861 : basetype = gfc_get_complex_type (spec->kind);
1367 25861 : break;
1368 :
1369 423524 : case BT_LOGICAL:
1370 423524 : basetype = gfc_get_logical_type (spec->kind);
1371 423524 : break;
1372 :
1373 61985 : case BT_CHARACTER:
1374 61985 : basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1375 61985 : break;
1376 :
1377 12 : case BT_HOLLERITH:
1378 : /* Since this cannot be used, return a length one character. */
1379 12 : basetype = gfc_get_character_type_len (gfc_default_character_kind,
1380 : gfc_index_one_node);
1381 12 : break;
1382 :
1383 116 : case BT_UNION:
1384 116 : basetype = gfc_get_union_type (spec->u.derived);
1385 116 : break;
1386 :
1387 158826 : case BT_DERIVED:
1388 158826 : case BT_CLASS:
1389 158826 : basetype = gfc_get_derived_type (spec->u.derived, codim);
1390 :
1391 : /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1392 : type and kind to fit a (void *) and the basetype returned was a
1393 : ptr_type_node. We need to pass up this new information to the
1394 : symbol that was declared of type C_PTR or C_FUNPTR. */
1395 158826 : if (spec->u.derived->ts.f90_type == BT_VOID)
1396 : {
1397 12345 : spec->type = BT_INTEGER;
1398 12345 : spec->kind = gfc_index_integer_kind;
1399 12345 : spec->f90_type = BT_VOID;
1400 12345 : spec->is_c_interop = 1; /* Mark as escaping later. */
1401 : }
1402 : break;
1403 3686 : case BT_VOID:
1404 3686 : case BT_ASSUMED:
1405 : /* This is for the second arg to c_f_pointer and c_f_procpointer
1406 : of the iso_c_binding module, to accept any ptr type. */
1407 3686 : basetype = ptr_type_node;
1408 3686 : if (spec->f90_type == BT_VOID)
1409 : {
1410 428 : if (spec->u.derived
1411 0 : && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1412 : basetype = ptr_type_node;
1413 : else
1414 428 : basetype = pfunc_type_node;
1415 : }
1416 : break;
1417 38 : case BT_PROCEDURE:
1418 38 : basetype = pfunc_type_node;
1419 38 : break;
1420 0 : default:
1421 0 : gcc_unreachable ();
1422 : }
1423 1288968 : return basetype;
1424 : }
1425 :
1426 : /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1427 :
1428 : static tree
1429 120599 : gfc_conv_array_bound (gfc_expr * expr)
1430 : {
1431 : /* If expr is an integer constant, return that. */
1432 120599 : if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1433 15050 : return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1434 :
1435 : /* Otherwise return NULL. */
1436 : return NULL_TREE;
1437 : }
1438 :
1439 : /* Return the type of an element of the array. Note that scalar coarrays
1440 : are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1441 : (with POINTER_TYPE stripped) is returned. */
1442 :
1443 : tree
1444 338575 : gfc_get_element_type (tree type)
1445 : {
1446 338575 : tree element;
1447 :
1448 338575 : if (GFC_ARRAY_TYPE_P (type))
1449 : {
1450 127890 : if (TREE_CODE (type) == POINTER_TYPE)
1451 20241 : type = TREE_TYPE (type);
1452 127890 : if (GFC_TYPE_ARRAY_RANK (type) == 0)
1453 : {
1454 504 : gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1455 : element = type;
1456 : }
1457 : else
1458 : {
1459 127386 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1460 127386 : element = TREE_TYPE (type);
1461 : }
1462 : }
1463 : else
1464 : {
1465 210685 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1466 210685 : element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1467 :
1468 210685 : gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1469 210685 : element = TREE_TYPE (element);
1470 :
1471 : /* For arrays, which are not scalar coarrays. */
1472 210685 : if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1473 209059 : element = TREE_TYPE (element);
1474 : }
1475 :
1476 338575 : return element;
1477 : }
1478 :
1479 : /* Build an array. This function is called from gfc_sym_type().
1480 : Actually returns array descriptor type.
1481 :
1482 : Format of array descriptors is as follows:
1483 :
1484 : struct gfc_array_descriptor
1485 : {
1486 : array *data;
1487 : index offset;
1488 : struct dtype_type dtype;
1489 : struct descriptor_dimension dimension[N_DIM];
1490 : }
1491 :
1492 : struct dtype_type
1493 : {
1494 : size_t elem_len;
1495 : int version;
1496 : signed char rank;
1497 : signed char type;
1498 : signed short attribute;
1499 : }
1500 :
1501 : struct descriptor_dimension
1502 : {
1503 : index stride;
1504 : index lbound;
1505 : index ubound;
1506 : }
1507 :
1508 : Translation code should use gfc_conv_descriptor_* rather than
1509 : accessing the descriptor directly. Any changes to the array
1510 : descriptor type will require changes in gfc_conv_descriptor_* and
1511 : gfc_build_array_initializer.
1512 :
1513 : This is represented internally as a RECORD_TYPE. The index nodes
1514 : are gfc_array_index_type and the data node is a pointer to the
1515 : data. See below for the handling of character types.
1516 :
1517 : I originally used nested ARRAY_TYPE nodes to represent arrays, but
1518 : this generated poor code for assumed/deferred size arrays. These
1519 : require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1520 : of the GENERIC grammar. Also, there is no way to explicitly set
1521 : the array stride, so all data must be packed(1). I've tried to
1522 : mark all the functions which would require modification with a GCC
1523 : ARRAYS comment.
1524 :
1525 : The data component points to the first element in the array. The
1526 : offset field is the position of the origin of the array (i.e. element
1527 : (0, 0 ...)). This may be outside the bounds of the array.
1528 :
1529 : An element is accessed by
1530 : data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1531 : This gives good performance as the computation does not involve the
1532 : bounds of the array. For packed arrays, this is optimized further
1533 : by substituting the known strides.
1534 :
1535 : This system has one problem: all array bounds must be within 2^31
1536 : elements of the origin (2^63 on 64-bit machines). For example
1537 : integer, dimension (80000:90000, 80000:90000, 2) :: array
1538 : may not work properly on 32-bit machines because 80000*80000 >
1539 : 2^31, so the calculation for stride2 would overflow. This may
1540 : still work, but I haven't checked, and it relies on the overflow
1541 : doing the right thing.
1542 :
1543 : The way to fix this problem is to access elements as follows:
1544 : data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1545 : Obviously this is much slower. I will make this a compile time
1546 : option, something like -fsmall-array-offsets. Mixing code compiled
1547 : with and without this switch will work.
1548 :
1549 : (1) This can be worked around by modifying the upper bound of the
1550 : previous dimension. This requires extra fields in the descriptor
1551 : (both real_ubound and fake_ubound). */
1552 :
1553 :
1554 : /* Returns true if the array sym does not require a descriptor. */
1555 :
1556 : bool
1557 111741 : gfc_is_nodesc_array (gfc_symbol * sym)
1558 : {
1559 111741 : symbol_attribute *array_attr;
1560 111741 : gfc_array_spec *as;
1561 111741 : bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
1562 :
1563 111741 : array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1564 111741 : as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1565 :
1566 111741 : gcc_assert (array_attr->dimension || array_attr->codimension);
1567 :
1568 : /* We only want local arrays. */
1569 111741 : if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1570 104471 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1571 104471 : || array_attr->allocatable)
1572 : return 0;
1573 :
1574 : /* We want a descriptor for associate-name arrays that do not have an
1575 : explicitly known shape already. */
1576 92100 : if (sym->assoc && as->type != AS_EXPLICIT)
1577 : return 0;
1578 :
1579 : /* The dummy is stored in sym and not in the component. */
1580 90294 : if (sym->attr.dummy)
1581 40289 : return as->type != AS_ASSUMED_SHAPE
1582 40289 : && as->type != AS_ASSUMED_RANK;
1583 :
1584 50005 : if (sym->attr.result || sym->attr.function)
1585 : return 0;
1586 :
1587 40057 : gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1588 :
1589 : return 1;
1590 : }
1591 :
1592 :
1593 : /* Create an array descriptor type. */
1594 :
1595 : static tree
1596 53269 : gfc_build_array_type (tree type, gfc_array_spec * as,
1597 : enum gfc_array_kind akind, bool restricted,
1598 : bool contiguous, int codim)
1599 : {
1600 53269 : tree lbound[GFC_MAX_DIMENSIONS];
1601 53269 : tree ubound[GFC_MAX_DIMENSIONS];
1602 53269 : int n, corank;
1603 :
1604 : /* Assumed-shape arrays do not have codimension information stored in the
1605 : descriptor. */
1606 53269 : corank = MAX (as->corank, codim);
1607 53269 : if (as->type == AS_ASSUMED_SHAPE ||
1608 8003 : (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1609 53269 : corank = codim;
1610 :
1611 53269 : if (as->type == AS_ASSUMED_RANK)
1612 128048 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1613 : {
1614 120045 : lbound[n] = NULL_TREE;
1615 120045 : ubound[n] = NULL_TREE;
1616 : }
1617 :
1618 121293 : for (n = 0; n < as->rank; n++)
1619 : {
1620 : /* Create expressions for the known bounds of the array. */
1621 68024 : if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1622 16713 : lbound[n] = gfc_index_one_node;
1623 : else
1624 51311 : lbound[n] = gfc_conv_array_bound (as->lower[n]);
1625 68024 : ubound[n] = gfc_conv_array_bound (as->upper[n]);
1626 : }
1627 :
1628 54325 : for (n = as->rank; n < as->rank + corank; n++)
1629 : {
1630 1056 : if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1631 18 : lbound[n] = gfc_index_one_node;
1632 : else
1633 1038 : lbound[n] = gfc_conv_array_bound (as->lower[n]);
1634 :
1635 1056 : if (n < as->rank + corank - 1)
1636 226 : ubound[n] = gfc_conv_array_bound (as->upper[n]);
1637 : }
1638 :
1639 53269 : if (as->type == AS_ASSUMED_SHAPE)
1640 16960 : akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1641 : : GFC_ARRAY_ASSUMED_SHAPE;
1642 36309 : else if (as->type == AS_ASSUMED_RANK)
1643 : {
1644 8003 : if (akind == GFC_ARRAY_ALLOCATABLE)
1645 : akind = GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE;
1646 7622 : else if (akind == GFC_ARRAY_POINTER || akind == GFC_ARRAY_POINTER_CONT)
1647 426 : akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_POINTER_CONT
1648 : : GFC_ARRAY_ASSUMED_RANK_POINTER;
1649 : else
1650 7196 : akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1651 : : GFC_ARRAY_ASSUMED_RANK;
1652 : }
1653 98535 : return gfc_get_array_type_bounds (type, as->rank == -1
1654 : ? GFC_MAX_DIMENSIONS : as->rank,
1655 : corank, lbound, ubound, 0, akind,
1656 53269 : restricted);
1657 : }
1658 :
1659 : /* Returns the struct descriptor_dimension type. */
1660 :
1661 : static tree
1662 32415 : gfc_get_desc_dim_type (void)
1663 : {
1664 32415 : tree type;
1665 32415 : tree decl, *chain = NULL;
1666 :
1667 32415 : if (gfc_desc_dim_type)
1668 : return gfc_desc_dim_type;
1669 :
1670 : /* Build the type node. */
1671 12244 : type = make_node (RECORD_TYPE);
1672 :
1673 12244 : TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1674 12244 : TYPE_PACKED (type) = 1;
1675 :
1676 : /* Consists of the stride, lbound and ubound members. */
1677 12244 : decl = gfc_add_field_to_struct_1 (type,
1678 : get_identifier ("stride"),
1679 : gfc_array_index_type, &chain);
1680 12244 : suppress_warning (decl);
1681 :
1682 12244 : decl = gfc_add_field_to_struct_1 (type,
1683 : get_identifier ("lbound"),
1684 : gfc_array_index_type, &chain);
1685 12244 : suppress_warning (decl);
1686 :
1687 12244 : decl = gfc_add_field_to_struct_1 (type,
1688 : get_identifier ("ubound"),
1689 : gfc_array_index_type, &chain);
1690 12244 : suppress_warning (decl);
1691 :
1692 : /* Finish off the type. */
1693 12244 : gfc_finish_type (type);
1694 12244 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1695 :
1696 12244 : gfc_desc_dim_type = type;
1697 12244 : return type;
1698 : }
1699 :
1700 :
1701 : /* Return the DTYPE for an array. This describes the type and type parameters
1702 : of the array. */
1703 : /* TODO: Only call this when the value is actually used, and make all the
1704 : unknown cases abort. */
1705 :
1706 : tree
1707 145206 : gfc_get_dtype_rank_type (int rank, tree etype)
1708 : {
1709 145206 : tree ptype;
1710 145206 : tree size;
1711 145206 : int n;
1712 145206 : tree tmp;
1713 145206 : tree dtype;
1714 145206 : tree field;
1715 145206 : vec<constructor_elt, va_gc> *v = NULL;
1716 :
1717 145206 : ptype = etype;
1718 145206 : while (TREE_CODE (etype) == POINTER_TYPE
1719 177010 : || TREE_CODE (etype) == ARRAY_TYPE)
1720 : {
1721 31804 : ptype = etype;
1722 31804 : etype = TREE_TYPE (etype);
1723 : }
1724 :
1725 145206 : gcc_assert (etype);
1726 :
1727 145206 : switch (TREE_CODE (etype))
1728 : {
1729 85215 : case INTEGER_TYPE:
1730 85215 : if (TREE_CODE (ptype) == ARRAY_TYPE
1731 85215 : && TYPE_STRING_FLAG (ptype))
1732 : n = BT_CHARACTER;
1733 : else
1734 : {
1735 61792 : if (TYPE_UNSIGNED (etype))
1736 : n = BT_UNSIGNED;
1737 : else
1738 : n = BT_INTEGER;
1739 : }
1740 : break;
1741 :
1742 : case BOOLEAN_TYPE:
1743 : n = BT_LOGICAL;
1744 : break;
1745 :
1746 : case REAL_TYPE:
1747 : n = BT_REAL;
1748 : break;
1749 :
1750 : case COMPLEX_TYPE:
1751 : n = BT_COMPLEX;
1752 : break;
1753 :
1754 22118 : case RECORD_TYPE:
1755 22118 : if (GFC_CLASS_TYPE_P (etype))
1756 : n = BT_CLASS;
1757 : else
1758 : n = BT_DERIVED;
1759 : break;
1760 :
1761 : case FUNCTION_TYPE:
1762 : case VOID_TYPE:
1763 : n = BT_VOID;
1764 : break;
1765 :
1766 0 : default:
1767 : /* TODO: Don't do dtype for temporary descriptorless arrays. */
1768 : /* We can encounter strange array types for temporary arrays. */
1769 0 : gcc_unreachable ();
1770 : }
1771 :
1772 23423 : switch (n)
1773 : {
1774 23423 : case BT_CHARACTER:
1775 23423 : gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1776 23423 : size = gfc_get_character_len_in_bytes (ptype);
1777 23423 : break;
1778 2443 : case BT_VOID:
1779 2443 : gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1780 2443 : size = size_in_bytes (ptype);
1781 2443 : break;
1782 119340 : default:
1783 119340 : size = size_in_bytes (etype);
1784 119340 : break;
1785 : }
1786 :
1787 145206 : gcc_assert (size);
1788 :
1789 145206 : STRIP_NOPS (size);
1790 145206 : size = fold_convert (size_type_node, size);
1791 145206 : tmp = get_dtype_type_node ();
1792 145206 : field = gfc_advance_chain (TYPE_FIELDS (tmp),
1793 : GFC_DTYPE_ELEM_LEN);
1794 145206 : CONSTRUCTOR_APPEND_ELT (v, field,
1795 : fold_convert (TREE_TYPE (field), size));
1796 145206 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1797 : GFC_DTYPE_VERSION);
1798 145206 : CONSTRUCTOR_APPEND_ELT (v, field,
1799 : build_zero_cst (TREE_TYPE (field)));
1800 :
1801 145206 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1802 : GFC_DTYPE_RANK);
1803 145206 : if (rank >= 0)
1804 144619 : CONSTRUCTOR_APPEND_ELT (v, field,
1805 : build_int_cst (TREE_TYPE (field), rank));
1806 :
1807 145206 : field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1808 : GFC_DTYPE_TYPE);
1809 145206 : CONSTRUCTOR_APPEND_ELT (v, field,
1810 : build_int_cst (TREE_TYPE (field), n));
1811 :
1812 145206 : dtype = build_constructor (tmp, v);
1813 :
1814 145206 : return dtype;
1815 : }
1816 :
1817 :
1818 : tree
1819 117015 : gfc_get_dtype (tree type, int * rank)
1820 : {
1821 117015 : tree dtype;
1822 117015 : tree etype;
1823 117015 : int irnk;
1824 :
1825 117015 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1826 :
1827 117015 : irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
1828 117015 : etype = gfc_get_element_type (type);
1829 117015 : dtype = gfc_get_dtype_rank_type (irnk, etype);
1830 :
1831 117015 : GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1832 117015 : return dtype;
1833 : }
1834 :
1835 :
1836 : /* Build an array type for use without a descriptor, packed according
1837 : to the value of PACKED. */
1838 :
1839 : tree
1840 117524 : gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1841 : bool restricted)
1842 : {
1843 117524 : tree range;
1844 117524 : tree type;
1845 117524 : tree tmp;
1846 117524 : int n;
1847 117524 : int known_stride;
1848 117524 : int known_offset;
1849 117524 : mpz_t offset;
1850 117524 : mpz_t stride;
1851 117524 : mpz_t delta;
1852 117524 : gfc_expr *expr;
1853 :
1854 117524 : mpz_init_set_ui (offset, 0);
1855 117524 : mpz_init_set_ui (stride, 1);
1856 117524 : mpz_init (delta);
1857 :
1858 : /* We don't use build_array_type because this does not include
1859 : lang-specific information (i.e. the bounds of the array) when checking
1860 : for duplicates. */
1861 117524 : if (as->rank)
1862 115607 : type = make_node (ARRAY_TYPE);
1863 : else
1864 1917 : type = build_variant_type_copy (etype);
1865 :
1866 117524 : GFC_ARRAY_TYPE_P (type) = 1;
1867 117524 : TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1868 :
1869 117524 : known_stride = (packed != PACKED_NO);
1870 117524 : known_offset = 1;
1871 255084 : for (n = 0; n < as->rank; n++)
1872 : {
1873 : /* Fill in the stride and bound components of the type. */
1874 137560 : if (known_stride)
1875 123862 : tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1876 : else
1877 : tmp = NULL_TREE;
1878 137560 : GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1879 :
1880 137560 : expr = as->lower[n];
1881 137560 : if (expr && expr->expr_type == EXPR_CONSTANT)
1882 : {
1883 136770 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1884 : gfc_index_integer_kind);
1885 : }
1886 : else
1887 : {
1888 : known_stride = 0;
1889 : tmp = NULL_TREE;
1890 : }
1891 137560 : GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1892 :
1893 137560 : if (known_stride)
1894 : {
1895 : /* Calculate the offset. */
1896 123408 : mpz_mul (delta, stride, as->lower[n]->value.integer);
1897 123408 : mpz_sub (offset, offset, delta);
1898 : }
1899 : else
1900 : known_offset = 0;
1901 :
1902 137560 : expr = as->upper[n];
1903 137560 : if (expr && expr->expr_type == EXPR_CONSTANT)
1904 : {
1905 109864 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1906 : gfc_index_integer_kind);
1907 : }
1908 : else
1909 : {
1910 : tmp = NULL_TREE;
1911 : known_stride = 0;
1912 : }
1913 137560 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1914 :
1915 137560 : if (known_stride)
1916 : {
1917 : /* Calculate the stride. */
1918 108909 : mpz_sub (delta, as->upper[n]->value.integer,
1919 108909 : as->lower[n]->value.integer);
1920 108909 : mpz_add_ui (delta, delta, 1);
1921 108909 : mpz_mul (stride, stride, delta);
1922 : }
1923 :
1924 : /* Only the first stride is known for partial packed arrays. */
1925 137560 : if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1926 10743 : known_stride = 0;
1927 : }
1928 120084 : for (n = as->rank; n < as->rank + as->corank; n++)
1929 : {
1930 2560 : expr = as->lower[n];
1931 2560 : if (expr && expr->expr_type == EXPR_CONSTANT)
1932 2446 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1933 : gfc_index_integer_kind);
1934 : else
1935 : tmp = NULL_TREE;
1936 2560 : GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1937 :
1938 2560 : expr = as->upper[n];
1939 2560 : if (expr && expr->expr_type == EXPR_CONSTANT)
1940 214 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1941 : gfc_index_integer_kind);
1942 : else
1943 : tmp = NULL_TREE;
1944 2560 : if (n < as->rank + as->corank - 1)
1945 274 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1946 : }
1947 :
1948 117524 : if (known_offset)
1949 : {
1950 106835 : GFC_TYPE_ARRAY_OFFSET (type) =
1951 106835 : gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1952 : }
1953 : else
1954 10689 : GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1955 :
1956 117524 : if (known_stride)
1957 : {
1958 87182 : GFC_TYPE_ARRAY_SIZE (type) =
1959 87182 : gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1960 : }
1961 : else
1962 30342 : GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1963 :
1964 117524 : GFC_TYPE_ARRAY_RANK (type) = as->rank;
1965 117524 : GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1966 117524 : GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1967 117524 : range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1968 : NULL_TREE);
1969 : /* TODO: use main type if it is unbounded. */
1970 117524 : GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1971 117524 : build_pointer_type (build_array_type (etype, range));
1972 117524 : if (restricted)
1973 114278 : GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1974 114278 : build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1975 : TYPE_QUAL_RESTRICT);
1976 :
1977 117524 : if (as->rank == 0)
1978 : {
1979 1917 : if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1980 : {
1981 1843 : type = build_pointer_type (type);
1982 :
1983 1843 : if (restricted)
1984 1843 : type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1985 :
1986 1843 : GFC_ARRAY_TYPE_P (type) = 1;
1987 1843 : TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1988 : }
1989 :
1990 1917 : goto array_type_done;
1991 : }
1992 :
1993 115607 : if (known_stride)
1994 : {
1995 85306 : mpz_sub_ui (stride, stride, 1);
1996 85306 : range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1997 : }
1998 : else
1999 : range = NULL_TREE;
2000 :
2001 115607 : range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
2002 115607 : TYPE_DOMAIN (type) = range;
2003 :
2004 115607 : build_pointer_type (etype);
2005 115607 : TREE_TYPE (type) = etype;
2006 :
2007 115607 : layout_type (type);
2008 :
2009 : /* Represent packed arrays as multi-dimensional if they have rank >
2010 : 1 and with proper bounds, instead of flat arrays. This makes for
2011 : better debug info. */
2012 115607 : if (known_offset)
2013 : {
2014 104918 : tree gtype = etype, rtype, type_decl;
2015 :
2016 225789 : for (n = as->rank - 1; n >= 0; n--)
2017 : {
2018 483484 : rtype = build_range_type (gfc_array_index_type,
2019 120871 : GFC_TYPE_ARRAY_LBOUND (type, n),
2020 120871 : GFC_TYPE_ARRAY_UBOUND (type, n));
2021 120871 : gtype = build_array_type (gtype, rtype);
2022 : }
2023 104918 : TYPE_NAME (type) = type_decl = build_decl (input_location,
2024 : TYPE_DECL, NULL, gtype);
2025 104918 : DECL_ORIGINAL_TYPE (type_decl) = gtype;
2026 : }
2027 :
2028 115607 : if (packed != PACKED_STATIC || !known_stride
2029 81091 : || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
2030 : {
2031 : /* For dummy arrays and automatic (heap allocated) arrays we
2032 : want a pointer to the array. */
2033 34630 : type = build_pointer_type (type);
2034 34630 : if (restricted)
2035 33337 : type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2036 34630 : GFC_ARRAY_TYPE_P (type) = 1;
2037 34630 : TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
2038 : }
2039 :
2040 80977 : array_type_done:
2041 117524 : mpz_clear (offset);
2042 117524 : mpz_clear (stride);
2043 117524 : mpz_clear (delta);
2044 :
2045 117524 : return type;
2046 : }
2047 :
2048 :
2049 : /* Return or create the base type for an array descriptor. */
2050 :
2051 : static tree
2052 306268 : gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
2053 : {
2054 306268 : tree fat_type, decl, arraytype, *chain = NULL;
2055 306268 : char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
2056 306268 : int idx;
2057 :
2058 : /* Assumed-rank array. */
2059 306268 : if (dimen == -1)
2060 0 : dimen = GFC_MAX_DIMENSIONS;
2061 :
2062 306268 : idx = 2 * (codimen + dimen) + restricted;
2063 :
2064 306268 : gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
2065 :
2066 306268 : if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
2067 : {
2068 2200 : if (gfc_array_descriptor_base_caf[idx])
2069 : return gfc_array_descriptor_base_caf[idx];
2070 : }
2071 304068 : else if (gfc_array_descriptor_base[idx])
2072 : return gfc_array_descriptor_base[idx];
2073 :
2074 : /* Build the type node. */
2075 35325 : fat_type = make_node (RECORD_TYPE);
2076 :
2077 35325 : sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
2078 35325 : TYPE_NAME (fat_type) = get_identifier (name);
2079 35325 : TYPE_NAMELESS (fat_type) = 1;
2080 :
2081 : /* Add the data member as the first element of the descriptor. */
2082 35325 : gfc_add_field_to_struct_1 (fat_type,
2083 : get_identifier ("data"),
2084 : (restricted
2085 : ? prvoid_type_node
2086 : : ptr_type_node), &chain);
2087 :
2088 : /* Add the base component. */
2089 35325 : decl = gfc_add_field_to_struct_1 (fat_type,
2090 : get_identifier ("offset"),
2091 : gfc_array_index_type, &chain);
2092 35325 : suppress_warning (decl);
2093 :
2094 : /* Add the dtype component. */
2095 35325 : decl = gfc_add_field_to_struct_1 (fat_type,
2096 : get_identifier ("dtype"),
2097 : get_dtype_type_node (), &chain);
2098 35325 : suppress_warning (decl);
2099 :
2100 : /* Add the span component. */
2101 35325 : decl = gfc_add_field_to_struct_1 (fat_type,
2102 : get_identifier ("span"),
2103 : gfc_array_index_type, &chain);
2104 35325 : suppress_warning (decl);
2105 :
2106 : /* Build the array type for the stride and bound components. */
2107 35325 : if (dimen + codimen > 0)
2108 : {
2109 32415 : arraytype =
2110 32415 : build_array_type (gfc_get_desc_dim_type (),
2111 : build_range_type (gfc_array_index_type,
2112 : gfc_index_zero_node,
2113 32415 : gfc_rank_cst[codimen + dimen - 1]));
2114 :
2115 32415 : decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
2116 : arraytype, &chain);
2117 32415 : suppress_warning (decl);
2118 : }
2119 :
2120 35325 : if (flag_coarray == GFC_FCOARRAY_LIB)
2121 : {
2122 1664 : decl = gfc_add_field_to_struct_1 (fat_type,
2123 : get_identifier ("token"),
2124 : prvoid_type_node, &chain);
2125 1664 : suppress_warning (decl);
2126 : }
2127 :
2128 : /* Finish off the type. */
2129 35325 : gfc_finish_type (fat_type);
2130 35325 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
2131 :
2132 35325 : if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
2133 902 : gfc_array_descriptor_base_caf[idx] = fat_type;
2134 : else
2135 34423 : gfc_array_descriptor_base[idx] = fat_type;
2136 :
2137 : return fat_type;
2138 : }
2139 :
2140 :
2141 : /* Build an array (descriptor) type with given bounds. */
2142 :
2143 : tree
2144 153134 : gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
2145 : tree * ubound, int packed,
2146 : enum gfc_array_kind akind, bool restricted)
2147 : {
2148 153134 : char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
2149 153134 : tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
2150 153134 : const char *type_name;
2151 153134 : int n;
2152 :
2153 153134 : base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
2154 153134 : fat_type = build_distinct_type_copy (base_type);
2155 : /* Unshare TYPE_FIELDs. */
2156 916887 : for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
2157 : {
2158 763753 : tree next = DECL_CHAIN (*tp);
2159 763753 : *tp = copy_node (*tp);
2160 763753 : DECL_CONTEXT (*tp) = fat_type;
2161 763753 : DECL_CHAIN (*tp) = next;
2162 : }
2163 : /* Make sure that nontarget and target array type have the same canonical
2164 : type (and same stub decl for debug info). */
2165 153134 : base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
2166 153134 : TYPE_CANONICAL (fat_type) = base_type;
2167 153134 : TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
2168 : /* Arrays of unknown type must alias with all array descriptors. */
2169 153134 : TYPE_TYPELESS_STORAGE (base_type) = 1;
2170 153134 : TYPE_TYPELESS_STORAGE (fat_type) = 1;
2171 153134 : gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
2172 :
2173 153134 : tmp = etype;
2174 153134 : if (TREE_CODE (tmp) == ARRAY_TYPE
2175 153134 : && TYPE_STRING_FLAG (tmp))
2176 23975 : tmp = TREE_TYPE (etype);
2177 153134 : tmp = TYPE_NAME (tmp);
2178 153134 : if (tmp && TREE_CODE (tmp) == TYPE_DECL)
2179 125853 : tmp = DECL_NAME (tmp);
2180 125853 : if (tmp)
2181 149266 : type_name = IDENTIFIER_POINTER (tmp);
2182 : else
2183 : type_name = "unknown";
2184 153134 : sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
2185 : GFC_MAX_SYMBOL_LEN, type_name);
2186 153134 : TYPE_NAME (fat_type) = get_identifier (name);
2187 153134 : TYPE_NAMELESS (fat_type) = 1;
2188 :
2189 153134 : GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
2190 153134 : TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
2191 :
2192 153134 : GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
2193 153134 : GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
2194 153134 : GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
2195 153134 : GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
2196 :
2197 : /* Build an array descriptor record type. */
2198 153134 : if (packed != 0)
2199 36623 : stride = gfc_index_one_node;
2200 : else
2201 : stride = NULL_TREE;
2202 479418 : for (n = 0; n < dimen + codimen; n++)
2203 : {
2204 328166 : if (n < dimen)
2205 325137 : GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
2206 :
2207 328166 : if (lbound)
2208 328166 : lower = lbound[n];
2209 : else
2210 : lower = NULL_TREE;
2211 :
2212 328166 : if (lower != NULL_TREE)
2213 : {
2214 169323 : if (INTEGER_CST_P (lower))
2215 168224 : GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
2216 : else
2217 : lower = NULL_TREE;
2218 : }
2219 :
2220 328166 : if (codimen && n == dimen + codimen - 1)
2221 : break;
2222 :
2223 326284 : upper = ubound[n];
2224 326284 : if (upper != NULL_TREE)
2225 : {
2226 136067 : if (INTEGER_CST_P (upper))
2227 102449 : GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
2228 : else
2229 : upper = NULL_TREE;
2230 : }
2231 :
2232 326284 : if (n >= dimen)
2233 1147 : continue;
2234 :
2235 325137 : if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
2236 : {
2237 28890 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2238 : gfc_array_index_type, upper, lower);
2239 28890 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2240 : gfc_array_index_type, tmp,
2241 : gfc_index_one_node);
2242 28890 : stride = fold_build2_loc (input_location, MULT_EXPR,
2243 : gfc_array_index_type, tmp, stride);
2244 : /* Check the folding worked. */
2245 28890 : gcc_assert (INTEGER_CST_P (stride));
2246 : }
2247 : else
2248 : stride = NULL_TREE;
2249 : }
2250 153134 : GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
2251 :
2252 : /* TODO: known offsets for descriptors. */
2253 153134 : GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2254 :
2255 153134 : if (dimen == 0)
2256 : {
2257 7799 : arraytype = build_pointer_type (etype);
2258 7799 : if (restricted)
2259 7101 : arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2260 :
2261 7799 : GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2262 7799 : return fat_type;
2263 : }
2264 :
2265 : /* We define data as an array with the correct size if possible.
2266 : Much better than doing pointer arithmetic. */
2267 145335 : if (stride)
2268 22532 : rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2269 : int_const_binop (MINUS_EXPR, stride,
2270 45064 : build_int_cst (TREE_TYPE (stride), 1)));
2271 : else
2272 122803 : rtype = gfc_array_range_type;
2273 145335 : arraytype = build_array_type (etype, rtype);
2274 145335 : arraytype = build_pointer_type (arraytype);
2275 145335 : if (restricted)
2276 69443 : arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2277 145335 : GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2278 :
2279 : /* This will generate the base declarations we need to emit debug
2280 : information for this type. FIXME: there must be a better way to
2281 : avoid divergence between compilations with and without debug
2282 : information. */
2283 145335 : {
2284 145335 : struct array_descr_info info;
2285 145335 : gfc_get_array_descr_info (fat_type, &info);
2286 145335 : gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2287 : }
2288 :
2289 145335 : return fat_type;
2290 : }
2291 :
2292 : /* Build a pointer type. This function is called from gfc_sym_type(). */
2293 :
2294 : static tree
2295 17121 : gfc_build_pointer_type (gfc_symbol * sym, tree type)
2296 : {
2297 : /* Array pointer types aren't actually pointers. */
2298 0 : if (sym->attr.dimension)
2299 : return type;
2300 : else
2301 17121 : return build_pointer_type (type);
2302 : }
2303 :
2304 : static tree gfc_nonrestricted_type (tree t);
2305 : /* Given two record or union type nodes TO and FROM, ensure
2306 : that all fields in FROM have a corresponding field in TO,
2307 : their type being nonrestrict variants. This accepts a TO
2308 : node that already has a prefix of the fields in FROM. */
2309 : static void
2310 4463 : mirror_fields (tree to, tree from)
2311 : {
2312 4463 : tree fto, ffrom;
2313 4463 : tree *chain;
2314 :
2315 : /* Forward to the end of TOs fields. */
2316 4463 : fto = TYPE_FIELDS (to);
2317 4463 : ffrom = TYPE_FIELDS (from);
2318 4463 : chain = &TYPE_FIELDS (to);
2319 4463 : while (fto)
2320 : {
2321 0 : gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2322 0 : chain = &DECL_CHAIN (fto);
2323 0 : fto = DECL_CHAIN (fto);
2324 0 : ffrom = DECL_CHAIN (ffrom);
2325 : }
2326 :
2327 : /* Now add all fields remaining in FROM (starting with ffrom). */
2328 21300 : for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2329 : {
2330 16837 : tree newfield = copy_node (ffrom);
2331 16837 : DECL_CONTEXT (newfield) = to;
2332 : /* The store to DECL_CHAIN might seem redundant with the
2333 : stores to *chain, but not clearing it here would mean
2334 : leaving a chain into the old fields. If ever
2335 : our called functions would look at them confusion
2336 : will arise. */
2337 16837 : DECL_CHAIN (newfield) = NULL_TREE;
2338 16837 : *chain = newfield;
2339 16837 : chain = &DECL_CHAIN (newfield);
2340 :
2341 16837 : if (TREE_CODE (ffrom) == FIELD_DECL)
2342 : {
2343 16837 : tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2344 16837 : TREE_TYPE (newfield) = elemtype;
2345 : }
2346 : }
2347 4463 : *chain = NULL_TREE;
2348 4463 : }
2349 :
2350 : /* Given a type T, returns a different type of the same structure,
2351 : except that all types it refers to (recursively) are always
2352 : non-restrict qualified types. */
2353 : static tree
2354 269326 : gfc_nonrestricted_type (tree t)
2355 : {
2356 269326 : tree ret = t;
2357 :
2358 : /* If the type isn't laid out yet, don't copy it. If something
2359 : needs it for real it should wait until the type got finished. */
2360 269326 : if (!TYPE_SIZE (t))
2361 : return t;
2362 :
2363 257196 : if (!TYPE_LANG_SPECIFIC (t))
2364 103892 : TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2365 : /* If we're dealing with this very node already further up
2366 : the call chain (recursion via pointers and struct members)
2367 : we haven't yet determined if we really need a new type node.
2368 : Assume we don't, return T itself. */
2369 257196 : if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2370 : return t;
2371 :
2372 : /* If we have calculated this all already, just return it. */
2373 249679 : if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2374 140467 : return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2375 :
2376 : /* Mark this type. */
2377 109212 : TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2378 :
2379 109212 : switch (TREE_CODE (t))
2380 : {
2381 : default:
2382 : break;
2383 :
2384 39475 : case POINTER_TYPE:
2385 39475 : case REFERENCE_TYPE:
2386 39475 : {
2387 39475 : tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2388 39475 : if (totype == TREE_TYPE (t))
2389 : ret = t;
2390 1567 : else if (TREE_CODE (t) == POINTER_TYPE)
2391 1567 : ret = build_pointer_type (totype);
2392 : else
2393 0 : ret = build_reference_type (totype);
2394 78950 : ret = build_qualified_type (ret,
2395 39475 : TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2396 : }
2397 39475 : break;
2398 :
2399 6492 : case ARRAY_TYPE:
2400 6492 : {
2401 6492 : tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2402 6492 : if (elemtype == TREE_TYPE (t))
2403 : ret = t;
2404 : else
2405 : {
2406 21 : ret = build_variant_type_copy (t);
2407 21 : TREE_TYPE (ret) = elemtype;
2408 21 : if (TYPE_LANG_SPECIFIC (t)
2409 21 : && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2410 : {
2411 21 : tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2412 21 : dataptr_type = gfc_nonrestricted_type (dataptr_type);
2413 21 : if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2414 : {
2415 21 : TYPE_LANG_SPECIFIC (ret)
2416 21 : = ggc_cleared_alloc<struct lang_type> ();
2417 21 : *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2418 21 : GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2419 : }
2420 : }
2421 : }
2422 : }
2423 : break;
2424 :
2425 30117 : case RECORD_TYPE:
2426 30117 : case UNION_TYPE:
2427 30117 : case QUAL_UNION_TYPE:
2428 30117 : {
2429 30117 : tree field;
2430 : /* First determine if we need a new type at all.
2431 : Careful, the two calls to gfc_nonrestricted_type per field
2432 : might return different values. That happens exactly when
2433 : one of the fields reaches back to this very record type
2434 : (via pointers). The first calls will assume that we don't
2435 : need to copy T (see the error_mark_node marking). If there
2436 : are any reasons for copying T apart from having to copy T,
2437 : we'll indeed copy it, and the second calls to
2438 : gfc_nonrestricted_type will use that new node if they
2439 : reach back to T. */
2440 148458 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2441 122804 : if (TREE_CODE (field) == FIELD_DECL)
2442 : {
2443 122804 : tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2444 122804 : if (elemtype != TREE_TYPE (field))
2445 : break;
2446 : }
2447 30117 : if (!field)
2448 : break;
2449 4463 : ret = build_variant_type_copy (t);
2450 4463 : TYPE_FIELDS (ret) = NULL_TREE;
2451 :
2452 : /* Here we make sure that as soon as we know we have to copy
2453 : T, that also fields reaching back to us will use the new
2454 : copy. It's okay if that copy still contains the old fields,
2455 : we won't look at them. */
2456 4463 : TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2457 4463 : mirror_fields (ret, t);
2458 : }
2459 4463 : break;
2460 : }
2461 :
2462 109212 : TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2463 109212 : return ret;
2464 : }
2465 :
2466 :
2467 : /* Return the type for a symbol. Special handling is required for character
2468 : types to get the correct level of indirection.
2469 : For functions return the return type.
2470 : For subroutines return void_type_node.
2471 : Calling this multiple times for the same symbol should be avoided,
2472 : especially for character and array types. */
2473 :
2474 : tree
2475 424691 : gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
2476 : {
2477 424691 : tree type;
2478 424691 : int byref;
2479 424691 : bool restricted;
2480 :
2481 : /* Procedure Pointers inside COMMON blocks. */
2482 424691 : if (sym->attr.proc_pointer && sym->attr.in_common)
2483 : {
2484 : /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2485 30 : sym->attr.proc_pointer = 0;
2486 30 : type = build_pointer_type (gfc_get_function_type (sym));
2487 30 : sym->attr.proc_pointer = 1;
2488 30 : return type;
2489 : }
2490 :
2491 424661 : if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2492 0 : return void_type_node;
2493 :
2494 : /* In the case of a function the fake result variable may have a
2495 : type different from the function type, so don't return early in
2496 : that case. */
2497 424661 : if (sym->backend_decl && !sym->attr.function)
2498 493 : return TREE_TYPE (sym->backend_decl);
2499 :
2500 424168 : if (sym->attr.result
2501 8586 : && sym->ts.type == BT_CHARACTER
2502 1192 : && sym->ts.u.cl->backend_decl == NULL_TREE
2503 514 : && sym->ns->proc_name
2504 508 : && sym->ns->proc_name->ts.u.cl
2505 506 : && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2506 6 : sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2507 :
2508 424168 : if (sym->ts.type == BT_CHARACTER
2509 424168 : && ((sym->attr.function && sym->attr.is_bind_c)
2510 42581 : || ((sym->attr.result || sym->attr.value)
2511 1614 : && sym->ns->proc_name
2512 1608 : && sym->ns->proc_name->attr.is_bind_c)
2513 42325 : || (sym->ts.deferred
2514 4635 : && (!sym->ts.u.cl
2515 4635 : || !sym->ts.u.cl->backend_decl
2516 3390 : || sym->attr.save))
2517 40903 : || (sym->attr.dummy
2518 19064 : && sym->attr.value
2519 168 : && gfc_length_one_character_type_p (&sym->ts))))
2520 1895 : type = gfc_get_char_type (sym->ts.kind);
2521 : else
2522 422273 : type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2523 :
2524 424168 : if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
2525 154420 : && !sym->pass_as_value)
2526 : byref = 1;
2527 : else
2528 271048 : byref = 0;
2529 :
2530 394610 : restricted = (!sym->attr.target && !IS_POINTER (sym)
2531 800408 : && !IS_PROC_POINTER (sym) && !sym->attr.cray_pointee);
2532 48677 : if (!restricted)
2533 48677 : type = gfc_nonrestricted_type (type);
2534 :
2535 : /* Dummy argument to a bind(C) procedure. */
2536 424168 : if (is_bind_c && is_CFI_desc (sym, NULL))
2537 3641 : type = gfc_get_cfi_type (sym->attr.dimension ? sym->as->rank : 0,
2538 : /* restricted = */ false);
2539 420527 : else if (sym->attr.dimension || sym->attr.codimension)
2540 : {
2541 100539 : if (gfc_is_nodesc_array (sym))
2542 : {
2543 : /* If this is a character argument of unknown length, just use the
2544 : base type. */
2545 54650 : if (sym->ts.type != BT_CHARACTER
2546 5891 : || !(sym->attr.dummy || sym->attr.function)
2547 1918 : || sym->ts.u.cl->backend_decl)
2548 : {
2549 54228 : type = gfc_get_nodesc_array_type (type, sym->as,
2550 : byref ? PACKED_FULL
2551 : : PACKED_STATIC,
2552 : restricted);
2553 54228 : byref = 0;
2554 : }
2555 : }
2556 : else
2557 : {
2558 45889 : enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2559 45889 : if (sym->attr.pointer)
2560 7270 : akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2561 : : GFC_ARRAY_POINTER;
2562 38619 : else if (sym->attr.allocatable)
2563 12287 : akind = GFC_ARRAY_ALLOCATABLE;
2564 45889 : type = gfc_build_array_type (type, sym->as, akind, restricted,
2565 45889 : sym->attr.contiguous, sym->as->corank);
2566 : }
2567 : }
2568 : else
2569 : {
2570 315569 : if (sym->attr.allocatable || sym->attr.pointer
2571 626017 : || gfc_is_associate_pointer (sym))
2572 17121 : type = gfc_build_pointer_type (sym, type);
2573 : }
2574 :
2575 : /* We currently pass all parameters by reference.
2576 : See f95_get_function_decl. For dummy function parameters return the
2577 : function type. */
2578 424168 : if (byref)
2579 : {
2580 : /* We must use pointer types for potentially absent variables. The
2581 : optimizers assume a reference type argument is never NULL. */
2582 138984 : if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2583 138984 : || sym->attr.optional
2584 120379 : || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2585 20273 : type = build_pointer_type (type);
2586 : else
2587 118711 : type = build_reference_type (type);
2588 :
2589 138984 : if (restricted)
2590 131407 : type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2591 : }
2592 :
2593 : return (type);
2594 : }
2595 :
2596 : /* Layout and output debug info for a record type. */
2597 :
2598 : void
2599 344590 : gfc_finish_type (tree type)
2600 : {
2601 344590 : tree decl;
2602 :
2603 344590 : decl = build_decl (input_location,
2604 : TYPE_DECL, NULL_TREE, type);
2605 344590 : TYPE_STUB_DECL (type) = decl;
2606 344590 : layout_type (type);
2607 344590 : rest_of_type_compilation (type, 1);
2608 344590 : rest_of_decl_compilation (decl, 1, 0);
2609 344590 : }
2610 :
2611 : /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2612 : or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2613 : to the end of the field list pointed to by *CHAIN.
2614 :
2615 : Returns a pointer to the new field. */
2616 :
2617 : static tree
2618 5086007 : gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2619 : {
2620 5086007 : tree decl = build_decl (input_location, FIELD_DECL, name, type);
2621 :
2622 5086007 : DECL_CONTEXT (decl) = context;
2623 5086007 : DECL_CHAIN (decl) = NULL_TREE;
2624 5086007 : if (TYPE_FIELDS (context) == NULL_TREE)
2625 337395 : TYPE_FIELDS (context) = decl;
2626 5086007 : if (chain != NULL)
2627 : {
2628 5086007 : if (*chain != NULL)
2629 4748612 : **chain = decl;
2630 5086007 : *chain = &DECL_CHAIN (decl);
2631 : }
2632 :
2633 5086007 : return decl;
2634 : }
2635 :
2636 : /* Like `gfc_add_field_to_struct_1', but adds alignment
2637 : information. */
2638 :
2639 : tree
2640 4700934 : gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2641 : {
2642 4700934 : tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2643 :
2644 4700934 : DECL_INITIAL (decl) = 0;
2645 4700934 : SET_DECL_ALIGN (decl, 0);
2646 4700934 : DECL_USER_ALIGN (decl) = 0;
2647 :
2648 4700934 : return decl;
2649 : }
2650 :
2651 :
2652 : /* Copy the backend_decl and component backend_decls if
2653 : the two derived type symbols are "equal", as described
2654 : in 4.4.2 and resolved by gfc_compare_derived_types. */
2655 :
2656 : bool
2657 356782 : gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2658 : bool from_gsym)
2659 : {
2660 356782 : gfc_component *to_cm;
2661 356782 : gfc_component *from_cm;
2662 :
2663 356782 : if (from == to)
2664 : return 1;
2665 :
2666 312536 : if (from->backend_decl == NULL
2667 312536 : || !gfc_compare_derived_types (from, to))
2668 : return 0;
2669 :
2670 15569 : to->backend_decl = from->backend_decl;
2671 :
2672 15569 : to_cm = to->components;
2673 15569 : from_cm = from->components;
2674 :
2675 : /* Copy the component declarations. If a component is itself
2676 : a derived type, we need a copy of its component declarations.
2677 : This is done by recursing into gfc_get_derived_type and
2678 : ensures that the component's component declarations have
2679 : been built. If it is a character, we need the character
2680 : length, as well. */
2681 58764 : for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2682 : {
2683 43195 : to_cm->backend_decl = from_cm->backend_decl;
2684 43195 : to_cm->caf_token = from_cm->caf_token;
2685 43195 : if (from_cm->ts.type == BT_UNION)
2686 28 : gfc_get_union_type (to_cm->ts.u.derived);
2687 43167 : else if (from_cm->ts.type == BT_DERIVED
2688 15069 : && (!from_cm->attr.pointer || from_gsym))
2689 13703 : gfc_get_derived_type (to_cm->ts.u.derived);
2690 29464 : else if (from_cm->ts.type == BT_CLASS
2691 794 : && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2692 787 : gfc_get_derived_type (to_cm->ts.u.derived);
2693 28677 : else if (from_cm->ts.type == BT_CHARACTER)
2694 870 : to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2695 : }
2696 :
2697 : return 1;
2698 : }
2699 :
2700 :
2701 : /* Build a tree node for a procedure pointer component. */
2702 :
2703 : static tree
2704 32401 : gfc_get_ppc_type (gfc_component* c)
2705 : {
2706 32401 : tree t;
2707 :
2708 : /* Explicit interface. */
2709 32401 : if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2710 3596 : return build_pointer_type (gfc_get_function_type (c->ts.interface));
2711 :
2712 : /* Implicit interface (only return value may be known). */
2713 28805 : if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2714 9 : t = gfc_typenode_for_spec (&c->ts);
2715 : else
2716 28796 : t = void_type_node;
2717 :
2718 : /* FIXME: it would be better to provide explicit interfaces in all
2719 : cases, since they should be known by the compiler. */
2720 28805 : return build_pointer_type (build_function_type (t, NULL_TREE));
2721 : }
2722 :
2723 :
2724 : /* Build a tree node for a union type. Requires building each map
2725 : structure which is an element of the union. */
2726 :
2727 : tree
2728 252 : gfc_get_union_type (gfc_symbol *un)
2729 : {
2730 252 : gfc_component *map = NULL;
2731 252 : tree typenode = NULL, map_type = NULL, map_field = NULL;
2732 252 : tree *chain = NULL;
2733 :
2734 252 : if (un->backend_decl)
2735 : {
2736 130 : if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2737 : return un->backend_decl;
2738 : else
2739 : typenode = un->backend_decl;
2740 : }
2741 : else
2742 : {
2743 122 : typenode = make_node (UNION_TYPE);
2744 122 : TYPE_NAME (typenode) = get_identifier (un->name);
2745 : }
2746 :
2747 : /* Add each contained MAP as a field. */
2748 363 : for (map = un->components; map; map = map->next)
2749 : {
2750 238 : gcc_assert (map->ts.type == BT_DERIVED);
2751 :
2752 : /* The map's type node, which is defined within this union's context. */
2753 238 : map_type = gfc_get_derived_type (map->ts.u.derived);
2754 238 : TYPE_CONTEXT (map_type) = typenode;
2755 :
2756 : /* The map field's declaration. */
2757 238 : map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2758 : map_type, &chain);
2759 238 : if (GFC_LOCUS_IS_SET (map->loc))
2760 238 : gfc_set_decl_location (map_field, &map->loc);
2761 0 : else if (GFC_LOCUS_IS_SET (un->declared_at))
2762 0 : gfc_set_decl_location (map_field, &un->declared_at);
2763 :
2764 238 : DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2765 238 : DECL_NAMELESS(map_field) = true;
2766 :
2767 : /* We should never clobber another backend declaration for this map,
2768 : because each map component is unique. */
2769 238 : if (!map->backend_decl)
2770 238 : map->backend_decl = map_field;
2771 : }
2772 :
2773 125 : un->backend_decl = typenode;
2774 125 : gfc_finish_type (typenode);
2775 :
2776 125 : return typenode;
2777 : }
2778 :
2779 : bool
2780 179 : cobounds_match_decl (const gfc_symbol *derived)
2781 : {
2782 179 : tree arrtype, tmp;
2783 179 : gfc_array_spec *as;
2784 :
2785 179 : if (!derived->backend_decl)
2786 : return false;
2787 : /* Care only about coarray declarations. Everything else is ok with us. */
2788 179 : if (!derived->components || strcmp (derived->components->name, "_data") != 0)
2789 : return true;
2790 179 : if (!derived->components->attr.codimension)
2791 : return true;
2792 :
2793 179 : arrtype = TREE_TYPE (TYPE_FIELDS (derived->backend_decl));
2794 179 : as = derived->components->as;
2795 179 : if (GFC_TYPE_ARRAY_CORANK (arrtype) != as->corank)
2796 : return false;
2797 :
2798 231 : for (int dim = as->rank; dim < as->rank + as->corank; ++dim)
2799 : {
2800 : /* Check lower bound. */
2801 120 : tmp = TYPE_LANG_SPECIFIC (arrtype)->lbound[dim];
2802 120 : if (!tmp || !INTEGER_CST_P (tmp))
2803 : return false;
2804 120 : if (as->lower[dim]->expr_type != EXPR_CONSTANT
2805 120 : || as->lower[dim]->ts.type != BT_INTEGER)
2806 : return false;
2807 120 : if (*tmp->int_cst.val != mpz_get_si (as->lower[dim]->value.integer))
2808 : return false;
2809 :
2810 : /* Check upper bound. */
2811 114 : tmp = TYPE_LANG_SPECIFIC (arrtype)->ubound[dim];
2812 114 : if (!tmp && !as->upper[dim])
2813 111 : continue;
2814 :
2815 3 : if (!tmp || !INTEGER_CST_P (tmp))
2816 : return false;
2817 3 : if (as->upper[dim]->expr_type != EXPR_CONSTANT
2818 3 : || as->upper[dim]->ts.type != BT_INTEGER)
2819 : return false;
2820 3 : if (*tmp->int_cst.val != mpz_get_si (as->upper[dim]->value.integer))
2821 : return false;
2822 : }
2823 :
2824 : return true;
2825 : }
2826 :
2827 : /* Build a tree node for a derived type. If there are equal
2828 : derived types, with different local names, these are built
2829 : at the same time. If an equal derived type has been built
2830 : in a parent namespace, this is used. */
2831 :
2832 : tree
2833 195054 : gfc_get_derived_type (gfc_symbol * derived, int codimen)
2834 : {
2835 195054 : tree typenode = NULL, field = NULL, field_type = NULL;
2836 195054 : tree canonical = NULL_TREE;
2837 195054 : tree *chain = NULL;
2838 195054 : bool got_canonical = false;
2839 195054 : bool unlimited_entity = false;
2840 195054 : gfc_component *c;
2841 195054 : gfc_namespace *ns;
2842 195054 : tree tmp;
2843 195054 : bool coarray_flag, class_coarray_flag;
2844 :
2845 390108 : coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2846 195054 : && derived->module && !derived->attr.vtype;
2847 390108 : class_coarray_flag = derived->components
2848 182708 : && derived->components->ts.type == BT_DERIVED
2849 61412 : && strcmp (derived->components->name, "_data") == 0
2850 35622 : && derived->components->attr.codimension
2851 195721 : && derived->components->as->cotype == AS_EXPLICIT;
2852 :
2853 195054 : gcc_assert (!derived->attr.pdt_template);
2854 :
2855 195054 : if (derived->attr.unlimited_polymorphic
2856 191219 : || (flag_coarray == GFC_FCOARRAY_LIB
2857 3986 : && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2858 127 : && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2859 : || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2860 127 : || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2861 3962 : return ptr_type_node;
2862 :
2863 191092 : if (flag_coarray != GFC_FCOARRAY_LIB
2864 187233 : && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2865 443 : && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2866 443 : || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2867 326 : return gfc_get_int_type (gfc_default_integer_kind);
2868 :
2869 190766 : if (derived && derived->attr.flavor == FL_PROCEDURE
2870 52 : && derived->attr.generic)
2871 52 : derived = gfc_find_dt_in_generic (derived);
2872 :
2873 : /* See if it's one of the iso_c_binding derived types. */
2874 190766 : if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2875 : {
2876 12571 : if (derived->backend_decl)
2877 : return derived->backend_decl;
2878 :
2879 5397 : if (derived->intmod_sym_id == ISOCBINDING_PTR)
2880 2913 : derived->backend_decl = ptr_type_node;
2881 : else
2882 2484 : derived->backend_decl = pfunc_type_node;
2883 :
2884 5397 : derived->ts.kind = gfc_index_integer_kind;
2885 5397 : derived->ts.type = BT_INTEGER;
2886 : /* Set the f90_type to BT_VOID as a way to recognize something of type
2887 : BT_INTEGER that needs to fit a void * for the purpose of the
2888 : iso_c_binding derived types. */
2889 5397 : derived->ts.f90_type = BT_VOID;
2890 :
2891 5397 : return derived->backend_decl;
2892 : }
2893 :
2894 : /* If use associated, use the module type for this one. */
2895 178195 : if (derived->backend_decl == NULL
2896 44373 : && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2897 11782 : && derived->module
2898 189977 : && gfc_get_module_backend_decl (derived))
2899 11298 : goto copy_derived_types;
2900 :
2901 : /* The derived types from an earlier namespace can be used as the
2902 : canonical type. */
2903 166897 : if (derived->backend_decl == NULL
2904 33075 : && !derived->attr.use_assoc
2905 32612 : && !derived->attr.used_in_submodule
2906 32591 : && gfc_global_ns_list)
2907 : {
2908 8537 : for (ns = gfc_global_ns_list;
2909 41122 : ns->translated && !got_canonical;
2910 8537 : ns = ns->sibling)
2911 : {
2912 8537 : if (ns->derived_types)
2913 : {
2914 31383 : for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2915 : dt = dt->dt_next)
2916 : {
2917 31132 : gfc_copy_dt_decls_ifequal (dt, derived, true);
2918 31132 : if (derived->backend_decl)
2919 370 : got_canonical = true;
2920 31132 : if (dt->dt_next == ns->derived_types)
2921 : break;
2922 : }
2923 : }
2924 : }
2925 : }
2926 :
2927 : /* Store up the canonical type to be added to this one. */
2928 32585 : if (got_canonical)
2929 : {
2930 370 : if (TYPE_CANONICAL (derived->backend_decl))
2931 370 : canonical = TYPE_CANONICAL (derived->backend_decl);
2932 : else
2933 : canonical = derived->backend_decl;
2934 :
2935 370 : derived->backend_decl = NULL_TREE;
2936 : }
2937 :
2938 : /* derived->backend_decl != 0 means we saw it before, but its
2939 : components' backend_decl may have not been built. */
2940 166897 : if (derived->backend_decl
2941 166897 : && (!class_coarray_flag || cobounds_match_decl (derived)))
2942 : {
2943 : /* Its components' backend_decl have been built or we are
2944 : seeing recursion through the formal arglist of a procedure
2945 : pointer component. */
2946 133754 : if (TYPE_FIELDS (derived->backend_decl))
2947 : return derived->backend_decl;
2948 4890 : else if (derived->attr.abstract
2949 731 : && derived->attr.proc_pointer_comp)
2950 : {
2951 : /* If an abstract derived type with procedure pointer
2952 : components has no other type of component, return the
2953 : backend_decl. Otherwise build the components if any of the
2954 : non-procedure pointer components have no backend_decl. */
2955 1 : for (c = derived->components; c; c = c->next)
2956 : {
2957 2 : bool same_alloc_type = c->attr.allocatable
2958 1 : && derived == c->ts.u.derived;
2959 1 : if (!c->attr.proc_pointer
2960 1 : && !same_alloc_type
2961 1 : && c->backend_decl == NULL)
2962 : break;
2963 0 : else if (c->next == NULL)
2964 : return derived->backend_decl;
2965 : }
2966 : typenode = derived->backend_decl;
2967 : }
2968 : else
2969 : typenode = derived->backend_decl;
2970 : }
2971 : else
2972 : {
2973 : /* We see this derived type first time, so build the type node. */
2974 33143 : typenode = make_node (RECORD_TYPE);
2975 33143 : TYPE_NAME (typenode) = get_identifier (derived->name);
2976 33143 : TYPE_PACKED (typenode) = flag_pack_derived;
2977 33143 : derived->backend_decl = typenode;
2978 33143 : if (derived->attr.is_class)
2979 7845 : GFC_CLASS_TYPE_P (typenode) = 1;
2980 : }
2981 :
2982 38033 : if (derived->components
2983 30843 : && derived->components->ts.type == BT_DERIVED
2984 11216 : && startswith (derived->name, "__class")
2985 7897 : && strcmp (derived->components->name, "_data") == 0
2986 45930 : && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2987 : unlimited_entity = true;
2988 :
2989 : /* Go through the derived type components, building them as
2990 : necessary. The reason for doing this now is that it is
2991 : possible to recurse back to this derived type through a
2992 : pointer component (PR24092). If this happens, the fields
2993 : will be built and so we can return the type. */
2994 151306 : for (c = derived->components; c; c = c->next)
2995 : {
2996 113273 : if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2997 108 : c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2998 :
2999 113273 : if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
3000 73675 : continue;
3001 :
3002 39598 : const bool incomplete_type
3003 39598 : = c->ts.u.derived->backend_decl
3004 32804 : && TREE_CODE (c->ts.u.derived->backend_decl) == RECORD_TYPE
3005 71042 : && !(TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)
3006 18584 : && TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)->size);
3007 79196 : const bool pointer_component
3008 39598 : = c->attr.pointer || c->attr.allocatable || c->attr.proc_pointer;
3009 :
3010 : /* Prevent endless recursion on recursive types (i.e. types that reference
3011 : themself in a component. Break the recursion by not building pointers
3012 : to incomplete types again, aka types that are already in the build. */
3013 39598 : if (c->ts.u.derived->backend_decl == NULL
3014 32804 : || (c->attr.codimension && c->as->corank != codimen)
3015 32513 : || !(incomplete_type && pointer_component))
3016 : {
3017 9450 : int local_codim = c->attr.codimension ? c->as->corank: codimen;
3018 9450 : c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
3019 : local_codim);
3020 : }
3021 :
3022 39598 : if (c->ts.u.derived->attr.is_iso_c)
3023 : {
3024 : /* Need to copy the modified ts from the derived type. The
3025 : typespec was modified because C_PTR/C_FUNPTR are translated
3026 : into (void *) from derived types. */
3027 1 : c->ts.type = c->ts.u.derived->ts.type;
3028 1 : c->ts.kind = c->ts.u.derived->ts.kind;
3029 1 : c->ts.f90_type = c->ts.u.derived->ts.f90_type;
3030 1 : if (c->initializer)
3031 : {
3032 0 : c->initializer->ts.type = c->ts.type;
3033 0 : c->initializer->ts.kind = c->ts.kind;
3034 0 : c->initializer->ts.f90_type = c->ts.f90_type;
3035 0 : c->initializer->expr_type = EXPR_NULL;
3036 : }
3037 : }
3038 : }
3039 :
3040 38033 : if (!class_coarray_flag && TYPE_FIELDS (derived->backend_decl))
3041 : return derived->backend_decl;
3042 :
3043 : /* Build the type member list. Install the newly created RECORD_TYPE
3044 : node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
3045 : through only the top-level linked list of components so we correctly
3046 : build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
3047 : types are built as part of gfc_get_union_type. */
3048 151092 : for (c = derived->components; c; c = c->next)
3049 : {
3050 226268 : bool same_alloc_type = c->attr.allocatable
3051 113134 : && derived == c->ts.u.derived;
3052 : /* Prevent infinite recursion, when the procedure pointer type is
3053 : the same as derived, by forcing the procedure pointer component to
3054 : be built as if the explicit interface does not exist. */
3055 113134 : if (c->attr.proc_pointer
3056 32445 : && (c->ts.type != BT_DERIVED || (c->ts.u.derived
3057 215 : && !gfc_compare_derived_types (derived, c->ts.u.derived)))
3058 145555 : && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
3059 340 : && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
3060 32401 : field_type = gfc_get_ppc_type (c);
3061 80733 : else if (c->attr.proc_pointer && derived->backend_decl)
3062 : {
3063 44 : tmp = build_function_type (derived->backend_decl, NULL_TREE);
3064 44 : field_type = build_pointer_type (tmp);
3065 : }
3066 80689 : else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
3067 38911 : field_type = c->ts.u.derived->backend_decl;
3068 41778 : else if (c->attr.caf_token)
3069 661 : field_type = pvoid_type_node;
3070 : else
3071 : {
3072 41117 : if (c->ts.type == BT_CHARACTER
3073 1897 : && !c->ts.deferred && !c->attr.pdt_string)
3074 : {
3075 : /* Evaluate the string length. */
3076 1428 : gfc_conv_const_charlen (c->ts.u.cl);
3077 1428 : gcc_assert (c->ts.u.cl->backend_decl);
3078 : }
3079 39689 : else if (c->ts.type == BT_CHARACTER)
3080 469 : c->ts.u.cl->backend_decl
3081 469 : = build_int_cst (gfc_charlen_type_node, 0);
3082 :
3083 41117 : field_type = gfc_typenode_for_spec (&c->ts, codimen);
3084 : }
3085 :
3086 : /* This returns an array descriptor type. Initialization may be
3087 : required. */
3088 113134 : if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
3089 : {
3090 9169 : if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
3091 : {
3092 7380 : enum gfc_array_kind akind;
3093 7380 : bool is_ptr = ((c == derived->components
3094 4955 : && derived->components->ts.type == BT_DERIVED
3095 3652 : && startswith (derived->name, "__class")
3096 3046 : && (strcmp (derived->components->name, "_data")
3097 : == 0))
3098 12335 : ? c->attr.class_pointer : c->attr.pointer);
3099 7380 : if (is_ptr)
3100 1898 : akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
3101 : : GFC_ARRAY_POINTER;
3102 5482 : else if (c->attr.allocatable)
3103 : akind = GFC_ARRAY_ALLOCATABLE;
3104 1271 : else if (c->as->type == AS_ASSUMED_RANK)
3105 : akind = GFC_ARRAY_ASSUMED_RANK;
3106 : else
3107 : /* FIXME – see PR fortran/104651. Additionally, the following
3108 : gfc_build_array_type should use !is_ptr instead of
3109 : c->attr.pointer and codim unconditionally without '? :'. */
3110 1115 : akind = GFC_ARRAY_ASSUMED_SHAPE;
3111 : /* Pointers to arrays aren't actually pointer types. The
3112 : descriptors are separate, but the data is common. Every
3113 : array pointer in a coarray derived type needs to provide space
3114 : for the coarray management, too. Therefore treat coarrays
3115 : and pointers to coarrays in derived types the same. */
3116 7380 : field_type = gfc_build_array_type
3117 10354 : (
3118 : field_type, c->as, akind, !c->attr.target && !c->attr.pointer,
3119 : c->attr.contiguous,
3120 7380 : c->attr.codimension || c->attr.pointer ? codimen : 0
3121 : );
3122 7380 : }
3123 : else
3124 1789 : field_type = gfc_get_nodesc_array_type (field_type, c->as,
3125 : PACKED_STATIC,
3126 : !c->attr.target);
3127 : }
3128 103965 : else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
3129 34750 : && !c->attr.proc_pointer
3130 34584 : && !(unlimited_entity && c == derived->components))
3131 34031 : field_type = build_pointer_type (field_type);
3132 :
3133 113134 : if (c->attr.pointer || same_alloc_type)
3134 35020 : field_type = gfc_nonrestricted_type (field_type);
3135 :
3136 : /* vtype fields can point to different types to the base type. */
3137 113134 : if (c->ts.type == BT_DERIVED
3138 38298 : && c->ts.u.derived && c->ts.u.derived->attr.vtype)
3139 16800 : field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
3140 : ptr_mode, true);
3141 :
3142 113134 : field = gfc_add_field_to_struct (typenode,
3143 : get_identifier (c->name),
3144 : field_type, &chain);
3145 113134 : if (GFC_LOCUS_IS_SET (c->loc))
3146 113134 : gfc_set_decl_location (field, &c->loc);
3147 0 : else if (GFC_LOCUS_IS_SET (derived->declared_at))
3148 0 : gfc_set_decl_location (field, &derived->declared_at);
3149 :
3150 113134 : gfc_finish_decl_attrs (field, &c->attr);
3151 :
3152 113134 : DECL_PACKED (field) |= TYPE_PACKED (typenode);
3153 :
3154 113134 : gcc_assert (field);
3155 : /* Overwrite for class array to supply different bounds for different
3156 : types. */
3157 113134 : if (class_coarray_flag || !c->backend_decl || c->attr.caf_token)
3158 112064 : c->backend_decl = field;
3159 :
3160 113134 : if (c->attr.pointer && (c->attr.dimension || c->attr.codimension)
3161 2984 : && !(c->ts.type == BT_DERIVED && strcmp (c->name, "_data") == 0))
3162 1243 : GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
3163 : }
3164 :
3165 : /* Now lay out the derived type, including the fields. */
3166 37958 : if (canonical)
3167 370 : TYPE_CANONICAL (typenode) = canonical;
3168 :
3169 37958 : gfc_finish_type (typenode);
3170 37958 : gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
3171 37958 : if (derived->module && derived->ns->proc_name
3172 20854 : && derived->ns->proc_name->attr.flavor == FL_MODULE)
3173 : {
3174 19615 : if (derived->ns->proc_name->backend_decl
3175 19600 : && TREE_CODE (derived->ns->proc_name->backend_decl)
3176 : == NAMESPACE_DECL)
3177 : {
3178 19600 : TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
3179 19600 : DECL_CONTEXT (TYPE_STUB_DECL (typenode))
3180 39200 : = derived->ns->proc_name->backend_decl;
3181 : }
3182 : }
3183 :
3184 37958 : derived->backend_decl = typenode;
3185 :
3186 49256 : copy_derived_types:
3187 :
3188 49256 : if (!derived->attr.vtype)
3189 92825 : for (c = derived->components; c; c = c->next)
3190 : {
3191 : /* Do not add a caf_token field for class container components. */
3192 55777 : if (codimen && coarray_flag && !c->attr.dimension
3193 4 : && !c->attr.codimension && (c->attr.allocatable || c->attr.pointer)
3194 1 : && !derived->attr.is_class)
3195 : {
3196 : /* Provide sufficient space to hold "_caf_symbol". */
3197 1 : char caf_name[GFC_MAX_SYMBOL_LEN + 6];
3198 1 : gfc_component *token;
3199 1 : snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
3200 1 : token = gfc_find_component (derived, caf_name, true, true, NULL);
3201 1 : gcc_assert (token);
3202 1 : gfc_comp_caf_token (c) = token->backend_decl;
3203 1 : suppress_warning (gfc_comp_caf_token (c));
3204 : }
3205 : }
3206 :
3207 313588 : for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
3208 : {
3209 312573 : gfc_copy_dt_decls_ifequal (derived, dt, false);
3210 312573 : if (dt->dt_next == gfc_derived_types)
3211 : break;
3212 : }
3213 :
3214 49256 : if (derived->attr.is_class)
3215 10380 : GFC_CLASS_TYPE_P (derived->backend_decl) = 1;
3216 :
3217 49256 : return derived->backend_decl;
3218 : }
3219 :
3220 :
3221 : bool
3222 970333 : gfc_return_by_reference (gfc_symbol * sym)
3223 : {
3224 970333 : if (!sym->attr.function)
3225 : return 0;
3226 :
3227 488975 : if (sym->attr.dimension)
3228 : return 1;
3229 :
3230 414753 : if (sym->ts.type == BT_CHARACTER
3231 23433 : && !sym->attr.is_bind_c
3232 22731 : && (!sym->attr.result
3233 16 : || !sym->ns->proc_name
3234 16 : || !sym->ns->proc_name->attr.is_bind_c))
3235 : return 1;
3236 :
3237 : /* Possibly return complex numbers by reference for g77 compatibility.
3238 : We don't do this for calls to intrinsics (as the library uses the
3239 : -fno-f2c calling convention) except for calls to specific wrappers
3240 : (_gfortran_f2c_specific_*), nor for calls to functions which always
3241 : require an explicit interface, as no compatibility problems can
3242 : arise there. */
3243 392022 : if (flag_f2c && sym->ts.type == BT_COMPLEX
3244 1780 : && !sym->attr.pointer
3245 1330 : && !sym->attr.allocatable
3246 1168 : && !sym->attr.always_explicit)
3247 1012 : return 1;
3248 :
3249 : return 0;
3250 : }
3251 :
3252 : static tree
3253 214 : gfc_get_entry_result_type (gfc_symbol *sym)
3254 : {
3255 214 : tree type;
3256 :
3257 214 : type = gfc_sym_type (sym->result);
3258 :
3259 : /* Mixed ENTRY master unions must use the ABI return type of each entry.
3260 : Under -ff2c, default REAL entries return C double even though their
3261 : Fortran result symbol remains default REAL. */
3262 214 : if (flag_f2c
3263 2 : && sym->ts.type == BT_REAL
3264 1 : && sym->ts.kind == gfc_default_real_kind
3265 1 : && !sym->attr.pointer
3266 1 : && !sym->attr.allocatable
3267 1 : && !sym->attr.always_explicit)
3268 1 : type = gfc_get_real_type (gfc_default_double_kind);
3269 :
3270 214 : return type;
3271 : }
3272 :
3273 : static tree
3274 98 : gfc_get_mixed_entry_union (gfc_namespace *ns)
3275 : {
3276 98 : tree type;
3277 98 : tree *chain = NULL;
3278 98 : char name[GFC_MAX_SYMBOL_LEN + 1];
3279 98 : gfc_entry_list *el, *el2;
3280 :
3281 98 : gcc_assert (ns->proc_name->attr.mixed_entry_master);
3282 98 : gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
3283 :
3284 98 : snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
3285 :
3286 : /* Build the type node. */
3287 98 : type = make_node (UNION_TYPE);
3288 :
3289 98 : TYPE_NAME (type) = get_identifier (name);
3290 :
3291 312 : for (el = ns->entries; el; el = el->next)
3292 : {
3293 : /* Search for duplicates. */
3294 348 : for (el2 = ns->entries; el2 != el; el2 = el2->next)
3295 134 : if (el2->sym->result == el->sym->result)
3296 : break;
3297 :
3298 214 : if (el == el2)
3299 428 : gfc_add_field_to_struct_1 (type,
3300 214 : get_identifier (el->sym->result->name),
3301 : gfc_get_entry_result_type (el->sym),
3302 : &chain);
3303 : }
3304 :
3305 : /* Finish off the type. */
3306 98 : gfc_finish_type (type);
3307 98 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
3308 98 : return type;
3309 : }
3310 :
3311 : /* Create a "fn spec" based on the formal arguments;
3312 : cf. create_function_arglist. */
3313 :
3314 : static tree
3315 111425 : create_fn_spec (gfc_symbol *sym, tree fntype)
3316 : {
3317 111425 : char spec[150];
3318 111425 : size_t spec_len;
3319 111425 : gfc_formal_arglist *f;
3320 111425 : tree tmp;
3321 :
3322 111425 : memset (&spec, 0, sizeof (spec));
3323 111425 : spec[0] = '.';
3324 111425 : spec[1] = ' ';
3325 111425 : spec_len = 2;
3326 :
3327 111425 : if (sym->attr.entry_master)
3328 : {
3329 667 : spec[spec_len++] = 'R';
3330 667 : spec[spec_len++] = ' ';
3331 : }
3332 111425 : if (gfc_return_by_reference (sym))
3333 : {
3334 10690 : gfc_symbol *result = sym->result ? sym->result : sym;
3335 :
3336 10690 : if (result->attr.pointer || sym->attr.proc_pointer)
3337 : {
3338 352 : spec[spec_len++] = '.';
3339 352 : spec[spec_len++] = ' ';
3340 : }
3341 : else
3342 : {
3343 10338 : spec[spec_len++] = 'w';
3344 10338 : spec[spec_len++] = ' ';
3345 : }
3346 10690 : if (sym->ts.type == BT_CHARACTER)
3347 : {
3348 2924 : if (!sym->ts.u.cl->length
3349 1567 : && (sym->attr.allocatable || sym->attr.pointer))
3350 312 : spec[spec_len++] = 'w';
3351 : else
3352 2612 : spec[spec_len++] = 'R';
3353 2924 : spec[spec_len++] = ' ';
3354 : }
3355 : }
3356 :
3357 262267 : for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3358 150842 : if (spec_len < sizeof (spec))
3359 : {
3360 150842 : bool is_class = false;
3361 150842 : bool is_pointer = false;
3362 :
3363 150842 : if (f->sym)
3364 : {
3365 10161 : is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
3366 160899 : && f->sym->attr.class_ok;
3367 150738 : is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
3368 140577 : : f->sym->attr.pointer;
3369 : }
3370 :
3371 150842 : if (f->sym == NULL || is_pointer || f->sym->attr.target
3372 143486 : || f->sym->attr.external || f->sym->attr.cray_pointer
3373 142973 : || (f->sym->ts.type == BT_DERIVED
3374 27504 : && (f->sym->ts.u.derived->attr.proc_pointer_comp
3375 26841 : || f->sym->ts.u.derived->attr.pointer_comp))
3376 139960 : || (is_class
3377 9181 : && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3378 8520 : || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3379 138567 : || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3380 : {
3381 20427 : spec[spec_len++] = '.';
3382 20427 : spec[spec_len++] = ' ';
3383 : }
3384 130415 : else if (f->sym->attr.intent == INTENT_IN)
3385 : {
3386 62251 : spec[spec_len++] = 'r';
3387 62251 : spec[spec_len++] = ' ';
3388 : }
3389 68164 : else if (f->sym)
3390 : {
3391 68164 : spec[spec_len++] = 'w';
3392 68164 : spec[spec_len++] = ' ';
3393 : }
3394 : }
3395 :
3396 111425 : tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3397 111425 : tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3398 111425 : return build_type_attribute_variant (fntype, tmp);
3399 : }
3400 :
3401 :
3402 : /* NOTE: The returned function type must match the argument list created by
3403 : create_function_arglist. */
3404 :
3405 : tree
3406 113568 : gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3407 : const char *fnspec)
3408 : {
3409 113568 : tree type;
3410 113568 : vec<tree, va_gc> *typelist = NULL;
3411 113568 : vec<tree, va_gc> *hidden_typelist = NULL;
3412 113568 : gfc_formal_arglist *f;
3413 113568 : gfc_symbol *arg;
3414 113568 : int alternate_return = 0;
3415 113568 : bool is_varargs = true;
3416 :
3417 : /* Make sure this symbol is a function, a subroutine or the main
3418 : program. */
3419 113568 : gcc_assert (sym->attr.flavor == FL_PROCEDURE
3420 : || sym->attr.flavor == FL_PROGRAM);
3421 :
3422 : /* To avoid recursing infinitely on recursive types, we use error_mark_node
3423 : so that they can be detected here and handled further down. */
3424 113568 : if (sym->backend_decl == NULL)
3425 113311 : sym->backend_decl = error_mark_node;
3426 257 : else if (sym->backend_decl == error_mark_node)
3427 53 : goto arg_type_list_done;
3428 204 : else if (sym->attr.proc_pointer)
3429 0 : return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3430 : else
3431 204 : return TREE_TYPE (sym->backend_decl);
3432 :
3433 113311 : if (sym->attr.entry_master)
3434 : /* Additional parameter for selecting an entry point. */
3435 667 : vec_safe_push (typelist, gfc_array_index_type);
3436 :
3437 113311 : if (sym->result)
3438 34107 : arg = sym->result;
3439 : else
3440 : arg = sym;
3441 :
3442 113311 : if (arg->ts.type == BT_CHARACTER)
3443 3251 : gfc_conv_const_charlen (arg->ts.u.cl);
3444 :
3445 : /* Some functions we use an extra parameter for the return value. */
3446 113311 : if (gfc_return_by_reference (sym))
3447 : {
3448 12474 : type = gfc_sym_type (arg);
3449 12474 : if (arg->ts.type == BT_COMPLEX
3450 12031 : || arg->attr.dimension
3451 1685 : || arg->ts.type == BT_CHARACTER)
3452 12474 : type = build_reference_type (type);
3453 :
3454 12474 : vec_safe_push (typelist, type);
3455 12474 : if (arg->ts.type == BT_CHARACTER)
3456 : {
3457 3164 : if (!arg->ts.deferred)
3458 : /* Transfer by value. */
3459 2804 : vec_safe_push (typelist, gfc_charlen_type_node);
3460 : else
3461 : /* Deferred character lengths are transferred by reference
3462 : so that the value can be returned. */
3463 360 : vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3464 : }
3465 : }
3466 113311 : if (sym->backend_decl == error_mark_node && actual_args != NULL
3467 16144 : && sym->ts.interface == NULL
3468 16138 : && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3469 1118 : || sym->attr.proc == PROC_UNKNOWN))
3470 796 : gfc_get_formal_from_actual_arglist (sym, actual_args);
3471 :
3472 : /* Build the argument types for the function. */
3473 269801 : for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3474 : {
3475 156490 : arg = f->sym;
3476 156490 : if (arg)
3477 : {
3478 : /* Evaluate constant character lengths here so that they can be
3479 : included in the type. */
3480 156386 : if (arg->ts.type == BT_CHARACTER)
3481 12824 : gfc_conv_const_charlen (arg->ts.u.cl);
3482 :
3483 156386 : if (arg->attr.flavor == FL_PROCEDURE)
3484 : {
3485 1040 : type = gfc_get_function_type (arg);
3486 1040 : type = build_pointer_type (type);
3487 : }
3488 : else
3489 155346 : type = gfc_sym_type (arg, sym->attr.is_bind_c);
3490 :
3491 : /* Parameter Passing Convention
3492 :
3493 : We currently pass all parameters by reference.
3494 : Parameters with INTENT(IN) could be passed by value.
3495 : The problem arises if a function is called via an implicit
3496 : prototype. In this situation the INTENT is not known.
3497 : For this reason all parameters to global functions must be
3498 : passed by reference. Passing by value would potentially
3499 : generate bad code. Worse there would be no way of telling that
3500 : this code was bad, except that it would give incorrect results.
3501 :
3502 : Contained procedures could pass by value as these are never
3503 : used without an explicit interface, and cannot be passed as
3504 : actual parameters for a dummy procedure. */
3505 :
3506 156386 : vec_safe_push (typelist, type);
3507 : }
3508 : else
3509 : {
3510 104 : if (sym->attr.subroutine)
3511 156490 : alternate_return = 1;
3512 : }
3513 : }
3514 :
3515 : /* Add hidden arguments. */
3516 269801 : for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3517 : {
3518 156490 : arg = f->sym;
3519 : /* Add hidden string length parameters. */
3520 156490 : if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3521 : {
3522 10737 : if (!arg->ts.deferred)
3523 : /* Transfer by value. */
3524 9863 : type = gfc_charlen_type_node;
3525 : else
3526 : /* Deferred character lengths are transferred by reference
3527 : so that the value can be returned. */
3528 874 : type = build_pointer_type (gfc_charlen_type_node);
3529 :
3530 10737 : vec_safe_push (hidden_typelist, type);
3531 : }
3532 : /* For scalar intrinsic types or derived types, VALUE passes the value,
3533 : hence, the optional status cannot be transferred via a NULL pointer.
3534 : Thus, we will use a hidden argument in that case. */
3535 : if (arg
3536 156386 : && arg->attr.optional
3537 20255 : && arg->attr.value
3538 524 : && !arg->attr.dimension
3539 524 : && arg->ts.type != BT_CLASS)
3540 524 : vec_safe_push (typelist, boolean_type_node);
3541 : /* Coarrays which are descriptorless or assumed-shape pass with
3542 : -fcoarray=lib the token and the offset as hidden arguments. */
3543 628 : if (arg
3544 156386 : && flag_coarray == GFC_FCOARRAY_LIB
3545 7447 : && ((arg->ts.type != BT_CLASS
3546 7416 : && arg->attr.codimension
3547 1611 : && !arg->attr.allocatable)
3548 5864 : || (arg->ts.type == BT_CLASS
3549 31 : && CLASS_DATA (arg)->attr.codimension
3550 24 : && !CLASS_DATA (arg)->attr.allocatable)))
3551 : {
3552 1603 : vec_safe_push (hidden_typelist, pvoid_type_node); /* caf_token. */
3553 1603 : vec_safe_push (hidden_typelist, gfc_array_index_type); /* caf_offset. */
3554 : }
3555 : }
3556 :
3557 : /* Put hidden character length, caf_token, caf_offset at the end. */
3558 122159 : vec_safe_reserve (typelist, vec_safe_length (hidden_typelist));
3559 113311 : vec_safe_splice (typelist, hidden_typelist);
3560 :
3561 113311 : if (!vec_safe_is_empty (typelist)
3562 43770 : || sym->attr.is_main_program
3563 17096 : || sym->attr.if_source != IFSRC_UNKNOWN)
3564 : is_varargs = false;
3565 :
3566 113311 : if (sym->backend_decl == error_mark_node)
3567 113311 : sym->backend_decl = NULL_TREE;
3568 :
3569 113364 : arg_type_list_done:
3570 :
3571 113364 : if (alternate_return)
3572 74 : type = integer_type_node;
3573 113290 : else if (!sym->attr.function || gfc_return_by_reference (sym))
3574 91224 : type = void_type_node;
3575 22066 : else if (sym->attr.mixed_entry_master)
3576 98 : type = gfc_get_mixed_entry_union (sym->ns);
3577 21968 : else if (flag_f2c && sym->ts.type == BT_REAL
3578 389 : && sym->ts.kind == gfc_default_real_kind
3579 215 : && !sym->attr.pointer
3580 190 : && !sym->attr.allocatable
3581 172 : && !sym->attr.always_explicit)
3582 : {
3583 : /* Special case: f2c calling conventions require that (scalar)
3584 : default REAL functions return the C type double instead. f2c
3585 : compatibility is only an issue with functions that don't
3586 : require an explicit interface, as only these could be
3587 : implemented in Fortran 77. */
3588 172 : sym->ts.kind = gfc_default_double_kind;
3589 172 : type = gfc_typenode_for_spec (&sym->ts);
3590 172 : sym->ts.kind = gfc_default_real_kind;
3591 : }
3592 21796 : else if (sym->result && sym->result->attr.proc_pointer)
3593 : /* Procedure pointer return values. */
3594 : {
3595 497 : if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3596 : {
3597 : /* Unset proc_pointer as gfc_get_function_type
3598 : is called recursively. */
3599 166 : sym->result->attr.proc_pointer = 0;
3600 166 : type = build_pointer_type (gfc_get_function_type (sym->result));
3601 166 : sym->result->attr.proc_pointer = 1;
3602 : }
3603 : else
3604 331 : type = gfc_sym_type (sym->result);
3605 : }
3606 : else
3607 21299 : type = gfc_sym_type (sym);
3608 :
3609 113364 : if (is_varargs)
3610 : /* This should be represented as an unprototyped type, not a type
3611 : with (...) prototype. */
3612 1970 : type = build_function_type (type, NULL_TREE);
3613 : else
3614 250476 : type = build_function_type_vec (type, typelist);
3615 :
3616 : /* If we were passed an fn spec, add it here, otherwise determine it from
3617 : the formal arguments. */
3618 113364 : if (fnspec)
3619 : {
3620 1939 : tree tmp;
3621 1939 : int spec_len = strlen (fnspec);
3622 1939 : tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3623 1939 : tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3624 1939 : type = build_type_attribute_variant (type, tmp);
3625 : }
3626 : else
3627 111425 : type = create_fn_spec (sym, type);
3628 :
3629 113364 : return type;
3630 : }
3631 :
3632 : /* Language hooks for middle-end access to type nodes. */
3633 :
3634 : /* Return an integer type with BITS bits of precision,
3635 : that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3636 :
3637 : tree
3638 739414 : gfc_type_for_size (unsigned bits, int unsignedp)
3639 : {
3640 739414 : if (!unsignedp)
3641 : {
3642 : int i;
3643 464124 : for (i = 0; i <= MAX_INT_KINDS; ++i)
3644 : {
3645 464094 : tree type = gfc_integer_types[i];
3646 464094 : if (type && bits == TYPE_PRECISION (type))
3647 : return type;
3648 : }
3649 :
3650 : /* Handle TImode as a special case because it is used by some backends
3651 : (e.g. ARM) even though it is not available for normal use. */
3652 : #if HOST_BITS_PER_WIDE_INT >= 64
3653 30 : if (bits == TYPE_PRECISION (intTI_type_node))
3654 : return intTI_type_node;
3655 : #endif
3656 :
3657 30 : if (bits <= TYPE_PRECISION (intQI_type_node))
3658 : return intQI_type_node;
3659 0 : if (bits <= TYPE_PRECISION (intHI_type_node))
3660 : return intHI_type_node;
3661 0 : if (bits <= TYPE_PRECISION (intSI_type_node))
3662 : return intSI_type_node;
3663 0 : if (bits <= TYPE_PRECISION (intDI_type_node))
3664 : return intDI_type_node;
3665 0 : if (bits <= TYPE_PRECISION (intTI_type_node))
3666 0 : return intTI_type_node;
3667 : }
3668 : else
3669 : {
3670 604299 : if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3671 : return unsigned_intQI_type_node;
3672 571470 : if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3673 : return unsigned_intHI_type_node;
3674 539076 : if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3675 : return unsigned_intSI_type_node;
3676 497980 : if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3677 : return unsigned_intDI_type_node;
3678 32183 : if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3679 32183 : return unsigned_intTI_type_node;
3680 : }
3681 :
3682 : return NULL_TREE;
3683 : }
3684 :
3685 : /* Return a data type that has machine mode MODE. If the mode is an
3686 : integer, then UNSIGNEDP selects between signed and unsigned types. */
3687 :
3688 : tree
3689 755674 : gfc_type_for_mode (machine_mode mode, int unsignedp)
3690 : {
3691 755674 : int i;
3692 755674 : tree *base;
3693 755674 : scalar_int_mode int_mode;
3694 :
3695 755674 : if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3696 : base = gfc_real_types;
3697 747528 : else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3698 : base = gfc_complex_types;
3699 555034 : else if (is_a <scalar_int_mode> (mode, &int_mode))
3700 : {
3701 554542 : tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3702 554542 : return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3703 : }
3704 492 : else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3705 492 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3706 : {
3707 0 : unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
3708 : GET_MODE_NUNITS (mode));
3709 0 : tree bool_type = build_nonstandard_boolean_type (elem_bits);
3710 0 : return build_vector_type_for_mode (bool_type, mode);
3711 : }
3712 5 : else if (VECTOR_MODE_P (mode)
3713 65143 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3714 : {
3715 487 : machine_mode inner_mode = GET_MODE_INNER (mode);
3716 487 : tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3717 487 : if (inner_type != NULL_TREE)
3718 487 : return build_vector_type_for_mode (inner_type, mode);
3719 : return NULL_TREE;
3720 : }
3721 : else
3722 : return NULL_TREE;
3723 :
3724 786032 : for (i = 0; i <= MAX_REAL_KINDS; ++i)
3725 : {
3726 721874 : tree type = base[i];
3727 721874 : if (type && mode == TYPE_MODE (type))
3728 : return type;
3729 : }
3730 :
3731 : return NULL_TREE;
3732 : }
3733 :
3734 : /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3735 : in that case. */
3736 :
3737 : bool
3738 424289 : gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3739 : {
3740 424289 : int rank, dim;
3741 424289 : bool indirect = false;
3742 424289 : tree etype, ptype, t, base_decl;
3743 424289 : tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
3744 424289 : tree lower_suboff, upper_suboff, stride_suboff;
3745 424289 : tree dtype, field, rank_off;
3746 :
3747 424289 : if (! GFC_DESCRIPTOR_TYPE_P (type))
3748 : {
3749 270676 : if (! POINTER_TYPE_P (type))
3750 : return false;
3751 169386 : type = TREE_TYPE (type);
3752 169386 : if (! GFC_DESCRIPTOR_TYPE_P (type))
3753 : return false;
3754 : indirect = true;
3755 : }
3756 :
3757 300740 : rank = GFC_TYPE_ARRAY_RANK (type);
3758 300740 : if (rank >= (int) (ARRAY_SIZE (info->dimen)))
3759 : return false;
3760 :
3761 300740 : etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3762 300740 : gcc_assert (POINTER_TYPE_P (etype));
3763 300740 : etype = TREE_TYPE (etype);
3764 :
3765 : /* If the type is not a scalar coarray. */
3766 300740 : if (TREE_CODE (etype) == ARRAY_TYPE)
3767 300715 : etype = TREE_TYPE (etype);
3768 :
3769 : /* Can't handle variable sized elements yet. */
3770 300740 : if (int_size_in_bytes (etype) <= 0)
3771 : return false;
3772 : /* Nor non-constant lower bounds in assumed shape arrays. */
3773 279819 : if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3774 279819 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3775 : {
3776 84935 : for (dim = 0; dim < rank; dim++)
3777 52592 : if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3778 52592 : || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3779 : return false;
3780 : }
3781 :
3782 279577 : memset (info, '\0', sizeof (*info));
3783 279577 : info->ndimensions = rank;
3784 279577 : info->ordering = array_descr_ordering_column_major;
3785 279577 : info->element_type = etype;
3786 279577 : ptype = build_pointer_type (gfc_array_index_type);
3787 279577 : base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3788 279577 : if (!base_decl)
3789 : {
3790 406616 : base_decl = build_debug_expr_decl (indirect
3791 135531 : ? build_pointer_type (ptype) : ptype);
3792 271085 : GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3793 : }
3794 279577 : info->base_decl = base_decl;
3795 279577 : if (indirect)
3796 137113 : base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3797 :
3798 279577 : gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3799 : &dim_off, &dim_size, &stride_suboff,
3800 : &lower_suboff, &upper_suboff);
3801 :
3802 279577 : t = fold_build_pointer_plus (base_decl, span_off);
3803 279577 : elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3804 :
3805 279577 : t = base_decl;
3806 279577 : if (!integer_zerop (data_off))
3807 0 : t = fold_build_pointer_plus (t, data_off);
3808 279577 : t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3809 279577 : info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3810 279577 : enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
3811 279577 : if (akind == GFC_ARRAY_ALLOCATABLE
3812 279577 : || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE)
3813 34832 : info->allocated = build2 (NE_EXPR, logical_type_node,
3814 : info->data_location, null_pointer_node);
3815 244745 : else if (akind == GFC_ARRAY_POINTER
3816 244745 : || akind == GFC_ARRAY_POINTER_CONT
3817 244745 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
3818 227222 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
3819 17523 : info->associated = build2 (NE_EXPR, logical_type_node,
3820 : info->data_location, null_pointer_node);
3821 279577 : if ((akind == GFC_ARRAY_ASSUMED_RANK
3822 : || akind == GFC_ARRAY_ASSUMED_RANK_CONT
3823 : || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
3824 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
3825 279577 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
3826 15066 : && dwarf_version >= 5)
3827 : {
3828 15066 : rank = 1;
3829 15066 : info->ndimensions = 1;
3830 15066 : t = fold_build_pointer_plus (base_decl, dtype_off);
3831 15066 : dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3832 15066 : field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3833 15066 : rank_off = byte_position (field);
3834 15066 : t = fold_build_pointer_plus (t, rank_off);
3835 :
3836 15066 : t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
3837 15066 : t = build1 (INDIRECT_REF, TREE_TYPE (field), t);
3838 15066 : info->rank = t;
3839 15066 : t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3840 15066 : t = size_binop (MULT_EXPR, t, dim_size);
3841 15066 : dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3842 : }
3843 :
3844 692970 : for (dim = 0; dim < rank; dim++)
3845 : {
3846 413393 : t = fold_build_pointer_plus (base_decl,
3847 : size_binop (PLUS_EXPR,
3848 : dim_off, lower_suboff));
3849 413393 : t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3850 413393 : info->dimen[dim].lower_bound = t;
3851 413393 : t = fold_build_pointer_plus (base_decl,
3852 : size_binop (PLUS_EXPR,
3853 : dim_off, upper_suboff));
3854 413393 : t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3855 413393 : info->dimen[dim].upper_bound = t;
3856 413393 : if (akind == GFC_ARRAY_ASSUMED_SHAPE
3857 413393 : || akind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3858 : {
3859 : /* Assumed shape arrays have known lower bounds. */
3860 52350 : info->dimen[dim].upper_bound
3861 52350 : = build2 (MINUS_EXPR, gfc_array_index_type,
3862 : info->dimen[dim].upper_bound,
3863 : info->dimen[dim].lower_bound);
3864 52350 : info->dimen[dim].lower_bound
3865 52350 : = fold_convert (gfc_array_index_type,
3866 : GFC_TYPE_ARRAY_LBOUND (type, dim));
3867 52350 : info->dimen[dim].upper_bound
3868 52350 : = build2 (PLUS_EXPR, gfc_array_index_type,
3869 : info->dimen[dim].lower_bound,
3870 : info->dimen[dim].upper_bound);
3871 : }
3872 413393 : t = fold_build_pointer_plus (base_decl,
3873 : size_binop (PLUS_EXPR,
3874 : dim_off, stride_suboff));
3875 413393 : t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3876 413393 : t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3877 413393 : info->dimen[dim].stride = t;
3878 413393 : if (dim + 1 < rank)
3879 133841 : dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3880 : }
3881 :
3882 : return true;
3883 : }
3884 :
3885 :
3886 : /* Create a type to handle vector subscripts for coarray library calls. It
3887 : has the form:
3888 : struct caf_vector_t {
3889 : size_t nvec; // size of the vector
3890 : union {
3891 : struct {
3892 : void *vector;
3893 : int kind;
3894 : } v;
3895 : struct {
3896 : ptrdiff_t lower_bound;
3897 : ptrdiff_t upper_bound;
3898 : ptrdiff_t stride;
3899 : } triplet;
3900 : } u;
3901 : }
3902 : where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3903 : size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3904 :
3905 : tree
3906 0 : gfc_get_caf_vector_type (int dim)
3907 : {
3908 0 : static tree vector_types[GFC_MAX_DIMENSIONS];
3909 0 : static tree vec_type = NULL_TREE;
3910 0 : tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3911 :
3912 0 : if (vector_types[dim-1] != NULL_TREE)
3913 : return vector_types[dim-1];
3914 :
3915 0 : if (vec_type == NULL_TREE)
3916 : {
3917 0 : chain = 0;
3918 0 : vect_struct_type = make_node (RECORD_TYPE);
3919 0 : tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3920 : get_identifier ("vector"),
3921 : pvoid_type_node, &chain);
3922 0 : suppress_warning (tmp);
3923 0 : tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3924 : get_identifier ("kind"),
3925 : integer_type_node, &chain);
3926 0 : suppress_warning (tmp);
3927 0 : gfc_finish_type (vect_struct_type);
3928 :
3929 0 : chain = 0;
3930 0 : triplet_struct_type = make_node (RECORD_TYPE);
3931 0 : tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3932 : get_identifier ("lower_bound"),
3933 : gfc_array_index_type, &chain);
3934 0 : suppress_warning (tmp);
3935 0 : tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3936 : get_identifier ("upper_bound"),
3937 : gfc_array_index_type, &chain);
3938 0 : suppress_warning (tmp);
3939 0 : tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3940 : gfc_array_index_type, &chain);
3941 0 : suppress_warning (tmp);
3942 0 : gfc_finish_type (triplet_struct_type);
3943 :
3944 0 : chain = 0;
3945 0 : union_type = make_node (UNION_TYPE);
3946 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3947 : vect_struct_type, &chain);
3948 0 : suppress_warning (tmp);
3949 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3950 : triplet_struct_type, &chain);
3951 0 : suppress_warning (tmp);
3952 0 : gfc_finish_type (union_type);
3953 :
3954 0 : chain = 0;
3955 0 : vec_type = make_node (RECORD_TYPE);
3956 0 : tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3957 : size_type_node, &chain);
3958 0 : suppress_warning (tmp);
3959 0 : tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3960 : union_type, &chain);
3961 0 : suppress_warning (tmp);
3962 0 : gfc_finish_type (vec_type);
3963 0 : TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3964 : }
3965 :
3966 0 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3967 : gfc_rank_cst[dim-1]);
3968 0 : vector_types[dim-1] = build_array_type (vec_type, tmp);
3969 0 : return vector_types[dim-1];
3970 : }
3971 :
3972 :
3973 : tree
3974 0 : gfc_get_caf_reference_type ()
3975 : {
3976 0 : static tree reference_type = NULL_TREE;
3977 0 : tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3978 : a_struct_type, u_union_type, tmp, *chain;
3979 :
3980 0 : if (reference_type != NULL_TREE)
3981 : return reference_type;
3982 :
3983 0 : chain = 0;
3984 0 : c_struct_type = make_node (RECORD_TYPE);
3985 0 : tmp = gfc_add_field_to_struct_1 (c_struct_type,
3986 : get_identifier ("offset"),
3987 : gfc_array_index_type, &chain);
3988 0 : suppress_warning (tmp);
3989 0 : tmp = gfc_add_field_to_struct_1 (c_struct_type,
3990 : get_identifier ("caf_token_offset"),
3991 : gfc_array_index_type, &chain);
3992 0 : suppress_warning (tmp);
3993 0 : gfc_finish_type (c_struct_type);
3994 :
3995 0 : chain = 0;
3996 0 : s_struct_type = make_node (RECORD_TYPE);
3997 0 : tmp = gfc_add_field_to_struct_1 (s_struct_type,
3998 : get_identifier ("start"),
3999 : gfc_array_index_type, &chain);
4000 0 : suppress_warning (tmp);
4001 0 : tmp = gfc_add_field_to_struct_1 (s_struct_type,
4002 : get_identifier ("end"),
4003 : gfc_array_index_type, &chain);
4004 0 : suppress_warning (tmp);
4005 0 : tmp = gfc_add_field_to_struct_1 (s_struct_type,
4006 : get_identifier ("stride"),
4007 : gfc_array_index_type, &chain);
4008 0 : suppress_warning (tmp);
4009 0 : gfc_finish_type (s_struct_type);
4010 :
4011 0 : chain = 0;
4012 0 : v_struct_type = make_node (RECORD_TYPE);
4013 0 : tmp = gfc_add_field_to_struct_1 (v_struct_type,
4014 : get_identifier ("vector"),
4015 : pvoid_type_node, &chain);
4016 0 : suppress_warning (tmp);
4017 0 : tmp = gfc_add_field_to_struct_1 (v_struct_type,
4018 : get_identifier ("nvec"),
4019 : size_type_node, &chain);
4020 0 : suppress_warning (tmp);
4021 0 : tmp = gfc_add_field_to_struct_1 (v_struct_type,
4022 : get_identifier ("kind"),
4023 : integer_type_node, &chain);
4024 0 : suppress_warning (tmp);
4025 0 : gfc_finish_type (v_struct_type);
4026 :
4027 0 : chain = 0;
4028 0 : union_type = make_node (UNION_TYPE);
4029 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
4030 : s_struct_type, &chain);
4031 0 : suppress_warning (tmp);
4032 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
4033 : v_struct_type, &chain);
4034 0 : suppress_warning (tmp);
4035 0 : gfc_finish_type (union_type);
4036 :
4037 0 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4038 : gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
4039 0 : dim_union_type = build_array_type (union_type, tmp);
4040 :
4041 0 : chain = 0;
4042 0 : a_struct_type = make_node (RECORD_TYPE);
4043 0 : tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
4044 : build_array_type (unsigned_char_type_node,
4045 : build_range_type (gfc_array_index_type,
4046 : gfc_index_zero_node,
4047 : gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
4048 : &chain);
4049 0 : suppress_warning (tmp);
4050 0 : tmp = gfc_add_field_to_struct_1 (a_struct_type,
4051 : get_identifier ("static_array_type"),
4052 : integer_type_node, &chain);
4053 0 : suppress_warning (tmp);
4054 0 : tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
4055 : dim_union_type, &chain);
4056 0 : suppress_warning (tmp);
4057 0 : gfc_finish_type (a_struct_type);
4058 :
4059 0 : chain = 0;
4060 0 : u_union_type = make_node (UNION_TYPE);
4061 0 : tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
4062 : c_struct_type, &chain);
4063 0 : suppress_warning (tmp);
4064 0 : tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
4065 : a_struct_type, &chain);
4066 0 : suppress_warning (tmp);
4067 0 : gfc_finish_type (u_union_type);
4068 :
4069 0 : chain = 0;
4070 0 : reference_type = make_node (RECORD_TYPE);
4071 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
4072 : build_pointer_type (reference_type), &chain);
4073 0 : suppress_warning (tmp);
4074 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
4075 : integer_type_node, &chain);
4076 0 : suppress_warning (tmp);
4077 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
4078 : size_type_node, &chain);
4079 0 : suppress_warning (tmp);
4080 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
4081 : u_union_type, &chain);
4082 0 : suppress_warning (tmp);
4083 0 : gfc_finish_type (reference_type);
4084 0 : TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
4085 :
4086 0 : return reference_type;
4087 : }
4088 :
4089 : static tree
4090 1337 : gfc_get_cfi_dim_type ()
4091 : {
4092 1337 : static tree CFI_dim_t = NULL;
4093 :
4094 1337 : if (CFI_dim_t)
4095 : return CFI_dim_t;
4096 :
4097 638 : CFI_dim_t = make_node (RECORD_TYPE);
4098 638 : TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
4099 638 : TYPE_NAMELESS (CFI_dim_t) = 1;
4100 638 : tree field;
4101 638 : tree *chain = NULL;
4102 638 : field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
4103 : gfc_array_index_type, &chain);
4104 638 : suppress_warning (field);
4105 638 : field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
4106 : gfc_array_index_type, &chain);
4107 638 : suppress_warning (field);
4108 638 : field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
4109 : gfc_array_index_type, &chain);
4110 638 : suppress_warning (field);
4111 638 : gfc_finish_type (CFI_dim_t);
4112 638 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
4113 638 : return CFI_dim_t;
4114 : }
4115 :
4116 :
4117 : /* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
4118 : otherwise dim[dimen] is used. */
4119 :
4120 : tree
4121 12516 : gfc_get_cfi_type (int dimen, bool restricted)
4122 : {
4123 12516 : gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
4124 :
4125 12516 : int idx = 2*(dimen + 1) + restricted;
4126 :
4127 12516 : if (gfc_cfi_descriptor_base[idx])
4128 : return gfc_cfi_descriptor_base[idx];
4129 :
4130 : /* Build the type node. */
4131 1517 : tree CFI_cdesc_t = make_node (RECORD_TYPE);
4132 1517 : char name[GFC_MAX_SYMBOL_LEN + 1];
4133 1517 : if (dimen != -1)
4134 960 : sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
4135 1517 : TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
4136 1517 : TYPE_NAMELESS (CFI_cdesc_t) = 1;
4137 :
4138 1517 : tree field;
4139 1517 : tree *chain = NULL;
4140 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
4141 : (restricted ? prvoid_type_node
4142 : : ptr_type_node), &chain);
4143 1517 : suppress_warning (field);
4144 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
4145 : size_type_node, &chain);
4146 1517 : suppress_warning (field);
4147 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
4148 : integer_type_node, &chain);
4149 1517 : suppress_warning (field);
4150 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
4151 : signed_char_type_node, &chain);
4152 1517 : suppress_warning (field);
4153 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
4154 : signed_char_type_node, &chain);
4155 1517 : suppress_warning (field);
4156 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
4157 : get_typenode_from_name (INT16_TYPE),
4158 : &chain);
4159 1517 : suppress_warning (field);
4160 :
4161 1517 : if (dimen != 0)
4162 : {
4163 1337 : tree range = NULL_TREE;
4164 1337 : if (dimen > 0)
4165 780 : range = gfc_rank_cst[dimen - 1];
4166 1337 : range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4167 : range);
4168 1337 : tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
4169 1337 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
4170 : CFI_dim_t, &chain);
4171 1337 : suppress_warning (field);
4172 : }
4173 :
4174 1517 : TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
4175 1517 : gfc_finish_type (CFI_cdesc_t);
4176 1517 : gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
4177 1517 : return CFI_cdesc_t;
4178 : }
4179 :
4180 : #include "gt-fortran-trans-types.h"
|