Daily bump.
[official-gcc.git] / gcc / fortran / trans-types.c
blob1c78a9063970374514a8e88ea88cb14d0529afff
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->attr.optional
2346 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2347 type = build_pointer_type (type);
2348 else
2350 type = build_reference_type (type);
2351 if (restricted)
2352 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2356 return (type);
2359 /* Layout and output debug info for a record type. */
2361 void
2362 gfc_finish_type (tree type)
2364 tree decl;
2366 decl = build_decl (input_location,
2367 TYPE_DECL, NULL_TREE, type);
2368 TYPE_STUB_DECL (type) = decl;
2369 layout_type (type);
2370 rest_of_type_compilation (type, 1);
2371 rest_of_decl_compilation (decl, 1, 0);
2374 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2375 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2376 to the end of the field list pointed to by *CHAIN.
2378 Returns a pointer to the new field. */
2380 static tree
2381 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2383 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2385 DECL_CONTEXT (decl) = context;
2386 DECL_CHAIN (decl) = NULL_TREE;
2387 if (TYPE_FIELDS (context) == NULL_TREE)
2388 TYPE_FIELDS (context) = decl;
2389 if (chain != NULL)
2391 if (*chain != NULL)
2392 **chain = decl;
2393 *chain = &DECL_CHAIN (decl);
2396 return decl;
2399 /* Like `gfc_add_field_to_struct_1', but adds alignment
2400 information. */
2402 tree
2403 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2405 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2407 DECL_INITIAL (decl) = 0;
2408 SET_DECL_ALIGN (decl, 0);
2409 DECL_USER_ALIGN (decl) = 0;
2411 return decl;
2415 /* Copy the backend_decl and component backend_decls if
2416 the two derived type symbols are "equal", as described
2417 in 4.4.2 and resolved by gfc_compare_derived_types. */
2420 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2421 bool from_gsym)
2423 gfc_component *to_cm;
2424 gfc_component *from_cm;
2426 if (from == to)
2427 return 1;
2429 if (from->backend_decl == NULL
2430 || !gfc_compare_derived_types (from, to))
2431 return 0;
2433 to->backend_decl = from->backend_decl;
2435 to_cm = to->components;
2436 from_cm = from->components;
2438 /* Copy the component declarations. If a component is itself
2439 a derived type, we need a copy of its component declarations.
2440 This is done by recursing into gfc_get_derived_type and
2441 ensures that the component's component declarations have
2442 been built. If it is a character, we need the character
2443 length, as well. */
2444 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2446 to_cm->backend_decl = from_cm->backend_decl;
2447 to_cm->caf_token = from_cm->caf_token;
2448 if (from_cm->ts.type == BT_UNION)
2449 gfc_get_union_type (to_cm->ts.u.derived);
2450 else if (from_cm->ts.type == BT_DERIVED
2451 && (!from_cm->attr.pointer || from_gsym))
2452 gfc_get_derived_type (to_cm->ts.u.derived);
2453 else if (from_cm->ts.type == BT_CLASS
2454 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2455 gfc_get_derived_type (to_cm->ts.u.derived);
2456 else if (from_cm->ts.type == BT_CHARACTER)
2457 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2460 return 1;
2464 /* Build a tree node for a procedure pointer component. */
2466 tree
2467 gfc_get_ppc_type (gfc_component* c)
2469 tree t;
2471 /* Explicit interface. */
2472 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2473 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2475 /* Implicit interface (only return value may be known). */
2476 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2477 t = gfc_typenode_for_spec (&c->ts);
2478 else
2479 t = void_type_node;
2481 /* FIXME: it would be better to provide explicit interfaces in all
2482 cases, since they should be known by the compiler. */
2483 return build_pointer_type (build_function_type (t, NULL_TREE));
2487 /* Build a tree node for a union type. Requires building each map
2488 structure which is an element of the union. */
2490 tree
2491 gfc_get_union_type (gfc_symbol *un)
2493 gfc_component *map = NULL;
2494 tree typenode = NULL, map_type = NULL, map_field = NULL;
2495 tree *chain = NULL;
2497 if (un->backend_decl)
2499 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2500 return un->backend_decl;
2501 else
2502 typenode = un->backend_decl;
2504 else
2506 typenode = make_node (UNION_TYPE);
2507 TYPE_NAME (typenode) = get_identifier (un->name);
2510 /* Add each contained MAP as a field. */
2511 for (map = un->components; map; map = map->next)
2513 gcc_assert (map->ts.type == BT_DERIVED);
2515 /* The map's type node, which is defined within this union's context. */
2516 map_type = gfc_get_derived_type (map->ts.u.derived);
2517 TYPE_CONTEXT (map_type) = typenode;
2519 /* The map field's declaration. */
2520 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2521 map_type, &chain);
2522 if (map->loc.lb)
2523 gfc_set_decl_location (map_field, &map->loc);
2524 else if (un->declared_at.lb)
2525 gfc_set_decl_location (map_field, &un->declared_at);
2527 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2528 DECL_NAMELESS(map_field) = true;
2530 /* We should never clobber another backend declaration for this map,
2531 because each map component is unique. */
2532 if (!map->backend_decl)
2533 map->backend_decl = map_field;
2536 un->backend_decl = typenode;
2537 gfc_finish_type (typenode);
2539 return typenode;
2543 /* Build a tree node for a derived type. If there are equal
2544 derived types, with different local names, these are built
2545 at the same time. If an equal derived type has been built
2546 in a parent namespace, this is used. */
2548 tree
2549 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2551 tree typenode = NULL, field = NULL, field_type = NULL;
2552 tree canonical = NULL_TREE;
2553 tree *chain = NULL;
2554 bool got_canonical = false;
2555 bool unlimited_entity = false;
2556 gfc_component *c;
2557 gfc_namespace *ns;
2558 tree tmp;
2559 bool coarray_flag;
2561 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2562 && derived->module && !derived->attr.vtype;
2564 gcc_assert (!derived->attr.pdt_template);
2566 if (derived->attr.unlimited_polymorphic
2567 || (flag_coarray == GFC_FCOARRAY_LIB
2568 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2569 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2570 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2571 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2572 return ptr_type_node;
2574 if (flag_coarray != GFC_FCOARRAY_LIB
2575 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2576 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2577 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2578 return gfc_get_int_type (gfc_default_integer_kind);
2580 if (derived && derived->attr.flavor == FL_PROCEDURE
2581 && derived->attr.generic)
2582 derived = gfc_find_dt_in_generic (derived);
2584 /* See if it's one of the iso_c_binding derived types. */
2585 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2587 if (derived->backend_decl)
2588 return derived->backend_decl;
2590 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2591 derived->backend_decl = ptr_type_node;
2592 else
2593 derived->backend_decl = pfunc_type_node;
2595 derived->ts.kind = gfc_index_integer_kind;
2596 derived->ts.type = BT_INTEGER;
2597 /* Set the f90_type to BT_VOID as a way to recognize something of type
2598 BT_INTEGER that needs to fit a void * for the purpose of the
2599 iso_c_binding derived types. */
2600 derived->ts.f90_type = BT_VOID;
2602 return derived->backend_decl;
2605 /* If use associated, use the module type for this one. */
2606 if (derived->backend_decl == NULL
2607 && (derived->attr.use_assoc || derived->attr.used_in_submodule)
2608 && derived->module
2609 && gfc_get_module_backend_decl (derived))
2610 goto copy_derived_types;
2612 /* The derived types from an earlier namespace can be used as the
2613 canonical type. */
2614 if (derived->backend_decl == NULL
2615 && !derived->attr.use_assoc
2616 && !derived->attr.used_in_submodule
2617 && gfc_global_ns_list)
2619 for (ns = gfc_global_ns_list;
2620 ns->translated && !got_canonical;
2621 ns = ns->sibling)
2623 if (ns->derived_types)
2625 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2626 dt = dt->dt_next)
2628 gfc_copy_dt_decls_ifequal (dt, derived, true);
2629 if (derived->backend_decl)
2630 got_canonical = true;
2631 if (dt->dt_next == ns->derived_types)
2632 break;
2638 /* Store up the canonical type to be added to this one. */
2639 if (got_canonical)
2641 if (TYPE_CANONICAL (derived->backend_decl))
2642 canonical = TYPE_CANONICAL (derived->backend_decl);
2643 else
2644 canonical = derived->backend_decl;
2646 derived->backend_decl = NULL_TREE;
2649 /* derived->backend_decl != 0 means we saw it before, but its
2650 components' backend_decl may have not been built. */
2651 if (derived->backend_decl)
2653 /* Its components' backend_decl have been built or we are
2654 seeing recursion through the formal arglist of a procedure
2655 pointer component. */
2656 if (TYPE_FIELDS (derived->backend_decl))
2657 return derived->backend_decl;
2658 else if (derived->attr.abstract
2659 && derived->attr.proc_pointer_comp)
2661 /* If an abstract derived type with procedure pointer
2662 components has no other type of component, return the
2663 backend_decl. Otherwise build the components if any of the
2664 non-procedure pointer components have no backend_decl. */
2665 for (c = derived->components; c; c = c->next)
2667 bool same_alloc_type = c->attr.allocatable
2668 && derived == c->ts.u.derived;
2669 if (!c->attr.proc_pointer
2670 && !same_alloc_type
2671 && c->backend_decl == NULL)
2672 break;
2673 else if (c->next == NULL)
2674 return derived->backend_decl;
2676 typenode = derived->backend_decl;
2678 else
2679 typenode = derived->backend_decl;
2681 else
2683 /* We see this derived type first time, so build the type node. */
2684 typenode = make_node (RECORD_TYPE);
2685 TYPE_NAME (typenode) = get_identifier (derived->name);
2686 TYPE_PACKED (typenode) = flag_pack_derived;
2687 derived->backend_decl = typenode;
2690 if (derived->components
2691 && derived->components->ts.type == BT_DERIVED
2692 && strcmp (derived->components->name, "_data") == 0
2693 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2694 unlimited_entity = true;
2696 /* Go through the derived type components, building them as
2697 necessary. The reason for doing this now is that it is
2698 possible to recurse back to this derived type through a
2699 pointer component (PR24092). If this happens, the fields
2700 will be built and so we can return the type. */
2701 for (c = derived->components; c; c = c->next)
2703 bool same_alloc_type = c->attr.allocatable
2704 && derived == c->ts.u.derived;
2706 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2707 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2709 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2710 continue;
2712 if ((!c->attr.pointer && !c->attr.proc_pointer
2713 && !same_alloc_type)
2714 || c->ts.u.derived->backend_decl == NULL)
2716 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2717 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2718 local_codim);
2721 if (c->ts.u.derived->attr.is_iso_c)
2723 /* Need to copy the modified ts from the derived type. The
2724 typespec was modified because C_PTR/C_FUNPTR are translated
2725 into (void *) from derived types. */
2726 c->ts.type = c->ts.u.derived->ts.type;
2727 c->ts.kind = c->ts.u.derived->ts.kind;
2728 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2729 if (c->initializer)
2731 c->initializer->ts.type = c->ts.type;
2732 c->initializer->ts.kind = c->ts.kind;
2733 c->initializer->ts.f90_type = c->ts.f90_type;
2734 c->initializer->expr_type = EXPR_NULL;
2739 if (TYPE_FIELDS (derived->backend_decl))
2740 return derived->backend_decl;
2742 /* Build the type member list. Install the newly created RECORD_TYPE
2743 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2744 through only the top-level linked list of components so we correctly
2745 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2746 types are built as part of gfc_get_union_type. */
2747 for (c = derived->components; c; c = c->next)
2749 bool same_alloc_type = c->attr.allocatable
2750 && derived == c->ts.u.derived;
2751 /* Prevent infinite recursion, when the procedure pointer type is
2752 the same as derived, by forcing the procedure pointer component to
2753 be built as if the explicit interface does not exist. */
2754 if (c->attr.proc_pointer
2755 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2756 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2757 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2758 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2759 field_type = gfc_get_ppc_type (c);
2760 else if (c->attr.proc_pointer && derived->backend_decl)
2762 tmp = build_function_type (derived->backend_decl, NULL_TREE);
2763 field_type = build_pointer_type (tmp);
2765 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2766 field_type = c->ts.u.derived->backend_decl;
2767 else if (c->attr.caf_token)
2768 field_type = pvoid_type_node;
2769 else
2771 if (c->ts.type == BT_CHARACTER
2772 && !c->ts.deferred && !c->attr.pdt_string)
2774 /* Evaluate the string length. */
2775 gfc_conv_const_charlen (c->ts.u.cl);
2776 gcc_assert (c->ts.u.cl->backend_decl);
2778 else if (c->ts.type == BT_CHARACTER)
2779 c->ts.u.cl->backend_decl
2780 = build_int_cst (gfc_charlen_type_node, 0);
2782 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2785 /* This returns an array descriptor type. Initialization may be
2786 required. */
2787 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2789 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2791 enum gfc_array_kind akind;
2792 if (c->attr.pointer)
2793 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2794 : GFC_ARRAY_POINTER;
2795 else
2796 akind = GFC_ARRAY_ALLOCATABLE;
2797 /* Pointers to arrays aren't actually pointer types. The
2798 descriptors are separate, but the data is common. */
2799 field_type = gfc_build_array_type (field_type, c->as, akind,
2800 !c->attr.target
2801 && !c->attr.pointer,
2802 c->attr.contiguous,
2803 codimen);
2805 else
2806 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2807 PACKED_STATIC,
2808 !c->attr.target);
2810 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2811 && !c->attr.proc_pointer
2812 && !(unlimited_entity && c == derived->components))
2813 field_type = build_pointer_type (field_type);
2815 if (c->attr.pointer || same_alloc_type)
2816 field_type = gfc_nonrestricted_type (field_type);
2818 /* vtype fields can point to different types to the base type. */
2819 if (c->ts.type == BT_DERIVED
2820 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2821 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2822 ptr_mode, true);
2824 /* Ensure that the CLASS language specific flag is set. */
2825 if (c->ts.type == BT_CLASS)
2827 if (POINTER_TYPE_P (field_type))
2828 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2829 else
2830 GFC_CLASS_TYPE_P (field_type) = 1;
2833 field = gfc_add_field_to_struct (typenode,
2834 get_identifier (c->name),
2835 field_type, &chain);
2836 if (c->loc.lb)
2837 gfc_set_decl_location (field, &c->loc);
2838 else if (derived->declared_at.lb)
2839 gfc_set_decl_location (field, &derived->declared_at);
2841 gfc_finish_decl_attrs (field, &c->attr);
2843 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2845 gcc_assert (field);
2846 if (!c->backend_decl)
2847 c->backend_decl = field;
2849 if (c->attr.pointer && c->attr.dimension
2850 && !(c->ts.type == BT_DERIVED
2851 && strcmp (c->name, "_data") == 0))
2852 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2855 /* Now lay out the derived type, including the fields. */
2856 if (canonical)
2857 TYPE_CANONICAL (typenode) = canonical;
2859 gfc_finish_type (typenode);
2860 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2861 if (derived->module && derived->ns->proc_name
2862 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2864 if (derived->ns->proc_name->backend_decl
2865 && TREE_CODE (derived->ns->proc_name->backend_decl)
2866 == NAMESPACE_DECL)
2868 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2869 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2870 = derived->ns->proc_name->backend_decl;
2874 derived->backend_decl = typenode;
2876 copy_derived_types:
2878 for (c = derived->components; c; c = c->next)
2880 /* Do not add a caf_token field for class container components. */
2881 if ((codimen || coarray_flag)
2882 && !c->attr.dimension && !c->attr.codimension
2883 && (c->attr.allocatable || c->attr.pointer)
2884 && !derived->attr.is_class)
2886 /* Provide sufficient space to hold "_caf_symbol". */
2887 char caf_name[GFC_MAX_SYMBOL_LEN + 6];
2888 gfc_component *token;
2889 snprintf (caf_name, sizeof (caf_name), "_caf_%s", c->name);
2890 token = gfc_find_component (derived, caf_name, true, true, NULL);
2891 gcc_assert (token);
2892 c->caf_token = token->backend_decl;
2893 suppress_warning (c->caf_token);
2897 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
2899 gfc_copy_dt_decls_ifequal (derived, dt, false);
2900 if (dt->dt_next == gfc_derived_types)
2901 break;
2904 return derived->backend_decl;
2909 gfc_return_by_reference (gfc_symbol * sym)
2911 if (!sym->attr.function)
2912 return 0;
2914 if (sym->attr.dimension)
2915 return 1;
2917 if (sym->ts.type == BT_CHARACTER
2918 && !sym->attr.is_bind_c
2919 && (!sym->attr.result
2920 || !sym->ns->proc_name
2921 || !sym->ns->proc_name->attr.is_bind_c))
2922 return 1;
2924 /* Possibly return complex numbers by reference for g77 compatibility.
2925 We don't do this for calls to intrinsics (as the library uses the
2926 -fno-f2c calling convention), nor for calls to functions which always
2927 require an explicit interface, as no compatibility problems can
2928 arise there. */
2929 if (flag_f2c && sym->ts.type == BT_COMPLEX
2930 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2931 return 1;
2933 return 0;
2936 static tree
2937 gfc_get_mixed_entry_union (gfc_namespace *ns)
2939 tree type;
2940 tree *chain = NULL;
2941 char name[GFC_MAX_SYMBOL_LEN + 1];
2942 gfc_entry_list *el, *el2;
2944 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2945 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2947 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2949 /* Build the type node. */
2950 type = make_node (UNION_TYPE);
2952 TYPE_NAME (type) = get_identifier (name);
2954 for (el = ns->entries; el; el = el->next)
2956 /* Search for duplicates. */
2957 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2958 if (el2->sym->result == el->sym->result)
2959 break;
2961 if (el == el2)
2962 gfc_add_field_to_struct_1 (type,
2963 get_identifier (el->sym->result->name),
2964 gfc_sym_type (el->sym->result), &chain);
2967 /* Finish off the type. */
2968 gfc_finish_type (type);
2969 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2970 return type;
2973 /* Create a "fn spec" based on the formal arguments;
2974 cf. create_function_arglist. */
2976 static tree
2977 create_fn_spec (gfc_symbol *sym, tree fntype)
2979 char spec[150];
2980 size_t spec_len;
2981 gfc_formal_arglist *f;
2982 tree tmp;
2984 memset (&spec, 0, sizeof (spec));
2985 spec[0] = '.';
2986 spec[1] = ' ';
2987 spec_len = 2;
2989 if (sym->attr.entry_master)
2991 spec[spec_len++] = 'R';
2992 spec[spec_len++] = ' ';
2994 if (gfc_return_by_reference (sym))
2996 gfc_symbol *result = sym->result ? sym->result : sym;
2998 if (result->attr.pointer || sym->attr.proc_pointer)
3000 spec[spec_len++] = '.';
3001 spec[spec_len++] = ' ';
3003 else
3005 spec[spec_len++] = 'w';
3006 spec[spec_len++] = ' ';
3008 if (sym->ts.type == BT_CHARACTER)
3010 spec[spec_len++] = 'R';
3011 spec[spec_len++] = ' ';
3015 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3016 if (spec_len < sizeof (spec))
3018 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
3019 || f->sym->attr.external || f->sym->attr.cray_pointer
3020 || (f->sym->ts.type == BT_DERIVED
3021 && (f->sym->ts.u.derived->attr.proc_pointer_comp
3022 || f->sym->ts.u.derived->attr.pointer_comp))
3023 || (f->sym->ts.type == BT_CLASS
3024 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
3025 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
3026 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
3028 spec[spec_len++] = '.';
3029 spec[spec_len++] = ' ';
3031 else if (f->sym->attr.intent == INTENT_IN)
3033 spec[spec_len++] = 'r';
3034 spec[spec_len++] = ' ';
3036 else if (f->sym)
3038 spec[spec_len++] = 'w';
3039 spec[spec_len++] = ' ';
3043 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
3044 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
3045 return build_type_attribute_variant (fntype, tmp);
3049 /* NOTE: The returned function type must match the argument list created by
3050 create_function_arglist. */
3052 tree
3053 gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args,
3054 const char *fnspec)
3056 tree type;
3057 vec<tree, va_gc> *typelist = NULL;
3058 gfc_formal_arglist *f;
3059 gfc_symbol *arg;
3060 int alternate_return = 0;
3061 bool is_varargs = true;
3063 /* Make sure this symbol is a function, a subroutine or the main
3064 program. */
3065 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3066 || sym->attr.flavor == FL_PROGRAM);
3068 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3069 so that they can be detected here and handled further down. */
3070 if (sym->backend_decl == NULL)
3071 sym->backend_decl = error_mark_node;
3072 else if (sym->backend_decl == error_mark_node)
3073 goto arg_type_list_done;
3074 else if (sym->attr.proc_pointer)
3075 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3076 else
3077 return TREE_TYPE (sym->backend_decl);
3079 if (sym->attr.entry_master)
3080 /* Additional parameter for selecting an entry point. */
3081 vec_safe_push (typelist, gfc_array_index_type);
3083 if (sym->result)
3084 arg = sym->result;
3085 else
3086 arg = sym;
3088 if (arg->ts.type == BT_CHARACTER)
3089 gfc_conv_const_charlen (arg->ts.u.cl);
3091 /* Some functions we use an extra parameter for the return value. */
3092 if (gfc_return_by_reference (sym))
3094 type = gfc_sym_type (arg);
3095 if (arg->ts.type == BT_COMPLEX
3096 || arg->attr.dimension
3097 || arg->ts.type == BT_CHARACTER)
3098 type = build_reference_type (type);
3100 vec_safe_push (typelist, type);
3101 if (arg->ts.type == BT_CHARACTER)
3103 if (!arg->ts.deferred)
3104 /* Transfer by value. */
3105 vec_safe_push (typelist, gfc_charlen_type_node);
3106 else
3107 /* Deferred character lengths are transferred by reference
3108 so that the value can be returned. */
3109 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3112 if (sym->backend_decl == error_mark_node && actual_args != NULL
3113 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3114 || sym->attr.proc == PROC_UNKNOWN))
3115 gfc_get_formal_from_actual_arglist (sym, actual_args);
3117 /* Build the argument types for the function. */
3118 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3120 arg = f->sym;
3121 if (arg)
3123 /* Evaluate constant character lengths here so that they can be
3124 included in the type. */
3125 if (arg->ts.type == BT_CHARACTER)
3126 gfc_conv_const_charlen (arg->ts.u.cl);
3128 if (arg->attr.flavor == FL_PROCEDURE)
3130 type = gfc_get_function_type (arg);
3131 type = build_pointer_type (type);
3133 else
3134 type = gfc_sym_type (arg);
3136 /* Parameter Passing Convention
3138 We currently pass all parameters by reference.
3139 Parameters with INTENT(IN) could be passed by value.
3140 The problem arises if a function is called via an implicit
3141 prototype. In this situation the INTENT is not known.
3142 For this reason all parameters to global functions must be
3143 passed by reference. Passing by value would potentially
3144 generate bad code. Worse there would be no way of telling that
3145 this code was bad, except that it would give incorrect results.
3147 Contained procedures could pass by value as these are never
3148 used without an explicit interface, and cannot be passed as
3149 actual parameters for a dummy procedure. */
3151 vec_safe_push (typelist, type);
3153 else
3155 if (sym->attr.subroutine)
3156 alternate_return = 1;
3160 /* Add hidden arguments. */
3161 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3163 arg = f->sym;
3164 /* Add hidden string length parameters. */
3165 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3167 if (!arg->ts.deferred)
3168 /* Transfer by value. */
3169 type = gfc_charlen_type_node;
3170 else
3171 /* Deferred character lengths are transferred by reference
3172 so that the value can be returned. */
3173 type = build_pointer_type (gfc_charlen_type_node);
3175 vec_safe_push (typelist, type);
3177 /* For noncharacter scalar intrinsic types, VALUE passes the value,
3178 hence, the optional status cannot be transferred via a NULL pointer.
3179 Thus, we will use a hidden argument in that case. */
3180 else if (arg
3181 && arg->attr.optional
3182 && arg->attr.value
3183 && !arg->attr.dimension
3184 && arg->ts.type != BT_CLASS
3185 && !gfc_bt_struct (arg->ts.type))
3186 vec_safe_push (typelist, boolean_type_node);
3187 /* Coarrays which are descriptorless or assumed-shape pass with
3188 -fcoarray=lib the token and the offset as hidden arguments. */
3189 if (arg
3190 && flag_coarray == GFC_FCOARRAY_LIB
3191 && ((arg->ts.type != BT_CLASS
3192 && arg->attr.codimension
3193 && !arg->attr.allocatable)
3194 || (arg->ts.type == BT_CLASS
3195 && CLASS_DATA (arg)->attr.codimension
3196 && !CLASS_DATA (arg)->attr.allocatable)))
3198 vec_safe_push (typelist, pvoid_type_node); /* caf_token. */
3199 vec_safe_push (typelist, gfc_array_index_type); /* caf_offset. */
3203 if (!vec_safe_is_empty (typelist)
3204 || sym->attr.is_main_program
3205 || sym->attr.if_source != IFSRC_UNKNOWN)
3206 is_varargs = false;
3208 if (sym->backend_decl == error_mark_node)
3209 sym->backend_decl = NULL_TREE;
3211 arg_type_list_done:
3213 if (alternate_return)
3214 type = integer_type_node;
3215 else if (!sym->attr.function || gfc_return_by_reference (sym))
3216 type = void_type_node;
3217 else if (sym->attr.mixed_entry_master)
3218 type = gfc_get_mixed_entry_union (sym->ns);
3219 else if (flag_f2c && sym->ts.type == BT_REAL
3220 && sym->ts.kind == gfc_default_real_kind
3221 && !sym->attr.always_explicit)
3223 /* Special case: f2c calling conventions require that (scalar)
3224 default REAL functions return the C type double instead. f2c
3225 compatibility is only an issue with functions that don't
3226 require an explicit interface, as only these could be
3227 implemented in Fortran 77. */
3228 sym->ts.kind = gfc_default_double_kind;
3229 type = gfc_typenode_for_spec (&sym->ts);
3230 sym->ts.kind = gfc_default_real_kind;
3232 else if (sym->result && sym->result->attr.proc_pointer)
3233 /* Procedure pointer return values. */
3235 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3237 /* Unset proc_pointer as gfc_get_function_type
3238 is called recursively. */
3239 sym->result->attr.proc_pointer = 0;
3240 type = build_pointer_type (gfc_get_function_type (sym->result));
3241 sym->result->attr.proc_pointer = 1;
3243 else
3244 type = gfc_sym_type (sym->result);
3246 else
3247 type = gfc_sym_type (sym);
3249 if (is_varargs)
3250 type = build_varargs_function_type_vec (type, typelist);
3251 else
3252 type = build_function_type_vec (type, typelist);
3254 /* If we were passed an fn spec, add it here, otherwise determine it from
3255 the formal arguments. */
3256 if (fnspec)
3258 tree tmp;
3259 int spec_len = strlen (fnspec);
3260 tmp = build_tree_list (NULL_TREE, build_string (spec_len, fnspec));
3261 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (type));
3262 type = build_type_attribute_variant (type, tmp);
3264 else
3265 type = create_fn_spec (sym, type);
3267 return type;
3270 /* Language hooks for middle-end access to type nodes. */
3272 /* Return an integer type with BITS bits of precision,
3273 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3275 tree
3276 gfc_type_for_size (unsigned bits, int unsignedp)
3278 if (!unsignedp)
3280 int i;
3281 for (i = 0; i <= MAX_INT_KINDS; ++i)
3283 tree type = gfc_integer_types[i];
3284 if (type && bits == TYPE_PRECISION (type))
3285 return type;
3288 /* Handle TImode as a special case because it is used by some backends
3289 (e.g. ARM) even though it is not available for normal use. */
3290 #if HOST_BITS_PER_WIDE_INT >= 64
3291 if (bits == TYPE_PRECISION (intTI_type_node))
3292 return intTI_type_node;
3293 #endif
3295 if (bits <= TYPE_PRECISION (intQI_type_node))
3296 return intQI_type_node;
3297 if (bits <= TYPE_PRECISION (intHI_type_node))
3298 return intHI_type_node;
3299 if (bits <= TYPE_PRECISION (intSI_type_node))
3300 return intSI_type_node;
3301 if (bits <= TYPE_PRECISION (intDI_type_node))
3302 return intDI_type_node;
3303 if (bits <= TYPE_PRECISION (intTI_type_node))
3304 return intTI_type_node;
3306 else
3308 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3309 return unsigned_intQI_type_node;
3310 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3311 return unsigned_intHI_type_node;
3312 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3313 return unsigned_intSI_type_node;
3314 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3315 return unsigned_intDI_type_node;
3316 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3317 return unsigned_intTI_type_node;
3320 return NULL_TREE;
3323 /* Return a data type that has machine mode MODE. If the mode is an
3324 integer, then UNSIGNEDP selects between signed and unsigned types. */
3326 tree
3327 gfc_type_for_mode (machine_mode mode, int unsignedp)
3329 int i;
3330 tree *base;
3331 scalar_int_mode int_mode;
3333 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3334 base = gfc_real_types;
3335 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3336 base = gfc_complex_types;
3337 else if (is_a <scalar_int_mode> (mode, &int_mode))
3339 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3340 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3342 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3343 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3345 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
3346 GET_MODE_NUNITS (mode));
3347 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3348 return build_vector_type_for_mode (bool_type, mode);
3350 else if (VECTOR_MODE_P (mode)
3351 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3353 machine_mode inner_mode = GET_MODE_INNER (mode);
3354 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3355 if (inner_type != NULL_TREE)
3356 return build_vector_type_for_mode (inner_type, mode);
3357 return NULL_TREE;
3359 else
3360 return NULL_TREE;
3362 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3364 tree type = base[i];
3365 if (type && mode == TYPE_MODE (type))
3366 return type;
3369 return NULL_TREE;
3372 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3373 in that case. */
3375 bool
3376 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3378 int rank, dim;
3379 bool indirect = false;
3380 tree etype, ptype, t, base_decl;
3381 tree data_off, span_off, dim_off, dtype_off, dim_size, elem_size;
3382 tree lower_suboff, upper_suboff, stride_suboff;
3383 tree dtype, field, rank_off;
3385 if (! GFC_DESCRIPTOR_TYPE_P (type))
3387 if (! POINTER_TYPE_P (type))
3388 return false;
3389 type = TREE_TYPE (type);
3390 if (! GFC_DESCRIPTOR_TYPE_P (type))
3391 return false;
3392 indirect = true;
3395 rank = GFC_TYPE_ARRAY_RANK (type);
3396 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3397 return false;
3399 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3400 gcc_assert (POINTER_TYPE_P (etype));
3401 etype = TREE_TYPE (etype);
3403 /* If the type is not a scalar coarray. */
3404 if (TREE_CODE (etype) == ARRAY_TYPE)
3405 etype = TREE_TYPE (etype);
3407 /* Can't handle variable sized elements yet. */
3408 if (int_size_in_bytes (etype) <= 0)
3409 return false;
3410 /* Nor non-constant lower bounds in assumed shape arrays. */
3411 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3412 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3414 for (dim = 0; dim < rank; dim++)
3415 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3416 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3417 return false;
3420 memset (info, '\0', sizeof (*info));
3421 info->ndimensions = rank;
3422 info->ordering = array_descr_ordering_column_major;
3423 info->element_type = etype;
3424 ptype = build_pointer_type (gfc_array_index_type);
3425 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3426 if (!base_decl)
3428 base_decl = make_node (DEBUG_EXPR_DECL);
3429 DECL_ARTIFICIAL (base_decl) = 1;
3430 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3431 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3432 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3434 info->base_decl = base_decl;
3435 if (indirect)
3436 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3438 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &span_off,
3439 &dim_off, &dim_size, &stride_suboff,
3440 &lower_suboff, &upper_suboff);
3442 t = fold_build_pointer_plus (base_decl, span_off);
3443 elem_size = build1 (INDIRECT_REF, gfc_array_index_type, t);
3445 t = base_decl;
3446 if (!integer_zerop (data_off))
3447 t = fold_build_pointer_plus (t, data_off);
3448 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3449 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3450 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3451 info->allocated = build2 (NE_EXPR, logical_type_node,
3452 info->data_location, null_pointer_node);
3453 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3454 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3455 info->associated = build2 (NE_EXPR, logical_type_node,
3456 info->data_location, null_pointer_node);
3457 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3458 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3459 && dwarf_version >= 5)
3461 rank = 1;
3462 info->ndimensions = 1;
3463 t = base_decl;
3464 if (!integer_zerop (dtype_off))
3465 t = fold_build_pointer_plus (t, dtype_off);
3466 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3467 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3468 rank_off = byte_position (field);
3469 if (!integer_zerop (dtype_off))
3470 t = fold_build_pointer_plus (t, rank_off);
3472 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3473 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3474 info->rank = t;
3475 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3476 t = size_binop (MULT_EXPR, t, dim_size);
3477 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3480 for (dim = 0; dim < rank; dim++)
3482 t = fold_build_pointer_plus (base_decl,
3483 size_binop (PLUS_EXPR,
3484 dim_off, lower_suboff));
3485 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3486 info->dimen[dim].lower_bound = t;
3487 t = fold_build_pointer_plus (base_decl,
3488 size_binop (PLUS_EXPR,
3489 dim_off, upper_suboff));
3490 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3491 info->dimen[dim].upper_bound = t;
3492 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3493 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3495 /* Assumed shape arrays have known lower bounds. */
3496 info->dimen[dim].upper_bound
3497 = build2 (MINUS_EXPR, gfc_array_index_type,
3498 info->dimen[dim].upper_bound,
3499 info->dimen[dim].lower_bound);
3500 info->dimen[dim].lower_bound
3501 = fold_convert (gfc_array_index_type,
3502 GFC_TYPE_ARRAY_LBOUND (type, dim));
3503 info->dimen[dim].upper_bound
3504 = build2 (PLUS_EXPR, gfc_array_index_type,
3505 info->dimen[dim].lower_bound,
3506 info->dimen[dim].upper_bound);
3508 t = fold_build_pointer_plus (base_decl,
3509 size_binop (PLUS_EXPR,
3510 dim_off, stride_suboff));
3511 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3512 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3513 info->dimen[dim].stride = t;
3514 if (dim + 1 < rank)
3515 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3518 return true;
3522 /* Create a type to handle vector subscripts for coarray library calls. It
3523 has the form:
3524 struct caf_vector_t {
3525 size_t nvec; // size of the vector
3526 union {
3527 struct {
3528 void *vector;
3529 int kind;
3530 } v;
3531 struct {
3532 ptrdiff_t lower_bound;
3533 ptrdiff_t upper_bound;
3534 ptrdiff_t stride;
3535 } triplet;
3536 } u;
3538 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3539 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3541 tree
3542 gfc_get_caf_vector_type (int dim)
3544 static tree vector_types[GFC_MAX_DIMENSIONS];
3545 static tree vec_type = NULL_TREE;
3546 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3548 if (vector_types[dim-1] != NULL_TREE)
3549 return vector_types[dim-1];
3551 if (vec_type == NULL_TREE)
3553 chain = 0;
3554 vect_struct_type = make_node (RECORD_TYPE);
3555 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3556 get_identifier ("vector"),
3557 pvoid_type_node, &chain);
3558 suppress_warning (tmp);
3559 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3560 get_identifier ("kind"),
3561 integer_type_node, &chain);
3562 suppress_warning (tmp);
3563 gfc_finish_type (vect_struct_type);
3565 chain = 0;
3566 triplet_struct_type = make_node (RECORD_TYPE);
3567 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3568 get_identifier ("lower_bound"),
3569 gfc_array_index_type, &chain);
3570 suppress_warning (tmp);
3571 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3572 get_identifier ("upper_bound"),
3573 gfc_array_index_type, &chain);
3574 suppress_warning (tmp);
3575 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3576 gfc_array_index_type, &chain);
3577 suppress_warning (tmp);
3578 gfc_finish_type (triplet_struct_type);
3580 chain = 0;
3581 union_type = make_node (UNION_TYPE);
3582 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3583 vect_struct_type, &chain);
3584 suppress_warning (tmp);
3585 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3586 triplet_struct_type, &chain);
3587 suppress_warning (tmp);
3588 gfc_finish_type (union_type);
3590 chain = 0;
3591 vec_type = make_node (RECORD_TYPE);
3592 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3593 size_type_node, &chain);
3594 suppress_warning (tmp);
3595 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3596 union_type, &chain);
3597 suppress_warning (tmp);
3598 gfc_finish_type (vec_type);
3599 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3602 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3603 gfc_rank_cst[dim-1]);
3604 vector_types[dim-1] = build_array_type (vec_type, tmp);
3605 return vector_types[dim-1];
3609 tree
3610 gfc_get_caf_reference_type ()
3612 static tree reference_type = NULL_TREE;
3613 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3614 a_struct_type, u_union_type, tmp, *chain;
3616 if (reference_type != NULL_TREE)
3617 return reference_type;
3619 chain = 0;
3620 c_struct_type = make_node (RECORD_TYPE);
3621 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3622 get_identifier ("offset"),
3623 gfc_array_index_type, &chain);
3624 suppress_warning (tmp);
3625 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3626 get_identifier ("caf_token_offset"),
3627 gfc_array_index_type, &chain);
3628 suppress_warning (tmp);
3629 gfc_finish_type (c_struct_type);
3631 chain = 0;
3632 s_struct_type = make_node (RECORD_TYPE);
3633 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3634 get_identifier ("start"),
3635 gfc_array_index_type, &chain);
3636 suppress_warning (tmp);
3637 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3638 get_identifier ("end"),
3639 gfc_array_index_type, &chain);
3640 suppress_warning (tmp);
3641 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3642 get_identifier ("stride"),
3643 gfc_array_index_type, &chain);
3644 suppress_warning (tmp);
3645 gfc_finish_type (s_struct_type);
3647 chain = 0;
3648 v_struct_type = make_node (RECORD_TYPE);
3649 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3650 get_identifier ("vector"),
3651 pvoid_type_node, &chain);
3652 suppress_warning (tmp);
3653 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3654 get_identifier ("nvec"),
3655 size_type_node, &chain);
3656 suppress_warning (tmp);
3657 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3658 get_identifier ("kind"),
3659 integer_type_node, &chain);
3660 suppress_warning (tmp);
3661 gfc_finish_type (v_struct_type);
3663 chain = 0;
3664 union_type = make_node (UNION_TYPE);
3665 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3666 s_struct_type, &chain);
3667 suppress_warning (tmp);
3668 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3669 v_struct_type, &chain);
3670 suppress_warning (tmp);
3671 gfc_finish_type (union_type);
3673 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3674 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3675 dim_union_type = build_array_type (union_type, tmp);
3677 chain = 0;
3678 a_struct_type = make_node (RECORD_TYPE);
3679 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3680 build_array_type (unsigned_char_type_node,
3681 build_range_type (gfc_array_index_type,
3682 gfc_index_zero_node,
3683 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3684 &chain);
3685 suppress_warning (tmp);
3686 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3687 get_identifier ("static_array_type"),
3688 integer_type_node, &chain);
3689 suppress_warning (tmp);
3690 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3691 dim_union_type, &chain);
3692 suppress_warning (tmp);
3693 gfc_finish_type (a_struct_type);
3695 chain = 0;
3696 u_union_type = make_node (UNION_TYPE);
3697 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3698 c_struct_type, &chain);
3699 suppress_warning (tmp);
3700 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3701 a_struct_type, &chain);
3702 suppress_warning (tmp);
3703 gfc_finish_type (u_union_type);
3705 chain = 0;
3706 reference_type = make_node (RECORD_TYPE);
3707 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3708 build_pointer_type (reference_type), &chain);
3709 suppress_warning (tmp);
3710 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3711 integer_type_node, &chain);
3712 suppress_warning (tmp);
3713 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3714 size_type_node, &chain);
3715 suppress_warning (tmp);
3716 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3717 u_union_type, &chain);
3718 suppress_warning (tmp);
3719 gfc_finish_type (reference_type);
3720 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3722 return reference_type;
3725 #include "gt-fortran-trans-types.h"