libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / trans-types.cc
blobd59c0cc19d4fa9e634e2ca4a3c92a6b3c8c309f0
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
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.
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.
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/>. */
22 /* trans-types.cc -- gfortran backend types */
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"
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
54 /* array of structs so we don't have to worry about xmalloc or free */
55 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
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;
66 tree logical_type_node;
67 tree logical_true_node;
68 tree logical_false_node;
69 tree gfc_charlen_type_node;
71 tree gfc_float128_type_node = NULL_TREE;
72 tree gfc_complex_float128_type_node = NULL_TREE;
74 bool gfc_real16_is_float128 = false;
75 bool gfc_real16_use_iec_60559 = false;
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)];
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. */
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];
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];
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];
104 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
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. */
109 int gfc_index_integer_kind;
111 /* The default kinds of the various types. */
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;
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;
131 /* The integer kind used to store character lengths. */
132 int gfc_charlen_int_kind;
134 /* Kind of internal integer for storing object sizes. */
135 int gfc_size_kind;
137 /* The size of the numeric storage unit and character storage unit. */
138 int gfc_numeric_storage_size;
139 int gfc_character_storage_size;
141 static tree dtype_type_node = NULL_TREE;
144 /* Build the dtype_type_node if necessary. */
145 tree get_dtype_type_node (void)
147 tree field;
148 tree dtype_node;
149 tree *dtype_chain = NULL;
151 if (dtype_type_node == NULL_TREE)
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;
180 return dtype_type_node;
183 static int
184 get_real_kind_from_node (tree type)
186 int i;
188 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
189 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
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)
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;
202 #endif
203 return gfc_real_kinds[i].kind;
206 return -4;
209 static int
210 get_int_kind_from_node (tree type)
212 int i;
214 if (!type)
215 return -2;
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;
221 return -1;
224 static int
225 get_int_kind_from_name (const char *name)
227 return get_int_kind_from_node (get_typenode_from_name (name));
230 static int
231 get_unsigned_kind_from_node (tree type)
233 int i;
235 if (!type)
236 return -2;
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;
242 return -1;
245 static int
246 get_uint_kind_from_name (const char *name)
248 return get_unsigned_kind_from_node (get_typenode_from_name (name));
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. */
255 gfc_get_int_kind_from_width_isofortranenv (int size)
257 int i;
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;
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;
269 return -1;
272 /* Same, but for unsigned. */
275 gfc_get_uint_kind_from_width_isofortranenv (int size)
277 int i;
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;
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;
289 return -1;
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. */
299 gfc_get_real_kind_from_width_isofortranenv (int size)
301 int digits, i, kind;
303 size /= 8;
305 kind = -1;
306 digits = 0;
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)
312 if (gfc_real_kinds[i].digits > digits)
314 digits = gfc_real_kinds[i].digits;
315 kind = gfc_real_kinds[i].kind;
319 if (kind != -1)
320 return kind;
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;
327 return kind;
332 static int
333 get_int_kind_from_width (int size)
335 int i;
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;
341 return -2;
344 static int
345 get_int_kind_from_minimal_width (int size)
347 int i;
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;
353 return -2;
356 static int
357 get_uint_kind_from_width (int size)
359 int i;
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;
365 return -2;
369 /* Generate the CInteropKind_t objects for the C interoperable
370 kinds. */
372 void
373 gfc_init_c_interop_kinds (void)
375 int i;
377 /* init all pointers in the list to NULL */
378 for (i = 0; i < ISOCBINDING_NUMBER; i++)
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;
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"
430 /* Query the target to determine which machine modes are available for
431 computation. Choose KIND numbers for them. */
433 void
434 gfc_init_kinds (void)
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;
444 i_index = 0;
445 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
447 scalar_int_mode mode = int_mode_iter.require ();
448 int kind, bitsize;
450 if (!targetm.scalar_mode_supported_p (mode))
451 continue;
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;
460 gcc_assert (i_index != MAX_INT_KINDS);
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;
466 if (kind == 4)
467 saw_i4 = true;
468 if (kind == 8)
469 saw_i8 = true;
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;
476 if (flag_unsigned)
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;
484 gfc_logical_kinds[i_index].kind = kind;
485 gfc_logical_kinds[i_index].bit_size = bitsize;
487 i_index += 1;
490 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
491 used for large file access. */
493 if (saw_i8)
494 gfc_intio_kind = 8;
495 else
496 gfc_intio_kind = 4;
498 /* If we do not at least have kind = 4, everything is pointless. */
499 gcc_assert(saw_i4);
501 /* Set the maximum integer kind. Used with at least BOZ constants. */
502 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
504 r_index = 0;
505 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
507 scalar_float_mode mode = float_mode_iter.require ();
508 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
509 int kind;
511 if (fmt == NULL)
512 continue;
513 if (!targetm.scalar_mode_supported_p (mode))
514 continue;
516 if (MODE_COMPOSITE_P (mode)
517 && (GET_MODE_PRECISION (mode) + 7) / 8 == 16)
518 composite_mode = mode;
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
532 continue;
534 /* Let the kind equal the precision divided by 8, rounding up. Again,
535 this insulates the programmer from the underlying byte size.
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.
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.
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.
554 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
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)
564 saw_r16 = true;
565 r16_mode = mode;
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);
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;
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))
600 for (int i = 0; i < r_index; ++i)
601 if (gfc_real_kinds[i].kind == 16)
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)))
608 if (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR >= 26)
610 gfc_real16_use_iec_60559 = true;
611 gfc_real_kinds[i].use_iec_60559 = 1;
613 gfc_real16_is_float128 = true;
614 gfc_real_kinds[i].c_float128 = 1;
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");
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. */
627 gfc_numeric_storage_size = 4 * 8;
629 if (flag_default_integer)
631 if (!saw_i8)
632 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
633 "%<-fdefault-integer-8%> option");
635 gfc_default_integer_kind = 8;
638 else if (flag_integer4_kind == 8)
640 if (!saw_i8)
641 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
642 "%<-finteger-4-integer-8%> option");
644 gfc_default_integer_kind = 8;
646 else if (saw_i4)
648 gfc_default_integer_kind = 4;
650 else
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;
656 gfc_default_unsigned_kind = gfc_default_integer_kind;
658 /* Choose the default real kind. Again, we choose 4 when possible. */
659 if (flag_default_real_8)
661 if (!saw_r8)
662 gfc_fatal_error ("REAL(KIND=8) is not available for "
663 "%<-fdefault-real-8%> option");
665 gfc_default_real_kind = 8;
667 else if (flag_default_real_10)
669 if (!saw_r10)
670 gfc_fatal_error ("REAL(KIND=10) is not available for "
671 "%<-fdefault-real-10%> option");
673 gfc_default_real_kind = 10;
675 else if (flag_default_real_16)
677 if (!saw_r16)
678 gfc_fatal_error ("REAL(KIND=16) is not available for "
679 "%<-fdefault-real-16%> option");
681 gfc_default_real_kind = 16;
683 else if (flag_real4_kind == 8)
685 if (!saw_r8)
686 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
687 "option");
689 gfc_default_real_kind = 8;
691 else if (flag_real4_kind == 10)
693 if (!saw_r10)
694 gfc_fatal_error ("REAL(KIND=10) is not available for "
695 "%<-freal-4-real-10%> option");
697 gfc_default_real_kind = 10;
699 else if (flag_real4_kind == 16)
701 if (!saw_r16)
702 gfc_fatal_error ("REAL(KIND=16) is not available for "
703 "%<-freal-4-real-16%> option");
705 gfc_default_real_kind = 16;
707 else if (saw_r4)
708 gfc_default_real_kind = 4;
709 else
710 gfc_default_real_kind = gfc_real_kinds[0].kind;
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)
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;
730 else if (flag_real8_kind == 4)
732 if (!saw_r4)
733 gfc_fatal_error ("REAL(KIND=4) is not available for "
734 "%<-freal-8-real-4%> option");
736 gfc_default_double_kind = 4;
738 else if (flag_real8_kind == 10 )
740 if (!saw_r10)
741 gfc_fatal_error ("REAL(KIND=10) is not available for "
742 "%<-freal-8-real-10%> option");
744 gfc_default_double_kind = 10;
746 else if (flag_real8_kind == 16 )
748 if (!saw_r16)
749 gfc_fatal_error ("REAL(KIND=10) is not available for "
750 "%<-freal-8-real-16%> option");
752 gfc_default_double_kind = 16;
754 else if (saw_r4 && saw_r8)
755 gfc_default_double_kind = 8;
756 else
758 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
759 real ... occupies two contiguous numeric storage units.
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. */
769 gfc_default_double_kind
770 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
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;
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)
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++;
789 if ((kind = get_int_kind_from_width (32)) > 0)
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++;
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;
801 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
803 /* Pick a kind the same size as the C "int" type. */
804 gfc_c_int_kind = INT_TYPE_SIZE / 8;
806 /* UNSIGNED has the same as INT. */
807 gfc_c_uint_kind = gfc_c_int_kind;
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;
813 gfc_c_intptr_kind = POINTER_SIZE / 8;
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. */
820 static int
821 validate_integer (int kind)
823 int i;
825 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
826 if (gfc_integer_kinds[i].kind == kind)
827 return i;
829 return -1;
832 static int
833 validate_unsigned (int kind)
835 int i;
837 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
838 if (gfc_unsigned_kinds[i].kind == kind)
839 return i;
841 return -1;
844 static int
845 validate_real (int kind)
847 int i;
849 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
850 if (gfc_real_kinds[i].kind == kind)
851 return i;
853 return -1;
856 static int
857 validate_logical (int kind)
859 int i;
861 for (i = 0; gfc_logical_kinds[i].kind; i++)
862 if (gfc_logical_kinds[i].kind == kind)
863 return i;
865 return -1;
868 static int
869 validate_character (int kind)
871 int i;
873 for (i = 0; gfc_character_kinds[i].kind; i++)
874 if (gfc_character_kinds[i].kind == kind)
875 return i;
877 return -1;
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. */
885 gfc_validate_kind (bt type, int kind, bool may_fail)
887 int rc;
889 switch (type)
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;
908 default:
909 gfc_internal_error ("gfc_validate_kind(): Got bad type");
912 if (rc < 0 && !may_fail)
913 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
915 return rc;
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. */
924 static tree
925 gfc_build_int_type (gfc_integer_info *info)
927 int mode_precision = info->bit_size;
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;
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;
951 return make_signed_type (mode_precision);
954 tree
955 gfc_build_uint_type (int size)
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;
968 return make_unsigned_type (size);
971 static tree
972 gfc_build_unsigned_type (gfc_unsigned_info *info)
974 int mode_precision = info->bit_size;
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;
987 return gfc_build_uint_type (mode_precision);
990 static tree
991 gfc_build_real_type (gfc_real_info *info)
993 int mode_precision = info->mode_precision;
994 tree new_type;
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)
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))
1012 info->use_iec_60559 = 1;
1013 gfc_real16_use_iec_60559 = true;
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;
1024 new_type = make_node (REAL_TYPE);
1025 TYPE_PRECISION (new_type) = mode_precision;
1026 layout_type (new_type);
1027 return new_type;
1030 static tree
1031 gfc_build_complex_type (tree scalar_type)
1033 tree new_type;
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;
1044 new_type = make_node (COMPLEX_TYPE);
1045 TREE_TYPE (new_type) = scalar_type;
1046 layout_type (new_type);
1047 return new_type;
1050 static tree
1051 gfc_build_logical_type (gfc_logical_info *info)
1053 int bit_size = info->bit_size;
1054 tree new_type;
1056 if (bit_size == BOOL_TYPE_SIZE)
1058 info->c_bool = 1;
1059 return boolean_type_node;
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;
1067 return new_type;
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.*/
1076 void
1077 gfc_init_types (void)
1079 char name_buf[26];
1080 int index;
1081 tree type;
1082 unsigned n;
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))
1089 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
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);
1101 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
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);
1110 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
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);
1118 if (gfc_real_kinds[index].c_float128)
1119 gfc_float128_type_node = type;
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);
1127 if (gfc_real_kinds[index].c_float128)
1128 gfc_complex_float128_type_node = type;
1131 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
1133 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
1134 type = build_qualified_type (type, TYPE_UNQUALIFIED);
1135 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
1136 gfc_character_kinds[index].kind);
1137 PUSH_TYPE (name_buf, type);
1138 gfc_character_types[index] = type;
1139 gfc_pcharacter_types[index] = build_pointer_type (type);
1141 gfc_character1_type_node = gfc_character_types[0];
1143 /* The middle end only recognizes a single unsigned type. For
1144 compatibility of existing test cases, let's just use the
1145 character type. The reader of tree dumps is expected to be able
1146 to deal with this. */
1148 if (flag_unsigned)
1150 for (index = 0; gfc_unsigned_kinds[index].kind != 0;++index)
1152 int index_char = -1;
1153 for (int i=0; gfc_character_kinds[i].kind != 0; i++)
1155 if (gfc_character_kinds[i].bit_size
1156 == gfc_unsigned_kinds[index].bit_size)
1158 index_char = i;
1159 break;
1162 if (index_char > 0)
1164 gfc_unsigned_types[index] = gfc_character_types[index_char];
1166 else
1168 type = gfc_build_unsigned_type (&gfc_unsigned_kinds[index]);
1169 gfc_unsigned_types[index] = type;
1170 snprintf (name_buf, sizeof(name_buf), "unsigned(kind=%d)",
1171 gfc_integer_kinds[index].kind);
1172 PUSH_TYPE (name_buf, type);
1177 PUSH_TYPE ("byte", unsigned_char_type_node);
1178 PUSH_TYPE ("void", void_type_node);
1180 /* DBX debugging output gets upset if these aren't set. */
1181 if (!TYPE_NAME (integer_type_node))
1182 PUSH_TYPE ("c_integer", integer_type_node);
1183 if (!TYPE_NAME (char_type_node))
1184 PUSH_TYPE ("c_char", char_type_node);
1186 #undef PUSH_TYPE
1188 pvoid_type_node = build_pointer_type (void_type_node);
1189 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
1190 ppvoid_type_node = build_pointer_type (pvoid_type_node);
1191 pchar_type_node = build_pointer_type (gfc_character1_type_node);
1192 pfunc_type_node
1193 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
1195 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
1196 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1197 since this function is called before gfc_init_constants. */
1198 gfc_array_range_type
1199 = build_range_type (gfc_array_index_type,
1200 build_int_cst (gfc_array_index_type, 0),
1201 NULL_TREE);
1203 /* The maximum array element size that can be handled is determined
1204 by the number of bits available to store this field in the array
1205 descriptor. */
1207 n = TYPE_PRECISION (size_type_node);
1208 gfc_max_array_element_size
1209 = wide_int_to_tree (size_type_node,
1210 wi::mask (n, UNSIGNED,
1211 TYPE_PRECISION (size_type_node)));
1213 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1214 logical_true_node = build_int_cst (logical_type_node, 1);
1215 logical_false_node = build_int_cst (logical_type_node, 0);
1217 /* Character lengths are of type size_t, except signed. */
1218 gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1219 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1221 /* Fortran kind number of size_type_node (size_t). This is used for
1222 the _size member in vtables. */
1223 gfc_size_kind = get_int_kind_from_node (size_type_node);
1226 /* Get the type node for the given type and kind. */
1228 tree
1229 gfc_get_int_type (int kind)
1231 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1232 return index < 0 ? 0 : gfc_integer_types[index];
1235 tree
1236 gfc_get_unsigned_type (int kind)
1238 int index = gfc_validate_kind (BT_UNSIGNED, kind, true);
1239 return index < 0 ? 0 : gfc_unsigned_types[index];
1242 tree
1243 gfc_get_real_type (int kind)
1245 int index = gfc_validate_kind (BT_REAL, kind, true);
1246 return index < 0 ? 0 : gfc_real_types[index];
1249 tree
1250 gfc_get_complex_type (int kind)
1252 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1253 return index < 0 ? 0 : gfc_complex_types[index];
1256 tree
1257 gfc_get_logical_type (int kind)
1259 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1260 return index < 0 ? 0 : gfc_logical_types[index];
1263 tree
1264 gfc_get_char_type (int kind)
1266 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1267 return index < 0 ? 0 : gfc_character_types[index];
1270 tree
1271 gfc_get_pchar_type (int kind)
1273 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1274 return index < 0 ? 0 : gfc_pcharacter_types[index];
1278 /* Create a character type with the given kind and length. */
1280 tree
1281 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1283 tree bounds, type;
1285 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1286 type = build_array_type (eltype, bounds);
1287 TYPE_STRING_FLAG (type) = 1;
1289 return type;
1292 tree
1293 gfc_get_character_type_len (int kind, tree len)
1295 gfc_validate_kind (BT_CHARACTER, kind, false);
1296 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1300 /* Get a type node for a character kind. */
1302 tree
1303 gfc_get_character_type (int kind, gfc_charlen * cl)
1305 tree len;
1307 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1308 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1309 len = build_fold_indirect_ref (len);
1311 return gfc_get_character_type_len (kind, len);
1314 /* Convert a basic type. This will be an array for character types. */
1316 tree
1317 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1319 tree basetype;
1321 switch (spec->type)
1323 case BT_UNKNOWN:
1324 gcc_unreachable ();
1326 case BT_INTEGER:
1327 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1328 has been resolved. This is done so we can convert C_PTR and
1329 C_FUNPTR to simple variables that get translated to (void *). */
1330 if (spec->f90_type == BT_VOID)
1332 if (spec->u.derived
1333 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1334 basetype = ptr_type_node;
1335 else
1336 basetype = pfunc_type_node;
1338 else
1339 basetype = gfc_get_int_type (spec->kind);
1340 break;
1342 case BT_UNSIGNED:
1343 basetype = gfc_get_unsigned_type (spec->kind);
1344 break;
1346 case BT_REAL:
1347 basetype = gfc_get_real_type (spec->kind);
1348 break;
1350 case BT_COMPLEX:
1351 basetype = gfc_get_complex_type (spec->kind);
1352 break;
1354 case BT_LOGICAL:
1355 basetype = gfc_get_logical_type (spec->kind);
1356 break;
1358 case BT_CHARACTER:
1359 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1360 break;
1362 case BT_HOLLERITH:
1363 /* Since this cannot be used, return a length one character. */
1364 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1365 gfc_index_one_node);
1366 break;
1368 case BT_UNION:
1369 basetype = gfc_get_union_type (spec->u.derived);
1370 break;
1372 case BT_DERIVED:
1373 case BT_CLASS:
1374 basetype = gfc_get_derived_type (spec->u.derived, codim);
1376 if (spec->type == BT_CLASS)
1377 GFC_CLASS_TYPE_P (basetype) = 1;
1379 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1380 type and kind to fit a (void *) and the basetype returned was a
1381 ptr_type_node. We need to pass up this new information to the
1382 symbol that was declared of type C_PTR or C_FUNPTR. */
1383 if (spec->u.derived->ts.f90_type == BT_VOID)
1385 spec->type = BT_INTEGER;
1386 spec->kind = gfc_index_integer_kind;
1387 spec->f90_type = BT_VOID;
1388 spec->is_c_interop = 1; /* Mark as escaping later. */
1390 break;
1391 case BT_VOID:
1392 case BT_ASSUMED:
1393 /* This is for the second arg to c_f_pointer and c_f_procpointer
1394 of the iso_c_binding module, to accept any ptr type. */
1395 basetype = ptr_type_node;
1396 if (spec->f90_type == BT_VOID)
1398 if (spec->u.derived
1399 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1400 basetype = ptr_type_node;
1401 else
1402 basetype = pfunc_type_node;
1404 break;
1405 case BT_PROCEDURE:
1406 basetype = pfunc_type_node;
1407 break;
1408 default:
1409 gcc_unreachable ();
1411 return basetype;
1414 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1416 static tree
1417 gfc_conv_array_bound (gfc_expr * expr)
1419 /* If expr is an integer constant, return that. */
1420 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1421 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1423 /* Otherwise return NULL. */
1424 return NULL_TREE;
1427 /* Return the type of an element of the array. Note that scalar coarrays
1428 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1429 (with POINTER_TYPE stripped) is returned. */
1431 tree
1432 gfc_get_element_type (tree type)
1434 tree element;
1436 if (GFC_ARRAY_TYPE_P (type))
1438 if (TREE_CODE (type) == POINTER_TYPE)
1439 type = TREE_TYPE (type);
1440 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1442 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1443 element = type;
1445 else
1447 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1448 element = TREE_TYPE (type);
1451 else
1453 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1454 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1456 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1457 element = TREE_TYPE (element);
1459 /* For arrays, which are not scalar coarrays. */
1460 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1461 element = TREE_TYPE (element);
1464 return element;
1467 /* Build an array. This function is called from gfc_sym_type().
1468 Actually returns array descriptor type.
1470 Format of array descriptors is as follows:
1472 struct gfc_array_descriptor
1474 array *data;
1475 index offset;
1476 struct dtype_type dtype;
1477 struct descriptor_dimension dimension[N_DIM];
1480 struct dtype_type
1482 size_t elem_len;
1483 int version;
1484 signed char rank;
1485 signed char type;
1486 signed short attribute;
1489 struct descriptor_dimension
1491 index stride;
1492 index lbound;
1493 index ubound;
1496 Translation code should use gfc_conv_descriptor_* rather than
1497 accessing the descriptor directly. Any changes to the array
1498 descriptor type will require changes in gfc_conv_descriptor_* and
1499 gfc_build_array_initializer.
1501 This is represented internally as a RECORD_TYPE. The index nodes
1502 are gfc_array_index_type and the data node is a pointer to the
1503 data. See below for the handling of character types.
1505 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1506 this generated poor code for assumed/deferred size arrays. These
1507 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1508 of the GENERIC grammar. Also, there is no way to explicitly set
1509 the array stride, so all data must be packed(1). I've tried to
1510 mark all the functions which would require modification with a GCC
1511 ARRAYS comment.
1513 The data component points to the first element in the array. The
1514 offset field is the position of the origin of the array (i.e. element
1515 (0, 0 ...)). This may be outside the bounds of the array.
1517 An element is accessed by
1518 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1519 This gives good performance as the computation does not involve the
1520 bounds of the array. For packed arrays, this is optimized further
1521 by substituting the known strides.
1523 This system has one problem: all array bounds must be within 2^31
1524 elements of the origin (2^63 on 64-bit machines). For example
1525 integer, dimension (80000:90000, 80000:90000, 2) :: array
1526 may not work properly on 32-bit machines because 80000*80000 >
1527 2^31, so the calculation for stride2 would overflow. This may
1528 still work, but I haven't checked, and it relies on the overflow
1529 doing the right thing.
1531 The way to fix this problem is to access elements as follows:
1532 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1533 Obviously this is much slower. I will make this a compile time
1534 option, something like -fsmall-array-offsets. Mixing code compiled
1535 with and without this switch will work.
1537 (1) This can be worked around by modifying the upper bound of the
1538 previous dimension. This requires extra fields in the descriptor
1539 (both real_ubound and fake_ubound). */
1542 /* Returns true if the array sym does not require a descriptor. */
1544 bool
1545 gfc_is_nodesc_array (gfc_symbol * sym)
1547 symbol_attribute *array_attr;
1548 gfc_array_spec *as;
1549 bool is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
1551 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1552 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1554 gcc_assert (array_attr->dimension || array_attr->codimension);
1556 /* We only want local arrays. */
1557 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1558 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1559 || array_attr->allocatable)
1560 return 0;
1562 /* We want a descriptor for associate-name arrays that do not have an
1563 explicitly known shape already. */
1564 if (sym->assoc && as->type != AS_EXPLICIT)
1565 return 0;
1567 /* The dummy is stored in sym and not in the component. */
1568 if (sym->attr.dummy)
1569 return as->type != AS_ASSUMED_SHAPE
1570 && as->type != AS_ASSUMED_RANK;
1572 if (sym->attr.result || sym->attr.function)
1573 return 0;
1575 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1577 return 1;
1581 /* Create an array descriptor type. */
1583 static tree
1584 gfc_build_array_type (tree type, gfc_array_spec * as,
1585 enum gfc_array_kind akind, bool restricted,
1586 bool contiguous, int codim)
1588 tree lbound[GFC_MAX_DIMENSIONS];
1589 tree ubound[GFC_MAX_DIMENSIONS];
1590 int n, corank;
1592 /* Assumed-shape arrays do not have codimension information stored in the
1593 descriptor. */
1594 corank = MAX (as->corank, codim);
1595 if (as->type == AS_ASSUMED_SHAPE ||
1596 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1597 corank = codim;
1599 if (as->type == AS_ASSUMED_RANK)
1600 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1602 lbound[n] = NULL_TREE;
1603 ubound[n] = NULL_TREE;
1606 for (n = 0; n < as->rank; n++)
1608 /* Create expressions for the known bounds of the array. */
1609 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1610 lbound[n] = gfc_index_one_node;
1611 else
1612 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1613 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1616 for (n = as->rank; n < as->rank + corank; n++)
1618 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1619 lbound[n] = gfc_index_one_node;
1620 else
1621 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1623 if (n < as->rank + corank - 1)
1624 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1627 if (as->type == AS_ASSUMED_SHAPE)
1628 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1629 : GFC_ARRAY_ASSUMED_SHAPE;
1630 else if (as->type == AS_ASSUMED_RANK)
1631 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1632 : GFC_ARRAY_ASSUMED_RANK;
1633 return gfc_get_array_type_bounds (type, as->rank == -1
1634 ? GFC_MAX_DIMENSIONS : as->rank,
1635 corank, lbound, ubound, 0, akind,
1636 restricted);
1639 /* Returns the struct descriptor_dimension type. */
1641 static tree
1642 gfc_get_desc_dim_type (void)
1644 tree type;
1645 tree decl, *chain = NULL;
1647 if (gfc_desc_dim_type)
1648 return gfc_desc_dim_type;
1650 /* Build the type node. */
1651 type = make_node (RECORD_TYPE);
1653 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1654 TYPE_PACKED (type) = 1;
1656 /* Consists of the stride, lbound and ubound members. */
1657 decl = gfc_add_field_to_struct_1 (type,
1658 get_identifier ("stride"),
1659 gfc_array_index_type, &chain);
1660 suppress_warning (decl);
1662 decl = gfc_add_field_to_struct_1 (type,
1663 get_identifier ("lbound"),
1664 gfc_array_index_type, &chain);
1665 suppress_warning (decl);
1667 decl = gfc_add_field_to_struct_1 (type,
1668 get_identifier ("ubound"),
1669 gfc_array_index_type, &chain);
1670 suppress_warning (decl);
1672 /* Finish off the type. */
1673 gfc_finish_type (type);
1674 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1676 gfc_desc_dim_type = type;
1677 return type;
1681 /* Return the DTYPE for an array. This describes the type and type parameters
1682 of the array. */
1683 /* TODO: Only call this when the value is actually used, and make all the
1684 unknown cases abort. */
1686 tree
1687 gfc_get_dtype_rank_type (int rank, tree etype)
1689 tree ptype;
1690 tree size;
1691 int n;
1692 tree tmp;
1693 tree dtype;
1694 tree field;
1695 vec<constructor_elt, va_gc> *v = NULL;
1697 ptype = etype;
1698 while (TREE_CODE (etype) == POINTER_TYPE
1699 || TREE_CODE (etype) == ARRAY_TYPE)
1701 ptype = etype;
1702 etype = TREE_TYPE (etype);
1705 gcc_assert (etype);
1707 switch (TREE_CODE (etype))
1709 case INTEGER_TYPE:
1710 if (TREE_CODE (ptype) == ARRAY_TYPE
1711 && TYPE_STRING_FLAG (ptype))
1712 n = BT_CHARACTER;
1713 else
1715 if (TYPE_UNSIGNED (etype))
1716 n = BT_UNSIGNED;
1717 else
1718 n = BT_INTEGER;
1720 break;
1722 case BOOLEAN_TYPE:
1723 n = BT_LOGICAL;
1724 break;
1726 case REAL_TYPE:
1727 n = BT_REAL;
1728 break;
1730 case COMPLEX_TYPE:
1731 n = BT_COMPLEX;
1732 break;
1734 case RECORD_TYPE:
1735 if (GFC_CLASS_TYPE_P (etype))
1736 n = BT_CLASS;
1737 else
1738 n = BT_DERIVED;
1739 break;
1741 case FUNCTION_TYPE:
1742 case VOID_TYPE:
1743 n = BT_VOID;
1744 break;
1746 default:
1747 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1748 /* We can encounter strange array types for temporary arrays. */
1749 gcc_unreachable ();
1752 switch (n)
1754 case BT_CHARACTER:
1755 gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1756 size = gfc_get_character_len_in_bytes (ptype);
1757 break;
1758 case BT_VOID:
1759 gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1760 size = size_in_bytes (ptype);
1761 break;
1762 default:
1763 size = size_in_bytes (etype);
1764 break;
1767 gcc_assert (size);
1769 STRIP_NOPS (size);
1770 size = fold_convert (size_type_node, size);
1771 tmp = get_dtype_type_node ();
1772 field = gfc_advance_chain (TYPE_FIELDS (tmp),
1773 GFC_DTYPE_ELEM_LEN);
1774 CONSTRUCTOR_APPEND_ELT (v, field,
1775 fold_convert (TREE_TYPE (field), size));
1776 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1777 GFC_DTYPE_VERSION);
1778 CONSTRUCTOR_APPEND_ELT (v, field,
1779 build_zero_cst (TREE_TYPE (field)));
1781 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1782 GFC_DTYPE_RANK);
1783 if (rank >= 0)
1784 CONSTRUCTOR_APPEND_ELT (v, field,
1785 build_int_cst (TREE_TYPE (field), rank));
1787 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1788 GFC_DTYPE_TYPE);
1789 CONSTRUCTOR_APPEND_ELT (v, field,
1790 build_int_cst (TREE_TYPE (field), n));
1792 dtype = build_constructor (tmp, v);
1794 return dtype;
1798 tree
1799 gfc_get_dtype (tree type, int * rank)
1801 tree dtype;
1802 tree etype;
1803 int irnk;
1805 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1807 irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
1808 etype = gfc_get_element_type (type);
1809 dtype = gfc_get_dtype_rank_type (irnk, etype);
1811 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1812 return dtype;
1816 /* Build an array type for use without a descriptor, packed according
1817 to the value of PACKED. */
1819 tree
1820 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1821 bool restricted)
1823 tree range;
1824 tree type;
1825 tree tmp;
1826 int n;
1827 int known_stride;
1828 int known_offset;
1829 mpz_t offset;
1830 mpz_t stride;
1831 mpz_t delta;
1832 gfc_expr *expr;
1834 mpz_init_set_ui (offset, 0);
1835 mpz_init_set_ui (stride, 1);
1836 mpz_init (delta);
1838 /* We don't use build_array_type because this does not include
1839 lang-specific information (i.e. the bounds of the array) when checking
1840 for duplicates. */
1841 if (as->rank)
1842 type = make_node (ARRAY_TYPE);
1843 else
1844 type = build_variant_type_copy (etype);
1846 GFC_ARRAY_TYPE_P (type) = 1;
1847 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1849 known_stride = (packed != PACKED_NO);
1850 known_offset = 1;
1851 for (n = 0; n < as->rank; n++)
1853 /* Fill in the stride and bound components of the type. */
1854 if (known_stride)
1855 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1856 else
1857 tmp = NULL_TREE;
1858 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1860 expr = as->lower[n];
1861 if (expr && expr->expr_type == EXPR_CONSTANT)
1863 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1864 gfc_index_integer_kind);
1866 else
1868 known_stride = 0;
1869 tmp = NULL_TREE;
1871 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1873 if (known_stride)
1875 /* Calculate the offset. */
1876 mpz_mul (delta, stride, as->lower[n]->value.integer);
1877 mpz_sub (offset, offset, delta);
1879 else
1880 known_offset = 0;
1882 expr = as->upper[n];
1883 if (expr && expr->expr_type == EXPR_CONSTANT)
1885 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1886 gfc_index_integer_kind);
1888 else
1890 tmp = NULL_TREE;
1891 known_stride = 0;
1893 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1895 if (known_stride)
1897 /* Calculate the stride. */
1898 mpz_sub (delta, as->upper[n]->value.integer,
1899 as->lower[n]->value.integer);
1900 mpz_add_ui (delta, delta, 1);
1901 mpz_mul (stride, stride, delta);
1904 /* Only the first stride is known for partial packed arrays. */
1905 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1906 known_stride = 0;
1908 for (n = as->rank; n < as->rank + as->corank; n++)
1910 expr = as->lower[n];
1911 if (expr && expr->expr_type == EXPR_CONSTANT)
1912 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1913 gfc_index_integer_kind);
1914 else
1915 tmp = NULL_TREE;
1916 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1918 expr = as->upper[n];
1919 if (expr && expr->expr_type == EXPR_CONSTANT)
1920 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1921 gfc_index_integer_kind);
1922 else
1923 tmp = NULL_TREE;
1924 if (n < as->rank + as->corank - 1)
1925 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1928 if (known_offset)
1930 GFC_TYPE_ARRAY_OFFSET (type) =
1931 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1933 else
1934 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1936 if (known_stride)
1938 GFC_TYPE_ARRAY_SIZE (type) =
1939 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1941 else
1942 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1944 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1945 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1946 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1947 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1948 NULL_TREE);
1949 /* TODO: use main type if it is unbounded. */
1950 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1951 build_pointer_type (build_array_type (etype, range));
1952 if (restricted)
1953 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1954 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1955 TYPE_QUAL_RESTRICT);
1957 if (as->rank == 0)
1959 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1961 type = build_pointer_type (type);
1963 if (restricted)
1964 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1966 GFC_ARRAY_TYPE_P (type) = 1;
1967 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1970 goto array_type_done;
1973 if (known_stride)
1975 mpz_sub_ui (stride, stride, 1);
1976 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1978 else
1979 range = NULL_TREE;
1981 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1982 TYPE_DOMAIN (type) = range;
1984 build_pointer_type (etype);
1985 TREE_TYPE (type) = etype;
1987 layout_type (type);
1989 /* Represent packed arrays as multi-dimensional if they have rank >
1990 1 and with proper bounds, instead of flat arrays. This makes for
1991 better debug info. */
1992 if (known_offset)
1994 tree gtype = etype, rtype, type_decl;
1996 for (n = as->rank - 1; n >= 0; n--)
1998 rtype = build_range_type (gfc_array_index_type,
1999 GFC_TYPE_ARRAY_LBOUND (type, n),
2000 GFC_TYPE_ARRAY_UBOUND (type, n));
2001 gtype = build_array_type (gtype, rtype);
2003 TYPE_NAME (type) = type_decl = build_decl (input_location,
2004 TYPE_DECL, NULL, gtype);
2005 DECL_ORIGINAL_TYPE (type_decl) = gtype;
2008 if (packed != PACKED_STATIC || !known_stride
2009 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
2011 /* For dummy arrays and automatic (heap allocated) arrays we
2012 want a pointer to the array. */
2013 type = build_pointer_type (type);
2014 if (restricted)
2015 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2016 GFC_ARRAY_TYPE_P (type) = 1;
2017 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
2020 array_type_done:
2021 mpz_clear (offset);
2022 mpz_clear (stride);
2023 mpz_clear (delta);
2025 return type;
2029 /* Return or create the base type for an array descriptor. */
2031 static tree
2032 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
2034 tree fat_type, decl, arraytype, *chain = NULL;
2035 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
2036 int idx;
2038 /* Assumed-rank array. */
2039 if (dimen == -1)
2040 dimen = GFC_MAX_DIMENSIONS;
2042 idx = 2 * (codimen + dimen) + restricted;
2044 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
2046 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
2048 if (gfc_array_descriptor_base_caf[idx])
2049 return gfc_array_descriptor_base_caf[idx];
2051 else if (gfc_array_descriptor_base[idx])
2052 return gfc_array_descriptor_base[idx];
2054 /* Build the type node. */
2055 fat_type = make_node (RECORD_TYPE);
2057 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
2058 TYPE_NAME (fat_type) = get_identifier (name);
2059 TYPE_NAMELESS (fat_type) = 1;
2061 /* Add the data member as the first element of the descriptor. */
2062 gfc_add_field_to_struct_1 (fat_type,
2063 get_identifier ("data"),
2064 (restricted
2065 ? prvoid_type_node
2066 : ptr_type_node), &chain);
2068 /* Add the base component. */
2069 decl = gfc_add_field_to_struct_1 (fat_type,
2070 get_identifier ("offset"),
2071 gfc_array_index_type, &chain);
2072 suppress_warning (decl);
2074 /* Add the dtype component. */
2075 decl = gfc_add_field_to_struct_1 (fat_type,
2076 get_identifier ("dtype"),
2077 get_dtype_type_node (), &chain);
2078 suppress_warning (decl);
2080 /* Add the span component. */
2081 decl = gfc_add_field_to_struct_1 (fat_type,
2082 get_identifier ("span"),
2083 gfc_array_index_type, &chain);
2084 suppress_warning (decl);
2086 /* Build the array type for the stride and bound components. */
2087 if (dimen + codimen > 0)
2089 arraytype =
2090 build_array_type (gfc_get_desc_dim_type (),
2091 build_range_type (gfc_array_index_type,
2092 gfc_index_zero_node,
2093 gfc_rank_cst[codimen + dimen - 1]));
2095 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
2096 arraytype, &chain);
2097 suppress_warning (decl);
2100 if (flag_coarray == GFC_FCOARRAY_LIB)
2102 decl = gfc_add_field_to_struct_1 (fat_type,
2103 get_identifier ("token"),
2104 prvoid_type_node, &chain);
2105 suppress_warning (decl);
2108 /* Finish off the type. */
2109 gfc_finish_type (fat_type);
2110 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
2112 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
2113 gfc_array_descriptor_base_caf[idx] = fat_type;
2114 else
2115 gfc_array_descriptor_base[idx] = fat_type;
2117 return fat_type;
2121 /* Build an array (descriptor) type with given bounds. */
2123 tree
2124 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
2125 tree * ubound, int packed,
2126 enum gfc_array_kind akind, bool restricted)
2128 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
2129 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
2130 const char *type_name;
2131 int n;
2133 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
2134 fat_type = build_distinct_type_copy (base_type);
2135 /* Unshare TYPE_FIELDs. */
2136 for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
2138 tree next = DECL_CHAIN (*tp);
2139 *tp = copy_node (*tp);
2140 DECL_CONTEXT (*tp) = fat_type;
2141 DECL_CHAIN (*tp) = next;
2143 /* Make sure that nontarget and target array type have the same canonical
2144 type (and same stub decl for debug info). */
2145 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
2146 TYPE_CANONICAL (fat_type) = base_type;
2147 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
2148 /* Arrays of unknown type must alias with all array descriptors. */
2149 TYPE_TYPELESS_STORAGE (base_type) = 1;
2150 TYPE_TYPELESS_STORAGE (fat_type) = 1;
2151 gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
2153 tmp = etype;
2154 if (TREE_CODE (tmp) == ARRAY_TYPE
2155 && TYPE_STRING_FLAG (tmp))
2156 tmp = TREE_TYPE (etype);
2157 tmp = TYPE_NAME (tmp);
2158 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
2159 tmp = DECL_NAME (tmp);
2160 if (tmp)
2161 type_name = IDENTIFIER_POINTER (tmp);
2162 else
2163 type_name = "unknown";
2164 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
2165 GFC_MAX_SYMBOL_LEN, type_name);
2166 TYPE_NAME (fat_type) = get_identifier (name);
2167 TYPE_NAMELESS (fat_type) = 1;
2169 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
2170 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
2172 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
2173 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
2174 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
2175 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
2177 /* Build an array descriptor record type. */
2178 if (packed != 0)
2179 stride = gfc_index_one_node;
2180 else
2181 stride = NULL_TREE;
2182 for (n = 0; n < dimen + codimen; n++)
2184 if (n < dimen)
2185 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
2187 if (lbound)
2188 lower = lbound[n];
2189 else
2190 lower = NULL_TREE;
2192 if (lower != NULL_TREE)
2194 if (INTEGER_CST_P (lower))
2195 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
2196 else
2197 lower = NULL_TREE;
2200 if (codimen && n == dimen + codimen - 1)
2201 break;
2203 upper = ubound[n];
2204 if (upper != NULL_TREE)
2206 if (INTEGER_CST_P (upper))
2207 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
2208 else
2209 upper = NULL_TREE;
2212 if (n >= dimen)
2213 continue;
2215 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
2217 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2218 gfc_array_index_type, upper, lower);
2219 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2220 gfc_array_index_type, tmp,
2221 gfc_index_one_node);
2222 stride = fold_build2_loc (input_location, MULT_EXPR,
2223 gfc_array_index_type, tmp, stride);
2224 /* Check the folding worked. */
2225 gcc_assert (INTEGER_CST_P (stride));
2227 else
2228 stride = NULL_TREE;
2230 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
2232 /* TODO: known offsets for descriptors. */
2233 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2235 if (dimen == 0)
2237 arraytype = build_pointer_type (etype);
2238 if (restricted)
2239 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2241 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2242 return fat_type;
2245 /* We define data as an array with the correct size if possible.
2246 Much better than doing pointer arithmetic. */
2247 if (stride)
2248 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2249 int_const_binop (MINUS_EXPR, stride,
2250 build_int_cst (TREE_TYPE (stride), 1)));
2251 else
2252 rtype = gfc_array_range_type;
2253 arraytype = build_array_type (etype, rtype);
2254 arraytype = build_pointer_type (arraytype);
2255 if (restricted)
2256 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2257 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2259 /* This will generate the base declarations we need to emit debug
2260 information for this type. FIXME: there must be a better way to
2261 avoid divergence between compilations with and without debug
2262 information. */
2264 struct array_descr_info info;
2265 gfc_get_array_descr_info (fat_type, &info);
2266 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2269 return fat_type;
2272 /* Build a pointer type. This function is called from gfc_sym_type(). */
2274 static tree
2275 gfc_build_pointer_type (gfc_symbol * sym, tree type)
2277 /* Array pointer types aren't actually pointers. */
2278 if (sym->attr.dimension)
2279 return type;
2280 else
2281 return build_pointer_type (type);
2284 static tree gfc_nonrestricted_type (tree t);
2285 /* Given two record or union type nodes TO and FROM, ensure
2286 that all fields in FROM have a corresponding field in TO,
2287 their type being nonrestrict variants. This accepts a TO
2288 node that already has a prefix of the fields in FROM. */
2289 static void
2290 mirror_fields (tree to, tree from)
2292 tree fto, ffrom;
2293 tree *chain;
2295 /* Forward to the end of TOs fields. */
2296 fto = TYPE_FIELDS (to);
2297 ffrom = TYPE_FIELDS (from);
2298 chain = &TYPE_FIELDS (to);
2299 while (fto)
2301 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2302 chain = &DECL_CHAIN (fto);
2303 fto = DECL_CHAIN (fto);
2304 ffrom = DECL_CHAIN (ffrom);
2307 /* Now add all fields remaining in FROM (starting with ffrom). */
2308 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2310 tree newfield = copy_node (ffrom);
2311 DECL_CONTEXT (newfield) = to;
2312 /* The store to DECL_CHAIN might seem redundant with the
2313 stores to *chain, but not clearing it here would mean
2314 leaving a chain into the old fields. If ever
2315 our called functions would look at them confusion
2316 will arise. */
2317 DECL_CHAIN (newfield) = NULL_TREE;
2318 *chain = newfield;
2319 chain = &DECL_CHAIN (newfield);
2321 if (TREE_CODE (ffrom) == FIELD_DECL)
2323 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2324 TREE_TYPE (newfield) = elemtype;
2327 *chain = NULL_TREE;
2330 /* Given a type T, returns a different type of the same structure,
2331 except that all types it refers to (recursively) are always
2332 non-restrict qualified types. */
2333 static tree
2334 gfc_nonrestricted_type (tree t)
2336 tree ret = t;
2338 /* If the type isn't laid out yet, don't copy it. If something
2339 needs it for real it should wait until the type got finished. */
2340 if (!TYPE_SIZE (t))
2341 return t;
2343 if (!TYPE_LANG_SPECIFIC (t))
2344 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2345 /* If we're dealing with this very node already further up
2346 the call chain (recursion via pointers and struct members)
2347 we haven't yet determined if we really need a new type node.
2348 Assume we don't, return T itself. */
2349 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2350 return t;
2352 /* If we have calculated this all already, just return it. */
2353 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2354 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2356 /* Mark this type. */
2357 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2359 switch (TREE_CODE (t))
2361 default:
2362 break;
2364 case POINTER_TYPE:
2365 case REFERENCE_TYPE:
2367 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2368 if (totype == TREE_TYPE (t))
2369 ret = t;
2370 else if (TREE_CODE (t) == POINTER_TYPE)
2371 ret = build_pointer_type (totype);
2372 else
2373 ret = build_reference_type (totype);
2374 ret = build_qualified_type (ret,
2375 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2377 break;
2379 case ARRAY_TYPE:
2381 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2382 if (elemtype == TREE_TYPE (t))
2383 ret = t;
2384 else
2386 ret = build_variant_type_copy (t);
2387 TREE_TYPE (ret) = elemtype;
2388 if (TYPE_LANG_SPECIFIC (t)
2389 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2391 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2392 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2393 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2395 TYPE_LANG_SPECIFIC (ret)
2396 = ggc_cleared_alloc<struct lang_type> ();
2397 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2398 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2403 break;
2405 case RECORD_TYPE:
2406 case UNION_TYPE:
2407 case QUAL_UNION_TYPE:
2409 tree field;
2410 /* First determine if we need a new type at all.
2411 Careful, the two calls to gfc_nonrestricted_type per field
2412 might return different values. That happens exactly when
2413 one of the fields reaches back to this very record type
2414 (via pointers). The first calls will assume that we don't
2415 need to copy T (see the error_mark_node marking). If there
2416 are any reasons for copying T apart from having to copy T,
2417 we'll indeed copy it, and the second calls to
2418 gfc_nonrestricted_type will use that new node if they
2419 reach back to T. */
2420 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2421 if (TREE_CODE (field) == FIELD_DECL)
2423 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2424 if (elemtype != TREE_TYPE (field))
2425 break;
2427 if (!field)
2428 break;
2429 ret = build_variant_type_copy (t);
2430 TYPE_FIELDS (ret) = NULL_TREE;
2432 /* Here we make sure that as soon as we know we have to copy
2433 T, that also fields reaching back to us will use the new
2434 copy. It's okay if that copy still contains the old fields,
2435 we won't look at them. */
2436 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2437 mirror_fields (ret, t);
2439 break;
2442 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2443 return ret;
2447 /* Return the type for a symbol. Special handling is required for character
2448 types to get the correct level of indirection.
2449 For functions return the return type.
2450 For subroutines return void_type_node.
2451 Calling this multiple times for the same symbol should be avoided,
2452 especially for character and array types. */
2454 tree
2455 gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
2457 tree type;
2458 int byref;
2459 bool restricted;
2461 /* Procedure Pointers inside COMMON blocks. */
2462 if (sym->attr.proc_pointer && sym->attr.in_common)
2464 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2465 sym->attr.proc_pointer = 0;
2466 type = build_pointer_type (gfc_get_function_type (sym));
2467 sym->attr.proc_pointer = 1;
2468 return type;
2471 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2472 return void_type_node;
2474 /* In the case of a function the fake result variable may have a
2475 type different from the function type, so don't return early in
2476 that case. */
2477 if (sym->backend_decl && !sym->attr.function)
2478 return TREE_TYPE (sym->backend_decl);
2480 if (sym->attr.result
2481 && sym->ts.type == BT_CHARACTER
2482 && sym->ts.u.cl->backend_decl == NULL_TREE
2483 && sym->ns->proc_name
2484 && sym->ns->proc_name->ts.u.cl
2485 && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2486 sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2488 if (sym->ts.type == BT_CHARACTER
2489 && ((sym->attr.function && sym->attr.is_bind_c)
2490 || ((sym->attr.result || sym->attr.value)
2491 && sym->ns->proc_name
2492 && sym->ns->proc_name->attr.is_bind_c)
2493 || (sym->ts.deferred
2494 && (!sym->ts.u.cl
2495 || !sym->ts.u.cl->backend_decl
2496 || sym->attr.save))
2497 || (sym->attr.dummy
2498 && sym->attr.value
2499 && gfc_length_one_character_type_p (&sym->ts))))
2500 type = gfc_get_char_type (sym->ts.kind);
2501 else
2502 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2504 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
2505 && !sym->pass_as_value)
2506 byref = 1;
2507 else
2508 byref = 0;
2510 restricted = (!sym->attr.target && !IS_POINTER (sym)
2511 && !IS_PROC_POINTER (sym) && !sym->attr.cray_pointee);
2512 if (!restricted)
2513 type = gfc_nonrestricted_type (type);
2515 /* Dummy argument to a bind(C) procedure. */
2516 if (is_bind_c && is_CFI_desc (sym, NULL))
2517 type = gfc_get_cfi_type (sym->attr.dimension ? sym->as->rank : 0,
2518 /* restricted = */ false);
2519 else if (sym->attr.dimension || sym->attr.codimension)
2521 if (gfc_is_nodesc_array (sym))
2523 /* If this is a character argument of unknown length, just use the
2524 base type. */
2525 if (sym->ts.type != BT_CHARACTER
2526 || !(sym->attr.dummy || sym->attr.function)
2527 || sym->ts.u.cl->backend_decl)
2529 type = gfc_get_nodesc_array_type (type, sym->as,
2530 byref ? PACKED_FULL
2531 : PACKED_STATIC,
2532 restricted);
2533 byref = 0;
2536 else
2538 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2539 if (sym->attr.pointer)
2540 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2541 : GFC_ARRAY_POINTER;
2542 else if (sym->attr.allocatable)
2543 akind = GFC_ARRAY_ALLOCATABLE;
2544 type = gfc_build_array_type (type, sym->as, akind, restricted,
2545 sym->attr.contiguous, sym->as->corank);
2548 else
2550 if (sym->attr.allocatable || sym->attr.pointer
2551 || gfc_is_associate_pointer (sym))
2552 type = gfc_build_pointer_type (sym, type);
2555 /* We currently pass all parameters by reference.
2556 See f95_get_function_decl. For dummy function parameters return the
2557 function type. */
2558 if (byref)
2560 /* We must use pointer types for potentially absent variables. The
2561 optimizers assume a reference type argument is never NULL. */
2562 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2563 || sym->attr.optional
2564 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2565 type = build_pointer_type (type);
2566 else
2567 type = build_reference_type (type);
2569 if (restricted)
2570 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2573 return (type);
2576 /* Layout and output debug info for a record type. */
2578 void
2579 gfc_finish_type (tree type)
2581 tree decl;
2583 decl = build_decl (input_location,
2584 TYPE_DECL, NULL_TREE, type);
2585 TYPE_STUB_DECL (type) = decl;
2586 layout_type (type);
2587 rest_of_type_compilation (type, 1);
2588 rest_of_decl_compilation (decl, 1, 0);
2591 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2592 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2593 to the end of the field list pointed to by *CHAIN.
2595 Returns a pointer to the new field. */
2597 static tree
2598 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2600 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2602 DECL_CONTEXT (decl) = context;
2603 DECL_CHAIN (decl) = NULL_TREE;
2604 if (TYPE_FIELDS (context) == NULL_TREE)
2605 TYPE_FIELDS (context) = decl;
2606 if (chain != NULL)
2608 if (*chain != NULL)
2609 **chain = decl;
2610 *chain = &DECL_CHAIN (decl);
2613 return decl;
2616 /* Like `gfc_add_field_to_struct_1', but adds alignment
2617 information. */
2619 tree
2620 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2622 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2624 DECL_INITIAL (decl) = 0;
2625 SET_DECL_ALIGN (decl, 0);
2626 DECL_USER_ALIGN (decl) = 0;
2628 return decl;
2632 /* Copy the backend_decl and component backend_decls if
2633 the two derived type symbols are "equal", as described
2634 in 4.4.2 and resolved by gfc_compare_derived_types. */
2636 bool
2637 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2638 bool from_gsym)
2640 gfc_component *to_cm;
2641 gfc_component *from_cm;
2643 if (from == to)
2644 return 1;
2646 if (from->backend_decl == NULL
2647 || !gfc_compare_derived_types (from, to))
2648 return 0;
2650 to->backend_decl = from->backend_decl;
2652 to_cm = to->components;
2653 from_cm = from->components;
2655 /* Copy the component declarations. If a component is itself
2656 a derived type, we need a copy of its component declarations.
2657 This is done by recursing into gfc_get_derived_type and
2658 ensures that the component's component declarations have
2659 been built. If it is a character, we need the character
2660 length, as well. */
2661 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2663 to_cm->backend_decl = from_cm->backend_decl;
2664 to_cm->caf_token = from_cm->caf_token;
2665 if (from_cm->ts.type == BT_UNION)
2666 gfc_get_union_type (to_cm->ts.u.derived);
2667 else if (from_cm->ts.type == BT_DERIVED
2668 && (!from_cm->attr.pointer || from_gsym))
2669 gfc_get_derived_type (to_cm->ts.u.derived);
2670 else if (from_cm->ts.type == BT_CLASS
2671 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2672 gfc_get_derived_type (to_cm->ts.u.derived);
2673 else if (from_cm->ts.type == BT_CHARACTER)
2674 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2677 return 1;
2681 /* Build a tree node for a procedure pointer component. */
2683 static tree
2684 gfc_get_ppc_type (gfc_component* c)
2686 tree t;
2688 /* Explicit interface. */
2689 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2690 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2692 /* Implicit interface (only return value may be known). */
2693 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2694 t = gfc_typenode_for_spec (&c->ts);
2695 else
2696 t = void_type_node;
2698 /* FIXME: it would be better to provide explicit interfaces in all
2699 cases, since they should be known by the compiler. */
2700 return build_pointer_type (build_function_type (t, NULL_TREE));
2704 /* Build a tree node for a union type. Requires building each map
2705 structure which is an element of the union. */
2707 tree
2708 gfc_get_union_type (gfc_symbol *un)
2710 gfc_component *map = NULL;
2711 tree typenode = NULL, map_type = NULL, map_field = NULL;
2712 tree *chain = NULL;
2714 if (un->backend_decl)
2716 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2717 return un->backend_decl;
2718 else
2719 typenode = un->backend_decl;
2721 else
2723 typenode = make_node (UNION_TYPE);
2724 TYPE_NAME (typenode) = get_identifier (un->name);
2727 /* Add each contained MAP as a field. */
2728 for (map = un->components; map; map = map->next)
2730 gcc_assert (map->ts.type == BT_DERIVED);
2732 /* The map's type node, which is defined within this union's context. */
2733 map_type = gfc_get_derived_type (map->ts.u.derived);
2734 TYPE_CONTEXT (map_type) = typenode;
2736 /* The map field's declaration. */
2737 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2738 map_type, &chain);
2739 if (map->loc.lb)
2740 gfc_set_decl_location (map_field, &map->loc);
2741 else if (un->declared_at.lb)
2742 gfc_set_decl_location (map_field, &un->declared_at);
2744 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2745 DECL_NAMELESS(map_field) = true;
2747 /* We should never clobber another backend declaration for this map,
2748 because each map component is unique. */
2749 if (!map->backend_decl)
2750 map->backend_decl = map_field;
2753 un->backend_decl = typenode;
2754 gfc_finish_type (typenode);
2756 return typenode;
2759 bool
2760 cobounds_match_decl (const gfc_symbol *derived)
2762 tree arrtype, tmp;
2763 gfc_array_spec *as;
2765 if (!derived->backend_decl)
2766 return false;
2767 /* Care only about coarray declarations. Everything else is ok with us. */
2768 if (!derived->components || strcmp (derived->components->name, "_data") != 0)
2769 return true;
2770 if (!derived->components->attr.codimension)
2771 return true;
2773 arrtype = TREE_TYPE (TYPE_FIELDS (derived->backend_decl));
2774 as = derived->components->as;
2775 if (GFC_TYPE_ARRAY_CORANK (arrtype) != as->corank)
2776 return false;
2778 for (int dim = as->rank; dim < as->rank + as->corank; ++dim)
2780 /* Check lower bound. */
2781 tmp = TYPE_LANG_SPECIFIC (arrtype)->lbound[dim];
2782 if (!tmp || !INTEGER_CST_P (tmp))
2783 return false;
2784 if (as->lower[dim]->expr_type != EXPR_CONSTANT
2785 || as->lower[dim]->ts.type != BT_INTEGER)
2786 return false;
2787 if (*tmp->int_cst.val != mpz_get_si (as->lower[dim]->value.integer))
2788 return false;
2790 /* Check upper bound. */
2791 tmp = TYPE_LANG_SPECIFIC (arrtype)->ubound[dim];
2792 if (!tmp && !as->upper[dim])
2793 continue;
2795 if (!tmp || !INTEGER_CST_P (tmp))
2796 return false;
2797 if (as->upper[dim]->expr_type != EXPR_CONSTANT
2798 || as->upper[dim]->ts.type != BT_INTEGER)
2799 return false;
2800 if (*tmp->int_cst.val != mpz_get_si (as->upper[dim]->value.integer))
2801 return false;
2804 return true;
2807 /* Build a tree node for a derived type. If there are equal
2808 derived types, with different local names, these are built
2809 at the same time. If an equal derived type has been built
2810 in a parent namespace, this is used. */
2812 tree
2813 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2815 tree typenode = NULL, field = NULL, field_type = NULL;
2816 tree canonical = NULL_TREE;
2817 tree *chain = NULL;
2818 bool got_canonical = false;
2819 bool unlimited_entity = false;
2820 gfc_component *c, *last_c = nullptr;
2821 gfc_namespace *ns;
2822 tree tmp;
2823 bool coarray_flag, class_coarray_flag;
2825 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2826 && derived->module && !derived->attr.vtype;
2827 class_coarray_flag = derived->components
2828 && derived->components->ts.type == BT_DERIVED
2829 && strcmp (derived->components->name, "_data") == 0
2830 && derived->components->attr.codimension
2831 && derived->components->as->cotype == AS_EXPLICIT;
2833 gcc_assert (!derived->attr.pdt_template);
2835 if (derived->attr.unlimited_polymorphic
2836 || (flag_coarray == GFC_FCOARRAY_LIB
2837 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2838 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2839 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2840 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2841 return ptr_type_node;
2843 if (flag_coarray != GFC_FCOARRAY_LIB
2844 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2845 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2846 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2847 return gfc_get_int_type (gfc_default_integer_kind);
2849 if (derived && derived->attr.flavor == FL_PROCEDURE
2850 && derived->attr.generic)
2851 derived = gfc_find_dt_in_generic (derived);
2853 /* See if it's one of the iso_c_binding derived types. */
2854 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2856 if (derived->backend_decl)
2857 return derived->backend_decl;
2859 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2860 derived->backend_decl = ptr_type_node;
2861 else
2862 derived->backend_decl = pfunc_type_node;
2864 derived->ts.kind = gfc_index_integer_kind;
2865 derived->ts.type = BT_INTEGER;
2866 /* Set the f90_type to BT_VOID as a way to recognize something of type
2867 BT_INTEGER that needs to fit a void * for the purpose of the
2868 iso_c_binding derived types. */
2869 derived->ts.f90_type = BT_VOID;
2871 return derived->backend_decl;
2874 /* If use associated, use the module type for this one. */
2875 if (derived->backend_decl == NULL
2876 && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2877 && derived->module
2878 && gfc_get_module_backend_decl (derived))
2879 goto copy_derived_types;
2881 /* The derived types from an earlier namespace can be used as the
2882 canonical type. */
2883 if (derived->backend_decl == NULL
2884 && !derived->attr.use_assoc
2885 && !derived->attr.used_in_submodule
2886 && gfc_global_ns_list)
2888 for (ns = gfc_global_ns_list;
2889 ns->translated && !got_canonical;
2890 ns = ns->sibling)
2892 if (ns->derived_types)
2894 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2895 dt = dt->dt_next)
2897 gfc_copy_dt_decls_ifequal (dt, derived, true);
2898 if (derived->backend_decl)
2899 got_canonical = true;
2900 if (dt->dt_next == ns->derived_types)
2901 break;
2907 /* Store up the canonical type to be added to this one. */
2908 if (got_canonical)
2910 if (TYPE_CANONICAL (derived->backend_decl))
2911 canonical = TYPE_CANONICAL (derived->backend_decl);
2912 else
2913 canonical = derived->backend_decl;
2915 derived->backend_decl = NULL_TREE;
2918 /* derived->backend_decl != 0 means we saw it before, but its
2919 components' backend_decl may have not been built. */
2920 if (derived->backend_decl
2921 && (!class_coarray_flag || cobounds_match_decl (derived)))
2923 /* Its components' backend_decl have been built or we are
2924 seeing recursion through the formal arglist of a procedure
2925 pointer component. */
2926 if (TYPE_FIELDS (derived->backend_decl))
2927 return derived->backend_decl;
2928 else if (derived->attr.abstract
2929 && derived->attr.proc_pointer_comp)
2931 /* If an abstract derived type with procedure pointer
2932 components has no other type of component, return the
2933 backend_decl. Otherwise build the components if any of the
2934 non-procedure pointer components have no backend_decl. */
2935 for (c = derived->components; c; c = c->next)
2937 bool same_alloc_type = c->attr.allocatable
2938 && derived == c->ts.u.derived;
2939 if (!c->attr.proc_pointer
2940 && !same_alloc_type
2941 && c->backend_decl == NULL)
2942 break;
2943 else if (c->next == NULL)
2944 return derived->backend_decl;
2946 typenode = derived->backend_decl;
2948 else
2949 typenode = derived->backend_decl;
2951 else
2953 /* We see this derived type first time, so build the type node. */
2954 typenode = make_node (RECORD_TYPE);
2955 TYPE_NAME (typenode) = get_identifier (derived->name);
2956 TYPE_PACKED (typenode) = flag_pack_derived;
2957 derived->backend_decl = typenode;
2960 if (derived->components
2961 && derived->components->ts.type == BT_DERIVED
2962 && strcmp (derived->components->name, "_data") == 0
2963 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2964 unlimited_entity = true;
2966 /* Go through the derived type components, building them as
2967 necessary. The reason for doing this now is that it is
2968 possible to recurse back to this derived type through a
2969 pointer component (PR24092). If this happens, the fields
2970 will be built and so we can return the type. */
2971 for (c = derived->components; c; c = c->next)
2973 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2974 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2976 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2977 continue;
2979 const bool incomplete_type
2980 = c->ts.u.derived->backend_decl
2981 && TREE_CODE (c->ts.u.derived->backend_decl) == RECORD_TYPE
2982 && !(TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)
2983 && TYPE_LANG_SPECIFIC (c->ts.u.derived->backend_decl)->size);
2984 const bool pointer_component
2985 = c->attr.pointer || c->attr.allocatable || c->attr.proc_pointer;
2987 /* Prevent endless recursion on recursive types (i.e. types that reference
2988 themself in a component. Break the recursion by not building pointers
2989 to incomplete types again, aka types that are already in the build. */
2990 if (c->ts.u.derived->backend_decl == NULL
2991 || (c->attr.codimension && c->as->corank != codimen)
2992 || !(incomplete_type && pointer_component))
2994 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2995 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2996 local_codim);
2999 if (c->ts.u.derived->attr.is_iso_c)
3001 /* Need to copy the modified ts from the derived type. The
3002 typespec was modified because C_PTR/C_FUNPTR are translated
3003 into (void *) from derived types. */
3004 c->ts.type = c->ts.u.derived->ts.type;
3005 c->ts.kind = c->ts.u.derived->ts.kind;
3006 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
3007 if (c->initializer)
3009 c->initializer->ts.type = c->ts.type;
3010 c->initializer->ts.kind = c->ts.kind;
3011 c->initializer->ts.f90_type = c->ts.f90_type;
3012 c->initializer->expr_type = EXPR_NULL;
3017 if (!class_coarray_flag && TYPE_FIELDS (derived->backend_decl))
3018 return derived->backend_decl;
3020 /* Build the type member list. Install the newly created RECORD_TYPE
3021 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
3022 through only the top-level linked list of components so we correctly
3023 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
3024 types are built as part of gfc_get_union_type. */
3025 for (c = derived->components; c; c = c->next)
3027 bool same_alloc_type = c->attr.allocatable
3028 && derived == c->ts.u.derived;
3029 /* Prevent infinite recursion, when the procedure pointer type is
3030 the same as derived, by forcing the procedure pointer component to
3031 be built as if the explicit interface does not exist. */
3032 if (c->attr.proc_pointer
3033 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
3034 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
3035 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
3036 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
3037 field_type = gfc_get_ppc_type (c);
3038 else if (c->attr.proc_pointer && derived->backend_decl)
3040 tmp = build_function_type (derived->backend_decl, NULL_TREE);
3041 field_type = build_pointer_type (tmp);
3043 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
3044 field_type = c->ts.u.derived->backend_decl;
3045 else if (c->attr.caf_token)
3046 field_type = pvoid_type_node;
3047 else
3049 if (c->ts.type == BT_CHARACTER
3050 && !c->ts.deferred && !c->attr.pdt_string)
3052 /* Evaluate the string length. */
3053 gfc_conv_const_charlen (c->ts.u.cl);
3054 gcc_assert (c->ts.u.cl->backend_decl);
3056 else if (c->ts.type == BT_CHARACTER)
3057 c->ts.u.cl->backend_decl
3058 = build_int_cst (gfc_charlen_type_node, 0);
3060 field_type = gfc_typenode_for_spec (&c->ts, codimen);
3063 /* This returns an array descriptor type. Initialization may be
3064 required. */
3065 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
3067 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
3069 enum gfc_array_kind akind;
3070 if (c->attr.pointer)
3071 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
3072 : GFC_ARRAY_POINTER;
3073 else
3074 akind = GFC_ARRAY_ALLOCATABLE;
3075 /* Pointers to arrays aren't actually pointer types. The
3076 descriptors are separate, but the data is common. Every
3077 array pointer in a coarray derived type needs to provide space
3078 for the coarray management, too. Therefore treat coarrays
3079 and pointers to coarrays in derived types the same. */
3080 field_type = gfc_build_array_type
3082 field_type, c->as, akind, !c->attr.target && !c->attr.pointer,
3083 c->attr.contiguous,
3084 c->attr.codimension || c->attr.pointer ? codimen : 0
3087 else
3088 field_type = gfc_get_nodesc_array_type (field_type, c->as,
3089 PACKED_STATIC,
3090 !c->attr.target);
3092 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
3093 && !c->attr.proc_pointer
3094 && !(unlimited_entity && c == derived->components))
3095 field_type = build_pointer_type (field_type);
3097 if (c->attr.pointer || same_alloc_type)
3098 field_type = gfc_nonrestricted_type (field_type);
3100 /* vtype fields can point to different types to the base type. */
3101 if (c->ts.type == BT_DERIVED
3102 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
3103 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
3104 ptr_mode, true);
3106 /* Ensure that the CLASS language specific flag is set. */
3107 if (c->ts.type == BT_CLASS)
3109 if (POINTER_TYPE_P (field_type))
3110 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
3111 else
3112 GFC_CLASS_TYPE_P (field_type) = 1;
3115 field = gfc_add_field_to_struct (typenode,
3116 get_identifier (c->name),
3117 field_type, &chain);
3118 if (c->loc.lb)
3119 gfc_set_decl_location (field, &c->loc);
3120 else if (derived->declared_at.lb)
3121 gfc_set_decl_location (field, &derived->declared_at);
3123 gfc_finish_decl_attrs (field, &c->attr);
3125 DECL_PACKED (field) |= TYPE_PACKED (typenode);
3127 gcc_assert (field);
3128 /* Overwrite for class array to supply different bounds for different
3129 types. */
3130 if (class_coarray_flag || !c->backend_decl)
3131 c->backend_decl = field;
3132 if (c->attr.caf_token && last_c)
3133 last_c->caf_token = field;
3135 if (c->attr.pointer && (c->attr.dimension || c->attr.codimension)
3136 && !(c->ts.type == BT_DERIVED && strcmp (c->name, "_data") == 0))
3137 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
3139 last_c = c;
3142 /* Now lay out the derived type, including the fields. */
3143 if (canonical)
3144 TYPE_CANONICAL (typenode) = canonical;
3146 gfc_finish_type (typenode);
3147 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
3148 if (derived->module && derived->ns->proc_name
3149 && derived->ns->proc_name->attr.flavor == FL_MODULE)
3151 if (derived->ns->proc_name->backend_decl
3152 && TREE_CODE (derived->ns->proc_name->backend_decl)
3153 == NAMESPACE_DECL)
3155 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
3156 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
3157 = derived->ns->proc_name->backend_decl;
3161 derived->backend_decl = typenode;
3163 copy_derived_types:
3165 for (c = derived->components; c; c = c->next)
3167 /* Do not add a caf_token field for class container components. */
3168 if ((codimen || coarray_flag)
3169 && !c->attr.dimension && !c->attr.codimension
3170 && (c->attr.allocatable || c->attr.pointer)
3171 && !derived->attr.is_class)
3173 /* Provide sufficient space to hold "_caf_symbol". */
3174 char caf_name[GFC_MAX_SYMBOL_LEN + 6];
3175 gfc_component *token;
3176 snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
3177 token = gfc_find_component (derived, caf_name, true, true, NULL);
3178 gcc_assert (token);
3179 c->caf_token = token->backend_decl;
3180 suppress_warning (c->caf_token);
3184 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
3186 gfc_copy_dt_decls_ifequal (derived, dt, false);
3187 if (dt->dt_next == gfc_derived_types)
3188 break;
3191 return derived->backend_decl;
3195 bool
3196 gfc_return_by_reference (gfc_symbol * sym)
3198 if (!sym->attr.function)
3199 return 0;
3201 if (sym->attr.dimension)
3202 return 1;
3204 if (sym->ts.type == BT_CHARACTER
3205 && !sym->attr.is_bind_c
3206 && (!sym->attr.result
3207 || !sym->ns->proc_name
3208 || !sym->ns->proc_name->attr.is_bind_c))
3209 return 1;
3211 /* Possibly return complex numbers by reference for g77 compatibility.
3212 We don't do this for calls to intrinsics (as the library uses the
3213 -fno-f2c calling convention), nor for calls to functions which always
3214 require an explicit interface, as no compatibility problems can
3215 arise there. */
3216 if (flag_f2c && sym->ts.type == BT_COMPLEX
3217 && !sym->attr.pointer
3218 && !sym->attr.allocatable
3219 && !sym->attr.intrinsic && !sym->attr.always_explicit)
3220 return 1;
3222 return 0;
3225 static tree
3226 gfc_get_mixed_entry_union (gfc_namespace *ns)
3228 tree type;
3229 tree *chain = NULL;
3230 char name[GFC_MAX_SYMBOL_LEN + 1];
3231 gfc_entry_list *el, *el2;
3233 gcc_assert (ns->proc_name->attr.mixed_entry_master);
3234 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
3236 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
3238 /* Build the type node. */
3239 type = make_node (UNION_TYPE);
3241 TYPE_NAME (type) = get_identifier (name);
3243 for (el = ns->entries; el; el = el->next)
3245 /* Search for duplicates. */
3246 for (el2 = ns->entries; el2 != el; el2 = el2->next)
3247 if (el2->sym->result == el->sym->result)
3248 break;
3250 if (el == el2)
3251 gfc_add_field_to_struct_1 (type,
3252 get_identifier (el->sym->result->name),
3253 gfc_sym_type (el->sym->result), &chain);
3256 /* Finish off the type. */
3257 gfc_finish_type (type);
3258 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
3259 return type;
3262 /* Create a "fn spec" based on the formal arguments;
3263 cf. create_function_arglist. */
3265 static tree
3266 create_fn_spec (gfc_symbol *sym, tree fntype)
3268 char spec[150];
3269 size_t spec_len;
3270 gfc_formal_arglist *f;
3271 tree tmp;
3273 memset (&spec, 0, sizeof (spec));
3274 spec[0] = '.';
3275 spec[1] = ' ';
3276 spec_len = 2;
3278 if (sym->attr.entry_master)
3280 spec[spec_len++] = 'R';
3281 spec[spec_len++] = ' ';
3283 if (gfc_return_by_reference (sym))
3285 gfc_symbol *result = sym->result ? sym->result : sym;
3287 if (result->attr.pointer || sym->attr.proc_pointer)
3289 spec[spec_len++] = '.';
3290 spec[spec_len++] = ' ';
3292 else
3294 spec[spec_len++] = 'w';
3295 spec[spec_len++] = ' ';
3297 if (sym->ts.type == BT_CHARACTER)
3299 if (!sym->ts.u.cl->length
3300 && (sym->attr.allocatable || sym->attr.pointer))
3301 spec[spec_len++] = 'w';
3302 else
3303 spec[spec_len++] = 'R';
3304 spec[spec_len++] = ' ';
3308 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3309 if (spec_len < sizeof (spec))
3311 bool is_class = false;
3312 bool is_pointer = false;
3314 if (f->sym)
3316 is_class = f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym)
3317 && f->sym->attr.class_ok;
3318 is_pointer = is_class ? CLASS_DATA (f->sym)->attr.class_pointer
3319 : f->sym->attr.pointer;
3322 if (f->sym == NULL || is_pointer || f->sym->attr.target
3323 || f->sym->attr.external || f->sym->attr.cray_pointer
3324 || (f->sym->ts.type == BT_DERIVED
3325 && (f->sym->ts.u.derived->attr.proc_pointer_comp
3326 || f->sym->ts.u.derived->attr.pointer_comp))
3327 || (is_class
3328 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3329 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3330 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3332 spec[spec_len++] = '.';
3333 spec[spec_len++] = ' ';
3335 else if (f->sym->attr.intent == INTENT_IN)
3337 spec[spec_len++] = 'r';
3338 spec[spec_len++] = ' ';
3340 else if (f->sym)
3342 spec[spec_len++] = 'w';
3343 spec[spec_len++] = ' ';
3347 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3348 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3349 return build_type_attribute_variant (fntype, tmp);
3353 /* NOTE: The returned function type must match the argument list created by
3354 create_function_arglist. */
3356 tree
3357 gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3358 const char *fnspec)
3360 tree type;
3361 vec<tree, va_gc> *typelist = NULL;
3362 vec<tree, va_gc> *hidden_typelist = NULL;
3363 gfc_formal_arglist *f;
3364 gfc_symbol *arg;
3365 int alternate_return = 0;
3366 bool is_varargs = true;
3368 /* Make sure this symbol is a function, a subroutine or the main
3369 program. */
3370 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3371 || sym->attr.flavor == FL_PROGRAM);
3373 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3374 so that they can be detected here and handled further down. */
3375 if (sym->backend_decl == NULL)
3376 sym->backend_decl = error_mark_node;
3377 else if (sym->backend_decl == error_mark_node)
3378 goto arg_type_list_done;
3379 else if (sym->attr.proc_pointer)
3380 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3381 else
3382 return TREE_TYPE (sym->backend_decl);
3384 if (sym->attr.entry_master)
3385 /* Additional parameter for selecting an entry point. */
3386 vec_safe_push (typelist, gfc_array_index_type);
3388 if (sym->result)
3389 arg = sym->result;
3390 else
3391 arg = sym;
3393 if (arg->ts.type == BT_CHARACTER)
3394 gfc_conv_const_charlen (arg->ts.u.cl);
3396 /* Some functions we use an extra parameter for the return value. */
3397 if (gfc_return_by_reference (sym))
3399 type = gfc_sym_type (arg);
3400 if (arg->ts.type == BT_COMPLEX
3401 || arg->attr.dimension
3402 || arg->ts.type == BT_CHARACTER)
3403 type = build_reference_type (type);
3405 vec_safe_push (typelist, type);
3406 if (arg->ts.type == BT_CHARACTER)
3408 if (!arg->ts.deferred)
3409 /* Transfer by value. */
3410 vec_safe_push (typelist, gfc_charlen_type_node);
3411 else
3412 /* Deferred character lengths are transferred by reference
3413 so that the value can be returned. */
3414 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3417 if (sym->backend_decl == error_mark_node && actual_args != NULL
3418 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3419 || sym->attr.proc == PROC_UNKNOWN))
3420 gfc_get_formal_from_actual_arglist (sym, actual_args);
3422 /* Build the argument types for the function. */
3423 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3425 arg = f->sym;
3426 if (arg)
3428 /* Evaluate constant character lengths here so that they can be
3429 included in the type. */
3430 if (arg->ts.type == BT_CHARACTER)
3431 gfc_conv_const_charlen (arg->ts.u.cl);
3433 if (arg->attr.flavor == FL_PROCEDURE)
3435 type = gfc_get_function_type (arg);
3436 type = build_pointer_type (type);
3438 else
3439 type = gfc_sym_type (arg, sym->attr.is_bind_c);
3441 /* Parameter Passing Convention
3443 We currently pass all parameters by reference.
3444 Parameters with INTENT(IN) could be passed by value.
3445 The problem arises if a function is called via an implicit
3446 prototype. In this situation the INTENT is not known.
3447 For this reason all parameters to global functions must be
3448 passed by reference. Passing by value would potentially
3449 generate bad code. Worse there would be no way of telling that
3450 this code was bad, except that it would give incorrect results.
3452 Contained procedures could pass by value as these are never
3453 used without an explicit interface, and cannot be passed as
3454 actual parameters for a dummy procedure. */
3456 vec_safe_push (typelist, type);
3458 else
3460 if (sym->attr.subroutine)
3461 alternate_return = 1;
3465 /* Add hidden arguments. */
3466 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3468 arg = f->sym;
3469 /* Add hidden string length parameters. */
3470 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3472 if (!arg->ts.deferred)
3473 /* Transfer by value. */
3474 type = gfc_charlen_type_node;
3475 else
3476 /* Deferred character lengths are transferred by reference
3477 so that the value can be returned. */
3478 type = build_pointer_type (gfc_charlen_type_node);
3480 vec_safe_push (hidden_typelist, type);
3482 /* For scalar intrinsic types, VALUE passes the value,
3483 hence, the optional status cannot be transferred via a NULL pointer.
3484 Thus, we will use a hidden argument in that case. */
3485 if (arg
3486 && arg->attr.optional
3487 && arg->attr.value
3488 && !arg->attr.dimension
3489 && arg->ts.type != BT_CLASS
3490 && !gfc_bt_struct (arg->ts.type))
3491 vec_safe_push (typelist, boolean_type_node);
3492 /* Coarrays which are descriptorless or assumed-shape pass with
3493 -fcoarray=lib the token and the offset as hidden arguments. */
3494 if (arg
3495 && flag_coarray == GFC_FCOARRAY_LIB
3496 && ((arg->ts.type != BT_CLASS
3497 && arg->attr.codimension
3498 && !arg->attr.allocatable)
3499 || (arg->ts.type == BT_CLASS
3500 && CLASS_DATA (arg)->attr.codimension
3501 && !CLASS_DATA (arg)->attr.allocatable)))
3503 vec_safe_push (hidden_typelist, pvoid_type_node); /* caf_token. */
3504 vec_safe_push (hidden_typelist, gfc_array_index_type); /* caf_offset. */
3508 /* Put hidden character length, caf_token, caf_offset at the end. */
3509 vec_safe_reserve (typelist, vec_safe_length (hidden_typelist));
3510 vec_safe_splice (typelist, hidden_typelist);
3512 if (!vec_safe_is_empty (typelist)
3513 || sym->attr.is_main_program
3514 || sym->attr.if_source != IFSRC_UNKNOWN)
3515 is_varargs = false;
3517 if (sym->backend_decl == error_mark_node)
3518 sym->backend_decl = NULL_TREE;
3520 arg_type_list_done:
3522 if (alternate_return)
3523 type = integer_type_node;
3524 else if (!sym->attr.function || gfc_return_by_reference (sym))
3525 type = void_type_node;
3526 else if (sym->attr.mixed_entry_master)
3527 type = gfc_get_mixed_entry_union (sym->ns);
3528 else if (flag_f2c && sym->ts.type == BT_REAL
3529 && sym->ts.kind == gfc_default_real_kind
3530 && !sym->attr.pointer
3531 && !sym->attr.allocatable
3532 && !sym->attr.always_explicit)
3534 /* Special case: f2c calling conventions require that (scalar)
3535 default REAL functions return the C type double instead. f2c
3536 compatibility is only an issue with functions that don't
3537 require an explicit interface, as only these could be
3538 implemented in Fortran 77. */
3539 sym->ts.kind = gfc_default_double_kind;
3540 type = gfc_typenode_for_spec (&sym->ts);
3541 sym->ts.kind = gfc_default_real_kind;
3543 else if (sym->result && sym->result->attr.proc_pointer)
3544 /* Procedure pointer return values. */
3546 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3548 /* Unset proc_pointer as gfc_get_function_type
3549 is called recursively. */
3550 sym->result->attr.proc_pointer = 0;
3551 type = build_pointer_type (gfc_get_function_type (sym->result));
3552 sym->result->attr.proc_pointer = 1;
3554 else
3555 type = gfc_sym_type (sym->result);
3557 else
3558 type = gfc_sym_type (sym);
3560 if (is_varargs)
3561 /* This should be represented as an unprototyped type, not a type
3562 with (...) prototype. */
3563 type = build_function_type (type, NULL_TREE);
3564 else
3565 type = build_function_type_vec (type, typelist);
3567 /* If we were passed an fn spec, add it here, otherwise determine it from
3568 the formal arguments. */
3569 if (fnspec)
3571 tree tmp;
3572 int spec_len = strlen (fnspec);
3573 tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3574 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3575 type = build_type_attribute_variant (type, tmp);
3577 else
3578 type = create_fn_spec (sym, type);
3580 return type;
3583 /* Language hooks for middle-end access to type nodes. */
3585 /* Return an integer type with BITS bits of precision,
3586 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3588 tree
3589 gfc_type_for_size (unsigned bits, int unsignedp)
3591 if (!unsignedp)
3593 int i;
3594 for (i = 0; i <= MAX_INT_KINDS; ++i)
3596 tree type = gfc_integer_types[i];
3597 if (type && bits == TYPE_PRECISION (type))
3598 return type;
3601 /* Handle TImode as a special case because it is used by some backends
3602 (e.g. ARM) even though it is not available for normal use. */
3603 #if HOST_BITS_PER_WIDE_INT >= 64
3604 if (bits == TYPE_PRECISION (intTI_type_node))
3605 return intTI_type_node;
3606 #endif
3608 if (bits <= TYPE_PRECISION (intQI_type_node))
3609 return intQI_type_node;
3610 if (bits <= TYPE_PRECISION (intHI_type_node))
3611 return intHI_type_node;
3612 if (bits <= TYPE_PRECISION (intSI_type_node))
3613 return intSI_type_node;
3614 if (bits <= TYPE_PRECISION (intDI_type_node))
3615 return intDI_type_node;
3616 if (bits <= TYPE_PRECISION (intTI_type_node))
3617 return intTI_type_node;
3619 else
3621 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3622 return unsigned_intQI_type_node;
3623 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3624 return unsigned_intHI_type_node;
3625 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3626 return unsigned_intSI_type_node;
3627 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3628 return unsigned_intDI_type_node;
3629 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3630 return unsigned_intTI_type_node;
3633 return NULL_TREE;
3636 /* Return a data type that has machine mode MODE. If the mode is an
3637 integer, then UNSIGNEDP selects between signed and unsigned types. */
3639 tree
3640 gfc_type_for_mode (machine_mode mode, int unsignedp)
3642 int i;
3643 tree *base;
3644 scalar_int_mode int_mode;
3646 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3647 base = gfc_real_types;
3648 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3649 base = gfc_complex_types;
3650 else if (is_a <scalar_int_mode> (mode, &int_mode))
3652 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3653 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3655 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3656 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3658 unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
3659 GET_MODE_NUNITS (mode));
3660 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3661 return build_vector_type_for_mode (bool_type, mode);
3663 else if (VECTOR_MODE_P (mode)
3664 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3666 machine_mode inner_mode = GET_MODE_INNER (mode);
3667 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3668 if (inner_type != NULL_TREE)
3669 return build_vector_type_for_mode (inner_type, mode);
3670 return NULL_TREE;
3672 else
3673 return NULL_TREE;
3675 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3677 tree type = base[i];
3678 if (type && mode == TYPE_MODE (type))
3679 return type;
3682 return NULL_TREE;
3685 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3686 in that case. */
3688 bool
3689 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3691 int rank, dim;
3692 bool indirect = false;
3693 tree etype, ptype, t, base_decl;
3694 tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
3695 tree lower_suboff, upper_suboff, stride_suboff;
3696 tree dtype, field, rank_off;
3698 if (! GFC_DESCRIPTOR_TYPE_P (type))
3700 if (! POINTER_TYPE_P (type))
3701 return false;
3702 type = TREE_TYPE (type);
3703 if (! GFC_DESCRIPTOR_TYPE_P (type))
3704 return false;
3705 indirect = true;
3708 rank = GFC_TYPE_ARRAY_RANK (type);
3709 if (rank >= (int) (ARRAY_SIZE (info->dimen)))
3710 return false;
3712 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3713 gcc_assert (POINTER_TYPE_P (etype));
3714 etype = TREE_TYPE (etype);
3716 /* If the type is not a scalar coarray. */
3717 if (TREE_CODE (etype) == ARRAY_TYPE)
3718 etype = TREE_TYPE (etype);
3720 /* Can't handle variable sized elements yet. */
3721 if (int_size_in_bytes (etype) <= 0)
3722 return false;
3723 /* Nor non-constant lower bounds in assumed shape arrays. */
3724 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3725 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3727 for (dim = 0; dim < rank; dim++)
3728 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3729 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3730 return false;
3733 memset (info, '\0', sizeof (*info));
3734 info->ndimensions = rank;
3735 info->ordering = array_descr_ordering_column_major;
3736 info->element_type = etype;
3737 ptype = build_pointer_type (gfc_array_index_type);
3738 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3739 if (!base_decl)
3741 base_decl = build_debug_expr_decl (indirect
3742 ? build_pointer_type (ptype) : ptype);
3743 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3745 info->base_decl = base_decl;
3746 if (indirect)
3747 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3749 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3750 &dim_off, &dim_size, &stride_suboff,
3751 &lower_suboff, &upper_suboff);
3753 t = fold_build_pointer_plus (base_decl, span_off);
3754 elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3756 t = base_decl;
3757 if (!integer_zerop (data_off))
3758 t = fold_build_pointer_plus (t, data_off);
3759 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3760 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3761 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3762 info->allocated = build2 (NE_EXPR, logical_type_node,
3763 info->data_location, null_pointer_node);
3764 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3765 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3766 info->associated = build2 (NE_EXPR, logical_type_node,
3767 info->data_location, null_pointer_node);
3768 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3769 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3770 && dwarf_version >= 5)
3772 rank = 1;
3773 info->ndimensions = 1;
3774 t = fold_build_pointer_plus (base_decl, dtype_off);
3775 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3776 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3777 rank_off = byte_position (field);
3778 t = fold_build_pointer_plus (t, rank_off);
3780 t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
3781 t = build1 (INDIRECT_REF, TREE_TYPE (field), t);
3782 info->rank = t;
3783 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3784 t = size_binop (MULT_EXPR, t, dim_size);
3785 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3788 for (dim = 0; dim < rank; dim++)
3790 t = fold_build_pointer_plus (base_decl,
3791 size_binop (PLUS_EXPR,
3792 dim_off, lower_suboff));
3793 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3794 info->dimen[dim].lower_bound = t;
3795 t = fold_build_pointer_plus (base_decl,
3796 size_binop (PLUS_EXPR,
3797 dim_off, upper_suboff));
3798 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3799 info->dimen[dim].upper_bound = t;
3800 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3801 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3803 /* Assumed shape arrays have known lower bounds. */
3804 info->dimen[dim].upper_bound
3805 = build2 (MINUS_EXPR, gfc_array_index_type,
3806 info->dimen[dim].upper_bound,
3807 info->dimen[dim].lower_bound);
3808 info->dimen[dim].lower_bound
3809 = fold_convert (gfc_array_index_type,
3810 GFC_TYPE_ARRAY_LBOUND (type, dim));
3811 info->dimen[dim].upper_bound
3812 = build2 (PLUS_EXPR, gfc_array_index_type,
3813 info->dimen[dim].lower_bound,
3814 info->dimen[dim].upper_bound);
3816 t = fold_build_pointer_plus (base_decl,
3817 size_binop (PLUS_EXPR,
3818 dim_off, stride_suboff));
3819 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3820 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3821 info->dimen[dim].stride = t;
3822 if (dim + 1 < rank)
3823 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3826 return true;
3830 /* Create a type to handle vector subscripts for coarray library calls. It
3831 has the form:
3832 struct caf_vector_t {
3833 size_t nvec; // size of the vector
3834 union {
3835 struct {
3836 void *vector;
3837 int kind;
3838 } v;
3839 struct {
3840 ptrdiff_t lower_bound;
3841 ptrdiff_t upper_bound;
3842 ptrdiff_t stride;
3843 } triplet;
3844 } u;
3846 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3847 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3849 tree
3850 gfc_get_caf_vector_type (int dim)
3852 static tree vector_types[GFC_MAX_DIMENSIONS];
3853 static tree vec_type = NULL_TREE;
3854 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3856 if (vector_types[dim-1] != NULL_TREE)
3857 return vector_types[dim-1];
3859 if (vec_type == NULL_TREE)
3861 chain = 0;
3862 vect_struct_type = make_node (RECORD_TYPE);
3863 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3864 get_identifier ("vector"),
3865 pvoid_type_node, &chain);
3866 suppress_warning (tmp);
3867 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3868 get_identifier ("kind"),
3869 integer_type_node, &chain);
3870 suppress_warning (tmp);
3871 gfc_finish_type (vect_struct_type);
3873 chain = 0;
3874 triplet_struct_type = make_node (RECORD_TYPE);
3875 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3876 get_identifier ("lower_bound"),
3877 gfc_array_index_type, &chain);
3878 suppress_warning (tmp);
3879 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3880 get_identifier ("upper_bound"),
3881 gfc_array_index_type, &chain);
3882 suppress_warning (tmp);
3883 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3884 gfc_array_index_type, &chain);
3885 suppress_warning (tmp);
3886 gfc_finish_type (triplet_struct_type);
3888 chain = 0;
3889 union_type = make_node (UNION_TYPE);
3890 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3891 vect_struct_type, &chain);
3892 suppress_warning (tmp);
3893 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3894 triplet_struct_type, &chain);
3895 suppress_warning (tmp);
3896 gfc_finish_type (union_type);
3898 chain = 0;
3899 vec_type = make_node (RECORD_TYPE);
3900 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3901 size_type_node, &chain);
3902 suppress_warning (tmp);
3903 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3904 union_type, &chain);
3905 suppress_warning (tmp);
3906 gfc_finish_type (vec_type);
3907 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3910 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3911 gfc_rank_cst[dim-1]);
3912 vector_types[dim-1] = build_array_type (vec_type, tmp);
3913 return vector_types[dim-1];
3917 tree
3918 gfc_get_caf_reference_type ()
3920 static tree reference_type = NULL_TREE;
3921 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3922 a_struct_type, u_union_type, tmp, *chain;
3924 if (reference_type != NULL_TREE)
3925 return reference_type;
3927 chain = 0;
3928 c_struct_type = make_node (RECORD_TYPE);
3929 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3930 get_identifier ("offset"),
3931 gfc_array_index_type, &chain);
3932 suppress_warning (tmp);
3933 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3934 get_identifier ("caf_token_offset"),
3935 gfc_array_index_type, &chain);
3936 suppress_warning (tmp);
3937 gfc_finish_type (c_struct_type);
3939 chain = 0;
3940 s_struct_type = make_node (RECORD_TYPE);
3941 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3942 get_identifier ("start"),
3943 gfc_array_index_type, &chain);
3944 suppress_warning (tmp);
3945 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3946 get_identifier ("end"),
3947 gfc_array_index_type, &chain);
3948 suppress_warning (tmp);
3949 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3950 get_identifier ("stride"),
3951 gfc_array_index_type, &chain);
3952 suppress_warning (tmp);
3953 gfc_finish_type (s_struct_type);
3955 chain = 0;
3956 v_struct_type = make_node (RECORD_TYPE);
3957 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3958 get_identifier ("vector"),
3959 pvoid_type_node, &chain);
3960 suppress_warning (tmp);
3961 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3962 get_identifier ("nvec"),
3963 size_type_node, &chain);
3964 suppress_warning (tmp);
3965 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3966 get_identifier ("kind"),
3967 integer_type_node, &chain);
3968 suppress_warning (tmp);
3969 gfc_finish_type (v_struct_type);
3971 chain = 0;
3972 union_type = make_node (UNION_TYPE);
3973 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3974 s_struct_type, &chain);
3975 suppress_warning (tmp);
3976 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3977 v_struct_type, &chain);
3978 suppress_warning (tmp);
3979 gfc_finish_type (union_type);
3981 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3982 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3983 dim_union_type = build_array_type (union_type, tmp);
3985 chain = 0;
3986 a_struct_type = make_node (RECORD_TYPE);
3987 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3988 build_array_type (unsigned_char_type_node,
3989 build_range_type (gfc_array_index_type,
3990 gfc_index_zero_node,
3991 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3992 &chain);
3993 suppress_warning (tmp);
3994 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3995 get_identifier ("static_array_type"),
3996 integer_type_node, &chain);
3997 suppress_warning (tmp);
3998 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3999 dim_union_type, &chain);
4000 suppress_warning (tmp);
4001 gfc_finish_type (a_struct_type);
4003 chain = 0;
4004 u_union_type = make_node (UNION_TYPE);
4005 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
4006 c_struct_type, &chain);
4007 suppress_warning (tmp);
4008 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
4009 a_struct_type, &chain);
4010 suppress_warning (tmp);
4011 gfc_finish_type (u_union_type);
4013 chain = 0;
4014 reference_type = make_node (RECORD_TYPE);
4015 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
4016 build_pointer_type (reference_type), &chain);
4017 suppress_warning (tmp);
4018 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
4019 integer_type_node, &chain);
4020 suppress_warning (tmp);
4021 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
4022 size_type_node, &chain);
4023 suppress_warning (tmp);
4024 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
4025 u_union_type, &chain);
4026 suppress_warning (tmp);
4027 gfc_finish_type (reference_type);
4028 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
4030 return reference_type;
4033 static tree
4034 gfc_get_cfi_dim_type ()
4036 static tree CFI_dim_t = NULL;
4038 if (CFI_dim_t)
4039 return CFI_dim_t;
4041 CFI_dim_t = make_node (RECORD_TYPE);
4042 TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
4043 TYPE_NAMELESS (CFI_dim_t) = 1;
4044 tree field;
4045 tree *chain = NULL;
4046 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
4047 gfc_array_index_type, &chain);
4048 suppress_warning (field);
4049 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
4050 gfc_array_index_type, &chain);
4051 suppress_warning (field);
4052 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
4053 gfc_array_index_type, &chain);
4054 suppress_warning (field);
4055 gfc_finish_type (CFI_dim_t);
4056 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
4057 return CFI_dim_t;
4061 /* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
4062 otherwise dim[dimen] is used. */
4064 tree
4065 gfc_get_cfi_type (int dimen, bool restricted)
4067 gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
4069 int idx = 2*(dimen + 1) + restricted;
4071 if (gfc_cfi_descriptor_base[idx])
4072 return gfc_cfi_descriptor_base[idx];
4074 /* Build the type node. */
4075 tree CFI_cdesc_t = make_node (RECORD_TYPE);
4076 char name[GFC_MAX_SYMBOL_LEN + 1];
4077 if (dimen != -1)
4078 sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
4079 TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
4080 TYPE_NAMELESS (CFI_cdesc_t) = 1;
4082 tree field;
4083 tree *chain = NULL;
4084 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
4085 (restricted ? prvoid_type_node
4086 : ptr_type_node), &chain);
4087 suppress_warning (field);
4088 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
4089 size_type_node, &chain);
4090 suppress_warning (field);
4091 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
4092 integer_type_node, &chain);
4093 suppress_warning (field);
4094 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
4095 signed_char_type_node, &chain);
4096 suppress_warning (field);
4097 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
4098 signed_char_type_node, &chain);
4099 suppress_warning (field);
4100 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
4101 get_typenode_from_name (INT16_TYPE),
4102 &chain);
4103 suppress_warning (field);
4105 if (dimen != 0)
4107 tree range = NULL_TREE;
4108 if (dimen > 0)
4109 range = gfc_rank_cst[dimen - 1];
4110 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
4111 range);
4112 tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
4113 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
4114 CFI_dim_t, &chain);
4115 suppress_warning (field);
4118 TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
4119 gfc_finish_type (CFI_cdesc_t);
4120 gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
4121 return CFI_cdesc_t;
4124 #include "gt-fortran-trans-types.h"