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 435466 : tree get_dtype_type_node (void)
148 : {
149 435466 : tree field;
150 435466 : tree dtype_node;
151 435466 : tree *dtype_chain = NULL;
152 :
153 435466 : if (dtype_type_node == NULL_TREE)
154 : {
155 32673 : dtype_node = make_node (RECORD_TYPE);
156 32673 : TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
157 32673 : TYPE_NAMELESS (dtype_node) = 1;
158 32673 : field = gfc_add_field_to_struct_1 (dtype_node,
159 : get_identifier ("elem_len"),
160 : size_type_node, &dtype_chain);
161 32673 : suppress_warning (field);
162 32673 : field = gfc_add_field_to_struct_1 (dtype_node,
163 : get_identifier ("version"),
164 : integer_type_node, &dtype_chain);
165 32673 : suppress_warning (field);
166 32673 : field = gfc_add_field_to_struct_1 (dtype_node,
167 : get_identifier ("rank"),
168 : gfc_array_dim_rank_type, &dtype_chain);
169 32673 : suppress_warning (field);
170 32673 : field = gfc_add_field_to_struct_1 (dtype_node,
171 : get_identifier ("type"),
172 : signed_char_type_node, &dtype_chain);
173 32673 : suppress_warning (field);
174 32673 : field = gfc_add_field_to_struct_1 (dtype_node,
175 : get_identifier ("attribute"),
176 : short_integer_type_node, &dtype_chain);
177 32673 : suppress_warning (field);
178 32673 : gfc_finish_type (dtype_node);
179 32673 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
180 32673 : dtype_type_node = dtype_node;
181 : }
182 435466 : return dtype_type_node;
183 : }
184 :
185 : static int
186 261520 : get_real_kind_from_node (tree type)
187 : {
188 261520 : int i;
189 :
190 653800 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
191 653800 : 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 849940 : get_int_kind_from_node (tree type)
213 : {
214 849940 : int i;
215 :
216 849940 : if (!type)
217 : return -2;
218 :
219 2741052 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
220 2741052 : 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 523040 : get_int_kind_from_name (const char *name)
228 : {
229 523040 : return get_int_kind_from_node (get_typenode_from_name (name));
230 : }
231 :
232 : static int
233 555730 : get_unsigned_kind_from_node (tree type)
234 : {
235 555730 : int i;
236 :
237 555730 : if (!type)
238 : return -2;
239 :
240 563728 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
241 12384 : 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 424970 : get_uint_kind_from_name (const char *name)
249 : {
250 424970 : 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 5632 : gfc_get_int_kind_from_width_isofortranenv (int size)
258 : {
259 5632 : int i;
260 :
261 : /* Look for a kind with matching storage size. */
262 14080 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
263 14080 : 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 2816 : gfc_get_uint_kind_from_width_isofortranenv (int size)
278 : {
279 2816 : int i;
280 :
281 : /* Look for a kind with matching storage size. */
282 2966 : 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 2716 : 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 2816 : gfc_get_real_kind_from_width_isofortranenv (int size)
302 : {
303 2816 : int digits, i, kind;
304 :
305 2816 : size /= 8;
306 :
307 2816 : kind = -1;
308 2816 : digits = 0;
309 :
310 : /* Look for a kind with matching storage size. */
311 14080 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
312 11264 : if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
313 : {
314 2812 : if (gfc_real_kinds[i].digits > digits)
315 : {
316 2812 : digits = gfc_real_kinds[i].digits;
317 2812 : kind = gfc_real_kinds[i].kind;
318 : }
319 : }
320 :
321 2816 : if (kind != -1)
322 : return kind;
323 :
324 : /* Look for a kind with larger storage size. */
325 3520 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
326 2816 : if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
327 2816 : kind = -2;
328 :
329 : return kind;
330 : }
331 :
332 :
333 :
334 : static int
335 163450 : get_int_kind_from_width (int size)
336 : {
337 163450 : int i;
338 :
339 490350 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
340 489532 : 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 32690 : get_int_kind_from_minimal_width (int size)
348 : {
349 32690 : int i;
350 :
351 163450 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
352 163041 : 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 98070 : get_uint_kind_from_width (int size)
360 : {
361 98070 : int i;
362 :
363 101166 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
364 3870 : if (gfc_integer_kinds[i].bit_size == size)
365 774 : 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 32690 : gfc_init_c_interop_kinds (void)
376 : {
377 32690 : int i;
378 :
379 : /* init all pointers in the list to NULL */
380 2484440 : for (i = 0; i < ISOCBINDING_NUMBER; i++)
381 : {
382 : /* Initialize the name and value fields. */
383 2451750 : c_interop_kinds_table[i].name[0] = '\0';
384 2451750 : c_interop_kinds_table[i].value = -100;
385 2451750 : 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 32690 : }
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 32690 : gfc_init_kinds (void)
437 : {
438 32690 : opt_scalar_int_mode int_mode_iter;
439 32690 : opt_scalar_float_mode float_mode_iter;
440 32690 : int i_index, r_index, kind;
441 32690 : bool saw_i4 = false, saw_i8 = false;
442 32690 : bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
443 32690 : scalar_mode r16_mode = QImode;
444 32690 : scalar_mode composite_mode = QImode;
445 :
446 32690 : i_index = 0;
447 261520 : FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
448 : {
449 228830 : scalar_int_mode mode = int_mode_iter.require ();
450 228830 : int kind, bitsize;
451 :
452 228830 : if (!targetm.scalar_mode_supported_p (mode))
453 228830 : 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 163041 : bitsize = GET_MODE_BITSIZE (mode);
459 163041 : if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
460 0 : continue;
461 :
462 163041 : 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 163041 : kind = bitsize / 8;
467 :
468 163041 : if (kind == 4)
469 : saw_i4 = true;
470 130351 : if (kind == 8)
471 32690 : saw_i8 = true;
472 :
473 163041 : gfc_integer_kinds[i_index].kind = kind;
474 163041 : gfc_integer_kinds[i_index].radix = 2;
475 163041 : gfc_integer_kinds[i_index].digits = bitsize - 1;
476 163041 : gfc_integer_kinds[i_index].bit_size = bitsize;
477 :
478 163041 : if (flag_unsigned)
479 : {
480 1290 : gfc_unsigned_kinds[i_index].kind = kind;
481 1290 : gfc_unsigned_kinds[i_index].radix = 2;
482 1290 : gfc_unsigned_kinds[i_index].digits = bitsize;
483 1290 : gfc_unsigned_kinds[i_index].bit_size = bitsize;
484 : }
485 :
486 163041 : gfc_logical_kinds[i_index].kind = kind;
487 163041 : gfc_logical_kinds[i_index].bit_size = bitsize;
488 :
489 163041 : 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 32690 : 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 32690 : gcc_assert(saw_i4);
502 :
503 : /* Set the maximum integer kind. Used with at least BOZ constants. */
504 32690 : gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
505 :
506 32690 : r_index = 0;
507 228830 : FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
508 : {
509 196140 : scalar_float_mode mode = float_mode_iter.require ();
510 196140 : const struct real_format *fmt = REAL_MODE_FORMAT (mode);
511 196140 : int kind;
512 :
513 196140 : if (fmt == NULL)
514 196140 : continue;
515 196140 : if (!targetm.scalar_mode_supported_p (mode))
516 0 : continue;
517 :
518 1372980 : 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 196140 : if (!targetm.libgcc_floating_mode_supported_p (mode))
526 0 : continue;
527 196140 : if (mode != TYPE_MODE (float_type_node)
528 163450 : && (mode != TYPE_MODE (double_type_node))
529 130760 : && (mode != TYPE_MODE (long_double_type_node))
530 : #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
531 294210 : && (mode != TFmode)
532 : #endif
533 : )
534 65380 : 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 130760 : kind = (GET_MODE_PRECISION (mode) + 7) / 8;
557 :
558 130760 : if (kind == 4)
559 : saw_r4 = true;
560 98070 : if (kind == 8)
561 : saw_r8 = true;
562 98070 : if (kind == 10)
563 : saw_r10 = true;
564 98070 : if (kind == 16)
565 : {
566 32690 : saw_r16 = true;
567 32690 : r16_mode = mode;
568 : }
569 :
570 : /* Careful we don't stumble a weird internal mode. */
571 130760 : gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
572 : /* Or have too many modes for the allocated space. */
573 98070 : gcc_assert (r_index != MAX_REAL_KINDS);
574 :
575 130760 : gfc_real_kinds[r_index].kind = kind;
576 130760 : gfc_real_kinds[r_index].abi_kind = kind;
577 130760 : gfc_real_kinds[r_index].radix = fmt->b;
578 130760 : gfc_real_kinds[r_index].digits = fmt->p;
579 130760 : gfc_real_kinds[r_index].min_exponent = fmt->emin;
580 130760 : gfc_real_kinds[r_index].max_exponent = fmt->emax;
581 130760 : 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 130760 : gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
591 130760 : 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 32690 : 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 32690 : 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 32690 : gfc_numeric_storage_size = 4 * 8;
630 :
631 32690 : 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 32599 : 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 32599 : else if (saw_i4)
649 : {
650 32599 : 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 32690 : gfc_default_unsigned_kind = gfc_default_integer_kind;
659 :
660 : /* Choose the default real kind. Again, we choose 4 when possible. */
661 32690 : 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 32688 : 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 32682 : 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 32676 : 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 32652 : 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 32628 : 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 32604 : else if (saw_r4)
710 32604 : 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 32690 : if (flag_default_double && saw_r8)
719 0 : gfc_default_double_kind = 8;
720 32690 : 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 32676 : 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 32652 : 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 32628 : 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 32604 : else if (saw_r4 && saw_r8)
757 32604 : 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 32690 : gfc_default_logical_kind = gfc_default_integer_kind;
778 32690 : 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 32690 : i_index = 0;
784 65380 : if ((kind = get_int_kind_from_width (8)) > 0)
785 : {
786 32690 : gfc_character_kinds[i_index].kind = kind;
787 32690 : gfc_character_kinds[i_index].bit_size = 8;
788 32690 : gfc_character_kinds[i_index].name = "ascii";
789 32690 : i_index++;
790 : }
791 65380 : if ((kind = get_int_kind_from_width (32)) > 0)
792 : {
793 32690 : gfc_character_kinds[i_index].kind = kind;
794 32690 : gfc_character_kinds[i_index].bit_size = 32;
795 32690 : gfc_character_kinds[i_index].name = "iso_10646";
796 32690 : i_index++;
797 : }
798 :
799 : /* Choose the smallest integer kind for our default character. */
800 32690 : gfc_default_character_kind = gfc_character_kinds[0].kind;
801 32690 : gfc_character_storage_size = gfc_default_character_kind * 8;
802 :
803 33099 : gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
804 :
805 32690 : 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 32690 : gfc_c_int_kind = INT_TYPE_SIZE / 8;
810 :
811 : /* UNSIGNED has the same as INT. */
812 32690 : gfc_c_uint_kind = gfc_c_int_kind;
813 :
814 : /* Choose atomic kinds to match C's int. */
815 32690 : gfc_atomic_int_kind = gfc_c_int_kind;
816 32690 : gfc_atomic_logical_kind = gfc_c_int_kind;
817 :
818 32690 : gfc_c_intptr_kind = POINTER_SIZE / 8;
819 32690 : }
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 83388502 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
831 83386435 : 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 4565760 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
843 1648410 : 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 5454117 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
855 5454106 : 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 2002565 : for (i = 0; gfc_logical_kinds[i].kind; i++)
867 2002557 : 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 1433534 : for (i = 0; gfc_character_kinds[i].kind; i++)
879 1433520 : 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 35082494 : gfc_validate_kind (bt type, int kind, bool may_fail)
891 : {
892 35082494 : int rc;
893 :
894 35082494 : switch (type)
895 : {
896 : case BT_REAL: /* Fall through */
897 : case BT_COMPLEX:
898 35082494 : rc = validate_real (kind);
899 : break;
900 : case BT_INTEGER:
901 35082494 : rc = validate_integer (kind);
902 : break;
903 : case BT_UNSIGNED:
904 35082494 : rc = validate_unsigned (kind);
905 : break;
906 : case BT_LOGICAL:
907 35082494 : rc = validate_logical (kind);
908 : break;
909 : case BT_CHARACTER:
910 35082494 : rc = validate_character (kind);
911 : break;
912 :
913 0 : default:
914 0 : gfc_internal_error ("gfc_validate_kind(): Got bad type");
915 : }
916 :
917 35082494 : if (rc < 0 && !may_fail)
918 0 : gfc_internal_error ("gfc_validate_kind(): Got bad kind");
919 :
920 35082494 : 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 163041 : gfc_build_int_type (gfc_integer_info *info)
931 : {
932 163041 : int mode_precision = info->bit_size;
933 :
934 163041 : if (mode_precision == CHAR_TYPE_SIZE)
935 32690 : info->c_char = 1;
936 163041 : if (mode_precision == SHORT_TYPE_SIZE)
937 32690 : info->c_short = 1;
938 163041 : if (mode_precision == INT_TYPE_SIZE)
939 32690 : info->c_int = 1;
940 164677 : if (mode_precision == LONG_TYPE_SIZE)
941 32690 : info->c_long = 1;
942 163041 : if (mode_precision == LONG_LONG_TYPE_SIZE)
943 32690 : info->c_long_long = 1;
944 :
945 163041 : if (TYPE_PRECISION (intQI_type_node) == mode_precision)
946 : return intQI_type_node;
947 130351 : if (TYPE_PRECISION (intHI_type_node) == mode_precision)
948 : return intHI_type_node;
949 97661 : if (TYPE_PRECISION (intSI_type_node) == mode_precision)
950 : return intSI_type_node;
951 64971 : if (TYPE_PRECISION (intDI_type_node) == mode_precision)
952 : return intDI_type_node;
953 32281 : 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 67584 : gfc_build_uint_type (int size)
961 : {
962 67584 : if (size == CHAR_TYPE_SIZE)
963 32960 : return unsigned_char_type_node;
964 34624 : if (size == SHORT_TYPE_SIZE)
965 528 : return short_unsigned_type_node;
966 34096 : if (size == INT_TYPE_SIZE)
967 33068 : return unsigned_type_node;
968 1028 : if (size == LONG_TYPE_SIZE)
969 528 : return long_unsigned_type_node;
970 500 : if (size == LONG_LONG_TYPE_SIZE)
971 0 : return long_long_unsigned_type_node;
972 :
973 500 : return make_unsigned_type (size);
974 : }
975 :
976 : static tree
977 774 : gfc_build_unsigned_type (gfc_unsigned_info *info)
978 : {
979 774 : int mode_precision = info->bit_size;
980 :
981 774 : if (mode_precision == CHAR_TYPE_SIZE)
982 0 : info->c_unsigned_char = 1;
983 774 : if (mode_precision == SHORT_TYPE_SIZE)
984 258 : info->c_unsigned_short = 1;
985 774 : if (mode_precision == INT_TYPE_SIZE)
986 0 : info->c_unsigned_int = 1;
987 774 : if (mode_precision == LONG_TYPE_SIZE)
988 258 : info->c_unsigned_long = 1;
989 774 : if (mode_precision == LONG_LONG_TYPE_SIZE)
990 258 : info->c_unsigned_long_long = 1;
991 :
992 774 : return gfc_build_uint_type (mode_precision);
993 : }
994 :
995 : static tree
996 130760 : gfc_build_real_type (gfc_real_info *info)
997 : {
998 130760 : int mode_precision = info->mode_precision;
999 130760 : tree new_type;
1000 :
1001 130760 : if (mode_precision == TYPE_PRECISION (float_type_node))
1002 32690 : info->c_float = 1;
1003 130760 : if (mode_precision == TYPE_PRECISION (double_type_node))
1004 32690 : info->c_double = 1;
1005 130760 : if (mode_precision == TYPE_PRECISION (long_double_type_node)
1006 130760 : && !info->c_float128)
1007 32690 : info->c_long_double = 1;
1008 130760 : if (mode_precision != TYPE_PRECISION (long_double_type_node)
1009 130760 : && mode_precision == 128)
1010 : {
1011 : /* TODO: see PR101835. */
1012 32690 : info->c_float128 = 1;
1013 32690 : gfc_real16_is_float128 = true;
1014 32690 : if (TARGET_GLIBC_MAJOR > 2
1015 : || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26))
1016 : {
1017 32690 : info->use_iec_60559 = 1;
1018 32690 : gfc_real16_use_iec_60559 = true;
1019 : }
1020 : }
1021 :
1022 130760 : if (TYPE_PRECISION (float_type_node) == mode_precision)
1023 : return float_type_node;
1024 98070 : if (TYPE_PRECISION (double_type_node) == mode_precision)
1025 : return double_type_node;
1026 65380 : if (TYPE_PRECISION (long_double_type_node) == mode_precision)
1027 : return long_double_type_node;
1028 :
1029 32690 : new_type = make_node (REAL_TYPE);
1030 32690 : TYPE_PRECISION (new_type) = mode_precision;
1031 32690 : layout_type (new_type);
1032 32690 : return new_type;
1033 : }
1034 :
1035 : static tree
1036 130760 : gfc_build_complex_type (tree scalar_type)
1037 : {
1038 130760 : tree new_type;
1039 :
1040 130760 : if (scalar_type == NULL)
1041 : return NULL;
1042 130760 : if (scalar_type == float_type_node)
1043 32690 : return complex_float_type_node;
1044 98070 : if (scalar_type == double_type_node)
1045 32690 : return complex_double_type_node;
1046 65380 : if (scalar_type == long_double_type_node)
1047 32690 : return complex_long_double_type_node;
1048 :
1049 32690 : new_type = make_node (COMPLEX_TYPE);
1050 32690 : TREE_TYPE (new_type) = scalar_type;
1051 32690 : layout_type (new_type);
1052 32690 : return new_type;
1053 : }
1054 :
1055 : static tree
1056 163041 : gfc_build_logical_type (gfc_logical_info *info)
1057 : {
1058 163041 : int bit_size = info->bit_size;
1059 163041 : tree new_type;
1060 :
1061 163041 : if (bit_size == BOOL_TYPE_SIZE)
1062 : {
1063 32690 : info->c_bool = 1;
1064 32690 : return boolean_type_node;
1065 : }
1066 :
1067 130351 : new_type = make_unsigned_type (bit_size);
1068 130351 : TREE_SET_CODE (new_type, BOOLEAN_TYPE);
1069 130351 : TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
1070 130351 : TYPE_PRECISION (new_type) = 1;
1071 :
1072 130351 : 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 32690 : gfc_init_types (void)
1083 : {
1084 32690 : char name_buf[26];
1085 32690 : int index;
1086 32690 : tree type;
1087 32690 : 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 195731 : for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
1095 : {
1096 163041 : type = gfc_build_int_type (&gfc_integer_kinds[index]);
1097 : /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
1098 163041 : if (TYPE_STRING_FLAG (type))
1099 32690 : type = make_signed_type (gfc_integer_kinds[index].bit_size);
1100 163041 : gfc_integer_types[index] = type;
1101 163041 : snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
1102 : gfc_integer_kinds[index].kind);
1103 163041 : PUSH_TYPE (name_buf, type);
1104 : }
1105 :
1106 195731 : for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
1107 : {
1108 163041 : type = gfc_build_logical_type (&gfc_logical_kinds[index]);
1109 163041 : gfc_logical_types[index] = type;
1110 163041 : snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
1111 : gfc_logical_kinds[index].kind);
1112 163041 : PUSH_TYPE (name_buf, type);
1113 : }
1114 :
1115 163450 : for (index = 0; gfc_real_kinds[index].kind != 0; index++)
1116 : {
1117 130760 : type = gfc_build_real_type (&gfc_real_kinds[index]);
1118 130760 : gfc_real_types[index] = type;
1119 130760 : snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
1120 : gfc_real_kinds[index].kind);
1121 130760 : PUSH_TYPE (name_buf, type);
1122 :
1123 130760 : if (gfc_real_kinds[index].c_float128)
1124 32690 : gfc_float128_type_node = type;
1125 :
1126 130760 : type = gfc_build_complex_type (type);
1127 130760 : gfc_complex_types[index] = type;
1128 130760 : snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
1129 : gfc_real_kinds[index].kind);
1130 130760 : PUSH_TYPE (name_buf, type);
1131 :
1132 130760 : if (gfc_real_kinds[index].c_float128)
1133 32690 : gfc_complex_float128_type_node = type;
1134 : }
1135 :
1136 98070 : for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
1137 : {
1138 65380 : type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
1139 65380 : type = build_qualified_type (type, TYPE_UNQUALIFIED);
1140 65380 : TYPE_STRING_FLAG (type) = 1;
1141 65380 : snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
1142 : gfc_character_kinds[index].kind);
1143 65380 : PUSH_TYPE (name_buf, type);
1144 65380 : gfc_character_types[index] = type;
1145 65380 : gfc_pcharacter_types[index] = build_pointer_type (type);
1146 : }
1147 32690 : gfc_character1_type_node = gfc_character_types[0];
1148 :
1149 32690 : if (flag_unsigned)
1150 : {
1151 1548 : for (index = 0; gfc_unsigned_kinds[index].kind != 0;++index)
1152 : {
1153 3096 : int index_char = -1;
1154 3096 : for (int i=0; gfc_character_kinds[i].kind != 0; i++)
1155 : {
1156 2322 : if (gfc_character_kinds[i].bit_size
1157 2322 : == gfc_unsigned_kinds[index].bit_size)
1158 : {
1159 : index_char = i;
1160 : break;
1161 : }
1162 : }
1163 1290 : if (index_char > -1)
1164 : {
1165 516 : type = gfc_character_types[index_char];
1166 516 : if (TYPE_STRING_FLAG (type))
1167 : {
1168 516 : type = build_distinct_type_copy (type);
1169 1032 : TYPE_CANONICAL (type)
1170 516 : = TYPE_CANONICAL (gfc_character_types[index_char]);
1171 : }
1172 : else
1173 0 : type = build_variant_type_copy (type);
1174 516 : TYPE_NAME (type) = NULL_TREE;
1175 516 : TYPE_STRING_FLAG (type) = 0;
1176 : }
1177 : else
1178 774 : type = gfc_build_unsigned_type (&gfc_unsigned_kinds[index]);
1179 1290 : gfc_unsigned_types[index] = type;
1180 1290 : snprintf (name_buf, sizeof(name_buf), "unsigned(kind=%d)",
1181 : gfc_integer_kinds[index].kind);
1182 1290 : PUSH_TYPE (name_buf, type);
1183 : }
1184 : }
1185 :
1186 32690 : PUSH_TYPE ("byte", unsigned_char_type_node);
1187 32690 : PUSH_TYPE ("void", void_type_node);
1188 :
1189 : /* DBX debugging output gets upset if these aren't set. */
1190 32690 : if (!TYPE_NAME (integer_type_node))
1191 0 : PUSH_TYPE ("c_integer", integer_type_node);
1192 32690 : if (!TYPE_NAME (char_type_node))
1193 32690 : PUSH_TYPE ("c_char", char_type_node);
1194 :
1195 : #undef PUSH_TYPE
1196 :
1197 32690 : pvoid_type_node = build_pointer_type (void_type_node);
1198 32690 : prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
1199 32690 : ppvoid_type_node = build_pointer_type (pvoid_type_node);
1200 32690 : pchar_type_node = build_pointer_type (gfc_character1_type_node);
1201 32690 : pfunc_type_node
1202 32690 : = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
1203 :
1204 32690 : 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 32690 : gfc_array_range_type
1208 32690 : = 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 32690 : n = TYPE_PRECISION (size_type_node);
1217 32690 : gfc_max_array_element_size
1218 32690 : = wide_int_to_tree (size_type_node,
1219 32690 : wi::mask (n, UNSIGNED,
1220 32690 : TYPE_PRECISION (size_type_node)));
1221 :
1222 32690 : logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1223 32690 : logical_true_node = build_int_cst (logical_type_node, 1);
1224 32690 : logical_false_node = build_int_cst (logical_type_node, 0);
1225 :
1226 : /* Character lengths are of type size_t, except signed. */
1227 32690 : gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1228 32690 : gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1229 :
1230 32690 : gfc_array_dim_rank_type
1231 32690 : = 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 32690 : gfc_size_kind = get_int_kind_from_node (size_type_node);
1239 32690 : }
1240 :
1241 : /* Get the type node for the given type and kind. */
1242 :
1243 : tree
1244 5951102 : gfc_get_int_type (int kind)
1245 : {
1246 5951102 : int index = gfc_validate_kind (BT_INTEGER, kind, true);
1247 5951102 : return index < 0 ? 0 : gfc_integer_types[index];
1248 : }
1249 :
1250 : tree
1251 3101177 : gfc_get_unsigned_type (int kind)
1252 : {
1253 3101177 : int index = gfc_validate_kind (BT_UNSIGNED, kind, true);
1254 3101177 : return index < 0 ? 0 : gfc_unsigned_types[index];
1255 : }
1256 :
1257 : tree
1258 798481 : gfc_get_real_type (int kind)
1259 : {
1260 798481 : int index = gfc_validate_kind (BT_REAL, kind, true);
1261 798481 : return index < 0 ? 0 : gfc_real_types[index];
1262 : }
1263 :
1264 : tree
1265 484166 : gfc_get_complex_type (int kind)
1266 : {
1267 484166 : int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1268 484166 : return index < 0 ? 0 : gfc_complex_types[index];
1269 : }
1270 :
1271 : tree
1272 600883 : gfc_get_logical_type (int kind)
1273 : {
1274 600883 : int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1275 600883 : return index < 0 ? 0 : gfc_logical_types[index];
1276 : }
1277 :
1278 : tree
1279 442902 : gfc_get_char_type (int kind)
1280 : {
1281 442902 : int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1282 442902 : return index < 0 ? 0 : gfc_character_types[index];
1283 : }
1284 :
1285 : tree
1286 169301 : gfc_get_pchar_type (int kind)
1287 : {
1288 169301 : int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1289 169301 : 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 104498 : gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1297 : {
1298 104498 : tree bounds, type;
1299 :
1300 104498 : bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1301 104498 : type = build_array_type (eltype, bounds);
1302 104498 : TYPE_STRING_FLAG (type) = 1;
1303 :
1304 104498 : return type;
1305 : }
1306 :
1307 : tree
1308 86692 : gfc_get_character_type_len (int kind, tree len)
1309 : {
1310 86692 : gfc_validate_kind (BT_CHARACTER, kind, false);
1311 86692 : 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 75657 : gfc_get_character_type (int kind, gfc_charlen * cl)
1319 : {
1320 75657 : tree len;
1321 :
1322 75657 : len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1323 74452 : if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1324 0 : len = build_fold_indirect_ref (len);
1325 :
1326 75657 : 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 1322762 : gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1333 : {
1334 1322762 : tree basetype;
1335 :
1336 1322762 : switch (spec->type)
1337 : {
1338 0 : case BT_UNKNOWN:
1339 0 : gcc_unreachable ();
1340 :
1341 484696 : 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 484696 : if (spec->f90_type == BT_VOID)
1346 : {
1347 344 : if (spec->u.derived
1348 344 : && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1349 259 : basetype = ptr_type_node;
1350 : else
1351 85 : basetype = pfunc_type_node;
1352 : }
1353 : else
1354 484352 : basetype = gfc_get_int_type (spec->kind);
1355 : break;
1356 :
1357 2833 : case BT_UNSIGNED:
1358 2833 : basetype = gfc_get_unsigned_type (spec->kind);
1359 2833 : break;
1360 :
1361 145284 : case BT_REAL:
1362 145284 : basetype = gfc_get_real_type (spec->kind);
1363 145284 : break;
1364 :
1365 26194 : case BT_COMPLEX:
1366 26194 : basetype = gfc_get_complex_type (spec->kind);
1367 26194 : break;
1368 :
1369 429780 : case BT_LOGICAL:
1370 429780 : basetype = gfc_get_logical_type (spec->kind);
1371 429780 : break;
1372 :
1373 63178 : case BT_CHARACTER:
1374 63178 : basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1375 63178 : 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 166897 : case BT_DERIVED:
1388 166897 : case BT_CLASS:
1389 166897 : 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 166897 : if (spec->u.derived->ts.f90_type == BT_VOID)
1396 : {
1397 12359 : spec->type = BT_INTEGER;
1398 12359 : spec->kind = gfc_index_integer_kind;
1399 12359 : spec->f90_type = BT_VOID;
1400 12359 : spec->is_c_interop = 1; /* Mark as escaping later. */
1401 : }
1402 : break;
1403 3734 : case BT_VOID:
1404 3734 : 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 3734 : basetype = ptr_type_node;
1408 3734 : 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 1322762 : return basetype;
1424 : }
1425 :
1426 : /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1427 :
1428 : static tree
1429 123527 : gfc_conv_array_bound (gfc_expr * expr)
1430 : {
1431 : /* If expr is an integer constant, return that. */
1432 123527 : if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1433 15815 : 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 345746 : gfc_get_element_type (tree type)
1445 : {
1446 345746 : tree element;
1447 :
1448 345746 : if (GFC_ARRAY_TYPE_P (type))
1449 : {
1450 130797 : if (TREE_CODE (type) == POINTER_TYPE)
1451 21698 : type = TREE_TYPE (type);
1452 130797 : if (GFC_TYPE_ARRAY_RANK (type) == 0)
1453 : {
1454 630 : gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1455 : element = type;
1456 : }
1457 : else
1458 : {
1459 130167 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1460 130167 : element = TREE_TYPE (type);
1461 : }
1462 : }
1463 : else
1464 : {
1465 214949 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1466 214949 : element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1467 :
1468 214949 : gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1469 214949 : element = TREE_TYPE (element);
1470 :
1471 : /* For arrays, which are not scalar coarrays. */
1472 214949 : if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1473 213130 : element = TREE_TYPE (element);
1474 : }
1475 :
1476 345746 : 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 115590 : gfc_is_nodesc_array (gfc_symbol * sym)
1558 : {
1559 115590 : symbol_attribute *array_attr;
1560 115590 : gfc_array_spec *as;
1561 115590 : bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
1562 :
1563 115590 : array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1564 115590 : as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1565 :
1566 115590 : gcc_assert (array_attr->dimension || array_attr->codimension);
1567 :
1568 : /* We only want local arrays. */
1569 115590 : if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1570 108132 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1571 108132 : || 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 95485 : if (sym->assoc && as->type != AS_EXPLICIT)
1577 : return 0;
1578 :
1579 : /* The dummy is stored in sym and not in the component. */
1580 93619 : if (sym->attr.dummy)
1581 42806 : return as->type != AS_ASSUMED_SHAPE
1582 42806 : && as->type != AS_ASSUMED_RANK;
1583 :
1584 50813 : if (sym->attr.result || sym->attr.function)
1585 : return 0;
1586 :
1587 40801 : 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 54846 : 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 54846 : tree lbound[GFC_MAX_DIMENSIONS];
1601 54846 : tree ubound[GFC_MAX_DIMENSIONS];
1602 54846 : int n, corank;
1603 :
1604 : /* Assumed-shape arrays do not have codimension information stored in the
1605 : descriptor. */
1606 54846 : corank = MAX (as->corank, codim);
1607 54846 : if (as->type == AS_ASSUMED_SHAPE ||
1608 8148 : (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1609 54846 : corank = codim;
1610 :
1611 54846 : if (as->type == AS_ASSUMED_RANK)
1612 130368 : for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1613 : {
1614 122220 : lbound[n] = NULL_TREE;
1615 122220 : ubound[n] = NULL_TREE;
1616 : }
1617 :
1618 124279 : for (n = 0; n < as->rank; n++)
1619 : {
1620 : /* Create expressions for the known bounds of the array. */
1621 69433 : if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1622 16777 : lbound[n] = gfc_index_one_node;
1623 : else
1624 52656 : lbound[n] = gfc_conv_array_bound (as->lower[n]);
1625 69433 : ubound[n] = gfc_conv_array_bound (as->upper[n]);
1626 : }
1627 :
1628 56073 : for (n = as->rank; n < as->rank + corank; n++)
1629 : {
1630 1227 : if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1631 18 : lbound[n] = gfc_index_one_node;
1632 : else
1633 1209 : lbound[n] = gfc_conv_array_bound (as->lower[n]);
1634 :
1635 1227 : if (n < as->rank + corank - 1)
1636 229 : ubound[n] = gfc_conv_array_bound (as->upper[n]);
1637 : }
1638 :
1639 54846 : if (as->type == AS_ASSUMED_SHAPE)
1640 17404 : akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1641 : : GFC_ARRAY_ASSUMED_SHAPE;
1642 37442 : else if (as->type == AS_ASSUMED_RANK)
1643 : {
1644 8148 : if (akind == GFC_ARRAY_ALLOCATABLE)
1645 : akind = GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE;
1646 7767 : 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 7341 : akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1651 : : GFC_ARRAY_ASSUMED_RANK;
1652 : }
1653 101544 : return gfc_get_array_type_bounds (type, as->rank == -1
1654 : ? GFC_MAX_DIMENSIONS : as->rank,
1655 : corank, lbound, ubound, 0, akind,
1656 54846 : restricted);
1657 : }
1658 :
1659 : /* Returns the struct descriptor_dimension type. */
1660 :
1661 : static tree
1662 33461 : gfc_get_desc_dim_type (void)
1663 : {
1664 33461 : tree type;
1665 33461 : tree decl, *chain = NULL;
1666 :
1667 33461 : if (gfc_desc_dim_type)
1668 : return gfc_desc_dim_type;
1669 :
1670 : /* Build the type node. */
1671 12641 : type = make_node (RECORD_TYPE);
1672 :
1673 12641 : TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1674 12641 : TYPE_PACKED (type) = 1;
1675 :
1676 : /* Consists of the stride, lbound and ubound members. */
1677 12641 : decl = gfc_add_field_to_struct_1 (type,
1678 : get_identifier ("stride"),
1679 : gfc_array_index_type, &chain);
1680 12641 : suppress_warning (decl);
1681 :
1682 12641 : decl = gfc_add_field_to_struct_1 (type,
1683 : get_identifier ("lbound"),
1684 : gfc_array_index_type, &chain);
1685 12641 : suppress_warning (decl);
1686 :
1687 12641 : decl = gfc_add_field_to_struct_1 (type,
1688 : get_identifier ("ubound"),
1689 : gfc_array_index_type, &chain);
1690 12641 : suppress_warning (decl);
1691 :
1692 : /* Finish off the type. */
1693 12641 : gfc_finish_type (type);
1694 12641 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1695 :
1696 12641 : gfc_desc_dim_type = type;
1697 12641 : 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 148462 : gfc_get_dtype_rank_type (int rank, tree etype)
1708 : {
1709 148462 : tree ptype;
1710 148462 : tree size;
1711 148462 : int n;
1712 :
1713 148462 : ptype = etype;
1714 148462 : while (TREE_CODE (etype) == POINTER_TYPE
1715 180388 : || TREE_CODE (etype) == ARRAY_TYPE)
1716 : {
1717 31926 : ptype = etype;
1718 31926 : etype = TREE_TYPE (etype);
1719 : }
1720 :
1721 148462 : gcc_assert (etype);
1722 :
1723 148462 : switch (TREE_CODE (etype))
1724 : {
1725 86853 : case INTEGER_TYPE:
1726 86853 : if (TREE_CODE (ptype) == ARRAY_TYPE
1727 86853 : && TYPE_STRING_FLAG (ptype))
1728 : n = BT_CHARACTER;
1729 : else
1730 : {
1731 63028 : if (TYPE_UNSIGNED (etype))
1732 : n = BT_UNSIGNED;
1733 : else
1734 : n = BT_INTEGER;
1735 : }
1736 : break;
1737 :
1738 : case BOOLEAN_TYPE:
1739 : n = BT_LOGICAL;
1740 : break;
1741 :
1742 : case REAL_TYPE:
1743 : n = BT_REAL;
1744 : break;
1745 :
1746 : case COMPLEX_TYPE:
1747 : n = BT_COMPLEX;
1748 : break;
1749 :
1750 23136 : case RECORD_TYPE:
1751 23136 : if (GFC_CLASS_TYPE_P (etype))
1752 : n = BT_CLASS;
1753 : else
1754 : n = BT_DERIVED;
1755 : break;
1756 :
1757 : case FUNCTION_TYPE:
1758 : case VOID_TYPE:
1759 : n = BT_VOID;
1760 : break;
1761 :
1762 0 : default:
1763 : /* TODO: Don't do dtype for temporary descriptorless arrays. */
1764 : /* We can encounter strange array types for temporary arrays. */
1765 0 : gcc_unreachable ();
1766 : }
1767 :
1768 23825 : switch (n)
1769 : {
1770 23825 : case BT_CHARACTER:
1771 23825 : gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1772 23825 : size = gfc_get_character_len_in_bytes (ptype);
1773 23825 : break;
1774 2193 : case BT_VOID:
1775 2193 : gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1776 2193 : size = size_in_bytes (ptype);
1777 2193 : break;
1778 122444 : default:
1779 122444 : size = size_in_bytes (etype);
1780 122444 : break;
1781 : }
1782 :
1783 148462 : return gfc_build_dtype_constructor (size, n, rank);
1784 : }
1785 :
1786 :
1787 : tree
1788 119737 : gfc_get_dtype (tree type, int * rank)
1789 : {
1790 119737 : tree dtype;
1791 119737 : tree etype;
1792 119737 : int irnk;
1793 :
1794 119737 : gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1795 :
1796 119737 : irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
1797 119737 : etype = gfc_get_element_type (type);
1798 119737 : dtype = gfc_get_dtype_rank_type (irnk, etype);
1799 :
1800 119737 : GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1801 119737 : return dtype;
1802 : }
1803 :
1804 :
1805 : /* Build an array type for use without a descriptor, packed according
1806 : to the value of PACKED. */
1807 :
1808 : tree
1809 119932 : gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1810 : bool restricted)
1811 : {
1812 119932 : tree range;
1813 119932 : tree type;
1814 119932 : tree tmp;
1815 119932 : int n;
1816 119932 : int known_stride;
1817 119932 : int known_offset;
1818 119932 : mpz_t offset;
1819 119932 : mpz_t stride;
1820 119932 : mpz_t delta;
1821 119932 : gfc_expr *expr;
1822 :
1823 119932 : mpz_init_set_ui (offset, 0);
1824 119932 : mpz_init_set_ui (stride, 1);
1825 119932 : mpz_init (delta);
1826 :
1827 : /* We don't use build_array_type because this does not include
1828 : lang-specific information (i.e. the bounds of the array) when checking
1829 : for duplicates. */
1830 119932 : if (as->rank)
1831 117948 : type = make_node (ARRAY_TYPE);
1832 : else
1833 1984 : type = build_variant_type_copy (etype);
1834 :
1835 119932 : GFC_ARRAY_TYPE_P (type) = 1;
1836 119932 : TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1837 :
1838 119932 : known_stride = (packed != PACKED_NO);
1839 119932 : known_offset = 1;
1840 260027 : for (n = 0; n < as->rank; n++)
1841 : {
1842 : /* Fill in the stride and bound components of the type. */
1843 140095 : if (known_stride)
1844 125984 : tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1845 : else
1846 : tmp = NULL_TREE;
1847 140095 : GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1848 :
1849 140095 : expr = as->lower[n];
1850 140095 : if (expr && expr->expr_type == EXPR_CONSTANT)
1851 : {
1852 139305 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1853 : gfc_index_integer_kind);
1854 : }
1855 : else
1856 : {
1857 : known_stride = 0;
1858 : tmp = NULL_TREE;
1859 : }
1860 140095 : GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1861 :
1862 140095 : if (known_stride)
1863 : {
1864 : /* Calculate the offset. */
1865 125530 : mpz_mul (delta, stride, as->lower[n]->value.integer);
1866 125530 : mpz_sub (offset, offset, delta);
1867 : }
1868 : else
1869 : known_offset = 0;
1870 :
1871 140095 : expr = as->upper[n];
1872 140095 : if (expr && expr->expr_type == EXPR_CONSTANT)
1873 : {
1874 111668 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1875 : gfc_index_integer_kind);
1876 : }
1877 : else
1878 : {
1879 : tmp = NULL_TREE;
1880 : known_stride = 0;
1881 : }
1882 140095 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1883 :
1884 140095 : if (known_stride)
1885 : {
1886 : /* Calculate the stride. */
1887 110700 : mpz_sub (delta, as->upper[n]->value.integer,
1888 110700 : as->lower[n]->value.integer);
1889 110700 : mpz_add_ui (delta, delta, 1);
1890 110700 : mpz_mul (stride, stride, delta);
1891 : }
1892 :
1893 : /* Only the first stride is known for partial packed arrays. */
1894 140095 : if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1895 11154 : known_stride = 0;
1896 : }
1897 122565 : for (n = as->rank; n < as->rank + as->corank; n++)
1898 : {
1899 2633 : expr = as->lower[n];
1900 2633 : if (expr && expr->expr_type == EXPR_CONSTANT)
1901 2519 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1902 : gfc_index_integer_kind);
1903 : else
1904 : tmp = NULL_TREE;
1905 2633 : GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1906 :
1907 2633 : expr = as->upper[n];
1908 2633 : if (expr && expr->expr_type == EXPR_CONSTANT)
1909 217 : tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1910 : gfc_index_integer_kind);
1911 : else
1912 : tmp = NULL_TREE;
1913 2633 : if (n < as->rank + as->corank - 1)
1914 277 : GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1915 : }
1916 :
1917 119932 : if (known_offset)
1918 : {
1919 108880 : GFC_TYPE_ARRAY_OFFSET (type) =
1920 108880 : gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1921 : }
1922 : else
1923 11052 : GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1924 :
1925 119932 : if (known_stride)
1926 : {
1927 88869 : GFC_TYPE_ARRAY_SIZE (type) =
1928 88869 : gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1929 : }
1930 : else
1931 31063 : GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1932 :
1933 119932 : GFC_TYPE_ARRAY_RANK (type) = as->rank;
1934 119932 : GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1935 119932 : GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1936 119932 : range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1937 : NULL_TREE);
1938 : /* TODO: use main type if it is unbounded. */
1939 119932 : GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1940 119932 : build_pointer_type (build_array_type (etype, range));
1941 119932 : if (restricted)
1942 116473 : GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1943 232946 : build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1944 : TYPE_QUAL_RESTRICT);
1945 :
1946 119932 : if (as->rank == 0)
1947 : {
1948 1984 : if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1949 : {
1950 1901 : type = build_pointer_type (type);
1951 :
1952 1901 : if (restricted)
1953 1901 : type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1954 :
1955 1901 : GFC_ARRAY_TYPE_P (type) = 1;
1956 1901 : TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1957 : }
1958 :
1959 1984 : goto array_type_done;
1960 : }
1961 :
1962 117948 : if (known_stride)
1963 : {
1964 86926 : mpz_sub_ui (stride, stride, 1);
1965 86926 : range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1966 : }
1967 : else
1968 : range = NULL_TREE;
1969 :
1970 117948 : range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1971 117948 : TYPE_DOMAIN (type) = range;
1972 :
1973 117948 : build_pointer_type (etype);
1974 117948 : TREE_TYPE (type) = etype;
1975 :
1976 117948 : layout_type (type);
1977 :
1978 : /* Represent packed arrays as multi-dimensional if they have rank >
1979 : 1 and with proper bounds, instead of flat arrays. This makes for
1980 : better debug info. */
1981 117948 : if (known_offset)
1982 : {
1983 106896 : tree gtype = etype, rtype, type_decl;
1984 :
1985 229888 : for (n = as->rank - 1; n >= 0; n--)
1986 : {
1987 491968 : rtype = build_range_type (gfc_array_index_type,
1988 122992 : GFC_TYPE_ARRAY_LBOUND (type, n),
1989 122992 : GFC_TYPE_ARRAY_UBOUND (type, n));
1990 122992 : gtype = build_array_type (gtype, rtype);
1991 : }
1992 106896 : TYPE_NAME (type) = type_decl = build_decl (input_location,
1993 : TYPE_DECL, NULL, gtype);
1994 106896 : DECL_ORIGINAL_TYPE (type_decl) = gtype;
1995 : }
1996 :
1997 117948 : if (packed != PACKED_STATIC || !known_stride
1998 82597 : || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1999 : {
2000 : /* For dummy arrays and automatic (heap allocated) arrays we
2001 : want a pointer to the array. */
2002 35467 : type = build_pointer_type (type);
2003 35467 : if (restricted)
2004 34076 : type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2005 35467 : GFC_ARRAY_TYPE_P (type) = 1;
2006 35467 : TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
2007 : }
2008 :
2009 82481 : array_type_done:
2010 119932 : mpz_clear (offset);
2011 119932 : mpz_clear (stride);
2012 119932 : mpz_clear (delta);
2013 :
2014 119932 : return type;
2015 : }
2016 :
2017 :
2018 : /* Return or create the base type for an array descriptor. */
2019 :
2020 : static tree
2021 312970 : gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
2022 : {
2023 312970 : tree fat_type, decl, arraytype, *chain = NULL;
2024 312970 : char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
2025 312970 : int idx;
2026 :
2027 : /* Assumed-rank array. */
2028 312970 : if (dimen == -1)
2029 0 : dimen = GFC_MAX_DIMENSIONS;
2030 :
2031 312970 : idx = 2 * (codimen + dimen) + restricted;
2032 :
2033 312970 : gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
2034 :
2035 312970 : if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
2036 : {
2037 2552 : if (gfc_array_descriptor_base_caf[idx])
2038 : return gfc_array_descriptor_base_caf[idx];
2039 : }
2040 310418 : else if (gfc_array_descriptor_base[idx])
2041 : return gfc_array_descriptor_base[idx];
2042 :
2043 : /* Build the type node. */
2044 36519 : fat_type = make_node (RECORD_TYPE);
2045 :
2046 36519 : sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
2047 36519 : TYPE_NAME (fat_type) = get_identifier (name);
2048 36519 : TYPE_NAMELESS (fat_type) = 1;
2049 :
2050 : /* Add the data member as the first element of the descriptor. */
2051 36519 : gfc_add_field_to_struct_1 (fat_type,
2052 : get_identifier ("data"),
2053 : (restricted
2054 : ? prvoid_type_node
2055 : : ptr_type_node), &chain);
2056 :
2057 : /* Add the base component. */
2058 36519 : decl = gfc_add_field_to_struct_1 (fat_type,
2059 : get_identifier ("offset"),
2060 : gfc_array_index_type, &chain);
2061 36519 : suppress_warning (decl);
2062 :
2063 : /* Add the dtype component. */
2064 36519 : decl = gfc_add_field_to_struct_1 (fat_type,
2065 : get_identifier ("dtype"),
2066 : get_dtype_type_node (), &chain);
2067 36519 : suppress_warning (decl);
2068 :
2069 : /* Add the span component. */
2070 36519 : decl = gfc_add_field_to_struct_1 (fat_type,
2071 : get_identifier ("span"),
2072 : gfc_array_index_type, &chain);
2073 36519 : suppress_warning (decl);
2074 :
2075 : /* Build the array type for the stride and bound components. */
2076 36519 : if (dimen + codimen > 0)
2077 : {
2078 33461 : arraytype =
2079 33461 : build_array_type (gfc_get_desc_dim_type (),
2080 : build_range_type (gfc_array_index_type,
2081 : gfc_index_zero_node,
2082 33461 : gfc_rank_cst[codimen + dimen - 1]));
2083 :
2084 33461 : decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
2085 : arraytype, &chain);
2086 33461 : suppress_warning (decl);
2087 : }
2088 :
2089 36519 : if (flag_coarray == GFC_FCOARRAY_LIB)
2090 : {
2091 1796 : decl = gfc_add_field_to_struct_1 (fat_type,
2092 : get_identifier ("token"),
2093 : prvoid_type_node, &chain);
2094 1796 : suppress_warning (decl);
2095 : }
2096 :
2097 : /* Finish off the type. */
2098 36519 : gfc_finish_type (fat_type);
2099 36519 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
2100 :
2101 36519 : if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
2102 974 : gfc_array_descriptor_base_caf[idx] = fat_type;
2103 : else
2104 35545 : gfc_array_descriptor_base[idx] = fat_type;
2105 :
2106 : return fat_type;
2107 : }
2108 :
2109 :
2110 : /* Build an array (descriptor) type with given bounds. */
2111 :
2112 : tree
2113 156485 : gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
2114 : tree * ubound, int packed,
2115 : enum gfc_array_kind akind, bool restricted)
2116 : {
2117 156485 : char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
2118 156485 : tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
2119 156485 : const char *type_name;
2120 156485 : int n;
2121 :
2122 156485 : base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
2123 156485 : fat_type = build_distinct_type_copy (base_type);
2124 : /* Unshare TYPE_FIELDs. */
2125 937255 : for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
2126 : {
2127 780770 : tree next = DECL_CHAIN (*tp);
2128 780770 : *tp = copy_node (*tp);
2129 780770 : DECL_CONTEXT (*tp) = fat_type;
2130 780770 : DECL_CHAIN (*tp) = next;
2131 : }
2132 : /* Make sure that nontarget and target array type have the same canonical
2133 : type (and same stub decl for debug info). */
2134 156485 : base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
2135 156485 : TYPE_CANONICAL (fat_type) = base_type;
2136 156485 : TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
2137 : /* Arrays of unknown type must alias with all array descriptors. */
2138 156485 : TYPE_TYPELESS_STORAGE (base_type) = 1;
2139 156485 : TYPE_TYPELESS_STORAGE (fat_type) = 1;
2140 156485 : gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
2141 :
2142 156485 : tmp = etype;
2143 156485 : if (TREE_CODE (tmp) == ARRAY_TYPE
2144 156485 : && TYPE_STRING_FLAG (tmp))
2145 24413 : tmp = TREE_TYPE (etype);
2146 156485 : tmp = TYPE_NAME (tmp);
2147 156485 : if (tmp && TREE_CODE (tmp) == TYPE_DECL)
2148 128207 : tmp = DECL_NAME (tmp);
2149 128207 : if (tmp)
2150 152769 : type_name = IDENTIFIER_POINTER (tmp);
2151 : else
2152 : type_name = "unknown";
2153 156485 : sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
2154 : GFC_MAX_SYMBOL_LEN, type_name);
2155 156485 : TYPE_NAME (fat_type) = get_identifier (name);
2156 156485 : TYPE_NAMELESS (fat_type) = 1;
2157 :
2158 156485 : GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
2159 156485 : TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
2160 :
2161 156485 : GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
2162 156485 : GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
2163 156485 : GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
2164 156485 : GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
2165 :
2166 : /* Build an array descriptor record type. */
2167 156485 : if (packed != 0)
2168 37550 : stride = gfc_index_one_node;
2169 : else
2170 : stride = NULL_TREE;
2171 488322 : for (n = 0; n < dimen + codimen; n++)
2172 : {
2173 333966 : if (n < dimen)
2174 330675 : GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
2175 :
2176 333966 : if (lbound)
2177 333966 : lower = lbound[n];
2178 : else
2179 : lower = NULL_TREE;
2180 :
2181 333966 : if (lower != NULL_TREE)
2182 : {
2183 171897 : if (INTEGER_CST_P (lower))
2184 170762 : GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
2185 : else
2186 : lower = NULL_TREE;
2187 : }
2188 :
2189 333966 : if (codimen && n == dimen + codimen - 1)
2190 : break;
2191 :
2192 331837 : upper = ubound[n];
2193 331837 : if (upper != NULL_TREE)
2194 : {
2195 137709 : if (INTEGER_CST_P (upper))
2196 103578 : GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
2197 : else
2198 : upper = NULL_TREE;
2199 : }
2200 :
2201 331837 : if (n >= dimen)
2202 1162 : continue;
2203 :
2204 330675 : if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
2205 : {
2206 29258 : tmp = fold_build2_loc (input_location, MINUS_EXPR,
2207 : gfc_array_index_type, upper, lower);
2208 29258 : tmp = fold_build2_loc (input_location, PLUS_EXPR,
2209 : gfc_array_index_type, tmp,
2210 : gfc_index_one_node);
2211 29258 : stride = fold_build2_loc (input_location, MULT_EXPR,
2212 : gfc_array_index_type, tmp, stride);
2213 : /* Check the folding worked. */
2214 29258 : gcc_assert (INTEGER_CST_P (stride));
2215 : }
2216 : else
2217 : stride = NULL_TREE;
2218 : }
2219 156485 : GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
2220 :
2221 : /* TODO: known offsets for descriptors. */
2222 156485 : GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2223 :
2224 156485 : if (dimen == 0)
2225 : {
2226 8062 : arraytype = build_pointer_type (etype);
2227 8062 : if (restricted)
2228 7317 : arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2229 :
2230 8062 : GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2231 8062 : return fat_type;
2232 : }
2233 :
2234 : /* We define data as an array with the correct size if possible.
2235 : Much better than doing pointer arithmetic. */
2236 148423 : if (stride)
2237 22886 : rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2238 : int_const_binop (MINUS_EXPR, stride,
2239 45772 : build_int_cst (TREE_TYPE (stride), 1)));
2240 : else
2241 125537 : rtype = gfc_array_range_type;
2242 148423 : arraytype = build_array_type (etype, rtype);
2243 148423 : arraytype = build_pointer_type (arraytype);
2244 148423 : if (restricted)
2245 71324 : arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2246 148423 : GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2247 :
2248 : /* This will generate the base declarations we need to emit debug
2249 : information for this type. FIXME: there must be a better way to
2250 : avoid divergence between compilations with and without debug
2251 : information. */
2252 148423 : {
2253 148423 : struct array_descr_info info;
2254 148423 : gfc_get_array_descr_info (fat_type, &info);
2255 148423 : gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2256 : }
2257 :
2258 148423 : return fat_type;
2259 : }
2260 :
2261 :
2262 : /* Create and return a zero-rank array descriptor type suitable to hold a scalar
2263 : value of type SCALAR_TYPE having attributes ATTR. An array descriptor of the
2264 : returned type is to be used as implementation detail when a scalar actual
2265 : argument of type SCALAR_TYPE and having attributes ATTR is associated with an
2266 : assumed-rank dummy. */
2267 :
2268 : tree
2269 7198 : gfc_get_scalar_to_descriptor_type (tree scalar_type, symbol_attribute attr)
2270 : {
2271 7198 : enum gfc_array_kind akind;
2272 :
2273 7198 : if (attr.pointer)
2274 : akind = GFC_ARRAY_POINTER_CONT;
2275 6936 : else if (attr.allocatable)
2276 : akind = GFC_ARRAY_ALLOCATABLE;
2277 : else
2278 5426 : akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
2279 :
2280 7198 : if (POINTER_TYPE_P (scalar_type))
2281 6219 : scalar_type = TREE_TYPE (scalar_type);
2282 :
2283 7198 : tree *lbound = NULL, *ubound = NULL;
2284 7198 : int codim = 0;
2285 7198 : if (TYPE_LANG_SPECIFIC (scalar_type))
2286 : {
2287 5894 : struct lang_type *lang_specific = TYPE_LANG_SPECIFIC (scalar_type);
2288 5894 : codim = lang_specific->corank;
2289 5894 : lbound = lang_specific->lbound;
2290 5894 : ubound = lang_specific->ubound;
2291 : }
2292 7556 : return gfc_get_array_type_bounds (scalar_type, 0, codim, lbound, ubound, 1,
2293 7198 : akind, !(attr.pointer || attr.target));
2294 : }
2295 :
2296 :
2297 : /* Build a pointer type. This function is called from gfc_sym_type(). */
2298 :
2299 : static tree
2300 17657 : gfc_build_pointer_type (gfc_symbol * sym, tree type)
2301 : {
2302 : /* Array pointer types aren't actually pointers. */
2303 0 : if (sym->attr.dimension)
2304 : return type;
2305 : else
2306 17657 : return build_pointer_type (type);
2307 : }
2308 :
2309 : static tree gfc_nonrestricted_type (tree t);
2310 : /* Given two record or union type nodes TO and FROM, ensure
2311 : that all fields in FROM have a corresponding field in TO,
2312 : their type being nonrestrict variants. This accepts a TO
2313 : node that already has a prefix of the fields in FROM. */
2314 : static void
2315 4864 : mirror_fields (tree to, tree from)
2316 : {
2317 4864 : tree fto, ffrom;
2318 4864 : tree *chain;
2319 :
2320 : /* Forward to the end of TOs fields. */
2321 4864 : fto = TYPE_FIELDS (to);
2322 4864 : ffrom = TYPE_FIELDS (from);
2323 4864 : chain = &TYPE_FIELDS (to);
2324 4864 : while (fto)
2325 : {
2326 0 : gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2327 0 : chain = &DECL_CHAIN (fto);
2328 0 : fto = DECL_CHAIN (fto);
2329 0 : ffrom = DECL_CHAIN (ffrom);
2330 : }
2331 :
2332 : /* Now add all fields remaining in FROM (starting with ffrom). */
2333 23044 : for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2334 : {
2335 18180 : tree newfield = copy_node (ffrom);
2336 18180 : DECL_CONTEXT (newfield) = to;
2337 : /* The store to DECL_CHAIN might seem redundant with the
2338 : stores to *chain, but not clearing it here would mean
2339 : leaving a chain into the old fields. If ever
2340 : our called functions would look at them confusion
2341 : will arise. */
2342 18180 : DECL_CHAIN (newfield) = NULL_TREE;
2343 18180 : *chain = newfield;
2344 18180 : chain = &DECL_CHAIN (newfield);
2345 :
2346 18180 : if (TREE_CODE (ffrom) == FIELD_DECL)
2347 : {
2348 18180 : tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2349 18180 : TREE_TYPE (newfield) = elemtype;
2350 : }
2351 : }
2352 4864 : *chain = NULL_TREE;
2353 4864 : }
2354 :
2355 : /* Given a type T, returns a different type of the same structure,
2356 : except that all types it refers to (recursively) are always
2357 : non-restrict qualified types. */
2358 : static tree
2359 284905 : gfc_nonrestricted_type (tree t)
2360 : {
2361 284905 : tree ret = t;
2362 :
2363 : /* If the type isn't laid out yet, don't copy it. If something
2364 : needs it for real it should wait until the type got finished. */
2365 284905 : if (!TYPE_SIZE (t))
2366 : return t;
2367 :
2368 272039 : if (!TYPE_LANG_SPECIFIC (t))
2369 109949 : TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2370 : /* If we're dealing with this very node already further up
2371 : the call chain (recursion via pointers and struct members)
2372 : we haven't yet determined if we really need a new type node.
2373 : Assume we don't, return T itself. */
2374 272039 : if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2375 : return t;
2376 :
2377 : /* If we have calculated this all already, just return it. */
2378 264035 : if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2379 148392 : return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2380 :
2381 : /* Mark this type. */
2382 115643 : TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2383 :
2384 115643 : switch (TREE_CODE (t))
2385 : {
2386 : default:
2387 : break;
2388 :
2389 41804 : case POINTER_TYPE:
2390 41804 : case REFERENCE_TYPE:
2391 41804 : {
2392 41804 : tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2393 41804 : if (totype == TREE_TYPE (t))
2394 : ret = t;
2395 1756 : else if (TREE_CODE (t) == POINTER_TYPE)
2396 1756 : ret = build_pointer_type (totype);
2397 : else
2398 0 : ret = build_reference_type (totype);
2399 83608 : ret = build_qualified_type (ret,
2400 41804 : TYPE_QUALS (t)
2401 : .without (TYPE_QUAL_RESTRICT));
2402 : }
2403 41804 : break;
2404 :
2405 6918 : case ARRAY_TYPE:
2406 6918 : {
2407 6918 : tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2408 6918 : if (elemtype == TREE_TYPE (t))
2409 : ret = t;
2410 : else
2411 : {
2412 41 : ret = build_variant_type_copy (t);
2413 41 : TREE_TYPE (ret) = elemtype;
2414 41 : if (TYPE_LANG_SPECIFIC (t)
2415 41 : && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2416 : {
2417 41 : tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2418 41 : dataptr_type = gfc_nonrestricted_type (dataptr_type);
2419 41 : if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2420 : {
2421 41 : TYPE_LANG_SPECIFIC (ret)
2422 41 : = ggc_cleared_alloc<struct lang_type> ();
2423 41 : *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2424 41 : GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2425 : }
2426 : }
2427 : }
2428 : }
2429 : break;
2430 :
2431 32046 : case RECORD_TYPE:
2432 32046 : case UNION_TYPE:
2433 32046 : case QUAL_UNION_TYPE:
2434 32046 : {
2435 32046 : tree field;
2436 : /* First determine if we need a new type at all.
2437 : Careful, the two calls to gfc_nonrestricted_type per field
2438 : might return different values. That happens exactly when
2439 : one of the fields reaches back to this very record type
2440 : (via pointers). The first calls will assume that we don't
2441 : need to copy T (see the error_mark_node marking). If there
2442 : are any reasons for copying T apart from having to copy T,
2443 : we'll indeed copy it, and the second calls to
2444 : gfc_nonrestricted_type will use that new node if they
2445 : reach back to T. */
2446 157697 : for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2447 130515 : if (TREE_CODE (field) == FIELD_DECL)
2448 : {
2449 130515 : tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2450 130515 : if (elemtype != TREE_TYPE (field))
2451 : break;
2452 : }
2453 32046 : if (!field)
2454 : break;
2455 4864 : ret = build_variant_type_copy (t);
2456 4864 : TYPE_FIELDS (ret) = NULL_TREE;
2457 :
2458 : /* Here we make sure that as soon as we know we have to copy
2459 : T, that also fields reaching back to us will use the new
2460 : copy. It's okay if that copy still contains the old fields,
2461 : we won't look at them. */
2462 4864 : TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2463 4864 : mirror_fields (ret, t);
2464 : }
2465 4864 : break;
2466 : }
2467 :
2468 115643 : TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2469 115643 : return ret;
2470 : }
2471 :
2472 :
2473 : /* Return the type for a symbol. Special handling is required for character
2474 : types to get the correct level of indirection.
2475 : For functions return the return type.
2476 : For subroutines return void_type_node.
2477 : Calling this multiple times for the same symbol should be avoided,
2478 : especially for character and array types. */
2479 :
2480 : tree
2481 435140 : gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
2482 : {
2483 435140 : tree type;
2484 435140 : int byref;
2485 435140 : bool restricted;
2486 :
2487 : /* Procedure Pointers inside COMMON blocks. */
2488 435140 : if (sym->attr.proc_pointer && sym->attr.in_common)
2489 : {
2490 : /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2491 30 : sym->attr.proc_pointer = 0;
2492 30 : type = build_pointer_type (gfc_get_function_type (sym));
2493 30 : sym->attr.proc_pointer = 1;
2494 30 : return type;
2495 : }
2496 :
2497 435110 : if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2498 0 : return void_type_node;
2499 :
2500 : /* In the case of a function the fake result variable may have a
2501 : type different from the function type, so don't return early in
2502 : that case. */
2503 435110 : if (sym->backend_decl && !sym->attr.function)
2504 494 : return TREE_TYPE (sym->backend_decl);
2505 :
2506 434616 : if (sym->attr.result
2507 8982 : && sym->ts.type == BT_CHARACTER
2508 1240 : && sym->ts.u.cl->backend_decl == NULL_TREE
2509 526 : && sym->ns->proc_name
2510 520 : && sym->ns->proc_name->ts.u.cl
2511 518 : && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2512 6 : sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2513 :
2514 434616 : if (sym->ts.type == BT_CHARACTER
2515 434616 : && ((sym->attr.function && sym->attr.is_bind_c)
2516 43342 : || ((sym->attr.result || sym->attr.value)
2517 1788 : && sym->ns->proc_name
2518 1782 : && sym->ns->proc_name->attr.is_bind_c)
2519 43080 : || (sym->ts.deferred
2520 4725 : && (!sym->ts.u.cl
2521 4725 : || !sym->ts.u.cl->backend_decl
2522 3474 : || sym->attr.save))
2523 41628 : || (sym->attr.dummy
2524 19328 : && sym->attr.value
2525 288 : && gfc_length_one_character_type_p (&sym->ts))))
2526 1931 : type = gfc_get_char_type (sym->ts.kind);
2527 : else
2528 432685 : type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2529 :
2530 434616 : if (sym->attr.dummy && !sym->attr.function
2531 169344 : && (!sym->attr.value
2532 12000 : || sym->attr.dimension
2533 11856 : || (sym->ts.type == BT_CHARACTER
2534 500 : && (!sym->ts.u.cl || !sym->ts.u.cl->length
2535 452 : || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)))
2536 157560 : && !sym->pass_as_value)
2537 : byref = 1;
2538 : else
2539 278372 : byref = 0;
2540 :
2541 403562 : restricted = (!sym->attr.target && !IS_POINTER (sym)
2542 819306 : && !IS_PROC_POINTER (sym) && !sym->attr.cray_pointee);
2543 50675 : if (!restricted)
2544 50675 : type = gfc_nonrestricted_type (type);
2545 :
2546 : /* Dummy argument to a bind(C) procedure. */
2547 434616 : if (is_bind_c && is_CFI_desc (sym, NULL))
2548 3641 : type = gfc_get_cfi_type (sym->attr.dimension ? sym->as->rank : 0,
2549 : /* restricted = */ false);
2550 430975 : else if (sym->attr.dimension || sym->attr.codimension)
2551 : {
2552 102543 : if (gfc_is_nodesc_array (sym))
2553 : {
2554 : /* If this is a character argument of unknown length, just use the
2555 : base type. */
2556 55566 : if (sym->ts.type != BT_CHARACTER
2557 6048 : || !(sym->attr.dummy || sym->attr.function)
2558 1960 : || sym->ts.u.cl->backend_decl)
2559 : {
2560 55138 : type = gfc_get_nodesc_array_type (type, sym->as,
2561 : byref ? PACKED_FULL
2562 : : PACKED_STATIC,
2563 : restricted);
2564 55138 : byref = 0;
2565 : }
2566 : }
2567 : else
2568 : {
2569 46977 : enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2570 46977 : if (sym->attr.pointer)
2571 7458 : akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2572 : : GFC_ARRAY_POINTER;
2573 39519 : else if (sym->attr.allocatable)
2574 12563 : akind = GFC_ARRAY_ALLOCATABLE;
2575 46977 : type = gfc_build_array_type (type, sym->as, akind, restricted,
2576 46977 : sym->attr.contiguous, sym->as->corank);
2577 : }
2578 : }
2579 : else
2580 : {
2581 323893 : if (sym->attr.allocatable || sym->attr.pointer
2582 642552 : || gfc_is_associate_pointer (sym))
2583 17657 : type = gfc_build_pointer_type (sym, type);
2584 : }
2585 :
2586 : /* We currently pass all parameters by reference.
2587 : See f95_get_function_decl. For dummy function parameters return the
2588 : function type. */
2589 434616 : if (byref)
2590 : {
2591 : /* We must use pointer types for potentially absent variables. The
2592 : optimizers assume a reference type argument is never NULL. */
2593 141942 : if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2594 141942 : || sym->attr.optional
2595 123057 : || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2596 20553 : type = build_pointer_type (type);
2597 : else
2598 121389 : type = build_reference_type (type);
2599 :
2600 141942 : if (restricted)
2601 134219 : type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2602 : }
2603 :
2604 : return (type);
2605 : }
2606 :
2607 : /* Layout and output debug info for a record type. */
2608 :
2609 : void
2610 352929 : gfc_finish_type (tree type)
2611 : {
2612 352929 : tree decl;
2613 :
2614 352929 : decl = build_decl (input_location,
2615 : TYPE_DECL, NULL_TREE, type);
2616 352929 : TYPE_STUB_DECL (type) = decl;
2617 352929 : layout_type (type);
2618 352929 : rest_of_type_compilation (type, 1);
2619 352929 : rest_of_decl_compilation (decl, 1, 0);
2620 352929 : }
2621 :
2622 : /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2623 : or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2624 : to the end of the field list pointed to by *CHAIN.
2625 :
2626 : Returns a pointer to the new field. */
2627 :
2628 : static tree
2629 5187957 : gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2630 : {
2631 5187957 : tree decl = build_decl (input_location, FIELD_DECL, name, type);
2632 :
2633 5187957 : DECL_CONTEXT (decl) = context;
2634 5187957 : DECL_CHAIN (decl) = NULL_TREE;
2635 5187957 : if (TYPE_FIELDS (context) == NULL_TREE)
2636 345480 : TYPE_FIELDS (context) = decl;
2637 5187957 : if (chain != NULL)
2638 : {
2639 5187957 : if (*chain != NULL)
2640 4842477 : **chain = decl;
2641 5187957 : *chain = &DECL_CHAIN (decl);
2642 : }
2643 :
2644 5187957 : return decl;
2645 : }
2646 :
2647 : /* Like `gfc_add_field_to_struct_1', but adds alignment
2648 : information. */
2649 :
2650 : tree
2651 4792769 : gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2652 : {
2653 4792769 : tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2654 :
2655 4792769 : DECL_INITIAL (decl) = 0;
2656 4792769 : SET_DECL_ALIGN (decl, 0);
2657 4792769 : DECL_USER_ALIGN (decl) = 0;
2658 :
2659 4792769 : return decl;
2660 : }
2661 :
2662 :
2663 : /* Copy the backend_decl and component backend_decls if
2664 : the two derived type symbols are "equal", as described
2665 : in 4.4.2 and resolved by gfc_compare_derived_types. */
2666 :
2667 : bool
2668 377350 : gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2669 : bool from_gsym)
2670 : {
2671 377350 : gfc_component *to_cm;
2672 377350 : gfc_component *from_cm;
2673 :
2674 377350 : if (from == to)
2675 : return 1;
2676 :
2677 330795 : if (from->backend_decl == NULL
2678 330795 : || !gfc_compare_derived_types (from, to))
2679 : return 0;
2680 :
2681 16095 : to->backend_decl = from->backend_decl;
2682 :
2683 16095 : to_cm = to->components;
2684 16095 : from_cm = from->components;
2685 :
2686 : /* Copy the component declarations. If a component is itself
2687 : a derived type, we need a copy of its component declarations.
2688 : This is done by recursing into gfc_get_derived_type and
2689 : ensures that the component's component declarations have
2690 : been built. If it is a character, we need the character
2691 : length, as well. */
2692 60790 : for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2693 : {
2694 44695 : to_cm->backend_decl = from_cm->backend_decl;
2695 44695 : to_cm->caf_token = from_cm->caf_token;
2696 44695 : if (from_cm->ts.type == BT_UNION)
2697 28 : gfc_get_union_type (to_cm->ts.u.derived);
2698 44667 : else if (from_cm->ts.type == BT_DERIVED
2699 15521 : && (!from_cm->attr.pointer || from_gsym))
2700 14155 : gfc_get_derived_type (to_cm->ts.u.derived);
2701 30512 : else if (from_cm->ts.type == BT_CLASS
2702 850 : && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2703 843 : gfc_get_derived_type (to_cm->ts.u.derived);
2704 29669 : else if (from_cm->ts.type == BT_CHARACTER)
2705 918 : to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2706 : }
2707 :
2708 : return 1;
2709 : }
2710 :
2711 :
2712 : /* Build a tree node for a procedure pointer component. */
2713 :
2714 : static tree
2715 34270 : gfc_get_ppc_type (gfc_component* c)
2716 : {
2717 34270 : tree t;
2718 :
2719 : /* Explicit interface. */
2720 34270 : if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2721 3698 : return build_pointer_type (gfc_get_function_type (c->ts.interface));
2722 :
2723 : /* Implicit interface (only return value may be known). */
2724 30572 : if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2725 9 : t = gfc_typenode_for_spec (&c->ts);
2726 : else
2727 30563 : t = void_type_node;
2728 :
2729 : /* FIXME: it would be better to provide explicit interfaces in all
2730 : cases, since they should be known by the compiler. */
2731 30572 : return build_pointer_type (build_function_type (t, NULL_TREE));
2732 : }
2733 :
2734 :
2735 : /* Build a tree node for a union type. Requires building each map
2736 : structure which is an element of the union. */
2737 :
2738 : tree
2739 252 : gfc_get_union_type (gfc_symbol *un)
2740 : {
2741 252 : gfc_component *map = NULL;
2742 252 : tree typenode = NULL, map_type = NULL, map_field = NULL;
2743 252 : tree *chain = NULL;
2744 :
2745 252 : if (un->backend_decl)
2746 : {
2747 130 : if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2748 : return un->backend_decl;
2749 : else
2750 : typenode = un->backend_decl;
2751 : }
2752 : else
2753 : {
2754 122 : typenode = make_node (UNION_TYPE);
2755 122 : TYPE_NAME (typenode) = get_identifier (un->name);
2756 : }
2757 :
2758 : /* Add each contained MAP as a field. */
2759 363 : for (map = un->components; map; map = map->next)
2760 : {
2761 238 : gcc_assert (map->ts.type == BT_DERIVED);
2762 :
2763 : /* The map's type node, which is defined within this union's context. */
2764 238 : map_type = gfc_get_derived_type (map->ts.u.derived);
2765 238 : TYPE_CONTEXT (map_type) = typenode;
2766 :
2767 : /* The map field's declaration. */
2768 238 : map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2769 : map_type, &chain);
2770 238 : if (GFC_LOCUS_IS_SET (map->loc))
2771 238 : gfc_set_decl_location (map_field, &map->loc);
2772 0 : else if (GFC_LOCUS_IS_SET (un->declared_at))
2773 0 : gfc_set_decl_location (map_field, &un->declared_at);
2774 :
2775 238 : DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2776 238 : DECL_NAMELESS(map_field) = true;
2777 :
2778 : /* We should never clobber another backend declaration for this map,
2779 : because each map component is unique. */
2780 238 : if (!map->backend_decl)
2781 238 : map->backend_decl = map_field;
2782 : }
2783 :
2784 125 : un->backend_decl = typenode;
2785 125 : gfc_finish_type (typenode);
2786 :
2787 125 : return typenode;
2788 : }
2789 :
2790 : bool
2791 179 : cobounds_match_decl (const gfc_symbol *derived)
2792 : {
2793 179 : tree arrtype, tmp;
2794 179 : gfc_array_spec *as;
2795 :
2796 179 : if (!derived->backend_decl)
2797 : return false;
2798 : /* Care only about coarray declarations. Everything else is ok with us. */
2799 179 : if (!derived->components || strcmp (derived->components->name, "_data") != 0)
2800 : return true;
2801 179 : if (!derived->components->attr.codimension)
2802 : return true;
2803 :
2804 179 : arrtype = TREE_TYPE (TYPE_FIELDS (derived->backend_decl));
2805 179 : as = derived->components->as;
2806 179 : if (GFC_TYPE_ARRAY_CORANK (arrtype) != as->corank)
2807 : return false;
2808 :
2809 231 : for (int dim = as->rank; dim < as->rank + as->corank; ++dim)
2810 : {
2811 : /* Check lower bound. */
2812 120 : tmp = TYPE_LANG_SPECIFIC (arrtype)->lbound[dim];
2813 120 : if (!tmp || !INTEGER_CST_P (tmp))
2814 : return false;
2815 120 : if (as->lower[dim]->expr_type != EXPR_CONSTANT
2816 120 : || as->lower[dim]->ts.type != BT_INTEGER)
2817 : return false;
2818 120 : if (*tmp->int_cst.val != mpz_get_si (as->lower[dim]->value.integer))
2819 : return false;
2820 :
2821 : /* Check upper bound. */
2822 114 : tmp = TYPE_LANG_SPECIFIC (arrtype)->ubound[dim];
2823 114 : if (!tmp && !as->upper[dim])
2824 111 : continue;
2825 :
2826 3 : if (!tmp || !INTEGER_CST_P (tmp))
2827 : return false;
2828 3 : if (as->upper[dim]->expr_type != EXPR_CONSTANT
2829 3 : || as->upper[dim]->ts.type != BT_INTEGER)
2830 : return false;
2831 3 : if (*tmp->int_cst.val != mpz_get_si (as->upper[dim]->value.integer))
2832 : return false;
2833 : }
2834 :
2835 : return true;
2836 : }
2837 :
2838 : /* Build a tree node for a derived type. If there are equal
2839 : derived types, with different local names, these are built
2840 : at the same time. If an equal derived type has been built
2841 : in a parent namespace, this is used. */
2842 :
2843 : tree
2844 204771 : gfc_get_derived_type (gfc_symbol * derived, int codimen)
2845 : {
2846 204771 : tree typenode = NULL, field = NULL, field_type = NULL;
2847 204771 : tree canonical = NULL_TREE;
2848 204771 : tree *chain = NULL;
2849 204771 : bool got_canonical = false;
2850 204771 : bool unlimited_entity = false;
2851 204771 : gfc_component *c;
2852 204771 : gfc_namespace *ns;
2853 204771 : tree tmp;
2854 204771 : bool coarray_flag, class_coarray_flag;
2855 :
2856 409542 : coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2857 204771 : && derived->module && !derived->attr.vtype;
2858 409542 : class_coarray_flag = derived->components
2859 191964 : && derived->components->ts.type == BT_DERIVED
2860 63786 : && strcmp (derived->components->name, "_data") == 0
2861 36566 : && derived->components->attr.codimension
2862 205450 : && derived->components->as->cotype == AS_EXPLICIT;
2863 :
2864 204771 : gcc_assert (!derived->attr.pdt_template);
2865 :
2866 204771 : if (derived->attr.unlimited_polymorphic
2867 200802 : || (flag_coarray == GFC_FCOARRAY_LIB
2868 4138 : && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2869 189 : && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2870 : || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2871 189 : || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2872 4158 : return ptr_type_node;
2873 :
2874 200613 : if (flag_coarray != GFC_FCOARRAY_LIB
2875 196664 : && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2876 448 : && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2877 448 : || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2878 331 : return gfc_get_int_type (gfc_default_integer_kind);
2879 :
2880 200282 : if (derived && derived->attr.flavor == FL_PROCEDURE
2881 52 : && derived->attr.generic)
2882 52 : derived = gfc_find_dt_in_generic (derived);
2883 :
2884 : /* See if it's one of the iso_c_binding derived types. */
2885 200282 : if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2886 : {
2887 12583 : if (derived->backend_decl)
2888 : return derived->backend_decl;
2889 :
2890 5401 : if (derived->intmod_sym_id == ISOCBINDING_PTR)
2891 2915 : derived->backend_decl = ptr_type_node;
2892 : else
2893 2486 : derived->backend_decl = pfunc_type_node;
2894 :
2895 5401 : derived->ts.kind = gfc_index_integer_kind;
2896 5401 : derived->ts.type = BT_INTEGER;
2897 : /* Set the f90_type to BT_VOID as a way to recognize something of type
2898 : BT_INTEGER that needs to fit a void * for the purpose of the
2899 : iso_c_binding derived types. */
2900 5401 : derived->ts.f90_type = BT_VOID;
2901 :
2902 5401 : return derived->backend_decl;
2903 : }
2904 :
2905 : /* If use associated, use the module type for this one. */
2906 187699 : if (derived->backend_decl == NULL
2907 46625 : && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2908 12272 : && derived->module
2909 199971 : && gfc_get_module_backend_decl (derived))
2910 11764 : goto copy_derived_types;
2911 :
2912 : /* The derived types from an earlier namespace can be used as the
2913 : canonical type. */
2914 175935 : if (derived->backend_decl == NULL
2915 34861 : && !derived->attr.use_assoc
2916 34374 : && !derived->attr.used_in_submodule
2917 34353 : && gfc_global_ns_list)
2918 : {
2919 8961 : for (ns = gfc_global_ns_list;
2920 43288 : ns->translated && !got_canonical;
2921 8961 : ns = ns->sibling)
2922 : {
2923 8961 : if (ns->derived_types)
2924 : {
2925 32681 : for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2926 : dt = dt->dt_next)
2927 : {
2928 32424 : gfc_copy_dt_decls_ifequal (dt, derived, true);
2929 32424 : if (derived->backend_decl)
2930 376 : got_canonical = true;
2931 32424 : if (dt->dt_next == ns->derived_types)
2932 : break;
2933 : }
2934 : }
2935 : }
2936 : }
2937 :
2938 : /* Store up the canonical type to be added to this one. */
2939 34327 : if (got_canonical)
2940 : {
2941 376 : if (TYPE_CANONICAL (derived->backend_decl))
2942 376 : canonical = TYPE_CANONICAL (derived->backend_decl);
2943 : else
2944 : canonical = derived->backend_decl;
2945 :
2946 376 : derived->backend_decl = NULL_TREE;
2947 : }
2948 :
2949 : /* derived->backend_decl != 0 means we saw it before, but its
2950 : components' backend_decl may have not been built. */
2951 175935 : if (derived->backend_decl
2952 175935 : && (!class_coarray_flag || cobounds_match_decl (derived)))
2953 : {
2954 : /* Its components' backend_decl have been built or we are
2955 : seeing recursion through the formal arglist of a procedure
2956 : pointer component. */
2957 141006 : if (TYPE_FIELDS (derived->backend_decl))
2958 : return derived->backend_decl;
2959 5100 : else if (derived->attr.abstract
2960 731 : && derived->attr.proc_pointer_comp)
2961 : {
2962 : /* If an abstract derived type with procedure pointer
2963 : components has no other type of component, return the
2964 : backend_decl. Otherwise build the components if any of the
2965 : non-procedure pointer components have no backend_decl. */
2966 1 : for (c = derived->components; c; c = c->next)
2967 : {
2968 2 : bool same_alloc_type = c->attr.allocatable
2969 1 : && derived == c->ts.u.derived;
2970 1 : if (!c->attr.proc_pointer
2971 1 : && !same_alloc_type
2972 1 : && c->backend_decl == NULL)
2973 : break;
2974 0 : else if (c->next == NULL)
2975 : return derived->backend_decl;
2976 : }
2977 : typenode = derived->backend_decl;
2978 : }
2979 : else
2980 : typenode = derived->backend_decl;
2981 : }
2982 : else
2983 : {
2984 : /* We see this derived type first time, so build the type node. */
2985 34929 : typenode = make_node (RECORD_TYPE);
2986 34929 : TYPE_NAME (typenode) = get_identifier (derived->name);
2987 34929 : TYPE_PACKED (typenode) = flag_pack_derived;
2988 34929 : derived->backend_decl = typenode;
2989 34929 : if (derived->attr.is_class)
2990 8189 : GFC_CLASS_TYPE_P (typenode) = 1;
2991 : }
2992 :
2993 40029 : if (derived->components
2994 32585 : && derived->components->ts.type == BT_DERIVED
2995 11744 : && startswith (derived->name, "__class")
2996 8241 : && strcmp (derived->components->name, "_data") == 0
2997 48270 : && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2998 : unlimited_entity = true;
2999 :
3000 : /* Go through the derived type components, building them as
3001 : necessary. The reason for doing this now is that it is
3002 : possible to recurse back to this derived type through a
3003 : pointer component (PR24092). If this happens, the fields
3004 : will be built and so we can return the type. */
3005 160195 : for (c = derived->components; c; c = c->next)
3006 : {
3007 120166 : if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
3008 108 : c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
3009 :
3010 120166 : if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
3011 78389 : continue;
3012 :
3013 41777 : const bool incomplete_type
3014 41777 : = c->ts.u.derived->backend_decl
3015 34595 : && TREE_CODE (c->ts.u.derived->backend_decl) == RECORD_TYPE
3016 74964 : && !(TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)
3017 19699 : && TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)->size);
3018 83554 : const bool pointer_component
3019 41777 : = c->attr.pointer || c->attr.allocatable || c->attr.proc_pointer;
3020 :
3021 : /* Prevent endless recursion on recursive types (i.e. types that reference
3022 : themself in a component. Break the recursion by not building pointers
3023 : to incomplete types again, aka types that are already in the build. */
3024 41777 : if (c->ts.u.derived->backend_decl == NULL
3025 34595 : || (c->attr.codimension && c->as->corank != codimen)
3026 34298 : || !(incomplete_type && pointer_component))
3027 : {
3028 10003 : int local_codim = c->attr.codimension ? c->as->corank: codimen;
3029 10003 : c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
3030 : local_codim);
3031 : }
3032 :
3033 41777 : if (c->ts.u.derived->attr.is_iso_c)
3034 : {
3035 : /* Need to copy the modified ts from the derived type. The
3036 : typespec was modified because C_PTR/C_FUNPTR are translated
3037 : into (void *) from derived types. */
3038 1 : c->ts.type = c->ts.u.derived->ts.type;
3039 1 : c->ts.kind = c->ts.u.derived->ts.kind;
3040 1 : c->ts.f90_type = c->ts.u.derived->ts.f90_type;
3041 1 : if (c->initializer)
3042 : {
3043 0 : c->initializer->ts.type = c->ts.type;
3044 0 : c->initializer->ts.kind = c->ts.kind;
3045 0 : c->initializer->ts.f90_type = c->ts.f90_type;
3046 0 : c->initializer->expr_type = EXPR_NULL;
3047 : }
3048 : }
3049 : }
3050 :
3051 40029 : if (!class_coarray_flag && TYPE_FIELDS (derived->backend_decl))
3052 : return derived->backend_decl;
3053 :
3054 : /* Build the type member list. Install the newly created RECORD_TYPE
3055 : node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
3056 : through only the top-level linked list of components so we correctly
3057 : build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
3058 : types are built as part of gfc_get_union_type. */
3059 159981 : for (c = derived->components; c; c = c->next)
3060 : {
3061 240054 : bool same_alloc_type = c->attr.allocatable
3062 120027 : && derived == c->ts.u.derived;
3063 : /* Prevent infinite recursion, when the procedure pointer type is
3064 : the same as derived, by forcing the procedure pointer component to
3065 : be built as if the explicit interface does not exist. */
3066 120027 : if (c->attr.proc_pointer
3067 34314 : && (c->ts.type != BT_DERIVED || (c->ts.u.derived
3068 215 : && !gfc_compare_derived_types (derived, c->ts.u.derived)))
3069 154317 : && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
3070 340 : && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
3071 34270 : field_type = gfc_get_ppc_type (c);
3072 85757 : else if (c->attr.proc_pointer && derived->backend_decl)
3073 : {
3074 44 : tmp = build_function_type (derived->backend_decl, NULL_TREE);
3075 44 : field_type = build_pointer_type (tmp);
3076 : }
3077 85713 : else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
3078 41090 : field_type = c->ts.u.derived->backend_decl;
3079 44623 : else if (c->attr.caf_token)
3080 661 : field_type = pvoid_type_node;
3081 : else
3082 : {
3083 43962 : if (c->ts.type == BT_CHARACTER
3084 2089 : && !c->ts.deferred && !c->attr.pdt_string)
3085 : {
3086 : /* Evaluate the string length. */
3087 1536 : gfc_conv_const_charlen (c->ts.u.cl);
3088 1536 : gcc_assert (c->ts.u.cl->backend_decl);
3089 : }
3090 42426 : else if (c->ts.type == BT_CHARACTER)
3091 553 : c->ts.u.cl->backend_decl
3092 553 : = build_int_cst (gfc_charlen_type_node, 0);
3093 :
3094 43962 : field_type = gfc_typenode_for_spec (&c->ts, codimen);
3095 : }
3096 :
3097 : /* This returns an array descriptor type. Initialization may be
3098 : required. */
3099 120027 : if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
3100 : {
3101 9813 : if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
3102 : {
3103 7869 : enum gfc_array_kind akind;
3104 7869 : bool is_ptr = ((c == derived->components
3105 5174 : && derived->components->ts.type == BT_DERIVED
3106 3838 : && startswith (derived->name, "__class")
3107 3225 : && (strcmp (derived->components->name, "_data")
3108 : == 0))
3109 13043 : ? c->attr.class_pointer : c->attr.pointer);
3110 7869 : if (is_ptr)
3111 1972 : akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
3112 : : GFC_ARRAY_POINTER;
3113 5897 : else if (c->attr.allocatable)
3114 : akind = GFC_ARRAY_ALLOCATABLE;
3115 1548 : else if (c->as->type == AS_ASSUMED_RANK)
3116 : akind = GFC_ARRAY_ASSUMED_RANK;
3117 : else
3118 : /* FIXME – see PR fortran/104651. Additionally, the following
3119 : gfc_build_array_type should use !is_ptr instead of
3120 : c->attr.pointer and codim unconditionally without '? :'. */
3121 1392 : akind = GFC_ARRAY_ASSUMED_SHAPE;
3122 : /* Pointers to arrays aren't actually pointer types. The
3123 : descriptors are separate, but the data is common. Every
3124 : array pointer in a coarray derived type needs to provide space
3125 : for the coarray management, too. Therefore treat coarrays
3126 : and pointers to coarrays in derived types the same. */
3127 7869 : field_type = gfc_build_array_type
3128 10948 : (
3129 : field_type, c->as, akind, !c->attr.target && !c->attr.pointer,
3130 : c->attr.contiguous,
3131 7869 : c->attr.codimension || c->attr.pointer ? codimen : 0
3132 : );
3133 7869 : }
3134 : else
3135 1944 : field_type = gfc_get_nodesc_array_type (field_type, c->as,
3136 : PACKED_STATIC,
3137 : !c->attr.target);
3138 : }
3139 110214 : else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
3140 36502 : && !c->attr.proc_pointer
3141 36336 : && !(unlimited_entity && c == derived->components))
3142 35765 : field_type = build_pointer_type (field_type);
3143 :
3144 120027 : if (c->attr.pointer || same_alloc_type)
3145 36772 : field_type = gfc_nonrestricted_type (field_type);
3146 :
3147 : /* vtype fields can point to different types to the base type. */
3148 120027 : if (c->ts.type == BT_DERIVED
3149 40379 : && c->ts.u.derived && c->ts.u.derived->attr.vtype)
3150 17717 : field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
3151 : ptr_mode, true);
3152 :
3153 120027 : field = gfc_add_field_to_struct (typenode,
3154 : get_identifier (c->name),
3155 : field_type, &chain);
3156 120027 : if (GFC_LOCUS_IS_SET (c->loc))
3157 120027 : gfc_set_decl_location (field, &c->loc);
3158 0 : else if (GFC_LOCUS_IS_SET (derived->declared_at))
3159 0 : gfc_set_decl_location (field, &derived->declared_at);
3160 :
3161 120027 : gfc_finish_decl_attrs (field, &c->attr);
3162 :
3163 120027 : DECL_PACKED (field) |= TYPE_PACKED (typenode);
3164 :
3165 120027 : gcc_assert (field);
3166 : /* Overwrite for class array to supply different bounds for different
3167 : types. */
3168 120027 : if (class_coarray_flag || !c->backend_decl || c->attr.caf_token)
3169 118945 : c->backend_decl = field;
3170 :
3171 120027 : if ((c->attr.dimension || c->attr.codimension)
3172 9925 : && ((derived->attr.is_class && c->attr.class_pointer)
3173 9186 : || (!derived->attr.is_class && c->attr.pointer)))
3174 1982 : GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
3175 : }
3176 :
3177 : /* Now lay out the derived type, including the fields. */
3178 39954 : if (canonical)
3179 376 : TYPE_CANONICAL (typenode) = canonical;
3180 :
3181 39954 : gfc_finish_type (typenode);
3182 39954 : gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
3183 39954 : if (derived->module && derived->ns->proc_name
3184 21963 : && derived->ns->proc_name->attr.flavor == FL_MODULE)
3185 : {
3186 20592 : if (derived->ns->proc_name->backend_decl
3187 20565 : && TREE_CODE (derived->ns->proc_name->backend_decl)
3188 : == NAMESPACE_DECL)
3189 : {
3190 20565 : TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
3191 20565 : DECL_CONTEXT (TYPE_STUB_DECL (typenode))
3192 41130 : = derived->ns->proc_name->backend_decl;
3193 : }
3194 : }
3195 :
3196 39954 : derived->backend_decl = typenode;
3197 :
3198 51718 : copy_derived_types:
3199 :
3200 51718 : if (!derived->attr.vtype)
3201 98108 : for (c = derived->components; c; c = c->next)
3202 : {
3203 : /* Do not add a caf_token field for class container components. */
3204 59257 : if (codimen && coarray_flag && !c->attr.dimension
3205 4 : && !c->attr.codimension && (c->attr.allocatable || c->attr.pointer)
3206 1 : && !derived->attr.is_class)
3207 : {
3208 : /* Provide sufficient space to hold "_caf_symbol". */
3209 1 : char caf_name[GFC_MAX_SYMBOL_LEN + 6];
3210 1 : gfc_component *token;
3211 1 : snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
3212 1 : token = gfc_find_component (derived, caf_name, true, true, NULL);
3213 1 : gcc_assert (token);
3214 1 : gfc_comp_caf_token (c) = token->backend_decl;
3215 1 : suppress_warning (gfc_comp_caf_token (c));
3216 : }
3217 : }
3218 :
3219 332378 : for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
3220 : {
3221 331353 : gfc_copy_dt_decls_ifequal (derived, dt, false);
3222 331353 : if (dt->dt_next == gfc_derived_types)
3223 : break;
3224 : }
3225 :
3226 51718 : if (derived->attr.is_class)
3227 10822 : GFC_CLASS_TYPE_P (derived->backend_decl) = 1;
3228 :
3229 51718 : return derived->backend_decl;
3230 : }
3231 :
3232 :
3233 : bool
3234 998808 : gfc_return_by_reference (gfc_symbol * sym)
3235 : {
3236 998808 : if (!sym->attr.function)
3237 : return 0;
3238 :
3239 505427 : if (sym->attr.dimension)
3240 : return 1;
3241 :
3242 429539 : if (sym->ts.type == BT_CHARACTER
3243 23643 : && !sym->attr.is_bind_c
3244 22941 : && (!sym->attr.result
3245 16 : || !sym->ns->proc_name
3246 16 : || !sym->ns->proc_name->attr.is_bind_c))
3247 : return 1;
3248 :
3249 : /* Possibly return complex numbers by reference for g77 compatibility.
3250 : We don't do this for calls to intrinsics (as the library uses the
3251 : -fno-f2c calling convention) except for calls to specific wrappers
3252 : (_gfortran_f2c_specific_*), nor for calls to functions which always
3253 : require an explicit interface, as no compatibility problems can
3254 : arise there. */
3255 406598 : if (flag_f2c && sym->ts.type == BT_COMPLEX
3256 1780 : && !sym->attr.pointer
3257 1330 : && !sym->attr.allocatable
3258 1168 : && !sym->attr.always_explicit)
3259 1012 : return 1;
3260 :
3261 : return 0;
3262 : }
3263 :
3264 : static tree
3265 214 : gfc_get_entry_result_type (gfc_symbol *sym)
3266 : {
3267 214 : tree type;
3268 :
3269 214 : type = gfc_sym_type (sym->result);
3270 :
3271 : /* Mixed ENTRY master unions must use the ABI return type of each entry.
3272 : Under -ff2c, default REAL entries return C double even though their
3273 : Fortran result symbol remains default REAL. */
3274 214 : if (flag_f2c
3275 2 : && sym->ts.type == BT_REAL
3276 1 : && sym->ts.kind == gfc_default_real_kind
3277 1 : && !sym->attr.pointer
3278 1 : && !sym->attr.allocatable
3279 1 : && !sym->attr.always_explicit)
3280 1 : type = gfc_get_real_type (gfc_default_double_kind);
3281 :
3282 214 : return type;
3283 : }
3284 :
3285 : static tree
3286 98 : gfc_get_mixed_entry_union (gfc_namespace *ns)
3287 : {
3288 98 : tree type;
3289 98 : tree *chain = NULL;
3290 98 : char name[GFC_MAX_SYMBOL_LEN + 1];
3291 98 : gfc_entry_list *el, *el2;
3292 :
3293 98 : gcc_assert (ns->proc_name->attr.mixed_entry_master);
3294 98 : gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
3295 :
3296 98 : snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
3297 :
3298 : /* Build the type node. */
3299 98 : type = make_node (UNION_TYPE);
3300 :
3301 98 : TYPE_NAME (type) = get_identifier (name);
3302 :
3303 312 : for (el = ns->entries; el; el = el->next)
3304 : {
3305 : /* Search for duplicates. */
3306 348 : for (el2 = ns->entries; el2 != el; el2 = el2->next)
3307 134 : if (el2->sym->result == el->sym->result)
3308 : break;
3309 :
3310 214 : if (el == el2)
3311 428 : gfc_add_field_to_struct_1 (type,
3312 214 : get_identifier (el->sym->result->name),
3313 : gfc_get_entry_result_type (el->sym),
3314 : &chain);
3315 : }
3316 :
3317 : /* Finish off the type. */
3318 98 : gfc_finish_type (type);
3319 98 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
3320 98 : return type;
3321 : }
3322 :
3323 : /* Create a "fn spec" based on the formal arguments;
3324 : cf. create_function_arglist. */
3325 :
3326 : static tree
3327 114110 : create_fn_spec (gfc_symbol *sym, tree fntype)
3328 : {
3329 114110 : char spec[150];
3330 114110 : size_t spec_len;
3331 114110 : gfc_formal_arglist *f;
3332 114110 : tree tmp;
3333 :
3334 114110 : memset (&spec, 0, sizeof (spec));
3335 114110 : spec[0] = '.';
3336 114110 : spec[1] = ' ';
3337 114110 : spec_len = 2;
3338 :
3339 114110 : if (sym->attr.entry_master)
3340 : {
3341 667 : spec[spec_len++] = 'R';
3342 667 : spec[spec_len++] = ' ';
3343 : }
3344 114110 : if (gfc_return_by_reference (sym))
3345 : {
3346 10828 : gfc_symbol *result = sym->result ? sym->result : sym;
3347 :
3348 10828 : if (result->attr.pointer || sym->attr.proc_pointer)
3349 : {
3350 424 : spec[spec_len++] = '.';
3351 424 : spec[spec_len++] = ' ';
3352 : }
3353 : else
3354 : {
3355 10404 : spec[spec_len++] = 'w';
3356 10404 : spec[spec_len++] = ' ';
3357 : }
3358 10828 : if (sym->ts.type == BT_CHARACTER)
3359 : {
3360 2960 : if (!sym->ts.u.cl->length
3361 1573 : && (sym->attr.allocatable || sym->attr.pointer))
3362 318 : spec[spec_len++] = 'w';
3363 : else
3364 2642 : spec[spec_len++] = 'R';
3365 2960 : spec[spec_len++] = ' ';
3366 : }
3367 : }
3368 :
3369 268189 : for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3370 154079 : if (spec_len < sizeof (spec))
3371 : {
3372 154079 : bool is_class = false;
3373 154079 : bool is_pointer = false;
3374 :
3375 154079 : if (f->sym)
3376 : {
3377 10355 : is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
3378 164330 : && f->sym->attr.class_ok;
3379 153975 : is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
3380 143620 : : f->sym->attr.pointer;
3381 : }
3382 :
3383 154079 : if (f->sym == NULL || is_pointer || f->sym->attr.target
3384 146577 : || f->sym->attr.external || f->sym->attr.cray_pointer
3385 146064 : || (f->sym->ts.type == BT_DERIVED
3386 28988 : && (f->sym->ts.u.derived->attr.proc_pointer_comp
3387 28265 : || f->sym->ts.u.derived->attr.pointer_comp))
3388 142973 : || (is_class
3389 9350 : && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3390 8617 : || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3391 141508 : || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3392 : {
3393 20739 : spec[spec_len++] = '.';
3394 20739 : spec[spec_len++] = ' ';
3395 : }
3396 133340 : else if (f->sym->attr.intent == INTENT_IN)
3397 : {
3398 63713 : spec[spec_len++] = 'r';
3399 63713 : spec[spec_len++] = ' ';
3400 : }
3401 69627 : else if (f->sym)
3402 : {
3403 69627 : spec[spec_len++] = 'w';
3404 69627 : spec[spec_len++] = ' ';
3405 : }
3406 : }
3407 :
3408 114110 : tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3409 114110 : tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3410 114110 : return build_type_attribute_variant (fntype, tmp);
3411 : }
3412 :
3413 :
3414 : /* NOTE: The returned function type must match the argument list created by
3415 : create_function_arglist. */
3416 :
3417 : tree
3418 116275 : gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3419 : const char *fnspec)
3420 : {
3421 116275 : tree type;
3422 116275 : vec<tree, va_gc> *typelist = NULL;
3423 116275 : vec<tree, va_gc> *hidden_typelist = NULL;
3424 116275 : gfc_formal_arglist *f;
3425 116275 : gfc_symbol *arg;
3426 116275 : int alternate_return = 0;
3427 116275 : bool is_varargs = true;
3428 :
3429 : /* Make sure this symbol is a function, a subroutine or the main
3430 : program. */
3431 116275 : gcc_assert (sym->attr.flavor == FL_PROCEDURE
3432 : || sym->attr.flavor == FL_PROGRAM);
3433 :
3434 : /* To avoid recursing infinitely on recursive types, we use error_mark_node
3435 : so that they can be detected here and handled further down. */
3436 116275 : if (sym->backend_decl == NULL)
3437 116006 : sym->backend_decl = error_mark_node;
3438 269 : else if (sym->backend_decl == error_mark_node)
3439 53 : goto arg_type_list_done;
3440 216 : else if (sym->attr.proc_pointer)
3441 0 : return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3442 : else
3443 216 : return TREE_TYPE (sym->backend_decl);
3444 :
3445 116006 : if (sym->attr.entry_master)
3446 : /* Additional parameter for selecting an entry point. */
3447 667 : vec_safe_push (typelist, gfc_array_index_type);
3448 :
3449 116006 : if (sym->result)
3450 34853 : arg = sym->result;
3451 : else
3452 : arg = sym;
3453 :
3454 116006 : if (arg->ts.type == BT_CHARACTER)
3455 3287 : gfc_conv_const_charlen (arg->ts.u.cl);
3456 :
3457 : /* Some functions we use an extra parameter for the return value. */
3458 116006 : if (gfc_return_by_reference (sym))
3459 : {
3460 12622 : type = gfc_sym_type (arg);
3461 12622 : if (arg->ts.type == BT_COMPLEX
3462 12179 : || arg->attr.dimension
3463 1697 : || arg->ts.type == BT_CHARACTER)
3464 12622 : type = build_reference_type (type);
3465 :
3466 12622 : vec_safe_push (typelist, type);
3467 12622 : if (arg->ts.type == BT_CHARACTER)
3468 : {
3469 3200 : if (!arg->ts.deferred)
3470 : /* Transfer by value. */
3471 2834 : vec_safe_push (typelist, gfc_charlen_type_node);
3472 : else
3473 : /* Deferred character lengths are transferred by reference
3474 : so that the value can be returned. */
3475 366 : vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3476 : }
3477 : }
3478 116006 : if (sym->backend_decl == error_mark_node && actual_args != NULL
3479 16307 : && sym->ts.interface == NULL
3480 16301 : && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3481 1119 : || sym->attr.proc == PROC_UNKNOWN))
3482 797 : gfc_get_formal_from_actual_arglist (sym, actual_args);
3483 :
3484 : /* Build the argument types for the function. */
3485 275775 : for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3486 : {
3487 159769 : arg = f->sym;
3488 159769 : if (arg)
3489 : {
3490 : /* Evaluate constant character lengths here so that they can be
3491 : included in the type. */
3492 159665 : if (arg->ts.type == BT_CHARACTER)
3493 12974 : gfc_conv_const_charlen (arg->ts.u.cl);
3494 :
3495 159665 : if (arg->attr.flavor == FL_PROCEDURE)
3496 : {
3497 1040 : type = gfc_get_function_type (arg);
3498 1040 : type = build_pointer_type (type);
3499 : }
3500 : else
3501 158625 : type = gfc_sym_type (arg, sym->attr.is_bind_c);
3502 :
3503 : /* Parameter Passing Convention
3504 :
3505 : We currently pass all parameters by reference.
3506 : Parameters with INTENT(IN) could be passed by value.
3507 : The problem arises if a function is called via an implicit
3508 : prototype. In this situation the INTENT is not known.
3509 : For this reason all parameters to global functions must be
3510 : passed by reference. Passing by value would potentially
3511 : generate bad code. Worse there would be no way of telling that
3512 : this code was bad, except that it would give incorrect results.
3513 :
3514 : Contained procedures could pass by value as these are never
3515 : used without an explicit interface, and cannot be passed as
3516 : actual parameters for a dummy procedure. */
3517 :
3518 159665 : vec_safe_push (typelist, type);
3519 : }
3520 : else
3521 : {
3522 104 : if (sym->attr.subroutine)
3523 159769 : alternate_return = 1;
3524 : }
3525 : }
3526 :
3527 : /* Add hidden arguments. */
3528 275775 : for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3529 : {
3530 159769 : arg = f->sym;
3531 : /* Add hidden string length parameters. */
3532 159769 : if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3533 : {
3534 10881 : if (!arg->ts.deferred)
3535 : /* Transfer by value. */
3536 10007 : type = gfc_charlen_type_node;
3537 : else
3538 : /* Deferred character lengths are transferred by reference
3539 : so that the value can be returned. */
3540 874 : type = build_pointer_type (gfc_charlen_type_node);
3541 :
3542 10881 : vec_safe_push (hidden_typelist, type);
3543 : }
3544 148888 : else if (gfc_pdt_requires_hidden_arg (arg) && arg->param_list)
3545 : {
3546 120 : if (arg->ts.type == BT_CLASS)
3547 36 : type = gfc_typenode_for_spec (&CLASS_DATA(arg)->ts);
3548 : else
3549 84 : type = gfc_typenode_for_spec (&arg->ts);
3550 120 : type = build_pointer_type (type);
3551 120 : vec_safe_push (hidden_typelist, type);
3552 : }
3553 :
3554 : /* For scalar intrinsic types or derived types, VALUE passes the value,
3555 : hence, the optional status cannot be transferred via a NULL pointer.
3556 : Thus, we will use a hidden argument in that case. */
3557 159769 : if (arg
3558 159665 : && arg->attr.optional
3559 20528 : && arg->attr.value
3560 573 : && !arg->attr.dimension
3561 537 : && arg->ts.type != BT_CLASS)
3562 537 : vec_safe_push (typelist, boolean_type_node);
3563 : /* Coarrays which are descriptorless or assumed-shape pass with
3564 : -fcoarray=lib the token and the offset as hidden arguments. */
3565 537 : if (arg
3566 159665 : && flag_coarray == GFC_FCOARRAY_LIB
3567 7659 : && ((arg->ts.type != BT_CLASS
3568 7628 : && arg->attr.codimension
3569 1653 : && !arg->attr.allocatable)
3570 6038 : || (arg->ts.type == BT_CLASS
3571 31 : && CLASS_DATA (arg)->attr.codimension
3572 24 : && !CLASS_DATA (arg)->attr.allocatable)))
3573 : {
3574 1641 : vec_safe_push (hidden_typelist, pvoid_type_node); /* caf_token. */
3575 1641 : vec_safe_push (hidden_typelist, gfc_array_index_type); /* caf_offset. */
3576 : }
3577 : }
3578 :
3579 : /* Put hidden character length, caf_token, caf_offset at the end. */
3580 125138 : vec_safe_reserve (typelist, vec_safe_length (hidden_typelist));
3581 116006 : vec_safe_splice (typelist, hidden_typelist);
3582 :
3583 116006 : if (!vec_safe_is_empty (typelist)
3584 44583 : || sym->attr.is_main_program
3585 17349 : || sym->attr.if_source != IFSRC_UNKNOWN)
3586 : is_varargs = false;
3587 :
3588 116006 : if (sym->backend_decl == error_mark_node)
3589 116006 : sym->backend_decl = NULL_TREE;
3590 :
3591 116059 : arg_type_list_done:
3592 :
3593 116059 : if (alternate_return)
3594 74 : type = integer_type_node;
3595 115985 : else if (!sym->attr.function || gfc_return_by_reference (sym))
3596 93321 : type = void_type_node;
3597 22664 : else if (sym->attr.mixed_entry_master)
3598 98 : type = gfc_get_mixed_entry_union (sym->ns);
3599 22566 : else if (flag_f2c && sym->ts.type == BT_REAL
3600 389 : && sym->ts.kind == gfc_default_real_kind
3601 215 : && !sym->attr.pointer
3602 190 : && !sym->attr.allocatable
3603 172 : && !sym->attr.always_explicit)
3604 : {
3605 : /* Special case: f2c calling conventions require that (scalar)
3606 : default REAL functions return the C type double instead. f2c
3607 : compatibility is only an issue with functions that don't
3608 : require an explicit interface, as only these could be
3609 : implemented in Fortran 77. */
3610 172 : sym->ts.kind = gfc_default_double_kind;
3611 172 : type = gfc_typenode_for_spec (&sym->ts);
3612 172 : sym->ts.kind = gfc_default_real_kind;
3613 : }
3614 22394 : else if (sym->result && sym->result->attr.proc_pointer)
3615 : /* Procedure pointer return values. */
3616 : {
3617 497 : if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3618 : {
3619 : /* Unset proc_pointer as gfc_get_function_type
3620 : is called recursively. */
3621 166 : sym->result->attr.proc_pointer = 0;
3622 166 : type = build_pointer_type (gfc_get_function_type (sym->result));
3623 166 : sym->result->attr.proc_pointer = 1;
3624 : }
3625 : else
3626 331 : type = gfc_sym_type (sym->result);
3627 : }
3628 : else
3629 21897 : type = gfc_sym_type (sym);
3630 :
3631 116059 : if (is_varargs)
3632 : /* This should be represented as an unprototyped type, not a type
3633 : with (...) prototype. */
3634 1980 : type = build_function_type (type, NULL_TREE);
3635 : else
3636 256925 : type = build_function_type_vec (type, typelist);
3637 :
3638 : /* If we were passed an fn spec, add it here, otherwise determine it from
3639 : the formal arguments. */
3640 116059 : if (fnspec)
3641 : {
3642 1949 : tree tmp;
3643 1949 : int spec_len = strlen (fnspec);
3644 1949 : tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3645 1949 : tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3646 1949 : type = build_type_attribute_variant (type, tmp);
3647 : }
3648 : else
3649 114110 : type = create_fn_spec (sym, type);
3650 :
3651 116059 : return type;
3652 : }
3653 :
3654 : /* Language hooks for middle-end access to type nodes. */
3655 :
3656 : /* Return an integer type with BITS bits of precision,
3657 : that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3658 :
3659 : tree
3660 702091 : gfc_type_for_size (unsigned bits, int unsignedp)
3661 : {
3662 702091 : if (!unsignedp)
3663 : {
3664 : int i;
3665 476359 : for (i = 0; i <= MAX_INT_KINDS; ++i)
3666 : {
3667 476329 : tree type = gfc_integer_types[i];
3668 476329 : if (type && bits == TYPE_PRECISION (type))
3669 : return type;
3670 : }
3671 :
3672 : /* Handle TImode as a special case because it is used by some backends
3673 : (e.g. ARM) even though it is not available for normal use. */
3674 : #if HOST_BITS_PER_WIDE_INT >= 64
3675 30 : if (bits == TYPE_PRECISION (intTI_type_node))
3676 : return intTI_type_node;
3677 : #endif
3678 :
3679 30 : if (bits <= TYPE_PRECISION (intQI_type_node))
3680 : return intQI_type_node;
3681 0 : if (bits <= TYPE_PRECISION (intHI_type_node))
3682 : return intHI_type_node;
3683 0 : if (bits <= TYPE_PRECISION (intSI_type_node))
3684 : return intSI_type_node;
3685 0 : if (bits <= TYPE_PRECISION (intDI_type_node))
3686 : return intDI_type_node;
3687 0 : if (bits <= TYPE_PRECISION (intTI_type_node))
3688 0 : return intTI_type_node;
3689 : }
3690 : else
3691 : {
3692 562776 : if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3693 : return unsigned_intQI_type_node;
3694 529367 : if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3695 : return unsigned_intHI_type_node;
3696 496377 : if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3697 : return unsigned_intSI_type_node;
3698 455404 : if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3699 : return unsigned_intDI_type_node;
3700 32745 : if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3701 32745 : return unsigned_intTI_type_node;
3702 : }
3703 :
3704 : return NULL_TREE;
3705 : }
3706 :
3707 : /* Return a data type that has machine mode MODE. If the mode is an
3708 : integer, then UNSIGNEDP selects between signed and unsigned types. */
3709 :
3710 : tree
3711 718506 : gfc_type_for_mode (machine_mode mode, int unsignedp)
3712 : {
3713 718506 : int i;
3714 718506 : tree *base;
3715 718506 : scalar_int_mode int_mode;
3716 :
3717 718506 : if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3718 : base = gfc_real_types;
3719 710220 : else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3720 : base = gfc_complex_types;
3721 514162 : else if (is_a <scalar_int_mode> (mode, &int_mode))
3722 : {
3723 513670 : tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3724 513670 : return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3725 : }
3726 492 : else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3727 492 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3728 : {
3729 0 : unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
3730 : GET_MODE_NUNITS (mode));
3731 0 : tree bool_type = build_nonstandard_boolean_type (elem_bits);
3732 0 : return build_vector_type_for_mode (bool_type, mode);
3733 : }
3734 5 : else if (VECTOR_MODE_P (mode)
3735 66331 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3736 : {
3737 487 : machine_mode inner_mode = GET_MODE_INNER (mode);
3738 487 : tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3739 487 : if (inner_type != NULL_TREE)
3740 487 : return build_vector_type_for_mode (inner_type, mode);
3741 : return NULL_TREE;
3742 : }
3743 : else
3744 : return NULL_TREE;
3745 :
3746 800365 : for (i = 0; i <= MAX_REAL_KINDS; ++i)
3747 : {
3748 735019 : tree type = base[i];
3749 735019 : if (type && mode == TYPE_MODE (type))
3750 : return type;
3751 : }
3752 :
3753 : return NULL_TREE;
3754 : }
3755 :
3756 : /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3757 : in that case. */
3758 :
3759 : bool
3760 434438 : gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3761 : {
3762 434438 : int rank, dim;
3763 434438 : bool indirect = false;
3764 434438 : tree etype, ptype, t, base_decl;
3765 434438 : tree data_off, span_off, dim_off, rank_off, dim_size, elem_size;
3766 434438 : tree lower_suboff, upper_suboff, stride_suboff;
3767 :
3768 434438 : if (! GFC_DESCRIPTOR_TYPE_P (type))
3769 : {
3770 277442 : if (! POINTER_TYPE_P (type))
3771 : return false;
3772 173176 : type = TREE_TYPE (type);
3773 173176 : if (! GFC_DESCRIPTOR_TYPE_P (type))
3774 : return false;
3775 : indirect = true;
3776 : }
3777 :
3778 307250 : rank = GFC_TYPE_ARRAY_RANK (type);
3779 307250 : if (rank >= (int) (ARRAY_SIZE (info->dimen)))
3780 : return false;
3781 :
3782 307250 : etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3783 307250 : gcc_assert (POINTER_TYPE_P (etype));
3784 307250 : etype = TREE_TYPE (etype);
3785 :
3786 : /* If the type is not a scalar coarray. */
3787 307250 : if (TREE_CODE (etype) == ARRAY_TYPE)
3788 307224 : etype = TREE_TYPE (etype);
3789 :
3790 : /* Can't handle variable sized elements yet. */
3791 307250 : if (int_size_in_bytes (etype) <= 0)
3792 : return false;
3793 : /* Nor non-constant lower bounds in assumed shape arrays. */
3794 285540 : if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3795 285540 : || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3796 : {
3797 87697 : for (dim = 0; dim < rank; dim++)
3798 54058 : if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3799 54058 : || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3800 : return false;
3801 : }
3802 :
3803 285284 : memset (info, '\0', sizeof (*info));
3804 285284 : info->ndimensions = rank;
3805 285284 : info->ordering = array_descr_ordering_column_major;
3806 285284 : info->element_type = etype;
3807 285284 : ptype = build_pointer_type (gfc_array_index_type);
3808 285284 : base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3809 285284 : if (!base_decl)
3810 : {
3811 414747 : base_decl = build_debug_expr_decl (indirect
3812 138241 : ? build_pointer_type (ptype) : ptype);
3813 276506 : GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3814 : }
3815 285284 : info->base_decl = base_decl;
3816 285284 : if (indirect)
3817 139859 : base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3818 :
3819 285284 : gfc_get_descriptor_offsets_for_info (type, &data_off, &rank_off, &span_off,
3820 : &dim_off, &dim_size, &stride_suboff,
3821 : &lower_suboff, &upper_suboff);
3822 :
3823 285284 : t = fold_build_pointer_plus (base_decl, span_off);
3824 285284 : elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3825 :
3826 285284 : t = base_decl;
3827 285284 : if (!integer_zerop (data_off))
3828 0 : t = fold_build_pointer_plus (t, data_off);
3829 285284 : t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3830 285284 : info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3831 285284 : enum gfc_array_kind akind = GFC_TYPE_ARRAY_AKIND (type);
3832 285284 : if (akind == GFC_ARRAY_ALLOCATABLE
3833 285284 : || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE)
3834 35241 : info->allocated = build2 (NE_EXPR, logical_type_node,
3835 : info->data_location, null_pointer_node);
3836 250043 : else if (akind == GFC_ARRAY_POINTER
3837 250043 : || akind == GFC_ARRAY_POINTER_CONT
3838 250043 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
3839 231948 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
3840 18095 : info->associated = build2 (NE_EXPR, logical_type_node,
3841 : info->data_location, null_pointer_node);
3842 285284 : if ((akind == GFC_ARRAY_ASSUMED_RANK
3843 : || akind == GFC_ARRAY_ASSUMED_RANK_CONT
3844 : || akind == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE
3845 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER
3846 285284 : || akind == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT)
3847 15387 : && dwarf_version >= 5)
3848 : {
3849 15387 : rank = 1;
3850 15387 : info->ndimensions = 1;
3851 15387 : t = fold_build_pointer_plus (base_decl, rank_off);
3852 15387 : t = build1 (NOP_EXPR, build_pointer_type (signed_char_type_node), t);
3853 15387 : t = build1 (INDIRECT_REF, signed_char_type_node, t);
3854 15387 : info->rank = t;
3855 15387 : t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3856 15387 : t = size_binop (MULT_EXPR, t, dim_size);
3857 15387 : dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3858 : }
3859 :
3860 705049 : for (dim = 0; dim < rank; dim++)
3861 : {
3862 419765 : t = fold_build_pointer_plus (base_decl,
3863 : size_binop (PLUS_EXPR,
3864 : dim_off, lower_suboff));
3865 419765 : t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3866 419765 : info->dimen[dim].lower_bound = t;
3867 419765 : t = fold_build_pointer_plus (base_decl,
3868 : size_binop (PLUS_EXPR,
3869 : dim_off, upper_suboff));
3870 419765 : t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3871 419765 : info->dimen[dim].upper_bound = t;
3872 419765 : if (akind == GFC_ARRAY_ASSUMED_SHAPE
3873 419765 : || akind == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3874 : {
3875 : /* Assumed shape arrays have known lower bounds. */
3876 53802 : info->dimen[dim].upper_bound
3877 53802 : = build2 (MINUS_EXPR, gfc_array_index_type,
3878 : info->dimen[dim].upper_bound,
3879 : info->dimen[dim].lower_bound);
3880 53802 : info->dimen[dim].lower_bound
3881 53802 : = fold_convert (gfc_array_index_type,
3882 : GFC_TYPE_ARRAY_LBOUND (type, dim));
3883 53802 : info->dimen[dim].upper_bound
3884 53802 : = build2 (PLUS_EXPR, gfc_array_index_type,
3885 : info->dimen[dim].lower_bound,
3886 : info->dimen[dim].upper_bound);
3887 : }
3888 419765 : t = fold_build_pointer_plus (base_decl,
3889 : size_binop (PLUS_EXPR,
3890 : dim_off, stride_suboff));
3891 419765 : t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3892 419765 : t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3893 419765 : info->dimen[dim].stride = t;
3894 419765 : if (dim + 1 < rank)
3895 134507 : dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3896 : }
3897 :
3898 : return true;
3899 : }
3900 :
3901 :
3902 : /* Create a type to handle vector subscripts for coarray library calls. It
3903 : has the form:
3904 : struct caf_vector_t {
3905 : size_t nvec; // size of the vector
3906 : union {
3907 : struct {
3908 : void *vector;
3909 : int kind;
3910 : } v;
3911 : struct {
3912 : ptrdiff_t lower_bound;
3913 : ptrdiff_t upper_bound;
3914 : ptrdiff_t stride;
3915 : } triplet;
3916 : } u;
3917 : }
3918 : where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3919 : size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3920 :
3921 : tree
3922 0 : gfc_get_caf_vector_type (int dim)
3923 : {
3924 0 : static tree vector_types[GFC_MAX_DIMENSIONS];
3925 0 : static tree vec_type = NULL_TREE;
3926 0 : tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3927 :
3928 0 : if (vector_types[dim-1] != NULL_TREE)
3929 : return vector_types[dim-1];
3930 :
3931 0 : if (vec_type == NULL_TREE)
3932 : {
3933 0 : chain = 0;
3934 0 : vect_struct_type = make_node (RECORD_TYPE);
3935 0 : tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3936 : get_identifier ("vector"),
3937 : pvoid_type_node, &chain);
3938 0 : suppress_warning (tmp);
3939 0 : tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3940 : get_identifier ("kind"),
3941 : integer_type_node, &chain);
3942 0 : suppress_warning (tmp);
3943 0 : gfc_finish_type (vect_struct_type);
3944 :
3945 0 : chain = 0;
3946 0 : triplet_struct_type = make_node (RECORD_TYPE);
3947 0 : tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3948 : get_identifier ("lower_bound"),
3949 : gfc_array_index_type, &chain);
3950 0 : suppress_warning (tmp);
3951 0 : tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3952 : get_identifier ("upper_bound"),
3953 : gfc_array_index_type, &chain);
3954 0 : suppress_warning (tmp);
3955 0 : tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3956 : gfc_array_index_type, &chain);
3957 0 : suppress_warning (tmp);
3958 0 : gfc_finish_type (triplet_struct_type);
3959 :
3960 0 : chain = 0;
3961 0 : union_type = make_node (UNION_TYPE);
3962 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3963 : vect_struct_type, &chain);
3964 0 : suppress_warning (tmp);
3965 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3966 : triplet_struct_type, &chain);
3967 0 : suppress_warning (tmp);
3968 0 : gfc_finish_type (union_type);
3969 :
3970 0 : chain = 0;
3971 0 : vec_type = make_node (RECORD_TYPE);
3972 0 : tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3973 : size_type_node, &chain);
3974 0 : suppress_warning (tmp);
3975 0 : tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3976 : union_type, &chain);
3977 0 : suppress_warning (tmp);
3978 0 : gfc_finish_type (vec_type);
3979 0 : TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3980 : }
3981 :
3982 0 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3983 : gfc_rank_cst[dim-1]);
3984 0 : vector_types[dim-1] = build_array_type (vec_type, tmp);
3985 0 : return vector_types[dim-1];
3986 : }
3987 :
3988 :
3989 : tree
3990 0 : gfc_get_caf_reference_type ()
3991 : {
3992 0 : static tree reference_type = NULL_TREE;
3993 0 : tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3994 : a_struct_type, u_union_type, tmp, *chain;
3995 :
3996 0 : if (reference_type != NULL_TREE)
3997 : return reference_type;
3998 :
3999 0 : chain = 0;
4000 0 : c_struct_type = make_node (RECORD_TYPE);
4001 0 : tmp = gfc_add_field_to_struct_1 (c_struct_type,
4002 : get_identifier ("offset"),
4003 : gfc_array_index_type, &chain);
4004 0 : suppress_warning (tmp);
4005 0 : tmp = gfc_add_field_to_struct_1 (c_struct_type,
4006 : get_identifier ("caf_token_offset"),
4007 : gfc_array_index_type, &chain);
4008 0 : suppress_warning (tmp);
4009 0 : gfc_finish_type (c_struct_type);
4010 :
4011 0 : chain = 0;
4012 0 : s_struct_type = make_node (RECORD_TYPE);
4013 0 : tmp = gfc_add_field_to_struct_1 (s_struct_type,
4014 : get_identifier ("start"),
4015 : gfc_array_index_type, &chain);
4016 0 : suppress_warning (tmp);
4017 0 : tmp = gfc_add_field_to_struct_1 (s_struct_type,
4018 : get_identifier ("end"),
4019 : gfc_array_index_type, &chain);
4020 0 : suppress_warning (tmp);
4021 0 : tmp = gfc_add_field_to_struct_1 (s_struct_type,
4022 : get_identifier ("stride"),
4023 : gfc_array_index_type, &chain);
4024 0 : suppress_warning (tmp);
4025 0 : gfc_finish_type (s_struct_type);
4026 :
4027 0 : chain = 0;
4028 0 : v_struct_type = make_node (RECORD_TYPE);
4029 0 : tmp = gfc_add_field_to_struct_1 (v_struct_type,
4030 : get_identifier ("vector"),
4031 : pvoid_type_node, &chain);
4032 0 : suppress_warning (tmp);
4033 0 : tmp = gfc_add_field_to_struct_1 (v_struct_type,
4034 : get_identifier ("nvec"),
4035 : size_type_node, &chain);
4036 0 : suppress_warning (tmp);
4037 0 : tmp = gfc_add_field_to_struct_1 (v_struct_type,
4038 : get_identifier ("kind"),
4039 : integer_type_node, &chain);
4040 0 : suppress_warning (tmp);
4041 0 : gfc_finish_type (v_struct_type);
4042 :
4043 0 : chain = 0;
4044 0 : union_type = make_node (UNION_TYPE);
4045 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
4046 : s_struct_type, &chain);
4047 0 : suppress_warning (tmp);
4048 0 : tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
4049 : v_struct_type, &chain);
4050 0 : suppress_warning (tmp);
4051 0 : gfc_finish_type (union_type);
4052 :
4053 0 : tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4054 : gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
4055 0 : dim_union_type = build_array_type (union_type, tmp);
4056 :
4057 0 : chain = 0;
4058 0 : a_struct_type = make_node (RECORD_TYPE);
4059 0 : tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
4060 : build_array_type (unsigned_char_type_node,
4061 : build_range_type (gfc_array_index_type,
4062 : gfc_index_zero_node,
4063 : gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
4064 : &chain);
4065 0 : suppress_warning (tmp);
4066 0 : tmp = gfc_add_field_to_struct_1 (a_struct_type,
4067 : get_identifier ("static_array_type"),
4068 : integer_type_node, &chain);
4069 0 : suppress_warning (tmp);
4070 0 : tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
4071 : dim_union_type, &chain);
4072 0 : suppress_warning (tmp);
4073 0 : gfc_finish_type (a_struct_type);
4074 :
4075 0 : chain = 0;
4076 0 : u_union_type = make_node (UNION_TYPE);
4077 0 : tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
4078 : c_struct_type, &chain);
4079 0 : suppress_warning (tmp);
4080 0 : tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
4081 : a_struct_type, &chain);
4082 0 : suppress_warning (tmp);
4083 0 : gfc_finish_type (u_union_type);
4084 :
4085 0 : chain = 0;
4086 0 : reference_type = make_node (RECORD_TYPE);
4087 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
4088 : build_pointer_type (reference_type), &chain);
4089 0 : suppress_warning (tmp);
4090 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
4091 : integer_type_node, &chain);
4092 0 : suppress_warning (tmp);
4093 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
4094 : size_type_node, &chain);
4095 0 : suppress_warning (tmp);
4096 0 : tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
4097 : u_union_type, &chain);
4098 0 : suppress_warning (tmp);
4099 0 : gfc_finish_type (reference_type);
4100 0 : TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
4101 :
4102 0 : return reference_type;
4103 : }
4104 :
4105 : static tree
4106 1337 : gfc_get_cfi_dim_type ()
4107 : {
4108 1337 : static tree CFI_dim_t = NULL;
4109 :
4110 1337 : if (CFI_dim_t)
4111 : return CFI_dim_t;
4112 :
4113 638 : CFI_dim_t = make_node (RECORD_TYPE);
4114 638 : TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
4115 638 : TYPE_NAMELESS (CFI_dim_t) = 1;
4116 638 : tree field;
4117 638 : tree *chain = NULL;
4118 638 : field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
4119 : gfc_array_index_type, &chain);
4120 638 : suppress_warning (field);
4121 638 : field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
4122 : gfc_array_index_type, &chain);
4123 638 : suppress_warning (field);
4124 638 : field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
4125 : gfc_array_index_type, &chain);
4126 638 : suppress_warning (field);
4127 638 : gfc_finish_type (CFI_dim_t);
4128 638 : TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
4129 638 : return CFI_dim_t;
4130 : }
4131 :
4132 :
4133 : /* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
4134 : otherwise dim[dimen] is used. */
4135 :
4136 : tree
4137 12516 : gfc_get_cfi_type (int dimen, bool restricted)
4138 : {
4139 12516 : gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
4140 :
4141 12516 : int idx = 2*(dimen + 1) + restricted;
4142 :
4143 12516 : if (gfc_cfi_descriptor_base[idx])
4144 : return gfc_cfi_descriptor_base[idx];
4145 :
4146 : /* Build the type node. */
4147 1517 : tree CFI_cdesc_t = make_node (RECORD_TYPE);
4148 1517 : char name[GFC_MAX_SYMBOL_LEN + 1];
4149 1517 : if (dimen != -1)
4150 960 : sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
4151 1517 : TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
4152 1517 : TYPE_NAMELESS (CFI_cdesc_t) = 1;
4153 :
4154 1517 : tree field;
4155 1517 : tree *chain = NULL;
4156 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
4157 : (restricted ? prvoid_type_node
4158 : : ptr_type_node), &chain);
4159 1517 : suppress_warning (field);
4160 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
4161 : size_type_node, &chain);
4162 1517 : suppress_warning (field);
4163 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
4164 : integer_type_node, &chain);
4165 1517 : suppress_warning (field);
4166 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
4167 : signed_char_type_node, &chain);
4168 1517 : suppress_warning (field);
4169 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
4170 : signed_char_type_node, &chain);
4171 1517 : suppress_warning (field);
4172 1517 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
4173 : get_typenode_from_name (INT16_TYPE),
4174 : &chain);
4175 1517 : suppress_warning (field);
4176 :
4177 1517 : if (dimen != 0)
4178 : {
4179 1337 : tree range = NULL_TREE;
4180 1337 : if (dimen > 0)
4181 780 : range = gfc_rank_cst[dimen - 1];
4182 1337 : range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4183 : range);
4184 1337 : tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
4185 1337 : field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
4186 : CFI_dim_t, &chain);
4187 1337 : suppress_warning (field);
4188 : }
4189 :
4190 1517 : TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
4191 1517 : gfc_finish_type (CFI_cdesc_t);
4192 1517 : gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
4193 1517 : return CFI_cdesc_t;
4194 : }
4195 :
4196 : #include "gt-fortran-trans-types.h"
|