Fix memory leak in tree-vect-slp.c
[official-gcc.git] / gcc / fortran / trans-types.c
blob0079d6cd422cba82b103c87b6555f6598bf3a0f6
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2016 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 float128_type_node = NULL_TREE;
66 tree 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;
237 /* Get the kind number corresponding to a real of given storage size,
238 following the required return values for ISO_FORTRAN_ENV REAL* constants:
239 -2 is returned if we support a kind of larger size, -1 otherwise. */
241 gfc_get_real_kind_from_width_isofortranenv (int size)
243 int i;
245 size /= 8;
247 /* Look for a kind with matching storage size. */
248 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
249 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
250 return gfc_real_kinds[i].kind;
252 /* Look for a kind with larger storage size. */
253 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
254 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
255 return -2;
257 return -1;
262 static int
263 get_int_kind_from_width (int size)
265 int i;
267 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
268 if (gfc_integer_kinds[i].bit_size == size)
269 return gfc_integer_kinds[i].kind;
271 return -2;
274 static int
275 get_int_kind_from_minimal_width (int size)
277 int i;
279 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
280 if (gfc_integer_kinds[i].bit_size >= size)
281 return gfc_integer_kinds[i].kind;
283 return -2;
287 /* Generate the CInteropKind_t objects for the C interoperable
288 kinds. */
290 void
291 gfc_init_c_interop_kinds (void)
293 int i;
295 /* init all pointers in the list to NULL */
296 for (i = 0; i < ISOCBINDING_NUMBER; i++)
298 /* Initialize the name and value fields. */
299 c_interop_kinds_table[i].name[0] = '\0';
300 c_interop_kinds_table[i].value = -100;
301 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
304 #define NAMED_INTCST(a,b,c,d) \
305 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
306 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
307 c_interop_kinds_table[a].value = c;
308 #define NAMED_REALCST(a,b,c,d) \
309 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
310 c_interop_kinds_table[a].f90_type = BT_REAL; \
311 c_interop_kinds_table[a].value = c;
312 #define NAMED_CMPXCST(a,b,c,d) \
313 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
314 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
315 c_interop_kinds_table[a].value = c;
316 #define NAMED_LOGCST(a,b,c) \
317 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
318 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
319 c_interop_kinds_table[a].value = c;
320 #define NAMED_CHARKNDCST(a,b,c) \
321 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
322 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
323 c_interop_kinds_table[a].value = c;
324 #define NAMED_CHARCST(a,b,c) \
325 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
326 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
327 c_interop_kinds_table[a].value = c;
328 #define DERIVED_TYPE(a,b,c) \
329 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
330 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
331 c_interop_kinds_table[a].value = c;
332 #define NAMED_FUNCTION(a,b,c,d) \
333 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
334 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
335 c_interop_kinds_table[a].value = c;
336 #define NAMED_SUBROUTINE(a,b,c,d) \
337 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
338 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
339 c_interop_kinds_table[a].value = c;
340 #include "iso-c-binding.def"
344 /* Query the target to determine which machine modes are available for
345 computation. Choose KIND numbers for them. */
347 void
348 gfc_init_kinds (void)
350 unsigned int mode;
351 int i_index, r_index, kind;
352 bool saw_i4 = false, saw_i8 = false;
353 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
355 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
357 int kind, bitsize;
359 if (!targetm.scalar_mode_supported_p ((machine_mode) mode))
360 continue;
362 /* The middle end doesn't support constants larger than 2*HWI.
363 Perhaps the target hook shouldn't have accepted these either,
364 but just to be safe... */
365 bitsize = GET_MODE_BITSIZE ((machine_mode) mode);
366 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
367 continue;
369 gcc_assert (i_index != MAX_INT_KINDS);
371 /* Let the kind equal the bit size divided by 8. This insulates the
372 programmer from the underlying byte size. */
373 kind = bitsize / 8;
375 if (kind == 4)
376 saw_i4 = true;
377 if (kind == 8)
378 saw_i8 = true;
380 gfc_integer_kinds[i_index].kind = kind;
381 gfc_integer_kinds[i_index].radix = 2;
382 gfc_integer_kinds[i_index].digits = bitsize - 1;
383 gfc_integer_kinds[i_index].bit_size = bitsize;
385 gfc_logical_kinds[i_index].kind = kind;
386 gfc_logical_kinds[i_index].bit_size = bitsize;
388 i_index += 1;
391 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
392 used for large file access. */
394 if (saw_i8)
395 gfc_intio_kind = 8;
396 else
397 gfc_intio_kind = 4;
399 /* If we do not at least have kind = 4, everything is pointless. */
400 gcc_assert(saw_i4);
402 /* Set the maximum integer kind. Used with at least BOZ constants. */
403 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
405 for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
407 const struct real_format *fmt =
408 REAL_MODE_FORMAT ((machine_mode) mode);
409 int kind;
411 if (fmt == NULL)
412 continue;
413 if (!targetm.scalar_mode_supported_p ((machine_mode) mode))
414 continue;
416 /* Only let float, double, long double and __float128 go through.
417 Runtime support for others is not provided, so they would be
418 useless. */
419 if (!targetm.libgcc_floating_mode_supported_p ((machine_mode)
420 mode))
421 continue;
422 if (mode != TYPE_MODE (float_type_node)
423 && (mode != TYPE_MODE (double_type_node))
424 && (mode != TYPE_MODE (long_double_type_node))
425 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
426 && (mode != TFmode)
427 #endif
429 continue;
431 /* Let the kind equal the precision divided by 8, rounding up. Again,
432 this insulates the programmer from the underlying byte size.
434 Also, it effectively deals with IEEE extended formats. There, the
435 total size of the type may equal 16, but it's got 6 bytes of padding
436 and the increased size can get in the way of a real IEEE quad format
437 which may also be supported by the target.
439 We round up so as to handle IA-64 __floatreg (RFmode), which is an
440 82 bit type. Not to be confused with __float80 (XFmode), which is
441 an 80 bit type also supported by IA-64. So XFmode should come out
442 to be kind=10, and RFmode should come out to be kind=11. Egads. */
444 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
446 if (kind == 4)
447 saw_r4 = true;
448 if (kind == 8)
449 saw_r8 = true;
450 if (kind == 10)
451 saw_r10 = true;
452 if (kind == 16)
453 saw_r16 = true;
455 /* Careful we don't stumble a weird internal mode. */
456 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
457 /* Or have too many modes for the allocated space. */
458 gcc_assert (r_index != MAX_REAL_KINDS);
460 gfc_real_kinds[r_index].kind = kind;
461 gfc_real_kinds[r_index].radix = fmt->b;
462 gfc_real_kinds[r_index].digits = fmt->p;
463 gfc_real_kinds[r_index].min_exponent = fmt->emin;
464 gfc_real_kinds[r_index].max_exponent = fmt->emax;
465 if (fmt->pnan < fmt->p)
466 /* This is an IBM extended double format (or the MIPS variant)
467 made up of two IEEE doubles. The value of the long double is
468 the sum of the values of the two parts. The most significant
469 part is required to be the value of the long double rounded
470 to the nearest double. If we use emax of 1024 then we can't
471 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
472 rounding will make the most significant part overflow. */
473 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
474 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
475 r_index += 1;
478 /* Choose the default integer kind. We choose 4 unless the user directs us
479 otherwise. Even if the user specified that the default integer kind is 8,
480 the numeric storage size is not 64 bits. In this case, a warning will be
481 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
483 gfc_numeric_storage_size = 4 * 8;
485 if (flag_default_integer)
487 if (!saw_i8)
488 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
489 "%<-fdefault-integer-8%> option");
491 gfc_default_integer_kind = 8;
494 else if (flag_integer4_kind == 8)
496 if (!saw_i8)
497 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
498 "%<-finteger-4-integer-8%> option");
500 gfc_default_integer_kind = 8;
502 else if (saw_i4)
504 gfc_default_integer_kind = 4;
506 else
508 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
509 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
512 /* Choose the default real kind. Again, we choose 4 when possible. */
513 if (flag_default_real)
515 if (!saw_r8)
516 gfc_fatal_error ("REAL(KIND=8) is not available for "
517 "%<-fdefault-real-8%> option");
519 gfc_default_real_kind = 8;
521 else if (flag_real4_kind == 8)
523 if (!saw_r8)
524 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
525 "option");
527 gfc_default_real_kind = 8;
529 else if (flag_real4_kind == 10)
531 if (!saw_r10)
532 gfc_fatal_error ("REAL(KIND=10) is not available for "
533 "%<-freal-4-real-10%> option");
535 gfc_default_real_kind = 10;
537 else if (flag_real4_kind == 16)
539 if (!saw_r16)
540 gfc_fatal_error ("REAL(KIND=16) is not available for "
541 "%<-freal-4-real-16%> option");
543 gfc_default_real_kind = 16;
545 else if (saw_r4)
546 gfc_default_real_kind = 4;
547 else
548 gfc_default_real_kind = gfc_real_kinds[0].kind;
550 /* Choose the default double kind. If -fdefault-real and -fdefault-double
551 are specified, we use kind=8, if it's available. If -fdefault-real is
552 specified without -fdefault-double, we use kind=16, if it's available.
553 Otherwise we do not change anything. */
554 if (flag_default_double && !flag_default_real)
555 gfc_fatal_error ("Use of %<-fdefault-double-8%> requires "
556 "%<-fdefault-real-8%>");
558 if (flag_default_real && flag_default_double && saw_r8)
559 gfc_default_double_kind = 8;
560 else if (flag_default_real && saw_r16)
561 gfc_default_double_kind = 16;
562 else if (flag_real8_kind == 4)
564 if (!saw_r4)
565 gfc_fatal_error ("REAL(KIND=4) is not available for "
566 "%<-freal-8-real-4%> option");
568 gfc_default_double_kind = 4;
570 else if (flag_real8_kind == 10 )
572 if (!saw_r10)
573 gfc_fatal_error ("REAL(KIND=10) is not available for "
574 "%<-freal-8-real-10%> option");
576 gfc_default_double_kind = 10;
578 else if (flag_real8_kind == 16 )
580 if (!saw_r16)
581 gfc_fatal_error ("REAL(KIND=10) is not available for "
582 "%<-freal-8-real-16%> option");
584 gfc_default_double_kind = 16;
586 else if (saw_r4 && saw_r8)
587 gfc_default_double_kind = 8;
588 else
590 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
591 real ... occupies two contiguous numeric storage units.
593 Therefore we must be supplied a kind twice as large as we chose
594 for single precision. There are loopholes, in that double
595 precision must *occupy* two storage units, though it doesn't have
596 to *use* two storage units. Which means that you can make this
597 kind artificially wide by padding it. But at present there are
598 no GCC targets for which a two-word type does not exist, so we
599 just let gfc_validate_kind abort and tell us if something breaks. */
601 gfc_default_double_kind
602 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
605 /* The default logical kind is constrained to be the same as the
606 default integer kind. Similarly with complex and real. */
607 gfc_default_logical_kind = gfc_default_integer_kind;
608 gfc_default_complex_kind = gfc_default_real_kind;
610 /* We only have two character kinds: ASCII and UCS-4.
611 ASCII corresponds to a 8-bit integer type, if one is available.
612 UCS-4 corresponds to a 32-bit integer type, if one is available. */
613 i_index = 0;
614 if ((kind = get_int_kind_from_width (8)) > 0)
616 gfc_character_kinds[i_index].kind = kind;
617 gfc_character_kinds[i_index].bit_size = 8;
618 gfc_character_kinds[i_index].name = "ascii";
619 i_index++;
621 if ((kind = get_int_kind_from_width (32)) > 0)
623 gfc_character_kinds[i_index].kind = kind;
624 gfc_character_kinds[i_index].bit_size = 32;
625 gfc_character_kinds[i_index].name = "iso_10646";
626 i_index++;
629 /* Choose the smallest integer kind for our default character. */
630 gfc_default_character_kind = gfc_character_kinds[0].kind;
631 gfc_character_storage_size = gfc_default_character_kind * 8;
633 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
635 /* Pick a kind the same size as the C "int" type. */
636 gfc_c_int_kind = INT_TYPE_SIZE / 8;
638 /* Choose atomic kinds to match C's int. */
639 gfc_atomic_int_kind = gfc_c_int_kind;
640 gfc_atomic_logical_kind = gfc_c_int_kind;
644 /* Make sure that a valid kind is present. Returns an index into the
645 associated kinds array, -1 if the kind is not present. */
647 static int
648 validate_integer (int kind)
650 int i;
652 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
653 if (gfc_integer_kinds[i].kind == kind)
654 return i;
656 return -1;
659 static int
660 validate_real (int kind)
662 int i;
664 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
665 if (gfc_real_kinds[i].kind == kind)
666 return i;
668 return -1;
671 static int
672 validate_logical (int kind)
674 int i;
676 for (i = 0; gfc_logical_kinds[i].kind; i++)
677 if (gfc_logical_kinds[i].kind == kind)
678 return i;
680 return -1;
683 static int
684 validate_character (int kind)
686 int i;
688 for (i = 0; gfc_character_kinds[i].kind; i++)
689 if (gfc_character_kinds[i].kind == kind)
690 return i;
692 return -1;
695 /* Validate a kind given a basic type. The return value is the same
696 for the child functions, with -1 indicating nonexistence of the
697 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
700 gfc_validate_kind (bt type, int kind, bool may_fail)
702 int rc;
704 switch (type)
706 case BT_REAL: /* Fall through */
707 case BT_COMPLEX:
708 rc = validate_real (kind);
709 break;
710 case BT_INTEGER:
711 rc = validate_integer (kind);
712 break;
713 case BT_LOGICAL:
714 rc = validate_logical (kind);
715 break;
716 case BT_CHARACTER:
717 rc = validate_character (kind);
718 break;
720 default:
721 gfc_internal_error ("gfc_validate_kind(): Got bad type");
724 if (rc < 0 && !may_fail)
725 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
727 return rc;
731 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
732 Reuse common type nodes where possible. Recognize if the kind matches up
733 with a C type. This will be used later in determining which routines may
734 be scarfed from libm. */
736 static tree
737 gfc_build_int_type (gfc_integer_info *info)
739 int mode_precision = info->bit_size;
741 if (mode_precision == CHAR_TYPE_SIZE)
742 info->c_char = 1;
743 if (mode_precision == SHORT_TYPE_SIZE)
744 info->c_short = 1;
745 if (mode_precision == INT_TYPE_SIZE)
746 info->c_int = 1;
747 if (mode_precision == LONG_TYPE_SIZE)
748 info->c_long = 1;
749 if (mode_precision == LONG_LONG_TYPE_SIZE)
750 info->c_long_long = 1;
752 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
753 return intQI_type_node;
754 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
755 return intHI_type_node;
756 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
757 return intSI_type_node;
758 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
759 return intDI_type_node;
760 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
761 return intTI_type_node;
763 return make_signed_type (mode_precision);
766 tree
767 gfc_build_uint_type (int size)
769 if (size == CHAR_TYPE_SIZE)
770 return unsigned_char_type_node;
771 if (size == SHORT_TYPE_SIZE)
772 return short_unsigned_type_node;
773 if (size == INT_TYPE_SIZE)
774 return unsigned_type_node;
775 if (size == LONG_TYPE_SIZE)
776 return long_unsigned_type_node;
777 if (size == LONG_LONG_TYPE_SIZE)
778 return long_long_unsigned_type_node;
780 return make_unsigned_type (size);
784 static tree
785 gfc_build_real_type (gfc_real_info *info)
787 int mode_precision = info->mode_precision;
788 tree new_type;
790 if (mode_precision == FLOAT_TYPE_SIZE)
791 info->c_float = 1;
792 if (mode_precision == DOUBLE_TYPE_SIZE)
793 info->c_double = 1;
794 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
795 info->c_long_double = 1;
796 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
798 info->c_float128 = 1;
799 gfc_real16_is_float128 = true;
802 if (TYPE_PRECISION (float_type_node) == mode_precision)
803 return float_type_node;
804 if (TYPE_PRECISION (double_type_node) == mode_precision)
805 return double_type_node;
806 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
807 return long_double_type_node;
809 new_type = make_node (REAL_TYPE);
810 TYPE_PRECISION (new_type) = mode_precision;
811 layout_type (new_type);
812 return new_type;
815 static tree
816 gfc_build_complex_type (tree scalar_type)
818 tree new_type;
820 if (scalar_type == NULL)
821 return NULL;
822 if (scalar_type == float_type_node)
823 return complex_float_type_node;
824 if (scalar_type == double_type_node)
825 return complex_double_type_node;
826 if (scalar_type == long_double_type_node)
827 return complex_long_double_type_node;
829 new_type = make_node (COMPLEX_TYPE);
830 TREE_TYPE (new_type) = scalar_type;
831 SET_TYPE_MODE (new_type, GET_MODE_COMPLEX_MODE (TYPE_MODE (scalar_type)));
832 layout_type (new_type);
833 return new_type;
836 static tree
837 gfc_build_logical_type (gfc_logical_info *info)
839 int bit_size = info->bit_size;
840 tree new_type;
842 if (bit_size == BOOL_TYPE_SIZE)
844 info->c_bool = 1;
845 return boolean_type_node;
848 new_type = make_unsigned_type (bit_size);
849 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
850 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
851 TYPE_PRECISION (new_type) = 1;
853 return new_type;
857 /* Create the backend type nodes. We map them to their
858 equivalent C type, at least for now. We also give
859 names to the types here, and we push them in the
860 global binding level context.*/
862 void
863 gfc_init_types (void)
865 char name_buf[18];
866 int index;
867 tree type;
868 unsigned n;
870 /* Create and name the types. */
871 #define PUSH_TYPE(name, node) \
872 pushdecl (build_decl (input_location, \
873 TYPE_DECL, get_identifier (name), node))
875 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
877 type = gfc_build_int_type (&gfc_integer_kinds[index]);
878 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
879 if (TYPE_STRING_FLAG (type))
880 type = make_signed_type (gfc_integer_kinds[index].bit_size);
881 gfc_integer_types[index] = type;
882 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
883 gfc_integer_kinds[index].kind);
884 PUSH_TYPE (name_buf, type);
887 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
889 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
890 gfc_logical_types[index] = type;
891 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
892 gfc_logical_kinds[index].kind);
893 PUSH_TYPE (name_buf, type);
896 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
898 type = gfc_build_real_type (&gfc_real_kinds[index]);
899 gfc_real_types[index] = type;
900 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
901 gfc_real_kinds[index].kind);
902 PUSH_TYPE (name_buf, type);
904 if (gfc_real_kinds[index].c_float128)
905 float128_type_node = type;
907 type = gfc_build_complex_type (type);
908 gfc_complex_types[index] = type;
909 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
910 gfc_real_kinds[index].kind);
911 PUSH_TYPE (name_buf, type);
913 if (gfc_real_kinds[index].c_float128)
914 complex_float128_type_node = type;
917 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
919 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
920 type = build_qualified_type (type, TYPE_UNQUALIFIED);
921 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
922 gfc_character_kinds[index].kind);
923 PUSH_TYPE (name_buf, type);
924 gfc_character_types[index] = type;
925 gfc_pcharacter_types[index] = build_pointer_type (type);
927 gfc_character1_type_node = gfc_character_types[0];
929 PUSH_TYPE ("byte", unsigned_char_type_node);
930 PUSH_TYPE ("void", void_type_node);
932 /* DBX debugging output gets upset if these aren't set. */
933 if (!TYPE_NAME (integer_type_node))
934 PUSH_TYPE ("c_integer", integer_type_node);
935 if (!TYPE_NAME (char_type_node))
936 PUSH_TYPE ("c_char", char_type_node);
938 #undef PUSH_TYPE
940 pvoid_type_node = build_pointer_type (void_type_node);
941 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
942 ppvoid_type_node = build_pointer_type (pvoid_type_node);
943 pchar_type_node = build_pointer_type (gfc_character1_type_node);
944 pfunc_type_node
945 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
947 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
948 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
949 since this function is called before gfc_init_constants. */
950 gfc_array_range_type
951 = build_range_type (gfc_array_index_type,
952 build_int_cst (gfc_array_index_type, 0),
953 NULL_TREE);
955 /* The maximum array element size that can be handled is determined
956 by the number of bits available to store this field in the array
957 descriptor. */
959 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
960 gfc_max_array_element_size
961 = wide_int_to_tree (size_type_node,
962 wi::mask (n, UNSIGNED,
963 TYPE_PRECISION (size_type_node)));
965 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
966 boolean_true_node = build_int_cst (boolean_type_node, 1);
967 boolean_false_node = build_int_cst (boolean_type_node, 0);
969 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
970 gfc_charlen_int_kind = 4;
971 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
974 /* Get the type node for the given type and kind. */
976 tree
977 gfc_get_int_type (int kind)
979 int index = gfc_validate_kind (BT_INTEGER, kind, true);
980 return index < 0 ? 0 : gfc_integer_types[index];
983 tree
984 gfc_get_real_type (int kind)
986 int index = gfc_validate_kind (BT_REAL, kind, true);
987 return index < 0 ? 0 : gfc_real_types[index];
990 tree
991 gfc_get_complex_type (int kind)
993 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
994 return index < 0 ? 0 : gfc_complex_types[index];
997 tree
998 gfc_get_logical_type (int kind)
1000 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1001 return index < 0 ? 0 : gfc_logical_types[index];
1004 tree
1005 gfc_get_char_type (int kind)
1007 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1008 return index < 0 ? 0 : gfc_character_types[index];
1011 tree
1012 gfc_get_pchar_type (int kind)
1014 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1015 return index < 0 ? 0 : gfc_pcharacter_types[index];
1019 /* Create a character type with the given kind and length. */
1021 tree
1022 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1024 tree bounds, type;
1026 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1027 type = build_array_type (eltype, bounds);
1028 TYPE_STRING_FLAG (type) = 1;
1030 return type;
1033 tree
1034 gfc_get_character_type_len (int kind, tree len)
1036 gfc_validate_kind (BT_CHARACTER, kind, false);
1037 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1041 /* Get a type node for a character kind. */
1043 tree
1044 gfc_get_character_type (int kind, gfc_charlen * cl)
1046 tree len;
1048 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1049 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1050 len = build_fold_indirect_ref (len);
1052 return gfc_get_character_type_len (kind, len);
1055 /* Covert a basic type. This will be an array for character types. */
1057 tree
1058 gfc_typenode_for_spec (gfc_typespec * spec)
1060 tree basetype;
1062 switch (spec->type)
1064 case BT_UNKNOWN:
1065 gcc_unreachable ();
1067 case BT_INTEGER:
1068 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1069 has been resolved. This is done so we can convert C_PTR and
1070 C_FUNPTR to simple variables that get translated to (void *). */
1071 if (spec->f90_type == BT_VOID)
1073 if (spec->u.derived
1074 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1075 basetype = ptr_type_node;
1076 else
1077 basetype = pfunc_type_node;
1079 else
1080 basetype = gfc_get_int_type (spec->kind);
1081 break;
1083 case BT_REAL:
1084 basetype = gfc_get_real_type (spec->kind);
1085 break;
1087 case BT_COMPLEX:
1088 basetype = gfc_get_complex_type (spec->kind);
1089 break;
1091 case BT_LOGICAL:
1092 basetype = gfc_get_logical_type (spec->kind);
1093 break;
1095 case BT_CHARACTER:
1096 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1097 break;
1099 case BT_HOLLERITH:
1100 /* Since this cannot be used, return a length one character. */
1101 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1102 gfc_index_one_node);
1103 break;
1105 case BT_UNION:
1106 basetype = gfc_get_union_type (spec->u.derived);
1107 break;
1109 case BT_DERIVED:
1110 case BT_CLASS:
1111 basetype = gfc_get_derived_type (spec->u.derived);
1113 if (spec->type == BT_CLASS)
1114 GFC_CLASS_TYPE_P (basetype) = 1;
1116 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1117 type and kind to fit a (void *) and the basetype returned was a
1118 ptr_type_node. We need to pass up this new information to the
1119 symbol that was declared of type C_PTR or C_FUNPTR. */
1120 if (spec->u.derived->ts.f90_type == BT_VOID)
1122 spec->type = BT_INTEGER;
1123 spec->kind = gfc_index_integer_kind;
1124 spec->f90_type = BT_VOID;
1126 break;
1127 case BT_VOID:
1128 case BT_ASSUMED:
1129 /* This is for the second arg to c_f_pointer and c_f_procpointer
1130 of the iso_c_binding module, to accept any ptr type. */
1131 basetype = ptr_type_node;
1132 if (spec->f90_type == BT_VOID)
1134 if (spec->u.derived
1135 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1136 basetype = ptr_type_node;
1137 else
1138 basetype = pfunc_type_node;
1140 break;
1141 default:
1142 gcc_unreachable ();
1144 return basetype;
1147 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1149 static tree
1150 gfc_conv_array_bound (gfc_expr * expr)
1152 /* If expr is an integer constant, return that. */
1153 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1154 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1156 /* Otherwise return NULL. */
1157 return NULL_TREE;
1160 /* Return the type of an element of the array. Note that scalar coarrays
1161 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1162 (with POINTER_TYPE stripped) is returned. */
1164 tree
1165 gfc_get_element_type (tree type)
1167 tree element;
1169 if (GFC_ARRAY_TYPE_P (type))
1171 if (TREE_CODE (type) == POINTER_TYPE)
1172 type = TREE_TYPE (type);
1173 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1175 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1176 element = type;
1178 else
1180 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1181 element = TREE_TYPE (type);
1184 else
1186 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1187 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1189 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1190 element = TREE_TYPE (element);
1192 /* For arrays, which are not scalar coarrays. */
1193 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1194 element = TREE_TYPE (element);
1197 return element;
1200 /* Build an array. This function is called from gfc_sym_type().
1201 Actually returns array descriptor type.
1203 Format of array descriptors is as follows:
1205 struct gfc_array_descriptor
1207 array *data
1208 index offset;
1209 index dtype;
1210 struct descriptor_dimension dimension[N_DIM];
1213 struct descriptor_dimension
1215 index stride;
1216 index lbound;
1217 index ubound;
1220 Translation code should use gfc_conv_descriptor_* rather than
1221 accessing the descriptor directly. Any changes to the array
1222 descriptor type will require changes in gfc_conv_descriptor_* and
1223 gfc_build_array_initializer.
1225 This is represented internally as a RECORD_TYPE. The index nodes
1226 are gfc_array_index_type and the data node is a pointer to the
1227 data. See below for the handling of character types.
1229 The dtype member is formatted as follows:
1230 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1231 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1232 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1234 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1235 this generated poor code for assumed/deferred size arrays. These
1236 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1237 of the GENERIC grammar. Also, there is no way to explicitly set
1238 the array stride, so all data must be packed(1). I've tried to
1239 mark all the functions which would require modification with a GCC
1240 ARRAYS comment.
1242 The data component points to the first element in the array. The
1243 offset field is the position of the origin of the array (i.e. element
1244 (0, 0 ...)). This may be outside the bounds of the array.
1246 An element is accessed by
1247 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1248 This gives good performance as the computation does not involve the
1249 bounds of the array. For packed arrays, this is optimized further
1250 by substituting the known strides.
1252 This system has one problem: all array bounds must be within 2^31
1253 elements of the origin (2^63 on 64-bit machines). For example
1254 integer, dimension (80000:90000, 80000:90000, 2) :: array
1255 may not work properly on 32-bit machines because 80000*80000 >
1256 2^31, so the calculation for stride2 would overflow. This may
1257 still work, but I haven't checked, and it relies on the overflow
1258 doing the right thing.
1260 The way to fix this problem is to access elements as follows:
1261 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1262 Obviously this is much slower. I will make this a compile time
1263 option, something like -fsmall-array-offsets. Mixing code compiled
1264 with and without this switch will work.
1266 (1) This can be worked around by modifying the upper bound of the
1267 previous dimension. This requires extra fields in the descriptor
1268 (both real_ubound and fake_ubound). */
1271 /* Returns true if the array sym does not require a descriptor. */
1274 gfc_is_nodesc_array (gfc_symbol * sym)
1276 symbol_attribute *array_attr;
1277 gfc_array_spec *as;
1278 bool is_classarray = IS_CLASS_ARRAY (sym);
1280 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1281 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1283 gcc_assert (array_attr->dimension || array_attr->codimension);
1285 /* We only want local arrays. */
1286 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1287 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1288 || array_attr->allocatable)
1289 return 0;
1291 /* We want a descriptor for associate-name arrays that do not have an
1292 explicitly known shape already. */
1293 if (sym->assoc && as->type != AS_EXPLICIT)
1294 return 0;
1296 /* The dummy is stored in sym and not in the component. */
1297 if (sym->attr.dummy)
1298 return as->type != AS_ASSUMED_SHAPE
1299 && as->type != AS_ASSUMED_RANK;
1301 if (sym->attr.result || sym->attr.function)
1302 return 0;
1304 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1306 return 1;
1310 /* Create an array descriptor type. */
1312 static tree
1313 gfc_build_array_type (tree type, gfc_array_spec * as,
1314 enum gfc_array_kind akind, bool restricted,
1315 bool contiguous)
1317 tree lbound[GFC_MAX_DIMENSIONS];
1318 tree ubound[GFC_MAX_DIMENSIONS];
1319 int n, corank;
1321 /* Assumed-shape arrays do not have codimension information stored in the
1322 descriptor. */
1323 corank = as->corank;
1324 if (as->type == AS_ASSUMED_SHAPE ||
1325 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1326 corank = 0;
1328 if (as->type == AS_ASSUMED_RANK)
1329 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1331 lbound[n] = NULL_TREE;
1332 ubound[n] = NULL_TREE;
1335 for (n = 0; n < as->rank; n++)
1337 /* Create expressions for the known bounds of the array. */
1338 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1339 lbound[n] = gfc_index_one_node;
1340 else
1341 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1342 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1345 for (n = as->rank; n < as->rank + corank; n++)
1347 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1348 lbound[n] = gfc_index_one_node;
1349 else
1350 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1352 if (n < as->rank + corank - 1)
1353 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1356 if (as->type == AS_ASSUMED_SHAPE)
1357 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1358 : GFC_ARRAY_ASSUMED_SHAPE;
1359 else if (as->type == AS_ASSUMED_RANK)
1360 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1361 : GFC_ARRAY_ASSUMED_RANK;
1362 return gfc_get_array_type_bounds (type, as->rank == -1
1363 ? GFC_MAX_DIMENSIONS : as->rank,
1364 corank, lbound,
1365 ubound, 0, akind, restricted);
1368 /* Returns the struct descriptor_dimension type. */
1370 static tree
1371 gfc_get_desc_dim_type (void)
1373 tree type;
1374 tree decl, *chain = NULL;
1376 if (gfc_desc_dim_type)
1377 return gfc_desc_dim_type;
1379 /* Build the type node. */
1380 type = make_node (RECORD_TYPE);
1382 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1383 TYPE_PACKED (type) = 1;
1385 /* Consists of the stride, lbound and ubound members. */
1386 decl = gfc_add_field_to_struct_1 (type,
1387 get_identifier ("stride"),
1388 gfc_array_index_type, &chain);
1389 TREE_NO_WARNING (decl) = 1;
1391 decl = gfc_add_field_to_struct_1 (type,
1392 get_identifier ("lbound"),
1393 gfc_array_index_type, &chain);
1394 TREE_NO_WARNING (decl) = 1;
1396 decl = gfc_add_field_to_struct_1 (type,
1397 get_identifier ("ubound"),
1398 gfc_array_index_type, &chain);
1399 TREE_NO_WARNING (decl) = 1;
1401 /* Finish off the type. */
1402 gfc_finish_type (type);
1403 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1405 gfc_desc_dim_type = type;
1406 return type;
1410 /* Return the DTYPE for an array. This describes the type and type parameters
1411 of the array. */
1412 /* TODO: Only call this when the value is actually used, and make all the
1413 unknown cases abort. */
1415 tree
1416 gfc_get_dtype_rank_type (int rank, tree etype)
1418 tree size;
1419 int n;
1420 HOST_WIDE_INT i;
1421 tree tmp;
1422 tree dtype;
1424 switch (TREE_CODE (etype))
1426 case INTEGER_TYPE:
1427 n = BT_INTEGER;
1428 break;
1430 case BOOLEAN_TYPE:
1431 n = BT_LOGICAL;
1432 break;
1434 case REAL_TYPE:
1435 n = BT_REAL;
1436 break;
1438 case COMPLEX_TYPE:
1439 n = BT_COMPLEX;
1440 break;
1442 /* We will never have arrays of arrays. */
1443 case RECORD_TYPE:
1444 n = BT_DERIVED;
1445 break;
1447 case ARRAY_TYPE:
1448 n = BT_CHARACTER;
1449 break;
1451 case POINTER_TYPE:
1452 n = BT_ASSUMED;
1453 break;
1455 default:
1456 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1457 /* We can strange array types for temporary arrays. */
1458 return gfc_index_zero_node;
1461 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1462 size = TYPE_SIZE_UNIT (etype);
1464 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1465 if (size && INTEGER_CST_P (size))
1467 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1468 gfc_fatal_error ("Array element size too big at %C");
1470 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1472 dtype = build_int_cst (gfc_array_index_type, i);
1474 if (size && !INTEGER_CST_P (size))
1476 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1477 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1478 gfc_array_index_type,
1479 fold_convert (gfc_array_index_type, size), tmp);
1480 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1481 tmp, dtype);
1483 /* If we don't know the size we leave it as zero. This should never happen
1484 for anything that is actually used. */
1485 /* TODO: Check this is actually true, particularly when repacking
1486 assumed size parameters. */
1488 return dtype;
1492 tree
1493 gfc_get_dtype (tree type)
1495 tree dtype;
1496 tree etype;
1497 int rank;
1499 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1501 if (GFC_TYPE_ARRAY_DTYPE (type))
1502 return GFC_TYPE_ARRAY_DTYPE (type);
1504 rank = GFC_TYPE_ARRAY_RANK (type);
1505 etype = gfc_get_element_type (type);
1506 dtype = gfc_get_dtype_rank_type (rank, etype);
1508 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1509 return dtype;
1513 /* Build an array type for use without a descriptor, packed according
1514 to the value of PACKED. */
1516 tree
1517 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1518 bool restricted)
1520 tree range;
1521 tree type;
1522 tree tmp;
1523 int n;
1524 int known_stride;
1525 int known_offset;
1526 mpz_t offset;
1527 mpz_t stride;
1528 mpz_t delta;
1529 gfc_expr *expr;
1531 mpz_init_set_ui (offset, 0);
1532 mpz_init_set_ui (stride, 1);
1533 mpz_init (delta);
1535 /* We don't use build_array_type because this does not include include
1536 lang-specific information (i.e. the bounds of the array) when checking
1537 for duplicates. */
1538 if (as->rank)
1539 type = make_node (ARRAY_TYPE);
1540 else
1541 type = build_variant_type_copy (etype);
1543 GFC_ARRAY_TYPE_P (type) = 1;
1544 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1546 known_stride = (packed != PACKED_NO);
1547 known_offset = 1;
1548 for (n = 0; n < as->rank; n++)
1550 /* Fill in the stride and bound components of the type. */
1551 if (known_stride)
1552 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1553 else
1554 tmp = NULL_TREE;
1555 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1557 expr = as->lower[n];
1558 if (expr->expr_type == EXPR_CONSTANT)
1560 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1561 gfc_index_integer_kind);
1563 else
1565 known_stride = 0;
1566 tmp = NULL_TREE;
1568 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1570 if (known_stride)
1572 /* Calculate the offset. */
1573 mpz_mul (delta, stride, as->lower[n]->value.integer);
1574 mpz_sub (offset, offset, delta);
1576 else
1577 known_offset = 0;
1579 expr = as->upper[n];
1580 if (expr && expr->expr_type == EXPR_CONSTANT)
1582 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1583 gfc_index_integer_kind);
1585 else
1587 tmp = NULL_TREE;
1588 known_stride = 0;
1590 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1592 if (known_stride)
1594 /* Calculate the stride. */
1595 mpz_sub (delta, as->upper[n]->value.integer,
1596 as->lower[n]->value.integer);
1597 mpz_add_ui (delta, delta, 1);
1598 mpz_mul (stride, stride, delta);
1601 /* Only the first stride is known for partial packed arrays. */
1602 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1603 known_stride = 0;
1605 for (n = as->rank; n < as->rank + as->corank; n++)
1607 expr = as->lower[n];
1608 if (expr->expr_type == EXPR_CONSTANT)
1609 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1610 gfc_index_integer_kind);
1611 else
1612 tmp = NULL_TREE;
1613 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1615 expr = as->upper[n];
1616 if (expr && expr->expr_type == EXPR_CONSTANT)
1617 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1618 gfc_index_integer_kind);
1619 else
1620 tmp = NULL_TREE;
1621 if (n < as->rank + as->corank - 1)
1622 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1625 if (known_offset)
1627 GFC_TYPE_ARRAY_OFFSET (type) =
1628 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1630 else
1631 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1633 if (known_stride)
1635 GFC_TYPE_ARRAY_SIZE (type) =
1636 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1638 else
1639 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1641 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1642 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1643 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1644 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1645 NULL_TREE);
1646 /* TODO: use main type if it is unbounded. */
1647 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1648 build_pointer_type (build_array_type (etype, range));
1649 if (restricted)
1650 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1651 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1652 TYPE_QUAL_RESTRICT);
1654 if (as->rank == 0)
1656 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1658 type = build_pointer_type (type);
1660 if (restricted)
1661 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1663 GFC_ARRAY_TYPE_P (type) = 1;
1664 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1667 return type;
1670 if (known_stride)
1672 mpz_sub_ui (stride, stride, 1);
1673 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1675 else
1676 range = NULL_TREE;
1678 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1679 TYPE_DOMAIN (type) = range;
1681 build_pointer_type (etype);
1682 TREE_TYPE (type) = etype;
1684 layout_type (type);
1686 mpz_clear (offset);
1687 mpz_clear (stride);
1688 mpz_clear (delta);
1690 /* Represent packed arrays as multi-dimensional if they have rank >
1691 1 and with proper bounds, instead of flat arrays. This makes for
1692 better debug info. */
1693 if (known_offset)
1695 tree gtype = etype, rtype, type_decl;
1697 for (n = as->rank - 1; n >= 0; n--)
1699 rtype = build_range_type (gfc_array_index_type,
1700 GFC_TYPE_ARRAY_LBOUND (type, n),
1701 GFC_TYPE_ARRAY_UBOUND (type, n));
1702 gtype = build_array_type (gtype, rtype);
1704 TYPE_NAME (type) = type_decl = build_decl (input_location,
1705 TYPE_DECL, NULL, gtype);
1706 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1709 if (packed != PACKED_STATIC || !known_stride
1710 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1712 /* For dummy arrays and automatic (heap allocated) arrays we
1713 want a pointer to the array. */
1714 type = build_pointer_type (type);
1715 if (restricted)
1716 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1717 GFC_ARRAY_TYPE_P (type) = 1;
1718 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1720 return type;
1724 /* Return or create the base type for an array descriptor. */
1726 static tree
1727 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted,
1728 enum gfc_array_kind akind)
1730 tree fat_type, decl, arraytype, *chain = NULL;
1731 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1732 int idx;
1734 /* Assumed-rank array. */
1735 if (dimen == -1)
1736 dimen = GFC_MAX_DIMENSIONS;
1738 idx = 2 * (codimen + dimen) + restricted;
1740 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1742 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1744 if (gfc_array_descriptor_base_caf[idx])
1745 return gfc_array_descriptor_base_caf[idx];
1747 else if (gfc_array_descriptor_base[idx])
1748 return gfc_array_descriptor_base[idx];
1750 /* Build the type node. */
1751 fat_type = make_node (RECORD_TYPE);
1753 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1754 TYPE_NAME (fat_type) = get_identifier (name);
1755 TYPE_NAMELESS (fat_type) = 1;
1757 /* Add the data member as the first element of the descriptor. */
1758 decl = gfc_add_field_to_struct_1 (fat_type,
1759 get_identifier ("data"),
1760 (restricted
1761 ? prvoid_type_node
1762 : ptr_type_node), &chain);
1764 /* Add the base component. */
1765 decl = gfc_add_field_to_struct_1 (fat_type,
1766 get_identifier ("offset"),
1767 gfc_array_index_type, &chain);
1768 TREE_NO_WARNING (decl) = 1;
1770 /* Add the dtype component. */
1771 decl = gfc_add_field_to_struct_1 (fat_type,
1772 get_identifier ("dtype"),
1773 gfc_array_index_type, &chain);
1774 TREE_NO_WARNING (decl) = 1;
1776 /* Build the array type for the stride and bound components. */
1777 if (dimen + codimen > 0)
1779 arraytype =
1780 build_array_type (gfc_get_desc_dim_type (),
1781 build_range_type (gfc_array_index_type,
1782 gfc_index_zero_node,
1783 gfc_rank_cst[codimen + dimen - 1]));
1785 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1786 arraytype, &chain);
1787 TREE_NO_WARNING (decl) = 1;
1790 if (flag_coarray == GFC_FCOARRAY_LIB && codimen
1791 && akind == GFC_ARRAY_ALLOCATABLE)
1793 decl = gfc_add_field_to_struct_1 (fat_type,
1794 get_identifier ("token"),
1795 prvoid_type_node, &chain);
1796 TREE_NO_WARNING (decl) = 1;
1799 /* Finish off the type. */
1800 gfc_finish_type (fat_type);
1801 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1803 if (flag_coarray == GFC_FCOARRAY_LIB && codimen
1804 && akind == GFC_ARRAY_ALLOCATABLE)
1805 gfc_array_descriptor_base_caf[idx] = fat_type;
1806 else
1807 gfc_array_descriptor_base[idx] = fat_type;
1809 return fat_type;
1813 /* Build an array (descriptor) type with given bounds. */
1815 tree
1816 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1817 tree * ubound, int packed,
1818 enum gfc_array_kind akind, bool restricted)
1820 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1821 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1822 const char *type_name;
1823 int n;
1825 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted, akind);
1826 fat_type = build_distinct_type_copy (base_type);
1827 /* Make sure that nontarget and target array type have the same canonical
1828 type (and same stub decl for debug info). */
1829 base_type = gfc_get_array_descriptor_base (dimen, codimen, false, akind);
1830 TYPE_CANONICAL (fat_type) = base_type;
1831 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1833 tmp = TYPE_NAME (etype);
1834 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1835 tmp = DECL_NAME (tmp);
1836 if (tmp)
1837 type_name = IDENTIFIER_POINTER (tmp);
1838 else
1839 type_name = "unknown";
1840 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1841 GFC_MAX_SYMBOL_LEN, type_name);
1842 TYPE_NAME (fat_type) = get_identifier (name);
1843 TYPE_NAMELESS (fat_type) = 1;
1845 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1846 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1848 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1849 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1850 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1851 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1853 /* Build an array descriptor record type. */
1854 if (packed != 0)
1855 stride = gfc_index_one_node;
1856 else
1857 stride = NULL_TREE;
1858 for (n = 0; n < dimen + codimen; n++)
1860 if (n < dimen)
1861 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1863 if (lbound)
1864 lower = lbound[n];
1865 else
1866 lower = NULL_TREE;
1868 if (lower != NULL_TREE)
1870 if (INTEGER_CST_P (lower))
1871 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1872 else
1873 lower = NULL_TREE;
1876 if (codimen && n == dimen + codimen - 1)
1877 break;
1879 upper = ubound[n];
1880 if (upper != NULL_TREE)
1882 if (INTEGER_CST_P (upper))
1883 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1884 else
1885 upper = NULL_TREE;
1888 if (n >= dimen)
1889 continue;
1891 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1893 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1894 gfc_array_index_type, upper, lower);
1895 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1896 gfc_array_index_type, tmp,
1897 gfc_index_one_node);
1898 stride = fold_build2_loc (input_location, MULT_EXPR,
1899 gfc_array_index_type, tmp, stride);
1900 /* Check the folding worked. */
1901 gcc_assert (INTEGER_CST_P (stride));
1903 else
1904 stride = NULL_TREE;
1906 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1908 /* TODO: known offsets for descriptors. */
1909 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1911 if (dimen == 0)
1913 arraytype = build_pointer_type (etype);
1914 if (restricted)
1915 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1917 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1918 return fat_type;
1921 /* We define data as an array with the correct size if possible.
1922 Much better than doing pointer arithmetic. */
1923 if (stride)
1924 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1925 int_const_binop (MINUS_EXPR, stride,
1926 build_int_cst (TREE_TYPE (stride), 1)));
1927 else
1928 rtype = gfc_array_range_type;
1929 arraytype = build_array_type (etype, rtype);
1930 arraytype = build_pointer_type (arraytype);
1931 if (restricted)
1932 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1933 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1935 /* This will generate the base declarations we need to emit debug
1936 information for this type. FIXME: there must be a better way to
1937 avoid divergence between compilations with and without debug
1938 information. */
1940 struct array_descr_info info;
1941 gfc_get_array_descr_info (fat_type, &info);
1942 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1945 return fat_type;
1948 /* Build a pointer type. This function is called from gfc_sym_type(). */
1950 static tree
1951 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1953 /* Array pointer types aren't actually pointers. */
1954 if (sym->attr.dimension)
1955 return type;
1956 else
1957 return build_pointer_type (type);
1960 static tree gfc_nonrestricted_type (tree t);
1961 /* Given two record or union type nodes TO and FROM, ensure
1962 that all fields in FROM have a corresponding field in TO,
1963 their type being nonrestrict variants. This accepts a TO
1964 node that already has a prefix of the fields in FROM. */
1965 static void
1966 mirror_fields (tree to, tree from)
1968 tree fto, ffrom;
1969 tree *chain;
1971 /* Forward to the end of TOs fields. */
1972 fto = TYPE_FIELDS (to);
1973 ffrom = TYPE_FIELDS (from);
1974 chain = &TYPE_FIELDS (to);
1975 while (fto)
1977 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
1978 chain = &DECL_CHAIN (fto);
1979 fto = DECL_CHAIN (fto);
1980 ffrom = DECL_CHAIN (ffrom);
1983 /* Now add all fields remaining in FROM (starting with ffrom). */
1984 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
1986 tree newfield = copy_node (ffrom);
1987 DECL_CONTEXT (newfield) = to;
1988 /* The store to DECL_CHAIN might seem redundant with the
1989 stores to *chain, but not clearing it here would mean
1990 leaving a chain into the old fields. If ever
1991 our called functions would look at them confusion
1992 will arise. */
1993 DECL_CHAIN (newfield) = NULL_TREE;
1994 *chain = newfield;
1995 chain = &DECL_CHAIN (newfield);
1997 if (TREE_CODE (ffrom) == FIELD_DECL)
1999 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2000 TREE_TYPE (newfield) = elemtype;
2003 *chain = NULL_TREE;
2006 /* Given a type T, returns a different type of the same structure,
2007 except that all types it refers to (recursively) are always
2008 non-restrict qualified types. */
2009 static tree
2010 gfc_nonrestricted_type (tree t)
2012 tree ret = t;
2014 /* If the type isn't laid out yet, don't copy it. If something
2015 needs it for real it should wait until the type got finished. */
2016 if (!TYPE_SIZE (t))
2017 return t;
2019 if (!TYPE_LANG_SPECIFIC (t))
2020 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2021 /* If we're dealing with this very node already further up
2022 the call chain (recursion via pointers and struct members)
2023 we haven't yet determined if we really need a new type node.
2024 Assume we don't, return T itself. */
2025 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2026 return t;
2028 /* If we have calculated this all already, just return it. */
2029 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2030 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2032 /* Mark this type. */
2033 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2035 switch (TREE_CODE (t))
2037 default:
2038 break;
2040 case POINTER_TYPE:
2041 case REFERENCE_TYPE:
2043 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2044 if (totype == TREE_TYPE (t))
2045 ret = t;
2046 else if (TREE_CODE (t) == POINTER_TYPE)
2047 ret = build_pointer_type (totype);
2048 else
2049 ret = build_reference_type (totype);
2050 ret = build_qualified_type (ret,
2051 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2053 break;
2055 case ARRAY_TYPE:
2057 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2058 if (elemtype == TREE_TYPE (t))
2059 ret = t;
2060 else
2062 ret = build_variant_type_copy (t);
2063 TREE_TYPE (ret) = elemtype;
2064 if (TYPE_LANG_SPECIFIC (t)
2065 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2067 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2068 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2069 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2071 TYPE_LANG_SPECIFIC (ret)
2072 = ggc_cleared_alloc<struct lang_type> ();
2073 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2074 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2079 break;
2081 case RECORD_TYPE:
2082 case UNION_TYPE:
2083 case QUAL_UNION_TYPE:
2085 tree field;
2086 /* First determine if we need a new type at all.
2087 Careful, the two calls to gfc_nonrestricted_type per field
2088 might return different values. That happens exactly when
2089 one of the fields reaches back to this very record type
2090 (via pointers). The first calls will assume that we don't
2091 need to copy T (see the error_mark_node marking). If there
2092 are any reasons for copying T apart from having to copy T,
2093 we'll indeed copy it, and the second calls to
2094 gfc_nonrestricted_type will use that new node if they
2095 reach back to T. */
2096 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2097 if (TREE_CODE (field) == FIELD_DECL)
2099 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2100 if (elemtype != TREE_TYPE (field))
2101 break;
2103 if (!field)
2104 break;
2105 ret = build_variant_type_copy (t);
2106 TYPE_FIELDS (ret) = NULL_TREE;
2108 /* Here we make sure that as soon as we know we have to copy
2109 T, that also fields reaching back to us will use the new
2110 copy. It's okay if that copy still contains the old fields,
2111 we won't look at them. */
2112 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2113 mirror_fields (ret, t);
2115 break;
2118 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2119 return ret;
2123 /* Return the type for a symbol. Special handling is required for character
2124 types to get the correct level of indirection.
2125 For functions return the return type.
2126 For subroutines return void_type_node.
2127 Calling this multiple times for the same symbol should be avoided,
2128 especially for character and array types. */
2130 tree
2131 gfc_sym_type (gfc_symbol * sym)
2133 tree type;
2134 int byref;
2135 bool restricted;
2137 /* Procedure Pointers inside COMMON blocks. */
2138 if (sym->attr.proc_pointer && sym->attr.in_common)
2140 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2141 sym->attr.proc_pointer = 0;
2142 type = build_pointer_type (gfc_get_function_type (sym));
2143 sym->attr.proc_pointer = 1;
2144 return type;
2147 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2148 return void_type_node;
2150 /* In the case of a function the fake result variable may have a
2151 type different from the function type, so don't return early in
2152 that case. */
2153 if (sym->backend_decl && !sym->attr.function)
2154 return TREE_TYPE (sym->backend_decl);
2156 if (sym->ts.type == BT_CHARACTER
2157 && ((sym->attr.function && sym->attr.is_bind_c)
2158 || (sym->attr.result
2159 && sym->ns->proc_name
2160 && sym->ns->proc_name->attr.is_bind_c)
2161 || (sym->ts.deferred && (!sym->ts.u.cl
2162 || !sym->ts.u.cl->backend_decl))))
2163 type = gfc_character1_type_node;
2164 else
2165 type = gfc_typenode_for_spec (&sym->ts);
2167 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2168 byref = 1;
2169 else
2170 byref = 0;
2172 restricted = !sym->attr.target && !sym->attr.pointer
2173 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2174 if (!restricted)
2175 type = gfc_nonrestricted_type (type);
2177 if (sym->attr.dimension || sym->attr.codimension)
2179 if (gfc_is_nodesc_array (sym))
2181 /* If this is a character argument of unknown length, just use the
2182 base type. */
2183 if (sym->ts.type != BT_CHARACTER
2184 || !(sym->attr.dummy || sym->attr.function)
2185 || sym->ts.u.cl->backend_decl)
2187 type = gfc_get_nodesc_array_type (type, sym->as,
2188 byref ? PACKED_FULL
2189 : PACKED_STATIC,
2190 restricted);
2191 byref = 0;
2194 else
2196 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2197 if (sym->attr.pointer)
2198 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2199 : GFC_ARRAY_POINTER;
2200 else if (sym->attr.allocatable)
2201 akind = GFC_ARRAY_ALLOCATABLE;
2202 type = gfc_build_array_type (type, sym->as, akind, restricted,
2203 sym->attr.contiguous);
2206 else
2208 if (sym->attr.allocatable || sym->attr.pointer
2209 || gfc_is_associate_pointer (sym))
2210 type = gfc_build_pointer_type (sym, type);
2213 /* We currently pass all parameters by reference.
2214 See f95_get_function_decl. For dummy function parameters return the
2215 function type. */
2216 if (byref)
2218 /* We must use pointer types for potentially absent variables. The
2219 optimizers assume a reference type argument is never NULL. */
2220 if (sym->attr.optional
2221 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2222 type = build_pointer_type (type);
2223 else
2225 type = build_reference_type (type);
2226 if (restricted)
2227 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2231 return (type);
2234 /* Layout and output debug info for a record type. */
2236 void
2237 gfc_finish_type (tree type)
2239 tree decl;
2241 decl = build_decl (input_location,
2242 TYPE_DECL, NULL_TREE, type);
2243 TYPE_STUB_DECL (type) = decl;
2244 layout_type (type);
2245 rest_of_type_compilation (type, 1);
2246 rest_of_decl_compilation (decl, 1, 0);
2249 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2250 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2251 to the end of the field list pointed to by *CHAIN.
2253 Returns a pointer to the new field. */
2255 static tree
2256 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2258 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2260 DECL_CONTEXT (decl) = context;
2261 DECL_CHAIN (decl) = NULL_TREE;
2262 if (TYPE_FIELDS (context) == NULL_TREE)
2263 TYPE_FIELDS (context) = decl;
2264 if (chain != NULL)
2266 if (*chain != NULL)
2267 **chain = decl;
2268 *chain = &DECL_CHAIN (decl);
2271 return decl;
2274 /* Like `gfc_add_field_to_struct_1', but adds alignment
2275 information. */
2277 tree
2278 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2280 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2282 DECL_INITIAL (decl) = 0;
2283 SET_DECL_ALIGN (decl, 0);
2284 DECL_USER_ALIGN (decl) = 0;
2286 return decl;
2290 /* Copy the backend_decl and component backend_decls if
2291 the two derived type symbols are "equal", as described
2292 in 4.4.2 and resolved by gfc_compare_derived_types. */
2295 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2296 bool from_gsym)
2298 gfc_component *to_cm;
2299 gfc_component *from_cm;
2301 if (from == to)
2302 return 1;
2304 if (from->backend_decl == NULL
2305 || !gfc_compare_derived_types (from, to))
2306 return 0;
2308 to->backend_decl = from->backend_decl;
2310 to_cm = to->components;
2311 from_cm = from->components;
2313 /* Copy the component declarations. If a component is itself
2314 a derived type, we need a copy of its component declarations.
2315 This is done by recursing into gfc_get_derived_type and
2316 ensures that the component's component declarations have
2317 been built. If it is a character, we need the character
2318 length, as well. */
2319 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2321 to_cm->backend_decl = from_cm->backend_decl;
2322 if (from_cm->ts.type == BT_UNION)
2323 gfc_get_union_type (to_cm->ts.u.derived);
2324 else if (from_cm->ts.type == BT_DERIVED
2325 && (!from_cm->attr.pointer || from_gsym))
2326 gfc_get_derived_type (to_cm->ts.u.derived);
2327 else if (from_cm->ts.type == BT_CLASS
2328 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2329 gfc_get_derived_type (to_cm->ts.u.derived);
2330 else if (from_cm->ts.type == BT_CHARACTER)
2331 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2334 return 1;
2338 /* Build a tree node for a procedure pointer component. */
2340 tree
2341 gfc_get_ppc_type (gfc_component* c)
2343 tree t;
2345 /* Explicit interface. */
2346 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2347 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2349 /* Implicit interface (only return value may be known). */
2350 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2351 t = gfc_typenode_for_spec (&c->ts);
2352 else
2353 t = void_type_node;
2355 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2359 /* Build a tree node for a union type. Requires building each map
2360 structure which is an element of the union. */
2362 tree
2363 gfc_get_union_type (gfc_symbol *un)
2365 gfc_component *map = NULL;
2366 tree typenode = NULL, map_type = NULL, map_field = NULL;
2367 tree *chain = NULL;
2369 if (un->backend_decl)
2371 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2372 return un->backend_decl;
2373 else
2374 typenode = un->backend_decl;
2376 else
2378 typenode = make_node (UNION_TYPE);
2379 TYPE_NAME (typenode) = get_identifier (un->name);
2382 /* Add each contained MAP as a field. */
2383 for (map = un->components; map; map = map->next)
2385 gcc_assert (map->ts.type == BT_DERIVED);
2387 /* The map's type node, which is defined within this union's context. */
2388 map_type = gfc_get_derived_type (map->ts.u.derived);
2389 TYPE_CONTEXT (map_type) = typenode;
2391 /* The map field's declaration. */
2392 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2393 map_type, &chain);
2394 if (map->loc.lb)
2395 gfc_set_decl_location (map_field, &map->loc);
2396 else if (un->declared_at.lb)
2397 gfc_set_decl_location (map_field, &un->declared_at);
2399 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2400 DECL_NAMELESS(map_field) = true;
2402 /* We should never clobber another backend declaration for this map,
2403 because each map component is unique. */
2404 if (!map->backend_decl)
2405 map->backend_decl = map_field;
2408 un->backend_decl = typenode;
2409 gfc_finish_type (typenode);
2411 return typenode;
2415 /* Build a tree node for a derived type. If there are equal
2416 derived types, with different local names, these are built
2417 at the same time. If an equal derived type has been built
2418 in a parent namespace, this is used. */
2420 tree
2421 gfc_get_derived_type (gfc_symbol * derived)
2423 tree typenode = NULL, field = NULL, field_type = NULL;
2424 tree canonical = NULL_TREE;
2425 tree *chain = NULL;
2426 bool got_canonical = false;
2427 bool unlimited_entity = false;
2428 gfc_component *c;
2429 gfc_dt_list *dt;
2430 gfc_namespace *ns;
2431 tree tmp;
2433 if (derived->attr.unlimited_polymorphic
2434 || (flag_coarray == GFC_FCOARRAY_LIB
2435 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2436 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2437 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
2438 return ptr_type_node;
2440 if (flag_coarray != GFC_FCOARRAY_LIB
2441 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2442 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2443 return gfc_get_int_type (gfc_default_integer_kind);
2445 if (derived && derived->attr.flavor == FL_PROCEDURE
2446 && derived->attr.generic)
2447 derived = gfc_find_dt_in_generic (derived);
2449 /* See if it's one of the iso_c_binding derived types. */
2450 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2452 if (derived->backend_decl)
2453 return derived->backend_decl;
2455 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2456 derived->backend_decl = ptr_type_node;
2457 else
2458 derived->backend_decl = pfunc_type_node;
2460 derived->ts.kind = gfc_index_integer_kind;
2461 derived->ts.type = BT_INTEGER;
2462 /* Set the f90_type to BT_VOID as a way to recognize something of type
2463 BT_INTEGER that needs to fit a void * for the purpose of the
2464 iso_c_binding derived types. */
2465 derived->ts.f90_type = BT_VOID;
2467 return derived->backend_decl;
2470 /* If use associated, use the module type for this one. */
2471 if (derived->backend_decl == NULL
2472 && derived->attr.use_assoc
2473 && derived->module
2474 && gfc_get_module_backend_decl (derived))
2475 goto copy_derived_types;
2477 /* The derived types from an earlier namespace can be used as the
2478 canonical type. */
2479 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2480 && gfc_global_ns_list)
2482 for (ns = gfc_global_ns_list;
2483 ns->translated && !got_canonical;
2484 ns = ns->sibling)
2486 dt = ns->derived_types;
2487 for (; dt && !canonical; dt = dt->next)
2489 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2490 if (derived->backend_decl)
2491 got_canonical = true;
2496 /* Store up the canonical type to be added to this one. */
2497 if (got_canonical)
2499 if (TYPE_CANONICAL (derived->backend_decl))
2500 canonical = TYPE_CANONICAL (derived->backend_decl);
2501 else
2502 canonical = derived->backend_decl;
2504 derived->backend_decl = NULL_TREE;
2507 /* derived->backend_decl != 0 means we saw it before, but its
2508 components' backend_decl may have not been built. */
2509 if (derived->backend_decl)
2511 /* Its components' backend_decl have been built or we are
2512 seeing recursion through the formal arglist of a procedure
2513 pointer component. */
2514 if (TYPE_FIELDS (derived->backend_decl))
2515 return derived->backend_decl;
2516 else if (derived->attr.abstract
2517 && derived->attr.proc_pointer_comp)
2519 /* If an abstract derived type with procedure pointer
2520 components has no other type of component, return the
2521 backend_decl. Otherwise build the components if any of the
2522 non-procedure pointer components have no backend_decl. */
2523 for (c = derived->components; c; c = c->next)
2525 if (!c->attr.proc_pointer && c->backend_decl == NULL)
2526 break;
2527 else if (c->next == NULL)
2528 return derived->backend_decl;
2530 typenode = derived->backend_decl;
2532 else
2533 typenode = derived->backend_decl;
2535 else
2537 /* We see this derived type first time, so build the type node. */
2538 typenode = make_node (RECORD_TYPE);
2539 TYPE_NAME (typenode) = get_identifier (derived->name);
2540 TYPE_PACKED (typenode) = flag_pack_derived;
2541 derived->backend_decl = typenode;
2544 if (derived->components
2545 && derived->components->ts.type == BT_DERIVED
2546 && strcmp (derived->components->name, "_data") == 0
2547 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2548 unlimited_entity = true;
2550 /* Go through the derived type components, building them as
2551 necessary. The reason for doing this now is that it is
2552 possible to recurse back to this derived type through a
2553 pointer component (PR24092). If this happens, the fields
2554 will be built and so we can return the type. */
2555 for (c = derived->components; c; c = c->next)
2557 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2558 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2560 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2561 continue;
2563 if ((!c->attr.pointer && !c->attr.proc_pointer)
2564 || c->ts.u.derived->backend_decl == NULL)
2565 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
2567 if (c->ts.u.derived->attr.is_iso_c)
2569 /* Need to copy the modified ts from the derived type. The
2570 typespec was modified because C_PTR/C_FUNPTR are translated
2571 into (void *) from derived types. */
2572 c->ts.type = c->ts.u.derived->ts.type;
2573 c->ts.kind = c->ts.u.derived->ts.kind;
2574 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2575 if (c->initializer)
2577 c->initializer->ts.type = c->ts.type;
2578 c->initializer->ts.kind = c->ts.kind;
2579 c->initializer->ts.f90_type = c->ts.f90_type;
2580 c->initializer->expr_type = EXPR_NULL;
2585 if (TYPE_FIELDS (derived->backend_decl))
2586 return derived->backend_decl;
2588 /* Build the type member list. Install the newly created RECORD_TYPE
2589 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2590 through only the top-level linked list of components so we correctly
2591 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2592 types are built as part of gfc_get_union_type. */
2593 for (c = derived->components; c; c = c->next)
2595 /* Prevent infinite recursion, when the procedure pointer type is
2596 the same as derived, by forcing the procedure pointer component to
2597 be built as if the explicit interface does not exist. */
2598 if (c->attr.proc_pointer
2599 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2600 || (c->ts.u.derived
2601 && !gfc_compare_derived_types (derived, c->ts.u.derived))))
2602 field_type = gfc_get_ppc_type (c);
2603 else if (c->attr.proc_pointer && derived->backend_decl)
2605 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2606 field_type = build_pointer_type (tmp);
2608 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2609 field_type = c->ts.u.derived->backend_decl;
2610 else
2612 if (c->ts.type == BT_CHARACTER && !c->ts.deferred)
2614 /* Evaluate the string length. */
2615 gfc_conv_const_charlen (c->ts.u.cl);
2616 gcc_assert (c->ts.u.cl->backend_decl);
2618 else if (c->ts.type == BT_CHARACTER)
2619 c->ts.u.cl->backend_decl
2620 = build_int_cst (gfc_charlen_type_node, 0);
2622 field_type = gfc_typenode_for_spec (&c->ts);
2625 /* This returns an array descriptor type. Initialization may be
2626 required. */
2627 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2629 if (c->attr.pointer || c->attr.allocatable)
2631 enum gfc_array_kind akind;
2632 if (c->attr.pointer)
2633 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2634 : GFC_ARRAY_POINTER;
2635 else
2636 akind = GFC_ARRAY_ALLOCATABLE;
2637 /* Pointers to arrays aren't actually pointer types. The
2638 descriptors are separate, but the data is common. */
2639 field_type = gfc_build_array_type (field_type, c->as, akind,
2640 !c->attr.target
2641 && !c->attr.pointer,
2642 c->attr.contiguous);
2644 else
2645 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2646 PACKED_STATIC,
2647 !c->attr.target);
2649 else if ((c->attr.pointer || c->attr.allocatable)
2650 && !c->attr.proc_pointer
2651 && !(unlimited_entity && c == derived->components))
2652 field_type = build_pointer_type (field_type);
2654 if (c->attr.pointer)
2655 field_type = gfc_nonrestricted_type (field_type);
2657 /* vtype fields can point to different types to the base type. */
2658 if (c->ts.type == BT_DERIVED
2659 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2660 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2661 ptr_mode, true);
2663 /* Ensure that the CLASS language specific flag is set. */
2664 if (c->ts.type == BT_CLASS)
2666 if (POINTER_TYPE_P (field_type))
2667 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2668 else
2669 GFC_CLASS_TYPE_P (field_type) = 1;
2672 field = gfc_add_field_to_struct (typenode,
2673 get_identifier (c->name),
2674 field_type, &chain);
2675 if (c->loc.lb)
2676 gfc_set_decl_location (field, &c->loc);
2677 else if (derived->declared_at.lb)
2678 gfc_set_decl_location (field, &derived->declared_at);
2680 gfc_finish_decl_attrs (field, &c->attr);
2682 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2684 gcc_assert (field);
2685 if (!c->backend_decl)
2686 c->backend_decl = field;
2689 /* Now lay out the derived type, including the fields. */
2690 if (canonical)
2691 TYPE_CANONICAL (typenode) = canonical;
2693 gfc_finish_type (typenode);
2694 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2695 if (derived->module && derived->ns->proc_name
2696 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2698 if (derived->ns->proc_name->backend_decl
2699 && TREE_CODE (derived->ns->proc_name->backend_decl)
2700 == NAMESPACE_DECL)
2702 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2703 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2704 = derived->ns->proc_name->backend_decl;
2708 derived->backend_decl = typenode;
2710 copy_derived_types:
2712 for (dt = gfc_derived_types; dt; dt = dt->next)
2713 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2715 return derived->backend_decl;
2720 gfc_return_by_reference (gfc_symbol * sym)
2722 if (!sym->attr.function)
2723 return 0;
2725 if (sym->attr.dimension)
2726 return 1;
2728 if (sym->ts.type == BT_CHARACTER
2729 && !sym->attr.is_bind_c
2730 && (!sym->attr.result
2731 || !sym->ns->proc_name
2732 || !sym->ns->proc_name->attr.is_bind_c))
2733 return 1;
2735 /* Possibly return complex numbers by reference for g77 compatibility.
2736 We don't do this for calls to intrinsics (as the library uses the
2737 -fno-f2c calling convention), nor for calls to functions which always
2738 require an explicit interface, as no compatibility problems can
2739 arise there. */
2740 if (flag_f2c && sym->ts.type == BT_COMPLEX
2741 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2742 return 1;
2744 return 0;
2747 static tree
2748 gfc_get_mixed_entry_union (gfc_namespace *ns)
2750 tree type;
2751 tree *chain = NULL;
2752 char name[GFC_MAX_SYMBOL_LEN + 1];
2753 gfc_entry_list *el, *el2;
2755 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2756 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2758 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2760 /* Build the type node. */
2761 type = make_node (UNION_TYPE);
2763 TYPE_NAME (type) = get_identifier (name);
2765 for (el = ns->entries; el; el = el->next)
2767 /* Search for duplicates. */
2768 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2769 if (el2->sym->result == el->sym->result)
2770 break;
2772 if (el == el2)
2773 gfc_add_field_to_struct_1 (type,
2774 get_identifier (el->sym->result->name),
2775 gfc_sym_type (el->sym->result), &chain);
2778 /* Finish off the type. */
2779 gfc_finish_type (type);
2780 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2781 return type;
2784 /* Create a "fn spec" based on the formal arguments;
2785 cf. create_function_arglist. */
2787 static tree
2788 create_fn_spec (gfc_symbol *sym, tree fntype)
2790 char spec[150];
2791 size_t spec_len;
2792 gfc_formal_arglist *f;
2793 tree tmp;
2795 memset (&spec, 0, sizeof (spec));
2796 spec[0] = '.';
2797 spec_len = 1;
2799 if (sym->attr.entry_master)
2800 spec[spec_len++] = 'R';
2801 if (gfc_return_by_reference (sym))
2803 gfc_symbol *result = sym->result ? sym->result : sym;
2805 if (result->attr.pointer || sym->attr.proc_pointer)
2806 spec[spec_len++] = '.';
2807 else
2808 spec[spec_len++] = 'w';
2809 if (sym->ts.type == BT_CHARACTER)
2810 spec[spec_len++] = 'R';
2813 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2814 if (spec_len < sizeof (spec))
2816 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2817 || f->sym->attr.external || f->sym->attr.cray_pointer
2818 || (f->sym->ts.type == BT_DERIVED
2819 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2820 || f->sym->ts.u.derived->attr.pointer_comp))
2821 || (f->sym->ts.type == BT_CLASS
2822 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2823 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2824 spec[spec_len++] = '.';
2825 else if (f->sym->attr.intent == INTENT_IN)
2826 spec[spec_len++] = 'r';
2827 else if (f->sym)
2828 spec[spec_len++] = 'w';
2831 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2832 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2833 return build_type_attribute_variant (fntype, tmp);
2837 tree
2838 gfc_get_function_type (gfc_symbol * sym)
2840 tree type;
2841 vec<tree, va_gc> *typelist = NULL;
2842 gfc_formal_arglist *f;
2843 gfc_symbol *arg;
2844 int alternate_return = 0;
2845 bool is_varargs = true;
2847 /* Make sure this symbol is a function, a subroutine or the main
2848 program. */
2849 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2850 || sym->attr.flavor == FL_PROGRAM);
2852 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2853 so that they can be detected here and handled further down. */
2854 if (sym->backend_decl == NULL)
2855 sym->backend_decl = error_mark_node;
2856 else if (sym->backend_decl == error_mark_node)
2857 goto arg_type_list_done;
2858 else if (sym->attr.proc_pointer)
2859 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2860 else
2861 return TREE_TYPE (sym->backend_decl);
2863 if (sym->attr.entry_master)
2864 /* Additional parameter for selecting an entry point. */
2865 vec_safe_push (typelist, gfc_array_index_type);
2867 if (sym->result)
2868 arg = sym->result;
2869 else
2870 arg = sym;
2872 if (arg->ts.type == BT_CHARACTER)
2873 gfc_conv_const_charlen (arg->ts.u.cl);
2875 /* Some functions we use an extra parameter for the return value. */
2876 if (gfc_return_by_reference (sym))
2878 type = gfc_sym_type (arg);
2879 if (arg->ts.type == BT_COMPLEX
2880 || arg->attr.dimension
2881 || arg->ts.type == BT_CHARACTER)
2882 type = build_reference_type (type);
2884 vec_safe_push (typelist, type);
2885 if (arg->ts.type == BT_CHARACTER)
2887 if (!arg->ts.deferred)
2888 /* Transfer by value. */
2889 vec_safe_push (typelist, gfc_charlen_type_node);
2890 else
2891 /* Deferred character lengths are transferred by reference
2892 so that the value can be returned. */
2893 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2897 /* Build the argument types for the function. */
2898 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2900 arg = f->sym;
2901 if (arg)
2903 /* Evaluate constant character lengths here so that they can be
2904 included in the type. */
2905 if (arg->ts.type == BT_CHARACTER)
2906 gfc_conv_const_charlen (arg->ts.u.cl);
2908 if (arg->attr.flavor == FL_PROCEDURE)
2910 type = gfc_get_function_type (arg);
2911 type = build_pointer_type (type);
2913 else
2914 type = gfc_sym_type (arg);
2916 /* Parameter Passing Convention
2918 We currently pass all parameters by reference.
2919 Parameters with INTENT(IN) could be passed by value.
2920 The problem arises if a function is called via an implicit
2921 prototype. In this situation the INTENT is not known.
2922 For this reason all parameters to global functions must be
2923 passed by reference. Passing by value would potentially
2924 generate bad code. Worse there would be no way of telling that
2925 this code was bad, except that it would give incorrect results.
2927 Contained procedures could pass by value as these are never
2928 used without an explicit interface, and cannot be passed as
2929 actual parameters for a dummy procedure. */
2931 vec_safe_push (typelist, type);
2933 else
2935 if (sym->attr.subroutine)
2936 alternate_return = 1;
2940 /* Add hidden string length parameters. */
2941 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2943 arg = f->sym;
2944 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2946 if (!arg->ts.deferred)
2947 /* Transfer by value. */
2948 type = gfc_charlen_type_node;
2949 else
2950 /* Deferred character lengths are transferred by reference
2951 so that the value can be returned. */
2952 type = build_pointer_type (gfc_charlen_type_node);
2954 vec_safe_push (typelist, type);
2958 if (!vec_safe_is_empty (typelist)
2959 || sym->attr.is_main_program
2960 || sym->attr.if_source != IFSRC_UNKNOWN)
2961 is_varargs = false;
2963 if (sym->backend_decl == error_mark_node)
2964 sym->backend_decl = NULL_TREE;
2966 arg_type_list_done:
2968 if (alternate_return)
2969 type = integer_type_node;
2970 else if (!sym->attr.function || gfc_return_by_reference (sym))
2971 type = void_type_node;
2972 else if (sym->attr.mixed_entry_master)
2973 type = gfc_get_mixed_entry_union (sym->ns);
2974 else if (flag_f2c && sym->ts.type == BT_REAL
2975 && sym->ts.kind == gfc_default_real_kind
2976 && !sym->attr.always_explicit)
2978 /* Special case: f2c calling conventions require that (scalar)
2979 default REAL functions return the C type double instead. f2c
2980 compatibility is only an issue with functions that don't
2981 require an explicit interface, as only these could be
2982 implemented in Fortran 77. */
2983 sym->ts.kind = gfc_default_double_kind;
2984 type = gfc_typenode_for_spec (&sym->ts);
2985 sym->ts.kind = gfc_default_real_kind;
2987 else if (sym->result && sym->result->attr.proc_pointer)
2988 /* Procedure pointer return values. */
2990 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
2992 /* Unset proc_pointer as gfc_get_function_type
2993 is called recursively. */
2994 sym->result->attr.proc_pointer = 0;
2995 type = build_pointer_type (gfc_get_function_type (sym->result));
2996 sym->result->attr.proc_pointer = 1;
2998 else
2999 type = gfc_sym_type (sym->result);
3001 else
3002 type = gfc_sym_type (sym);
3004 if (is_varargs)
3005 type = build_varargs_function_type_vec (type, typelist);
3006 else
3007 type = build_function_type_vec (type, typelist);
3008 type = create_fn_spec (sym, type);
3010 return type;
3013 /* Language hooks for middle-end access to type nodes. */
3015 /* Return an integer type with BITS bits of precision,
3016 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3018 tree
3019 gfc_type_for_size (unsigned bits, int unsignedp)
3021 if (!unsignedp)
3023 int i;
3024 for (i = 0; i <= MAX_INT_KINDS; ++i)
3026 tree type = gfc_integer_types[i];
3027 if (type && bits == TYPE_PRECISION (type))
3028 return type;
3031 /* Handle TImode as a special case because it is used by some backends
3032 (e.g. ARM) even though it is not available for normal use. */
3033 #if HOST_BITS_PER_WIDE_INT >= 64
3034 if (bits == TYPE_PRECISION (intTI_type_node))
3035 return intTI_type_node;
3036 #endif
3038 if (bits <= TYPE_PRECISION (intQI_type_node))
3039 return intQI_type_node;
3040 if (bits <= TYPE_PRECISION (intHI_type_node))
3041 return intHI_type_node;
3042 if (bits <= TYPE_PRECISION (intSI_type_node))
3043 return intSI_type_node;
3044 if (bits <= TYPE_PRECISION (intDI_type_node))
3045 return intDI_type_node;
3046 if (bits <= TYPE_PRECISION (intTI_type_node))
3047 return intTI_type_node;
3049 else
3051 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3052 return unsigned_intQI_type_node;
3053 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3054 return unsigned_intHI_type_node;
3055 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3056 return unsigned_intSI_type_node;
3057 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3058 return unsigned_intDI_type_node;
3059 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3060 return unsigned_intTI_type_node;
3063 return NULL_TREE;
3066 /* Return a data type that has machine mode MODE. If the mode is an
3067 integer, then UNSIGNEDP selects between signed and unsigned types. */
3069 tree
3070 gfc_type_for_mode (machine_mode mode, int unsignedp)
3072 int i;
3073 tree *base;
3075 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3076 base = gfc_real_types;
3077 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3078 base = gfc_complex_types;
3079 else if (SCALAR_INT_MODE_P (mode))
3081 tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
3082 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3084 else if (VECTOR_MODE_P (mode))
3086 machine_mode inner_mode = GET_MODE_INNER (mode);
3087 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3088 if (inner_type != NULL_TREE)
3089 return build_vector_type_for_mode (inner_type, mode);
3090 return NULL_TREE;
3092 else
3093 return NULL_TREE;
3095 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3097 tree type = base[i];
3098 if (type && mode == TYPE_MODE (type))
3099 return type;
3102 return NULL_TREE;
3105 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3106 in that case. */
3108 bool
3109 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3111 int rank, dim;
3112 bool indirect = false;
3113 tree etype, ptype, field, t, base_decl;
3114 tree data_off, dim_off, dim_size, elem_size;
3115 tree lower_suboff, upper_suboff, stride_suboff;
3117 if (! GFC_DESCRIPTOR_TYPE_P (type))
3119 if (! POINTER_TYPE_P (type))
3120 return false;
3121 type = TREE_TYPE (type);
3122 if (! GFC_DESCRIPTOR_TYPE_P (type))
3123 return false;
3124 indirect = true;
3127 rank = GFC_TYPE_ARRAY_RANK (type);
3128 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3129 return false;
3131 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3132 gcc_assert (POINTER_TYPE_P (etype));
3133 etype = TREE_TYPE (etype);
3135 /* If the type is not a scalar coarray. */
3136 if (TREE_CODE (etype) == ARRAY_TYPE)
3137 etype = TREE_TYPE (etype);
3139 /* Can't handle variable sized elements yet. */
3140 if (int_size_in_bytes (etype) <= 0)
3141 return false;
3142 /* Nor non-constant lower bounds in assumed shape arrays. */
3143 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3144 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3146 for (dim = 0; dim < rank; dim++)
3147 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3148 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3149 return false;
3152 memset (info, '\0', sizeof (*info));
3153 info->ndimensions = rank;
3154 info->ordering = array_descr_ordering_column_major;
3155 info->element_type = etype;
3156 ptype = build_pointer_type (gfc_array_index_type);
3157 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3158 if (!base_decl)
3160 base_decl = make_node (DEBUG_EXPR_DECL);
3161 DECL_ARTIFICIAL (base_decl) = 1;
3162 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3163 DECL_MODE (base_decl) = TYPE_MODE (TREE_TYPE (base_decl));
3164 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3166 info->base_decl = base_decl;
3167 if (indirect)
3168 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3170 if (GFC_TYPE_ARRAY_SPAN (type))
3171 elem_size = GFC_TYPE_ARRAY_SPAN (type);
3172 else
3173 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3174 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
3175 data_off = byte_position (field);
3176 field = DECL_CHAIN (field);
3177 field = DECL_CHAIN (field);
3178 field = DECL_CHAIN (field);
3179 dim_off = byte_position (field);
3180 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
3181 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
3182 stride_suboff = byte_position (field);
3183 field = DECL_CHAIN (field);
3184 lower_suboff = byte_position (field);
3185 field = DECL_CHAIN (field);
3186 upper_suboff = byte_position (field);
3188 t = base_decl;
3189 if (!integer_zerop (data_off))
3190 t = fold_build_pointer_plus (t, data_off);
3191 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3192 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3193 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3194 info->allocated = build2 (NE_EXPR, boolean_type_node,
3195 info->data_location, null_pointer_node);
3196 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3197 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3198 info->associated = build2 (NE_EXPR, boolean_type_node,
3199 info->data_location, null_pointer_node);
3201 for (dim = 0; dim < rank; dim++)
3203 t = fold_build_pointer_plus (base_decl,
3204 size_binop (PLUS_EXPR,
3205 dim_off, lower_suboff));
3206 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3207 info->dimen[dim].lower_bound = t;
3208 t = fold_build_pointer_plus (base_decl,
3209 size_binop (PLUS_EXPR,
3210 dim_off, upper_suboff));
3211 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3212 info->dimen[dim].upper_bound = t;
3213 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3214 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3216 /* Assumed shape arrays have known lower bounds. */
3217 info->dimen[dim].upper_bound
3218 = build2 (MINUS_EXPR, gfc_array_index_type,
3219 info->dimen[dim].upper_bound,
3220 info->dimen[dim].lower_bound);
3221 info->dimen[dim].lower_bound
3222 = fold_convert (gfc_array_index_type,
3223 GFC_TYPE_ARRAY_LBOUND (type, dim));
3224 info->dimen[dim].upper_bound
3225 = build2 (PLUS_EXPR, gfc_array_index_type,
3226 info->dimen[dim].lower_bound,
3227 info->dimen[dim].upper_bound);
3229 t = fold_build_pointer_plus (base_decl,
3230 size_binop (PLUS_EXPR,
3231 dim_off, stride_suboff));
3232 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3233 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3234 info->dimen[dim].stride = t;
3235 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3238 return true;
3242 /* Create a type to handle vector subscripts for coarray library calls. It
3243 has the form:
3244 struct caf_vector_t {
3245 size_t nvec; // size of the vector
3246 union {
3247 struct {
3248 void *vector;
3249 int kind;
3250 } v;
3251 struct {
3252 ptrdiff_t lower_bound;
3253 ptrdiff_t upper_bound;
3254 ptrdiff_t stride;
3255 } triplet;
3256 } u;
3258 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3259 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3261 tree
3262 gfc_get_caf_vector_type (int dim)
3264 static tree vector_types[GFC_MAX_DIMENSIONS];
3265 static tree vec_type = NULL_TREE;
3266 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3268 if (vector_types[dim-1] != NULL_TREE)
3269 return vector_types[dim-1];
3271 if (vec_type == NULL_TREE)
3273 chain = 0;
3274 vect_struct_type = make_node (RECORD_TYPE);
3275 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3276 get_identifier ("vector"),
3277 pvoid_type_node, &chain);
3278 TREE_NO_WARNING (tmp) = 1;
3279 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3280 get_identifier ("kind"),
3281 integer_type_node, &chain);
3282 TREE_NO_WARNING (tmp) = 1;
3283 gfc_finish_type (vect_struct_type);
3285 chain = 0;
3286 triplet_struct_type = make_node (RECORD_TYPE);
3287 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3288 get_identifier ("lower_bound"),
3289 gfc_array_index_type, &chain);
3290 TREE_NO_WARNING (tmp) = 1;
3291 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3292 get_identifier ("upper_bound"),
3293 gfc_array_index_type, &chain);
3294 TREE_NO_WARNING (tmp) = 1;
3295 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3296 gfc_array_index_type, &chain);
3297 TREE_NO_WARNING (tmp) = 1;
3298 gfc_finish_type (triplet_struct_type);
3300 chain = 0;
3301 union_type = make_node (UNION_TYPE);
3302 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3303 vect_struct_type, &chain);
3304 TREE_NO_WARNING (tmp) = 1;
3305 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3306 triplet_struct_type, &chain);
3307 TREE_NO_WARNING (tmp) = 1;
3308 gfc_finish_type (union_type);
3310 chain = 0;
3311 vec_type = make_node (RECORD_TYPE);
3312 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3313 size_type_node, &chain);
3314 TREE_NO_WARNING (tmp) = 1;
3315 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3316 union_type, &chain);
3317 TREE_NO_WARNING (tmp) = 1;
3318 gfc_finish_type (vec_type);
3319 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3322 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3323 gfc_rank_cst[dim-1]);
3324 vector_types[dim-1] = build_array_type (vec_type, tmp);
3325 return vector_types[dim-1];
3328 #include "gt-fortran-trans-types.h"