* i386.c (ix86_expand_set_or_movmem): Disable 512bit loops for targets
[official-gcc.git] / gcc / fortran / trans-types.c
blob78477a90f80c3d73104a17d240ed3fe5483eabdb
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 gfc_charlen_type_node;
67 tree gfc_float128_type_node = NULL_TREE;
68 tree gfc_complex_float128_type_node = NULL_TREE;
70 bool gfc_real16_is_float128 = false;
72 static GTY(()) tree gfc_desc_dim_type;
73 static GTY(()) tree gfc_max_array_element_size;
74 static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
75 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
77 /* Arrays for all integral and real kinds. We'll fill this in at runtime
78 after the target has a chance to process command-line options. */
80 #define MAX_INT_KINDS 5
81 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
82 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
83 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
84 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
86 #define MAX_REAL_KINDS 5
87 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
88 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
89 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
91 #define MAX_CHARACTER_KINDS 2
92 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
93 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
94 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
96 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
98 /* The integer kind to use for array indices. This will be set to the
99 proper value based on target information from the backend. */
101 int gfc_index_integer_kind;
103 /* The default kinds of the various types. */
105 int gfc_default_integer_kind;
106 int gfc_max_integer_kind;
107 int gfc_default_real_kind;
108 int gfc_default_double_kind;
109 int gfc_default_character_kind;
110 int gfc_default_logical_kind;
111 int gfc_default_complex_kind;
112 int gfc_c_int_kind;
113 int gfc_atomic_int_kind;
114 int gfc_atomic_logical_kind;
116 /* The kind size used for record offsets. If the target system supports
117 kind=8, this will be set to 8, otherwise it is set to 4. */
118 int gfc_intio_kind;
120 /* The integer kind used to store character lengths. */
121 int gfc_charlen_int_kind;
123 /* The size of the numeric storage unit and character storage unit. */
124 int gfc_numeric_storage_size;
125 int gfc_character_storage_size;
128 bool
129 gfc_check_any_c_kind (gfc_typespec *ts)
131 int i;
133 for (i = 0; i < ISOCBINDING_NUMBER; i++)
135 /* Check for any C interoperable kind for the given type/kind in ts.
136 This can be used after verify_c_interop to make sure that the
137 Fortran kind being used exists in at least some form for C. */
138 if (c_interop_kinds_table[i].f90_type == ts->type &&
139 c_interop_kinds_table[i].value == ts->kind)
140 return true;
143 return false;
147 static int
148 get_real_kind_from_node (tree type)
150 int i;
152 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
153 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
154 return gfc_real_kinds[i].kind;
156 return -4;
159 static int
160 get_int_kind_from_node (tree type)
162 int i;
164 if (!type)
165 return -2;
167 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
168 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
169 return gfc_integer_kinds[i].kind;
171 return -1;
174 /* Return a typenode for the "standard" C type with a given name. */
175 static tree
176 get_typenode_from_name (const char *name)
178 if (name == NULL || *name == '\0')
179 return NULL_TREE;
181 if (strcmp (name, "char") == 0)
182 return char_type_node;
183 if (strcmp (name, "unsigned char") == 0)
184 return unsigned_char_type_node;
185 if (strcmp (name, "signed char") == 0)
186 return signed_char_type_node;
188 if (strcmp (name, "short int") == 0)
189 return short_integer_type_node;
190 if (strcmp (name, "short unsigned int") == 0)
191 return short_unsigned_type_node;
193 if (strcmp (name, "int") == 0)
194 return integer_type_node;
195 if (strcmp (name, "unsigned int") == 0)
196 return unsigned_type_node;
198 if (strcmp (name, "long int") == 0)
199 return long_integer_type_node;
200 if (strcmp (name, "long unsigned int") == 0)
201 return long_unsigned_type_node;
203 if (strcmp (name, "long long int") == 0)
204 return long_long_integer_type_node;
205 if (strcmp (name, "long long unsigned int") == 0)
206 return long_long_unsigned_type_node;
208 gcc_unreachable ();
211 static int
212 get_int_kind_from_name (const char *name)
214 return get_int_kind_from_node (get_typenode_from_name (name));
218 /* Get the kind number corresponding to an integer of given size,
219 following the required return values for ISO_FORTRAN_ENV INT* constants:
220 -2 is returned if we support a kind of larger size, -1 otherwise. */
222 gfc_get_int_kind_from_width_isofortranenv (int size)
224 int i;
226 /* Look for a kind with matching storage size. */
227 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
228 if (gfc_integer_kinds[i].bit_size == size)
229 return gfc_integer_kinds[i].kind;
231 /* Look for a kind with larger storage size. */
232 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
233 if (gfc_integer_kinds[i].bit_size > size)
234 return -2;
236 return -1;
240 /* Get the kind number corresponding to a real of a given storage size.
241 If two real's have the same storage size, then choose the real with
242 the largest precision. If a kind type is unavailable and a real
243 exists with wider storage, then return -2; otherwise, return -1. */
246 gfc_get_real_kind_from_width_isofortranenv (int size)
248 int digits, i, kind;
250 size /= 8;
252 kind = -1;
253 digits = 0;
255 /* Look for a kind with matching storage size. */
256 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
257 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
259 if (gfc_real_kinds[i].digits > digits)
261 digits = gfc_real_kinds[i].digits;
262 kind = gfc_real_kinds[i].kind;
266 if (kind != -1)
267 return kind;
269 /* Look for a kind with larger storage size. */
270 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
271 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
272 kind = -2;
274 return kind;
279 static int
280 get_int_kind_from_width (int size)
282 int i;
284 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
285 if (gfc_integer_kinds[i].bit_size == size)
286 return gfc_integer_kinds[i].kind;
288 return -2;
291 static int
292 get_int_kind_from_minimal_width (int size)
294 int i;
296 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
297 if (gfc_integer_kinds[i].bit_size >= size)
298 return gfc_integer_kinds[i].kind;
300 return -2;
304 /* Generate the CInteropKind_t objects for the C interoperable
305 kinds. */
307 void
308 gfc_init_c_interop_kinds (void)
310 int i;
312 /* init all pointers in the list to NULL */
313 for (i = 0; i < ISOCBINDING_NUMBER; i++)
315 /* Initialize the name and value fields. */
316 c_interop_kinds_table[i].name[0] = '\0';
317 c_interop_kinds_table[i].value = -100;
318 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
321 #define NAMED_INTCST(a,b,c,d) \
322 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
323 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
324 c_interop_kinds_table[a].value = c;
325 #define NAMED_REALCST(a,b,c,d) \
326 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
327 c_interop_kinds_table[a].f90_type = BT_REAL; \
328 c_interop_kinds_table[a].value = c;
329 #define NAMED_CMPXCST(a,b,c,d) \
330 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
331 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
332 c_interop_kinds_table[a].value = c;
333 #define NAMED_LOGCST(a,b,c) \
334 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
335 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
336 c_interop_kinds_table[a].value = c;
337 #define NAMED_CHARKNDCST(a,b,c) \
338 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
339 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
340 c_interop_kinds_table[a].value = c;
341 #define NAMED_CHARCST(a,b,c) \
342 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
343 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
344 c_interop_kinds_table[a].value = c;
345 #define DERIVED_TYPE(a,b,c) \
346 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
347 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
348 c_interop_kinds_table[a].value = c;
349 #define NAMED_FUNCTION(a,b,c,d) \
350 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
351 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
352 c_interop_kinds_table[a].value = c;
353 #define NAMED_SUBROUTINE(a,b,c,d) \
354 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
355 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
356 c_interop_kinds_table[a].value = c;
357 #include "iso-c-binding.def"
361 /* Query the target to determine which machine modes are available for
362 computation. Choose KIND numbers for them. */
364 void
365 gfc_init_kinds (void)
367 opt_scalar_int_mode int_mode_iter;
368 opt_scalar_float_mode float_mode_iter;
369 int i_index, r_index, kind;
370 bool saw_i4 = false, saw_i8 = false;
371 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
373 i_index = 0;
374 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
376 scalar_int_mode mode = int_mode_iter.require ();
377 int kind, bitsize;
379 if (!targetm.scalar_mode_supported_p (mode))
380 continue;
382 /* The middle end doesn't support constants larger than 2*HWI.
383 Perhaps the target hook shouldn't have accepted these either,
384 but just to be safe... */
385 bitsize = GET_MODE_BITSIZE (mode);
386 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
387 continue;
389 gcc_assert (i_index != MAX_INT_KINDS);
391 /* Let the kind equal the bit size divided by 8. This insulates the
392 programmer from the underlying byte size. */
393 kind = bitsize / 8;
395 if (kind == 4)
396 saw_i4 = true;
397 if (kind == 8)
398 saw_i8 = true;
400 gfc_integer_kinds[i_index].kind = kind;
401 gfc_integer_kinds[i_index].radix = 2;
402 gfc_integer_kinds[i_index].digits = bitsize - 1;
403 gfc_integer_kinds[i_index].bit_size = bitsize;
405 gfc_logical_kinds[i_index].kind = kind;
406 gfc_logical_kinds[i_index].bit_size = bitsize;
408 i_index += 1;
411 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
412 used for large file access. */
414 if (saw_i8)
415 gfc_intio_kind = 8;
416 else
417 gfc_intio_kind = 4;
419 /* If we do not at least have kind = 4, everything is pointless. */
420 gcc_assert(saw_i4);
422 /* Set the maximum integer kind. Used with at least BOZ constants. */
423 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
425 r_index = 0;
426 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
428 scalar_float_mode mode = float_mode_iter.require ();
429 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
430 int kind;
432 if (fmt == NULL)
433 continue;
434 if (!targetm.scalar_mode_supported_p (mode))
435 continue;
437 /* Only let float, double, long double and __float128 go through.
438 Runtime support for others is not provided, so they would be
439 useless. */
440 if (!targetm.libgcc_floating_mode_supported_p (mode))
441 continue;
442 if (mode != TYPE_MODE (float_type_node)
443 && (mode != TYPE_MODE (double_type_node))
444 && (mode != TYPE_MODE (long_double_type_node))
445 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
446 && (mode != TFmode)
447 #endif
449 continue;
451 /* Let the kind equal the precision divided by 8, rounding up. Again,
452 this insulates the programmer from the underlying byte size.
454 Also, it effectively deals with IEEE extended formats. There, the
455 total size of the type may equal 16, but it's got 6 bytes of padding
456 and the increased size can get in the way of a real IEEE quad format
457 which may also be supported by the target.
459 We round up so as to handle IA-64 __floatreg (RFmode), which is an
460 82 bit type. Not to be confused with __float80 (XFmode), which is
461 an 80 bit type also supported by IA-64. So XFmode should come out
462 to be kind=10, and RFmode should come out to be kind=11. Egads. */
464 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
466 if (kind == 4)
467 saw_r4 = true;
468 if (kind == 8)
469 saw_r8 = true;
470 if (kind == 10)
471 saw_r10 = true;
472 if (kind == 16)
473 saw_r16 = true;
475 /* Careful we don't stumble a weird internal mode. */
476 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
477 /* Or have too many modes for the allocated space. */
478 gcc_assert (r_index != MAX_REAL_KINDS);
480 gfc_real_kinds[r_index].kind = kind;
481 gfc_real_kinds[r_index].radix = fmt->b;
482 gfc_real_kinds[r_index].digits = fmt->p;
483 gfc_real_kinds[r_index].min_exponent = fmt->emin;
484 gfc_real_kinds[r_index].max_exponent = fmt->emax;
485 if (fmt->pnan < fmt->p)
486 /* This is an IBM extended double format (or the MIPS variant)
487 made up of two IEEE doubles. The value of the long double is
488 the sum of the values of the two parts. The most significant
489 part is required to be the value of the long double rounded
490 to the nearest double. If we use emax of 1024 then we can't
491 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
492 rounding will make the most significant part overflow. */
493 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
494 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
495 r_index += 1;
498 /* Choose the default integer kind. We choose 4 unless the user directs us
499 otherwise. Even if the user specified that the default integer kind is 8,
500 the numeric storage size is not 64 bits. In this case, a warning will be
501 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
503 gfc_numeric_storage_size = 4 * 8;
505 if (flag_default_integer)
507 if (!saw_i8)
508 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
509 "%<-fdefault-integer-8%> option");
511 gfc_default_integer_kind = 8;
514 else if (flag_integer4_kind == 8)
516 if (!saw_i8)
517 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
518 "%<-finteger-4-integer-8%> option");
520 gfc_default_integer_kind = 8;
522 else if (saw_i4)
524 gfc_default_integer_kind = 4;
526 else
528 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
529 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
532 /* Choose the default real kind. Again, we choose 4 when possible. */
533 if (flag_default_real_8)
535 if (!saw_r8)
536 gfc_fatal_error ("REAL(KIND=8) is not available for "
537 "%<-fdefault-real-8%> option");
539 gfc_default_real_kind = 8;
541 else if (flag_default_real_10)
543 if (!saw_r10)
544 gfc_fatal_error ("REAL(KIND=10) is not available for "
545 "%<-fdefault-real-10%> option");
547 gfc_default_real_kind = 10;
549 else if (flag_default_real_16)
551 if (!saw_r16)
552 gfc_fatal_error ("REAL(KIND=16) is not available for "
553 "%<-fdefault-real-16%> option");
555 gfc_default_real_kind = 16;
557 else if (flag_real4_kind == 8)
559 if (!saw_r8)
560 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
561 "option");
563 gfc_default_real_kind = 8;
565 else if (flag_real4_kind == 10)
567 if (!saw_r10)
568 gfc_fatal_error ("REAL(KIND=10) is not available for "
569 "%<-freal-4-real-10%> option");
571 gfc_default_real_kind = 10;
573 else if (flag_real4_kind == 16)
575 if (!saw_r16)
576 gfc_fatal_error ("REAL(KIND=16) is not available for "
577 "%<-freal-4-real-16%> option");
579 gfc_default_real_kind = 16;
581 else if (saw_r4)
582 gfc_default_real_kind = 4;
583 else
584 gfc_default_real_kind = gfc_real_kinds[0].kind;
586 /* Choose the default double kind. If -fdefault-real and -fdefault-double
587 are specified, we use kind=8, if it's available. If -fdefault-real is
588 specified without -fdefault-double, we use kind=16, if it's available.
589 Otherwise we do not change anything. */
590 if (flag_default_double && saw_r8)
591 gfc_default_double_kind = 8;
592 else if (flag_default_real_8 || flag_default_real_10 || flag_default_real_16)
594 /* Use largest available kind. */
595 if (saw_r16)
596 gfc_default_double_kind = 16;
597 else if (saw_r10)
598 gfc_default_double_kind = 10;
599 else if (saw_r8)
600 gfc_default_double_kind = 8;
601 else
602 gfc_default_double_kind = gfc_default_real_kind;
604 else if (flag_real8_kind == 4)
606 if (!saw_r4)
607 gfc_fatal_error ("REAL(KIND=4) is not available for "
608 "%<-freal-8-real-4%> option");
610 gfc_default_double_kind = 4;
612 else if (flag_real8_kind == 10 )
614 if (!saw_r10)
615 gfc_fatal_error ("REAL(KIND=10) is not available for "
616 "%<-freal-8-real-10%> option");
618 gfc_default_double_kind = 10;
620 else if (flag_real8_kind == 16 )
622 if (!saw_r16)
623 gfc_fatal_error ("REAL(KIND=10) is not available for "
624 "%<-freal-8-real-16%> option");
626 gfc_default_double_kind = 16;
628 else if (saw_r4 && saw_r8)
629 gfc_default_double_kind = 8;
630 else
632 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
633 real ... occupies two contiguous numeric storage units.
635 Therefore we must be supplied a kind twice as large as we chose
636 for single precision. There are loopholes, in that double
637 precision must *occupy* two storage units, though it doesn't have
638 to *use* two storage units. Which means that you can make this
639 kind artificially wide by padding it. But at present there are
640 no GCC targets for which a two-word type does not exist, so we
641 just let gfc_validate_kind abort and tell us if something breaks. */
643 gfc_default_double_kind
644 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
647 /* The default logical kind is constrained to be the same as the
648 default integer kind. Similarly with complex and real. */
649 gfc_default_logical_kind = gfc_default_integer_kind;
650 gfc_default_complex_kind = gfc_default_real_kind;
652 /* We only have two character kinds: ASCII and UCS-4.
653 ASCII corresponds to a 8-bit integer type, if one is available.
654 UCS-4 corresponds to a 32-bit integer type, if one is available. */
655 i_index = 0;
656 if ((kind = get_int_kind_from_width (8)) > 0)
658 gfc_character_kinds[i_index].kind = kind;
659 gfc_character_kinds[i_index].bit_size = 8;
660 gfc_character_kinds[i_index].name = "ascii";
661 i_index++;
663 if ((kind = get_int_kind_from_width (32)) > 0)
665 gfc_character_kinds[i_index].kind = kind;
666 gfc_character_kinds[i_index].bit_size = 32;
667 gfc_character_kinds[i_index].name = "iso_10646";
668 i_index++;
671 /* Choose the smallest integer kind for our default character. */
672 gfc_default_character_kind = gfc_character_kinds[0].kind;
673 gfc_character_storage_size = gfc_default_character_kind * 8;
675 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
677 /* Pick a kind the same size as the C "int" type. */
678 gfc_c_int_kind = INT_TYPE_SIZE / 8;
680 /* Choose atomic kinds to match C's int. */
681 gfc_atomic_int_kind = gfc_c_int_kind;
682 gfc_atomic_logical_kind = gfc_c_int_kind;
686 /* Make sure that a valid kind is present. Returns an index into the
687 associated kinds array, -1 if the kind is not present. */
689 static int
690 validate_integer (int kind)
692 int i;
694 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
695 if (gfc_integer_kinds[i].kind == kind)
696 return i;
698 return -1;
701 static int
702 validate_real (int kind)
704 int i;
706 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
707 if (gfc_real_kinds[i].kind == kind)
708 return i;
710 return -1;
713 static int
714 validate_logical (int kind)
716 int i;
718 for (i = 0; gfc_logical_kinds[i].kind; i++)
719 if (gfc_logical_kinds[i].kind == kind)
720 return i;
722 return -1;
725 static int
726 validate_character (int kind)
728 int i;
730 for (i = 0; gfc_character_kinds[i].kind; i++)
731 if (gfc_character_kinds[i].kind == kind)
732 return i;
734 return -1;
737 /* Validate a kind given a basic type. The return value is the same
738 for the child functions, with -1 indicating nonexistence of the
739 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
742 gfc_validate_kind (bt type, int kind, bool may_fail)
744 int rc;
746 switch (type)
748 case BT_REAL: /* Fall through */
749 case BT_COMPLEX:
750 rc = validate_real (kind);
751 break;
752 case BT_INTEGER:
753 rc = validate_integer (kind);
754 break;
755 case BT_LOGICAL:
756 rc = validate_logical (kind);
757 break;
758 case BT_CHARACTER:
759 rc = validate_character (kind);
760 break;
762 default:
763 gfc_internal_error ("gfc_validate_kind(): Got bad type");
766 if (rc < 0 && !may_fail)
767 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
769 return rc;
773 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
774 Reuse common type nodes where possible. Recognize if the kind matches up
775 with a C type. This will be used later in determining which routines may
776 be scarfed from libm. */
778 static tree
779 gfc_build_int_type (gfc_integer_info *info)
781 int mode_precision = info->bit_size;
783 if (mode_precision == CHAR_TYPE_SIZE)
784 info->c_char = 1;
785 if (mode_precision == SHORT_TYPE_SIZE)
786 info->c_short = 1;
787 if (mode_precision == INT_TYPE_SIZE)
788 info->c_int = 1;
789 if (mode_precision == LONG_TYPE_SIZE)
790 info->c_long = 1;
791 if (mode_precision == LONG_LONG_TYPE_SIZE)
792 info->c_long_long = 1;
794 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
795 return intQI_type_node;
796 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
797 return intHI_type_node;
798 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
799 return intSI_type_node;
800 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
801 return intDI_type_node;
802 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
803 return intTI_type_node;
805 return make_signed_type (mode_precision);
808 tree
809 gfc_build_uint_type (int size)
811 if (size == CHAR_TYPE_SIZE)
812 return unsigned_char_type_node;
813 if (size == SHORT_TYPE_SIZE)
814 return short_unsigned_type_node;
815 if (size == INT_TYPE_SIZE)
816 return unsigned_type_node;
817 if (size == LONG_TYPE_SIZE)
818 return long_unsigned_type_node;
819 if (size == LONG_LONG_TYPE_SIZE)
820 return long_long_unsigned_type_node;
822 return make_unsigned_type (size);
826 static tree
827 gfc_build_real_type (gfc_real_info *info)
829 int mode_precision = info->mode_precision;
830 tree new_type;
832 if (mode_precision == FLOAT_TYPE_SIZE)
833 info->c_float = 1;
834 if (mode_precision == DOUBLE_TYPE_SIZE)
835 info->c_double = 1;
836 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
837 info->c_long_double = 1;
838 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
840 info->c_float128 = 1;
841 gfc_real16_is_float128 = true;
844 if (TYPE_PRECISION (float_type_node) == mode_precision)
845 return float_type_node;
846 if (TYPE_PRECISION (double_type_node) == mode_precision)
847 return double_type_node;
848 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
849 return long_double_type_node;
851 new_type = make_node (REAL_TYPE);
852 TYPE_PRECISION (new_type) = mode_precision;
853 layout_type (new_type);
854 return new_type;
857 static tree
858 gfc_build_complex_type (tree scalar_type)
860 tree new_type;
862 if (scalar_type == NULL)
863 return NULL;
864 if (scalar_type == float_type_node)
865 return complex_float_type_node;
866 if (scalar_type == double_type_node)
867 return complex_double_type_node;
868 if (scalar_type == long_double_type_node)
869 return complex_long_double_type_node;
871 new_type = make_node (COMPLEX_TYPE);
872 TREE_TYPE (new_type) = scalar_type;
873 layout_type (new_type);
874 return new_type;
877 static tree
878 gfc_build_logical_type (gfc_logical_info *info)
880 int bit_size = info->bit_size;
881 tree new_type;
883 if (bit_size == BOOL_TYPE_SIZE)
885 info->c_bool = 1;
886 return boolean_type_node;
889 new_type = make_unsigned_type (bit_size);
890 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
891 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
892 TYPE_PRECISION (new_type) = 1;
894 return new_type;
898 /* Create the backend type nodes. We map them to their
899 equivalent C type, at least for now. We also give
900 names to the types here, and we push them in the
901 global binding level context.*/
903 void
904 gfc_init_types (void)
906 char name_buf[26];
907 int index;
908 tree type;
909 unsigned n;
911 /* Create and name the types. */
912 #define PUSH_TYPE(name, node) \
913 pushdecl (build_decl (input_location, \
914 TYPE_DECL, get_identifier (name), node))
916 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
918 type = gfc_build_int_type (&gfc_integer_kinds[index]);
919 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
920 if (TYPE_STRING_FLAG (type))
921 type = make_signed_type (gfc_integer_kinds[index].bit_size);
922 gfc_integer_types[index] = type;
923 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
924 gfc_integer_kinds[index].kind);
925 PUSH_TYPE (name_buf, type);
928 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
930 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
931 gfc_logical_types[index] = type;
932 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
933 gfc_logical_kinds[index].kind);
934 PUSH_TYPE (name_buf, type);
937 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
939 type = gfc_build_real_type (&gfc_real_kinds[index]);
940 gfc_real_types[index] = type;
941 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
942 gfc_real_kinds[index].kind);
943 PUSH_TYPE (name_buf, type);
945 if (gfc_real_kinds[index].c_float128)
946 gfc_float128_type_node = type;
948 type = gfc_build_complex_type (type);
949 gfc_complex_types[index] = type;
950 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
951 gfc_real_kinds[index].kind);
952 PUSH_TYPE (name_buf, type);
954 if (gfc_real_kinds[index].c_float128)
955 gfc_complex_float128_type_node = type;
958 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
960 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
961 type = build_qualified_type (type, TYPE_UNQUALIFIED);
962 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
963 gfc_character_kinds[index].kind);
964 PUSH_TYPE (name_buf, type);
965 gfc_character_types[index] = type;
966 gfc_pcharacter_types[index] = build_pointer_type (type);
968 gfc_character1_type_node = gfc_character_types[0];
970 PUSH_TYPE ("byte", unsigned_char_type_node);
971 PUSH_TYPE ("void", void_type_node);
973 /* DBX debugging output gets upset if these aren't set. */
974 if (!TYPE_NAME (integer_type_node))
975 PUSH_TYPE ("c_integer", integer_type_node);
976 if (!TYPE_NAME (char_type_node))
977 PUSH_TYPE ("c_char", char_type_node);
979 #undef PUSH_TYPE
981 pvoid_type_node = build_pointer_type (void_type_node);
982 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
983 ppvoid_type_node = build_pointer_type (pvoid_type_node);
984 pchar_type_node = build_pointer_type (gfc_character1_type_node);
985 pfunc_type_node
986 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
988 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
989 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
990 since this function is called before gfc_init_constants. */
991 gfc_array_range_type
992 = build_range_type (gfc_array_index_type,
993 build_int_cst (gfc_array_index_type, 0),
994 NULL_TREE);
996 /* The maximum array element size that can be handled is determined
997 by the number of bits available to store this field in the array
998 descriptor. */
1000 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
1001 gfc_max_array_element_size
1002 = wide_int_to_tree (size_type_node,
1003 wi::mask (n, UNSIGNED,
1004 TYPE_PRECISION (size_type_node)));
1006 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
1007 gfc_charlen_int_kind = 4;
1008 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
1011 /* Get the type node for the given type and kind. */
1013 tree
1014 gfc_get_int_type (int kind)
1016 int index = gfc_validate_kind (BT_INTEGER, kind, true);
1017 return index < 0 ? 0 : gfc_integer_types[index];
1020 tree
1021 gfc_get_real_type (int kind)
1023 int index = gfc_validate_kind (BT_REAL, kind, true);
1024 return index < 0 ? 0 : gfc_real_types[index];
1027 tree
1028 gfc_get_complex_type (int kind)
1030 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1031 return index < 0 ? 0 : gfc_complex_types[index];
1034 tree
1035 gfc_get_logical_type (int kind)
1037 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1038 return index < 0 ? 0 : gfc_logical_types[index];
1041 tree
1042 gfc_get_char_type (int kind)
1044 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1045 return index < 0 ? 0 : gfc_character_types[index];
1048 tree
1049 gfc_get_pchar_type (int kind)
1051 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1052 return index < 0 ? 0 : gfc_pcharacter_types[index];
1056 /* Create a character type with the given kind and length. */
1058 tree
1059 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1061 tree bounds, type;
1063 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1064 type = build_array_type (eltype, bounds);
1065 TYPE_STRING_FLAG (type) = 1;
1067 return type;
1070 tree
1071 gfc_get_character_type_len (int kind, tree len)
1073 gfc_validate_kind (BT_CHARACTER, kind, false);
1074 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1078 /* Get a type node for a character kind. */
1080 tree
1081 gfc_get_character_type (int kind, gfc_charlen * cl)
1083 tree len;
1085 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1086 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1087 len = build_fold_indirect_ref (len);
1089 return gfc_get_character_type_len (kind, len);
1092 /* Convert a basic type. This will be an array for character types. */
1094 tree
1095 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1097 tree basetype;
1099 switch (spec->type)
1101 case BT_UNKNOWN:
1102 gcc_unreachable ();
1104 case BT_INTEGER:
1105 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1106 has been resolved. This is done so we can convert C_PTR and
1107 C_FUNPTR to simple variables that get translated to (void *). */
1108 if (spec->f90_type == BT_VOID)
1110 if (spec->u.derived
1111 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1112 basetype = ptr_type_node;
1113 else
1114 basetype = pfunc_type_node;
1116 else
1117 basetype = gfc_get_int_type (spec->kind);
1118 break;
1120 case BT_REAL:
1121 basetype = gfc_get_real_type (spec->kind);
1122 break;
1124 case BT_COMPLEX:
1125 basetype = gfc_get_complex_type (spec->kind);
1126 break;
1128 case BT_LOGICAL:
1129 basetype = gfc_get_logical_type (spec->kind);
1130 break;
1132 case BT_CHARACTER:
1133 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1134 break;
1136 case BT_HOLLERITH:
1137 /* Since this cannot be used, return a length one character. */
1138 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1139 gfc_index_one_node);
1140 break;
1142 case BT_UNION:
1143 basetype = gfc_get_union_type (spec->u.derived);
1144 break;
1146 case BT_DERIVED:
1147 case BT_CLASS:
1148 basetype = gfc_get_derived_type (spec->u.derived, codim);
1150 if (spec->type == BT_CLASS)
1151 GFC_CLASS_TYPE_P (basetype) = 1;
1153 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1154 type and kind to fit a (void *) and the basetype returned was a
1155 ptr_type_node. We need to pass up this new information to the
1156 symbol that was declared of type C_PTR or C_FUNPTR. */
1157 if (spec->u.derived->ts.f90_type == BT_VOID)
1159 spec->type = BT_INTEGER;
1160 spec->kind = gfc_index_integer_kind;
1161 spec->f90_type = BT_VOID;
1163 break;
1164 case BT_VOID:
1165 case BT_ASSUMED:
1166 /* This is for the second arg to c_f_pointer and c_f_procpointer
1167 of the iso_c_binding module, to accept any ptr type. */
1168 basetype = ptr_type_node;
1169 if (spec->f90_type == BT_VOID)
1171 if (spec->u.derived
1172 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1173 basetype = ptr_type_node;
1174 else
1175 basetype = pfunc_type_node;
1177 break;
1178 default:
1179 gcc_unreachable ();
1181 return basetype;
1184 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1186 static tree
1187 gfc_conv_array_bound (gfc_expr * expr)
1189 /* If expr is an integer constant, return that. */
1190 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1191 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1193 /* Otherwise return NULL. */
1194 return NULL_TREE;
1197 /* Return the type of an element of the array. Note that scalar coarrays
1198 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1199 (with POINTER_TYPE stripped) is returned. */
1201 tree
1202 gfc_get_element_type (tree type)
1204 tree element;
1206 if (GFC_ARRAY_TYPE_P (type))
1208 if (TREE_CODE (type) == POINTER_TYPE)
1209 type = TREE_TYPE (type);
1210 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1212 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1213 element = type;
1215 else
1217 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1218 element = TREE_TYPE (type);
1221 else
1223 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1224 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1226 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1227 element = TREE_TYPE (element);
1229 /* For arrays, which are not scalar coarrays. */
1230 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1231 element = TREE_TYPE (element);
1234 return element;
1237 /* Build an array. This function is called from gfc_sym_type().
1238 Actually returns array descriptor type.
1240 Format of array descriptors is as follows:
1242 struct gfc_array_descriptor
1244 array *data
1245 index offset;
1246 index dtype;
1247 struct descriptor_dimension dimension[N_DIM];
1250 struct descriptor_dimension
1252 index stride;
1253 index lbound;
1254 index ubound;
1257 Translation code should use gfc_conv_descriptor_* rather than
1258 accessing the descriptor directly. Any changes to the array
1259 descriptor type will require changes in gfc_conv_descriptor_* and
1260 gfc_build_array_initializer.
1262 This is represented internally as a RECORD_TYPE. The index nodes
1263 are gfc_array_index_type and the data node is a pointer to the
1264 data. See below for the handling of character types.
1266 The dtype member is formatted as follows:
1267 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1268 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1269 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1271 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1272 this generated poor code for assumed/deferred size arrays. These
1273 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1274 of the GENERIC grammar. Also, there is no way to explicitly set
1275 the array stride, so all data must be packed(1). I've tried to
1276 mark all the functions which would require modification with a GCC
1277 ARRAYS comment.
1279 The data component points to the first element in the array. The
1280 offset field is the position of the origin of the array (i.e. element
1281 (0, 0 ...)). This may be outside the bounds of the array.
1283 An element is accessed by
1284 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1285 This gives good performance as the computation does not involve the
1286 bounds of the array. For packed arrays, this is optimized further
1287 by substituting the known strides.
1289 This system has one problem: all array bounds must be within 2^31
1290 elements of the origin (2^63 on 64-bit machines). For example
1291 integer, dimension (80000:90000, 80000:90000, 2) :: array
1292 may not work properly on 32-bit machines because 80000*80000 >
1293 2^31, so the calculation for stride2 would overflow. This may
1294 still work, but I haven't checked, and it relies on the overflow
1295 doing the right thing.
1297 The way to fix this problem is to access elements as follows:
1298 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1299 Obviously this is much slower. I will make this a compile time
1300 option, something like -fsmall-array-offsets. Mixing code compiled
1301 with and without this switch will work.
1303 (1) This can be worked around by modifying the upper bound of the
1304 previous dimension. This requires extra fields in the descriptor
1305 (both real_ubound and fake_ubound). */
1308 /* Returns true if the array sym does not require a descriptor. */
1311 gfc_is_nodesc_array (gfc_symbol * sym)
1313 symbol_attribute *array_attr;
1314 gfc_array_spec *as;
1315 bool is_classarray = IS_CLASS_ARRAY (sym);
1317 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1318 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1320 gcc_assert (array_attr->dimension || array_attr->codimension);
1322 /* We only want local arrays. */
1323 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1324 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1325 || array_attr->allocatable)
1326 return 0;
1328 /* We want a descriptor for associate-name arrays that do not have an
1329 explicitly known shape already. */
1330 if (sym->assoc && as->type != AS_EXPLICIT)
1331 return 0;
1333 /* The dummy is stored in sym and not in the component. */
1334 if (sym->attr.dummy)
1335 return as->type != AS_ASSUMED_SHAPE
1336 && as->type != AS_ASSUMED_RANK;
1338 if (sym->attr.result || sym->attr.function)
1339 return 0;
1341 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1343 return 1;
1347 /* Create an array descriptor type. */
1349 static tree
1350 gfc_build_array_type (tree type, gfc_array_spec * as,
1351 enum gfc_array_kind akind, bool restricted,
1352 bool contiguous, int codim)
1354 tree lbound[GFC_MAX_DIMENSIONS];
1355 tree ubound[GFC_MAX_DIMENSIONS];
1356 int n, corank;
1358 /* Assumed-shape arrays do not have codimension information stored in the
1359 descriptor. */
1360 corank = MAX (as->corank, codim);
1361 if (as->type == AS_ASSUMED_SHAPE ||
1362 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1363 corank = codim;
1365 if (as->type == AS_ASSUMED_RANK)
1366 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1368 lbound[n] = NULL_TREE;
1369 ubound[n] = NULL_TREE;
1372 for (n = 0; n < as->rank; n++)
1374 /* Create expressions for the known bounds of the array. */
1375 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1376 lbound[n] = gfc_index_one_node;
1377 else
1378 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1379 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1382 for (n = as->rank; n < as->rank + corank; n++)
1384 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1385 lbound[n] = gfc_index_one_node;
1386 else
1387 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1389 if (n < as->rank + corank - 1)
1390 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1393 if (as->type == AS_ASSUMED_SHAPE)
1394 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1395 : GFC_ARRAY_ASSUMED_SHAPE;
1396 else if (as->type == AS_ASSUMED_RANK)
1397 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1398 : GFC_ARRAY_ASSUMED_RANK;
1399 return gfc_get_array_type_bounds (type, as->rank == -1
1400 ? GFC_MAX_DIMENSIONS : as->rank,
1401 corank, lbound, ubound, 0, akind,
1402 restricted);
1405 /* Returns the struct descriptor_dimension type. */
1407 static tree
1408 gfc_get_desc_dim_type (void)
1410 tree type;
1411 tree decl, *chain = NULL;
1413 if (gfc_desc_dim_type)
1414 return gfc_desc_dim_type;
1416 /* Build the type node. */
1417 type = make_node (RECORD_TYPE);
1419 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1420 TYPE_PACKED (type) = 1;
1422 /* Consists of the stride, lbound and ubound members. */
1423 decl = gfc_add_field_to_struct_1 (type,
1424 get_identifier ("stride"),
1425 gfc_array_index_type, &chain);
1426 TREE_NO_WARNING (decl) = 1;
1428 decl = gfc_add_field_to_struct_1 (type,
1429 get_identifier ("lbound"),
1430 gfc_array_index_type, &chain);
1431 TREE_NO_WARNING (decl) = 1;
1433 decl = gfc_add_field_to_struct_1 (type,
1434 get_identifier ("ubound"),
1435 gfc_array_index_type, &chain);
1436 TREE_NO_WARNING (decl) = 1;
1438 /* Finish off the type. */
1439 gfc_finish_type (type);
1440 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1442 gfc_desc_dim_type = type;
1443 return type;
1447 /* Return the DTYPE for an array. This describes the type and type parameters
1448 of the array. */
1449 /* TODO: Only call this when the value is actually used, and make all the
1450 unknown cases abort. */
1452 tree
1453 gfc_get_dtype_rank_type (int rank, tree etype)
1455 tree size;
1456 int n;
1457 HOST_WIDE_INT i;
1458 tree tmp;
1459 tree dtype;
1461 switch (TREE_CODE (etype))
1463 case INTEGER_TYPE:
1464 n = BT_INTEGER;
1465 break;
1467 case BOOLEAN_TYPE:
1468 n = BT_LOGICAL;
1469 break;
1471 case REAL_TYPE:
1472 n = BT_REAL;
1473 break;
1475 case COMPLEX_TYPE:
1476 n = BT_COMPLEX;
1477 break;
1479 /* We will never have arrays of arrays. */
1480 case RECORD_TYPE:
1481 n = BT_DERIVED;
1482 break;
1484 case ARRAY_TYPE:
1485 n = BT_CHARACTER;
1486 break;
1488 case POINTER_TYPE:
1489 n = BT_ASSUMED;
1490 break;
1492 default:
1493 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1494 /* We can strange array types for temporary arrays. */
1495 return gfc_index_zero_node;
1498 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1499 size = TYPE_SIZE_UNIT (etype);
1501 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1502 if (size && INTEGER_CST_P (size))
1504 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1505 gfc_fatal_error ("Array element size too big at %C");
1507 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1509 dtype = build_int_cst (gfc_array_index_type, i);
1511 if (size && !INTEGER_CST_P (size))
1513 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1514 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1515 gfc_array_index_type,
1516 fold_convert (gfc_array_index_type, size), tmp);
1517 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1518 tmp, dtype);
1520 /* If we don't know the size we leave it as zero. This should never happen
1521 for anything that is actually used. */
1522 /* TODO: Check this is actually true, particularly when repacking
1523 assumed size parameters. */
1525 return dtype;
1529 tree
1530 gfc_get_dtype (tree type)
1532 tree dtype;
1533 tree etype;
1534 int rank;
1536 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1538 if (GFC_TYPE_ARRAY_DTYPE (type))
1539 return GFC_TYPE_ARRAY_DTYPE (type);
1541 rank = GFC_TYPE_ARRAY_RANK (type);
1542 etype = gfc_get_element_type (type);
1543 dtype = gfc_get_dtype_rank_type (rank, etype);
1545 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1546 return dtype;
1550 /* Build an array type for use without a descriptor, packed according
1551 to the value of PACKED. */
1553 tree
1554 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1555 bool restricted)
1557 tree range;
1558 tree type;
1559 tree tmp;
1560 int n;
1561 int known_stride;
1562 int known_offset;
1563 mpz_t offset;
1564 mpz_t stride;
1565 mpz_t delta;
1566 gfc_expr *expr;
1568 mpz_init_set_ui (offset, 0);
1569 mpz_init_set_ui (stride, 1);
1570 mpz_init (delta);
1572 /* We don't use build_array_type because this does not include include
1573 lang-specific information (i.e. the bounds of the array) when checking
1574 for duplicates. */
1575 if (as->rank)
1576 type = make_node (ARRAY_TYPE);
1577 else
1578 type = build_variant_type_copy (etype);
1580 GFC_ARRAY_TYPE_P (type) = 1;
1581 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1583 known_stride = (packed != PACKED_NO);
1584 known_offset = 1;
1585 for (n = 0; n < as->rank; n++)
1587 /* Fill in the stride and bound components of the type. */
1588 if (known_stride)
1589 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1590 else
1591 tmp = NULL_TREE;
1592 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1594 expr = as->lower[n];
1595 if (expr->expr_type == EXPR_CONSTANT)
1597 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1598 gfc_index_integer_kind);
1600 else
1602 known_stride = 0;
1603 tmp = NULL_TREE;
1605 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1607 if (known_stride)
1609 /* Calculate the offset. */
1610 mpz_mul (delta, stride, as->lower[n]->value.integer);
1611 mpz_sub (offset, offset, delta);
1613 else
1614 known_offset = 0;
1616 expr = as->upper[n];
1617 if (expr && expr->expr_type == EXPR_CONSTANT)
1619 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1620 gfc_index_integer_kind);
1622 else
1624 tmp = NULL_TREE;
1625 known_stride = 0;
1627 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1629 if (known_stride)
1631 /* Calculate the stride. */
1632 mpz_sub (delta, as->upper[n]->value.integer,
1633 as->lower[n]->value.integer);
1634 mpz_add_ui (delta, delta, 1);
1635 mpz_mul (stride, stride, delta);
1638 /* Only the first stride is known for partial packed arrays. */
1639 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1640 known_stride = 0;
1642 for (n = as->rank; n < as->rank + as->corank; n++)
1644 expr = as->lower[n];
1645 if (expr->expr_type == EXPR_CONSTANT)
1646 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1647 gfc_index_integer_kind);
1648 else
1649 tmp = NULL_TREE;
1650 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1652 expr = as->upper[n];
1653 if (expr && 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 if (n < as->rank + as->corank - 1)
1659 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1662 if (known_offset)
1664 GFC_TYPE_ARRAY_OFFSET (type) =
1665 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1667 else
1668 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1670 if (known_stride)
1672 GFC_TYPE_ARRAY_SIZE (type) =
1673 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1675 else
1676 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1678 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1679 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1680 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1681 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1682 NULL_TREE);
1683 /* TODO: use main type if it is unbounded. */
1684 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1685 build_pointer_type (build_array_type (etype, range));
1686 if (restricted)
1687 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1688 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1689 TYPE_QUAL_RESTRICT);
1691 if (as->rank == 0)
1693 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1695 type = build_pointer_type (type);
1697 if (restricted)
1698 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1700 GFC_ARRAY_TYPE_P (type) = 1;
1701 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1704 return type;
1707 if (known_stride)
1709 mpz_sub_ui (stride, stride, 1);
1710 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1712 else
1713 range = NULL_TREE;
1715 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1716 TYPE_DOMAIN (type) = range;
1718 build_pointer_type (etype);
1719 TREE_TYPE (type) = etype;
1721 layout_type (type);
1723 mpz_clear (offset);
1724 mpz_clear (stride);
1725 mpz_clear (delta);
1727 /* Represent packed arrays as multi-dimensional if they have rank >
1728 1 and with proper bounds, instead of flat arrays. This makes for
1729 better debug info. */
1730 if (known_offset)
1732 tree gtype = etype, rtype, type_decl;
1734 for (n = as->rank - 1; n >= 0; n--)
1736 rtype = build_range_type (gfc_array_index_type,
1737 GFC_TYPE_ARRAY_LBOUND (type, n),
1738 GFC_TYPE_ARRAY_UBOUND (type, n));
1739 gtype = build_array_type (gtype, rtype);
1741 TYPE_NAME (type) = type_decl = build_decl (input_location,
1742 TYPE_DECL, NULL, gtype);
1743 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1746 if (packed != PACKED_STATIC || !known_stride
1747 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1749 /* For dummy arrays and automatic (heap allocated) arrays we
1750 want a pointer to the array. */
1751 type = build_pointer_type (type);
1752 if (restricted)
1753 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1754 GFC_ARRAY_TYPE_P (type) = 1;
1755 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1757 return type;
1761 /* Return or create the base type for an array descriptor. */
1763 static tree
1764 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1766 tree fat_type, decl, arraytype, *chain = NULL;
1767 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1768 int idx;
1770 /* Assumed-rank array. */
1771 if (dimen == -1)
1772 dimen = GFC_MAX_DIMENSIONS;
1774 idx = 2 * (codimen + dimen) + restricted;
1776 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1778 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1780 if (gfc_array_descriptor_base_caf[idx])
1781 return gfc_array_descriptor_base_caf[idx];
1783 else if (gfc_array_descriptor_base[idx])
1784 return gfc_array_descriptor_base[idx];
1786 /* Build the type node. */
1787 fat_type = make_node (RECORD_TYPE);
1789 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1790 TYPE_NAME (fat_type) = get_identifier (name);
1791 TYPE_NAMELESS (fat_type) = 1;
1793 /* Add the data member as the first element of the descriptor. */
1794 decl = gfc_add_field_to_struct_1 (fat_type,
1795 get_identifier ("data"),
1796 (restricted
1797 ? prvoid_type_node
1798 : ptr_type_node), &chain);
1800 /* Add the base component. */
1801 decl = gfc_add_field_to_struct_1 (fat_type,
1802 get_identifier ("offset"),
1803 gfc_array_index_type, &chain);
1804 TREE_NO_WARNING (decl) = 1;
1806 /* Add the dtype component. */
1807 decl = gfc_add_field_to_struct_1 (fat_type,
1808 get_identifier ("dtype"),
1809 gfc_array_index_type, &chain);
1810 TREE_NO_WARNING (decl) = 1;
1812 /* Add the span component. */
1813 decl = gfc_add_field_to_struct_1 (fat_type,
1814 get_identifier ("span"),
1815 gfc_array_index_type, &chain);
1816 TREE_NO_WARNING (decl) = 1;
1818 /* Build the array type for the stride and bound components. */
1819 if (dimen + codimen > 0)
1821 arraytype =
1822 build_array_type (gfc_get_desc_dim_type (),
1823 build_range_type (gfc_array_index_type,
1824 gfc_index_zero_node,
1825 gfc_rank_cst[codimen + dimen - 1]));
1827 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1828 arraytype, &chain);
1829 TREE_NO_WARNING (decl) = 1;
1832 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1834 decl = gfc_add_field_to_struct_1 (fat_type,
1835 get_identifier ("token"),
1836 prvoid_type_node, &chain);
1837 TREE_NO_WARNING (decl) = 1;
1840 /* Finish off the type. */
1841 gfc_finish_type (fat_type);
1842 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1844 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1845 gfc_array_descriptor_base_caf[idx] = fat_type;
1846 else
1847 gfc_array_descriptor_base[idx] = fat_type;
1849 return fat_type;
1853 /* Build an array (descriptor) type with given bounds. */
1855 tree
1856 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1857 tree * ubound, int packed,
1858 enum gfc_array_kind akind, bool restricted)
1860 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1861 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1862 const char *type_name;
1863 int n;
1865 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1866 fat_type = build_distinct_type_copy (base_type);
1867 /* Make sure that nontarget and target array type have the same canonical
1868 type (and same stub decl for debug info). */
1869 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1870 TYPE_CANONICAL (fat_type) = base_type;
1871 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1873 tmp = TYPE_NAME (etype);
1874 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1875 tmp = DECL_NAME (tmp);
1876 if (tmp)
1877 type_name = IDENTIFIER_POINTER (tmp);
1878 else
1879 type_name = "unknown";
1880 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1881 GFC_MAX_SYMBOL_LEN, type_name);
1882 TYPE_NAME (fat_type) = get_identifier (name);
1883 TYPE_NAMELESS (fat_type) = 1;
1885 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1886 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1888 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1889 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1890 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1891 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1893 /* Build an array descriptor record type. */
1894 if (packed != 0)
1895 stride = gfc_index_one_node;
1896 else
1897 stride = NULL_TREE;
1898 for (n = 0; n < dimen + codimen; n++)
1900 if (n < dimen)
1901 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1903 if (lbound)
1904 lower = lbound[n];
1905 else
1906 lower = NULL_TREE;
1908 if (lower != NULL_TREE)
1910 if (INTEGER_CST_P (lower))
1911 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1912 else
1913 lower = NULL_TREE;
1916 if (codimen && n == dimen + codimen - 1)
1917 break;
1919 upper = ubound[n];
1920 if (upper != NULL_TREE)
1922 if (INTEGER_CST_P (upper))
1923 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1924 else
1925 upper = NULL_TREE;
1928 if (n >= dimen)
1929 continue;
1931 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1933 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1934 gfc_array_index_type, upper, lower);
1935 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1936 gfc_array_index_type, tmp,
1937 gfc_index_one_node);
1938 stride = fold_build2_loc (input_location, MULT_EXPR,
1939 gfc_array_index_type, tmp, stride);
1940 /* Check the folding worked. */
1941 gcc_assert (INTEGER_CST_P (stride));
1943 else
1944 stride = NULL_TREE;
1946 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1948 /* TODO: known offsets for descriptors. */
1949 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1951 if (dimen == 0)
1953 arraytype = build_pointer_type (etype);
1954 if (restricted)
1955 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1957 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1958 return fat_type;
1961 /* We define data as an array with the correct size if possible.
1962 Much better than doing pointer arithmetic. */
1963 if (stride)
1964 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1965 int_const_binop (MINUS_EXPR, stride,
1966 build_int_cst (TREE_TYPE (stride), 1)));
1967 else
1968 rtype = gfc_array_range_type;
1969 arraytype = build_array_type (etype, rtype);
1970 arraytype = build_pointer_type (arraytype);
1971 if (restricted)
1972 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1973 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1975 /* This will generate the base declarations we need to emit debug
1976 information for this type. FIXME: there must be a better way to
1977 avoid divergence between compilations with and without debug
1978 information. */
1980 struct array_descr_info info;
1981 gfc_get_array_descr_info (fat_type, &info);
1982 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1985 return fat_type;
1988 /* Build a pointer type. This function is called from gfc_sym_type(). */
1990 static tree
1991 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1993 /* Array pointer types aren't actually pointers. */
1994 if (sym->attr.dimension)
1995 return type;
1996 else
1997 return build_pointer_type (type);
2000 static tree gfc_nonrestricted_type (tree t);
2001 /* Given two record or union type nodes TO and FROM, ensure
2002 that all fields in FROM have a corresponding field in TO,
2003 their type being nonrestrict variants. This accepts a TO
2004 node that already has a prefix of the fields in FROM. */
2005 static void
2006 mirror_fields (tree to, tree from)
2008 tree fto, ffrom;
2009 tree *chain;
2011 /* Forward to the end of TOs fields. */
2012 fto = TYPE_FIELDS (to);
2013 ffrom = TYPE_FIELDS (from);
2014 chain = &TYPE_FIELDS (to);
2015 while (fto)
2017 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
2018 chain = &DECL_CHAIN (fto);
2019 fto = DECL_CHAIN (fto);
2020 ffrom = DECL_CHAIN (ffrom);
2023 /* Now add all fields remaining in FROM (starting with ffrom). */
2024 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2026 tree newfield = copy_node (ffrom);
2027 DECL_CONTEXT (newfield) = to;
2028 /* The store to DECL_CHAIN might seem redundant with the
2029 stores to *chain, but not clearing it here would mean
2030 leaving a chain into the old fields. If ever
2031 our called functions would look at them confusion
2032 will arise. */
2033 DECL_CHAIN (newfield) = NULL_TREE;
2034 *chain = newfield;
2035 chain = &DECL_CHAIN (newfield);
2037 if (TREE_CODE (ffrom) == FIELD_DECL)
2039 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2040 TREE_TYPE (newfield) = elemtype;
2043 *chain = NULL_TREE;
2046 /* Given a type T, returns a different type of the same structure,
2047 except that all types it refers to (recursively) are always
2048 non-restrict qualified types. */
2049 static tree
2050 gfc_nonrestricted_type (tree t)
2052 tree ret = t;
2054 /* If the type isn't laid out yet, don't copy it. If something
2055 needs it for real it should wait until the type got finished. */
2056 if (!TYPE_SIZE (t))
2057 return t;
2059 if (!TYPE_LANG_SPECIFIC (t))
2060 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2061 /* If we're dealing with this very node already further up
2062 the call chain (recursion via pointers and struct members)
2063 we haven't yet determined if we really need a new type node.
2064 Assume we don't, return T itself. */
2065 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2066 return t;
2068 /* If we have calculated this all already, just return it. */
2069 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2070 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2072 /* Mark this type. */
2073 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2075 switch (TREE_CODE (t))
2077 default:
2078 break;
2080 case POINTER_TYPE:
2081 case REFERENCE_TYPE:
2083 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2084 if (totype == TREE_TYPE (t))
2085 ret = t;
2086 else if (TREE_CODE (t) == POINTER_TYPE)
2087 ret = build_pointer_type (totype);
2088 else
2089 ret = build_reference_type (totype);
2090 ret = build_qualified_type (ret,
2091 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2093 break;
2095 case ARRAY_TYPE:
2097 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2098 if (elemtype == TREE_TYPE (t))
2099 ret = t;
2100 else
2102 ret = build_variant_type_copy (t);
2103 TREE_TYPE (ret) = elemtype;
2104 if (TYPE_LANG_SPECIFIC (t)
2105 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2107 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2108 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2109 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2111 TYPE_LANG_SPECIFIC (ret)
2112 = ggc_cleared_alloc<struct lang_type> ();
2113 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2114 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2119 break;
2121 case RECORD_TYPE:
2122 case UNION_TYPE:
2123 case QUAL_UNION_TYPE:
2125 tree field;
2126 /* First determine if we need a new type at all.
2127 Careful, the two calls to gfc_nonrestricted_type per field
2128 might return different values. That happens exactly when
2129 one of the fields reaches back to this very record type
2130 (via pointers). The first calls will assume that we don't
2131 need to copy T (see the error_mark_node marking). If there
2132 are any reasons for copying T apart from having to copy T,
2133 we'll indeed copy it, and the second calls to
2134 gfc_nonrestricted_type will use that new node if they
2135 reach back to T. */
2136 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2137 if (TREE_CODE (field) == FIELD_DECL)
2139 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2140 if (elemtype != TREE_TYPE (field))
2141 break;
2143 if (!field)
2144 break;
2145 ret = build_variant_type_copy (t);
2146 TYPE_FIELDS (ret) = NULL_TREE;
2148 /* Here we make sure that as soon as we know we have to copy
2149 T, that also fields reaching back to us will use the new
2150 copy. It's okay if that copy still contains the old fields,
2151 we won't look at them. */
2152 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2153 mirror_fields (ret, t);
2155 break;
2158 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2159 return ret;
2163 /* Return the type for a symbol. Special handling is required for character
2164 types to get the correct level of indirection.
2165 For functions return the return type.
2166 For subroutines return void_type_node.
2167 Calling this multiple times for the same symbol should be avoided,
2168 especially for character and array types. */
2170 tree
2171 gfc_sym_type (gfc_symbol * sym)
2173 tree type;
2174 int byref;
2175 bool restricted;
2177 /* Procedure Pointers inside COMMON blocks. */
2178 if (sym->attr.proc_pointer && sym->attr.in_common)
2180 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2181 sym->attr.proc_pointer = 0;
2182 type = build_pointer_type (gfc_get_function_type (sym));
2183 sym->attr.proc_pointer = 1;
2184 return type;
2187 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2188 return void_type_node;
2190 /* In the case of a function the fake result variable may have a
2191 type different from the function type, so don't return early in
2192 that case. */
2193 if (sym->backend_decl && !sym->attr.function)
2194 return TREE_TYPE (sym->backend_decl);
2196 if (sym->ts.type == BT_CHARACTER
2197 && ((sym->attr.function && sym->attr.is_bind_c)
2198 || (sym->attr.result
2199 && sym->ns->proc_name
2200 && sym->ns->proc_name->attr.is_bind_c)
2201 || (sym->ts.deferred && (!sym->ts.u.cl
2202 || !sym->ts.u.cl->backend_decl))))
2203 type = gfc_character1_type_node;
2204 else
2205 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2207 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2208 byref = 1;
2209 else
2210 byref = 0;
2212 restricted = !sym->attr.target && !sym->attr.pointer
2213 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2214 if (!restricted)
2215 type = gfc_nonrestricted_type (type);
2217 if (sym->attr.dimension || sym->attr.codimension)
2219 if (gfc_is_nodesc_array (sym))
2221 /* If this is a character argument of unknown length, just use the
2222 base type. */
2223 if (sym->ts.type != BT_CHARACTER
2224 || !(sym->attr.dummy || sym->attr.function)
2225 || sym->ts.u.cl->backend_decl)
2227 type = gfc_get_nodesc_array_type (type, sym->as,
2228 byref ? PACKED_FULL
2229 : PACKED_STATIC,
2230 restricted);
2231 byref = 0;
2234 else
2236 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2237 if (sym->attr.pointer)
2238 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2239 : GFC_ARRAY_POINTER;
2240 else if (sym->attr.allocatable)
2241 akind = GFC_ARRAY_ALLOCATABLE;
2242 type = gfc_build_array_type (type, sym->as, akind, restricted,
2243 sym->attr.contiguous, false);
2246 else
2248 if (sym->attr.allocatable || sym->attr.pointer
2249 || gfc_is_associate_pointer (sym))
2250 type = gfc_build_pointer_type (sym, type);
2253 /* We currently pass all parameters by reference.
2254 See f95_get_function_decl. For dummy function parameters return the
2255 function type. */
2256 if (byref)
2258 /* We must use pointer types for potentially absent variables. The
2259 optimizers assume a reference type argument is never NULL. */
2260 if (sym->attr.optional
2261 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2262 type = build_pointer_type (type);
2263 else
2265 type = build_reference_type (type);
2266 if (restricted)
2267 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2271 return (type);
2274 /* Layout and output debug info for a record type. */
2276 void
2277 gfc_finish_type (tree type)
2279 tree decl;
2281 decl = build_decl (input_location,
2282 TYPE_DECL, NULL_TREE, type);
2283 TYPE_STUB_DECL (type) = decl;
2284 layout_type (type);
2285 rest_of_type_compilation (type, 1);
2286 rest_of_decl_compilation (decl, 1, 0);
2289 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2290 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2291 to the end of the field list pointed to by *CHAIN.
2293 Returns a pointer to the new field. */
2295 static tree
2296 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2298 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2300 DECL_CONTEXT (decl) = context;
2301 DECL_CHAIN (decl) = NULL_TREE;
2302 if (TYPE_FIELDS (context) == NULL_TREE)
2303 TYPE_FIELDS (context) = decl;
2304 if (chain != NULL)
2306 if (*chain != NULL)
2307 **chain = decl;
2308 *chain = &DECL_CHAIN (decl);
2311 return decl;
2314 /* Like `gfc_add_field_to_struct_1', but adds alignment
2315 information. */
2317 tree
2318 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2320 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2322 DECL_INITIAL (decl) = 0;
2323 SET_DECL_ALIGN (decl, 0);
2324 DECL_USER_ALIGN (decl) = 0;
2326 return decl;
2330 /* Copy the backend_decl and component backend_decls if
2331 the two derived type symbols are "equal", as described
2332 in 4.4.2 and resolved by gfc_compare_derived_types. */
2335 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2336 bool from_gsym)
2338 gfc_component *to_cm;
2339 gfc_component *from_cm;
2341 if (from == to)
2342 return 1;
2344 if (from->backend_decl == NULL
2345 || !gfc_compare_derived_types (from, to))
2346 return 0;
2348 to->backend_decl = from->backend_decl;
2350 to_cm = to->components;
2351 from_cm = from->components;
2353 /* Copy the component declarations. If a component is itself
2354 a derived type, we need a copy of its component declarations.
2355 This is done by recursing into gfc_get_derived_type and
2356 ensures that the component's component declarations have
2357 been built. If it is a character, we need the character
2358 length, as well. */
2359 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2361 to_cm->backend_decl = from_cm->backend_decl;
2362 if (from_cm->ts.type == BT_UNION)
2363 gfc_get_union_type (to_cm->ts.u.derived);
2364 else if (from_cm->ts.type == BT_DERIVED
2365 && (!from_cm->attr.pointer || from_gsym))
2366 gfc_get_derived_type (to_cm->ts.u.derived);
2367 else if (from_cm->ts.type == BT_CLASS
2368 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2369 gfc_get_derived_type (to_cm->ts.u.derived);
2370 else if (from_cm->ts.type == BT_CHARACTER)
2371 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2374 return 1;
2378 /* Build a tree node for a procedure pointer component. */
2380 tree
2381 gfc_get_ppc_type (gfc_component* c)
2383 tree t;
2385 /* Explicit interface. */
2386 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2387 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2389 /* Implicit interface (only return value may be known). */
2390 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2391 t = gfc_typenode_for_spec (&c->ts);
2392 else
2393 t = void_type_node;
2395 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2399 /* Build a tree node for a union type. Requires building each map
2400 structure which is an element of the union. */
2402 tree
2403 gfc_get_union_type (gfc_symbol *un)
2405 gfc_component *map = NULL;
2406 tree typenode = NULL, map_type = NULL, map_field = NULL;
2407 tree *chain = NULL;
2409 if (un->backend_decl)
2411 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2412 return un->backend_decl;
2413 else
2414 typenode = un->backend_decl;
2416 else
2418 typenode = make_node (UNION_TYPE);
2419 TYPE_NAME (typenode) = get_identifier (un->name);
2422 /* Add each contained MAP as a field. */
2423 for (map = un->components; map; map = map->next)
2425 gcc_assert (map->ts.type == BT_DERIVED);
2427 /* The map's type node, which is defined within this union's context. */
2428 map_type = gfc_get_derived_type (map->ts.u.derived);
2429 TYPE_CONTEXT (map_type) = typenode;
2431 /* The map field's declaration. */
2432 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2433 map_type, &chain);
2434 if (map->loc.lb)
2435 gfc_set_decl_location (map_field, &map->loc);
2436 else if (un->declared_at.lb)
2437 gfc_set_decl_location (map_field, &un->declared_at);
2439 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2440 DECL_NAMELESS(map_field) = true;
2442 /* We should never clobber another backend declaration for this map,
2443 because each map component is unique. */
2444 if (!map->backend_decl)
2445 map->backend_decl = map_field;
2448 un->backend_decl = typenode;
2449 gfc_finish_type (typenode);
2451 return typenode;
2455 /* Build a tree node for a derived type. If there are equal
2456 derived types, with different local names, these are built
2457 at the same time. If an equal derived type has been built
2458 in a parent namespace, this is used. */
2460 tree
2461 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2463 tree typenode = NULL, field = NULL, field_type = NULL;
2464 tree canonical = NULL_TREE;
2465 tree *chain = NULL;
2466 bool got_canonical = false;
2467 bool unlimited_entity = false;
2468 gfc_component *c;
2469 gfc_dt_list *dt;
2470 gfc_namespace *ns;
2471 tree tmp;
2473 gcc_assert (!derived->attr.pdt_template);
2475 if (derived->attr.unlimited_polymorphic
2476 || (flag_coarray == GFC_FCOARRAY_LIB
2477 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2478 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2479 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
2480 return ptr_type_node;
2482 if (flag_coarray != GFC_FCOARRAY_LIB
2483 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2484 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2485 return gfc_get_int_type (gfc_default_integer_kind);
2487 if (derived && derived->attr.flavor == FL_PROCEDURE
2488 && derived->attr.generic)
2489 derived = gfc_find_dt_in_generic (derived);
2491 /* See if it's one of the iso_c_binding derived types. */
2492 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2494 if (derived->backend_decl)
2495 return derived->backend_decl;
2497 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2498 derived->backend_decl = ptr_type_node;
2499 else
2500 derived->backend_decl = pfunc_type_node;
2502 derived->ts.kind = gfc_index_integer_kind;
2503 derived->ts.type = BT_INTEGER;
2504 /* Set the f90_type to BT_VOID as a way to recognize something of type
2505 BT_INTEGER that needs to fit a void * for the purpose of the
2506 iso_c_binding derived types. */
2507 derived->ts.f90_type = BT_VOID;
2509 return derived->backend_decl;
2512 /* If use associated, use the module type for this one. */
2513 if (derived->backend_decl == NULL
2514 && derived->attr.use_assoc
2515 && derived->module
2516 && gfc_get_module_backend_decl (derived))
2517 goto copy_derived_types;
2519 /* The derived types from an earlier namespace can be used as the
2520 canonical type. */
2521 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2522 && gfc_global_ns_list)
2524 for (ns = gfc_global_ns_list;
2525 ns->translated && !got_canonical;
2526 ns = ns->sibling)
2528 dt = ns->derived_types;
2529 for (; dt && !canonical; dt = dt->next)
2531 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2532 if (derived->backend_decl)
2533 got_canonical = true;
2538 /* Store up the canonical type to be added to this one. */
2539 if (got_canonical)
2541 if (TYPE_CANONICAL (derived->backend_decl))
2542 canonical = TYPE_CANONICAL (derived->backend_decl);
2543 else
2544 canonical = derived->backend_decl;
2546 derived->backend_decl = NULL_TREE;
2549 /* derived->backend_decl != 0 means we saw it before, but its
2550 components' backend_decl may have not been built. */
2551 if (derived->backend_decl)
2553 /* Its components' backend_decl have been built or we are
2554 seeing recursion through the formal arglist of a procedure
2555 pointer component. */
2556 if (TYPE_FIELDS (derived->backend_decl))
2557 return derived->backend_decl;
2558 else if (derived->attr.abstract
2559 && derived->attr.proc_pointer_comp)
2561 /* If an abstract derived type with procedure pointer
2562 components has no other type of component, return the
2563 backend_decl. Otherwise build the components if any of the
2564 non-procedure pointer components have no backend_decl. */
2565 for (c = derived->components; c; c = c->next)
2567 bool same_alloc_type = c->attr.allocatable
2568 && derived == c->ts.u.derived;
2569 if (!c->attr.proc_pointer
2570 && !same_alloc_type
2571 && c->backend_decl == NULL)
2572 break;
2573 else if (c->next == NULL)
2574 return derived->backend_decl;
2576 typenode = derived->backend_decl;
2578 else
2579 typenode = derived->backend_decl;
2581 else
2583 /* We see this derived type first time, so build the type node. */
2584 typenode = make_node (RECORD_TYPE);
2585 TYPE_NAME (typenode) = get_identifier (derived->name);
2586 TYPE_PACKED (typenode) = flag_pack_derived;
2587 derived->backend_decl = typenode;
2590 if (derived->components
2591 && derived->components->ts.type == BT_DERIVED
2592 && strcmp (derived->components->name, "_data") == 0
2593 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2594 unlimited_entity = true;
2596 /* Go through the derived type components, building them as
2597 necessary. The reason for doing this now is that it is
2598 possible to recurse back to this derived type through a
2599 pointer component (PR24092). If this happens, the fields
2600 will be built and so we can return the type. */
2601 for (c = derived->components; c; c = c->next)
2603 bool same_alloc_type = c->attr.allocatable
2604 && derived == c->ts.u.derived;
2606 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2607 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2609 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2610 continue;
2612 if ((!c->attr.pointer && !c->attr.proc_pointer
2613 && !same_alloc_type)
2614 || c->ts.u.derived->backend_decl == NULL)
2616 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2617 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2618 local_codim);
2621 if (c->ts.u.derived->attr.is_iso_c)
2623 /* Need to copy the modified ts from the derived type. The
2624 typespec was modified because C_PTR/C_FUNPTR are translated
2625 into (void *) from derived types. */
2626 c->ts.type = c->ts.u.derived->ts.type;
2627 c->ts.kind = c->ts.u.derived->ts.kind;
2628 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2629 if (c->initializer)
2631 c->initializer->ts.type = c->ts.type;
2632 c->initializer->ts.kind = c->ts.kind;
2633 c->initializer->ts.f90_type = c->ts.f90_type;
2634 c->initializer->expr_type = EXPR_NULL;
2639 if (TYPE_FIELDS (derived->backend_decl))
2640 return derived->backend_decl;
2642 /* Build the type member list. Install the newly created RECORD_TYPE
2643 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2644 through only the top-level linked list of components so we correctly
2645 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2646 types are built as part of gfc_get_union_type. */
2647 for (c = derived->components; c; c = c->next)
2649 bool same_alloc_type = c->attr.allocatable
2650 && derived == c->ts.u.derived;
2651 /* Prevent infinite recursion, when the procedure pointer type is
2652 the same as derived, by forcing the procedure pointer component to
2653 be built as if the explicit interface does not exist. */
2654 if (c->attr.proc_pointer
2655 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2656 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2657 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2658 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2659 field_type = gfc_get_ppc_type (c);
2660 else if (c->attr.proc_pointer && derived->backend_decl)
2662 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2663 field_type = build_pointer_type (tmp);
2665 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2666 field_type = c->ts.u.derived->backend_decl;
2667 else
2669 if (c->ts.type == BT_CHARACTER
2670 && !c->ts.deferred && !c->attr.pdt_string)
2672 /* Evaluate the string length. */
2673 gfc_conv_const_charlen (c->ts.u.cl);
2674 gcc_assert (c->ts.u.cl->backend_decl);
2676 else if (c->ts.type == BT_CHARACTER)
2677 c->ts.u.cl->backend_decl
2678 = build_int_cst (gfc_charlen_type_node, 0);
2680 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2683 /* This returns an array descriptor type. Initialization may be
2684 required. */
2685 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2687 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2689 enum gfc_array_kind akind;
2690 if (c->attr.pointer)
2691 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2692 : GFC_ARRAY_POINTER;
2693 else
2694 akind = GFC_ARRAY_ALLOCATABLE;
2695 /* Pointers to arrays aren't actually pointer types. The
2696 descriptors are separate, but the data is common. */
2697 field_type = gfc_build_array_type (field_type, c->as, akind,
2698 !c->attr.target
2699 && !c->attr.pointer,
2700 c->attr.contiguous,
2701 codimen);
2703 else
2704 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2705 PACKED_STATIC,
2706 !c->attr.target);
2708 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2709 && !c->attr.proc_pointer
2710 && !(unlimited_entity && c == derived->components))
2711 field_type = build_pointer_type (field_type);
2713 if (c->attr.pointer || same_alloc_type)
2714 field_type = gfc_nonrestricted_type (field_type);
2716 /* vtype fields can point to different types to the base type. */
2717 if (c->ts.type == BT_DERIVED
2718 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2719 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2720 ptr_mode, true);
2722 /* Ensure that the CLASS language specific flag is set. */
2723 if (c->ts.type == BT_CLASS)
2725 if (POINTER_TYPE_P (field_type))
2726 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2727 else
2728 GFC_CLASS_TYPE_P (field_type) = 1;
2731 field = gfc_add_field_to_struct (typenode,
2732 get_identifier (c->name),
2733 field_type, &chain);
2734 if (c->loc.lb)
2735 gfc_set_decl_location (field, &c->loc);
2736 else if (derived->declared_at.lb)
2737 gfc_set_decl_location (field, &derived->declared_at);
2739 gfc_finish_decl_attrs (field, &c->attr);
2741 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2743 gcc_assert (field);
2744 if (!c->backend_decl)
2745 c->backend_decl = field;
2747 if (c->attr.pointer && c->attr.dimension
2748 && !(c->ts.type == BT_DERIVED
2749 && strcmp (c->name, "_data") == 0))
2750 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2752 /* Do not add a caf_token field for classes' data components. */
2753 if (codimen && !c->attr.dimension && !c->attr.codimension
2754 && (c->attr.allocatable || c->attr.pointer)
2755 && c->caf_token == NULL_TREE && strcmp ("_data", c->name) != 0)
2757 char caf_name[GFC_MAX_SYMBOL_LEN];
2758 snprintf (caf_name, GFC_MAX_SYMBOL_LEN, "_caf_%s", c->name);
2759 c->caf_token = gfc_add_field_to_struct (typenode,
2760 get_identifier (caf_name),
2761 pvoid_type_node, &chain);
2762 TREE_NO_WARNING (c->caf_token) = 1;
2766 /* Now lay out the derived type, including the fields. */
2767 if (canonical)
2768 TYPE_CANONICAL (typenode) = canonical;
2770 gfc_finish_type (typenode);
2771 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2772 if (derived->module && derived->ns->proc_name
2773 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2775 if (derived->ns->proc_name->backend_decl
2776 && TREE_CODE (derived->ns->proc_name->backend_decl)
2777 == NAMESPACE_DECL)
2779 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2780 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2781 = derived->ns->proc_name->backend_decl;
2785 derived->backend_decl = typenode;
2787 copy_derived_types:
2789 for (dt = gfc_derived_types; dt; dt = dt->next)
2790 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2792 return derived->backend_decl;
2797 gfc_return_by_reference (gfc_symbol * sym)
2799 if (!sym->attr.function)
2800 return 0;
2802 if (sym->attr.dimension)
2803 return 1;
2805 if (sym->ts.type == BT_CHARACTER
2806 && !sym->attr.is_bind_c
2807 && (!sym->attr.result
2808 || !sym->ns->proc_name
2809 || !sym->ns->proc_name->attr.is_bind_c))
2810 return 1;
2812 /* Possibly return complex numbers by reference for g77 compatibility.
2813 We don't do this for calls to intrinsics (as the library uses the
2814 -fno-f2c calling convention), nor for calls to functions which always
2815 require an explicit interface, as no compatibility problems can
2816 arise there. */
2817 if (flag_f2c && sym->ts.type == BT_COMPLEX
2818 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2819 return 1;
2821 return 0;
2824 static tree
2825 gfc_get_mixed_entry_union (gfc_namespace *ns)
2827 tree type;
2828 tree *chain = NULL;
2829 char name[GFC_MAX_SYMBOL_LEN + 1];
2830 gfc_entry_list *el, *el2;
2832 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2833 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2835 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2837 /* Build the type node. */
2838 type = make_node (UNION_TYPE);
2840 TYPE_NAME (type) = get_identifier (name);
2842 for (el = ns->entries; el; el = el->next)
2844 /* Search for duplicates. */
2845 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2846 if (el2->sym->result == el->sym->result)
2847 break;
2849 if (el == el2)
2850 gfc_add_field_to_struct_1 (type,
2851 get_identifier (el->sym->result->name),
2852 gfc_sym_type (el->sym->result), &chain);
2855 /* Finish off the type. */
2856 gfc_finish_type (type);
2857 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2858 return type;
2861 /* Create a "fn spec" based on the formal arguments;
2862 cf. create_function_arglist. */
2864 static tree
2865 create_fn_spec (gfc_symbol *sym, tree fntype)
2867 char spec[150];
2868 size_t spec_len;
2869 gfc_formal_arglist *f;
2870 tree tmp;
2872 memset (&spec, 0, sizeof (spec));
2873 spec[0] = '.';
2874 spec_len = 1;
2876 if (sym->attr.entry_master)
2877 spec[spec_len++] = 'R';
2878 if (gfc_return_by_reference (sym))
2880 gfc_symbol *result = sym->result ? sym->result : sym;
2882 if (result->attr.pointer || sym->attr.proc_pointer)
2883 spec[spec_len++] = '.';
2884 else
2885 spec[spec_len++] = 'w';
2886 if (sym->ts.type == BT_CHARACTER)
2887 spec[spec_len++] = 'R';
2890 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2891 if (spec_len < sizeof (spec))
2893 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2894 || f->sym->attr.external || f->sym->attr.cray_pointer
2895 || (f->sym->ts.type == BT_DERIVED
2896 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2897 || f->sym->ts.u.derived->attr.pointer_comp))
2898 || (f->sym->ts.type == BT_CLASS
2899 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2900 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2901 spec[spec_len++] = '.';
2902 else if (f->sym->attr.intent == INTENT_IN)
2903 spec[spec_len++] = 'r';
2904 else if (f->sym)
2905 spec[spec_len++] = 'w';
2908 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2909 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2910 return build_type_attribute_variant (fntype, tmp);
2914 tree
2915 gfc_get_function_type (gfc_symbol * sym)
2917 tree type;
2918 vec<tree, va_gc> *typelist = NULL;
2919 gfc_formal_arglist *f;
2920 gfc_symbol *arg;
2921 int alternate_return = 0;
2922 bool is_varargs = true;
2924 /* Make sure this symbol is a function, a subroutine or the main
2925 program. */
2926 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2927 || sym->attr.flavor == FL_PROGRAM);
2929 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2930 so that they can be detected here and handled further down. */
2931 if (sym->backend_decl == NULL)
2932 sym->backend_decl = error_mark_node;
2933 else if (sym->backend_decl == error_mark_node)
2934 goto arg_type_list_done;
2935 else if (sym->attr.proc_pointer)
2936 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2937 else
2938 return TREE_TYPE (sym->backend_decl);
2940 if (sym->attr.entry_master)
2941 /* Additional parameter for selecting an entry point. */
2942 vec_safe_push (typelist, gfc_array_index_type);
2944 if (sym->result)
2945 arg = sym->result;
2946 else
2947 arg = sym;
2949 if (arg->ts.type == BT_CHARACTER)
2950 gfc_conv_const_charlen (arg->ts.u.cl);
2952 /* Some functions we use an extra parameter for the return value. */
2953 if (gfc_return_by_reference (sym))
2955 type = gfc_sym_type (arg);
2956 if (arg->ts.type == BT_COMPLEX
2957 || arg->attr.dimension
2958 || arg->ts.type == BT_CHARACTER)
2959 type = build_reference_type (type);
2961 vec_safe_push (typelist, type);
2962 if (arg->ts.type == BT_CHARACTER)
2964 if (!arg->ts.deferred)
2965 /* Transfer by value. */
2966 vec_safe_push (typelist, gfc_charlen_type_node);
2967 else
2968 /* Deferred character lengths are transferred by reference
2969 so that the value can be returned. */
2970 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2974 /* Build the argument types for the function. */
2975 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2977 arg = f->sym;
2978 if (arg)
2980 /* Evaluate constant character lengths here so that they can be
2981 included in the type. */
2982 if (arg->ts.type == BT_CHARACTER)
2983 gfc_conv_const_charlen (arg->ts.u.cl);
2985 if (arg->attr.flavor == FL_PROCEDURE)
2987 type = gfc_get_function_type (arg);
2988 type = build_pointer_type (type);
2990 else
2991 type = gfc_sym_type (arg);
2993 /* Parameter Passing Convention
2995 We currently pass all parameters by reference.
2996 Parameters with INTENT(IN) could be passed by value.
2997 The problem arises if a function is called via an implicit
2998 prototype. In this situation the INTENT is not known.
2999 For this reason all parameters to global functions must be
3000 passed by reference. Passing by value would potentially
3001 generate bad code. Worse there would be no way of telling that
3002 this code was bad, except that it would give incorrect results.
3004 Contained procedures could pass by value as these are never
3005 used without an explicit interface, and cannot be passed as
3006 actual parameters for a dummy procedure. */
3008 vec_safe_push (typelist, type);
3010 else
3012 if (sym->attr.subroutine)
3013 alternate_return = 1;
3017 /* Add hidden string length parameters. */
3018 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
3020 arg = f->sym;
3021 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3023 if (!arg->ts.deferred)
3024 /* Transfer by value. */
3025 type = gfc_charlen_type_node;
3026 else
3027 /* Deferred character lengths are transferred by reference
3028 so that the value can be returned. */
3029 type = build_pointer_type (gfc_charlen_type_node);
3031 vec_safe_push (typelist, type);
3035 if (!vec_safe_is_empty (typelist)
3036 || sym->attr.is_main_program
3037 || sym->attr.if_source != IFSRC_UNKNOWN)
3038 is_varargs = false;
3040 if (sym->backend_decl == error_mark_node)
3041 sym->backend_decl = NULL_TREE;
3043 arg_type_list_done:
3045 if (alternate_return)
3046 type = integer_type_node;
3047 else if (!sym->attr.function || gfc_return_by_reference (sym))
3048 type = void_type_node;
3049 else if (sym->attr.mixed_entry_master)
3050 type = gfc_get_mixed_entry_union (sym->ns);
3051 else if (flag_f2c && sym->ts.type == BT_REAL
3052 && sym->ts.kind == gfc_default_real_kind
3053 && !sym->attr.always_explicit)
3055 /* Special case: f2c calling conventions require that (scalar)
3056 default REAL functions return the C type double instead. f2c
3057 compatibility is only an issue with functions that don't
3058 require an explicit interface, as only these could be
3059 implemented in Fortran 77. */
3060 sym->ts.kind = gfc_default_double_kind;
3061 type = gfc_typenode_for_spec (&sym->ts);
3062 sym->ts.kind = gfc_default_real_kind;
3064 else if (sym->result && sym->result->attr.proc_pointer)
3065 /* Procedure pointer return values. */
3067 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3069 /* Unset proc_pointer as gfc_get_function_type
3070 is called recursively. */
3071 sym->result->attr.proc_pointer = 0;
3072 type = build_pointer_type (gfc_get_function_type (sym->result));
3073 sym->result->attr.proc_pointer = 1;
3075 else
3076 type = gfc_sym_type (sym->result);
3078 else
3079 type = gfc_sym_type (sym);
3081 if (is_varargs)
3082 type = build_varargs_function_type_vec (type, typelist);
3083 else
3084 type = build_function_type_vec (type, typelist);
3085 type = create_fn_spec (sym, type);
3087 return type;
3090 /* Language hooks for middle-end access to type nodes. */
3092 /* Return an integer type with BITS bits of precision,
3093 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3095 tree
3096 gfc_type_for_size (unsigned bits, int unsignedp)
3098 if (!unsignedp)
3100 int i;
3101 for (i = 0; i <= MAX_INT_KINDS; ++i)
3103 tree type = gfc_integer_types[i];
3104 if (type && bits == TYPE_PRECISION (type))
3105 return type;
3108 /* Handle TImode as a special case because it is used by some backends
3109 (e.g. ARM) even though it is not available for normal use. */
3110 #if HOST_BITS_PER_WIDE_INT >= 64
3111 if (bits == TYPE_PRECISION (intTI_type_node))
3112 return intTI_type_node;
3113 #endif
3115 if (bits <= TYPE_PRECISION (intQI_type_node))
3116 return intQI_type_node;
3117 if (bits <= TYPE_PRECISION (intHI_type_node))
3118 return intHI_type_node;
3119 if (bits <= TYPE_PRECISION (intSI_type_node))
3120 return intSI_type_node;
3121 if (bits <= TYPE_PRECISION (intDI_type_node))
3122 return intDI_type_node;
3123 if (bits <= TYPE_PRECISION (intTI_type_node))
3124 return intTI_type_node;
3126 else
3128 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3129 return unsigned_intQI_type_node;
3130 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3131 return unsigned_intHI_type_node;
3132 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3133 return unsigned_intSI_type_node;
3134 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3135 return unsigned_intDI_type_node;
3136 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3137 return unsigned_intTI_type_node;
3140 return NULL_TREE;
3143 /* Return a data type that has machine mode MODE. If the mode is an
3144 integer, then UNSIGNEDP selects between signed and unsigned types. */
3146 tree
3147 gfc_type_for_mode (machine_mode mode, int unsignedp)
3149 int i;
3150 tree *base;
3151 scalar_int_mode int_mode;
3153 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3154 base = gfc_real_types;
3155 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3156 base = gfc_complex_types;
3157 else if (is_a <scalar_int_mode> (mode, &int_mode))
3159 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3160 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3162 else if (VECTOR_MODE_P (mode))
3164 machine_mode inner_mode = GET_MODE_INNER (mode);
3165 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3166 if (inner_type != NULL_TREE)
3167 return build_vector_type_for_mode (inner_type, mode);
3168 return NULL_TREE;
3170 else
3171 return NULL_TREE;
3173 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3175 tree type = base[i];
3176 if (type && mode == TYPE_MODE (type))
3177 return type;
3180 return NULL_TREE;
3183 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3184 in that case. */
3186 bool
3187 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3189 int rank, dim;
3190 bool indirect = false;
3191 tree etype, ptype, t, base_decl;
3192 tree data_off, dim_off, dtype_off, dim_size, elem_size;
3193 tree lower_suboff, upper_suboff, stride_suboff;
3195 if (! GFC_DESCRIPTOR_TYPE_P (type))
3197 if (! POINTER_TYPE_P (type))
3198 return false;
3199 type = TREE_TYPE (type);
3200 if (! GFC_DESCRIPTOR_TYPE_P (type))
3201 return false;
3202 indirect = true;
3205 rank = GFC_TYPE_ARRAY_RANK (type);
3206 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3207 return false;
3209 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3210 gcc_assert (POINTER_TYPE_P (etype));
3211 etype = TREE_TYPE (etype);
3213 /* If the type is not a scalar coarray. */
3214 if (TREE_CODE (etype) == ARRAY_TYPE)
3215 etype = TREE_TYPE (etype);
3217 /* Can't handle variable sized elements yet. */
3218 if (int_size_in_bytes (etype) <= 0)
3219 return false;
3220 /* Nor non-constant lower bounds in assumed shape arrays. */
3221 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3222 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3224 for (dim = 0; dim < rank; dim++)
3225 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3226 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3227 return false;
3230 memset (info, '\0', sizeof (*info));
3231 info->ndimensions = rank;
3232 info->ordering = array_descr_ordering_column_major;
3233 info->element_type = etype;
3234 ptype = build_pointer_type (gfc_array_index_type);
3235 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3236 if (!base_decl)
3238 base_decl = make_node (DEBUG_EXPR_DECL);
3239 DECL_ARTIFICIAL (base_decl) = 1;
3240 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3241 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3242 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3244 info->base_decl = base_decl;
3245 if (indirect)
3246 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3248 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3250 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &dim_off,
3251 &dim_size, &stride_suboff,
3252 &lower_suboff, &upper_suboff);
3254 t = base_decl;
3255 if (!integer_zerop (data_off))
3256 t = fold_build_pointer_plus (t, data_off);
3257 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3258 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3259 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3260 info->allocated = build2 (NE_EXPR, boolean_type_node,
3261 info->data_location, null_pointer_node);
3262 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3263 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3264 info->associated = build2 (NE_EXPR, boolean_type_node,
3265 info->data_location, null_pointer_node);
3266 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3267 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3268 && dwarf_version >= 5)
3270 rank = 1;
3271 info->ndimensions = 1;
3272 t = base_decl;
3273 if (!integer_zerop (dtype_off))
3274 t = fold_build_pointer_plus (t, dtype_off);
3275 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3276 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3277 info->rank = build2 (BIT_AND_EXPR, gfc_array_index_type, t,
3278 build_int_cst (gfc_array_index_type,
3279 GFC_DTYPE_RANK_MASK));
3280 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3281 t = size_binop (MULT_EXPR, t, dim_size);
3282 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3285 for (dim = 0; dim < rank; dim++)
3287 t = fold_build_pointer_plus (base_decl,
3288 size_binop (PLUS_EXPR,
3289 dim_off, lower_suboff));
3290 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3291 info->dimen[dim].lower_bound = t;
3292 t = fold_build_pointer_plus (base_decl,
3293 size_binop (PLUS_EXPR,
3294 dim_off, upper_suboff));
3295 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3296 info->dimen[dim].upper_bound = t;
3297 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3298 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3300 /* Assumed shape arrays have known lower bounds. */
3301 info->dimen[dim].upper_bound
3302 = build2 (MINUS_EXPR, gfc_array_index_type,
3303 info->dimen[dim].upper_bound,
3304 info->dimen[dim].lower_bound);
3305 info->dimen[dim].lower_bound
3306 = fold_convert (gfc_array_index_type,
3307 GFC_TYPE_ARRAY_LBOUND (type, dim));
3308 info->dimen[dim].upper_bound
3309 = build2 (PLUS_EXPR, gfc_array_index_type,
3310 info->dimen[dim].lower_bound,
3311 info->dimen[dim].upper_bound);
3313 t = fold_build_pointer_plus (base_decl,
3314 size_binop (PLUS_EXPR,
3315 dim_off, stride_suboff));
3316 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3317 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3318 info->dimen[dim].stride = t;
3319 if (dim + 1 < rank)
3320 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3323 return true;
3327 /* Create a type to handle vector subscripts for coarray library calls. It
3328 has the form:
3329 struct caf_vector_t {
3330 size_t nvec; // size of the vector
3331 union {
3332 struct {
3333 void *vector;
3334 int kind;
3335 } v;
3336 struct {
3337 ptrdiff_t lower_bound;
3338 ptrdiff_t upper_bound;
3339 ptrdiff_t stride;
3340 } triplet;
3341 } u;
3343 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3344 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3346 tree
3347 gfc_get_caf_vector_type (int dim)
3349 static tree vector_types[GFC_MAX_DIMENSIONS];
3350 static tree vec_type = NULL_TREE;
3351 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3353 if (vector_types[dim-1] != NULL_TREE)
3354 return vector_types[dim-1];
3356 if (vec_type == NULL_TREE)
3358 chain = 0;
3359 vect_struct_type = make_node (RECORD_TYPE);
3360 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3361 get_identifier ("vector"),
3362 pvoid_type_node, &chain);
3363 TREE_NO_WARNING (tmp) = 1;
3364 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3365 get_identifier ("kind"),
3366 integer_type_node, &chain);
3367 TREE_NO_WARNING (tmp) = 1;
3368 gfc_finish_type (vect_struct_type);
3370 chain = 0;
3371 triplet_struct_type = make_node (RECORD_TYPE);
3372 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3373 get_identifier ("lower_bound"),
3374 gfc_array_index_type, &chain);
3375 TREE_NO_WARNING (tmp) = 1;
3376 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3377 get_identifier ("upper_bound"),
3378 gfc_array_index_type, &chain);
3379 TREE_NO_WARNING (tmp) = 1;
3380 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3381 gfc_array_index_type, &chain);
3382 TREE_NO_WARNING (tmp) = 1;
3383 gfc_finish_type (triplet_struct_type);
3385 chain = 0;
3386 union_type = make_node (UNION_TYPE);
3387 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3388 vect_struct_type, &chain);
3389 TREE_NO_WARNING (tmp) = 1;
3390 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3391 triplet_struct_type, &chain);
3392 TREE_NO_WARNING (tmp) = 1;
3393 gfc_finish_type (union_type);
3395 chain = 0;
3396 vec_type = make_node (RECORD_TYPE);
3397 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3398 size_type_node, &chain);
3399 TREE_NO_WARNING (tmp) = 1;
3400 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3401 union_type, &chain);
3402 TREE_NO_WARNING (tmp) = 1;
3403 gfc_finish_type (vec_type);
3404 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3407 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3408 gfc_rank_cst[dim-1]);
3409 vector_types[dim-1] = build_array_type (vec_type, tmp);
3410 return vector_types[dim-1];
3414 tree
3415 gfc_get_caf_reference_type ()
3417 static tree reference_type = NULL_TREE;
3418 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3419 a_struct_type, u_union_type, tmp, *chain;
3421 if (reference_type != NULL_TREE)
3422 return reference_type;
3424 chain = 0;
3425 c_struct_type = make_node (RECORD_TYPE);
3426 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3427 get_identifier ("offset"),
3428 gfc_array_index_type, &chain);
3429 TREE_NO_WARNING (tmp) = 1;
3430 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3431 get_identifier ("caf_token_offset"),
3432 gfc_array_index_type, &chain);
3433 TREE_NO_WARNING (tmp) = 1;
3434 gfc_finish_type (c_struct_type);
3436 chain = 0;
3437 s_struct_type = make_node (RECORD_TYPE);
3438 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3439 get_identifier ("start"),
3440 gfc_array_index_type, &chain);
3441 TREE_NO_WARNING (tmp) = 1;
3442 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3443 get_identifier ("end"),
3444 gfc_array_index_type, &chain);
3445 TREE_NO_WARNING (tmp) = 1;
3446 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3447 get_identifier ("stride"),
3448 gfc_array_index_type, &chain);
3449 TREE_NO_WARNING (tmp) = 1;
3450 gfc_finish_type (s_struct_type);
3452 chain = 0;
3453 v_struct_type = make_node (RECORD_TYPE);
3454 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3455 get_identifier ("vector"),
3456 pvoid_type_node, &chain);
3457 TREE_NO_WARNING (tmp) = 1;
3458 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3459 get_identifier ("nvec"),
3460 size_type_node, &chain);
3461 TREE_NO_WARNING (tmp) = 1;
3462 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3463 get_identifier ("kind"),
3464 integer_type_node, &chain);
3465 TREE_NO_WARNING (tmp) = 1;
3466 gfc_finish_type (v_struct_type);
3468 chain = 0;
3469 union_type = make_node (UNION_TYPE);
3470 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3471 s_struct_type, &chain);
3472 TREE_NO_WARNING (tmp) = 1;
3473 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3474 v_struct_type, &chain);
3475 TREE_NO_WARNING (tmp) = 1;
3476 gfc_finish_type (union_type);
3478 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3479 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3480 dim_union_type = build_array_type (union_type, tmp);
3482 chain = 0;
3483 a_struct_type = make_node (RECORD_TYPE);
3484 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3485 build_array_type (unsigned_char_type_node,
3486 build_range_type (gfc_array_index_type,
3487 gfc_index_zero_node,
3488 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3489 &chain);
3490 TREE_NO_WARNING (tmp) = 1;
3491 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3492 get_identifier ("static_array_type"),
3493 integer_type_node, &chain);
3494 TREE_NO_WARNING (tmp) = 1;
3495 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3496 dim_union_type, &chain);
3497 TREE_NO_WARNING (tmp) = 1;
3498 gfc_finish_type (a_struct_type);
3500 chain = 0;
3501 u_union_type = make_node (UNION_TYPE);
3502 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3503 c_struct_type, &chain);
3504 TREE_NO_WARNING (tmp) = 1;
3505 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3506 a_struct_type, &chain);
3507 TREE_NO_WARNING (tmp) = 1;
3508 gfc_finish_type (u_union_type);
3510 chain = 0;
3511 reference_type = make_node (RECORD_TYPE);
3512 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3513 build_pointer_type (reference_type), &chain);
3514 TREE_NO_WARNING (tmp) = 1;
3515 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3516 integer_type_node, &chain);
3517 TREE_NO_WARNING (tmp) = 1;
3518 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3519 size_type_node, &chain);
3520 TREE_NO_WARNING (tmp) = 1;
3521 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3522 u_union_type, &chain);
3523 TREE_NO_WARNING (tmp) = 1;
3524 gfc_finish_type (reference_type);
3525 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3527 return reference_type;
3530 #include "gt-fortran-trans-types.h"