* de.po: Update.
[official-gcc.git] / gcc / fortran / trans-types.c
blobfc5e486379f9bf067c85f6e0dcb32e47ba792077
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.type != BT_CLASS)
2621 || (c->ts.u.derived
2622 && !gfc_compare_derived_types (derived, c->ts.u.derived))))
2623 field_type = gfc_get_ppc_type (c);
2624 else if (c->attr.proc_pointer && derived->backend_decl)
2626 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2627 field_type = build_pointer_type (tmp);
2629 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2630 field_type = c->ts.u.derived->backend_decl;
2631 else
2633 if (c->ts.type == BT_CHARACTER && !c->ts.deferred)
2635 /* Evaluate the string length. */
2636 gfc_conv_const_charlen (c->ts.u.cl);
2637 gcc_assert (c->ts.u.cl->backend_decl);
2639 else if (c->ts.type == BT_CHARACTER)
2640 c->ts.u.cl->backend_decl
2641 = build_int_cst (gfc_charlen_type_node, 0);
2643 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2646 /* This returns an array descriptor type. Initialization may be
2647 required. */
2648 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2650 if (c->attr.pointer || c->attr.allocatable)
2652 enum gfc_array_kind akind;
2653 if (c->attr.pointer)
2654 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2655 : GFC_ARRAY_POINTER;
2656 else
2657 akind = GFC_ARRAY_ALLOCATABLE;
2658 /* Pointers to arrays aren't actually pointer types. The
2659 descriptors are separate, but the data is common. */
2660 field_type = gfc_build_array_type (field_type, c->as, akind,
2661 !c->attr.target
2662 && !c->attr.pointer,
2663 c->attr.contiguous,
2664 codimen);
2666 else
2667 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2668 PACKED_STATIC,
2669 !c->attr.target);
2671 else if ((c->attr.pointer || c->attr.allocatable)
2672 && !c->attr.proc_pointer
2673 && !(unlimited_entity && c == derived->components))
2674 field_type = build_pointer_type (field_type);
2676 if (c->attr.pointer || same_alloc_type)
2677 field_type = gfc_nonrestricted_type (field_type);
2679 /* vtype fields can point to different types to the base type. */
2680 if (c->ts.type == BT_DERIVED
2681 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2682 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2683 ptr_mode, true);
2685 /* Ensure that the CLASS language specific flag is set. */
2686 if (c->ts.type == BT_CLASS)
2688 if (POINTER_TYPE_P (field_type))
2689 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2690 else
2691 GFC_CLASS_TYPE_P (field_type) = 1;
2694 field = gfc_add_field_to_struct (typenode,
2695 get_identifier (c->name),
2696 field_type, &chain);
2697 if (c->loc.lb)
2698 gfc_set_decl_location (field, &c->loc);
2699 else if (derived->declared_at.lb)
2700 gfc_set_decl_location (field, &derived->declared_at);
2702 gfc_finish_decl_attrs (field, &c->attr);
2704 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2706 gcc_assert (field);
2707 if (!c->backend_decl)
2708 c->backend_decl = field;
2710 /* Do not add a caf_token field for classes' data components. */
2711 if (codimen && !c->attr.dimension && !c->attr.codimension
2712 && (c->attr.allocatable || c->attr.pointer)
2713 && c->caf_token == NULL_TREE && strcmp ("_data", c->name) != 0)
2715 char caf_name[GFC_MAX_SYMBOL_LEN];
2716 snprintf (caf_name, GFC_MAX_SYMBOL_LEN, "_caf_%s", c->name);
2717 c->caf_token = gfc_add_field_to_struct (typenode,
2718 get_identifier (caf_name),
2719 pvoid_type_node, &chain);
2720 TREE_NO_WARNING (c->caf_token) = 1;
2724 /* Now lay out the derived type, including the fields. */
2725 if (canonical)
2726 TYPE_CANONICAL (typenode) = canonical;
2728 gfc_finish_type (typenode);
2729 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2730 if (derived->module && derived->ns->proc_name
2731 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2733 if (derived->ns->proc_name->backend_decl
2734 && TREE_CODE (derived->ns->proc_name->backend_decl)
2735 == NAMESPACE_DECL)
2737 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2738 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2739 = derived->ns->proc_name->backend_decl;
2743 derived->backend_decl = typenode;
2745 copy_derived_types:
2747 for (dt = gfc_derived_types; dt; dt = dt->next)
2748 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2750 return derived->backend_decl;
2755 gfc_return_by_reference (gfc_symbol * sym)
2757 if (!sym->attr.function)
2758 return 0;
2760 if (sym->attr.dimension)
2761 return 1;
2763 if (sym->ts.type == BT_CHARACTER
2764 && !sym->attr.is_bind_c
2765 && (!sym->attr.result
2766 || !sym->ns->proc_name
2767 || !sym->ns->proc_name->attr.is_bind_c))
2768 return 1;
2770 /* Possibly return complex numbers by reference for g77 compatibility.
2771 We don't do this for calls to intrinsics (as the library uses the
2772 -fno-f2c calling convention), nor for calls to functions which always
2773 require an explicit interface, as no compatibility problems can
2774 arise there. */
2775 if (flag_f2c && sym->ts.type == BT_COMPLEX
2776 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2777 return 1;
2779 return 0;
2782 static tree
2783 gfc_get_mixed_entry_union (gfc_namespace *ns)
2785 tree type;
2786 tree *chain = NULL;
2787 char name[GFC_MAX_SYMBOL_LEN + 1];
2788 gfc_entry_list *el, *el2;
2790 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2791 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2793 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2795 /* Build the type node. */
2796 type = make_node (UNION_TYPE);
2798 TYPE_NAME (type) = get_identifier (name);
2800 for (el = ns->entries; el; el = el->next)
2802 /* Search for duplicates. */
2803 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2804 if (el2->sym->result == el->sym->result)
2805 break;
2807 if (el == el2)
2808 gfc_add_field_to_struct_1 (type,
2809 get_identifier (el->sym->result->name),
2810 gfc_sym_type (el->sym->result), &chain);
2813 /* Finish off the type. */
2814 gfc_finish_type (type);
2815 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2816 return type;
2819 /* Create a "fn spec" based on the formal arguments;
2820 cf. create_function_arglist. */
2822 static tree
2823 create_fn_spec (gfc_symbol *sym, tree fntype)
2825 char spec[150];
2826 size_t spec_len;
2827 gfc_formal_arglist *f;
2828 tree tmp;
2830 memset (&spec, 0, sizeof (spec));
2831 spec[0] = '.';
2832 spec_len = 1;
2834 if (sym->attr.entry_master)
2835 spec[spec_len++] = 'R';
2836 if (gfc_return_by_reference (sym))
2838 gfc_symbol *result = sym->result ? sym->result : sym;
2840 if (result->attr.pointer || sym->attr.proc_pointer)
2841 spec[spec_len++] = '.';
2842 else
2843 spec[spec_len++] = 'w';
2844 if (sym->ts.type == BT_CHARACTER)
2845 spec[spec_len++] = 'R';
2848 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2849 if (spec_len < sizeof (spec))
2851 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2852 || f->sym->attr.external || f->sym->attr.cray_pointer
2853 || (f->sym->ts.type == BT_DERIVED
2854 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2855 || f->sym->ts.u.derived->attr.pointer_comp))
2856 || (f->sym->ts.type == BT_CLASS
2857 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2858 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2859 spec[spec_len++] = '.';
2860 else if (f->sym->attr.intent == INTENT_IN)
2861 spec[spec_len++] = 'r';
2862 else if (f->sym)
2863 spec[spec_len++] = 'w';
2866 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2867 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2868 return build_type_attribute_variant (fntype, tmp);
2872 tree
2873 gfc_get_function_type (gfc_symbol * sym)
2875 tree type;
2876 vec<tree, va_gc> *typelist = NULL;
2877 gfc_formal_arglist *f;
2878 gfc_symbol *arg;
2879 int alternate_return = 0;
2880 bool is_varargs = true;
2882 /* Make sure this symbol is a function, a subroutine or the main
2883 program. */
2884 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2885 || sym->attr.flavor == FL_PROGRAM);
2887 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2888 so that they can be detected here and handled further down. */
2889 if (sym->backend_decl == NULL)
2890 sym->backend_decl = error_mark_node;
2891 else if (sym->backend_decl == error_mark_node)
2892 goto arg_type_list_done;
2893 else if (sym->attr.proc_pointer)
2894 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2895 else
2896 return TREE_TYPE (sym->backend_decl);
2898 if (sym->attr.entry_master)
2899 /* Additional parameter for selecting an entry point. */
2900 vec_safe_push (typelist, gfc_array_index_type);
2902 if (sym->result)
2903 arg = sym->result;
2904 else
2905 arg = sym;
2907 if (arg->ts.type == BT_CHARACTER)
2908 gfc_conv_const_charlen (arg->ts.u.cl);
2910 /* Some functions we use an extra parameter for the return value. */
2911 if (gfc_return_by_reference (sym))
2913 type = gfc_sym_type (arg);
2914 if (arg->ts.type == BT_COMPLEX
2915 || arg->attr.dimension
2916 || arg->ts.type == BT_CHARACTER)
2917 type = build_reference_type (type);
2919 vec_safe_push (typelist, type);
2920 if (arg->ts.type == BT_CHARACTER)
2922 if (!arg->ts.deferred)
2923 /* Transfer by value. */
2924 vec_safe_push (typelist, gfc_charlen_type_node);
2925 else
2926 /* Deferred character lengths are transferred by reference
2927 so that the value can be returned. */
2928 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2932 /* Build the argument types for the function. */
2933 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2935 arg = f->sym;
2936 if (arg)
2938 /* Evaluate constant character lengths here so that they can be
2939 included in the type. */
2940 if (arg->ts.type == BT_CHARACTER)
2941 gfc_conv_const_charlen (arg->ts.u.cl);
2943 if (arg->attr.flavor == FL_PROCEDURE)
2945 type = gfc_get_function_type (arg);
2946 type = build_pointer_type (type);
2948 else
2949 type = gfc_sym_type (arg);
2951 /* Parameter Passing Convention
2953 We currently pass all parameters by reference.
2954 Parameters with INTENT(IN) could be passed by value.
2955 The problem arises if a function is called via an implicit
2956 prototype. In this situation the INTENT is not known.
2957 For this reason all parameters to global functions must be
2958 passed by reference. Passing by value would potentially
2959 generate bad code. Worse there would be no way of telling that
2960 this code was bad, except that it would give incorrect results.
2962 Contained procedures could pass by value as these are never
2963 used without an explicit interface, and cannot be passed as
2964 actual parameters for a dummy procedure. */
2966 vec_safe_push (typelist, type);
2968 else
2970 if (sym->attr.subroutine)
2971 alternate_return = 1;
2975 /* Add hidden string length parameters. */
2976 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2978 arg = f->sym;
2979 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2981 if (!arg->ts.deferred)
2982 /* Transfer by value. */
2983 type = gfc_charlen_type_node;
2984 else
2985 /* Deferred character lengths are transferred by reference
2986 so that the value can be returned. */
2987 type = build_pointer_type (gfc_charlen_type_node);
2989 vec_safe_push (typelist, type);
2993 if (!vec_safe_is_empty (typelist)
2994 || sym->attr.is_main_program
2995 || sym->attr.if_source != IFSRC_UNKNOWN)
2996 is_varargs = false;
2998 if (sym->backend_decl == error_mark_node)
2999 sym->backend_decl = NULL_TREE;
3001 arg_type_list_done:
3003 if (alternate_return)
3004 type = integer_type_node;
3005 else if (!sym->attr.function || gfc_return_by_reference (sym))
3006 type = void_type_node;
3007 else if (sym->attr.mixed_entry_master)
3008 type = gfc_get_mixed_entry_union (sym->ns);
3009 else if (flag_f2c && sym->ts.type == BT_REAL
3010 && sym->ts.kind == gfc_default_real_kind
3011 && !sym->attr.always_explicit)
3013 /* Special case: f2c calling conventions require that (scalar)
3014 default REAL functions return the C type double instead. f2c
3015 compatibility is only an issue with functions that don't
3016 require an explicit interface, as only these could be
3017 implemented in Fortran 77. */
3018 sym->ts.kind = gfc_default_double_kind;
3019 type = gfc_typenode_for_spec (&sym->ts);
3020 sym->ts.kind = gfc_default_real_kind;
3022 else if (sym->result && sym->result->attr.proc_pointer)
3023 /* Procedure pointer return values. */
3025 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3027 /* Unset proc_pointer as gfc_get_function_type
3028 is called recursively. */
3029 sym->result->attr.proc_pointer = 0;
3030 type = build_pointer_type (gfc_get_function_type (sym->result));
3031 sym->result->attr.proc_pointer = 1;
3033 else
3034 type = gfc_sym_type (sym->result);
3036 else
3037 type = gfc_sym_type (sym);
3039 if (is_varargs)
3040 type = build_varargs_function_type_vec (type, typelist);
3041 else
3042 type = build_function_type_vec (type, typelist);
3043 type = create_fn_spec (sym, type);
3045 return type;
3048 /* Language hooks for middle-end access to type nodes. */
3050 /* Return an integer type with BITS bits of precision,
3051 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3053 tree
3054 gfc_type_for_size (unsigned bits, int unsignedp)
3056 if (!unsignedp)
3058 int i;
3059 for (i = 0; i <= MAX_INT_KINDS; ++i)
3061 tree type = gfc_integer_types[i];
3062 if (type && bits == TYPE_PRECISION (type))
3063 return type;
3066 /* Handle TImode as a special case because it is used by some backends
3067 (e.g. ARM) even though it is not available for normal use. */
3068 #if HOST_BITS_PER_WIDE_INT >= 64
3069 if (bits == TYPE_PRECISION (intTI_type_node))
3070 return intTI_type_node;
3071 #endif
3073 if (bits <= TYPE_PRECISION (intQI_type_node))
3074 return intQI_type_node;
3075 if (bits <= TYPE_PRECISION (intHI_type_node))
3076 return intHI_type_node;
3077 if (bits <= TYPE_PRECISION (intSI_type_node))
3078 return intSI_type_node;
3079 if (bits <= TYPE_PRECISION (intDI_type_node))
3080 return intDI_type_node;
3081 if (bits <= TYPE_PRECISION (intTI_type_node))
3082 return intTI_type_node;
3084 else
3086 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3087 return unsigned_intQI_type_node;
3088 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3089 return unsigned_intHI_type_node;
3090 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3091 return unsigned_intSI_type_node;
3092 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3093 return unsigned_intDI_type_node;
3094 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3095 return unsigned_intTI_type_node;
3098 return NULL_TREE;
3101 /* Return a data type that has machine mode MODE. If the mode is an
3102 integer, then UNSIGNEDP selects between signed and unsigned types. */
3104 tree
3105 gfc_type_for_mode (machine_mode mode, int unsignedp)
3107 int i;
3108 tree *base;
3110 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3111 base = gfc_real_types;
3112 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3113 base = gfc_complex_types;
3114 else if (SCALAR_INT_MODE_P (mode))
3116 tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
3117 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3119 else if (VECTOR_MODE_P (mode))
3121 machine_mode inner_mode = GET_MODE_INNER (mode);
3122 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3123 if (inner_type != NULL_TREE)
3124 return build_vector_type_for_mode (inner_type, mode);
3125 return NULL_TREE;
3127 else
3128 return NULL_TREE;
3130 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3132 tree type = base[i];
3133 if (type && mode == TYPE_MODE (type))
3134 return type;
3137 return NULL_TREE;
3140 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3141 in that case. */
3143 bool
3144 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3146 int rank, dim;
3147 bool indirect = false;
3148 tree etype, ptype, field, t, base_decl;
3149 tree data_off, dim_off, dtype_off, dim_size, elem_size;
3150 tree lower_suboff, upper_suboff, stride_suboff;
3152 if (! GFC_DESCRIPTOR_TYPE_P (type))
3154 if (! POINTER_TYPE_P (type))
3155 return false;
3156 type = TREE_TYPE (type);
3157 if (! GFC_DESCRIPTOR_TYPE_P (type))
3158 return false;
3159 indirect = true;
3162 rank = GFC_TYPE_ARRAY_RANK (type);
3163 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3164 return false;
3166 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3167 gcc_assert (POINTER_TYPE_P (etype));
3168 etype = TREE_TYPE (etype);
3170 /* If the type is not a scalar coarray. */
3171 if (TREE_CODE (etype) == ARRAY_TYPE)
3172 etype = TREE_TYPE (etype);
3174 /* Can't handle variable sized elements yet. */
3175 if (int_size_in_bytes (etype) <= 0)
3176 return false;
3177 /* Nor non-constant lower bounds in assumed shape arrays. */
3178 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3179 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3181 for (dim = 0; dim < rank; dim++)
3182 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3183 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3184 return false;
3187 memset (info, '\0', sizeof (*info));
3188 info->ndimensions = rank;
3189 info->ordering = array_descr_ordering_column_major;
3190 info->element_type = etype;
3191 ptype = build_pointer_type (gfc_array_index_type);
3192 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3193 if (!base_decl)
3195 base_decl = make_node (DEBUG_EXPR_DECL);
3196 DECL_ARTIFICIAL (base_decl) = 1;
3197 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3198 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3199 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3201 info->base_decl = base_decl;
3202 if (indirect)
3203 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3205 if (GFC_TYPE_ARRAY_SPAN (type))
3206 elem_size = GFC_TYPE_ARRAY_SPAN (type);
3207 else
3208 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3209 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
3210 data_off = byte_position (field);
3211 field = DECL_CHAIN (field);
3212 field = DECL_CHAIN (field);
3213 dtype_off = byte_position (field);
3214 field = DECL_CHAIN (field);
3215 dim_off = byte_position (field);
3216 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
3217 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
3218 stride_suboff = byte_position (field);
3219 field = DECL_CHAIN (field);
3220 lower_suboff = byte_position (field);
3221 field = DECL_CHAIN (field);
3222 upper_suboff = byte_position (field);
3224 t = base_decl;
3225 if (!integer_zerop (data_off))
3226 t = fold_build_pointer_plus (t, data_off);
3227 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3228 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3229 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3230 info->allocated = build2 (NE_EXPR, boolean_type_node,
3231 info->data_location, null_pointer_node);
3232 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3233 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3234 info->associated = build2 (NE_EXPR, boolean_type_node,
3235 info->data_location, null_pointer_node);
3236 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3237 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3238 && dwarf_version >= 5)
3240 rank = 1;
3241 info->ndimensions = 1;
3242 t = base_decl;
3243 if (!integer_zerop (dtype_off))
3244 t = fold_build_pointer_plus (t, dtype_off);
3245 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3246 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3247 info->rank = build2 (BIT_AND_EXPR, gfc_array_index_type, t,
3248 build_int_cst (gfc_array_index_type,
3249 GFC_DTYPE_RANK_MASK));
3250 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3251 t = size_binop (MULT_EXPR, t, dim_size);
3252 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3255 for (dim = 0; dim < rank; dim++)
3257 t = fold_build_pointer_plus (base_decl,
3258 size_binop (PLUS_EXPR,
3259 dim_off, lower_suboff));
3260 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3261 info->dimen[dim].lower_bound = t;
3262 t = fold_build_pointer_plus (base_decl,
3263 size_binop (PLUS_EXPR,
3264 dim_off, upper_suboff));
3265 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3266 info->dimen[dim].upper_bound = t;
3267 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3268 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3270 /* Assumed shape arrays have known lower bounds. */
3271 info->dimen[dim].upper_bound
3272 = build2 (MINUS_EXPR, gfc_array_index_type,
3273 info->dimen[dim].upper_bound,
3274 info->dimen[dim].lower_bound);
3275 info->dimen[dim].lower_bound
3276 = fold_convert (gfc_array_index_type,
3277 GFC_TYPE_ARRAY_LBOUND (type, dim));
3278 info->dimen[dim].upper_bound
3279 = build2 (PLUS_EXPR, gfc_array_index_type,
3280 info->dimen[dim].lower_bound,
3281 info->dimen[dim].upper_bound);
3283 t = fold_build_pointer_plus (base_decl,
3284 size_binop (PLUS_EXPR,
3285 dim_off, stride_suboff));
3286 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3287 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3288 info->dimen[dim].stride = t;
3289 if (dim + 1 < rank)
3290 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3293 return true;
3297 /* Create a type to handle vector subscripts for coarray library calls. It
3298 has the form:
3299 struct caf_vector_t {
3300 size_t nvec; // size of the vector
3301 union {
3302 struct {
3303 void *vector;
3304 int kind;
3305 } v;
3306 struct {
3307 ptrdiff_t lower_bound;
3308 ptrdiff_t upper_bound;
3309 ptrdiff_t stride;
3310 } triplet;
3311 } u;
3313 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3314 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3316 tree
3317 gfc_get_caf_vector_type (int dim)
3319 static tree vector_types[GFC_MAX_DIMENSIONS];
3320 static tree vec_type = NULL_TREE;
3321 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3323 if (vector_types[dim-1] != NULL_TREE)
3324 return vector_types[dim-1];
3326 if (vec_type == NULL_TREE)
3328 chain = 0;
3329 vect_struct_type = make_node (RECORD_TYPE);
3330 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3331 get_identifier ("vector"),
3332 pvoid_type_node, &chain);
3333 TREE_NO_WARNING (tmp) = 1;
3334 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3335 get_identifier ("kind"),
3336 integer_type_node, &chain);
3337 TREE_NO_WARNING (tmp) = 1;
3338 gfc_finish_type (vect_struct_type);
3340 chain = 0;
3341 triplet_struct_type = make_node (RECORD_TYPE);
3342 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3343 get_identifier ("lower_bound"),
3344 gfc_array_index_type, &chain);
3345 TREE_NO_WARNING (tmp) = 1;
3346 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3347 get_identifier ("upper_bound"),
3348 gfc_array_index_type, &chain);
3349 TREE_NO_WARNING (tmp) = 1;
3350 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3351 gfc_array_index_type, &chain);
3352 TREE_NO_WARNING (tmp) = 1;
3353 gfc_finish_type (triplet_struct_type);
3355 chain = 0;
3356 union_type = make_node (UNION_TYPE);
3357 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3358 vect_struct_type, &chain);
3359 TREE_NO_WARNING (tmp) = 1;
3360 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3361 triplet_struct_type, &chain);
3362 TREE_NO_WARNING (tmp) = 1;
3363 gfc_finish_type (union_type);
3365 chain = 0;
3366 vec_type = make_node (RECORD_TYPE);
3367 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3368 size_type_node, &chain);
3369 TREE_NO_WARNING (tmp) = 1;
3370 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3371 union_type, &chain);
3372 TREE_NO_WARNING (tmp) = 1;
3373 gfc_finish_type (vec_type);
3374 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3377 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3378 gfc_rank_cst[dim-1]);
3379 vector_types[dim-1] = build_array_type (vec_type, tmp);
3380 return vector_types[dim-1];
3384 tree
3385 gfc_get_caf_reference_type ()
3387 static tree reference_type = NULL_TREE;
3388 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3389 a_struct_type, u_union_type, tmp, *chain;
3391 if (reference_type != NULL_TREE)
3392 return reference_type;
3394 chain = 0;
3395 c_struct_type = make_node (RECORD_TYPE);
3396 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3397 get_identifier ("offset"),
3398 gfc_array_index_type, &chain);
3399 TREE_NO_WARNING (tmp) = 1;
3400 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3401 get_identifier ("caf_token_offset"),
3402 gfc_array_index_type, &chain);
3403 TREE_NO_WARNING (tmp) = 1;
3404 gfc_finish_type (c_struct_type);
3406 chain = 0;
3407 s_struct_type = make_node (RECORD_TYPE);
3408 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3409 get_identifier ("start"),
3410 gfc_array_index_type, &chain);
3411 TREE_NO_WARNING (tmp) = 1;
3412 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3413 get_identifier ("end"),
3414 gfc_array_index_type, &chain);
3415 TREE_NO_WARNING (tmp) = 1;
3416 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3417 get_identifier ("stride"),
3418 gfc_array_index_type, &chain);
3419 TREE_NO_WARNING (tmp) = 1;
3420 gfc_finish_type (s_struct_type);
3422 chain = 0;
3423 v_struct_type = make_node (RECORD_TYPE);
3424 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3425 get_identifier ("vector"),
3426 pvoid_type_node, &chain);
3427 TREE_NO_WARNING (tmp) = 1;
3428 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3429 get_identifier ("nvec"),
3430 size_type_node, &chain);
3431 TREE_NO_WARNING (tmp) = 1;
3432 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3433 get_identifier ("kind"),
3434 integer_type_node, &chain);
3435 TREE_NO_WARNING (tmp) = 1;
3436 gfc_finish_type (v_struct_type);
3438 chain = 0;
3439 union_type = make_node (UNION_TYPE);
3440 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3441 s_struct_type, &chain);
3442 TREE_NO_WARNING (tmp) = 1;
3443 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3444 v_struct_type, &chain);
3445 TREE_NO_WARNING (tmp) = 1;
3446 gfc_finish_type (union_type);
3448 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3449 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3450 dim_union_type = build_array_type (union_type, tmp);
3452 chain = 0;
3453 a_struct_type = make_node (RECORD_TYPE);
3454 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3455 build_array_type (unsigned_char_type_node,
3456 build_range_type (gfc_array_index_type,
3457 gfc_index_zero_node,
3458 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3459 &chain);
3460 TREE_NO_WARNING (tmp) = 1;
3461 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3462 get_identifier ("static_array_type"),
3463 integer_type_node, &chain);
3464 TREE_NO_WARNING (tmp) = 1;
3465 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3466 dim_union_type, &chain);
3467 TREE_NO_WARNING (tmp) = 1;
3468 gfc_finish_type (a_struct_type);
3470 chain = 0;
3471 u_union_type = make_node (UNION_TYPE);
3472 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3473 c_struct_type, &chain);
3474 TREE_NO_WARNING (tmp) = 1;
3475 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3476 a_struct_type, &chain);
3477 TREE_NO_WARNING (tmp) = 1;
3478 gfc_finish_type (u_union_type);
3480 chain = 0;
3481 reference_type = make_node (RECORD_TYPE);
3482 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3483 build_pointer_type (reference_type), &chain);
3484 TREE_NO_WARNING (tmp) = 1;
3485 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3486 integer_type_node, &chain);
3487 TREE_NO_WARNING (tmp) = 1;
3488 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3489 size_type_node, &chain);
3490 TREE_NO_WARNING (tmp) = 1;
3491 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3492 u_union_type, &chain);
3493 TREE_NO_WARNING (tmp) = 1;
3494 gfc_finish_type (reference_type);
3495 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3497 return reference_type;
3500 #include "gt-fortran-trans-types.h"