c, c++: attribute format on a ctor with a vbase [PR101833, PR47634]
[official-gcc.git] / gcc / fortran / trans-types.cc
blob3cdc529eb2813ffe5bdd87d675c688b68fe60711
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2022 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;
76 static GTY(()) tree gfc_desc_dim_type;
77 static GTY(()) tree gfc_max_array_element_size;
78 static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
79 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
80 static GTY(()) tree gfc_cfi_descriptor_base[2 * (CFI_MAX_RANK + 2)];
82 /* Arrays for all integral and real kinds. We'll fill this in at runtime
83 after the target has a chance to process command-line options. */
85 #define MAX_INT_KINDS 5
86 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
87 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
88 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
89 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
91 #define MAX_REAL_KINDS 5
92 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
93 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
94 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
96 #define MAX_CHARACTER_KINDS 2
97 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
98 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
99 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
101 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
103 /* The integer kind to use for array indices. This will be set to the
104 proper value based on target information from the backend. */
106 int gfc_index_integer_kind;
108 /* The default kinds of the various types. */
110 int gfc_default_integer_kind;
111 int gfc_max_integer_kind;
112 int gfc_default_real_kind;
113 int gfc_default_double_kind;
114 int gfc_default_character_kind;
115 int gfc_default_logical_kind;
116 int gfc_default_complex_kind;
117 int gfc_c_int_kind;
118 int gfc_c_intptr_kind;
119 int gfc_atomic_int_kind;
120 int gfc_atomic_logical_kind;
122 /* The kind size used for record offsets. If the target system supports
123 kind=8, this will be set to 8, otherwise it is set to 4. */
124 int gfc_intio_kind;
126 /* The integer kind used to store character lengths. */
127 int gfc_charlen_int_kind;
129 /* Kind of internal integer for storing object sizes. */
130 int gfc_size_kind;
132 /* The size of the numeric storage unit and character storage unit. */
133 int gfc_numeric_storage_size;
134 int gfc_character_storage_size;
136 static tree dtype_type_node = NULL_TREE;
139 /* Build the dtype_type_node if necessary. */
140 tree get_dtype_type_node (void)
142 tree field;
143 tree dtype_node;
144 tree *dtype_chain = NULL;
146 if (dtype_type_node == NULL_TREE)
148 dtype_node = make_node (RECORD_TYPE);
149 TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
150 TYPE_NAMELESS (dtype_node) = 1;
151 field = gfc_add_field_to_struct_1 (dtype_node,
152 get_identifier ("elem_len"),
153 size_type_node, &dtype_chain);
154 suppress_warning (field);
155 field = gfc_add_field_to_struct_1 (dtype_node,
156 get_identifier ("version"),
157 integer_type_node, &dtype_chain);
158 suppress_warning (field);
159 field = gfc_add_field_to_struct_1 (dtype_node,
160 get_identifier ("rank"),
161 signed_char_type_node, &dtype_chain);
162 suppress_warning (field);
163 field = gfc_add_field_to_struct_1 (dtype_node,
164 get_identifier ("type"),
165 signed_char_type_node, &dtype_chain);
166 suppress_warning (field);
167 field = gfc_add_field_to_struct_1 (dtype_node,
168 get_identifier ("attribute"),
169 short_integer_type_node, &dtype_chain);
170 suppress_warning (field);
171 gfc_finish_type (dtype_node);
172 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
173 dtype_type_node = dtype_node;
175 return dtype_type_node;
178 static int
179 get_real_kind_from_node (tree type)
181 int i;
183 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
184 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
185 return gfc_real_kinds[i].kind;
187 return -4;
190 static int
191 get_int_kind_from_node (tree type)
193 int i;
195 if (!type)
196 return -2;
198 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
199 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
200 return gfc_integer_kinds[i].kind;
202 return -1;
205 static int
206 get_int_kind_from_name (const char *name)
208 return get_int_kind_from_node (get_typenode_from_name (name));
212 /* Get the kind number corresponding to an integer of given size,
213 following the required return values for ISO_FORTRAN_ENV INT* constants:
214 -2 is returned if we support a kind of larger size, -1 otherwise. */
216 gfc_get_int_kind_from_width_isofortranenv (int size)
218 int i;
220 /* Look for a kind with matching storage size. */
221 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
222 if (gfc_integer_kinds[i].bit_size == size)
223 return gfc_integer_kinds[i].kind;
225 /* Look for a kind with larger storage size. */
226 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
227 if (gfc_integer_kinds[i].bit_size > size)
228 return -2;
230 return -1;
234 /* Get the kind number corresponding to a real of a given storage size.
235 If two real's have the same storage size, then choose the real with
236 the largest precision. If a kind type is unavailable and a real
237 exists with wider storage, then return -2; otherwise, return -1. */
240 gfc_get_real_kind_from_width_isofortranenv (int size)
242 int digits, i, kind;
244 size /= 8;
246 kind = -1;
247 digits = 0;
249 /* Look for a kind with matching storage size. */
250 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
251 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
253 if (gfc_real_kinds[i].digits > digits)
255 digits = gfc_real_kinds[i].digits;
256 kind = gfc_real_kinds[i].kind;
260 if (kind != -1)
261 return kind;
263 /* Look for a kind with larger storage size. */
264 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
265 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
266 kind = -2;
268 return kind;
273 static int
274 get_int_kind_from_width (int size)
276 int i;
278 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
279 if (gfc_integer_kinds[i].bit_size == size)
280 return gfc_integer_kinds[i].kind;
282 return -2;
285 static int
286 get_int_kind_from_minimal_width (int size)
288 int i;
290 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
291 if (gfc_integer_kinds[i].bit_size >= size)
292 return gfc_integer_kinds[i].kind;
294 return -2;
298 /* Generate the CInteropKind_t objects for the C interoperable
299 kinds. */
301 void
302 gfc_init_c_interop_kinds (void)
304 int i;
306 /* init all pointers in the list to NULL */
307 for (i = 0; i < ISOCBINDING_NUMBER; i++)
309 /* Initialize the name and value fields. */
310 c_interop_kinds_table[i].name[0] = '\0';
311 c_interop_kinds_table[i].value = -100;
312 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
315 #define NAMED_INTCST(a,b,c,d) \
316 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
317 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
318 c_interop_kinds_table[a].value = c;
319 #define NAMED_REALCST(a,b,c,d) \
320 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
321 c_interop_kinds_table[a].f90_type = BT_REAL; \
322 c_interop_kinds_table[a].value = c;
323 #define NAMED_CMPXCST(a,b,c,d) \
324 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
325 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
326 c_interop_kinds_table[a].value = c;
327 #define NAMED_LOGCST(a,b,c) \
328 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
329 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
330 c_interop_kinds_table[a].value = c;
331 #define NAMED_CHARKNDCST(a,b,c) \
332 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
333 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
334 c_interop_kinds_table[a].value = c;
335 #define NAMED_CHARCST(a,b,c) \
336 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
337 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
338 c_interop_kinds_table[a].value = c;
339 #define DERIVED_TYPE(a,b,c) \
340 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
341 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
342 c_interop_kinds_table[a].value = c;
343 #define NAMED_FUNCTION(a,b,c,d) \
344 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
345 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
346 c_interop_kinds_table[a].value = c;
347 #define NAMED_SUBROUTINE(a,b,c,d) \
348 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
349 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
350 c_interop_kinds_table[a].value = c;
351 #include "iso-c-binding.def"
355 /* Query the target to determine which machine modes are available for
356 computation. Choose KIND numbers for them. */
358 void
359 gfc_init_kinds (void)
361 opt_scalar_int_mode int_mode_iter;
362 opt_scalar_float_mode float_mode_iter;
363 int i_index, r_index, kind;
364 bool saw_i4 = false, saw_i8 = false;
365 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
366 scalar_mode r16_mode = QImode;
367 scalar_mode composite_mode = QImode;
369 i_index = 0;
370 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
372 scalar_int_mode mode = int_mode_iter.require ();
373 int kind, bitsize;
375 if (!targetm.scalar_mode_supported_p (mode))
376 continue;
378 /* The middle end doesn't support constants larger than 2*HWI.
379 Perhaps the target hook shouldn't have accepted these either,
380 but just to be safe... */
381 bitsize = GET_MODE_BITSIZE (mode);
382 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
383 continue;
385 gcc_assert (i_index != MAX_INT_KINDS);
387 /* Let the kind equal the bit size divided by 8. This insulates the
388 programmer from the underlying byte size. */
389 kind = bitsize / 8;
391 if (kind == 4)
392 saw_i4 = true;
393 if (kind == 8)
394 saw_i8 = true;
396 gfc_integer_kinds[i_index].kind = kind;
397 gfc_integer_kinds[i_index].radix = 2;
398 gfc_integer_kinds[i_index].digits = bitsize - 1;
399 gfc_integer_kinds[i_index].bit_size = bitsize;
401 gfc_logical_kinds[i_index].kind = kind;
402 gfc_logical_kinds[i_index].bit_size = bitsize;
404 i_index += 1;
407 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
408 used for large file access. */
410 if (saw_i8)
411 gfc_intio_kind = 8;
412 else
413 gfc_intio_kind = 4;
415 /* If we do not at least have kind = 4, everything is pointless. */
416 gcc_assert(saw_i4);
418 /* Set the maximum integer kind. Used with at least BOZ constants. */
419 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
421 r_index = 0;
422 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
424 scalar_float_mode mode = float_mode_iter.require ();
425 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
426 int kind;
428 if (fmt == NULL)
429 continue;
430 if (!targetm.scalar_mode_supported_p (mode))
431 continue;
433 if (MODE_COMPOSITE_P (mode)
434 && (GET_MODE_PRECISION (mode) + 7) / 8 == 16)
435 composite_mode = mode;
437 /* Only let float, double, long double and TFmode go through.
438 Runtime support for others is not provided, so they would be
439 useless. */
440 if (!targetm.libgcc_floating_mode_supported_p (mode))
441 continue;
442 if (mode != TYPE_MODE (float_type_node)
443 && (mode != TYPE_MODE (double_type_node))
444 && (mode != TYPE_MODE (long_double_type_node))
445 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
446 && (mode != TFmode)
447 #endif
449 continue;
451 /* Let the kind equal the precision divided by 8, rounding up. Again,
452 this insulates the programmer from the underlying byte size.
454 Also, it effectively deals with IEEE extended formats. There, the
455 total size of the type may equal 16, but it's got 6 bytes of padding
456 and the increased size can get in the way of a real IEEE quad format
457 which may also be supported by the target.
459 We round up so as to handle IA-64 __floatreg (RFmode), which is an
460 82 bit type. Not to be confused with __float80 (XFmode), which is
461 an 80 bit type also supported by IA-64. So XFmode should come out
462 to be kind=10, and RFmode should come out to be kind=11. Egads.
464 TODO: The kind calculation has to be modified to support all
465 three 128-bit floating-point modes on PowerPC as IFmode, KFmode,
466 and TFmode since the following line would all map to kind=16.
467 However, currently only float, double, long double, and TFmode
468 reach this code.
471 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
473 if (kind == 4)
474 saw_r4 = true;
475 if (kind == 8)
476 saw_r8 = true;
477 if (kind == 10)
478 saw_r10 = true;
479 if (kind == 16)
481 saw_r16 = true;
482 r16_mode = mode;
485 /* Careful we don't stumble a weird internal mode. */
486 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
487 /* Or have too many modes for the allocated space. */
488 gcc_assert (r_index != MAX_REAL_KINDS);
490 gfc_real_kinds[r_index].kind = kind;
491 gfc_real_kinds[r_index].abi_kind = kind;
492 gfc_real_kinds[r_index].radix = fmt->b;
493 gfc_real_kinds[r_index].digits = fmt->p;
494 gfc_real_kinds[r_index].min_exponent = fmt->emin;
495 gfc_real_kinds[r_index].max_exponent = fmt->emax;
496 if (fmt->pnan < fmt->p)
497 /* This is an IBM extended double format (or the MIPS variant)
498 made up of two IEEE doubles. The value of the long double is
499 the sum of the values of the two parts. The most significant
500 part is required to be the value of the long double rounded
501 to the nearest double. If we use emax of 1024 then we can't
502 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
503 rounding will make the most significant part overflow. */
504 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
505 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
506 r_index += 1;
509 /* Detect the powerpc64le-linux case with -mabi=ieeelongdouble, where
510 the long double type is non-MODE_COMPOSITE_P TFmode but one can use
511 -mabi=ibmlongdouble too and get MODE_COMPOSITE_P TFmode with the same
512 precision. For libgfortran calls pretend the IEEE 754 quad TFmode has
513 kind 17 rather than 16 and use kind 16 for the IBM extended format
514 TFmode. */
515 if (composite_mode != QImode && saw_r16 && !MODE_COMPOSITE_P (r16_mode))
517 for (int i = 0; i < r_index; ++i)
518 if (gfc_real_kinds[i].kind == 16)
520 gfc_real_kinds[i].abi_kind = 17;
521 if (flag_building_libgfortran
522 && (TARGET_GLIBC_MAJOR < 2
523 || (TARGET_GLIBC_MAJOR == 2 && TARGET_GLIBC_MINOR < 32)))
525 gfc_real16_is_float128 = true;
526 gfc_real_kinds[i].c_float128 = 1;
530 else if ((flag_convert & (GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM)) != 0)
531 gfc_fatal_error ("%<-fconvert=r16_ieee%> or %<-fconvert=r16_ibm%> not "
532 "supported on this architecture");
534 /* Choose the default integer kind. We choose 4 unless the user directs us
535 otherwise. Even if the user specified that the default integer kind is 8,
536 the numeric storage size is not 64 bits. In this case, a warning will be
537 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
539 gfc_numeric_storage_size = 4 * 8;
541 if (flag_default_integer)
543 if (!saw_i8)
544 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
545 "%<-fdefault-integer-8%> option");
547 gfc_default_integer_kind = 8;
550 else if (flag_integer4_kind == 8)
552 if (!saw_i8)
553 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
554 "%<-finteger-4-integer-8%> option");
556 gfc_default_integer_kind = 8;
558 else if (saw_i4)
560 gfc_default_integer_kind = 4;
562 else
564 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
565 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
568 /* Choose the default real kind. Again, we choose 4 when possible. */
569 if (flag_default_real_8)
571 if (!saw_r8)
572 gfc_fatal_error ("REAL(KIND=8) is not available for "
573 "%<-fdefault-real-8%> option");
575 gfc_default_real_kind = 8;
577 else if (flag_default_real_10)
579 if (!saw_r10)
580 gfc_fatal_error ("REAL(KIND=10) is not available for "
581 "%<-fdefault-real-10%> option");
583 gfc_default_real_kind = 10;
585 else if (flag_default_real_16)
587 if (!saw_r16)
588 gfc_fatal_error ("REAL(KIND=16) is not available for "
589 "%<-fdefault-real-16%> option");
591 gfc_default_real_kind = 16;
593 else if (flag_real4_kind == 8)
595 if (!saw_r8)
596 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
597 "option");
599 gfc_default_real_kind = 8;
601 else if (flag_real4_kind == 10)
603 if (!saw_r10)
604 gfc_fatal_error ("REAL(KIND=10) is not available for "
605 "%<-freal-4-real-10%> option");
607 gfc_default_real_kind = 10;
609 else if (flag_real4_kind == 16)
611 if (!saw_r16)
612 gfc_fatal_error ("REAL(KIND=16) is not available for "
613 "%<-freal-4-real-16%> option");
615 gfc_default_real_kind = 16;
617 else if (saw_r4)
618 gfc_default_real_kind = 4;
619 else
620 gfc_default_real_kind = gfc_real_kinds[0].kind;
622 /* Choose the default double kind. If -fdefault-real and -fdefault-double
623 are specified, we use kind=8, if it's available. If -fdefault-real is
624 specified without -fdefault-double, we use kind=16, if it's available.
625 Otherwise we do not change anything. */
626 if (flag_default_double && saw_r8)
627 gfc_default_double_kind = 8;
628 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
630 /* Use largest available kind. */
631 if (saw_r16)
632 gfc_default_double_kind = 16;
633 else if (saw_r10)
634 gfc_default_double_kind = 10;
635 else if (saw_r8)
636 gfc_default_double_kind = 8;
637 else
638 gfc_default_double_kind = gfc_default_real_kind;
640 else if (flag_real8_kind == 4)
642 if (!saw_r4)
643 gfc_fatal_error ("REAL(KIND=4) is not available for "
644 "%<-freal-8-real-4%> option");
646 gfc_default_double_kind = 4;
648 else if (flag_real8_kind == 10 )
650 if (!saw_r10)
651 gfc_fatal_error ("REAL(KIND=10) is not available for "
652 "%<-freal-8-real-10%> option");
654 gfc_default_double_kind = 10;
656 else if (flag_real8_kind == 16 )
658 if (!saw_r16)
659 gfc_fatal_error ("REAL(KIND=10) is not available for "
660 "%<-freal-8-real-16%> option");
662 gfc_default_double_kind = 16;
664 else if (saw_r4 && saw_r8)
665 gfc_default_double_kind = 8;
666 else
668 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
669 real ... occupies two contiguous numeric storage units.
671 Therefore we must be supplied a kind twice as large as we chose
672 for single precision. There are loopholes, in that double
673 precision must *occupy* two storage units, though it doesn't have
674 to *use* two storage units. Which means that you can make this
675 kind artificially wide by padding it. But at present there are
676 no GCC targets for which a two-word type does not exist, so we
677 just let gfc_validate_kind abort and tell us if something breaks. */
679 gfc_default_double_kind
680 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
683 /* The default logical kind is constrained to be the same as the
684 default integer kind. Similarly with complex and real. */
685 gfc_default_logical_kind = gfc_default_integer_kind;
686 gfc_default_complex_kind = gfc_default_real_kind;
688 /* We only have two character kinds: ASCII and UCS-4.
689 ASCII corresponds to a 8-bit integer type, if one is available.
690 UCS-4 corresponds to a 32-bit integer type, if one is available. */
691 i_index = 0;
692 if ((kind = get_int_kind_from_width (8)) > 0)
694 gfc_character_kinds[i_index].kind = kind;
695 gfc_character_kinds[i_index].bit_size = 8;
696 gfc_character_kinds[i_index].name = "ascii";
697 i_index++;
699 if ((kind = get_int_kind_from_width (32)) > 0)
701 gfc_character_kinds[i_index].kind = kind;
702 gfc_character_kinds[i_index].bit_size = 32;
703 gfc_character_kinds[i_index].name = "iso_10646";
704 i_index++;
707 /* Choose the smallest integer kind for our default character. */
708 gfc_default_character_kind = gfc_character_kinds[0].kind;
709 gfc_character_storage_size = gfc_default_character_kind * 8;
711 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
713 /* Pick a kind the same size as the C "int" type. */
714 gfc_c_int_kind = INT_TYPE_SIZE / 8;
716 /* Choose atomic kinds to match C's int. */
717 gfc_atomic_int_kind = gfc_c_int_kind;
718 gfc_atomic_logical_kind = gfc_c_int_kind;
720 gfc_c_intptr_kind = POINTER_SIZE / 8;
724 /* Make sure that a valid kind is present. Returns an index into the
725 associated kinds array, -1 if the kind is not present. */
727 static int
728 validate_integer (int kind)
730 int i;
732 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
733 if (gfc_integer_kinds[i].kind == kind)
734 return i;
736 return -1;
739 static int
740 validate_real (int kind)
742 int i;
744 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
745 if (gfc_real_kinds[i].kind == kind)
746 return i;
748 return -1;
751 static int
752 validate_logical (int kind)
754 int i;
756 for (i = 0; gfc_logical_kinds[i].kind; i++)
757 if (gfc_logical_kinds[i].kind == kind)
758 return i;
760 return -1;
763 static int
764 validate_character (int kind)
766 int i;
768 for (i = 0; gfc_character_kinds[i].kind; i++)
769 if (gfc_character_kinds[i].kind == kind)
770 return i;
772 return -1;
775 /* Validate a kind given a basic type. The return value is the same
776 for the child functions, with -1 indicating nonexistence of the
777 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
780 gfc_validate_kind (bt type, int kind, bool may_fail)
782 int rc;
784 switch (type)
786 case BT_REAL: /* Fall through */
787 case BT_COMPLEX:
788 rc = validate_real (kind);
789 break;
790 case BT_INTEGER:
791 rc = validate_integer (kind);
792 break;
793 case BT_LOGICAL:
794 rc = validate_logical (kind);
795 break;
796 case BT_CHARACTER:
797 rc = validate_character (kind);
798 break;
800 default:
801 gfc_internal_error ("gfc_validate_kind(): Got bad type");
804 if (rc < 0 && !may_fail)
805 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
807 return rc;
811 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
812 Reuse common type nodes where possible. Recognize if the kind matches up
813 with a C type. This will be used later in determining which routines may
814 be scarfed from libm. */
816 static tree
817 gfc_build_int_type (gfc_integer_info *info)
819 int mode_precision = info->bit_size;
821 if (mode_precision == CHAR_TYPE_SIZE)
822 info->c_char = 1;
823 if (mode_precision == SHORT_TYPE_SIZE)
824 info->c_short = 1;
825 if (mode_precision == INT_TYPE_SIZE)
826 info->c_int = 1;
827 if (mode_precision == LONG_TYPE_SIZE)
828 info->c_long = 1;
829 if (mode_precision == LONG_LONG_TYPE_SIZE)
830 info->c_long_long = 1;
832 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
833 return intQI_type_node;
834 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
835 return intHI_type_node;
836 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
837 return intSI_type_node;
838 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
839 return intDI_type_node;
840 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
841 return intTI_type_node;
843 return make_signed_type (mode_precision);
846 tree
847 gfc_build_uint_type (int size)
849 if (size == CHAR_TYPE_SIZE)
850 return unsigned_char_type_node;
851 if (size == SHORT_TYPE_SIZE)
852 return short_unsigned_type_node;
853 if (size == INT_TYPE_SIZE)
854 return unsigned_type_node;
855 if (size == LONG_TYPE_SIZE)
856 return long_unsigned_type_node;
857 if (size == LONG_LONG_TYPE_SIZE)
858 return long_long_unsigned_type_node;
860 return make_unsigned_type (size);
864 static tree
865 gfc_build_real_type (gfc_real_info *info)
867 int mode_precision = info->mode_precision;
868 tree new_type;
870 if (mode_precision == FLOAT_TYPE_SIZE)
871 info->c_float = 1;
872 if (mode_precision == DOUBLE_TYPE_SIZE)
873 info->c_double = 1;
874 if (mode_precision == LONG_DOUBLE_TYPE_SIZE && !info->c_float128)
875 info->c_long_double = 1;
876 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
878 /* TODO: see PR101835. */
879 info->c_float128 = 1;
880 gfc_real16_is_float128 = true;
883 if (TYPE_PRECISION (float_type_node) == mode_precision)
884 return float_type_node;
885 if (TYPE_PRECISION (double_type_node) == mode_precision)
886 return double_type_node;
887 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
888 return long_double_type_node;
890 new_type = make_node (REAL_TYPE);
891 TYPE_PRECISION (new_type) = mode_precision;
892 layout_type (new_type);
893 return new_type;
896 static tree
897 gfc_build_complex_type (tree scalar_type)
899 tree new_type;
901 if (scalar_type == NULL)
902 return NULL;
903 if (scalar_type == float_type_node)
904 return complex_float_type_node;
905 if (scalar_type == double_type_node)
906 return complex_double_type_node;
907 if (scalar_type == long_double_type_node)
908 return complex_long_double_type_node;
910 new_type = make_node (COMPLEX_TYPE);
911 TREE_TYPE (new_type) = scalar_type;
912 layout_type (new_type);
913 return new_type;
916 static tree
917 gfc_build_logical_type (gfc_logical_info *info)
919 int bit_size = info->bit_size;
920 tree new_type;
922 if (bit_size == BOOL_TYPE_SIZE)
924 info->c_bool = 1;
925 return boolean_type_node;
928 new_type = make_unsigned_type (bit_size);
929 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
930 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
931 TYPE_PRECISION (new_type) = 1;
933 return new_type;
937 /* Create the backend type nodes. We map them to their
938 equivalent C type, at least for now. We also give
939 names to the types here, and we push them in the
940 global binding level context.*/
942 void
943 gfc_init_types (void)
945 char name_buf[26];
946 int index;
947 tree type;
948 unsigned n;
950 /* Create and name the types. */
951 #define PUSH_TYPE(name, node) \
952 pushdecl (build_decl (input_location, \
953 TYPE_DECL, get_identifier (name), node))
955 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
957 type = gfc_build_int_type (&gfc_integer_kinds[index]);
958 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
959 if (TYPE_STRING_FLAG (type))
960 type = make_signed_type (gfc_integer_kinds[index].bit_size);
961 gfc_integer_types[index] = type;
962 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
963 gfc_integer_kinds[index].kind);
964 PUSH_TYPE (name_buf, type);
967 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
969 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
970 gfc_logical_types[index] = type;
971 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
972 gfc_logical_kinds[index].kind);
973 PUSH_TYPE (name_buf, type);
976 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
978 type = gfc_build_real_type (&gfc_real_kinds[index]);
979 gfc_real_types[index] = type;
980 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
981 gfc_real_kinds[index].kind);
982 PUSH_TYPE (name_buf, type);
984 if (gfc_real_kinds[index].c_float128)
985 gfc_float128_type_node = type;
987 type = gfc_build_complex_type (type);
988 gfc_complex_types[index] = type;
989 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
990 gfc_real_kinds[index].kind);
991 PUSH_TYPE (name_buf, type);
993 if (gfc_real_kinds[index].c_float128)
994 gfc_complex_float128_type_node = type;
997 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
999 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
1000 type = build_qualified_type (type, TYPE_UNQUALIFIED);
1001 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
1002 gfc_character_kinds[index].kind);
1003 PUSH_TYPE (name_buf, type);
1004 gfc_character_types[index] = type;
1005 gfc_pcharacter_types[index] = build_pointer_type (type);
1007 gfc_character1_type_node = gfc_character_types[0];
1009 PUSH_TYPE ("byte", unsigned_char_type_node);
1010 PUSH_TYPE ("void", void_type_node);
1012 /* DBX debugging output gets upset if these aren't set. */
1013 if (!TYPE_NAME (integer_type_node))
1014 PUSH_TYPE ("c_integer", integer_type_node);
1015 if (!TYPE_NAME (char_type_node))
1016 PUSH_TYPE ("c_char", char_type_node);
1018 #undef PUSH_TYPE
1020 pvoid_type_node = build_pointer_type (void_type_node);
1021 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
1022 ppvoid_type_node = build_pointer_type (pvoid_type_node);
1023 pchar_type_node = build_pointer_type (gfc_character1_type_node);
1024 pfunc_type_node
1025 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
1027 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
1028 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1029 since this function is called before gfc_init_constants. */
1030 gfc_array_range_type
1031 = build_range_type (gfc_array_index_type,
1032 build_int_cst (gfc_array_index_type, 0),
1033 NULL_TREE);
1035 /* The maximum array element size that can be handled is determined
1036 by the number of bits available to store this field in the array
1037 descriptor. */
1039 n = TYPE_PRECISION (size_type_node);
1040 gfc_max_array_element_size
1041 = wide_int_to_tree (size_type_node,
1042 wi::mask (n, UNSIGNED,
1043 TYPE_PRECISION (size_type_node)));
1045 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1046 logical_true_node = build_int_cst (logical_type_node, 1);
1047 logical_false_node = build_int_cst (logical_type_node, 0);
1049 /* Character lengths are of type size_t, except signed. */
1050 gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1051 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1053 /* Fortran kind number of size_type_node (size_t). This is used for
1054 the _size member in vtables. */
1055 gfc_size_kind = get_int_kind_from_node (size_type_node);
1058 /* Get the type node for the given type and kind. */
1060 tree
1061 gfc_get_int_type (int kind)
1063 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1064 return index < 0 ? 0 : gfc_integer_types[index];
1067 tree
1068 gfc_get_real_type (int kind)
1070 int index = gfc_validate_kind (BT_REAL, kind, true);
1071 return index < 0 ? 0 : gfc_real_types[index];
1074 tree
1075 gfc_get_complex_type (int kind)
1077 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1078 return index < 0 ? 0 : gfc_complex_types[index];
1081 tree
1082 gfc_get_logical_type (int kind)
1084 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1085 return index < 0 ? 0 : gfc_logical_types[index];
1088 tree
1089 gfc_get_char_type (int kind)
1091 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1092 return index < 0 ? 0 : gfc_character_types[index];
1095 tree
1096 gfc_get_pchar_type (int kind)
1098 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1099 return index < 0 ? 0 : gfc_pcharacter_types[index];
1103 /* Create a character type with the given kind and length. */
1105 tree
1106 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1108 tree bounds, type;
1110 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1111 type = build_array_type (eltype, bounds);
1112 TYPE_STRING_FLAG (type) = 1;
1114 return type;
1117 tree
1118 gfc_get_character_type_len (int kind, tree len)
1120 gfc_validate_kind (BT_CHARACTER, kind, false);
1121 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1125 /* Get a type node for a character kind. */
1127 tree
1128 gfc_get_character_type (int kind, gfc_charlen * cl)
1130 tree len;
1132 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1133 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1134 len = build_fold_indirect_ref (len);
1136 return gfc_get_character_type_len (kind, len);
1139 /* Convert a basic type. This will be an array for character types. */
1141 tree
1142 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1144 tree basetype;
1146 switch (spec->type)
1148 case BT_UNKNOWN:
1149 gcc_unreachable ();
1151 case BT_INTEGER:
1152 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1153 has been resolved. This is done so we can convert C_PTR and
1154 C_FUNPTR to simple variables that get translated to (void *). */
1155 if (spec->f90_type == BT_VOID)
1157 if (spec->u.derived
1158 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1159 basetype = ptr_type_node;
1160 else
1161 basetype = pfunc_type_node;
1163 else
1164 basetype = gfc_get_int_type (spec->kind);
1165 break;
1167 case BT_REAL:
1168 basetype = gfc_get_real_type (spec->kind);
1169 break;
1171 case BT_COMPLEX:
1172 basetype = gfc_get_complex_type (spec->kind);
1173 break;
1175 case BT_LOGICAL:
1176 basetype = gfc_get_logical_type (spec->kind);
1177 break;
1179 case BT_CHARACTER:
1180 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1181 break;
1183 case BT_HOLLERITH:
1184 /* Since this cannot be used, return a length one character. */
1185 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1186 gfc_index_one_node);
1187 break;
1189 case BT_UNION:
1190 basetype = gfc_get_union_type (spec->u.derived);
1191 break;
1193 case BT_DERIVED:
1194 case BT_CLASS:
1195 basetype = gfc_get_derived_type (spec->u.derived, codim);
1197 if (spec->type == BT_CLASS)
1198 GFC_CLASS_TYPE_P (basetype) = 1;
1200 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1201 type and kind to fit a (void *) and the basetype returned was a
1202 ptr_type_node. We need to pass up this new information to the
1203 symbol that was declared of type C_PTR or C_FUNPTR. */
1204 if (spec->u.derived->ts.f90_type == BT_VOID)
1206 spec->type = BT_INTEGER;
1207 spec->kind = gfc_index_integer_kind;
1208 spec->f90_type = BT_VOID;
1209 spec->is_c_interop = 1; /* Mark as escaping later. */
1211 break;
1212 case BT_VOID:
1213 case BT_ASSUMED:
1214 /* This is for the second arg to c_f_pointer and c_f_procpointer
1215 of the iso_c_binding module, to accept any ptr type. */
1216 basetype = ptr_type_node;
1217 if (spec->f90_type == BT_VOID)
1219 if (spec->u.derived
1220 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1221 basetype = ptr_type_node;
1222 else
1223 basetype = pfunc_type_node;
1225 break;
1226 case BT_PROCEDURE:
1227 basetype = pfunc_type_node;
1228 break;
1229 default:
1230 gcc_unreachable ();
1232 return basetype;
1235 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1237 static tree
1238 gfc_conv_array_bound (gfc_expr * expr)
1240 /* If expr is an integer constant, return that. */
1241 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1242 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1244 /* Otherwise return NULL. */
1245 return NULL_TREE;
1248 /* Return the type of an element of the array. Note that scalar coarrays
1249 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1250 (with POINTER_TYPE stripped) is returned. */
1252 tree
1253 gfc_get_element_type (tree type)
1255 tree element;
1257 if (GFC_ARRAY_TYPE_P (type))
1259 if (TREE_CODE (type) == POINTER_TYPE)
1260 type = TREE_TYPE (type);
1261 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1263 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1264 element = type;
1266 else
1268 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1269 element = TREE_TYPE (type);
1272 else
1274 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1275 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1277 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1278 element = TREE_TYPE (element);
1280 /* For arrays, which are not scalar coarrays. */
1281 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1282 element = TREE_TYPE (element);
1285 return element;
1288 /* Build an array. This function is called from gfc_sym_type().
1289 Actually returns array descriptor type.
1291 Format of array descriptors is as follows:
1293 struct gfc_array_descriptor
1295 array *data;
1296 index offset;
1297 struct dtype_type dtype;
1298 struct descriptor_dimension dimension[N_DIM];
1301 struct dtype_type
1303 size_t elem_len;
1304 int version;
1305 signed char rank;
1306 signed char type;
1307 signed short attribute;
1310 struct descriptor_dimension
1312 index stride;
1313 index lbound;
1314 index ubound;
1317 Translation code should use gfc_conv_descriptor_* rather than
1318 accessing the descriptor directly. Any changes to the array
1319 descriptor type will require changes in gfc_conv_descriptor_* and
1320 gfc_build_array_initializer.
1322 This is represented internally as a RECORD_TYPE. The index nodes
1323 are gfc_array_index_type and the data node is a pointer to the
1324 data. See below for the handling of character types.
1326 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1327 this generated poor code for assumed/deferred size arrays. These
1328 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1329 of the GENERIC grammar. Also, there is no way to explicitly set
1330 the array stride, so all data must be packed(1). I've tried to
1331 mark all the functions which would require modification with a GCC
1332 ARRAYS comment.
1334 The data component points to the first element in the array. The
1335 offset field is the position of the origin of the array (i.e. element
1336 (0, 0 ...)). This may be outside the bounds of the array.
1338 An element is accessed by
1339 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1340 This gives good performance as the computation does not involve the
1341 bounds of the array. For packed arrays, this is optimized further
1342 by substituting the known strides.
1344 This system has one problem: all array bounds must be within 2^31
1345 elements of the origin (2^63 on 64-bit machines). For example
1346 integer, dimension (80000:90000, 80000:90000, 2) :: array
1347 may not work properly on 32-bit machines because 80000*80000 >
1348 2^31, so the calculation for stride2 would overflow. This may
1349 still work, but I haven't checked, and it relies on the overflow
1350 doing the right thing.
1352 The way to fix this problem is to access elements as follows:
1353 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1354 Obviously this is much slower. I will make this a compile time
1355 option, something like -fsmall-array-offsets. Mixing code compiled
1356 with and without this switch will work.
1358 (1) This can be worked around by modifying the upper bound of the
1359 previous dimension. This requires extra fields in the descriptor
1360 (both real_ubound and fake_ubound). */
1363 /* Returns true if the array sym does not require a descriptor. */
1366 gfc_is_nodesc_array (gfc_symbol * sym)
1368 symbol_attribute *array_attr;
1369 gfc_array_spec *as;
1370 bool is_classarray = IS_CLASS_ARRAY (sym);
1372 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1373 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1375 gcc_assert (array_attr->dimension || array_attr->codimension);
1377 /* We only want local arrays. */
1378 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1379 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1380 || array_attr->allocatable)
1381 return 0;
1383 /* We want a descriptor for associate-name arrays that do not have an
1384 explicitly known shape already. */
1385 if (sym->assoc && as->type != AS_EXPLICIT)
1386 return 0;
1388 /* The dummy is stored in sym and not in the component. */
1389 if (sym->attr.dummy)
1390 return as->type != AS_ASSUMED_SHAPE
1391 && as->type != AS_ASSUMED_RANK;
1393 if (sym->attr.result || sym->attr.function)
1394 return 0;
1396 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1398 return 1;
1402 /* Create an array descriptor type. */
1404 static tree
1405 gfc_build_array_type (tree type, gfc_array_spec * as,
1406 enum gfc_array_kind akind, bool restricted,
1407 bool contiguous, int codim)
1409 tree lbound[GFC_MAX_DIMENSIONS];
1410 tree ubound[GFC_MAX_DIMENSIONS];
1411 int n, corank;
1413 /* Assumed-shape arrays do not have codimension information stored in the
1414 descriptor. */
1415 corank = MAX (as->corank, codim);
1416 if (as->type == AS_ASSUMED_SHAPE ||
1417 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1418 corank = codim;
1420 if (as->type == AS_ASSUMED_RANK)
1421 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1423 lbound[n] = NULL_TREE;
1424 ubound[n] = NULL_TREE;
1427 for (n = 0; n < as->rank; n++)
1429 /* Create expressions for the known bounds of the array. */
1430 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1431 lbound[n] = gfc_index_one_node;
1432 else
1433 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1434 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1437 for (n = as->rank; n < as->rank + corank; n++)
1439 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1440 lbound[n] = gfc_index_one_node;
1441 else
1442 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1444 if (n < as->rank + corank - 1)
1445 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1448 if (as->type == AS_ASSUMED_SHAPE)
1449 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1450 : GFC_ARRAY_ASSUMED_SHAPE;
1451 else if (as->type == AS_ASSUMED_RANK)
1452 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1453 : GFC_ARRAY_ASSUMED_RANK;
1454 return gfc_get_array_type_bounds (type, as->rank == -1
1455 ? GFC_MAX_DIMENSIONS : as->rank,
1456 corank, lbound, ubound, 0, akind,
1457 restricted);
1460 /* Returns the struct descriptor_dimension type. */
1462 static tree
1463 gfc_get_desc_dim_type (void)
1465 tree type;
1466 tree decl, *chain = NULL;
1468 if (gfc_desc_dim_type)
1469 return gfc_desc_dim_type;
1471 /* Build the type node. */
1472 type = make_node (RECORD_TYPE);
1474 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1475 TYPE_PACKED (type) = 1;
1477 /* Consists of the stride, lbound and ubound members. */
1478 decl = gfc_add_field_to_struct_1 (type,
1479 get_identifier ("stride"),
1480 gfc_array_index_type, &chain);
1481 suppress_warning (decl);
1483 decl = gfc_add_field_to_struct_1 (type,
1484 get_identifier ("lbound"),
1485 gfc_array_index_type, &chain);
1486 suppress_warning (decl);
1488 decl = gfc_add_field_to_struct_1 (type,
1489 get_identifier ("ubound"),
1490 gfc_array_index_type, &chain);
1491 suppress_warning (decl);
1493 /* Finish off the type. */
1494 gfc_finish_type (type);
1495 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1497 gfc_desc_dim_type = type;
1498 return type;
1502 /* Return the DTYPE for an array. This describes the type and type parameters
1503 of the array. */
1504 /* TODO: Only call this when the value is actually used, and make all the
1505 unknown cases abort. */
1507 tree
1508 gfc_get_dtype_rank_type (int rank, tree etype)
1510 tree ptype;
1511 tree size;
1512 int n;
1513 tree tmp;
1514 tree dtype;
1515 tree field;
1516 vec<constructor_elt, va_gc> *v = NULL;
1518 ptype = etype;
1519 while (TREE_CODE (etype) == POINTER_TYPE
1520 || TREE_CODE (etype) == ARRAY_TYPE)
1522 ptype = etype;
1523 etype = TREE_TYPE (etype);
1526 gcc_assert (etype);
1528 switch (TREE_CODE (etype))
1530 case INTEGER_TYPE:
1531 if (TREE_CODE (ptype) == ARRAY_TYPE
1532 && TYPE_STRING_FLAG (ptype))
1533 n = BT_CHARACTER;
1534 else
1535 n = BT_INTEGER;
1536 break;
1538 case BOOLEAN_TYPE:
1539 n = BT_LOGICAL;
1540 break;
1542 case REAL_TYPE:
1543 n = BT_REAL;
1544 break;
1546 case COMPLEX_TYPE:
1547 n = BT_COMPLEX;
1548 break;
1550 case RECORD_TYPE:
1551 if (GFC_CLASS_TYPE_P (etype))
1552 n = BT_CLASS;
1553 else
1554 n = BT_DERIVED;
1555 break;
1557 case FUNCTION_TYPE:
1558 case VOID_TYPE:
1559 n = BT_VOID;
1560 break;
1562 default:
1563 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1564 /* We can encounter strange array types for temporary arrays. */
1565 gcc_unreachable ();
1568 switch (n)
1570 case BT_CHARACTER:
1571 gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1572 size = gfc_get_character_len_in_bytes (ptype);
1573 break;
1574 case BT_VOID:
1575 gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1576 size = size_in_bytes (ptype);
1577 break;
1578 default:
1579 size = size_in_bytes (etype);
1580 break;
1583 gcc_assert (size);
1585 STRIP_NOPS (size);
1586 size = fold_convert (size_type_node, size);
1587 tmp = get_dtype_type_node ();
1588 field = gfc_advance_chain (TYPE_FIELDS (tmp),
1589 GFC_DTYPE_ELEM_LEN);
1590 CONSTRUCTOR_APPEND_ELT (v, field,
1591 fold_convert (TREE_TYPE (field), size));
1593 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1594 GFC_DTYPE_RANK);
1595 if (rank >= 0)
1596 CONSTRUCTOR_APPEND_ELT (v, field,
1597 build_int_cst (TREE_TYPE (field), rank));
1599 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1600 GFC_DTYPE_TYPE);
1601 CONSTRUCTOR_APPEND_ELT (v, field,
1602 build_int_cst (TREE_TYPE (field), n));
1604 dtype = build_constructor (tmp, v);
1606 return dtype;
1610 tree
1611 gfc_get_dtype (tree type, int * rank)
1613 tree dtype;
1614 tree etype;
1615 int irnk;
1617 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1619 irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
1620 etype = gfc_get_element_type (type);
1621 dtype = gfc_get_dtype_rank_type (irnk, etype);
1623 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1624 return dtype;
1628 /* Build an array type for use without a descriptor, packed according
1629 to the value of PACKED. */
1631 tree
1632 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1633 bool restricted)
1635 tree range;
1636 tree type;
1637 tree tmp;
1638 int n;
1639 int known_stride;
1640 int known_offset;
1641 mpz_t offset;
1642 mpz_t stride;
1643 mpz_t delta;
1644 gfc_expr *expr;
1646 mpz_init_set_ui (offset, 0);
1647 mpz_init_set_ui (stride, 1);
1648 mpz_init (delta);
1650 /* We don't use build_array_type because this does not include
1651 lang-specific information (i.e. the bounds of the array) when checking
1652 for duplicates. */
1653 if (as->rank)
1654 type = make_node (ARRAY_TYPE);
1655 else
1656 type = build_variant_type_copy (etype);
1658 GFC_ARRAY_TYPE_P (type) = 1;
1659 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1661 known_stride = (packed != PACKED_NO);
1662 known_offset = 1;
1663 for (n = 0; n < as->rank; n++)
1665 /* Fill in the stride and bound components of the type. */
1666 if (known_stride)
1667 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1668 else
1669 tmp = NULL_TREE;
1670 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1672 expr = as->lower[n];
1673 if (expr && expr->expr_type == EXPR_CONSTANT)
1675 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1676 gfc_index_integer_kind);
1678 else
1680 known_stride = 0;
1681 tmp = NULL_TREE;
1683 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1685 if (known_stride)
1687 /* Calculate the offset. */
1688 mpz_mul (delta, stride, as->lower[n]->value.integer);
1689 mpz_sub (offset, offset, delta);
1691 else
1692 known_offset = 0;
1694 expr = as->upper[n];
1695 if (expr && expr->expr_type == EXPR_CONSTANT)
1697 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1698 gfc_index_integer_kind);
1700 else
1702 tmp = NULL_TREE;
1703 known_stride = 0;
1705 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1707 if (known_stride)
1709 /* Calculate the stride. */
1710 mpz_sub (delta, as->upper[n]->value.integer,
1711 as->lower[n]->value.integer);
1712 mpz_add_ui (delta, delta, 1);
1713 mpz_mul (stride, stride, delta);
1716 /* Only the first stride is known for partial packed arrays. */
1717 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1718 known_stride = 0;
1720 for (n = as->rank; n < as->rank + as->corank; n++)
1722 expr = as->lower[n];
1723 if (expr && expr->expr_type == EXPR_CONSTANT)
1724 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1725 gfc_index_integer_kind);
1726 else
1727 tmp = NULL_TREE;
1728 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1730 expr = as->upper[n];
1731 if (expr && expr->expr_type == EXPR_CONSTANT)
1732 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1733 gfc_index_integer_kind);
1734 else
1735 tmp = NULL_TREE;
1736 if (n < as->rank + as->corank - 1)
1737 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1740 if (known_offset)
1742 GFC_TYPE_ARRAY_OFFSET (type) =
1743 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1745 else
1746 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1748 if (known_stride)
1750 GFC_TYPE_ARRAY_SIZE (type) =
1751 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1753 else
1754 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1756 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1757 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1758 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1759 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1760 NULL_TREE);
1761 /* TODO: use main type if it is unbounded. */
1762 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1763 build_pointer_type (build_array_type (etype, range));
1764 if (restricted)
1765 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1766 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1767 TYPE_QUAL_RESTRICT);
1769 if (as->rank == 0)
1771 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1773 type = build_pointer_type (type);
1775 if (restricted)
1776 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1778 GFC_ARRAY_TYPE_P (type) = 1;
1779 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1782 return type;
1785 if (known_stride)
1787 mpz_sub_ui (stride, stride, 1);
1788 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1790 else
1791 range = NULL_TREE;
1793 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1794 TYPE_DOMAIN (type) = range;
1796 build_pointer_type (etype);
1797 TREE_TYPE (type) = etype;
1799 layout_type (type);
1801 mpz_clear (offset);
1802 mpz_clear (stride);
1803 mpz_clear (delta);
1805 /* Represent packed arrays as multi-dimensional if they have rank >
1806 1 and with proper bounds, instead of flat arrays. This makes for
1807 better debug info. */
1808 if (known_offset)
1810 tree gtype = etype, rtype, type_decl;
1812 for (n = as->rank - 1; n >= 0; n--)
1814 rtype = build_range_type (gfc_array_index_type,
1815 GFC_TYPE_ARRAY_LBOUND (type, n),
1816 GFC_TYPE_ARRAY_UBOUND (type, n));
1817 gtype = build_array_type (gtype, rtype);
1819 TYPE_NAME (type) = type_decl = build_decl (input_location,
1820 TYPE_DECL, NULL, gtype);
1821 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1824 if (packed != PACKED_STATIC || !known_stride
1825 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1827 /* For dummy arrays and automatic (heap allocated) arrays we
1828 want a pointer to the array. */
1829 type = build_pointer_type (type);
1830 if (restricted)
1831 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1832 GFC_ARRAY_TYPE_P (type) = 1;
1833 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1835 return type;
1839 /* Return or create the base type for an array descriptor. */
1841 static tree
1842 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1844 tree fat_type, decl, arraytype, *chain = NULL;
1845 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1846 int idx;
1848 /* Assumed-rank array. */
1849 if (dimen == -1)
1850 dimen = GFC_MAX_DIMENSIONS;
1852 idx = 2 * (codimen + dimen) + restricted;
1854 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1856 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1858 if (gfc_array_descriptor_base_caf[idx])
1859 return gfc_array_descriptor_base_caf[idx];
1861 else if (gfc_array_descriptor_base[idx])
1862 return gfc_array_descriptor_base[idx];
1864 /* Build the type node. */
1865 fat_type = make_node (RECORD_TYPE);
1867 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1868 TYPE_NAME (fat_type) = get_identifier (name);
1869 TYPE_NAMELESS (fat_type) = 1;
1871 /* Add the data member as the first element of the descriptor. */
1872 gfc_add_field_to_struct_1 (fat_type,
1873 get_identifier ("data"),
1874 (restricted
1875 ? prvoid_type_node
1876 : ptr_type_node), &chain);
1878 /* Add the base component. */
1879 decl = gfc_add_field_to_struct_1 (fat_type,
1880 get_identifier ("offset"),
1881 gfc_array_index_type, &chain);
1882 suppress_warning (decl);
1884 /* Add the dtype component. */
1885 decl = gfc_add_field_to_struct_1 (fat_type,
1886 get_identifier ("dtype"),
1887 get_dtype_type_node (), &chain);
1888 suppress_warning (decl);
1890 /* Add the span component. */
1891 decl = gfc_add_field_to_struct_1 (fat_type,
1892 get_identifier ("span"),
1893 gfc_array_index_type, &chain);
1894 suppress_warning (decl);
1896 /* Build the array type for the stride and bound components. */
1897 if (dimen + codimen > 0)
1899 arraytype =
1900 build_array_type (gfc_get_desc_dim_type (),
1901 build_range_type (gfc_array_index_type,
1902 gfc_index_zero_node,
1903 gfc_rank_cst[codimen + dimen - 1]));
1905 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1906 arraytype, &chain);
1907 suppress_warning (decl);
1910 if (flag_coarray == GFC_FCOARRAY_LIB)
1912 decl = gfc_add_field_to_struct_1 (fat_type,
1913 get_identifier ("token"),
1914 prvoid_type_node, &chain);
1915 suppress_warning (decl);
1918 /* Finish off the type. */
1919 gfc_finish_type (fat_type);
1920 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1922 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1923 gfc_array_descriptor_base_caf[idx] = fat_type;
1924 else
1925 gfc_array_descriptor_base[idx] = fat_type;
1927 return fat_type;
1931 /* Build an array (descriptor) type with given bounds. */
1933 tree
1934 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1935 tree * ubound, int packed,
1936 enum gfc_array_kind akind, bool restricted)
1938 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1939 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1940 const char *type_name;
1941 int n;
1943 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1944 fat_type = build_distinct_type_copy (base_type);
1945 /* Unshare TYPE_FIELDs. */
1946 for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
1948 tree next = DECL_CHAIN (*tp);
1949 *tp = copy_node (*tp);
1950 DECL_CONTEXT (*tp) = fat_type;
1951 DECL_CHAIN (*tp) = next;
1953 /* Make sure that nontarget and target array type have the same canonical
1954 type (and same stub decl for debug info). */
1955 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1956 TYPE_CANONICAL (fat_type) = base_type;
1957 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1958 /* Arrays of unknown type must alias with all array descriptors. */
1959 TYPE_TYPELESS_STORAGE (base_type) = 1;
1960 TYPE_TYPELESS_STORAGE (fat_type) = 1;
1961 gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
1963 tmp = etype;
1964 if (TREE_CODE (tmp) == ARRAY_TYPE
1965 && TYPE_STRING_FLAG (tmp))
1966 tmp = TREE_TYPE (etype);
1967 tmp = TYPE_NAME (tmp);
1968 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1969 tmp = DECL_NAME (tmp);
1970 if (tmp)
1971 type_name = IDENTIFIER_POINTER (tmp);
1972 else
1973 type_name = "unknown";
1974 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1975 GFC_MAX_SYMBOL_LEN, type_name);
1976 TYPE_NAME (fat_type) = get_identifier (name);
1977 TYPE_NAMELESS (fat_type) = 1;
1979 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1980 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1982 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1983 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1984 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1985 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1987 /* Build an array descriptor record type. */
1988 if (packed != 0)
1989 stride = gfc_index_one_node;
1990 else
1991 stride = NULL_TREE;
1992 for (n = 0; n < dimen + codimen; n++)
1994 if (n < dimen)
1995 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1997 if (lbound)
1998 lower = lbound[n];
1999 else
2000 lower = NULL_TREE;
2002 if (lower != NULL_TREE)
2004 if (INTEGER_CST_P (lower))
2005 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
2006 else
2007 lower = NULL_TREE;
2010 if (codimen && n == dimen + codimen - 1)
2011 break;
2013 upper = ubound[n];
2014 if (upper != NULL_TREE)
2016 if (INTEGER_CST_P (upper))
2017 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
2018 else
2019 upper = NULL_TREE;
2022 if (n >= dimen)
2023 continue;
2025 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
2027 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2028 gfc_array_index_type, upper, lower);
2029 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2030 gfc_array_index_type, tmp,
2031 gfc_index_one_node);
2032 stride = fold_build2_loc (input_location, MULT_EXPR,
2033 gfc_array_index_type, tmp, stride);
2034 /* Check the folding worked. */
2035 gcc_assert (INTEGER_CST_P (stride));
2037 else
2038 stride = NULL_TREE;
2040 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
2042 /* TODO: known offsets for descriptors. */
2043 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2045 if (dimen == 0)
2047 arraytype = build_pointer_type (etype);
2048 if (restricted)
2049 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2051 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2052 return fat_type;
2055 /* We define data as an array with the correct size if possible.
2056 Much better than doing pointer arithmetic. */
2057 if (stride)
2058 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2059 int_const_binop (MINUS_EXPR, stride,
2060 build_int_cst (TREE_TYPE (stride), 1)));
2061 else
2062 rtype = gfc_array_range_type;
2063 arraytype = build_array_type (etype, rtype);
2064 arraytype = build_pointer_type (arraytype);
2065 if (restricted)
2066 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2067 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2069 /* This will generate the base declarations we need to emit debug
2070 information for this type. FIXME: there must be a better way to
2071 avoid divergence between compilations with and without debug
2072 information. */
2074 struct array_descr_info info;
2075 gfc_get_array_descr_info (fat_type, &info);
2076 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2079 return fat_type;
2082 /* Build a pointer type. This function is called from gfc_sym_type(). */
2084 static tree
2085 gfc_build_pointer_type (gfc_symbol * sym, tree type)
2087 /* Array pointer types aren't actually pointers. */
2088 if (sym->attr.dimension)
2089 return type;
2090 else
2091 return build_pointer_type (type);
2094 static tree gfc_nonrestricted_type (tree t);
2095 /* Given two record or union type nodes TO and FROM, ensure
2096 that all fields in FROM have a corresponding field in TO,
2097 their type being nonrestrict variants. This accepts a TO
2098 node that already has a prefix of the fields in FROM. */
2099 static void
2100 mirror_fields (tree to, tree from)
2102 tree fto, ffrom;
2103 tree *chain;
2105 /* Forward to the end of TOs fields. */
2106 fto = TYPE_FIELDS (to);
2107 ffrom = TYPE_FIELDS (from);
2108 chain = &TYPE_FIELDS (to);
2109 while (fto)
2111 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2112 chain = &DECL_CHAIN (fto);
2113 fto = DECL_CHAIN (fto);
2114 ffrom = DECL_CHAIN (ffrom);
2117 /* Now add all fields remaining in FROM (starting with ffrom). */
2118 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2120 tree newfield = copy_node (ffrom);
2121 DECL_CONTEXT (newfield) = to;
2122 /* The store to DECL_CHAIN might seem redundant with the
2123 stores to *chain, but not clearing it here would mean
2124 leaving a chain into the old fields. If ever
2125 our called functions would look at them confusion
2126 will arise. */
2127 DECL_CHAIN (newfield) = NULL_TREE;
2128 *chain = newfield;
2129 chain = &DECL_CHAIN (newfield);
2131 if (TREE_CODE (ffrom) == FIELD_DECL)
2133 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2134 TREE_TYPE (newfield) = elemtype;
2137 *chain = NULL_TREE;
2140 /* Given a type T, returns a different type of the same structure,
2141 except that all types it refers to (recursively) are always
2142 non-restrict qualified types. */
2143 static tree
2144 gfc_nonrestricted_type (tree t)
2146 tree ret = t;
2148 /* If the type isn't laid out yet, don't copy it. If something
2149 needs it for real it should wait until the type got finished. */
2150 if (!TYPE_SIZE (t))
2151 return t;
2153 if (!TYPE_LANG_SPECIFIC (t))
2154 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2155 /* If we're dealing with this very node already further up
2156 the call chain (recursion via pointers and struct members)
2157 we haven't yet determined if we really need a new type node.
2158 Assume we don't, return T itself. */
2159 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2160 return t;
2162 /* If we have calculated this all already, just return it. */
2163 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2164 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2166 /* Mark this type. */
2167 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2169 switch (TREE_CODE (t))
2171 default:
2172 break;
2174 case POINTER_TYPE:
2175 case REFERENCE_TYPE:
2177 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2178 if (totype == TREE_TYPE (t))
2179 ret = t;
2180 else if (TREE_CODE (t) == POINTER_TYPE)
2181 ret = build_pointer_type (totype);
2182 else
2183 ret = build_reference_type (totype);
2184 ret = build_qualified_type (ret,
2185 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2187 break;
2189 case ARRAY_TYPE:
2191 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2192 if (elemtype == TREE_TYPE (t))
2193 ret = t;
2194 else
2196 ret = build_variant_type_copy (t);
2197 TREE_TYPE (ret) = elemtype;
2198 if (TYPE_LANG_SPECIFIC (t)
2199 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2201 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2202 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2203 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2205 TYPE_LANG_SPECIFIC (ret)
2206 = ggc_cleared_alloc<struct lang_type> ();
2207 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2208 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2213 break;
2215 case RECORD_TYPE:
2216 case UNION_TYPE:
2217 case QUAL_UNION_TYPE:
2219 tree field;
2220 /* First determine if we need a new type at all.
2221 Careful, the two calls to gfc_nonrestricted_type per field
2222 might return different values. That happens exactly when
2223 one of the fields reaches back to this very record type
2224 (via pointers). The first calls will assume that we don't
2225 need to copy T (see the error_mark_node marking). If there
2226 are any reasons for copying T apart from having to copy T,
2227 we'll indeed copy it, and the second calls to
2228 gfc_nonrestricted_type will use that new node if they
2229 reach back to T. */
2230 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2231 if (TREE_CODE (field) == FIELD_DECL)
2233 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2234 if (elemtype != TREE_TYPE (field))
2235 break;
2237 if (!field)
2238 break;
2239 ret = build_variant_type_copy (t);
2240 TYPE_FIELDS (ret) = NULL_TREE;
2242 /* Here we make sure that as soon as we know we have to copy
2243 T, that also fields reaching back to us will use the new
2244 copy. It's okay if that copy still contains the old fields,
2245 we won't look at them. */
2246 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2247 mirror_fields (ret, t);
2249 break;
2252 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2253 return ret;
2257 /* Return the type for a symbol. Special handling is required for character
2258 types to get the correct level of indirection.
2259 For functions return the return type.
2260 For subroutines return void_type_node.
2261 Calling this multiple times for the same symbol should be avoided,
2262 especially for character and array types. */
2264 tree
2265 gfc_sym_type (gfc_symbol * sym, bool is_bind_c)
2267 tree type;
2268 int byref;
2269 bool restricted;
2271 /* Procedure Pointers inside COMMON blocks. */
2272 if (sym->attr.proc_pointer && sym->attr.in_common)
2274 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2275 sym->attr.proc_pointer = 0;
2276 type = build_pointer_type (gfc_get_function_type (sym));
2277 sym->attr.proc_pointer = 1;
2278 return type;
2281 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2282 return void_type_node;
2284 /* In the case of a function the fake result variable may have a
2285 type different from the function type, so don't return early in
2286 that case. */
2287 if (sym->backend_decl && !sym->attr.function)
2288 return TREE_TYPE (sym->backend_decl);
2290 if (sym->attr.result
2291 && sym->ts.type == BT_CHARACTER
2292 && sym->ts.u.cl->backend_decl == NULL_TREE
2293 && sym->ns->proc_name
2294 && sym->ns->proc_name->ts.u.cl
2295 && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2296 sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2298 if (sym->ts.type == BT_CHARACTER
2299 && ((sym->attr.function && sym->attr.is_bind_c)
2300 || ((sym->attr.result || sym->attr.value)
2301 && sym->ns->proc_name
2302 && sym->ns->proc_name->attr.is_bind_c)
2303 || (sym->ts.deferred && (!sym->ts.u.cl
2304 || !sym->ts.u.cl->backend_decl))))
2305 type = gfc_character1_type_node;
2306 else
2307 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2309 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
2310 && !sym->pass_as_value)
2311 byref = 1;
2312 else
2313 byref = 0;
2315 restricted = !sym->attr.target && !sym->attr.pointer
2316 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2317 if (!restricted)
2318 type = gfc_nonrestricted_type (type);
2320 /* Dummy argument to a bind(C) procedure. */
2321 if (is_bind_c && is_CFI_desc (sym, NULL))
2322 type = gfc_get_cfi_type (sym->attr.dimension ? sym->as->rank : 0,
2323 /* restricted = */ false);
2324 else if (sym->attr.dimension || sym->attr.codimension)
2326 if (gfc_is_nodesc_array (sym))
2328 /* If this is a character argument of unknown length, just use the
2329 base type. */
2330 if (sym->ts.type != BT_CHARACTER
2331 || !(sym->attr.dummy || sym->attr.function)
2332 || sym->ts.u.cl->backend_decl)
2334 type = gfc_get_nodesc_array_type (type, sym->as,
2335 byref ? PACKED_FULL
2336 : PACKED_STATIC,
2337 restricted);
2338 byref = 0;
2341 else
2343 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2344 if (sym->attr.pointer)
2345 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2346 : GFC_ARRAY_POINTER;
2347 else if (sym->attr.allocatable)
2348 akind = GFC_ARRAY_ALLOCATABLE;
2349 type = gfc_build_array_type (type, sym->as, akind, restricted,
2350 sym->attr.contiguous, false);
2353 else
2355 if (sym->attr.allocatable || sym->attr.pointer
2356 || gfc_is_associate_pointer (sym))
2357 type = gfc_build_pointer_type (sym, type);
2360 /* We currently pass all parameters by reference.
2361 See f95_get_function_decl. For dummy function parameters return the
2362 function type. */
2363 if (byref)
2365 /* We must use pointer types for potentially absent variables. The
2366 optimizers assume a reference type argument is never NULL. */
2367 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2368 || sym->attr.optional
2369 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2370 type = build_pointer_type (type);
2371 else
2373 type = build_reference_type (type);
2374 if (restricted)
2375 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2379 return (type);
2382 /* Layout and output debug info for a record type. */
2384 void
2385 gfc_finish_type (tree type)
2387 tree decl;
2389 decl = build_decl (input_location,
2390 TYPE_DECL, NULL_TREE, type);
2391 TYPE_STUB_DECL (type) = decl;
2392 layout_type (type);
2393 rest_of_type_compilation (type, 1);
2394 rest_of_decl_compilation (decl, 1, 0);
2397 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2398 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2399 to the end of the field list pointed to by *CHAIN.
2401 Returns a pointer to the new field. */
2403 static tree
2404 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2406 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2408 DECL_CONTEXT (decl) = context;
2409 DECL_CHAIN (decl) = NULL_TREE;
2410 if (TYPE_FIELDS (context) == NULL_TREE)
2411 TYPE_FIELDS (context) = decl;
2412 if (chain != NULL)
2414 if (*chain != NULL)
2415 **chain = decl;
2416 *chain = &DECL_CHAIN (decl);
2419 return decl;
2422 /* Like `gfc_add_field_to_struct_1', but adds alignment
2423 information. */
2425 tree
2426 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2428 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2430 DECL_INITIAL (decl) = 0;
2431 SET_DECL_ALIGN (decl, 0);
2432 DECL_USER_ALIGN (decl) = 0;
2434 return decl;
2438 /* Copy the backend_decl and component backend_decls if
2439 the two derived type symbols are "equal", as described
2440 in 4.4.2 and resolved by gfc_compare_derived_types. */
2443 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2444 bool from_gsym)
2446 gfc_component *to_cm;
2447 gfc_component *from_cm;
2449 if (from == to)
2450 return 1;
2452 if (from->backend_decl == NULL
2453 || !gfc_compare_derived_types (from, to))
2454 return 0;
2456 to->backend_decl = from->backend_decl;
2458 to_cm = to->components;
2459 from_cm = from->components;
2461 /* Copy the component declarations. If a component is itself
2462 a derived type, we need a copy of its component declarations.
2463 This is done by recursing into gfc_get_derived_type and
2464 ensures that the component's component declarations have
2465 been built. If it is a character, we need the character
2466 length, as well. */
2467 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2469 to_cm->backend_decl = from_cm->backend_decl;
2470 to_cm->caf_token = from_cm->caf_token;
2471 if (from_cm->ts.type == BT_UNION)
2472 gfc_get_union_type (to_cm->ts.u.derived);
2473 else if (from_cm->ts.type == BT_DERIVED
2474 && (!from_cm->attr.pointer || from_gsym))
2475 gfc_get_derived_type (to_cm->ts.u.derived);
2476 else if (from_cm->ts.type == BT_CLASS
2477 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2478 gfc_get_derived_type (to_cm->ts.u.derived);
2479 else if (from_cm->ts.type == BT_CHARACTER)
2480 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2483 return 1;
2487 /* Build a tree node for a procedure pointer component. */
2489 static tree
2490 gfc_get_ppc_type (gfc_component* c)
2492 tree t;
2494 /* Explicit interface. */
2495 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2496 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2498 /* Implicit interface (only return value may be known). */
2499 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2500 t = gfc_typenode_for_spec (&c->ts);
2501 else
2502 t = void_type_node;
2504 /* FIXME: it would be better to provide explicit interfaces in all
2505 cases, since they should be known by the compiler. */
2506 return build_pointer_type (build_function_type (t, NULL_TREE));
2510 /* Build a tree node for a union type. Requires building each map
2511 structure which is an element of the union. */
2513 tree
2514 gfc_get_union_type (gfc_symbol *un)
2516 gfc_component *map = NULL;
2517 tree typenode = NULL, map_type = NULL, map_field = NULL;
2518 tree *chain = NULL;
2520 if (un->backend_decl)
2522 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2523 return un->backend_decl;
2524 else
2525 typenode = un->backend_decl;
2527 else
2529 typenode = make_node (UNION_TYPE);
2530 TYPE_NAME (typenode) = get_identifier (un->name);
2533 /* Add each contained MAP as a field. */
2534 for (map = un->components; map; map = map->next)
2536 gcc_assert (map->ts.type == BT_DERIVED);
2538 /* The map's type node, which is defined within this union's context. */
2539 map_type = gfc_get_derived_type (map->ts.u.derived);
2540 TYPE_CONTEXT (map_type) = typenode;
2542 /* The map field's declaration. */
2543 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2544 map_type, &chain);
2545 if (map->loc.lb)
2546 gfc_set_decl_location (map_field, &map->loc);
2547 else if (un->declared_at.lb)
2548 gfc_set_decl_location (map_field, &un->declared_at);
2550 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2551 DECL_NAMELESS(map_field) = true;
2553 /* We should never clobber another backend declaration for this map,
2554 because each map component is unique. */
2555 if (!map->backend_decl)
2556 map->backend_decl = map_field;
2559 un->backend_decl = typenode;
2560 gfc_finish_type (typenode);
2562 return typenode;
2566 /* Build a tree node for a derived type. If there are equal
2567 derived types, with different local names, these are built
2568 at the same time. If an equal derived type has been built
2569 in a parent namespace, this is used. */
2571 tree
2572 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2574 tree typenode = NULL, field = NULL, field_type = NULL;
2575 tree canonical = NULL_TREE;
2576 tree *chain = NULL;
2577 bool got_canonical = false;
2578 bool unlimited_entity = false;
2579 gfc_component *c;
2580 gfc_namespace *ns;
2581 tree tmp;
2582 bool coarray_flag;
2584 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2585 && derived->module && !derived->attr.vtype;
2587 gcc_assert (!derived->attr.pdt_template);
2589 if (derived->attr.unlimited_polymorphic
2590 || (flag_coarray == GFC_FCOARRAY_LIB
2591 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2592 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2593 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2594 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2595 return ptr_type_node;
2597 if (flag_coarray != GFC_FCOARRAY_LIB
2598 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2599 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2600 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2601 return gfc_get_int_type (gfc_default_integer_kind);
2603 if (derived && derived->attr.flavor == FL_PROCEDURE
2604 && derived->attr.generic)
2605 derived = gfc_find_dt_in_generic (derived);
2607 /* See if it's one of the iso_c_binding derived types. */
2608 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2610 if (derived->backend_decl)
2611 return derived->backend_decl;
2613 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2614 derived->backend_decl = ptr_type_node;
2615 else
2616 derived->backend_decl = pfunc_type_node;
2618 derived->ts.kind = gfc_index_integer_kind;
2619 derived->ts.type = BT_INTEGER;
2620 /* Set the f90_type to BT_VOID as a way to recognize something of type
2621 BT_INTEGER that needs to fit a void * for the purpose of the
2622 iso_c_binding derived types. */
2623 derived->ts.f90_type = BT_VOID;
2625 return derived->backend_decl;
2628 /* If use associated, use the module type for this one. */
2629 if (derived->backend_decl == NULL
2630 && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2631 && derived->module
2632 && gfc_get_module_backend_decl (derived))
2633 goto copy_derived_types;
2635 /* The derived types from an earlier namespace can be used as the
2636 canonical type. */
2637 if (derived->backend_decl == NULL
2638 && !derived->attr.use_assoc
2639 && !derived->attr.used_in_submodule
2640 && gfc_global_ns_list)
2642 for (ns = gfc_global_ns_list;
2643 ns->translated && !got_canonical;
2644 ns = ns->sibling)
2646 if (ns->derived_types)
2648 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2649 dt = dt->dt_next)
2651 gfc_copy_dt_decls_ifequal (dt, derived, true);
2652 if (derived->backend_decl)
2653 got_canonical = true;
2654 if (dt->dt_next == ns->derived_types)
2655 break;
2661 /* Store up the canonical type to be added to this one. */
2662 if (got_canonical)
2664 if (TYPE_CANONICAL (derived->backend_decl))
2665 canonical = TYPE_CANONICAL (derived->backend_decl);
2666 else
2667 canonical = derived->backend_decl;
2669 derived->backend_decl = NULL_TREE;
2672 /* derived->backend_decl != 0 means we saw it before, but its
2673 components' backend_decl may have not been built. */
2674 if (derived->backend_decl)
2676 /* Its components' backend_decl have been built or we are
2677 seeing recursion through the formal arglist of a procedure
2678 pointer component. */
2679 if (TYPE_FIELDS (derived->backend_decl))
2680 return derived->backend_decl;
2681 else if (derived->attr.abstract
2682 && derived->attr.proc_pointer_comp)
2684 /* If an abstract derived type with procedure pointer
2685 components has no other type of component, return the
2686 backend_decl. Otherwise build the components if any of the
2687 non-procedure pointer components have no backend_decl. */
2688 for (c = derived->components; c; c = c->next)
2690 bool same_alloc_type = c->attr.allocatable
2691 && derived == c->ts.u.derived;
2692 if (!c->attr.proc_pointer
2693 && !same_alloc_type
2694 && c->backend_decl == NULL)
2695 break;
2696 else if (c->next == NULL)
2697 return derived->backend_decl;
2699 typenode = derived->backend_decl;
2701 else
2702 typenode = derived->backend_decl;
2704 else
2706 /* We see this derived type first time, so build the type node. */
2707 typenode = make_node (RECORD_TYPE);
2708 TYPE_NAME (typenode) = get_identifier (derived->name);
2709 TYPE_PACKED (typenode) = flag_pack_derived;
2710 derived->backend_decl = typenode;
2713 if (derived->components
2714 && derived->components->ts.type == BT_DERIVED
2715 && strcmp (derived->components->name, "_data") == 0
2716 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2717 unlimited_entity = true;
2719 /* Go through the derived type components, building them as
2720 necessary. The reason for doing this now is that it is
2721 possible to recurse back to this derived type through a
2722 pointer component (PR24092). If this happens, the fields
2723 will be built and so we can return the type. */
2724 for (c = derived->components; c; c = c->next)
2726 bool same_alloc_type = c->attr.allocatable
2727 && derived == c->ts.u.derived;
2729 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2730 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2732 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2733 continue;
2735 if ((!c->attr.pointer && !c->attr.proc_pointer
2736 && !same_alloc_type)
2737 || c->ts.u.derived->backend_decl == NULL)
2739 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2740 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2741 local_codim);
2744 if (c->ts.u.derived->attr.is_iso_c)
2746 /* Need to copy the modified ts from the derived type. The
2747 typespec was modified because C_PTR/C_FUNPTR are translated
2748 into (void *) from derived types. */
2749 c->ts.type = c->ts.u.derived->ts.type;
2750 c->ts.kind = c->ts.u.derived->ts.kind;
2751 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2752 if (c->initializer)
2754 c->initializer->ts.type = c->ts.type;
2755 c->initializer->ts.kind = c->ts.kind;
2756 c->initializer->ts.f90_type = c->ts.f90_type;
2757 c->initializer->expr_type = EXPR_NULL;
2762 if (TYPE_FIELDS (derived->backend_decl))
2763 return derived->backend_decl;
2765 /* Build the type member list. Install the newly created RECORD_TYPE
2766 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2767 through only the top-level linked list of components so we correctly
2768 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2769 types are built as part of gfc_get_union_type. */
2770 for (c = derived->components; c; c = c->next)
2772 bool same_alloc_type = c->attr.allocatable
2773 && derived == c->ts.u.derived;
2774 /* Prevent infinite recursion, when the procedure pointer type is
2775 the same as derived, by forcing the procedure pointer component to
2776 be built as if the explicit interface does not exist. */
2777 if (c->attr.proc_pointer
2778 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2779 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2780 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2781 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2782 field_type = gfc_get_ppc_type (c);
2783 else if (c->attr.proc_pointer && derived->backend_decl)
2785 tmp = build_function_type (derived->backend_decl, NULL_TREE);
2786 field_type = build_pointer_type (tmp);
2788 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2789 field_type = c->ts.u.derived->backend_decl;
2790 else if (c->attr.caf_token)
2791 field_type = pvoid_type_node;
2792 else
2794 if (c->ts.type == BT_CHARACTER
2795 && !c->ts.deferred && !c->attr.pdt_string)
2797 /* Evaluate the string length. */
2798 gfc_conv_const_charlen (c->ts.u.cl);
2799 gcc_assert (c->ts.u.cl->backend_decl);
2801 else if (c->ts.type == BT_CHARACTER)
2802 c->ts.u.cl->backend_decl
2803 = build_int_cst (gfc_charlen_type_node, 0);
2805 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2808 /* This returns an array descriptor type. Initialization may be
2809 required. */
2810 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2812 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2814 enum gfc_array_kind akind;
2815 if (c->attr.pointer)
2816 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2817 : GFC_ARRAY_POINTER;
2818 else
2819 akind = GFC_ARRAY_ALLOCATABLE;
2820 /* Pointers to arrays aren't actually pointer types. The
2821 descriptors are separate, but the data is common. */
2822 field_type = gfc_build_array_type (field_type, c->as, akind,
2823 !c->attr.target
2824 && !c->attr.pointer,
2825 c->attr.contiguous,
2826 codimen);
2828 else
2829 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2830 PACKED_STATIC,
2831 !c->attr.target);
2833 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2834 && !c->attr.proc_pointer
2835 && !(unlimited_entity && c == derived->components))
2836 field_type = build_pointer_type (field_type);
2838 if (c->attr.pointer || same_alloc_type)
2839 field_type = gfc_nonrestricted_type (field_type);
2841 /* vtype fields can point to different types to the base type. */
2842 if (c->ts.type == BT_DERIVED
2843 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2844 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2845 ptr_mode, true);
2847 /* Ensure that the CLASS language specific flag is set. */
2848 if (c->ts.type == BT_CLASS)
2850 if (POINTER_TYPE_P (field_type))
2851 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2852 else
2853 GFC_CLASS_TYPE_P (field_type) = 1;
2856 field = gfc_add_field_to_struct (typenode,
2857 get_identifier (c->name),
2858 field_type, &chain);
2859 if (c->loc.lb)
2860 gfc_set_decl_location (field, &c->loc);
2861 else if (derived->declared_at.lb)
2862 gfc_set_decl_location (field, &derived->declared_at);
2864 gfc_finish_decl_attrs (field, &c->attr);
2866 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2868 gcc_assert (field);
2869 if (!c->backend_decl)
2870 c->backend_decl = field;
2872 if (c->attr.pointer && c->attr.dimension
2873 && !(c->ts.type == BT_DERIVED
2874 && strcmp (c->name, "_data") == 0))
2875 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2878 /* Now lay out the derived type, including the fields. */
2879 if (canonical)
2880 TYPE_CANONICAL (typenode) = canonical;
2882 gfc_finish_type (typenode);
2883 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2884 if (derived->module && derived->ns->proc_name
2885 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2887 if (derived->ns->proc_name->backend_decl
2888 && TREE_CODE (derived->ns->proc_name->backend_decl)
2889 == NAMESPACE_DECL)
2891 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2892 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2893 = derived->ns->proc_name->backend_decl;
2897 derived->backend_decl = typenode;
2899 copy_derived_types:
2901 for (c = derived->components; c; c = c->next)
2903 /* Do not add a caf_token field for class container components. */
2904 if ((codimen || coarray_flag)
2905 && !c->attr.dimension && !c->attr.codimension
2906 && (c->attr.allocatable || c->attr.pointer)
2907 && !derived->attr.is_class)
2909 /* Provide sufficient space to hold "_caf_symbol". */
2910 char caf_name[GFC_MAX_SYMBOL_LEN + 6];
2911 gfc_component *token;
2912 snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
2913 token = gfc_find_component (derived, caf_name, true, true, NULL);
2914 gcc_assert (token);
2915 c->caf_token = token->backend_decl;
2916 suppress_warning (c->caf_token);
2920 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
2922 gfc_copy_dt_decls_ifequal (derived, dt, false);
2923 if (dt->dt_next == gfc_derived_types)
2924 break;
2927 return derived->backend_decl;
2932 gfc_return_by_reference (gfc_symbol * sym)
2934 if (!sym->attr.function)
2935 return 0;
2937 if (sym->attr.dimension)
2938 return 1;
2940 if (sym->ts.type == BT_CHARACTER
2941 && !sym->attr.is_bind_c
2942 && (!sym->attr.result
2943 || !sym->ns->proc_name
2944 || !sym->ns->proc_name->attr.is_bind_c))
2945 return 1;
2947 /* Possibly return complex numbers by reference for g77 compatibility.
2948 We don't do this for calls to intrinsics (as the library uses the
2949 -fno-f2c calling convention), nor for calls to functions which always
2950 require an explicit interface, as no compatibility problems can
2951 arise there. */
2952 if (flag_f2c && sym->ts.type == BT_COMPLEX
2953 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2954 return 1;
2956 return 0;
2959 static tree
2960 gfc_get_mixed_entry_union (gfc_namespace *ns)
2962 tree type;
2963 tree *chain = NULL;
2964 char name[GFC_MAX_SYMBOL_LEN + 1];
2965 gfc_entry_list *el, *el2;
2967 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2968 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2970 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2972 /* Build the type node. */
2973 type = make_node (UNION_TYPE);
2975 TYPE_NAME (type) = get_identifier (name);
2977 for (el = ns->entries; el; el = el->next)
2979 /* Search for duplicates. */
2980 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2981 if (el2->sym->result == el->sym->result)
2982 break;
2984 if (el == el2)
2985 gfc_add_field_to_struct_1 (type,
2986 get_identifier (el->sym->result->name),
2987 gfc_sym_type (el->sym->result), &chain);
2990 /* Finish off the type. */
2991 gfc_finish_type (type);
2992 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2993 return type;
2996 /* Create a "fn spec" based on the formal arguments;
2997 cf. create_function_arglist. */
2999 static tree
3000 create_fn_spec (gfc_symbol *sym, tree fntype)
3002 char spec[150];
3003 size_t spec_len;
3004 gfc_formal_arglist *f;
3005 tree tmp;
3007 memset (&spec, 0, sizeof (spec));
3008 spec[0] = '.';
3009 spec[1] = ' ';
3010 spec_len = 2;
3012 if (sym->attr.entry_master)
3014 spec[spec_len++] = 'R';
3015 spec[spec_len++] = ' ';
3017 if (gfc_return_by_reference (sym))
3019 gfc_symbol *result = sym->result ? sym->result : sym;
3021 if (result->attr.pointer || sym->attr.proc_pointer)
3023 spec[spec_len++] = '.';
3024 spec[spec_len++] = ' ';
3026 else
3028 spec[spec_len++] = 'w';
3029 spec[spec_len++] = ' ';
3031 if (sym->ts.type == BT_CHARACTER)
3033 if (!sym->ts.u.cl->length
3034 && (sym->attr.allocatable || sym->attr.pointer))
3035 spec[spec_len++] = 'w';
3036 else
3037 spec[spec_len++] = 'R';
3038 spec[spec_len++] = ' ';
3042 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3043 if (spec_len < sizeof (spec))
3045 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
3046 || f->sym->attr.external || f->sym->attr.cray_pointer
3047 || (f->sym->ts.type == BT_DERIVED
3048 && (f->sym->ts.u.derived->attr.proc_pointer_comp
3049 || f->sym->ts.u.derived->attr.pointer_comp))
3050 || (f->sym->ts.type == BT_CLASS
3051 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3052 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3053 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3055 spec[spec_len++] = '.';
3056 spec[spec_len++] = ' ';
3058 else if (f->sym->attr.intent == INTENT_IN)
3060 spec[spec_len++] = 'r';
3061 spec[spec_len++] = ' ';
3063 else if (f->sym)
3065 spec[spec_len++] = 'w';
3066 spec[spec_len++] = ' ';
3070 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3071 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3072 return build_type_attribute_variant (fntype, tmp);
3076 /* NOTE: The returned function type must match the argument list created by
3077 create_function_arglist. */
3079 tree
3080 gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3081 const char *fnspec)
3083 tree type;
3084 vec<tree, va_gc> *typelist = NULL;
3085 gfc_formal_arglist *f;
3086 gfc_symbol *arg;
3087 int alternate_return = 0;
3088 bool is_varargs = true;
3090 /* Make sure this symbol is a function, a subroutine or the main
3091 program. */
3092 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3093 || sym->attr.flavor == FL_PROGRAM);
3095 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3096 so that they can be detected here and handled further down. */
3097 if (sym->backend_decl == NULL)
3098 sym->backend_decl = error_mark_node;
3099 else if (sym->backend_decl == error_mark_node)
3100 goto arg_type_list_done;
3101 else if (sym->attr.proc_pointer)
3102 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3103 else
3104 return TREE_TYPE (sym->backend_decl);
3106 if (sym->attr.entry_master)
3107 /* Additional parameter for selecting an entry point. */
3108 vec_safe_push (typelist, gfc_array_index_type);
3110 if (sym->result)
3111 arg = sym->result;
3112 else
3113 arg = sym;
3115 if (arg->ts.type == BT_CHARACTER)
3116 gfc_conv_const_charlen (arg->ts.u.cl);
3118 /* Some functions we use an extra parameter for the return value. */
3119 if (gfc_return_by_reference (sym))
3121 type = gfc_sym_type (arg);
3122 if (arg->ts.type == BT_COMPLEX
3123 || arg->attr.dimension
3124 || arg->ts.type == BT_CHARACTER)
3125 type = build_reference_type (type);
3127 vec_safe_push (typelist, type);
3128 if (arg->ts.type == BT_CHARACTER)
3130 if (!arg->ts.deferred)
3131 /* Transfer by value. */
3132 vec_safe_push (typelist, gfc_charlen_type_node);
3133 else
3134 /* Deferred character lengths are transferred by reference
3135 so that the value can be returned. */
3136 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3139 if (sym->backend_decl == error_mark_node && actual_args != NULL
3140 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3141 || sym->attr.proc == PROC_UNKNOWN))
3142 gfc_get_formal_from_actual_arglist (sym, actual_args);
3144 /* Build the argument types for the function. */
3145 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3147 arg = f->sym;
3148 if (arg)
3150 /* Evaluate constant character lengths here so that they can be
3151 included in the type. */
3152 if (arg->ts.type == BT_CHARACTER)
3153 gfc_conv_const_charlen (arg->ts.u.cl);
3155 if (arg->attr.flavor == FL_PROCEDURE)
3157 type = gfc_get_function_type (arg);
3158 type = build_pointer_type (type);
3160 else
3161 type = gfc_sym_type (arg, sym->attr.is_bind_c);
3163 /* Parameter Passing Convention
3165 We currently pass all parameters by reference.
3166 Parameters with INTENT(IN) could be passed by value.
3167 The problem arises if a function is called via an implicit
3168 prototype. In this situation the INTENT is not known.
3169 For this reason all parameters to global functions must be
3170 passed by reference. Passing by value would potentially
3171 generate bad code. Worse there would be no way of telling that
3172 this code was bad, except that it would give incorrect results.
3174 Contained procedures could pass by value as these are never
3175 used without an explicit interface, and cannot be passed as
3176 actual parameters for a dummy procedure. */
3178 vec_safe_push (typelist, type);
3180 else
3182 if (sym->attr.subroutine)
3183 alternate_return = 1;
3187 /* Add hidden arguments. */
3188 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3190 arg = f->sym;
3191 /* Add hidden string length parameters. */
3192 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3194 if (!arg->ts.deferred)
3195 /* Transfer by value. */
3196 type = gfc_charlen_type_node;
3197 else
3198 /* Deferred character lengths are transferred by reference
3199 so that the value can be returned. */
3200 type = build_pointer_type (gfc_charlen_type_node);
3202 vec_safe_push (typelist, type);
3204 /* For noncharacter scalar intrinsic types, VALUE passes the value,
3205 hence, the optional status cannot be transferred via a NULL pointer.
3206 Thus, we will use a hidden argument in that case. */
3207 else if (arg
3208 && arg->attr.optional
3209 && arg->attr.value
3210 && !arg->attr.dimension
3211 && arg->ts.type != BT_CLASS
3212 && !gfc_bt_struct (arg->ts.type))
3213 vec_safe_push (typelist, boolean_type_node);
3214 /* Coarrays which are descriptorless or assumed-shape pass with
3215 -fcoarray=lib the token and the offset as hidden arguments. */
3216 if (arg
3217 && flag_coarray == GFC_FCOARRAY_LIB
3218 && ((arg->ts.type != BT_CLASS
3219 && arg->attr.codimension
3220 && !arg->attr.allocatable)
3221 || (arg->ts.type == BT_CLASS
3222 && CLASS_DATA (arg)->attr.codimension
3223 && !CLASS_DATA (arg)->attr.allocatable)))
3225 vec_safe_push (typelist, pvoid_type_node); /* caf_token. */
3226 vec_safe_push (typelist, gfc_array_index_type); /* caf_offset. */
3230 if (!vec_safe_is_empty (typelist)
3231 || sym->attr.is_main_program
3232 || sym->attr.if_source != IFSRC_UNKNOWN)
3233 is_varargs = false;
3235 if (sym->backend_decl == error_mark_node)
3236 sym->backend_decl = NULL_TREE;
3238 arg_type_list_done:
3240 if (alternate_return)
3241 type = integer_type_node;
3242 else if (!sym->attr.function || gfc_return_by_reference (sym))
3243 type = void_type_node;
3244 else if (sym->attr.mixed_entry_master)
3245 type = gfc_get_mixed_entry_union (sym->ns);
3246 else if (flag_f2c && sym->ts.type == BT_REAL
3247 && sym->ts.kind == gfc_default_real_kind
3248 && !sym->attr.always_explicit)
3250 /* Special case: f2c calling conventions require that (scalar)
3251 default REAL functions return the C type double instead. f2c
3252 compatibility is only an issue with functions that don't
3253 require an explicit interface, as only these could be
3254 implemented in Fortran 77. */
3255 sym->ts.kind = gfc_default_double_kind;
3256 type = gfc_typenode_for_spec (&sym->ts);
3257 sym->ts.kind = gfc_default_real_kind;
3259 else if (sym->result && sym->result->attr.proc_pointer)
3260 /* Procedure pointer return values. */
3262 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3264 /* Unset proc_pointer as gfc_get_function_type
3265 is called recursively. */
3266 sym->result->attr.proc_pointer = 0;
3267 type = build_pointer_type (gfc_get_function_type (sym->result));
3268 sym->result->attr.proc_pointer = 1;
3270 else
3271 type = gfc_sym_type (sym->result);
3273 else
3274 type = gfc_sym_type (sym);
3276 if (is_varargs)
3277 type = build_varargs_function_type_vec (type, typelist);
3278 else
3279 type = build_function_type_vec (type, typelist);
3281 /* If we were passed an fn spec, add it here, otherwise determine it from
3282 the formal arguments. */
3283 if (fnspec)
3285 tree tmp;
3286 int spec_len = strlen (fnspec);
3287 tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3288 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3289 type = build_type_attribute_variant (type, tmp);
3291 else
3292 type = create_fn_spec (sym, type);
3294 return type;
3297 /* Language hooks for middle-end access to type nodes. */
3299 /* Return an integer type with BITS bits of precision,
3300 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3302 tree
3303 gfc_type_for_size (unsigned bits, int unsignedp)
3305 if (!unsignedp)
3307 int i;
3308 for (i = 0; i <= MAX_INT_KINDS; ++i)
3310 tree type = gfc_integer_types[i];
3311 if (type && bits == TYPE_PRECISION (type))
3312 return type;
3315 /* Handle TImode as a special case because it is used by some backends
3316 (e.g. ARM) even though it is not available for normal use. */
3317 #if HOST_BITS_PER_WIDE_INT >= 64
3318 if (bits == TYPE_PRECISION (intTI_type_node))
3319 return intTI_type_node;
3320 #endif
3322 if (bits <= TYPE_PRECISION (intQI_type_node))
3323 return intQI_type_node;
3324 if (bits <= TYPE_PRECISION (intHI_type_node))
3325 return intHI_type_node;
3326 if (bits <= TYPE_PRECISION (intSI_type_node))
3327 return intSI_type_node;
3328 if (bits <= TYPE_PRECISION (intDI_type_node))
3329 return intDI_type_node;
3330 if (bits <= TYPE_PRECISION (intTI_type_node))
3331 return intTI_type_node;
3333 else
3335 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3336 return unsigned_intQI_type_node;
3337 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3338 return unsigned_intHI_type_node;
3339 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3340 return unsigned_intSI_type_node;
3341 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3342 return unsigned_intDI_type_node;
3343 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3344 return unsigned_intTI_type_node;
3347 return NULL_TREE;
3350 /* Return a data type that has machine mode MODE. If the mode is an
3351 integer, then UNSIGNEDP selects between signed and unsigned types. */
3353 tree
3354 gfc_type_for_mode (machine_mode mode, int unsignedp)
3356 int i;
3357 tree *base;
3358 scalar_int_mode int_mode;
3360 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3361 base = gfc_real_types;
3362 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3363 base = gfc_complex_types;
3364 else if (is_a <scalar_int_mode> (mode, &int_mode))
3366 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3367 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3369 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3370 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3372 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
3373 GET_MODE_NUNITS (mode));
3374 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3375 return build_vector_type_for_mode (bool_type, mode);
3377 else if (VECTOR_MODE_P (mode)
3378 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3380 machine_mode inner_mode = GET_MODE_INNER (mode);
3381 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3382 if (inner_type != NULL_TREE)
3383 return build_vector_type_for_mode (inner_type, mode);
3384 return NULL_TREE;
3386 else
3387 return NULL_TREE;
3389 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3391 tree type = base[i];
3392 if (type && mode == TYPE_MODE (type))
3393 return type;
3396 return NULL_TREE;
3399 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3400 in that case. */
3402 bool
3403 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3405 int rank, dim;
3406 bool indirect = false;
3407 tree etype, ptype, t, base_decl;
3408 tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
3409 tree lower_suboff, upper_suboff, stride_suboff;
3410 tree dtype, field, rank_off;
3412 if (! GFC_DESCRIPTOR_TYPE_P (type))
3414 if (! POINTER_TYPE_P (type))
3415 return false;
3416 type = TREE_TYPE (type);
3417 if (! GFC_DESCRIPTOR_TYPE_P (type))
3418 return false;
3419 indirect = true;
3422 rank = GFC_TYPE_ARRAY_RANK (type);
3423 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3424 return false;
3426 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3427 gcc_assert (POINTER_TYPE_P (etype));
3428 etype = TREE_TYPE (etype);
3430 /* If the type is not a scalar coarray. */
3431 if (TREE_CODE (etype) == ARRAY_TYPE)
3432 etype = TREE_TYPE (etype);
3434 /* Can't handle variable sized elements yet. */
3435 if (int_size_in_bytes (etype) <= 0)
3436 return false;
3437 /* Nor non-constant lower bounds in assumed shape arrays. */
3438 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3439 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3441 for (dim = 0; dim < rank; dim++)
3442 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3443 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3444 return false;
3447 memset (info, '\0', sizeof (*info));
3448 info->ndimensions = rank;
3449 info->ordering = array_descr_ordering_column_major;
3450 info->element_type = etype;
3451 ptype = build_pointer_type (gfc_array_index_type);
3452 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3453 if (!base_decl)
3455 base_decl = build_debug_expr_decl (indirect
3456 ? build_pointer_type (ptype) : ptype);
3457 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3459 info->base_decl = base_decl;
3460 if (indirect)
3461 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3463 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3464 &dim_off, &dim_size, &stride_suboff,
3465 &lower_suboff, &upper_suboff);
3467 t = fold_build_pointer_plus (base_decl, span_off);
3468 elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3470 t = base_decl;
3471 if (!integer_zerop (data_off))
3472 t = fold_build_pointer_plus (t, data_off);
3473 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3474 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3475 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3476 info->allocated = build2 (NE_EXPR, logical_type_node,
3477 info->data_location, null_pointer_node);
3478 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3479 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3480 info->associated = build2 (NE_EXPR, logical_type_node,
3481 info->data_location, null_pointer_node);
3482 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3483 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3484 && dwarf_version >= 5)
3486 rank = 1;
3487 info->ndimensions = 1;
3488 t = base_decl;
3489 if (!integer_zerop (dtype_off))
3490 t = fold_build_pointer_plus (t, dtype_off);
3491 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3492 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3493 rank_off = byte_position (field);
3494 if (!integer_zerop (dtype_off))
3495 t = fold_build_pointer_plus (t, rank_off);
3497 t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
3498 t = build1 (INDIRECT_REF, TREE_TYPE (field), t);
3499 info->rank = t;
3500 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3501 t = size_binop (MULT_EXPR, t, dim_size);
3502 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3505 for (dim = 0; dim < rank; dim++)
3507 t = fold_build_pointer_plus (base_decl,
3508 size_binop (PLUS_EXPR,
3509 dim_off, lower_suboff));
3510 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3511 info->dimen[dim].lower_bound = t;
3512 t = fold_build_pointer_plus (base_decl,
3513 size_binop (PLUS_EXPR,
3514 dim_off, upper_suboff));
3515 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3516 info->dimen[dim].upper_bound = t;
3517 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3518 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3520 /* Assumed shape arrays have known lower bounds. */
3521 info->dimen[dim].upper_bound
3522 = build2 (MINUS_EXPR, gfc_array_index_type,
3523 info->dimen[dim].upper_bound,
3524 info->dimen[dim].lower_bound);
3525 info->dimen[dim].lower_bound
3526 = fold_convert (gfc_array_index_type,
3527 GFC_TYPE_ARRAY_LBOUND (type, dim));
3528 info->dimen[dim].upper_bound
3529 = build2 (PLUS_EXPR, gfc_array_index_type,
3530 info->dimen[dim].lower_bound,
3531 info->dimen[dim].upper_bound);
3533 t = fold_build_pointer_plus (base_decl,
3534 size_binop (PLUS_EXPR,
3535 dim_off, stride_suboff));
3536 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3537 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3538 info->dimen[dim].stride = t;
3539 if (dim + 1 < rank)
3540 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3543 return true;
3547 /* Create a type to handle vector subscripts for coarray library calls. It
3548 has the form:
3549 struct caf_vector_t {
3550 size_t nvec; // size of the vector
3551 union {
3552 struct {
3553 void *vector;
3554 int kind;
3555 } v;
3556 struct {
3557 ptrdiff_t lower_bound;
3558 ptrdiff_t upper_bound;
3559 ptrdiff_t stride;
3560 } triplet;
3561 } u;
3563 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3564 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3566 tree
3567 gfc_get_caf_vector_type (int dim)
3569 static tree vector_types[GFC_MAX_DIMENSIONS];
3570 static tree vec_type = NULL_TREE;
3571 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3573 if (vector_types[dim-1] != NULL_TREE)
3574 return vector_types[dim-1];
3576 if (vec_type == NULL_TREE)
3578 chain = 0;
3579 vect_struct_type = make_node (RECORD_TYPE);
3580 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3581 get_identifier ("vector"),
3582 pvoid_type_node, &chain);
3583 suppress_warning (tmp);
3584 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3585 get_identifier ("kind"),
3586 integer_type_node, &chain);
3587 suppress_warning (tmp);
3588 gfc_finish_type (vect_struct_type);
3590 chain = 0;
3591 triplet_struct_type = make_node (RECORD_TYPE);
3592 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3593 get_identifier ("lower_bound"),
3594 gfc_array_index_type, &chain);
3595 suppress_warning (tmp);
3596 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3597 get_identifier ("upper_bound"),
3598 gfc_array_index_type, &chain);
3599 suppress_warning (tmp);
3600 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3601 gfc_array_index_type, &chain);
3602 suppress_warning (tmp);
3603 gfc_finish_type (triplet_struct_type);
3605 chain = 0;
3606 union_type = make_node (UNION_TYPE);
3607 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3608 vect_struct_type, &chain);
3609 suppress_warning (tmp);
3610 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3611 triplet_struct_type, &chain);
3612 suppress_warning (tmp);
3613 gfc_finish_type (union_type);
3615 chain = 0;
3616 vec_type = make_node (RECORD_TYPE);
3617 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3618 size_type_node, &chain);
3619 suppress_warning (tmp);
3620 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3621 union_type, &chain);
3622 suppress_warning (tmp);
3623 gfc_finish_type (vec_type);
3624 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3627 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3628 gfc_rank_cst[dim-1]);
3629 vector_types[dim-1] = build_array_type (vec_type, tmp);
3630 return vector_types[dim-1];
3634 tree
3635 gfc_get_caf_reference_type ()
3637 static tree reference_type = NULL_TREE;
3638 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3639 a_struct_type, u_union_type, tmp, *chain;
3641 if (reference_type != NULL_TREE)
3642 return reference_type;
3644 chain = 0;
3645 c_struct_type = make_node (RECORD_TYPE);
3646 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3647 get_identifier ("offset"),
3648 gfc_array_index_type, &chain);
3649 suppress_warning (tmp);
3650 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3651 get_identifier ("caf_token_offset"),
3652 gfc_array_index_type, &chain);
3653 suppress_warning (tmp);
3654 gfc_finish_type (c_struct_type);
3656 chain = 0;
3657 s_struct_type = make_node (RECORD_TYPE);
3658 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3659 get_identifier ("start"),
3660 gfc_array_index_type, &chain);
3661 suppress_warning (tmp);
3662 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3663 get_identifier ("end"),
3664 gfc_array_index_type, &chain);
3665 suppress_warning (tmp);
3666 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3667 get_identifier ("stride"),
3668 gfc_array_index_type, &chain);
3669 suppress_warning (tmp);
3670 gfc_finish_type (s_struct_type);
3672 chain = 0;
3673 v_struct_type = make_node (RECORD_TYPE);
3674 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3675 get_identifier ("vector"),
3676 pvoid_type_node, &chain);
3677 suppress_warning (tmp);
3678 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3679 get_identifier ("nvec"),
3680 size_type_node, &chain);
3681 suppress_warning (tmp);
3682 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3683 get_identifier ("kind"),
3684 integer_type_node, &chain);
3685 suppress_warning (tmp);
3686 gfc_finish_type (v_struct_type);
3688 chain = 0;
3689 union_type = make_node (UNION_TYPE);
3690 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3691 s_struct_type, &chain);
3692 suppress_warning (tmp);
3693 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3694 v_struct_type, &chain);
3695 suppress_warning (tmp);
3696 gfc_finish_type (union_type);
3698 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3699 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3700 dim_union_type = build_array_type (union_type, tmp);
3702 chain = 0;
3703 a_struct_type = make_node (RECORD_TYPE);
3704 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3705 build_array_type (unsigned_char_type_node,
3706 build_range_type (gfc_array_index_type,
3707 gfc_index_zero_node,
3708 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3709 &chain);
3710 suppress_warning (tmp);
3711 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3712 get_identifier ("static_array_type"),
3713 integer_type_node, &chain);
3714 suppress_warning (tmp);
3715 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3716 dim_union_type, &chain);
3717 suppress_warning (tmp);
3718 gfc_finish_type (a_struct_type);
3720 chain = 0;
3721 u_union_type = make_node (UNION_TYPE);
3722 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3723 c_struct_type, &chain);
3724 suppress_warning (tmp);
3725 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3726 a_struct_type, &chain);
3727 suppress_warning (tmp);
3728 gfc_finish_type (u_union_type);
3730 chain = 0;
3731 reference_type = make_node (RECORD_TYPE);
3732 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3733 build_pointer_type (reference_type), &chain);
3734 suppress_warning (tmp);
3735 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3736 integer_type_node, &chain);
3737 suppress_warning (tmp);
3738 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3739 size_type_node, &chain);
3740 suppress_warning (tmp);
3741 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3742 u_union_type, &chain);
3743 suppress_warning (tmp);
3744 gfc_finish_type (reference_type);
3745 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3747 return reference_type;
3750 static tree
3751 gfc_get_cfi_dim_type ()
3753 static tree CFI_dim_t = NULL;
3755 if (CFI_dim_t)
3756 return CFI_dim_t;
3758 CFI_dim_t = make_node (RECORD_TYPE);
3759 TYPE_NAME (CFI_dim_t) = get_identifier ("CFI_dim_t");
3760 TYPE_NAMELESS (CFI_dim_t) = 1;
3761 tree field;
3762 tree *chain = NULL;
3763 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("lower_bound"),
3764 gfc_array_index_type, &chain);
3765 suppress_warning (field);
3766 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("extent"),
3767 gfc_array_index_type, &chain);
3768 suppress_warning (field);
3769 field = gfc_add_field_to_struct_1 (CFI_dim_t, get_identifier ("sm"),
3770 gfc_array_index_type, &chain);
3771 suppress_warning (field);
3772 gfc_finish_type (CFI_dim_t);
3773 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (CFI_dim_t)) = 1;
3774 return CFI_dim_t;
3778 /* Return the CFI type; use dimen == -1 for dim[] (only for pointers);
3779 otherwise dim[dimen] is used. */
3781 tree
3782 gfc_get_cfi_type (int dimen, bool restricted)
3784 gcc_assert (dimen >= -1 && dimen <= CFI_MAX_RANK);
3786 int idx = 2*(dimen + 1) + restricted;
3788 if (gfc_cfi_descriptor_base[idx])
3789 return gfc_cfi_descriptor_base[idx];
3791 /* Build the type node. */
3792 tree CFI_cdesc_t = make_node (RECORD_TYPE);
3793 char name[GFC_MAX_SYMBOL_LEN + 1];
3794 if (dimen != -1)
3795 sprintf (name, "CFI_cdesc_t" GFC_RANK_PRINTF_FORMAT, dimen);
3796 TYPE_NAME (CFI_cdesc_t) = get_identifier (dimen < 0 ? "CFI_cdesc_t" : name);
3797 TYPE_NAMELESS (CFI_cdesc_t) = 1;
3799 tree field;
3800 tree *chain = NULL;
3801 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("base_addr"),
3802 (restricted ? prvoid_type_node
3803 : ptr_type_node), &chain);
3804 suppress_warning (field);
3805 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("elem_len"),
3806 size_type_node, &chain);
3807 suppress_warning (field);
3808 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("version"),
3809 integer_type_node, &chain);
3810 suppress_warning (field);
3811 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("rank"),
3812 signed_char_type_node, &chain);
3813 suppress_warning (field);
3814 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("attribute"),
3815 signed_char_type_node, &chain);
3816 suppress_warning (field);
3817 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("type"),
3818 get_typenode_from_name (INT16_TYPE),
3819 &chain);
3820 suppress_warning (field);
3822 if (dimen != 0)
3824 tree range = NULL_TREE;
3825 if (dimen > 0)
3826 range = gfc_rank_cst[dimen - 1];
3827 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3828 range);
3829 tree CFI_dim_t = build_array_type (gfc_get_cfi_dim_type (), range);
3830 field = gfc_add_field_to_struct_1 (CFI_cdesc_t, get_identifier ("dim"),
3831 CFI_dim_t, &chain);
3832 suppress_warning (field);
3835 TYPE_TYPELESS_STORAGE (CFI_cdesc_t) = 1;
3836 gfc_finish_type (CFI_cdesc_t);
3837 gfc_cfi_descriptor_base[idx] = CFI_cdesc_t;
3838 return CFI_cdesc_t;
3841 #include "gt-fortran-trans-types.h"