Implement C _FloatN, _FloatNx types.
[official-gcc.git] / gcc / fortran / trans-types.c
blob6a89b30e3bf1121016059b8ab9e228424c98adc3
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* trans-types.c -- gfortran backend types */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "tree.h"
29 #include "gfortran.h"
30 #include "trans.h"
31 #include "stringpool.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "langhooks.h" /* For iso-c-bindings.def. */
35 #include "toplev.h" /* For rest_of_decl_compilation. */
36 #include "trans-types.h"
37 #include "trans-const.h"
38 #include "dwarf2out.h" /* For struct array_descr_info. */
41 #if (GFC_MAX_DIMENSIONS < 10)
42 #define GFC_RANK_DIGITS 1
43 #define GFC_RANK_PRINTF_FORMAT "%01d"
44 #elif (GFC_MAX_DIMENSIONS < 100)
45 #define GFC_RANK_DIGITS 2
46 #define GFC_RANK_PRINTF_FORMAT "%02d"
47 #else
48 #error If you really need >99 dimensions, continue the sequence above...
49 #endif
51 /* array of structs so we don't have to worry about xmalloc or free */
52 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
54 tree gfc_array_index_type;
55 tree gfc_array_range_type;
56 tree gfc_character1_type_node;
57 tree pvoid_type_node;
58 tree prvoid_type_node;
59 tree ppvoid_type_node;
60 tree pchar_type_node;
61 tree pfunc_type_node;
63 tree gfc_charlen_type_node;
65 tree gfc_float128_type_node = NULL_TREE;
66 tree gfc_complex_float128_type_node = NULL_TREE;
68 bool gfc_real16_is_float128 = false;
70 static GTY(()) tree gfc_desc_dim_type;
71 static GTY(()) tree gfc_max_array_element_size;
72 static GTY(()) tree gfc_array_descriptor_base[2 * (GFC_MAX_DIMENSIONS+1)];
73 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
75 /* Arrays for all integral and real kinds. We'll fill this in at runtime
76 after the target has a chance to process command-line options. */
78 #define MAX_INT_KINDS 5
79 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
80 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
81 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
82 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
84 #define MAX_REAL_KINDS 5
85 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
86 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
87 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
89 #define MAX_CHARACTER_KINDS 2
90 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
91 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
92 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
94 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
96 /* The integer kind to use for array indices. This will be set to the
97 proper value based on target information from the backend. */
99 int gfc_index_integer_kind;
101 /* The default kinds of the various types. */
103 int gfc_default_integer_kind;
104 int gfc_max_integer_kind;
105 int gfc_default_real_kind;
106 int gfc_default_double_kind;
107 int gfc_default_character_kind;
108 int gfc_default_logical_kind;
109 int gfc_default_complex_kind;
110 int gfc_c_int_kind;
111 int gfc_atomic_int_kind;
112 int gfc_atomic_logical_kind;
114 /* The kind size used for record offsets. If the target system supports
115 kind=8, this will be set to 8, otherwise it is set to 4. */
116 int gfc_intio_kind;
118 /* The integer kind used to store character lengths. */
119 int gfc_charlen_int_kind;
121 /* The size of the numeric storage unit and character storage unit. */
122 int gfc_numeric_storage_size;
123 int gfc_character_storage_size;
126 bool
127 gfc_check_any_c_kind (gfc_typespec *ts)
129 int i;
131 for (i = 0; i < ISOCBINDING_NUMBER; i++)
133 /* Check for any C interoperable kind for the given type/kind in ts.
134 This can be used after verify_c_interop to make sure that the
135 Fortran kind being used exists in at least some form for C. */
136 if (c_interop_kinds_table[i].f90_type == ts->type &&
137 c_interop_kinds_table[i].value == ts->kind)
138 return true;
141 return false;
145 static int
146 get_real_kind_from_node (tree type)
148 int i;
150 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
151 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
152 return gfc_real_kinds[i].kind;
154 return -4;
157 static int
158 get_int_kind_from_node (tree type)
160 int i;
162 if (!type)
163 return -2;
165 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
166 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
167 return gfc_integer_kinds[i].kind;
169 return -1;
172 /* Return a typenode for the "standard" C type with a given name. */
173 static tree
174 get_typenode_from_name (const char *name)
176 if (name == NULL || *name == '\0')
177 return NULL_TREE;
179 if (strcmp (name, "char") == 0)
180 return char_type_node;
181 if (strcmp (name, "unsigned char") == 0)
182 return unsigned_char_type_node;
183 if (strcmp (name, "signed char") == 0)
184 return signed_char_type_node;
186 if (strcmp (name, "short int") == 0)
187 return short_integer_type_node;
188 if (strcmp (name, "short unsigned int") == 0)
189 return short_unsigned_type_node;
191 if (strcmp (name, "int") == 0)
192 return integer_type_node;
193 if (strcmp (name, "unsigned int") == 0)
194 return unsigned_type_node;
196 if (strcmp (name, "long int") == 0)
197 return long_integer_type_node;
198 if (strcmp (name, "long unsigned int") == 0)
199 return long_unsigned_type_node;
201 if (strcmp (name, "long long int") == 0)
202 return long_long_integer_type_node;
203 if (strcmp (name, "long long unsigned int") == 0)
204 return long_long_unsigned_type_node;
206 gcc_unreachable ();
209 static int
210 get_int_kind_from_name (const char *name)
212 return get_int_kind_from_node (get_typenode_from_name (name));
216 /* Get the kind number corresponding to an integer of given size,
217 following the required return values for ISO_FORTRAN_ENV INT* constants:
218 -2 is returned if we support a kind of larger size, -1 otherwise. */
220 gfc_get_int_kind_from_width_isofortranenv (int size)
222 int i;
224 /* Look for a kind with matching storage size. */
225 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
226 if (gfc_integer_kinds[i].bit_size == size)
227 return gfc_integer_kinds[i].kind;
229 /* Look for a kind with larger storage size. */
230 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
231 if (gfc_integer_kinds[i].bit_size > size)
232 return -2;
234 return -1;
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 gfc_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 gfc_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;
1048 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1049 len = build_fold_indirect_ref (len);
1051 return gfc_get_character_type_len (kind, len);
1054 /* Convert a basic type. This will be an array for character types. */
1056 tree
1057 gfc_typenode_for_spec (gfc_typespec * spec)
1059 tree basetype;
1061 switch (spec->type)
1063 case BT_UNKNOWN:
1064 gcc_unreachable ();
1066 case BT_INTEGER:
1067 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1068 has been resolved. This is done so we can convert C_PTR and
1069 C_FUNPTR to simple variables that get translated to (void *). */
1070 if (spec->f90_type == BT_VOID)
1072 if (spec->u.derived
1073 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1074 basetype = ptr_type_node;
1075 else
1076 basetype = pfunc_type_node;
1078 else
1079 basetype = gfc_get_int_type (spec->kind);
1080 break;
1082 case BT_REAL:
1083 basetype = gfc_get_real_type (spec->kind);
1084 break;
1086 case BT_COMPLEX:
1087 basetype = gfc_get_complex_type (spec->kind);
1088 break;
1090 case BT_LOGICAL:
1091 basetype = gfc_get_logical_type (spec->kind);
1092 break;
1094 case BT_CHARACTER:
1095 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1096 break;
1098 case BT_HOLLERITH:
1099 /* Since this cannot be used, return a length one character. */
1100 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1101 gfc_index_one_node);
1102 break;
1104 case BT_UNION:
1105 basetype = gfc_get_union_type (spec->u.derived);
1106 break;
1108 case BT_DERIVED:
1109 case BT_CLASS:
1110 basetype = gfc_get_derived_type (spec->u.derived);
1112 if (spec->type == BT_CLASS)
1113 GFC_CLASS_TYPE_P (basetype) = 1;
1115 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1116 type and kind to fit a (void *) and the basetype returned was a
1117 ptr_type_node. We need to pass up this new information to the
1118 symbol that was declared of type C_PTR or C_FUNPTR. */
1119 if (spec->u.derived->ts.f90_type == BT_VOID)
1121 spec->type = BT_INTEGER;
1122 spec->kind = gfc_index_integer_kind;
1123 spec->f90_type = BT_VOID;
1125 break;
1126 case BT_VOID:
1127 case BT_ASSUMED:
1128 /* This is for the second arg to c_f_pointer and c_f_procpointer
1129 of the iso_c_binding module, to accept any ptr type. */
1130 basetype = ptr_type_node;
1131 if (spec->f90_type == BT_VOID)
1133 if (spec->u.derived
1134 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1135 basetype = ptr_type_node;
1136 else
1137 basetype = pfunc_type_node;
1139 break;
1140 default:
1141 gcc_unreachable ();
1143 return basetype;
1146 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1148 static tree
1149 gfc_conv_array_bound (gfc_expr * expr)
1151 /* If expr is an integer constant, return that. */
1152 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1153 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1155 /* Otherwise return NULL. */
1156 return NULL_TREE;
1159 /* Return the type of an element of the array. Note that scalar coarrays
1160 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1161 (with POINTER_TYPE stripped) is returned. */
1163 tree
1164 gfc_get_element_type (tree type)
1166 tree element;
1168 if (GFC_ARRAY_TYPE_P (type))
1170 if (TREE_CODE (type) == POINTER_TYPE)
1171 type = TREE_TYPE (type);
1172 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1174 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1175 element = type;
1177 else
1179 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1180 element = TREE_TYPE (type);
1183 else
1185 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1186 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1188 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1189 element = TREE_TYPE (element);
1191 /* For arrays, which are not scalar coarrays. */
1192 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1193 element = TREE_TYPE (element);
1196 return element;
1199 /* Build an array. This function is called from gfc_sym_type().
1200 Actually returns array descriptor type.
1202 Format of array descriptors is as follows:
1204 struct gfc_array_descriptor
1206 array *data
1207 index offset;
1208 index dtype;
1209 struct descriptor_dimension dimension[N_DIM];
1212 struct descriptor_dimension
1214 index stride;
1215 index lbound;
1216 index ubound;
1219 Translation code should use gfc_conv_descriptor_* rather than
1220 accessing the descriptor directly. Any changes to the array
1221 descriptor type will require changes in gfc_conv_descriptor_* and
1222 gfc_build_array_initializer.
1224 This is represented internally as a RECORD_TYPE. The index nodes
1225 are gfc_array_index_type and the data node is a pointer to the
1226 data. See below for the handling of character types.
1228 The dtype member is formatted as follows:
1229 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1230 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1231 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1233 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1234 this generated poor code for assumed/deferred size arrays. These
1235 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1236 of the GENERIC grammar. Also, there is no way to explicitly set
1237 the array stride, so all data must be packed(1). I've tried to
1238 mark all the functions which would require modification with a GCC
1239 ARRAYS comment.
1241 The data component points to the first element in the array. The
1242 offset field is the position of the origin of the array (i.e. element
1243 (0, 0 ...)). This may be outside the bounds of the array.
1245 An element is accessed by
1246 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1247 This gives good performance as the computation does not involve the
1248 bounds of the array. For packed arrays, this is optimized further
1249 by substituting the known strides.
1251 This system has one problem: all array bounds must be within 2^31
1252 elements of the origin (2^63 on 64-bit machines). For example
1253 integer, dimension (80000:90000, 80000:90000, 2) :: array
1254 may not work properly on 32-bit machines because 80000*80000 >
1255 2^31, so the calculation for stride2 would overflow. This may
1256 still work, but I haven't checked, and it relies on the overflow
1257 doing the right thing.
1259 The way to fix this problem is to access elements as follows:
1260 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1261 Obviously this is much slower. I will make this a compile time
1262 option, something like -fsmall-array-offsets. Mixing code compiled
1263 with and without this switch will work.
1265 (1) This can be worked around by modifying the upper bound of the
1266 previous dimension. This requires extra fields in the descriptor
1267 (both real_ubound and fake_ubound). */
1270 /* Returns true if the array sym does not require a descriptor. */
1273 gfc_is_nodesc_array (gfc_symbol * sym)
1275 symbol_attribute *array_attr;
1276 gfc_array_spec *as;
1277 bool is_classarray = IS_CLASS_ARRAY (sym);
1279 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1280 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1282 gcc_assert (array_attr->dimension || array_attr->codimension);
1284 /* We only want local arrays. */
1285 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1286 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1287 || array_attr->allocatable)
1288 return 0;
1290 /* We want a descriptor for associate-name arrays that do not have an
1291 explicitly known shape already. */
1292 if (sym->assoc && as->type != AS_EXPLICIT)
1293 return 0;
1295 /* The dummy is stored in sym and not in the component. */
1296 if (sym->attr.dummy)
1297 return as->type != AS_ASSUMED_SHAPE
1298 && as->type != AS_ASSUMED_RANK;
1300 if (sym->attr.result || sym->attr.function)
1301 return 0;
1303 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1305 return 1;
1309 /* Create an array descriptor type. */
1311 static tree
1312 gfc_build_array_type (tree type, gfc_array_spec * as,
1313 enum gfc_array_kind akind, bool restricted,
1314 bool contiguous)
1316 tree lbound[GFC_MAX_DIMENSIONS];
1317 tree ubound[GFC_MAX_DIMENSIONS];
1318 int n, corank;
1320 /* Assumed-shape arrays do not have codimension information stored in the
1321 descriptor. */
1322 corank = as->corank;
1323 if (as->type == AS_ASSUMED_SHAPE ||
1324 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1325 corank = 0;
1327 if (as->type == AS_ASSUMED_RANK)
1328 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1330 lbound[n] = NULL_TREE;
1331 ubound[n] = NULL_TREE;
1334 for (n = 0; n < as->rank; n++)
1336 /* Create expressions for the known bounds of the array. */
1337 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1338 lbound[n] = gfc_index_one_node;
1339 else
1340 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1341 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1344 for (n = as->rank; n < as->rank + corank; n++)
1346 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1347 lbound[n] = gfc_index_one_node;
1348 else
1349 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1351 if (n < as->rank + corank - 1)
1352 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1355 if (as->type == AS_ASSUMED_SHAPE)
1356 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1357 : GFC_ARRAY_ASSUMED_SHAPE;
1358 else if (as->type == AS_ASSUMED_RANK)
1359 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1360 : GFC_ARRAY_ASSUMED_RANK;
1361 return gfc_get_array_type_bounds (type, as->rank == -1
1362 ? GFC_MAX_DIMENSIONS : as->rank,
1363 corank, lbound,
1364 ubound, 0, akind, restricted);
1367 /* Returns the struct descriptor_dimension type. */
1369 static tree
1370 gfc_get_desc_dim_type (void)
1372 tree type;
1373 tree decl, *chain = NULL;
1375 if (gfc_desc_dim_type)
1376 return gfc_desc_dim_type;
1378 /* Build the type node. */
1379 type = make_node (RECORD_TYPE);
1381 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1382 TYPE_PACKED (type) = 1;
1384 /* Consists of the stride, lbound and ubound members. */
1385 decl = gfc_add_field_to_struct_1 (type,
1386 get_identifier ("stride"),
1387 gfc_array_index_type, &chain);
1388 TREE_NO_WARNING (decl) = 1;
1390 decl = gfc_add_field_to_struct_1 (type,
1391 get_identifier ("lbound"),
1392 gfc_array_index_type, &chain);
1393 TREE_NO_WARNING (decl) = 1;
1395 decl = gfc_add_field_to_struct_1 (type,
1396 get_identifier ("ubound"),
1397 gfc_array_index_type, &chain);
1398 TREE_NO_WARNING (decl) = 1;
1400 /* Finish off the type. */
1401 gfc_finish_type (type);
1402 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1404 gfc_desc_dim_type = type;
1405 return type;
1409 /* Return the DTYPE for an array. This describes the type and type parameters
1410 of the array. */
1411 /* TODO: Only call this when the value is actually used, and make all the
1412 unknown cases abort. */
1414 tree
1415 gfc_get_dtype_rank_type (int rank, tree etype)
1417 tree size;
1418 int n;
1419 HOST_WIDE_INT i;
1420 tree tmp;
1421 tree dtype;
1423 switch (TREE_CODE (etype))
1425 case INTEGER_TYPE:
1426 n = BT_INTEGER;
1427 break;
1429 case BOOLEAN_TYPE:
1430 n = BT_LOGICAL;
1431 break;
1433 case REAL_TYPE:
1434 n = BT_REAL;
1435 break;
1437 case COMPLEX_TYPE:
1438 n = BT_COMPLEX;
1439 break;
1441 /* We will never have arrays of arrays. */
1442 case RECORD_TYPE:
1443 n = BT_DERIVED;
1444 break;
1446 case ARRAY_TYPE:
1447 n = BT_CHARACTER;
1448 break;
1450 case POINTER_TYPE:
1451 n = BT_ASSUMED;
1452 break;
1454 default:
1455 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1456 /* We can strange array types for temporary arrays. */
1457 return gfc_index_zero_node;
1460 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1461 size = TYPE_SIZE_UNIT (etype);
1463 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1464 if (size && INTEGER_CST_P (size))
1466 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1467 gfc_fatal_error ("Array element size too big at %C");
1469 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1471 dtype = build_int_cst (gfc_array_index_type, i);
1473 if (size && !INTEGER_CST_P (size))
1475 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1476 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1477 gfc_array_index_type,
1478 fold_convert (gfc_array_index_type, size), tmp);
1479 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1480 tmp, dtype);
1482 /* If we don't know the size we leave it as zero. This should never happen
1483 for anything that is actually used. */
1484 /* TODO: Check this is actually true, particularly when repacking
1485 assumed size parameters. */
1487 return dtype;
1491 tree
1492 gfc_get_dtype (tree type)
1494 tree dtype;
1495 tree etype;
1496 int rank;
1498 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1500 if (GFC_TYPE_ARRAY_DTYPE (type))
1501 return GFC_TYPE_ARRAY_DTYPE (type);
1503 rank = GFC_TYPE_ARRAY_RANK (type);
1504 etype = gfc_get_element_type (type);
1505 dtype = gfc_get_dtype_rank_type (rank, etype);
1507 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1508 return dtype;
1512 /* Build an array type for use without a descriptor, packed according
1513 to the value of PACKED. */
1515 tree
1516 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1517 bool restricted)
1519 tree range;
1520 tree type;
1521 tree tmp;
1522 int n;
1523 int known_stride;
1524 int known_offset;
1525 mpz_t offset;
1526 mpz_t stride;
1527 mpz_t delta;
1528 gfc_expr *expr;
1530 mpz_init_set_ui (offset, 0);
1531 mpz_init_set_ui (stride, 1);
1532 mpz_init (delta);
1534 /* We don't use build_array_type because this does not include include
1535 lang-specific information (i.e. the bounds of the array) when checking
1536 for duplicates. */
1537 if (as->rank)
1538 type = make_node (ARRAY_TYPE);
1539 else
1540 type = build_variant_type_copy (etype);
1542 GFC_ARRAY_TYPE_P (type) = 1;
1543 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1545 known_stride = (packed != PACKED_NO);
1546 known_offset = 1;
1547 for (n = 0; n < as->rank; n++)
1549 /* Fill in the stride and bound components of the type. */
1550 if (known_stride)
1551 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1552 else
1553 tmp = NULL_TREE;
1554 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1556 expr = as->lower[n];
1557 if (expr->expr_type == EXPR_CONSTANT)
1559 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1560 gfc_index_integer_kind);
1562 else
1564 known_stride = 0;
1565 tmp = NULL_TREE;
1567 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1569 if (known_stride)
1571 /* Calculate the offset. */
1572 mpz_mul (delta, stride, as->lower[n]->value.integer);
1573 mpz_sub (offset, offset, delta);
1575 else
1576 known_offset = 0;
1578 expr = as->upper[n];
1579 if (expr && expr->expr_type == EXPR_CONSTANT)
1581 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1582 gfc_index_integer_kind);
1584 else
1586 tmp = NULL_TREE;
1587 known_stride = 0;
1589 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1591 if (known_stride)
1593 /* Calculate the stride. */
1594 mpz_sub (delta, as->upper[n]->value.integer,
1595 as->lower[n]->value.integer);
1596 mpz_add_ui (delta, delta, 1);
1597 mpz_mul (stride, stride, delta);
1600 /* Only the first stride is known for partial packed arrays. */
1601 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1602 known_stride = 0;
1604 for (n = as->rank; n < as->rank + as->corank; n++)
1606 expr = as->lower[n];
1607 if (expr->expr_type == EXPR_CONSTANT)
1608 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1609 gfc_index_integer_kind);
1610 else
1611 tmp = NULL_TREE;
1612 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1614 expr = as->upper[n];
1615 if (expr && expr->expr_type == EXPR_CONSTANT)
1616 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1617 gfc_index_integer_kind);
1618 else
1619 tmp = NULL_TREE;
1620 if (n < as->rank + as->corank - 1)
1621 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1624 if (known_offset)
1626 GFC_TYPE_ARRAY_OFFSET (type) =
1627 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1629 else
1630 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1632 if (known_stride)
1634 GFC_TYPE_ARRAY_SIZE (type) =
1635 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1637 else
1638 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1640 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1641 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1642 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1643 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1644 NULL_TREE);
1645 /* TODO: use main type if it is unbounded. */
1646 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1647 build_pointer_type (build_array_type (etype, range));
1648 if (restricted)
1649 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1650 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1651 TYPE_QUAL_RESTRICT);
1653 if (as->rank == 0)
1655 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1657 type = build_pointer_type (type);
1659 if (restricted)
1660 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1662 GFC_ARRAY_TYPE_P (type) = 1;
1663 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1666 return type;
1669 if (known_stride)
1671 mpz_sub_ui (stride, stride, 1);
1672 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1674 else
1675 range = NULL_TREE;
1677 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1678 TYPE_DOMAIN (type) = range;
1680 build_pointer_type (etype);
1681 TREE_TYPE (type) = etype;
1683 layout_type (type);
1685 mpz_clear (offset);
1686 mpz_clear (stride);
1687 mpz_clear (delta);
1689 /* Represent packed arrays as multi-dimensional if they have rank >
1690 1 and with proper bounds, instead of flat arrays. This makes for
1691 better debug info. */
1692 if (known_offset)
1694 tree gtype = etype, rtype, type_decl;
1696 for (n = as->rank - 1; n >= 0; n--)
1698 rtype = build_range_type (gfc_array_index_type,
1699 GFC_TYPE_ARRAY_LBOUND (type, n),
1700 GFC_TYPE_ARRAY_UBOUND (type, n));
1701 gtype = build_array_type (gtype, rtype);
1703 TYPE_NAME (type) = type_decl = build_decl (input_location,
1704 TYPE_DECL, NULL, gtype);
1705 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1708 if (packed != PACKED_STATIC || !known_stride
1709 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1711 /* For dummy arrays and automatic (heap allocated) arrays we
1712 want a pointer to the array. */
1713 type = build_pointer_type (type);
1714 if (restricted)
1715 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1716 GFC_ARRAY_TYPE_P (type) = 1;
1717 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1719 return type;
1723 /* Return or create the base type for an array descriptor. */
1725 static tree
1726 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted,
1727 enum gfc_array_kind akind)
1729 tree fat_type, decl, arraytype, *chain = NULL;
1730 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1731 int idx;
1733 /* Assumed-rank array. */
1734 if (dimen == -1)
1735 dimen = GFC_MAX_DIMENSIONS;
1737 idx = 2 * (codimen + dimen) + restricted;
1739 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1741 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1743 if (gfc_array_descriptor_base_caf[idx])
1744 return gfc_array_descriptor_base_caf[idx];
1746 else if (gfc_array_descriptor_base[idx])
1747 return gfc_array_descriptor_base[idx];
1749 /* Build the type node. */
1750 fat_type = make_node (RECORD_TYPE);
1752 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1753 TYPE_NAME (fat_type) = get_identifier (name);
1754 TYPE_NAMELESS (fat_type) = 1;
1756 /* Add the data member as the first element of the descriptor. */
1757 decl = gfc_add_field_to_struct_1 (fat_type,
1758 get_identifier ("data"),
1759 (restricted
1760 ? prvoid_type_node
1761 : ptr_type_node), &chain);
1763 /* Add the base component. */
1764 decl = gfc_add_field_to_struct_1 (fat_type,
1765 get_identifier ("offset"),
1766 gfc_array_index_type, &chain);
1767 TREE_NO_WARNING (decl) = 1;
1769 /* Add the dtype component. */
1770 decl = gfc_add_field_to_struct_1 (fat_type,
1771 get_identifier ("dtype"),
1772 gfc_array_index_type, &chain);
1773 TREE_NO_WARNING (decl) = 1;
1775 /* Build the array type for the stride and bound components. */
1776 if (dimen + codimen > 0)
1778 arraytype =
1779 build_array_type (gfc_get_desc_dim_type (),
1780 build_range_type (gfc_array_index_type,
1781 gfc_index_zero_node,
1782 gfc_rank_cst[codimen + dimen - 1]));
1784 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1785 arraytype, &chain);
1786 TREE_NO_WARNING (decl) = 1;
1789 if (flag_coarray == GFC_FCOARRAY_LIB && codimen
1790 && akind == GFC_ARRAY_ALLOCATABLE)
1792 decl = gfc_add_field_to_struct_1 (fat_type,
1793 get_identifier ("token"),
1794 prvoid_type_node, &chain);
1795 TREE_NO_WARNING (decl) = 1;
1798 /* Finish off the type. */
1799 gfc_finish_type (fat_type);
1800 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1802 if (flag_coarray == GFC_FCOARRAY_LIB && codimen
1803 && akind == GFC_ARRAY_ALLOCATABLE)
1804 gfc_array_descriptor_base_caf[idx] = fat_type;
1805 else
1806 gfc_array_descriptor_base[idx] = fat_type;
1808 return fat_type;
1812 /* Build an array (descriptor) type with given bounds. */
1814 tree
1815 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1816 tree * ubound, int packed,
1817 enum gfc_array_kind akind, bool restricted)
1819 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1820 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1821 const char *type_name;
1822 int n;
1824 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted, akind);
1825 fat_type = build_distinct_type_copy (base_type);
1826 /* Make sure that nontarget and target array type have the same canonical
1827 type (and same stub decl for debug info). */
1828 base_type = gfc_get_array_descriptor_base (dimen, codimen, false, akind);
1829 TYPE_CANONICAL (fat_type) = base_type;
1830 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1832 tmp = TYPE_NAME (etype);
1833 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1834 tmp = DECL_NAME (tmp);
1835 if (tmp)
1836 type_name = IDENTIFIER_POINTER (tmp);
1837 else
1838 type_name = "unknown";
1839 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1840 GFC_MAX_SYMBOL_LEN, type_name);
1841 TYPE_NAME (fat_type) = get_identifier (name);
1842 TYPE_NAMELESS (fat_type) = 1;
1844 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1845 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1847 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1848 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1849 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1850 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1852 /* Build an array descriptor record type. */
1853 if (packed != 0)
1854 stride = gfc_index_one_node;
1855 else
1856 stride = NULL_TREE;
1857 for (n = 0; n < dimen + codimen; n++)
1859 if (n < dimen)
1860 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1862 if (lbound)
1863 lower = lbound[n];
1864 else
1865 lower = NULL_TREE;
1867 if (lower != NULL_TREE)
1869 if (INTEGER_CST_P (lower))
1870 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1871 else
1872 lower = NULL_TREE;
1875 if (codimen && n == dimen + codimen - 1)
1876 break;
1878 upper = ubound[n];
1879 if (upper != NULL_TREE)
1881 if (INTEGER_CST_P (upper))
1882 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1883 else
1884 upper = NULL_TREE;
1887 if (n >= dimen)
1888 continue;
1890 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1892 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1893 gfc_array_index_type, upper, lower);
1894 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1895 gfc_array_index_type, tmp,
1896 gfc_index_one_node);
1897 stride = fold_build2_loc (input_location, MULT_EXPR,
1898 gfc_array_index_type, tmp, stride);
1899 /* Check the folding worked. */
1900 gcc_assert (INTEGER_CST_P (stride));
1902 else
1903 stride = NULL_TREE;
1905 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1907 /* TODO: known offsets for descriptors. */
1908 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1910 if (dimen == 0)
1912 arraytype = build_pointer_type (etype);
1913 if (restricted)
1914 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1916 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1917 return fat_type;
1920 /* We define data as an array with the correct size if possible.
1921 Much better than doing pointer arithmetic. */
1922 if (stride)
1923 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1924 int_const_binop (MINUS_EXPR, stride,
1925 build_int_cst (TREE_TYPE (stride), 1)));
1926 else
1927 rtype = gfc_array_range_type;
1928 arraytype = build_array_type (etype, rtype);
1929 arraytype = build_pointer_type (arraytype);
1930 if (restricted)
1931 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1932 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1934 /* This will generate the base declarations we need to emit debug
1935 information for this type. FIXME: there must be a better way to
1936 avoid divergence between compilations with and without debug
1937 information. */
1939 struct array_descr_info info;
1940 gfc_get_array_descr_info (fat_type, &info);
1941 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1944 return fat_type;
1947 /* Build a pointer type. This function is called from gfc_sym_type(). */
1949 static tree
1950 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1952 /* Array pointer types aren't actually pointers. */
1953 if (sym->attr.dimension)
1954 return type;
1955 else
1956 return build_pointer_type (type);
1959 static tree gfc_nonrestricted_type (tree t);
1960 /* Given two record or union type nodes TO and FROM, ensure
1961 that all fields in FROM have a corresponding field in TO,
1962 their type being nonrestrict variants. This accepts a TO
1963 node that already has a prefix of the fields in FROM. */
1964 static void
1965 mirror_fields (tree to, tree from)
1967 tree fto, ffrom;
1968 tree *chain;
1970 /* Forward to the end of TOs fields. */
1971 fto = TYPE_FIELDS (to);
1972 ffrom = TYPE_FIELDS (from);
1973 chain = &TYPE_FIELDS (to);
1974 while (fto)
1976 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
1977 chain = &DECL_CHAIN (fto);
1978 fto = DECL_CHAIN (fto);
1979 ffrom = DECL_CHAIN (ffrom);
1982 /* Now add all fields remaining in FROM (starting with ffrom). */
1983 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
1985 tree newfield = copy_node (ffrom);
1986 DECL_CONTEXT (newfield) = to;
1987 /* The store to DECL_CHAIN might seem redundant with the
1988 stores to *chain, but not clearing it here would mean
1989 leaving a chain into the old fields. If ever
1990 our called functions would look at them confusion
1991 will arise. */
1992 DECL_CHAIN (newfield) = NULL_TREE;
1993 *chain = newfield;
1994 chain = &DECL_CHAIN (newfield);
1996 if (TREE_CODE (ffrom) == FIELD_DECL)
1998 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
1999 TREE_TYPE (newfield) = elemtype;
2002 *chain = NULL_TREE;
2005 /* Given a type T, returns a different type of the same structure,
2006 except that all types it refers to (recursively) are always
2007 non-restrict qualified types. */
2008 static tree
2009 gfc_nonrestricted_type (tree t)
2011 tree ret = t;
2013 /* If the type isn't laid out yet, don't copy it. If something
2014 needs it for real it should wait until the type got finished. */
2015 if (!TYPE_SIZE (t))
2016 return t;
2018 if (!TYPE_LANG_SPECIFIC (t))
2019 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2020 /* If we're dealing with this very node already further up
2021 the call chain (recursion via pointers and struct members)
2022 we haven't yet determined if we really need a new type node.
2023 Assume we don't, return T itself. */
2024 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2025 return t;
2027 /* If we have calculated this all already, just return it. */
2028 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2029 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2031 /* Mark this type. */
2032 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2034 switch (TREE_CODE (t))
2036 default:
2037 break;
2039 case POINTER_TYPE:
2040 case REFERENCE_TYPE:
2042 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2043 if (totype == TREE_TYPE (t))
2044 ret = t;
2045 else if (TREE_CODE (t) == POINTER_TYPE)
2046 ret = build_pointer_type (totype);
2047 else
2048 ret = build_reference_type (totype);
2049 ret = build_qualified_type (ret,
2050 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2052 break;
2054 case ARRAY_TYPE:
2056 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2057 if (elemtype == TREE_TYPE (t))
2058 ret = t;
2059 else
2061 ret = build_variant_type_copy (t);
2062 TREE_TYPE (ret) = elemtype;
2063 if (TYPE_LANG_SPECIFIC (t)
2064 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2066 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2067 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2068 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2070 TYPE_LANG_SPECIFIC (ret)
2071 = ggc_cleared_alloc<struct lang_type> ();
2072 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2073 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2078 break;
2080 case RECORD_TYPE:
2081 case UNION_TYPE:
2082 case QUAL_UNION_TYPE:
2084 tree field;
2085 /* First determine if we need a new type at all.
2086 Careful, the two calls to gfc_nonrestricted_type per field
2087 might return different values. That happens exactly when
2088 one of the fields reaches back to this very record type
2089 (via pointers). The first calls will assume that we don't
2090 need to copy T (see the error_mark_node marking). If there
2091 are any reasons for copying T apart from having to copy T,
2092 we'll indeed copy it, and the second calls to
2093 gfc_nonrestricted_type will use that new node if they
2094 reach back to T. */
2095 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2096 if (TREE_CODE (field) == FIELD_DECL)
2098 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2099 if (elemtype != TREE_TYPE (field))
2100 break;
2102 if (!field)
2103 break;
2104 ret = build_variant_type_copy (t);
2105 TYPE_FIELDS (ret) = NULL_TREE;
2107 /* Here we make sure that as soon as we know we have to copy
2108 T, that also fields reaching back to us will use the new
2109 copy. It's okay if that copy still contains the old fields,
2110 we won't look at them. */
2111 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2112 mirror_fields (ret, t);
2114 break;
2117 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2118 return ret;
2122 /* Return the type for a symbol. Special handling is required for character
2123 types to get the correct level of indirection.
2124 For functions return the return type.
2125 For subroutines return void_type_node.
2126 Calling this multiple times for the same symbol should be avoided,
2127 especially for character and array types. */
2129 tree
2130 gfc_sym_type (gfc_symbol * sym)
2132 tree type;
2133 int byref;
2134 bool restricted;
2136 /* Procedure Pointers inside COMMON blocks. */
2137 if (sym->attr.proc_pointer && sym->attr.in_common)
2139 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2140 sym->attr.proc_pointer = 0;
2141 type = build_pointer_type (gfc_get_function_type (sym));
2142 sym->attr.proc_pointer = 1;
2143 return type;
2146 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2147 return void_type_node;
2149 /* In the case of a function the fake result variable may have a
2150 type different from the function type, so don't return early in
2151 that case. */
2152 if (sym->backend_decl && !sym->attr.function)
2153 return TREE_TYPE (sym->backend_decl);
2155 if (sym->ts.type == BT_CHARACTER
2156 && ((sym->attr.function && sym->attr.is_bind_c)
2157 || (sym->attr.result
2158 && sym->ns->proc_name
2159 && sym->ns->proc_name->attr.is_bind_c)
2160 || (sym->ts.deferred && (!sym->ts.u.cl
2161 || !sym->ts.u.cl->backend_decl))))
2162 type = gfc_character1_type_node;
2163 else
2164 type = gfc_typenode_for_spec (&sym->ts);
2166 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2167 byref = 1;
2168 else
2169 byref = 0;
2171 restricted = !sym->attr.target && !sym->attr.pointer
2172 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2173 if (!restricted)
2174 type = gfc_nonrestricted_type (type);
2176 if (sym->attr.dimension || sym->attr.codimension)
2178 if (gfc_is_nodesc_array (sym))
2180 /* If this is a character argument of unknown length, just use the
2181 base type. */
2182 if (sym->ts.type != BT_CHARACTER
2183 || !(sym->attr.dummy || sym->attr.function)
2184 || sym->ts.u.cl->backend_decl)
2186 type = gfc_get_nodesc_array_type (type, sym->as,
2187 byref ? PACKED_FULL
2188 : PACKED_STATIC,
2189 restricted);
2190 byref = 0;
2193 else
2195 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2196 if (sym->attr.pointer)
2197 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2198 : GFC_ARRAY_POINTER;
2199 else if (sym->attr.allocatable)
2200 akind = GFC_ARRAY_ALLOCATABLE;
2201 type = gfc_build_array_type (type, sym->as, akind, restricted,
2202 sym->attr.contiguous);
2205 else
2207 if (sym->attr.allocatable || sym->attr.pointer
2208 || gfc_is_associate_pointer (sym))
2209 type = gfc_build_pointer_type (sym, type);
2212 /* We currently pass all parameters by reference.
2213 See f95_get_function_decl. For dummy function parameters return the
2214 function type. */
2215 if (byref)
2217 /* We must use pointer types for potentially absent variables. The
2218 optimizers assume a reference type argument is never NULL. */
2219 if (sym->attr.optional
2220 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2221 type = build_pointer_type (type);
2222 else
2224 type = build_reference_type (type);
2225 if (restricted)
2226 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2230 return (type);
2233 /* Layout and output debug info for a record type. */
2235 void
2236 gfc_finish_type (tree type)
2238 tree decl;
2240 decl = build_decl (input_location,
2241 TYPE_DECL, NULL_TREE, type);
2242 TYPE_STUB_DECL (type) = decl;
2243 layout_type (type);
2244 rest_of_type_compilation (type, 1);
2245 rest_of_decl_compilation (decl, 1, 0);
2248 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2249 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2250 to the end of the field list pointed to by *CHAIN.
2252 Returns a pointer to the new field. */
2254 static tree
2255 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2257 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2259 DECL_CONTEXT (decl) = context;
2260 DECL_CHAIN (decl) = NULL_TREE;
2261 if (TYPE_FIELDS (context) == NULL_TREE)
2262 TYPE_FIELDS (context) = decl;
2263 if (chain != NULL)
2265 if (*chain != NULL)
2266 **chain = decl;
2267 *chain = &DECL_CHAIN (decl);
2270 return decl;
2273 /* Like `gfc_add_field_to_struct_1', but adds alignment
2274 information. */
2276 tree
2277 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2279 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2281 DECL_INITIAL (decl) = 0;
2282 SET_DECL_ALIGN (decl, 0);
2283 DECL_USER_ALIGN (decl) = 0;
2285 return decl;
2289 /* Copy the backend_decl and component backend_decls if
2290 the two derived type symbols are "equal", as described
2291 in 4.4.2 and resolved by gfc_compare_derived_types. */
2294 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2295 bool from_gsym)
2297 gfc_component *to_cm;
2298 gfc_component *from_cm;
2300 if (from == to)
2301 return 1;
2303 if (from->backend_decl == NULL
2304 || !gfc_compare_derived_types (from, to))
2305 return 0;
2307 to->backend_decl = from->backend_decl;
2309 to_cm = to->components;
2310 from_cm = from->components;
2312 /* Copy the component declarations. If a component is itself
2313 a derived type, we need a copy of its component declarations.
2314 This is done by recursing into gfc_get_derived_type and
2315 ensures that the component's component declarations have
2316 been built. If it is a character, we need the character
2317 length, as well. */
2318 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2320 to_cm->backend_decl = from_cm->backend_decl;
2321 if (from_cm->ts.type == BT_UNION)
2322 gfc_get_union_type (to_cm->ts.u.derived);
2323 else if (from_cm->ts.type == BT_DERIVED
2324 && (!from_cm->attr.pointer || from_gsym))
2325 gfc_get_derived_type (to_cm->ts.u.derived);
2326 else if (from_cm->ts.type == BT_CLASS
2327 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2328 gfc_get_derived_type (to_cm->ts.u.derived);
2329 else if (from_cm->ts.type == BT_CHARACTER)
2330 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2333 return 1;
2337 /* Build a tree node for a procedure pointer component. */
2339 tree
2340 gfc_get_ppc_type (gfc_component* c)
2342 tree t;
2344 /* Explicit interface. */
2345 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2346 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2348 /* Implicit interface (only return value may be known). */
2349 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2350 t = gfc_typenode_for_spec (&c->ts);
2351 else
2352 t = void_type_node;
2354 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2358 /* Build a tree node for a union type. Requires building each map
2359 structure which is an element of the union. */
2361 tree
2362 gfc_get_union_type (gfc_symbol *un)
2364 gfc_component *map = NULL;
2365 tree typenode = NULL, map_type = NULL, map_field = NULL;
2366 tree *chain = NULL;
2368 if (un->backend_decl)
2370 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2371 return un->backend_decl;
2372 else
2373 typenode = un->backend_decl;
2375 else
2377 typenode = make_node (UNION_TYPE);
2378 TYPE_NAME (typenode) = get_identifier (un->name);
2381 /* Add each contained MAP as a field. */
2382 for (map = un->components; map; map = map->next)
2384 gcc_assert (map->ts.type == BT_DERIVED);
2386 /* The map's type node, which is defined within this union's context. */
2387 map_type = gfc_get_derived_type (map->ts.u.derived);
2388 TYPE_CONTEXT (map_type) = typenode;
2390 /* The map field's declaration. */
2391 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2392 map_type, &chain);
2393 if (map->loc.lb)
2394 gfc_set_decl_location (map_field, &map->loc);
2395 else if (un->declared_at.lb)
2396 gfc_set_decl_location (map_field, &un->declared_at);
2398 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2399 DECL_NAMELESS(map_field) = true;
2401 /* We should never clobber another backend declaration for this map,
2402 because each map component is unique. */
2403 if (!map->backend_decl)
2404 map->backend_decl = map_field;
2407 un->backend_decl = typenode;
2408 gfc_finish_type (typenode);
2410 return typenode;
2414 /* Build a tree node for a derived type. If there are equal
2415 derived types, with different local names, these are built
2416 at the same time. If an equal derived type has been built
2417 in a parent namespace, this is used. */
2419 tree
2420 gfc_get_derived_type (gfc_symbol * derived)
2422 tree typenode = NULL, field = NULL, field_type = NULL;
2423 tree canonical = NULL_TREE;
2424 tree *chain = NULL;
2425 bool got_canonical = false;
2426 bool unlimited_entity = false;
2427 gfc_component *c;
2428 gfc_dt_list *dt;
2429 gfc_namespace *ns;
2430 tree tmp;
2432 if (derived->attr.unlimited_polymorphic
2433 || (flag_coarray == GFC_FCOARRAY_LIB
2434 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2435 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2436 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
2437 return ptr_type_node;
2439 if (flag_coarray != GFC_FCOARRAY_LIB
2440 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2441 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2442 return gfc_get_int_type (gfc_default_integer_kind);
2444 if (derived && derived->attr.flavor == FL_PROCEDURE
2445 && derived->attr.generic)
2446 derived = gfc_find_dt_in_generic (derived);
2448 /* See if it's one of the iso_c_binding derived types. */
2449 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2451 if (derived->backend_decl)
2452 return derived->backend_decl;
2454 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2455 derived->backend_decl = ptr_type_node;
2456 else
2457 derived->backend_decl = pfunc_type_node;
2459 derived->ts.kind = gfc_index_integer_kind;
2460 derived->ts.type = BT_INTEGER;
2461 /* Set the f90_type to BT_VOID as a way to recognize something of type
2462 BT_INTEGER that needs to fit a void * for the purpose of the
2463 iso_c_binding derived types. */
2464 derived->ts.f90_type = BT_VOID;
2466 return derived->backend_decl;
2469 /* If use associated, use the module type for this one. */
2470 if (derived->backend_decl == NULL
2471 && derived->attr.use_assoc
2472 && derived->module
2473 && gfc_get_module_backend_decl (derived))
2474 goto copy_derived_types;
2476 /* The derived types from an earlier namespace can be used as the
2477 canonical type. */
2478 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2479 && gfc_global_ns_list)
2481 for (ns = gfc_global_ns_list;
2482 ns->translated && !got_canonical;
2483 ns = ns->sibling)
2485 dt = ns->derived_types;
2486 for (; dt && !canonical; dt = dt->next)
2488 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2489 if (derived->backend_decl)
2490 got_canonical = true;
2495 /* Store up the canonical type to be added to this one. */
2496 if (got_canonical)
2498 if (TYPE_CANONICAL (derived->backend_decl))
2499 canonical = TYPE_CANONICAL (derived->backend_decl);
2500 else
2501 canonical = derived->backend_decl;
2503 derived->backend_decl = NULL_TREE;
2506 /* derived->backend_decl != 0 means we saw it before, but its
2507 components' backend_decl may have not been built. */
2508 if (derived->backend_decl)
2510 /* Its components' backend_decl have been built or we are
2511 seeing recursion through the formal arglist of a procedure
2512 pointer component. */
2513 if (TYPE_FIELDS (derived->backend_decl))
2514 return derived->backend_decl;
2515 else if (derived->attr.abstract
2516 && derived->attr.proc_pointer_comp)
2518 /* If an abstract derived type with procedure pointer
2519 components has no other type of component, return the
2520 backend_decl. Otherwise build the components if any of the
2521 non-procedure pointer components have no backend_decl. */
2522 for (c = derived->components; c; c = c->next)
2524 if (!c->attr.proc_pointer && c->backend_decl == NULL)
2525 break;
2526 else if (c->next == NULL)
2527 return derived->backend_decl;
2529 typenode = derived->backend_decl;
2531 else
2532 typenode = derived->backend_decl;
2534 else
2536 /* We see this derived type first time, so build the type node. */
2537 typenode = make_node (RECORD_TYPE);
2538 TYPE_NAME (typenode) = get_identifier (derived->name);
2539 TYPE_PACKED (typenode) = flag_pack_derived;
2540 derived->backend_decl = typenode;
2543 if (derived->components
2544 && derived->components->ts.type == BT_DERIVED
2545 && strcmp (derived->components->name, "_data") == 0
2546 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2547 unlimited_entity = true;
2549 /* Go through the derived type components, building them as
2550 necessary. The reason for doing this now is that it is
2551 possible to recurse back to this derived type through a
2552 pointer component (PR24092). If this happens, the fields
2553 will be built and so we can return the type. */
2554 for (c = derived->components; c; c = c->next)
2556 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2557 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2559 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2560 continue;
2562 if ((!c->attr.pointer && !c->attr.proc_pointer)
2563 || c->ts.u.derived->backend_decl == NULL)
2564 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
2566 if (c->ts.u.derived->attr.is_iso_c)
2568 /* Need to copy the modified ts from the derived type. The
2569 typespec was modified because C_PTR/C_FUNPTR are translated
2570 into (void *) from derived types. */
2571 c->ts.type = c->ts.u.derived->ts.type;
2572 c->ts.kind = c->ts.u.derived->ts.kind;
2573 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2574 if (c->initializer)
2576 c->initializer->ts.type = c->ts.type;
2577 c->initializer->ts.kind = c->ts.kind;
2578 c->initializer->ts.f90_type = c->ts.f90_type;
2579 c->initializer->expr_type = EXPR_NULL;
2584 if (TYPE_FIELDS (derived->backend_decl))
2585 return derived->backend_decl;
2587 /* Build the type member list. Install the newly created RECORD_TYPE
2588 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2589 through only the top-level linked list of components so we correctly
2590 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2591 types are built as part of gfc_get_union_type. */
2592 for (c = derived->components; c; c = c->next)
2594 /* Prevent infinite recursion, when the procedure pointer type is
2595 the same as derived, by forcing the procedure pointer component to
2596 be built as if the explicit interface does not exist. */
2597 if (c->attr.proc_pointer
2598 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2599 || (c->ts.u.derived
2600 && !gfc_compare_derived_types (derived, c->ts.u.derived))))
2601 field_type = gfc_get_ppc_type (c);
2602 else if (c->attr.proc_pointer && derived->backend_decl)
2604 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2605 field_type = build_pointer_type (tmp);
2607 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2608 field_type = c->ts.u.derived->backend_decl;
2609 else
2611 if (c->ts.type == BT_CHARACTER && !c->ts.deferred)
2613 /* Evaluate the string length. */
2614 gfc_conv_const_charlen (c->ts.u.cl);
2615 gcc_assert (c->ts.u.cl->backend_decl);
2617 else if (c->ts.type == BT_CHARACTER)
2618 c->ts.u.cl->backend_decl
2619 = build_int_cst (gfc_charlen_type_node, 0);
2621 field_type = gfc_typenode_for_spec (&c->ts);
2624 /* This returns an array descriptor type. Initialization may be
2625 required. */
2626 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2628 if (c->attr.pointer || c->attr.allocatable)
2630 enum gfc_array_kind akind;
2631 if (c->attr.pointer)
2632 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2633 : GFC_ARRAY_POINTER;
2634 else
2635 akind = GFC_ARRAY_ALLOCATABLE;
2636 /* Pointers to arrays aren't actually pointer types. The
2637 descriptors are separate, but the data is common. */
2638 field_type = gfc_build_array_type (field_type, c->as, akind,
2639 !c->attr.target
2640 && !c->attr.pointer,
2641 c->attr.contiguous);
2643 else
2644 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2645 PACKED_STATIC,
2646 !c->attr.target);
2648 else if ((c->attr.pointer || c->attr.allocatable)
2649 && !c->attr.proc_pointer
2650 && !(unlimited_entity && c == derived->components))
2651 field_type = build_pointer_type (field_type);
2653 if (c->attr.pointer)
2654 field_type = gfc_nonrestricted_type (field_type);
2656 /* vtype fields can point to different types to the base type. */
2657 if (c->ts.type == BT_DERIVED
2658 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2659 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2660 ptr_mode, true);
2662 /* Ensure that the CLASS language specific flag is set. */
2663 if (c->ts.type == BT_CLASS)
2665 if (POINTER_TYPE_P (field_type))
2666 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2667 else
2668 GFC_CLASS_TYPE_P (field_type) = 1;
2671 field = gfc_add_field_to_struct (typenode,
2672 get_identifier (c->name),
2673 field_type, &chain);
2674 if (c->loc.lb)
2675 gfc_set_decl_location (field, &c->loc);
2676 else if (derived->declared_at.lb)
2677 gfc_set_decl_location (field, &derived->declared_at);
2679 gfc_finish_decl_attrs (field, &c->attr);
2681 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2683 gcc_assert (field);
2684 if (!c->backend_decl)
2685 c->backend_decl = field;
2688 /* Now lay out the derived type, including the fields. */
2689 if (canonical)
2690 TYPE_CANONICAL (typenode) = canonical;
2692 gfc_finish_type (typenode);
2693 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2694 if (derived->module && derived->ns->proc_name
2695 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2697 if (derived->ns->proc_name->backend_decl
2698 && TREE_CODE (derived->ns->proc_name->backend_decl)
2699 == NAMESPACE_DECL)
2701 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2702 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2703 = derived->ns->proc_name->backend_decl;
2707 derived->backend_decl = typenode;
2709 copy_derived_types:
2711 for (dt = gfc_derived_types; dt; dt = dt->next)
2712 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2714 return derived->backend_decl;
2719 gfc_return_by_reference (gfc_symbol * sym)
2721 if (!sym->attr.function)
2722 return 0;
2724 if (sym->attr.dimension)
2725 return 1;
2727 if (sym->ts.type == BT_CHARACTER
2728 && !sym->attr.is_bind_c
2729 && (!sym->attr.result
2730 || !sym->ns->proc_name
2731 || !sym->ns->proc_name->attr.is_bind_c))
2732 return 1;
2734 /* Possibly return complex numbers by reference for g77 compatibility.
2735 We don't do this for calls to intrinsics (as the library uses the
2736 -fno-f2c calling convention), nor for calls to functions which always
2737 require an explicit interface, as no compatibility problems can
2738 arise there. */
2739 if (flag_f2c && sym->ts.type == BT_COMPLEX
2740 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2741 return 1;
2743 return 0;
2746 static tree
2747 gfc_get_mixed_entry_union (gfc_namespace *ns)
2749 tree type;
2750 tree *chain = NULL;
2751 char name[GFC_MAX_SYMBOL_LEN + 1];
2752 gfc_entry_list *el, *el2;
2754 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2755 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2757 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2759 /* Build the type node. */
2760 type = make_node (UNION_TYPE);
2762 TYPE_NAME (type) = get_identifier (name);
2764 for (el = ns->entries; el; el = el->next)
2766 /* Search for duplicates. */
2767 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2768 if (el2->sym->result == el->sym->result)
2769 break;
2771 if (el == el2)
2772 gfc_add_field_to_struct_1 (type,
2773 get_identifier (el->sym->result->name),
2774 gfc_sym_type (el->sym->result), &chain);
2777 /* Finish off the type. */
2778 gfc_finish_type (type);
2779 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2780 return type;
2783 /* Create a "fn spec" based on the formal arguments;
2784 cf. create_function_arglist. */
2786 static tree
2787 create_fn_spec (gfc_symbol *sym, tree fntype)
2789 char spec[150];
2790 size_t spec_len;
2791 gfc_formal_arglist *f;
2792 tree tmp;
2794 memset (&spec, 0, sizeof (spec));
2795 spec[0] = '.';
2796 spec_len = 1;
2798 if (sym->attr.entry_master)
2799 spec[spec_len++] = 'R';
2800 if (gfc_return_by_reference (sym))
2802 gfc_symbol *result = sym->result ? sym->result : sym;
2804 if (result->attr.pointer || sym->attr.proc_pointer)
2805 spec[spec_len++] = '.';
2806 else
2807 spec[spec_len++] = 'w';
2808 if (sym->ts.type == BT_CHARACTER)
2809 spec[spec_len++] = 'R';
2812 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2813 if (spec_len < sizeof (spec))
2815 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2816 || f->sym->attr.external || f->sym->attr.cray_pointer
2817 || (f->sym->ts.type == BT_DERIVED
2818 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2819 || f->sym->ts.u.derived->attr.pointer_comp))
2820 || (f->sym->ts.type == BT_CLASS
2821 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2822 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2823 spec[spec_len++] = '.';
2824 else if (f->sym->attr.intent == INTENT_IN)
2825 spec[spec_len++] = 'r';
2826 else if (f->sym)
2827 spec[spec_len++] = 'w';
2830 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2831 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2832 return build_type_attribute_variant (fntype, tmp);
2836 tree
2837 gfc_get_function_type (gfc_symbol * sym)
2839 tree type;
2840 vec<tree, va_gc> *typelist = NULL;
2841 gfc_formal_arglist *f;
2842 gfc_symbol *arg;
2843 int alternate_return = 0;
2844 bool is_varargs = true;
2846 /* Make sure this symbol is a function, a subroutine or the main
2847 program. */
2848 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2849 || sym->attr.flavor == FL_PROGRAM);
2851 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2852 so that they can be detected here and handled further down. */
2853 if (sym->backend_decl == NULL)
2854 sym->backend_decl = error_mark_node;
2855 else if (sym->backend_decl == error_mark_node)
2856 goto arg_type_list_done;
2857 else if (sym->attr.proc_pointer)
2858 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2859 else
2860 return TREE_TYPE (sym->backend_decl);
2862 if (sym->attr.entry_master)
2863 /* Additional parameter for selecting an entry point. */
2864 vec_safe_push (typelist, gfc_array_index_type);
2866 if (sym->result)
2867 arg = sym->result;
2868 else
2869 arg = sym;
2871 if (arg->ts.type == BT_CHARACTER)
2872 gfc_conv_const_charlen (arg->ts.u.cl);
2874 /* Some functions we use an extra parameter for the return value. */
2875 if (gfc_return_by_reference (sym))
2877 type = gfc_sym_type (arg);
2878 if (arg->ts.type == BT_COMPLEX
2879 || arg->attr.dimension
2880 || arg->ts.type == BT_CHARACTER)
2881 type = build_reference_type (type);
2883 vec_safe_push (typelist, type);
2884 if (arg->ts.type == BT_CHARACTER)
2886 if (!arg->ts.deferred)
2887 /* Transfer by value. */
2888 vec_safe_push (typelist, gfc_charlen_type_node);
2889 else
2890 /* Deferred character lengths are transferred by reference
2891 so that the value can be returned. */
2892 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2896 /* Build the argument types for the function. */
2897 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2899 arg = f->sym;
2900 if (arg)
2902 /* Evaluate constant character lengths here so that they can be
2903 included in the type. */
2904 if (arg->ts.type == BT_CHARACTER)
2905 gfc_conv_const_charlen (arg->ts.u.cl);
2907 if (arg->attr.flavor == FL_PROCEDURE)
2909 type = gfc_get_function_type (arg);
2910 type = build_pointer_type (type);
2912 else
2913 type = gfc_sym_type (arg);
2915 /* Parameter Passing Convention
2917 We currently pass all parameters by reference.
2918 Parameters with INTENT(IN) could be passed by value.
2919 The problem arises if a function is called via an implicit
2920 prototype. In this situation the INTENT is not known.
2921 For this reason all parameters to global functions must be
2922 passed by reference. Passing by value would potentially
2923 generate bad code. Worse there would be no way of telling that
2924 this code was bad, except that it would give incorrect results.
2926 Contained procedures could pass by value as these are never
2927 used without an explicit interface, and cannot be passed as
2928 actual parameters for a dummy procedure. */
2930 vec_safe_push (typelist, type);
2932 else
2934 if (sym->attr.subroutine)
2935 alternate_return = 1;
2939 /* Add hidden string length parameters. */
2940 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2942 arg = f->sym;
2943 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2945 if (!arg->ts.deferred)
2946 /* Transfer by value. */
2947 type = gfc_charlen_type_node;
2948 else
2949 /* Deferred character lengths are transferred by reference
2950 so that the value can be returned. */
2951 type = build_pointer_type (gfc_charlen_type_node);
2953 vec_safe_push (typelist, type);
2957 if (!vec_safe_is_empty (typelist)
2958 || sym->attr.is_main_program
2959 || sym->attr.if_source != IFSRC_UNKNOWN)
2960 is_varargs = false;
2962 if (sym->backend_decl == error_mark_node)
2963 sym->backend_decl = NULL_TREE;
2965 arg_type_list_done:
2967 if (alternate_return)
2968 type = integer_type_node;
2969 else if (!sym->attr.function || gfc_return_by_reference (sym))
2970 type = void_type_node;
2971 else if (sym->attr.mixed_entry_master)
2972 type = gfc_get_mixed_entry_union (sym->ns);
2973 else if (flag_f2c && sym->ts.type == BT_REAL
2974 && sym->ts.kind == gfc_default_real_kind
2975 && !sym->attr.always_explicit)
2977 /* Special case: f2c calling conventions require that (scalar)
2978 default REAL functions return the C type double instead. f2c
2979 compatibility is only an issue with functions that don't
2980 require an explicit interface, as only these could be
2981 implemented in Fortran 77. */
2982 sym->ts.kind = gfc_default_double_kind;
2983 type = gfc_typenode_for_spec (&sym->ts);
2984 sym->ts.kind = gfc_default_real_kind;
2986 else if (sym->result && sym->result->attr.proc_pointer)
2987 /* Procedure pointer return values. */
2989 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
2991 /* Unset proc_pointer as gfc_get_function_type
2992 is called recursively. */
2993 sym->result->attr.proc_pointer = 0;
2994 type = build_pointer_type (gfc_get_function_type (sym->result));
2995 sym->result->attr.proc_pointer = 1;
2997 else
2998 type = gfc_sym_type (sym->result);
3000 else
3001 type = gfc_sym_type (sym);
3003 if (is_varargs)
3004 type = build_varargs_function_type_vec (type, typelist);
3005 else
3006 type = build_function_type_vec (type, typelist);
3007 type = create_fn_spec (sym, type);
3009 return type;
3012 /* Language hooks for middle-end access to type nodes. */
3014 /* Return an integer type with BITS bits of precision,
3015 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3017 tree
3018 gfc_type_for_size (unsigned bits, int unsignedp)
3020 if (!unsignedp)
3022 int i;
3023 for (i = 0; i <= MAX_INT_KINDS; ++i)
3025 tree type = gfc_integer_types[i];
3026 if (type && bits == TYPE_PRECISION (type))
3027 return type;
3030 /* Handle TImode as a special case because it is used by some backends
3031 (e.g. ARM) even though it is not available for normal use. */
3032 #if HOST_BITS_PER_WIDE_INT >= 64
3033 if (bits == TYPE_PRECISION (intTI_type_node))
3034 return intTI_type_node;
3035 #endif
3037 if (bits <= TYPE_PRECISION (intQI_type_node))
3038 return intQI_type_node;
3039 if (bits <= TYPE_PRECISION (intHI_type_node))
3040 return intHI_type_node;
3041 if (bits <= TYPE_PRECISION (intSI_type_node))
3042 return intSI_type_node;
3043 if (bits <= TYPE_PRECISION (intDI_type_node))
3044 return intDI_type_node;
3045 if (bits <= TYPE_PRECISION (intTI_type_node))
3046 return intTI_type_node;
3048 else
3050 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3051 return unsigned_intQI_type_node;
3052 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3053 return unsigned_intHI_type_node;
3054 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3055 return unsigned_intSI_type_node;
3056 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3057 return unsigned_intDI_type_node;
3058 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3059 return unsigned_intTI_type_node;
3062 return NULL_TREE;
3065 /* Return a data type that has machine mode MODE. If the mode is an
3066 integer, then UNSIGNEDP selects between signed and unsigned types. */
3068 tree
3069 gfc_type_for_mode (machine_mode mode, int unsignedp)
3071 int i;
3072 tree *base;
3074 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3075 base = gfc_real_types;
3076 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3077 base = gfc_complex_types;
3078 else if (SCALAR_INT_MODE_P (mode))
3080 tree type = gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
3081 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3083 else if (VECTOR_MODE_P (mode))
3085 machine_mode inner_mode = GET_MODE_INNER (mode);
3086 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3087 if (inner_type != NULL_TREE)
3088 return build_vector_type_for_mode (inner_type, mode);
3089 return NULL_TREE;
3091 else
3092 return NULL_TREE;
3094 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3096 tree type = base[i];
3097 if (type && mode == TYPE_MODE (type))
3098 return type;
3101 return NULL_TREE;
3104 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3105 in that case. */
3107 bool
3108 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3110 int rank, dim;
3111 bool indirect = false;
3112 tree etype, ptype, field, t, base_decl;
3113 tree data_off, dim_off, dim_size, elem_size;
3114 tree lower_suboff, upper_suboff, stride_suboff;
3116 if (! GFC_DESCRIPTOR_TYPE_P (type))
3118 if (! POINTER_TYPE_P (type))
3119 return false;
3120 type = TREE_TYPE (type);
3121 if (! GFC_DESCRIPTOR_TYPE_P (type))
3122 return false;
3123 indirect = true;
3126 rank = GFC_TYPE_ARRAY_RANK (type);
3127 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3128 return false;
3130 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3131 gcc_assert (POINTER_TYPE_P (etype));
3132 etype = TREE_TYPE (etype);
3134 /* If the type is not a scalar coarray. */
3135 if (TREE_CODE (etype) == ARRAY_TYPE)
3136 etype = TREE_TYPE (etype);
3138 /* Can't handle variable sized elements yet. */
3139 if (int_size_in_bytes (etype) <= 0)
3140 return false;
3141 /* Nor non-constant lower bounds in assumed shape arrays. */
3142 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3143 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3145 for (dim = 0; dim < rank; dim++)
3146 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3147 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3148 return false;
3151 memset (info, '\0', sizeof (*info));
3152 info->ndimensions = rank;
3153 info->ordering = array_descr_ordering_column_major;
3154 info->element_type = etype;
3155 ptype = build_pointer_type (gfc_array_index_type);
3156 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3157 if (!base_decl)
3159 base_decl = make_node (DEBUG_EXPR_DECL);
3160 DECL_ARTIFICIAL (base_decl) = 1;
3161 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3162 DECL_MODE (base_decl) = TYPE_MODE (TREE_TYPE (base_decl));
3163 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3165 info->base_decl = base_decl;
3166 if (indirect)
3167 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3169 if (GFC_TYPE_ARRAY_SPAN (type))
3170 elem_size = GFC_TYPE_ARRAY_SPAN (type);
3171 else
3172 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3173 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
3174 data_off = byte_position (field);
3175 field = DECL_CHAIN (field);
3176 field = DECL_CHAIN (field);
3177 field = DECL_CHAIN (field);
3178 dim_off = byte_position (field);
3179 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
3180 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
3181 stride_suboff = byte_position (field);
3182 field = DECL_CHAIN (field);
3183 lower_suboff = byte_position (field);
3184 field = DECL_CHAIN (field);
3185 upper_suboff = byte_position (field);
3187 t = base_decl;
3188 if (!integer_zerop (data_off))
3189 t = fold_build_pointer_plus (t, data_off);
3190 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3191 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3192 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3193 info->allocated = build2 (NE_EXPR, boolean_type_node,
3194 info->data_location, null_pointer_node);
3195 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3196 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3197 info->associated = build2 (NE_EXPR, boolean_type_node,
3198 info->data_location, null_pointer_node);
3200 for (dim = 0; dim < rank; dim++)
3202 t = fold_build_pointer_plus (base_decl,
3203 size_binop (PLUS_EXPR,
3204 dim_off, lower_suboff));
3205 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3206 info->dimen[dim].lower_bound = t;
3207 t = fold_build_pointer_plus (base_decl,
3208 size_binop (PLUS_EXPR,
3209 dim_off, upper_suboff));
3210 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3211 info->dimen[dim].upper_bound = t;
3212 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3213 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3215 /* Assumed shape arrays have known lower bounds. */
3216 info->dimen[dim].upper_bound
3217 = build2 (MINUS_EXPR, gfc_array_index_type,
3218 info->dimen[dim].upper_bound,
3219 info->dimen[dim].lower_bound);
3220 info->dimen[dim].lower_bound
3221 = fold_convert (gfc_array_index_type,
3222 GFC_TYPE_ARRAY_LBOUND (type, dim));
3223 info->dimen[dim].upper_bound
3224 = build2 (PLUS_EXPR, gfc_array_index_type,
3225 info->dimen[dim].lower_bound,
3226 info->dimen[dim].upper_bound);
3228 t = fold_build_pointer_plus (base_decl,
3229 size_binop (PLUS_EXPR,
3230 dim_off, stride_suboff));
3231 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3232 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3233 info->dimen[dim].stride = t;
3234 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3237 return true;
3241 /* Create a type to handle vector subscripts for coarray library calls. It
3242 has the form:
3243 struct caf_vector_t {
3244 size_t nvec; // size of the vector
3245 union {
3246 struct {
3247 void *vector;
3248 int kind;
3249 } v;
3250 struct {
3251 ptrdiff_t lower_bound;
3252 ptrdiff_t upper_bound;
3253 ptrdiff_t stride;
3254 } triplet;
3255 } u;
3257 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3258 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3260 tree
3261 gfc_get_caf_vector_type (int dim)
3263 static tree vector_types[GFC_MAX_DIMENSIONS];
3264 static tree vec_type = NULL_TREE;
3265 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3267 if (vector_types[dim-1] != NULL_TREE)
3268 return vector_types[dim-1];
3270 if (vec_type == NULL_TREE)
3272 chain = 0;
3273 vect_struct_type = make_node (RECORD_TYPE);
3274 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3275 get_identifier ("vector"),
3276 pvoid_type_node, &chain);
3277 TREE_NO_WARNING (tmp) = 1;
3278 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3279 get_identifier ("kind"),
3280 integer_type_node, &chain);
3281 TREE_NO_WARNING (tmp) = 1;
3282 gfc_finish_type (vect_struct_type);
3284 chain = 0;
3285 triplet_struct_type = make_node (RECORD_TYPE);
3286 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3287 get_identifier ("lower_bound"),
3288 gfc_array_index_type, &chain);
3289 TREE_NO_WARNING (tmp) = 1;
3290 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3291 get_identifier ("upper_bound"),
3292 gfc_array_index_type, &chain);
3293 TREE_NO_WARNING (tmp) = 1;
3294 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3295 gfc_array_index_type, &chain);
3296 TREE_NO_WARNING (tmp) = 1;
3297 gfc_finish_type (triplet_struct_type);
3299 chain = 0;
3300 union_type = make_node (UNION_TYPE);
3301 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3302 vect_struct_type, &chain);
3303 TREE_NO_WARNING (tmp) = 1;
3304 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3305 triplet_struct_type, &chain);
3306 TREE_NO_WARNING (tmp) = 1;
3307 gfc_finish_type (union_type);
3309 chain = 0;
3310 vec_type = make_node (RECORD_TYPE);
3311 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3312 size_type_node, &chain);
3313 TREE_NO_WARNING (tmp) = 1;
3314 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3315 union_type, &chain);
3316 TREE_NO_WARNING (tmp) = 1;
3317 gfc_finish_type (vec_type);
3318 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3321 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3322 gfc_rank_cst[dim-1]);
3323 vector_types[dim-1] = build_array_type (vec_type, tmp);
3324 return vector_types[dim-1];
3327 #include "gt-fortran-trans-types.h"