pr88074.c: Require c99_runtime.
[official-gcc.git] / gcc / fortran / trans-types.c
blob9ae516bb666417cab0a2c4537d679955c37e82ab
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2019 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"
43 #if (GFC_MAX_DIMENSIONS < 10)
44 #define GFC_RANK_DIGITS 1
45 #define GFC_RANK_PRINTF_FORMAT "%01d"
46 #elif (GFC_MAX_DIMENSIONS < 100)
47 #define GFC_RANK_DIGITS 2
48 #define GFC_RANK_PRINTF_FORMAT "%02d"
49 #else
50 #error If you really need >99 dimensions, continue the sequence above...
51 #endif
53 /* array of structs so we don't have to worry about xmalloc or free */
54 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
56 tree gfc_array_index_type;
57 tree gfc_array_range_type;
58 tree gfc_character1_type_node;
59 tree pvoid_type_node;
60 tree prvoid_type_node;
61 tree ppvoid_type_node;
62 tree pchar_type_node;
63 tree pfunc_type_node;
65 tree logical_type_node;
66 tree logical_true_node;
67 tree logical_false_node;
68 tree gfc_charlen_type_node;
70 tree gfc_float128_type_node = NULL_TREE;
71 tree gfc_complex_float128_type_node = NULL_TREE;
73 bool gfc_real16_is_float128 = false;
75 static GTY(()) tree gfc_desc_dim_type;
76 static GTY(()) tree gfc_max_array_element_size;
77 static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
78 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
80 /* Arrays for all integral and real kinds. We'll fill this in at runtime
81 after the target has a chance to process command-line options. */
83 #define MAX_INT_KINDS 5
84 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
85 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
86 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
87 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
89 #define MAX_REAL_KINDS 5
90 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
91 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
92 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
94 #define MAX_CHARACTER_KINDS 2
95 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
96 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
97 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
99 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
101 /* The integer kind to use for array indices. This will be set to the
102 proper value based on target information from the backend. */
104 int gfc_index_integer_kind;
106 /* The default kinds of the various types. */
108 int gfc_default_integer_kind;
109 int gfc_max_integer_kind;
110 int gfc_default_real_kind;
111 int gfc_default_double_kind;
112 int gfc_default_character_kind;
113 int gfc_default_logical_kind;
114 int gfc_default_complex_kind;
115 int gfc_c_int_kind;
116 int gfc_atomic_int_kind;
117 int gfc_atomic_logical_kind;
119 /* The kind size used for record offsets. If the target system supports
120 kind=8, this will be set to 8, otherwise it is set to 4. */
121 int gfc_intio_kind;
123 /* The integer kind used to store character lengths. */
124 int gfc_charlen_int_kind;
126 /* Kind of internal integer for storing object sizes. */
127 int gfc_size_kind;
129 /* The size of the numeric storage unit and character storage unit. */
130 int gfc_numeric_storage_size;
131 int gfc_character_storage_size;
133 tree dtype_type_node = NULL_TREE;
136 /* Build the dtype_type_node if necessary. */
137 tree get_dtype_type_node (void)
139 tree field;
140 tree dtype_node;
141 tree *dtype_chain = NULL;
143 if (dtype_type_node == NULL_TREE)
145 dtype_node = make_node (RECORD_TYPE);
146 TYPE_NAME (dtype_node) = get_identifier ("dtype_type");
147 TYPE_NAMELESS (dtype_node) = 1;
148 field = gfc_add_field_to_struct_1 (dtype_node,
149 get_identifier ("elem_len"),
150 size_type_node, &dtype_chain);
151 TREE_NO_WARNING (field) = 1;
152 field = gfc_add_field_to_struct_1 (dtype_node,
153 get_identifier ("version"),
154 integer_type_node, &dtype_chain);
155 TREE_NO_WARNING (field) = 1;
156 field = gfc_add_field_to_struct_1 (dtype_node,
157 get_identifier ("rank"),
158 signed_char_type_node, &dtype_chain);
159 TREE_NO_WARNING (field) = 1;
160 field = gfc_add_field_to_struct_1 (dtype_node,
161 get_identifier ("type"),
162 signed_char_type_node, &dtype_chain);
163 TREE_NO_WARNING (field) = 1;
164 field = gfc_add_field_to_struct_1 (dtype_node,
165 get_identifier ("attribute"),
166 short_integer_type_node, &dtype_chain);
167 TREE_NO_WARNING (field) = 1;
168 gfc_finish_type (dtype_node);
169 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node)) = 1;
170 dtype_type_node = dtype_node;
172 return dtype_type_node;
175 bool
176 gfc_check_any_c_kind (gfc_typespec *ts)
178 int i;
180 for (i = 0; i < ISOCBINDING_NUMBER; i++)
182 /* Check for any C interoperable kind for the given type/kind in ts.
183 This can be used after verify_c_interop to make sure that the
184 Fortran kind being used exists in at least some form for C. */
185 if (c_interop_kinds_table[i].f90_type == ts->type &&
186 c_interop_kinds_table[i].value == ts->kind)
187 return true;
190 return false;
194 static int
195 get_real_kind_from_node (tree type)
197 int i;
199 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
200 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
201 return gfc_real_kinds[i].kind;
203 return -4;
206 static int
207 get_int_kind_from_node (tree type)
209 int i;
211 if (!type)
212 return -2;
214 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
215 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
216 return gfc_integer_kinds[i].kind;
218 return -1;
221 static int
222 get_int_kind_from_name (const char *name)
224 return get_int_kind_from_node (get_typenode_from_name (name));
228 /* Get the kind number corresponding to an integer of given size,
229 following the required return values for ISO_FORTRAN_ENV INT* constants:
230 -2 is returned if we support a kind of larger size, -1 otherwise. */
232 gfc_get_int_kind_from_width_isofortranenv (int size)
234 int i;
236 /* Look for a kind with matching storage size. */
237 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
238 if (gfc_integer_kinds[i].bit_size == size)
239 return gfc_integer_kinds[i].kind;
241 /* Look for a kind with larger storage size. */
242 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
243 if (gfc_integer_kinds[i].bit_size > size)
244 return -2;
246 return -1;
250 /* Get the kind number corresponding to a real of a given storage size.
251 If two real's have the same storage size, then choose the real with
252 the largest precision. If a kind type is unavailable and a real
253 exists with wider storage, then return -2; otherwise, return -1. */
256 gfc_get_real_kind_from_width_isofortranenv (int size)
258 int digits, i, kind;
260 size /= 8;
262 kind = -1;
263 digits = 0;
265 /* Look for a kind with matching storage size. */
266 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
267 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
269 if (gfc_real_kinds[i].digits > digits)
271 digits = gfc_real_kinds[i].digits;
272 kind = gfc_real_kinds[i].kind;
276 if (kind != -1)
277 return kind;
279 /* Look for a kind with larger storage size. */
280 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
281 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
282 kind = -2;
284 return kind;
289 static int
290 get_int_kind_from_width (int size)
292 int i;
294 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
295 if (gfc_integer_kinds[i].bit_size == size)
296 return gfc_integer_kinds[i].kind;
298 return -2;
301 static int
302 get_int_kind_from_minimal_width (int size)
304 int i;
306 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
307 if (gfc_integer_kinds[i].bit_size >= size)
308 return gfc_integer_kinds[i].kind;
310 return -2;
314 /* Generate the CInteropKind_t objects for the C interoperable
315 kinds. */
317 void
318 gfc_init_c_interop_kinds (void)
320 int i;
322 /* init all pointers in the list to NULL */
323 for (i = 0; i < ISOCBINDING_NUMBER; i++)
325 /* Initialize the name and value fields. */
326 c_interop_kinds_table[i].name[0] = '\0';
327 c_interop_kinds_table[i].value = -100;
328 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
331 #define NAMED_INTCST(a,b,c,d) \
332 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
333 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
334 c_interop_kinds_table[a].value = c;
335 #define NAMED_REALCST(a,b,c,d) \
336 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
337 c_interop_kinds_table[a].f90_type = BT_REAL; \
338 c_interop_kinds_table[a].value = c;
339 #define NAMED_CMPXCST(a,b,c,d) \
340 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
341 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
342 c_interop_kinds_table[a].value = c;
343 #define NAMED_LOGCST(a,b,c) \
344 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
345 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
346 c_interop_kinds_table[a].value = c;
347 #define NAMED_CHARKNDCST(a,b,c) \
348 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
349 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
350 c_interop_kinds_table[a].value = c;
351 #define NAMED_CHARCST(a,b,c) \
352 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
353 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
354 c_interop_kinds_table[a].value = c;
355 #define DERIVED_TYPE(a,b,c) \
356 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
357 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
358 c_interop_kinds_table[a].value = c;
359 #define NAMED_FUNCTION(a,b,c,d) \
360 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
361 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
362 c_interop_kinds_table[a].value = c;
363 #define NAMED_SUBROUTINE(a,b,c,d) \
364 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
365 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
366 c_interop_kinds_table[a].value = c;
367 #include "iso-c-binding.def"
371 /* Query the target to determine which machine modes are available for
372 computation. Choose KIND numbers for them. */
374 void
375 gfc_init_kinds (void)
377 opt_scalar_int_mode int_mode_iter;
378 opt_scalar_float_mode float_mode_iter;
379 int i_index, r_index, kind;
380 bool saw_i4 = false, saw_i8 = false;
381 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
383 i_index = 0;
384 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
386 scalar_int_mode mode = int_mode_iter.require ();
387 int kind, bitsize;
389 if (!targetm.scalar_mode_supported_p (mode))
390 continue;
392 /* The middle end doesn't support constants larger than 2*HWI.
393 Perhaps the target hook shouldn't have accepted these either,
394 but just to be safe... */
395 bitsize = GET_MODE_BITSIZE (mode);
396 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
397 continue;
399 gcc_assert (i_index != MAX_INT_KINDS);
401 /* Let the kind equal the bit size divided by 8. This insulates the
402 programmer from the underlying byte size. */
403 kind = bitsize / 8;
405 if (kind == 4)
406 saw_i4 = true;
407 if (kind == 8)
408 saw_i8 = true;
410 gfc_integer_kinds[i_index].kind = kind;
411 gfc_integer_kinds[i_index].radix = 2;
412 gfc_integer_kinds[i_index].digits = bitsize - 1;
413 gfc_integer_kinds[i_index].bit_size = bitsize;
415 gfc_logical_kinds[i_index].kind = kind;
416 gfc_logical_kinds[i_index].bit_size = bitsize;
418 i_index += 1;
421 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
422 used for large file access. */
424 if (saw_i8)
425 gfc_intio_kind = 8;
426 else
427 gfc_intio_kind = 4;
429 /* If we do not at least have kind = 4, everything is pointless. */
430 gcc_assert(saw_i4);
432 /* Set the maximum integer kind. Used with at least BOZ constants. */
433 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
435 r_index = 0;
436 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
438 scalar_float_mode mode = float_mode_iter.require ();
439 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
440 int kind;
442 if (fmt == NULL)
443 continue;
444 if (!targetm.scalar_mode_supported_p (mode))
445 continue;
447 /* Only let float, double, long double and __float128 go through.
448 Runtime support for others is not provided, so they would be
449 useless. */
450 if (!targetm.libgcc_floating_mode_supported_p (mode))
451 continue;
452 if (mode != TYPE_MODE (float_type_node)
453 && (mode != TYPE_MODE (double_type_node))
454 && (mode != TYPE_MODE (long_double_type_node))
455 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
456 && (mode != TFmode)
457 #endif
459 continue;
461 /* Let the kind equal the precision divided by 8, rounding up. Again,
462 this insulates the programmer from the underlying byte size.
464 Also, it effectively deals with IEEE extended formats. There, the
465 total size of the type may equal 16, but it's got 6 bytes of padding
466 and the increased size can get in the way of a real IEEE quad format
467 which may also be supported by the target.
469 We round up so as to handle IA-64 __floatreg (RFmode), which is an
470 82 bit type. Not to be confused with __float80 (XFmode), which is
471 an 80 bit type also supported by IA-64. So XFmode should come out
472 to be kind=10, and RFmode should come out to be kind=11. Egads. */
474 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
476 if (kind == 4)
477 saw_r4 = true;
478 if (kind == 8)
479 saw_r8 = true;
480 if (kind == 10)
481 saw_r10 = true;
482 if (kind == 16)
483 saw_r16 = true;
485 /* Careful we don't stumble a weird internal mode. */
486 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
487 /* Or have too many modes for the allocated space. */
488 gcc_assert (r_index != MAX_REAL_KINDS);
490 gfc_real_kinds[r_index].kind = kind;
491 gfc_real_kinds[r_index].radix = fmt->b;
492 gfc_real_kinds[r_index].digits = fmt->p;
493 gfc_real_kinds[r_index].min_exponent = fmt->emin;
494 gfc_real_kinds[r_index].max_exponent = fmt->emax;
495 if (fmt->pnan < fmt->p)
496 /* This is an IBM extended double format (or the MIPS variant)
497 made up of two IEEE doubles. The value of the long double is
498 the sum of the values of the two parts. The most significant
499 part is required to be the value of the long double rounded
500 to the nearest double. If we use emax of 1024 then we can't
501 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
502 rounding will make the most significant part overflow. */
503 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
504 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
505 r_index += 1;
508 /* Choose the default integer kind. We choose 4 unless the user directs us
509 otherwise. Even if the user specified that the default integer kind is 8,
510 the numeric storage size is not 64 bits. In this case, a warning will be
511 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
513 gfc_numeric_storage_size = 4 * 8;
515 if (flag_default_integer)
517 if (!saw_i8)
518 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
519 "%<-fdefault-integer-8%> option");
521 gfc_default_integer_kind = 8;
524 else if (flag_integer4_kind == 8)
526 if (!saw_i8)
527 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
528 "%<-finteger-4-integer-8%> option");
530 gfc_default_integer_kind = 8;
532 else if (saw_i4)
534 gfc_default_integer_kind = 4;
536 else
538 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
539 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
542 /* Choose the default real kind. Again, we choose 4 when possible. */
543 if (flag_default_real_8)
545 if (!saw_r8)
546 gfc_fatal_error ("REAL(KIND=8) is not available for "
547 "%<-fdefault-real-8%> option");
549 gfc_default_real_kind = 8;
551 else if (flag_default_real_10)
553 if (!saw_r10)
554 gfc_fatal_error ("REAL(KIND=10) is not available for "
555 "%<-fdefault-real-10%> option");
557 gfc_default_real_kind = 10;
559 else if (flag_default_real_16)
561 if (!saw_r16)
562 gfc_fatal_error ("REAL(KIND=16) is not available for "
563 "%<-fdefault-real-16%> option");
565 gfc_default_real_kind = 16;
567 else if (flag_real4_kind == 8)
569 if (!saw_r8)
570 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
571 "option");
573 gfc_default_real_kind = 8;
575 else if (flag_real4_kind == 10)
577 if (!saw_r10)
578 gfc_fatal_error ("REAL(KIND=10) is not available for "
579 "%<-freal-4-real-10%> option");
581 gfc_default_real_kind = 10;
583 else if (flag_real4_kind == 16)
585 if (!saw_r16)
586 gfc_fatal_error ("REAL(KIND=16) is not available for "
587 "%<-freal-4-real-16%> option");
589 gfc_default_real_kind = 16;
591 else if (saw_r4)
592 gfc_default_real_kind = 4;
593 else
594 gfc_default_real_kind = gfc_real_kinds[0].kind;
596 /* Choose the default double kind. If -fdefault-real and -fdefault-double
597 are specified, we use kind=8, if it's available. If -fdefault-real is
598 specified without -fdefault-double, we use kind=16, if it's available.
599 Otherwise we do not change anything. */
600 if (flag_default_double && saw_r8)
601 gfc_default_double_kind = 8;
602 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
604 /* Use largest available kind. */
605 if (saw_r16)
606 gfc_default_double_kind = 16;
607 else if (saw_r10)
608 gfc_default_double_kind = 10;
609 else if (saw_r8)
610 gfc_default_double_kind = 8;
611 else
612 gfc_default_double_kind = gfc_default_real_kind;
614 else if (flag_real8_kind == 4)
616 if (!saw_r4)
617 gfc_fatal_error ("REAL(KIND=4) is not available for "
618 "%<-freal-8-real-4%> option");
620 gfc_default_double_kind = 4;
622 else if (flag_real8_kind == 10 )
624 if (!saw_r10)
625 gfc_fatal_error ("REAL(KIND=10) is not available for "
626 "%<-freal-8-real-10%> option");
628 gfc_default_double_kind = 10;
630 else if (flag_real8_kind == 16 )
632 if (!saw_r16)
633 gfc_fatal_error ("REAL(KIND=10) is not available for "
634 "%<-freal-8-real-16%> option");
636 gfc_default_double_kind = 16;
638 else if (saw_r4 && saw_r8)
639 gfc_default_double_kind = 8;
640 else
642 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
643 real ... occupies two contiguous numeric storage units.
645 Therefore we must be supplied a kind twice as large as we chose
646 for single precision. There are loopholes, in that double
647 precision must *occupy* two storage units, though it doesn't have
648 to *use* two storage units. Which means that you can make this
649 kind artificially wide by padding it. But at present there are
650 no GCC targets for which a two-word type does not exist, so we
651 just let gfc_validate_kind abort and tell us if something breaks. */
653 gfc_default_double_kind
654 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
657 /* The default logical kind is constrained to be the same as the
658 default integer kind. Similarly with complex and real. */
659 gfc_default_logical_kind = gfc_default_integer_kind;
660 gfc_default_complex_kind = gfc_default_real_kind;
662 /* We only have two character kinds: ASCII and UCS-4.
663 ASCII corresponds to a 8-bit integer type, if one is available.
664 UCS-4 corresponds to a 32-bit integer type, if one is available. */
665 i_index = 0;
666 if ((kind = get_int_kind_from_width (8)) > 0)
668 gfc_character_kinds[i_index].kind = kind;
669 gfc_character_kinds[i_index].bit_size = 8;
670 gfc_character_kinds[i_index].name = "ascii";
671 i_index++;
673 if ((kind = get_int_kind_from_width (32)) > 0)
675 gfc_character_kinds[i_index].kind = kind;
676 gfc_character_kinds[i_index].bit_size = 32;
677 gfc_character_kinds[i_index].name = "iso_10646";
678 i_index++;
681 /* Choose the smallest integer kind for our default character. */
682 gfc_default_character_kind = gfc_character_kinds[0].kind;
683 gfc_character_storage_size = gfc_default_character_kind * 8;
685 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
687 /* Pick a kind the same size as the C "int" type. */
688 gfc_c_int_kind = INT_TYPE_SIZE / 8;
690 /* Choose atomic kinds to match C's int. */
691 gfc_atomic_int_kind = gfc_c_int_kind;
692 gfc_atomic_logical_kind = gfc_c_int_kind;
696 /* Make sure that a valid kind is present. Returns an index into the
697 associated kinds array, -1 if the kind is not present. */
699 static int
700 validate_integer (int kind)
702 int i;
704 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
705 if (gfc_integer_kinds[i].kind == kind)
706 return i;
708 return -1;
711 static int
712 validate_real (int kind)
714 int i;
716 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
717 if (gfc_real_kinds[i].kind == kind)
718 return i;
720 return -1;
723 static int
724 validate_logical (int kind)
726 int i;
728 for (i = 0; gfc_logical_kinds[i].kind; i++)
729 if (gfc_logical_kinds[i].kind == kind)
730 return i;
732 return -1;
735 static int
736 validate_character (int kind)
738 int i;
740 for (i = 0; gfc_character_kinds[i].kind; i++)
741 if (gfc_character_kinds[i].kind == kind)
742 return i;
744 return -1;
747 /* Validate a kind given a basic type. The return value is the same
748 for the child functions, with -1 indicating nonexistence of the
749 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
752 gfc_validate_kind (bt type, int kind, bool may_fail)
754 int rc;
756 switch (type)
758 case BT_REAL: /* Fall through */
759 case BT_COMPLEX:
760 rc = validate_real (kind);
761 break;
762 case BT_INTEGER:
763 rc = validate_integer (kind);
764 break;
765 case BT_LOGICAL:
766 rc = validate_logical (kind);
767 break;
768 case BT_CHARACTER:
769 rc = validate_character (kind);
770 break;
772 default:
773 gfc_internal_error ("gfc_validate_kind(): Got bad type");
776 if (rc < 0 && !may_fail)
777 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
779 return rc;
783 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
784 Reuse common type nodes where possible. Recognize if the kind matches up
785 with a C type. This will be used later in determining which routines may
786 be scarfed from libm. */
788 static tree
789 gfc_build_int_type (gfc_integer_info *info)
791 int mode_precision = info->bit_size;
793 if (mode_precision == CHAR_TYPE_SIZE)
794 info->c_char = 1;
795 if (mode_precision == SHORT_TYPE_SIZE)
796 info->c_short = 1;
797 if (mode_precision == INT_TYPE_SIZE)
798 info->c_int = 1;
799 if (mode_precision == LONG_TYPE_SIZE)
800 info->c_long = 1;
801 if (mode_precision == LONG_LONG_TYPE_SIZE)
802 info->c_long_long = 1;
804 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
805 return intQI_type_node;
806 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
807 return intHI_type_node;
808 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
809 return intSI_type_node;
810 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
811 return intDI_type_node;
812 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
813 return intTI_type_node;
815 return make_signed_type (mode_precision);
818 tree
819 gfc_build_uint_type (int size)
821 if (size == CHAR_TYPE_SIZE)
822 return unsigned_char_type_node;
823 if (size == SHORT_TYPE_SIZE)
824 return short_unsigned_type_node;
825 if (size == INT_TYPE_SIZE)
826 return unsigned_type_node;
827 if (size == LONG_TYPE_SIZE)
828 return long_unsigned_type_node;
829 if (size == LONG_LONG_TYPE_SIZE)
830 return long_long_unsigned_type_node;
832 return make_unsigned_type (size);
836 static tree
837 gfc_build_real_type (gfc_real_info *info)
839 int mode_precision = info->mode_precision;
840 tree new_type;
842 if (mode_precision == FLOAT_TYPE_SIZE)
843 info->c_float = 1;
844 if (mode_precision == DOUBLE_TYPE_SIZE)
845 info->c_double = 1;
846 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
847 info->c_long_double = 1;
848 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
850 info->c_float128 = 1;
851 gfc_real16_is_float128 = true;
854 if (TYPE_PRECISION (float_type_node) == mode_precision)
855 return float_type_node;
856 if (TYPE_PRECISION (double_type_node) == mode_precision)
857 return double_type_node;
858 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
859 return long_double_type_node;
861 new_type = make_node (REAL_TYPE);
862 TYPE_PRECISION (new_type) = mode_precision;
863 layout_type (new_type);
864 return new_type;
867 static tree
868 gfc_build_complex_type (tree scalar_type)
870 tree new_type;
872 if (scalar_type == NULL)
873 return NULL;
874 if (scalar_type == float_type_node)
875 return complex_float_type_node;
876 if (scalar_type == double_type_node)
877 return complex_double_type_node;
878 if (scalar_type == long_double_type_node)
879 return complex_long_double_type_node;
881 new_type = make_node (COMPLEX_TYPE);
882 TREE_TYPE (new_type) = scalar_type;
883 layout_type (new_type);
884 return new_type;
887 static tree
888 gfc_build_logical_type (gfc_logical_info *info)
890 int bit_size = info->bit_size;
891 tree new_type;
893 if (bit_size == BOOL_TYPE_SIZE)
895 info->c_bool = 1;
896 return boolean_type_node;
899 new_type = make_unsigned_type (bit_size);
900 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
901 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
902 TYPE_PRECISION (new_type) = 1;
904 return new_type;
908 /* Create the backend type nodes. We map them to their
909 equivalent C type, at least for now. We also give
910 names to the types here, and we push them in the
911 global binding level context.*/
913 void
914 gfc_init_types (void)
916 char name_buf[26];
917 int index;
918 tree type;
919 unsigned n;
921 /* Create and name the types. */
922 #define PUSH_TYPE(name, node) \
923 pushdecl (build_decl (input_location, \
924 TYPE_DECL, get_identifier (name), node))
926 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
928 type = gfc_build_int_type (&gfc_integer_kinds[index]);
929 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
930 if (TYPE_STRING_FLAG (type))
931 type = make_signed_type (gfc_integer_kinds[index].bit_size);
932 gfc_integer_types[index] = type;
933 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
934 gfc_integer_kinds[index].kind);
935 PUSH_TYPE (name_buf, type);
938 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
940 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
941 gfc_logical_types[index] = type;
942 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
943 gfc_logical_kinds[index].kind);
944 PUSH_TYPE (name_buf, type);
947 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
949 type = gfc_build_real_type (&gfc_real_kinds[index]);
950 gfc_real_types[index] = type;
951 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
952 gfc_real_kinds[index].kind);
953 PUSH_TYPE (name_buf, type);
955 if (gfc_real_kinds[index].c_float128)
956 gfc_float128_type_node = type;
958 type = gfc_build_complex_type (type);
959 gfc_complex_types[index] = type;
960 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
961 gfc_real_kinds[index].kind);
962 PUSH_TYPE (name_buf, type);
964 if (gfc_real_kinds[index].c_float128)
965 gfc_complex_float128_type_node = type;
968 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
970 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
971 type = build_qualified_type (type, TYPE_UNQUALIFIED);
972 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
973 gfc_character_kinds[index].kind);
974 PUSH_TYPE (name_buf, type);
975 gfc_character_types[index] = type;
976 gfc_pcharacter_types[index] = build_pointer_type (type);
978 gfc_character1_type_node = gfc_character_types[0];
980 PUSH_TYPE ("byte", unsigned_char_type_node);
981 PUSH_TYPE ("void", void_type_node);
983 /* DBX debugging output gets upset if these aren't set. */
984 if (!TYPE_NAME (integer_type_node))
985 PUSH_TYPE ("c_integer", integer_type_node);
986 if (!TYPE_NAME (char_type_node))
987 PUSH_TYPE ("c_char", char_type_node);
989 #undef PUSH_TYPE
991 pvoid_type_node = build_pointer_type (void_type_node);
992 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
993 ppvoid_type_node = build_pointer_type (pvoid_type_node);
994 pchar_type_node = build_pointer_type (gfc_character1_type_node);
995 pfunc_type_node
996 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
998 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
999 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1000 since this function is called before gfc_init_constants. */
1001 gfc_array_range_type
1002 = build_range_type (gfc_array_index_type,
1003 build_int_cst (gfc_array_index_type, 0),
1004 NULL_TREE);
1006 /* The maximum array element size that can be handled is determined
1007 by the number of bits available to store this field in the array
1008 descriptor. */
1010 n = TYPE_PRECISION (size_type_node);
1011 gfc_max_array_element_size
1012 = wide_int_to_tree (size_type_node,
1013 wi::mask (n, UNSIGNED,
1014 TYPE_PRECISION (size_type_node)));
1016 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1017 logical_true_node = build_int_cst (logical_type_node, 1);
1018 logical_false_node = build_int_cst (logical_type_node, 0);
1020 /* Character lengths are of type size_t, except signed. */
1021 gfc_charlen_int_kind = get_int_kind_from_node (size_type_node);
1022 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1024 /* Fortran kind number of size_type_node (size_t). This is used for
1025 the _size member in vtables. */
1026 gfc_size_kind = get_int_kind_from_node (size_type_node);
1029 /* Get the type node for the given type and kind. */
1031 tree
1032 gfc_get_int_type (int kind)
1034 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1035 return index < 0 ? 0 : gfc_integer_types[index];
1038 tree
1039 gfc_get_real_type (int kind)
1041 int index = gfc_validate_kind (BT_REAL, kind, true);
1042 return index < 0 ? 0 : gfc_real_types[index];
1045 tree
1046 gfc_get_complex_type (int kind)
1048 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1049 return index < 0 ? 0 : gfc_complex_types[index];
1052 tree
1053 gfc_get_logical_type (int kind)
1055 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1056 return index < 0 ? 0 : gfc_logical_types[index];
1059 tree
1060 gfc_get_char_type (int kind)
1062 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1063 return index < 0 ? 0 : gfc_character_types[index];
1066 tree
1067 gfc_get_pchar_type (int kind)
1069 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1070 return index < 0 ? 0 : gfc_pcharacter_types[index];
1074 /* Create a character type with the given kind and length. */
1076 tree
1077 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1079 tree bounds, type;
1081 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1082 type = build_array_type (eltype, bounds);
1083 TYPE_STRING_FLAG (type) = 1;
1085 return type;
1088 tree
1089 gfc_get_character_type_len (int kind, tree len)
1091 gfc_validate_kind (BT_CHARACTER, kind, false);
1092 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1096 /* Get a type node for a character kind. */
1098 tree
1099 gfc_get_character_type (int kind, gfc_charlen * cl)
1101 tree len;
1103 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1104 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1105 len = build_fold_indirect_ref (len);
1107 return gfc_get_character_type_len (kind, len);
1110 /* Convert a basic type. This will be an array for character types. */
1112 tree
1113 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1115 tree basetype;
1117 switch (spec->type)
1119 case BT_UNKNOWN:
1120 gcc_unreachable ();
1122 case BT_INTEGER:
1123 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1124 has been resolved. This is done so we can convert C_PTR and
1125 C_FUNPTR to simple variables that get translated to (void *). */
1126 if (spec->f90_type == BT_VOID)
1128 if (spec->u.derived
1129 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1130 basetype = ptr_type_node;
1131 else
1132 basetype = pfunc_type_node;
1134 else
1135 basetype = gfc_get_int_type (spec->kind);
1136 break;
1138 case BT_REAL:
1139 basetype = gfc_get_real_type (spec->kind);
1140 break;
1142 case BT_COMPLEX:
1143 basetype = gfc_get_complex_type (spec->kind);
1144 break;
1146 case BT_LOGICAL:
1147 basetype = gfc_get_logical_type (spec->kind);
1148 break;
1150 case BT_CHARACTER:
1151 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1152 break;
1154 case BT_HOLLERITH:
1155 /* Since this cannot be used, return a length one character. */
1156 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1157 gfc_index_one_node);
1158 break;
1160 case BT_UNION:
1161 basetype = gfc_get_union_type (spec->u.derived);
1162 break;
1164 case BT_DERIVED:
1165 case BT_CLASS:
1166 basetype = gfc_get_derived_type (spec->u.derived, codim);
1168 if (spec->type == BT_CLASS)
1169 GFC_CLASS_TYPE_P (basetype) = 1;
1171 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1172 type and kind to fit a (void *) and the basetype returned was a
1173 ptr_type_node. We need to pass up this new information to the
1174 symbol that was declared of type C_PTR or C_FUNPTR. */
1175 if (spec->u.derived->ts.f90_type == BT_VOID)
1177 spec->type = BT_INTEGER;
1178 spec->kind = gfc_index_integer_kind;
1179 spec->f90_type = BT_VOID;
1180 spec->is_c_interop = 1; /* Mark as escaping later. */
1182 break;
1183 case BT_VOID:
1184 case BT_ASSUMED:
1185 /* This is for the second arg to c_f_pointer and c_f_procpointer
1186 of the iso_c_binding module, to accept any ptr type. */
1187 basetype = ptr_type_node;
1188 if (spec->f90_type == BT_VOID)
1190 if (spec->u.derived
1191 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1192 basetype = ptr_type_node;
1193 else
1194 basetype = pfunc_type_node;
1196 break;
1197 default:
1198 gcc_unreachable ();
1200 return basetype;
1203 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1205 static tree
1206 gfc_conv_array_bound (gfc_expr * expr)
1208 /* If expr is an integer constant, return that. */
1209 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1210 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1212 /* Otherwise return NULL. */
1213 return NULL_TREE;
1216 /* Return the type of an element of the array. Note that scalar coarrays
1217 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1218 (with POINTER_TYPE stripped) is returned. */
1220 tree
1221 gfc_get_element_type (tree type)
1223 tree element;
1225 if (GFC_ARRAY_TYPE_P (type))
1227 if (TREE_CODE (type) == POINTER_TYPE)
1228 type = TREE_TYPE (type);
1229 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1231 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1232 element = type;
1234 else
1236 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1237 element = TREE_TYPE (type);
1240 else
1242 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1243 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1245 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1246 element = TREE_TYPE (element);
1248 /* For arrays, which are not scalar coarrays. */
1249 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1250 element = TREE_TYPE (element);
1253 return element;
1256 /* Build an array. This function is called from gfc_sym_type().
1257 Actually returns array descriptor type.
1259 Format of array descriptors is as follows:
1261 struct gfc_array_descriptor
1263 array *data;
1264 index offset;
1265 struct dtype_type dtype;
1266 struct descriptor_dimension dimension[N_DIM];
1269 struct dtype_type
1271 size_t elem_len;
1272 int version;
1273 signed char rank;
1274 signed char type;
1275 signed short attribute;
1278 struct descriptor_dimension
1280 index stride;
1281 index lbound;
1282 index ubound;
1285 Translation code should use gfc_conv_descriptor_* rather than
1286 accessing the descriptor directly. Any changes to the array
1287 descriptor type will require changes in gfc_conv_descriptor_* and
1288 gfc_build_array_initializer.
1290 This is represented internally as a RECORD_TYPE. The index nodes
1291 are gfc_array_index_type and the data node is a pointer to the
1292 data. See below for the handling of character types.
1294 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1295 this generated poor code for assumed/deferred size arrays. These
1296 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1297 of the GENERIC grammar. Also, there is no way to explicitly set
1298 the array stride, so all data must be packed(1). I've tried to
1299 mark all the functions which would require modification with a GCC
1300 ARRAYS comment.
1302 The data component points to the first element in the array. The
1303 offset field is the position of the origin of the array (i.e. element
1304 (0, 0 ...)). This may be outside the bounds of the array.
1306 An element is accessed by
1307 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1308 This gives good performance as the computation does not involve the
1309 bounds of the array. For packed arrays, this is optimized further
1310 by substituting the known strides.
1312 This system has one problem: all array bounds must be within 2^31
1313 elements of the origin (2^63 on 64-bit machines). For example
1314 integer, dimension (80000:90000, 80000:90000, 2) :: array
1315 may not work properly on 32-bit machines because 80000*80000 >
1316 2^31, so the calculation for stride2 would overflow. This may
1317 still work, but I haven't checked, and it relies on the overflow
1318 doing the right thing.
1320 The way to fix this problem is to access elements as follows:
1321 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1322 Obviously this is much slower. I will make this a compile time
1323 option, something like -fsmall-array-offsets. Mixing code compiled
1324 with and without this switch will work.
1326 (1) This can be worked around by modifying the upper bound of the
1327 previous dimension. This requires extra fields in the descriptor
1328 (both real_ubound and fake_ubound). */
1331 /* Returns true if the array sym does not require a descriptor. */
1334 gfc_is_nodesc_array (gfc_symbol * sym)
1336 symbol_attribute *array_attr;
1337 gfc_array_spec *as;
1338 bool is_classarray = IS_CLASS_ARRAY (sym);
1340 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1341 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1343 gcc_assert (array_attr->dimension || array_attr->codimension);
1345 /* We only want local arrays. */
1346 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1347 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1348 || array_attr->allocatable)
1349 return 0;
1351 /* We want a descriptor for associate-name arrays that do not have an
1352 explicitly known shape already. */
1353 if (sym->assoc && as->type != AS_EXPLICIT)
1354 return 0;
1356 /* The dummy is stored in sym and not in the component. */
1357 if (sym->attr.dummy)
1358 return as->type != AS_ASSUMED_SHAPE
1359 && as->type != AS_ASSUMED_RANK;
1361 if (sym->attr.result || sym->attr.function)
1362 return 0;
1364 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1366 return 1;
1370 /* Create an array descriptor type. */
1372 static tree
1373 gfc_build_array_type (tree type, gfc_array_spec * as,
1374 enum gfc_array_kind akind, bool restricted,
1375 bool contiguous, int codim)
1377 tree lbound[GFC_MAX_DIMENSIONS];
1378 tree ubound[GFC_MAX_DIMENSIONS];
1379 int n, corank;
1381 /* Assumed-shape arrays do not have codimension information stored in the
1382 descriptor. */
1383 corank = MAX (as->corank, codim);
1384 if (as->type == AS_ASSUMED_SHAPE ||
1385 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1386 corank = codim;
1388 if (as->type == AS_ASSUMED_RANK)
1389 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1391 lbound[n] = NULL_TREE;
1392 ubound[n] = NULL_TREE;
1395 for (n = 0; n < as->rank; n++)
1397 /* Create expressions for the known bounds of the array. */
1398 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1399 lbound[n] = gfc_index_one_node;
1400 else
1401 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1402 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1405 for (n = as->rank; n < as->rank + corank; n++)
1407 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1408 lbound[n] = gfc_index_one_node;
1409 else
1410 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1412 if (n < as->rank + corank - 1)
1413 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1416 if (as->type == AS_ASSUMED_SHAPE)
1417 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1418 : GFC_ARRAY_ASSUMED_SHAPE;
1419 else if (as->type == AS_ASSUMED_RANK)
1420 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1421 : GFC_ARRAY_ASSUMED_RANK;
1422 return gfc_get_array_type_bounds (type, as->rank == -1
1423 ? GFC_MAX_DIMENSIONS : as->rank,
1424 corank, lbound, ubound, 0, akind,
1425 restricted);
1428 /* Returns the struct descriptor_dimension type. */
1430 static tree
1431 gfc_get_desc_dim_type (void)
1433 tree type;
1434 tree decl, *chain = NULL;
1436 if (gfc_desc_dim_type)
1437 return gfc_desc_dim_type;
1439 /* Build the type node. */
1440 type = make_node (RECORD_TYPE);
1442 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1443 TYPE_PACKED (type) = 1;
1445 /* Consists of the stride, lbound and ubound members. */
1446 decl = gfc_add_field_to_struct_1 (type,
1447 get_identifier ("stride"),
1448 gfc_array_index_type, &chain);
1449 TREE_NO_WARNING (decl) = 1;
1451 decl = gfc_add_field_to_struct_1 (type,
1452 get_identifier ("lbound"),
1453 gfc_array_index_type, &chain);
1454 TREE_NO_WARNING (decl) = 1;
1456 decl = gfc_add_field_to_struct_1 (type,
1457 get_identifier ("ubound"),
1458 gfc_array_index_type, &chain);
1459 TREE_NO_WARNING (decl) = 1;
1461 /* Finish off the type. */
1462 gfc_finish_type (type);
1463 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1465 gfc_desc_dim_type = type;
1466 return type;
1470 /* Return the DTYPE for an array. This describes the type and type parameters
1471 of the array. */
1472 /* TODO: Only call this when the value is actually used, and make all the
1473 unknown cases abort. */
1475 tree
1476 gfc_get_dtype_rank_type (int rank, tree etype)
1478 tree size;
1479 int n;
1480 tree tmp;
1481 tree dtype;
1482 tree field;
1483 vec<constructor_elt, va_gc> *v = NULL;
1485 size = TYPE_SIZE_UNIT (etype);
1487 switch (TREE_CODE (etype))
1489 case INTEGER_TYPE:
1490 n = BT_INTEGER;
1491 break;
1493 case BOOLEAN_TYPE:
1494 n = BT_LOGICAL;
1495 break;
1497 case REAL_TYPE:
1498 n = BT_REAL;
1499 break;
1501 case COMPLEX_TYPE:
1502 n = BT_COMPLEX;
1503 break;
1505 case RECORD_TYPE:
1506 if (GFC_CLASS_TYPE_P (etype))
1507 n = BT_CLASS;
1508 else
1509 n = BT_DERIVED;
1510 break;
1512 /* We will never have arrays of arrays. */
1513 case ARRAY_TYPE:
1514 n = BT_CHARACTER;
1515 if (size == NULL_TREE)
1516 size = TYPE_SIZE_UNIT (TREE_TYPE (etype));
1517 break;
1519 case POINTER_TYPE:
1520 n = BT_ASSUMED;
1521 if (TREE_CODE (TREE_TYPE (etype)) != VOID_TYPE)
1522 size = TYPE_SIZE_UNIT (TREE_TYPE (etype));
1523 else
1524 size = build_int_cst (size_type_node, 0);
1525 break;
1527 default:
1528 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1529 /* We can encounter strange array types for temporary arrays. */
1530 return gfc_index_zero_node;
1533 tmp = get_dtype_type_node ();
1534 field = gfc_advance_chain (TYPE_FIELDS (tmp),
1535 GFC_DTYPE_ELEM_LEN);
1536 CONSTRUCTOR_APPEND_ELT (v, field,
1537 fold_convert (TREE_TYPE (field), size));
1539 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1540 GFC_DTYPE_RANK);
1541 CONSTRUCTOR_APPEND_ELT (v, field,
1542 build_int_cst (TREE_TYPE (field), rank));
1544 field = gfc_advance_chain (TYPE_FIELDS (dtype_type_node),
1545 GFC_DTYPE_TYPE);
1546 CONSTRUCTOR_APPEND_ELT (v, field,
1547 build_int_cst (TREE_TYPE (field), n));
1549 dtype = build_constructor (tmp, v);
1551 return dtype;
1555 tree
1556 gfc_get_dtype (tree type)
1558 tree dtype;
1559 tree etype;
1560 int rank;
1562 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1564 rank = GFC_TYPE_ARRAY_RANK (type);
1565 etype = gfc_get_element_type (type);
1566 dtype = gfc_get_dtype_rank_type (rank, etype);
1568 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1569 return dtype;
1573 /* Build an array type for use without a descriptor, packed according
1574 to the value of PACKED. */
1576 tree
1577 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1578 bool restricted)
1580 tree range;
1581 tree type;
1582 tree tmp;
1583 int n;
1584 int known_stride;
1585 int known_offset;
1586 mpz_t offset;
1587 mpz_t stride;
1588 mpz_t delta;
1589 gfc_expr *expr;
1591 mpz_init_set_ui (offset, 0);
1592 mpz_init_set_ui (stride, 1);
1593 mpz_init (delta);
1595 /* We don't use build_array_type because this does not include include
1596 lang-specific information (i.e. the bounds of the array) when checking
1597 for duplicates. */
1598 if (as->rank)
1599 type = make_node (ARRAY_TYPE);
1600 else
1601 type = build_variant_type_copy (etype);
1603 GFC_ARRAY_TYPE_P (type) = 1;
1604 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1606 known_stride = (packed != PACKED_NO);
1607 known_offset = 1;
1608 for (n = 0; n < as->rank; n++)
1610 /* Fill in the stride and bound components of the type. */
1611 if (known_stride)
1612 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1613 else
1614 tmp = NULL_TREE;
1615 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1617 expr = as->lower[n];
1618 if (expr->expr_type == EXPR_CONSTANT)
1620 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1621 gfc_index_integer_kind);
1623 else
1625 known_stride = 0;
1626 tmp = NULL_TREE;
1628 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1630 if (known_stride)
1632 /* Calculate the offset. */
1633 mpz_mul (delta, stride, as->lower[n]->value.integer);
1634 mpz_sub (offset, offset, delta);
1636 else
1637 known_offset = 0;
1639 expr = as->upper[n];
1640 if (expr && expr->expr_type == EXPR_CONSTANT)
1642 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1643 gfc_index_integer_kind);
1645 else
1647 tmp = NULL_TREE;
1648 known_stride = 0;
1650 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1652 if (known_stride)
1654 /* Calculate the stride. */
1655 mpz_sub (delta, as->upper[n]->value.integer,
1656 as->lower[n]->value.integer);
1657 mpz_add_ui (delta, delta, 1);
1658 mpz_mul (stride, stride, delta);
1661 /* Only the first stride is known for partial packed arrays. */
1662 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1663 known_stride = 0;
1665 for (n = as->rank; n < as->rank + as->corank; n++)
1667 expr = as->lower[n];
1668 if (expr->expr_type == EXPR_CONSTANT)
1669 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1670 gfc_index_integer_kind);
1671 else
1672 tmp = NULL_TREE;
1673 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1675 expr = as->upper[n];
1676 if (expr && expr->expr_type == EXPR_CONSTANT)
1677 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1678 gfc_index_integer_kind);
1679 else
1680 tmp = NULL_TREE;
1681 if (n < as->rank + as->corank - 1)
1682 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1685 if (known_offset)
1687 GFC_TYPE_ARRAY_OFFSET (type) =
1688 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1690 else
1691 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1693 if (known_stride)
1695 GFC_TYPE_ARRAY_SIZE (type) =
1696 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1698 else
1699 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1701 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1702 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1703 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1704 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1705 NULL_TREE);
1706 /* TODO: use main type if it is unbounded. */
1707 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1708 build_pointer_type (build_array_type (etype, range));
1709 if (restricted)
1710 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1711 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1712 TYPE_QUAL_RESTRICT);
1714 if (as->rank == 0)
1716 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1718 type = build_pointer_type (type);
1720 if (restricted)
1721 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1723 GFC_ARRAY_TYPE_P (type) = 1;
1724 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1727 return type;
1730 if (known_stride)
1732 mpz_sub_ui (stride, stride, 1);
1733 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1735 else
1736 range = NULL_TREE;
1738 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1739 TYPE_DOMAIN (type) = range;
1741 build_pointer_type (etype);
1742 TREE_TYPE (type) = etype;
1744 layout_type (type);
1746 mpz_clear (offset);
1747 mpz_clear (stride);
1748 mpz_clear (delta);
1750 /* Represent packed arrays as multi-dimensional if they have rank >
1751 1 and with proper bounds, instead of flat arrays. This makes for
1752 better debug info. */
1753 if (known_offset)
1755 tree gtype = etype, rtype, type_decl;
1757 for (n = as->rank - 1; n >= 0; n--)
1759 rtype = build_range_type (gfc_array_index_type,
1760 GFC_TYPE_ARRAY_LBOUND (type, n),
1761 GFC_TYPE_ARRAY_UBOUND (type, n));
1762 gtype = build_array_type (gtype, rtype);
1764 TYPE_NAME (type) = type_decl = build_decl (input_location,
1765 TYPE_DECL, NULL, gtype);
1766 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1769 if (packed != PACKED_STATIC || !known_stride
1770 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1772 /* For dummy arrays and automatic (heap allocated) arrays we
1773 want a pointer to the array. */
1774 type = build_pointer_type (type);
1775 if (restricted)
1776 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1777 GFC_ARRAY_TYPE_P (type) = 1;
1778 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1780 return type;
1784 /* Return or create the base type for an array descriptor. */
1786 static tree
1787 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1789 tree fat_type, decl, arraytype, *chain = NULL;
1790 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1791 int idx;
1793 /* Assumed-rank array. */
1794 if (dimen == -1)
1795 dimen = GFC_MAX_DIMENSIONS;
1797 idx = 2 * (codimen + dimen) + restricted;
1799 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1801 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1803 if (gfc_array_descriptor_base_caf[idx])
1804 return gfc_array_descriptor_base_caf[idx];
1806 else if (gfc_array_descriptor_base[idx])
1807 return gfc_array_descriptor_base[idx];
1809 /* Build the type node. */
1810 fat_type = make_node (RECORD_TYPE);
1812 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1813 TYPE_NAME (fat_type) = get_identifier (name);
1814 TYPE_NAMELESS (fat_type) = 1;
1816 /* Add the data member as the first element of the descriptor. */
1817 decl = gfc_add_field_to_struct_1 (fat_type,
1818 get_identifier ("data"),
1819 (restricted
1820 ? prvoid_type_node
1821 : ptr_type_node), &chain);
1823 /* Add the base component. */
1824 decl = gfc_add_field_to_struct_1 (fat_type,
1825 get_identifier ("offset"),
1826 gfc_array_index_type, &chain);
1827 TREE_NO_WARNING (decl) = 1;
1829 /* Add the dtype component. */
1830 decl = gfc_add_field_to_struct_1 (fat_type,
1831 get_identifier ("dtype"),
1832 get_dtype_type_node (), &chain);
1833 TREE_NO_WARNING (decl) = 1;
1835 /* Add the span component. */
1836 decl = gfc_add_field_to_struct_1 (fat_type,
1837 get_identifier ("span"),
1838 gfc_array_index_type, &chain);
1839 TREE_NO_WARNING (decl) = 1;
1841 /* Build the array type for the stride and bound components. */
1842 if (dimen + codimen > 0)
1844 arraytype =
1845 build_array_type (gfc_get_desc_dim_type (),
1846 build_range_type (gfc_array_index_type,
1847 gfc_index_zero_node,
1848 gfc_rank_cst[codimen + dimen - 1]));
1850 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1851 arraytype, &chain);
1852 TREE_NO_WARNING (decl) = 1;
1855 if (flag_coarray == GFC_FCOARRAY_LIB)
1857 decl = gfc_add_field_to_struct_1 (fat_type,
1858 get_identifier ("token"),
1859 prvoid_type_node, &chain);
1860 TREE_NO_WARNING (decl) = 1;
1863 /* Finish off the type. */
1864 gfc_finish_type (fat_type);
1865 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1867 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1868 gfc_array_descriptor_base_caf[idx] = fat_type;
1869 else
1870 gfc_array_descriptor_base[idx] = fat_type;
1872 return fat_type;
1876 /* Build an array (descriptor) type with given bounds. */
1878 tree
1879 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1880 tree * ubound, int packed,
1881 enum gfc_array_kind akind, bool restricted)
1883 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1884 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1885 const char *type_name;
1886 int n;
1888 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1889 fat_type = build_distinct_type_copy (base_type);
1890 /* Unshare TYPE_FIELDs. */
1891 for (tree *tp = &TYPE_FIELDS (fat_type); *tp; tp = &DECL_CHAIN (*tp))
1893 tree next = DECL_CHAIN (*tp);
1894 *tp = copy_node (*tp);
1895 DECL_CONTEXT (*tp) = fat_type;
1896 DECL_CHAIN (*tp) = next;
1898 /* Make sure that nontarget and target array type have the same canonical
1899 type (and same stub decl for debug info). */
1900 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1901 TYPE_CANONICAL (fat_type) = base_type;
1902 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1904 tmp = TYPE_NAME (etype);
1905 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1906 tmp = DECL_NAME (tmp);
1907 if (tmp)
1908 type_name = IDENTIFIER_POINTER (tmp);
1909 else
1910 type_name = "unknown";
1911 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1912 GFC_MAX_SYMBOL_LEN, type_name);
1913 TYPE_NAME (fat_type) = get_identifier (name);
1914 TYPE_NAMELESS (fat_type) = 1;
1916 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1917 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1919 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1920 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1921 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1922 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1924 /* Build an array descriptor record type. */
1925 if (packed != 0)
1926 stride = gfc_index_one_node;
1927 else
1928 stride = NULL_TREE;
1929 for (n = 0; n < dimen + codimen; n++)
1931 if (n < dimen)
1932 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1934 if (lbound)
1935 lower = lbound[n];
1936 else
1937 lower = NULL_TREE;
1939 if (lower != NULL_TREE)
1941 if (INTEGER_CST_P (lower))
1942 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1943 else
1944 lower = NULL_TREE;
1947 if (codimen && n == dimen + codimen - 1)
1948 break;
1950 upper = ubound[n];
1951 if (upper != NULL_TREE)
1953 if (INTEGER_CST_P (upper))
1954 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1955 else
1956 upper = NULL_TREE;
1959 if (n >= dimen)
1960 continue;
1962 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1964 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1965 gfc_array_index_type, upper, lower);
1966 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1967 gfc_array_index_type, tmp,
1968 gfc_index_one_node);
1969 stride = fold_build2_loc (input_location, MULT_EXPR,
1970 gfc_array_index_type, tmp, stride);
1971 /* Check the folding worked. */
1972 gcc_assert (INTEGER_CST_P (stride));
1974 else
1975 stride = NULL_TREE;
1977 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1979 /* TODO: known offsets for descriptors. */
1980 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1982 if (dimen == 0)
1984 arraytype = build_pointer_type (etype);
1985 if (restricted)
1986 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1988 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1989 return fat_type;
1992 /* We define data as an array with the correct size if possible.
1993 Much better than doing pointer arithmetic. */
1994 if (stride)
1995 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1996 int_const_binop (MINUS_EXPR, stride,
1997 build_int_cst (TREE_TYPE (stride), 1)));
1998 else
1999 rtype = gfc_array_range_type;
2000 arraytype = build_array_type (etype, rtype);
2001 arraytype = build_pointer_type (arraytype);
2002 if (restricted)
2003 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
2004 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
2006 /* This will generate the base declarations we need to emit debug
2007 information for this type. FIXME: there must be a better way to
2008 avoid divergence between compilations with and without debug
2009 information. */
2011 struct array_descr_info info;
2012 gfc_get_array_descr_info (fat_type, &info);
2013 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
2016 return fat_type;
2019 /* Build a pointer type. This function is called from gfc_sym_type(). */
2021 static tree
2022 gfc_build_pointer_type (gfc_symbol * sym, tree type)
2024 /* Array pointer types aren't actually pointers. */
2025 if (sym->attr.dimension)
2026 return type;
2027 else
2028 return build_pointer_type (type);
2031 static tree gfc_nonrestricted_type (tree t);
2032 /* Given two record or union type nodes TO and FROM, ensure
2033 that all fields in FROM have a corresponding field in TO,
2034 their type being nonrestrict variants. This accepts a TO
2035 node that already has a prefix of the fields in FROM. */
2036 static void
2037 mirror_fields (tree to, tree from)
2039 tree fto, ffrom;
2040 tree *chain;
2042 /* Forward to the end of TOs fields. */
2043 fto = TYPE_FIELDS (to);
2044 ffrom = TYPE_FIELDS (from);
2045 chain = &TYPE_FIELDS (to);
2046 while (fto)
2048 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2049 chain = &DECL_CHAIN (fto);
2050 fto = DECL_CHAIN (fto);
2051 ffrom = DECL_CHAIN (ffrom);
2054 /* Now add all fields remaining in FROM (starting with ffrom). */
2055 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2057 tree newfield = copy_node (ffrom);
2058 DECL_CONTEXT (newfield) = to;
2059 /* The store to DECL_CHAIN might seem redundant with the
2060 stores to *chain, but not clearing it here would mean
2061 leaving a chain into the old fields. If ever
2062 our called functions would look at them confusion
2063 will arise. */
2064 DECL_CHAIN (newfield) = NULL_TREE;
2065 *chain = newfield;
2066 chain = &DECL_CHAIN (newfield);
2068 if (TREE_CODE (ffrom) == FIELD_DECL)
2070 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2071 TREE_TYPE (newfield) = elemtype;
2074 *chain = NULL_TREE;
2077 /* Given a type T, returns a different type of the same structure,
2078 except that all types it refers to (recursively) are always
2079 non-restrict qualified types. */
2080 static tree
2081 gfc_nonrestricted_type (tree t)
2083 tree ret = t;
2085 /* If the type isn't laid out yet, don't copy it. If something
2086 needs it for real it should wait until the type got finished. */
2087 if (!TYPE_SIZE (t))
2088 return t;
2090 if (!TYPE_LANG_SPECIFIC (t))
2091 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2092 /* If we're dealing with this very node already further up
2093 the call chain (recursion via pointers and struct members)
2094 we haven't yet determined if we really need a new type node.
2095 Assume we don't, return T itself. */
2096 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2097 return t;
2099 /* If we have calculated this all already, just return it. */
2100 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2101 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2103 /* Mark this type. */
2104 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2106 switch (TREE_CODE (t))
2108 default:
2109 break;
2111 case POINTER_TYPE:
2112 case REFERENCE_TYPE:
2114 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2115 if (totype == TREE_TYPE (t))
2116 ret = t;
2117 else if (TREE_CODE (t) == POINTER_TYPE)
2118 ret = build_pointer_type (totype);
2119 else
2120 ret = build_reference_type (totype);
2121 ret = build_qualified_type (ret,
2122 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2124 break;
2126 case ARRAY_TYPE:
2128 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2129 if (elemtype == TREE_TYPE (t))
2130 ret = t;
2131 else
2133 ret = build_variant_type_copy (t);
2134 TREE_TYPE (ret) = elemtype;
2135 if (TYPE_LANG_SPECIFIC (t)
2136 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2138 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2139 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2140 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2142 TYPE_LANG_SPECIFIC (ret)
2143 = ggc_cleared_alloc<struct lang_type> ();
2144 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2145 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2150 break;
2152 case RECORD_TYPE:
2153 case UNION_TYPE:
2154 case QUAL_UNION_TYPE:
2156 tree field;
2157 /* First determine if we need a new type at all.
2158 Careful, the two calls to gfc_nonrestricted_type per field
2159 might return different values. That happens exactly when
2160 one of the fields reaches back to this very record type
2161 (via pointers). The first calls will assume that we don't
2162 need to copy T (see the error_mark_node marking). If there
2163 are any reasons for copying T apart from having to copy T,
2164 we'll indeed copy it, and the second calls to
2165 gfc_nonrestricted_type will use that new node if they
2166 reach back to T. */
2167 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2168 if (TREE_CODE (field) == FIELD_DECL)
2170 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2171 if (elemtype != TREE_TYPE (field))
2172 break;
2174 if (!field)
2175 break;
2176 ret = build_variant_type_copy (t);
2177 TYPE_FIELDS (ret) = NULL_TREE;
2179 /* Here we make sure that as soon as we know we have to copy
2180 T, that also fields reaching back to us will use the new
2181 copy. It's okay if that copy still contains the old fields,
2182 we won't look at them. */
2183 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2184 mirror_fields (ret, t);
2186 break;
2189 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2190 return ret;
2194 /* Return the type for a symbol. Special handling is required for character
2195 types to get the correct level of indirection.
2196 For functions return the return type.
2197 For subroutines return void_type_node.
2198 Calling this multiple times for the same symbol should be avoided,
2199 especially for character and array types. */
2201 tree
2202 gfc_sym_type (gfc_symbol * sym)
2204 tree type;
2205 int byref;
2206 bool restricted;
2208 /* Procedure Pointers inside COMMON blocks. */
2209 if (sym->attr.proc_pointer && sym->attr.in_common)
2211 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2212 sym->attr.proc_pointer = 0;
2213 type = build_pointer_type (gfc_get_function_type (sym));
2214 sym->attr.proc_pointer = 1;
2215 return type;
2218 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2219 return void_type_node;
2221 /* In the case of a function the fake result variable may have a
2222 type different from the function type, so don't return early in
2223 that case. */
2224 if (sym->backend_decl && !sym->attr.function)
2225 return TREE_TYPE (sym->backend_decl);
2227 if (sym->attr.result
2228 && sym->ts.type == BT_CHARACTER
2229 && sym->ts.u.cl->backend_decl == NULL_TREE
2230 && sym->ns->proc_name
2231 && sym->ns->proc_name->ts.u.cl
2232 && sym->ns->proc_name->ts.u.cl->backend_decl != NULL_TREE)
2233 sym->ts.u.cl->backend_decl = sym->ns->proc_name->ts.u.cl->backend_decl;
2235 if (sym->ts.type == BT_CHARACTER
2236 && ((sym->attr.function && sym->attr.is_bind_c)
2237 || (sym->attr.result
2238 && sym->ns->proc_name
2239 && sym->ns->proc_name->attr.is_bind_c)
2240 || (sym->ts.deferred && (!sym->ts.u.cl
2241 || !sym->ts.u.cl->backend_decl))))
2242 type = gfc_character1_type_node;
2243 else
2244 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2246 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2247 byref = 1;
2248 else
2249 byref = 0;
2251 restricted = !sym->attr.target && !sym->attr.pointer
2252 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2253 if (!restricted)
2254 type = gfc_nonrestricted_type (type);
2256 if (sym->attr.dimension || sym->attr.codimension)
2258 if (gfc_is_nodesc_array (sym))
2260 /* If this is a character argument of unknown length, just use the
2261 base type. */
2262 if (sym->ts.type != BT_CHARACTER
2263 || !(sym->attr.dummy || sym->attr.function)
2264 || sym->ts.u.cl->backend_decl)
2266 type = gfc_get_nodesc_array_type (type, sym->as,
2267 byref ? PACKED_FULL
2268 : PACKED_STATIC,
2269 restricted);
2270 byref = 0;
2273 else
2275 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2276 if (sym->attr.pointer)
2277 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2278 : GFC_ARRAY_POINTER;
2279 else if (sym->attr.allocatable)
2280 akind = GFC_ARRAY_ALLOCATABLE;
2281 type = gfc_build_array_type (type, sym->as, akind, restricted,
2282 sym->attr.contiguous, false);
2285 else
2287 if (sym->attr.allocatable || sym->attr.pointer
2288 || gfc_is_associate_pointer (sym))
2289 type = gfc_build_pointer_type (sym, type);
2292 /* We currently pass all parameters by reference.
2293 See f95_get_function_decl. For dummy function parameters return the
2294 function type. */
2295 if (byref)
2297 /* We must use pointer types for potentially absent variables. The
2298 optimizers assume a reference type argument is never NULL. */
2299 if (sym->attr.optional
2300 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2301 type = build_pointer_type (type);
2302 else
2304 type = build_reference_type (type);
2305 if (restricted)
2306 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2310 return (type);
2313 /* Layout and output debug info for a record type. */
2315 void
2316 gfc_finish_type (tree type)
2318 tree decl;
2320 decl = build_decl (input_location,
2321 TYPE_DECL, NULL_TREE, type);
2322 TYPE_STUB_DECL (type) = decl;
2323 layout_type (type);
2324 rest_of_type_compilation (type, 1);
2325 rest_of_decl_compilation (decl, 1, 0);
2328 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2329 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2330 to the end of the field list pointed to by *CHAIN.
2332 Returns a pointer to the new field. */
2334 static tree
2335 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2337 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2339 DECL_CONTEXT (decl) = context;
2340 DECL_CHAIN (decl) = NULL_TREE;
2341 if (TYPE_FIELDS (context) == NULL_TREE)
2342 TYPE_FIELDS (context) = decl;
2343 if (chain != NULL)
2345 if (*chain != NULL)
2346 **chain = decl;
2347 *chain = &DECL_CHAIN (decl);
2350 return decl;
2353 /* Like `gfc_add_field_to_struct_1', but adds alignment
2354 information. */
2356 tree
2357 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2359 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2361 DECL_INITIAL (decl) = 0;
2362 SET_DECL_ALIGN (decl, 0);
2363 DECL_USER_ALIGN (decl) = 0;
2365 return decl;
2369 /* Copy the backend_decl and component backend_decls if
2370 the two derived type symbols are "equal", as described
2371 in 4.4.2 and resolved by gfc_compare_derived_types. */
2374 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2375 bool from_gsym)
2377 gfc_component *to_cm;
2378 gfc_component *from_cm;
2380 if (from == to)
2381 return 1;
2383 if (from->backend_decl == NULL
2384 || !gfc_compare_derived_types (from, to))
2385 return 0;
2387 to->backend_decl = from->backend_decl;
2389 to_cm = to->components;
2390 from_cm = from->components;
2392 /* Copy the component declarations. If a component is itself
2393 a derived type, we need a copy of its component declarations.
2394 This is done by recursing into gfc_get_derived_type and
2395 ensures that the component's component declarations have
2396 been built. If it is a character, we need the character
2397 length, as well. */
2398 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2400 to_cm->backend_decl = from_cm->backend_decl;
2401 to_cm->caf_token = from_cm->caf_token;
2402 if (from_cm->ts.type == BT_UNION)
2403 gfc_get_union_type (to_cm->ts.u.derived);
2404 else if (from_cm->ts.type == BT_DERIVED
2405 && (!from_cm->attr.pointer || from_gsym))
2406 gfc_get_derived_type (to_cm->ts.u.derived);
2407 else if (from_cm->ts.type == BT_CLASS
2408 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2409 gfc_get_derived_type (to_cm->ts.u.derived);
2410 else if (from_cm->ts.type == BT_CHARACTER)
2411 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2414 return 1;
2418 /* Build a tree node for a procedure pointer component. */
2420 tree
2421 gfc_get_ppc_type (gfc_component* c)
2423 tree t;
2425 /* Explicit interface. */
2426 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2427 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2429 /* Implicit interface (only return value may be known). */
2430 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2431 t = gfc_typenode_for_spec (&c->ts);
2432 else
2433 t = void_type_node;
2435 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2439 /* Build a tree node for a union type. Requires building each map
2440 structure which is an element of the union. */
2442 tree
2443 gfc_get_union_type (gfc_symbol *un)
2445 gfc_component *map = NULL;
2446 tree typenode = NULL, map_type = NULL, map_field = NULL;
2447 tree *chain = NULL;
2449 if (un->backend_decl)
2451 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2452 return un->backend_decl;
2453 else
2454 typenode = un->backend_decl;
2456 else
2458 typenode = make_node (UNION_TYPE);
2459 TYPE_NAME (typenode) = get_identifier (un->name);
2462 /* Add each contained MAP as a field. */
2463 for (map = un->components; map; map = map->next)
2465 gcc_assert (map->ts.type == BT_DERIVED);
2467 /* The map's type node, which is defined within this union's context. */
2468 map_type = gfc_get_derived_type (map->ts.u.derived);
2469 TYPE_CONTEXT (map_type) = typenode;
2471 /* The map field's declaration. */
2472 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2473 map_type, &chain);
2474 if (map->loc.lb)
2475 gfc_set_decl_location (map_field, &map->loc);
2476 else if (un->declared_at.lb)
2477 gfc_set_decl_location (map_field, &un->declared_at);
2479 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2480 DECL_NAMELESS(map_field) = true;
2482 /* We should never clobber another backend declaration for this map,
2483 because each map component is unique. */
2484 if (!map->backend_decl)
2485 map->backend_decl = map_field;
2488 un->backend_decl = typenode;
2489 gfc_finish_type (typenode);
2491 return typenode;
2495 /* Build a tree node for a derived type. If there are equal
2496 derived types, with different local names, these are built
2497 at the same time. If an equal derived type has been built
2498 in a parent namespace, this is used. */
2500 tree
2501 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2503 tree typenode = NULL, field = NULL, field_type = NULL;
2504 tree canonical = NULL_TREE;
2505 tree *chain = NULL;
2506 bool got_canonical = false;
2507 bool unlimited_entity = false;
2508 gfc_component *c;
2509 gfc_namespace *ns;
2510 tree tmp;
2511 bool coarray_flag;
2513 coarray_flag = flag_coarray == GFC_FCOARRAY_LIB
2514 && derived->module && !derived->attr.vtype;
2516 gcc_assert (!derived->attr.pdt_template);
2518 if (derived->attr.unlimited_polymorphic
2519 || (flag_coarray == GFC_FCOARRAY_LIB
2520 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2521 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2522 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2523 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE)))
2524 return ptr_type_node;
2526 if (flag_coarray != GFC_FCOARRAY_LIB
2527 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2528 && (derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE
2529 || derived->intmod_sym_id == ISOFORTRAN_TEAM_TYPE))
2530 return gfc_get_int_type (gfc_default_integer_kind);
2532 if (derived && derived->attr.flavor == FL_PROCEDURE
2533 && derived->attr.generic)
2534 derived = gfc_find_dt_in_generic (derived);
2536 /* See if it's one of the iso_c_binding derived types. */
2537 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2539 if (derived->backend_decl)
2540 return derived->backend_decl;
2542 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2543 derived->backend_decl = ptr_type_node;
2544 else
2545 derived->backend_decl = pfunc_type_node;
2547 derived->ts.kind = gfc_index_integer_kind;
2548 derived->ts.type = BT_INTEGER;
2549 /* Set the f90_type to BT_VOID as a way to recognize something of type
2550 BT_INTEGER that needs to fit a void * for the purpose of the
2551 iso_c_binding derived types. */
2552 derived->ts.f90_type = BT_VOID;
2554 return derived->backend_decl;
2557 /* If use associated, use the module type for this one. */
2558 if (derived->backend_decl == NULL
2559 && derived->attr.use_assoc
2560 && derived->module
2561 && gfc_get_module_backend_decl (derived))
2562 goto copy_derived_types;
2564 /* The derived types from an earlier namespace can be used as the
2565 canonical type. */
2566 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2567 && gfc_global_ns_list)
2569 for (ns = gfc_global_ns_list;
2570 ns->translated && !got_canonical;
2571 ns = ns->sibling)
2573 if (ns->derived_types)
2575 for (gfc_symbol *dt = ns->derived_types; dt && !got_canonical;
2576 dt = dt->dt_next)
2578 gfc_copy_dt_decls_ifequal (dt, derived, true);
2579 if (derived->backend_decl)
2580 got_canonical = true;
2581 if (dt->dt_next == ns->derived_types)
2582 break;
2588 /* Store up the canonical type to be added to this one. */
2589 if (got_canonical)
2591 if (TYPE_CANONICAL (derived->backend_decl))
2592 canonical = TYPE_CANONICAL (derived->backend_decl);
2593 else
2594 canonical = derived->backend_decl;
2596 derived->backend_decl = NULL_TREE;
2599 /* derived->backend_decl != 0 means we saw it before, but its
2600 components' backend_decl may have not been built. */
2601 if (derived->backend_decl)
2603 /* Its components' backend_decl have been built or we are
2604 seeing recursion through the formal arglist of a procedure
2605 pointer component. */
2606 if (TYPE_FIELDS (derived->backend_decl))
2607 return derived->backend_decl;
2608 else if (derived->attr.abstract
2609 && derived->attr.proc_pointer_comp)
2611 /* If an abstract derived type with procedure pointer
2612 components has no other type of component, return the
2613 backend_decl. Otherwise build the components if any of the
2614 non-procedure pointer components have no backend_decl. */
2615 for (c = derived->components; c; c = c->next)
2617 bool same_alloc_type = c->attr.allocatable
2618 && derived == c->ts.u.derived;
2619 if (!c->attr.proc_pointer
2620 && !same_alloc_type
2621 && c->backend_decl == NULL)
2622 break;
2623 else if (c->next == NULL)
2624 return derived->backend_decl;
2626 typenode = derived->backend_decl;
2628 else
2629 typenode = derived->backend_decl;
2631 else
2633 /* We see this derived type first time, so build the type node. */
2634 typenode = make_node (RECORD_TYPE);
2635 TYPE_NAME (typenode) = get_identifier (derived->name);
2636 TYPE_PACKED (typenode) = flag_pack_derived;
2637 derived->backend_decl = typenode;
2640 if (derived->components
2641 && derived->components->ts.type == BT_DERIVED
2642 && strcmp (derived->components->name, "_data") == 0
2643 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2644 unlimited_entity = true;
2646 /* Go through the derived type components, building them as
2647 necessary. The reason for doing this now is that it is
2648 possible to recurse back to this derived type through a
2649 pointer component (PR24092). If this happens, the fields
2650 will be built and so we can return the type. */
2651 for (c = derived->components; c; c = c->next)
2653 bool same_alloc_type = c->attr.allocatable
2654 && derived == c->ts.u.derived;
2656 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2657 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2659 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2660 continue;
2662 if ((!c->attr.pointer && !c->attr.proc_pointer
2663 && !same_alloc_type)
2664 || c->ts.u.derived->backend_decl == NULL)
2666 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2667 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2668 local_codim);
2671 if (c->ts.u.derived->attr.is_iso_c)
2673 /* Need to copy the modified ts from the derived type. The
2674 typespec was modified because C_PTR/C_FUNPTR are translated
2675 into (void *) from derived types. */
2676 c->ts.type = c->ts.u.derived->ts.type;
2677 c->ts.kind = c->ts.u.derived->ts.kind;
2678 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2679 if (c->initializer)
2681 c->initializer->ts.type = c->ts.type;
2682 c->initializer->ts.kind = c->ts.kind;
2683 c->initializer->ts.f90_type = c->ts.f90_type;
2684 c->initializer->expr_type = EXPR_NULL;
2689 if (TYPE_FIELDS (derived->backend_decl))
2690 return derived->backend_decl;
2692 /* Build the type member list. Install the newly created RECORD_TYPE
2693 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2694 through only the top-level linked list of components so we correctly
2695 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2696 types are built as part of gfc_get_union_type. */
2697 for (c = derived->components; c; c = c->next)
2699 bool same_alloc_type = c->attr.allocatable
2700 && derived == c->ts.u.derived;
2701 /* Prevent infinite recursion, when the procedure pointer type is
2702 the same as derived, by forcing the procedure pointer component to
2703 be built as if the explicit interface does not exist. */
2704 if (c->attr.proc_pointer
2705 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2706 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2707 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2708 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2709 field_type = gfc_get_ppc_type (c);
2710 else if (c->attr.proc_pointer && derived->backend_decl)
2712 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2713 field_type = build_pointer_type (tmp);
2715 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2716 field_type = c->ts.u.derived->backend_decl;
2717 else if (c->attr.caf_token)
2718 field_type = pvoid_type_node;
2719 else
2721 if (c->ts.type == BT_CHARACTER
2722 && !c->ts.deferred && !c->attr.pdt_string)
2724 /* Evaluate the string length. */
2725 gfc_conv_const_charlen (c->ts.u.cl);
2726 gcc_assert (c->ts.u.cl->backend_decl);
2728 else if (c->ts.type == BT_CHARACTER)
2729 c->ts.u.cl->backend_decl
2730 = build_int_cst (gfc_charlen_type_node, 0);
2732 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2735 /* This returns an array descriptor type. Initialization may be
2736 required. */
2737 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2739 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2741 enum gfc_array_kind akind;
2742 if (c->attr.pointer)
2743 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2744 : GFC_ARRAY_POINTER;
2745 else
2746 akind = GFC_ARRAY_ALLOCATABLE;
2747 /* Pointers to arrays aren't actually pointer types. The
2748 descriptors are separate, but the data is common. */
2749 field_type = gfc_build_array_type (field_type, c->as, akind,
2750 !c->attr.target
2751 && !c->attr.pointer,
2752 c->attr.contiguous,
2753 codimen);
2755 else
2756 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2757 PACKED_STATIC,
2758 !c->attr.target);
2760 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2761 && !c->attr.proc_pointer
2762 && !(unlimited_entity && c == derived->components))
2763 field_type = build_pointer_type (field_type);
2765 if (c->attr.pointer || same_alloc_type)
2766 field_type = gfc_nonrestricted_type (field_type);
2768 /* vtype fields can point to different types to the base type. */
2769 if (c->ts.type == BT_DERIVED
2770 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2771 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2772 ptr_mode, true);
2774 /* Ensure that the CLASS language specific flag is set. */
2775 if (c->ts.type == BT_CLASS)
2777 if (POINTER_TYPE_P (field_type))
2778 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2779 else
2780 GFC_CLASS_TYPE_P (field_type) = 1;
2783 field = gfc_add_field_to_struct (typenode,
2784 get_identifier (c->name),
2785 field_type, &chain);
2786 if (c->loc.lb)
2787 gfc_set_decl_location (field, &c->loc);
2788 else if (derived->declared_at.lb)
2789 gfc_set_decl_location (field, &derived->declared_at);
2791 gfc_finish_decl_attrs (field, &c->attr);
2793 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2795 gcc_assert (field);
2796 if (!c->backend_decl)
2797 c->backend_decl = field;
2799 if (c->attr.pointer && c->attr.dimension
2800 && !(c->ts.type == BT_DERIVED
2801 && strcmp (c->name, "_data") == 0))
2802 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2805 /* Now lay out the derived type, including the fields. */
2806 if (canonical)
2807 TYPE_CANONICAL (typenode) = canonical;
2809 gfc_finish_type (typenode);
2810 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2811 if (derived->module && derived->ns->proc_name
2812 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2814 if (derived->ns->proc_name->backend_decl
2815 && TREE_CODE (derived->ns->proc_name->backend_decl)
2816 == NAMESPACE_DECL)
2818 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2819 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2820 = derived->ns->proc_name->backend_decl;
2824 derived->backend_decl = typenode;
2826 copy_derived_types:
2828 for (c = derived->components; c; c = c->next)
2830 /* Do not add a caf_token field for class container components. */
2831 if ((codimen || coarray_flag)
2832 && !c->attr.dimension && !c->attr.codimension
2833 && (c->attr.allocatable || c->attr.pointer)
2834 && !derived->attr.is_class)
2836 char caf_name[GFC_MAX_SYMBOL_LEN];
2837 gfc_component *token;
2838 snprintf (caf_name, GFC_MAX_SYMBOL_LEN, "_caf_%s", c->name);
2839 token = gfc_find_component (derived, caf_name, true, true, NULL);
2840 gcc_assert (token);
2841 c->caf_token = token->backend_decl;
2842 TREE_NO_WARNING (c->caf_token) = 1;
2846 for (gfc_symbol *dt = gfc_derived_types; dt; dt = dt->dt_next)
2848 gfc_copy_dt_decls_ifequal (derived, dt, false);
2849 if (dt->dt_next == gfc_derived_types)
2850 break;
2853 return derived->backend_decl;
2858 gfc_return_by_reference (gfc_symbol * sym)
2860 if (!sym->attr.function)
2861 return 0;
2863 if (sym->attr.dimension)
2864 return 1;
2866 if (sym->ts.type == BT_CHARACTER
2867 && !sym->attr.is_bind_c
2868 && (!sym->attr.result
2869 || !sym->ns->proc_name
2870 || !sym->ns->proc_name->attr.is_bind_c))
2871 return 1;
2873 /* Possibly return complex numbers by reference for g77 compatibility.
2874 We don't do this for calls to intrinsics (as the library uses the
2875 -fno-f2c calling convention), nor for calls to functions which always
2876 require an explicit interface, as no compatibility problems can
2877 arise there. */
2878 if (flag_f2c && sym->ts.type == BT_COMPLEX
2879 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2880 return 1;
2882 return 0;
2885 static tree
2886 gfc_get_mixed_entry_union (gfc_namespace *ns)
2888 tree type;
2889 tree *chain = NULL;
2890 char name[GFC_MAX_SYMBOL_LEN + 1];
2891 gfc_entry_list *el, *el2;
2893 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2894 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2896 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2898 /* Build the type node. */
2899 type = make_node (UNION_TYPE);
2901 TYPE_NAME (type) = get_identifier (name);
2903 for (el = ns->entries; el; el = el->next)
2905 /* Search for duplicates. */
2906 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2907 if (el2->sym->result == el->sym->result)
2908 break;
2910 if (el == el2)
2911 gfc_add_field_to_struct_1 (type,
2912 get_identifier (el->sym->result->name),
2913 gfc_sym_type (el->sym->result), &chain);
2916 /* Finish off the type. */
2917 gfc_finish_type (type);
2918 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2919 return type;
2922 /* Create a "fn spec" based on the formal arguments;
2923 cf. create_function_arglist. */
2925 static tree
2926 create_fn_spec (gfc_symbol *sym, tree fntype)
2928 char spec[150];
2929 size_t spec_len;
2930 gfc_formal_arglist *f;
2931 tree tmp;
2933 memset (&spec, 0, sizeof (spec));
2934 spec[0] = '.';
2935 spec_len = 1;
2937 if (sym->attr.entry_master)
2938 spec[spec_len++] = 'R';
2939 if (gfc_return_by_reference (sym))
2941 gfc_symbol *result = sym->result ? sym->result : sym;
2943 if (result->attr.pointer || sym->attr.proc_pointer)
2944 spec[spec_len++] = '.';
2945 else
2946 spec[spec_len++] = 'w';
2947 if (sym->ts.type == BT_CHARACTER)
2948 spec[spec_len++] = 'R';
2951 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2952 if (spec_len < sizeof (spec))
2954 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2955 || f->sym->attr.external || f->sym->attr.cray_pointer
2956 || (f->sym->ts.type == BT_DERIVED
2957 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2958 || f->sym->ts.u.derived->attr.pointer_comp))
2959 || (f->sym->ts.type == BT_CLASS
2960 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2961 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp))
2962 || (f->sym->ts.type == BT_INTEGER && f->sym->ts.is_c_interop))
2963 spec[spec_len++] = '.';
2964 else if (f->sym->attr.intent == INTENT_IN)
2965 spec[spec_len++] = 'r';
2966 else if (f->sym)
2967 spec[spec_len++] = 'w';
2970 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2971 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2972 return build_type_attribute_variant (fntype, tmp);
2975 /* Helper function - if we do not find an interface for a procedure,
2976 construct it from the actual arglist. Luckily, this can only
2977 happen for call by reference, so the information we actually need
2978 to provide (and which would be impossible to guess from the call
2979 itself) is not actually needed. */
2981 static void
2982 get_formal_from_actual_arglist (gfc_symbol *sym, gfc_actual_arglist *actual_args)
2984 gfc_actual_arglist *a;
2985 gfc_formal_arglist **f;
2986 gfc_symbol *s;
2987 char name[GFC_MAX_SYMBOL_LEN + 1];
2988 static int var_num;
2990 f = &sym->formal;
2991 for (a = actual_args; a != NULL; a = a->next)
2993 (*f) = gfc_get_formal_arglist ();
2994 if (a->expr)
2996 snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
2997 gfc_get_symbol (name, NULL, &s);
2998 if (a->expr->ts.type == BT_PROCEDURE)
3000 s->attr.flavor = FL_PROCEDURE;
3002 else
3004 s->ts = a->expr->ts;
3005 s->attr.flavor = FL_VARIABLE;
3006 if (a->expr->rank > 0)
3008 s->attr.dimension = 1;
3009 s->as = gfc_get_array_spec ();
3010 s->as->type = AS_ASSUMED_SIZE;
3013 s->attr.dummy = 1;
3014 s->attr.intent = INTENT_UNKNOWN;
3015 (*f)->sym = s;
3017 else /* If a->expr is NULL, this is an alternate rerturn. */
3018 (*f)->sym = NULL;
3020 f = &((*f)->next);
3024 tree
3025 gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args)
3027 tree type;
3028 vec<tree, va_gc> *typelist = NULL;
3029 gfc_formal_arglist *f;
3030 gfc_symbol *arg;
3031 int alternate_return = 0;
3032 bool is_varargs = true;
3034 /* Make sure this symbol is a function, a subroutine or the main
3035 program. */
3036 gcc_assert (sym->attr.flavor == FL_PROCEDURE
3037 || sym->attr.flavor == FL_PROGRAM);
3039 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3040 so that they can be detected here and handled further down. */
3041 if (sym->backend_decl == NULL)
3042 sym->backend_decl = error_mark_node;
3043 else if (sym->backend_decl == error_mark_node)
3044 goto arg_type_list_done;
3045 else if (sym->attr.proc_pointer)
3046 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
3047 else
3048 return TREE_TYPE (sym->backend_decl);
3050 if (sym->attr.entry_master)
3051 /* Additional parameter for selecting an entry point. */
3052 vec_safe_push (typelist, gfc_array_index_type);
3054 if (sym->result)
3055 arg = sym->result;
3056 else
3057 arg = sym;
3059 if (arg->ts.type == BT_CHARACTER)
3060 gfc_conv_const_charlen (arg->ts.u.cl);
3062 /* Some functions we use an extra parameter for the return value. */
3063 if (gfc_return_by_reference (sym))
3065 type = gfc_sym_type (arg);
3066 if (arg->ts.type == BT_COMPLEX
3067 || arg->attr.dimension
3068 || arg->ts.type == BT_CHARACTER)
3069 type = build_reference_type (type);
3071 vec_safe_push (typelist, type);
3072 if (arg->ts.type == BT_CHARACTER)
3074 if (!arg->ts.deferred)
3075 /* Transfer by value. */
3076 vec_safe_push (typelist, gfc_charlen_type_node);
3077 else
3078 /* Deferred character lengths are transferred by reference
3079 so that the value can be returned. */
3080 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
3083 if (sym->backend_decl == error_mark_node && actual_args != NULL
3084 && sym->formal == NULL && (sym->attr.proc == PROC_EXTERNAL
3085 || sym->attr.proc == PROC_UNKNOWN))
3086 get_formal_from_actual_arglist (sym, actual_args);
3088 /* Build the argument types for the function. */
3089 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3091 arg = f->sym;
3092 if (arg)
3094 /* Evaluate constant character lengths here so that they can be
3095 included in the type. */
3096 if (arg->ts.type == BT_CHARACTER)
3097 gfc_conv_const_charlen (arg->ts.u.cl);
3099 if (arg->attr.flavor == FL_PROCEDURE)
3101 type = gfc_get_function_type (arg);
3102 type = build_pointer_type (type);
3104 else
3105 type = gfc_sym_type (arg);
3107 /* Parameter Passing Convention
3109 We currently pass all parameters by reference.
3110 Parameters with INTENT(IN) could be passed by value.
3111 The problem arises if a function is called via an implicit
3112 prototype. In this situation the INTENT is not known.
3113 For this reason all parameters to global functions must be
3114 passed by reference. Passing by value would potentially
3115 generate bad code. Worse there would be no way of telling that
3116 this code was bad, except that it would give incorrect results.
3118 Contained procedures could pass by value as these are never
3119 used without an explicit interface, and cannot be passed as
3120 actual parameters for a dummy procedure. */
3122 vec_safe_push (typelist, type);
3124 else
3126 if (sym->attr.subroutine)
3127 alternate_return = 1;
3131 /* Add hidden string length parameters. */
3132 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3134 arg = f->sym;
3135 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3137 if (!arg->ts.deferred)
3138 /* Transfer by value. */
3139 type = gfc_charlen_type_node;
3140 else
3141 /* Deferred character lengths are transferred by reference
3142 so that the value can be returned. */
3143 type = build_pointer_type (gfc_charlen_type_node);
3145 vec_safe_push (typelist, type);
3149 if (!vec_safe_is_empty (typelist)
3150 || sym->attr.is_main_program
3151 || sym->attr.if_source != IFSRC_UNKNOWN)
3152 is_varargs = false;
3154 if (sym->backend_decl == error_mark_node)
3155 sym->backend_decl = NULL_TREE;
3157 arg_type_list_done:
3159 if (alternate_return)
3160 type = integer_type_node;
3161 else if (!sym->attr.function || gfc_return_by_reference (sym))
3162 type = void_type_node;
3163 else if (sym->attr.mixed_entry_master)
3164 type = gfc_get_mixed_entry_union (sym->ns);
3165 else if (flag_f2c && sym->ts.type == BT_REAL
3166 && sym->ts.kind == gfc_default_real_kind
3167 && !sym->attr.always_explicit)
3169 /* Special case: f2c calling conventions require that (scalar)
3170 default REAL functions return the C type double instead. f2c
3171 compatibility is only an issue with functions that don't
3172 require an explicit interface, as only these could be
3173 implemented in Fortran 77. */
3174 sym->ts.kind = gfc_default_double_kind;
3175 type = gfc_typenode_for_spec (&sym->ts);
3176 sym->ts.kind = gfc_default_real_kind;
3178 else if (sym->result && sym->result->attr.proc_pointer)
3179 /* Procedure pointer return values. */
3181 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3183 /* Unset proc_pointer as gfc_get_function_type
3184 is called recursively. */
3185 sym->result->attr.proc_pointer = 0;
3186 type = build_pointer_type (gfc_get_function_type (sym->result));
3187 sym->result->attr.proc_pointer = 1;
3189 else
3190 type = gfc_sym_type (sym->result);
3192 else
3193 type = gfc_sym_type (sym);
3195 if (is_varargs)
3196 type = build_varargs_function_type_vec (type, typelist);
3197 else
3198 type = build_function_type_vec (type, typelist);
3199 type = create_fn_spec (sym, type);
3201 return type;
3204 /* Language hooks for middle-end access to type nodes. */
3206 /* Return an integer type with BITS bits of precision,
3207 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3209 tree
3210 gfc_type_for_size (unsigned bits, int unsignedp)
3212 if (!unsignedp)
3214 int i;
3215 for (i = 0; i <= MAX_INT_KINDS; ++i)
3217 tree type = gfc_integer_types[i];
3218 if (type && bits == TYPE_PRECISION (type))
3219 return type;
3222 /* Handle TImode as a special case because it is used by some backends
3223 (e.g. ARM) even though it is not available for normal use. */
3224 #if HOST_BITS_PER_WIDE_INT >= 64
3225 if (bits == TYPE_PRECISION (intTI_type_node))
3226 return intTI_type_node;
3227 #endif
3229 if (bits <= TYPE_PRECISION (intQI_type_node))
3230 return intQI_type_node;
3231 if (bits <= TYPE_PRECISION (intHI_type_node))
3232 return intHI_type_node;
3233 if (bits <= TYPE_PRECISION (intSI_type_node))
3234 return intSI_type_node;
3235 if (bits <= TYPE_PRECISION (intDI_type_node))
3236 return intDI_type_node;
3237 if (bits <= TYPE_PRECISION (intTI_type_node))
3238 return intTI_type_node;
3240 else
3242 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3243 return unsigned_intQI_type_node;
3244 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3245 return unsigned_intHI_type_node;
3246 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3247 return unsigned_intSI_type_node;
3248 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3249 return unsigned_intDI_type_node;
3250 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3251 return unsigned_intTI_type_node;
3254 return NULL_TREE;
3257 /* Return a data type that has machine mode MODE. If the mode is an
3258 integer, then UNSIGNEDP selects between signed and unsigned types. */
3260 tree
3261 gfc_type_for_mode (machine_mode mode, int unsignedp)
3263 int i;
3264 tree *base;
3265 scalar_int_mode int_mode;
3267 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3268 base = gfc_real_types;
3269 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3270 base = gfc_complex_types;
3271 else if (is_a <scalar_int_mode> (mode, &int_mode))
3273 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3274 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3276 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
3277 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3279 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
3280 GET_MODE_NUNITS (mode));
3281 tree bool_type = build_nonstandard_boolean_type (elem_bits);
3282 return build_vector_type_for_mode (bool_type, mode);
3284 else if (VECTOR_MODE_P (mode)
3285 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
3287 machine_mode inner_mode = GET_MODE_INNER (mode);
3288 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3289 if (inner_type != NULL_TREE)
3290 return build_vector_type_for_mode (inner_type, mode);
3291 return NULL_TREE;
3293 else
3294 return NULL_TREE;
3296 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3298 tree type = base[i];
3299 if (type && mode == TYPE_MODE (type))
3300 return type;
3303 return NULL_TREE;
3306 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3307 in that case. */
3309 bool
3310 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3312 int rank, dim;
3313 bool indirect = false;
3314 tree etype, ptype, t, base_decl;
3315 tree data_off, dim_off, dtype_off, dim_size, elem_size;
3316 tree lower_suboff, upper_suboff, stride_suboff;
3317 tree dtype, field, rank_off;
3319 if (! GFC_DESCRIPTOR_TYPE_P (type))
3321 if (! POINTER_TYPE_P (type))
3322 return false;
3323 type = TREE_TYPE (type);
3324 if (! GFC_DESCRIPTOR_TYPE_P (type))
3325 return false;
3326 indirect = true;
3329 rank = GFC_TYPE_ARRAY_RANK (type);
3330 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3331 return false;
3333 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3334 gcc_assert (POINTER_TYPE_P (etype));
3335 etype = TREE_TYPE (etype);
3337 /* If the type is not a scalar coarray. */
3338 if (TREE_CODE (etype) == ARRAY_TYPE)
3339 etype = TREE_TYPE (etype);
3341 /* Can't handle variable sized elements yet. */
3342 if (int_size_in_bytes (etype) <= 0)
3343 return false;
3344 /* Nor non-constant lower bounds in assumed shape arrays. */
3345 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3346 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3348 for (dim = 0; dim < rank; dim++)
3349 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3350 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3351 return false;
3354 memset (info, '\0', sizeof (*info));
3355 info->ndimensions = rank;
3356 info->ordering = array_descr_ordering_column_major;
3357 info->element_type = etype;
3358 ptype = build_pointer_type (gfc_array_index_type);
3359 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3360 if (!base_decl)
3362 base_decl = make_node (DEBUG_EXPR_DECL);
3363 DECL_ARTIFICIAL (base_decl) = 1;
3364 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3365 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3366 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3368 info->base_decl = base_decl;
3369 if (indirect)
3370 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3372 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3374 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &dim_off,
3375 &dim_size, &stride_suboff,
3376 &lower_suboff, &upper_suboff);
3378 t = base_decl;
3379 if (!integer_zerop (data_off))
3380 t = fold_build_pointer_plus (t, data_off);
3381 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3382 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3383 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3384 info->allocated = build2 (NE_EXPR, logical_type_node,
3385 info->data_location, null_pointer_node);
3386 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3387 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3388 info->associated = build2 (NE_EXPR, logical_type_node,
3389 info->data_location, null_pointer_node);
3390 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3391 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3392 && dwarf_version >= 5)
3394 rank = 1;
3395 info->ndimensions = 1;
3396 t = base_decl;
3397 if (!integer_zerop (dtype_off))
3398 t = fold_build_pointer_plus (t, dtype_off);
3399 dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
3400 field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
3401 rank_off = byte_position (field);
3402 if (!integer_zerop (dtype_off))
3403 t = fold_build_pointer_plus (t, rank_off);
3405 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3406 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3407 info->rank = t;
3408 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3409 t = size_binop (MULT_EXPR, t, dim_size);
3410 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3413 for (dim = 0; dim < rank; dim++)
3415 t = fold_build_pointer_plus (base_decl,
3416 size_binop (PLUS_EXPR,
3417 dim_off, lower_suboff));
3418 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3419 info->dimen[dim].lower_bound = t;
3420 t = fold_build_pointer_plus (base_decl,
3421 size_binop (PLUS_EXPR,
3422 dim_off, upper_suboff));
3423 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3424 info->dimen[dim].upper_bound = t;
3425 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3426 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3428 /* Assumed shape arrays have known lower bounds. */
3429 info->dimen[dim].upper_bound
3430 = build2 (MINUS_EXPR, gfc_array_index_type,
3431 info->dimen[dim].upper_bound,
3432 info->dimen[dim].lower_bound);
3433 info->dimen[dim].lower_bound
3434 = fold_convert (gfc_array_index_type,
3435 GFC_TYPE_ARRAY_LBOUND (type, dim));
3436 info->dimen[dim].upper_bound
3437 = build2 (PLUS_EXPR, gfc_array_index_type,
3438 info->dimen[dim].lower_bound,
3439 info->dimen[dim].upper_bound);
3441 t = fold_build_pointer_plus (base_decl,
3442 size_binop (PLUS_EXPR,
3443 dim_off, stride_suboff));
3444 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3445 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3446 info->dimen[dim].stride = t;
3447 if (dim + 1 < rank)
3448 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3451 return true;
3455 /* Create a type to handle vector subscripts for coarray library calls. It
3456 has the form:
3457 struct caf_vector_t {
3458 size_t nvec; // size of the vector
3459 union {
3460 struct {
3461 void *vector;
3462 int kind;
3463 } v;
3464 struct {
3465 ptrdiff_t lower_bound;
3466 ptrdiff_t upper_bound;
3467 ptrdiff_t stride;
3468 } triplet;
3469 } u;
3471 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3472 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3474 tree
3475 gfc_get_caf_vector_type (int dim)
3477 static tree vector_types[GFC_MAX_DIMENSIONS];
3478 static tree vec_type = NULL_TREE;
3479 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3481 if (vector_types[dim-1] != NULL_TREE)
3482 return vector_types[dim-1];
3484 if (vec_type == NULL_TREE)
3486 chain = 0;
3487 vect_struct_type = make_node (RECORD_TYPE);
3488 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3489 get_identifier ("vector"),
3490 pvoid_type_node, &chain);
3491 TREE_NO_WARNING (tmp) = 1;
3492 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3493 get_identifier ("kind"),
3494 integer_type_node, &chain);
3495 TREE_NO_WARNING (tmp) = 1;
3496 gfc_finish_type (vect_struct_type);
3498 chain = 0;
3499 triplet_struct_type = make_node (RECORD_TYPE);
3500 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3501 get_identifier ("lower_bound"),
3502 gfc_array_index_type, &chain);
3503 TREE_NO_WARNING (tmp) = 1;
3504 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3505 get_identifier ("upper_bound"),
3506 gfc_array_index_type, &chain);
3507 TREE_NO_WARNING (tmp) = 1;
3508 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3509 gfc_array_index_type, &chain);
3510 TREE_NO_WARNING (tmp) = 1;
3511 gfc_finish_type (triplet_struct_type);
3513 chain = 0;
3514 union_type = make_node (UNION_TYPE);
3515 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3516 vect_struct_type, &chain);
3517 TREE_NO_WARNING (tmp) = 1;
3518 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3519 triplet_struct_type, &chain);
3520 TREE_NO_WARNING (tmp) = 1;
3521 gfc_finish_type (union_type);
3523 chain = 0;
3524 vec_type = make_node (RECORD_TYPE);
3525 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3526 size_type_node, &chain);
3527 TREE_NO_WARNING (tmp) = 1;
3528 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3529 union_type, &chain);
3530 TREE_NO_WARNING (tmp) = 1;
3531 gfc_finish_type (vec_type);
3532 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3535 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3536 gfc_rank_cst[dim-1]);
3537 vector_types[dim-1] = build_array_type (vec_type, tmp);
3538 return vector_types[dim-1];
3542 tree
3543 gfc_get_caf_reference_type ()
3545 static tree reference_type = NULL_TREE;
3546 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3547 a_struct_type, u_union_type, tmp, *chain;
3549 if (reference_type != NULL_TREE)
3550 return reference_type;
3552 chain = 0;
3553 c_struct_type = make_node (RECORD_TYPE);
3554 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3555 get_identifier ("offset"),
3556 gfc_array_index_type, &chain);
3557 TREE_NO_WARNING (tmp) = 1;
3558 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3559 get_identifier ("caf_token_offset"),
3560 gfc_array_index_type, &chain);
3561 TREE_NO_WARNING (tmp) = 1;
3562 gfc_finish_type (c_struct_type);
3564 chain = 0;
3565 s_struct_type = make_node (RECORD_TYPE);
3566 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3567 get_identifier ("start"),
3568 gfc_array_index_type, &chain);
3569 TREE_NO_WARNING (tmp) = 1;
3570 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3571 get_identifier ("end"),
3572 gfc_array_index_type, &chain);
3573 TREE_NO_WARNING (tmp) = 1;
3574 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3575 get_identifier ("stride"),
3576 gfc_array_index_type, &chain);
3577 TREE_NO_WARNING (tmp) = 1;
3578 gfc_finish_type (s_struct_type);
3580 chain = 0;
3581 v_struct_type = make_node (RECORD_TYPE);
3582 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3583 get_identifier ("vector"),
3584 pvoid_type_node, &chain);
3585 TREE_NO_WARNING (tmp) = 1;
3586 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3587 get_identifier ("nvec"),
3588 size_type_node, &chain);
3589 TREE_NO_WARNING (tmp) = 1;
3590 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3591 get_identifier ("kind"),
3592 integer_type_node, &chain);
3593 TREE_NO_WARNING (tmp) = 1;
3594 gfc_finish_type (v_struct_type);
3596 chain = 0;
3597 union_type = make_node (UNION_TYPE);
3598 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3599 s_struct_type, &chain);
3600 TREE_NO_WARNING (tmp) = 1;
3601 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3602 v_struct_type, &chain);
3603 TREE_NO_WARNING (tmp) = 1;
3604 gfc_finish_type (union_type);
3606 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3607 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3608 dim_union_type = build_array_type (union_type, tmp);
3610 chain = 0;
3611 a_struct_type = make_node (RECORD_TYPE);
3612 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3613 build_array_type (unsigned_char_type_node,
3614 build_range_type (gfc_array_index_type,
3615 gfc_index_zero_node,
3616 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3617 &chain);
3618 TREE_NO_WARNING (tmp) = 1;
3619 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3620 get_identifier ("static_array_type"),
3621 integer_type_node, &chain);
3622 TREE_NO_WARNING (tmp) = 1;
3623 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3624 dim_union_type, &chain);
3625 TREE_NO_WARNING (tmp) = 1;
3626 gfc_finish_type (a_struct_type);
3628 chain = 0;
3629 u_union_type = make_node (UNION_TYPE);
3630 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3631 c_struct_type, &chain);
3632 TREE_NO_WARNING (tmp) = 1;
3633 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3634 a_struct_type, &chain);
3635 TREE_NO_WARNING (tmp) = 1;
3636 gfc_finish_type (u_union_type);
3638 chain = 0;
3639 reference_type = make_node (RECORD_TYPE);
3640 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3641 build_pointer_type (reference_type), &chain);
3642 TREE_NO_WARNING (tmp) = 1;
3643 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3644 integer_type_node, &chain);
3645 TREE_NO_WARNING (tmp) = 1;
3646 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3647 size_type_node, &chain);
3648 TREE_NO_WARNING (tmp) = 1;
3649 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3650 u_union_type, &chain);
3651 TREE_NO_WARNING (tmp) = 1;
3652 gfc_finish_type (reference_type);
3653 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3655 return reference_type;
3658 #include "gt-fortran-trans-types.h"