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