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