runtime: complete defer handling in CgocallBackDone
[official-gcc.git] / gcc / fortran / trans-types.c
blob8617cd51a7c6f57c3ae138fe401fd5efa4ac3124
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 "dwarf2out.h" /* For struct array_descr_info. */
41 #if (GFC_MAX_DIMENSIONS < 10)
42 #define GFC_RANK_DIGITS 1
43 #define GFC_RANK_PRINTF_FORMAT "%01d"
44 #elif (GFC_MAX_DIMENSIONS < 100)
45 #define GFC_RANK_DIGITS 2
46 #define GFC_RANK_PRINTF_FORMAT "%02d"
47 #else
48 #error If you really need >99 dimensions, continue the sequence above...
49 #endif
51 /* array of structs so we don't have to worry about xmalloc or free */
52 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
54 tree gfc_array_index_type;
55 tree gfc_array_range_type;
56 tree gfc_character1_type_node;
57 tree pvoid_type_node;
58 tree prvoid_type_node;
59 tree ppvoid_type_node;
60 tree pchar_type_node;
61 tree pfunc_type_node;
63 tree gfc_charlen_type_node;
65 tree gfc_float128_type_node = NULL_TREE;
66 tree gfc_complex_float128_type_node = NULL_TREE;
68 bool gfc_real16_is_float128 = false;
70 static GTY(()) tree gfc_desc_dim_type;
71 static GTY(()) tree gfc_max_array_element_size;
72 static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
73 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
75 /* Arrays for all integral and real kinds. We'll fill this in at runtime
76 after the target has a chance to process command-line options. */
78 #define MAX_INT_KINDS 5
79 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
80 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
81 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
82 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
84 #define MAX_REAL_KINDS 5
85 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
86 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
87 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
89 #define MAX_CHARACTER_KINDS 2
90 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
91 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
92 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
94 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
96 /* The integer kind to use for array indices. This will be set to the
97 proper value based on target information from the backend. */
99 int gfc_index_integer_kind;
101 /* The default kinds of the various types. */
103 int gfc_default_integer_kind;
104 int gfc_max_integer_kind;
105 int gfc_default_real_kind;
106 int gfc_default_double_kind;
107 int gfc_default_character_kind;
108 int gfc_default_logical_kind;
109 int gfc_default_complex_kind;
110 int gfc_c_int_kind;
111 int gfc_atomic_int_kind;
112 int gfc_atomic_logical_kind;
114 /* The kind size used for record offsets. If the target system supports
115 kind=8, this will be set to 8, otherwise it is set to 4. */
116 int gfc_intio_kind;
118 /* The integer kind used to store character lengths. */
119 int gfc_charlen_int_kind;
121 /* The size of the numeric storage unit and character storage unit. */
122 int gfc_numeric_storage_size;
123 int gfc_character_storage_size;
126 bool
127 gfc_check_any_c_kind (gfc_typespec *ts)
129 int i;
131 for (i = 0; i < ISOCBINDING_NUMBER; i++)
133 /* Check for any C interoperable kind for the given type/kind in ts.
134 This can be used after verify_c_interop to make sure that the
135 Fortran kind being used exists in at least some form for C. */
136 if (c_interop_kinds_table[i].f90_type == ts->type &&
137 c_interop_kinds_table[i].value == ts->kind)
138 return true;
141 return false;
145 static int
146 get_real_kind_from_node (tree type)
148 int i;
150 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
151 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
152 return gfc_real_kinds[i].kind;
154 return -4;
157 static int
158 get_int_kind_from_node (tree type)
160 int i;
162 if (!type)
163 return -2;
165 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
166 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
167 return gfc_integer_kinds[i].kind;
169 return -1;
172 /* Return a typenode for the "standard" C type with a given name. */
173 static tree
174 get_typenode_from_name (const char *name)
176 if (name == NULL || *name == '\0')
177 return NULL_TREE;
179 if (strcmp (name, "char") == 0)
180 return char_type_node;
181 if (strcmp (name, "unsigned char") == 0)
182 return unsigned_char_type_node;
183 if (strcmp (name, "signed char") == 0)
184 return signed_char_type_node;
186 if (strcmp (name, "short int") == 0)
187 return short_integer_type_node;
188 if (strcmp (name, "short unsigned int") == 0)
189 return short_unsigned_type_node;
191 if (strcmp (name, "int") == 0)
192 return integer_type_node;
193 if (strcmp (name, "unsigned int") == 0)
194 return unsigned_type_node;
196 if (strcmp (name, "long int") == 0)
197 return long_integer_type_node;
198 if (strcmp (name, "long unsigned int") == 0)
199 return long_unsigned_type_node;
201 if (strcmp (name, "long long int") == 0)
202 return long_long_integer_type_node;
203 if (strcmp (name, "long long unsigned int") == 0)
204 return long_long_unsigned_type_node;
206 gcc_unreachable ();
209 static int
210 get_int_kind_from_name (const char *name)
212 return get_int_kind_from_node (get_typenode_from_name (name));
216 /* Get the kind number corresponding to an integer of given size,
217 following the required return values for ISO_FORTRAN_ENV INT* constants:
218 -2 is returned if we support a kind of larger size, -1 otherwise. */
220 gfc_get_int_kind_from_width_isofortranenv (int size)
222 int i;
224 /* Look for a kind with matching storage size. */
225 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
226 if (gfc_integer_kinds[i].bit_size == size)
227 return gfc_integer_kinds[i].kind;
229 /* Look for a kind with larger storage size. */
230 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
231 if (gfc_integer_kinds[i].bit_size > size)
232 return -2;
234 return -1;
238 /* Get the kind number corresponding to a real of a given storage size.
239 If two real's have the same storage size, then choose the real with
240 the largest precision. If a kind type is unavailable and a real
241 exists with wider storage, then return -2; otherwise, return -1. */
244 gfc_get_real_kind_from_width_isofortranenv (int size)
246 int digits, i, kind;
248 size /= 8;
250 kind = -1;
251 digits = 0;
253 /* Look for a kind with matching storage size. */
254 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
255 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
257 if (gfc_real_kinds[i].digits > digits)
259 digits = gfc_real_kinds[i].digits;
260 kind = gfc_real_kinds[i].kind;
264 if (kind != -1)
265 return kind;
267 /* Look for a kind with larger storage size. */
268 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
269 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
270 kind = -2;
272 return kind;
277 static int
278 get_int_kind_from_width (int size)
280 int i;
282 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
283 if (gfc_integer_kinds[i].bit_size == size)
284 return gfc_integer_kinds[i].kind;
286 return -2;
289 static int
290 get_int_kind_from_minimal_width (int size)
292 int i;
294 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
295 if (gfc_integer_kinds[i].bit_size >= size)
296 return gfc_integer_kinds[i].kind;
298 return -2;
302 /* Generate the CInteropKind_t objects for the C interoperable
303 kinds. */
305 void
306 gfc_init_c_interop_kinds (void)
308 int i;
310 /* init all pointers in the list to NULL */
311 for (i = 0; i < ISOCBINDING_NUMBER; i++)
313 /* Initialize the name and value fields. */
314 c_interop_kinds_table[i].name[0] = '\0';
315 c_interop_kinds_table[i].value = -100;
316 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
319 #define NAMED_INTCST(a,b,c,d) \
320 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
321 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
322 c_interop_kinds_table[a].value = c;
323 #define NAMED_REALCST(a,b,c,d) \
324 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
325 c_interop_kinds_table[a].f90_type = BT_REAL; \
326 c_interop_kinds_table[a].value = c;
327 #define NAMED_CMPXCST(a,b,c,d) \
328 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
329 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
330 c_interop_kinds_table[a].value = c;
331 #define NAMED_LOGCST(a,b,c) \
332 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
333 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
334 c_interop_kinds_table[a].value = c;
335 #define NAMED_CHARKNDCST(a,b,c) \
336 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
337 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
338 c_interop_kinds_table[a].value = c;
339 #define NAMED_CHARCST(a,b,c) \
340 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
341 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
342 c_interop_kinds_table[a].value = c;
343 #define DERIVED_TYPE(a,b,c) \
344 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
345 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
346 c_interop_kinds_table[a].value = c;
347 #define NAMED_FUNCTION(a,b,c,d) \
348 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
349 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
350 c_interop_kinds_table[a].value = c;
351 #define NAMED_SUBROUTINE(a,b,c,d) \
352 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
353 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
354 c_interop_kinds_table[a].value = c;
355 #include "iso-c-binding.def"
359 /* Query the target to determine which machine modes are available for
360 computation. Choose KIND numbers for them. */
362 void
363 gfc_init_kinds (void)
365 unsigned int mode;
366 int i_index, r_index, kind;
367 bool saw_i4 = false, saw_i8 = false;
368 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
370 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
372 int kind, bitsize;
374 if (!targetm.scalar_mode_supported_p ((machine_mode) mode))
375 continue;
377 /* The middle end doesn't support constants larger than 2*HWI.
378 Perhaps the target hook shouldn't have accepted these either,
379 but just to be safe... */
380 bitsize = GET_MODE_BITSIZE ((machine_mode) mode);
381 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
382 continue;
384 gcc_assert (i_index != MAX_INT_KINDS);
386 /* Let the kind equal the bit size divided by 8. This insulates the
387 programmer from the underlying byte size. */
388 kind = bitsize / 8;
390 if (kind == 4)
391 saw_i4 = true;
392 if (kind == 8)
393 saw_i8 = true;
395 gfc_integer_kinds[i_index].kind = kind;
396 gfc_integer_kinds[i_index].radix = 2;
397 gfc_integer_kinds[i_index].digits = bitsize - 1;
398 gfc_integer_kinds[i_index].bit_size = bitsize;
400 gfc_logical_kinds[i_index].kind = kind;
401 gfc_logical_kinds[i_index].bit_size = bitsize;
403 i_index += 1;
406 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
407 used for large file access. */
409 if (saw_i8)
410 gfc_intio_kind = 8;
411 else
412 gfc_intio_kind = 4;
414 /* If we do not at least have kind = 4, everything is pointless. */
415 gcc_assert(saw_i4);
417 /* Set the maximum integer kind. Used with at least BOZ constants. */
418 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
420 for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
422 const struct real_format *fmt =
423 REAL_MODE_FORMAT ((machine_mode) mode);
424 int kind;
426 if (fmt == NULL)
427 continue;
428 if (!targetm.scalar_mode_supported_p ((machine_mode) mode))
429 continue;
431 /* Only let float, double, long double and __float128 go through.
432 Runtime support for others is not provided, so they would be
433 useless. */
434 if (!targetm.libgcc_floating_mode_supported_p ((machine_mode)
435 mode))
436 continue;
437 if (mode != TYPE_MODE (float_type_node)
438 && (mode != TYPE_MODE (double_type_node))
439 && (mode != TYPE_MODE (long_double_type_node))
440 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
441 && (mode != TFmode)
442 #endif
444 continue;
446 /* Let the kind equal the precision divided by 8, rounding up. Again,
447 this insulates the programmer from the underlying byte size.
449 Also, it effectively deals with IEEE extended formats. There, the
450 total size of the type may equal 16, but it's got 6 bytes of padding
451 and the increased size can get in the way of a real IEEE quad format
452 which may also be supported by the target.
454 We round up so as to handle IA-64 __floatreg (RFmode), which is an
455 82 bit type. Not to be confused with __float80 (XFmode), which is
456 an 80 bit type also supported by IA-64. So XFmode should come out
457 to be kind=10, and RFmode should come out to be kind=11. Egads. */
459 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
461 if (kind == 4)
462 saw_r4 = true;
463 if (kind == 8)
464 saw_r8 = true;
465 if (kind == 10)
466 saw_r10 = true;
467 if (kind == 16)
468 saw_r16 = true;
470 /* Careful we don't stumble a weird internal mode. */
471 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
472 /* Or have too many modes for the allocated space. */
473 gcc_assert (r_index != MAX_REAL_KINDS);
475 gfc_real_kinds[r_index].kind = kind;
476 gfc_real_kinds[r_index].radix = fmt->b;
477 gfc_real_kinds[r_index].digits = fmt->p;
478 gfc_real_kinds[r_index].min_exponent = fmt->emin;
479 gfc_real_kinds[r_index].max_exponent = fmt->emax;
480 if (fmt->pnan < fmt->p)
481 /* This is an IBM extended double format (or the MIPS variant)
482 made up of two IEEE doubles. The value of the long double is
483 the sum of the values of the two parts. The most significant
484 part is required to be the value of the long double rounded
485 to the nearest double. If we use emax of 1024 then we can't
486 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
487 rounding will make the most significant part overflow. */
488 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
489 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
490 r_index += 1;
493 /* Choose the default integer kind. We choose 4 unless the user directs us
494 otherwise. Even if the user specified that the default integer kind is 8,
495 the numeric storage size is not 64 bits. In this case, a warning will be
496 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
498 gfc_numeric_storage_size = 4 * 8;
500 if (flag_default_integer)
502 if (!saw_i8)
503 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
504 "%<-fdefault-integer-8%> option");
506 gfc_default_integer_kind = 8;
509 else if (flag_integer4_kind == 8)
511 if (!saw_i8)
512 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
513 "%<-finteger-4-integer-8%> option");
515 gfc_default_integer_kind = 8;
517 else if (saw_i4)
519 gfc_default_integer_kind = 4;
521 else
523 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
524 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
527 /* Choose the default real kind. Again, we choose 4 when possible. */
528 if (flag_default_real)
530 if (!saw_r8)
531 gfc_fatal_error ("REAL(KIND=8) is not available for "
532 "%<-fdefault-real-8%> option");
534 gfc_default_real_kind = 8;
536 else if (flag_real4_kind == 8)
538 if (!saw_r8)
539 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
540 "option");
542 gfc_default_real_kind = 8;
544 else if (flag_real4_kind == 10)
546 if (!saw_r10)
547 gfc_fatal_error ("REAL(KIND=10) is not available for "
548 "%<-freal-4-real-10%> option");
550 gfc_default_real_kind = 10;
552 else if (flag_real4_kind == 16)
554 if (!saw_r16)
555 gfc_fatal_error ("REAL(KIND=16) is not available for "
556 "%<-freal-4-real-16%> option");
558 gfc_default_real_kind = 16;
560 else if (saw_r4)
561 gfc_default_real_kind = 4;
562 else
563 gfc_default_real_kind = gfc_real_kinds[0].kind;
565 /* Choose the default double kind. If -fdefault-real and -fdefault-double
566 are specified, we use kind=8, if it's available. If -fdefault-real is
567 specified without -fdefault-double, we use kind=16, if it's available.
568 Otherwise we do not change anything. */
569 if (flag_default_double && !flag_default_real)
570 gfc_fatal_error ("Use of %<-fdefault-double-8%> requires "
571 "%<-fdefault-real-8%>");
573 if (flag_default_real && flag_default_double && saw_r8)
574 gfc_default_double_kind = 8;
575 else if (flag_default_real && saw_r16)
576 gfc_default_double_kind = 16;
577 else if (flag_real8_kind == 4)
579 if (!saw_r4)
580 gfc_fatal_error ("REAL(KIND=4) is not available for "
581 "%<-freal-8-real-4%> option");
583 gfc_default_double_kind = 4;
585 else if (flag_real8_kind == 10 )
587 if (!saw_r10)
588 gfc_fatal_error ("REAL(KIND=10) is not available for "
589 "%<-freal-8-real-10%> option");
591 gfc_default_double_kind = 10;
593 else if (flag_real8_kind == 16 )
595 if (!saw_r16)
596 gfc_fatal_error ("REAL(KIND=10) is not available for "
597 "%<-freal-8-real-16%> option");
599 gfc_default_double_kind = 16;
601 else if (saw_r4 && saw_r8)
602 gfc_default_double_kind = 8;
603 else
605 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
606 real ... occupies two contiguous numeric storage units.
608 Therefore we must be supplied a kind twice as large as we chose
609 for single precision. There are loopholes, in that double
610 precision must *occupy* two storage units, though it doesn't have
611 to *use* two storage units. Which means that you can make this
612 kind artificially wide by padding it. But at present there are
613 no GCC targets for which a two-word type does not exist, so we
614 just let gfc_validate_kind abort and tell us if something breaks. */
616 gfc_default_double_kind
617 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
620 /* The default logical kind is constrained to be the same as the
621 default integer kind. Similarly with complex and real. */
622 gfc_default_logical_kind = gfc_default_integer_kind;
623 gfc_default_complex_kind = gfc_default_real_kind;
625 /* We only have two character kinds: ASCII and UCS-4.
626 ASCII corresponds to a 8-bit integer type, if one is available.
627 UCS-4 corresponds to a 32-bit integer type, if one is available. */
628 i_index = 0;
629 if ((kind = get_int_kind_from_width (8)) > 0)
631 gfc_character_kinds[i_index].kind = kind;
632 gfc_character_kinds[i_index].bit_size = 8;
633 gfc_character_kinds[i_index].name = "ascii";
634 i_index++;
636 if ((kind = get_int_kind_from_width (32)) > 0)
638 gfc_character_kinds[i_index].kind = kind;
639 gfc_character_kinds[i_index].bit_size = 32;
640 gfc_character_kinds[i_index].name = "iso_10646";
641 i_index++;
644 /* Choose the smallest integer kind for our default character. */
645 gfc_default_character_kind = gfc_character_kinds[0].kind;
646 gfc_character_storage_size = gfc_default_character_kind * 8;
648 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
650 /* Pick a kind the same size as the C "int" type. */
651 gfc_c_int_kind = INT_TYPE_SIZE / 8;
653 /* Choose atomic kinds to match C's int. */
654 gfc_atomic_int_kind = gfc_c_int_kind;
655 gfc_atomic_logical_kind = gfc_c_int_kind;
659 /* Make sure that a valid kind is present. Returns an index into the
660 associated kinds array, -1 if the kind is not present. */
662 static int
663 validate_integer (int kind)
665 int i;
667 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
668 if (gfc_integer_kinds[i].kind == kind)
669 return i;
671 return -1;
674 static int
675 validate_real (int kind)
677 int i;
679 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
680 if (gfc_real_kinds[i].kind == kind)
681 return i;
683 return -1;
686 static int
687 validate_logical (int kind)
689 int i;
691 for (i = 0; gfc_logical_kinds[i].kind; i++)
692 if (gfc_logical_kinds[i].kind == kind)
693 return i;
695 return -1;
698 static int
699 validate_character (int kind)
701 int i;
703 for (i = 0; gfc_character_kinds[i].kind; i++)
704 if (gfc_character_kinds[i].kind == kind)
705 return i;
707 return -1;
710 /* Validate a kind given a basic type. The return value is the same
711 for the child functions, with -1 indicating nonexistence of the
712 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
715 gfc_validate_kind (bt type, int kind, bool may_fail)
717 int rc;
719 switch (type)
721 case BT_REAL: /* Fall through */
722 case BT_COMPLEX:
723 rc = validate_real (kind);
724 break;
725 case BT_INTEGER:
726 rc = validate_integer (kind);
727 break;
728 case BT_LOGICAL:
729 rc = validate_logical (kind);
730 break;
731 case BT_CHARACTER:
732 rc = validate_character (kind);
733 break;
735 default:
736 gfc_internal_error ("gfc_validate_kind(): Got bad type");
739 if (rc < 0 && !may_fail)
740 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
742 return rc;
746 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
747 Reuse common type nodes where possible. Recognize if the kind matches up
748 with a C type. This will be used later in determining which routines may
749 be scarfed from libm. */
751 static tree
752 gfc_build_int_type (gfc_integer_info *info)
754 int mode_precision = info->bit_size;
756 if (mode_precision == CHAR_TYPE_SIZE)
757 info->c_char = 1;
758 if (mode_precision == SHORT_TYPE_SIZE)
759 info->c_short = 1;
760 if (mode_precision == INT_TYPE_SIZE)
761 info->c_int = 1;
762 if (mode_precision == LONG_TYPE_SIZE)
763 info->c_long = 1;
764 if (mode_precision == LONG_LONG_TYPE_SIZE)
765 info->c_long_long = 1;
767 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
768 return intQI_type_node;
769 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
770 return intHI_type_node;
771 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
772 return intSI_type_node;
773 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
774 return intDI_type_node;
775 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
776 return intTI_type_node;
778 return make_signed_type (mode_precision);
781 tree
782 gfc_build_uint_type (int size)
784 if (size == CHAR_TYPE_SIZE)
785 return unsigned_char_type_node;
786 if (size == SHORT_TYPE_SIZE)
787 return short_unsigned_type_node;
788 if (size == INT_TYPE_SIZE)
789 return unsigned_type_node;
790 if (size == LONG_TYPE_SIZE)
791 return long_unsigned_type_node;
792 if (size == LONG_LONG_TYPE_SIZE)
793 return long_long_unsigned_type_node;
795 return make_unsigned_type (size);
799 static tree
800 gfc_build_real_type (gfc_real_info *info)
802 int mode_precision = info->mode_precision;
803 tree new_type;
805 if (mode_precision == FLOAT_TYPE_SIZE)
806 info->c_float = 1;
807 if (mode_precision == DOUBLE_TYPE_SIZE)
808 info->c_double = 1;
809 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
810 info->c_long_double = 1;
811 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
813 info->c_float128 = 1;
814 gfc_real16_is_float128 = true;
817 if (TYPE_PRECISION (float_type_node) == mode_precision)
818 return float_type_node;
819 if (TYPE_PRECISION (double_type_node) == mode_precision)
820 return double_type_node;
821 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
822 return long_double_type_node;
824 new_type = make_node (REAL_TYPE);
825 TYPE_PRECISION (new_type) = mode_precision;
826 layout_type (new_type);
827 return new_type;
830 static tree
831 gfc_build_complex_type (tree scalar_type)
833 tree new_type;
835 if (scalar_type == NULL)
836 return NULL;
837 if (scalar_type == float_type_node)
838 return complex_float_type_node;
839 if (scalar_type == double_type_node)
840 return complex_double_type_node;
841 if (scalar_type == long_double_type_node)
842 return complex_long_double_type_node;
844 new_type = make_node (COMPLEX_TYPE);
845 TREE_TYPE (new_type) = scalar_type;
846 layout_type (new_type);
847 return new_type;
850 static tree
851 gfc_build_logical_type (gfc_logical_info *info)
853 int bit_size = info->bit_size;
854 tree new_type;
856 if (bit_size == BOOL_TYPE_SIZE)
858 info->c_bool = 1;
859 return boolean_type_node;
862 new_type = make_unsigned_type (bit_size);
863 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
864 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
865 TYPE_PRECISION (new_type) = 1;
867 return new_type;
871 /* Create the backend type nodes. We map them to their
872 equivalent C type, at least for now. We also give
873 names to the types here, and we push them in the
874 global binding level context.*/
876 void
877 gfc_init_types (void)
879 char name_buf[26];
880 int index;
881 tree type;
882 unsigned n;
884 /* Create and name the types. */
885 #define PUSH_TYPE(name, node) \
886 pushdecl (build_decl (input_location, \
887 TYPE_DECL, get_identifier (name), node))
889 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
891 type = gfc_build_int_type (&gfc_integer_kinds[index]);
892 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
893 if (TYPE_STRING_FLAG (type))
894 type = make_signed_type (gfc_integer_kinds[index].bit_size);
895 gfc_integer_types[index] = type;
896 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
897 gfc_integer_kinds[index].kind);
898 PUSH_TYPE (name_buf, type);
901 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
903 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
904 gfc_logical_types[index] = type;
905 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
906 gfc_logical_kinds[index].kind);
907 PUSH_TYPE (name_buf, type);
910 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
912 type = gfc_build_real_type (&gfc_real_kinds[index]);
913 gfc_real_types[index] = type;
914 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
915 gfc_real_kinds[index].kind);
916 PUSH_TYPE (name_buf, type);
918 if (gfc_real_kinds[index].c_float128)
919 gfc_float128_type_node = type;
921 type = gfc_build_complex_type (type);
922 gfc_complex_types[index] = type;
923 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
924 gfc_real_kinds[index].kind);
925 PUSH_TYPE (name_buf, type);
927 if (gfc_real_kinds[index].c_float128)
928 gfc_complex_float128_type_node = type;
931 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
933 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
934 type = build_qualified_type (type, TYPE_UNQUALIFIED);
935 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
936 gfc_character_kinds[index].kind);
937 PUSH_TYPE (name_buf, type);
938 gfc_character_types[index] = type;
939 gfc_pcharacter_types[index] = build_pointer_type (type);
941 gfc_character1_type_node = gfc_character_types[0];
943 PUSH_TYPE ("byte", unsigned_char_type_node);
944 PUSH_TYPE ("void", void_type_node);
946 /* DBX debugging output gets upset if these aren't set. */
947 if (!TYPE_NAME (integer_type_node))
948 PUSH_TYPE ("c_integer", integer_type_node);
949 if (!TYPE_NAME (char_type_node))
950 PUSH_TYPE ("c_char", char_type_node);
952 #undef PUSH_TYPE
954 pvoid_type_node = build_pointer_type (void_type_node);
955 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
956 ppvoid_type_node = build_pointer_type (pvoid_type_node);
957 pchar_type_node = build_pointer_type (gfc_character1_type_node);
958 pfunc_type_node
959 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
961 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
962 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
963 since this function is called before gfc_init_constants. */
964 gfc_array_range_type
965 = build_range_type (gfc_array_index_type,
966 build_int_cst (gfc_array_index_type, 0),
967 NULL_TREE);
969 /* The maximum array element size that can be handled is determined
970 by the number of bits available to store this field in the array
971 descriptor. */
973 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
974 gfc_max_array_element_size
975 = wide_int_to_tree (size_type_node,
976 wi::mask (n, UNSIGNED,
977 TYPE_PRECISION (size_type_node)));
979 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
980 gfc_charlen_int_kind = 4;
981 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
984 /* Get the type node for the given type and kind. */
986 tree
987 gfc_get_int_type (int kind)
989 int index = gfc_validate_kind (BT_INTEGER, kind, true);
990 return index < 0 ? 0 : gfc_integer_types[index];
993 tree
994 gfc_get_real_type (int kind)
996 int index = gfc_validate_kind (BT_REAL, kind, true);
997 return index < 0 ? 0 : gfc_real_types[index];
1000 tree
1001 gfc_get_complex_type (int kind)
1003 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1004 return index < 0 ? 0 : gfc_complex_types[index];
1007 tree
1008 gfc_get_logical_type (int kind)
1010 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1011 return index < 0 ? 0 : gfc_logical_types[index];
1014 tree
1015 gfc_get_char_type (int kind)
1017 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1018 return index < 0 ? 0 : gfc_character_types[index];
1021 tree
1022 gfc_get_pchar_type (int kind)
1024 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1025 return index < 0 ? 0 : gfc_pcharacter_types[index];
1029 /* Create a character type with the given kind and length. */
1031 tree
1032 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1034 tree bounds, type;
1036 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1037 type = build_array_type (eltype, bounds);
1038 TYPE_STRING_FLAG (type) = 1;
1040 return type;
1043 tree
1044 gfc_get_character_type_len (int kind, tree len)
1046 gfc_validate_kind (BT_CHARACTER, kind, false);
1047 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1051 /* Get a type node for a character kind. */
1053 tree
1054 gfc_get_character_type (int kind, gfc_charlen * cl)
1056 tree len;
1058 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1059 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1060 len = build_fold_indirect_ref (len);
1062 return gfc_get_character_type_len (kind, len);
1065 /* Convert a basic type. This will be an array for character types. */
1067 tree
1068 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1070 tree basetype;
1072 switch (spec->type)
1074 case BT_UNKNOWN:
1075 gcc_unreachable ();
1077 case BT_INTEGER:
1078 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1079 has been resolved. This is done so we can convert C_PTR and
1080 C_FUNPTR to simple variables that get translated to (void *). */
1081 if (spec->f90_type == BT_VOID)
1083 if (spec->u.derived
1084 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1085 basetype = ptr_type_node;
1086 else
1087 basetype = pfunc_type_node;
1089 else
1090 basetype = gfc_get_int_type (spec->kind);
1091 break;
1093 case BT_REAL:
1094 basetype = gfc_get_real_type (spec->kind);
1095 break;
1097 case BT_COMPLEX:
1098 basetype = gfc_get_complex_type (spec->kind);
1099 break;
1101 case BT_LOGICAL:
1102 basetype = gfc_get_logical_type (spec->kind);
1103 break;
1105 case BT_CHARACTER:
1106 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1107 break;
1109 case BT_HOLLERITH:
1110 /* Since this cannot be used, return a length one character. */
1111 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1112 gfc_index_one_node);
1113 break;
1115 case BT_UNION:
1116 basetype = gfc_get_union_type (spec->u.derived);
1117 break;
1119 case BT_DERIVED:
1120 case BT_CLASS:
1121 basetype = gfc_get_derived_type (spec->u.derived, codim);
1123 if (spec->type == BT_CLASS)
1124 GFC_CLASS_TYPE_P (basetype) = 1;
1126 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1127 type and kind to fit a (void *) and the basetype returned was a
1128 ptr_type_node. We need to pass up this new information to the
1129 symbol that was declared of type C_PTR or C_FUNPTR. */
1130 if (spec->u.derived->ts.f90_type == BT_VOID)
1132 spec->type = BT_INTEGER;
1133 spec->kind = gfc_index_integer_kind;
1134 spec->f90_type = BT_VOID;
1136 break;
1137 case BT_VOID:
1138 case BT_ASSUMED:
1139 /* This is for the second arg to c_f_pointer and c_f_procpointer
1140 of the iso_c_binding module, to accept any ptr type. */
1141 basetype = ptr_type_node;
1142 if (spec->f90_type == BT_VOID)
1144 if (spec->u.derived
1145 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1146 basetype = ptr_type_node;
1147 else
1148 basetype = pfunc_type_node;
1150 break;
1151 default:
1152 gcc_unreachable ();
1154 return basetype;
1157 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1159 static tree
1160 gfc_conv_array_bound (gfc_expr * expr)
1162 /* If expr is an integer constant, return that. */
1163 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1164 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1166 /* Otherwise return NULL. */
1167 return NULL_TREE;
1170 /* Return the type of an element of the array. Note that scalar coarrays
1171 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1172 (with POINTER_TYPE stripped) is returned. */
1174 tree
1175 gfc_get_element_type (tree type)
1177 tree element;
1179 if (GFC_ARRAY_TYPE_P (type))
1181 if (TREE_CODE (type) == POINTER_TYPE)
1182 type = TREE_TYPE (type);
1183 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1185 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1186 element = type;
1188 else
1190 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1191 element = TREE_TYPE (type);
1194 else
1196 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1197 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1199 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1200 element = TREE_TYPE (element);
1202 /* For arrays, which are not scalar coarrays. */
1203 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1204 element = TREE_TYPE (element);
1207 return element;
1210 /* Build an array. This function is called from gfc_sym_type().
1211 Actually returns array descriptor type.
1213 Format of array descriptors is as follows:
1215 struct gfc_array_descriptor
1217 array *data
1218 index offset;
1219 index dtype;
1220 struct descriptor_dimension dimension[N_DIM];
1223 struct descriptor_dimension
1225 index stride;
1226 index lbound;
1227 index ubound;
1230 Translation code should use gfc_conv_descriptor_* rather than
1231 accessing the descriptor directly. Any changes to the array
1232 descriptor type will require changes in gfc_conv_descriptor_* and
1233 gfc_build_array_initializer.
1235 This is represented internally as a RECORD_TYPE. The index nodes
1236 are gfc_array_index_type and the data node is a pointer to the
1237 data. See below for the handling of character types.
1239 The dtype member is formatted as follows:
1240 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1241 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1242 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1244 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1245 this generated poor code for assumed/deferred size arrays. These
1246 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1247 of the GENERIC grammar. Also, there is no way to explicitly set
1248 the array stride, so all data must be packed(1). I've tried to
1249 mark all the functions which would require modification with a GCC
1250 ARRAYS comment.
1252 The data component points to the first element in the array. The
1253 offset field is the position of the origin of the array (i.e. element
1254 (0, 0 ...)). This may be outside the bounds of the array.
1256 An element is accessed by
1257 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1258 This gives good performance as the computation does not involve the
1259 bounds of the array. For packed arrays, this is optimized further
1260 by substituting the known strides.
1262 This system has one problem: all array bounds must be within 2^31
1263 elements of the origin (2^63 on 64-bit machines). For example
1264 integer, dimension (80000:90000, 80000:90000, 2) :: array
1265 may not work properly on 32-bit machines because 80000*80000 >
1266 2^31, so the calculation for stride2 would overflow. This may
1267 still work, but I haven't checked, and it relies on the overflow
1268 doing the right thing.
1270 The way to fix this problem is to access elements as follows:
1271 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1272 Obviously this is much slower. I will make this a compile time
1273 option, something like -fsmall-array-offsets. Mixing code compiled
1274 with and without this switch will work.
1276 (1) This can be worked around by modifying the upper bound of the
1277 previous dimension. This requires extra fields in the descriptor
1278 (both real_ubound and fake_ubound). */
1281 /* Returns true if the array sym does not require a descriptor. */
1284 gfc_is_nodesc_array (gfc_symbol * sym)
1286 symbol_attribute *array_attr;
1287 gfc_array_spec *as;
1288 bool is_classarray = IS_CLASS_ARRAY (sym);
1290 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1291 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1293 gcc_assert (array_attr->dimension || array_attr->codimension);
1295 /* We only want local arrays. */
1296 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1297 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1298 || array_attr->allocatable)
1299 return 0;
1301 /* We want a descriptor for associate-name arrays that do not have an
1302 explicitly known shape already. */
1303 if (sym->assoc && as->type != AS_EXPLICIT)
1304 return 0;
1306 /* The dummy is stored in sym and not in the component. */
1307 if (sym->attr.dummy)
1308 return as->type != AS_ASSUMED_SHAPE
1309 && as->type != AS_ASSUMED_RANK;
1311 if (sym->attr.result || sym->attr.function)
1312 return 0;
1314 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1316 return 1;
1320 /* Create an array descriptor type. */
1322 static tree
1323 gfc_build_array_type (tree type, gfc_array_spec * as,
1324 enum gfc_array_kind akind, bool restricted,
1325 bool contiguous, int codim)
1327 tree lbound[GFC_MAX_DIMENSIONS];
1328 tree ubound[GFC_MAX_DIMENSIONS];
1329 int n, corank;
1331 /* Assumed-shape arrays do not have codimension information stored in the
1332 descriptor. */
1333 corank = MAX (as->corank, codim);
1334 if (as->type == AS_ASSUMED_SHAPE ||
1335 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1336 corank = codim;
1338 if (as->type == AS_ASSUMED_RANK)
1339 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1341 lbound[n] = NULL_TREE;
1342 ubound[n] = NULL_TREE;
1345 for (n = 0; n < as->rank; n++)
1347 /* Create expressions for the known bounds of the array. */
1348 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1349 lbound[n] = gfc_index_one_node;
1350 else
1351 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1352 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1355 for (n = as->rank; n < as->rank + corank; n++)
1357 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1358 lbound[n] = gfc_index_one_node;
1359 else
1360 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1362 if (n < as->rank + corank - 1)
1363 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1366 if (as->type == AS_ASSUMED_SHAPE)
1367 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1368 : GFC_ARRAY_ASSUMED_SHAPE;
1369 else if (as->type == AS_ASSUMED_RANK)
1370 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1371 : GFC_ARRAY_ASSUMED_RANK;
1372 return gfc_get_array_type_bounds (type, as->rank == -1
1373 ? GFC_MAX_DIMENSIONS : as->rank,
1374 corank, lbound, ubound, 0, akind,
1375 restricted);
1378 /* Returns the struct descriptor_dimension type. */
1380 static tree
1381 gfc_get_desc_dim_type (void)
1383 tree type;
1384 tree decl, *chain = NULL;
1386 if (gfc_desc_dim_type)
1387 return gfc_desc_dim_type;
1389 /* Build the type node. */
1390 type = make_node (RECORD_TYPE);
1392 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1393 TYPE_PACKED (type) = 1;
1395 /* Consists of the stride, lbound and ubound members. */
1396 decl = gfc_add_field_to_struct_1 (type,
1397 get_identifier ("stride"),
1398 gfc_array_index_type, &chain);
1399 TREE_NO_WARNING (decl) = 1;
1401 decl = gfc_add_field_to_struct_1 (type,
1402 get_identifier ("lbound"),
1403 gfc_array_index_type, &chain);
1404 TREE_NO_WARNING (decl) = 1;
1406 decl = gfc_add_field_to_struct_1 (type,
1407 get_identifier ("ubound"),
1408 gfc_array_index_type, &chain);
1409 TREE_NO_WARNING (decl) = 1;
1411 /* Finish off the type. */
1412 gfc_finish_type (type);
1413 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1415 gfc_desc_dim_type = type;
1416 return type;
1420 /* Return the DTYPE for an array. This describes the type and type parameters
1421 of the array. */
1422 /* TODO: Only call this when the value is actually used, and make all the
1423 unknown cases abort. */
1425 tree
1426 gfc_get_dtype_rank_type (int rank, tree etype)
1428 tree size;
1429 int n;
1430 HOST_WIDE_INT i;
1431 tree tmp;
1432 tree dtype;
1434 switch (TREE_CODE (etype))
1436 case INTEGER_TYPE:
1437 n = BT_INTEGER;
1438 break;
1440 case BOOLEAN_TYPE:
1441 n = BT_LOGICAL;
1442 break;
1444 case REAL_TYPE:
1445 n = BT_REAL;
1446 break;
1448 case COMPLEX_TYPE:
1449 n = BT_COMPLEX;
1450 break;
1452 /* We will never have arrays of arrays. */
1453 case RECORD_TYPE:
1454 n = BT_DERIVED;
1455 break;
1457 case ARRAY_TYPE:
1458 n = BT_CHARACTER;
1459 break;
1461 case POINTER_TYPE:
1462 n = BT_ASSUMED;
1463 break;
1465 default:
1466 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1467 /* We can strange array types for temporary arrays. */
1468 return gfc_index_zero_node;
1471 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1472 size = TYPE_SIZE_UNIT (etype);
1474 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1475 if (size && INTEGER_CST_P (size))
1477 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1478 gfc_fatal_error ("Array element size too big at %C");
1480 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1482 dtype = build_int_cst (gfc_array_index_type, i);
1484 if (size && !INTEGER_CST_P (size))
1486 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1487 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1488 gfc_array_index_type,
1489 fold_convert (gfc_array_index_type, size), tmp);
1490 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1491 tmp, dtype);
1493 /* If we don't know the size we leave it as zero. This should never happen
1494 for anything that is actually used. */
1495 /* TODO: Check this is actually true, particularly when repacking
1496 assumed size parameters. */
1498 return dtype;
1502 tree
1503 gfc_get_dtype (tree type)
1505 tree dtype;
1506 tree etype;
1507 int rank;
1509 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1511 if (GFC_TYPE_ARRAY_DTYPE (type))
1512 return GFC_TYPE_ARRAY_DTYPE (type);
1514 rank = GFC_TYPE_ARRAY_RANK (type);
1515 etype = gfc_get_element_type (type);
1516 dtype = gfc_get_dtype_rank_type (rank, etype);
1518 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1519 return dtype;
1523 /* Build an array type for use without a descriptor, packed according
1524 to the value of PACKED. */
1526 tree
1527 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1528 bool restricted)
1530 tree range;
1531 tree type;
1532 tree tmp;
1533 int n;
1534 int known_stride;
1535 int known_offset;
1536 mpz_t offset;
1537 mpz_t stride;
1538 mpz_t delta;
1539 gfc_expr *expr;
1541 mpz_init_set_ui (offset, 0);
1542 mpz_init_set_ui (stride, 1);
1543 mpz_init (delta);
1545 /* We don't use build_array_type because this does not include include
1546 lang-specific information (i.e. the bounds of the array) when checking
1547 for duplicates. */
1548 if (as->rank)
1549 type = make_node (ARRAY_TYPE);
1550 else
1551 type = build_variant_type_copy (etype);
1553 GFC_ARRAY_TYPE_P (type) = 1;
1554 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1556 known_stride = (packed != PACKED_NO);
1557 known_offset = 1;
1558 for (n = 0; n < as->rank; n++)
1560 /* Fill in the stride and bound components of the type. */
1561 if (known_stride)
1562 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1563 else
1564 tmp = NULL_TREE;
1565 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1567 expr = as->lower[n];
1568 if (expr->expr_type == EXPR_CONSTANT)
1570 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1571 gfc_index_integer_kind);
1573 else
1575 known_stride = 0;
1576 tmp = NULL_TREE;
1578 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1580 if (known_stride)
1582 /* Calculate the offset. */
1583 mpz_mul (delta, stride, as->lower[n]->value.integer);
1584 mpz_sub (offset, offset, delta);
1586 else
1587 known_offset = 0;
1589 expr = as->upper[n];
1590 if (expr && expr->expr_type == EXPR_CONSTANT)
1592 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1593 gfc_index_integer_kind);
1595 else
1597 tmp = NULL_TREE;
1598 known_stride = 0;
1600 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1602 if (known_stride)
1604 /* Calculate the stride. */
1605 mpz_sub (delta, as->upper[n]->value.integer,
1606 as->lower[n]->value.integer);
1607 mpz_add_ui (delta, delta, 1);
1608 mpz_mul (stride, stride, delta);
1611 /* Only the first stride is known for partial packed arrays. */
1612 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1613 known_stride = 0;
1615 for (n = as->rank; n < as->rank + as->corank; n++)
1617 expr = as->lower[n];
1618 if (expr->expr_type == EXPR_CONSTANT)
1619 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1620 gfc_index_integer_kind);
1621 else
1622 tmp = NULL_TREE;
1623 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1625 expr = as->upper[n];
1626 if (expr && expr->expr_type == EXPR_CONSTANT)
1627 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1628 gfc_index_integer_kind);
1629 else
1630 tmp = NULL_TREE;
1631 if (n < as->rank + as->corank - 1)
1632 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1635 if (known_offset)
1637 GFC_TYPE_ARRAY_OFFSET (type) =
1638 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1640 else
1641 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1643 if (known_stride)
1645 GFC_TYPE_ARRAY_SIZE (type) =
1646 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1648 else
1649 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1651 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1652 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1653 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1654 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1655 NULL_TREE);
1656 /* TODO: use main type if it is unbounded. */
1657 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1658 build_pointer_type (build_array_type (etype, range));
1659 if (restricted)
1660 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1661 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1662 TYPE_QUAL_RESTRICT);
1664 if (as->rank == 0)
1666 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1668 type = build_pointer_type (type);
1670 if (restricted)
1671 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1673 GFC_ARRAY_TYPE_P (type) = 1;
1674 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1677 return type;
1680 if (known_stride)
1682 mpz_sub_ui (stride, stride, 1);
1683 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1685 else
1686 range = NULL_TREE;
1688 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1689 TYPE_DOMAIN (type) = range;
1691 build_pointer_type (etype);
1692 TREE_TYPE (type) = etype;
1694 layout_type (type);
1696 mpz_clear (offset);
1697 mpz_clear (stride);
1698 mpz_clear (delta);
1700 /* Represent packed arrays as multi-dimensional if they have rank >
1701 1 and with proper bounds, instead of flat arrays. This makes for
1702 better debug info. */
1703 if (known_offset)
1705 tree gtype = etype, rtype, type_decl;
1707 for (n = as->rank - 1; n >= 0; n--)
1709 rtype = build_range_type (gfc_array_index_type,
1710 GFC_TYPE_ARRAY_LBOUND (type, n),
1711 GFC_TYPE_ARRAY_UBOUND (type, n));
1712 gtype = build_array_type (gtype, rtype);
1714 TYPE_NAME (type) = type_decl = build_decl (input_location,
1715 TYPE_DECL, NULL, gtype);
1716 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1719 if (packed != PACKED_STATIC || !known_stride
1720 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1722 /* For dummy arrays and automatic (heap allocated) arrays we
1723 want a pointer to the array. */
1724 type = build_pointer_type (type);
1725 if (restricted)
1726 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1727 GFC_ARRAY_TYPE_P (type) = 1;
1728 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1730 return type;
1734 /* Return or create the base type for an array descriptor. */
1736 static tree
1737 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1739 tree fat_type, decl, arraytype, *chain = NULL;
1740 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1741 int idx;
1743 /* Assumed-rank array. */
1744 if (dimen == -1)
1745 dimen = GFC_MAX_DIMENSIONS;
1747 idx = 2 * (codimen + dimen) + restricted;
1749 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1751 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1753 if (gfc_array_descriptor_base_caf[idx])
1754 return gfc_array_descriptor_base_caf[idx];
1756 else if (gfc_array_descriptor_base[idx])
1757 return gfc_array_descriptor_base[idx];
1759 /* Build the type node. */
1760 fat_type = make_node (RECORD_TYPE);
1762 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1763 TYPE_NAME (fat_type) = get_identifier (name);
1764 TYPE_NAMELESS (fat_type) = 1;
1766 /* Add the data member as the first element of the descriptor. */
1767 decl = gfc_add_field_to_struct_1 (fat_type,
1768 get_identifier ("data"),
1769 (restricted
1770 ? prvoid_type_node
1771 : ptr_type_node), &chain);
1773 /* Add the base component. */
1774 decl = gfc_add_field_to_struct_1 (fat_type,
1775 get_identifier ("offset"),
1776 gfc_array_index_type, &chain);
1777 TREE_NO_WARNING (decl) = 1;
1779 /* Add the dtype component. */
1780 decl = gfc_add_field_to_struct_1 (fat_type,
1781 get_identifier ("dtype"),
1782 gfc_array_index_type, &chain);
1783 TREE_NO_WARNING (decl) = 1;
1785 /* Build the array type for the stride and bound components. */
1786 if (dimen + codimen > 0)
1788 arraytype =
1789 build_array_type (gfc_get_desc_dim_type (),
1790 build_range_type (gfc_array_index_type,
1791 gfc_index_zero_node,
1792 gfc_rank_cst[codimen + dimen - 1]));
1794 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1795 arraytype, &chain);
1796 TREE_NO_WARNING (decl) = 1;
1799 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1801 decl = gfc_add_field_to_struct_1 (fat_type,
1802 get_identifier ("token"),
1803 prvoid_type_node, &chain);
1804 TREE_NO_WARNING (decl) = 1;
1807 /* Finish off the type. */
1808 gfc_finish_type (fat_type);
1809 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1811 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1812 gfc_array_descriptor_base_caf[idx] = fat_type;
1813 else
1814 gfc_array_descriptor_base[idx] = fat_type;
1816 return fat_type;
1820 /* Build an array (descriptor) type with given bounds. */
1822 tree
1823 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1824 tree * ubound, int packed,
1825 enum gfc_array_kind akind, bool restricted)
1827 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1828 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1829 const char *type_name;
1830 int n;
1832 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1833 fat_type = build_distinct_type_copy (base_type);
1834 /* Make sure that nontarget and target array type have the same canonical
1835 type (and same stub decl for debug info). */
1836 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1837 TYPE_CANONICAL (fat_type) = base_type;
1838 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1840 tmp = TYPE_NAME (etype);
1841 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1842 tmp = DECL_NAME (tmp);
1843 if (tmp)
1844 type_name = IDENTIFIER_POINTER (tmp);
1845 else
1846 type_name = "unknown";
1847 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1848 GFC_MAX_SYMBOL_LEN, type_name);
1849 TYPE_NAME (fat_type) = get_identifier (name);
1850 TYPE_NAMELESS (fat_type) = 1;
1852 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1853 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1855 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1856 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1857 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1858 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1860 /* Build an array descriptor record type. */
1861 if (packed != 0)
1862 stride = gfc_index_one_node;
1863 else
1864 stride = NULL_TREE;
1865 for (n = 0; n < dimen + codimen; n++)
1867 if (n < dimen)
1868 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1870 if (lbound)
1871 lower = lbound[n];
1872 else
1873 lower = NULL_TREE;
1875 if (lower != NULL_TREE)
1877 if (INTEGER_CST_P (lower))
1878 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1879 else
1880 lower = NULL_TREE;
1883 if (codimen && n == dimen + codimen - 1)
1884 break;
1886 upper = ubound[n];
1887 if (upper != NULL_TREE)
1889 if (INTEGER_CST_P (upper))
1890 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1891 else
1892 upper = NULL_TREE;
1895 if (n >= dimen)
1896 continue;
1898 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1900 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1901 gfc_array_index_type, upper, lower);
1902 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1903 gfc_array_index_type, tmp,
1904 gfc_index_one_node);
1905 stride = fold_build2_loc (input_location, MULT_EXPR,
1906 gfc_array_index_type, tmp, stride);
1907 /* Check the folding worked. */
1908 gcc_assert (INTEGER_CST_P (stride));
1910 else
1911 stride = NULL_TREE;
1913 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1915 /* TODO: known offsets for descriptors. */
1916 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1918 if (dimen == 0)
1920 arraytype = build_pointer_type (etype);
1921 if (restricted)
1922 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1924 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1925 return fat_type;
1928 /* We define data as an array with the correct size if possible.
1929 Much better than doing pointer arithmetic. */
1930 if (stride)
1931 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1932 int_const_binop (MINUS_EXPR, stride,
1933 build_int_cst (TREE_TYPE (stride), 1)));
1934 else
1935 rtype = gfc_array_range_type;
1936 arraytype = build_array_type (etype, rtype);
1937 arraytype = build_pointer_type (arraytype);
1938 if (restricted)
1939 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1940 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1942 /* This will generate the base declarations we need to emit debug
1943 information for this type. FIXME: there must be a better way to
1944 avoid divergence between compilations with and without debug
1945 information. */
1947 struct array_descr_info info;
1948 gfc_get_array_descr_info (fat_type, &info);
1949 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1952 return fat_type;
1955 /* Build a pointer type. This function is called from gfc_sym_type(). */
1957 static tree
1958 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1960 /* Array pointer types aren't actually pointers. */
1961 if (sym->attr.dimension)
1962 return type;
1963 else
1964 return build_pointer_type (type);
1967 static tree gfc_nonrestricted_type (tree t);
1968 /* Given two record or union type nodes TO and FROM, ensure
1969 that all fields in FROM have a corresponding field in TO,
1970 their type being nonrestrict variants. This accepts a TO
1971 node that already has a prefix of the fields in FROM. */
1972 static void
1973 mirror_fields (tree to, tree from)
1975 tree fto, ffrom;
1976 tree *chain;
1978 /* Forward to the end of TOs fields. */
1979 fto = TYPE_FIELDS (to);
1980 ffrom = TYPE_FIELDS (from);
1981 chain = &TYPE_FIELDS (to);
1982 while (fto)
1984 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
1985 chain = &DECL_CHAIN (fto);
1986 fto = DECL_CHAIN (fto);
1987 ffrom = DECL_CHAIN (ffrom);
1990 /* Now add all fields remaining in FROM (starting with ffrom). */
1991 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
1993 tree newfield = copy_node (ffrom);
1994 DECL_CONTEXT (newfield) = to;
1995 /* The store to DECL_CHAIN might seem redundant with the
1996 stores to *chain, but not clearing it here would mean
1997 leaving a chain into the old fields. If ever
1998 our called functions would look at them confusion
1999 will arise. */
2000 DECL_CHAIN (newfield) = NULL_TREE;
2001 *chain = newfield;
2002 chain = &DECL_CHAIN (newfield);
2004 if (TREE_CODE (ffrom) == FIELD_DECL)
2006 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2007 TREE_TYPE (newfield) = elemtype;
2010 *chain = NULL_TREE;
2013 /* Given a type T, returns a different type of the same structure,
2014 except that all types it refers to (recursively) are always
2015 non-restrict qualified types. */
2016 static tree
2017 gfc_nonrestricted_type (tree t)
2019 tree ret = t;
2021 /* If the type isn't laid out yet, don't copy it. If something
2022 needs it for real it should wait until the type got finished. */
2023 if (!TYPE_SIZE (t))
2024 return t;
2026 if (!TYPE_LANG_SPECIFIC (t))
2027 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2028 /* If we're dealing with this very node already further up
2029 the call chain (recursion via pointers and struct members)
2030 we haven't yet determined if we really need a new type node.
2031 Assume we don't, return T itself. */
2032 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2033 return t;
2035 /* If we have calculated this all already, just return it. */
2036 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2037 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2039 /* Mark this type. */
2040 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2042 switch (TREE_CODE (t))
2044 default:
2045 break;
2047 case POINTER_TYPE:
2048 case REFERENCE_TYPE:
2050 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2051 if (totype == TREE_TYPE (t))
2052 ret = t;
2053 else if (TREE_CODE (t) == POINTER_TYPE)
2054 ret = build_pointer_type (totype);
2055 else
2056 ret = build_reference_type (totype);
2057 ret = build_qualified_type (ret,
2058 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2060 break;
2062 case ARRAY_TYPE:
2064 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2065 if (elemtype == TREE_TYPE (t))
2066 ret = t;
2067 else
2069 ret = build_variant_type_copy (t);
2070 TREE_TYPE (ret) = elemtype;
2071 if (TYPE_LANG_SPECIFIC (t)
2072 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2074 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2075 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2076 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2078 TYPE_LANG_SPECIFIC (ret)
2079 = ggc_cleared_alloc<struct lang_type> ();
2080 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2081 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2086 break;
2088 case RECORD_TYPE:
2089 case UNION_TYPE:
2090 case QUAL_UNION_TYPE:
2092 tree field;
2093 /* First determine if we need a new type at all.
2094 Careful, the two calls to gfc_nonrestricted_type per field
2095 might return different values. That happens exactly when
2096 one of the fields reaches back to this very record type
2097 (via pointers). The first calls will assume that we don't
2098 need to copy T (see the error_mark_node marking). If there
2099 are any reasons for copying T apart from having to copy T,
2100 we'll indeed copy it, and the second calls to
2101 gfc_nonrestricted_type will use that new node if they
2102 reach back to T. */
2103 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2104 if (TREE_CODE (field) == FIELD_DECL)
2106 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2107 if (elemtype != TREE_TYPE (field))
2108 break;
2110 if (!field)
2111 break;
2112 ret = build_variant_type_copy (t);
2113 TYPE_FIELDS (ret) = NULL_TREE;
2115 /* Here we make sure that as soon as we know we have to copy
2116 T, that also fields reaching back to us will use the new
2117 copy. It's okay if that copy still contains the old fields,
2118 we won't look at them. */
2119 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2120 mirror_fields (ret, t);
2122 break;
2125 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2126 return ret;
2130 /* Return the type for a symbol. Special handling is required for character
2131 types to get the correct level of indirection.
2132 For functions return the return type.
2133 For subroutines return void_type_node.
2134 Calling this multiple times for the same symbol should be avoided,
2135 especially for character and array types. */
2137 tree
2138 gfc_sym_type (gfc_symbol * sym)
2140 tree type;
2141 int byref;
2142 bool restricted;
2144 /* Procedure Pointers inside COMMON blocks. */
2145 if (sym->attr.proc_pointer && sym->attr.in_common)
2147 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2148 sym->attr.proc_pointer = 0;
2149 type = build_pointer_type (gfc_get_function_type (sym));
2150 sym->attr.proc_pointer = 1;
2151 return type;
2154 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2155 return void_type_node;
2157 /* In the case of a function the fake result variable may have a
2158 type different from the function type, so don't return early in
2159 that case. */
2160 if (sym->backend_decl && !sym->attr.function)
2161 return TREE_TYPE (sym->backend_decl);
2163 if (sym->ts.type == BT_CHARACTER
2164 && ((sym->attr.function && sym->attr.is_bind_c)
2165 || (sym->attr.result
2166 && sym->ns->proc_name
2167 && sym->ns->proc_name->attr.is_bind_c)
2168 || (sym->ts.deferred && (!sym->ts.u.cl
2169 || !sym->ts.u.cl->backend_decl))))
2170 type = gfc_character1_type_node;
2171 else
2172 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2174 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2175 byref = 1;
2176 else
2177 byref = 0;
2179 restricted = !sym->attr.target && !sym->attr.pointer
2180 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2181 if (!restricted)
2182 type = gfc_nonrestricted_type (type);
2184 if (sym->attr.dimension || sym->attr.codimension)
2186 if (gfc_is_nodesc_array (sym))
2188 /* If this is a character argument of unknown length, just use the
2189 base type. */
2190 if (sym->ts.type != BT_CHARACTER
2191 || !(sym->attr.dummy || sym->attr.function)
2192 || sym->ts.u.cl->backend_decl)
2194 type = gfc_get_nodesc_array_type (type, sym->as,
2195 byref ? PACKED_FULL
2196 : PACKED_STATIC,
2197 restricted);
2198 byref = 0;
2201 else
2203 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2204 if (sym->attr.pointer)
2205 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2206 : GFC_ARRAY_POINTER;
2207 else if (sym->attr.allocatable)
2208 akind = GFC_ARRAY_ALLOCATABLE;
2209 type = gfc_build_array_type (type, sym->as, akind, restricted,
2210 sym->attr.contiguous, false);
2213 else
2215 if (sym->attr.allocatable || sym->attr.pointer
2216 || gfc_is_associate_pointer (sym))
2217 type = gfc_build_pointer_type (sym, type);
2220 /* We currently pass all parameters by reference.
2221 See f95_get_function_decl. For dummy function parameters return the
2222 function type. */
2223 if (byref)
2225 /* We must use pointer types for potentially absent variables. The
2226 optimizers assume a reference type argument is never NULL. */
2227 if (sym->attr.optional
2228 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2229 type = build_pointer_type (type);
2230 else
2232 type = build_reference_type (type);
2233 if (restricted)
2234 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2238 return (type);
2241 /* Layout and output debug info for a record type. */
2243 void
2244 gfc_finish_type (tree type)
2246 tree decl;
2248 decl = build_decl (input_location,
2249 TYPE_DECL, NULL_TREE, type);
2250 TYPE_STUB_DECL (type) = decl;
2251 layout_type (type);
2252 rest_of_type_compilation (type, 1);
2253 rest_of_decl_compilation (decl, 1, 0);
2256 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2257 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2258 to the end of the field list pointed to by *CHAIN.
2260 Returns a pointer to the new field. */
2262 static tree
2263 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2265 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2267 DECL_CONTEXT (decl) = context;
2268 DECL_CHAIN (decl) = NULL_TREE;
2269 if (TYPE_FIELDS (context) == NULL_TREE)
2270 TYPE_FIELDS (context) = decl;
2271 if (chain != NULL)
2273 if (*chain != NULL)
2274 **chain = decl;
2275 *chain = &DECL_CHAIN (decl);
2278 return decl;
2281 /* Like `gfc_add_field_to_struct_1', but adds alignment
2282 information. */
2284 tree
2285 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2287 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2289 DECL_INITIAL (decl) = 0;
2290 SET_DECL_ALIGN (decl, 0);
2291 DECL_USER_ALIGN (decl) = 0;
2293 return decl;
2297 /* Copy the backend_decl and component backend_decls if
2298 the two derived type symbols are "equal", as described
2299 in 4.4.2 and resolved by gfc_compare_derived_types. */
2302 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2303 bool from_gsym)
2305 gfc_component *to_cm;
2306 gfc_component *from_cm;
2308 if (from == to)
2309 return 1;
2311 if (from->backend_decl == NULL
2312 || !gfc_compare_derived_types (from, to))
2313 return 0;
2315 to->backend_decl = from->backend_decl;
2317 to_cm = to->components;
2318 from_cm = from->components;
2320 /* Copy the component declarations. If a component is itself
2321 a derived type, we need a copy of its component declarations.
2322 This is done by recursing into gfc_get_derived_type and
2323 ensures that the component's component declarations have
2324 been built. If it is a character, we need the character
2325 length, as well. */
2326 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2328 to_cm->backend_decl = from_cm->backend_decl;
2329 if (from_cm->ts.type == BT_UNION)
2330 gfc_get_union_type (to_cm->ts.u.derived);
2331 else if (from_cm->ts.type == BT_DERIVED
2332 && (!from_cm->attr.pointer || from_gsym))
2333 gfc_get_derived_type (to_cm->ts.u.derived);
2334 else if (from_cm->ts.type == BT_CLASS
2335 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2336 gfc_get_derived_type (to_cm->ts.u.derived);
2337 else if (from_cm->ts.type == BT_CHARACTER)
2338 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2341 return 1;
2345 /* Build a tree node for a procedure pointer component. */
2347 tree
2348 gfc_get_ppc_type (gfc_component* c)
2350 tree t;
2352 /* Explicit interface. */
2353 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2354 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2356 /* Implicit interface (only return value may be known). */
2357 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2358 t = gfc_typenode_for_spec (&c->ts);
2359 else
2360 t = void_type_node;
2362 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2366 /* Build a tree node for a union type. Requires building each map
2367 structure which is an element of the union. */
2369 tree
2370 gfc_get_union_type (gfc_symbol *un)
2372 gfc_component *map = NULL;
2373 tree typenode = NULL, map_type = NULL, map_field = NULL;
2374 tree *chain = NULL;
2376 if (un->backend_decl)
2378 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2379 return un->backend_decl;
2380 else
2381 typenode = un->backend_decl;
2383 else
2385 typenode = make_node (UNION_TYPE);
2386 TYPE_NAME (typenode) = get_identifier (un->name);
2389 /* Add each contained MAP as a field. */
2390 for (map = un->components; map; map = map->next)
2392 gcc_assert (map->ts.type == BT_DERIVED);
2394 /* The map's type node, which is defined within this union's context. */
2395 map_type = gfc_get_derived_type (map->ts.u.derived);
2396 TYPE_CONTEXT (map_type) = typenode;
2398 /* The map field's declaration. */
2399 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2400 map_type, &chain);
2401 if (map->loc.lb)
2402 gfc_set_decl_location (map_field, &map->loc);
2403 else if (un->declared_at.lb)
2404 gfc_set_decl_location (map_field, &un->declared_at);
2406 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2407 DECL_NAMELESS(map_field) = true;
2409 /* We should never clobber another backend declaration for this map,
2410 because each map component is unique. */
2411 if (!map->backend_decl)
2412 map->backend_decl = map_field;
2415 un->backend_decl = typenode;
2416 gfc_finish_type (typenode);
2418 return typenode;
2422 /* Build a tree node for a derived type. If there are equal
2423 derived types, with different local names, these are built
2424 at the same time. If an equal derived type has been built
2425 in a parent namespace, this is used. */
2427 tree
2428 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2430 tree typenode = NULL, field = NULL, field_type = NULL;
2431 tree canonical = NULL_TREE;
2432 tree *chain = NULL;
2433 bool got_canonical = false;
2434 bool unlimited_entity = false;
2435 gfc_component *c;
2436 gfc_dt_list *dt;
2437 gfc_namespace *ns;
2438 tree tmp;
2440 if (derived->attr.unlimited_polymorphic
2441 || (flag_coarray == GFC_FCOARRAY_LIB
2442 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2443 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2444 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
2445 return ptr_type_node;
2447 if (flag_coarray != GFC_FCOARRAY_LIB
2448 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2449 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2450 return gfc_get_int_type (gfc_default_integer_kind);
2452 if (derived && derived->attr.flavor == FL_PROCEDURE
2453 && derived->attr.generic)
2454 derived = gfc_find_dt_in_generic (derived);
2456 /* See if it's one of the iso_c_binding derived types. */
2457 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2459 if (derived->backend_decl)
2460 return derived->backend_decl;
2462 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2463 derived->backend_decl = ptr_type_node;
2464 else
2465 derived->backend_decl = pfunc_type_node;
2467 derived->ts.kind = gfc_index_integer_kind;
2468 derived->ts.type = BT_INTEGER;
2469 /* Set the f90_type to BT_VOID as a way to recognize something of type
2470 BT_INTEGER that needs to fit a void * for the purpose of the
2471 iso_c_binding derived types. */
2472 derived->ts.f90_type = BT_VOID;
2474 return derived->backend_decl;
2477 /* If use associated, use the module type for this one. */
2478 if (derived->backend_decl == NULL
2479 && derived->attr.use_assoc
2480 && derived->module
2481 && gfc_get_module_backend_decl (derived))
2482 goto copy_derived_types;
2484 /* The derived types from an earlier namespace can be used as the
2485 canonical type. */
2486 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2487 && gfc_global_ns_list)
2489 for (ns = gfc_global_ns_list;
2490 ns->translated && !got_canonical;
2491 ns = ns->sibling)
2493 dt = ns->derived_types;
2494 for (; dt && !canonical; dt = dt->next)
2496 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2497 if (derived->backend_decl)
2498 got_canonical = true;
2503 /* Store up the canonical type to be added to this one. */
2504 if (got_canonical)
2506 if (TYPE_CANONICAL (derived->backend_decl))
2507 canonical = TYPE_CANONICAL (derived->backend_decl);
2508 else
2509 canonical = derived->backend_decl;
2511 derived->backend_decl = NULL_TREE;
2514 /* derived->backend_decl != 0 means we saw it before, but its
2515 components' backend_decl may have not been built. */
2516 if (derived->backend_decl)
2518 /* Its components' backend_decl have been built or we are
2519 seeing recursion through the formal arglist of a procedure
2520 pointer component. */
2521 if (TYPE_FIELDS (derived->backend_decl))
2522 return derived->backend_decl;
2523 else if (derived->attr.abstract
2524 && derived->attr.proc_pointer_comp)
2526 /* If an abstract derived type with procedure pointer
2527 components has no other type of component, return the
2528 backend_decl. Otherwise build the components if any of the
2529 non-procedure pointer components have no backend_decl. */
2530 for (c = derived->components; c; c = c->next)
2532 bool same_alloc_type = c->attr.allocatable
2533 && derived == c->ts.u.derived;
2534 if (!c->attr.proc_pointer
2535 && !same_alloc_type
2536 && c->backend_decl == NULL)
2537 break;
2538 else if (c->next == NULL)
2539 return derived->backend_decl;
2541 typenode = derived->backend_decl;
2543 else
2544 typenode = derived->backend_decl;
2546 else
2548 /* We see this derived type first time, so build the type node. */
2549 typenode = make_node (RECORD_TYPE);
2550 TYPE_NAME (typenode) = get_identifier (derived->name);
2551 TYPE_PACKED (typenode) = flag_pack_derived;
2552 derived->backend_decl = typenode;
2555 if (derived->components
2556 && derived->components->ts.type == BT_DERIVED
2557 && strcmp (derived->components->name, "_data") == 0
2558 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2559 unlimited_entity = true;
2561 /* Go through the derived type components, building them as
2562 necessary. The reason for doing this now is that it is
2563 possible to recurse back to this derived type through a
2564 pointer component (PR24092). If this happens, the fields
2565 will be built and so we can return the type. */
2566 for (c = derived->components; c; c = c->next)
2568 bool same_alloc_type = c->attr.allocatable
2569 && derived == c->ts.u.derived;
2571 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2572 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2574 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2575 continue;
2577 if ((!c->attr.pointer && !c->attr.proc_pointer
2578 && !same_alloc_type)
2579 || c->ts.u.derived->backend_decl == NULL)
2581 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2582 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2583 local_codim);
2586 if (c->ts.u.derived->attr.is_iso_c)
2588 /* Need to copy the modified ts from the derived type. The
2589 typespec was modified because C_PTR/C_FUNPTR are translated
2590 into (void *) from derived types. */
2591 c->ts.type = c->ts.u.derived->ts.type;
2592 c->ts.kind = c->ts.u.derived->ts.kind;
2593 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2594 if (c->initializer)
2596 c->initializer->ts.type = c->ts.type;
2597 c->initializer->ts.kind = c->ts.kind;
2598 c->initializer->ts.f90_type = c->ts.f90_type;
2599 c->initializer->expr_type = EXPR_NULL;
2604 if (TYPE_FIELDS (derived->backend_decl))
2605 return derived->backend_decl;
2607 /* Build the type member list. Install the newly created RECORD_TYPE
2608 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2609 through only the top-level linked list of components so we correctly
2610 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2611 types are built as part of gfc_get_union_type. */
2612 for (c = derived->components; c; c = c->next)
2614 bool same_alloc_type = c->attr.allocatable
2615 && derived == c->ts.u.derived;
2616 /* Prevent infinite recursion, when the procedure pointer type is
2617 the same as derived, by forcing the procedure pointer component to
2618 be built as if the explicit interface does not exist. */
2619 if (c->attr.proc_pointer
2620 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2621 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2622 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2623 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2624 field_type = gfc_get_ppc_type (c);
2625 else if (c->attr.proc_pointer && derived->backend_decl)
2627 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2628 field_type = build_pointer_type (tmp);
2630 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2631 field_type = c->ts.u.derived->backend_decl;
2632 else
2634 if (c->ts.type == BT_CHARACTER && !c->ts.deferred)
2636 /* Evaluate the string length. */
2637 gfc_conv_const_charlen (c->ts.u.cl);
2638 gcc_assert (c->ts.u.cl->backend_decl);
2640 else if (c->ts.type == BT_CHARACTER)
2641 c->ts.u.cl->backend_decl
2642 = build_int_cst (gfc_charlen_type_node, 0);
2644 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2647 /* This returns an array descriptor type. Initialization may be
2648 required. */
2649 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2651 if (c->attr.pointer || c->attr.allocatable)
2653 enum gfc_array_kind akind;
2654 if (c->attr.pointer)
2655 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2656 : GFC_ARRAY_POINTER;
2657 else
2658 akind = GFC_ARRAY_ALLOCATABLE;
2659 /* Pointers to arrays aren't actually pointer types. The
2660 descriptors are separate, but the data is common. */
2661 field_type = gfc_build_array_type (field_type, c->as, akind,
2662 !c->attr.target
2663 && !c->attr.pointer,
2664 c->attr.contiguous,
2665 codimen);
2667 else
2668 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2669 PACKED_STATIC,
2670 !c->attr.target);
2672 else if ((c->attr.pointer || c->attr.allocatable)
2673 && !c->attr.proc_pointer
2674 && !(unlimited_entity && c == derived->components))
2675 field_type = build_pointer_type (field_type);
2677 if (c->attr.pointer || same_alloc_type)
2678 field_type = gfc_nonrestricted_type (field_type);
2680 /* vtype fields can point to different types to the base type. */
2681 if (c->ts.type == BT_DERIVED
2682 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2683 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2684 ptr_mode, true);
2686 /* Ensure that the CLASS language specific flag is set. */
2687 if (c->ts.type == BT_CLASS)
2689 if (POINTER_TYPE_P (field_type))
2690 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2691 else
2692 GFC_CLASS_TYPE_P (field_type) = 1;
2695 field = gfc_add_field_to_struct (typenode,
2696 get_identifier (c->name),
2697 field_type, &chain);
2698 if (c->loc.lb)
2699 gfc_set_decl_location (field, &c->loc);
2700 else if (derived->declared_at.lb)
2701 gfc_set_decl_location (field, &derived->declared_at);
2703 gfc_finish_decl_attrs (field, &c->attr);
2705 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2707 gcc_assert (field);
2708 if (!c->backend_decl)
2709 c->backend_decl = field;
2711 /* Do not add a caf_token field for classes' data components. */
2712 if (codimen && !c->attr.dimension && !c->attr.codimension
2713 && (c->attr.allocatable || c->attr.pointer)
2714 && c->caf_token == NULL_TREE && strcmp ("_data", c->name) != 0)
2716 char caf_name[GFC_MAX_SYMBOL_LEN];
2717 snprintf (caf_name, GFC_MAX_SYMBOL_LEN, "_caf_%s", c->name);
2718 c->caf_token = gfc_add_field_to_struct (typenode,
2719 get_identifier (caf_name),
2720 pvoid_type_node, &chain);
2721 TREE_NO_WARNING (c->caf_token) = 1;
2725 /* Now lay out the derived type, including the fields. */
2726 if (canonical)
2727 TYPE_CANONICAL (typenode) = canonical;
2729 gfc_finish_type (typenode);
2730 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2731 if (derived->module && derived->ns->proc_name
2732 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2734 if (derived->ns->proc_name->backend_decl
2735 && TREE_CODE (derived->ns->proc_name->backend_decl)
2736 == NAMESPACE_DECL)
2738 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2739 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2740 = derived->ns->proc_name->backend_decl;
2744 derived->backend_decl = typenode;
2746 copy_derived_types:
2748 for (dt = gfc_derived_types; dt; dt = dt->next)
2749 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2751 return derived->backend_decl;
2756 gfc_return_by_reference (gfc_symbol * sym)
2758 if (!sym->attr.function)
2759 return 0;
2761 if (sym->attr.dimension)
2762 return 1;
2764 if (sym->ts.type == BT_CHARACTER
2765 && !sym->attr.is_bind_c
2766 && (!sym->attr.result
2767 || !sym->ns->proc_name
2768 || !sym->ns->proc_name->attr.is_bind_c))
2769 return 1;
2771 /* Possibly return complex numbers by reference for g77 compatibility.
2772 We don't do this for calls to intrinsics (as the library uses the
2773 -fno-f2c calling convention), nor for calls to functions which always
2774 require an explicit interface, as no compatibility problems can
2775 arise there. */
2776 if (flag_f2c && sym->ts.type == BT_COMPLEX
2777 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2778 return 1;
2780 return 0;
2783 static tree
2784 gfc_get_mixed_entry_union (gfc_namespace *ns)
2786 tree type;
2787 tree *chain = NULL;
2788 char name[GFC_MAX_SYMBOL_LEN + 1];
2789 gfc_entry_list *el, *el2;
2791 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2792 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2794 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2796 /* Build the type node. */
2797 type = make_node (UNION_TYPE);
2799 TYPE_NAME (type) = get_identifier (name);
2801 for (el = ns->entries; el; el = el->next)
2803 /* Search for duplicates. */
2804 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2805 if (el2->sym->result == el->sym->result)
2806 break;
2808 if (el == el2)
2809 gfc_add_field_to_struct_1 (type,
2810 get_identifier (el->sym->result->name),
2811 gfc_sym_type (el->sym->result), &chain);
2814 /* Finish off the type. */
2815 gfc_finish_type (type);
2816 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2817 return type;
2820 /* Create a "fn spec" based on the formal arguments;
2821 cf. create_function_arglist. */
2823 static tree
2824 create_fn_spec (gfc_symbol *sym, tree fntype)
2826 char spec[150];
2827 size_t spec_len;
2828 gfc_formal_arglist *f;
2829 tree tmp;
2831 memset (&spec, 0, sizeof (spec));
2832 spec[0] = '.';
2833 spec_len = 1;
2835 if (sym->attr.entry_master)
2836 spec[spec_len++] = 'R';
2837 if (gfc_return_by_reference (sym))
2839 gfc_symbol *result = sym->result ? sym->result : sym;
2841 if (result->attr.pointer || sym->attr.proc_pointer)
2842 spec[spec_len++] = '.';
2843 else
2844 spec[spec_len++] = 'w';
2845 if (sym->ts.type == BT_CHARACTER)
2846 spec[spec_len++] = 'R';
2849 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2850 if (spec_len < sizeof (spec))
2852 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2853 || f->sym->attr.external || f->sym->attr.cray_pointer
2854 || (f->sym->ts.type == BT_DERIVED
2855 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2856 || f->sym->ts.u.derived->attr.pointer_comp))
2857 || (f->sym->ts.type == BT_CLASS
2858 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2859 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2860 spec[spec_len++] = '.';
2861 else if (f->sym->attr.intent == INTENT_IN)
2862 spec[spec_len++] = 'r';
2863 else if (f->sym)
2864 spec[spec_len++] = 'w';
2867 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2868 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2869 return build_type_attribute_variant (fntype, tmp);
2873 tree
2874 gfc_get_function_type (gfc_symbol * sym)
2876 tree type;
2877 vec<tree, va_gc> *typelist = NULL;
2878 gfc_formal_arglist *f;
2879 gfc_symbol *arg;
2880 int alternate_return = 0;
2881 bool is_varargs = true;
2883 /* Make sure this symbol is a function, a subroutine or the main
2884 program. */
2885 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2886 || sym->attr.flavor == FL_PROGRAM);
2888 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2889 so that they can be detected here and handled further down. */
2890 if (sym->backend_decl == NULL)
2891 sym->backend_decl = error_mark_node;
2892 else if (sym->backend_decl == error_mark_node)
2893 goto arg_type_list_done;
2894 else if (sym->attr.proc_pointer)
2895 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2896 else
2897 return TREE_TYPE (sym->backend_decl);
2899 if (sym->attr.entry_master)
2900 /* Additional parameter for selecting an entry point. */
2901 vec_safe_push (typelist, gfc_array_index_type);
2903 if (sym->result)
2904 arg = sym->result;
2905 else
2906 arg = sym;
2908 if (arg->ts.type == BT_CHARACTER)
2909 gfc_conv_const_charlen (arg->ts.u.cl);
2911 /* Some functions we use an extra parameter for the return value. */
2912 if (gfc_return_by_reference (sym))
2914 type = gfc_sym_type (arg);
2915 if (arg->ts.type == BT_COMPLEX
2916 || arg->attr.dimension
2917 || arg->ts.type == BT_CHARACTER)
2918 type = build_reference_type (type);
2920 vec_safe_push (typelist, type);
2921 if (arg->ts.type == BT_CHARACTER)
2923 if (!arg->ts.deferred)
2924 /* Transfer by value. */
2925 vec_safe_push (typelist, gfc_charlen_type_node);
2926 else
2927 /* Deferred character lengths are transferred by reference
2928 so that the value can be returned. */
2929 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2933 /* Build the argument types for the function. */
2934 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2936 arg = f->sym;
2937 if (arg)
2939 /* Evaluate constant character lengths here so that they can be
2940 included in the type. */
2941 if (arg->ts.type == BT_CHARACTER)
2942 gfc_conv_const_charlen (arg->ts.u.cl);
2944 if (arg->attr.flavor == FL_PROCEDURE)
2946 type = gfc_get_function_type (arg);
2947 type = build_pointer_type (type);
2949 else
2950 type = gfc_sym_type (arg);
2952 /* Parameter Passing Convention
2954 We currently pass all parameters by reference.
2955 Parameters with INTENT(IN) could be passed by value.
2956 The problem arises if a function is called via an implicit
2957 prototype. In this situation the INTENT is not known.
2958 For this reason all parameters to global functions must be
2959 passed by reference. Passing by value would potentially
2960 generate bad code. Worse there would be no way of telling that
2961 this code was bad, except that it would give incorrect results.
2963 Contained procedures could pass by value as these are never
2964 used without an explicit interface, and cannot be passed as
2965 actual parameters for a dummy procedure. */
2967 vec_safe_push (typelist, type);
2969 else
2971 if (sym->attr.subroutine)
2972 alternate_return = 1;
2976 /* Add hidden string length parameters. */
2977 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2979 arg = f->sym;
2980 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2982 if (!arg->ts.deferred)
2983 /* Transfer by value. */
2984 type = gfc_charlen_type_node;
2985 else
2986 /* Deferred character lengths are transferred by reference
2987 so that the value can be returned. */
2988 type = build_pointer_type (gfc_charlen_type_node);
2990 vec_safe_push (typelist, type);
2994 if (!vec_safe_is_empty (typelist)
2995 || sym->attr.is_main_program
2996 || sym->attr.if_source != IFSRC_UNKNOWN)
2997 is_varargs = false;
2999 if (sym->backend_decl == error_mark_node)
3000 sym->backend_decl = NULL_TREE;
3002 arg_type_list_done:
3004 if (alternate_return)
3005 type = integer_type_node;
3006 else if (!sym->attr.function || gfc_return_by_reference (sym))
3007 type = void_type_node;
3008 else if (sym->attr.mixed_entry_master)
3009 type = gfc_get_mixed_entry_union (sym->ns);
3010 else if (flag_f2c && sym->ts.type == BT_REAL
3011 && sym->ts.kind == gfc_default_real_kind
3012 && !sym->attr.always_explicit)
3014 /* Special case: f2c calling conventions require that (scalar)
3015 default REAL functions return the C type double instead. f2c
3016 compatibility is only an issue with functions that don't
3017 require an explicit interface, as only these could be
3018 implemented in Fortran 77. */
3019 sym->ts.kind = gfc_default_double_kind;
3020 type = gfc_typenode_for_spec (&sym->ts);
3021 sym->ts.kind = gfc_default_real_kind;
3023 else if (sym->result && sym->result->attr.proc_pointer)
3024 /* Procedure pointer return values. */
3026 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3028 /* Unset proc_pointer as gfc_get_function_type
3029 is called recursively. */
3030 sym->result->attr.proc_pointer = 0;
3031 type = build_pointer_type (gfc_get_function_type (sym->result));
3032 sym->result->attr.proc_pointer = 1;
3034 else
3035 type = gfc_sym_type (sym->result);
3037 else
3038 type = gfc_sym_type (sym);
3040 if (is_varargs)
3041 type = build_varargs_function_type_vec (type, typelist);
3042 else
3043 type = build_function_type_vec (type, typelist);
3044 type = create_fn_spec (sym, type);
3046 return type;
3049 /* Language hooks for middle-end access to type nodes. */
3051 /* Return an integer type with BITS bits of precision,
3052 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3054 tree
3055 gfc_type_for_size (unsigned bits, int unsignedp)
3057 if (!unsignedp)
3059 int i;
3060 for (i = 0; i <= MAX_INT_KINDS; ++i)
3062 tree type = gfc_integer_types[i];
3063 if (type && bits == TYPE_PRECISION (type))
3064 return type;
3067 /* Handle TImode as a special case because it is used by some backends
3068 (e.g. ARM) even though it is not available for normal use. */
3069 #if HOST_BITS_PER_WIDE_INT >= 64
3070 if (bits == TYPE_PRECISION (intTI_type_node))
3071 return intTI_type_node;
3072 #endif
3074 if (bits <= TYPE_PRECISION (intQI_type_node))
3075 return intQI_type_node;
3076 if (bits <= TYPE_PRECISION (intHI_type_node))
3077 return intHI_type_node;
3078 if (bits <= TYPE_PRECISION (intSI_type_node))
3079 return intSI_type_node;
3080 if (bits <= TYPE_PRECISION (intDI_type_node))
3081 return intDI_type_node;
3082 if (bits <= TYPE_PRECISION (intTI_type_node))
3083 return intTI_type_node;
3085 else
3087 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3088 return unsigned_intQI_type_node;
3089 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3090 return unsigned_intHI_type_node;
3091 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3092 return unsigned_intSI_type_node;
3093 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3094 return unsigned_intDI_type_node;
3095 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3096 return unsigned_intTI_type_node;
3099 return NULL_TREE;
3102 /* Return a data type that has machine mode MODE. If the mode is an
3103 integer, then UNSIGNEDP selects between signed and unsigned types. */
3105 tree
3106 gfc_type_for_mode (machine_mode mode, int unsignedp)
3108 int i;
3109 tree *base;
3111 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3112 base = gfc_real_types;
3113 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3114 base = gfc_complex_types;
3115 else if (SCALAR_INT_MODE_P (mode))
3117 tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
3118 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3120 else if (VECTOR_MODE_P (mode))
3122 machine_mode inner_mode = GET_MODE_INNER (mode);
3123 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3124 if (inner_type != NULL_TREE)
3125 return build_vector_type_for_mode (inner_type, mode);
3126 return NULL_TREE;
3128 else
3129 return NULL_TREE;
3131 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3133 tree type = base[i];
3134 if (type && mode == TYPE_MODE (type))
3135 return type;
3138 return NULL_TREE;
3141 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3142 in that case. */
3144 bool
3145 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3147 int rank, dim;
3148 bool indirect = false;
3149 tree etype, ptype, field, t, base_decl;
3150 tree data_off, dim_off, dtype_off, dim_size, elem_size;
3151 tree lower_suboff, upper_suboff, stride_suboff;
3153 if (! GFC_DESCRIPTOR_TYPE_P (type))
3155 if (! POINTER_TYPE_P (type))
3156 return false;
3157 type = TREE_TYPE (type);
3158 if (! GFC_DESCRIPTOR_TYPE_P (type))
3159 return false;
3160 indirect = true;
3163 rank = GFC_TYPE_ARRAY_RANK (type);
3164 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3165 return false;
3167 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3168 gcc_assert (POINTER_TYPE_P (etype));
3169 etype = TREE_TYPE (etype);
3171 /* If the type is not a scalar coarray. */
3172 if (TREE_CODE (etype) == ARRAY_TYPE)
3173 etype = TREE_TYPE (etype);
3175 /* Can't handle variable sized elements yet. */
3176 if (int_size_in_bytes (etype) <= 0)
3177 return false;
3178 /* Nor non-constant lower bounds in assumed shape arrays. */
3179 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3180 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3182 for (dim = 0; dim < rank; dim++)
3183 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3184 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3185 return false;
3188 memset (info, '\0', sizeof (*info));
3189 info->ndimensions = rank;
3190 info->ordering = array_descr_ordering_column_major;
3191 info->element_type = etype;
3192 ptype = build_pointer_type (gfc_array_index_type);
3193 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3194 if (!base_decl)
3196 base_decl = make_node (DEBUG_EXPR_DECL);
3197 DECL_ARTIFICIAL (base_decl) = 1;
3198 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3199 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3200 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3202 info->base_decl = base_decl;
3203 if (indirect)
3204 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3206 if (GFC_TYPE_ARRAY_SPAN (type))
3207 elem_size = GFC_TYPE_ARRAY_SPAN (type);
3208 else
3209 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3210 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
3211 data_off = byte_position (field);
3212 field = DECL_CHAIN (field);
3213 field = DECL_CHAIN (field);
3214 dtype_off = byte_position (field);
3215 field = DECL_CHAIN (field);
3216 dim_off = byte_position (field);
3217 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
3218 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
3219 stride_suboff = byte_position (field);
3220 field = DECL_CHAIN (field);
3221 lower_suboff = byte_position (field);
3222 field = DECL_CHAIN (field);
3223 upper_suboff = byte_position (field);
3225 t = base_decl;
3226 if (!integer_zerop (data_off))
3227 t = fold_build_pointer_plus (t, data_off);
3228 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3229 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3230 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3231 info->allocated = build2 (NE_EXPR, boolean_type_node,
3232 info->data_location, null_pointer_node);
3233 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3234 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3235 info->associated = build2 (NE_EXPR, boolean_type_node,
3236 info->data_location, null_pointer_node);
3237 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3238 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3239 && dwarf_version >= 5)
3241 rank = 1;
3242 info->ndimensions = 1;
3243 t = base_decl;
3244 if (!integer_zerop (dtype_off))
3245 t = fold_build_pointer_plus (t, dtype_off);
3246 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3247 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3248 info->rank = build2 (BIT_AND_EXPR, gfc_array_index_type, t,
3249 build_int_cst (gfc_array_index_type,
3250 GFC_DTYPE_RANK_MASK));
3251 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3252 t = size_binop (MULT_EXPR, t, dim_size);
3253 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3256 for (dim = 0; dim < rank; dim++)
3258 t = fold_build_pointer_plus (base_decl,
3259 size_binop (PLUS_EXPR,
3260 dim_off, lower_suboff));
3261 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3262 info->dimen[dim].lower_bound = t;
3263 t = fold_build_pointer_plus (base_decl,
3264 size_binop (PLUS_EXPR,
3265 dim_off, upper_suboff));
3266 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3267 info->dimen[dim].upper_bound = t;
3268 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3269 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3271 /* Assumed shape arrays have known lower bounds. */
3272 info->dimen[dim].upper_bound
3273 = build2 (MINUS_EXPR, gfc_array_index_type,
3274 info->dimen[dim].upper_bound,
3275 info->dimen[dim].lower_bound);
3276 info->dimen[dim].lower_bound
3277 = fold_convert (gfc_array_index_type,
3278 GFC_TYPE_ARRAY_LBOUND (type, dim));
3279 info->dimen[dim].upper_bound
3280 = build2 (PLUS_EXPR, gfc_array_index_type,
3281 info->dimen[dim].lower_bound,
3282 info->dimen[dim].upper_bound);
3284 t = fold_build_pointer_plus (base_decl,
3285 size_binop (PLUS_EXPR,
3286 dim_off, stride_suboff));
3287 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3288 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3289 info->dimen[dim].stride = t;
3290 if (dim + 1 < rank)
3291 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3294 return true;
3298 /* Create a type to handle vector subscripts for coarray library calls. It
3299 has the form:
3300 struct caf_vector_t {
3301 size_t nvec; // size of the vector
3302 union {
3303 struct {
3304 void *vector;
3305 int kind;
3306 } v;
3307 struct {
3308 ptrdiff_t lower_bound;
3309 ptrdiff_t upper_bound;
3310 ptrdiff_t stride;
3311 } triplet;
3312 } u;
3314 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3315 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3317 tree
3318 gfc_get_caf_vector_type (int dim)
3320 static tree vector_types[GFC_MAX_DIMENSIONS];
3321 static tree vec_type = NULL_TREE;
3322 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3324 if (vector_types[dim-1] != NULL_TREE)
3325 return vector_types[dim-1];
3327 if (vec_type == NULL_TREE)
3329 chain = 0;
3330 vect_struct_type = make_node (RECORD_TYPE);
3331 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3332 get_identifier ("vector"),
3333 pvoid_type_node, &chain);
3334 TREE_NO_WARNING (tmp) = 1;
3335 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3336 get_identifier ("kind"),
3337 integer_type_node, &chain);
3338 TREE_NO_WARNING (tmp) = 1;
3339 gfc_finish_type (vect_struct_type);
3341 chain = 0;
3342 triplet_struct_type = make_node (RECORD_TYPE);
3343 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3344 get_identifier ("lower_bound"),
3345 gfc_array_index_type, &chain);
3346 TREE_NO_WARNING (tmp) = 1;
3347 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3348 get_identifier ("upper_bound"),
3349 gfc_array_index_type, &chain);
3350 TREE_NO_WARNING (tmp) = 1;
3351 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3352 gfc_array_index_type, &chain);
3353 TREE_NO_WARNING (tmp) = 1;
3354 gfc_finish_type (triplet_struct_type);
3356 chain = 0;
3357 union_type = make_node (UNION_TYPE);
3358 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3359 vect_struct_type, &chain);
3360 TREE_NO_WARNING (tmp) = 1;
3361 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3362 triplet_struct_type, &chain);
3363 TREE_NO_WARNING (tmp) = 1;
3364 gfc_finish_type (union_type);
3366 chain = 0;
3367 vec_type = make_node (RECORD_TYPE);
3368 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3369 size_type_node, &chain);
3370 TREE_NO_WARNING (tmp) = 1;
3371 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3372 union_type, &chain);
3373 TREE_NO_WARNING (tmp) = 1;
3374 gfc_finish_type (vec_type);
3375 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3378 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3379 gfc_rank_cst[dim-1]);
3380 vector_types[dim-1] = build_array_type (vec_type, tmp);
3381 return vector_types[dim-1];
3385 tree
3386 gfc_get_caf_reference_type ()
3388 static tree reference_type = NULL_TREE;
3389 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3390 a_struct_type, u_union_type, tmp, *chain;
3392 if (reference_type != NULL_TREE)
3393 return reference_type;
3395 chain = 0;
3396 c_struct_type = make_node (RECORD_TYPE);
3397 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3398 get_identifier ("offset"),
3399 gfc_array_index_type, &chain);
3400 TREE_NO_WARNING (tmp) = 1;
3401 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3402 get_identifier ("caf_token_offset"),
3403 gfc_array_index_type, &chain);
3404 TREE_NO_WARNING (tmp) = 1;
3405 gfc_finish_type (c_struct_type);
3407 chain = 0;
3408 s_struct_type = make_node (RECORD_TYPE);
3409 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3410 get_identifier ("start"),
3411 gfc_array_index_type, &chain);
3412 TREE_NO_WARNING (tmp) = 1;
3413 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3414 get_identifier ("end"),
3415 gfc_array_index_type, &chain);
3416 TREE_NO_WARNING (tmp) = 1;
3417 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3418 get_identifier ("stride"),
3419 gfc_array_index_type, &chain);
3420 TREE_NO_WARNING (tmp) = 1;
3421 gfc_finish_type (s_struct_type);
3423 chain = 0;
3424 v_struct_type = make_node (RECORD_TYPE);
3425 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3426 get_identifier ("vector"),
3427 pvoid_type_node, &chain);
3428 TREE_NO_WARNING (tmp) = 1;
3429 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3430 get_identifier ("nvec"),
3431 size_type_node, &chain);
3432 TREE_NO_WARNING (tmp) = 1;
3433 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3434 get_identifier ("kind"),
3435 integer_type_node, &chain);
3436 TREE_NO_WARNING (tmp) = 1;
3437 gfc_finish_type (v_struct_type);
3439 chain = 0;
3440 union_type = make_node (UNION_TYPE);
3441 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3442 s_struct_type, &chain);
3443 TREE_NO_WARNING (tmp) = 1;
3444 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3445 v_struct_type, &chain);
3446 TREE_NO_WARNING (tmp) = 1;
3447 gfc_finish_type (union_type);
3449 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3450 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3451 dim_union_type = build_array_type (union_type, tmp);
3453 chain = 0;
3454 a_struct_type = make_node (RECORD_TYPE);
3455 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3456 build_array_type (unsigned_char_type_node,
3457 build_range_type (gfc_array_index_type,
3458 gfc_index_zero_node,
3459 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3460 &chain);
3461 TREE_NO_WARNING (tmp) = 1;
3462 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3463 get_identifier ("static_array_type"),
3464 integer_type_node, &chain);
3465 TREE_NO_WARNING (tmp) = 1;
3466 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3467 dim_union_type, &chain);
3468 TREE_NO_WARNING (tmp) = 1;
3469 gfc_finish_type (a_struct_type);
3471 chain = 0;
3472 u_union_type = make_node (UNION_TYPE);
3473 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3474 c_struct_type, &chain);
3475 TREE_NO_WARNING (tmp) = 1;
3476 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3477 a_struct_type, &chain);
3478 TREE_NO_WARNING (tmp) = 1;
3479 gfc_finish_type (u_union_type);
3481 chain = 0;
3482 reference_type = make_node (RECORD_TYPE);
3483 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3484 build_pointer_type (reference_type), &chain);
3485 TREE_NO_WARNING (tmp) = 1;
3486 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3487 integer_type_node, &chain);
3488 TREE_NO_WARNING (tmp) = 1;
3489 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3490 size_type_node, &chain);
3491 TREE_NO_WARNING (tmp) = 1;
3492 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3493 u_union_type, &chain);
3494 TREE_NO_WARNING (tmp) = 1;
3495 gfc_finish_type (reference_type);
3496 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3498 return reference_type;
3501 #include "gt-fortran-trans-types.h"