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