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