OSDN Git Service

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