2015-12-02 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / trans-types.c
blob60bd8e1b9820743ea89dcbbc82fdea5a869bb964
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2015 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 layout_type (new_type);
832 return new_type;
835 static tree
836 gfc_build_logical_type (gfc_logical_info *info)
838 int bit_size = info->bit_size;
839 tree new_type;
841 if (bit_size == BOOL_TYPE_SIZE)
843 info->c_bool = 1;
844 return boolean_type_node;
847 new_type = make_unsigned_type (bit_size);
848 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
849 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
850 TYPE_PRECISION (new_type) = 1;
852 return new_type;
856 /* Create the backend type nodes. We map them to their
857 equivalent C type, at least for now. We also give
858 names to the types here, and we push them in the
859 global binding level context.*/
861 void
862 gfc_init_types (void)
864 char name_buf[18];
865 int index;
866 tree type;
867 unsigned n;
869 /* Create and name the types. */
870 #define PUSH_TYPE(name, node) \
871 pushdecl (build_decl (input_location, \
872 TYPE_DECL, get_identifier (name), node))
874 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
876 type = gfc_build_int_type (&gfc_integer_kinds[index]);
877 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
878 if (TYPE_STRING_FLAG (type))
879 type = make_signed_type (gfc_integer_kinds[index].bit_size);
880 gfc_integer_types[index] = type;
881 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
882 gfc_integer_kinds[index].kind);
883 PUSH_TYPE (name_buf, type);
886 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
888 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
889 gfc_logical_types[index] = type;
890 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
891 gfc_logical_kinds[index].kind);
892 PUSH_TYPE (name_buf, type);
895 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
897 type = gfc_build_real_type (&gfc_real_kinds[index]);
898 gfc_real_types[index] = type;
899 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
900 gfc_real_kinds[index].kind);
901 PUSH_TYPE (name_buf, type);
903 if (gfc_real_kinds[index].c_float128)
904 float128_type_node = type;
906 type = gfc_build_complex_type (type);
907 gfc_complex_types[index] = type;
908 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
909 gfc_real_kinds[index].kind);
910 PUSH_TYPE (name_buf, type);
912 if (gfc_real_kinds[index].c_float128)
913 complex_float128_type_node = type;
916 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
918 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
919 type = build_qualified_type (type, TYPE_UNQUALIFIED);
920 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
921 gfc_character_kinds[index].kind);
922 PUSH_TYPE (name_buf, type);
923 gfc_character_types[index] = type;
924 gfc_pcharacter_types[index] = build_pointer_type (type);
926 gfc_character1_type_node = gfc_character_types[0];
928 PUSH_TYPE ("byte", unsigned_char_type_node);
929 PUSH_TYPE ("void", void_type_node);
931 /* DBX debugging output gets upset if these aren't set. */
932 if (!TYPE_NAME (integer_type_node))
933 PUSH_TYPE ("c_integer", integer_type_node);
934 if (!TYPE_NAME (char_type_node))
935 PUSH_TYPE ("c_char", char_type_node);
937 #undef PUSH_TYPE
939 pvoid_type_node = build_pointer_type (void_type_node);
940 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
941 ppvoid_type_node = build_pointer_type (pvoid_type_node);
942 pchar_type_node = build_pointer_type (gfc_character1_type_node);
943 pfunc_type_node
944 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
946 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
947 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
948 since this function is called before gfc_init_constants. */
949 gfc_array_range_type
950 = build_range_type (gfc_array_index_type,
951 build_int_cst (gfc_array_index_type, 0),
952 NULL_TREE);
954 /* The maximum array element size that can be handled is determined
955 by the number of bits available to store this field in the array
956 descriptor. */
958 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
959 gfc_max_array_element_size
960 = wide_int_to_tree (size_type_node,
961 wi::mask (n, UNSIGNED,
962 TYPE_PRECISION (size_type_node)));
964 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
965 boolean_true_node = build_int_cst (boolean_type_node, 1);
966 boolean_false_node = build_int_cst (boolean_type_node, 0);
968 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
969 gfc_charlen_int_kind = 4;
970 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
973 /* Get the type node for the given type and kind. */
975 tree
976 gfc_get_int_type (int kind)
978 int index = gfc_validate_kind (BT_INTEGER, kind, true);
979 return index < 0 ? 0 : gfc_integer_types[index];
982 tree
983 gfc_get_real_type (int kind)
985 int index = gfc_validate_kind (BT_REAL, kind, true);
986 return index < 0 ? 0 : gfc_real_types[index];
989 tree
990 gfc_get_complex_type (int kind)
992 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
993 return index < 0 ? 0 : gfc_complex_types[index];
996 tree
997 gfc_get_logical_type (int kind)
999 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1000 return index < 0 ? 0 : gfc_logical_types[index];
1003 tree
1004 gfc_get_char_type (int kind)
1006 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1007 return index < 0 ? 0 : gfc_character_types[index];
1010 tree
1011 gfc_get_pchar_type (int kind)
1013 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1014 return index < 0 ? 0 : gfc_pcharacter_types[index];
1018 /* Create a character type with the given kind and length. */
1020 tree
1021 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1023 tree bounds, type;
1025 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1026 type = build_array_type (eltype, bounds);
1027 TYPE_STRING_FLAG (type) = 1;
1029 return type;
1032 tree
1033 gfc_get_character_type_len (int kind, tree len)
1035 gfc_validate_kind (BT_CHARACTER, kind, false);
1036 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1040 /* Get a type node for a character kind. */
1042 tree
1043 gfc_get_character_type (int kind, gfc_charlen * cl)
1045 tree len;
1047 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1049 return gfc_get_character_type_len (kind, len);
1052 /* Covert a basic type. This will be an array for character types. */
1054 tree
1055 gfc_typenode_for_spec (gfc_typespec * spec)
1057 tree basetype;
1059 switch (spec->type)
1061 case BT_UNKNOWN:
1062 gcc_unreachable ();
1064 case BT_INTEGER:
1065 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1066 has been resolved. This is done so we can convert C_PTR and
1067 C_FUNPTR to simple variables that get translated to (void *). */
1068 if (spec->f90_type == BT_VOID)
1070 if (spec->u.derived
1071 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1072 basetype = ptr_type_node;
1073 else
1074 basetype = pfunc_type_node;
1076 else
1077 basetype = gfc_get_int_type (spec->kind);
1078 break;
1080 case BT_REAL:
1081 basetype = gfc_get_real_type (spec->kind);
1082 break;
1084 case BT_COMPLEX:
1085 basetype = gfc_get_complex_type (spec->kind);
1086 break;
1088 case BT_LOGICAL:
1089 basetype = gfc_get_logical_type (spec->kind);
1090 break;
1092 case BT_CHARACTER:
1093 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1094 break;
1096 case BT_HOLLERITH:
1097 /* Since this cannot be used, return a length one character. */
1098 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1099 gfc_index_one_node);
1100 break;
1102 case BT_DERIVED:
1103 case BT_CLASS:
1104 basetype = gfc_get_derived_type (spec->u.derived);
1106 if (spec->type == BT_CLASS)
1107 GFC_CLASS_TYPE_P (basetype) = 1;
1109 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1110 type and kind to fit a (void *) and the basetype returned was a
1111 ptr_type_node. We need to pass up this new information to the
1112 symbol that was declared of type C_PTR or C_FUNPTR. */
1113 if (spec->u.derived->ts.f90_type == BT_VOID)
1115 spec->type = BT_INTEGER;
1116 spec->kind = gfc_index_integer_kind;
1117 spec->f90_type = BT_VOID;
1119 break;
1120 case BT_VOID:
1121 case BT_ASSUMED:
1122 /* This is for the second arg to c_f_pointer and c_f_procpointer
1123 of the iso_c_binding module, to accept any ptr type. */
1124 basetype = ptr_type_node;
1125 if (spec->f90_type == BT_VOID)
1127 if (spec->u.derived
1128 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1129 basetype = ptr_type_node;
1130 else
1131 basetype = pfunc_type_node;
1133 break;
1134 default:
1135 gcc_unreachable ();
1137 return basetype;
1140 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1142 static tree
1143 gfc_conv_array_bound (gfc_expr * expr)
1145 /* If expr is an integer constant, return that. */
1146 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1147 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1149 /* Otherwise return NULL. */
1150 return NULL_TREE;
1153 /* Return the type of an element of the array. Note that scalar coarrays
1154 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1155 (with POINTER_TYPE stripped) is returned. */
1157 tree
1158 gfc_get_element_type (tree type)
1160 tree element;
1162 if (GFC_ARRAY_TYPE_P (type))
1164 if (TREE_CODE (type) == POINTER_TYPE)
1165 type = TREE_TYPE (type);
1166 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1168 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1169 element = type;
1171 else
1173 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1174 element = TREE_TYPE (type);
1177 else
1179 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1180 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1182 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1183 element = TREE_TYPE (element);
1185 /* For arrays, which are not scalar coarrays. */
1186 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1187 element = TREE_TYPE (element);
1190 return element;
1193 /* Build an array. This function is called from gfc_sym_type().
1194 Actually returns array descriptor type.
1196 Format of array descriptors is as follows:
1198 struct gfc_array_descriptor
1200 array *data
1201 index offset;
1202 index dtype;
1203 struct descriptor_dimension dimension[N_DIM];
1206 struct descriptor_dimension
1208 index stride;
1209 index lbound;
1210 index ubound;
1213 Translation code should use gfc_conv_descriptor_* rather than
1214 accessing the descriptor directly. Any changes to the array
1215 descriptor type will require changes in gfc_conv_descriptor_* and
1216 gfc_build_array_initializer.
1218 This is represented internally as a RECORD_TYPE. The index nodes
1219 are gfc_array_index_type and the data node is a pointer to the
1220 data. See below for the handling of character types.
1222 The dtype member is formatted as follows:
1223 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1224 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1225 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1227 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1228 this generated poor code for assumed/deferred size arrays. These
1229 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1230 of the GENERIC grammar. Also, there is no way to explicitly set
1231 the array stride, so all data must be packed(1). I've tried to
1232 mark all the functions which would require modification with a GCC
1233 ARRAYS comment.
1235 The data component points to the first element in the array. The
1236 offset field is the position of the origin of the array (i.e. element
1237 (0, 0 ...)). This may be outside the bounds of the array.
1239 An element is accessed by
1240 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1241 This gives good performance as the computation does not involve the
1242 bounds of the array. For packed arrays, this is optimized further
1243 by substituting the known strides.
1245 This system has one problem: all array bounds must be within 2^31
1246 elements of the origin (2^63 on 64-bit machines). For example
1247 integer, dimension (80000:90000, 80000:90000, 2) :: array
1248 may not work properly on 32-bit machines because 80000*80000 >
1249 2^31, so the calculation for stride2 would overflow. This may
1250 still work, but I haven't checked, and it relies on the overflow
1251 doing the right thing.
1253 The way to fix this problem is to access elements as follows:
1254 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1255 Obviously this is much slower. I will make this a compile time
1256 option, something like -fsmall-array-offsets. Mixing code compiled
1257 with and without this switch will work.
1259 (1) This can be worked around by modifying the upper bound of the
1260 previous dimension. This requires extra fields in the descriptor
1261 (both real_ubound and fake_ubound). */
1264 /* Returns true if the array sym does not require a descriptor. */
1267 gfc_is_nodesc_array (gfc_symbol * sym)
1269 symbol_attribute *array_attr;
1270 gfc_array_spec *as;
1271 bool is_classarray = IS_CLASS_ARRAY (sym);
1273 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1274 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1276 gcc_assert (array_attr->dimension || array_attr->codimension);
1278 /* We only want local arrays. */
1279 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1280 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1281 || array_attr->allocatable)
1282 return 0;
1284 /* We want a descriptor for associate-name arrays that do not have an
1285 explicitly known shape already. */
1286 if (sym->assoc && as->type != AS_EXPLICIT)
1287 return 0;
1289 /* The dummy is stored in sym and not in the component. */
1290 if (sym->attr.dummy)
1291 return as->type != AS_ASSUMED_SHAPE
1292 && as->type != AS_ASSUMED_RANK;
1294 if (sym->attr.result || sym->attr.function)
1295 return 0;
1297 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1299 return 1;
1303 /* Create an array descriptor type. */
1305 static tree
1306 gfc_build_array_type (tree type, gfc_array_spec * as,
1307 enum gfc_array_kind akind, bool restricted,
1308 bool contiguous)
1310 tree lbound[GFC_MAX_DIMENSIONS];
1311 tree ubound[GFC_MAX_DIMENSIONS];
1312 int n, corank;
1314 /* Assumed-shape arrays do not have codimension information stored in the
1315 descriptor. */
1316 corank = as->corank;
1317 if (as->type == AS_ASSUMED_SHAPE ||
1318 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1319 corank = 0;
1321 if (as->type == AS_ASSUMED_RANK)
1322 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1324 lbound[n] = NULL_TREE;
1325 ubound[n] = NULL_TREE;
1328 for (n = 0; n < as->rank; n++)
1330 /* Create expressions for the known bounds of the array. */
1331 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1332 lbound[n] = gfc_index_one_node;
1333 else
1334 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1335 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1338 for (n = as->rank; n < as->rank + corank; n++)
1340 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1341 lbound[n] = gfc_index_one_node;
1342 else
1343 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1345 if (n < as->rank + corank - 1)
1346 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1349 if (as->type == AS_ASSUMED_SHAPE)
1350 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1351 : GFC_ARRAY_ASSUMED_SHAPE;
1352 else if (as->type == AS_ASSUMED_RANK)
1353 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1354 : GFC_ARRAY_ASSUMED_RANK;
1355 return gfc_get_array_type_bounds (type, as->rank == -1
1356 ? GFC_MAX_DIMENSIONS : as->rank,
1357 corank, lbound,
1358 ubound, 0, akind, restricted);
1361 /* Returns the struct descriptor_dimension type. */
1363 static tree
1364 gfc_get_desc_dim_type (void)
1366 tree type;
1367 tree decl, *chain = NULL;
1369 if (gfc_desc_dim_type)
1370 return gfc_desc_dim_type;
1372 /* Build the type node. */
1373 type = make_node (RECORD_TYPE);
1375 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1376 TYPE_PACKED (type) = 1;
1378 /* Consists of the stride, lbound and ubound members. */
1379 decl = gfc_add_field_to_struct_1 (type,
1380 get_identifier ("stride"),
1381 gfc_array_index_type, &chain);
1382 TREE_NO_WARNING (decl) = 1;
1384 decl = gfc_add_field_to_struct_1 (type,
1385 get_identifier ("lbound"),
1386 gfc_array_index_type, &chain);
1387 TREE_NO_WARNING (decl) = 1;
1389 decl = gfc_add_field_to_struct_1 (type,
1390 get_identifier ("ubound"),
1391 gfc_array_index_type, &chain);
1392 TREE_NO_WARNING (decl) = 1;
1394 /* Finish off the type. */
1395 gfc_finish_type (type);
1396 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1398 gfc_desc_dim_type = type;
1399 return type;
1403 /* Return the DTYPE for an array. This describes the type and type parameters
1404 of the array. */
1405 /* TODO: Only call this when the value is actually used, and make all the
1406 unknown cases abort. */
1408 tree
1409 gfc_get_dtype_rank_type (int rank, tree etype)
1411 tree size;
1412 int n;
1413 HOST_WIDE_INT i;
1414 tree tmp;
1415 tree dtype;
1417 switch (TREE_CODE (etype))
1419 case INTEGER_TYPE:
1420 n = BT_INTEGER;
1421 break;
1423 case BOOLEAN_TYPE:
1424 n = BT_LOGICAL;
1425 break;
1427 case REAL_TYPE:
1428 n = BT_REAL;
1429 break;
1431 case COMPLEX_TYPE:
1432 n = BT_COMPLEX;
1433 break;
1435 /* We will never have arrays of arrays. */
1436 case RECORD_TYPE:
1437 n = BT_DERIVED;
1438 break;
1440 case ARRAY_TYPE:
1441 n = BT_CHARACTER;
1442 break;
1444 case POINTER_TYPE:
1445 n = BT_ASSUMED;
1446 break;
1448 default:
1449 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1450 /* We can strange array types for temporary arrays. */
1451 return gfc_index_zero_node;
1454 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1455 size = TYPE_SIZE_UNIT (etype);
1457 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1458 if (size && INTEGER_CST_P (size))
1460 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1461 gfc_fatal_error ("Array element size too big at %C");
1463 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1465 dtype = build_int_cst (gfc_array_index_type, i);
1467 if (size && !INTEGER_CST_P (size))
1469 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1470 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1471 gfc_array_index_type,
1472 fold_convert (gfc_array_index_type, size), tmp);
1473 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1474 tmp, dtype);
1476 /* If we don't know the size we leave it as zero. This should never happen
1477 for anything that is actually used. */
1478 /* TODO: Check this is actually true, particularly when repacking
1479 assumed size parameters. */
1481 return dtype;
1485 tree
1486 gfc_get_dtype (tree type)
1488 tree dtype;
1489 tree etype;
1490 int rank;
1492 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1494 if (GFC_TYPE_ARRAY_DTYPE (type))
1495 return GFC_TYPE_ARRAY_DTYPE (type);
1497 rank = GFC_TYPE_ARRAY_RANK (type);
1498 etype = gfc_get_element_type (type);
1499 dtype = gfc_get_dtype_rank_type (rank, etype);
1501 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1502 return dtype;
1506 /* Build an array type for use without a descriptor, packed according
1507 to the value of PACKED. */
1509 tree
1510 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1511 bool restricted)
1513 tree range;
1514 tree type;
1515 tree tmp;
1516 int n;
1517 int known_stride;
1518 int known_offset;
1519 mpz_t offset;
1520 mpz_t stride;
1521 mpz_t delta;
1522 gfc_expr *expr;
1524 mpz_init_set_ui (offset, 0);
1525 mpz_init_set_ui (stride, 1);
1526 mpz_init (delta);
1528 /* We don't use build_array_type because this does not include include
1529 lang-specific information (i.e. the bounds of the array) when checking
1530 for duplicates. */
1531 if (as->rank)
1532 type = make_node (ARRAY_TYPE);
1533 else
1534 type = build_variant_type_copy (etype);
1536 GFC_ARRAY_TYPE_P (type) = 1;
1537 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1539 known_stride = (packed != PACKED_NO);
1540 known_offset = 1;
1541 for (n = 0; n < as->rank; n++)
1543 /* Fill in the stride and bound components of the type. */
1544 if (known_stride)
1545 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1546 else
1547 tmp = NULL_TREE;
1548 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1550 expr = as->lower[n];
1551 if (expr->expr_type == EXPR_CONSTANT)
1553 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1554 gfc_index_integer_kind);
1556 else
1558 known_stride = 0;
1559 tmp = NULL_TREE;
1561 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1563 if (known_stride)
1565 /* Calculate the offset. */
1566 mpz_mul (delta, stride, as->lower[n]->value.integer);
1567 mpz_sub (offset, offset, delta);
1569 else
1570 known_offset = 0;
1572 expr = as->upper[n];
1573 if (expr && expr->expr_type == EXPR_CONSTANT)
1575 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1576 gfc_index_integer_kind);
1578 else
1580 tmp = NULL_TREE;
1581 known_stride = 0;
1583 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1585 if (known_stride)
1587 /* Calculate the stride. */
1588 mpz_sub (delta, as->upper[n]->value.integer,
1589 as->lower[n]->value.integer);
1590 mpz_add_ui (delta, delta, 1);
1591 mpz_mul (stride, stride, delta);
1594 /* Only the first stride is known for partial packed arrays. */
1595 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1596 known_stride = 0;
1598 for (n = as->rank; n < as->rank + as->corank; n++)
1600 expr = as->lower[n];
1601 if (expr->expr_type == EXPR_CONSTANT)
1602 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1603 gfc_index_integer_kind);
1604 else
1605 tmp = NULL_TREE;
1606 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1608 expr = as->upper[n];
1609 if (expr && expr->expr_type == EXPR_CONSTANT)
1610 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1611 gfc_index_integer_kind);
1612 else
1613 tmp = NULL_TREE;
1614 if (n < as->rank + as->corank - 1)
1615 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1618 if (known_offset)
1620 GFC_TYPE_ARRAY_OFFSET (type) =
1621 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1623 else
1624 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1626 if (known_stride)
1628 GFC_TYPE_ARRAY_SIZE (type) =
1629 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1631 else
1632 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1634 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1635 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1636 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1637 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1638 NULL_TREE);
1639 /* TODO: use main type if it is unbounded. */
1640 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1641 build_pointer_type (build_array_type (etype, range));
1642 if (restricted)
1643 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1644 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1645 TYPE_QUAL_RESTRICT);
1647 if (as->rank == 0)
1649 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1651 type = build_pointer_type (type);
1653 if (restricted)
1654 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1656 GFC_ARRAY_TYPE_P (type) = 1;
1657 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1660 return type;
1663 if (known_stride)
1665 mpz_sub_ui (stride, stride, 1);
1666 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1668 else
1669 range = NULL_TREE;
1671 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1672 TYPE_DOMAIN (type) = range;
1674 build_pointer_type (etype);
1675 TREE_TYPE (type) = etype;
1677 layout_type (type);
1679 mpz_clear (offset);
1680 mpz_clear (stride);
1681 mpz_clear (delta);
1683 /* Represent packed arrays as multi-dimensional if they have rank >
1684 1 and with proper bounds, instead of flat arrays. This makes for
1685 better debug info. */
1686 if (known_offset)
1688 tree gtype = etype, rtype, type_decl;
1690 for (n = as->rank - 1; n >= 0; n--)
1692 rtype = build_range_type (gfc_array_index_type,
1693 GFC_TYPE_ARRAY_LBOUND (type, n),
1694 GFC_TYPE_ARRAY_UBOUND (type, n));
1695 gtype = build_array_type (gtype, rtype);
1697 TYPE_NAME (type) = type_decl = build_decl (input_location,
1698 TYPE_DECL, NULL, gtype);
1699 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1702 if (packed != PACKED_STATIC || !known_stride
1703 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1705 /* For dummy arrays and automatic (heap allocated) arrays we
1706 want a pointer to the array. */
1707 type = build_pointer_type (type);
1708 if (restricted)
1709 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1710 GFC_ARRAY_TYPE_P (type) = 1;
1711 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1713 return type;
1717 /* Return or create the base type for an array descriptor. */
1719 static tree
1720 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted,
1721 enum gfc_array_kind akind)
1723 tree fat_type, decl, arraytype, *chain = NULL;
1724 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1725 int idx;
1727 /* Assumed-rank array. */
1728 if (dimen == -1)
1729 dimen = GFC_MAX_DIMENSIONS;
1731 idx = 2 * (codimen + dimen) + restricted;
1733 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1735 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1737 if (gfc_array_descriptor_base_caf[idx])
1738 return gfc_array_descriptor_base_caf[idx];
1740 else if (gfc_array_descriptor_base[idx])
1741 return gfc_array_descriptor_base[idx];
1743 /* Build the type node. */
1744 fat_type = make_node (RECORD_TYPE);
1746 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1747 TYPE_NAME (fat_type) = get_identifier (name);
1748 TYPE_NAMELESS (fat_type) = 1;
1750 /* Add the data member as the first element of the descriptor. */
1751 decl = gfc_add_field_to_struct_1 (fat_type,
1752 get_identifier ("data"),
1753 (restricted
1754 ? prvoid_type_node
1755 : ptr_type_node), &chain);
1757 /* Add the base component. */
1758 decl = gfc_add_field_to_struct_1 (fat_type,
1759 get_identifier ("offset"),
1760 gfc_array_index_type, &chain);
1761 TREE_NO_WARNING (decl) = 1;
1763 /* Add the dtype component. */
1764 decl = gfc_add_field_to_struct_1 (fat_type,
1765 get_identifier ("dtype"),
1766 gfc_array_index_type, &chain);
1767 TREE_NO_WARNING (decl) = 1;
1769 /* Build the array type for the stride and bound components. */
1770 if (dimen + codimen > 0)
1772 arraytype =
1773 build_array_type (gfc_get_desc_dim_type (),
1774 build_range_type (gfc_array_index_type,
1775 gfc_index_zero_node,
1776 gfc_rank_cst[codimen + dimen - 1]));
1778 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1779 arraytype, &chain);
1780 TREE_NO_WARNING (decl) = 1;
1783 if (flag_coarray == GFC_FCOARRAY_LIB && codimen
1784 && akind == GFC_ARRAY_ALLOCATABLE)
1786 decl = gfc_add_field_to_struct_1 (fat_type,
1787 get_identifier ("token"),
1788 prvoid_type_node, &chain);
1789 TREE_NO_WARNING (decl) = 1;
1792 /* Finish off the type. */
1793 gfc_finish_type (fat_type);
1794 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1796 if (flag_coarray == GFC_FCOARRAY_LIB && codimen
1797 && akind == GFC_ARRAY_ALLOCATABLE)
1798 gfc_array_descriptor_base_caf[idx] = fat_type;
1799 else
1800 gfc_array_descriptor_base[idx] = fat_type;
1802 return fat_type;
1806 /* Build an array (descriptor) type with given bounds. */
1808 tree
1809 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1810 tree * ubound, int packed,
1811 enum gfc_array_kind akind, bool restricted)
1813 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1814 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1815 const char *type_name;
1816 int n;
1818 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted, akind);
1819 fat_type = build_distinct_type_copy (base_type);
1820 /* Make sure that nontarget and target array type have the same canonical
1821 type (and same stub decl for debug info). */
1822 base_type = gfc_get_array_descriptor_base (dimen, codimen, false, akind);
1823 TYPE_CANONICAL (fat_type) = base_type;
1824 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1826 tmp = TYPE_NAME (etype);
1827 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1828 tmp = DECL_NAME (tmp);
1829 if (tmp)
1830 type_name = IDENTIFIER_POINTER (tmp);
1831 else
1832 type_name = "unknown";
1833 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1834 GFC_MAX_SYMBOL_LEN, type_name);
1835 TYPE_NAME (fat_type) = get_identifier (name);
1836 TYPE_NAMELESS (fat_type) = 1;
1838 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1839 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1841 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1842 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1843 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1844 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1846 /* Build an array descriptor record type. */
1847 if (packed != 0)
1848 stride = gfc_index_one_node;
1849 else
1850 stride = NULL_TREE;
1851 for (n = 0; n < dimen + codimen; n++)
1853 if (n < dimen)
1854 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1856 if (lbound)
1857 lower = lbound[n];
1858 else
1859 lower = NULL_TREE;
1861 if (lower != NULL_TREE)
1863 if (INTEGER_CST_P (lower))
1864 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1865 else
1866 lower = NULL_TREE;
1869 if (codimen && n == dimen + codimen - 1)
1870 break;
1872 upper = ubound[n];
1873 if (upper != NULL_TREE)
1875 if (INTEGER_CST_P (upper))
1876 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1877 else
1878 upper = NULL_TREE;
1881 if (n >= dimen)
1882 continue;
1884 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1886 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1887 gfc_array_index_type, upper, lower);
1888 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1889 gfc_array_index_type, tmp,
1890 gfc_index_one_node);
1891 stride = fold_build2_loc (input_location, MULT_EXPR,
1892 gfc_array_index_type, tmp, stride);
1893 /* Check the folding worked. */
1894 gcc_assert (INTEGER_CST_P (stride));
1896 else
1897 stride = NULL_TREE;
1899 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1901 /* TODO: known offsets for descriptors. */
1902 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1904 if (dimen == 0)
1906 arraytype = build_pointer_type (etype);
1907 if (restricted)
1908 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1910 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1911 return fat_type;
1914 /* We define data as an array with the correct size if possible.
1915 Much better than doing pointer arithmetic. */
1916 if (stride)
1917 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1918 int_const_binop (MINUS_EXPR, stride,
1919 build_int_cst (TREE_TYPE (stride), 1)));
1920 else
1921 rtype = gfc_array_range_type;
1922 arraytype = build_array_type (etype, rtype);
1923 arraytype = build_pointer_type (arraytype);
1924 if (restricted)
1925 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1926 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1928 /* This will generate the base declarations we need to emit debug
1929 information for this type. FIXME: there must be a better way to
1930 avoid divergence between compilations with and without debug
1931 information. */
1933 struct array_descr_info info;
1934 gfc_get_array_descr_info (fat_type, &info);
1935 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1938 return fat_type;
1941 /* Build a pointer type. This function is called from gfc_sym_type(). */
1943 static tree
1944 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1946 /* Array pointer types aren't actually pointers. */
1947 if (sym->attr.dimension)
1948 return type;
1949 else
1950 return build_pointer_type (type);
1953 static tree gfc_nonrestricted_type (tree t);
1954 /* Given two record or union type nodes TO and FROM, ensure
1955 that all fields in FROM have a corresponding field in TO,
1956 their type being nonrestrict variants. This accepts a TO
1957 node that already has a prefix of the fields in FROM. */
1958 static void
1959 mirror_fields (tree to, tree from)
1961 tree fto, ffrom;
1962 tree *chain;
1964 /* Forward to the end of TOs fields. */
1965 fto = TYPE_FIELDS (to);
1966 ffrom = TYPE_FIELDS (from);
1967 chain = &TYPE_FIELDS (to);
1968 while (fto)
1970 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
1971 chain = &DECL_CHAIN (fto);
1972 fto = DECL_CHAIN (fto);
1973 ffrom = DECL_CHAIN (ffrom);
1976 /* Now add all fields remaining in FROM (starting with ffrom). */
1977 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
1979 tree newfield = copy_node (ffrom);
1980 DECL_CONTEXT (newfield) = to;
1981 /* The store to DECL_CHAIN might seem redundant with the
1982 stores to *chain, but not clearing it here would mean
1983 leaving a chain into the old fields. If ever
1984 our called functions would look at them confusion
1985 will arise. */
1986 DECL_CHAIN (newfield) = NULL_TREE;
1987 *chain = newfield;
1988 chain = &DECL_CHAIN (newfield);
1990 if (TREE_CODE (ffrom) == FIELD_DECL)
1992 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
1993 TREE_TYPE (newfield) = elemtype;
1996 *chain = NULL_TREE;
1999 /* Given a type T, returns a different type of the same structure,
2000 except that all types it refers to (recursively) are always
2001 non-restrict qualified types. */
2002 static tree
2003 gfc_nonrestricted_type (tree t)
2005 tree ret = t;
2007 /* If the type isn't laid out yet, don't copy it. If something
2008 needs it for real it should wait until the type got finished. */
2009 if (!TYPE_SIZE (t))
2010 return t;
2012 if (!TYPE_LANG_SPECIFIC (t))
2013 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2014 /* If we're dealing with this very node already further up
2015 the call chain (recursion via pointers and struct members)
2016 we haven't yet determined if we really need a new type node.
2017 Assume we don't, return T itself. */
2018 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2019 return t;
2021 /* If we have calculated this all already, just return it. */
2022 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2023 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2025 /* Mark this type. */
2026 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2028 switch (TREE_CODE (t))
2030 default:
2031 break;
2033 case POINTER_TYPE:
2034 case REFERENCE_TYPE:
2036 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2037 if (totype == TREE_TYPE (t))
2038 ret = t;
2039 else if (TREE_CODE (t) == POINTER_TYPE)
2040 ret = build_pointer_type (totype);
2041 else
2042 ret = build_reference_type (totype);
2043 ret = build_qualified_type (ret,
2044 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2046 break;
2048 case ARRAY_TYPE:
2050 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2051 if (elemtype == TREE_TYPE (t))
2052 ret = t;
2053 else
2055 ret = build_variant_type_copy (t);
2056 TREE_TYPE (ret) = elemtype;
2057 if (TYPE_LANG_SPECIFIC (t)
2058 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2060 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2061 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2062 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2064 TYPE_LANG_SPECIFIC (ret)
2065 = ggc_cleared_alloc<struct lang_type> ();
2066 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2067 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2072 break;
2074 case RECORD_TYPE:
2075 case UNION_TYPE:
2076 case QUAL_UNION_TYPE:
2078 tree field;
2079 /* First determine if we need a new type at all.
2080 Careful, the two calls to gfc_nonrestricted_type per field
2081 might return different values. That happens exactly when
2082 one of the fields reaches back to this very record type
2083 (via pointers). The first calls will assume that we don't
2084 need to copy T (see the error_mark_node marking). If there
2085 are any reasons for copying T apart from having to copy T,
2086 we'll indeed copy it, and the second calls to
2087 gfc_nonrestricted_type will use that new node if they
2088 reach back to T. */
2089 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2090 if (TREE_CODE (field) == FIELD_DECL)
2092 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2093 if (elemtype != TREE_TYPE (field))
2094 break;
2096 if (!field)
2097 break;
2098 ret = build_variant_type_copy (t);
2099 TYPE_FIELDS (ret) = NULL_TREE;
2101 /* Here we make sure that as soon as we know we have to copy
2102 T, that also fields reaching back to us will use the new
2103 copy. It's okay if that copy still contains the old fields,
2104 we won't look at them. */
2105 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2106 mirror_fields (ret, t);
2108 break;
2111 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2112 return ret;
2116 /* Return the type for a symbol. Special handling is required for character
2117 types to get the correct level of indirection.
2118 For functions return the return type.
2119 For subroutines return void_type_node.
2120 Calling this multiple times for the same symbol should be avoided,
2121 especially for character and array types. */
2123 tree
2124 gfc_sym_type (gfc_symbol * sym)
2126 tree type;
2127 int byref;
2128 bool restricted;
2130 /* Procedure Pointers inside COMMON blocks. */
2131 if (sym->attr.proc_pointer && sym->attr.in_common)
2133 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2134 sym->attr.proc_pointer = 0;
2135 type = build_pointer_type (gfc_get_function_type (sym));
2136 sym->attr.proc_pointer = 1;
2137 return type;
2140 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2141 return void_type_node;
2143 /* In the case of a function the fake result variable may have a
2144 type different from the function type, so don't return early in
2145 that case. */
2146 if (sym->backend_decl && !sym->attr.function)
2147 return TREE_TYPE (sym->backend_decl);
2149 if (sym->ts.type == BT_CHARACTER
2150 && ((sym->attr.function && sym->attr.is_bind_c)
2151 || (sym->attr.result
2152 && sym->ns->proc_name
2153 && sym->ns->proc_name->attr.is_bind_c)
2154 || (sym->ts.deferred && (!sym->ts.u.cl
2155 || !sym->ts.u.cl->backend_decl))))
2156 type = gfc_character1_type_node;
2157 else
2158 type = gfc_typenode_for_spec (&sym->ts);
2160 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2161 byref = 1;
2162 else
2163 byref = 0;
2165 restricted = !sym->attr.target && !sym->attr.pointer
2166 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2167 if (!restricted)
2168 type = gfc_nonrestricted_type (type);
2170 if (sym->attr.dimension || sym->attr.codimension)
2172 if (gfc_is_nodesc_array (sym))
2174 /* If this is a character argument of unknown length, just use the
2175 base type. */
2176 if (sym->ts.type != BT_CHARACTER
2177 || !(sym->attr.dummy || sym->attr.function)
2178 || sym->ts.u.cl->backend_decl)
2180 type = gfc_get_nodesc_array_type (type, sym->as,
2181 byref ? PACKED_FULL
2182 : PACKED_STATIC,
2183 restricted);
2184 byref = 0;
2187 else
2189 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2190 if (sym->attr.pointer)
2191 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2192 : GFC_ARRAY_POINTER;
2193 else if (sym->attr.allocatable)
2194 akind = GFC_ARRAY_ALLOCATABLE;
2195 type = gfc_build_array_type (type, sym->as, akind, restricted,
2196 sym->attr.contiguous);
2199 else
2201 if (sym->attr.allocatable || sym->attr.pointer
2202 || gfc_is_associate_pointer (sym))
2203 type = gfc_build_pointer_type (sym, type);
2206 /* We currently pass all parameters by reference.
2207 See f95_get_function_decl. For dummy function parameters return the
2208 function type. */
2209 if (byref)
2211 /* We must use pointer types for potentially absent variables. The
2212 optimizers assume a reference type argument is never NULL. */
2213 if (sym->attr.optional
2214 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2215 type = build_pointer_type (type);
2216 else
2218 type = build_reference_type (type);
2219 if (restricted)
2220 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2224 return (type);
2227 /* Layout and output debug info for a record type. */
2229 void
2230 gfc_finish_type (tree type)
2232 tree decl;
2234 decl = build_decl (input_location,
2235 TYPE_DECL, NULL_TREE, type);
2236 TYPE_STUB_DECL (type) = decl;
2237 layout_type (type);
2238 rest_of_type_compilation (type, 1);
2239 rest_of_decl_compilation (decl, 1, 0);
2242 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2243 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2244 to the end of the field list pointed to by *CHAIN.
2246 Returns a pointer to the new field. */
2248 static tree
2249 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2251 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2253 DECL_CONTEXT (decl) = context;
2254 DECL_CHAIN (decl) = NULL_TREE;
2255 if (TYPE_FIELDS (context) == NULL_TREE)
2256 TYPE_FIELDS (context) = decl;
2257 if (chain != NULL)
2259 if (*chain != NULL)
2260 **chain = decl;
2261 *chain = &DECL_CHAIN (decl);
2264 return decl;
2267 /* Like `gfc_add_field_to_struct_1', but adds alignment
2268 information. */
2270 tree
2271 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2273 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2275 DECL_INITIAL (decl) = 0;
2276 DECL_ALIGN (decl) = 0;
2277 DECL_USER_ALIGN (decl) = 0;
2279 return decl;
2283 /* Copy the backend_decl and component backend_decls if
2284 the two derived type symbols are "equal", as described
2285 in 4.4.2 and resolved by gfc_compare_derived_types. */
2288 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2289 bool from_gsym)
2291 gfc_component *to_cm;
2292 gfc_component *from_cm;
2294 if (from == to)
2295 return 1;
2297 if (from->backend_decl == NULL
2298 || !gfc_compare_derived_types (from, to))
2299 return 0;
2301 to->backend_decl = from->backend_decl;
2303 to_cm = to->components;
2304 from_cm = from->components;
2306 /* Copy the component declarations. If a component is itself
2307 a derived type, we need a copy of its component declarations.
2308 This is done by recursing into gfc_get_derived_type and
2309 ensures that the component's component declarations have
2310 been built. If it is a character, we need the character
2311 length, as well. */
2312 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2314 to_cm->backend_decl = from_cm->backend_decl;
2315 if (from_cm->ts.type == BT_DERIVED
2316 && (!from_cm->attr.pointer || from_gsym))
2317 gfc_get_derived_type (to_cm->ts.u.derived);
2318 else if (from_cm->ts.type == BT_CLASS
2319 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2320 gfc_get_derived_type (to_cm->ts.u.derived);
2321 else if (from_cm->ts.type == BT_CHARACTER)
2322 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2325 return 1;
2329 /* Build a tree node for a procedure pointer component. */
2331 tree
2332 gfc_get_ppc_type (gfc_component* c)
2334 tree t;
2336 /* Explicit interface. */
2337 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2338 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2340 /* Implicit interface (only return value may be known). */
2341 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2342 t = gfc_typenode_for_spec (&c->ts);
2343 else
2344 t = void_type_node;
2346 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2350 /* Build a tree node for a derived type. If there are equal
2351 derived types, with different local names, these are built
2352 at the same time. If an equal derived type has been built
2353 in a parent namespace, this is used. */
2355 tree
2356 gfc_get_derived_type (gfc_symbol * derived)
2358 tree typenode = NULL, field = NULL, field_type = NULL;
2359 tree canonical = NULL_TREE;
2360 tree *chain = NULL;
2361 bool got_canonical = false;
2362 bool unlimited_entity = false;
2363 gfc_component *c;
2364 gfc_dt_list *dt;
2365 gfc_namespace *ns;
2366 tree tmp;
2368 if (derived->attr.unlimited_polymorphic
2369 || (flag_coarray == GFC_FCOARRAY_LIB
2370 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2371 && derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE))
2372 return ptr_type_node;
2374 if (flag_coarray != GFC_FCOARRAY_LIB
2375 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2376 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2377 return gfc_get_int_type (gfc_default_integer_kind);
2379 if (derived && derived->attr.flavor == FL_PROCEDURE
2380 && derived->attr.generic)
2381 derived = gfc_find_dt_in_generic (derived);
2383 /* See if it's one of the iso_c_binding derived types. */
2384 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2386 if (derived->backend_decl)
2387 return derived->backend_decl;
2389 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2390 derived->backend_decl = ptr_type_node;
2391 else
2392 derived->backend_decl = pfunc_type_node;
2394 derived->ts.kind = gfc_index_integer_kind;
2395 derived->ts.type = BT_INTEGER;
2396 /* Set the f90_type to BT_VOID as a way to recognize something of type
2397 BT_INTEGER that needs to fit a void * for the purpose of the
2398 iso_c_binding derived types. */
2399 derived->ts.f90_type = BT_VOID;
2401 return derived->backend_decl;
2404 /* If use associated, use the module type for this one. */
2405 if (derived->backend_decl == NULL
2406 && derived->attr.use_assoc
2407 && derived->module
2408 && gfc_get_module_backend_decl (derived))
2409 goto copy_derived_types;
2411 /* The derived types from an earlier namespace can be used as the
2412 canonical type. */
2413 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2414 && gfc_global_ns_list)
2416 for (ns = gfc_global_ns_list;
2417 ns->translated && !got_canonical;
2418 ns = ns->sibling)
2420 dt = ns->derived_types;
2421 for (; dt && !canonical; dt = dt->next)
2423 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2424 if (derived->backend_decl)
2425 got_canonical = true;
2430 /* Store up the canonical type to be added to this one. */
2431 if (got_canonical)
2433 if (TYPE_CANONICAL (derived->backend_decl))
2434 canonical = TYPE_CANONICAL (derived->backend_decl);
2435 else
2436 canonical = derived->backend_decl;
2438 derived->backend_decl = NULL_TREE;
2441 /* derived->backend_decl != 0 means we saw it before, but its
2442 components' backend_decl may have not been built. */
2443 if (derived->backend_decl)
2445 /* Its components' backend_decl have been built or we are
2446 seeing recursion through the formal arglist of a procedure
2447 pointer component. */
2448 if (TYPE_FIELDS (derived->backend_decl))
2449 return derived->backend_decl;
2450 else if (derived->attr.abstract
2451 && derived->attr.proc_pointer_comp)
2453 /* If an abstract derived type with procedure pointer
2454 components has no other type of component, return the
2455 backend_decl. Otherwise build the components if any of the
2456 non-procedure pointer components have no backend_decl. */
2457 for (c = derived->components; c; c = c->next)
2459 if (!c->attr.proc_pointer && c->backend_decl == NULL)
2460 break;
2461 else if (c->next == NULL)
2462 return derived->backend_decl;
2464 typenode = derived->backend_decl;
2466 else
2467 typenode = derived->backend_decl;
2469 else
2471 /* We see this derived type first time, so build the type node. */
2472 typenode = make_node (RECORD_TYPE);
2473 TYPE_NAME (typenode) = get_identifier (derived->name);
2474 TYPE_PACKED (typenode) = flag_pack_derived;
2475 derived->backend_decl = typenode;
2478 if (derived->components
2479 && derived->components->ts.type == BT_DERIVED
2480 && strcmp (derived->components->name, "_data") == 0
2481 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2482 unlimited_entity = true;
2484 /* Go through the derived type components, building them as
2485 necessary. The reason for doing this now is that it is
2486 possible to recurse back to this derived type through a
2487 pointer component (PR24092). If this happens, the fields
2488 will be built and so we can return the type. */
2489 for (c = derived->components; c; c = c->next)
2491 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2492 continue;
2494 if ((!c->attr.pointer && !c->attr.proc_pointer)
2495 || c->ts.u.derived->backend_decl == NULL)
2496 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
2498 if (c->ts.u.derived->attr.is_iso_c)
2500 /* Need to copy the modified ts from the derived type. The
2501 typespec was modified because C_PTR/C_FUNPTR are translated
2502 into (void *) from derived types. */
2503 c->ts.type = c->ts.u.derived->ts.type;
2504 c->ts.kind = c->ts.u.derived->ts.kind;
2505 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2506 if (c->initializer)
2508 c->initializer->ts.type = c->ts.type;
2509 c->initializer->ts.kind = c->ts.kind;
2510 c->initializer->ts.f90_type = c->ts.f90_type;
2511 c->initializer->expr_type = EXPR_NULL;
2516 if (TYPE_FIELDS (derived->backend_decl))
2517 return derived->backend_decl;
2519 /* Build the type member list. Install the newly created RECORD_TYPE
2520 node as DECL_CONTEXT of each FIELD_DECL. */
2521 for (c = derived->components; c; c = c->next)
2523 /* Prevent infinite recursion, when the procedure pointer type is
2524 the same as derived, by forcing the procedure pointer component to
2525 be built as if the explicit interface does not exist. */
2526 if (c->attr.proc_pointer
2527 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2528 || (c->ts.u.derived
2529 && !gfc_compare_derived_types (derived, c->ts.u.derived))))
2530 field_type = gfc_get_ppc_type (c);
2531 else if (c->attr.proc_pointer && derived->backend_decl)
2533 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2534 field_type = build_pointer_type (tmp);
2536 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2537 field_type = c->ts.u.derived->backend_decl;
2538 else
2540 if (c->ts.type == BT_CHARACTER && !c->ts.deferred)
2542 /* Evaluate the string length. */
2543 gfc_conv_const_charlen (c->ts.u.cl);
2544 gcc_assert (c->ts.u.cl->backend_decl);
2546 else if (c->ts.type == BT_CHARACTER)
2547 c->ts.u.cl->backend_decl
2548 = build_int_cst (gfc_charlen_type_node, 0);
2550 field_type = gfc_typenode_for_spec (&c->ts);
2553 /* This returns an array descriptor type. Initialization may be
2554 required. */
2555 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2557 if (c->attr.pointer || c->attr.allocatable)
2559 enum gfc_array_kind akind;
2560 if (c->attr.pointer)
2561 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2562 : GFC_ARRAY_POINTER;
2563 else
2564 akind = GFC_ARRAY_ALLOCATABLE;
2565 /* Pointers to arrays aren't actually pointer types. The
2566 descriptors are separate, but the data is common. */
2567 field_type = gfc_build_array_type (field_type, c->as, akind,
2568 !c->attr.target
2569 && !c->attr.pointer,
2570 c->attr.contiguous);
2572 else
2573 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2574 PACKED_STATIC,
2575 !c->attr.target);
2577 else if ((c->attr.pointer || c->attr.allocatable)
2578 && !c->attr.proc_pointer
2579 && !(unlimited_entity && c == derived->components))
2580 field_type = build_pointer_type (field_type);
2582 if (c->attr.pointer)
2583 field_type = gfc_nonrestricted_type (field_type);
2585 /* vtype fields can point to different types to the base type. */
2586 if (c->ts.type == BT_DERIVED
2587 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2588 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2589 ptr_mode, true);
2591 /* Ensure that the CLASS language specific flag is set. */
2592 if (c->ts.type == BT_CLASS)
2594 if (POINTER_TYPE_P (field_type))
2595 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2596 else
2597 GFC_CLASS_TYPE_P (field_type) = 1;
2600 field = gfc_add_field_to_struct (typenode,
2601 get_identifier (c->name),
2602 field_type, &chain);
2603 if (c->loc.lb)
2604 gfc_set_decl_location (field, &c->loc);
2605 else if (derived->declared_at.lb)
2606 gfc_set_decl_location (field, &derived->declared_at);
2608 gfc_finish_decl_attrs (field, &c->attr);
2610 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2612 gcc_assert (field);
2613 if (!c->backend_decl)
2614 c->backend_decl = field;
2617 /* Now lay out the derived type, including the fields. */
2618 if (canonical)
2619 TYPE_CANONICAL (typenode) = canonical;
2621 gfc_finish_type (typenode);
2622 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2623 if (derived->module && derived->ns->proc_name
2624 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2626 if (derived->ns->proc_name->backend_decl
2627 && TREE_CODE (derived->ns->proc_name->backend_decl)
2628 == NAMESPACE_DECL)
2630 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2631 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2632 = derived->ns->proc_name->backend_decl;
2636 derived->backend_decl = typenode;
2638 copy_derived_types:
2640 for (dt = gfc_derived_types; dt; dt = dt->next)
2641 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2643 return derived->backend_decl;
2648 gfc_return_by_reference (gfc_symbol * sym)
2650 if (!sym->attr.function)
2651 return 0;
2653 if (sym->attr.dimension)
2654 return 1;
2656 if (sym->ts.type == BT_CHARACTER
2657 && !sym->attr.is_bind_c
2658 && (!sym->attr.result
2659 || !sym->ns->proc_name
2660 || !sym->ns->proc_name->attr.is_bind_c))
2661 return 1;
2663 /* Possibly return complex numbers by reference for g77 compatibility.
2664 We don't do this for calls to intrinsics (as the library uses the
2665 -fno-f2c calling convention), nor for calls to functions which always
2666 require an explicit interface, as no compatibility problems can
2667 arise there. */
2668 if (flag_f2c && sym->ts.type == BT_COMPLEX
2669 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2670 return 1;
2672 return 0;
2675 static tree
2676 gfc_get_mixed_entry_union (gfc_namespace *ns)
2678 tree type;
2679 tree *chain = NULL;
2680 char name[GFC_MAX_SYMBOL_LEN + 1];
2681 gfc_entry_list *el, *el2;
2683 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2684 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2686 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2688 /* Build the type node. */
2689 type = make_node (UNION_TYPE);
2691 TYPE_NAME (type) = get_identifier (name);
2693 for (el = ns->entries; el; el = el->next)
2695 /* Search for duplicates. */
2696 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2697 if (el2->sym->result == el->sym->result)
2698 break;
2700 if (el == el2)
2701 gfc_add_field_to_struct_1 (type,
2702 get_identifier (el->sym->result->name),
2703 gfc_sym_type (el->sym->result), &chain);
2706 /* Finish off the type. */
2707 gfc_finish_type (type);
2708 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2709 return type;
2712 /* Create a "fn spec" based on the formal arguments;
2713 cf. create_function_arglist. */
2715 static tree
2716 create_fn_spec (gfc_symbol *sym, tree fntype)
2718 char spec[150];
2719 size_t spec_len;
2720 gfc_formal_arglist *f;
2721 tree tmp;
2723 memset (&spec, 0, sizeof (spec));
2724 spec[0] = '.';
2725 spec_len = 1;
2727 if (sym->attr.entry_master)
2728 spec[spec_len++] = 'R';
2729 if (gfc_return_by_reference (sym))
2731 gfc_symbol *result = sym->result ? sym->result : sym;
2733 if (result->attr.pointer || sym->attr.proc_pointer)
2734 spec[spec_len++] = '.';
2735 else
2736 spec[spec_len++] = 'w';
2737 if (sym->ts.type == BT_CHARACTER)
2738 spec[spec_len++] = 'R';
2741 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2742 if (spec_len < sizeof (spec))
2744 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2745 || f->sym->attr.external || f->sym->attr.cray_pointer
2746 || (f->sym->ts.type == BT_DERIVED
2747 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2748 || f->sym->ts.u.derived->attr.pointer_comp))
2749 || (f->sym->ts.type == BT_CLASS
2750 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2751 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2752 spec[spec_len++] = '.';
2753 else if (f->sym->attr.intent == INTENT_IN)
2754 spec[spec_len++] = 'r';
2755 else if (f->sym)
2756 spec[spec_len++] = 'w';
2759 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2760 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2761 return build_type_attribute_variant (fntype, tmp);
2765 tree
2766 gfc_get_function_type (gfc_symbol * sym)
2768 tree type;
2769 vec<tree, va_gc> *typelist = NULL;
2770 gfc_formal_arglist *f;
2771 gfc_symbol *arg;
2772 int alternate_return = 0;
2773 bool is_varargs = true;
2775 /* Make sure this symbol is a function, a subroutine or the main
2776 program. */
2777 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2778 || sym->attr.flavor == FL_PROGRAM);
2780 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2781 so that they can be detected here and handled further down. */
2782 if (sym->backend_decl == NULL)
2783 sym->backend_decl = error_mark_node;
2784 else if (sym->backend_decl == error_mark_node)
2785 goto arg_type_list_done;
2786 else if (sym->attr.proc_pointer)
2787 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2788 else
2789 return TREE_TYPE (sym->backend_decl);
2791 if (sym->attr.entry_master)
2792 /* Additional parameter for selecting an entry point. */
2793 vec_safe_push (typelist, gfc_array_index_type);
2795 if (sym->result)
2796 arg = sym->result;
2797 else
2798 arg = sym;
2800 if (arg->ts.type == BT_CHARACTER)
2801 gfc_conv_const_charlen (arg->ts.u.cl);
2803 /* Some functions we use an extra parameter for the return value. */
2804 if (gfc_return_by_reference (sym))
2806 type = gfc_sym_type (arg);
2807 if (arg->ts.type == BT_COMPLEX
2808 || arg->attr.dimension
2809 || arg->ts.type == BT_CHARACTER)
2810 type = build_reference_type (type);
2812 vec_safe_push (typelist, type);
2813 if (arg->ts.type == BT_CHARACTER)
2815 if (!arg->ts.deferred)
2816 /* Transfer by value. */
2817 vec_safe_push (typelist, gfc_charlen_type_node);
2818 else
2819 /* Deferred character lengths are transferred by reference
2820 so that the value can be returned. */
2821 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2825 /* Build the argument types for the function. */
2826 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2828 arg = f->sym;
2829 if (arg)
2831 /* Evaluate constant character lengths here so that they can be
2832 included in the type. */
2833 if (arg->ts.type == BT_CHARACTER)
2834 gfc_conv_const_charlen (arg->ts.u.cl);
2836 if (arg->attr.flavor == FL_PROCEDURE)
2838 type = gfc_get_function_type (arg);
2839 type = build_pointer_type (type);
2841 else
2842 type = gfc_sym_type (arg);
2844 /* Parameter Passing Convention
2846 We currently pass all parameters by reference.
2847 Parameters with INTENT(IN) could be passed by value.
2848 The problem arises if a function is called via an implicit
2849 prototype. In this situation the INTENT is not known.
2850 For this reason all parameters to global functions must be
2851 passed by reference. Passing by value would potentially
2852 generate bad code. Worse there would be no way of telling that
2853 this code was bad, except that it would give incorrect results.
2855 Contained procedures could pass by value as these are never
2856 used without an explicit interface, and cannot be passed as
2857 actual parameters for a dummy procedure. */
2859 vec_safe_push (typelist, type);
2861 else
2863 if (sym->attr.subroutine)
2864 alternate_return = 1;
2868 /* Add hidden string length parameters. */
2869 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2871 arg = f->sym;
2872 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2874 if (!arg->ts.deferred)
2875 /* Transfer by value. */
2876 type = gfc_charlen_type_node;
2877 else
2878 /* Deferred character lengths are transferred by reference
2879 so that the value can be returned. */
2880 type = build_pointer_type (gfc_charlen_type_node);
2882 vec_safe_push (typelist, type);
2886 if (!vec_safe_is_empty (typelist)
2887 || sym->attr.is_main_program
2888 || sym->attr.if_source != IFSRC_UNKNOWN)
2889 is_varargs = false;
2891 if (sym->backend_decl == error_mark_node)
2892 sym->backend_decl = NULL_TREE;
2894 arg_type_list_done:
2896 if (alternate_return)
2897 type = integer_type_node;
2898 else if (!sym->attr.function || gfc_return_by_reference (sym))
2899 type = void_type_node;
2900 else if (sym->attr.mixed_entry_master)
2901 type = gfc_get_mixed_entry_union (sym->ns);
2902 else if (flag_f2c && sym->ts.type == BT_REAL
2903 && sym->ts.kind == gfc_default_real_kind
2904 && !sym->attr.always_explicit)
2906 /* Special case: f2c calling conventions require that (scalar)
2907 default REAL functions return the C type double instead. f2c
2908 compatibility is only an issue with functions that don't
2909 require an explicit interface, as only these could be
2910 implemented in Fortran 77. */
2911 sym->ts.kind = gfc_default_double_kind;
2912 type = gfc_typenode_for_spec (&sym->ts);
2913 sym->ts.kind = gfc_default_real_kind;
2915 else if (sym->result && sym->result->attr.proc_pointer)
2916 /* Procedure pointer return values. */
2918 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
2920 /* Unset proc_pointer as gfc_get_function_type
2921 is called recursively. */
2922 sym->result->attr.proc_pointer = 0;
2923 type = build_pointer_type (gfc_get_function_type (sym->result));
2924 sym->result->attr.proc_pointer = 1;
2926 else
2927 type = gfc_sym_type (sym->result);
2929 else
2930 type = gfc_sym_type (sym);
2932 if (is_varargs)
2933 type = build_varargs_function_type_vec (type, typelist);
2934 else
2935 type = build_function_type_vec (type, typelist);
2936 type = create_fn_spec (sym, type);
2938 return type;
2941 /* Language hooks for middle-end access to type nodes. */
2943 /* Return an integer type with BITS bits of precision,
2944 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2946 tree
2947 gfc_type_for_size (unsigned bits, int unsignedp)
2949 if (!unsignedp)
2951 int i;
2952 for (i = 0; i <= MAX_INT_KINDS; ++i)
2954 tree type = gfc_integer_types[i];
2955 if (type && bits == TYPE_PRECISION (type))
2956 return type;
2959 /* Handle TImode as a special case because it is used by some backends
2960 (e.g. ARM) even though it is not available for normal use. */
2961 #if HOST_BITS_PER_WIDE_INT >= 64
2962 if (bits == TYPE_PRECISION (intTI_type_node))
2963 return intTI_type_node;
2964 #endif
2966 if (bits <= TYPE_PRECISION (intQI_type_node))
2967 return intQI_type_node;
2968 if (bits <= TYPE_PRECISION (intHI_type_node))
2969 return intHI_type_node;
2970 if (bits <= TYPE_PRECISION (intSI_type_node))
2971 return intSI_type_node;
2972 if (bits <= TYPE_PRECISION (intDI_type_node))
2973 return intDI_type_node;
2974 if (bits <= TYPE_PRECISION (intTI_type_node))
2975 return intTI_type_node;
2977 else
2979 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
2980 return unsigned_intQI_type_node;
2981 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
2982 return unsigned_intHI_type_node;
2983 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
2984 return unsigned_intSI_type_node;
2985 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
2986 return unsigned_intDI_type_node;
2987 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
2988 return unsigned_intTI_type_node;
2991 return NULL_TREE;
2994 /* Return a data type that has machine mode MODE. If the mode is an
2995 integer, then UNSIGNEDP selects between signed and unsigned types. */
2997 tree
2998 gfc_type_for_mode (machine_mode mode, int unsignedp)
3000 int i;
3001 tree *base;
3003 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3004 base = gfc_real_types;
3005 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3006 base = gfc_complex_types;
3007 else if (SCALAR_INT_MODE_P (mode))
3009 tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
3010 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3012 else if (VECTOR_MODE_P (mode))
3014 machine_mode inner_mode = GET_MODE_INNER (mode);
3015 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3016 if (inner_type != NULL_TREE)
3017 return build_vector_type_for_mode (inner_type, mode);
3018 return NULL_TREE;
3020 else
3021 return NULL_TREE;
3023 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3025 tree type = base[i];
3026 if (type && mode == TYPE_MODE (type))
3027 return type;
3030 return NULL_TREE;
3033 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3034 in that case. */
3036 bool
3037 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3039 int rank, dim;
3040 bool indirect = false;
3041 tree etype, ptype, field, t, base_decl;
3042 tree data_off, dim_off, dim_size, elem_size;
3043 tree lower_suboff, upper_suboff, stride_suboff;
3045 if (! GFC_DESCRIPTOR_TYPE_P (type))
3047 if (! POINTER_TYPE_P (type))
3048 return false;
3049 type = TREE_TYPE (type);
3050 if (! GFC_DESCRIPTOR_TYPE_P (type))
3051 return false;
3052 indirect = true;
3055 rank = GFC_TYPE_ARRAY_RANK (type);
3056 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3057 return false;
3059 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3060 gcc_assert (POINTER_TYPE_P (etype));
3061 etype = TREE_TYPE (etype);
3063 /* If the type is not a scalar coarray. */
3064 if (TREE_CODE (etype) == ARRAY_TYPE)
3065 etype = TREE_TYPE (etype);
3067 /* Can't handle variable sized elements yet. */
3068 if (int_size_in_bytes (etype) <= 0)
3069 return false;
3070 /* Nor non-constant lower bounds in assumed shape arrays. */
3071 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3072 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3074 for (dim = 0; dim < rank; dim++)
3075 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3076 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3077 return false;
3080 memset (info, '\0', sizeof (*info));
3081 info->ndimensions = rank;
3082 info->ordering = array_descr_ordering_column_major;
3083 info->element_type = etype;
3084 ptype = build_pointer_type (gfc_array_index_type);
3085 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3086 if (!base_decl)
3088 base_decl = make_node (DEBUG_EXPR_DECL);
3089 DECL_ARTIFICIAL (base_decl) = 1;
3090 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3091 DECL_MODE (base_decl) = TYPE_MODE (TREE_TYPE (base_decl));
3092 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3094 info->base_decl = base_decl;
3095 if (indirect)
3096 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3098 if (GFC_TYPE_ARRAY_SPAN (type))
3099 elem_size = GFC_TYPE_ARRAY_SPAN (type);
3100 else
3101 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3102 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
3103 data_off = byte_position (field);
3104 field = DECL_CHAIN (field);
3105 field = DECL_CHAIN (field);
3106 field = DECL_CHAIN (field);
3107 dim_off = byte_position (field);
3108 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
3109 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
3110 stride_suboff = byte_position (field);
3111 field = DECL_CHAIN (field);
3112 lower_suboff = byte_position (field);
3113 field = DECL_CHAIN (field);
3114 upper_suboff = byte_position (field);
3116 t = base_decl;
3117 if (!integer_zerop (data_off))
3118 t = fold_build_pointer_plus (t, data_off);
3119 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3120 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3121 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3122 info->allocated = build2 (NE_EXPR, boolean_type_node,
3123 info->data_location, null_pointer_node);
3124 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3125 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3126 info->associated = build2 (NE_EXPR, boolean_type_node,
3127 info->data_location, null_pointer_node);
3129 for (dim = 0; dim < rank; dim++)
3131 t = fold_build_pointer_plus (base_decl,
3132 size_binop (PLUS_EXPR,
3133 dim_off, lower_suboff));
3134 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3135 info->dimen[dim].lower_bound = t;
3136 t = fold_build_pointer_plus (base_decl,
3137 size_binop (PLUS_EXPR,
3138 dim_off, upper_suboff));
3139 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3140 info->dimen[dim].upper_bound = t;
3141 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3142 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3144 /* Assumed shape arrays have known lower bounds. */
3145 info->dimen[dim].upper_bound
3146 = build2 (MINUS_EXPR, gfc_array_index_type,
3147 info->dimen[dim].upper_bound,
3148 info->dimen[dim].lower_bound);
3149 info->dimen[dim].lower_bound
3150 = fold_convert (gfc_array_index_type,
3151 GFC_TYPE_ARRAY_LBOUND (type, dim));
3152 info->dimen[dim].upper_bound
3153 = build2 (PLUS_EXPR, gfc_array_index_type,
3154 info->dimen[dim].lower_bound,
3155 info->dimen[dim].upper_bound);
3157 t = fold_build_pointer_plus (base_decl,
3158 size_binop (PLUS_EXPR,
3159 dim_off, stride_suboff));
3160 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3161 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3162 info->dimen[dim].stride = t;
3163 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3166 return true;
3170 /* Create a type to handle vector subscripts for coarray library calls. It
3171 has the form:
3172 struct caf_vector_t {
3173 size_t nvec; // size of the vector
3174 union {
3175 struct {
3176 void *vector;
3177 int kind;
3178 } v;
3179 struct {
3180 ptrdiff_t lower_bound;
3181 ptrdiff_t upper_bound;
3182 ptrdiff_t stride;
3183 } triplet;
3184 } u;
3186 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3187 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3189 tree
3190 gfc_get_caf_vector_type (int dim)
3192 static tree vector_types[GFC_MAX_DIMENSIONS];
3193 static tree vec_type = NULL_TREE;
3194 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3196 if (vector_types[dim-1] != NULL_TREE)
3197 return vector_types[dim-1];
3199 if (vec_type == NULL_TREE)
3201 chain = 0;
3202 vect_struct_type = make_node (RECORD_TYPE);
3203 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3204 get_identifier ("vector"),
3205 pvoid_type_node, &chain);
3206 TREE_NO_WARNING (tmp) = 1;
3207 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3208 get_identifier ("kind"),
3209 integer_type_node, &chain);
3210 TREE_NO_WARNING (tmp) = 1;
3211 gfc_finish_type (vect_struct_type);
3213 chain = 0;
3214 triplet_struct_type = make_node (RECORD_TYPE);
3215 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3216 get_identifier ("lower_bound"),
3217 gfc_array_index_type, &chain);
3218 TREE_NO_WARNING (tmp) = 1;
3219 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3220 get_identifier ("upper_bound"),
3221 gfc_array_index_type, &chain);
3222 TREE_NO_WARNING (tmp) = 1;
3223 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3224 gfc_array_index_type, &chain);
3225 TREE_NO_WARNING (tmp) = 1;
3226 gfc_finish_type (triplet_struct_type);
3228 chain = 0;
3229 union_type = make_node (UNION_TYPE);
3230 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3231 vect_struct_type, &chain);
3232 TREE_NO_WARNING (tmp) = 1;
3233 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3234 triplet_struct_type, &chain);
3235 TREE_NO_WARNING (tmp) = 1;
3236 gfc_finish_type (union_type);
3238 chain = 0;
3239 vec_type = make_node (RECORD_TYPE);
3240 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3241 size_type_node, &chain);
3242 TREE_NO_WARNING (tmp) = 1;
3243 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3244 union_type, &chain);
3245 TREE_NO_WARNING (tmp) = 1;
3246 gfc_finish_type (vec_type);
3247 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3250 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3251 gfc_rank_cst[dim-1]);
3252 vector_types[dim-1] = build_array_type (vec_type, tmp);
3253 return vector_types[dim-1];
3256 #include "gt-fortran-trans-types.h"