PR rtl-optimization/82913
[official-gcc.git] / gcc / fortran / trans-types.c
blobb4ddfdb37cf384783ff369157ea6be11843b5f65
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2017 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 /* The size of the numeric storage unit and character storage unit. */
127 int gfc_numeric_storage_size;
128 int gfc_character_storage_size;
131 bool
132 gfc_check_any_c_kind (gfc_typespec *ts)
134 int i;
136 for (i = 0; i < ISOCBINDING_NUMBER; i++)
138 /* Check for any C interoperable kind for the given type/kind in ts.
139 This can be used after verify_c_interop to make sure that the
140 Fortran kind being used exists in at least some form for C. */
141 if (c_interop_kinds_table[i].f90_type == ts->type &&
142 c_interop_kinds_table[i].value == ts->kind)
143 return true;
146 return false;
150 static int
151 get_real_kind_from_node (tree type)
153 int i;
155 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
156 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
157 return gfc_real_kinds[i].kind;
159 return -4;
162 static int
163 get_int_kind_from_node (tree type)
165 int i;
167 if (!type)
168 return -2;
170 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
171 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
172 return gfc_integer_kinds[i].kind;
174 return -1;
177 /* Return a typenode for the "standard" C type with a given name. */
178 static tree
179 get_typenode_from_name (const char *name)
181 if (name == NULL || *name == '\0')
182 return NULL_TREE;
184 if (strcmp (name, "char") == 0)
185 return char_type_node;
186 if (strcmp (name, "unsigned char") == 0)
187 return unsigned_char_type_node;
188 if (strcmp (name, "signed char") == 0)
189 return signed_char_type_node;
191 if (strcmp (name, "short int") == 0)
192 return short_integer_type_node;
193 if (strcmp (name, "short unsigned int") == 0)
194 return short_unsigned_type_node;
196 if (strcmp (name, "int") == 0)
197 return integer_type_node;
198 if (strcmp (name, "unsigned int") == 0)
199 return unsigned_type_node;
201 if (strcmp (name, "long int") == 0)
202 return long_integer_type_node;
203 if (strcmp (name, "long unsigned int") == 0)
204 return long_unsigned_type_node;
206 if (strcmp (name, "long long int") == 0)
207 return long_long_integer_type_node;
208 if (strcmp (name, "long long unsigned int") == 0)
209 return long_long_unsigned_type_node;
211 gcc_unreachable ();
214 static int
215 get_int_kind_from_name (const char *name)
217 return get_int_kind_from_node (get_typenode_from_name (name));
221 /* Get the kind number corresponding to an integer of given size,
222 following the required return values for ISO_FORTRAN_ENV INT* constants:
223 -2 is returned if we support a kind of larger size, -1 otherwise. */
225 gfc_get_int_kind_from_width_isofortranenv (int size)
227 int i;
229 /* Look for a kind with matching storage size. */
230 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
231 if (gfc_integer_kinds[i].bit_size == size)
232 return gfc_integer_kinds[i].kind;
234 /* Look for a kind with larger storage size. */
235 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
236 if (gfc_integer_kinds[i].bit_size > size)
237 return -2;
239 return -1;
243 /* Get the kind number corresponding to a real of a given storage size.
244 If two real's have the same storage size, then choose the real with
245 the largest precision. If a kind type is unavailable and a real
246 exists with wider storage, then return -2; otherwise, return -1. */
249 gfc_get_real_kind_from_width_isofortranenv (int size)
251 int digits, i, kind;
253 size /= 8;
255 kind = -1;
256 digits = 0;
258 /* Look for a kind with matching storage size. */
259 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
260 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
262 if (gfc_real_kinds[i].digits > digits)
264 digits = gfc_real_kinds[i].digits;
265 kind = gfc_real_kinds[i].kind;
269 if (kind != -1)
270 return kind;
272 /* Look for a kind with larger storage size. */
273 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
274 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
275 kind = -2;
277 return kind;
282 static int
283 get_int_kind_from_width (int size)
285 int i;
287 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
288 if (gfc_integer_kinds[i].bit_size == size)
289 return gfc_integer_kinds[i].kind;
291 return -2;
294 static int
295 get_int_kind_from_minimal_width (int size)
297 int i;
299 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
300 if (gfc_integer_kinds[i].bit_size >= size)
301 return gfc_integer_kinds[i].kind;
303 return -2;
307 /* Generate the CInteropKind_t objects for the C interoperable
308 kinds. */
310 void
311 gfc_init_c_interop_kinds (void)
313 int i;
315 /* init all pointers in the list to NULL */
316 for (i = 0; i < ISOCBINDING_NUMBER; i++)
318 /* Initialize the name and value fields. */
319 c_interop_kinds_table[i].name[0] = '\0';
320 c_interop_kinds_table[i].value = -100;
321 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
324 #define NAMED_INTCST(a,b,c,d) \
325 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
326 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
327 c_interop_kinds_table[a].value = c;
328 #define NAMED_REALCST(a,b,c,d) \
329 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
330 c_interop_kinds_table[a].f90_type = BT_REAL; \
331 c_interop_kinds_table[a].value = c;
332 #define NAMED_CMPXCST(a,b,c,d) \
333 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
334 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
335 c_interop_kinds_table[a].value = c;
336 #define NAMED_LOGCST(a,b,c) \
337 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
338 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
339 c_interop_kinds_table[a].value = c;
340 #define NAMED_CHARKNDCST(a,b,c) \
341 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
342 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
343 c_interop_kinds_table[a].value = c;
344 #define NAMED_CHARCST(a,b,c) \
345 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
346 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
347 c_interop_kinds_table[a].value = c;
348 #define DERIVED_TYPE(a,b,c) \
349 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
350 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
351 c_interop_kinds_table[a].value = c;
352 #define NAMED_FUNCTION(a,b,c,d) \
353 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
354 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
355 c_interop_kinds_table[a].value = c;
356 #define NAMED_SUBROUTINE(a,b,c,d) \
357 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
358 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
359 c_interop_kinds_table[a].value = c;
360 #include "iso-c-binding.def"
364 /* Query the target to determine which machine modes are available for
365 computation. Choose KIND numbers for them. */
367 void
368 gfc_init_kinds (void)
370 opt_scalar_int_mode int_mode_iter;
371 opt_scalar_float_mode float_mode_iter;
372 int i_index, r_index, kind;
373 bool saw_i4 = false, saw_i8 = false;
374 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
376 i_index = 0;
377 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
379 scalar_int_mode mode = int_mode_iter.require ();
380 int kind, bitsize;
382 if (!targetm.scalar_mode_supported_p (mode))
383 continue;
385 /* The middle end doesn't support constants larger than 2*HWI.
386 Perhaps the target hook shouldn't have accepted these either,
387 but just to be safe... */
388 bitsize = GET_MODE_BITSIZE (mode);
389 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
390 continue;
392 gcc_assert (i_index != MAX_INT_KINDS);
394 /* Let the kind equal the bit size divided by 8. This insulates the
395 programmer from the underlying byte size. */
396 kind = bitsize / 8;
398 if (kind == 4)
399 saw_i4 = true;
400 if (kind == 8)
401 saw_i8 = true;
403 gfc_integer_kinds[i_index].kind = kind;
404 gfc_integer_kinds[i_index].radix = 2;
405 gfc_integer_kinds[i_index].digits = bitsize - 1;
406 gfc_integer_kinds[i_index].bit_size = bitsize;
408 gfc_logical_kinds[i_index].kind = kind;
409 gfc_logical_kinds[i_index].bit_size = bitsize;
411 i_index += 1;
414 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
415 used for large file access. */
417 if (saw_i8)
418 gfc_intio_kind = 8;
419 else
420 gfc_intio_kind = 4;
422 /* If we do not at least have kind = 4, everything is pointless. */
423 gcc_assert(saw_i4);
425 /* Set the maximum integer kind. Used with at least BOZ constants. */
426 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
428 r_index = 0;
429 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
431 scalar_float_mode mode = float_mode_iter.require ();
432 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
433 int kind;
435 if (fmt == NULL)
436 continue;
437 if (!targetm.scalar_mode_supported_p (mode))
438 continue;
440 /* Only let float, double, long double and __float128 go through.
441 Runtime support for others is not provided, so they would be
442 useless. */
443 if (!targetm.libgcc_floating_mode_supported_p (mode))
444 continue;
445 if (mode != TYPE_MODE (float_type_node)
446 && (mode != TYPE_MODE (double_type_node))
447 && (mode != TYPE_MODE (long_double_type_node))
448 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
449 && (mode != TFmode)
450 #endif
452 continue;
454 /* Let the kind equal the precision divided by 8, rounding up. Again,
455 this insulates the programmer from the underlying byte size.
457 Also, it effectively deals with IEEE extended formats. There, the
458 total size of the type may equal 16, but it's got 6 bytes of padding
459 and the increased size can get in the way of a real IEEE quad format
460 which may also be supported by the target.
462 We round up so as to handle IA-64 __floatreg (RFmode), which is an
463 82 bit type. Not to be confused with __float80 (XFmode), which is
464 an 80 bit type also supported by IA-64. So XFmode should come out
465 to be kind=10, and RFmode should come out to be kind=11. Egads. */
467 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
469 if (kind == 4)
470 saw_r4 = true;
471 if (kind == 8)
472 saw_r8 = true;
473 if (kind == 10)
474 saw_r10 = true;
475 if (kind == 16)
476 saw_r16 = true;
478 /* Careful we don't stumble a weird internal mode. */
479 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
480 /* Or have too many modes for the allocated space. */
481 gcc_assert (r_index != MAX_REAL_KINDS);
483 gfc_real_kinds[r_index].kind = kind;
484 gfc_real_kinds[r_index].radix = fmt->b;
485 gfc_real_kinds[r_index].digits = fmt->p;
486 gfc_real_kinds[r_index].min_exponent = fmt->emin;
487 gfc_real_kinds[r_index].max_exponent = fmt->emax;
488 if (fmt->pnan < fmt->p)
489 /* This is an IBM extended double format (or the MIPS variant)
490 made up of two IEEE doubles. The value of the long double is
491 the sum of the values of the two parts. The most significant
492 part is required to be the value of the long double rounded
493 to the nearest double. If we use emax of 1024 then we can't
494 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
495 rounding will make the most significant part overflow. */
496 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
497 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
498 r_index += 1;
501 /* Choose the default integer kind. We choose 4 unless the user directs us
502 otherwise. Even if the user specified that the default integer kind is 8,
503 the numeric storage size is not 64 bits. In this case, a warning will be
504 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
506 gfc_numeric_storage_size = 4 * 8;
508 if (flag_default_integer)
510 if (!saw_i8)
511 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
512 "%<-fdefault-integer-8%> option");
514 gfc_default_integer_kind = 8;
517 else if (flag_integer4_kind == 8)
519 if (!saw_i8)
520 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
521 "%<-finteger-4-integer-8%> option");
523 gfc_default_integer_kind = 8;
525 else if (saw_i4)
527 gfc_default_integer_kind = 4;
529 else
531 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
532 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
535 /* Choose the default real kind. Again, we choose 4 when possible. */
536 if (flag_default_real_8)
538 if (!saw_r8)
539 gfc_fatal_error ("REAL(KIND=8) is not available for "
540 "%<-fdefault-real-8%> option");
542 gfc_default_real_kind = 8;
544 else if (flag_default_real_10)
546 if (!saw_r10)
547 gfc_fatal_error ("REAL(KIND=10) is not available for "
548 "%<-fdefault-real-10%> option");
550 gfc_default_real_kind = 10;
552 else if (flag_default_real_16)
554 if (!saw_r16)
555 gfc_fatal_error ("REAL(KIND=16) is not available for "
556 "%<-fdefault-real-16%> option");
558 gfc_default_real_kind = 16;
560 else if (flag_real4_kind == 8)
562 if (!saw_r8)
563 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
564 "option");
566 gfc_default_real_kind = 8;
568 else if (flag_real4_kind == 10)
570 if (!saw_r10)
571 gfc_fatal_error ("REAL(KIND=10) is not available for "
572 "%<-freal-4-real-10%> option");
574 gfc_default_real_kind = 10;
576 else if (flag_real4_kind == 16)
578 if (!saw_r16)
579 gfc_fatal_error ("REAL(KIND=16) is not available for "
580 "%<-freal-4-real-16%> option");
582 gfc_default_real_kind = 16;
584 else if (saw_r4)
585 gfc_default_real_kind = 4;
586 else
587 gfc_default_real_kind = gfc_real_kinds[0].kind;
589 /* Choose the default double kind. If -fdefault-real and -fdefault-double
590 are specified, we use kind=8, if it's available. If -fdefault-real is
591 specified without -fdefault-double, we use kind=16, if it's available.
592 Otherwise we do not change anything. */
593 if (flag_default_double && saw_r8)
594 gfc_default_double_kind = 8;
595 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
597 /* Use largest available kind. */
598 if (saw_r16)
599 gfc_default_double_kind = 16;
600 else if (saw_r10)
601 gfc_default_double_kind = 10;
602 else if (saw_r8)
603 gfc_default_double_kind = 8;
604 else
605 gfc_default_double_kind = gfc_default_real_kind;
607 else if (flag_real8_kind == 4)
609 if (!saw_r4)
610 gfc_fatal_error ("REAL(KIND=4) is not available for "
611 "%<-freal-8-real-4%> option");
613 gfc_default_double_kind = 4;
615 else if (flag_real8_kind == 10 )
617 if (!saw_r10)
618 gfc_fatal_error ("REAL(KIND=10) is not available for "
619 "%<-freal-8-real-10%> option");
621 gfc_default_double_kind = 10;
623 else if (flag_real8_kind == 16 )
625 if (!saw_r16)
626 gfc_fatal_error ("REAL(KIND=10) is not available for "
627 "%<-freal-8-real-16%> option");
629 gfc_default_double_kind = 16;
631 else if (saw_r4 && saw_r8)
632 gfc_default_double_kind = 8;
633 else
635 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
636 real ... occupies two contiguous numeric storage units.
638 Therefore we must be supplied a kind twice as large as we chose
639 for single precision. There are loopholes, in that double
640 precision must *occupy* two storage units, though it doesn't have
641 to *use* two storage units. Which means that you can make this
642 kind artificially wide by padding it. But at present there are
643 no GCC targets for which a two-word type does not exist, so we
644 just let gfc_validate_kind abort and tell us if something breaks. */
646 gfc_default_double_kind
647 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
650 /* The default logical kind is constrained to be the same as the
651 default integer kind. Similarly with complex and real. */
652 gfc_default_logical_kind = gfc_default_integer_kind;
653 gfc_default_complex_kind = gfc_default_real_kind;
655 /* We only have two character kinds: ASCII and UCS-4.
656 ASCII corresponds to a 8-bit integer type, if one is available.
657 UCS-4 corresponds to a 32-bit integer type, if one is available. */
658 i_index = 0;
659 if ((kind = get_int_kind_from_width (8)) > 0)
661 gfc_character_kinds[i_index].kind = kind;
662 gfc_character_kinds[i_index].bit_size = 8;
663 gfc_character_kinds[i_index].name = "ascii";
664 i_index++;
666 if ((kind = get_int_kind_from_width (32)) > 0)
668 gfc_character_kinds[i_index].kind = kind;
669 gfc_character_kinds[i_index].bit_size = 32;
670 gfc_character_kinds[i_index].name = "iso_10646";
671 i_index++;
674 /* Choose the smallest integer kind for our default character. */
675 gfc_default_character_kind = gfc_character_kinds[0].kind;
676 gfc_character_storage_size = gfc_default_character_kind * 8;
678 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
680 /* Pick a kind the same size as the C "int" type. */
681 gfc_c_int_kind = INT_TYPE_SIZE / 8;
683 /* Choose atomic kinds to match C's int. */
684 gfc_atomic_int_kind = gfc_c_int_kind;
685 gfc_atomic_logical_kind = gfc_c_int_kind;
689 /* Make sure that a valid kind is present. Returns an index into the
690 associated kinds array, -1 if the kind is not present. */
692 static int
693 validate_integer (int kind)
695 int i;
697 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
698 if (gfc_integer_kinds[i].kind == kind)
699 return i;
701 return -1;
704 static int
705 validate_real (int kind)
707 int i;
709 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
710 if (gfc_real_kinds[i].kind == kind)
711 return i;
713 return -1;
716 static int
717 validate_logical (int kind)
719 int i;
721 for (i = 0; gfc_logical_kinds[i].kind; i++)
722 if (gfc_logical_kinds[i].kind == kind)
723 return i;
725 return -1;
728 static int
729 validate_character (int kind)
731 int i;
733 for (i = 0; gfc_character_kinds[i].kind; i++)
734 if (gfc_character_kinds[i].kind == kind)
735 return i;
737 return -1;
740 /* Validate a kind given a basic type. The return value is the same
741 for the child functions, with -1 indicating nonexistence of the
742 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
745 gfc_validate_kind (bt type, int kind, bool may_fail)
747 int rc;
749 switch (type)
751 case BT_REAL: /* Fall through */
752 case BT_COMPLEX:
753 rc = validate_real (kind);
754 break;
755 case BT_INTEGER:
756 rc = validate_integer (kind);
757 break;
758 case BT_LOGICAL:
759 rc = validate_logical (kind);
760 break;
761 case BT_CHARACTER:
762 rc = validate_character (kind);
763 break;
765 default:
766 gfc_internal_error ("gfc_validate_kind(): Got bad type");
769 if (rc < 0 && !may_fail)
770 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
772 return rc;
776 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
777 Reuse common type nodes where possible. Recognize if the kind matches up
778 with a C type. This will be used later in determining which routines may
779 be scarfed from libm. */
781 static tree
782 gfc_build_int_type (gfc_integer_info *info)
784 int mode_precision = info->bit_size;
786 if (mode_precision == CHAR_TYPE_SIZE)
787 info->c_char = 1;
788 if (mode_precision == SHORT_TYPE_SIZE)
789 info->c_short = 1;
790 if (mode_precision == INT_TYPE_SIZE)
791 info->c_int = 1;
792 if (mode_precision == LONG_TYPE_SIZE)
793 info->c_long = 1;
794 if (mode_precision == LONG_LONG_TYPE_SIZE)
795 info->c_long_long = 1;
797 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
798 return intQI_type_node;
799 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
800 return intHI_type_node;
801 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
802 return intSI_type_node;
803 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
804 return intDI_type_node;
805 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
806 return intTI_type_node;
808 return make_signed_type (mode_precision);
811 tree
812 gfc_build_uint_type (int size)
814 if (size == CHAR_TYPE_SIZE)
815 return unsigned_char_type_node;
816 if (size == SHORT_TYPE_SIZE)
817 return short_unsigned_type_node;
818 if (size == INT_TYPE_SIZE)
819 return unsigned_type_node;
820 if (size == LONG_TYPE_SIZE)
821 return long_unsigned_type_node;
822 if (size == LONG_LONG_TYPE_SIZE)
823 return long_long_unsigned_type_node;
825 return make_unsigned_type (size);
829 static tree
830 gfc_build_real_type (gfc_real_info *info)
832 int mode_precision = info->mode_precision;
833 tree new_type;
835 if (mode_precision == FLOAT_TYPE_SIZE)
836 info->c_float = 1;
837 if (mode_precision == DOUBLE_TYPE_SIZE)
838 info->c_double = 1;
839 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
840 info->c_long_double = 1;
841 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
843 info->c_float128 = 1;
844 gfc_real16_is_float128 = true;
847 if (TYPE_PRECISION (float_type_node) == mode_precision)
848 return float_type_node;
849 if (TYPE_PRECISION (double_type_node) == mode_precision)
850 return double_type_node;
851 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
852 return long_double_type_node;
854 new_type = make_node (REAL_TYPE);
855 TYPE_PRECISION (new_type) = mode_precision;
856 layout_type (new_type);
857 return new_type;
860 static tree
861 gfc_build_complex_type (tree scalar_type)
863 tree new_type;
865 if (scalar_type == NULL)
866 return NULL;
867 if (scalar_type == float_type_node)
868 return complex_float_type_node;
869 if (scalar_type == double_type_node)
870 return complex_double_type_node;
871 if (scalar_type == long_double_type_node)
872 return complex_long_double_type_node;
874 new_type = make_node (COMPLEX_TYPE);
875 TREE_TYPE (new_type) = scalar_type;
876 layout_type (new_type);
877 return new_type;
880 static tree
881 gfc_build_logical_type (gfc_logical_info *info)
883 int bit_size = info->bit_size;
884 tree new_type;
886 if (bit_size == BOOL_TYPE_SIZE)
888 info->c_bool = 1;
889 return boolean_type_node;
892 new_type = make_unsigned_type (bit_size);
893 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
894 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
895 TYPE_PRECISION (new_type) = 1;
897 return new_type;
901 /* Create the backend type nodes. We map them to their
902 equivalent C type, at least for now. We also give
903 names to the types here, and we push them in the
904 global binding level context.*/
906 void
907 gfc_init_types (void)
909 char name_buf[26];
910 int index;
911 tree type;
912 unsigned n;
914 /* Create and name the types. */
915 #define PUSH_TYPE(name, node) \
916 pushdecl (build_decl (input_location, \
917 TYPE_DECL, get_identifier (name), node))
919 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
921 type = gfc_build_int_type (&gfc_integer_kinds[index]);
922 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
923 if (TYPE_STRING_FLAG (type))
924 type = make_signed_type (gfc_integer_kinds[index].bit_size);
925 gfc_integer_types[index] = type;
926 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
927 gfc_integer_kinds[index].kind);
928 PUSH_TYPE (name_buf, type);
931 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
933 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
934 gfc_logical_types[index] = type;
935 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
936 gfc_logical_kinds[index].kind);
937 PUSH_TYPE (name_buf, type);
940 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
942 type = gfc_build_real_type (&gfc_real_kinds[index]);
943 gfc_real_types[index] = type;
944 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
945 gfc_real_kinds[index].kind);
946 PUSH_TYPE (name_buf, type);
948 if (gfc_real_kinds[index].c_float128)
949 gfc_float128_type_node = type;
951 type = gfc_build_complex_type (type);
952 gfc_complex_types[index] = type;
953 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
954 gfc_real_kinds[index].kind);
955 PUSH_TYPE (name_buf, type);
957 if (gfc_real_kinds[index].c_float128)
958 gfc_complex_float128_type_node = type;
961 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
963 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
964 type = build_qualified_type (type, TYPE_UNQUALIFIED);
965 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
966 gfc_character_kinds[index].kind);
967 PUSH_TYPE (name_buf, type);
968 gfc_character_types[index] = type;
969 gfc_pcharacter_types[index] = build_pointer_type (type);
971 gfc_character1_type_node = gfc_character_types[0];
973 PUSH_TYPE ("byte", unsigned_char_type_node);
974 PUSH_TYPE ("void", void_type_node);
976 /* DBX debugging output gets upset if these aren't set. */
977 if (!TYPE_NAME (integer_type_node))
978 PUSH_TYPE ("c_integer", integer_type_node);
979 if (!TYPE_NAME (char_type_node))
980 PUSH_TYPE ("c_char", char_type_node);
982 #undef PUSH_TYPE
984 pvoid_type_node = build_pointer_type (void_type_node);
985 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
986 ppvoid_type_node = build_pointer_type (pvoid_type_node);
987 pchar_type_node = build_pointer_type (gfc_character1_type_node);
988 pfunc_type_node
989 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
991 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
992 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
993 since this function is called before gfc_init_constants. */
994 gfc_array_range_type
995 = build_range_type (gfc_array_index_type,
996 build_int_cst (gfc_array_index_type, 0),
997 NULL_TREE);
999 /* The maximum array element size that can be handled is determined
1000 by the number of bits available to store this field in the array
1001 descriptor. */
1003 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
1004 gfc_max_array_element_size
1005 = wide_int_to_tree (size_type_node,
1006 wi::mask (n, UNSIGNED,
1007 TYPE_PRECISION (size_type_node)));
1010 logical_type_node = gfc_get_logical_type (gfc_default_logical_kind);
1011 logical_true_node = build_int_cst (logical_type_node, 1);
1012 logical_false_node = build_int_cst (logical_type_node, 0);
1014 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
1015 gfc_charlen_int_kind = 4;
1016 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1019 /* Get the type node for the given type and kind. */
1021 tree
1022 gfc_get_int_type (int kind)
1024 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1025 return index < 0 ? 0 : gfc_integer_types[index];
1028 tree
1029 gfc_get_real_type (int kind)
1031 int index = gfc_validate_kind (BT_REAL, kind, true);
1032 return index < 0 ? 0 : gfc_real_types[index];
1035 tree
1036 gfc_get_complex_type (int kind)
1038 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1039 return index < 0 ? 0 : gfc_complex_types[index];
1042 tree
1043 gfc_get_logical_type (int kind)
1045 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1046 return index < 0 ? 0 : gfc_logical_types[index];
1049 tree
1050 gfc_get_char_type (int kind)
1052 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1053 return index < 0 ? 0 : gfc_character_types[index];
1056 tree
1057 gfc_get_pchar_type (int kind)
1059 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1060 return index < 0 ? 0 : gfc_pcharacter_types[index];
1064 /* Create a character type with the given kind and length. */
1066 tree
1067 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1069 tree bounds, type;
1071 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1072 type = build_array_type (eltype, bounds);
1073 TYPE_STRING_FLAG (type) = 1;
1075 return type;
1078 tree
1079 gfc_get_character_type_len (int kind, tree len)
1081 gfc_validate_kind (BT_CHARACTER, kind, false);
1082 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1086 /* Get a type node for a character kind. */
1088 tree
1089 gfc_get_character_type (int kind, gfc_charlen * cl)
1091 tree len;
1093 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1094 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1095 len = build_fold_indirect_ref (len);
1097 return gfc_get_character_type_len (kind, len);
1100 /* Convert a basic type. This will be an array for character types. */
1102 tree
1103 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1105 tree basetype;
1107 switch (spec->type)
1109 case BT_UNKNOWN:
1110 gcc_unreachable ();
1112 case BT_INTEGER:
1113 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1114 has been resolved. This is done so we can convert C_PTR and
1115 C_FUNPTR to simple variables that get translated to (void *). */
1116 if (spec->f90_type == BT_VOID)
1118 if (spec->u.derived
1119 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1120 basetype = ptr_type_node;
1121 else
1122 basetype = pfunc_type_node;
1124 else
1125 basetype = gfc_get_int_type (spec->kind);
1126 break;
1128 case BT_REAL:
1129 basetype = gfc_get_real_type (spec->kind);
1130 break;
1132 case BT_COMPLEX:
1133 basetype = gfc_get_complex_type (spec->kind);
1134 break;
1136 case BT_LOGICAL:
1137 basetype = gfc_get_logical_type (spec->kind);
1138 break;
1140 case BT_CHARACTER:
1141 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1142 break;
1144 case BT_HOLLERITH:
1145 /* Since this cannot be used, return a length one character. */
1146 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1147 gfc_index_one_node);
1148 break;
1150 case BT_UNION:
1151 basetype = gfc_get_union_type (spec->u.derived);
1152 break;
1154 case BT_DERIVED:
1155 case BT_CLASS:
1156 basetype = gfc_get_derived_type (spec->u.derived, codim);
1158 if (spec->type == BT_CLASS)
1159 GFC_CLASS_TYPE_P (basetype) = 1;
1161 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1162 type and kind to fit a (void *) and the basetype returned was a
1163 ptr_type_node. We need to pass up this new information to the
1164 symbol that was declared of type C_PTR or C_FUNPTR. */
1165 if (spec->u.derived->ts.f90_type == BT_VOID)
1167 spec->type = BT_INTEGER;
1168 spec->kind = gfc_index_integer_kind;
1169 spec->f90_type = BT_VOID;
1171 break;
1172 case BT_VOID:
1173 case BT_ASSUMED:
1174 /* This is for the second arg to c_f_pointer and c_f_procpointer
1175 of the iso_c_binding module, to accept any ptr type. */
1176 basetype = ptr_type_node;
1177 if (spec->f90_type == BT_VOID)
1179 if (spec->u.derived
1180 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1181 basetype = ptr_type_node;
1182 else
1183 basetype = pfunc_type_node;
1185 break;
1186 default:
1187 gcc_unreachable ();
1189 return basetype;
1192 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1194 static tree
1195 gfc_conv_array_bound (gfc_expr * expr)
1197 /* If expr is an integer constant, return that. */
1198 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1199 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1201 /* Otherwise return NULL. */
1202 return NULL_TREE;
1205 /* Return the type of an element of the array. Note that scalar coarrays
1206 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1207 (with POINTER_TYPE stripped) is returned. */
1209 tree
1210 gfc_get_element_type (tree type)
1212 tree element;
1214 if (GFC_ARRAY_TYPE_P (type))
1216 if (TREE_CODE (type) == POINTER_TYPE)
1217 type = TREE_TYPE (type);
1218 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1220 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1221 element = type;
1223 else
1225 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1226 element = TREE_TYPE (type);
1229 else
1231 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1232 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1234 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1235 element = TREE_TYPE (element);
1237 /* For arrays, which are not scalar coarrays. */
1238 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1239 element = TREE_TYPE (element);
1242 return element;
1245 /* Build an array. This function is called from gfc_sym_type().
1246 Actually returns array descriptor type.
1248 Format of array descriptors is as follows:
1250 struct gfc_array_descriptor
1252 array *data
1253 index offset;
1254 index dtype;
1255 struct descriptor_dimension dimension[N_DIM];
1258 struct descriptor_dimension
1260 index stride;
1261 index lbound;
1262 index ubound;
1265 Translation code should use gfc_conv_descriptor_* rather than
1266 accessing the descriptor directly. Any changes to the array
1267 descriptor type will require changes in gfc_conv_descriptor_* and
1268 gfc_build_array_initializer.
1270 This is represented internally as a RECORD_TYPE. The index nodes
1271 are gfc_array_index_type and the data node is a pointer to the
1272 data. See below for the handling of character types.
1274 The dtype member is formatted as follows:
1275 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1276 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1277 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1279 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1280 this generated poor code for assumed/deferred size arrays. These
1281 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1282 of the GENERIC grammar. Also, there is no way to explicitly set
1283 the array stride, so all data must be packed(1). I've tried to
1284 mark all the functions which would require modification with a GCC
1285 ARRAYS comment.
1287 The data component points to the first element in the array. The
1288 offset field is the position of the origin of the array (i.e. element
1289 (0, 0 ...)). This may be outside the bounds of the array.
1291 An element is accessed by
1292 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1293 This gives good performance as the computation does not involve the
1294 bounds of the array. For packed arrays, this is optimized further
1295 by substituting the known strides.
1297 This system has one problem: all array bounds must be within 2^31
1298 elements of the origin (2^63 on 64-bit machines). For example
1299 integer, dimension (80000:90000, 80000:90000, 2) :: array
1300 may not work properly on 32-bit machines because 80000*80000 >
1301 2^31, so the calculation for stride2 would overflow. This may
1302 still work, but I haven't checked, and it relies on the overflow
1303 doing the right thing.
1305 The way to fix this problem is to access elements as follows:
1306 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1307 Obviously this is much slower. I will make this a compile time
1308 option, something like -fsmall-array-offsets. Mixing code compiled
1309 with and without this switch will work.
1311 (1) This can be worked around by modifying the upper bound of the
1312 previous dimension. This requires extra fields in the descriptor
1313 (both real_ubound and fake_ubound). */
1316 /* Returns true if the array sym does not require a descriptor. */
1319 gfc_is_nodesc_array (gfc_symbol * sym)
1321 symbol_attribute *array_attr;
1322 gfc_array_spec *as;
1323 bool is_classarray = IS_CLASS_ARRAY (sym);
1325 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1326 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1328 gcc_assert (array_attr->dimension || array_attr->codimension);
1330 /* We only want local arrays. */
1331 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1332 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1333 || array_attr->allocatable)
1334 return 0;
1336 /* We want a descriptor for associate-name arrays that do not have an
1337 explicitly known shape already. */
1338 if (sym->assoc && as->type != AS_EXPLICIT)
1339 return 0;
1341 /* The dummy is stored in sym and not in the component. */
1342 if (sym->attr.dummy)
1343 return as->type != AS_ASSUMED_SHAPE
1344 && as->type != AS_ASSUMED_RANK;
1346 if (sym->attr.result || sym->attr.function)
1347 return 0;
1349 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1351 return 1;
1355 /* Create an array descriptor type. */
1357 static tree
1358 gfc_build_array_type (tree type, gfc_array_spec * as,
1359 enum gfc_array_kind akind, bool restricted,
1360 bool contiguous, int codim)
1362 tree lbound[GFC_MAX_DIMENSIONS];
1363 tree ubound[GFC_MAX_DIMENSIONS];
1364 int n, corank;
1366 /* Assumed-shape arrays do not have codimension information stored in the
1367 descriptor. */
1368 corank = MAX (as->corank, codim);
1369 if (as->type == AS_ASSUMED_SHAPE ||
1370 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1371 corank = codim;
1373 if (as->type == AS_ASSUMED_RANK)
1374 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1376 lbound[n] = NULL_TREE;
1377 ubound[n] = NULL_TREE;
1380 for (n = 0; n < as->rank; n++)
1382 /* Create expressions for the known bounds of the array. */
1383 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1384 lbound[n] = gfc_index_one_node;
1385 else
1386 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1387 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1390 for (n = as->rank; n < as->rank + corank; n++)
1392 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1393 lbound[n] = gfc_index_one_node;
1394 else
1395 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1397 if (n < as->rank + corank - 1)
1398 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1401 if (as->type == AS_ASSUMED_SHAPE)
1402 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1403 : GFC_ARRAY_ASSUMED_SHAPE;
1404 else if (as->type == AS_ASSUMED_RANK)
1405 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1406 : GFC_ARRAY_ASSUMED_RANK;
1407 return gfc_get_array_type_bounds (type, as->rank == -1
1408 ? GFC_MAX_DIMENSIONS : as->rank,
1409 corank, lbound, ubound, 0, akind,
1410 restricted);
1413 /* Returns the struct descriptor_dimension type. */
1415 static tree
1416 gfc_get_desc_dim_type (void)
1418 tree type;
1419 tree decl, *chain = NULL;
1421 if (gfc_desc_dim_type)
1422 return gfc_desc_dim_type;
1424 /* Build the type node. */
1425 type = make_node (RECORD_TYPE);
1427 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1428 TYPE_PACKED (type) = 1;
1430 /* Consists of the stride, lbound and ubound members. */
1431 decl = gfc_add_field_to_struct_1 (type,
1432 get_identifier ("stride"),
1433 gfc_array_index_type, &chain);
1434 TREE_NO_WARNING (decl) = 1;
1436 decl = gfc_add_field_to_struct_1 (type,
1437 get_identifier ("lbound"),
1438 gfc_array_index_type, &chain);
1439 TREE_NO_WARNING (decl) = 1;
1441 decl = gfc_add_field_to_struct_1 (type,
1442 get_identifier ("ubound"),
1443 gfc_array_index_type, &chain);
1444 TREE_NO_WARNING (decl) = 1;
1446 /* Finish off the type. */
1447 gfc_finish_type (type);
1448 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1450 gfc_desc_dim_type = type;
1451 return type;
1455 /* Return the DTYPE for an array. This describes the type and type parameters
1456 of the array. */
1457 /* TODO: Only call this when the value is actually used, and make all the
1458 unknown cases abort. */
1460 tree
1461 gfc_get_dtype_rank_type (int rank, tree etype)
1463 tree size;
1464 int n;
1465 HOST_WIDE_INT i;
1466 tree tmp;
1467 tree dtype;
1469 switch (TREE_CODE (etype))
1471 case INTEGER_TYPE:
1472 n = BT_INTEGER;
1473 break;
1475 case BOOLEAN_TYPE:
1476 n = BT_LOGICAL;
1477 break;
1479 case REAL_TYPE:
1480 n = BT_REAL;
1481 break;
1483 case COMPLEX_TYPE:
1484 n = BT_COMPLEX;
1485 break;
1487 /* We will never have arrays of arrays. */
1488 case RECORD_TYPE:
1489 n = BT_DERIVED;
1490 break;
1492 case ARRAY_TYPE:
1493 n = BT_CHARACTER;
1494 break;
1496 case POINTER_TYPE:
1497 n = BT_ASSUMED;
1498 break;
1500 default:
1501 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1502 /* We can strange array types for temporary arrays. */
1503 return gfc_index_zero_node;
1506 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1507 size = TYPE_SIZE_UNIT (etype);
1509 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1510 if (size && INTEGER_CST_P (size))
1512 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1513 gfc_fatal_error ("Array element size too big at %C");
1515 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1517 dtype = build_int_cst (gfc_array_index_type, i);
1519 if (size && !INTEGER_CST_P (size))
1521 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1522 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1523 gfc_array_index_type,
1524 fold_convert (gfc_array_index_type, size), tmp);
1525 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1526 tmp, dtype);
1528 /* If we don't know the size we leave it as zero. This should never happen
1529 for anything that is actually used. */
1530 /* TODO: Check this is actually true, particularly when repacking
1531 assumed size parameters. */
1533 return dtype;
1537 tree
1538 gfc_get_dtype (tree type)
1540 tree dtype;
1541 tree etype;
1542 int rank;
1544 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1546 if (GFC_TYPE_ARRAY_DTYPE (type))
1547 return GFC_TYPE_ARRAY_DTYPE (type);
1549 rank = GFC_TYPE_ARRAY_RANK (type);
1550 etype = gfc_get_element_type (type);
1551 dtype = gfc_get_dtype_rank_type (rank, etype);
1553 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1554 return dtype;
1558 /* Build an array type for use without a descriptor, packed according
1559 to the value of PACKED. */
1561 tree
1562 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1563 bool restricted)
1565 tree range;
1566 tree type;
1567 tree tmp;
1568 int n;
1569 int known_stride;
1570 int known_offset;
1571 mpz_t offset;
1572 mpz_t stride;
1573 mpz_t delta;
1574 gfc_expr *expr;
1576 mpz_init_set_ui (offset, 0);
1577 mpz_init_set_ui (stride, 1);
1578 mpz_init (delta);
1580 /* We don't use build_array_type because this does not include include
1581 lang-specific information (i.e. the bounds of the array) when checking
1582 for duplicates. */
1583 if (as->rank)
1584 type = make_node (ARRAY_TYPE);
1585 else
1586 type = build_variant_type_copy (etype);
1588 GFC_ARRAY_TYPE_P (type) = 1;
1589 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1591 known_stride = (packed != PACKED_NO);
1592 known_offset = 1;
1593 for (n = 0; n < as->rank; n++)
1595 /* Fill in the stride and bound components of the type. */
1596 if (known_stride)
1597 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1598 else
1599 tmp = NULL_TREE;
1600 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1602 expr = as->lower[n];
1603 if (expr->expr_type == EXPR_CONSTANT)
1605 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1606 gfc_index_integer_kind);
1608 else
1610 known_stride = 0;
1611 tmp = NULL_TREE;
1613 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1615 if (known_stride)
1617 /* Calculate the offset. */
1618 mpz_mul (delta, stride, as->lower[n]->value.integer);
1619 mpz_sub (offset, offset, delta);
1621 else
1622 known_offset = 0;
1624 expr = as->upper[n];
1625 if (expr && expr->expr_type == EXPR_CONSTANT)
1627 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1628 gfc_index_integer_kind);
1630 else
1632 tmp = NULL_TREE;
1633 known_stride = 0;
1635 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1637 if (known_stride)
1639 /* Calculate the stride. */
1640 mpz_sub (delta, as->upper[n]->value.integer,
1641 as->lower[n]->value.integer);
1642 mpz_add_ui (delta, delta, 1);
1643 mpz_mul (stride, stride, delta);
1646 /* Only the first stride is known for partial packed arrays. */
1647 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1648 known_stride = 0;
1650 for (n = as->rank; n < as->rank + as->corank; n++)
1652 expr = as->lower[n];
1653 if (expr->expr_type == EXPR_CONSTANT)
1654 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1655 gfc_index_integer_kind);
1656 else
1657 tmp = NULL_TREE;
1658 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1660 expr = as->upper[n];
1661 if (expr && expr->expr_type == EXPR_CONSTANT)
1662 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1663 gfc_index_integer_kind);
1664 else
1665 tmp = NULL_TREE;
1666 if (n < as->rank + as->corank - 1)
1667 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1670 if (known_offset)
1672 GFC_TYPE_ARRAY_OFFSET (type) =
1673 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1675 else
1676 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1678 if (known_stride)
1680 GFC_TYPE_ARRAY_SIZE (type) =
1681 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1683 else
1684 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1686 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1687 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1688 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1689 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1690 NULL_TREE);
1691 /* TODO: use main type if it is unbounded. */
1692 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1693 build_pointer_type (build_array_type (etype, range));
1694 if (restricted)
1695 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1696 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1697 TYPE_QUAL_RESTRICT);
1699 if (as->rank == 0)
1701 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1703 type = build_pointer_type (type);
1705 if (restricted)
1706 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1708 GFC_ARRAY_TYPE_P (type) = 1;
1709 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1712 return type;
1715 if (known_stride)
1717 mpz_sub_ui (stride, stride, 1);
1718 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1720 else
1721 range = NULL_TREE;
1723 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1724 TYPE_DOMAIN (type) = range;
1726 build_pointer_type (etype);
1727 TREE_TYPE (type) = etype;
1729 layout_type (type);
1731 mpz_clear (offset);
1732 mpz_clear (stride);
1733 mpz_clear (delta);
1735 /* Represent packed arrays as multi-dimensional if they have rank >
1736 1 and with proper bounds, instead of flat arrays. This makes for
1737 better debug info. */
1738 if (known_offset)
1740 tree gtype = etype, rtype, type_decl;
1742 for (n = as->rank - 1; n >= 0; n--)
1744 rtype = build_range_type (gfc_array_index_type,
1745 GFC_TYPE_ARRAY_LBOUND (type, n),
1746 GFC_TYPE_ARRAY_UBOUND (type, n));
1747 gtype = build_array_type (gtype, rtype);
1749 TYPE_NAME (type) = type_decl = build_decl (input_location,
1750 TYPE_DECL, NULL, gtype);
1751 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1754 if (packed != PACKED_STATIC || !known_stride
1755 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1757 /* For dummy arrays and automatic (heap allocated) arrays we
1758 want a pointer to the array. */
1759 type = build_pointer_type (type);
1760 if (restricted)
1761 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1762 GFC_ARRAY_TYPE_P (type) = 1;
1763 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1765 return type;
1769 /* Return or create the base type for an array descriptor. */
1771 static tree
1772 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1774 tree fat_type, decl, arraytype, *chain = NULL;
1775 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1776 int idx;
1778 /* Assumed-rank array. */
1779 if (dimen == -1)
1780 dimen = GFC_MAX_DIMENSIONS;
1782 idx = 2 * (codimen + dimen) + restricted;
1784 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1786 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1788 if (gfc_array_descriptor_base_caf[idx])
1789 return gfc_array_descriptor_base_caf[idx];
1791 else if (gfc_array_descriptor_base[idx])
1792 return gfc_array_descriptor_base[idx];
1794 /* Build the type node. */
1795 fat_type = make_node (RECORD_TYPE);
1797 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1798 TYPE_NAME (fat_type) = get_identifier (name);
1799 TYPE_NAMELESS (fat_type) = 1;
1801 /* Add the data member as the first element of the descriptor. */
1802 decl = gfc_add_field_to_struct_1 (fat_type,
1803 get_identifier ("data"),
1804 (restricted
1805 ? prvoid_type_node
1806 : ptr_type_node), &chain);
1808 /* Add the base component. */
1809 decl = gfc_add_field_to_struct_1 (fat_type,
1810 get_identifier ("offset"),
1811 gfc_array_index_type, &chain);
1812 TREE_NO_WARNING (decl) = 1;
1814 /* Add the dtype component. */
1815 decl = gfc_add_field_to_struct_1 (fat_type,
1816 get_identifier ("dtype"),
1817 gfc_array_index_type, &chain);
1818 TREE_NO_WARNING (decl) = 1;
1820 /* Add the span component. */
1821 decl = gfc_add_field_to_struct_1 (fat_type,
1822 get_identifier ("span"),
1823 gfc_array_index_type, &chain);
1824 TREE_NO_WARNING (decl) = 1;
1826 /* Build the array type for the stride and bound components. */
1827 if (dimen + codimen > 0)
1829 arraytype =
1830 build_array_type (gfc_get_desc_dim_type (),
1831 build_range_type (gfc_array_index_type,
1832 gfc_index_zero_node,
1833 gfc_rank_cst[codimen + dimen - 1]));
1835 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1836 arraytype, &chain);
1837 TREE_NO_WARNING (decl) = 1;
1840 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1842 decl = gfc_add_field_to_struct_1 (fat_type,
1843 get_identifier ("token"),
1844 prvoid_type_node, &chain);
1845 TREE_NO_WARNING (decl) = 1;
1848 /* Finish off the type. */
1849 gfc_finish_type (fat_type);
1850 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1852 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1853 gfc_array_descriptor_base_caf[idx] = fat_type;
1854 else
1855 gfc_array_descriptor_base[idx] = fat_type;
1857 return fat_type;
1861 /* Build an array (descriptor) type with given bounds. */
1863 tree
1864 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1865 tree * ubound, int packed,
1866 enum gfc_array_kind akind, bool restricted)
1868 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1869 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1870 const char *type_name;
1871 int n;
1873 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1874 fat_type = build_distinct_type_copy (base_type);
1875 /* Make sure that nontarget and target array type have the same canonical
1876 type (and same stub decl for debug info). */
1877 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1878 TYPE_CANONICAL (fat_type) = base_type;
1879 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1881 tmp = TYPE_NAME (etype);
1882 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1883 tmp = DECL_NAME (tmp);
1884 if (tmp)
1885 type_name = IDENTIFIER_POINTER (tmp);
1886 else
1887 type_name = "unknown";
1888 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1889 GFC_MAX_SYMBOL_LEN, type_name);
1890 TYPE_NAME (fat_type) = get_identifier (name);
1891 TYPE_NAMELESS (fat_type) = 1;
1893 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1894 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1896 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1897 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1898 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1899 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1901 /* Build an array descriptor record type. */
1902 if (packed != 0)
1903 stride = gfc_index_one_node;
1904 else
1905 stride = NULL_TREE;
1906 for (n = 0; n < dimen + codimen; n++)
1908 if (n < dimen)
1909 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1911 if (lbound)
1912 lower = lbound[n];
1913 else
1914 lower = NULL_TREE;
1916 if (lower != NULL_TREE)
1918 if (INTEGER_CST_P (lower))
1919 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1920 else
1921 lower = NULL_TREE;
1924 if (codimen && n == dimen + codimen - 1)
1925 break;
1927 upper = ubound[n];
1928 if (upper != NULL_TREE)
1930 if (INTEGER_CST_P (upper))
1931 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1932 else
1933 upper = NULL_TREE;
1936 if (n >= dimen)
1937 continue;
1939 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1941 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1942 gfc_array_index_type, upper, lower);
1943 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1944 gfc_array_index_type, tmp,
1945 gfc_index_one_node);
1946 stride = fold_build2_loc (input_location, MULT_EXPR,
1947 gfc_array_index_type, tmp, stride);
1948 /* Check the folding worked. */
1949 gcc_assert (INTEGER_CST_P (stride));
1951 else
1952 stride = NULL_TREE;
1954 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1956 /* TODO: known offsets for descriptors. */
1957 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1959 if (dimen == 0)
1961 arraytype = build_pointer_type (etype);
1962 if (restricted)
1963 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1965 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1966 return fat_type;
1969 /* We define data as an array with the correct size if possible.
1970 Much better than doing pointer arithmetic. */
1971 if (stride)
1972 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1973 int_const_binop (MINUS_EXPR, stride,
1974 build_int_cst (TREE_TYPE (stride), 1)));
1975 else
1976 rtype = gfc_array_range_type;
1977 arraytype = build_array_type (etype, rtype);
1978 arraytype = build_pointer_type (arraytype);
1979 if (restricted)
1980 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1981 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1983 /* This will generate the base declarations we need to emit debug
1984 information for this type. FIXME: there must be a better way to
1985 avoid divergence between compilations with and without debug
1986 information. */
1988 struct array_descr_info info;
1989 gfc_get_array_descr_info (fat_type, &info);
1990 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1993 return fat_type;
1996 /* Build a pointer type. This function is called from gfc_sym_type(). */
1998 static tree
1999 gfc_build_pointer_type (gfc_symbol * sym, tree type)
2001 /* Array pointer types aren't actually pointers. */
2002 if (sym->attr.dimension)
2003 return type;
2004 else
2005 return build_pointer_type (type);
2008 static tree gfc_nonrestricted_type (tree t);
2009 /* Given two record or union type nodes TO and FROM, ensure
2010 that all fields in FROM have a corresponding field in TO,
2011 their type being nonrestrict variants. This accepts a TO
2012 node that already has a prefix of the fields in FROM. */
2013 static void
2014 mirror_fields (tree to, tree from)
2016 tree fto, ffrom;
2017 tree *chain;
2019 /* Forward to the end of TOs fields. */
2020 fto = TYPE_FIELDS (to);
2021 ffrom = TYPE_FIELDS (from);
2022 chain = &TYPE_FIELDS (to);
2023 while (fto)
2025 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2026 chain = &DECL_CHAIN (fto);
2027 fto = DECL_CHAIN (fto);
2028 ffrom = DECL_CHAIN (ffrom);
2031 /* Now add all fields remaining in FROM (starting with ffrom). */
2032 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2034 tree newfield = copy_node (ffrom);
2035 DECL_CONTEXT (newfield) = to;
2036 /* The store to DECL_CHAIN might seem redundant with the
2037 stores to *chain, but not clearing it here would mean
2038 leaving a chain into the old fields. If ever
2039 our called functions would look at them confusion
2040 will arise. */
2041 DECL_CHAIN (newfield) = NULL_TREE;
2042 *chain = newfield;
2043 chain = &DECL_CHAIN (newfield);
2045 if (TREE_CODE (ffrom) == FIELD_DECL)
2047 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2048 TREE_TYPE (newfield) = elemtype;
2051 *chain = NULL_TREE;
2054 /* Given a type T, returns a different type of the same structure,
2055 except that all types it refers to (recursively) are always
2056 non-restrict qualified types. */
2057 static tree
2058 gfc_nonrestricted_type (tree t)
2060 tree ret = t;
2062 /* If the type isn't laid out yet, don't copy it. If something
2063 needs it for real it should wait until the type got finished. */
2064 if (!TYPE_SIZE (t))
2065 return t;
2067 if (!TYPE_LANG_SPECIFIC (t))
2068 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2069 /* If we're dealing with this very node already further up
2070 the call chain (recursion via pointers and struct members)
2071 we haven't yet determined if we really need a new type node.
2072 Assume we don't, return T itself. */
2073 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2074 return t;
2076 /* If we have calculated this all already, just return it. */
2077 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2078 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2080 /* Mark this type. */
2081 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2083 switch (TREE_CODE (t))
2085 default:
2086 break;
2088 case POINTER_TYPE:
2089 case REFERENCE_TYPE:
2091 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2092 if (totype == TREE_TYPE (t))
2093 ret = t;
2094 else if (TREE_CODE (t) == POINTER_TYPE)
2095 ret = build_pointer_type (totype);
2096 else
2097 ret = build_reference_type (totype);
2098 ret = build_qualified_type (ret,
2099 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2101 break;
2103 case ARRAY_TYPE:
2105 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2106 if (elemtype == TREE_TYPE (t))
2107 ret = t;
2108 else
2110 ret = build_variant_type_copy (t);
2111 TREE_TYPE (ret) = elemtype;
2112 if (TYPE_LANG_SPECIFIC (t)
2113 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2115 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2116 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2117 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2119 TYPE_LANG_SPECIFIC (ret)
2120 = ggc_cleared_alloc<struct lang_type> ();
2121 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2122 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2127 break;
2129 case RECORD_TYPE:
2130 case UNION_TYPE:
2131 case QUAL_UNION_TYPE:
2133 tree field;
2134 /* First determine if we need a new type at all.
2135 Careful, the two calls to gfc_nonrestricted_type per field
2136 might return different values. That happens exactly when
2137 one of the fields reaches back to this very record type
2138 (via pointers). The first calls will assume that we don't
2139 need to copy T (see the error_mark_node marking). If there
2140 are any reasons for copying T apart from having to copy T,
2141 we'll indeed copy it, and the second calls to
2142 gfc_nonrestricted_type will use that new node if they
2143 reach back to T. */
2144 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2145 if (TREE_CODE (field) == FIELD_DECL)
2147 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2148 if (elemtype != TREE_TYPE (field))
2149 break;
2151 if (!field)
2152 break;
2153 ret = build_variant_type_copy (t);
2154 TYPE_FIELDS (ret) = NULL_TREE;
2156 /* Here we make sure that as soon as we know we have to copy
2157 T, that also fields reaching back to us will use the new
2158 copy. It's okay if that copy still contains the old fields,
2159 we won't look at them. */
2160 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2161 mirror_fields (ret, t);
2163 break;
2166 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2167 return ret;
2171 /* Return the type for a symbol. Special handling is required for character
2172 types to get the correct level of indirection.
2173 For functions return the return type.
2174 For subroutines return void_type_node.
2175 Calling this multiple times for the same symbol should be avoided,
2176 especially for character and array types. */
2178 tree
2179 gfc_sym_type (gfc_symbol * sym)
2181 tree type;
2182 int byref;
2183 bool restricted;
2185 /* Procedure Pointers inside COMMON blocks. */
2186 if (sym->attr.proc_pointer && sym->attr.in_common)
2188 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2189 sym->attr.proc_pointer = 0;
2190 type = build_pointer_type (gfc_get_function_type (sym));
2191 sym->attr.proc_pointer = 1;
2192 return type;
2195 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2196 return void_type_node;
2198 /* In the case of a function the fake result variable may have a
2199 type different from the function type, so don't return early in
2200 that case. */
2201 if (sym->backend_decl && !sym->attr.function)
2202 return TREE_TYPE (sym->backend_decl);
2204 if (sym->ts.type == BT_CHARACTER
2205 && ((sym->attr.function && sym->attr.is_bind_c)
2206 || (sym->attr.result
2207 && sym->ns->proc_name
2208 && sym->ns->proc_name->attr.is_bind_c)
2209 || (sym->ts.deferred && (!sym->ts.u.cl
2210 || !sym->ts.u.cl->backend_decl))))
2211 type = gfc_character1_type_node;
2212 else
2213 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2215 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2216 byref = 1;
2217 else
2218 byref = 0;
2220 restricted = !sym->attr.target && !sym->attr.pointer
2221 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2222 if (!restricted)
2223 type = gfc_nonrestricted_type (type);
2225 if (sym->attr.dimension || sym->attr.codimension)
2227 if (gfc_is_nodesc_array (sym))
2229 /* If this is a character argument of unknown length, just use the
2230 base type. */
2231 if (sym->ts.type != BT_CHARACTER
2232 || !(sym->attr.dummy || sym->attr.function)
2233 || sym->ts.u.cl->backend_decl)
2235 type = gfc_get_nodesc_array_type (type, sym->as,
2236 byref ? PACKED_FULL
2237 : PACKED_STATIC,
2238 restricted);
2239 byref = 0;
2242 else
2244 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2245 if (sym->attr.pointer)
2246 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2247 : GFC_ARRAY_POINTER;
2248 else if (sym->attr.allocatable)
2249 akind = GFC_ARRAY_ALLOCATABLE;
2250 type = gfc_build_array_type (type, sym->as, akind, restricted,
2251 sym->attr.contiguous, false);
2254 else
2256 if (sym->attr.allocatable || sym->attr.pointer
2257 || gfc_is_associate_pointer (sym))
2258 type = gfc_build_pointer_type (sym, type);
2261 /* We currently pass all parameters by reference.
2262 See f95_get_function_decl. For dummy function parameters return the
2263 function type. */
2264 if (byref)
2266 /* We must use pointer types for potentially absent variables. The
2267 optimizers assume a reference type argument is never NULL. */
2268 if (sym->attr.optional
2269 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2270 type = build_pointer_type (type);
2271 else
2273 type = build_reference_type (type);
2274 if (restricted)
2275 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2279 return (type);
2282 /* Layout and output debug info for a record type. */
2284 void
2285 gfc_finish_type (tree type)
2287 tree decl;
2289 decl = build_decl (input_location,
2290 TYPE_DECL, NULL_TREE, type);
2291 TYPE_STUB_DECL (type) = decl;
2292 layout_type (type);
2293 rest_of_type_compilation (type, 1);
2294 rest_of_decl_compilation (decl, 1, 0);
2297 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2298 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2299 to the end of the field list pointed to by *CHAIN.
2301 Returns a pointer to the new field. */
2303 static tree
2304 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2306 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2308 DECL_CONTEXT (decl) = context;
2309 DECL_CHAIN (decl) = NULL_TREE;
2310 if (TYPE_FIELDS (context) == NULL_TREE)
2311 TYPE_FIELDS (context) = decl;
2312 if (chain != NULL)
2314 if (*chain != NULL)
2315 **chain = decl;
2316 *chain = &DECL_CHAIN (decl);
2319 return decl;
2322 /* Like `gfc_add_field_to_struct_1', but adds alignment
2323 information. */
2325 tree
2326 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2328 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2330 DECL_INITIAL (decl) = 0;
2331 SET_DECL_ALIGN (decl, 0);
2332 DECL_USER_ALIGN (decl) = 0;
2334 return decl;
2338 /* Copy the backend_decl and component backend_decls if
2339 the two derived type symbols are "equal", as described
2340 in 4.4.2 and resolved by gfc_compare_derived_types. */
2343 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2344 bool from_gsym)
2346 gfc_component *to_cm;
2347 gfc_component *from_cm;
2349 if (from == to)
2350 return 1;
2352 if (from->backend_decl == NULL
2353 || !gfc_compare_derived_types (from, to))
2354 return 0;
2356 to->backend_decl = from->backend_decl;
2358 to_cm = to->components;
2359 from_cm = from->components;
2361 /* Copy the component declarations. If a component is itself
2362 a derived type, we need a copy of its component declarations.
2363 This is done by recursing into gfc_get_derived_type and
2364 ensures that the component's component declarations have
2365 been built. If it is a character, we need the character
2366 length, as well. */
2367 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2369 to_cm->backend_decl = from_cm->backend_decl;
2370 if (from_cm->ts.type == BT_UNION)
2371 gfc_get_union_type (to_cm->ts.u.derived);
2372 else if (from_cm->ts.type == BT_DERIVED
2373 && (!from_cm->attr.pointer || from_gsym))
2374 gfc_get_derived_type (to_cm->ts.u.derived);
2375 else if (from_cm->ts.type == BT_CLASS
2376 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2377 gfc_get_derived_type (to_cm->ts.u.derived);
2378 else if (from_cm->ts.type == BT_CHARACTER)
2379 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2382 return 1;
2386 /* Build a tree node for a procedure pointer component. */
2388 tree
2389 gfc_get_ppc_type (gfc_component* c)
2391 tree t;
2393 /* Explicit interface. */
2394 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2395 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2397 /* Implicit interface (only return value may be known). */
2398 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2399 t = gfc_typenode_for_spec (&c->ts);
2400 else
2401 t = void_type_node;
2403 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2407 /* Build a tree node for a union type. Requires building each map
2408 structure which is an element of the union. */
2410 tree
2411 gfc_get_union_type (gfc_symbol *un)
2413 gfc_component *map = NULL;
2414 tree typenode = NULL, map_type = NULL, map_field = NULL;
2415 tree *chain = NULL;
2417 if (un->backend_decl)
2419 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2420 return un->backend_decl;
2421 else
2422 typenode = un->backend_decl;
2424 else
2426 typenode = make_node (UNION_TYPE);
2427 TYPE_NAME (typenode) = get_identifier (un->name);
2430 /* Add each contained MAP as a field. */
2431 for (map = un->components; map; map = map->next)
2433 gcc_assert (map->ts.type == BT_DERIVED);
2435 /* The map's type node, which is defined within this union's context. */
2436 map_type = gfc_get_derived_type (map->ts.u.derived);
2437 TYPE_CONTEXT (map_type) = typenode;
2439 /* The map field's declaration. */
2440 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2441 map_type, &chain);
2442 if (map->loc.lb)
2443 gfc_set_decl_location (map_field, &map->loc);
2444 else if (un->declared_at.lb)
2445 gfc_set_decl_location (map_field, &un->declared_at);
2447 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2448 DECL_NAMELESS(map_field) = true;
2450 /* We should never clobber another backend declaration for this map,
2451 because each map component is unique. */
2452 if (!map->backend_decl)
2453 map->backend_decl = map_field;
2456 un->backend_decl = typenode;
2457 gfc_finish_type (typenode);
2459 return typenode;
2463 /* Build a tree node for a derived type. If there are equal
2464 derived types, with different local names, these are built
2465 at the same time. If an equal derived type has been built
2466 in a parent namespace, this is used. */
2468 tree
2469 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2471 tree typenode = NULL, field = NULL, field_type = NULL;
2472 tree canonical = NULL_TREE;
2473 tree *chain = NULL;
2474 bool got_canonical = false;
2475 bool unlimited_entity = false;
2476 gfc_component *c;
2477 gfc_dt_list *dt;
2478 gfc_namespace *ns;
2479 tree tmp;
2481 gcc_assert (!derived->attr.pdt_template);
2483 if (derived->attr.unlimited_polymorphic
2484 || (flag_coarray == GFC_FCOARRAY_LIB
2485 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2486 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2487 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
2488 return ptr_type_node;
2490 if (flag_coarray != GFC_FCOARRAY_LIB
2491 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2492 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2493 return gfc_get_int_type (gfc_default_integer_kind);
2495 if (derived && derived->attr.flavor == FL_PROCEDURE
2496 && derived->attr.generic)
2497 derived = gfc_find_dt_in_generic (derived);
2499 /* See if it's one of the iso_c_binding derived types. */
2500 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2502 if (derived->backend_decl)
2503 return derived->backend_decl;
2505 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2506 derived->backend_decl = ptr_type_node;
2507 else
2508 derived->backend_decl = pfunc_type_node;
2510 derived->ts.kind = gfc_index_integer_kind;
2511 derived->ts.type = BT_INTEGER;
2512 /* Set the f90_type to BT_VOID as a way to recognize something of type
2513 BT_INTEGER that needs to fit a void * for the purpose of the
2514 iso_c_binding derived types. */
2515 derived->ts.f90_type = BT_VOID;
2517 return derived->backend_decl;
2520 /* If use associated, use the module type for this one. */
2521 if (derived->backend_decl == NULL
2522 && derived->attr.use_assoc
2523 && derived->module
2524 && gfc_get_module_backend_decl (derived))
2525 goto copy_derived_types;
2527 /* The derived types from an earlier namespace can be used as the
2528 canonical type. */
2529 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2530 && gfc_global_ns_list)
2532 for (ns = gfc_global_ns_list;
2533 ns->translated && !got_canonical;
2534 ns = ns->sibling)
2536 dt = ns->derived_types;
2537 for (; dt && !canonical; dt = dt->next)
2539 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2540 if (derived->backend_decl)
2541 got_canonical = true;
2546 /* Store up the canonical type to be added to this one. */
2547 if (got_canonical)
2549 if (TYPE_CANONICAL (derived->backend_decl))
2550 canonical = TYPE_CANONICAL (derived->backend_decl);
2551 else
2552 canonical = derived->backend_decl;
2554 derived->backend_decl = NULL_TREE;
2557 /* derived->backend_decl != 0 means we saw it before, but its
2558 components' backend_decl may have not been built. */
2559 if (derived->backend_decl)
2561 /* Its components' backend_decl have been built or we are
2562 seeing recursion through the formal arglist of a procedure
2563 pointer component. */
2564 if (TYPE_FIELDS (derived->backend_decl))
2565 return derived->backend_decl;
2566 else if (derived->attr.abstract
2567 && derived->attr.proc_pointer_comp)
2569 /* If an abstract derived type with procedure pointer
2570 components has no other type of component, return the
2571 backend_decl. Otherwise build the components if any of the
2572 non-procedure pointer components have no backend_decl. */
2573 for (c = derived->components; c; c = c->next)
2575 bool same_alloc_type = c->attr.allocatable
2576 && derived == c->ts.u.derived;
2577 if (!c->attr.proc_pointer
2578 && !same_alloc_type
2579 && c->backend_decl == NULL)
2580 break;
2581 else if (c->next == NULL)
2582 return derived->backend_decl;
2584 typenode = derived->backend_decl;
2586 else
2587 typenode = derived->backend_decl;
2589 else
2591 /* We see this derived type first time, so build the type node. */
2592 typenode = make_node (RECORD_TYPE);
2593 TYPE_NAME (typenode) = get_identifier (derived->name);
2594 TYPE_PACKED (typenode) = flag_pack_derived;
2595 derived->backend_decl = typenode;
2598 if (derived->components
2599 && derived->components->ts.type == BT_DERIVED
2600 && strcmp (derived->components->name, "_data") == 0
2601 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2602 unlimited_entity = true;
2604 /* Go through the derived type components, building them as
2605 necessary. The reason for doing this now is that it is
2606 possible to recurse back to this derived type through a
2607 pointer component (PR24092). If this happens, the fields
2608 will be built and so we can return the type. */
2609 for (c = derived->components; c; c = c->next)
2611 bool same_alloc_type = c->attr.allocatable
2612 && derived == c->ts.u.derived;
2614 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2615 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2617 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2618 continue;
2620 if ((!c->attr.pointer && !c->attr.proc_pointer
2621 && !same_alloc_type)
2622 || c->ts.u.derived->backend_decl == NULL)
2624 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2625 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2626 local_codim);
2629 if (c->ts.u.derived->attr.is_iso_c)
2631 /* Need to copy the modified ts from the derived type. The
2632 typespec was modified because C_PTR/C_FUNPTR are translated
2633 into (void *) from derived types. */
2634 c->ts.type = c->ts.u.derived->ts.type;
2635 c->ts.kind = c->ts.u.derived->ts.kind;
2636 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2637 if (c->initializer)
2639 c->initializer->ts.type = c->ts.type;
2640 c->initializer->ts.kind = c->ts.kind;
2641 c->initializer->ts.f90_type = c->ts.f90_type;
2642 c->initializer->expr_type = EXPR_NULL;
2647 if (TYPE_FIELDS (derived->backend_decl))
2648 return derived->backend_decl;
2650 /* Build the type member list. Install the newly created RECORD_TYPE
2651 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2652 through only the top-level linked list of components so we correctly
2653 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2654 types are built as part of gfc_get_union_type. */
2655 for (c = derived->components; c; c = c->next)
2657 bool same_alloc_type = c->attr.allocatable
2658 && derived == c->ts.u.derived;
2659 /* Prevent infinite recursion, when the procedure pointer type is
2660 the same as derived, by forcing the procedure pointer component to
2661 be built as if the explicit interface does not exist. */
2662 if (c->attr.proc_pointer
2663 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2664 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2665 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2666 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2667 field_type = gfc_get_ppc_type (c);
2668 else if (c->attr.proc_pointer && derived->backend_decl)
2670 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2671 field_type = build_pointer_type (tmp);
2673 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2674 field_type = c->ts.u.derived->backend_decl;
2675 else
2677 if (c->ts.type == BT_CHARACTER
2678 && !c->ts.deferred && !c->attr.pdt_string)
2680 /* Evaluate the string length. */
2681 gfc_conv_const_charlen (c->ts.u.cl);
2682 gcc_assert (c->ts.u.cl->backend_decl);
2684 else if (c->ts.type == BT_CHARACTER)
2685 c->ts.u.cl->backend_decl
2686 = build_int_cst (gfc_charlen_type_node, 0);
2688 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2691 /* This returns an array descriptor type. Initialization may be
2692 required. */
2693 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2695 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2697 enum gfc_array_kind akind;
2698 if (c->attr.pointer)
2699 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2700 : GFC_ARRAY_POINTER;
2701 else
2702 akind = GFC_ARRAY_ALLOCATABLE;
2703 /* Pointers to arrays aren't actually pointer types. The
2704 descriptors are separate, but the data is common. */
2705 field_type = gfc_build_array_type (field_type, c->as, akind,
2706 !c->attr.target
2707 && !c->attr.pointer,
2708 c->attr.contiguous,
2709 codimen);
2711 else
2712 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2713 PACKED_STATIC,
2714 !c->attr.target);
2716 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2717 && !c->attr.proc_pointer
2718 && !(unlimited_entity && c == derived->components))
2719 field_type = build_pointer_type (field_type);
2721 if (c->attr.pointer || same_alloc_type)
2722 field_type = gfc_nonrestricted_type (field_type);
2724 /* vtype fields can point to different types to the base type. */
2725 if (c->ts.type == BT_DERIVED
2726 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2727 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2728 ptr_mode, true);
2730 /* Ensure that the CLASS language specific flag is set. */
2731 if (c->ts.type == BT_CLASS)
2733 if (POINTER_TYPE_P (field_type))
2734 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2735 else
2736 GFC_CLASS_TYPE_P (field_type) = 1;
2739 field = gfc_add_field_to_struct (typenode,
2740 get_identifier (c->name),
2741 field_type, &chain);
2742 if (c->loc.lb)
2743 gfc_set_decl_location (field, &c->loc);
2744 else if (derived->declared_at.lb)
2745 gfc_set_decl_location (field, &derived->declared_at);
2747 gfc_finish_decl_attrs (field, &c->attr);
2749 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2751 gcc_assert (field);
2752 if (!c->backend_decl)
2753 c->backend_decl = field;
2755 if (c->attr.pointer && c->attr.dimension
2756 && !(c->ts.type == BT_DERIVED
2757 && strcmp (c->name, "_data") == 0))
2758 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2760 /* Do not add a caf_token field for classes' data components. */
2761 if (codimen && !c->attr.dimension && !c->attr.codimension
2762 && (c->attr.allocatable || c->attr.pointer)
2763 && c->caf_token == NULL_TREE && strcmp ("_data", c->name) != 0)
2765 char caf_name[GFC_MAX_SYMBOL_LEN];
2766 snprintf (caf_name, GFC_MAX_SYMBOL_LEN, "_caf_%s", c->name);
2767 c->caf_token = gfc_add_field_to_struct (typenode,
2768 get_identifier (caf_name),
2769 pvoid_type_node, &chain);
2770 TREE_NO_WARNING (c->caf_token) = 1;
2774 /* Now lay out the derived type, including the fields. */
2775 if (canonical)
2776 TYPE_CANONICAL (typenode) = canonical;
2778 gfc_finish_type (typenode);
2779 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2780 if (derived->module && derived->ns->proc_name
2781 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2783 if (derived->ns->proc_name->backend_decl
2784 && TREE_CODE (derived->ns->proc_name->backend_decl)
2785 == NAMESPACE_DECL)
2787 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2788 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2789 = derived->ns->proc_name->backend_decl;
2793 derived->backend_decl = typenode;
2795 copy_derived_types:
2797 for (dt = gfc_derived_types; dt; dt = dt->next)
2798 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2800 return derived->backend_decl;
2805 gfc_return_by_reference (gfc_symbol * sym)
2807 if (!sym->attr.function)
2808 return 0;
2810 if (sym->attr.dimension)
2811 return 1;
2813 if (sym->ts.type == BT_CHARACTER
2814 && !sym->attr.is_bind_c
2815 && (!sym->attr.result
2816 || !sym->ns->proc_name
2817 || !sym->ns->proc_name->attr.is_bind_c))
2818 return 1;
2820 /* Possibly return complex numbers by reference for g77 compatibility.
2821 We don't do this for calls to intrinsics (as the library uses the
2822 -fno-f2c calling convention), nor for calls to functions which always
2823 require an explicit interface, as no compatibility problems can
2824 arise there. */
2825 if (flag_f2c && sym->ts.type == BT_COMPLEX
2826 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2827 return 1;
2829 return 0;
2832 static tree
2833 gfc_get_mixed_entry_union (gfc_namespace *ns)
2835 tree type;
2836 tree *chain = NULL;
2837 char name[GFC_MAX_SYMBOL_LEN + 1];
2838 gfc_entry_list *el, *el2;
2840 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2841 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2843 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2845 /* Build the type node. */
2846 type = make_node (UNION_TYPE);
2848 TYPE_NAME (type) = get_identifier (name);
2850 for (el = ns->entries; el; el = el->next)
2852 /* Search for duplicates. */
2853 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2854 if (el2->sym->result == el->sym->result)
2855 break;
2857 if (el == el2)
2858 gfc_add_field_to_struct_1 (type,
2859 get_identifier (el->sym->result->name),
2860 gfc_sym_type (el->sym->result), &chain);
2863 /* Finish off the type. */
2864 gfc_finish_type (type);
2865 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2866 return type;
2869 /* Create a "fn spec" based on the formal arguments;
2870 cf. create_function_arglist. */
2872 static tree
2873 create_fn_spec (gfc_symbol *sym, tree fntype)
2875 char spec[150];
2876 size_t spec_len;
2877 gfc_formal_arglist *f;
2878 tree tmp;
2880 memset (&spec, 0, sizeof (spec));
2881 spec[0] = '.';
2882 spec_len = 1;
2884 if (sym->attr.entry_master)
2885 spec[spec_len++] = 'R';
2886 if (gfc_return_by_reference (sym))
2888 gfc_symbol *result = sym->result ? sym->result : sym;
2890 if (result->attr.pointer || sym->attr.proc_pointer)
2891 spec[spec_len++] = '.';
2892 else
2893 spec[spec_len++] = 'w';
2894 if (sym->ts.type == BT_CHARACTER)
2895 spec[spec_len++] = 'R';
2898 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2899 if (spec_len < sizeof (spec))
2901 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2902 || f->sym->attr.external || f->sym->attr.cray_pointer
2903 || (f->sym->ts.type == BT_DERIVED
2904 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2905 || f->sym->ts.u.derived->attr.pointer_comp))
2906 || (f->sym->ts.type == BT_CLASS
2907 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2908 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2909 spec[spec_len++] = '.';
2910 else if (f->sym->attr.intent == INTENT_IN)
2911 spec[spec_len++] = 'r';
2912 else if (f->sym)
2913 spec[spec_len++] = 'w';
2916 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2917 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2918 return build_type_attribute_variant (fntype, tmp);
2922 tree
2923 gfc_get_function_type (gfc_symbol * sym)
2925 tree type;
2926 vec<tree, va_gc> *typelist = NULL;
2927 gfc_formal_arglist *f;
2928 gfc_symbol *arg;
2929 int alternate_return = 0;
2930 bool is_varargs = true;
2932 /* Make sure this symbol is a function, a subroutine or the main
2933 program. */
2934 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2935 || sym->attr.flavor == FL_PROGRAM);
2937 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2938 so that they can be detected here and handled further down. */
2939 if (sym->backend_decl == NULL)
2940 sym->backend_decl = error_mark_node;
2941 else if (sym->backend_decl == error_mark_node)
2942 goto arg_type_list_done;
2943 else if (sym->attr.proc_pointer)
2944 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2945 else
2946 return TREE_TYPE (sym->backend_decl);
2948 if (sym->attr.entry_master)
2949 /* Additional parameter for selecting an entry point. */
2950 vec_safe_push (typelist, gfc_array_index_type);
2952 if (sym->result)
2953 arg = sym->result;
2954 else
2955 arg = sym;
2957 if (arg->ts.type == BT_CHARACTER)
2958 gfc_conv_const_charlen (arg->ts.u.cl);
2960 /* Some functions we use an extra parameter for the return value. */
2961 if (gfc_return_by_reference (sym))
2963 type = gfc_sym_type (arg);
2964 if (arg->ts.type == BT_COMPLEX
2965 || arg->attr.dimension
2966 || arg->ts.type == BT_CHARACTER)
2967 type = build_reference_type (type);
2969 vec_safe_push (typelist, type);
2970 if (arg->ts.type == BT_CHARACTER)
2972 if (!arg->ts.deferred)
2973 /* Transfer by value. */
2974 vec_safe_push (typelist, gfc_charlen_type_node);
2975 else
2976 /* Deferred character lengths are transferred by reference
2977 so that the value can be returned. */
2978 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2982 /* Build the argument types for the function. */
2983 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2985 arg = f->sym;
2986 if (arg)
2988 /* Evaluate constant character lengths here so that they can be
2989 included in the type. */
2990 if (arg->ts.type == BT_CHARACTER)
2991 gfc_conv_const_charlen (arg->ts.u.cl);
2993 if (arg->attr.flavor == FL_PROCEDURE)
2995 type = gfc_get_function_type (arg);
2996 type = build_pointer_type (type);
2998 else
2999 type = gfc_sym_type (arg);
3001 /* Parameter Passing Convention
3003 We currently pass all parameters by reference.
3004 Parameters with INTENT(IN) could be passed by value.
3005 The problem arises if a function is called via an implicit
3006 prototype. In this situation the INTENT is not known.
3007 For this reason all parameters to global functions must be
3008 passed by reference. Passing by value would potentially
3009 generate bad code. Worse there would be no way of telling that
3010 this code was bad, except that it would give incorrect results.
3012 Contained procedures could pass by value as these are never
3013 used without an explicit interface, and cannot be passed as
3014 actual parameters for a dummy procedure. */
3016 vec_safe_push (typelist, type);
3018 else
3020 if (sym->attr.subroutine)
3021 alternate_return = 1;
3025 /* Add hidden string length parameters. */
3026 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3028 arg = f->sym;
3029 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3031 if (!arg->ts.deferred)
3032 /* Transfer by value. */
3033 type = gfc_charlen_type_node;
3034 else
3035 /* Deferred character lengths are transferred by reference
3036 so that the value can be returned. */
3037 type = build_pointer_type (gfc_charlen_type_node);
3039 vec_safe_push (typelist, type);
3043 if (!vec_safe_is_empty (typelist)
3044 || sym->attr.is_main_program
3045 || sym->attr.if_source != IFSRC_UNKNOWN)
3046 is_varargs = false;
3048 if (sym->backend_decl == error_mark_node)
3049 sym->backend_decl = NULL_TREE;
3051 arg_type_list_done:
3053 if (alternate_return)
3054 type = integer_type_node;
3055 else if (!sym->attr.function || gfc_return_by_reference (sym))
3056 type = void_type_node;
3057 else if (sym->attr.mixed_entry_master)
3058 type = gfc_get_mixed_entry_union (sym->ns);
3059 else if (flag_f2c && sym->ts.type == BT_REAL
3060 && sym->ts.kind == gfc_default_real_kind
3061 && !sym->attr.always_explicit)
3063 /* Special case: f2c calling conventions require that (scalar)
3064 default REAL functions return the C type double instead. f2c
3065 compatibility is only an issue with functions that don't
3066 require an explicit interface, as only these could be
3067 implemented in Fortran 77. */
3068 sym->ts.kind = gfc_default_double_kind;
3069 type = gfc_typenode_for_spec (&sym->ts);
3070 sym->ts.kind = gfc_default_real_kind;
3072 else if (sym->result && sym->result->attr.proc_pointer)
3073 /* Procedure pointer return values. */
3075 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3077 /* Unset proc_pointer as gfc_get_function_type
3078 is called recursively. */
3079 sym->result->attr.proc_pointer = 0;
3080 type = build_pointer_type (gfc_get_function_type (sym->result));
3081 sym->result->attr.proc_pointer = 1;
3083 else
3084 type = gfc_sym_type (sym->result);
3086 else
3087 type = gfc_sym_type (sym);
3089 if (is_varargs)
3090 type = build_varargs_function_type_vec (type, typelist);
3091 else
3092 type = build_function_type_vec (type, typelist);
3093 type = create_fn_spec (sym, type);
3095 return type;
3098 /* Language hooks for middle-end access to type nodes. */
3100 /* Return an integer type with BITS bits of precision,
3101 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3103 tree
3104 gfc_type_for_size (unsigned bits, int unsignedp)
3106 if (!unsignedp)
3108 int i;
3109 for (i = 0; i <= MAX_INT_KINDS; ++i)
3111 tree type = gfc_integer_types[i];
3112 if (type && bits == TYPE_PRECISION (type))
3113 return type;
3116 /* Handle TImode as a special case because it is used by some backends
3117 (e.g. ARM) even though it is not available for normal use. */
3118 #if HOST_BITS_PER_WIDE_INT >= 64
3119 if (bits == TYPE_PRECISION (intTI_type_node))
3120 return intTI_type_node;
3121 #endif
3123 if (bits <= TYPE_PRECISION (intQI_type_node))
3124 return intQI_type_node;
3125 if (bits <= TYPE_PRECISION (intHI_type_node))
3126 return intHI_type_node;
3127 if (bits <= TYPE_PRECISION (intSI_type_node))
3128 return intSI_type_node;
3129 if (bits <= TYPE_PRECISION (intDI_type_node))
3130 return intDI_type_node;
3131 if (bits <= TYPE_PRECISION (intTI_type_node))
3132 return intTI_type_node;
3134 else
3136 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3137 return unsigned_intQI_type_node;
3138 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3139 return unsigned_intHI_type_node;
3140 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3141 return unsigned_intSI_type_node;
3142 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3143 return unsigned_intDI_type_node;
3144 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3145 return unsigned_intTI_type_node;
3148 return NULL_TREE;
3151 /* Return a data type that has machine mode MODE. If the mode is an
3152 integer, then UNSIGNEDP selects between signed and unsigned types. */
3154 tree
3155 gfc_type_for_mode (machine_mode mode, int unsignedp)
3157 int i;
3158 tree *base;
3159 scalar_int_mode int_mode;
3161 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3162 base = gfc_real_types;
3163 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3164 base = gfc_complex_types;
3165 else if (is_a <scalar_int_mode> (mode, &int_mode))
3167 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3168 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3170 else if (VECTOR_MODE_P (mode))
3172 machine_mode inner_mode = GET_MODE_INNER (mode);
3173 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3174 if (inner_type != NULL_TREE)
3175 return build_vector_type_for_mode (inner_type, mode);
3176 return NULL_TREE;
3178 else
3179 return NULL_TREE;
3181 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3183 tree type = base[i];
3184 if (type && mode == TYPE_MODE (type))
3185 return type;
3188 return NULL_TREE;
3191 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3192 in that case. */
3194 bool
3195 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3197 int rank, dim;
3198 bool indirect = false;
3199 tree etype, ptype, t, base_decl;
3200 tree data_off, dim_off, dtype_off, dim_size, elem_size;
3201 tree lower_suboff, upper_suboff, stride_suboff;
3203 if (! GFC_DESCRIPTOR_TYPE_P (type))
3205 if (! POINTER_TYPE_P (type))
3206 return false;
3207 type = TREE_TYPE (type);
3208 if (! GFC_DESCRIPTOR_TYPE_P (type))
3209 return false;
3210 indirect = true;
3213 rank = GFC_TYPE_ARRAY_RANK (type);
3214 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3215 return false;
3217 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3218 gcc_assert (POINTER_TYPE_P (etype));
3219 etype = TREE_TYPE (etype);
3221 /* If the type is not a scalar coarray. */
3222 if (TREE_CODE (etype) == ARRAY_TYPE)
3223 etype = TREE_TYPE (etype);
3225 /* Can't handle variable sized elements yet. */
3226 if (int_size_in_bytes (etype) <= 0)
3227 return false;
3228 /* Nor non-constant lower bounds in assumed shape arrays. */
3229 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3230 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3232 for (dim = 0; dim < rank; dim++)
3233 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3234 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3235 return false;
3238 memset (info, '\0', sizeof (*info));
3239 info->ndimensions = rank;
3240 info->ordering = array_descr_ordering_column_major;
3241 info->element_type = etype;
3242 ptype = build_pointer_type (gfc_array_index_type);
3243 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3244 if (!base_decl)
3246 base_decl = make_node (DEBUG_EXPR_DECL);
3247 DECL_ARTIFICIAL (base_decl) = 1;
3248 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3249 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3250 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3252 info->base_decl = base_decl;
3253 if (indirect)
3254 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3256 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3258 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &dim_off,
3259 &dim_size, &stride_suboff,
3260 &lower_suboff, &upper_suboff);
3262 t = base_decl;
3263 if (!integer_zerop (data_off))
3264 t = fold_build_pointer_plus (t, data_off);
3265 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3266 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3267 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3268 info->allocated = build2 (NE_EXPR, logical_type_node,
3269 info->data_location, null_pointer_node);
3270 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3271 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3272 info->associated = build2 (NE_EXPR, logical_type_node,
3273 info->data_location, null_pointer_node);
3274 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3275 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3276 && dwarf_version >= 5)
3278 rank = 1;
3279 info->ndimensions = 1;
3280 t = base_decl;
3281 if (!integer_zerop (dtype_off))
3282 t = fold_build_pointer_plus (t, dtype_off);
3283 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3284 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3285 info->rank = build2 (BIT_AND_EXPR, gfc_array_index_type, t,
3286 build_int_cst (gfc_array_index_type,
3287 GFC_DTYPE_RANK_MASK));
3288 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3289 t = size_binop (MULT_EXPR, t, dim_size);
3290 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3293 for (dim = 0; dim < rank; dim++)
3295 t = fold_build_pointer_plus (base_decl,
3296 size_binop (PLUS_EXPR,
3297 dim_off, lower_suboff));
3298 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3299 info->dimen[dim].lower_bound = t;
3300 t = fold_build_pointer_plus (base_decl,
3301 size_binop (PLUS_EXPR,
3302 dim_off, upper_suboff));
3303 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3304 info->dimen[dim].upper_bound = t;
3305 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3306 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3308 /* Assumed shape arrays have known lower bounds. */
3309 info->dimen[dim].upper_bound
3310 = build2 (MINUS_EXPR, gfc_array_index_type,
3311 info->dimen[dim].upper_bound,
3312 info->dimen[dim].lower_bound);
3313 info->dimen[dim].lower_bound
3314 = fold_convert (gfc_array_index_type,
3315 GFC_TYPE_ARRAY_LBOUND (type, dim));
3316 info->dimen[dim].upper_bound
3317 = build2 (PLUS_EXPR, gfc_array_index_type,
3318 info->dimen[dim].lower_bound,
3319 info->dimen[dim].upper_bound);
3321 t = fold_build_pointer_plus (base_decl,
3322 size_binop (PLUS_EXPR,
3323 dim_off, stride_suboff));
3324 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3325 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3326 info->dimen[dim].stride = t;
3327 if (dim + 1 < rank)
3328 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3331 return true;
3335 /* Create a type to handle vector subscripts for coarray library calls. It
3336 has the form:
3337 struct caf_vector_t {
3338 size_t nvec; // size of the vector
3339 union {
3340 struct {
3341 void *vector;
3342 int kind;
3343 } v;
3344 struct {
3345 ptrdiff_t lower_bound;
3346 ptrdiff_t upper_bound;
3347 ptrdiff_t stride;
3348 } triplet;
3349 } u;
3351 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3352 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3354 tree
3355 gfc_get_caf_vector_type (int dim)
3357 static tree vector_types[GFC_MAX_DIMENSIONS];
3358 static tree vec_type = NULL_TREE;
3359 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3361 if (vector_types[dim-1] != NULL_TREE)
3362 return vector_types[dim-1];
3364 if (vec_type == NULL_TREE)
3366 chain = 0;
3367 vect_struct_type = make_node (RECORD_TYPE);
3368 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3369 get_identifier ("vector"),
3370 pvoid_type_node, &chain);
3371 TREE_NO_WARNING (tmp) = 1;
3372 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3373 get_identifier ("kind"),
3374 integer_type_node, &chain);
3375 TREE_NO_WARNING (tmp) = 1;
3376 gfc_finish_type (vect_struct_type);
3378 chain = 0;
3379 triplet_struct_type = make_node (RECORD_TYPE);
3380 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3381 get_identifier ("lower_bound"),
3382 gfc_array_index_type, &chain);
3383 TREE_NO_WARNING (tmp) = 1;
3384 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3385 get_identifier ("upper_bound"),
3386 gfc_array_index_type, &chain);
3387 TREE_NO_WARNING (tmp) = 1;
3388 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3389 gfc_array_index_type, &chain);
3390 TREE_NO_WARNING (tmp) = 1;
3391 gfc_finish_type (triplet_struct_type);
3393 chain = 0;
3394 union_type = make_node (UNION_TYPE);
3395 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3396 vect_struct_type, &chain);
3397 TREE_NO_WARNING (tmp) = 1;
3398 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3399 triplet_struct_type, &chain);
3400 TREE_NO_WARNING (tmp) = 1;
3401 gfc_finish_type (union_type);
3403 chain = 0;
3404 vec_type = make_node (RECORD_TYPE);
3405 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3406 size_type_node, &chain);
3407 TREE_NO_WARNING (tmp) = 1;
3408 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3409 union_type, &chain);
3410 TREE_NO_WARNING (tmp) = 1;
3411 gfc_finish_type (vec_type);
3412 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3415 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3416 gfc_rank_cst[dim-1]);
3417 vector_types[dim-1] = build_array_type (vec_type, tmp);
3418 return vector_types[dim-1];
3422 tree
3423 gfc_get_caf_reference_type ()
3425 static tree reference_type = NULL_TREE;
3426 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3427 a_struct_type, u_union_type, tmp, *chain;
3429 if (reference_type != NULL_TREE)
3430 return reference_type;
3432 chain = 0;
3433 c_struct_type = make_node (RECORD_TYPE);
3434 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3435 get_identifier ("offset"),
3436 gfc_array_index_type, &chain);
3437 TREE_NO_WARNING (tmp) = 1;
3438 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3439 get_identifier ("caf_token_offset"),
3440 gfc_array_index_type, &chain);
3441 TREE_NO_WARNING (tmp) = 1;
3442 gfc_finish_type (c_struct_type);
3444 chain = 0;
3445 s_struct_type = make_node (RECORD_TYPE);
3446 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3447 get_identifier ("start"),
3448 gfc_array_index_type, &chain);
3449 TREE_NO_WARNING (tmp) = 1;
3450 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3451 get_identifier ("end"),
3452 gfc_array_index_type, &chain);
3453 TREE_NO_WARNING (tmp) = 1;
3454 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3455 get_identifier ("stride"),
3456 gfc_array_index_type, &chain);
3457 TREE_NO_WARNING (tmp) = 1;
3458 gfc_finish_type (s_struct_type);
3460 chain = 0;
3461 v_struct_type = make_node (RECORD_TYPE);
3462 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3463 get_identifier ("vector"),
3464 pvoid_type_node, &chain);
3465 TREE_NO_WARNING (tmp) = 1;
3466 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3467 get_identifier ("nvec"),
3468 size_type_node, &chain);
3469 TREE_NO_WARNING (tmp) = 1;
3470 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3471 get_identifier ("kind"),
3472 integer_type_node, &chain);
3473 TREE_NO_WARNING (tmp) = 1;
3474 gfc_finish_type (v_struct_type);
3476 chain = 0;
3477 union_type = make_node (UNION_TYPE);
3478 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3479 s_struct_type, &chain);
3480 TREE_NO_WARNING (tmp) = 1;
3481 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3482 v_struct_type, &chain);
3483 TREE_NO_WARNING (tmp) = 1;
3484 gfc_finish_type (union_type);
3486 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3487 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3488 dim_union_type = build_array_type (union_type, tmp);
3490 chain = 0;
3491 a_struct_type = make_node (RECORD_TYPE);
3492 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3493 build_array_type (unsigned_char_type_node,
3494 build_range_type (gfc_array_index_type,
3495 gfc_index_zero_node,
3496 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3497 &chain);
3498 TREE_NO_WARNING (tmp) = 1;
3499 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3500 get_identifier ("static_array_type"),
3501 integer_type_node, &chain);
3502 TREE_NO_WARNING (tmp) = 1;
3503 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3504 dim_union_type, &chain);
3505 TREE_NO_WARNING (tmp) = 1;
3506 gfc_finish_type (a_struct_type);
3508 chain = 0;
3509 u_union_type = make_node (UNION_TYPE);
3510 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3511 c_struct_type, &chain);
3512 TREE_NO_WARNING (tmp) = 1;
3513 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3514 a_struct_type, &chain);
3515 TREE_NO_WARNING (tmp) = 1;
3516 gfc_finish_type (u_union_type);
3518 chain = 0;
3519 reference_type = make_node (RECORD_TYPE);
3520 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3521 build_pointer_type (reference_type), &chain);
3522 TREE_NO_WARNING (tmp) = 1;
3523 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3524 integer_type_node, &chain);
3525 TREE_NO_WARNING (tmp) = 1;
3526 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3527 size_type_node, &chain);
3528 TREE_NO_WARNING (tmp) = 1;
3529 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3530 u_union_type, &chain);
3531 TREE_NO_WARNING (tmp) = 1;
3532 gfc_finish_type (reference_type);
3533 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3535 return reference_type;
3538 #include "gt-fortran-trans-types.h"