Debian Codegraph @

gcc/fortran/trans-types.cc

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