* trans-types.c (gfc_type_for_mode): Return NULL for unknown modes.
[official-gcc.git] / gcc / fortran / trans-types.c
blob610449ffea13b1ba0e000fc774872aa2576d9e03
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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"
38 #include <assert.h>
41 #if (GFC_MAX_DIMENSIONS < 10)
42 #define GFC_RANK_DIGITS 1
43 #define GFC_RANK_PRINTF_FORMAT "%01d"
44 #elif (GFC_MAX_DIMENSIONS < 100)
45 #define GFC_RANK_DIGITS 2
46 #define GFC_RANK_PRINTF_FORMAT "%02d"
47 #else
48 #error If you really need >99 dimensions, continue the sequence above...
49 #endif
51 static tree gfc_get_derived_type (gfc_symbol * derived);
53 tree gfc_array_index_type;
54 tree pvoid_type_node;
55 tree ppvoid_type_node;
56 tree pchar_type_node;
57 tree gfc_character1_type_node;
58 tree gfc_charlen_type_node;
60 static GTY(()) tree gfc_desc_dim_type;
61 static GTY(()) tree gfc_max_array_element_size;
63 /* Arrays for all integral and real kinds. We'll fill this in at runtime
64 after the target has a chance to process command-line options. */
66 #define MAX_INT_KINDS 5
67 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
68 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
69 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
70 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
72 #define MAX_REAL_KINDS 4
73 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
74 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
75 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
77 /* The integer kind to use for array indices. This will be set to the
78 proper value based on target information from the backend. */
80 int gfc_index_integer_kind;
82 /* The default kinds of the various types. */
84 int gfc_default_integer_kind;
85 int gfc_default_real_kind;
86 int gfc_default_double_kind;
87 int gfc_default_character_kind;
88 int gfc_default_logical_kind;
89 int gfc_default_complex_kind;
90 int gfc_c_int_kind;
92 /* Query the target to determine which machine modes are available for
93 computation. Choose KIND numbers for them. */
95 void
96 gfc_init_kinds (void)
98 enum machine_mode mode;
99 int i_index, r_index;
100 bool saw_i4 = false, saw_i8 = false;
101 bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
103 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
105 int kind, bitsize;
107 if (!targetm.scalar_mode_supported_p (mode))
108 continue;
110 /* The middle end doesn't support constants larger than 2*HWI.
111 Perhaps the target hook shouldn't have accepted these either,
112 but just to be safe... */
113 bitsize = GET_MODE_BITSIZE (mode);
114 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
115 continue;
117 if (i_index == MAX_INT_KINDS)
118 abort ();
120 /* Let the kind equal the bit size divided by 8. This insulates the
121 programmer from the underlying byte size. */
122 kind = bitsize / 8;
124 if (kind == 4)
125 saw_i4 = true;
126 if (kind == 8)
127 saw_i8 = true;
129 gfc_integer_kinds[i_index].kind = kind;
130 gfc_integer_kinds[i_index].radix = 2;
131 gfc_integer_kinds[i_index].digits = bitsize - 1;
132 gfc_integer_kinds[i_index].bit_size = bitsize;
134 gfc_logical_kinds[i_index].kind = kind;
135 gfc_logical_kinds[i_index].bit_size = bitsize;
137 i_index += 1;
140 for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
142 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
143 int kind;
145 if (fmt == NULL)
146 continue;
147 if (!targetm.scalar_mode_supported_p (mode))
148 continue;
150 /* Let the kind equal the precision divided by 8, rounding up. Again,
151 this insulates the programmer from the underlying byte size.
153 Also, it effectively deals with IEEE extended formats. There, the
154 total size of the type may equal 16, but it's got 6 bytes of padding
155 and the increased size can get in the way of a real IEEE quad format
156 which may also be supported by the target.
158 We round up so as to handle IA-64 __floatreg (RFmode), which is an
159 82 bit type. Not to be confused with __float80 (XFmode), which is
160 an 80 bit type also supported by IA-64. So XFmode should come out
161 to be kind=10, and RFmode should come out to be kind=11. Egads. */
163 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
165 if (kind == 4)
166 saw_r4 = true;
167 if (kind == 8)
168 saw_r8 = true;
169 if (kind == 16)
170 saw_r16 = true;
172 /* Careful we don't stumble a wierd internal mode. */
173 if (r_index > 0 && gfc_real_kinds[r_index-1].kind == kind)
174 abort ();
175 /* Or have too many modes for the allocated space. */
176 if (r_index == MAX_REAL_KINDS)
177 abort ();
179 gfc_real_kinds[r_index].kind = kind;
180 gfc_real_kinds[r_index].radix = fmt->b;
181 gfc_real_kinds[r_index].digits = fmt->p;
182 gfc_real_kinds[r_index].min_exponent = fmt->emin;
183 gfc_real_kinds[r_index].max_exponent = fmt->emax;
184 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
185 r_index += 1;
188 /* Choose the default integer kind. We choose 4 unless the user
189 directs us otherwise. */
190 if (gfc_option.i8)
192 if (!saw_i8)
193 fatal_error ("integer kind=8 not available for -i8 option");
194 gfc_default_integer_kind = 8;
196 else if (saw_i4)
197 gfc_default_integer_kind = 4;
198 else
199 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
201 /* Choose the default real kind. Again, we choose 4 when possible. */
202 if (gfc_option.r8)
204 if (!saw_r8)
205 fatal_error ("real kind=8 not available for -r8 option");
206 gfc_default_real_kind = 8;
208 else if (saw_r4)
209 gfc_default_real_kind = 4;
210 else
211 gfc_default_real_kind = gfc_real_kinds[0].kind;
213 /* Choose the default double kind. If -r8 is specified, we use kind=16,
214 if it's available, otherwise we do not change anything. */
215 if (gfc_option.r8 && saw_r16)
216 gfc_default_double_kind = 16;
217 else if (saw_r4 && saw_r8)
218 gfc_default_double_kind = 8;
219 else
221 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
222 real ... occupies two contiguous numeric storage units.
224 Therefore we must be supplied a kind twice as large as we chose
225 for single precision. There are loopholes, in that double
226 precision must *occupy* two storage units, though it doesn't have
227 to *use* two storage units. Which means that you can make this
228 kind artificially wide by padding it. But at present there are
229 no GCC targets for which a two-word type does not exist, so we
230 just let gfc_validate_kind abort and tell us if something breaks. */
232 gfc_default_double_kind
233 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
236 /* The default logical kind is constrained to be the same as the
237 default integer kind. Similarly with complex and real. */
238 gfc_default_logical_kind = gfc_default_integer_kind;
239 gfc_default_complex_kind = gfc_default_real_kind;
241 /* Choose the smallest integer kind for our default character. */
242 gfc_default_character_kind = gfc_integer_kinds[0].kind;
244 /* Choose the integer kind the same size as "void*" for our index kind. */
245 gfc_index_integer_kind = POINTER_SIZE / 8;
246 /* Pick a kind the same size as the C "int" type. */
247 gfc_c_int_kind = INT_TYPE_SIZE / 8;
250 /* Make sure that a valid kind is present. Returns an index into the
251 associated kinds array, -1 if the kind is not present. */
253 static int
254 validate_integer (int kind)
256 int i;
258 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
259 if (gfc_integer_kinds[i].kind == kind)
260 return i;
262 return -1;
265 static int
266 validate_real (int kind)
268 int i;
270 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
271 if (gfc_real_kinds[i].kind == kind)
272 return i;
274 return -1;
277 static int
278 validate_logical (int kind)
280 int i;
282 for (i = 0; gfc_logical_kinds[i].kind; i++)
283 if (gfc_logical_kinds[i].kind == kind)
284 return i;
286 return -1;
289 static int
290 validate_character (int kind)
292 return kind == gfc_default_character_kind ? 0 : -1;
295 /* Validate a kind given a basic type. The return value is the same
296 for the child functions, with -1 indicating nonexistence of the
297 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
300 gfc_validate_kind (bt type, int kind, bool may_fail)
302 int rc;
304 switch (type)
306 case BT_REAL: /* Fall through */
307 case BT_COMPLEX:
308 rc = validate_real (kind);
309 break;
310 case BT_INTEGER:
311 rc = validate_integer (kind);
312 break;
313 case BT_LOGICAL:
314 rc = validate_logical (kind);
315 break;
316 case BT_CHARACTER:
317 rc = validate_character (kind);
318 break;
320 default:
321 gfc_internal_error ("gfc_validate_kind(): Got bad type");
324 if (rc < 0 && !may_fail)
325 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
327 return rc;
331 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
332 Reuse common type nodes where possible. Recognize if the kind matches up
333 with a C type. This will be used later in determining which routines may
334 be scarfed from libm. */
336 static tree
337 gfc_build_int_type (gfc_integer_info *info)
339 int mode_precision = info->bit_size;
341 if (mode_precision == CHAR_TYPE_SIZE)
342 info->c_char = 1;
343 if (mode_precision == SHORT_TYPE_SIZE)
344 info->c_short = 1;
345 if (mode_precision == INT_TYPE_SIZE)
346 info->c_int = 1;
347 if (mode_precision == LONG_TYPE_SIZE)
348 info->c_long = 1;
349 if (mode_precision == LONG_LONG_TYPE_SIZE)
350 info->c_long_long = 1;
352 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
353 return intQI_type_node;
354 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
355 return intHI_type_node;
356 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
357 return intSI_type_node;
358 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
359 return intDI_type_node;
360 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
361 return intTI_type_node;
363 return make_signed_type (mode_precision);
366 static tree
367 gfc_build_real_type (gfc_real_info *info)
369 int mode_precision = info->mode_precision;
370 tree new_type;
372 if (mode_precision == FLOAT_TYPE_SIZE)
373 info->c_float = 1;
374 if (mode_precision == DOUBLE_TYPE_SIZE)
375 info->c_double = 1;
376 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
377 info->c_long_double = 1;
379 if (TYPE_PRECISION (float_type_node) == mode_precision)
380 return float_type_node;
381 if (TYPE_PRECISION (double_type_node) == mode_precision)
382 return double_type_node;
383 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
384 return long_double_type_node;
386 new_type = make_node (REAL_TYPE);
387 TYPE_PRECISION (new_type) = mode_precision;
388 layout_type (new_type);
389 return new_type;
392 static tree
393 gfc_build_complex_type (tree scalar_type)
395 tree new_type;
397 if (scalar_type == NULL)
398 return NULL;
399 if (scalar_type == float_type_node)
400 return complex_float_type_node;
401 if (scalar_type == double_type_node)
402 return complex_double_type_node;
403 if (scalar_type == long_double_type_node)
404 return complex_long_double_type_node;
406 new_type = make_node (COMPLEX_TYPE);
407 TREE_TYPE (new_type) = scalar_type;
408 layout_type (new_type);
409 return new_type;
412 static tree
413 gfc_build_logical_type (gfc_logical_info *info)
415 int bit_size = info->bit_size;
416 tree new_type;
418 if (bit_size == BOOL_TYPE_SIZE)
420 info->c_bool = 1;
421 return boolean_type_node;
424 new_type = make_unsigned_type (bit_size);
425 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
426 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
427 TYPE_PRECISION (new_type) = 1;
429 return new_type;
432 #if 0
433 /* Return the bit size of the C "size_t". */
435 static unsigned int
436 c_size_t_size (void)
438 #ifdef SIZE_TYPE
439 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
440 return INT_TYPE_SIZE;
441 if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
442 return LONG_TYPE_SIZE;
443 if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
444 return SHORT_TYPE_SIZE;
445 abort ();
446 #else
447 return LONG_TYPE_SIZE;
448 #endif
450 #endif
452 /* Create the backend type nodes. We map them to their
453 equivalent C type, at least for now. We also give
454 names to the types here, and we push them in the
455 global binding level context.*/
457 void
458 gfc_init_types (void)
460 char name_buf[16];
461 int index;
462 tree type;
463 unsigned n;
464 unsigned HOST_WIDE_INT hi;
465 unsigned HOST_WIDE_INT lo;
467 /* Create and name the types. */
468 #define PUSH_TYPE(name, node) \
469 pushdecl (build_decl (TYPE_DECL, get_identifier (name), node))
471 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
473 type = gfc_build_int_type (&gfc_integer_kinds[index]);
474 gfc_integer_types[index] = type;
475 snprintf (name_buf, sizeof(name_buf), "int%d",
476 gfc_integer_kinds[index].kind);
477 PUSH_TYPE (name_buf, type);
480 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
482 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
483 gfc_logical_types[index] = type;
484 snprintf (name_buf, sizeof(name_buf), "logical%d",
485 gfc_logical_kinds[index].kind);
486 PUSH_TYPE (name_buf, type);
489 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
491 type = gfc_build_real_type (&gfc_real_kinds[index]);
492 gfc_real_types[index] = type;
493 snprintf (name_buf, sizeof(name_buf), "real%d",
494 gfc_real_kinds[index].kind);
495 PUSH_TYPE (name_buf, type);
497 type = gfc_build_complex_type (type);
498 gfc_complex_types[index] = type;
499 snprintf (name_buf, sizeof(name_buf), "complex%d",
500 gfc_real_kinds[index].kind);
501 PUSH_TYPE (name_buf, type);
504 gfc_character1_type_node = build_type_variant (unsigned_char_type_node,
505 0, 0);
506 PUSH_TYPE ("char", gfc_character1_type_node);
508 PUSH_TYPE ("byte", unsigned_char_type_node);
509 PUSH_TYPE ("void", void_type_node);
511 /* DBX debugging output gets upset if these aren't set. */
512 if (!TYPE_NAME (integer_type_node))
513 PUSH_TYPE ("c_integer", integer_type_node);
514 if (!TYPE_NAME (char_type_node))
515 PUSH_TYPE ("c_char", char_type_node);
517 #undef PUSH_TYPE
519 pvoid_type_node = build_pointer_type (void_type_node);
520 ppvoid_type_node = build_pointer_type (pvoid_type_node);
521 pchar_type_node = build_pointer_type (gfc_character1_type_node);
523 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
525 /* The maximum array element size that can be handled is determined
526 by the number of bits available to store this field in the array
527 descriptor. */
529 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
530 lo = ~ (unsigned HOST_WIDE_INT) 0;
531 if (n > HOST_BITS_PER_WIDE_INT)
532 hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
533 else
534 hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
535 gfc_max_array_element_size
536 = build_int_cst_wide (long_unsigned_type_node, lo, hi);
538 size_type_node = gfc_array_index_type;
540 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
541 boolean_true_node = build_int_cst (boolean_type_node, 1);
542 boolean_false_node = build_int_cst (boolean_type_node, 0);
544 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
545 gfc_charlen_type_node = gfc_get_int_type (4);
548 /* Get the type node for the given type and kind. */
550 tree
551 gfc_get_int_type (int kind)
553 int index = gfc_validate_kind (BT_INTEGER, kind, false);
554 return gfc_integer_types[index];
557 tree
558 gfc_get_real_type (int kind)
560 int index = gfc_validate_kind (BT_REAL, kind, false);
561 return gfc_real_types[index];
564 tree
565 gfc_get_complex_type (int kind)
567 int index = gfc_validate_kind (BT_COMPLEX, kind, false);
568 return gfc_complex_types[index];
571 tree
572 gfc_get_logical_type (int kind)
574 int index = gfc_validate_kind (BT_LOGICAL, kind, false);
575 return gfc_logical_types[index];
578 /* Create a character type with the given kind and length. */
580 tree
581 gfc_get_character_type_len (int kind, tree len)
583 tree bounds, type;
585 gfc_validate_kind (BT_CHARACTER, kind, false);
587 bounds = build_range_type (gfc_array_index_type, gfc_index_one_node, len);
588 type = build_array_type (gfc_character1_type_node, bounds);
589 TYPE_STRING_FLAG (type) = 1;
591 return type;
595 /* Get a type node for a character kind. */
597 tree
598 gfc_get_character_type (int kind, gfc_charlen * cl)
600 tree len;
602 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
604 return gfc_get_character_type_len (kind, len);
607 /* Covert a basic type. This will be an array for character types. */
609 tree
610 gfc_typenode_for_spec (gfc_typespec * spec)
612 tree basetype;
614 switch (spec->type)
616 case BT_UNKNOWN:
617 abort ();
618 break;
620 case BT_INTEGER:
621 basetype = gfc_get_int_type (spec->kind);
622 break;
624 case BT_REAL:
625 basetype = gfc_get_real_type (spec->kind);
626 break;
628 case BT_COMPLEX:
629 basetype = gfc_get_complex_type (spec->kind);
630 break;
632 case BT_LOGICAL:
633 basetype = gfc_get_logical_type (spec->kind);
634 break;
636 case BT_CHARACTER:
637 basetype = gfc_get_character_type (spec->kind, spec->cl);
638 break;
640 case BT_DERIVED:
641 basetype = gfc_get_derived_type (spec->derived);
642 break;
644 default:
645 abort ();
646 break;
648 return basetype;
651 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
653 static tree
654 gfc_conv_array_bound (gfc_expr * expr)
656 /* If expr is an integer constant, return that. */
657 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
658 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
660 /* Otherwise return NULL. */
661 return NULL_TREE;
664 tree
665 gfc_get_element_type (tree type)
667 tree element;
669 if (GFC_ARRAY_TYPE_P (type))
671 if (TREE_CODE (type) == POINTER_TYPE)
672 type = TREE_TYPE (type);
673 assert (TREE_CODE (type) == ARRAY_TYPE);
674 element = TREE_TYPE (type);
676 else
678 assert (GFC_DESCRIPTOR_TYPE_P (type));
679 element = TREE_TYPE (TYPE_FIELDS (type));
681 assert (TREE_CODE (element) == POINTER_TYPE);
682 element = TREE_TYPE (element);
684 assert (TREE_CODE (element) == ARRAY_TYPE);
685 element = TREE_TYPE (element);
688 return element;
691 /* Build an array. This function is called from gfc_sym_type().
692 Actually returns array descriptor type.
694 Format of array descriptors is as follows:
696 struct gfc_array_descriptor
698 array *data
699 index offset;
700 index dtype;
701 struct descriptor_dimension dimension[N_DIM];
704 struct descriptor_dimension
706 index stride;
707 index lbound;
708 index ubound;
711 Translation code should use gfc_conv_descriptor_* rather than accessing
712 the descriptor directly. Any changes to the array descriptor type will
713 require changes in gfc_conv_descriptor_* and gfc_build_array_initializer.
715 This is represented internally as a RECORD_TYPE. The index nodes are
716 gfc_array_index_type and the data node is a pointer to the data. See below
717 for the handling of character types.
719 The dtype member is formatted as follows:
720 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
721 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
722 size = dtype >> GFC_DTYPE_SIZE_SHIFT
724 I originally used nested ARRAY_TYPE nodes to represent arrays, but this
725 generated poor code for assumed/deferred size arrays. These require
726 use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part of the GENERIC
727 grammar. Also, there is no way to explicitly set the array stride, so
728 all data must be packed(1). I've tried to mark all the functions which
729 would require modification with a GCC ARRAYS comment.
731 The data component points to the first element in the array.
732 The offset field is the position of the origin of the array
733 (ie element (0, 0 ...)). This may be outsite the bounds of the array.
735 An element is accessed by
736 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
737 This gives good performance as the computation does not involve the
738 bounds of the array. For packed arrays, this is optimized further by
739 substituting the known strides.
741 This system has one problem: all array bounds must be withing 2^31 elements
742 of the origin (2^63 on 64-bit machines). For example
743 integer, dimension (80000:90000, 80000:90000, 2) :: array
744 may not work properly on 32-bit machines because 80000*80000 > 2^31, so
745 the calculation for stride02 would overflow. This may still work, but
746 I haven't checked, and it relies on the overflow doing the right thing.
748 The way to fix this problem is to access alements as follows:
749 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
750 Obviously this is much slower. I will make this a compile time option,
751 something like -fsmall-array-offsets. Mixing code compiled with and without
752 this switch will work.
754 (1) This can be worked around by modifying the upper bound of the previous
755 dimension. This requires extra fields in the descriptor (both real_ubound
756 and fake_ubound). In tree.def there is mention of TYPE_SEP, which
757 may allow us to do this. However I can't find mention of this anywhere
758 else. */
761 /* Returns true if the array sym does not require a descriptor. */
764 gfc_is_nodesc_array (gfc_symbol * sym)
766 assert (sym->attr.dimension);
768 /* We only want local arrays. */
769 if (sym->attr.pointer || sym->attr.allocatable)
770 return 0;
772 if (sym->attr.dummy)
774 if (sym->as->type != AS_ASSUMED_SHAPE)
775 return 1;
776 else
777 return 0;
780 if (sym->attr.result || sym->attr.function)
781 return 0;
783 if (sym->attr.pointer || sym->attr.allocatable)
784 return 0;
786 assert (sym->as->type == AS_EXPLICIT);
788 return 1;
792 /* Create an array descriptor type. */
794 static tree
795 gfc_build_array_type (tree type, gfc_array_spec * as)
797 tree lbound[GFC_MAX_DIMENSIONS];
798 tree ubound[GFC_MAX_DIMENSIONS];
799 int n;
801 for (n = 0; n < as->rank; n++)
803 /* Create expressions for the known bounds of the array. */
804 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
805 lbound[n] = gfc_index_one_node;
806 else
807 lbound[n] = gfc_conv_array_bound (as->lower[n]);
808 ubound[n] = gfc_conv_array_bound (as->upper[n]);
811 return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0);
814 /* Returns the struct descriptor_dimension type. */
816 static tree
817 gfc_get_desc_dim_type (void)
819 tree type;
820 tree decl;
821 tree fieldlist;
823 if (gfc_desc_dim_type)
824 return gfc_desc_dim_type;
826 /* Build the type node. */
827 type = make_node (RECORD_TYPE);
829 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
830 TYPE_PACKED (type) = 1;
832 /* Consists of the stride, lbound and ubound members. */
833 decl = build_decl (FIELD_DECL,
834 get_identifier ("stride"), gfc_array_index_type);
835 DECL_CONTEXT (decl) = type;
836 fieldlist = decl;
838 decl = build_decl (FIELD_DECL,
839 get_identifier ("lbound"), gfc_array_index_type);
840 DECL_CONTEXT (decl) = type;
841 fieldlist = chainon (fieldlist, decl);
843 decl = build_decl (FIELD_DECL,
844 get_identifier ("ubound"), gfc_array_index_type);
845 DECL_CONTEXT (decl) = type;
846 fieldlist = chainon (fieldlist, decl);
848 /* Finish off the type. */
849 TYPE_FIELDS (type) = fieldlist;
851 gfc_finish_type (type);
853 gfc_desc_dim_type = type;
854 return type;
857 static tree
858 gfc_get_dtype (tree type, int rank)
860 tree size;
861 int n;
862 HOST_WIDE_INT i;
863 tree tmp;
864 tree dtype;
866 if (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type))
867 return (GFC_TYPE_ARRAY_DTYPE (type));
869 /* TODO: Correctly identify LOGICAL types. */
870 switch (TREE_CODE (type))
872 case INTEGER_TYPE:
873 n = GFC_DTYPE_INTEGER;
874 break;
876 case BOOLEAN_TYPE:
877 n = GFC_DTYPE_LOGICAL;
878 break;
880 case REAL_TYPE:
881 n = GFC_DTYPE_REAL;
882 break;
884 case COMPLEX_TYPE:
885 n = GFC_DTYPE_COMPLEX;
886 break;
888 /* Arrays have already been dealt with. */
889 case RECORD_TYPE:
890 n = GFC_DTYPE_DERIVED;
891 break;
893 case ARRAY_TYPE:
894 n = GFC_DTYPE_CHARACTER;
895 break;
897 default:
898 /* TODO: Don't do dtype for temporary descriptorless arrays. */
899 /* We can strange array types for temporary arrays. */
900 return gfc_index_zero_node;
903 assert (rank <= GFC_DTYPE_RANK_MASK);
904 size = TYPE_SIZE_UNIT (type);
906 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
907 if (size && INTEGER_CST_P (size))
909 if (tree_int_cst_lt (gfc_max_array_element_size, size))
910 internal_error ("Array element size too big");
912 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
914 dtype = build_int_cst (gfc_array_index_type, i);
916 if (size && !INTEGER_CST_P (size))
918 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
919 tmp = fold (build2 (LSHIFT_EXPR, gfc_array_index_type, size, tmp));
920 dtype = fold (build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype));
922 /* If we don't know the size we leave it as zero. This should never happen
923 for anything that is actually used. */
924 /* TODO: Check this is actually true, particularly when repacking
925 assumed size parameters. */
927 return dtype;
931 /* Build an array type for use without a descriptor. Valid values of packed
932 are 0=no, 1=partial, 2=full, 3=static. */
934 tree
935 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, int packed)
937 tree range;
938 tree type;
939 tree tmp;
940 int n;
941 int known_stride;
942 int known_offset;
943 mpz_t offset;
944 mpz_t stride;
945 mpz_t delta;
946 gfc_expr *expr;
948 mpz_init_set_ui (offset, 0);
949 mpz_init_set_ui (stride, 1);
950 mpz_init (delta);
952 /* We don't use build_array_type because this does not include include
953 lang-specific information (ie. the bounds of the array) when checking
954 for duplicates. */
955 type = make_node (ARRAY_TYPE);
957 GFC_ARRAY_TYPE_P (type) = 1;
958 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
959 ggc_alloc_cleared (sizeof (struct lang_type));
961 known_stride = (packed != 0);
962 known_offset = 1;
963 for (n = 0; n < as->rank; n++)
965 /* Fill in the stride and bound components of the type. */
966 if (known_stride)
967 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
968 else
969 tmp = NULL_TREE;
970 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
972 expr = as->lower[n];
973 if (expr->expr_type == EXPR_CONSTANT)
975 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
976 gfc_index_integer_kind);
978 else
980 known_stride = 0;
981 tmp = NULL_TREE;
983 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
985 if (known_stride)
987 /* Calculate the offset. */
988 mpz_mul (delta, stride, as->lower[n]->value.integer);
989 mpz_sub (offset, offset, delta);
991 else
992 known_offset = 0;
994 expr = as->upper[n];
995 if (expr && expr->expr_type == EXPR_CONSTANT)
997 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
998 gfc_index_integer_kind);
1000 else
1002 tmp = NULL_TREE;
1003 known_stride = 0;
1005 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1007 if (known_stride)
1009 /* Calculate the stride. */
1010 mpz_sub (delta, as->upper[n]->value.integer,
1011 as->lower[n]->value.integer);
1012 mpz_add_ui (delta, delta, 1);
1013 mpz_mul (stride, stride, delta);
1016 /* Only the first stride is known for partial packed arrays. */
1017 if (packed < 2)
1018 known_stride = 0;
1021 if (known_offset)
1023 GFC_TYPE_ARRAY_OFFSET (type) =
1024 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1026 else
1027 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1029 if (known_stride)
1031 GFC_TYPE_ARRAY_SIZE (type) =
1032 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1034 else
1035 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1037 GFC_TYPE_ARRAY_DTYPE (type) = gfc_get_dtype (etype, as->rank);
1038 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1039 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1040 NULL_TREE);
1041 /* TODO: use main type if it is unbounded. */
1042 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1043 build_pointer_type (build_array_type (etype, range));
1045 if (known_stride)
1047 mpz_sub_ui (stride, stride, 1);
1048 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1050 else
1051 range = NULL_TREE;
1053 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1054 TYPE_DOMAIN (type) = range;
1056 build_pointer_type (etype);
1057 TREE_TYPE (type) = etype;
1059 layout_type (type);
1061 mpz_clear (offset);
1062 mpz_clear (stride);
1063 mpz_clear (delta);
1065 if (packed < 3 || !known_stride)
1067 /* For dummy arrays and automatic (heap allocated) arrays we
1068 want a pointer to the array. */
1069 type = build_pointer_type (type);
1070 GFC_ARRAY_TYPE_P (type) = 1;
1071 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1073 return type;
1077 /* Build an array (descriptor) type with given bounds. */
1079 tree
1080 gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound,
1081 tree * ubound, int packed)
1083 tree fat_type, fat_pointer_type;
1084 tree fieldlist;
1085 tree arraytype;
1086 tree decl;
1087 int n;
1088 char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN];
1089 const char *typename;
1090 tree lower;
1091 tree upper;
1092 tree stride;
1093 tree tmp;
1095 /* Build the type node. */
1096 fat_type = make_node (RECORD_TYPE);
1097 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1098 TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *)
1099 ggc_alloc_cleared (sizeof (struct lang_type));
1100 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1101 GFC_TYPE_ARRAY_DTYPE (fat_type) = gfc_get_dtype (etype, dimen);
1103 tmp = TYPE_NAME (etype);
1104 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1105 tmp = DECL_NAME (tmp);
1106 if (tmp)
1107 typename = IDENTIFIER_POINTER (tmp);
1108 else
1109 typename = "unknown";
1111 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen,
1112 GFC_MAX_SYMBOL_LEN, typename);
1113 TYPE_NAME (fat_type) = get_identifier (name);
1114 TYPE_PACKED (fat_type) = 0;
1116 fat_pointer_type = build_pointer_type (fat_type);
1118 /* Build an array descriptor record type. */
1119 if (packed != 0)
1120 stride = gfc_index_one_node;
1121 else
1122 stride = NULL_TREE;
1124 for (n = 0; n < dimen; n++)
1126 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1128 if (lbound)
1129 lower = lbound[n];
1130 else
1131 lower = NULL_TREE;
1133 if (lower != NULL_TREE)
1135 if (INTEGER_CST_P (lower))
1136 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1137 else
1138 lower = NULL_TREE;
1141 upper = ubound[n];
1142 if (upper != NULL_TREE)
1144 if (INTEGER_CST_P (upper))
1145 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1146 else
1147 upper = NULL_TREE;
1150 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1152 tmp = fold (build2 (MINUS_EXPR, gfc_array_index_type, upper, lower));
1153 tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1154 gfc_index_one_node));
1155 stride =
1156 fold (build2 (MULT_EXPR, gfc_array_index_type, tmp, stride));
1157 /* Check the folding worked. */
1158 assert (INTEGER_CST_P (stride));
1160 else
1161 stride = NULL_TREE;
1163 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1164 /* TODO: known offsets for descriptors. */
1165 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1167 /* We define data as an unknown size array. Much better than doing
1168 pointer arithmetic. */
1169 arraytype =
1170 build_array_type (etype,
1171 build_range_type (gfc_array_index_type,
1172 gfc_index_zero_node, NULL_TREE));
1173 arraytype = build_pointer_type (arraytype);
1174 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1176 /* The pointer to the array data. */
1177 decl = build_decl (FIELD_DECL, get_identifier ("data"), arraytype);
1179 DECL_CONTEXT (decl) = fat_type;
1180 /* Add the data member as the first element of the descriptor. */
1181 fieldlist = decl;
1183 /* Add the base component. */
1184 decl = build_decl (FIELD_DECL, get_identifier ("offset"),
1185 gfc_array_index_type);
1186 DECL_CONTEXT (decl) = fat_type;
1187 fieldlist = chainon (fieldlist, decl);
1189 /* Add the dtype component. */
1190 decl = build_decl (FIELD_DECL, get_identifier ("dtype"),
1191 gfc_array_index_type);
1192 DECL_CONTEXT (decl) = fat_type;
1193 fieldlist = chainon (fieldlist, decl);
1195 /* Build the array type for the stride and bound components. */
1196 arraytype =
1197 build_array_type (gfc_get_desc_dim_type (),
1198 build_range_type (gfc_array_index_type,
1199 gfc_index_zero_node,
1200 gfc_rank_cst[dimen - 1]));
1202 decl = build_decl (FIELD_DECL, get_identifier ("dim"), arraytype);
1203 DECL_CONTEXT (decl) = fat_type;
1204 DECL_INITIAL (decl) = NULL_TREE;
1205 fieldlist = chainon (fieldlist, decl);
1207 /* Finish off the type. */
1208 TYPE_FIELDS (fat_type) = fieldlist;
1210 gfc_finish_type (fat_type);
1212 return fat_type;
1215 /* Build a pointer type. This function is called from gfc_sym_type(). */
1217 static tree
1218 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1220 /* Array pointer types aren't actually pointers. */
1221 if (sym->attr.dimension)
1222 return type;
1223 else
1224 return build_pointer_type (type);
1227 /* Return the type for a symbol. Special handling is required for character
1228 types to get the correct level of indirection.
1229 For functions return the return type.
1230 For subroutines return void_type_node.
1231 Calling this multiple times for the same symbol should be avoided,
1232 especially for character and array types. */
1234 tree
1235 gfc_sym_type (gfc_symbol * sym)
1237 tree type;
1238 int byref;
1240 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1241 return void_type_node;
1243 if (sym->backend_decl)
1245 if (sym->attr.function)
1246 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
1247 else
1248 return TREE_TYPE (sym->backend_decl);
1251 /* The frontend doesn't set all the attributes for a function with an
1252 explicit result value, so we use that instead when present. */
1253 if (sym->attr.function && sym->result)
1254 sym = sym->result;
1256 type = gfc_typenode_for_spec (&sym->ts);
1258 if (sym->attr.dummy && !sym->attr.function)
1259 byref = 1;
1260 else
1261 byref = 0;
1263 if (sym->attr.dimension)
1265 if (gfc_is_nodesc_array (sym))
1267 /* If this is a character argument of unknown length, just use the
1268 base type. */
1269 if (sym->ts.type != BT_CHARACTER
1270 || !(sym->attr.dummy || sym->attr.function || sym->attr.result)
1271 || sym->ts.cl->backend_decl)
1273 type = gfc_get_nodesc_array_type (type, sym->as,
1274 byref ? 2 : 3);
1275 byref = 0;
1278 else
1279 type = gfc_build_array_type (type, sym->as);
1281 else
1283 if (sym->attr.allocatable || sym->attr.pointer)
1284 type = gfc_build_pointer_type (sym, type);
1287 /* We currently pass all parameters by reference.
1288 See f95_get_function_decl. For dummy function parameters return the
1289 function type. */
1290 if (byref)
1292 /* We must use pointer types for potentially absent variables. The
1293 optimizers assume a reference type argument is never NULL. */
1294 if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1295 type = build_pointer_type (type);
1296 else
1297 type = build_reference_type (type);
1300 return (type);
1303 /* Layout and output debug info for a record type. */
1305 void
1306 gfc_finish_type (tree type)
1308 tree decl;
1310 decl = build_decl (TYPE_DECL, NULL_TREE, type);
1311 TYPE_STUB_DECL (type) = decl;
1312 layout_type (type);
1313 rest_of_type_compilation (type, 1);
1314 rest_of_decl_compilation (decl, 1, 0);
1317 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1318 or RECORD_TYPE pointed to by STYPE. The new field is chained
1319 to the fieldlist pointed to by FIELDLIST.
1321 Returns a pointer to the new field. */
1323 tree
1324 gfc_add_field_to_struct (tree *fieldlist, tree context,
1325 tree name, tree type)
1327 tree decl;
1329 decl = build_decl (FIELD_DECL, name, type);
1331 DECL_CONTEXT (decl) = context;
1332 DECL_INITIAL (decl) = 0;
1333 DECL_ALIGN (decl) = 0;
1334 DECL_USER_ALIGN (decl) = 0;
1335 TREE_CHAIN (decl) = NULL_TREE;
1336 *fieldlist = chainon (*fieldlist, decl);
1338 return decl;
1342 /* Build a tree node for a derived type. */
1344 static tree
1345 gfc_get_derived_type (gfc_symbol * derived)
1347 tree typenode, field, field_type, fieldlist;
1348 gfc_component *c;
1350 assert (derived && derived->attr.flavor == FL_DERIVED);
1352 /* derived->backend_decl != 0 means we saw it before, but its
1353 components' backend_decl may have not been built. */
1354 if (derived->backend_decl)
1356 /* Its components' backend_decl have been built. */
1357 if (TYPE_FIELDS (derived->backend_decl))
1358 return derived->backend_decl;
1359 else
1360 typenode = derived->backend_decl;
1362 else
1364 /* We see this derived type first time, so build the type node. */
1365 typenode = make_node (RECORD_TYPE);
1366 TYPE_NAME (typenode) = get_identifier (derived->name);
1367 TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
1368 derived->backend_decl = typenode;
1371 /* Build the type member list. Install the newly created RECORD_TYPE
1372 node as DECL_CONTEXT of each FIELD_DECL. */
1373 fieldlist = NULL_TREE;
1374 for (c = derived->components; c; c = c->next)
1376 if (c->ts.type == BT_DERIVED && c->pointer)
1378 if (c->ts.derived->backend_decl)
1379 field_type = c->ts.derived->backend_decl;
1380 else
1382 /* Build the type node. */
1383 field_type = make_node (RECORD_TYPE);
1384 TYPE_NAME (field_type) = get_identifier (c->ts.derived->name);
1385 TYPE_PACKED (field_type) = gfc_option.flag_pack_derived;
1386 c->ts.derived->backend_decl = field_type;
1389 else
1391 if (c->ts.type == BT_CHARACTER)
1393 /* Evaluate the string length. */
1394 gfc_conv_const_charlen (c->ts.cl);
1395 assert (c->ts.cl->backend_decl);
1398 field_type = gfc_typenode_for_spec (&c->ts);
1401 /* This returns an array descriptor type. Initialisation may be
1402 required. */
1403 if (c->dimension)
1405 if (c->pointer)
1407 /* Pointers to arrays aren't actualy pointer types. The
1408 descriptors are seperate, but the data is common. */
1409 field_type = gfc_build_array_type (field_type, c->as);
1411 else
1412 field_type = gfc_get_nodesc_array_type (field_type, c->as, 3);
1414 else if (c->pointer)
1415 field_type = build_pointer_type (field_type);
1417 field = gfc_add_field_to_struct (&fieldlist, typenode,
1418 get_identifier (c->name),
1419 field_type);
1421 DECL_PACKED (field) |= TYPE_PACKED (typenode);
1423 assert (!c->backend_decl);
1424 c->backend_decl = field;
1427 /* Now we have the final fieldlist. Record it, then lay out the
1428 derived type, including the fields. */
1429 TYPE_FIELDS (typenode) = fieldlist;
1431 gfc_finish_type (typenode);
1433 derived->backend_decl = typenode;
1435 return typenode;
1439 gfc_return_by_reference (gfc_symbol * sym)
1441 if (!sym->attr.function)
1442 return 0;
1444 assert (sym->attr.function);
1446 if (sym->result)
1447 sym = sym->result;
1449 if (sym->attr.dimension)
1450 return 1;
1452 if (sym->ts.type == BT_CHARACTER)
1453 return 1;
1455 /* Possibly return complex numbers by reference for g77 compatibility. */
1456 return 0;
1459 tree
1460 gfc_get_function_type (gfc_symbol * sym)
1462 tree type;
1463 tree typelist;
1464 gfc_formal_arglist *f;
1465 gfc_symbol *arg;
1466 int nstr;
1467 int alternate_return;
1469 /* Make sure this symbol is a function or a subroutine. */
1470 assert (sym->attr.flavor == FL_PROCEDURE);
1472 if (sym->backend_decl)
1473 return TREE_TYPE (sym->backend_decl);
1475 nstr = 0;
1476 alternate_return = 0;
1477 typelist = NULL_TREE;
1479 if (sym->attr.entry_master)
1481 /* Additional parameter for selecting an entry point. */
1482 typelist = gfc_chainon_list (typelist, gfc_array_index_type);
1485 /* Some functions we use an extra parameter for the return value. */
1486 if (gfc_return_by_reference (sym))
1488 if (sym->result)
1489 arg = sym->result;
1490 else
1491 arg = sym;
1493 if (arg->ts.type == BT_CHARACTER)
1494 gfc_conv_const_charlen (arg->ts.cl);
1496 type = gfc_sym_type (arg);
1497 if (arg->ts.type == BT_DERIVED
1498 || arg->attr.dimension
1499 || arg->ts.type == BT_CHARACTER)
1500 type = build_reference_type (type);
1502 typelist = gfc_chainon_list (typelist, type);
1503 if (arg->ts.type == BT_CHARACTER)
1504 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1507 /* Build the argument types for the function. */
1508 for (f = sym->formal; f; f = f->next)
1510 arg = f->sym;
1511 if (arg)
1513 /* Evaluate constant character lengths here so that they can be
1514 included in the type. */
1515 if (arg->ts.type == BT_CHARACTER)
1516 gfc_conv_const_charlen (arg->ts.cl);
1518 if (arg->attr.flavor == FL_PROCEDURE)
1520 type = gfc_get_function_type (arg);
1521 type = build_pointer_type (type);
1523 else
1524 type = gfc_sym_type (arg);
1526 /* Parameter Passing Convention
1528 We currently pass all parameters by reference.
1529 Parameters with INTENT(IN) could be passed by value.
1530 The problem arises if a function is called via an implicit
1531 prototype. In this situation the INTENT is not known.
1532 For this reason all parameters to global functions must be
1533 passed by reference. Passing by value would potentialy
1534 generate bad code. Worse there would be no way of telling that
1535 this code was bad, except that it would give incorrect results.
1537 Contained procedures could pass by value as these are never
1538 used without an explicit interface, and connot be passed as
1539 actual parameters for a dummy procedure. */
1540 if (arg->ts.type == BT_CHARACTER)
1541 nstr++;
1542 typelist = gfc_chainon_list (typelist, type);
1544 else
1546 if (sym->attr.subroutine)
1547 alternate_return = 1;
1551 /* Add hidden string length parameters. */
1552 while (nstr--)
1553 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1555 typelist = gfc_chainon_list (typelist, void_type_node);
1557 if (alternate_return)
1558 type = integer_type_node;
1559 else if (!sym->attr.function || gfc_return_by_reference (sym))
1560 type = void_type_node;
1561 else
1562 type = gfc_sym_type (sym);
1564 type = build_function_type (type, typelist);
1566 return type;
1569 /* Language hooks for middle-end access to type nodes. */
1571 /* Return an integer type with BITS bits of precision,
1572 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1574 tree
1575 gfc_type_for_size (unsigned bits, int unsignedp)
1577 if (!unsignedp)
1579 int i;
1580 for (i = 0; i <= MAX_INT_KINDS; ++i)
1582 tree type = gfc_integer_types[i];
1583 if (type && bits == TYPE_PRECISION (type))
1584 return type;
1587 else
1589 if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
1590 return unsigned_intQI_type_node;
1591 if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
1592 return unsigned_intHI_type_node;
1593 if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
1594 return unsigned_intSI_type_node;
1595 if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
1596 return unsigned_intDI_type_node;
1597 if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
1598 return unsigned_intTI_type_node;
1601 return NULL_TREE;
1604 /* Return a data type that has machine mode MODE. If the mode is an
1605 integer, then UNSIGNEDP selects between signed and unsigned types. */
1607 tree
1608 gfc_type_for_mode (enum machine_mode mode, int unsignedp)
1610 int i;
1611 tree *base;
1613 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1614 base = gfc_real_types;
1615 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
1616 base = gfc_complex_types;
1617 else if (SCALAR_INT_MODE_P (mode))
1618 return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
1619 else if (VECTOR_MODE_P (mode))
1621 enum machine_mode inner_mode = GET_MODE_INNER (mode);
1622 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
1623 if (inner_type != NULL_TREE)
1624 return build_vector_type_for_mode (inner_type, mode);
1625 return NULL_TREE;
1627 else
1628 return NULL_TREE;
1630 for (i = 0; i <= MAX_REAL_KINDS; ++i)
1632 tree type = base[i];
1633 if (type && mode == TYPE_MODE (type))
1634 return type;
1637 return NULL_TREE;
1640 /* Return a type the same as TYPE except unsigned or
1641 signed according to UNSIGNEDP. */
1643 tree
1644 gfc_signed_or_unsigned_type (int unsignedp, tree type)
1646 if (TREE_CODE (type) != INTEGER_TYPE || TYPE_UNSIGNED (type) == unsignedp)
1647 return type;
1648 else
1649 return gfc_type_for_size (TYPE_PRECISION (type), unsignedp);
1652 /* Return an unsigned type the same as TYPE in other respects. */
1654 tree
1655 gfc_unsigned_type (tree type)
1657 return gfc_signed_or_unsigned_type (1, type);
1660 /* Return a signed type the same as TYPE in other respects. */
1662 tree
1663 gfc_signed_type (tree type)
1665 return gfc_signed_or_unsigned_type (0, type);
1668 #include "gt-fortran-trans-types.h"