invoke.texi (powerpc msdata-data): Static data doesn't go in small data sections.
[official-gcc.git] / gcc / fortran / trans-types.c
blob6aaf81a5b4c864d44cadebf7569f61fc961010c2
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002, 2003, 2004, 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
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 "tm.h"
30 #include "target.h"
31 #include "ggc.h"
32 #include "toplev.h"
33 #include "gfortran.h"
34 #include "trans.h"
35 #include "trans-types.h"
36 #include "trans-const.h"
37 #include "real.h"
40 #if (GFC_MAX_DIMENSIONS < 10)
41 #define GFC_RANK_DIGITS 1
42 #define GFC_RANK_PRINTF_FORMAT "%01d"
43 #elif (GFC_MAX_DIMENSIONS < 100)
44 #define GFC_RANK_DIGITS 2
45 #define GFC_RANK_PRINTF_FORMAT "%02d"
46 #else
47 #error If you really need >99 dimensions, continue the sequence above...
48 #endif
50 static tree gfc_get_derived_type (gfc_symbol * derived);
52 tree gfc_array_index_type;
53 tree gfc_array_range_type;
54 tree gfc_character1_type_node;
55 tree pvoid_type_node;
56 tree ppvoid_type_node;
57 tree pchar_type_node;
59 tree gfc_charlen_type_node;
61 static GTY(()) tree gfc_desc_dim_type;
62 static GTY(()) tree gfc_max_array_element_size;
63 static GTY(()) tree gfc_array_descriptor_base[GFC_MAX_DIMENSIONS];
65 /* Arrays for all integral and real kinds. We'll fill this in at runtime
66 after the target has a chance to process command-line options. */
68 #define MAX_INT_KINDS 5
69 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
70 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
71 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
72 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
74 #define MAX_REAL_KINDS 5
75 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
76 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
77 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
79 /* The integer kind to use for array indices. This will be set to the
80 proper value based on target information from the backend. */
82 int gfc_index_integer_kind;
84 /* The default kinds of the various types. */
86 int gfc_default_integer_kind;
87 int gfc_max_integer_kind;
88 int gfc_default_real_kind;
89 int gfc_default_double_kind;
90 int gfc_default_character_kind;
91 int gfc_default_logical_kind;
92 int gfc_default_complex_kind;
93 int gfc_c_int_kind;
95 /* Query the target to determine which machine modes are available for
96 computation. Choose KIND numbers for them. */
98 void
99 gfc_init_kinds (void)
101 enum machine_mode mode;
102 int i_index, r_index;
103 bool saw_i4 = false, saw_i8 = false;
104 bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
106 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
108 int kind, bitsize;
110 if (!targetm.scalar_mode_supported_p (mode))
111 continue;
113 /* The middle end doesn't support constants larger than 2*HWI.
114 Perhaps the target hook shouldn't have accepted these either,
115 but just to be safe... */
116 bitsize = GET_MODE_BITSIZE (mode);
117 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
118 continue;
120 gcc_assert (i_index != MAX_INT_KINDS);
122 /* Let the kind equal the bit size divided by 8. This insulates the
123 programmer from the underlying byte size. */
124 kind = bitsize / 8;
126 if (kind == 4)
127 saw_i4 = true;
128 if (kind == 8)
129 saw_i8 = true;
131 gfc_integer_kinds[i_index].kind = kind;
132 gfc_integer_kinds[i_index].radix = 2;
133 gfc_integer_kinds[i_index].digits = bitsize - 1;
134 gfc_integer_kinds[i_index].bit_size = bitsize;
136 gfc_logical_kinds[i_index].kind = kind;
137 gfc_logical_kinds[i_index].bit_size = bitsize;
139 i_index += 1;
142 /* Set the maximum integer kind. Used with at least BOZ constants. */
143 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
145 for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
147 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
148 int kind;
150 if (fmt == NULL)
151 continue;
152 if (!targetm.scalar_mode_supported_p (mode))
153 continue;
155 /* Only let float/double/long double go through because the fortran
156 library assumes these are the only floating point types. */
158 if (mode != TYPE_MODE (float_type_node)
159 && (mode != TYPE_MODE (double_type_node))
160 && (mode != TYPE_MODE (long_double_type_node)))
161 continue;
163 /* Let the kind equal the precision divided by 8, rounding up. Again,
164 this insulates the programmer from the underlying byte size.
166 Also, it effectively deals with IEEE extended formats. There, the
167 total size of the type may equal 16, but it's got 6 bytes of padding
168 and the increased size can get in the way of a real IEEE quad format
169 which may also be supported by the target.
171 We round up so as to handle IA-64 __floatreg (RFmode), which is an
172 82 bit type. Not to be confused with __float80 (XFmode), which is
173 an 80 bit type also supported by IA-64. So XFmode should come out
174 to be kind=10, and RFmode should come out to be kind=11. Egads. */
176 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
178 if (kind == 4)
179 saw_r4 = true;
180 if (kind == 8)
181 saw_r8 = true;
182 if (kind == 16)
183 saw_r16 = true;
185 /* Careful we don't stumble a wierd internal mode. */
186 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
187 /* Or have too many modes for the allocated space. */
188 gcc_assert (r_index != MAX_REAL_KINDS);
190 gfc_real_kinds[r_index].kind = kind;
191 gfc_real_kinds[r_index].radix = fmt->b;
192 gfc_real_kinds[r_index].digits = fmt->p;
193 gfc_real_kinds[r_index].min_exponent = fmt->emin;
194 gfc_real_kinds[r_index].max_exponent = fmt->emax;
195 if (fmt->pnan < fmt->p)
196 /* This is an IBM extended double format (or the MIPS variant)
197 made up of two IEEE doubles. The value of the long double is
198 the sum of the values of the two parts. The most significant
199 part is required to be the value of the long double rounded
200 to the nearest double. If we use emax of 1024 then we can't
201 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
202 rounding will make the most significant part overflow. */
203 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
204 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
205 r_index += 1;
208 /* Choose the default integer kind. We choose 4 unless the user
209 directs us otherwise. */
210 if (gfc_option.flag_default_integer)
212 if (!saw_i8)
213 fatal_error ("integer kind=8 not available for -fdefault-integer-8 option");
214 gfc_default_integer_kind = 8;
216 else if (saw_i4)
217 gfc_default_integer_kind = 4;
218 else
219 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
221 /* Choose the default real kind. Again, we choose 4 when possible. */
222 if (gfc_option.flag_default_real)
224 if (!saw_r8)
225 fatal_error ("real kind=8 not available for -fdefault-real-8 option");
226 gfc_default_real_kind = 8;
228 else if (saw_r4)
229 gfc_default_real_kind = 4;
230 else
231 gfc_default_real_kind = gfc_real_kinds[0].kind;
233 /* Choose the default double kind. If -fdefault-real and -fdefault-double
234 are specified, we use kind=8, if it's available. If -fdefault-real is
235 specified without -fdefault-double, we use kind=16, if it's available.
236 Otherwise we do not change anything. */
237 if (gfc_option.flag_default_double && !gfc_option.flag_default_real)
238 fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8");
240 if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8)
241 gfc_default_double_kind = 8;
242 else if (gfc_option.flag_default_real && saw_r16)
243 gfc_default_double_kind = 16;
244 else if (saw_r4 && saw_r8)
245 gfc_default_double_kind = 8;
246 else
248 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
249 real ... occupies two contiguous numeric storage units.
251 Therefore we must be supplied a kind twice as large as we chose
252 for single precision. There are loopholes, in that double
253 precision must *occupy* two storage units, though it doesn't have
254 to *use* two storage units. Which means that you can make this
255 kind artificially wide by padding it. But at present there are
256 no GCC targets for which a two-word type does not exist, so we
257 just let gfc_validate_kind abort and tell us if something breaks. */
259 gfc_default_double_kind
260 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
263 /* The default logical kind is constrained to be the same as the
264 default integer kind. Similarly with complex and real. */
265 gfc_default_logical_kind = gfc_default_integer_kind;
266 gfc_default_complex_kind = gfc_default_real_kind;
268 /* Choose the smallest integer kind for our default character. */
269 gfc_default_character_kind = gfc_integer_kinds[0].kind;
271 /* Choose the integer kind the same size as "void*" for our index kind. */
272 gfc_index_integer_kind = POINTER_SIZE / 8;
273 /* Pick a kind the same size as the C "int" type. */
274 gfc_c_int_kind = INT_TYPE_SIZE / 8;
277 /* Make sure that a valid kind is present. Returns an index into the
278 associated kinds array, -1 if the kind is not present. */
280 static int
281 validate_integer (int kind)
283 int i;
285 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
286 if (gfc_integer_kinds[i].kind == kind)
287 return i;
289 return -1;
292 static int
293 validate_real (int kind)
295 int i;
297 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
298 if (gfc_real_kinds[i].kind == kind)
299 return i;
301 return -1;
304 static int
305 validate_logical (int kind)
307 int i;
309 for (i = 0; gfc_logical_kinds[i].kind; i++)
310 if (gfc_logical_kinds[i].kind == kind)
311 return i;
313 return -1;
316 static int
317 validate_character (int kind)
319 return kind == gfc_default_character_kind ? 0 : -1;
322 /* Validate a kind given a basic type. The return value is the same
323 for the child functions, with -1 indicating nonexistence of the
324 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
327 gfc_validate_kind (bt type, int kind, bool may_fail)
329 int rc;
331 switch (type)
333 case BT_REAL: /* Fall through */
334 case BT_COMPLEX:
335 rc = validate_real (kind);
336 break;
337 case BT_INTEGER:
338 rc = validate_integer (kind);
339 break;
340 case BT_LOGICAL:
341 rc = validate_logical (kind);
342 break;
343 case BT_CHARACTER:
344 rc = validate_character (kind);
345 break;
347 default:
348 gfc_internal_error ("gfc_validate_kind(): Got bad type");
351 if (rc < 0 && !may_fail)
352 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
354 return rc;
358 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
359 Reuse common type nodes where possible. Recognize if the kind matches up
360 with a C type. This will be used later in determining which routines may
361 be scarfed from libm. */
363 static tree
364 gfc_build_int_type (gfc_integer_info *info)
366 int mode_precision = info->bit_size;
368 if (mode_precision == CHAR_TYPE_SIZE)
369 info->c_char = 1;
370 if (mode_precision == SHORT_TYPE_SIZE)
371 info->c_short = 1;
372 if (mode_precision == INT_TYPE_SIZE)
373 info->c_int = 1;
374 if (mode_precision == LONG_TYPE_SIZE)
375 info->c_long = 1;
376 if (mode_precision == LONG_LONG_TYPE_SIZE)
377 info->c_long_long = 1;
379 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
380 return intQI_type_node;
381 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
382 return intHI_type_node;
383 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
384 return intSI_type_node;
385 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
386 return intDI_type_node;
387 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
388 return intTI_type_node;
390 return make_signed_type (mode_precision);
393 static tree
394 gfc_build_real_type (gfc_real_info *info)
396 int mode_precision = info->mode_precision;
397 tree new_type;
399 if (mode_precision == FLOAT_TYPE_SIZE)
400 info->c_float = 1;
401 if (mode_precision == DOUBLE_TYPE_SIZE)
402 info->c_double = 1;
403 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
404 info->c_long_double = 1;
406 if (TYPE_PRECISION (float_type_node) == mode_precision)
407 return float_type_node;
408 if (TYPE_PRECISION (double_type_node) == mode_precision)
409 return double_type_node;
410 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
411 return long_double_type_node;
413 new_type = make_node (REAL_TYPE);
414 TYPE_PRECISION (new_type) = mode_precision;
415 layout_type (new_type);
416 return new_type;
419 static tree
420 gfc_build_complex_type (tree scalar_type)
422 tree new_type;
424 if (scalar_type == NULL)
425 return NULL;
426 if (scalar_type == float_type_node)
427 return complex_float_type_node;
428 if (scalar_type == double_type_node)
429 return complex_double_type_node;
430 if (scalar_type == long_double_type_node)
431 return complex_long_double_type_node;
433 new_type = make_node (COMPLEX_TYPE);
434 TREE_TYPE (new_type) = scalar_type;
435 layout_type (new_type);
436 return new_type;
439 static tree
440 gfc_build_logical_type (gfc_logical_info *info)
442 int bit_size = info->bit_size;
443 tree new_type;
445 if (bit_size == BOOL_TYPE_SIZE)
447 info->c_bool = 1;
448 return boolean_type_node;
451 new_type = make_unsigned_type (bit_size);
452 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
453 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
454 TYPE_PRECISION (new_type) = 1;
456 return new_type;
459 #if 0
460 /* Return the bit size of the C "size_t". */
462 static unsigned int
463 c_size_t_size (void)
465 #ifdef SIZE_TYPE
466 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
467 return INT_TYPE_SIZE;
468 if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
469 return LONG_TYPE_SIZE;
470 if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
471 return SHORT_TYPE_SIZE;
472 gcc_unreachable ();
473 #else
474 return LONG_TYPE_SIZE;
475 #endif
477 #endif
479 /* Create the backend type nodes. We map them to their
480 equivalent C type, at least for now. We also give
481 names to the types here, and we push them in the
482 global binding level context.*/
484 void
485 gfc_init_types (void)
487 char name_buf[16];
488 int index;
489 tree type;
490 unsigned n;
491 unsigned HOST_WIDE_INT hi;
492 unsigned HOST_WIDE_INT lo;
494 /* Create and name the types. */
495 #define PUSH_TYPE(name, node) \
496 pushdecl (build_decl (TYPE_DECL, get_identifier (name), node))
498 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
500 type = gfc_build_int_type (&gfc_integer_kinds[index]);
501 gfc_integer_types[index] = type;
502 snprintf (name_buf, sizeof(name_buf), "int%d",
503 gfc_integer_kinds[index].kind);
504 PUSH_TYPE (name_buf, type);
507 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
509 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
510 gfc_logical_types[index] = type;
511 snprintf (name_buf, sizeof(name_buf), "logical%d",
512 gfc_logical_kinds[index].kind);
513 PUSH_TYPE (name_buf, type);
516 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
518 type = gfc_build_real_type (&gfc_real_kinds[index]);
519 gfc_real_types[index] = type;
520 snprintf (name_buf, sizeof(name_buf), "real%d",
521 gfc_real_kinds[index].kind);
522 PUSH_TYPE (name_buf, type);
524 type = gfc_build_complex_type (type);
525 gfc_complex_types[index] = type;
526 snprintf (name_buf, sizeof(name_buf), "complex%d",
527 gfc_real_kinds[index].kind);
528 PUSH_TYPE (name_buf, type);
531 gfc_character1_type_node = build_type_variant (unsigned_char_type_node,
532 0, 0);
533 PUSH_TYPE ("char", gfc_character1_type_node);
535 PUSH_TYPE ("byte", unsigned_char_type_node);
536 PUSH_TYPE ("void", void_type_node);
538 /* DBX debugging output gets upset if these aren't set. */
539 if (!TYPE_NAME (integer_type_node))
540 PUSH_TYPE ("c_integer", integer_type_node);
541 if (!TYPE_NAME (char_type_node))
542 PUSH_TYPE ("c_char", char_type_node);
544 #undef PUSH_TYPE
546 pvoid_type_node = build_pointer_type (void_type_node);
547 ppvoid_type_node = build_pointer_type (pvoid_type_node);
548 pchar_type_node = build_pointer_type (gfc_character1_type_node);
550 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
551 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
552 since this function is called before gfc_init_constants. */
553 gfc_array_range_type
554 = build_range_type (gfc_array_index_type,
555 build_int_cst (gfc_array_index_type, 0),
556 NULL_TREE);
558 /* The maximum array element size that can be handled is determined
559 by the number of bits available to store this field in the array
560 descriptor. */
562 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
563 lo = ~ (unsigned HOST_WIDE_INT) 0;
564 if (n > HOST_BITS_PER_WIDE_INT)
565 hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
566 else
567 hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
568 gfc_max_array_element_size
569 = build_int_cst_wide (long_unsigned_type_node, lo, hi);
571 size_type_node = gfc_array_index_type;
573 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
574 boolean_true_node = build_int_cst (boolean_type_node, 1);
575 boolean_false_node = build_int_cst (boolean_type_node, 0);
577 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
578 gfc_charlen_type_node = gfc_get_int_type (4);
581 /* Get the type node for the given type and kind. */
583 tree
584 gfc_get_int_type (int kind)
586 int index = gfc_validate_kind (BT_INTEGER, kind, true);
587 return index < 0 ? 0 : gfc_integer_types[index];
590 tree
591 gfc_get_real_type (int kind)
593 int index = gfc_validate_kind (BT_REAL, kind, true);
594 return index < 0 ? 0 : gfc_real_types[index];
597 tree
598 gfc_get_complex_type (int kind)
600 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
601 return index < 0 ? 0 : gfc_complex_types[index];
604 tree
605 gfc_get_logical_type (int kind)
607 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
608 return index < 0 ? 0 : gfc_logical_types[index];
611 /* Create a character type with the given kind and length. */
613 tree
614 gfc_get_character_type_len (int kind, tree len)
616 tree bounds, type;
618 gfc_validate_kind (BT_CHARACTER, kind, false);
620 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
621 type = build_array_type (gfc_character1_type_node, bounds);
622 TYPE_STRING_FLAG (type) = 1;
624 return type;
628 /* Get a type node for a character kind. */
630 tree
631 gfc_get_character_type (int kind, gfc_charlen * cl)
633 tree len;
635 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
637 return gfc_get_character_type_len (kind, len);
640 /* Covert a basic type. This will be an array for character types. */
642 tree
643 gfc_typenode_for_spec (gfc_typespec * spec)
645 tree basetype;
647 switch (spec->type)
649 case BT_UNKNOWN:
650 gcc_unreachable ();
652 case BT_INTEGER:
653 basetype = gfc_get_int_type (spec->kind);
654 break;
656 case BT_REAL:
657 basetype = gfc_get_real_type (spec->kind);
658 break;
660 case BT_COMPLEX:
661 basetype = gfc_get_complex_type (spec->kind);
662 break;
664 case BT_LOGICAL:
665 basetype = gfc_get_logical_type (spec->kind);
666 break;
668 case BT_CHARACTER:
669 basetype = gfc_get_character_type (spec->kind, spec->cl);
670 break;
672 case BT_DERIVED:
673 basetype = gfc_get_derived_type (spec->derived);
674 break;
676 default:
677 gcc_unreachable ();
679 return basetype;
682 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
684 static tree
685 gfc_conv_array_bound (gfc_expr * expr)
687 /* If expr is an integer constant, return that. */
688 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
689 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
691 /* Otherwise return NULL. */
692 return NULL_TREE;
695 tree
696 gfc_get_element_type (tree type)
698 tree element;
700 if (GFC_ARRAY_TYPE_P (type))
702 if (TREE_CODE (type) == POINTER_TYPE)
703 type = TREE_TYPE (type);
704 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
705 element = TREE_TYPE (type);
707 else
709 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
710 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
712 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
713 element = TREE_TYPE (element);
715 gcc_assert (TREE_CODE (element) == ARRAY_TYPE);
716 element = TREE_TYPE (element);
719 return element;
722 /* Build an array. This function is called from gfc_sym_type().
723 Actually returns array descriptor type.
725 Format of array descriptors is as follows:
727 struct gfc_array_descriptor
729 array *data
730 index offset;
731 index dtype;
732 struct descriptor_dimension dimension[N_DIM];
735 struct descriptor_dimension
737 index stride;
738 index lbound;
739 index ubound;
742 Translation code should use gfc_conv_descriptor_* rather than accessing
743 the descriptor directly. Any changes to the array descriptor type will
744 require changes in gfc_conv_descriptor_* and gfc_build_array_initializer.
746 This is represented internally as a RECORD_TYPE. The index nodes are
747 gfc_array_index_type and the data node is a pointer to the data. See below
748 for the handling of character types.
750 The dtype member is formatted as follows:
751 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
752 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
753 size = dtype >> GFC_DTYPE_SIZE_SHIFT
755 I originally used nested ARRAY_TYPE nodes to represent arrays, but this
756 generated poor code for assumed/deferred size arrays. These require
757 use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part of the GENERIC
758 grammar. Also, there is no way to explicitly set the array stride, so
759 all data must be packed(1). I've tried to mark all the functions which
760 would require modification with a GCC ARRAYS comment.
762 The data component points to the first element in the array.
763 The offset field is the position of the origin of the array
764 (ie element (0, 0 ...)). This may be outsite the bounds of the array.
766 An element is accessed by
767 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
768 This gives good performance as the computation does not involve the
769 bounds of the array. For packed arrays, this is optimized further by
770 substituting the known strides.
772 This system has one problem: all array bounds must be withing 2^31 elements
773 of the origin (2^63 on 64-bit machines). For example
774 integer, dimension (80000:90000, 80000:90000, 2) :: array
775 may not work properly on 32-bit machines because 80000*80000 > 2^31, so
776 the calculation for stride02 would overflow. This may still work, but
777 I haven't checked, and it relies on the overflow doing the right thing.
779 The way to fix this problem is to access elements as follows:
780 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
781 Obviously this is much slower. I will make this a compile time option,
782 something like -fsmall-array-offsets. Mixing code compiled with and without
783 this switch will work.
785 (1) This can be worked around by modifying the upper bound of the previous
786 dimension. This requires extra fields in the descriptor (both real_ubound
787 and fake_ubound). In tree.def there is mention of TYPE_SEP, which
788 may allow us to do this. However I can't find mention of this anywhere
789 else. */
792 /* Returns true if the array sym does not require a descriptor. */
795 gfc_is_nodesc_array (gfc_symbol * sym)
797 gcc_assert (sym->attr.dimension);
799 /* We only want local arrays. */
800 if (sym->attr.pointer || sym->attr.allocatable)
801 return 0;
803 if (sym->attr.dummy)
805 if (sym->as->type != AS_ASSUMED_SHAPE)
806 return 1;
807 else
808 return 0;
811 if (sym->attr.result || sym->attr.function)
812 return 0;
814 gcc_assert (sym->as->type == AS_EXPLICIT);
816 return 1;
820 /* Create an array descriptor type. */
822 static tree
823 gfc_build_array_type (tree type, gfc_array_spec * as)
825 tree lbound[GFC_MAX_DIMENSIONS];
826 tree ubound[GFC_MAX_DIMENSIONS];
827 int n;
829 for (n = 0; n < as->rank; n++)
831 /* Create expressions for the known bounds of the array. */
832 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
833 lbound[n] = gfc_index_one_node;
834 else
835 lbound[n] = gfc_conv_array_bound (as->lower[n]);
836 ubound[n] = gfc_conv_array_bound (as->upper[n]);
839 return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0);
842 /* Returns the struct descriptor_dimension type. */
844 static tree
845 gfc_get_desc_dim_type (void)
847 tree type;
848 tree decl;
849 tree fieldlist;
851 if (gfc_desc_dim_type)
852 return gfc_desc_dim_type;
854 /* Build the type node. */
855 type = make_node (RECORD_TYPE);
857 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
858 TYPE_PACKED (type) = 1;
860 /* Consists of the stride, lbound and ubound members. */
861 decl = build_decl (FIELD_DECL,
862 get_identifier ("stride"), gfc_array_index_type);
863 DECL_CONTEXT (decl) = type;
864 fieldlist = decl;
866 decl = build_decl (FIELD_DECL,
867 get_identifier ("lbound"), gfc_array_index_type);
868 DECL_CONTEXT (decl) = type;
869 fieldlist = chainon (fieldlist, decl);
871 decl = build_decl (FIELD_DECL,
872 get_identifier ("ubound"), gfc_array_index_type);
873 DECL_CONTEXT (decl) = type;
874 fieldlist = chainon (fieldlist, decl);
876 /* Finish off the type. */
877 TYPE_FIELDS (type) = fieldlist;
879 gfc_finish_type (type);
881 gfc_desc_dim_type = type;
882 return type;
886 /* Return the DTYPE for an array. This describes the type and type parameters
887 of the array. */
888 /* TODO: Only call this when the value is actually used, and make all the
889 unknown cases abort. */
891 tree
892 gfc_get_dtype (tree type)
894 tree size;
895 int n;
896 HOST_WIDE_INT i;
897 tree tmp;
898 tree dtype;
899 tree etype;
900 int rank;
902 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
904 if (GFC_TYPE_ARRAY_DTYPE (type))
905 return GFC_TYPE_ARRAY_DTYPE (type);
907 rank = GFC_TYPE_ARRAY_RANK (type);
908 etype = gfc_get_element_type (type);
910 switch (TREE_CODE (etype))
912 case INTEGER_TYPE:
913 n = GFC_DTYPE_INTEGER;
914 break;
916 case BOOLEAN_TYPE:
917 n = GFC_DTYPE_LOGICAL;
918 break;
920 case REAL_TYPE:
921 n = GFC_DTYPE_REAL;
922 break;
924 case COMPLEX_TYPE:
925 n = GFC_DTYPE_COMPLEX;
926 break;
928 /* We will never have arrays of arrays. */
929 case RECORD_TYPE:
930 n = GFC_DTYPE_DERIVED;
931 break;
933 case ARRAY_TYPE:
934 n = GFC_DTYPE_CHARACTER;
935 break;
937 default:
938 /* TODO: Don't do dtype for temporary descriptorless arrays. */
939 /* We can strange array types for temporary arrays. */
940 return gfc_index_zero_node;
943 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
944 size = TYPE_SIZE_UNIT (etype);
946 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
947 if (size && INTEGER_CST_P (size))
949 if (tree_int_cst_lt (gfc_max_array_element_size, size))
950 internal_error ("Array element size too big");
952 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
954 dtype = build_int_cst (gfc_array_index_type, i);
956 if (size && !INTEGER_CST_P (size))
958 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
959 tmp = fold_build2 (LSHIFT_EXPR, gfc_array_index_type, size, tmp);
960 dtype = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype);
962 /* If we don't know the size we leave it as zero. This should never happen
963 for anything that is actually used. */
964 /* TODO: Check this is actually true, particularly when repacking
965 assumed size parameters. */
967 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
968 return dtype;
972 /* Build an array type for use without a descriptor. Valid values of packed
973 are 0=no, 1=partial, 2=full, 3=static. */
975 tree
976 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, int packed)
978 tree range;
979 tree type;
980 tree tmp;
981 int n;
982 int known_stride;
983 int known_offset;
984 mpz_t offset;
985 mpz_t stride;
986 mpz_t delta;
987 gfc_expr *expr;
989 mpz_init_set_ui (offset, 0);
990 mpz_init_set_ui (stride, 1);
991 mpz_init (delta);
993 /* We don't use build_array_type because this does not include include
994 lang-specific information (i.e. the bounds of the array) when checking
995 for duplicates. */
996 type = make_node (ARRAY_TYPE);
998 GFC_ARRAY_TYPE_P (type) = 1;
999 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
1000 ggc_alloc_cleared (sizeof (struct lang_type));
1002 known_stride = (packed != 0);
1003 known_offset = 1;
1004 for (n = 0; n < as->rank; n++)
1006 /* Fill in the stride and bound components of the type. */
1007 if (known_stride)
1008 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1009 else
1010 tmp = NULL_TREE;
1011 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1013 expr = as->lower[n];
1014 if (expr->expr_type == EXPR_CONSTANT)
1016 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1017 gfc_index_integer_kind);
1019 else
1021 known_stride = 0;
1022 tmp = NULL_TREE;
1024 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1026 if (known_stride)
1028 /* Calculate the offset. */
1029 mpz_mul (delta, stride, as->lower[n]->value.integer);
1030 mpz_sub (offset, offset, delta);
1032 else
1033 known_offset = 0;
1035 expr = as->upper[n];
1036 if (expr && expr->expr_type == EXPR_CONSTANT)
1038 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1039 gfc_index_integer_kind);
1041 else
1043 tmp = NULL_TREE;
1044 known_stride = 0;
1046 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1048 if (known_stride)
1050 /* Calculate the stride. */
1051 mpz_sub (delta, as->upper[n]->value.integer,
1052 as->lower[n]->value.integer);
1053 mpz_add_ui (delta, delta, 1);
1054 mpz_mul (stride, stride, delta);
1057 /* Only the first stride is known for partial packed arrays. */
1058 if (packed < 2)
1059 known_stride = 0;
1062 if (known_offset)
1064 GFC_TYPE_ARRAY_OFFSET (type) =
1065 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1067 else
1068 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1070 if (known_stride)
1072 GFC_TYPE_ARRAY_SIZE (type) =
1073 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1075 else
1076 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1078 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1079 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1080 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1081 NULL_TREE);
1082 /* TODO: use main type if it is unbounded. */
1083 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1084 build_pointer_type (build_array_type (etype, range));
1086 if (known_stride)
1088 mpz_sub_ui (stride, stride, 1);
1089 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1091 else
1092 range = NULL_TREE;
1094 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1095 TYPE_DOMAIN (type) = range;
1097 build_pointer_type (etype);
1098 TREE_TYPE (type) = etype;
1100 layout_type (type);
1102 mpz_clear (offset);
1103 mpz_clear (stride);
1104 mpz_clear (delta);
1106 if (packed < 3 || !known_stride)
1108 /* For dummy arrays and automatic (heap allocated) arrays we
1109 want a pointer to the array. */
1110 type = build_pointer_type (type);
1111 GFC_ARRAY_TYPE_P (type) = 1;
1112 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1114 return type;
1117 /* Return or create the base type for an array descriptor. */
1119 static tree
1120 gfc_get_array_descriptor_base (int dimen)
1122 tree fat_type, fieldlist, decl, arraytype;
1123 char name[16 + GFC_RANK_DIGITS + 1];
1125 gcc_assert (dimen >= 1 && dimen <= GFC_MAX_DIMENSIONS);
1126 if (gfc_array_descriptor_base[dimen - 1])
1127 return gfc_array_descriptor_base[dimen - 1];
1129 /* Build the type node. */
1130 fat_type = make_node (RECORD_TYPE);
1132 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen);
1133 TYPE_NAME (fat_type) = get_identifier (name);
1135 /* Add the data member as the first element of the descriptor. */
1136 decl = build_decl (FIELD_DECL, get_identifier ("data"), ptr_type_node);
1138 DECL_CONTEXT (decl) = fat_type;
1139 fieldlist = decl;
1141 /* Add the base component. */
1142 decl = build_decl (FIELD_DECL, get_identifier ("offset"),
1143 gfc_array_index_type);
1144 DECL_CONTEXT (decl) = fat_type;
1145 fieldlist = chainon (fieldlist, decl);
1147 /* Add the dtype component. */
1148 decl = build_decl (FIELD_DECL, get_identifier ("dtype"),
1149 gfc_array_index_type);
1150 DECL_CONTEXT (decl) = fat_type;
1151 fieldlist = chainon (fieldlist, decl);
1153 /* Build the array type for the stride and bound components. */
1154 arraytype =
1155 build_array_type (gfc_get_desc_dim_type (),
1156 build_range_type (gfc_array_index_type,
1157 gfc_index_zero_node,
1158 gfc_rank_cst[dimen - 1]));
1160 decl = build_decl (FIELD_DECL, get_identifier ("dim"), arraytype);
1161 DECL_CONTEXT (decl) = fat_type;
1162 fieldlist = chainon (fieldlist, decl);
1164 /* Finish off the type. */
1165 TYPE_FIELDS (fat_type) = fieldlist;
1167 gfc_finish_type (fat_type);
1169 gfc_array_descriptor_base[dimen - 1] = fat_type;
1170 return fat_type;
1173 /* Build an array (descriptor) type with given bounds. */
1175 tree
1176 gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound,
1177 tree * ubound, int packed)
1179 char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN];
1180 tree fat_type, base_type, arraytype, lower, upper, stride, tmp;
1181 const char *typename;
1182 int n;
1184 base_type = gfc_get_array_descriptor_base (dimen);
1185 fat_type = build_variant_type_copy (base_type);
1187 tmp = TYPE_NAME (etype);
1188 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1189 tmp = DECL_NAME (tmp);
1190 if (tmp)
1191 typename = IDENTIFIER_POINTER (tmp);
1192 else
1193 typename = "unknown";
1194 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen,
1195 GFC_MAX_SYMBOL_LEN, typename);
1196 TYPE_NAME (fat_type) = get_identifier (name);
1198 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1199 TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *)
1200 ggc_alloc_cleared (sizeof (struct lang_type));
1202 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1203 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1205 /* Build an array descriptor record type. */
1206 if (packed != 0)
1207 stride = gfc_index_one_node;
1208 else
1209 stride = NULL_TREE;
1210 for (n = 0; n < dimen; n++)
1212 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1214 if (lbound)
1215 lower = lbound[n];
1216 else
1217 lower = NULL_TREE;
1219 if (lower != NULL_TREE)
1221 if (INTEGER_CST_P (lower))
1222 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1223 else
1224 lower = NULL_TREE;
1227 upper = ubound[n];
1228 if (upper != NULL_TREE)
1230 if (INTEGER_CST_P (upper))
1231 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1232 else
1233 upper = NULL_TREE;
1236 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1238 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
1239 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1240 gfc_index_one_node);
1241 stride =
1242 fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, stride);
1243 /* Check the folding worked. */
1244 gcc_assert (INTEGER_CST_P (stride));
1246 else
1247 stride = NULL_TREE;
1249 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1251 /* TODO: known offsets for descriptors. */
1252 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1254 /* We define data as an unknown size array. Much better than doing
1255 pointer arithmetic. */
1256 arraytype =
1257 build_array_type (etype, gfc_array_range_type);
1258 arraytype = build_pointer_type (arraytype);
1259 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1261 return fat_type;
1264 /* Build a pointer type. This function is called from gfc_sym_type(). */
1266 static tree
1267 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1269 /* Array pointer types aren't actually pointers. */
1270 if (sym->attr.dimension)
1271 return type;
1272 else
1273 return build_pointer_type (type);
1276 /* Return the type for a symbol. Special handling is required for character
1277 types to get the correct level of indirection.
1278 For functions return the return type.
1279 For subroutines return void_type_node.
1280 Calling this multiple times for the same symbol should be avoided,
1281 especially for character and array types. */
1283 tree
1284 gfc_sym_type (gfc_symbol * sym)
1286 tree type;
1287 int byref;
1289 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1290 return void_type_node;
1292 if (sym->backend_decl)
1294 if (sym->attr.function)
1295 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
1296 else
1297 return TREE_TYPE (sym->backend_decl);
1300 type = gfc_typenode_for_spec (&sym->ts);
1301 if (gfc_option.flag_f2c
1302 && sym->attr.function
1303 && sym->ts.type == BT_REAL
1304 && sym->ts.kind == gfc_default_real_kind
1305 && !sym->attr.always_explicit)
1307 /* Special case: f2c calling conventions require that (scalar)
1308 default REAL functions return the C type double instead. */
1309 sym->ts.kind = gfc_default_double_kind;
1310 type = gfc_typenode_for_spec (&sym->ts);
1311 sym->ts.kind = gfc_default_real_kind;
1314 if (sym->attr.dummy && !sym->attr.function)
1315 byref = 1;
1316 else
1317 byref = 0;
1319 if (sym->attr.dimension)
1321 if (gfc_is_nodesc_array (sym))
1323 /* If this is a character argument of unknown length, just use the
1324 base type. */
1325 if (sym->ts.type != BT_CHARACTER
1326 || !(sym->attr.dummy || sym->attr.function)
1327 || sym->ts.cl->backend_decl)
1329 type = gfc_get_nodesc_array_type (type, sym->as,
1330 byref ? 2 : 3);
1331 byref = 0;
1334 else
1335 type = gfc_build_array_type (type, sym->as);
1337 else
1339 if (sym->attr.allocatable || sym->attr.pointer)
1340 type = gfc_build_pointer_type (sym, type);
1343 /* We currently pass all parameters by reference.
1344 See f95_get_function_decl. For dummy function parameters return the
1345 function type. */
1346 if (byref)
1348 /* We must use pointer types for potentially absent variables. The
1349 optimizers assume a reference type argument is never NULL. */
1350 if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1351 type = build_pointer_type (type);
1352 else
1353 type = build_reference_type (type);
1356 return (type);
1359 /* Layout and output debug info for a record type. */
1361 void
1362 gfc_finish_type (tree type)
1364 tree decl;
1366 decl = build_decl (TYPE_DECL, NULL_TREE, type);
1367 TYPE_STUB_DECL (type) = decl;
1368 layout_type (type);
1369 rest_of_type_compilation (type, 1);
1370 rest_of_decl_compilation (decl, 1, 0);
1373 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1374 or RECORD_TYPE pointed to by STYPE. The new field is chained
1375 to the fieldlist pointed to by FIELDLIST.
1377 Returns a pointer to the new field. */
1379 tree
1380 gfc_add_field_to_struct (tree *fieldlist, tree context,
1381 tree name, tree type)
1383 tree decl;
1385 decl = build_decl (FIELD_DECL, name, type);
1387 DECL_CONTEXT (decl) = context;
1388 DECL_INITIAL (decl) = 0;
1389 DECL_ALIGN (decl) = 0;
1390 DECL_USER_ALIGN (decl) = 0;
1391 TREE_CHAIN (decl) = NULL_TREE;
1392 *fieldlist = chainon (*fieldlist, decl);
1394 return decl;
1398 /* Build a tree node for a derived type. */
1400 static tree
1401 gfc_get_derived_type (gfc_symbol * derived)
1403 tree typenode, field, field_type, fieldlist;
1404 gfc_component *c;
1406 gcc_assert (derived && derived->attr.flavor == FL_DERIVED);
1408 /* derived->backend_decl != 0 means we saw it before, but its
1409 components' backend_decl may have not been built. */
1410 if (derived->backend_decl)
1412 /* Its components' backend_decl have been built. */
1413 if (TYPE_FIELDS (derived->backend_decl))
1414 return derived->backend_decl;
1415 else
1416 typenode = derived->backend_decl;
1418 else
1420 /* We see this derived type first time, so build the type node. */
1421 typenode = make_node (RECORD_TYPE);
1422 TYPE_NAME (typenode) = get_identifier (derived->name);
1423 TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
1424 derived->backend_decl = typenode;
1427 /* Go through the derived type components, building them as
1428 necessary. The reason for doing this now is that it is
1429 possible to recurse back to this derived type through a
1430 pointer component (PR24092). If this happens, the fields
1431 will be built and so we can return the type. */
1432 for (c = derived->components; c; c = c->next)
1434 if (c->ts.type != BT_DERIVED)
1435 continue;
1437 if (!c->pointer || c->ts.derived->backend_decl == NULL)
1438 c->ts.derived->backend_decl = gfc_get_derived_type (c->ts.derived);
1441 if (TYPE_FIELDS (derived->backend_decl))
1442 return derived->backend_decl;
1444 /* Build the type member list. Install the newly created RECORD_TYPE
1445 node as DECL_CONTEXT of each FIELD_DECL. */
1446 fieldlist = NULL_TREE;
1447 for (c = derived->components; c; c = c->next)
1449 if (c->ts.type == BT_DERIVED)
1450 field_type = c->ts.derived->backend_decl;
1451 else
1453 if (c->ts.type == BT_CHARACTER)
1455 /* Evaluate the string length. */
1456 gfc_conv_const_charlen (c->ts.cl);
1457 gcc_assert (c->ts.cl->backend_decl);
1460 field_type = gfc_typenode_for_spec (&c->ts);
1463 /* This returns an array descriptor type. Initialization may be
1464 required. */
1465 if (c->dimension)
1467 if (c->pointer)
1469 /* Pointers to arrays aren't actually pointer types. The
1470 descriptors are separate, but the data is common. */
1471 field_type = gfc_build_array_type (field_type, c->as);
1473 else
1474 field_type = gfc_get_nodesc_array_type (field_type, c->as, 3);
1476 else if (c->pointer)
1477 field_type = build_pointer_type (field_type);
1479 field = gfc_add_field_to_struct (&fieldlist, typenode,
1480 get_identifier (c->name),
1481 field_type);
1483 DECL_PACKED (field) |= TYPE_PACKED (typenode);
1485 gcc_assert (field);
1486 if (!c->backend_decl)
1487 c->backend_decl = field;
1490 /* Now we have the final fieldlist. Record it, then lay out the
1491 derived type, including the fields. */
1492 TYPE_FIELDS (typenode) = fieldlist;
1494 gfc_finish_type (typenode);
1496 derived->backend_decl = typenode;
1498 return typenode;
1502 gfc_return_by_reference (gfc_symbol * sym)
1504 if (!sym->attr.function)
1505 return 0;
1507 if (sym->attr.dimension)
1508 return 1;
1510 if (sym->ts.type == BT_CHARACTER)
1511 return 1;
1513 /* Possibly return complex numbers by reference for g77 compatibility.
1514 We don't do this for calls to intrinsics (as the library uses the
1515 -fno-f2c calling convention), nor for calls to functions which always
1516 require an explicit interface, as no compatibility problems can
1517 arise there. */
1518 if (gfc_option.flag_f2c
1519 && sym->ts.type == BT_COMPLEX
1520 && !sym->attr.intrinsic && !sym->attr.always_explicit)
1521 return 1;
1523 return 0;
1526 static tree
1527 gfc_get_mixed_entry_union (gfc_namespace *ns)
1529 tree type;
1530 tree decl;
1531 tree fieldlist;
1532 char name[GFC_MAX_SYMBOL_LEN + 1];
1533 gfc_entry_list *el, *el2;
1535 gcc_assert (ns->proc_name->attr.mixed_entry_master);
1536 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
1538 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
1540 /* Build the type node. */
1541 type = make_node (UNION_TYPE);
1543 TYPE_NAME (type) = get_identifier (name);
1544 fieldlist = NULL;
1546 for (el = ns->entries; el; el = el->next)
1548 /* Search for duplicates. */
1549 for (el2 = ns->entries; el2 != el; el2 = el2->next)
1550 if (el2->sym->result == el->sym->result)
1551 break;
1553 if (el == el2)
1555 decl = build_decl (FIELD_DECL,
1556 get_identifier (el->sym->result->name),
1557 gfc_sym_type (el->sym->result));
1558 DECL_CONTEXT (decl) = type;
1559 fieldlist = chainon (fieldlist, decl);
1563 /* Finish off the type. */
1564 TYPE_FIELDS (type) = fieldlist;
1566 gfc_finish_type (type);
1567 return type;
1570 tree
1571 gfc_get_function_type (gfc_symbol * sym)
1573 tree type;
1574 tree typelist;
1575 gfc_formal_arglist *f;
1576 gfc_symbol *arg;
1577 int nstr;
1578 int alternate_return;
1580 /* Make sure this symbol is a function or a subroutine. */
1581 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1583 if (sym->backend_decl)
1584 return TREE_TYPE (sym->backend_decl);
1586 nstr = 0;
1587 alternate_return = 0;
1588 typelist = NULL_TREE;
1590 if (sym->attr.entry_master)
1592 /* Additional parameter for selecting an entry point. */
1593 typelist = gfc_chainon_list (typelist, gfc_array_index_type);
1596 /* Some functions we use an extra parameter for the return value. */
1597 if (gfc_return_by_reference (sym))
1599 if (sym->result)
1600 arg = sym->result;
1601 else
1602 arg = sym;
1604 if (arg->ts.type == BT_CHARACTER)
1605 gfc_conv_const_charlen (arg->ts.cl);
1607 type = gfc_sym_type (arg);
1608 if (arg->ts.type == BT_COMPLEX
1609 || arg->attr.dimension
1610 || arg->ts.type == BT_CHARACTER)
1611 type = build_reference_type (type);
1613 typelist = gfc_chainon_list (typelist, type);
1614 if (arg->ts.type == BT_CHARACTER)
1615 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1618 /* Build the argument types for the function. */
1619 for (f = sym->formal; f; f = f->next)
1621 arg = f->sym;
1622 if (arg)
1624 /* Evaluate constant character lengths here so that they can be
1625 included in the type. */
1626 if (arg->ts.type == BT_CHARACTER)
1627 gfc_conv_const_charlen (arg->ts.cl);
1629 if (arg->attr.flavor == FL_PROCEDURE)
1631 type = gfc_get_function_type (arg);
1632 type = build_pointer_type (type);
1634 else
1635 type = gfc_sym_type (arg);
1637 /* Parameter Passing Convention
1639 We currently pass all parameters by reference.
1640 Parameters with INTENT(IN) could be passed by value.
1641 The problem arises if a function is called via an implicit
1642 prototype. In this situation the INTENT is not known.
1643 For this reason all parameters to global functions must be
1644 passed by reference. Passing by value would potentially
1645 generate bad code. Worse there would be no way of telling that
1646 this code was bad, except that it would give incorrect results.
1648 Contained procedures could pass by value as these are never
1649 used without an explicit interface, and connot be passed as
1650 actual parameters for a dummy procedure. */
1651 if (arg->ts.type == BT_CHARACTER)
1652 nstr++;
1653 typelist = gfc_chainon_list (typelist, type);
1655 else
1657 if (sym->attr.subroutine)
1658 alternate_return = 1;
1662 /* Add hidden string length parameters. */
1663 while (nstr--)
1664 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1666 typelist = gfc_chainon_list (typelist, void_type_node);
1668 if (alternate_return)
1669 type = integer_type_node;
1670 else if (!sym->attr.function || gfc_return_by_reference (sym))
1671 type = void_type_node;
1672 else if (sym->attr.mixed_entry_master)
1673 type = gfc_get_mixed_entry_union (sym->ns);
1674 else
1675 type = gfc_sym_type (sym);
1677 type = build_function_type (type, typelist);
1679 return type;
1682 /* Language hooks for middle-end access to type nodes. */
1684 /* Return an integer type with BITS bits of precision,
1685 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1687 tree
1688 gfc_type_for_size (unsigned bits, int unsignedp)
1690 if (!unsignedp)
1692 int i;
1693 for (i = 0; i <= MAX_INT_KINDS; ++i)
1695 tree type = gfc_integer_types[i];
1696 if (type && bits == TYPE_PRECISION (type))
1697 return type;
1700 else
1702 if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
1703 return unsigned_intQI_type_node;
1704 if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
1705 return unsigned_intHI_type_node;
1706 if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
1707 return unsigned_intSI_type_node;
1708 if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
1709 return unsigned_intDI_type_node;
1710 if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
1711 return unsigned_intTI_type_node;
1714 return NULL_TREE;
1717 /* Return a data type that has machine mode MODE. If the mode is an
1718 integer, then UNSIGNEDP selects between signed and unsigned types. */
1720 tree
1721 gfc_type_for_mode (enum machine_mode mode, int unsignedp)
1723 int i;
1724 tree *base;
1726 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1727 base = gfc_real_types;
1728 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
1729 base = gfc_complex_types;
1730 else if (SCALAR_INT_MODE_P (mode))
1731 return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
1732 else if (VECTOR_MODE_P (mode))
1734 enum machine_mode inner_mode = GET_MODE_INNER (mode);
1735 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
1736 if (inner_type != NULL_TREE)
1737 return build_vector_type_for_mode (inner_type, mode);
1738 return NULL_TREE;
1740 else
1741 return NULL_TREE;
1743 for (i = 0; i <= MAX_REAL_KINDS; ++i)
1745 tree type = base[i];
1746 if (type && mode == TYPE_MODE (type))
1747 return type;
1750 return NULL_TREE;
1753 /* Return a type the same as TYPE except unsigned or
1754 signed according to UNSIGNEDP. */
1756 tree
1757 gfc_signed_or_unsigned_type (int unsignedp, tree type)
1759 if (TREE_CODE (type) != INTEGER_TYPE || TYPE_UNSIGNED (type) == unsignedp)
1760 return type;
1761 else
1762 return gfc_type_for_size (TYPE_PRECISION (type), unsignedp);
1765 /* Return an unsigned type the same as TYPE in other respects. */
1767 tree
1768 gfc_unsigned_type (tree type)
1770 return gfc_signed_or_unsigned_type (1, type);
1773 /* Return a signed type the same as TYPE in other respects. */
1775 tree
1776 gfc_signed_type (tree type)
1778 return gfc_signed_or_unsigned_type (0, type);
1781 #include "gt-fortran-trans-types.h"