OSDN Git Service

e247fb3139669c9788d246958a24393aa930ab11
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-types.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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.  */
22
23 /* trans-types.c -- gfortran backend types */
24
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>
39 \f
40
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
50
51 static tree gfc_get_derived_type (gfc_symbol * derived);
52
53 tree gfc_type_nodes[NUM_F95_TYPES];
54
55 tree gfc_array_index_type;
56 tree pvoid_type_node;
57 tree ppvoid_type_node;
58 tree pchar_type_node;
59
60 static GTY(()) tree gfc_desc_dim_type = NULL;
61
62 static GTY(()) tree gfc_max_array_element_size;
63
64 /* Arrays for all integral and real kinds.  We'll fill this in at runtime
65    after the target has a chance to process command-line options.  */
66
67 #define MAX_INT_KINDS 5
68 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
69 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
70
71 #define MAX_REAL_KINDS 4
72 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
73
74 /* The integer kind to use for array indices.  This will be set to the
75    proper value based on target information from the backend.  */
76
77 int gfc_index_integer_kind;
78
79 /* The default kinds of the various types.  */
80
81 int gfc_default_integer_kind;
82 int gfc_default_real_kind;
83 int gfc_default_double_kind;
84 int gfc_default_character_kind;
85 int gfc_default_logical_kind;
86 int gfc_default_complex_kind;
87
88 /* Query the target to determine which machine modes are available for
89    computation.  Choose KIND numbers for them.  */
90
91 void
92 gfc_init_kinds (void)
93 {
94   enum machine_mode mode;
95   int i_index, r_index;
96   bool saw_i4 = false, saw_i8 = false;
97   bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
98
99   for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
100     {
101       int kind, bitsize;
102
103       if (!targetm.scalar_mode_supported_p (mode))
104         continue;
105
106       if (i_index == MAX_INT_KINDS)
107         abort ();
108
109       /* Let the kind equal the bit size divided by 8.  This insulates the
110          programmer from the underlying byte size.  */
111       bitsize = GET_MODE_BITSIZE (mode);
112       kind = bitsize / 8;
113
114       if (kind == 4)
115         saw_i4 = true;
116       if (kind == 8)
117         saw_i8 = true;
118
119       gfc_integer_kinds[i_index].kind = kind;
120       gfc_integer_kinds[i_index].radix = 2;
121       gfc_integer_kinds[i_index].digits = bitsize - 1;
122       gfc_integer_kinds[i_index].bit_size = bitsize;
123
124       gfc_logical_kinds[i_index].kind = kind;
125       gfc_logical_kinds[i_index].bit_size = bitsize;
126
127       i_index += 1;
128     }
129
130   for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
131     {
132       const struct real_format *fmt = REAL_MODE_FORMAT (mode);
133       int kind;
134
135       if (fmt == NULL)
136         continue;
137       if (!targetm.scalar_mode_supported_p (mode))
138         continue;
139
140       /* Let the kind equal the precision divided by 8, rounding up.  Again,
141          this insulates the programmer from the underlying byte size.
142
143          Also, it effectively deals with IEEE extended formats.  There, the
144          total size of the type may equal 16, but it's got 6 bytes of padding
145          and the increased size can get in the way of a real IEEE quad format
146          which may also be supported by the target.
147
148          We round up so as to handle IA-64 __floatreg (RFmode), which is an
149          82 bit type.  Not to be confused with __float80 (XFmode), which is
150          an 80 bit type also supported by IA-64.  So XFmode should come out
151          to be kind=10, and RFmode should come out to be kind=11.  Egads.  */
152
153       kind = (GET_MODE_PRECISION (mode) + 7) / 8;
154
155       if (kind == 4)
156         saw_r4 = true;
157       if (kind == 8)
158         saw_r8 = true;
159       if (kind == 16)
160         saw_r16 = true;
161
162       /* Careful we don't stumble a wierd internal mode.  */
163       if (r_index > 0 && gfc_real_kinds[r_index-1].kind == kind)
164         abort ();
165       /* Or have too many modes for the allocated space.  */
166       if (r_index == MAX_REAL_KINDS)
167         abort ();
168
169       gfc_real_kinds[r_index].kind = kind;
170       gfc_real_kinds[r_index].radix = fmt->b;
171       gfc_real_kinds[r_index].digits = fmt->p;
172       gfc_real_kinds[r_index].min_exponent = fmt->emin;
173       gfc_real_kinds[r_index].max_exponent = fmt->emax;
174       r_index += 1;
175     }
176
177   /* Choose the default integer kind.  We choose 4 unless the user
178      directs us otherwise.  */
179   if (gfc_option.i8)
180     {
181       if (!saw_i8)
182         fatal_error ("integer kind=8 not available for -i8 option");
183       gfc_default_integer_kind = 8;
184     }
185   else if (saw_i4)
186     gfc_default_integer_kind = 4;
187   else
188     gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
189
190   /* Choose the default real kind.  Again, we choose 4 when possible.  */
191   if (gfc_option.r8)
192     {
193       if (!saw_r8)
194         fatal_error ("real kind=8 not available for -r8 option");
195       gfc_default_real_kind = 8;
196     }
197   else if (saw_r4)
198     gfc_default_real_kind = 4;
199   else
200     gfc_default_real_kind = gfc_real_kinds[0].kind;
201
202   /* Choose the default double kind.  If -r8 is specified, we use kind=16,
203      if it's available, otherwise we do not change anything.  */
204   if (gfc_option.r8 && saw_r16)
205     gfc_default_double_kind = 16;
206   else if (saw_r4 && saw_r8)
207     gfc_default_double_kind = 8;
208   else
209     {
210       /* F95 14.6.3.1: A nonpointer scalar object of type double precision
211          real ... occupies two contiguous numeric storage units.
212
213          Therefore we must be supplied a kind twice as large as we chose
214          for single precision.  There are loopholes, in that double
215          precision must *occupy* two storage units, though it doesn't have
216          to *use* two storage units.  Which means that you can make this
217          kind artificially wide by padding it.  But at present there are
218          no GCC targets for which a two-word type does not exist, so we
219          just let gfc_validate_kind abort and tell us if something breaks.  */
220
221       gfc_default_double_kind
222         = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
223     }
224
225   /* The default logical kind is constrained to be the same as the
226      default integer kind.  Similarly with complex and real.  */
227   gfc_default_logical_kind = gfc_default_integer_kind;
228   gfc_default_complex_kind = gfc_default_real_kind;
229
230   /* Choose the smallest integer kind for our default character.  */
231   gfc_default_character_kind = gfc_integer_kinds[0].kind;
232
233   /* Choose the integer kind the same size as "void*" for our index kind.  */
234   gfc_index_integer_kind = POINTER_SIZE / 8;
235 }
236
237 /* Make sure that a valid kind is present.  Returns an index into the
238    associated kinds array, -1 if the kind is not present.  */
239
240 static int
241 validate_integer (int kind)
242 {
243   int i;
244
245   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
246     if (gfc_integer_kinds[i].kind == kind)
247       return i;
248
249   return -1;
250 }
251
252 static int
253 validate_real (int kind)
254 {
255   int i;
256
257   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
258     if (gfc_real_kinds[i].kind == kind)
259       return i;
260
261   return -1;
262 }
263
264 static int
265 validate_logical (int kind)
266 {
267   int i;
268
269   for (i = 0; gfc_logical_kinds[i].kind; i++)
270     if (gfc_logical_kinds[i].kind == kind)
271       return i;
272
273   return -1;
274 }
275
276 static int
277 validate_character (int kind)
278 {
279   return kind == gfc_default_character_kind ? 0 : -1;
280 }
281
282 /* Validate a kind given a basic type.  The return value is the same
283    for the child functions, with -1 indicating nonexistence of the
284    type.  If MAY_FAIL is false, then -1 is never returned, and we ICE.  */
285
286 int
287 gfc_validate_kind (bt type, int kind, bool may_fail)
288 {
289   int rc;
290
291   switch (type)
292     {
293     case BT_REAL:               /* Fall through */
294     case BT_COMPLEX:
295       rc = validate_real (kind);
296       break;
297     case BT_INTEGER:
298       rc = validate_integer (kind);
299       break;
300     case BT_LOGICAL:
301       rc = validate_logical (kind);
302       break;
303     case BT_CHARACTER:
304       rc = validate_character (kind);
305       break;
306
307     default:
308       gfc_internal_error ("gfc_validate_kind(): Got bad type");
309     }
310
311   if (rc < 0 && !may_fail)
312     gfc_internal_error ("gfc_validate_kind(): Got bad kind");
313
314   return rc;
315 }
316
317
318 /* Create the backend type nodes. We map them to their
319    equivalent C type, at least for now.  We also give
320    names to the types here, and we push them in the
321    global binding level context.*/
322
323 void
324 gfc_init_types (void)
325 {
326   unsigned n;
327   unsigned HOST_WIDE_INT hi;
328   unsigned HOST_WIDE_INT lo;
329
330   /* Name the types.  */
331 #define PUSH_TYPE(name, node) \
332   pushdecl (build_decl (TYPE_DECL, get_identifier (name), node))
333
334   gfc_int1_type_node = signed_char_type_node;
335   PUSH_TYPE ("int1", gfc_int1_type_node);
336   gfc_int2_type_node = short_integer_type_node;
337   PUSH_TYPE ("int2", gfc_int2_type_node);
338   gfc_int4_type_node = gfc_type_for_size (32, 0 /*unsigned */ );
339   PUSH_TYPE ("int4", gfc_int4_type_node);
340   gfc_int8_type_node = gfc_type_for_size (64, 0 /*unsigned */ );
341   PUSH_TYPE ("int8", gfc_int8_type_node);
342 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
343   gfc_int16_type_node = gfc_type_for_size (128, 0 /*unsigned */ );
344   PUSH_TYPE ("int16", gfc_int16_type_node);
345 #endif
346
347   gfc_real4_type_node = float_type_node;
348   PUSH_TYPE ("real4", gfc_real4_type_node);
349   gfc_real8_type_node = double_type_node;
350   PUSH_TYPE ("real8", gfc_real8_type_node);
351 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
352   /* Hmm, this will not work. Ref. g77 */
353   gfc_real16_type_node = long_double_type_node;
354   PUSH_TYPE ("real16", gfc_real16_type_node);
355 #endif
356
357   gfc_complex4_type_node = complex_float_type_node;
358   PUSH_TYPE ("complex4", gfc_complex4_type_node);
359   gfc_complex8_type_node = complex_double_type_node;
360   PUSH_TYPE ("complex8", gfc_complex8_type_node);
361 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
362   /* Hmm, this will not work. Ref. g77 */
363   gfc_complex16_type_node = complex_long_double_type_node;
364   PUSH_TYPE ("complex16", gfc_complex16_type_node);
365 #endif
366
367   gfc_logical1_type_node = make_node (BOOLEAN_TYPE);
368   TYPE_PRECISION (gfc_logical1_type_node) = 8;
369   fixup_unsigned_type (gfc_logical1_type_node);
370   PUSH_TYPE ("logical1", gfc_logical1_type_node);
371   gfc_logical2_type_node = make_node (BOOLEAN_TYPE);
372   TYPE_PRECISION (gfc_logical2_type_node) = 16;
373   fixup_unsigned_type (gfc_logical2_type_node);
374   PUSH_TYPE ("logical2", gfc_logical2_type_node);
375   gfc_logical4_type_node = make_node (BOOLEAN_TYPE);
376   TYPE_PRECISION (gfc_logical4_type_node) = 32;
377   fixup_unsigned_type (gfc_logical4_type_node);
378   PUSH_TYPE ("logical4", gfc_logical4_type_node);
379   gfc_logical8_type_node = make_node (BOOLEAN_TYPE);
380   TYPE_PRECISION (gfc_logical8_type_node) = 64;
381   fixup_unsigned_type (gfc_logical8_type_node);
382   PUSH_TYPE ("logical8", gfc_logical8_type_node);
383 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
384   gfc_logical16_type_node = make_node (BOOLEAN_TYPE);
385   TYPE_PRECISION (gfc_logical16_type_node) = 128;
386   fixup_unsigned_type (gfc_logical16_type_node);
387   PUSH_TYPE ("logical16", gfc_logical16_type_node);
388 #endif
389
390   gfc_character1_type_node = build_type_variant (signed_char_type_node, 0, 0);
391   PUSH_TYPE ("char", gfc_character1_type_node);
392
393   PUSH_TYPE ("byte", unsigned_char_type_node);
394   PUSH_TYPE ("void", void_type_node);
395
396   /* DBX debugging output gets upset if these aren't set.  */
397   if (!TYPE_NAME (integer_type_node))
398     PUSH_TYPE ("c_integer", integer_type_node);
399   if (!TYPE_NAME (char_type_node))
400     PUSH_TYPE ("c_char", char_type_node);
401 #undef PUSH_TYPE
402
403   pvoid_type_node = build_pointer_type (void_type_node);
404   ppvoid_type_node = build_pointer_type (pvoid_type_node);
405   pchar_type_node = build_pointer_type (gfc_character1_type_node);
406
407   gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
408
409   /* The maximum array element size that can be handled is determined
410      by the number of bits available to store this field in the array
411      descriptor.  */
412
413   n = TREE_INT_CST_LOW (TYPE_SIZE (gfc_array_index_type))
414       - GFC_DTYPE_SIZE_SHIFT;
415
416   if (n > sizeof (HOST_WIDE_INT) * 8)
417     {
418       lo = ~(unsigned HOST_WIDE_INT) 0;
419       hi = lo >> (sizeof (HOST_WIDE_INT) * 16 - n);
420     }
421   else
422     {
423       hi = 0;
424       lo = (~(unsigned HOST_WIDE_INT) 0) >> (sizeof (HOST_WIDE_INT) * 8 - n);
425     }
426   gfc_max_array_element_size
427     = build_int_cst_wide (long_unsigned_type_node, lo, hi);
428
429   size_type_node = gfc_array_index_type;
430   boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
431
432   boolean_true_node = build_int_cst (boolean_type_node, 1);
433   boolean_false_node = build_int_cst (boolean_type_node, 0);
434 }
435
436 /* Get a type node for an integer kind.  */
437
438 tree
439 gfc_get_int_type (int kind)
440 {
441   switch (kind)
442     {
443     case 1:
444       return (gfc_int1_type_node);
445     case 2:
446       return (gfc_int2_type_node);
447     case 4:
448       return (gfc_int4_type_node);
449     case 8:
450       return (gfc_int8_type_node);
451 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
452     case 16:
453       return (95 _int16_type_node);
454 #endif
455     default:
456       fatal_error ("integer kind=%d not available", kind);
457     }
458 }
459
460 /* Get a type node for a real kind.  */
461
462 tree
463 gfc_get_real_type (int kind)
464 {
465   switch (kind)
466     {
467     case 4:
468       return (gfc_real4_type_node);
469     case 8:
470       return (gfc_real8_type_node);
471 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
472     case 16:
473       return (gfc_real16_type_node);
474 #endif
475     default:
476       fatal_error ("real kind=%d not available", kind);
477     }
478 }
479
480 /* Get a type node for a complex kind.  */
481
482 tree
483 gfc_get_complex_type (int kind)
484 {
485
486   switch (kind)
487     {
488     case 4:
489       return (gfc_complex4_type_node);
490     case 8:
491       return (gfc_complex8_type_node);
492 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
493     case 16:
494       return (gfc_complex16_type_node);
495 #endif
496     default:
497       fatal_error ("complex kind=%d not available", kind);
498     }
499 }
500
501 /* Get a type node for a logical kind.  */
502
503 tree
504 gfc_get_logical_type (int kind)
505 {
506   switch (kind)
507     {
508     case 1:
509       return (gfc_logical1_type_node);
510     case 2:
511       return (gfc_logical2_type_node);
512     case 4:
513       return (gfc_logical4_type_node);
514     case 8:
515       return (gfc_logical8_type_node);
516 #if (GFC_USE_TYPES16 && (HOST_BITS_PER_WIDE_INT >= 64))
517     case 16:
518       return (gfc_logical16_type_node);
519 #endif
520     default:
521       fatal_error ("logical kind=%d not available", kind);
522     }
523 }
524 \f
525 /* Create a character type with the given kind and length.  */
526
527 tree
528 gfc_get_character_type_len (int kind, tree len)
529 {
530   tree base;
531   tree bounds;
532   tree type;
533
534   switch (kind)
535     {
536     case 1:
537       base = gfc_character1_type_node;
538       break;
539
540     default:
541       fatal_error ("character kind=%d not available", kind);
542     }
543
544   bounds = build_range_type (gfc_array_index_type, gfc_index_one_node, len);
545   type = build_array_type (base, bounds);
546   TYPE_STRING_FLAG (type) = 1;
547
548   return type;
549 }
550
551
552 /* Get a type node for a character kind.  */
553
554 tree
555 gfc_get_character_type (int kind, gfc_charlen * cl)
556 {
557   tree len;
558
559   len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
560
561   return gfc_get_character_type_len (kind, len);
562 }
563 \f
564 /* Covert a basic type.  This will be an array for character types.  */
565
566 tree
567 gfc_typenode_for_spec (gfc_typespec * spec)
568 {
569   tree basetype;
570
571   switch (spec->type)
572     {
573     case BT_UNKNOWN:
574       abort ();
575       break;
576
577     case BT_INTEGER:
578       basetype = gfc_get_int_type (spec->kind);
579       break;
580
581     case BT_REAL:
582       basetype = gfc_get_real_type (spec->kind);
583       break;
584
585     case BT_COMPLEX:
586       basetype = gfc_get_complex_type (spec->kind);
587       break;
588
589     case BT_LOGICAL:
590       basetype = gfc_get_logical_type (spec->kind);
591       break;
592
593     case BT_CHARACTER:
594       basetype = gfc_get_character_type (spec->kind, spec->cl);
595       break;
596
597     case BT_DERIVED:
598       basetype = gfc_get_derived_type (spec->derived);
599       break;
600
601     default:
602       abort ();
603       break;
604     }
605   return basetype;
606 }
607 \f
608 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE.  */
609
610 static tree
611 gfc_conv_array_bound (gfc_expr * expr)
612 {
613   /* If expr is an integer constant, return that.  */
614   if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
615     return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
616
617   /* Otherwise return NULL.  */
618   return NULL_TREE;
619 }
620 \f
621 tree
622 gfc_get_element_type (tree type)
623 {
624   tree element;
625
626   if (GFC_ARRAY_TYPE_P (type))
627     {
628       if (TREE_CODE (type) == POINTER_TYPE)
629         type = TREE_TYPE (type);
630       assert (TREE_CODE (type) == ARRAY_TYPE);
631       element = TREE_TYPE (type);
632     }
633   else
634     {
635       assert (GFC_DESCRIPTOR_TYPE_P (type));
636       element = TREE_TYPE (TYPE_FIELDS (type));
637
638       assert (TREE_CODE (element) == POINTER_TYPE);
639       element = TREE_TYPE (element);
640
641       assert (TREE_CODE (element) == ARRAY_TYPE);
642       element = TREE_TYPE (element);
643     }
644
645   return element;
646 }
647 \f
648 /* Build an array. This function is called from gfc_sym_type().
649    Actually returns array descriptor type.
650
651    Format of array descriptors is as follows:
652
653     struct gfc_array_descriptor
654     {
655       array *data
656       index offset;
657       index dtype;
658       struct descriptor_dimension dimension[N_DIM];
659     }
660
661     struct descriptor_dimension
662     {
663       index stride;
664       index lbound;
665       index ubound;
666     }
667
668    Translation code should use gfc_conv_descriptor_* rather than accessing
669    the descriptor directly. Any changes to the array descriptor type will
670    require changes in gfc_conv_descriptor_* and gfc_build_array_initializer.
671
672    This is represented internally as a RECORD_TYPE. The index nodes are
673    gfc_array_index_type and the data node is a pointer to the data. See below
674    for the handling of character types.
675
676    The dtype member is formatted as follows:
677     rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
678     type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
679     size = dtype >> GFC_DTYPE_SIZE_SHIFT
680
681    I originally used nested ARRAY_TYPE nodes to represent arrays, but this
682    generated poor code for assumed/deferred size arrays.  These require
683    use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part of the GENERIC
684    grammar.  Also, there is no way to explicitly set the array stride, so
685    all data must be packed(1).  I've tried to mark all the functions which
686    would require modification with a GCC ARRAYS comment.
687
688    The data component points to the first element in the array.
689    The offset field is the position of the origin of the array
690    (ie element (0, 0 ...)).  This may be outsite the bounds of the array.
691
692    An element is accessed by
693    data[offset + index0*stride0 + index1*stride1 + index2*stride2]
694    This gives good performance as the computation does not involve the
695    bounds of the array.  For packed arrays, this is optimized further by
696    substituting the known strides.
697
698    This system has one problem: all array bounds must be withing 2^31 elements
699    of the origin (2^63 on 64-bit machines).  For example
700    integer, dimension (80000:90000, 80000:90000, 2) :: array
701    may not work properly on 32-bit machines because 80000*80000 > 2^31, so
702    the calculation for stride02 would overflow.  This may still work, but
703    I haven't checked, and it relies on the overflow doing the right thing.
704
705    The way to fix this problem is to access alements as follows:
706    data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
707    Obviously this is much slower.  I will make this a compile time option,
708    something like -fsmall-array-offsets.  Mixing code compiled with and without
709    this switch will work.
710
711    (1) This can be worked around by modifying the upper bound of the previous
712    dimension.  This requires extra fields in the descriptor (both real_ubound
713    and fake_ubound).  In tree.def there is mention of TYPE_SEP, which
714    may allow us to do this.  However I can't find mention of this anywhere
715    else.  */
716
717
718 /* Returns true if the array sym does not require a descriptor.  */
719
720 int
721 gfc_is_nodesc_array (gfc_symbol * sym)
722 {
723   assert (sym->attr.dimension);
724
725   /* We only want local arrays.  */
726   if (sym->attr.pointer || sym->attr.allocatable)
727     return 0;
728
729   if (sym->attr.dummy)
730     {
731       if (sym->as->type != AS_ASSUMED_SHAPE)
732         return 1;
733       else
734         return 0;
735     }
736
737   if (sym->attr.result || sym->attr.function)
738     return 0;
739
740   if (sym->attr.pointer || sym->attr.allocatable)
741     return 0;
742
743   assert (sym->as->type == AS_EXPLICIT);
744
745   return 1;
746 }
747
748
749 /* Create an array descriptor type.  */
750
751 static tree
752 gfc_build_array_type (tree type, gfc_array_spec * as)
753 {
754   tree lbound[GFC_MAX_DIMENSIONS];
755   tree ubound[GFC_MAX_DIMENSIONS];
756   int n;
757
758   for (n = 0; n < as->rank; n++)
759     {
760       /* Create expressions for the known bounds of the array.  */
761       if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
762         lbound[n] = gfc_index_one_node;
763       else
764         lbound[n] = gfc_conv_array_bound (as->lower[n]);
765       ubound[n] = gfc_conv_array_bound (as->upper[n]);
766     }
767
768   return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0);
769 }
770 \f
771 /* Returns the struct descriptor_dimension type.  */
772
773 static tree
774 gfc_get_desc_dim_type (void)
775 {
776   tree type;
777   tree decl;
778   tree fieldlist;
779
780   if (gfc_desc_dim_type)
781     return gfc_desc_dim_type;
782
783   /* Build the type node.  */
784   type = make_node (RECORD_TYPE);
785
786   TYPE_NAME (type) = get_identifier ("descriptor_dimension");
787   TYPE_PACKED (type) = 1;
788
789   /* Consists of the stride, lbound and ubound members.  */
790   decl = build_decl (FIELD_DECL,
791                      get_identifier ("stride"), gfc_array_index_type);
792   DECL_CONTEXT (decl) = type;
793   fieldlist = decl;
794
795   decl = build_decl (FIELD_DECL,
796                      get_identifier ("lbound"), gfc_array_index_type);
797   DECL_CONTEXT (decl) = type;
798   fieldlist = chainon (fieldlist, decl);
799
800   decl = build_decl (FIELD_DECL,
801                      get_identifier ("ubound"), gfc_array_index_type);
802   DECL_CONTEXT (decl) = type;
803   fieldlist = chainon (fieldlist, decl);
804
805   /* Finish off the type.  */
806   TYPE_FIELDS (type) = fieldlist;
807
808   gfc_finish_type (type);
809
810   gfc_desc_dim_type = type;
811   return type;
812 }
813
814 static tree
815 gfc_get_dtype (tree type, int rank)
816 {
817   tree size;
818   int n;
819   HOST_WIDE_INT i;
820   tree tmp;
821   tree dtype;
822
823   if (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type))
824     return (GFC_TYPE_ARRAY_DTYPE (type));
825
826   /* TODO: Correctly identify LOGICAL types.  */
827   switch (TREE_CODE (type))
828     {
829     case INTEGER_TYPE:
830       n = GFC_DTYPE_INTEGER;
831       break;
832
833     case BOOLEAN_TYPE:
834       n = GFC_DTYPE_LOGICAL;
835       break;
836
837     case REAL_TYPE:
838       n = GFC_DTYPE_REAL;
839       break;
840
841     case COMPLEX_TYPE:
842       n = GFC_DTYPE_COMPLEX;
843       break;
844
845     /* Arrays have already been dealt with.  */
846     case RECORD_TYPE:
847       n = GFC_DTYPE_DERIVED;
848       break;
849
850     case ARRAY_TYPE:
851       n = GFC_DTYPE_CHARACTER;
852       break;
853
854     default:
855       /* TODO: Don't do dtype for temporary descriptorless arrays.  */
856       /* We can strange array types for temporary arrays.  */
857       return gfc_index_zero_node;
858     }
859
860   assert (rank <= GFC_DTYPE_RANK_MASK);
861   size = TYPE_SIZE_UNIT (type);
862
863   i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
864   if (size && INTEGER_CST_P (size))
865     {
866       if (tree_int_cst_lt (gfc_max_array_element_size, size))
867         internal_error ("Array element size too big");
868
869       i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
870     }
871   dtype = build_int_cst (gfc_array_index_type, i);
872
873   if (size && !INTEGER_CST_P (size))
874     {
875       tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
876       tmp  = fold (build2 (LSHIFT_EXPR, gfc_array_index_type, size, tmp));
877       dtype = fold (build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype));
878     }
879   /* If we don't know the size we leave it as zero.  This should never happen
880      for anything that is actually used.  */
881   /* TODO: Check this is actually true, particularly when repacking
882      assumed size parameters.  */
883
884   return dtype;
885 }
886
887
888 /* Build an array type for use without a descriptor.  Valid values of packed
889    are 0=no, 1=partial, 2=full, 3=static.  */
890
891 tree
892 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, int packed)
893 {
894   tree range;
895   tree type;
896   tree tmp;
897   int n;
898   int known_stride;
899   int known_offset;
900   mpz_t offset;
901   mpz_t stride;
902   mpz_t delta;
903   gfc_expr *expr;
904
905   mpz_init_set_ui (offset, 0);
906   mpz_init_set_ui (stride, 1);
907   mpz_init (delta);
908
909   /* We don't use build_array_type because this does not include include
910      lang-specific information (ie. the bounds of the array) when checking
911      for duplicates.  */
912   type = make_node (ARRAY_TYPE);
913
914   GFC_ARRAY_TYPE_P (type) = 1;
915   TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
916     ggc_alloc_cleared (sizeof (struct lang_type));
917
918   known_stride = (packed != 0);
919   known_offset = 1;
920   for (n = 0; n < as->rank; n++)
921     {
922       /* Fill in the stride and bound components of the type.  */
923       if (known_stride)
924         tmp =  gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
925       else
926         tmp = NULL_TREE;
927       GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
928
929       expr = as->lower[n];
930       if (expr->expr_type == EXPR_CONSTANT)
931         {
932           tmp = gfc_conv_mpz_to_tree (expr->value.integer,
933                                   gfc_index_integer_kind);
934         }
935       else
936         {
937           known_stride = 0;
938           tmp = NULL_TREE;
939         }
940       GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
941
942       if (known_stride)
943         {
944           /* Calculate the offset.  */
945           mpz_mul (delta, stride, as->lower[n]->value.integer);
946           mpz_sub (offset, offset, delta);
947         }
948       else
949         known_offset = 0;
950
951       expr = as->upper[n];
952       if (expr && expr->expr_type == EXPR_CONSTANT)
953         {
954           tmp = gfc_conv_mpz_to_tree (expr->value.integer,
955                                   gfc_index_integer_kind);
956         }
957       else
958         {
959           tmp = NULL_TREE;
960           known_stride = 0;
961         }
962       GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
963
964       if (known_stride)
965         {
966           /* Calculate the stride.  */
967           mpz_sub (delta, as->upper[n]->value.integer,
968                    as->lower[n]->value.integer);
969           mpz_add_ui (delta, delta, 1);
970           mpz_mul (stride, stride, delta);
971         }
972
973       /* Only the first stride is known for partial packed arrays.  */
974       if (packed < 2)
975         known_stride = 0;
976     }
977
978   if (known_offset)
979     {
980       GFC_TYPE_ARRAY_OFFSET (type) =
981         gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
982     }
983   else
984     GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
985
986   if (known_stride)
987     {
988       GFC_TYPE_ARRAY_SIZE (type) =
989         gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
990     }
991   else
992     GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
993
994   GFC_TYPE_ARRAY_DTYPE (type) = gfc_get_dtype (etype, as->rank);
995   GFC_TYPE_ARRAY_RANK (type) = as->rank;
996   range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
997                             NULL_TREE);
998   /* TODO: use main type if it is unbounded.  */
999   GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1000     build_pointer_type (build_array_type (etype, range));
1001
1002   if (known_stride)
1003     {
1004       mpz_sub_ui (stride, stride, 1);
1005       range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1006     }
1007   else
1008     range = NULL_TREE;
1009
1010   range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1011   TYPE_DOMAIN (type) = range;
1012
1013   build_pointer_type (etype);
1014   TREE_TYPE (type) = etype;
1015
1016   layout_type (type);
1017
1018   mpz_clear (offset);
1019   mpz_clear (stride);
1020   mpz_clear (delta);
1021
1022   if (packed < 3 || !known_stride)
1023     {
1024       /* For dummy arrays and automatic (heap allocated) arrays we
1025          want a pointer to the array.  */
1026       type = build_pointer_type (type);
1027       GFC_ARRAY_TYPE_P (type) = 1;
1028       TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1029     }
1030   return type;
1031 }
1032
1033
1034 /* Build an array (descriptor) type with given bounds.  */
1035
1036 tree
1037 gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound,
1038                            tree * ubound, int packed)
1039 {
1040   tree fat_type, fat_pointer_type;
1041   tree fieldlist;
1042   tree arraytype;
1043   tree decl;
1044   int n;
1045   char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN];
1046   const char *typename;
1047   tree lower;
1048   tree upper;
1049   tree stride;
1050   tree tmp;
1051
1052   /* Build the type node.  */
1053   fat_type = make_node (RECORD_TYPE);
1054   GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1055   TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *)
1056     ggc_alloc_cleared (sizeof (struct lang_type));
1057   GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1058   GFC_TYPE_ARRAY_DTYPE (fat_type) = gfc_get_dtype (etype, dimen);
1059
1060   tmp = TYPE_NAME (etype);
1061   if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1062     tmp = DECL_NAME (tmp);
1063   if (tmp)
1064     typename = IDENTIFIER_POINTER (tmp);
1065   else
1066     typename = "unknown";
1067
1068   sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen,
1069            GFC_MAX_SYMBOL_LEN, typename);
1070   TYPE_NAME (fat_type) = get_identifier (name);
1071   TYPE_PACKED (fat_type) = 0;
1072
1073   fat_pointer_type = build_pointer_type (fat_type);
1074
1075   /* Build an array descriptor record type.  */
1076   if (packed != 0)
1077     stride = gfc_index_one_node;
1078   else
1079     stride = NULL_TREE;
1080
1081   for (n = 0; n < dimen; n++)
1082     {
1083       GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1084
1085       if (lbound)
1086         lower = lbound[n];
1087       else
1088         lower = NULL_TREE;
1089
1090       if (lower != NULL_TREE)
1091         {
1092           if (INTEGER_CST_P (lower))
1093             GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1094           else
1095             lower = NULL_TREE;
1096         }
1097
1098       upper = ubound[n];
1099       if (upper != NULL_TREE)
1100         {
1101           if (INTEGER_CST_P (upper))
1102             GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1103           else
1104             upper = NULL_TREE;
1105         }
1106
1107       if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1108         {
1109           tmp = fold (build2 (MINUS_EXPR, gfc_array_index_type, upper, lower));
1110           tmp = fold (build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1111                               gfc_index_one_node));
1112           stride =
1113             fold (build2 (MULT_EXPR, gfc_array_index_type, tmp, stride));
1114           /* Check the folding worked.  */
1115           assert (INTEGER_CST_P (stride));
1116         }
1117       else
1118         stride = NULL_TREE;
1119     }
1120   GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1121   /* TODO: known offsets for descriptors.  */
1122   GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1123
1124   /* We define data as an unknown size array. Much better than doing
1125      pointer arithmetic.  */
1126   arraytype =
1127     build_array_type (etype,
1128                       build_range_type (gfc_array_index_type,
1129                                         gfc_index_zero_node, NULL_TREE));
1130   arraytype = build_pointer_type (arraytype);
1131   GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1132
1133   /* The pointer to the array data.  */
1134   decl = build_decl (FIELD_DECL, get_identifier ("data"), arraytype);
1135
1136   DECL_CONTEXT (decl) = fat_type;
1137   /* Add the data member as the first element of the descriptor.  */
1138   fieldlist = decl;
1139
1140   /* Add the base component.  */
1141   decl = build_decl (FIELD_DECL, get_identifier ("offset"),
1142                      gfc_array_index_type);
1143   DECL_CONTEXT (decl) = fat_type;
1144   fieldlist = chainon (fieldlist, decl);
1145
1146   /* Add the dtype component.  */
1147   decl = build_decl (FIELD_DECL, get_identifier ("dtype"),
1148                      gfc_array_index_type);
1149   DECL_CONTEXT (decl) = fat_type;
1150   fieldlist = chainon (fieldlist, decl);
1151
1152   /* Build the array type for the stride and bound components.  */
1153   arraytype =
1154     build_array_type (gfc_get_desc_dim_type (),
1155                       build_range_type (gfc_array_index_type,
1156                                         gfc_index_zero_node,
1157                                         gfc_rank_cst[dimen - 1]));
1158
1159   decl = build_decl (FIELD_DECL, get_identifier ("dim"), arraytype);
1160   DECL_CONTEXT (decl) = fat_type;
1161   DECL_INITIAL (decl) = NULL_TREE;
1162   fieldlist = chainon (fieldlist, decl);
1163
1164   /* Finish off the type.  */
1165   TYPE_FIELDS (fat_type) = fieldlist;
1166
1167   gfc_finish_type (fat_type);
1168
1169   return fat_type;
1170 }
1171 \f
1172 /* Build a pointer type. This function is called from gfc_sym_type().  */
1173
1174 static tree
1175 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1176 {
1177   /* Array pointer types aren't actually pointers.  */
1178   if (sym->attr.dimension)
1179     return type;
1180   else
1181     return build_pointer_type (type);
1182 }
1183 \f
1184 /* Return the type for a symbol.  Special handling is required for character
1185    types to get the correct level of indirection.
1186    For functions return the return type.
1187    For subroutines return void_type_node.
1188    Calling this multiple times for the same symbol should be avoided,
1189    especially for character and array types.  */
1190
1191 tree
1192 gfc_sym_type (gfc_symbol * sym)
1193 {
1194   tree type;
1195   int byref;
1196
1197   if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1198     return void_type_node;
1199
1200   if (sym->backend_decl)
1201     {
1202       if (sym->attr.function)
1203         return TREE_TYPE (TREE_TYPE (sym->backend_decl));
1204       else
1205         return TREE_TYPE (sym->backend_decl);
1206     }
1207
1208   /* The frontend doesn't set all the attributes for a function with an
1209      explicit result value, so we use that instead when present.  */
1210   if (sym->attr.function && sym->result)
1211     sym = sym->result;
1212
1213   type = gfc_typenode_for_spec (&sym->ts);
1214
1215   if (sym->attr.dummy && !sym->attr.function)
1216     byref = 1;
1217   else
1218     byref = 0;
1219
1220   if (sym->attr.dimension)
1221     {
1222       if (gfc_is_nodesc_array (sym))
1223         {
1224           /* If this is a character argument of unknown length, just use the
1225              base type.  */
1226           if (sym->ts.type != BT_CHARACTER
1227               || !(sym->attr.dummy || sym->attr.function || sym->attr.result)
1228               || sym->ts.cl->backend_decl)
1229             {
1230               type = gfc_get_nodesc_array_type (type, sym->as,
1231                                                 byref ? 2 : 3);
1232               byref = 0;
1233             }
1234         }
1235       else
1236         type = gfc_build_array_type (type, sym->as);
1237     }
1238   else
1239     {
1240       if (sym->attr.allocatable || sym->attr.pointer)
1241         type = gfc_build_pointer_type (sym, type);
1242     }
1243
1244   /* We currently pass all parameters by reference.
1245      See f95_get_function_decl.  For dummy function parameters return the
1246      function type.  */
1247   if (byref)
1248     {
1249       /* We must use pointer types for potentially absent variables.  The
1250          optimizers assume a reference type argument is never NULL.  */
1251       if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1252         type = build_pointer_type (type);
1253       else
1254         type = build_reference_type (type);
1255     }
1256
1257   return (type);
1258 }
1259 \f
1260 /* Layout and output debug info for a record type.  */
1261
1262 void
1263 gfc_finish_type (tree type)
1264 {
1265   tree decl;
1266
1267   decl = build_decl (TYPE_DECL, NULL_TREE, type);
1268   TYPE_STUB_DECL (type) = decl;
1269   layout_type (type);
1270   rest_of_type_compilation (type, 1);
1271   rest_of_decl_compilation (decl, 1, 0);
1272 }
1273 \f
1274 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1275    or RECORD_TYPE pointed to by STYPE.  The new field is chained
1276    to the fieldlist pointed to by FIELDLIST.
1277
1278    Returns a pointer to the new field.  */
1279
1280 tree
1281 gfc_add_field_to_struct (tree *fieldlist, tree context,
1282                          tree name, tree type)
1283 {
1284   tree decl;
1285
1286   decl = build_decl (FIELD_DECL, name, type);
1287
1288   DECL_CONTEXT (decl) = context;
1289   DECL_INITIAL (decl) = 0;
1290   DECL_ALIGN (decl) = 0;
1291   DECL_USER_ALIGN (decl) = 0;
1292   TREE_CHAIN (decl) = NULL_TREE;
1293   *fieldlist = chainon (*fieldlist, decl);
1294
1295   return decl;
1296 }
1297
1298
1299 /* Build a tree node for a derived type.  */
1300
1301 static tree
1302 gfc_get_derived_type (gfc_symbol * derived)
1303 {
1304   tree typenode, field, field_type, fieldlist;
1305   gfc_component *c;
1306
1307   assert (derived && derived->attr.flavor == FL_DERIVED);
1308
1309   /* derived->backend_decl != 0 means we saw it before, but its
1310      components' backend_decl may have not been built.  */
1311   if (derived->backend_decl)
1312     {
1313       /* Its components' backend_decl have been built.  */
1314       if (TYPE_FIELDS (derived->backend_decl))
1315         return derived->backend_decl;
1316       else
1317         typenode = derived->backend_decl;
1318     }
1319   else
1320     {
1321       /* We see this derived type first time, so build the type node.  */
1322       typenode = make_node (RECORD_TYPE);
1323       TYPE_NAME (typenode) = get_identifier (derived->name);
1324       TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
1325       derived->backend_decl = typenode;
1326     }
1327
1328   /* Build the type member list. Install the newly created RECORD_TYPE
1329      node as DECL_CONTEXT of each FIELD_DECL.  */
1330   fieldlist = NULL_TREE;
1331   for (c = derived->components; c; c = c->next)
1332     {
1333       if (c->ts.type == BT_DERIVED && c->pointer)
1334         {
1335           if (c->ts.derived->backend_decl)
1336             field_type = c->ts.derived->backend_decl;
1337           else
1338             {
1339               /* Build the type node.  */
1340               field_type = make_node (RECORD_TYPE);
1341               TYPE_NAME (field_type) = get_identifier (c->ts.derived->name);
1342               TYPE_PACKED (field_type) = gfc_option.flag_pack_derived;
1343               c->ts.derived->backend_decl = field_type;
1344             }
1345         }
1346       else
1347         {
1348           if (c->ts.type == BT_CHARACTER)
1349             {
1350               /* Evaluate the string length.  */
1351               gfc_conv_const_charlen (c->ts.cl);
1352               assert (c->ts.cl->backend_decl);
1353             }
1354
1355           field_type = gfc_typenode_for_spec (&c->ts);
1356         }
1357
1358       /* This returns an array descriptor type.  Initialisation may be
1359          required.  */
1360       if (c->dimension)
1361         {
1362           if (c->pointer)
1363             {
1364               /* Pointers to arrays aren't actualy pointer types.  The
1365                  descriptors are seperate, but the data is common.  */
1366               field_type = gfc_build_array_type (field_type, c->as);
1367             }
1368           else
1369             field_type = gfc_get_nodesc_array_type (field_type, c->as, 3);
1370         }
1371       else if (c->pointer)
1372         field_type = build_pointer_type (field_type);
1373
1374       field = gfc_add_field_to_struct (&fieldlist, typenode,
1375                                        get_identifier (c->name),
1376                                        field_type);
1377
1378       DECL_PACKED (field) |= TYPE_PACKED (typenode);
1379
1380       assert (!c->backend_decl);
1381       c->backend_decl = field;
1382     }
1383
1384   /* Now we have the final fieldlist.  Record it, then lay out the
1385      derived type, including the fields.  */
1386   TYPE_FIELDS (typenode) = fieldlist;
1387
1388   gfc_finish_type (typenode);
1389
1390   derived->backend_decl = typenode;
1391
1392   return typenode;
1393 }
1394 \f
1395 int
1396 gfc_return_by_reference (gfc_symbol * sym)
1397 {
1398   if (!sym->attr.function)
1399     return 0;
1400
1401   assert (sym->attr.function);
1402
1403   if (sym->result)
1404     sym = sym->result;
1405
1406   if (sym->attr.dimension)
1407     return 1;
1408
1409   if (sym->ts.type == BT_CHARACTER)
1410     return 1;
1411
1412   if (sym->ts.type == BT_DERIVED)
1413     gfc_todo_error ("Returning derived types");
1414   /* Possibly return derived types by reference.  */
1415   return 0;
1416 }
1417 \f
1418 tree
1419 gfc_get_function_type (gfc_symbol * sym)
1420 {
1421   tree type;
1422   tree typelist;
1423   gfc_formal_arglist *f;
1424   gfc_symbol *arg;
1425   int nstr;
1426   int alternate_return;
1427
1428   /* Make sure this symbol is a function or a subroutine.  */
1429   assert (sym->attr.flavor == FL_PROCEDURE);
1430
1431   if (sym->backend_decl)
1432     return TREE_TYPE (sym->backend_decl);
1433
1434   nstr = 0;
1435   alternate_return = 0;
1436   typelist = NULL_TREE;
1437
1438   if (sym->attr.entry_master)
1439     {
1440       /* Additional parameter for selecting an entry point.  */
1441       typelist = gfc_chainon_list (typelist, gfc_array_index_type);
1442     }
1443
1444   /* Some functions we use an extra parameter for the return value.  */
1445   if (gfc_return_by_reference (sym))
1446     {
1447       if (sym->result)
1448         arg = sym->result;
1449       else
1450         arg = sym;
1451
1452       if (arg->ts.type == BT_CHARACTER)
1453         gfc_conv_const_charlen (arg->ts.cl);
1454
1455       type = gfc_sym_type (arg);
1456       if (arg->ts.type == BT_DERIVED
1457           || arg->attr.dimension
1458           || arg->ts.type == BT_CHARACTER)
1459         type = build_reference_type (type);
1460
1461       typelist = gfc_chainon_list (typelist, type);
1462       if (arg->ts.type == BT_CHARACTER)
1463         typelist = gfc_chainon_list (typelist, gfc_strlen_type_node);
1464     }
1465
1466   /* Build the argument types for the function.  */
1467   for (f = sym->formal; f; f = f->next)
1468     {
1469       arg = f->sym;
1470       if (arg)
1471         {
1472           /* Evaluate constant character lengths here so that they can be
1473              included in the type.  */
1474           if (arg->ts.type == BT_CHARACTER)
1475             gfc_conv_const_charlen (arg->ts.cl);
1476
1477           if (arg->attr.flavor == FL_PROCEDURE)
1478             {
1479               type = gfc_get_function_type (arg);
1480               type = build_pointer_type (type);
1481             }
1482           else
1483             type = gfc_sym_type (arg);
1484
1485           /* Parameter Passing Convention
1486
1487              We currently pass all parameters by reference.
1488              Parameters with INTENT(IN) could be passed by value.
1489              The problem arises if a function is called via an implicit
1490              prototype. In this situation the INTENT is not known.
1491              For this reason all parameters to global functions must be
1492              passed by reference.  Passing by value would potentialy
1493              generate bad code.  Worse there would be no way of telling that
1494              this code was bad, except that it would give incorrect results.
1495
1496              Contained procedures could pass by value as these are never
1497              used without an explicit interface, and connot be passed as
1498              actual parameters for a dummy procedure.  */
1499           if (arg->ts.type == BT_CHARACTER)
1500             nstr++;
1501           typelist = gfc_chainon_list (typelist, type);
1502         }
1503       else
1504         {
1505           if (sym->attr.subroutine)
1506             alternate_return = 1;
1507         }
1508     }
1509
1510   /* Add hidden string length parameters.  */
1511   while (nstr--)
1512     typelist = gfc_chainon_list (typelist, gfc_strlen_type_node);
1513
1514   typelist = gfc_chainon_list (typelist, void_type_node);
1515
1516   if (alternate_return)
1517     type = integer_type_node;
1518   else if (!sym->attr.function || gfc_return_by_reference (sym))
1519     type = void_type_node;
1520   else
1521     type = gfc_sym_type (sym);
1522
1523   type = build_function_type (type, typelist);
1524
1525   return type;
1526 }
1527 \f
1528 /* Routines for getting integer type nodes.  */
1529
1530
1531 /* Return an integer type with BITS bits of precision,
1532    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
1533
1534 tree
1535 gfc_type_for_size (unsigned bits, int unsignedp)
1536 {
1537   if (bits == TYPE_PRECISION (integer_type_node))
1538     return unsignedp ? unsigned_type_node : integer_type_node;
1539
1540   if (bits == TYPE_PRECISION (signed_char_type_node))
1541     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1542
1543   if (bits == TYPE_PRECISION (short_integer_type_node))
1544     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1545
1546   if (bits == TYPE_PRECISION (long_integer_type_node))
1547     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1548
1549   if (bits == TYPE_PRECISION (long_long_integer_type_node))
1550     return (unsignedp ? long_long_unsigned_type_node
1551             : long_long_integer_type_node);
1552 /*TODO: We currently don't initialise this...
1553   if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1554     return (unsignedp ? widest_unsigned_literal_type_node
1555             : widest_integer_literal_type_node);*/
1556
1557   if (bits <= TYPE_PRECISION (intQI_type_node))
1558     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1559
1560   if (bits <= TYPE_PRECISION (intHI_type_node))
1561     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1562
1563   if (bits <= TYPE_PRECISION (intSI_type_node))
1564     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1565
1566   if (bits <= TYPE_PRECISION (intDI_type_node))
1567     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1568
1569   return 0;
1570 }
1571
1572 /* Return a data type that has machine mode MODE.
1573    If the mode is an integer,
1574    then UNSIGNEDP selects between signed and unsigned types.  */
1575
1576 tree
1577 gfc_type_for_mode (enum machine_mode mode, int unsignedp)
1578 {
1579   if (mode == TYPE_MODE (integer_type_node))
1580     return unsignedp ? unsigned_type_node : integer_type_node;
1581
1582   if (mode == TYPE_MODE (signed_char_type_node))
1583     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1584
1585   if (mode == TYPE_MODE (short_integer_type_node))
1586     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1587
1588   if (mode == TYPE_MODE (long_integer_type_node))
1589     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1590
1591   if (mode == TYPE_MODE (long_long_integer_type_node))
1592     return unsignedp ? long_long_unsigned_type_node :
1593       long_long_integer_type_node;
1594
1595 /*TODO: see above
1596   if (mode == TYPE_MODE (widest_integer_literal_type_node))
1597     return unsignedp ? widest_unsigned_literal_type_node
1598                      : widest_integer_literal_type_node;
1599 */
1600
1601   if (mode == QImode)
1602     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1603
1604   if (mode == HImode)
1605     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1606
1607   if (mode == SImode)
1608     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1609
1610   if (mode == DImode)
1611     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1612
1613 #if HOST_BITS_PER_WIDE_INT >= 64
1614   if (mode == TYPE_MODE (intTI_type_node))
1615     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1616 #endif
1617
1618   if (mode == TYPE_MODE (float_type_node))
1619     return float_type_node;
1620
1621   if (mode == TYPE_MODE (double_type_node))
1622     return double_type_node;
1623
1624   if (mode == TYPE_MODE (long_double_type_node))
1625     return long_double_type_node;
1626
1627   if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1628     return build_pointer_type (char_type_node);
1629
1630   if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1631     return build_pointer_type (integer_type_node);
1632
1633   if (VECTOR_MODE_P (mode))
1634     {
1635       enum machine_mode inner_mode = GET_MODE_INNER (mode);
1636       tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
1637       if (inner_type != NULL_TREE)
1638         return build_vector_type_for_mode (inner_type, mode);
1639     }
1640
1641   return 0;
1642 }
1643
1644 /* Return an unsigned type the same as TYPE in other respects.  */
1645
1646 tree
1647 gfc_unsigned_type (tree type)
1648 {
1649   tree type1 = TYPE_MAIN_VARIANT (type);
1650
1651   if (type1 == signed_char_type_node || type1 == char_type_node)
1652     return unsigned_char_type_node;
1653   if (type1 == integer_type_node)
1654     return unsigned_type_node;
1655   if (type1 == short_integer_type_node)
1656     return short_unsigned_type_node;
1657   if (type1 == long_integer_type_node)
1658     return long_unsigned_type_node;
1659   if (type1 == long_long_integer_type_node)
1660     return long_long_unsigned_type_node;
1661 /*TODO :see others
1662   if (type1 == widest_integer_literal_type_node)
1663     return widest_unsigned_literal_type_node;
1664 */
1665 #if HOST_BITS_PER_WIDE_INT >= 64
1666   if (type1 == intTI_type_node)
1667     return unsigned_intTI_type_node;
1668 #endif
1669   if (type1 == intDI_type_node)
1670     return unsigned_intDI_type_node;
1671   if (type1 == intSI_type_node)
1672     return unsigned_intSI_type_node;
1673   if (type1 == intHI_type_node)
1674     return unsigned_intHI_type_node;
1675   if (type1 == intQI_type_node)
1676     return unsigned_intQI_type_node;
1677
1678   return gfc_signed_or_unsigned_type (1, type);
1679 }
1680
1681 /* Return a signed type the same as TYPE in other respects.  */
1682
1683 tree
1684 gfc_signed_type (tree type)
1685 {
1686   tree type1 = TYPE_MAIN_VARIANT (type);
1687
1688   if (type1 == unsigned_char_type_node || type1 == char_type_node)
1689     return signed_char_type_node;
1690   if (type1 == unsigned_type_node)
1691     return integer_type_node;
1692   if (type1 == short_unsigned_type_node)
1693     return short_integer_type_node;
1694   if (type1 == long_unsigned_type_node)
1695     return long_integer_type_node;
1696   if (type1 == long_long_unsigned_type_node)
1697     return long_long_integer_type_node;
1698 /*TODO: see others
1699   if (type1 == widest_unsigned_literal_type_node)
1700     return widest_integer_literal_type_node;
1701 */
1702 #if HOST_BITS_PER_WIDE_INT >= 64
1703   if (type1 == unsigned_intTI_type_node)
1704     return intTI_type_node;
1705 #endif
1706   if (type1 == unsigned_intDI_type_node)
1707     return intDI_type_node;
1708   if (type1 == unsigned_intSI_type_node)
1709     return intSI_type_node;
1710   if (type1 == unsigned_intHI_type_node)
1711     return intHI_type_node;
1712   if (type1 == unsigned_intQI_type_node)
1713     return intQI_type_node;
1714
1715   return gfc_signed_or_unsigned_type (0, type);
1716 }
1717
1718 /* Return a type the same as TYPE except unsigned or
1719    signed according to UNSIGNEDP.  */
1720
1721 tree
1722 gfc_signed_or_unsigned_type (int unsignedp, tree type)
1723 {
1724   if (!INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
1725     return type;
1726
1727   if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1728     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1729   if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1730     return unsignedp ? unsigned_type_node : integer_type_node;
1731   if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1732     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1733   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1734     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1735   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1736     return (unsignedp ? long_long_unsigned_type_node
1737             : long_long_integer_type_node);
1738 /*TODO: see others
1739   if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
1740     return (unsignedp ? widest_unsigned_literal_type_node
1741             : widest_integer_literal_type_node);
1742 */
1743 #if HOST_BITS_PER_WIDE_INT >= 64
1744   if (TYPE_PRECISION (type) == TYPE_PRECISION (intTI_type_node))
1745     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1746 #endif
1747   if (TYPE_PRECISION (type) == TYPE_PRECISION (intDI_type_node))
1748     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1749   if (TYPE_PRECISION (type) == TYPE_PRECISION (intSI_type_node))
1750     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1751   if (TYPE_PRECISION (type) == TYPE_PRECISION (intHI_type_node))
1752     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1753   if (TYPE_PRECISION (type) == TYPE_PRECISION (intQI_type_node))
1754     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1755
1756   return type;
1757 }
1758
1759 #include "gt-fortran-trans-types.h"