Daily bump.
[official-gcc.git] / gcc / fortran / trans-types.c
blob220976babb81ad222ba2b864ec3401d9dadbc160
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2021 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.c -- 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 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)];
81 /* Arrays for all integral and real kinds. We'll fill this in at runtime
82 after the target has a chance to process command-line options. */
84 #define MAX_INT_KINDS 5
85 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
86 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
87 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
88 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
90 #define MAX_REAL_KINDS 5
91 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
92 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
93 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
95 #define MAX_CHARACTER_KINDS 2
96 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
97 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
98 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
100 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
102 /* The integer kind to use for array indices. This will be set to the
103 proper value based on target information from the backend. */
105 int gfc_index_integer_kind;
107 /* The default kinds of the various types. */
109 int gfc_default_integer_kind;
110 int gfc_max_integer_kind;
111 int gfc_default_real_kind;
112 int gfc_default_double_kind;
113 int gfc_default_character_kind;
114 int gfc_default_logical_kind;
115 int gfc_default_complex_kind;
116 int gfc_c_int_kind;
117 int gfc_c_intptr_kind;
118 int gfc_atomic_int_kind;
119 int gfc_atomic_logical_kind;
121 /* The kind size used for record offsets. If the target system supports
122 kind=8, this will be set to 8, otherwise it is set to 4. */
123 int gfc_intio_kind;
125 /* The integer kind used to store character lengths. */
126 int gfc_charlen_int_kind;
128 /* Kind of internal integer for storing object sizes. */
129 int gfc_size_kind;
131 /* The size of the numeric storage unit and character storage unit. */
132 int gfc_numeric_storage_size;
133 int gfc_character_storage_size;
135 tree dtype_type_node = NULL_TREE;
138 /* Build the dtype_type_node if necessary. */
139 tree get_dtype_type_node (void)
141 tree field;
142 tree dtype_node;
143 tree *dtype_chain = NULL;
145 if (dtype_type_node == NULL_TREE)
147 dtype_node = make_node (RECORD_TYPE);
148 TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
149 TYPE_NAMELESS (dtype_node) = 1;
150 field = gfc_add_field_to_struct_1 (dtype_node,
151 get_identifier ("elem_len"),
152 size_type_node, &dtype_chain);
153 suppress_warning (field);
154 field = gfc_add_field_to_struct_1 (dtype_node,
155 get_identifier ("version"),
156 integer_type_node, &dtype_chain);
157 suppress_warning (field);
158 field = gfc_add_field_to_struct_1 (dtype_node,
159 get_identifier ("rank"),
160 signed_char_type_node, &dtype_chain);
161 suppress_warning (field);
162 field = gfc_add_field_to_struct_1 (dtype_node,
163 get_identifier ("type"),
164 signed_char_type_node, &dtype_chain);
165 suppress_warning (field);
166 field = gfc_add_field_to_struct_1 (dtype_node,
167 get_identifier ("attribute"),
168 short_integer_type_node, &dtype_chain);
169 suppress_warning (field);
170 gfc_finish_type (dtype_node);
171 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
172 dtype_type_node = dtype_node;
174 return dtype_type_node;
177 bool
178 gfc_check_any_c_kind (gfc_typespec *ts)
180 int i;
182 for (i = 0; i < ISOCBINDING_NUMBER; i++)
184 /* Check for any C interoperable kind for the given type/kind in ts.
185 This can be used after verify_c_interop to make sure that the
186 Fortran kind being used exists in at least some form for C. */
187 if (c_interop_kinds_table[i].f90_type == ts->type &&
188 c_interop_kinds_table[i].value == ts->kind)
189 return true;
192 return false;
196 static int
197 get_real_kind_from_node (tree type)
199 int i;
201 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
202 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
203 return gfc_real_kinds[i].kind;
205 return -4;
208 static int
209 get_int_kind_from_node (tree type)
211 int i;
213 if (!type)
214 return -2;
216 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
217 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
218 return gfc_integer_kinds[i].kind;
220 return -1;
223 static int
224 get_int_kind_from_name (const char *name)
226 return get_int_kind_from_node (get_typenode_from_name (name));
230 /* Get the kind number corresponding to an integer of given size,
231 following the required return values for ISO_FORTRAN_ENV INT* constants:
232 -2 is returned if we support a kind of larger size, -1 otherwise. */
234 gfc_get_int_kind_from_width_isofortranenv (int size)
236 int i;
238 /* Look for a kind with matching storage size. */
239 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
240 if (gfc_integer_kinds[i].bit_size == size)
241 return gfc_integer_kinds[i].kind;
243 /* Look for a kind with larger storage size. */
244 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
245 if (gfc_integer_kinds[i].bit_size > size)
246 return -2;
248 return -1;
252 /* Get the kind number corresponding to a real of a given storage size.
253 If two real's have the same storage size, then choose the real with
254 the largest precision. If a kind type is unavailable and a real
255 exists with wider storage, then return -2; otherwise, return -1. */
258 gfc_get_real_kind_from_width_isofortranenv (int size)
260 int digits, i, kind;
262 size /= 8;
264 kind = -1;
265 digits = 0;
267 /* Look for a kind with matching storage size. */
268 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
269 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
271 if (gfc_real_kinds[i].digits > digits)
273 digits = gfc_real_kinds[i].digits;
274 kind = gfc_real_kinds[i].kind;
278 if (kind != -1)
279 return kind;
281 /* Look for a kind with larger storage size. */
282 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
283 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
284 kind = -2;
286 return kind;
291 static int
292 get_int_kind_from_width (int size)
294 int i;
296 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
297 if (gfc_integer_kinds[i].bit_size == size)
298 return gfc_integer_kinds[i].kind;
300 return -2;
303 static int
304 get_int_kind_from_minimal_width (int size)
306 int i;
308 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
309 if (gfc_integer_kinds[i].bit_size >= size)
310 return gfc_integer_kinds[i].kind;
312 return -2;
316 /* Generate the CInteropKind_t objects for the C interoperable
317 kinds. */
319 void
320 gfc_init_c_interop_kinds (void)
322 int i;
324 /* init all pointers in the list to NULL */
325 for (i = 0; i < ISOCBINDING_NUMBER; i++)
327 /* Initialize the name and value fields. */
328 c_interop_kinds_table[i].name[0] = '\0';
329 c_interop_kinds_table[i].value = -100;
330 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
333 #define NAMED_INTCST(a,b,c,d) \
334 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
335 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
336 c_interop_kinds_table[a].value = c;
337 #define NAMED_REALCST(a,b,c,d) \
338 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
339 c_interop_kinds_table[a].f90_type = BT_REAL; \
340 c_interop_kinds_table[a].value = c;
341 #define NAMED_CMPXCST(a,b,c,d) \
342 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
343 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
344 c_interop_kinds_table[a].value = c;
345 #define NAMED_LOGCST(a,b,c) \
346 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
347 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
348 c_interop_kinds_table[a].value = c;
349 #define NAMED_CHARKNDCST(a,b,c) \
350 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
351 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
352 c_interop_kinds_table[a].value = c;
353 #define NAMED_CHARCST(a,b,c) \
354 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
355 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
356 c_interop_kinds_table[a].value = c;
357 #define DERIVED_TYPE(a,b,c) \
358 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
359 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
360 c_interop_kinds_table[a].value = c;
361 #define NAMED_FUNCTION(a,b,c,d) \
362 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
363 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
364 c_interop_kinds_table[a].value = c;
365 #define NAMED_SUBROUTINE(a,b,c,d) \
366 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
367 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
368 c_interop_kinds_table[a].value = c;
369 #include "iso-c-binding.def"
373 /* Query the target to determine which machine modes are available for
374 computation. Choose KIND numbers for them. */
376 void
377 gfc_init_kinds (void)
379 opt_scalar_int_mode int_mode_iter;
380 opt_scalar_float_mode float_mode_iter;
381 int i_index, r_index, kind;
382 bool saw_i4 = false, saw_i8 = false;
383 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
385 i_index = 0;
386 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
388 scalar_int_mode mode = int_mode_iter.require ();
389 int kind, bitsize;
391 if (!targetm.scalar_mode_supported_p (mode))
392 continue;
394 /* The middle end doesn't support constants larger than 2*HWI.
395 Perhaps the target hook shouldn't have accepted these either,
396 but just to be safe... */
397 bitsize = GET_MODE_BITSIZE (mode);
398 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
399 continue;
401 gcc_assert (i_index != MAX_INT_KINDS);
403 /* Let the kind equal the bit size divided by 8. This insulates the
404 programmer from the underlying byte size. */
405 kind = bitsize / 8;
407 if (kind == 4)
408 saw_i4 = true;
409 if (kind == 8)
410 saw_i8 = true;
412 gfc_integer_kinds[i_index].kind = kind;
413 gfc_integer_kinds[i_index].radix = 2;
414 gfc_integer_kinds[i_index].digits = bitsize - 1;
415 gfc_integer_kinds[i_index].bit_size = bitsize;
417 gfc_logical_kinds[i_index].kind = kind;
418 gfc_logical_kinds[i_index].bit_size = bitsize;
420 i_index += 1;
423 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
424 used for large file access. */
426 if (saw_i8)
427 gfc_intio_kind = 8;
428 else
429 gfc_intio_kind = 4;
431 /* If we do not at least have kind = 4, everything is pointless. */
432 gcc_assert(saw_i4);
434 /* Set the maximum integer kind. Used with at least BOZ constants. */
435 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
437 r_index = 0;
438 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
440 scalar_float_mode mode = float_mode_iter.require ();
441 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
442 int kind;
444 if (fmt == NULL)
445 continue;
446 if (!targetm.scalar_mode_supported_p (mode))
447 continue;
449 /* Only let float, double, long double and TFmode go through.
450 Runtime support for others is not provided, so they would be
451 useless. */
452 if (!targetm.libgcc_floating_mode_supported_p (mode))
453 continue;
454 if (mode != TYPE_MODE (float_type_node)
455 && (mode != TYPE_MODE (double_type_node))
456 && (mode != TYPE_MODE (long_double_type_node))
457 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
458 && (mode != TFmode)
459 #endif
461 continue;
463 /* Let the kind equal the precision divided by 8, rounding up. Again,
464 this insulates the programmer from the underlying byte size.
466 Also, it effectively deals with IEEE extended formats. There, the
467 total size of the type may equal 16, but it's got 6 bytes of padding
468 and the increased size can get in the way of a real IEEE quad format
469 which may also be supported by the target.
471 We round up so as to handle IA-64 __floatreg (RFmode), which is an
472 82 bit type. Not to be confused with __float80 (XFmode), which is
473 an 80 bit type also supported by IA-64. So XFmode should come out
474 to be kind=10, and RFmode should come out to be kind=11. Egads.
476 TODO: The kind calculation has to be modified to support all
477 three 128-bit floating-point modes on PowerPC as IFmode, KFmode,
478 and TFmode since the following line would all map to kind=16.
479 However, currently only float, double, long double, and TFmode
480 reach this code.
483 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
485 if (kind == 4)
486 saw_r4 = true;
487 if (kind == 8)
488 saw_r8 = true;
489 if (kind == 10)
490 saw_r10 = true;
491 if (kind == 16)
492 saw_r16 = true;
494 /* Careful we don't stumble a weird internal mode. */
495 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
496 /* Or have too many modes for the allocated space. */
497 gcc_assert (r_index != MAX_REAL_KINDS);
499 gfc_real_kinds[r_index].kind = kind;
500 gfc_real_kinds[r_index].radix = fmt->b;
501 gfc_real_kinds[r_index].digits = fmt->p;
502 gfc_real_kinds[r_index].min_exponent = fmt->emin;
503 gfc_real_kinds[r_index].max_exponent = fmt->emax;
504 if (fmt->pnan < fmt->p)
505 /* This is an IBM extended double format (or the MIPS variant)
506 made up of two IEEE doubles. The value of the long double is
507 the sum of the values of the two parts. The most significant
508 part is required to be the value of the long double rounded
509 to the nearest double. If we use emax of 1024 then we can't
510 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
511 rounding will make the most significant part overflow. */
512 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
513 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
514 r_index += 1;
517 /* Choose the default integer kind. We choose 4 unless the user directs us
518 otherwise. Even if the user specified that the default integer kind is 8,
519 the numeric storage size is not 64 bits. In this case, a warning will be
520 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
522 gfc_numeric_storage_size = 4 * 8;
524 if (flag_default_integer)
526 if (!saw_i8)
527 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
528 "%<-fdefault-integer-8%> option");
530 gfc_default_integer_kind = 8;
533 else if (flag_integer4_kind == 8)
535 if (!saw_i8)
536 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
537 "%<-finteger-4-integer-8%> option");
539 gfc_default_integer_kind = 8;
541 else if (saw_i4)
543 gfc_default_integer_kind = 4;
545 else
547 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
548 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
551 /* Choose the default real kind. Again, we choose 4 when possible. */
552 if (flag_default_real_8)
554 if (!saw_r8)
555 gfc_fatal_error ("REAL(KIND=8) is not available for "
556 "%<-fdefault-real-8%> option");
558 gfc_default_real_kind = 8;
560 else if (flag_default_real_10)
562 if (!saw_r10)
563 gfc_fatal_error ("REAL(KIND=10) is not available for "
564 "%<-fdefault-real-10%> option");
566 gfc_default_real_kind = 10;
568 else if (flag_default_real_16)
570 if (!saw_r16)
571 gfc_fatal_error ("REAL(KIND=16) is not available for "
572 "%<-fdefault-real-16%> option");
574 gfc_default_real_kind = 16;
576 else if (flag_real4_kind == 8)
578 if (!saw_r8)
579 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
580 "option");
582 gfc_default_real_kind = 8;
584 else if (flag_real4_kind == 10)
586 if (!saw_r10)
587 gfc_fatal_error ("REAL(KIND=10) is not available for "
588 "%<-freal-4-real-10%> option");
590 gfc_default_real_kind = 10;
592 else if (flag_real4_kind == 16)
594 if (!saw_r16)
595 gfc_fatal_error ("REAL(KIND=16) is not available for "
596 "%<-freal-4-real-16%> option");
598 gfc_default_real_kind = 16;
600 else if (saw_r4)
601 gfc_default_real_kind = 4;
602 else
603 gfc_default_real_kind = gfc_real_kinds[0].kind;
605 /* Choose the default double kind. If -fdefault-real and -fdefault-double
606 are specified, we use kind=8, if it's available. If -fdefault-real is
607 specified without -fdefault-double, we use kind=16, if it's available.
608 Otherwise we do not change anything. */
609 if (flag_default_double && saw_r8)
610 gfc_default_double_kind = 8;
611 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
613 /* Use largest available kind. */
614 if (saw_r16)
615 gfc_default_double_kind = 16;
616 else if (saw_r10)
617 gfc_default_double_kind = 10;
618 else if (saw_r8)
619 gfc_default_double_kind = 8;
620 else
621 gfc_default_double_kind = gfc_default_real_kind;
623 else if (flag_real8_kind == 4)
625 if (!saw_r4)
626 gfc_fatal_error ("REAL(KIND=4) is not available for "
627 "%<-freal-8-real-4%> option");
629 gfc_default_double_kind = 4;
631 else if (flag_real8_kind == 10 )
633 if (!saw_r10)
634 gfc_fatal_error ("REAL(KIND=10) is not available for "
635 "%<-freal-8-real-10%> option");
637 gfc_default_double_kind = 10;
639 else if (flag_real8_kind == 16 )
641 if (!saw_r16)
642 gfc_fatal_error ("REAL(KIND=10) is not available for "
643 "%<-freal-8-real-16%> option");
645 gfc_default_double_kind = 16;
647 else if (saw_r4 && saw_r8)
648 gfc_default_double_kind = 8;
649 else
651 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
652 real ... occupies two contiguous numeric storage units.
654 Therefore we must be supplied a kind twice as large as we chose
655 for single precision. There are loopholes, in that double
656 precision must *occupy* two storage units, though it doesn't have
657 to *use* two storage units. Which means that you can make this
658 kind artificially wide by padding it. But at present there are
659 no GCC targets for which a two-word type does not exist, so we
660 just let gfc_validate_kind abort and tell us if something breaks. */
662 gfc_default_double_kind
663 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
666 /* The default logical kind is constrained to be the same as the
667 default integer kind. Similarly with complex and real. */
668 gfc_default_logical_kind = gfc_default_integer_kind;
669 gfc_default_complex_kind = gfc_default_real_kind;
671 /* We only have two character kinds: ASCII and UCS-4.
672 ASCII corresponds to a 8-bit integer type, if one is available.
673 UCS-4 corresponds to a 32-bit integer type, if one is available. */
674 i_index = 0;
675 if ((kind = get_int_kind_from_width (8)) > 0)
677 gfc_character_kinds[i_index].kind = kind;
678 gfc_character_kinds[i_index].bit_size = 8;
679 gfc_character_kinds[i_index].name = "ascii";
680 i_index++;
682 if ((kind = get_int_kind_from_width (32)) > 0)
684 gfc_character_kinds[i_index].kind = kind;
685 gfc_character_kinds[i_index].bit_size = 32;
686 gfc_character_kinds[i_index].name = "iso_10646";
687 i_index++;
690 /* Choose the smallest integer kind for our default character. */
691 gfc_default_character_kind = gfc_character_kinds[0].kind;
692 gfc_character_storage_size = gfc_default_character_kind * 8;
694 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
696 /* Pick a kind the same size as the C "int" type. */
697 gfc_c_int_kind = INT_TYPE_SIZE / 8;
699 /* Choose atomic kinds to match C's int. */
700 gfc_atomic_int_kind = gfc_c_int_kind;
701 gfc_atomic_logical_kind = gfc_c_int_kind;
703 gfc_c_intptr_kind = POINTER_SIZE / 8;
707 /* Make sure that a valid kind is present. Returns an index into the
708 associated kinds array, -1 if the kind is not present. */
710 static int
711 validate_integer (int kind)
713 int i;
715 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
716 if (gfc_integer_kinds[i].kind == kind)
717 return i;
719 return -1;
722 static int
723 validate_real (int kind)
725 int i;
727 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
728 if (gfc_real_kinds[i].kind == kind)
729 return i;
731 return -1;
734 static int
735 validate_logical (int kind)
737 int i;
739 for (i = 0; gfc_logical_kinds[i].kind; i++)
740 if (gfc_logical_kinds[i].kind == kind)
741 return i;
743 return -1;
746 static int
747 validate_character (int kind)
749 int i;
751 for (i = 0; gfc_character_kinds[i].kind; i++)
752 if (gfc_character_kinds[i].kind == kind)
753 return i;
755 return -1;
758 /* Validate a kind given a basic type. The return value is the same
759 for the child functions, with -1 indicating nonexistence of the
760 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
763 gfc_validate_kind (bt type, int kind, bool may_fail)
765 int rc;
767 switch (type)
769 case BT_REAL: /* Fall through */
770 case BT_COMPLEX:
771 rc = validate_real (kind);
772 break;
773 case BT_INTEGER:
774 rc = validate_integer (kind);
775 break;
776 case BT_LOGICAL:
777 rc = validate_logical (kind);
778 break;
779 case BT_CHARACTER:
780 rc = validate_character (kind);
781 break;
783 default:
784 gfc_internal_error ("gfc_validate_kind(): Got bad type");
787 if (rc < 0 && !may_fail)
788 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
790 return rc;
794 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
795 Reuse common type nodes where possible. Recognize if the kind matches up
796 with a C type. This will be used later in determining which routines may
797 be scarfed from libm. */
799 static tree
800 gfc_build_int_type (gfc_integer_info *info)
802 int mode_precision = info->bit_size;
804 if (mode_precision == CHAR_TYPE_SIZE)
805 info->c_char = 1;
806 if (mode_precision == SHORT_TYPE_SIZE)
807 info->c_short = 1;
808 if (mode_precision == INT_TYPE_SIZE)
809 info->c_int = 1;
810 if (mode_precision == LONG_TYPE_SIZE)
811 info->c_long = 1;
812 if (mode_precision == LONG_LONG_TYPE_SIZE)
813 info->c_long_long = 1;
815 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
816 return intQI_type_node;
817 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
818 return intHI_type_node;
819 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
820 return intSI_type_node;
821 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
822 return intDI_type_node;
823 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
824 return intTI_type_node;
826 return make_signed_type (mode_precision);
829 tree
830 gfc_build_uint_type (int size)
832 if (size == CHAR_TYPE_SIZE)
833 return unsigned_char_type_node;
834 if (size == SHORT_TYPE_SIZE)
835 return short_unsigned_type_node;
836 if (size == INT_TYPE_SIZE)
837 return unsigned_type_node;
838 if (size == LONG_TYPE_SIZE)
839 return long_unsigned_type_node;
840 if (size == LONG_LONG_TYPE_SIZE)
841 return long_long_unsigned_type_node;
843 return make_unsigned_type (size);
847 static tree
848 gfc_build_real_type (gfc_real_info *info)
850 int mode_precision = info->mode_precision;
851 tree new_type;
853 if (mode_precision == FLOAT_TYPE_SIZE)
854 info->c_float = 1;
855 if (mode_precision == DOUBLE_TYPE_SIZE)
856 info->c_double = 1;
857 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
858 info->c_long_double = 1;
859 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
861 /* TODO: see PR101835. */
862 info->c_float128 = 1;
863 gfc_real16_is_float128 = true;
866 if (TYPE_PRECISION (float_type_node) == mode_precision)
867 return float_type_node;
868 if (TYPE_PRECISION (double_type_node) == mode_precision)
869 return double_type_node;
870 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
871 return long_double_type_node;
873 new_type = make_node (REAL_TYPE);
874 TYPE_PRECISION (new_type) = mode_precision;
875 layout_type (new_type);
876 return new_type;
879 static tree
880 gfc_build_complex_type (tree scalar_type)
882 tree new_type;
884 if (scalar_type == NULL)
885 return NULL;
886 if (scalar_type == float_type_node)
887 return complex_float_type_node;
888 if (scalar_type == double_type_node)
889 return complex_double_type_node;
890 if (scalar_type == long_double_type_node)
891 return complex_long_double_type_node;
893 new_type = make_node (COMPLEX_TYPE);
894 TREE_TYPE (new_type) = scalar_type;
895 layout_type (new_type);
896 return new_type;
899 static tree
900 gfc_build_logical_type (gfc_logical_info *info)
902 int bit_size = info->bit_size;
903 tree new_type;
905 if (bit_size == BOOL_TYPE_SIZE)
907 info->c_bool = 1;
908 return boolean_type_node;
911 new_type = make_unsigned_type (bit_size);
912 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
913 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
914 TYPE_PRECISION (new_type) = 1;
916 return new_type;
920 /* Create the backend type nodes. We map them to their
921 equivalent C type, at least for now. We also give
922 names to the types here, and we push them in the
923 global binding level context.*/
925 void
926 gfc_init_types (void)
928 char name_buf[26];
929 int index;
930 tree type;
931 unsigned n;
933 /* Create and name the types. */
934 #define PUSH_TYPE(name, node) \
935 pushdecl (build_decl (input_location, \
936 TYPE_DECL, get_identifier (name), node))
938 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
940 type = gfc_build_int_type (&gfc_integer_kinds[index]);
941 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
942 if (TYPE_STRING_FLAG (type))
943 type = make_signed_type (gfc_integer_kinds[index].bit_size);
944 gfc_integer_types[index] = type;
945 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
946 gfc_integer_kinds[index].kind);
947 PUSH_TYPE (name_buf, type);
950 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
952 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
953 gfc_logical_types[index] = type;
954 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
955 gfc_logical_kinds[index].kind);
956 PUSH_TYPE (name_buf, type);
959 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
961 type = gfc_build_real_type (&gfc_real_kinds[index]);
962 gfc_real_types[index] = type;
963 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
964 gfc_real_kinds[index].kind);
965 PUSH_TYPE (name_buf, type);
967 if (gfc_real_kinds[index].c_float128)
968 gfc_float128_type_node = type;
970 type = gfc_build_complex_type (type);
971 gfc_complex_types[index] = type;
972 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
973 gfc_real_kinds[index].kind);
974 PUSH_TYPE (name_buf, type);
976 if (gfc_real_kinds[index].c_float128)
977 gfc_complex_float128_type_node = type;
980 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
982 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
983 type = build_qualified_type (type, TYPE_UNQUALIFIED);
984 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
985 gfc_character_kinds[index].kind);
986 PUSH_TYPE (name_buf, type);
987 gfc_character_types[index] = type;
988 gfc_pcharacter_types[index] = build_pointer_type (type);
990 gfc_character1_type_node = gfc_character_types[0];
992 PUSH_TYPE ("byte", unsigned_char_type_node);
993 PUSH_TYPE ("void", void_type_node);
995 /* DBX debugging output gets upset if these aren't set. */
996 if (!TYPE_NAME (integer_type_node))
997 PUSH_TYPE ("c_integer", integer_type_node);
998 if (!TYPE_NAME (char_type_node))
999 PUSH_TYPE ("c_char", char_type_node);
1001 #undef PUSH_TYPE
1003 pvoid_type_node = build_pointer_type (void_type_node);
1004 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
1005 ppvoid_type_node = build_pointer_type (pvoid_type_node);
1006 pchar_type_node = build_pointer_type (gfc_character1_type_node);
1007 pfunc_type_node
1008 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
1010 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
1011 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1012 since this function is called before gfc_init_constants. */
1013 gfc_array_range_type
1014 = build_range_type (gfc_array_index_type,
1015 build_int_cst (gfc_array_index_type, 0),
1016 NULL_TREE);
1018 /* The maximum array element size that can be handled is determined
1019 by the number of bits available to store this field in the array
1020 descriptor. */
1022 n = TYPE_PRECISION (size_type_node);
1023 gfc_max_array_element_size
1024 = wide_int_to_tree (size_type_node,
1025 wi::mask (n, UNSIGNED,
1026 TYPE_PRECISION (size_type_node)));
1028 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1029 logical_true_node = build_int_cst (logical_type_node, 1);
1030 logical_false_node = build_int_cst (logical_type_node, 0);
1032 /* Character lengths are of type size_t, except signed. */
1033 gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1034 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1036 /* Fortran kind number of size_type_node (size_t). This is used for
1037 the _size member in vtables. */
1038 gfc_size_kind = get_int_kind_from_node (size_type_node);
1041 /* Get the type node for the given type and kind. */
1043 tree
1044 gfc_get_int_type (int kind)
1046 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1047 return index < 0 ? 0 : gfc_integer_types[index];
1050 tree
1051 gfc_get_real_type (int kind)
1053 int index = gfc_validate_kind (BT_REAL, kind, true);
1054 return index < 0 ? 0 : gfc_real_types[index];
1057 tree
1058 gfc_get_complex_type (int kind)
1060 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1061 return index < 0 ? 0 : gfc_complex_types[index];
1064 tree
1065 gfc_get_logical_type (int kind)
1067 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1068 return index < 0 ? 0 : gfc_logical_types[index];
1071 tree
1072 gfc_get_char_type (int kind)
1074 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1075 return index < 0 ? 0 : gfc_character_types[index];
1078 tree
1079 gfc_get_pchar_type (int kind)
1081 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1082 return index < 0 ? 0 : gfc_pcharacter_types[index];
1086 /* Create a character type with the given kind and length. */
1088 tree
1089 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1091 tree bounds, type;
1093 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1094 type = build_array_type (eltype, bounds);
1095 TYPE_STRING_FLAG (type) = 1;
1097 return type;
1100 tree
1101 gfc_get_character_type_len (int kind, tree len)
1103 gfc_validate_kind (BT_CHARACTER, kind, false);
1104 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1108 /* Get a type node for a character kind. */
1110 tree
1111 gfc_get_character_type (int kind, gfc_charlen * cl)
1113 tree len;
1115 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1116 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1117 len = build_fold_indirect_ref (len);
1119 return gfc_get_character_type_len (kind, len);
1122 /* Convert a basic type. This will be an array for character types. */
1124 tree
1125 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1127 tree basetype;
1129 switch (spec->type)
1131 case BT_UNKNOWN:
1132 gcc_unreachable ();
1134 case BT_INTEGER:
1135 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1136 has been resolved. This is done so we can convert C_PTR and
1137 C_FUNPTR to simple variables that get translated to (void *). */
1138 if (spec->f90_type == BT_VOID)
1140 if (spec->u.derived
1141 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1142 basetype = ptr_type_node;
1143 else
1144 basetype = pfunc_type_node;
1146 else
1147 basetype = gfc_get_int_type (spec->kind);
1148 break;
1150 case BT_REAL:
1151 basetype = gfc_get_real_type (spec->kind);
1152 break;
1154 case BT_COMPLEX:
1155 basetype = gfc_get_complex_type (spec->kind);
1156 break;
1158 case BT_LOGICAL:
1159 basetype = gfc_get_logical_type (spec->kind);
1160 break;
1162 case BT_CHARACTER:
1163 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1164 break;
1166 case BT_HOLLERITH:
1167 /* Since this cannot be used, return a length one character. */
1168 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1169 gfc_index_one_node);
1170 break;
1172 case BT_UNION:
1173 basetype = gfc_get_union_type (spec->u.derived);
1174 break;
1176 case BT_DERIVED:
1177 case BT_CLASS:
1178 basetype = gfc_get_derived_type (spec->u.derived, codim);
1180 if (spec->type == BT_CLASS)
1181 GFC_CLASS_TYPE_P (basetype) = 1;
1183 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1184 type and kind to fit a (void *) and the basetype returned was a
1185 ptr_type_node. We need to pass up this new information to the
1186 symbol that was declared of type C_PTR or C_FUNPTR. */
1187 if (spec->u.derived->ts.f90_type == BT_VOID)
1189 spec->type = BT_INTEGER;
1190 spec->kind = gfc_index_integer_kind;
1191 spec->f90_type = BT_VOID;
1192 spec->is_c_interop = 1; /* Mark as escaping later. */
1194 break;
1195 case BT_VOID:
1196 case BT_ASSUMED:
1197 /* This is for the second arg to c_f_pointer and c_f_procpointer
1198 of the iso_c_binding module, to accept any ptr type. */
1199 basetype = ptr_type_node;
1200 if (spec->f90_type == BT_VOID)
1202 if (spec->u.derived
1203 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1204 basetype = ptr_type_node;
1205 else
1206 basetype = pfunc_type_node;
1208 break;
1209 case BT_PROCEDURE:
1210 basetype = pfunc_type_node;
1211 break;
1212 default:
1213 gcc_unreachable ();
1215 return basetype;
1218 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1220 static tree
1221 gfc_conv_array_bound (gfc_expr * expr)
1223 /* If expr is an integer constant, return that. */
1224 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1225 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1227 /* Otherwise return NULL. */
1228 return NULL_TREE;
1231 /* Return the type of an element of the array. Note that scalar coarrays
1232 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1233 (with POINTER_TYPE stripped) is returned. */
1235 tree
1236 gfc_get_element_type (tree type)
1238 tree element;
1240 if (GFC_ARRAY_TYPE_P (type))
1242 if (TREE_CODE (type) == POINTER_TYPE)
1243 type = TREE_TYPE (type);
1244 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1246 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1247 element = type;
1249 else
1251 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1252 element = TREE_TYPE (type);
1255 else
1257 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1258 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1260 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1261 element = TREE_TYPE (element);
1263 /* For arrays, which are not scalar coarrays. */
1264 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1265 element = TREE_TYPE (element);
1268 return element;
1271 /* Build an array. This function is called from gfc_sym_type().
1272 Actually returns array descriptor type.
1274 Format of array descriptors is as follows:
1276 struct gfc_array_descriptor
1278 array *data;
1279 index offset;
1280 struct dtype_type dtype;
1281 struct descriptor_dimension dimension[N_DIM];
1284 struct dtype_type
1286 size_t elem_len;
1287 int version;
1288 signed char rank;
1289 signed char type;
1290 signed short attribute;
1293 struct descriptor_dimension
1295 index stride;
1296 index lbound;
1297 index ubound;
1300 Translation code should use gfc_conv_descriptor_* rather than
1301 accessing the descriptor directly. Any changes to the array
1302 descriptor type will require changes in gfc_conv_descriptor_* and
1303 gfc_build_array_initializer.
1305 This is represented internally as a RECORD_TYPE. The index nodes
1306 are gfc_array_index_type and the data node is a pointer to the
1307 data. See below for the handling of character types.
1309 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1310 this generated poor code for assumed/deferred size arrays. These
1311 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1312 of the GENERIC grammar. Also, there is no way to explicitly set
1313 the array stride, so all data must be packed(1). I've tried to
1314 mark all the functions which would require modification with a GCC
1315 ARRAYS comment.
1317 The data component points to the first element in the array. The
1318 offset field is the position of the origin of the array (i.e. element
1319 (0, 0 ...)). This may be outside the bounds of the array.
1321 An element is accessed by
1322 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1323 This gives good performance as the computation does not involve the
1324 bounds of the array. For packed arrays, this is optimized further
1325 by substituting the known strides.
1327 This system has one problem: all array bounds must be within 2^31
1328 elements of the origin (2^63 on 64-bit machines). For example
1329 integer, dimension (80000:90000, 80000:90000, 2) :: array
1330 may not work properly on 32-bit machines because 80000*80000 >
1331 2^31, so the calculation for stride2 would overflow. This may
1332 still work, but I haven't checked, and it relies on the overflow
1333 doing the right thing.
1335 The way to fix this problem is to access elements as follows:
1336 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1337 Obviously this is much slower. I will make this a compile time
1338 option, something like -fsmall-array-offsets. Mixing code compiled
1339 with and without this switch will work.
1341 (1) This can be worked around by modifying the upper bound of the
1342 previous dimension. This requires extra fields in the descriptor
1343 (both real_ubound and fake_ubound). */
1346 /* Returns true if the array sym does not require a descriptor. */
1349 gfc_is_nodesc_array (gfc_symbol * sym)
1351 symbol_attribute *array_attr;
1352 gfc_array_spec *as;
1353 bool is_classarray = IS_CLASS_ARRAY (sym);
1355 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1356 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1358 gcc_assert (array_attr->dimension || array_attr->codimension);
1360 /* We only want local arrays. */
1361 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1362 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1363 || array_attr->allocatable)
1364 return 0;
1366 /* We want a descriptor for associate-name arrays that do not have an
1367 explicitly known shape already. */
1368 if (sym->assoc && as->type != AS_EXPLICIT)
1369 return 0;
1371 /* The dummy is stored in sym and not in the component. */
1372 if (sym->attr.dummy)
1373 return as->type != AS_ASSUMED_SHAPE
1374 && as->type != AS_ASSUMED_RANK;
1376 if (sym->attr.result || sym->attr.function)
1377 return 0;
1379 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1381 return 1;
1385 /* Create an array descriptor type. */
1387 static tree
1388 gfc_build_array_type (tree type, gfc_array_spec * as,
1389 enum gfc_array_kind akind, bool restricted,
1390 bool contiguous, int codim)
1392 tree lbound[GFC_MAX_DIMENSIONS];
1393 tree ubound[GFC_MAX_DIMENSIONS];
1394 int n, corank;
1396 /* Assumed-shape arrays do not have codimension information stored in the
1397 descriptor. */
1398 corank = MAX (as->corank, codim);
1399 if (as->type == AS_ASSUMED_SHAPE ||
1400 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1401 corank = codim;
1403 if (as->type == AS_ASSUMED_RANK)
1404 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1406 lbound[n] = NULL_TREE;
1407 ubound[n] = NULL_TREE;
1410 for (n = 0; n < as->rank; n++)
1412 /* Create expressions for the known bounds of the array. */
1413 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1414 lbound[n] = gfc_index_one_node;
1415 else
1416 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1417 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1420 for (n = as->rank; n < as->rank + corank; n++)
1422 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1423 lbound[n] = gfc_index_one_node;
1424 else
1425 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1427 if (n < as->rank + corank - 1)
1428 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1431 if (as->type == AS_ASSUMED_SHAPE)
1432 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1433 : GFC_ARRAY_ASSUMED_SHAPE;
1434 else if (as->type == AS_ASSUMED_RANK)
1435 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1436 : GFC_ARRAY_ASSUMED_RANK;
1437 return gfc_get_array_type_bounds (type, as->rank == -1
1438 ? GFC_MAX_DIMENSIONS : as->rank,
1439 corank, lbound, ubound, 0, akind,
1440 restricted);
1443 /* Returns the struct descriptor_dimension type. */
1445 static tree
1446 gfc_get_desc_dim_type (void)
1448 tree type;
1449 tree decl, *chain = NULL;
1451 if (gfc_desc_dim_type)
1452 return gfc_desc_dim_type;
1454 /* Build the type node. */
1455 type = make_node (RECORD_TYPE);
1457 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1458 TYPE_PACKED (type) = 1;
1460 /* Consists of the stride, lbound and ubound members. */
1461 decl = gfc_add_field_to_struct_1 (type,
1462 get_identifier ("stride"),
1463 gfc_array_index_type, &chain);
1464 suppress_warning (decl);
1466 decl = gfc_add_field_to_struct_1 (type,
1467 get_identifier ("lbound"),
1468 gfc_array_index_type, &chain);
1469 suppress_warning (decl);
1471 decl = gfc_add_field_to_struct_1 (type,
1472 get_identifier ("ubound"),
1473 gfc_array_index_type, &chain);
1474 suppress_warning (decl);
1476 /* Finish off the type. */
1477 gfc_finish_type (type);
1478 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1480 gfc_desc_dim_type = type;
1481 return type;
1485 /* Return the DTYPE for an array. This describes the type and type parameters
1486 of the array. */
1487 /* TODO: Only call this when the value is actually used, and make all the
1488 unknown cases abort. */
1490 tree
1491 gfc_get_dtype_rank_type (int rank, tree etype)
1493 tree ptype;
1494 tree size;
1495 int n;
1496 tree tmp;
1497 tree dtype;
1498 tree field;
1499 vec<constructor_elt, va_gc> *v = NULL;
1501 ptype = etype;
1502 while (TREE_CODE (etype) == POINTER_TYPE
1503 || TREE_CODE (etype) == ARRAY_TYPE)
1505 ptype = etype;
1506 etype = TREE_TYPE (etype);
1509 gcc_assert (etype);
1511 switch (TREE_CODE (etype))
1513 case INTEGER_TYPE:
1514 if (TREE_CODE (ptype) == ARRAY_TYPE
1515 && TYPE_STRING_FLAG (ptype))
1516 n = BT_CHARACTER;
1517 else
1518 n = BT_INTEGER;
1519 break;
1521 case BOOLEAN_TYPE:
1522 n = BT_LOGICAL;
1523 break;
1525 case REAL_TYPE:
1526 n = BT_REAL;
1527 break;
1529 case COMPLEX_TYPE:
1530 n = BT_COMPLEX;
1531 break;
1533 case RECORD_TYPE:
1534 if (GFC_CLASS_TYPE_P (etype))
1535 n = BT_CLASS;
1536 else
1537 n = BT_DERIVED;
1538 break;
1540 case FUNCTION_TYPE:
1541 case VOID_TYPE:
1542 n = BT_VOID;
1543 break;
1545 default:
1546 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1547 /* We can encounter strange array types for temporary arrays. */
1548 gcc_unreachable ();
1551 switch (n)
1553 case BT_CHARACTER:
1554 gcc_assert (TREE_CODE (ptype) == ARRAY_TYPE);
1555 size = gfc_get_character_len_in_bytes (ptype);
1556 break;
1557 case BT_VOID:
1558 gcc_assert (TREE_CODE (ptype) == POINTER_TYPE);
1559 size = size_in_bytes (ptype);
1560 break;
1561 default:
1562 size = size_in_bytes (etype);
1563 break;
1566 gcc_assert (size);
1568 STRIP_NOPS (size);
1569 size = fold_convert (size_type_node, size);
1570 tmp = get_dtype_type_node ();
1571 field = gfc_advance_chain (TYPE_FIELDS (tmp),
1572 GFC_DTYPE_ELEM_LEN);
1573 CONSTRUCTOR_APPEND_ELT (v, field,
1574 fold_convert (TREE_TYPE (field), size));
1576 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1577 GFC_DTYPE_RANK);
1578 CONSTRUCTOR_APPEND_ELT (v, field,
1579 build_int_cst (TREE_TYPE (field), rank));
1581 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1582 GFC_DTYPE_TYPE);
1583 CONSTRUCTOR_APPEND_ELT (v, field,
1584 build_int_cst (TREE_TYPE (field), n));
1586 dtype = build_constructor (tmp, v);
1588 return dtype;
1592 tree
1593 gfc_get_dtype (tree type, int * rank)
1595 tree dtype;
1596 tree etype;
1597 int irnk;
1599 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1601 irnk = (rank) ? (*rank) : (GFC_TYPE_ARRAY_RANK (type));
1602 etype = gfc_get_element_type (type);
1603 dtype = gfc_get_dtype_rank_type (irnk, etype);
1605 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1606 return dtype;
1610 /* Build an array type for use without a descriptor, packed according
1611 to the value of PACKED. */
1613 tree
1614 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1615 bool restricted)
1617 tree range;
1618 tree type;
1619 tree tmp;
1620 int n;
1621 int known_stride;
1622 int known_offset;
1623 mpz_t offset;
1624 mpz_t stride;
1625 mpz_t delta;
1626 gfc_expr *expr;
1628 mpz_init_set_ui (offset, 0);
1629 mpz_init_set_ui (stride, 1);
1630 mpz_init (delta);
1632 /* We don't use build_array_type because this does not include
1633 lang-specific information (i.e. the bounds of the array) when checking
1634 for duplicates. */
1635 if (as->rank)
1636 type = make_node (ARRAY_TYPE);
1637 else
1638 type = build_variant_type_copy (etype);
1640 GFC_ARRAY_TYPE_P (type) = 1;
1641 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1643 known_stride = (packed != PACKED_NO);
1644 known_offset = 1;
1645 for (n = 0; n < as->rank; n++)
1647 /* Fill in the stride and bound components of the type. */
1648 if (known_stride)
1649 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1650 else
1651 tmp = NULL_TREE;
1652 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1654 expr = as->lower[n];
1655 if (expr && expr->expr_type == EXPR_CONSTANT)
1657 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1658 gfc_index_integer_kind);
1660 else
1662 known_stride = 0;
1663 tmp = NULL_TREE;
1665 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1667 if (known_stride)
1669 /* Calculate the offset. */
1670 mpz_mul (delta, stride, as->lower[n]->value.integer);
1671 mpz_sub (offset, offset, delta);
1673 else
1674 known_offset = 0;
1676 expr = as->upper[n];
1677 if (expr && expr->expr_type == EXPR_CONSTANT)
1679 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1680 gfc_index_integer_kind);
1682 else
1684 tmp = NULL_TREE;
1685 known_stride = 0;
1687 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1689 if (known_stride)
1691 /* Calculate the stride. */
1692 mpz_sub (delta, as->upper[n]->value.integer,
1693 as->lower[n]->value.integer);
1694 mpz_add_ui (delta, delta, 1);
1695 mpz_mul (stride, stride, delta);
1698 /* Only the first stride is known for partial packed arrays. */
1699 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1700 known_stride = 0;
1702 for (n = as->rank; n < as->rank + as->corank; n++)
1704 expr = as->lower[n];
1705 if (expr && expr->expr_type == EXPR_CONSTANT)
1706 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1707 gfc_index_integer_kind);
1708 else
1709 tmp = NULL_TREE;
1710 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1712 expr = as->upper[n];
1713 if (expr && expr->expr_type == EXPR_CONSTANT)
1714 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1715 gfc_index_integer_kind);
1716 else
1717 tmp = NULL_TREE;
1718 if (n < as->rank + as->corank - 1)
1719 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1722 if (known_offset)
1724 GFC_TYPE_ARRAY_OFFSET (type) =
1725 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1727 else
1728 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1730 if (known_stride)
1732 GFC_TYPE_ARRAY_SIZE (type) =
1733 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1735 else
1736 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1738 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1739 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1740 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1741 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1742 NULL_TREE);
1743 /* TODO: use main type if it is unbounded. */
1744 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1745 build_pointer_type (build_array_type (etype, range));
1746 if (restricted)
1747 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1748 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1749 TYPE_QUAL_RESTRICT);
1751 if (as->rank == 0)
1753 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1755 type = build_pointer_type (type);
1757 if (restricted)
1758 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1760 GFC_ARRAY_TYPE_P (type) = 1;
1761 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1764 return type;
1767 if (known_stride)
1769 mpz_sub_ui (stride, stride, 1);
1770 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1772 else
1773 range = NULL_TREE;
1775 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1776 TYPE_DOMAIN (type) = range;
1778 build_pointer_type (etype);
1779 TREE_TYPE (type) = etype;
1781 layout_type (type);
1783 mpz_clear (offset);
1784 mpz_clear (stride);
1785 mpz_clear (delta);
1787 /* Represent packed arrays as multi-dimensional if they have rank >
1788 1 and with proper bounds, instead of flat arrays. This makes for
1789 better debug info. */
1790 if (known_offset)
1792 tree gtype = etype, rtype, type_decl;
1794 for (n = as->rank - 1; n >= 0; n--)
1796 rtype = build_range_type (gfc_array_index_type,
1797 GFC_TYPE_ARRAY_LBOUND (type, n),
1798 GFC_TYPE_ARRAY_UBOUND (type, n));
1799 gtype = build_array_type (gtype, rtype);
1801 TYPE_NAME (type) = type_decl = build_decl (input_location,
1802 TYPE_DECL, NULL, gtype);
1803 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1806 if (packed != PACKED_STATIC || !known_stride
1807 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1809 /* For dummy arrays and automatic (heap allocated) arrays we
1810 want a pointer to the array. */
1811 type = build_pointer_type (type);
1812 if (restricted)
1813 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1814 GFC_ARRAY_TYPE_P (type) = 1;
1815 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1817 return type;
1821 /* Return or create the base type for an array descriptor. */
1823 static tree
1824 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1826 tree fat_type, decl, arraytype, *chain = NULL;
1827 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1828 int idx;
1830 /* Assumed-rank array. */
1831 if (dimen == -1)
1832 dimen = GFC_MAX_DIMENSIONS;
1834 idx = 2 * (codimen + dimen) + restricted;
1836 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1838 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1840 if (gfc_array_descriptor_base_caf[idx])
1841 return gfc_array_descriptor_base_caf[idx];
1843 else if (gfc_array_descriptor_base[idx])
1844 return gfc_array_descriptor_base[idx];
1846 /* Build the type node. */
1847 fat_type = make_node (RECORD_TYPE);
1849 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1850 TYPE_NAME (fat_type) = get_identifier (name);
1851 TYPE_NAMELESS (fat_type) = 1;
1853 /* Add the data member as the first element of the descriptor. */
1854 gfc_add_field_to_struct_1 (fat_type,
1855 get_identifier ("data"),
1856 (restricted
1857 ? prvoid_type_node
1858 : ptr_type_node), &chain);
1860 /* Add the base component. */
1861 decl = gfc_add_field_to_struct_1 (fat_type,
1862 get_identifier ("offset"),
1863 gfc_array_index_type, &chain);
1864 suppress_warning (decl);
1866 /* Add the dtype component. */
1867 decl = gfc_add_field_to_struct_1 (fat_type,
1868 get_identifier ("dtype"),
1869 get_dtype_type_node (), &chain);
1870 suppress_warning (decl);
1872 /* Add the span component. */
1873 decl = gfc_add_field_to_struct_1 (fat_type,
1874 get_identifier ("span"),
1875 gfc_array_index_type, &chain);
1876 suppress_warning (decl);
1878 /* Build the array type for the stride and bound components. */
1879 if (dimen + codimen > 0)
1881 arraytype =
1882 build_array_type (gfc_get_desc_dim_type (),
1883 build_range_type (gfc_array_index_type,
1884 gfc_index_zero_node,
1885 gfc_rank_cst[codimen + dimen - 1]));
1887 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1888 arraytype, &chain);
1889 suppress_warning (decl);
1892 if (flag_coarray == GFC_FCOARRAY_LIB)
1894 decl = gfc_add_field_to_struct_1 (fat_type,
1895 get_identifier ("token"),
1896 prvoid_type_node, &chain);
1897 suppress_warning (decl);
1900 /* Finish off the type. */
1901 gfc_finish_type (fat_type);
1902 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1904 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1905 gfc_array_descriptor_base_caf[idx] = fat_type;
1906 else
1907 gfc_array_descriptor_base[idx] = fat_type;
1909 return fat_type;
1913 /* Build an array (descriptor) type with given bounds. */
1915 tree
1916 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1917 tree * ubound, int packed,
1918 enum gfc_array_kind akind, bool restricted)
1920 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1921 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1922 const char *type_name;
1923 int n;
1925 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1926 fat_type = build_distinct_type_copy (base_type);
1927 /* Unshare TYPE_FIELDs. */
1928 for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
1930 tree next = DECL_CHAIN (*tp);
1931 *tp = copy_node (*tp);
1932 DECL_CONTEXT (*tp) = fat_type;
1933 DECL_CHAIN (*tp) = next;
1935 /* Make sure that nontarget and target array type have the same canonical
1936 type (and same stub decl for debug info). */
1937 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1938 TYPE_CANONICAL (fat_type) = base_type;
1939 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1940 /* Arrays of unknown type must alias with all array descriptors. */
1941 TYPE_TYPELESS_STORAGE (base_type) = 1;
1942 TYPE_TYPELESS_STORAGE (fat_type) = 1;
1943 gcc_checking_assert (!get_alias_set (base_type) && !get_alias_set (fat_type));
1945 tmp = etype;
1946 if (TREE_CODE (tmp) == ARRAY_TYPE
1947 && TYPE_STRING_FLAG (tmp))
1948 tmp = TREE_TYPE (etype);
1949 tmp = TYPE_NAME (tmp);
1950 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1951 tmp = DECL_NAME (tmp);
1952 if (tmp)
1953 type_name = IDENTIFIER_POINTER (tmp);
1954 else
1955 type_name = "unknown";
1956 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1957 GFC_MAX_SYMBOL_LEN, type_name);
1958 TYPE_NAME (fat_type) = get_identifier (name);
1959 TYPE_NAMELESS (fat_type) = 1;
1961 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1962 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1964 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1965 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1966 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1967 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1969 /* Build an array descriptor record type. */
1970 if (packed != 0)
1971 stride = gfc_index_one_node;
1972 else
1973 stride = NULL_TREE;
1974 for (n = 0; n < dimen + codimen; n++)
1976 if (n < dimen)
1977 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1979 if (lbound)
1980 lower = lbound[n];
1981 else
1982 lower = NULL_TREE;
1984 if (lower != NULL_TREE)
1986 if (INTEGER_CST_P (lower))
1987 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1988 else
1989 lower = NULL_TREE;
1992 if (codimen && n == dimen + codimen - 1)
1993 break;
1995 upper = ubound[n];
1996 if (upper != NULL_TREE)
1998 if (INTEGER_CST_P (upper))
1999 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
2000 else
2001 upper = NULL_TREE;
2004 if (n >= dimen)
2005 continue;
2007 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
2009 tmp = fold_build2_loc (input_location, MINUS_EXPR,
2010 gfc_array_index_type, upper, lower);
2011 tmp = fold_build2_loc (input_location, PLUS_EXPR,
2012 gfc_array_index_type, tmp,
2013 gfc_index_one_node);
2014 stride = fold_build2_loc (input_location, MULT_EXPR,
2015 gfc_array_index_type, tmp, stride);
2016 /* Check the folding worked. */
2017 gcc_assert (INTEGER_CST_P (stride));
2019 else
2020 stride = NULL_TREE;
2022 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
2024 /* TODO: known offsets for descriptors. */
2025 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
2027 if (dimen == 0)
2029 arraytype = build_pointer_type (etype);
2030 if (restricted)
2031 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2033 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2034 return fat_type;
2037 /* We define data as an array with the correct size if possible.
2038 Much better than doing pointer arithmetic. */
2039 if (stride)
2040 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
2041 int_const_binop (MINUS_EXPR, stride,
2042 build_int_cst (TREE_TYPE (stride), 1)));
2043 else
2044 rtype = gfc_array_range_type;
2045 arraytype = build_array_type (etype, rtype);
2046 arraytype = build_pointer_type (arraytype);
2047 if (restricted)
2048 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2049 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2051 /* This will generate the base declarations we need to emit debug
2052 information for this type. FIXME: there must be a better way to
2053 avoid divergence between compilations with and without debug
2054 information. */
2056 struct array_descr_info info;
2057 gfc_get_array_descr_info (fat_type, &info);
2058 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2061 return fat_type;
2064 /* Build a pointer type. This function is called from gfc_sym_type(). */
2066 static tree
2067 gfc_build_pointer_type (gfc_symbol * sym, tree type)
2069 /* Array pointer types aren't actually pointers. */
2070 if (sym->attr.dimension)
2071 return type;
2072 else
2073 return build_pointer_type (type);
2076 static tree gfc_nonrestricted_type (tree t);
2077 /* Given two record or union type nodes TO and FROM, ensure
2078 that all fields in FROM have a corresponding field in TO,
2079 their type being nonrestrict variants. This accepts a TO
2080 node that already has a prefix of the fields in FROM. */
2081 static void
2082 mirror_fields (tree to, tree from)
2084 tree fto, ffrom;
2085 tree *chain;
2087 /* Forward to the end of TOs fields. */
2088 fto = TYPE_FIELDS (to);
2089 ffrom = TYPE_FIELDS (from);
2090 chain = &TYPE_FIELDS (to);
2091 while (fto)
2093 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2094 chain = &DECL_CHAIN (fto);
2095 fto = DECL_CHAIN (fto);
2096 ffrom = DECL_CHAIN (ffrom);
2099 /* Now add all fields remaining in FROM (starting with ffrom). */
2100 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2102 tree newfield = copy_node (ffrom);
2103 DECL_CONTEXT (newfield) = to;
2104 /* The store to DECL_CHAIN might seem redundant with the
2105 stores to *chain, but not clearing it here would mean
2106 leaving a chain into the old fields. If ever
2107 our called functions would look at them confusion
2108 will arise. */
2109 DECL_CHAIN (newfield) = NULL_TREE;
2110 *chain = newfield;
2111 chain = &DECL_CHAIN (newfield);
2113 if (TREE_CODE (ffrom) == FIELD_DECL)
2115 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2116 TREE_TYPE (newfield) = elemtype;
2119 *chain = NULL_TREE;
2122 /* Given a type T, returns a different type of the same structure,
2123 except that all types it refers to (recursively) are always
2124 non-restrict qualified types. */
2125 static tree
2126 gfc_nonrestricted_type (tree t)
2128 tree ret = t;
2130 /* If the type isn't laid out yet, don't copy it. If something
2131 needs it for real it should wait until the type got finished. */
2132 if (!TYPE_SIZE (t))
2133 return t;
2135 if (!TYPE_LANG_SPECIFIC (t))
2136 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2137 /* If we're dealing with this very node already further up
2138 the call chain (recursion via pointers and struct members)
2139 we haven't yet determined if we really need a new type node.
2140 Assume we don't, return T itself. */
2141 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2142 return t;
2144 /* If we have calculated this all already, just return it. */
2145 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2146 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2148 /* Mark this type. */
2149 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2151 switch (TREE_CODE (t))
2153 default:
2154 break;
2156 case POINTER_TYPE:
2157 case REFERENCE_TYPE:
2159 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2160 if (totype == TREE_TYPE (t))
2161 ret = t;
2162 else if (TREE_CODE (t) == POINTER_TYPE)
2163 ret = build_pointer_type (totype);
2164 else
2165 ret = build_reference_type (totype);
2166 ret = build_qualified_type (ret,
2167 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2169 break;
2171 case ARRAY_TYPE:
2173 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2174 if (elemtype == TREE_TYPE (t))
2175 ret = t;
2176 else
2178 ret = build_variant_type_copy (t);
2179 TREE_TYPE (ret) = elemtype;
2180 if (TYPE_LANG_SPECIFIC (t)
2181 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2183 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2184 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2185 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2187 TYPE_LANG_SPECIFIC (ret)
2188 = ggc_cleared_alloc<struct lang_type> ();
2189 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2190 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2195 break;
2197 case RECORD_TYPE:
2198 case UNION_TYPE:
2199 case QUAL_UNION_TYPE:
2201 tree field;
2202 /* First determine if we need a new type at all.
2203 Careful, the two calls to gfc_nonrestricted_type per field
2204 might return different values. That happens exactly when
2205 one of the fields reaches back to this very record type
2206 (via pointers). The first calls will assume that we don't
2207 need to copy T (see the error_mark_node marking). If there
2208 are any reasons for copying T apart from having to copy T,
2209 we'll indeed copy it, and the second calls to
2210 gfc_nonrestricted_type will use that new node if they
2211 reach back to T. */
2212 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2213 if (TREE_CODE (field) == FIELD_DECL)
2215 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2216 if (elemtype != TREE_TYPE (field))
2217 break;
2219 if (!field)
2220 break;
2221 ret = build_variant_type_copy (t);
2222 TYPE_FIELDS (ret) = NULL_TREE;
2224 /* Here we make sure that as soon as we know we have to copy
2225 T, that also fields reaching back to us will use the new
2226 copy. It's okay if that copy still contains the old fields,
2227 we won't look at them. */
2228 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2229 mirror_fields (ret, t);
2231 break;
2234 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2235 return ret;
2239 /* Return the type for a symbol. Special handling is required for character
2240 types to get the correct level of indirection.
2241 For functions return the return type.
2242 For subroutines return void_type_node.
2243 Calling this multiple times for the same symbol should be avoided,
2244 especially for character and array types. */
2246 tree
2247 gfc_sym_type (gfc_symbol * sym)
2249 tree type;
2250 int byref;
2251 bool restricted;
2253 /* Procedure Pointers inside COMMON blocks. */
2254 if (sym->attr.proc_pointer && sym->attr.in_common)
2256 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2257 sym->attr.proc_pointer = 0;
2258 type = build_pointer_type (gfc_get_function_type (sym));
2259 sym->attr.proc_pointer = 1;
2260 return type;
2263 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2264 return void_type_node;
2266 /* In the case of a function the fake result variable may have a
2267 type different from the function type, so don't return early in
2268 that case. */
2269 if (sym->backend_decl && !sym->attr.function)
2270 return TREE_TYPE (sym->backend_decl);
2272 if (sym->attr.result
2273 && sym->ts.type == BT_CHARACTER
2274 && sym->ts.u.cl->backend_decl == NULL_TREE
2275 && sym->ns->proc_name
2276 && sym->ns->proc_name->ts.u.cl
2277 && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2278 sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2280 if (sym->ts.type == BT_CHARACTER
2281 && ((sym->attr.function && sym->attr.is_bind_c)
2282 || (sym->attr.result
2283 && sym->ns->proc_name
2284 && sym->ns->proc_name->attr.is_bind_c)
2285 || (sym->ts.deferred && (!sym->ts.u.cl
2286 || !sym->ts.u.cl->backend_decl))))
2287 type = gfc_character1_type_node;
2288 else
2289 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2291 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value
2292 && !sym->pass_as_value)
2293 byref = 1;
2294 else
2295 byref = 0;
2297 restricted = !sym->attr.target && !sym->attr.pointer
2298 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2299 if (!restricted)
2300 type = gfc_nonrestricted_type (type);
2302 if (sym->attr.dimension || sym->attr.codimension)
2304 if (gfc_is_nodesc_array (sym))
2306 /* If this is a character argument of unknown length, just use the
2307 base type. */
2308 if (sym->ts.type != BT_CHARACTER
2309 || !(sym->attr.dummy || sym->attr.function)
2310 || sym->ts.u.cl->backend_decl)
2312 type = gfc_get_nodesc_array_type (type, sym->as,
2313 byref ? PACKED_FULL
2314 : PACKED_STATIC,
2315 restricted);
2316 byref = 0;
2319 else
2321 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2322 if (sym->attr.pointer)
2323 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2324 : GFC_ARRAY_POINTER;
2325 else if (sym->attr.allocatable)
2326 akind = GFC_ARRAY_ALLOCATABLE;
2327 type = gfc_build_array_type (type, sym->as, akind, restricted,
2328 sym->attr.contiguous, false);
2331 else
2333 if (sym->attr.allocatable || sym->attr.pointer
2334 || gfc_is_associate_pointer (sym))
2335 type = gfc_build_pointer_type (sym, type);
2338 /* We currently pass all parameters by reference.
2339 See f95_get_function_decl. For dummy function parameters return the
2340 function type. */
2341 if (byref)
2343 /* We must use pointer types for potentially absent variables. The
2344 optimizers assume a reference type argument is never NULL. */
2345 if ((sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.optional)
2346 || sym->attr.optional
2347 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2348 type = build_pointer_type (type);
2349 else
2351 type = build_reference_type (type);
2352 if (restricted)
2353 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2357 return (type);
2360 /* Layout and output debug info for a record type. */
2362 void
2363 gfc_finish_type (tree type)
2365 tree decl;
2367 decl = build_decl (input_location,
2368 TYPE_DECL, NULL_TREE, type);
2369 TYPE_STUB_DECL (type) = decl;
2370 layout_type (type);
2371 rest_of_type_compilation (type, 1);
2372 rest_of_decl_compilation (decl, 1, 0);
2375 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2376 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2377 to the end of the field list pointed to by *CHAIN.
2379 Returns a pointer to the new field. */
2381 static tree
2382 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2384 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2386 DECL_CONTEXT (decl) = context;
2387 DECL_CHAIN (decl) = NULL_TREE;
2388 if (TYPE_FIELDS (context) == NULL_TREE)
2389 TYPE_FIELDS (context) = decl;
2390 if (chain != NULL)
2392 if (*chain != NULL)
2393 **chain = decl;
2394 *chain = &DECL_CHAIN (decl);
2397 return decl;
2400 /* Like `gfc_add_field_to_struct_1', but adds alignment
2401 information. */
2403 tree
2404 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2406 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2408 DECL_INITIAL (decl) = 0;
2409 SET_DECL_ALIGN (decl, 0);
2410 DECL_USER_ALIGN (decl) = 0;
2412 return decl;
2416 /* Copy the backend_decl and component backend_decls if
2417 the two derived type symbols are "equal", as described
2418 in 4.4.2 and resolved by gfc_compare_derived_types. */
2421 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2422 bool from_gsym)
2424 gfc_component *to_cm;
2425 gfc_component *from_cm;
2427 if (from == to)
2428 return 1;
2430 if (from->backend_decl == NULL
2431 || !gfc_compare_derived_types (from, to))
2432 return 0;
2434 to->backend_decl = from->backend_decl;
2436 to_cm = to->components;
2437 from_cm = from->components;
2439 /* Copy the component declarations. If a component is itself
2440 a derived type, we need a copy of its component declarations.
2441 This is done by recursing into gfc_get_derived_type and
2442 ensures that the component's component declarations have
2443 been built. If it is a character, we need the character
2444 length, as well. */
2445 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2447 to_cm->backend_decl = from_cm->backend_decl;
2448 to_cm->caf_token = from_cm->caf_token;
2449 if (from_cm->ts.type == BT_UNION)
2450 gfc_get_union_type (to_cm->ts.u.derived);
2451 else if (from_cm->ts.type == BT_DERIVED
2452 && (!from_cm->attr.pointer || from_gsym))
2453 gfc_get_derived_type (to_cm->ts.u.derived);
2454 else if (from_cm->ts.type == BT_CLASS
2455 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2456 gfc_get_derived_type (to_cm->ts.u.derived);
2457 else if (from_cm->ts.type == BT_CHARACTER)
2458 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2461 return 1;
2465 /* Build a tree node for a procedure pointer component. */
2467 tree
2468 gfc_get_ppc_type (gfc_component* c)
2470 tree t;
2472 /* Explicit interface. */
2473 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2474 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2476 /* Implicit interface (only return value may be known). */
2477 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2478 t = gfc_typenode_for_spec (&c->ts);
2479 else
2480 t = void_type_node;
2482 /* FIXME: it would be better to provide explicit interfaces in all
2483 cases, since they should be known by the compiler. */
2484 return build_pointer_type (build_function_type (t, NULL_TREE));
2488 /* Build a tree node for a union type. Requires building each map
2489 structure which is an element of the union. */
2491 tree
2492 gfc_get_union_type (gfc_symbol *un)
2494 gfc_component *map = NULL;
2495 tree typenode = NULL, map_type = NULL, map_field = NULL;
2496 tree *chain = NULL;
2498 if (un->backend_decl)
2500 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2501 return un->backend_decl;
2502 else
2503 typenode = un->backend_decl;
2505 else
2507 typenode = make_node (UNION_TYPE);
2508 TYPE_NAME (typenode) = get_identifier (un->name);
2511 /* Add each contained MAP as a field. */
2512 for (map = un->components; map; map = map->next)
2514 gcc_assert (map->ts.type == BT_DERIVED);
2516 /* The map's type node, which is defined within this union's context. */
2517 map_type = gfc_get_derived_type (map->ts.u.derived);
2518 TYPE_CONTEXT (map_type) = typenode;
2520 /* The map field's declaration. */
2521 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2522 map_type, &chain);
2523 if (map->loc.lb)
2524 gfc_set_decl_location (map_field, &map->loc);
2525 else if (un->declared_at.lb)
2526 gfc_set_decl_location (map_field, &un->declared_at);
2528 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2529 DECL_NAMELESS(map_field) = true;
2531 /* We should never clobber another backend declaration for this map,
2532 because each map component is unique. */
2533 if (!map->backend_decl)
2534 map->backend_decl = map_field;
2537 un->backend_decl = typenode;
2538 gfc_finish_type (typenode);
2540 return typenode;
2544 /* Build a tree node for a derived type. If there are equal
2545 derived types, with different local names, these are built
2546 at the same time. If an equal derived type has been built
2547 in a parent namespace, this is used. */
2549 tree
2550 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2552 tree typenode = NULL, field = NULL, field_type = NULL;
2553 tree canonical = NULL_TREE;
2554 tree *chain = NULL;
2555 bool got_canonical = false;
2556 bool unlimited_entity = false;
2557 gfc_component *c;
2558 gfc_namespace *ns;
2559 tree tmp;
2560 bool coarray_flag;
2562 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2563 && derived->module && !derived->attr.vtype;
2565 gcc_assert (!derived->attr.pdt_template);
2567 if (derived->attr.unlimited_polymorphic
2568 || (flag_coarray == GFC_FCOARRAY_LIB
2569 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2570 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2571 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2572 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2573 return ptr_type_node;
2575 if (flag_coarray != GFC_FCOARRAY_LIB
2576 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2577 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2578 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2579 return gfc_get_int_type (gfc_default_integer_kind);
2581 if (derived && derived->attr.flavor == FL_PROCEDURE
2582 && derived->attr.generic)
2583 derived = gfc_find_dt_in_generic (derived);
2585 /* See if it's one of the iso_c_binding derived types. */
2586 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2588 if (derived->backend_decl)
2589 return derived->backend_decl;
2591 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2592 derived->backend_decl = ptr_type_node;
2593 else
2594 derived->backend_decl = pfunc_type_node;
2596 derived->ts.kind = gfc_index_integer_kind;
2597 derived->ts.type = BT_INTEGER;
2598 /* Set the f90_type to BT_VOID as a way to recognize something of type
2599 BT_INTEGER that needs to fit a void * for the purpose of the
2600 iso_c_binding derived types. */
2601 derived->ts.f90_type = BT_VOID;
2603 return derived->backend_decl;
2606 /* If use associated, use the module type for this one. */
2607 if (derived->backend_decl == NULL
2608 && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2609 && derived->module
2610 && gfc_get_module_backend_decl (derived))
2611 goto copy_derived_types;
2613 /* The derived types from an earlier namespace can be used as the
2614 canonical type. */
2615 if (derived->backend_decl == NULL
2616 && !derived->attr.use_assoc
2617 && !derived->attr.used_in_submodule
2618 && gfc_global_ns_list)
2620 for (ns = gfc_global_ns_list;
2621 ns->translated && !got_canonical;
2622 ns = ns->sibling)
2624 if (ns->derived_types)
2626 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2627 dt = dt->dt_next)
2629 gfc_copy_dt_decls_ifequal (dt, derived, true);
2630 if (derived->backend_decl)
2631 got_canonical = true;
2632 if (dt->dt_next == ns->derived_types)
2633 break;
2639 /* Store up the canonical type to be added to this one. */
2640 if (got_canonical)
2642 if (TYPE_CANONICAL (derived->backend_decl))
2643 canonical = TYPE_CANONICAL (derived->backend_decl);
2644 else
2645 canonical = derived->backend_decl;
2647 derived->backend_decl = NULL_TREE;
2650 /* derived->backend_decl != 0 means we saw it before, but its
2651 components' backend_decl may have not been built. */
2652 if (derived->backend_decl)
2654 /* Its components' backend_decl have been built or we are
2655 seeing recursion through the formal arglist of a procedure
2656 pointer component. */
2657 if (TYPE_FIELDS (derived->backend_decl))
2658 return derived->backend_decl;
2659 else if (derived->attr.abstract
2660 && derived->attr.proc_pointer_comp)
2662 /* If an abstract derived type with procedure pointer
2663 components has no other type of component, return the
2664 backend_decl. Otherwise build the components if any of the
2665 non-procedure pointer components have no backend_decl. */
2666 for (c = derived->components; c; c = c->next)
2668 bool same_alloc_type = c->attr.allocatable
2669 && derived == c->ts.u.derived;
2670 if (!c->attr.proc_pointer
2671 && !same_alloc_type
2672 && c->backend_decl == NULL)
2673 break;
2674 else if (c->next == NULL)
2675 return derived->backend_decl;
2677 typenode = derived->backend_decl;
2679 else
2680 typenode = derived->backend_decl;
2682 else
2684 /* We see this derived type first time, so build the type node. */
2685 typenode = make_node (RECORD_TYPE);
2686 TYPE_NAME (typenode) = get_identifier (derived->name);
2687 TYPE_PACKED (typenode) = flag_pack_derived;
2688 derived->backend_decl = typenode;
2691 if (derived->components
2692 && derived->components->ts.type == BT_DERIVED
2693 && strcmp (derived->components->name, "_data") == 0
2694 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2695 unlimited_entity = true;
2697 /* Go through the derived type components, building them as
2698 necessary. The reason for doing this now is that it is
2699 possible to recurse back to this derived type through a
2700 pointer component (PR24092). If this happens, the fields
2701 will be built and so we can return the type. */
2702 for (c = derived->components; c; c = c->next)
2704 bool same_alloc_type = c->attr.allocatable
2705 && derived == c->ts.u.derived;
2707 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2708 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2710 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2711 continue;
2713 if ((!c->attr.pointer && !c->attr.proc_pointer
2714 && !same_alloc_type)
2715 || c->ts.u.derived->backend_decl == NULL)
2717 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2718 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2719 local_codim);
2722 if (c->ts.u.derived->attr.is_iso_c)
2724 /* Need to copy the modified ts from the derived type. The
2725 typespec was modified because C_PTR/C_FUNPTR are translated
2726 into (void *) from derived types. */
2727 c->ts.type = c->ts.u.derived->ts.type;
2728 c->ts.kind = c->ts.u.derived->ts.kind;
2729 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2730 if (c->initializer)
2732 c->initializer->ts.type = c->ts.type;
2733 c->initializer->ts.kind = c->ts.kind;
2734 c->initializer->ts.f90_type = c->ts.f90_type;
2735 c->initializer->expr_type = EXPR_NULL;
2740 if (TYPE_FIELDS (derived->backend_decl))
2741 return derived->backend_decl;
2743 /* Build the type member list. Install the newly created RECORD_TYPE
2744 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2745 through only the top-level linked list of components so we correctly
2746 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2747 types are built as part of gfc_get_union_type. */
2748 for (c = derived->components; c; c = c->next)
2750 bool same_alloc_type = c->attr.allocatable
2751 && derived == c->ts.u.derived;
2752 /* Prevent infinite recursion, when the procedure pointer type is
2753 the same as derived, by forcing the procedure pointer component to
2754 be built as if the explicit interface does not exist. */
2755 if (c->attr.proc_pointer
2756 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2757 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2758 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2759 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2760 field_type = gfc_get_ppc_type (c);
2761 else if (c->attr.proc_pointer && derived->backend_decl)
2763 tmp = build_function_type (derived->backend_decl, NULL_TREE);
2764 field_type = build_pointer_type (tmp);
2766 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2767 field_type = c->ts.u.derived->backend_decl;
2768 else if (c->attr.caf_token)
2769 field_type = pvoid_type_node;
2770 else
2772 if (c->ts.type == BT_CHARACTER
2773 && !c->ts.deferred && !c->attr.pdt_string)
2775 /* Evaluate the string length. */
2776 gfc_conv_const_charlen (c->ts.u.cl);
2777 gcc_assert (c->ts.u.cl->backend_decl);
2779 else if (c->ts.type == BT_CHARACTER)
2780 c->ts.u.cl->backend_decl
2781 = build_int_cst (gfc_charlen_type_node, 0);
2783 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2786 /* This returns an array descriptor type. Initialization may be
2787 required. */
2788 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2790 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2792 enum gfc_array_kind akind;
2793 if (c->attr.pointer)
2794 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2795 : GFC_ARRAY_POINTER;
2796 else
2797 akind = GFC_ARRAY_ALLOCATABLE;
2798 /* Pointers to arrays aren't actually pointer types. The
2799 descriptors are separate, but the data is common. */
2800 field_type = gfc_build_array_type (field_type, c->as, akind,
2801 !c->attr.target
2802 && !c->attr.pointer,
2803 c->attr.contiguous,
2804 codimen);
2806 else
2807 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2808 PACKED_STATIC,
2809 !c->attr.target);
2811 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2812 && !c->attr.proc_pointer
2813 && !(unlimited_entity && c == derived->components))
2814 field_type = build_pointer_type (field_type);
2816 if (c->attr.pointer || same_alloc_type)
2817 field_type = gfc_nonrestricted_type (field_type);
2819 /* vtype fields can point to different types to the base type. */
2820 if (c->ts.type == BT_DERIVED
2821 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2822 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2823 ptr_mode, true);
2825 /* Ensure that the CLASS language specific flag is set. */
2826 if (c->ts.type == BT_CLASS)
2828 if (POINTER_TYPE_P (field_type))
2829 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2830 else
2831 GFC_CLASS_TYPE_P (field_type) = 1;
2834 field = gfc_add_field_to_struct (typenode,
2835 get_identifier (c->name),
2836 field_type, &chain);
2837 if (c->loc.lb)
2838 gfc_set_decl_location (field, &c->loc);
2839 else if (derived->declared_at.lb)
2840 gfc_set_decl_location (field, &derived->declared_at);
2842 gfc_finish_decl_attrs (field, &c->attr);
2844 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2846 gcc_assert (field);
2847 if (!c->backend_decl)
2848 c->backend_decl = field;
2850 if (c->attr.pointer && c->attr.dimension
2851 && !(c->ts.type == BT_DERIVED
2852 && strcmp (c->name, "_data") == 0))
2853 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2856 /* Now lay out the derived type, including the fields. */
2857 if (canonical)
2858 TYPE_CANONICAL (typenode) = canonical;
2860 gfc_finish_type (typenode);
2861 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2862 if (derived->module && derived->ns->proc_name
2863 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2865 if (derived->ns->proc_name->backend_decl
2866 && TREE_CODE (derived->ns->proc_name->backend_decl)
2867 == NAMESPACE_DECL)
2869 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2870 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2871 = derived->ns->proc_name->backend_decl;
2875 derived->backend_decl = typenode;
2877 copy_derived_types:
2879 for (c = derived->components; c; c = c->next)
2881 /* Do not add a caf_token field for class container components. */
2882 if ((codimen || coarray_flag)
2883 && !c->attr.dimension && !c->attr.codimension
2884 && (c->attr.allocatable || c->attr.pointer)
2885 && !derived->attr.is_class)
2887 /* Provide sufficient space to hold "_caf_symbol". */
2888 char caf_name[GFC_MAX_SYMBOL_LEN + 6];
2889 gfc_component *token;
2890 snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
2891 token = gfc_find_component (derived, caf_name, true, true, NULL);
2892 gcc_assert (token);
2893 c->caf_token = token->backend_decl;
2894 suppress_warning (c->caf_token);
2898 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
2900 gfc_copy_dt_decls_ifequal (derived, dt, false);
2901 if (dt->dt_next == gfc_derived_types)
2902 break;
2905 return derived->backend_decl;
2910 gfc_return_by_reference (gfc_symbol * sym)
2912 if (!sym->attr.function)
2913 return 0;
2915 if (sym->attr.dimension)
2916 return 1;
2918 if (sym->ts.type == BT_CHARACTER
2919 && !sym->attr.is_bind_c
2920 && (!sym->attr.result
2921 || !sym->ns->proc_name
2922 || !sym->ns->proc_name->attr.is_bind_c))
2923 return 1;
2925 /* Possibly return complex numbers by reference for g77 compatibility.
2926 We don't do this for calls to intrinsics (as the library uses the
2927 -fno-f2c calling convention), nor for calls to functions which always
2928 require an explicit interface, as no compatibility problems can
2929 arise there. */
2930 if (flag_f2c && sym->ts.type == BT_COMPLEX
2931 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2932 return 1;
2934 return 0;
2937 static tree
2938 gfc_get_mixed_entry_union (gfc_namespace *ns)
2940 tree type;
2941 tree *chain = NULL;
2942 char name[GFC_MAX_SYMBOL_LEN + 1];
2943 gfc_entry_list *el, *el2;
2945 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2946 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2948 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2950 /* Build the type node. */
2951 type = make_node (UNION_TYPE);
2953 TYPE_NAME (type) = get_identifier (name);
2955 for (el = ns->entries; el; el = el->next)
2957 /* Search for duplicates. */
2958 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2959 if (el2->sym->result == el->sym->result)
2960 break;
2962 if (el == el2)
2963 gfc_add_field_to_struct_1 (type,
2964 get_identifier (el->sym->result->name),
2965 gfc_sym_type (el->sym->result), &chain);
2968 /* Finish off the type. */
2969 gfc_finish_type (type);
2970 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2971 return type;
2974 /* Create a "fn spec" based on the formal arguments;
2975 cf. create_function_arglist. */
2977 static tree
2978 create_fn_spec (gfc_symbol *sym, tree fntype)
2980 char spec[150];
2981 size_t spec_len;
2982 gfc_formal_arglist *f;
2983 tree tmp;
2985 memset (&spec, 0, sizeof (spec));
2986 spec[0] = '.';
2987 spec[1] = ' ';
2988 spec_len = 2;
2990 if (sym->attr.entry_master)
2992 spec[spec_len++] = 'R';
2993 spec[spec_len++] = ' ';
2995 if (gfc_return_by_reference (sym))
2997 gfc_symbol *result = sym->result ? sym->result : sym;
2999 if (result->attr.pointer || sym->attr.proc_pointer)
3001 spec[spec_len++] = '.';
3002 spec[spec_len++] = ' ';
3004 else
3006 spec[spec_len++] = 'w';
3007 spec[spec_len++] = ' ';
3009 if (sym->ts.type == BT_CHARACTER)
3011 spec[spec_len++] = 'R';
3012 spec[spec_len++] = ' ';
3016 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3017 if (spec_len < sizeof (spec))
3019 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
3020 || f->sym->attr.external || f->sym->attr.cray_pointer
3021 || (f->sym->ts.type == BT_DERIVED
3022 && (f->sym->ts.u.derived->attr.proc_pointer_comp
3023 || f->sym->ts.u.derived->attr.pointer_comp))
3024 || (f->sym->ts.type == BT_CLASS
3025 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3026 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3027 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3029 spec[spec_len++] = '.';
3030 spec[spec_len++] = ' ';
3032 else if (f->sym->attr.intent == INTENT_IN)
3034 spec[spec_len++] = 'r';
3035 spec[spec_len++] = ' ';
3037 else if (f->sym)
3039 spec[spec_len++] = 'w';
3040 spec[spec_len++] = ' ';
3044 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3045 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3046 return build_type_attribute_variant (fntype, tmp);
3050 /* NOTE: The returned function type must match the argument list created by
3051 create_function_arglist. */
3053 tree
3054 gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3055 const char *fnspec)
3057 tree type;
3058 vec<tree, va_gc> *typelist = NULL;
3059 gfc_formal_arglist *f;
3060 gfc_symbol *arg;
3061 int alternate_return = 0;
3062 bool is_varargs = true;
3064 /* Make sure this symbol is a function, a subroutine or the main
3065 program. */
3066 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3067 || sym->attr.flavor == FL_PROGRAM);
3069 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3070 so that they can be detected here and handled further down. */
3071 if (sym->backend_decl == NULL)
3072 sym->backend_decl = error_mark_node;
3073 else if (sym->backend_decl == error_mark_node)
3074 goto arg_type_list_done;
3075 else if (sym->attr.proc_pointer)
3076 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3077 else
3078 return TREE_TYPE (sym->backend_decl);
3080 if (sym->attr.entry_master)
3081 /* Additional parameter for selecting an entry point. */
3082 vec_safe_push (typelist, gfc_array_index_type);
3084 if (sym->result)
3085 arg = sym->result;
3086 else
3087 arg = sym;
3089 if (arg->ts.type == BT_CHARACTER)
3090 gfc_conv_const_charlen (arg->ts.u.cl);
3092 /* Some functions we use an extra parameter for the return value. */
3093 if (gfc_return_by_reference (sym))
3095 type = gfc_sym_type (arg);
3096 if (arg->ts.type == BT_COMPLEX
3097 || arg->attr.dimension
3098 || arg->ts.type == BT_CHARACTER)
3099 type = build_reference_type (type);
3101 vec_safe_push (typelist, type);
3102 if (arg->ts.type == BT_CHARACTER)
3104 if (!arg->ts.deferred)
3105 /* Transfer by value. */
3106 vec_safe_push (typelist, gfc_charlen_type_node);
3107 else
3108 /* Deferred character lengths are transferred by reference
3109 so that the value can be returned. */
3110 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3113 if (sym->backend_decl == error_mark_node && actual_args != NULL
3114 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3115 || sym->attr.proc == PROC_UNKNOWN))
3116 gfc_get_formal_from_actual_arglist (sym, actual_args);
3118 /* Build the argument types for the function. */
3119 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3121 arg = f->sym;
3122 if (arg)
3124 /* Evaluate constant character lengths here so that they can be
3125 included in the type. */
3126 if (arg->ts.type == BT_CHARACTER)
3127 gfc_conv_const_charlen (arg->ts.u.cl);
3129 if (arg->attr.flavor == FL_PROCEDURE)
3131 type = gfc_get_function_type (arg);
3132 type = build_pointer_type (type);
3134 else
3135 type = gfc_sym_type (arg);
3137 /* Parameter Passing Convention
3139 We currently pass all parameters by reference.
3140 Parameters with INTENT(IN) could be passed by value.
3141 The problem arises if a function is called via an implicit
3142 prototype. In this situation the INTENT is not known.
3143 For this reason all parameters to global functions must be
3144 passed by reference. Passing by value would potentially
3145 generate bad code. Worse there would be no way of telling that
3146 this code was bad, except that it would give incorrect results.
3148 Contained procedures could pass by value as these are never
3149 used without an explicit interface, and cannot be passed as
3150 actual parameters for a dummy procedure. */
3152 vec_safe_push (typelist, type);
3154 else
3156 if (sym->attr.subroutine)
3157 alternate_return = 1;
3161 /* Add hidden arguments. */
3162 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3164 arg = f->sym;
3165 /* Add hidden string length parameters. */
3166 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3168 if (!arg->ts.deferred)
3169 /* Transfer by value. */
3170 type = gfc_charlen_type_node;
3171 else
3172 /* Deferred character lengths are transferred by reference
3173 so that the value can be returned. */
3174 type = build_pointer_type (gfc_charlen_type_node);
3176 vec_safe_push (typelist, type);
3178 /* For noncharacter scalar intrinsic types, VALUE passes the value,
3179 hence, the optional status cannot be transferred via a NULL pointer.
3180 Thus, we will use a hidden argument in that case. */
3181 else if (arg
3182 && arg->attr.optional
3183 && arg->attr.value
3184 && !arg->attr.dimension
3185 && arg->ts.type != BT_CLASS
3186 && !gfc_bt_struct (arg->ts.type))
3187 vec_safe_push (typelist, boolean_type_node);
3188 /* Coarrays which are descriptorless or assumed-shape pass with
3189 -fcoarray=lib the token and the offset as hidden arguments. */
3190 if (arg
3191 && flag_coarray == GFC_FCOARRAY_LIB
3192 && ((arg->ts.type != BT_CLASS
3193 && arg->attr.codimension
3194 && !arg->attr.allocatable)
3195 || (arg->ts.type == BT_CLASS
3196 && CLASS_DATA (arg)->attr.codimension
3197 && !CLASS_DATA (arg)->attr.allocatable)))
3199 vec_safe_push (typelist, pvoid_type_node); /* caf_token. */
3200 vec_safe_push (typelist, gfc_array_index_type); /* caf_offset. */
3204 if (!vec_safe_is_empty (typelist)
3205 || sym->attr.is_main_program
3206 || sym->attr.if_source != IFSRC_UNKNOWN)
3207 is_varargs = false;
3209 if (sym->backend_decl == error_mark_node)
3210 sym->backend_decl = NULL_TREE;
3212 arg_type_list_done:
3214 if (alternate_return)
3215 type = integer_type_node;
3216 else if (!sym->attr.function || gfc_return_by_reference (sym))
3217 type = void_type_node;
3218 else if (sym->attr.mixed_entry_master)
3219 type = gfc_get_mixed_entry_union (sym->ns);
3220 else if (flag_f2c && sym->ts.type == BT_REAL
3221 && sym->ts.kind == gfc_default_real_kind
3222 && !sym->attr.always_explicit)
3224 /* Special case: f2c calling conventions require that (scalar)
3225 default REAL functions return the C type double instead. f2c
3226 compatibility is only an issue with functions that don't
3227 require an explicit interface, as only these could be
3228 implemented in Fortran 77. */
3229 sym->ts.kind = gfc_default_double_kind;
3230 type = gfc_typenode_for_spec (&sym->ts);
3231 sym->ts.kind = gfc_default_real_kind;
3233 else if (sym->result && sym->result->attr.proc_pointer)
3234 /* Procedure pointer return values. */
3236 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3238 /* Unset proc_pointer as gfc_get_function_type
3239 is called recursively. */
3240 sym->result->attr.proc_pointer = 0;
3241 type = build_pointer_type (gfc_get_function_type (sym->result));
3242 sym->result->attr.proc_pointer = 1;
3244 else
3245 type = gfc_sym_type (sym->result);
3247 else
3248 type = gfc_sym_type (sym);
3250 if (is_varargs)
3251 type = build_varargs_function_type_vec (type, typelist);
3252 else
3253 type = build_function_type_vec (type, typelist);
3255 /* If we were passed an fn spec, add it here, otherwise determine it from
3256 the formal arguments. */
3257 if (fnspec)
3259 tree tmp;
3260 int spec_len = strlen (fnspec);
3261 tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3262 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3263 type = build_type_attribute_variant (type, tmp);
3265 else
3266 type = create_fn_spec (sym, type);
3268 return type;
3271 /* Language hooks for middle-end access to type nodes. */
3273 /* Return an integer type with BITS bits of precision,
3274 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3276 tree
3277 gfc_type_for_size (unsigned bits, int unsignedp)
3279 if (!unsignedp)
3281 int i;
3282 for (i = 0; i <= MAX_INT_KINDS; ++i)
3284 tree type = gfc_integer_types[i];
3285 if (type && bits == TYPE_PRECISION (type))
3286 return type;
3289 /* Handle TImode as a special case because it is used by some backends
3290 (e.g. ARM) even though it is not available for normal use. */
3291 #if HOST_BITS_PER_WIDE_INT >= 64
3292 if (bits == TYPE_PRECISION (intTI_type_node))
3293 return intTI_type_node;
3294 #endif
3296 if (bits <= TYPE_PRECISION (intQI_type_node))
3297 return intQI_type_node;
3298 if (bits <= TYPE_PRECISION (intHI_type_node))
3299 return intHI_type_node;
3300 if (bits <= TYPE_PRECISION (intSI_type_node))
3301 return intSI_type_node;
3302 if (bits <= TYPE_PRECISION (intDI_type_node))
3303 return intDI_type_node;
3304 if (bits <= TYPE_PRECISION (intTI_type_node))
3305 return intTI_type_node;
3307 else
3309 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3310 return unsigned_intQI_type_node;
3311 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3312 return unsigned_intHI_type_node;
3313 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3314 return unsigned_intSI_type_node;
3315 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3316 return unsigned_intDI_type_node;
3317 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3318 return unsigned_intTI_type_node;
3321 return NULL_TREE;
3324 /* Return a data type that has machine mode MODE. If the mode is an
3325 integer, then UNSIGNEDP selects between signed and unsigned types. */
3327 tree
3328 gfc_type_for_mode (machine_mode mode, int unsignedp)
3330 int i;
3331 tree *base;
3332 scalar_int_mode int_mode;
3334 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3335 base = gfc_real_types;
3336 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3337 base = gfc_complex_types;
3338 else if (is_a <scalar_int_mode> (mode, &int_mode))
3340 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3341 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3343 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3344 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3346 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
3347 GET_MODE_NUNITS (mode));
3348 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3349 return build_vector_type_for_mode (bool_type, mode);
3351 else if (VECTOR_MODE_P (mode)
3352 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3354 machine_mode inner_mode = GET_MODE_INNER (mode);
3355 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3356 if (inner_type != NULL_TREE)
3357 return build_vector_type_for_mode (inner_type, mode);
3358 return NULL_TREE;
3360 else
3361 return NULL_TREE;
3363 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3365 tree type = base[i];
3366 if (type && mode == TYPE_MODE (type))
3367 return type;
3370 return NULL_TREE;
3373 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3374 in that case. */
3376 bool
3377 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3379 int rank, dim;
3380 bool indirect = false;
3381 tree etype, ptype, t, base_decl;
3382 tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
3383 tree lower_suboff, upper_suboff, stride_suboff;
3384 tree dtype, field, rank_off;
3386 if (! GFC_DESCRIPTOR_TYPE_P (type))
3388 if (! POINTER_TYPE_P (type))
3389 return false;
3390 type = TREE_TYPE (type);
3391 if (! GFC_DESCRIPTOR_TYPE_P (type))
3392 return false;
3393 indirect = true;
3396 rank = GFC_TYPE_ARRAY_RANK (type);
3397 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3398 return false;
3400 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3401 gcc_assert (POINTER_TYPE_P (etype));
3402 etype = TREE_TYPE (etype);
3404 /* If the type is not a scalar coarray. */
3405 if (TREE_CODE (etype) == ARRAY_TYPE)
3406 etype = TREE_TYPE (etype);
3408 /* Can't handle variable sized elements yet. */
3409 if (int_size_in_bytes (etype) <= 0)
3410 return false;
3411 /* Nor non-constant lower bounds in assumed shape arrays. */
3412 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3413 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3415 for (dim = 0; dim < rank; dim++)
3416 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3417 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3418 return false;
3421 memset (info, '\0', sizeof (*info));
3422 info->ndimensions = rank;
3423 info->ordering = array_descr_ordering_column_major;
3424 info->element_type = etype;
3425 ptype = build_pointer_type (gfc_array_index_type);
3426 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3427 if (!base_decl)
3429 base_decl = make_node (DEBUG_EXPR_DECL);
3430 DECL_ARTIFICIAL (base_decl) = 1;
3431 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3432 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3433 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3435 info->base_decl = base_decl;
3436 if (indirect)
3437 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3439 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3440 &dim_off, &dim_size, &stride_suboff,
3441 &lower_suboff, &upper_suboff);
3443 t = fold_build_pointer_plus (base_decl, span_off);
3444 elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3446 t = base_decl;
3447 if (!integer_zerop (data_off))
3448 t = fold_build_pointer_plus (t, data_off);
3449 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3450 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3451 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3452 info->allocated = build2 (NE_EXPR, logical_type_node,
3453 info->data_location, null_pointer_node);
3454 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3455 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3456 info->associated = build2 (NE_EXPR, logical_type_node,
3457 info->data_location, null_pointer_node);
3458 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3459 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3460 && dwarf_version >= 5)
3462 rank = 1;
3463 info->ndimensions = 1;
3464 t = base_decl;
3465 if (!integer_zerop (dtype_off))
3466 t = fold_build_pointer_plus (t, dtype_off);
3467 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3468 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3469 rank_off = byte_position (field);
3470 if (!integer_zerop (dtype_off))
3471 t = fold_build_pointer_plus (t, rank_off);
3473 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3474 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3475 info->rank = t;
3476 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3477 t = size_binop (MULT_EXPR, t, dim_size);
3478 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3481 for (dim = 0; dim < rank; dim++)
3483 t = fold_build_pointer_plus (base_decl,
3484 size_binop (PLUS_EXPR,
3485 dim_off, lower_suboff));
3486 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3487 info->dimen[dim].lower_bound = t;
3488 t = fold_build_pointer_plus (base_decl,
3489 size_binop (PLUS_EXPR,
3490 dim_off, upper_suboff));
3491 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3492 info->dimen[dim].upper_bound = t;
3493 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3494 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3496 /* Assumed shape arrays have known lower bounds. */
3497 info->dimen[dim].upper_bound
3498 = build2 (MINUS_EXPR, gfc_array_index_type,
3499 info->dimen[dim].upper_bound,
3500 info->dimen[dim].lower_bound);
3501 info->dimen[dim].lower_bound
3502 = fold_convert (gfc_array_index_type,
3503 GFC_TYPE_ARRAY_LBOUND (type, dim));
3504 info->dimen[dim].upper_bound
3505 = build2 (PLUS_EXPR, gfc_array_index_type,
3506 info->dimen[dim].lower_bound,
3507 info->dimen[dim].upper_bound);
3509 t = fold_build_pointer_plus (base_decl,
3510 size_binop (PLUS_EXPR,
3511 dim_off, stride_suboff));
3512 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3513 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3514 info->dimen[dim].stride = t;
3515 if (dim + 1 < rank)
3516 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3519 return true;
3523 /* Create a type to handle vector subscripts for coarray library calls. It
3524 has the form:
3525 struct caf_vector_t {
3526 size_t nvec; // size of the vector
3527 union {
3528 struct {
3529 void *vector;
3530 int kind;
3531 } v;
3532 struct {
3533 ptrdiff_t lower_bound;
3534 ptrdiff_t upper_bound;
3535 ptrdiff_t stride;
3536 } triplet;
3537 } u;
3539 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3540 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3542 tree
3543 gfc_get_caf_vector_type (int dim)
3545 static tree vector_types[GFC_MAX_DIMENSIONS];
3546 static tree vec_type = NULL_TREE;
3547 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3549 if (vector_types[dim-1] != NULL_TREE)
3550 return vector_types[dim-1];
3552 if (vec_type == NULL_TREE)
3554 chain = 0;
3555 vect_struct_type = make_node (RECORD_TYPE);
3556 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3557 get_identifier ("vector"),
3558 pvoid_type_node, &chain);
3559 suppress_warning (tmp);
3560 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3561 get_identifier ("kind"),
3562 integer_type_node, &chain);
3563 suppress_warning (tmp);
3564 gfc_finish_type (vect_struct_type);
3566 chain = 0;
3567 triplet_struct_type = make_node (RECORD_TYPE);
3568 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3569 get_identifier ("lower_bound"),
3570 gfc_array_index_type, &chain);
3571 suppress_warning (tmp);
3572 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3573 get_identifier ("upper_bound"),
3574 gfc_array_index_type, &chain);
3575 suppress_warning (tmp);
3576 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3577 gfc_array_index_type, &chain);
3578 suppress_warning (tmp);
3579 gfc_finish_type (triplet_struct_type);
3581 chain = 0;
3582 union_type = make_node (UNION_TYPE);
3583 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3584 vect_struct_type, &chain);
3585 suppress_warning (tmp);
3586 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3587 triplet_struct_type, &chain);
3588 suppress_warning (tmp);
3589 gfc_finish_type (union_type);
3591 chain = 0;
3592 vec_type = make_node (RECORD_TYPE);
3593 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3594 size_type_node, &chain);
3595 suppress_warning (tmp);
3596 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3597 union_type, &chain);
3598 suppress_warning (tmp);
3599 gfc_finish_type (vec_type);
3600 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3603 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3604 gfc_rank_cst[dim-1]);
3605 vector_types[dim-1] = build_array_type (vec_type, tmp);
3606 return vector_types[dim-1];
3610 tree
3611 gfc_get_caf_reference_type ()
3613 static tree reference_type = NULL_TREE;
3614 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3615 a_struct_type, u_union_type, tmp, *chain;
3617 if (reference_type != NULL_TREE)
3618 return reference_type;
3620 chain = 0;
3621 c_struct_type = make_node (RECORD_TYPE);
3622 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3623 get_identifier ("offset"),
3624 gfc_array_index_type, &chain);
3625 suppress_warning (tmp);
3626 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3627 get_identifier ("caf_token_offset"),
3628 gfc_array_index_type, &chain);
3629 suppress_warning (tmp);
3630 gfc_finish_type (c_struct_type);
3632 chain = 0;
3633 s_struct_type = make_node (RECORD_TYPE);
3634 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3635 get_identifier ("start"),
3636 gfc_array_index_type, &chain);
3637 suppress_warning (tmp);
3638 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3639 get_identifier ("end"),
3640 gfc_array_index_type, &chain);
3641 suppress_warning (tmp);
3642 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3643 get_identifier ("stride"),
3644 gfc_array_index_type, &chain);
3645 suppress_warning (tmp);
3646 gfc_finish_type (s_struct_type);
3648 chain = 0;
3649 v_struct_type = make_node (RECORD_TYPE);
3650 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3651 get_identifier ("vector"),
3652 pvoid_type_node, &chain);
3653 suppress_warning (tmp);
3654 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3655 get_identifier ("nvec"),
3656 size_type_node, &chain);
3657 suppress_warning (tmp);
3658 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3659 get_identifier ("kind"),
3660 integer_type_node, &chain);
3661 suppress_warning (tmp);
3662 gfc_finish_type (v_struct_type);
3664 chain = 0;
3665 union_type = make_node (UNION_TYPE);
3666 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3667 s_struct_type, &chain);
3668 suppress_warning (tmp);
3669 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3670 v_struct_type, &chain);
3671 suppress_warning (tmp);
3672 gfc_finish_type (union_type);
3674 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3675 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3676 dim_union_type = build_array_type (union_type, tmp);
3678 chain = 0;
3679 a_struct_type = make_node (RECORD_TYPE);
3680 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3681 build_array_type (unsigned_char_type_node,
3682 build_range_type (gfc_array_index_type,
3683 gfc_index_zero_node,
3684 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3685 &chain);
3686 suppress_warning (tmp);
3687 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3688 get_identifier ("static_array_type"),
3689 integer_type_node, &chain);
3690 suppress_warning (tmp);
3691 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3692 dim_union_type, &chain);
3693 suppress_warning (tmp);
3694 gfc_finish_type (a_struct_type);
3696 chain = 0;
3697 u_union_type = make_node (UNION_TYPE);
3698 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3699 c_struct_type, &chain);
3700 suppress_warning (tmp);
3701 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3702 a_struct_type, &chain);
3703 suppress_warning (tmp);
3704 gfc_finish_type (u_union_type);
3706 chain = 0;
3707 reference_type = make_node (RECORD_TYPE);
3708 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3709 build_pointer_type (reference_type), &chain);
3710 suppress_warning (tmp);
3711 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3712 integer_type_node, &chain);
3713 suppress_warning (tmp);
3714 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3715 size_type_node, &chain);
3716 suppress_warning (tmp);
3717 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3718 u_union_type, &chain);
3719 suppress_warning (tmp);
3720 gfc_finish_type (reference_type);
3721 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3723 return reference_type;
3726 #include "gt-fortran-trans-types.h"