1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 Free Software Foundation, Inc.
5 Contributed by Paul Brook <paul@nowt.org>
6 and Steven Bosscher <s.bosscher@student.tudelft.nl>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* trans-types.c -- gfortran backend types */
28 #include "coretypes.h"
30 #include "langhooks.h" /* For iso-c-bindings.def. */
33 #include "diagnostic-core.h" /* For fatal_error. */
34 #include "toplev.h" /* For rest_of_decl_compilation. */
37 #include "trans-types.h"
38 #include "trans-const.h"
40 #include "dwarf2out.h" /* For struct array_descr_info. */
43 #if (GFC_MAX_DIMENSIONS < 10)
44 #define GFC_RANK_DIGITS 1
45 #define GFC_RANK_PRINTF_FORMAT "%01d"
46 #elif (GFC_MAX_DIMENSIONS < 100)
47 #define GFC_RANK_DIGITS 2
48 #define GFC_RANK_PRINTF_FORMAT "%02d"
50 #error If you really need >99 dimensions, continue the sequence above...
53 /* array of structs so we don't have to worry about xmalloc or free */
54 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
56 tree gfc_array_index_type;
57 tree gfc_array_range_type;
58 tree gfc_character1_type_node;
60 tree prvoid_type_node;
61 tree ppvoid_type_node;
65 tree gfc_charlen_type_node;
67 tree float128_type_node = NULL_TREE;
68 tree complex_float128_type_node = NULL_TREE;
70 bool gfc_real16_is_float128 = false;
72 static GTY(()) tree gfc_desc_dim_type;
73 static GTY(()) tree gfc_max_array_element_size;
74 static GTY(()) tree gfc_array_descriptor_base[2 * GFC_MAX_DIMENSIONS];
76 /* Arrays for all integral and real kinds. We'll fill this in at runtime
77 after the target has a chance to process command-line options. */
79 #define MAX_INT_KINDS 5
80 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
81 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
82 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
83 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
85 #define MAX_REAL_KINDS 5
86 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
87 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
88 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
90 #define MAX_CHARACTER_KINDS 2
91 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
92 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
93 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
95 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
97 /* The integer kind to use for array indices. This will be set to the
98 proper value based on target information from the backend. */
100 int gfc_index_integer_kind;
102 /* The default kinds of the various types. */
104 int gfc_default_integer_kind;
105 int gfc_max_integer_kind;
106 int gfc_default_real_kind;
107 int gfc_default_double_kind;
108 int gfc_default_character_kind;
109 int gfc_default_logical_kind;
110 int gfc_default_complex_kind;
113 /* The kind size used for record offsets. If the target system supports
114 kind=8, this will be set to 8, otherwise it is set to 4. */
117 /* The integer kind used to store character lengths. */
118 int gfc_charlen_int_kind;
120 /* The size of the numeric storage unit and character storage unit. */
121 int gfc_numeric_storage_size;
122 int gfc_character_storage_size;
126 gfc_check_any_c_kind (gfc_typespec *ts)
130 for (i = 0; i < ISOCBINDING_NUMBER; i++)
132 /* Check for any C interoperable kind for the given type/kind in ts.
133 This can be used after verify_c_interop to make sure that the
134 Fortran kind being used exists in at least some form for C. */
135 if (c_interop_kinds_table[i].f90_type == ts->type &&
136 c_interop_kinds_table[i].value == ts->kind)
145 get_real_kind_from_node (tree type)
149 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
150 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
151 return gfc_real_kinds[i].kind;
157 get_int_kind_from_node (tree type)
164 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
165 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
166 return gfc_integer_kinds[i].kind;
171 /* Return a typenode for the "standard" C type with a given name. */
173 get_typenode_from_name (const char *name)
175 if (name == NULL || *name == '\0')
178 if (strcmp (name, "char") == 0)
179 return char_type_node;
180 if (strcmp (name, "unsigned char") == 0)
181 return unsigned_char_type_node;
182 if (strcmp (name, "signed char") == 0)
183 return signed_char_type_node;
185 if (strcmp (name, "short int") == 0)
186 return short_integer_type_node;
187 if (strcmp (name, "short unsigned int") == 0)
188 return short_unsigned_type_node;
190 if (strcmp (name, "int") == 0)
191 return integer_type_node;
192 if (strcmp (name, "unsigned int") == 0)
193 return unsigned_type_node;
195 if (strcmp (name, "long int") == 0)
196 return long_integer_type_node;
197 if (strcmp (name, "long unsigned int") == 0)
198 return long_unsigned_type_node;
200 if (strcmp (name, "long long int") == 0)
201 return long_long_integer_type_node;
202 if (strcmp (name, "long long unsigned int") == 0)
203 return long_long_unsigned_type_node;
209 get_int_kind_from_name (const char *name)
211 return get_int_kind_from_node (get_typenode_from_name (name));
215 /* Get the kind number corresponding to an integer of given size,
216 following the required return values for ISO_FORTRAN_ENV INT* constants:
217 -2 is returned if we support a kind of larger size, -1 otherwise. */
219 gfc_get_int_kind_from_width_isofortranenv (int size)
223 /* Look for a kind with matching storage size. */
224 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
225 if (gfc_integer_kinds[i].bit_size == size)
226 return gfc_integer_kinds[i].kind;
228 /* Look for a kind with larger storage size. */
229 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
230 if (gfc_integer_kinds[i].bit_size > size)
236 /* Get the kind number corresponding to a real of given storage size,
237 following the required return values for ISO_FORTRAN_ENV REAL* constants:
238 -2 is returned if we support a kind of larger size, -1 otherwise. */
240 gfc_get_real_kind_from_width_isofortranenv (int size)
246 /* Look for a kind with matching storage size. */
247 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
248 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
249 return gfc_real_kinds[i].kind;
251 /* Look for a kind with larger storage size. */
252 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
253 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
262 get_int_kind_from_width (int size)
266 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
267 if (gfc_integer_kinds[i].bit_size == size)
268 return gfc_integer_kinds[i].kind;
274 get_int_kind_from_minimal_width (int size)
278 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
279 if (gfc_integer_kinds[i].bit_size >= size)
280 return gfc_integer_kinds[i].kind;
286 /* Generate the CInteropKind_t objects for the C interoperable
290 void init_c_interop_kinds (void)
294 /* init all pointers in the list to NULL */
295 for (i = 0; i < ISOCBINDING_NUMBER; i++)
297 /* Initialize the name and value fields. */
298 c_interop_kinds_table[i].name[0] = '\0';
299 c_interop_kinds_table[i].value = -100;
300 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
303 #define NAMED_INTCST(a,b,c,d) \
304 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
305 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
306 c_interop_kinds_table[a].value = c;
307 #define NAMED_REALCST(a,b,c) \
308 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
309 c_interop_kinds_table[a].f90_type = BT_REAL; \
310 c_interop_kinds_table[a].value = c;
311 #define NAMED_CMPXCST(a,b,c) \
312 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
313 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
314 c_interop_kinds_table[a].value = c;
315 #define NAMED_LOGCST(a,b,c) \
316 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
317 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
318 c_interop_kinds_table[a].value = c;
319 #define NAMED_CHARKNDCST(a,b,c) \
320 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
321 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
322 c_interop_kinds_table[a].value = c;
323 #define NAMED_CHARCST(a,b,c) \
324 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
325 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
326 c_interop_kinds_table[a].value = c;
327 #define DERIVED_TYPE(a,b,c) \
328 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
329 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
330 c_interop_kinds_table[a].value = c;
331 #define PROCEDURE(a,b) \
332 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
333 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
334 c_interop_kinds_table[a].value = 0;
335 #include "iso-c-binding.def"
336 #define NAMED_FUNCTION(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. */
348 gfc_init_kinds (void)
351 int i_index, r_index, kind;
352 bool saw_i4 = false, saw_i8 = false;
353 bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
355 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
359 if (!targetm.scalar_mode_supported_p ((enum machine_mode) mode))
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 (mode);
366 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
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. */
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;
391 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
392 used for large file access. */
399 /* If we do not at least have kind = 4, everything is pointless. */
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 ((enum machine_mode) mode);
413 if (!targetm.scalar_mode_supported_p ((enum machine_mode) mode))
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. TODO: TFmode support should be enabled once libgfortran
420 if (mode != TYPE_MODE (float_type_node)
421 && (mode != TYPE_MODE (double_type_node))
422 && (mode != TYPE_MODE (long_double_type_node))
423 #ifdef LIBGCC2_HAS_TF_MODE
429 /* Let the kind equal the precision divided by 8, rounding up. Again,
430 this insulates the programmer from the underlying byte size.
432 Also, it effectively deals with IEEE extended formats. There, the
433 total size of the type may equal 16, but it's got 6 bytes of padding
434 and the increased size can get in the way of a real IEEE quad format
435 which may also be supported by the target.
437 We round up so as to handle IA-64 __floatreg (RFmode), which is an
438 82 bit type. Not to be confused with __float80 (XFmode), which is
439 an 80 bit type also supported by IA-64. So XFmode should come out
440 to be kind=10, and RFmode should come out to be kind=11. Egads. */
442 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
451 /* Careful we don't stumble a weird internal mode. */
452 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
453 /* Or have too many modes for the allocated space. */
454 gcc_assert (r_index != MAX_REAL_KINDS);
456 gfc_real_kinds[r_index].kind = kind;
457 gfc_real_kinds[r_index].radix = fmt->b;
458 gfc_real_kinds[r_index].digits = fmt->p;
459 gfc_real_kinds[r_index].min_exponent = fmt->emin;
460 gfc_real_kinds[r_index].max_exponent = fmt->emax;
461 if (fmt->pnan < fmt->p)
462 /* This is an IBM extended double format (or the MIPS variant)
463 made up of two IEEE doubles. The value of the long double is
464 the sum of the values of the two parts. The most significant
465 part is required to be the value of the long double rounded
466 to the nearest double. If we use emax of 1024 then we can't
467 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
468 rounding will make the most significant part overflow. */
469 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
470 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
474 /* Choose the default integer kind. We choose 4 unless the user
475 directs us otherwise. */
476 if (gfc_option.flag_default_integer)
479 fatal_error ("integer kind=8 not available for -fdefault-integer-8 option");
480 gfc_default_integer_kind = 8;
482 /* Even if the user specified that the default integer kind be 8,
483 the numeric storage size isn't 64. In this case, a warning will
484 be issued when NUMERIC_STORAGE_SIZE is used. */
485 gfc_numeric_storage_size = 4 * 8;
489 gfc_default_integer_kind = 4;
490 gfc_numeric_storage_size = 4 * 8;
494 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
495 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
498 /* Choose the default real kind. Again, we choose 4 when possible. */
499 if (gfc_option.flag_default_real)
502 fatal_error ("real kind=8 not available for -fdefault-real-8 option");
503 gfc_default_real_kind = 8;
506 gfc_default_real_kind = 4;
508 gfc_default_real_kind = gfc_real_kinds[0].kind;
510 /* Choose the default double kind. If -fdefault-real and -fdefault-double
511 are specified, we use kind=8, if it's available. If -fdefault-real is
512 specified without -fdefault-double, we use kind=16, if it's available.
513 Otherwise we do not change anything. */
514 if (gfc_option.flag_default_double && !gfc_option.flag_default_real)
515 fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8");
517 if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8)
518 gfc_default_double_kind = 8;
519 else if (gfc_option.flag_default_real && saw_r16)
520 gfc_default_double_kind = 16;
521 else if (saw_r4 && saw_r8)
522 gfc_default_double_kind = 8;
525 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
526 real ... occupies two contiguous numeric storage units.
528 Therefore we must be supplied a kind twice as large as we chose
529 for single precision. There are loopholes, in that double
530 precision must *occupy* two storage units, though it doesn't have
531 to *use* two storage units. Which means that you can make this
532 kind artificially wide by padding it. But at present there are
533 no GCC targets for which a two-word type does not exist, so we
534 just let gfc_validate_kind abort and tell us if something breaks. */
536 gfc_default_double_kind
537 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
540 /* The default logical kind is constrained to be the same as the
541 default integer kind. Similarly with complex and real. */
542 gfc_default_logical_kind = gfc_default_integer_kind;
543 gfc_default_complex_kind = gfc_default_real_kind;
545 /* We only have two character kinds: ASCII and UCS-4.
546 ASCII corresponds to a 8-bit integer type, if one is available.
547 UCS-4 corresponds to a 32-bit integer type, if one is available. */
549 if ((kind = get_int_kind_from_width (8)) > 0)
551 gfc_character_kinds[i_index].kind = kind;
552 gfc_character_kinds[i_index].bit_size = 8;
553 gfc_character_kinds[i_index].name = "ascii";
556 if ((kind = get_int_kind_from_width (32)) > 0)
558 gfc_character_kinds[i_index].kind = kind;
559 gfc_character_kinds[i_index].bit_size = 32;
560 gfc_character_kinds[i_index].name = "iso_10646";
564 /* Choose the smallest integer kind for our default character. */
565 gfc_default_character_kind = gfc_character_kinds[0].kind;
566 gfc_character_storage_size = gfc_default_character_kind * 8;
568 /* Choose the integer kind the same size as "void*" for our index kind. */
569 gfc_index_integer_kind = POINTER_SIZE / 8;
570 /* Pick a kind the same size as the C "int" type. */
571 gfc_c_int_kind = INT_TYPE_SIZE / 8;
573 /* initialize the C interoperable kinds */
574 init_c_interop_kinds();
577 /* Make sure that a valid kind is present. Returns an index into the
578 associated kinds array, -1 if the kind is not present. */
581 validate_integer (int kind)
585 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
586 if (gfc_integer_kinds[i].kind == kind)
593 validate_real (int kind)
597 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
598 if (gfc_real_kinds[i].kind == kind)
605 validate_logical (int kind)
609 for (i = 0; gfc_logical_kinds[i].kind; i++)
610 if (gfc_logical_kinds[i].kind == kind)
617 validate_character (int kind)
621 for (i = 0; gfc_character_kinds[i].kind; i++)
622 if (gfc_character_kinds[i].kind == kind)
628 /* Validate a kind given a basic type. The return value is the same
629 for the child functions, with -1 indicating nonexistence of the
630 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
633 gfc_validate_kind (bt type, int kind, bool may_fail)
639 case BT_REAL: /* Fall through */
641 rc = validate_real (kind);
644 rc = validate_integer (kind);
647 rc = validate_logical (kind);
650 rc = validate_character (kind);
654 gfc_internal_error ("gfc_validate_kind(): Got bad type");
657 if (rc < 0 && !may_fail)
658 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
664 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
665 Reuse common type nodes where possible. Recognize if the kind matches up
666 with a C type. This will be used later in determining which routines may
667 be scarfed from libm. */
670 gfc_build_int_type (gfc_integer_info *info)
672 int mode_precision = info->bit_size;
674 if (mode_precision == CHAR_TYPE_SIZE)
676 if (mode_precision == SHORT_TYPE_SIZE)
678 if (mode_precision == INT_TYPE_SIZE)
680 if (mode_precision == LONG_TYPE_SIZE)
682 if (mode_precision == LONG_LONG_TYPE_SIZE)
683 info->c_long_long = 1;
685 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
686 return intQI_type_node;
687 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
688 return intHI_type_node;
689 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
690 return intSI_type_node;
691 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
692 return intDI_type_node;
693 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
694 return intTI_type_node;
696 return make_signed_type (mode_precision);
700 gfc_build_uint_type (int size)
702 if (size == CHAR_TYPE_SIZE)
703 return unsigned_char_type_node;
704 if (size == SHORT_TYPE_SIZE)
705 return short_unsigned_type_node;
706 if (size == INT_TYPE_SIZE)
707 return unsigned_type_node;
708 if (size == LONG_TYPE_SIZE)
709 return long_unsigned_type_node;
710 if (size == LONG_LONG_TYPE_SIZE)
711 return long_long_unsigned_type_node;
713 return make_unsigned_type (size);
718 gfc_build_real_type (gfc_real_info *info)
720 int mode_precision = info->mode_precision;
723 if (mode_precision == FLOAT_TYPE_SIZE)
725 if (mode_precision == DOUBLE_TYPE_SIZE)
727 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
728 info->c_long_double = 1;
729 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
731 info->c_float128 = 1;
732 gfc_real16_is_float128 = true;
735 if (TYPE_PRECISION (float_type_node) == mode_precision)
736 return float_type_node;
737 if (TYPE_PRECISION (double_type_node) == mode_precision)
738 return double_type_node;
739 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
740 return long_double_type_node;
742 new_type = make_node (REAL_TYPE);
743 TYPE_PRECISION (new_type) = mode_precision;
744 layout_type (new_type);
749 gfc_build_complex_type (tree scalar_type)
753 if (scalar_type == NULL)
755 if (scalar_type == float_type_node)
756 return complex_float_type_node;
757 if (scalar_type == double_type_node)
758 return complex_double_type_node;
759 if (scalar_type == long_double_type_node)
760 return complex_long_double_type_node;
762 new_type = make_node (COMPLEX_TYPE);
763 TREE_TYPE (new_type) = scalar_type;
764 layout_type (new_type);
769 gfc_build_logical_type (gfc_logical_info *info)
771 int bit_size = info->bit_size;
774 if (bit_size == BOOL_TYPE_SIZE)
777 return boolean_type_node;
780 new_type = make_unsigned_type (bit_size);
781 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
782 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
783 TYPE_PRECISION (new_type) = 1;
790 /* Return the bit size of the C "size_t". */
796 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
797 return INT_TYPE_SIZE;
798 if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
799 return LONG_TYPE_SIZE;
800 if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
801 return SHORT_TYPE_SIZE;
804 return LONG_TYPE_SIZE;
809 /* Create the backend type nodes. We map them to their
810 equivalent C type, at least for now. We also give
811 names to the types here, and we push them in the
812 global binding level context.*/
815 gfc_init_types (void)
821 unsigned HOST_WIDE_INT hi;
822 unsigned HOST_WIDE_INT lo;
824 /* Create and name the types. */
825 #define PUSH_TYPE(name, node) \
826 pushdecl (build_decl (input_location, \
827 TYPE_DECL, get_identifier (name), node))
829 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
831 type = gfc_build_int_type (&gfc_integer_kinds[index]);
832 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
833 if (TYPE_STRING_FLAG (type))
834 type = make_signed_type (gfc_integer_kinds[index].bit_size);
835 gfc_integer_types[index] = type;
836 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
837 gfc_integer_kinds[index].kind);
838 PUSH_TYPE (name_buf, type);
841 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
843 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
844 gfc_logical_types[index] = type;
845 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
846 gfc_logical_kinds[index].kind);
847 PUSH_TYPE (name_buf, type);
850 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
852 type = gfc_build_real_type (&gfc_real_kinds[index]);
853 gfc_real_types[index] = type;
854 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
855 gfc_real_kinds[index].kind);
856 PUSH_TYPE (name_buf, type);
858 if (gfc_real_kinds[index].c_float128)
859 float128_type_node = type;
861 type = gfc_build_complex_type (type);
862 gfc_complex_types[index] = type;
863 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
864 gfc_real_kinds[index].kind);
865 PUSH_TYPE (name_buf, type);
867 if (gfc_real_kinds[index].c_float128)
868 complex_float128_type_node = type;
871 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
873 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
874 type = build_qualified_type (type, TYPE_UNQUALIFIED);
875 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
876 gfc_character_kinds[index].kind);
877 PUSH_TYPE (name_buf, type);
878 gfc_character_types[index] = type;
879 gfc_pcharacter_types[index] = build_pointer_type (type);
881 gfc_character1_type_node = gfc_character_types[0];
883 PUSH_TYPE ("byte", unsigned_char_type_node);
884 PUSH_TYPE ("void", void_type_node);
886 /* DBX debugging output gets upset if these aren't set. */
887 if (!TYPE_NAME (integer_type_node))
888 PUSH_TYPE ("c_integer", integer_type_node);
889 if (!TYPE_NAME (char_type_node))
890 PUSH_TYPE ("c_char", char_type_node);
894 pvoid_type_node = build_pointer_type (void_type_node);
895 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
896 ppvoid_type_node = build_pointer_type (pvoid_type_node);
897 pchar_type_node = build_pointer_type (gfc_character1_type_node);
899 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
901 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
902 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
903 since this function is called before gfc_init_constants. */
905 = build_range_type (gfc_array_index_type,
906 build_int_cst (gfc_array_index_type, 0),
909 /* The maximum array element size that can be handled is determined
910 by the number of bits available to store this field in the array
913 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
914 lo = ~ (unsigned HOST_WIDE_INT) 0;
915 if (n > HOST_BITS_PER_WIDE_INT)
916 hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
918 hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
919 gfc_max_array_element_size
920 = build_int_cst_wide (long_unsigned_type_node, lo, hi);
922 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
923 boolean_true_node = build_int_cst (boolean_type_node, 1);
924 boolean_false_node = build_int_cst (boolean_type_node, 0);
926 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
927 gfc_charlen_int_kind = 4;
928 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
931 /* Get the type node for the given type and kind. */
934 gfc_get_int_type (int kind)
936 int index = gfc_validate_kind (BT_INTEGER, kind, true);
937 return index < 0 ? 0 : gfc_integer_types[index];
941 gfc_get_real_type (int kind)
943 int index = gfc_validate_kind (BT_REAL, kind, true);
944 return index < 0 ? 0 : gfc_real_types[index];
948 gfc_get_complex_type (int kind)
950 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
951 return index < 0 ? 0 : gfc_complex_types[index];
955 gfc_get_logical_type (int kind)
957 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
958 return index < 0 ? 0 : gfc_logical_types[index];
962 gfc_get_char_type (int kind)
964 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
965 return index < 0 ? 0 : gfc_character_types[index];
969 gfc_get_pchar_type (int kind)
971 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
972 return index < 0 ? 0 : gfc_pcharacter_types[index];
976 /* Create a character type with the given kind and length. */
979 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
983 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
984 type = build_array_type (eltype, bounds);
985 TYPE_STRING_FLAG (type) = 1;
991 gfc_get_character_type_len (int kind, tree len)
993 gfc_validate_kind (BT_CHARACTER, kind, false);
994 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
998 /* Get a type node for a character kind. */
1001 gfc_get_character_type (int kind, gfc_charlen * cl)
1005 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1007 return gfc_get_character_type_len (kind, len);
1010 /* Covert a basic type. This will be an array for character types. */
1013 gfc_typenode_for_spec (gfc_typespec * spec)
1023 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1024 has been resolved. This is done so we can convert C_PTR and
1025 C_FUNPTR to simple variables that get translated to (void *). */
1026 if (spec->f90_type == BT_VOID)
1029 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1030 basetype = ptr_type_node;
1032 basetype = pfunc_type_node;
1035 basetype = gfc_get_int_type (spec->kind);
1039 basetype = gfc_get_real_type (spec->kind);
1043 basetype = gfc_get_complex_type (spec->kind);
1047 basetype = gfc_get_logical_type (spec->kind);
1053 basetype = gfc_get_character_type (spec->kind, NULL);
1056 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1061 basetype = gfc_get_derived_type (spec->u.derived);
1063 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1064 type and kind to fit a (void *) and the basetype returned was a
1065 ptr_type_node. We need to pass up this new information to the
1066 symbol that was declared of type C_PTR or C_FUNPTR. */
1067 if (spec->u.derived->attr.is_iso_c)
1069 spec->type = spec->u.derived->ts.type;
1070 spec->kind = spec->u.derived->ts.kind;
1071 spec->f90_type = spec->u.derived->ts.f90_type;
1075 /* This is for the second arg to c_f_pointer and c_f_procpointer
1076 of the iso_c_binding module, to accept any ptr type. */
1077 basetype = ptr_type_node;
1078 if (spec->f90_type == BT_VOID)
1081 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1082 basetype = ptr_type_node;
1084 basetype = pfunc_type_node;
1093 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1096 gfc_conv_array_bound (gfc_expr * expr)
1098 /* If expr is an integer constant, return that. */
1099 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1100 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1102 /* Otherwise return NULL. */
1107 gfc_get_element_type (tree type)
1111 if (GFC_ARRAY_TYPE_P (type))
1113 if (TREE_CODE (type) == POINTER_TYPE)
1114 type = TREE_TYPE (type);
1115 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1116 element = TREE_TYPE (type);
1120 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1121 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1123 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1124 element = TREE_TYPE (element);
1126 gcc_assert (TREE_CODE (element) == ARRAY_TYPE);
1127 element = TREE_TYPE (element);
1133 /* Build an array. This function is called from gfc_sym_type().
1134 Actually returns array descriptor type.
1136 Format of array descriptors is as follows:
1138 struct gfc_array_descriptor
1143 struct descriptor_dimension dimension[N_DIM];
1146 struct descriptor_dimension
1153 Translation code should use gfc_conv_descriptor_* rather than
1154 accessing the descriptor directly. Any changes to the array
1155 descriptor type will require changes in gfc_conv_descriptor_* and
1156 gfc_build_array_initializer.
1158 This is represented internally as a RECORD_TYPE. The index nodes
1159 are gfc_array_index_type and the data node is a pointer to the
1160 data. See below for the handling of character types.
1162 The dtype member is formatted as follows:
1163 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1164 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1165 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1167 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1168 this generated poor code for assumed/deferred size arrays. These
1169 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1170 of the GENERIC grammar. Also, there is no way to explicitly set
1171 the array stride, so all data must be packed(1). I've tried to
1172 mark all the functions which would require modification with a GCC
1175 The data component points to the first element in the array. The
1176 offset field is the position of the origin of the array (i.e. element
1177 (0, 0 ...)). This may be outside the bounds of the array.
1179 An element is accessed by
1180 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1181 This gives good performance as the computation does not involve the
1182 bounds of the array. For packed arrays, this is optimized further
1183 by substituting the known strides.
1185 This system has one problem: all array bounds must be within 2^31
1186 elements of the origin (2^63 on 64-bit machines). For example
1187 integer, dimension (80000:90000, 80000:90000, 2) :: array
1188 may not work properly on 32-bit machines because 80000*80000 >
1189 2^31, so the calculation for stride2 would overflow. This may
1190 still work, but I haven't checked, and it relies on the overflow
1191 doing the right thing.
1193 The way to fix this problem is to access elements as follows:
1194 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1195 Obviously this is much slower. I will make this a compile time
1196 option, something like -fsmall-array-offsets. Mixing code compiled
1197 with and without this switch will work.
1199 (1) This can be worked around by modifying the upper bound of the
1200 previous dimension. This requires extra fields in the descriptor
1201 (both real_ubound and fake_ubound). */
1204 /* Returns true if the array sym does not require a descriptor. */
1207 gfc_is_nodesc_array (gfc_symbol * sym)
1209 gcc_assert (sym->attr.dimension);
1211 /* We only want local arrays. */
1212 if (sym->attr.pointer || sym->attr.allocatable)
1215 /* We want a descriptor for associate-name arrays that do not have an
1216 explicitely known shape already. */
1217 if (sym->assoc && sym->as->type != AS_EXPLICIT)
1220 if (sym->attr.dummy)
1221 return sym->as->type != AS_ASSUMED_SHAPE;
1223 if (sym->attr.result || sym->attr.function)
1226 gcc_assert (sym->as->type == AS_EXPLICIT || sym->as->cp_was_assumed);
1232 /* Create an array descriptor type. */
1235 gfc_build_array_type (tree type, gfc_array_spec * as,
1236 enum gfc_array_kind akind, bool restricted,
1239 tree lbound[GFC_MAX_DIMENSIONS];
1240 tree ubound[GFC_MAX_DIMENSIONS];
1243 for (n = 0; n < as->rank; n++)
1245 /* Create expressions for the known bounds of the array. */
1246 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1247 lbound[n] = gfc_index_one_node;
1249 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1250 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1253 if (as->type == AS_ASSUMED_SHAPE)
1254 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1255 : GFC_ARRAY_ASSUMED_SHAPE;
1256 return gfc_get_array_type_bounds (type, as->rank, as->corank, lbound,
1257 ubound, 0, akind, restricted);
1260 /* Returns the struct descriptor_dimension type. */
1263 gfc_get_desc_dim_type (void)
1266 tree decl, *chain = NULL;
1268 if (gfc_desc_dim_type)
1269 return gfc_desc_dim_type;
1271 /* Build the type node. */
1272 type = make_node (RECORD_TYPE);
1274 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1275 TYPE_PACKED (type) = 1;
1277 /* Consists of the stride, lbound and ubound members. */
1278 decl = gfc_add_field_to_struct_1 (type,
1279 get_identifier ("stride"),
1280 gfc_array_index_type, &chain);
1281 TREE_NO_WARNING (decl) = 1;
1283 decl = gfc_add_field_to_struct_1 (type,
1284 get_identifier ("lbound"),
1285 gfc_array_index_type, &chain);
1286 TREE_NO_WARNING (decl) = 1;
1288 decl = gfc_add_field_to_struct_1 (type,
1289 get_identifier ("ubound"),
1290 gfc_array_index_type, &chain);
1291 TREE_NO_WARNING (decl) = 1;
1293 /* Finish off the type. */
1294 gfc_finish_type (type);
1295 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1297 gfc_desc_dim_type = type;
1302 /* Return the DTYPE for an array. This describes the type and type parameters
1304 /* TODO: Only call this when the value is actually used, and make all the
1305 unknown cases abort. */
1308 gfc_get_dtype (tree type)
1318 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1320 if (GFC_TYPE_ARRAY_DTYPE (type))
1321 return GFC_TYPE_ARRAY_DTYPE (type);
1323 rank = GFC_TYPE_ARRAY_RANK (type);
1324 etype = gfc_get_element_type (type);
1326 switch (TREE_CODE (etype))
1344 /* We will never have arrays of arrays. */
1354 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1355 /* We can strange array types for temporary arrays. */
1356 return gfc_index_zero_node;
1359 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1360 size = TYPE_SIZE_UNIT (etype);
1362 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1363 if (size && INTEGER_CST_P (size))
1365 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1366 internal_error ("Array element size too big");
1368 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1370 dtype = build_int_cst (gfc_array_index_type, i);
1372 if (size && !INTEGER_CST_P (size))
1374 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1375 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1376 gfc_array_index_type,
1377 fold_convert (gfc_array_index_type, size), tmp);
1378 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1381 /* If we don't know the size we leave it as zero. This should never happen
1382 for anything that is actually used. */
1383 /* TODO: Check this is actually true, particularly when repacking
1384 assumed size parameters. */
1386 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1391 /* Build an array type for use without a descriptor, packed according
1392 to the value of PACKED. */
1395 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1409 mpz_init_set_ui (offset, 0);
1410 mpz_init_set_ui (stride, 1);
1413 /* We don't use build_array_type because this does not include include
1414 lang-specific information (i.e. the bounds of the array) when checking
1416 type = make_node (ARRAY_TYPE);
1418 GFC_ARRAY_TYPE_P (type) = 1;
1419 TYPE_LANG_SPECIFIC (type)
1420 = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
1422 known_stride = (packed != PACKED_NO);
1424 for (n = 0; n < as->rank; n++)
1426 /* Fill in the stride and bound components of the type. */
1428 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1431 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1433 expr = as->lower[n];
1434 if (expr->expr_type == EXPR_CONSTANT)
1436 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1437 gfc_index_integer_kind);
1444 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1448 /* Calculate the offset. */
1449 mpz_mul (delta, stride, as->lower[n]->value.integer);
1450 mpz_sub (offset, offset, delta);
1455 expr = as->upper[n];
1456 if (expr && expr->expr_type == EXPR_CONSTANT)
1458 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1459 gfc_index_integer_kind);
1466 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1470 /* Calculate the stride. */
1471 mpz_sub (delta, as->upper[n]->value.integer,
1472 as->lower[n]->value.integer);
1473 mpz_add_ui (delta, delta, 1);
1474 mpz_mul (stride, stride, delta);
1477 /* Only the first stride is known for partial packed arrays. */
1478 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1484 GFC_TYPE_ARRAY_OFFSET (type) =
1485 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1488 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1492 GFC_TYPE_ARRAY_SIZE (type) =
1493 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1496 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1498 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1499 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1500 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1502 /* TODO: use main type if it is unbounded. */
1503 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1504 build_pointer_type (build_array_type (etype, range));
1506 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1507 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1508 TYPE_QUAL_RESTRICT);
1512 mpz_sub_ui (stride, stride, 1);
1513 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1518 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1519 TYPE_DOMAIN (type) = range;
1521 build_pointer_type (etype);
1522 TREE_TYPE (type) = etype;
1530 /* Represent packed arrays as multi-dimensional if they have rank >
1531 1 and with proper bounds, instead of flat arrays. This makes for
1532 better debug info. */
1535 tree gtype = etype, rtype, type_decl;
1537 for (n = as->rank - 1; n >= 0; n--)
1539 rtype = build_range_type (gfc_array_index_type,
1540 GFC_TYPE_ARRAY_LBOUND (type, n),
1541 GFC_TYPE_ARRAY_UBOUND (type, n));
1542 gtype = build_array_type (gtype, rtype);
1544 TYPE_NAME (type) = type_decl = build_decl (input_location,
1545 TYPE_DECL, NULL, gtype);
1546 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1549 if (packed != PACKED_STATIC || !known_stride)
1551 /* For dummy arrays and automatic (heap allocated) arrays we
1552 want a pointer to the array. */
1553 type = build_pointer_type (type);
1555 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1556 GFC_ARRAY_TYPE_P (type) = 1;
1557 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1562 /* Return or create the base type for an array descriptor. */
1565 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1567 tree fat_type, decl, arraytype, *chain = NULL;
1568 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1569 int idx = 2 * (codimen + dimen - 1) + restricted;
1571 gcc_assert (dimen >= 1 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1572 if (gfc_array_descriptor_base[idx])
1573 return gfc_array_descriptor_base[idx];
1575 /* Build the type node. */
1576 fat_type = make_node (RECORD_TYPE);
1578 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1579 TYPE_NAME (fat_type) = get_identifier (name);
1580 TYPE_NAMELESS (fat_type) = 1;
1582 /* Add the data member as the first element of the descriptor. */
1583 decl = gfc_add_field_to_struct_1 (fat_type,
1584 get_identifier ("data"),
1587 : ptr_type_node), &chain);
1589 /* Add the base component. */
1590 decl = gfc_add_field_to_struct_1 (fat_type,
1591 get_identifier ("offset"),
1592 gfc_array_index_type, &chain);
1593 TREE_NO_WARNING (decl) = 1;
1595 /* Add the dtype component. */
1596 decl = gfc_add_field_to_struct_1 (fat_type,
1597 get_identifier ("dtype"),
1598 gfc_array_index_type, &chain);
1599 TREE_NO_WARNING (decl) = 1;
1601 /* Build the array type for the stride and bound components. */
1603 build_array_type (gfc_get_desc_dim_type (),
1604 build_range_type (gfc_array_index_type,
1605 gfc_index_zero_node,
1606 gfc_rank_cst[codimen + dimen - 1]));
1608 decl = gfc_add_field_to_struct_1 (fat_type,
1609 get_identifier ("dim"),
1611 TREE_NO_WARNING (decl) = 1;
1613 /* Finish off the type. */
1614 gfc_finish_type (fat_type);
1615 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1617 gfc_array_descriptor_base[idx] = fat_type;
1621 /* Build an array (descriptor) type with given bounds. */
1624 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1625 tree * ubound, int packed,
1626 enum gfc_array_kind akind, bool restricted)
1628 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1629 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1630 const char *type_name;
1633 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1634 fat_type = build_distinct_type_copy (base_type);
1635 /* Make sure that nontarget and target array type have the same canonical
1636 type (and same stub decl for debug info). */
1637 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1638 TYPE_CANONICAL (fat_type) = base_type;
1639 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1641 tmp = TYPE_NAME (etype);
1642 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1643 tmp = DECL_NAME (tmp);
1645 type_name = IDENTIFIER_POINTER (tmp);
1647 type_name = "unknown";
1648 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1649 GFC_MAX_SYMBOL_LEN, type_name);
1650 TYPE_NAME (fat_type) = get_identifier (name);
1651 TYPE_NAMELESS (fat_type) = 1;
1653 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1654 TYPE_LANG_SPECIFIC (fat_type)
1655 = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
1657 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1658 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1659 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1661 /* Build an array descriptor record type. */
1663 stride = gfc_index_one_node;
1666 for (n = 0; n < dimen; n++)
1668 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1675 if (lower != NULL_TREE)
1677 if (INTEGER_CST_P (lower))
1678 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1684 if (upper != NULL_TREE)
1686 if (INTEGER_CST_P (upper))
1687 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1692 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1694 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1695 gfc_array_index_type, upper, lower);
1696 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1697 gfc_array_index_type, tmp,
1698 gfc_index_one_node);
1699 stride = fold_build2_loc (input_location, MULT_EXPR,
1700 gfc_array_index_type, tmp, stride);
1701 /* Check the folding worked. */
1702 gcc_assert (INTEGER_CST_P (stride));
1707 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1709 /* TODO: known offsets for descriptors. */
1710 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1712 /* We define data as an array with the correct size if possible.
1713 Much better than doing pointer arithmetic. */
1715 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1716 int_const_binop (MINUS_EXPR, stride,
1717 integer_one_node, 0));
1719 rtype = gfc_array_range_type;
1720 arraytype = build_array_type (etype, rtype);
1721 arraytype = build_pointer_type (arraytype);
1723 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1724 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1726 /* This will generate the base declarations we need to emit debug
1727 information for this type. FIXME: there must be a better way to
1728 avoid divergence between compilations with and without debug
1731 struct array_descr_info info;
1732 gfc_get_array_descr_info (fat_type, &info);
1733 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1739 /* Build a pointer type. This function is called from gfc_sym_type(). */
1742 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1744 /* Array pointer types aren't actually pointers. */
1745 if (sym->attr.dimension)
1748 return build_pointer_type (type);
1751 /* Return the type for a symbol. Special handling is required for character
1752 types to get the correct level of indirection.
1753 For functions return the return type.
1754 For subroutines return void_type_node.
1755 Calling this multiple times for the same symbol should be avoided,
1756 especially for character and array types. */
1759 gfc_sym_type (gfc_symbol * sym)
1765 /* Procedure Pointers inside COMMON blocks. */
1766 if (sym->attr.proc_pointer && sym->attr.in_common)
1768 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
1769 sym->attr.proc_pointer = 0;
1770 type = build_pointer_type (gfc_get_function_type (sym));
1771 sym->attr.proc_pointer = 1;
1775 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1776 return void_type_node;
1778 /* In the case of a function the fake result variable may have a
1779 type different from the function type, so don't return early in
1781 if (sym->backend_decl && !sym->attr.function)
1782 return TREE_TYPE (sym->backend_decl);
1784 if (sym->ts.type == BT_CHARACTER
1785 && ((sym->attr.function && sym->attr.is_bind_c)
1786 || (sym->attr.result
1787 && sym->ns->proc_name
1788 && sym->ns->proc_name->attr.is_bind_c)))
1789 type = gfc_character1_type_node;
1791 type = gfc_typenode_for_spec (&sym->ts);
1793 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
1798 restricted = !sym->attr.target && !sym->attr.pointer
1799 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
1800 if (sym->attr.dimension)
1802 if (gfc_is_nodesc_array (sym))
1804 /* If this is a character argument of unknown length, just use the
1806 if (sym->ts.type != BT_CHARACTER
1807 || !(sym->attr.dummy || sym->attr.function)
1808 || sym->ts.u.cl->backend_decl)
1810 type = gfc_get_nodesc_array_type (type, sym->as,
1817 if (sym->attr.cray_pointee)
1818 GFC_POINTER_TYPE_P (type) = 1;
1822 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
1823 if (sym->attr.pointer)
1824 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
1825 : GFC_ARRAY_POINTER;
1826 else if (sym->attr.allocatable)
1827 akind = GFC_ARRAY_ALLOCATABLE;
1828 type = gfc_build_array_type (type, sym->as, akind, restricted,
1829 sym->attr.contiguous);
1834 if (sym->attr.allocatable || sym->attr.pointer
1835 || gfc_is_associate_pointer (sym))
1836 type = gfc_build_pointer_type (sym, type);
1837 if (sym->attr.pointer || sym->attr.cray_pointee)
1838 GFC_POINTER_TYPE_P (type) = 1;
1841 /* We currently pass all parameters by reference.
1842 See f95_get_function_decl. For dummy function parameters return the
1846 /* We must use pointer types for potentially absent variables. The
1847 optimizers assume a reference type argument is never NULL. */
1848 if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1849 type = build_pointer_type (type);
1852 type = build_reference_type (type);
1854 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1861 /* Layout and output debug info for a record type. */
1864 gfc_finish_type (tree type)
1868 decl = build_decl (input_location,
1869 TYPE_DECL, NULL_TREE, type);
1870 TYPE_STUB_DECL (type) = decl;
1872 rest_of_type_compilation (type, 1);
1873 rest_of_decl_compilation (decl, 1, 0);
1876 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1877 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
1878 to the end of the field list pointed to by *CHAIN.
1880 Returns a pointer to the new field. */
1883 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
1885 tree decl = build_decl (input_location, FIELD_DECL, name, type);
1887 DECL_CONTEXT (decl) = context;
1888 DECL_CHAIN (decl) = NULL_TREE;
1889 if (TYPE_FIELDS (context) == NULL_TREE)
1890 TYPE_FIELDS (context) = decl;
1895 *chain = &DECL_CHAIN (decl);
1901 /* Like `gfc_add_field_to_struct_1', but adds alignment
1905 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
1907 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
1909 DECL_INITIAL (decl) = 0;
1910 DECL_ALIGN (decl) = 0;
1911 DECL_USER_ALIGN (decl) = 0;
1917 /* Copy the backend_decl and component backend_decls if
1918 the two derived type symbols are "equal", as described
1919 in 4.4.2 and resolved by gfc_compare_derived_types. */
1922 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
1925 gfc_component *to_cm;
1926 gfc_component *from_cm;
1928 if (from->backend_decl == NULL
1929 || !gfc_compare_derived_types (from, to))
1932 to->backend_decl = from->backend_decl;
1934 to_cm = to->components;
1935 from_cm = from->components;
1937 /* Copy the component declarations. If a component is itself
1938 a derived type, we need a copy of its component declarations.
1939 This is done by recursing into gfc_get_derived_type and
1940 ensures that the component's component declarations have
1941 been built. If it is a character, we need the character
1943 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
1945 to_cm->backend_decl = from_cm->backend_decl;
1946 if (from_cm->ts.type == BT_DERIVED
1947 && (!from_cm->attr.pointer || from_gsym))
1948 gfc_get_derived_type (to_cm->ts.u.derived);
1949 else if (from_cm->ts.type == BT_CLASS
1950 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
1951 gfc_get_derived_type (to_cm->ts.u.derived);
1952 else if (from_cm->ts.type == BT_CHARACTER)
1953 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
1960 /* Build a tree node for a procedure pointer component. */
1963 gfc_get_ppc_type (gfc_component* c)
1967 /* Explicit interface. */
1968 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
1969 return build_pointer_type (gfc_get_function_type (c->ts.interface));
1971 /* Implicit interface (only return value may be known). */
1972 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
1973 t = gfc_typenode_for_spec (&c->ts);
1977 return build_pointer_type (build_function_type_list (t, NULL_TREE));
1981 /* Build a tree node for a derived type. If there are equal
1982 derived types, with different local names, these are built
1983 at the same time. If an equal derived type has been built
1984 in a parent namespace, this is used. */
1987 gfc_get_derived_type (gfc_symbol * derived)
1989 tree typenode = NULL, field = NULL, field_type = NULL;
1990 tree canonical = NULL_TREE;
1992 bool got_canonical = false;
1998 gcc_assert (derived && derived->attr.flavor == FL_DERIVED);
2000 /* See if it's one of the iso_c_binding derived types. */
2001 if (derived->attr.is_iso_c == 1)
2003 if (derived->backend_decl)
2004 return derived->backend_decl;
2006 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2007 derived->backend_decl = ptr_type_node;
2009 derived->backend_decl = pfunc_type_node;
2011 derived->ts.kind = gfc_index_integer_kind;
2012 derived->ts.type = BT_INTEGER;
2013 /* Set the f90_type to BT_VOID as a way to recognize something of type
2014 BT_INTEGER that needs to fit a void * for the purpose of the
2015 iso_c_binding derived types. */
2016 derived->ts.f90_type = BT_VOID;
2018 return derived->backend_decl;
2021 /* If use associated, use the module type for this one. */
2022 if (gfc_option.flag_whole_file
2023 && derived->backend_decl == NULL
2024 && derived->attr.use_assoc
2027 gsym = gfc_find_gsymbol (gfc_gsym_root, derived->module);
2028 if (gsym && gsym->ns && gsym->type == GSYM_MODULE)
2032 gfc_find_symbol (derived->name, gsym->ns, 0, &s);
2035 if (!s->backend_decl)
2036 s->backend_decl = gfc_get_derived_type (s);
2037 gfc_copy_dt_decls_ifequal (s, derived, true);
2038 goto copy_derived_types;
2043 /* If a whole file compilation, the derived types from an earlier
2044 namespace can be used as the the canonical type. */
2045 if (gfc_option.flag_whole_file
2046 && derived->backend_decl == NULL
2047 && !derived->attr.use_assoc
2048 && gfc_global_ns_list)
2050 for (ns = gfc_global_ns_list;
2051 ns->translated && !got_canonical;
2054 dt = ns->derived_types;
2055 for (; dt && !canonical; dt = dt->next)
2057 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2058 if (derived->backend_decl)
2059 got_canonical = true;
2064 /* Store up the canonical type to be added to this one. */
2067 if (TYPE_CANONICAL (derived->backend_decl))
2068 canonical = TYPE_CANONICAL (derived->backend_decl);
2070 canonical = derived->backend_decl;
2072 derived->backend_decl = NULL_TREE;
2075 /* derived->backend_decl != 0 means we saw it before, but its
2076 components' backend_decl may have not been built. */
2077 if (derived->backend_decl)
2079 /* Its components' backend_decl have been built or we are
2080 seeing recursion through the formal arglist of a procedure
2081 pointer component. */
2082 if (TYPE_FIELDS (derived->backend_decl)
2083 || derived->attr.proc_pointer_comp)
2084 return derived->backend_decl;
2086 typenode = derived->backend_decl;
2090 /* We see this derived type first time, so build the type node. */
2091 typenode = make_node (RECORD_TYPE);
2092 TYPE_NAME (typenode) = get_identifier (derived->name);
2093 TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
2094 derived->backend_decl = typenode;
2097 /* Go through the derived type components, building them as
2098 necessary. The reason for doing this now is that it is
2099 possible to recurse back to this derived type through a
2100 pointer component (PR24092). If this happens, the fields
2101 will be built and so we can return the type. */
2102 for (c = derived->components; c; c = c->next)
2104 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2107 if ((!c->attr.pointer && !c->attr.proc_pointer)
2108 || c->ts.u.derived->backend_decl == NULL)
2109 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
2111 if (c->ts.u.derived && c->ts.u.derived->attr.is_iso_c)
2113 /* Need to copy the modified ts from the derived type. The
2114 typespec was modified because C_PTR/C_FUNPTR are translated
2115 into (void *) from derived types. */
2116 c->ts.type = c->ts.u.derived->ts.type;
2117 c->ts.kind = c->ts.u.derived->ts.kind;
2118 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2121 c->initializer->ts.type = c->ts.type;
2122 c->initializer->ts.kind = c->ts.kind;
2123 c->initializer->ts.f90_type = c->ts.f90_type;
2124 c->initializer->expr_type = EXPR_NULL;
2129 if (TYPE_FIELDS (derived->backend_decl))
2130 return derived->backend_decl;
2132 /* Build the type member list. Install the newly created RECORD_TYPE
2133 node as DECL_CONTEXT of each FIELD_DECL. */
2134 for (c = derived->components; c; c = c->next)
2136 if (c->attr.proc_pointer)
2137 field_type = gfc_get_ppc_type (c);
2138 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2139 field_type = c->ts.u.derived->backend_decl;
2142 if (c->ts.type == BT_CHARACTER)
2144 /* Evaluate the string length. */
2145 gfc_conv_const_charlen (c->ts.u.cl);
2146 gcc_assert (c->ts.u.cl->backend_decl);
2149 field_type = gfc_typenode_for_spec (&c->ts);
2152 /* This returns an array descriptor type. Initialization may be
2154 if (c->attr.dimension && !c->attr.proc_pointer)
2156 if (c->attr.pointer || c->attr.allocatable)
2158 enum gfc_array_kind akind;
2159 if (c->attr.pointer)
2160 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2161 : GFC_ARRAY_POINTER;
2163 akind = GFC_ARRAY_ALLOCATABLE;
2164 /* Pointers to arrays aren't actually pointer types. The
2165 descriptors are separate, but the data is common. */
2166 field_type = gfc_build_array_type (field_type, c->as, akind,
2168 && !c->attr.pointer,
2169 c->attr.contiguous);
2172 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2176 else if ((c->attr.pointer || c->attr.allocatable)
2177 && !c->attr.proc_pointer)
2178 field_type = build_pointer_type (field_type);
2180 /* vtype fields can point to different types to the base type. */
2181 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.vtype)
2182 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2185 field = gfc_add_field_to_struct (typenode,
2186 get_identifier (c->name),
2187 field_type, &chain);
2189 gfc_set_decl_location (field, &c->loc);
2190 else if (derived->declared_at.lb)
2191 gfc_set_decl_location (field, &derived->declared_at);
2193 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2196 if (!c->backend_decl)
2197 c->backend_decl = field;
2200 /* Now lay out the derived type, including the fields. */
2202 TYPE_CANONICAL (typenode) = canonical;
2204 gfc_finish_type (typenode);
2205 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2206 if (derived->module && derived->ns->proc_name
2207 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2209 if (derived->ns->proc_name->backend_decl
2210 && TREE_CODE (derived->ns->proc_name->backend_decl)
2213 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2214 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2215 = derived->ns->proc_name->backend_decl;
2219 derived->backend_decl = typenode;
2223 for (dt = gfc_derived_types; dt; dt = dt->next)
2224 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2226 return derived->backend_decl;
2231 gfc_return_by_reference (gfc_symbol * sym)
2233 if (!sym->attr.function)
2236 if (sym->attr.dimension)
2239 if (sym->ts.type == BT_CHARACTER
2240 && !sym->attr.is_bind_c
2241 && (!sym->attr.result
2242 || !sym->ns->proc_name
2243 || !sym->ns->proc_name->attr.is_bind_c))
2246 /* Possibly return complex numbers by reference for g77 compatibility.
2247 We don't do this for calls to intrinsics (as the library uses the
2248 -fno-f2c calling convention), nor for calls to functions which always
2249 require an explicit interface, as no compatibility problems can
2251 if (gfc_option.flag_f2c
2252 && sym->ts.type == BT_COMPLEX
2253 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2260 gfc_get_mixed_entry_union (gfc_namespace *ns)
2264 char name[GFC_MAX_SYMBOL_LEN + 1];
2265 gfc_entry_list *el, *el2;
2267 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2268 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2270 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2272 /* Build the type node. */
2273 type = make_node (UNION_TYPE);
2275 TYPE_NAME (type) = get_identifier (name);
2277 for (el = ns->entries; el; el = el->next)
2279 /* Search for duplicates. */
2280 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2281 if (el2->sym->result == el->sym->result)
2285 gfc_add_field_to_struct_1 (type,
2286 get_identifier (el->sym->result->name),
2287 gfc_sym_type (el->sym->result), &chain);
2290 /* Finish off the type. */
2291 gfc_finish_type (type);
2292 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2296 /* Create a "fn spec" based on the formal arguments;
2297 cf. create_function_arglist. */
2300 create_fn_spec (gfc_symbol *sym, tree fntype)
2304 gfc_formal_arglist *f;
2307 memset (&spec, 0, sizeof (spec));
2311 if (sym->attr.entry_master)
2312 spec[spec_len++] = 'R';
2313 if (gfc_return_by_reference (sym))
2315 gfc_symbol *result = sym->result ? sym->result : sym;
2317 if (result->attr.pointer || sym->attr.proc_pointer)
2318 spec[spec_len++] = '.';
2320 spec[spec_len++] = 'w';
2321 if (sym->ts.type == BT_CHARACTER)
2322 spec[spec_len++] = 'R';
2325 for (f = sym->formal; f; f = f->next)
2326 if (spec_len < sizeof (spec))
2328 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2329 || f->sym->attr.external || f->sym->attr.cray_pointer)
2330 spec[spec_len++] = '.';
2331 else if (f->sym->attr.intent == INTENT_IN)
2332 spec[spec_len++] = 'r';
2334 spec[spec_len++] = 'w';
2337 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2338 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2339 return build_type_attribute_variant (fntype, tmp);
2344 gfc_get_function_type (gfc_symbol * sym)
2348 gfc_formal_arglist *f;
2351 int alternate_return;
2353 /* Make sure this symbol is a function, a subroutine or the main
2355 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2356 || sym->attr.flavor == FL_PROGRAM);
2358 if (sym->backend_decl)
2359 return TREE_TYPE (sym->backend_decl);
2362 alternate_return = 0;
2363 typelist = NULL_TREE;
2365 if (sym->attr.entry_master)
2367 /* Additional parameter for selecting an entry point. */
2368 typelist = gfc_chainon_list (typelist, gfc_array_index_type);
2376 if (arg->ts.type == BT_CHARACTER)
2377 gfc_conv_const_charlen (arg->ts.u.cl);
2379 /* Some functions we use an extra parameter for the return value. */
2380 if (gfc_return_by_reference (sym))
2382 type = gfc_sym_type (arg);
2383 if (arg->ts.type == BT_COMPLEX
2384 || arg->attr.dimension
2385 || arg->ts.type == BT_CHARACTER)
2386 type = build_reference_type (type);
2388 typelist = gfc_chainon_list (typelist, type);
2389 if (arg->ts.type == BT_CHARACTER)
2390 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
2393 /* Build the argument types for the function. */
2394 for (f = sym->formal; f; f = f->next)
2399 /* Evaluate constant character lengths here so that they can be
2400 included in the type. */
2401 if (arg->ts.type == BT_CHARACTER)
2402 gfc_conv_const_charlen (arg->ts.u.cl);
2404 if (arg->attr.flavor == FL_PROCEDURE)
2406 type = gfc_get_function_type (arg);
2407 type = build_pointer_type (type);
2410 type = gfc_sym_type (arg);
2412 /* Parameter Passing Convention
2414 We currently pass all parameters by reference.
2415 Parameters with INTENT(IN) could be passed by value.
2416 The problem arises if a function is called via an implicit
2417 prototype. In this situation the INTENT is not known.
2418 For this reason all parameters to global functions must be
2419 passed by reference. Passing by value would potentially
2420 generate bad code. Worse there would be no way of telling that
2421 this code was bad, except that it would give incorrect results.
2423 Contained procedures could pass by value as these are never
2424 used without an explicit interface, and cannot be passed as
2425 actual parameters for a dummy procedure. */
2426 if (arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2428 typelist = gfc_chainon_list (typelist, type);
2432 if (sym->attr.subroutine)
2433 alternate_return = 1;
2437 /* Add hidden string length parameters. */
2439 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
2442 typelist = chainon (typelist, void_list_node);
2443 else if (sym->attr.is_main_program)
2444 typelist = void_list_node;
2446 if (alternate_return)
2447 type = integer_type_node;
2448 else if (!sym->attr.function || gfc_return_by_reference (sym))
2449 type = void_type_node;
2450 else if (sym->attr.mixed_entry_master)
2451 type = gfc_get_mixed_entry_union (sym->ns);
2452 else if (gfc_option.flag_f2c
2453 && sym->ts.type == BT_REAL
2454 && sym->ts.kind == gfc_default_real_kind
2455 && !sym->attr.always_explicit)
2457 /* Special case: f2c calling conventions require that (scalar)
2458 default REAL functions return the C type double instead. f2c
2459 compatibility is only an issue with functions that don't
2460 require an explicit interface, as only these could be
2461 implemented in Fortran 77. */
2462 sym->ts.kind = gfc_default_double_kind;
2463 type = gfc_typenode_for_spec (&sym->ts);
2464 sym->ts.kind = gfc_default_real_kind;
2466 else if (sym->result && sym->result->attr.proc_pointer)
2467 /* Procedure pointer return values. */
2469 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
2471 /* Unset proc_pointer as gfc_get_function_type
2472 is called recursively. */
2473 sym->result->attr.proc_pointer = 0;
2474 type = build_pointer_type (gfc_get_function_type (sym->result));
2475 sym->result->attr.proc_pointer = 1;
2478 type = gfc_sym_type (sym->result);
2481 type = gfc_sym_type (sym);
2483 type = build_function_type (type, typelist);
2484 type = create_fn_spec (sym, type);
2489 /* Language hooks for middle-end access to type nodes. */
2491 /* Return an integer type with BITS bits of precision,
2492 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2495 gfc_type_for_size (unsigned bits, int unsignedp)
2500 for (i = 0; i <= MAX_INT_KINDS; ++i)
2502 tree type = gfc_integer_types[i];
2503 if (type && bits == TYPE_PRECISION (type))
2507 /* Handle TImode as a special case because it is used by some backends
2508 (e.g. ARM) even though it is not available for normal use. */
2509 #if HOST_BITS_PER_WIDE_INT >= 64
2510 if (bits == TYPE_PRECISION (intTI_type_node))
2511 return intTI_type_node;
2516 if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
2517 return unsigned_intQI_type_node;
2518 if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
2519 return unsigned_intHI_type_node;
2520 if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
2521 return unsigned_intSI_type_node;
2522 if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
2523 return unsigned_intDI_type_node;
2524 if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
2525 return unsigned_intTI_type_node;
2531 /* Return a data type that has machine mode MODE. If the mode is an
2532 integer, then UNSIGNEDP selects between signed and unsigned types. */
2535 gfc_type_for_mode (enum machine_mode mode, int unsignedp)
2540 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2541 base = gfc_real_types;
2542 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2543 base = gfc_complex_types;
2544 else if (SCALAR_INT_MODE_P (mode))
2545 return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
2546 else if (VECTOR_MODE_P (mode))
2548 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2549 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
2550 if (inner_type != NULL_TREE)
2551 return build_vector_type_for_mode (inner_type, mode);
2557 for (i = 0; i <= MAX_REAL_KINDS; ++i)
2559 tree type = base[i];
2560 if (type && mode == TYPE_MODE (type))
2567 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
2571 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
2574 bool indirect = false;
2575 tree etype, ptype, field, t, base_decl;
2576 tree data_off, dim_off, dim_size, elem_size;
2577 tree lower_suboff, upper_suboff, stride_suboff;
2579 if (! GFC_DESCRIPTOR_TYPE_P (type))
2581 if (! POINTER_TYPE_P (type))
2583 type = TREE_TYPE (type);
2584 if (! GFC_DESCRIPTOR_TYPE_P (type))
2589 rank = GFC_TYPE_ARRAY_RANK (type);
2590 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
2593 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2594 gcc_assert (POINTER_TYPE_P (etype));
2595 etype = TREE_TYPE (etype);
2596 gcc_assert (TREE_CODE (etype) == ARRAY_TYPE);
2597 etype = TREE_TYPE (etype);
2598 /* Can't handle variable sized elements yet. */
2599 if (int_size_in_bytes (etype) <= 0)
2601 /* Nor non-constant lower bounds in assumed shape arrays. */
2602 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
2603 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
2605 for (dim = 0; dim < rank; dim++)
2606 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
2607 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
2611 memset (info, '\0', sizeof (*info));
2612 info->ndimensions = rank;
2613 info->element_type = etype;
2614 ptype = build_pointer_type (gfc_array_index_type);
2615 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
2618 base_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
2619 indirect ? build_pointer_type (ptype) : ptype);
2620 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
2622 info->base_decl = base_decl;
2624 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
2626 if (GFC_TYPE_ARRAY_SPAN (type))
2627 elem_size = GFC_TYPE_ARRAY_SPAN (type);
2629 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
2630 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
2631 data_off = byte_position (field);
2632 field = DECL_CHAIN (field);
2633 field = DECL_CHAIN (field);
2634 field = DECL_CHAIN (field);
2635 dim_off = byte_position (field);
2636 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
2637 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
2638 stride_suboff = byte_position (field);
2639 field = DECL_CHAIN (field);
2640 lower_suboff = byte_position (field);
2641 field = DECL_CHAIN (field);
2642 upper_suboff = byte_position (field);
2645 if (!integer_zerop (data_off))
2646 t = build2 (POINTER_PLUS_EXPR, ptype, t, data_off);
2647 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
2648 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
2649 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
2650 info->allocated = build2 (NE_EXPR, boolean_type_node,
2651 info->data_location, null_pointer_node);
2652 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
2653 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
2654 info->associated = build2 (NE_EXPR, boolean_type_node,
2655 info->data_location, null_pointer_node);
2657 for (dim = 0; dim < rank; dim++)
2659 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2660 size_binop (PLUS_EXPR, dim_off, lower_suboff));
2661 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2662 info->dimen[dim].lower_bound = t;
2663 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2664 size_binop (PLUS_EXPR, dim_off, upper_suboff));
2665 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2666 info->dimen[dim].upper_bound = t;
2667 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
2668 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
2670 /* Assumed shape arrays have known lower bounds. */
2671 info->dimen[dim].upper_bound
2672 = build2 (MINUS_EXPR, gfc_array_index_type,
2673 info->dimen[dim].upper_bound,
2674 info->dimen[dim].lower_bound);
2675 info->dimen[dim].lower_bound
2676 = fold_convert (gfc_array_index_type,
2677 GFC_TYPE_ARRAY_LBOUND (type, dim));
2678 info->dimen[dim].upper_bound
2679 = build2 (PLUS_EXPR, gfc_array_index_type,
2680 info->dimen[dim].lower_bound,
2681 info->dimen[dim].upper_bound);
2683 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2684 size_binop (PLUS_EXPR, dim_off, stride_suboff));
2685 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2686 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
2687 info->dimen[dim].stride = t;
2688 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
2694 #include "gt-fortran-trans-types.h"