Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / fortran / trans-types.c
blob67eaf041c23f9292f982d6b5f602cd2d5e416eac
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
3 Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
5 and Steven Bosscher <s.bosscher@student.tudelft.nl>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* trans-types.c -- gfortran backend types */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tree.h"
29 #include "langhooks.h"
30 #include "tm.h"
31 #include "target.h"
32 #include "ggc.h"
33 #include "toplev.h"
34 #include "gfortran.h"
35 #include "trans.h"
36 #include "trans-types.h"
37 #include "trans-const.h"
38 #include "real.h"
39 #include "flags.h"
40 #include "dwarf2out.h"
43 #if (GFC_MAX_DIMENSIONS < 10)
44 #define GFC_RANK_DIGITS 1
45 #define GFC_RANK_PRINTF_FORMAT "%01d"
46 #elif (GFC_MAX_DIMENSIONS < 100)
47 #define GFC_RANK_DIGITS 2
48 #define GFC_RANK_PRINTF_FORMAT "%02d"
49 #else
50 #error If you really need >99 dimensions, continue the sequence above...
51 #endif
53 /* array of structs so we don't have to worry about xmalloc or free */
54 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
56 static tree gfc_get_derived_type (gfc_symbol * derived);
58 tree gfc_array_index_type;
59 tree gfc_array_range_type;
60 tree gfc_character1_type_node;
61 tree pvoid_type_node;
62 tree ppvoid_type_node;
63 tree pchar_type_node;
64 tree pfunc_type_node;
66 tree gfc_charlen_type_node;
68 static GTY(()) tree gfc_desc_dim_type;
69 static GTY(()) tree gfc_max_array_element_size;
70 static GTY(()) tree gfc_array_descriptor_base[GFC_MAX_DIMENSIONS];
72 /* Arrays for all integral and real kinds. We'll fill this in at runtime
73 after the target has a chance to process command-line options. */
75 #define MAX_INT_KINDS 5
76 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
77 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
78 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
79 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
81 #define MAX_REAL_KINDS 5
82 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
83 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
84 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
87 /* The integer kind to use for array indices. This will be set to the
88 proper value based on target information from the backend. */
90 int gfc_index_integer_kind;
92 /* The default kinds of the various types. */
94 int gfc_default_integer_kind;
95 int gfc_max_integer_kind;
96 int gfc_default_real_kind;
97 int gfc_default_double_kind;
98 int gfc_default_character_kind;
99 int gfc_default_logical_kind;
100 int gfc_default_complex_kind;
101 int gfc_c_int_kind;
103 /* The kind size used for record offsets. If the target system supports
104 kind=8, this will be set to 8, otherwise it is set to 4. */
105 int gfc_intio_kind;
107 /* The integer kind used to store character lengths. */
108 int gfc_charlen_int_kind;
110 /* The size of the numeric storage unit and character storage unit. */
111 int gfc_numeric_storage_size;
112 int gfc_character_storage_size;
115 /* Validate that the f90_type of the given gfc_typespec is valid for
116 the type it represents. The f90_type represents the Fortran types
117 this C kind can be used with. For example, c_int has a f90_type of
118 BT_INTEGER and c_float has a f90_type of BT_REAL. Returns FAILURE
119 if a mismatch occurs between ts->f90_type and ts->type; SUCCESS if
120 they match. */
123 gfc_validate_c_kind (gfc_typespec *ts)
125 return ((ts->type == ts->f90_type) ? SUCCESS : FAILURE);
130 gfc_check_any_c_kind (gfc_typespec *ts)
132 int i;
134 for (i = 0; i < ISOCBINDING_NUMBER; i++)
136 /* Check for any C interoperable kind for the given type/kind in ts.
137 This can be used after verify_c_interop to make sure that the
138 Fortran kind being used exists in at least some form for C. */
139 if (c_interop_kinds_table[i].f90_type == ts->type &&
140 c_interop_kinds_table[i].value == ts->kind)
141 return SUCCESS;
144 return FAILURE;
148 static int
149 get_real_kind_from_node (tree type)
151 int i;
153 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
154 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
155 return gfc_real_kinds[i].kind;
157 return -4;
160 static int
161 get_int_kind_from_node (tree type)
163 int i;
165 if (!type)
166 return -2;
168 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
169 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
170 return gfc_integer_kinds[i].kind;
172 return -1;
175 static int
176 get_int_kind_from_width (int size)
178 int i;
180 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
181 if (gfc_integer_kinds[i].bit_size == size)
182 return gfc_integer_kinds[i].kind;
184 return -2;
187 static int
188 get_int_kind_from_minimal_width (int size)
190 int i;
192 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
193 if (gfc_integer_kinds[i].bit_size >= size)
194 return gfc_integer_kinds[i].kind;
196 return -2;
200 /* Generate the CInteropKind_t objects for the C interoperable
201 kinds. */
203 static
204 void init_c_interop_kinds (void)
206 int i;
207 tree intmax_type_node = INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE ?
208 integer_type_node :
209 (LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE ?
210 long_integer_type_node :
211 long_long_integer_type_node);
213 /* init all pointers in the list to NULL */
214 for (i = 0; i < ISOCBINDING_NUMBER; i++)
216 /* Initialize the name and value fields. */
217 c_interop_kinds_table[i].name[0] = '\0';
218 c_interop_kinds_table[i].value = -100;
219 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
222 #define NAMED_INTCST(a,b,c) \
223 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
224 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
225 c_interop_kinds_table[a].value = c;
226 #define NAMED_REALCST(a,b,c) \
227 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
228 c_interop_kinds_table[a].f90_type = BT_REAL; \
229 c_interop_kinds_table[a].value = c;
230 #define NAMED_CMPXCST(a,b,c) \
231 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
232 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
233 c_interop_kinds_table[a].value = c;
234 #define NAMED_LOGCST(a,b,c) \
235 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
236 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
237 c_interop_kinds_table[a].value = c;
238 #define NAMED_CHARKNDCST(a,b,c) \
239 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
240 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
241 c_interop_kinds_table[a].value = c;
242 #define NAMED_CHARCST(a,b,c) \
243 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
244 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
245 c_interop_kinds_table[a].value = c;
246 #define DERIVED_TYPE(a,b,c) \
247 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
248 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
249 c_interop_kinds_table[a].value = c;
250 #define PROCEDURE(a,b) \
251 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
252 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
253 c_interop_kinds_table[a].value = 0;
254 #include "iso-c-binding.def"
258 /* Query the target to determine which machine modes are available for
259 computation. Choose KIND numbers for them. */
261 void
262 gfc_init_kinds (void)
264 enum machine_mode mode;
265 int i_index, r_index;
266 bool saw_i4 = false, saw_i8 = false;
267 bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
269 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
271 int kind, bitsize;
273 if (!targetm.scalar_mode_supported_p (mode))
274 continue;
276 /* The middle end doesn't support constants larger than 2*HWI.
277 Perhaps the target hook shouldn't have accepted these either,
278 but just to be safe... */
279 bitsize = GET_MODE_BITSIZE (mode);
280 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
281 continue;
283 gcc_assert (i_index != MAX_INT_KINDS);
285 /* Let the kind equal the bit size divided by 8. This insulates the
286 programmer from the underlying byte size. */
287 kind = bitsize / 8;
289 if (kind == 4)
290 saw_i4 = true;
291 if (kind == 8)
292 saw_i8 = true;
294 gfc_integer_kinds[i_index].kind = kind;
295 gfc_integer_kinds[i_index].radix = 2;
296 gfc_integer_kinds[i_index].digits = bitsize - 1;
297 gfc_integer_kinds[i_index].bit_size = bitsize;
299 gfc_logical_kinds[i_index].kind = kind;
300 gfc_logical_kinds[i_index].bit_size = bitsize;
302 i_index += 1;
305 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
306 used for large file access. */
308 if (saw_i8)
309 gfc_intio_kind = 8;
310 else
311 gfc_intio_kind = 4;
313 /* If we do not at least have kind = 4, everything is pointless. */
314 gcc_assert(saw_i4);
316 /* Set the maximum integer kind. Used with at least BOZ constants. */
317 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
319 for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
321 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
322 int kind;
324 if (fmt == NULL)
325 continue;
326 if (!targetm.scalar_mode_supported_p (mode))
327 continue;
329 /* Only let float/double/long double go through because the fortran
330 library assumes these are the only floating point types. */
332 if (mode != TYPE_MODE (float_type_node)
333 && (mode != TYPE_MODE (double_type_node))
334 && (mode != TYPE_MODE (long_double_type_node)))
335 continue;
337 /* Let the kind equal the precision divided by 8, rounding up. Again,
338 this insulates the programmer from the underlying byte size.
340 Also, it effectively deals with IEEE extended formats. There, the
341 total size of the type may equal 16, but it's got 6 bytes of padding
342 and the increased size can get in the way of a real IEEE quad format
343 which may also be supported by the target.
345 We round up so as to handle IA-64 __floatreg (RFmode), which is an
346 82 bit type. Not to be confused with __float80 (XFmode), which is
347 an 80 bit type also supported by IA-64. So XFmode should come out
348 to be kind=10, and RFmode should come out to be kind=11. Egads. */
350 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
352 if (kind == 4)
353 saw_r4 = true;
354 if (kind == 8)
355 saw_r8 = true;
356 if (kind == 16)
357 saw_r16 = true;
359 /* Careful we don't stumble a wierd internal mode. */
360 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
361 /* Or have too many modes for the allocated space. */
362 gcc_assert (r_index != MAX_REAL_KINDS);
364 gfc_real_kinds[r_index].kind = kind;
365 gfc_real_kinds[r_index].radix = fmt->b;
366 gfc_real_kinds[r_index].digits = fmt->p;
367 gfc_real_kinds[r_index].min_exponent = fmt->emin;
368 gfc_real_kinds[r_index].max_exponent = fmt->emax;
369 if (fmt->pnan < fmt->p)
370 /* This is an IBM extended double format (or the MIPS variant)
371 made up of two IEEE doubles. The value of the long double is
372 the sum of the values of the two parts. The most significant
373 part is required to be the value of the long double rounded
374 to the nearest double. If we use emax of 1024 then we can't
375 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
376 rounding will make the most significant part overflow. */
377 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
378 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
379 r_index += 1;
382 /* Choose the default integer kind. We choose 4 unless the user
383 directs us otherwise. */
384 if (gfc_option.flag_default_integer)
386 if (!saw_i8)
387 fatal_error ("integer kind=8 not available for -fdefault-integer-8 option");
388 gfc_default_integer_kind = 8;
390 /* Even if the user specified that the default integer kind be 8,
391 the numerica storage size isn't 64. In this case, a warning will
392 be issued when NUMERIC_STORAGE_SIZE is used. */
393 gfc_numeric_storage_size = 4 * 8;
395 else if (saw_i4)
397 gfc_default_integer_kind = 4;
398 gfc_numeric_storage_size = 4 * 8;
400 else
402 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
403 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
406 /* Choose the default real kind. Again, we choose 4 when possible. */
407 if (gfc_option.flag_default_real)
409 if (!saw_r8)
410 fatal_error ("real kind=8 not available for -fdefault-real-8 option");
411 gfc_default_real_kind = 8;
413 else if (saw_r4)
414 gfc_default_real_kind = 4;
415 else
416 gfc_default_real_kind = gfc_real_kinds[0].kind;
418 /* Choose the default double kind. If -fdefault-real and -fdefault-double
419 are specified, we use kind=8, if it's available. If -fdefault-real is
420 specified without -fdefault-double, we use kind=16, if it's available.
421 Otherwise we do not change anything. */
422 if (gfc_option.flag_default_double && !gfc_option.flag_default_real)
423 fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8");
425 if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8)
426 gfc_default_double_kind = 8;
427 else if (gfc_option.flag_default_real && saw_r16)
428 gfc_default_double_kind = 16;
429 else if (saw_r4 && saw_r8)
430 gfc_default_double_kind = 8;
431 else
433 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
434 real ... occupies two contiguous numeric storage units.
436 Therefore we must be supplied a kind twice as large as we chose
437 for single precision. There are loopholes, in that double
438 precision must *occupy* two storage units, though it doesn't have
439 to *use* two storage units. Which means that you can make this
440 kind artificially wide by padding it. But at present there are
441 no GCC targets for which a two-word type does not exist, so we
442 just let gfc_validate_kind abort and tell us if something breaks. */
444 gfc_default_double_kind
445 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
448 /* The default logical kind is constrained to be the same as the
449 default integer kind. Similarly with complex and real. */
450 gfc_default_logical_kind = gfc_default_integer_kind;
451 gfc_default_complex_kind = gfc_default_real_kind;
453 /* Choose the smallest integer kind for our default character. */
454 gfc_default_character_kind = gfc_integer_kinds[0].kind;
455 gfc_character_storage_size = gfc_default_character_kind * 8;
457 /* Choose the integer kind the same size as "void*" for our index kind. */
458 gfc_index_integer_kind = POINTER_SIZE / 8;
459 /* Pick a kind the same size as the C "int" type. */
460 gfc_c_int_kind = INT_TYPE_SIZE / 8;
462 /* initialize the C interoperable kinds */
463 init_c_interop_kinds();
466 /* Make sure that a valid kind is present. Returns an index into the
467 associated kinds array, -1 if the kind is not present. */
469 static int
470 validate_integer (int kind)
472 int i;
474 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
475 if (gfc_integer_kinds[i].kind == kind)
476 return i;
478 return -1;
481 static int
482 validate_real (int kind)
484 int i;
486 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
487 if (gfc_real_kinds[i].kind == kind)
488 return i;
490 return -1;
493 static int
494 validate_logical (int kind)
496 int i;
498 for (i = 0; gfc_logical_kinds[i].kind; i++)
499 if (gfc_logical_kinds[i].kind == kind)
500 return i;
502 return -1;
505 static int
506 validate_character (int kind)
508 return kind == gfc_default_character_kind ? 0 : -1;
511 /* Validate a kind given a basic type. The return value is the same
512 for the child functions, with -1 indicating nonexistence of the
513 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
516 gfc_validate_kind (bt type, int kind, bool may_fail)
518 int rc;
520 switch (type)
522 case BT_REAL: /* Fall through */
523 case BT_COMPLEX:
524 rc = validate_real (kind);
525 break;
526 case BT_INTEGER:
527 rc = validate_integer (kind);
528 break;
529 case BT_LOGICAL:
530 rc = validate_logical (kind);
531 break;
532 case BT_CHARACTER:
533 rc = validate_character (kind);
534 break;
536 default:
537 gfc_internal_error ("gfc_validate_kind(): Got bad type");
540 if (rc < 0 && !may_fail)
541 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
543 return rc;
547 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
548 Reuse common type nodes where possible. Recognize if the kind matches up
549 with a C type. This will be used later in determining which routines may
550 be scarfed from libm. */
552 static tree
553 gfc_build_int_type (gfc_integer_info *info)
555 int mode_precision = info->bit_size;
557 if (mode_precision == CHAR_TYPE_SIZE)
558 info->c_char = 1;
559 if (mode_precision == SHORT_TYPE_SIZE)
560 info->c_short = 1;
561 if (mode_precision == INT_TYPE_SIZE)
562 info->c_int = 1;
563 if (mode_precision == LONG_TYPE_SIZE)
564 info->c_long = 1;
565 if (mode_precision == LONG_LONG_TYPE_SIZE)
566 info->c_long_long = 1;
568 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
569 return intQI_type_node;
570 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
571 return intHI_type_node;
572 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
573 return intSI_type_node;
574 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
575 return intDI_type_node;
576 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
577 return intTI_type_node;
579 return make_signed_type (mode_precision);
582 static tree
583 gfc_build_real_type (gfc_real_info *info)
585 int mode_precision = info->mode_precision;
586 tree new_type;
588 if (mode_precision == FLOAT_TYPE_SIZE)
589 info->c_float = 1;
590 if (mode_precision == DOUBLE_TYPE_SIZE)
591 info->c_double = 1;
592 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
593 info->c_long_double = 1;
595 if (TYPE_PRECISION (float_type_node) == mode_precision)
596 return float_type_node;
597 if (TYPE_PRECISION (double_type_node) == mode_precision)
598 return double_type_node;
599 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
600 return long_double_type_node;
602 new_type = make_node (REAL_TYPE);
603 TYPE_PRECISION (new_type) = mode_precision;
604 layout_type (new_type);
605 return new_type;
608 static tree
609 gfc_build_complex_type (tree scalar_type)
611 tree new_type;
613 if (scalar_type == NULL)
614 return NULL;
615 if (scalar_type == float_type_node)
616 return complex_float_type_node;
617 if (scalar_type == double_type_node)
618 return complex_double_type_node;
619 if (scalar_type == long_double_type_node)
620 return complex_long_double_type_node;
622 new_type = make_node (COMPLEX_TYPE);
623 TREE_TYPE (new_type) = scalar_type;
624 layout_type (new_type);
625 return new_type;
628 static tree
629 gfc_build_logical_type (gfc_logical_info *info)
631 int bit_size = info->bit_size;
632 tree new_type;
634 if (bit_size == BOOL_TYPE_SIZE)
636 info->c_bool = 1;
637 return boolean_type_node;
640 new_type = make_unsigned_type (bit_size);
641 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
642 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
643 TYPE_PRECISION (new_type) = 1;
645 return new_type;
648 #if 0
649 /* Return the bit size of the C "size_t". */
651 static unsigned int
652 c_size_t_size (void)
654 #ifdef SIZE_TYPE
655 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
656 return INT_TYPE_SIZE;
657 if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
658 return LONG_TYPE_SIZE;
659 if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
660 return SHORT_TYPE_SIZE;
661 gcc_unreachable ();
662 #else
663 return LONG_TYPE_SIZE;
664 #endif
666 #endif
668 /* Create the backend type nodes. We map them to their
669 equivalent C type, at least for now. We also give
670 names to the types here, and we push them in the
671 global binding level context.*/
673 void
674 gfc_init_types (void)
676 char name_buf[18];
677 int index;
678 tree type;
679 unsigned n;
680 unsigned HOST_WIDE_INT hi;
681 unsigned HOST_WIDE_INT lo;
683 /* Create and name the types. */
684 #define PUSH_TYPE(name, node) \
685 pushdecl (build_decl (TYPE_DECL, get_identifier (name), node))
687 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
689 type = gfc_build_int_type (&gfc_integer_kinds[index]);
690 gfc_integer_types[index] = type;
691 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
692 gfc_integer_kinds[index].kind);
693 PUSH_TYPE (name_buf, type);
696 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
698 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
699 gfc_logical_types[index] = type;
700 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
701 gfc_logical_kinds[index].kind);
702 PUSH_TYPE (name_buf, type);
705 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
707 type = gfc_build_real_type (&gfc_real_kinds[index]);
708 gfc_real_types[index] = type;
709 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
710 gfc_real_kinds[index].kind);
711 PUSH_TYPE (name_buf, type);
713 type = gfc_build_complex_type (type);
714 gfc_complex_types[index] = type;
715 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
716 gfc_real_kinds[index].kind);
717 PUSH_TYPE (name_buf, type);
720 gfc_character1_type_node = build_qualified_type (unsigned_char_type_node,
721 TYPE_UNQUALIFIED);
722 PUSH_TYPE ("character(kind=1)", gfc_character1_type_node);
724 PUSH_TYPE ("byte", unsigned_char_type_node);
725 PUSH_TYPE ("void", void_type_node);
727 /* DBX debugging output gets upset if these aren't set. */
728 if (!TYPE_NAME (integer_type_node))
729 PUSH_TYPE ("c_integer", integer_type_node);
730 if (!TYPE_NAME (char_type_node))
731 PUSH_TYPE ("c_char", char_type_node);
733 #undef PUSH_TYPE
735 pvoid_type_node = build_pointer_type (void_type_node);
736 ppvoid_type_node = build_pointer_type (pvoid_type_node);
737 pchar_type_node = build_pointer_type (gfc_character1_type_node);
738 pfunc_type_node
739 = build_pointer_type (build_function_type (void_type_node, NULL_TREE));
741 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
742 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
743 since this function is called before gfc_init_constants. */
744 gfc_array_range_type
745 = build_range_type (gfc_array_index_type,
746 build_int_cst (gfc_array_index_type, 0),
747 NULL_TREE);
749 /* The maximum array element size that can be handled is determined
750 by the number of bits available to store this field in the array
751 descriptor. */
753 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
754 lo = ~ (unsigned HOST_WIDE_INT) 0;
755 if (n > HOST_BITS_PER_WIDE_INT)
756 hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
757 else
758 hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
759 gfc_max_array_element_size
760 = build_int_cst_wide (long_unsigned_type_node, lo, hi);
762 size_type_node = gfc_array_index_type;
764 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
765 boolean_true_node = build_int_cst (boolean_type_node, 1);
766 boolean_false_node = build_int_cst (boolean_type_node, 0);
768 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
769 gfc_charlen_int_kind = 4;
770 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
773 /* Get the type node for the given type and kind. */
775 tree
776 gfc_get_int_type (int kind)
778 int index = gfc_validate_kind (BT_INTEGER, kind, true);
779 return index < 0 ? 0 : gfc_integer_types[index];
782 tree
783 gfc_get_real_type (int kind)
785 int index = gfc_validate_kind (BT_REAL, kind, true);
786 return index < 0 ? 0 : gfc_real_types[index];
789 tree
790 gfc_get_complex_type (int kind)
792 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
793 return index < 0 ? 0 : gfc_complex_types[index];
796 tree
797 gfc_get_logical_type (int kind)
799 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
800 return index < 0 ? 0 : gfc_logical_types[index];
803 /* Create a character type with the given kind and length. */
805 tree
806 gfc_get_character_type_len (int kind, tree len)
808 tree bounds, type;
810 gfc_validate_kind (BT_CHARACTER, kind, false);
812 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
813 type = build_array_type (gfc_character1_type_node, bounds);
814 TYPE_STRING_FLAG (type) = 1;
816 return type;
820 /* Get a type node for a character kind. */
822 tree
823 gfc_get_character_type (int kind, gfc_charlen * cl)
825 tree len;
827 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
829 return gfc_get_character_type_len (kind, len);
832 /* Covert a basic type. This will be an array for character types. */
834 tree
835 gfc_typenode_for_spec (gfc_typespec * spec)
837 tree basetype;
839 switch (spec->type)
841 case BT_UNKNOWN:
842 gcc_unreachable ();
844 case BT_INTEGER:
845 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
846 has been resolved. This is done so we can convert C_PTR and
847 C_FUNPTR to simple variables that get translated to (void *). */
848 if (spec->f90_type == BT_VOID)
850 if (spec->derived
851 && spec->derived->intmod_sym_id == ISOCBINDING_PTR)
852 basetype = ptr_type_node;
853 else
854 basetype = pfunc_type_node;
856 else
857 basetype = gfc_get_int_type (spec->kind);
858 break;
860 case BT_REAL:
861 basetype = gfc_get_real_type (spec->kind);
862 break;
864 case BT_COMPLEX:
865 basetype = gfc_get_complex_type (spec->kind);
866 break;
868 case BT_LOGICAL:
869 basetype = gfc_get_logical_type (spec->kind);
870 break;
872 case BT_CHARACTER:
873 basetype = gfc_get_character_type (spec->kind, spec->cl);
874 break;
876 case BT_DERIVED:
877 basetype = gfc_get_derived_type (spec->derived);
879 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
880 type and kind to fit a (void *) and the basetype returned was a
881 ptr_type_node. We need to pass up this new information to the
882 symbol that was declared of type C_PTR or C_FUNPTR. */
883 if (spec->derived->attr.is_iso_c)
885 spec->type = spec->derived->ts.type;
886 spec->kind = spec->derived->ts.kind;
887 spec->f90_type = spec->derived->ts.f90_type;
889 break;
890 case BT_VOID:
891 /* This is for the second arg to c_f_pointer and c_f_procpointer
892 of the iso_c_binding module, to accept any ptr type. */
893 basetype = ptr_type_node;
894 if (spec->f90_type == BT_VOID)
896 if (spec->derived
897 && spec->derived->intmod_sym_id == ISOCBINDING_PTR)
898 basetype = ptr_type_node;
899 else
900 basetype = pfunc_type_node;
902 break;
903 default:
904 gcc_unreachable ();
906 return basetype;
909 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
911 static tree
912 gfc_conv_array_bound (gfc_expr * expr)
914 /* If expr is an integer constant, return that. */
915 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
916 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
918 /* Otherwise return NULL. */
919 return NULL_TREE;
922 tree
923 gfc_get_element_type (tree type)
925 tree element;
927 if (GFC_ARRAY_TYPE_P (type))
929 if (TREE_CODE (type) == POINTER_TYPE)
930 type = TREE_TYPE (type);
931 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
932 element = TREE_TYPE (type);
934 else
936 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
937 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
939 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
940 element = TREE_TYPE (element);
942 gcc_assert (TREE_CODE (element) == ARRAY_TYPE);
943 element = TREE_TYPE (element);
946 return element;
949 /* Build an array. This function is called from gfc_sym_type().
950 Actually returns array descriptor type.
952 Format of array descriptors is as follows:
954 struct gfc_array_descriptor
956 array *data
957 index offset;
958 index dtype;
959 struct descriptor_dimension dimension[N_DIM];
962 struct descriptor_dimension
964 index stride;
965 index lbound;
966 index ubound;
969 Translation code should use gfc_conv_descriptor_* rather than
970 accessing the descriptor directly. Any changes to the array
971 descriptor type will require changes in gfc_conv_descriptor_* and
972 gfc_build_array_initializer.
974 This is represented internally as a RECORD_TYPE. The index nodes
975 are gfc_array_index_type and the data node is a pointer to the
976 data. See below for the handling of character types.
978 The dtype member is formatted as follows:
979 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
980 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
981 size = dtype >> GFC_DTYPE_SIZE_SHIFT
983 I originally used nested ARRAY_TYPE nodes to represent arrays, but
984 this generated poor code for assumed/deferred size arrays. These
985 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
986 of the GENERIC grammar. Also, there is no way to explicitly set
987 the array stride, so all data must be packed(1). I've tried to
988 mark all the functions which would require modification with a GCC
989 ARRAYS comment.
991 The data component points to the first element in the array. The
992 offset field is the position of the origin of the array (ie element
993 (0, 0 ...)). This may be outsite the bounds of the array.
995 An element is accessed by
996 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
997 This gives good performance as the computation does not involve the
998 bounds of the array. For packed arrays, this is optimized further
999 by substituting the known strides.
1001 This system has one problem: all array bounds must be within 2^31
1002 elements of the origin (2^63 on 64-bit machines). For example
1003 integer, dimension (80000:90000, 80000:90000, 2) :: array
1004 may not work properly on 32-bit machines because 80000*80000 >
1005 2^31, so the calculation for stride02 would overflow. This may
1006 still work, but I haven't checked, and it relies on the overflow
1007 doing the right thing.
1009 The way to fix this problem is to access elements as follows:
1010 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1011 Obviously this is much slower. I will make this a compile time
1012 option, something like -fsmall-array-offsets. Mixing code compiled
1013 with and without this switch will work.
1015 (1) This can be worked around by modifying the upper bound of the
1016 previous dimension. This requires extra fields in the descriptor
1017 (both real_ubound and fake_ubound). */
1020 /* Returns true if the array sym does not require a descriptor. */
1023 gfc_is_nodesc_array (gfc_symbol * sym)
1025 gcc_assert (sym->attr.dimension);
1027 /* We only want local arrays. */
1028 if (sym->attr.pointer || sym->attr.allocatable)
1029 return 0;
1031 if (sym->attr.dummy)
1033 if (sym->as->type != AS_ASSUMED_SHAPE)
1034 return 1;
1035 else
1036 return 0;
1039 if (sym->attr.result || sym->attr.function)
1040 return 0;
1042 gcc_assert (sym->as->type == AS_EXPLICIT);
1044 return 1;
1048 /* Create an array descriptor type. */
1050 static tree
1051 gfc_build_array_type (tree type, gfc_array_spec * as,
1052 enum gfc_array_kind akind)
1054 tree lbound[GFC_MAX_DIMENSIONS];
1055 tree ubound[GFC_MAX_DIMENSIONS];
1056 int n;
1058 for (n = 0; n < as->rank; n++)
1060 /* Create expressions for the known bounds of the array. */
1061 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1062 lbound[n] = gfc_index_one_node;
1063 else
1064 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1065 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1068 if (as->type == AS_ASSUMED_SHAPE)
1069 akind = GFC_ARRAY_ASSUMED_SHAPE;
1070 return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0, akind);
1073 /* Returns the struct descriptor_dimension type. */
1075 static tree
1076 gfc_get_desc_dim_type (void)
1078 tree type;
1079 tree decl;
1080 tree fieldlist;
1082 if (gfc_desc_dim_type)
1083 return gfc_desc_dim_type;
1085 /* Build the type node. */
1086 type = make_node (RECORD_TYPE);
1088 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1089 TYPE_PACKED (type) = 1;
1091 /* Consists of the stride, lbound and ubound members. */
1092 decl = build_decl (FIELD_DECL,
1093 get_identifier ("stride"), gfc_array_index_type);
1094 DECL_CONTEXT (decl) = type;
1095 TREE_NO_WARNING (decl) = 1;
1096 fieldlist = decl;
1098 decl = build_decl (FIELD_DECL,
1099 get_identifier ("lbound"), gfc_array_index_type);
1100 DECL_CONTEXT (decl) = type;
1101 TREE_NO_WARNING (decl) = 1;
1102 fieldlist = chainon (fieldlist, decl);
1104 decl = build_decl (FIELD_DECL,
1105 get_identifier ("ubound"), gfc_array_index_type);
1106 DECL_CONTEXT (decl) = type;
1107 TREE_NO_WARNING (decl) = 1;
1108 fieldlist = chainon (fieldlist, decl);
1110 /* Finish off the type. */
1111 TYPE_FIELDS (type) = fieldlist;
1113 gfc_finish_type (type);
1114 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1116 gfc_desc_dim_type = type;
1117 return type;
1121 /* Return the DTYPE for an array. This describes the type and type parameters
1122 of the array. */
1123 /* TODO: Only call this when the value is actually used, and make all the
1124 unknown cases abort. */
1126 tree
1127 gfc_get_dtype (tree type)
1129 tree size;
1130 int n;
1131 HOST_WIDE_INT i;
1132 tree tmp;
1133 tree dtype;
1134 tree etype;
1135 int rank;
1137 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1139 if (GFC_TYPE_ARRAY_DTYPE (type))
1140 return GFC_TYPE_ARRAY_DTYPE (type);
1142 rank = GFC_TYPE_ARRAY_RANK (type);
1143 etype = gfc_get_element_type (type);
1145 switch (TREE_CODE (etype))
1147 case INTEGER_TYPE:
1148 n = GFC_DTYPE_INTEGER;
1149 break;
1151 case BOOLEAN_TYPE:
1152 n = GFC_DTYPE_LOGICAL;
1153 break;
1155 case REAL_TYPE:
1156 n = GFC_DTYPE_REAL;
1157 break;
1159 case COMPLEX_TYPE:
1160 n = GFC_DTYPE_COMPLEX;
1161 break;
1163 /* We will never have arrays of arrays. */
1164 case RECORD_TYPE:
1165 n = GFC_DTYPE_DERIVED;
1166 break;
1168 case ARRAY_TYPE:
1169 n = GFC_DTYPE_CHARACTER;
1170 break;
1172 default:
1173 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1174 /* We can strange array types for temporary arrays. */
1175 return gfc_index_zero_node;
1178 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1179 size = TYPE_SIZE_UNIT (etype);
1181 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1182 if (size && INTEGER_CST_P (size))
1184 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1185 internal_error ("Array element size too big");
1187 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1189 dtype = build_int_cst (gfc_array_index_type, i);
1191 if (size && !INTEGER_CST_P (size))
1193 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1194 tmp = fold_build2 (LSHIFT_EXPR, gfc_array_index_type,
1195 fold_convert (gfc_array_index_type, size), tmp);
1196 dtype = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype);
1198 /* If we don't know the size we leave it as zero. This should never happen
1199 for anything that is actually used. */
1200 /* TODO: Check this is actually true, particularly when repacking
1201 assumed size parameters. */
1203 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1204 return dtype;
1208 /* Build an array type for use without a descriptor, packed according
1209 to the value of PACKED. */
1211 tree
1212 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed)
1214 tree range;
1215 tree type;
1216 tree tmp;
1217 int n;
1218 int known_stride;
1219 int known_offset;
1220 mpz_t offset;
1221 mpz_t stride;
1222 mpz_t delta;
1223 gfc_expr *expr;
1225 mpz_init_set_ui (offset, 0);
1226 mpz_init_set_ui (stride, 1);
1227 mpz_init (delta);
1229 /* We don't use build_array_type because this does not include include
1230 lang-specific information (i.e. the bounds of the array) when checking
1231 for duplicates. */
1232 type = make_node (ARRAY_TYPE);
1234 GFC_ARRAY_TYPE_P (type) = 1;
1235 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
1236 ggc_alloc_cleared (sizeof (struct lang_type));
1238 known_stride = (packed != PACKED_NO);
1239 known_offset = 1;
1240 for (n = 0; n < as->rank; n++)
1242 /* Fill in the stride and bound components of the type. */
1243 if (known_stride)
1244 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1245 else
1246 tmp = NULL_TREE;
1247 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1249 expr = as->lower[n];
1250 if (expr->expr_type == EXPR_CONSTANT)
1252 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1253 gfc_index_integer_kind);
1255 else
1257 known_stride = 0;
1258 tmp = NULL_TREE;
1260 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1262 if (known_stride)
1264 /* Calculate the offset. */
1265 mpz_mul (delta, stride, as->lower[n]->value.integer);
1266 mpz_sub (offset, offset, delta);
1268 else
1269 known_offset = 0;
1271 expr = as->upper[n];
1272 if (expr && expr->expr_type == EXPR_CONSTANT)
1274 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1275 gfc_index_integer_kind);
1277 else
1279 tmp = NULL_TREE;
1280 known_stride = 0;
1282 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1284 if (known_stride)
1286 /* Calculate the stride. */
1287 mpz_sub (delta, as->upper[n]->value.integer,
1288 as->lower[n]->value.integer);
1289 mpz_add_ui (delta, delta, 1);
1290 mpz_mul (stride, stride, delta);
1293 /* Only the first stride is known for partial packed arrays. */
1294 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1295 known_stride = 0;
1298 if (known_offset)
1300 GFC_TYPE_ARRAY_OFFSET (type) =
1301 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1303 else
1304 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1306 if (known_stride)
1308 GFC_TYPE_ARRAY_SIZE (type) =
1309 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1311 else
1312 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1314 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1315 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1316 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1317 NULL_TREE);
1318 /* TODO: use main type if it is unbounded. */
1319 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1320 build_pointer_type (build_array_type (etype, range));
1322 if (known_stride)
1324 mpz_sub_ui (stride, stride, 1);
1325 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1327 else
1328 range = NULL_TREE;
1330 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1331 TYPE_DOMAIN (type) = range;
1333 build_pointer_type (etype);
1334 TREE_TYPE (type) = etype;
1336 layout_type (type);
1338 mpz_clear (offset);
1339 mpz_clear (stride);
1340 mpz_clear (delta);
1342 /* In debug info represent packed arrays as multi-dimensional
1343 if they have rank > 1 and with proper bounds, instead of flat
1344 arrays. */
1345 if (known_offset && write_symbols != NO_DEBUG)
1347 tree gtype = etype, rtype, type_decl;
1349 for (n = as->rank - 1; n >= 0; n--)
1351 rtype = build_range_type (gfc_array_index_type,
1352 GFC_TYPE_ARRAY_LBOUND (type, n),
1353 GFC_TYPE_ARRAY_UBOUND (type, n));
1354 gtype = build_array_type (gtype, rtype);
1356 TYPE_NAME (type) = type_decl = build_decl (TYPE_DECL, NULL, gtype);
1357 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1360 if (packed != PACKED_STATIC || !known_stride)
1362 /* For dummy arrays and automatic (heap allocated) arrays we
1363 want a pointer to the array. */
1364 type = build_pointer_type (type);
1365 GFC_ARRAY_TYPE_P (type) = 1;
1366 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1368 return type;
1371 /* Return or create the base type for an array descriptor. */
1373 static tree
1374 gfc_get_array_descriptor_base (int dimen)
1376 tree fat_type, fieldlist, decl, arraytype;
1377 char name[16 + GFC_RANK_DIGITS + 1];
1379 gcc_assert (dimen >= 1 && dimen <= GFC_MAX_DIMENSIONS);
1380 if (gfc_array_descriptor_base[dimen - 1])
1381 return gfc_array_descriptor_base[dimen - 1];
1383 /* Build the type node. */
1384 fat_type = make_node (RECORD_TYPE);
1386 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen);
1387 TYPE_NAME (fat_type) = get_identifier (name);
1389 /* Add the data member as the first element of the descriptor. */
1390 decl = build_decl (FIELD_DECL, get_identifier ("data"), ptr_type_node);
1392 DECL_CONTEXT (decl) = fat_type;
1393 fieldlist = decl;
1395 /* Add the base component. */
1396 decl = build_decl (FIELD_DECL, get_identifier ("offset"),
1397 gfc_array_index_type);
1398 DECL_CONTEXT (decl) = fat_type;
1399 TREE_NO_WARNING (decl) = 1;
1400 fieldlist = chainon (fieldlist, decl);
1402 /* Add the dtype component. */
1403 decl = build_decl (FIELD_DECL, get_identifier ("dtype"),
1404 gfc_array_index_type);
1405 DECL_CONTEXT (decl) = fat_type;
1406 TREE_NO_WARNING (decl) = 1;
1407 fieldlist = chainon (fieldlist, decl);
1409 /* Build the array type for the stride and bound components. */
1410 arraytype =
1411 build_array_type (gfc_get_desc_dim_type (),
1412 build_range_type (gfc_array_index_type,
1413 gfc_index_zero_node,
1414 gfc_rank_cst[dimen - 1]));
1416 decl = build_decl (FIELD_DECL, get_identifier ("dim"), arraytype);
1417 DECL_CONTEXT (decl) = fat_type;
1418 TREE_NO_WARNING (decl) = 1;
1419 fieldlist = chainon (fieldlist, decl);
1421 /* Finish off the type. */
1422 TYPE_FIELDS (fat_type) = fieldlist;
1424 gfc_finish_type (fat_type);
1425 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1427 gfc_array_descriptor_base[dimen - 1] = fat_type;
1428 return fat_type;
1431 /* Build an array (descriptor) type with given bounds. */
1433 tree
1434 gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound,
1435 tree * ubound, int packed,
1436 enum gfc_array_kind akind)
1438 char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN];
1439 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1440 const char *typename;
1441 int n;
1443 base_type = gfc_get_array_descriptor_base (dimen);
1444 fat_type = build_variant_type_copy (base_type);
1446 tmp = TYPE_NAME (etype);
1447 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1448 tmp = DECL_NAME (tmp);
1449 if (tmp)
1450 typename = IDENTIFIER_POINTER (tmp);
1451 else
1452 typename = "unknown";
1453 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen,
1454 GFC_MAX_SYMBOL_LEN, typename);
1455 TYPE_NAME (fat_type) = get_identifier (name);
1457 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1458 TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *)
1459 ggc_alloc_cleared (sizeof (struct lang_type));
1461 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1462 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1463 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1465 /* Build an array descriptor record type. */
1466 if (packed != 0)
1467 stride = gfc_index_one_node;
1468 else
1469 stride = NULL_TREE;
1470 for (n = 0; n < dimen; n++)
1472 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1474 if (lbound)
1475 lower = lbound[n];
1476 else
1477 lower = NULL_TREE;
1479 if (lower != NULL_TREE)
1481 if (INTEGER_CST_P (lower))
1482 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1483 else
1484 lower = NULL_TREE;
1487 upper = ubound[n];
1488 if (upper != NULL_TREE)
1490 if (INTEGER_CST_P (upper))
1491 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1492 else
1493 upper = NULL_TREE;
1496 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1498 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
1499 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1500 gfc_index_one_node);
1501 stride =
1502 fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, stride);
1503 /* Check the folding worked. */
1504 gcc_assert (INTEGER_CST_P (stride));
1506 else
1507 stride = NULL_TREE;
1509 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1511 /* TODO: known offsets for descriptors. */
1512 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1514 /* We define data as an array with the correct size if possible.
1515 Much better than doing pointer arithmetic. */
1516 if (stride)
1517 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1518 int_const_binop (MINUS_EXPR, stride,
1519 integer_one_node, 0));
1520 else
1521 rtype = gfc_array_range_type;
1522 arraytype = build_array_type (etype, rtype);
1523 arraytype = build_pointer_type (arraytype);
1524 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1526 return fat_type;
1529 /* Build a pointer type. This function is called from gfc_sym_type(). */
1531 static tree
1532 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1534 /* Array pointer types aren't actually pointers. */
1535 if (sym->attr.dimension)
1536 return type;
1537 else
1538 return build_pointer_type (type);
1541 /* Return the type for a symbol. Special handling is required for character
1542 types to get the correct level of indirection.
1543 For functions return the return type.
1544 For subroutines return void_type_node.
1545 Calling this multiple times for the same symbol should be avoided,
1546 especially for character and array types. */
1548 tree
1549 gfc_sym_type (gfc_symbol * sym)
1551 tree type;
1552 int byref;
1554 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1555 return void_type_node;
1557 /* In the case of a function the fake result variable may have a
1558 type different from the function type, so don't return early in
1559 that case. */
1560 if (sym->backend_decl && !sym->attr.function)
1561 return TREE_TYPE (sym->backend_decl);
1563 if (sym->ts.type == BT_CHARACTER && sym->attr.is_bind_c
1564 && (sym->attr.function || sym->attr.result))
1565 type = gfc_character1_type_node;
1566 else
1567 type = gfc_typenode_for_spec (&sym->ts);
1569 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
1570 byref = 1;
1571 else
1572 byref = 0;
1574 if (sym->attr.dimension)
1576 if (gfc_is_nodesc_array (sym))
1578 /* If this is a character argument of unknown length, just use the
1579 base type. */
1580 if (sym->ts.type != BT_CHARACTER
1581 || !(sym->attr.dummy || sym->attr.function)
1582 || sym->ts.cl->backend_decl)
1584 type = gfc_get_nodesc_array_type (type, sym->as,
1585 byref ? PACKED_FULL
1586 : PACKED_STATIC);
1587 byref = 0;
1590 else
1592 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
1593 if (sym->attr.pointer)
1594 akind = GFC_ARRAY_POINTER;
1595 else if (sym->attr.allocatable)
1596 akind = GFC_ARRAY_ALLOCATABLE;
1597 type = gfc_build_array_type (type, sym->as, akind);
1600 else
1602 if (sym->attr.allocatable || sym->attr.pointer)
1603 type = gfc_build_pointer_type (sym, type);
1604 if (sym->attr.pointer)
1605 GFC_POINTER_TYPE_P (type) = 1;
1608 /* We currently pass all parameters by reference.
1609 See f95_get_function_decl. For dummy function parameters return the
1610 function type. */
1611 if (byref)
1613 /* We must use pointer types for potentially absent variables. The
1614 optimizers assume a reference type argument is never NULL. */
1615 if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1616 type = build_pointer_type (type);
1617 else
1618 type = build_reference_type (type);
1621 return (type);
1624 /* Layout and output debug info for a record type. */
1626 void
1627 gfc_finish_type (tree type)
1629 tree decl;
1631 decl = build_decl (TYPE_DECL, NULL_TREE, type);
1632 TYPE_STUB_DECL (type) = decl;
1633 layout_type (type);
1634 rest_of_type_compilation (type, 1);
1635 rest_of_decl_compilation (decl, 1, 0);
1638 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1639 or RECORD_TYPE pointed to by STYPE. The new field is chained
1640 to the fieldlist pointed to by FIELDLIST.
1642 Returns a pointer to the new field. */
1644 tree
1645 gfc_add_field_to_struct (tree *fieldlist, tree context,
1646 tree name, tree type)
1648 tree decl;
1650 decl = build_decl (FIELD_DECL, name, type);
1652 DECL_CONTEXT (decl) = context;
1653 DECL_INITIAL (decl) = 0;
1654 DECL_ALIGN (decl) = 0;
1655 DECL_USER_ALIGN (decl) = 0;
1656 TREE_CHAIN (decl) = NULL_TREE;
1657 *fieldlist = chainon (*fieldlist, decl);
1659 return decl;
1663 /* Copy the backend_decl and component backend_decls if
1664 the two derived type symbols are "equal", as described
1665 in 4.4.2 and resolved by gfc_compare_derived_types. */
1667 static int
1668 copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to)
1670 gfc_component *to_cm;
1671 gfc_component *from_cm;
1673 if (from->backend_decl == NULL
1674 || !gfc_compare_derived_types (from, to))
1675 return 0;
1677 to->backend_decl = from->backend_decl;
1679 to_cm = to->components;
1680 from_cm = from->components;
1682 /* Copy the component declarations. If a component is itself
1683 a derived type, we need a copy of its component declarations.
1684 This is done by recursing into gfc_get_derived_type and
1685 ensures that the component's component declarations have
1686 been built. If it is a character, we need the character
1687 length, as well. */
1688 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
1690 to_cm->backend_decl = from_cm->backend_decl;
1691 if (!from_cm->pointer && from_cm->ts.type == BT_DERIVED)
1692 gfc_get_derived_type (to_cm->ts.derived);
1694 else if (from_cm->ts.type == BT_CHARACTER)
1695 to_cm->ts.cl->backend_decl = from_cm->ts.cl->backend_decl;
1698 return 1;
1702 /* Build a tree node for a derived type. If there are equal
1703 derived types, with different local names, these are built
1704 at the same time. If an equal derived type has been built
1705 in a parent namespace, this is used. */
1707 static tree
1708 gfc_get_derived_type (gfc_symbol * derived)
1710 tree typenode = NULL, field = NULL, field_type = NULL, fieldlist = NULL;
1711 gfc_component *c;
1712 gfc_dt_list *dt;
1714 gcc_assert (derived && derived->attr.flavor == FL_DERIVED);
1716 /* See if it's one of the iso_c_binding derived types. */
1717 if (derived->attr.is_iso_c == 1)
1719 if (derived->backend_decl)
1720 return derived->backend_decl;
1722 if (derived->intmod_sym_id == ISOCBINDING_PTR)
1723 derived->backend_decl = ptr_type_node;
1724 else
1725 derived->backend_decl = pfunc_type_node;
1727 /* Create a backend_decl for the __c_ptr_c_address field. */
1728 derived->components->backend_decl =
1729 gfc_add_field_to_struct (&(derived->backend_decl->type.values),
1730 derived->backend_decl,
1731 get_identifier (derived->components->name),
1732 gfc_typenode_for_spec (
1733 &(derived->components->ts)));
1735 derived->ts.kind = gfc_index_integer_kind;
1736 derived->ts.type = BT_INTEGER;
1737 /* Set the f90_type to BT_VOID as a way to recognize something of type
1738 BT_INTEGER that needs to fit a void * for the purpose of the
1739 iso_c_binding derived types. */
1740 derived->ts.f90_type = BT_VOID;
1742 return derived->backend_decl;
1745 /* derived->backend_decl != 0 means we saw it before, but its
1746 components' backend_decl may have not been built. */
1747 if (derived->backend_decl)
1749 /* Its components' backend_decl have been built. */
1750 if (TYPE_FIELDS (derived->backend_decl))
1751 return derived->backend_decl;
1752 else
1753 typenode = derived->backend_decl;
1755 else
1758 /* We see this derived type first time, so build the type node. */
1759 typenode = make_node (RECORD_TYPE);
1760 TYPE_NAME (typenode) = get_identifier (derived->name);
1761 TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
1762 derived->backend_decl = typenode;
1765 /* Go through the derived type components, building them as
1766 necessary. The reason for doing this now is that it is
1767 possible to recurse back to this derived type through a
1768 pointer component (PR24092). If this happens, the fields
1769 will be built and so we can return the type. */
1770 for (c = derived->components; c; c = c->next)
1772 if (c->ts.type != BT_DERIVED)
1773 continue;
1775 if (!c->pointer || c->ts.derived->backend_decl == NULL)
1776 c->ts.derived->backend_decl = gfc_get_derived_type (c->ts.derived);
1778 if (c->ts.derived && c->ts.derived->attr.is_iso_c)
1780 /* Need to copy the modified ts from the derived type. The
1781 typespec was modified because C_PTR/C_FUNPTR are translated
1782 into (void *) from derived types. */
1783 c->ts.type = c->ts.derived->ts.type;
1784 c->ts.kind = c->ts.derived->ts.kind;
1785 c->ts.f90_type = c->ts.derived->ts.f90_type;
1786 if (c->initializer)
1788 c->initializer->ts.type = c->ts.type;
1789 c->initializer->ts.kind = c->ts.kind;
1790 c->initializer->ts.f90_type = c->ts.f90_type;
1791 c->initializer->expr_type = EXPR_NULL;
1796 if (TYPE_FIELDS (derived->backend_decl))
1797 return derived->backend_decl;
1799 /* Build the type member list. Install the newly created RECORD_TYPE
1800 node as DECL_CONTEXT of each FIELD_DECL. */
1801 fieldlist = NULL_TREE;
1802 for (c = derived->components; c; c = c->next)
1804 if (c->ts.type == BT_DERIVED)
1805 field_type = c->ts.derived->backend_decl;
1806 else
1808 if (c->ts.type == BT_CHARACTER)
1810 /* Evaluate the string length. */
1811 gfc_conv_const_charlen (c->ts.cl);
1812 gcc_assert (c->ts.cl->backend_decl);
1815 field_type = gfc_typenode_for_spec (&c->ts);
1818 /* This returns an array descriptor type. Initialization may be
1819 required. */
1820 if (c->dimension)
1822 if (c->pointer || c->allocatable)
1824 enum gfc_array_kind akind;
1825 if (c->pointer)
1826 akind = GFC_ARRAY_POINTER;
1827 else
1828 akind = GFC_ARRAY_ALLOCATABLE;
1829 /* Pointers to arrays aren't actually pointer types. The
1830 descriptors are separate, but the data is common. */
1831 field_type = gfc_build_array_type (field_type, c->as, akind);
1833 else
1834 field_type = gfc_get_nodesc_array_type (field_type, c->as,
1835 PACKED_STATIC);
1837 else if (c->pointer)
1838 field_type = build_pointer_type (field_type);
1840 field = gfc_add_field_to_struct (&fieldlist, typenode,
1841 get_identifier (c->name),
1842 field_type);
1843 if (c->loc.lb)
1844 gfc_set_decl_location (field, &c->loc);
1845 else if (derived->declared_at.lb)
1846 gfc_set_decl_location (field, &derived->declared_at);
1848 DECL_PACKED (field) |= TYPE_PACKED (typenode);
1850 gcc_assert (field);
1851 if (!c->backend_decl)
1852 c->backend_decl = field;
1855 /* Now we have the final fieldlist. Record it, then lay out the
1856 derived type, including the fields. */
1857 TYPE_FIELDS (typenode) = fieldlist;
1859 gfc_finish_type (typenode);
1860 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
1862 derived->backend_decl = typenode;
1864 /* Add this backend_decl to all the other, equal derived types. */
1865 for (dt = gfc_derived_types; dt; dt = dt->next)
1866 copy_dt_decls_ifequal (derived, dt->derived);
1868 return derived->backend_decl;
1873 gfc_return_by_reference (gfc_symbol * sym)
1875 if (!sym->attr.function)
1876 return 0;
1878 if (sym->attr.dimension)
1879 return 1;
1881 if (sym->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
1882 return 1;
1884 /* Possibly return complex numbers by reference for g77 compatibility.
1885 We don't do this for calls to intrinsics (as the library uses the
1886 -fno-f2c calling convention), nor for calls to functions which always
1887 require an explicit interface, as no compatibility problems can
1888 arise there. */
1889 if (gfc_option.flag_f2c
1890 && sym->ts.type == BT_COMPLEX
1891 && !sym->attr.intrinsic && !sym->attr.always_explicit)
1892 return 1;
1894 return 0;
1897 static tree
1898 gfc_get_mixed_entry_union (gfc_namespace *ns)
1900 tree type;
1901 tree decl;
1902 tree fieldlist;
1903 char name[GFC_MAX_SYMBOL_LEN + 1];
1904 gfc_entry_list *el, *el2;
1906 gcc_assert (ns->proc_name->attr.mixed_entry_master);
1907 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
1909 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
1911 /* Build the type node. */
1912 type = make_node (UNION_TYPE);
1914 TYPE_NAME (type) = get_identifier (name);
1915 fieldlist = NULL;
1917 for (el = ns->entries; el; el = el->next)
1919 /* Search for duplicates. */
1920 for (el2 = ns->entries; el2 != el; el2 = el2->next)
1921 if (el2->sym->result == el->sym->result)
1922 break;
1924 if (el == el2)
1926 decl = build_decl (FIELD_DECL,
1927 get_identifier (el->sym->result->name),
1928 gfc_sym_type (el->sym->result));
1929 DECL_CONTEXT (decl) = type;
1930 fieldlist = chainon (fieldlist, decl);
1934 /* Finish off the type. */
1935 TYPE_FIELDS (type) = fieldlist;
1937 gfc_finish_type (type);
1938 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1939 return type;
1942 tree
1943 gfc_get_function_type (gfc_symbol * sym)
1945 tree type;
1946 tree typelist;
1947 gfc_formal_arglist *f;
1948 gfc_symbol *arg;
1949 int nstr;
1950 int alternate_return;
1952 /* Make sure this symbol is a function, a subroutine or the main
1953 program. */
1954 gcc_assert (sym->attr.flavor == FL_PROCEDURE
1955 || sym->attr.flavor == FL_PROGRAM);
1957 if (sym->backend_decl)
1958 return TREE_TYPE (sym->backend_decl);
1960 nstr = 0;
1961 alternate_return = 0;
1962 typelist = NULL_TREE;
1964 if (sym->attr.entry_master)
1966 /* Additional parameter for selecting an entry point. */
1967 typelist = gfc_chainon_list (typelist, gfc_array_index_type);
1970 if (sym->result)
1971 arg = sym->result;
1972 else
1973 arg = sym;
1975 if (arg->ts.type == BT_CHARACTER)
1976 gfc_conv_const_charlen (arg->ts.cl);
1978 /* Some functions we use an extra parameter for the return value. */
1979 if (gfc_return_by_reference (sym))
1981 type = gfc_sym_type (arg);
1982 if (arg->ts.type == BT_COMPLEX
1983 || arg->attr.dimension
1984 || arg->ts.type == BT_CHARACTER)
1985 type = build_reference_type (type);
1987 typelist = gfc_chainon_list (typelist, type);
1988 if (arg->ts.type == BT_CHARACTER)
1989 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1992 /* Build the argument types for the function. */
1993 for (f = sym->formal; f; f = f->next)
1995 arg = f->sym;
1996 if (arg)
1998 /* Evaluate constant character lengths here so that they can be
1999 included in the type. */
2000 if (arg->ts.type == BT_CHARACTER)
2001 gfc_conv_const_charlen (arg->ts.cl);
2003 if (arg->attr.flavor == FL_PROCEDURE)
2005 type = gfc_get_function_type (arg);
2006 type = build_pointer_type (type);
2008 else
2009 type = gfc_sym_type (arg);
2011 /* Parameter Passing Convention
2013 We currently pass all parameters by reference.
2014 Parameters with INTENT(IN) could be passed by value.
2015 The problem arises if a function is called via an implicit
2016 prototype. In this situation the INTENT is not known.
2017 For this reason all parameters to global functions must be
2018 passed by reference. Passing by value would potentially
2019 generate bad code. Worse there would be no way of telling that
2020 this code was bad, except that it would give incorrect results.
2022 Contained procedures could pass by value as these are never
2023 used without an explicit interface, and cannot be passed as
2024 actual parameters for a dummy procedure. */
2025 if (arg->ts.type == BT_CHARACTER)
2026 nstr++;
2027 typelist = gfc_chainon_list (typelist, type);
2029 else
2031 if (sym->attr.subroutine)
2032 alternate_return = 1;
2036 /* Add hidden string length parameters. */
2037 while (nstr--)
2038 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
2040 if (typelist)
2041 typelist = gfc_chainon_list (typelist, void_type_node);
2043 if (alternate_return)
2044 type = integer_type_node;
2045 else if (!sym->attr.function || gfc_return_by_reference (sym))
2046 type = void_type_node;
2047 else if (sym->attr.mixed_entry_master)
2048 type = gfc_get_mixed_entry_union (sym->ns);
2049 else if (gfc_option.flag_f2c
2050 && sym->ts.type == BT_REAL
2051 && sym->ts.kind == gfc_default_real_kind
2052 && !sym->attr.always_explicit)
2054 /* Special case: f2c calling conventions require that (scalar)
2055 default REAL functions return the C type double instead. f2c
2056 compatibility is only an issue with functions that don't
2057 require an explicit interface, as only these could be
2058 implemented in Fortran 77. */
2059 sym->ts.kind = gfc_default_double_kind;
2060 type = gfc_typenode_for_spec (&sym->ts);
2061 sym->ts.kind = gfc_default_real_kind;
2063 else
2064 type = gfc_sym_type (sym);
2066 type = build_function_type (type, typelist);
2068 return type;
2071 /* Language hooks for middle-end access to type nodes. */
2073 /* Return an integer type with BITS bits of precision,
2074 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2076 tree
2077 gfc_type_for_size (unsigned bits, int unsignedp)
2079 if (!unsignedp)
2081 int i;
2082 for (i = 0; i <= MAX_INT_KINDS; ++i)
2084 tree type = gfc_integer_types[i];
2085 if (type && bits == TYPE_PRECISION (type))
2086 return type;
2089 /* Handle TImode as a special case because it is used by some backends
2090 (eg. ARM) even though it is not available for normal use. */
2091 #if HOST_BITS_PER_WIDE_INT >= 64
2092 if (bits == TYPE_PRECISION (intTI_type_node))
2093 return intTI_type_node;
2094 #endif
2096 else
2098 if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
2099 return unsigned_intQI_type_node;
2100 if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
2101 return unsigned_intHI_type_node;
2102 if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
2103 return unsigned_intSI_type_node;
2104 if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
2105 return unsigned_intDI_type_node;
2106 if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
2107 return unsigned_intTI_type_node;
2110 return NULL_TREE;
2113 /* Return a data type that has machine mode MODE. If the mode is an
2114 integer, then UNSIGNEDP selects between signed and unsigned types. */
2116 tree
2117 gfc_type_for_mode (enum machine_mode mode, int unsignedp)
2119 int i;
2120 tree *base;
2122 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2123 base = gfc_real_types;
2124 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2125 base = gfc_complex_types;
2126 else if (SCALAR_INT_MODE_P (mode))
2127 return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
2128 else if (VECTOR_MODE_P (mode))
2130 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2131 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
2132 if (inner_type != NULL_TREE)
2133 return build_vector_type_for_mode (inner_type, mode);
2134 return NULL_TREE;
2136 else
2137 return NULL_TREE;
2139 for (i = 0; i <= MAX_REAL_KINDS; ++i)
2141 tree type = base[i];
2142 if (type && mode == TYPE_MODE (type))
2143 return type;
2146 return NULL_TREE;
2149 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
2150 in that case. */
2152 bool
2153 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
2155 int rank, dim;
2156 bool indirect = false;
2157 tree etype, ptype, field, t, base_decl;
2158 tree data_off, offset_off, dim_off, dim_size, elem_size;
2159 tree lower_suboff, upper_suboff, stride_suboff;
2161 if (! GFC_DESCRIPTOR_TYPE_P (type))
2163 if (! POINTER_TYPE_P (type))
2164 return false;
2165 type = TREE_TYPE (type);
2166 if (! GFC_DESCRIPTOR_TYPE_P (type))
2167 return false;
2168 indirect = true;
2171 rank = GFC_TYPE_ARRAY_RANK (type);
2172 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
2173 return false;
2175 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2176 gcc_assert (POINTER_TYPE_P (etype));
2177 etype = TREE_TYPE (etype);
2178 gcc_assert (TREE_CODE (etype) == ARRAY_TYPE);
2179 etype = TREE_TYPE (etype);
2180 /* Can't handle variable sized elements yet. */
2181 if (int_size_in_bytes (etype) <= 0)
2182 return false;
2183 /* Nor non-constant lower bounds in assumed shape arrays. */
2184 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE)
2186 for (dim = 0; dim < rank; dim++)
2187 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
2188 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
2189 return false;
2192 memset (info, '\0', sizeof (*info));
2193 info->ndimensions = rank;
2194 info->element_type = etype;
2195 ptype = build_pointer_type (gfc_array_index_type);
2196 if (indirect)
2198 info->base_decl = build_decl (VAR_DECL, NULL_TREE,
2199 build_pointer_type (ptype));
2200 base_decl = build1 (INDIRECT_REF, ptype, info->base_decl);
2202 else
2203 info->base_decl = base_decl = build_decl (VAR_DECL, NULL_TREE, ptype);
2205 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
2206 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
2207 data_off = byte_position (field);
2208 field = TREE_CHAIN (field);
2209 offset_off = byte_position (field);
2210 field = TREE_CHAIN (field);
2211 field = TREE_CHAIN (field);
2212 dim_off = byte_position (field);
2213 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
2214 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
2215 stride_suboff = byte_position (field);
2216 field = TREE_CHAIN (field);
2217 lower_suboff = byte_position (field);
2218 field = TREE_CHAIN (field);
2219 upper_suboff = byte_position (field);
2221 t = base_decl;
2222 if (!integer_zerop (data_off))
2223 t = build2 (POINTER_PLUS_EXPR, ptype, t, data_off);
2224 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
2225 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
2226 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
2227 info->allocated = build2 (NE_EXPR, boolean_type_node,
2228 info->data_location, null_pointer_node);
2229 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER)
2230 info->associated = build2 (NE_EXPR, boolean_type_node,
2231 info->data_location, null_pointer_node);
2233 for (dim = 0; dim < rank; dim++)
2235 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2236 size_binop (PLUS_EXPR, dim_off, lower_suboff));
2237 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2238 info->dimen[dim].lower_bound = t;
2239 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2240 size_binop (PLUS_EXPR, dim_off, upper_suboff));
2241 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2242 info->dimen[dim].upper_bound = t;
2243 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE)
2245 /* Assumed shape arrays have known lower bounds. */
2246 info->dimen[dim].upper_bound
2247 = build2 (MINUS_EXPR, gfc_array_index_type,
2248 info->dimen[dim].upper_bound,
2249 info->dimen[dim].lower_bound);
2250 info->dimen[dim].lower_bound
2251 = fold_convert (gfc_array_index_type,
2252 GFC_TYPE_ARRAY_LBOUND (type, dim));
2253 info->dimen[dim].upper_bound
2254 = build2 (PLUS_EXPR, gfc_array_index_type,
2255 info->dimen[dim].lower_bound,
2256 info->dimen[dim].upper_bound);
2258 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2259 size_binop (PLUS_EXPR, dim_off, stride_suboff));
2260 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2261 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
2262 info->dimen[dim].stride = t;
2263 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
2266 return true;
2269 #include "gt-fortran-trans-types.h"