OSDN Git Service

gcc/ada:
[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, 2005, 2006, 2007, 2008, 2009,
3    2010
4    Free Software Foundation, Inc.
5    Contributed by Paul Brook <paul@nowt.org>
6    and Steven Bosscher <s.bosscher@student.tudelft.nl>
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* trans-types.c -- gfortran backend types */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tree.h"
30 #include "langhooks.h"  /* For iso-c-bindings.def.  */
31 #include "target.h"
32 #include "ggc.h"
33 #include "toplev.h"     /* For rest_of_decl_compilation/fatal_error.  */
34 #include "gfortran.h"
35 #include "trans.h"
36 #include "trans-types.h"
37 #include "trans-const.h"
38 #include "flags.h"
39 #include "dwarf2out.h"  /* For struct array_descr_info.  */
40 \f
41
42 #if (GFC_MAX_DIMENSIONS < 10)
43 #define GFC_RANK_DIGITS 1
44 #define GFC_RANK_PRINTF_FORMAT "%01d"
45 #elif (GFC_MAX_DIMENSIONS < 100)
46 #define GFC_RANK_DIGITS 2
47 #define GFC_RANK_PRINTF_FORMAT "%02d"
48 #else
49 #error If you really need >99 dimensions, continue the sequence above...
50 #endif
51
52 /* array of structs so we don't have to worry about xmalloc or free */
53 CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
54
55 tree gfc_array_index_type;
56 tree gfc_array_range_type;
57 tree gfc_character1_type_node;
58 tree pvoid_type_node;
59 tree prvoid_type_node;
60 tree ppvoid_type_node;
61 tree pchar_type_node;
62 tree pfunc_type_node;
63
64 tree gfc_charlen_type_node;
65
66 static GTY(()) tree gfc_desc_dim_type;
67 static GTY(()) tree gfc_max_array_element_size;
68 static GTY(()) tree gfc_array_descriptor_base[2 * GFC_MAX_DIMENSIONS];
69
70 /* Arrays for all integral and real kinds.  We'll fill this in at runtime
71    after the target has a chance to process command-line options.  */
72
73 #define MAX_INT_KINDS 5
74 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
75 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
76 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
77 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
78
79 #define MAX_REAL_KINDS 5
80 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
81 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
82 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
83
84 #define MAX_CHARACTER_KINDS 2
85 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
86 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
87 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
88
89
90 /* The integer kind to use for array indices.  This will be set to the
91    proper value based on target information from the backend.  */
92
93 int gfc_index_integer_kind;
94
95 /* The default kinds of the various types.  */
96
97 int gfc_default_integer_kind;
98 int gfc_max_integer_kind;
99 int gfc_default_real_kind;
100 int gfc_default_double_kind;
101 int gfc_default_character_kind;
102 int gfc_default_logical_kind;
103 int gfc_default_complex_kind;
104 int gfc_c_int_kind;
105
106 /* The kind size used for record offsets. If the target system supports
107    kind=8, this will be set to 8, otherwise it is set to 4.  */
108 int gfc_intio_kind; 
109
110 /* The integer kind used to store character lengths.  */
111 int gfc_charlen_int_kind;
112
113 /* The size of the numeric storage unit and character storage unit.  */
114 int gfc_numeric_storage_size;
115 int gfc_character_storage_size;
116
117
118 gfc_try
119 gfc_check_any_c_kind (gfc_typespec *ts)
120 {
121   int i;
122   
123   for (i = 0; i < ISOCBINDING_NUMBER; i++)
124     {
125       /* Check for any C interoperable kind for the given type/kind in ts.
126          This can be used after verify_c_interop to make sure that the
127          Fortran kind being used exists in at least some form for C.  */
128       if (c_interop_kinds_table[i].f90_type == ts->type &&
129           c_interop_kinds_table[i].value == ts->kind)
130         return SUCCESS;
131     }
132
133   return FAILURE;
134 }
135
136
137 static int
138 get_real_kind_from_node (tree type)
139 {
140   int i;
141
142   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
143     if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
144       return gfc_real_kinds[i].kind;
145
146   return -4;
147 }
148
149 static int
150 get_int_kind_from_node (tree type)
151 {
152   int i;
153
154   if (!type)
155     return -2;
156
157   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
158     if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
159       return gfc_integer_kinds[i].kind;
160
161   return -1;
162 }
163
164 /* Return a typenode for the "standard" C type with a given name.  */
165 static tree
166 get_typenode_from_name (const char *name)
167 {
168   if (name == NULL || *name == '\0')
169     return NULL_TREE;
170
171   if (strcmp (name, "char") == 0)
172     return char_type_node;
173   if (strcmp (name, "unsigned char") == 0)
174     return unsigned_char_type_node;
175   if (strcmp (name, "signed char") == 0)
176     return signed_char_type_node;
177
178   if (strcmp (name, "short int") == 0)
179     return short_integer_type_node;
180   if (strcmp (name, "short unsigned int") == 0)
181     return short_unsigned_type_node;
182
183   if (strcmp (name, "int") == 0)
184     return integer_type_node;
185   if (strcmp (name, "unsigned int") == 0)
186     return unsigned_type_node;
187
188   if (strcmp (name, "long int") == 0)
189     return long_integer_type_node;
190   if (strcmp (name, "long unsigned int") == 0)
191     return long_unsigned_type_node;
192
193   if (strcmp (name, "long long int") == 0)
194     return long_long_integer_type_node;
195   if (strcmp (name, "long long unsigned int") == 0)
196     return long_long_unsigned_type_node;
197
198   gcc_unreachable ();
199 }
200
201 static int
202 get_int_kind_from_name (const char *name)
203 {
204   return get_int_kind_from_node (get_typenode_from_name (name));
205 }
206
207
208 /* Get the kind number corresponding to an integer of given size,
209    following the required return values for ISO_FORTRAN_ENV INT* constants:
210    -2 is returned if we support a kind of larger size, -1 otherwise.  */
211 int
212 gfc_get_int_kind_from_width_isofortranenv (int size)
213 {
214   int i;
215
216   /* Look for a kind with matching storage size.  */
217   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
218     if (gfc_integer_kinds[i].bit_size == size)
219       return gfc_integer_kinds[i].kind;
220
221   /* Look for a kind with larger storage size.  */
222   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
223     if (gfc_integer_kinds[i].bit_size > size)
224       return -2;
225
226   return -1;
227 }
228
229 /* Get the kind number corresponding to a real of given storage size,
230    following the required return values for ISO_FORTRAN_ENV REAL* constants:
231    -2 is returned if we support a kind of larger size, -1 otherwise.  */
232 int
233 gfc_get_real_kind_from_width_isofortranenv (int size)
234 {
235   int i;
236
237   size /= 8;
238
239   /* Look for a kind with matching storage size.  */
240   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
241     if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
242       return gfc_real_kinds[i].kind;
243
244   /* Look for a kind with larger storage size.  */
245   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
246     if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
247       return -2;
248
249   return -1;
250 }
251
252
253
254 static int
255 get_int_kind_from_width (int size)
256 {
257   int i;
258
259   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
260     if (gfc_integer_kinds[i].bit_size == size)
261       return gfc_integer_kinds[i].kind;
262
263   return -2;
264 }
265
266 static int
267 get_int_kind_from_minimal_width (int size)
268 {
269   int i;
270
271   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
272     if (gfc_integer_kinds[i].bit_size >= size)
273       return gfc_integer_kinds[i].kind;
274
275   return -2;
276 }
277
278
279 /* Generate the CInteropKind_t objects for the C interoperable
280    kinds.  */
281
282 static
283 void init_c_interop_kinds (void)
284 {
285   int i;
286
287   /* init all pointers in the list to NULL */
288   for (i = 0; i < ISOCBINDING_NUMBER; i++)
289     {
290       /* Initialize the name and value fields.  */
291       c_interop_kinds_table[i].name[0] = '\0';
292       c_interop_kinds_table[i].value = -100;
293       c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
294     }
295
296 #define NAMED_INTCST(a,b,c,d) \
297   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
298   c_interop_kinds_table[a].f90_type = BT_INTEGER; \
299   c_interop_kinds_table[a].value = c;
300 #define NAMED_REALCST(a,b,c) \
301   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
302   c_interop_kinds_table[a].f90_type = BT_REAL; \
303   c_interop_kinds_table[a].value = c;
304 #define NAMED_CMPXCST(a,b,c) \
305   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
306   c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
307   c_interop_kinds_table[a].value = c;
308 #define NAMED_LOGCST(a,b,c) \
309   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
310   c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
311   c_interop_kinds_table[a].value = c;
312 #define NAMED_CHARKNDCST(a,b,c) \
313   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
314   c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
315   c_interop_kinds_table[a].value = c;
316 #define NAMED_CHARCST(a,b,c) \
317   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
318   c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
319   c_interop_kinds_table[a].value = c;
320 #define DERIVED_TYPE(a,b,c) \
321   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
322   c_interop_kinds_table[a].f90_type = BT_DERIVED; \
323   c_interop_kinds_table[a].value = c;
324 #define PROCEDURE(a,b) \
325   strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
326   c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
327   c_interop_kinds_table[a].value = 0;
328 #include "iso-c-binding.def"
329 }
330
331
332 /* Query the target to determine which machine modes are available for
333    computation.  Choose KIND numbers for them.  */
334
335 void
336 gfc_init_kinds (void)
337 {
338   unsigned int mode;
339   int i_index, r_index, kind;
340   bool saw_i4 = false, saw_i8 = false;
341   bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
342
343   for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
344     {
345       int kind, bitsize;
346
347       if (!targetm.scalar_mode_supported_p ((enum machine_mode) mode))
348         continue;
349
350       /* The middle end doesn't support constants larger than 2*HWI.
351          Perhaps the target hook shouldn't have accepted these either,
352          but just to be safe...  */
353       bitsize = GET_MODE_BITSIZE (mode);
354       if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
355         continue;
356
357       gcc_assert (i_index != MAX_INT_KINDS);
358
359       /* Let the kind equal the bit size divided by 8.  This insulates the
360          programmer from the underlying byte size.  */
361       kind = bitsize / 8;
362
363       if (kind == 4)
364         saw_i4 = true;
365       if (kind == 8)
366         saw_i8 = true;
367
368       gfc_integer_kinds[i_index].kind = kind;
369       gfc_integer_kinds[i_index].radix = 2;
370       gfc_integer_kinds[i_index].digits = bitsize - 1;
371       gfc_integer_kinds[i_index].bit_size = bitsize;
372
373       gfc_logical_kinds[i_index].kind = kind;
374       gfc_logical_kinds[i_index].bit_size = bitsize;
375
376       i_index += 1;
377     }
378
379   /* Set the kind used to match GFC_INT_IO in libgfortran.  This is 
380      used for large file access.  */
381
382   if (saw_i8)
383     gfc_intio_kind = 8;
384   else
385     gfc_intio_kind = 4;
386
387   /* If we do not at least have kind = 4, everything is pointless.  */  
388   gcc_assert(saw_i4);  
389
390   /* Set the maximum integer kind.  Used with at least BOZ constants.  */
391   gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
392
393   for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
394     {
395       const struct real_format *fmt =
396         REAL_MODE_FORMAT ((enum machine_mode) mode);
397       int kind;
398
399       if (fmt == NULL)
400         continue;
401       if (!targetm.scalar_mode_supported_p ((enum machine_mode) mode))
402         continue;
403
404       /* Only let float/double/long double go through because the fortran
405          library assumes these are the only floating point types.  */
406
407       if (mode != TYPE_MODE (float_type_node)
408           && (mode != TYPE_MODE (double_type_node))
409           && (mode != TYPE_MODE (long_double_type_node)))
410         continue;
411
412       /* Let the kind equal the precision divided by 8, rounding up.  Again,
413          this insulates the programmer from the underlying byte size.
414
415          Also, it effectively deals with IEEE extended formats.  There, the
416          total size of the type may equal 16, but it's got 6 bytes of padding
417          and the increased size can get in the way of a real IEEE quad format
418          which may also be supported by the target.
419
420          We round up so as to handle IA-64 __floatreg (RFmode), which is an
421          82 bit type.  Not to be confused with __float80 (XFmode), which is
422          an 80 bit type also supported by IA-64.  So XFmode should come out
423          to be kind=10, and RFmode should come out to be kind=11.  Egads.  */
424
425       kind = (GET_MODE_PRECISION (mode) + 7) / 8;
426
427       if (kind == 4)
428         saw_r4 = true;
429       if (kind == 8)
430         saw_r8 = true;
431       if (kind == 16)
432         saw_r16 = true;
433
434       /* Careful we don't stumble a weird internal mode.  */
435       gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
436       /* Or have too many modes for the allocated space.  */
437       gcc_assert (r_index != MAX_REAL_KINDS);
438
439       gfc_real_kinds[r_index].kind = kind;
440       gfc_real_kinds[r_index].radix = fmt->b;
441       gfc_real_kinds[r_index].digits = fmt->p;
442       gfc_real_kinds[r_index].min_exponent = fmt->emin;
443       gfc_real_kinds[r_index].max_exponent = fmt->emax;
444       if (fmt->pnan < fmt->p)
445         /* This is an IBM extended double format (or the MIPS variant)
446            made up of two IEEE doubles.  The value of the long double is
447            the sum of the values of the two parts.  The most significant
448            part is required to be the value of the long double rounded
449            to the nearest double.  If we use emax of 1024 then we can't
450            represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
451            rounding will make the most significant part overflow.  */
452         gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
453       gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
454       r_index += 1;
455     }
456
457   /* Choose the default integer kind.  We choose 4 unless the user
458      directs us otherwise.  */
459   if (gfc_option.flag_default_integer)
460     {
461       if (!saw_i8)
462         fatal_error ("integer kind=8 not available for -fdefault-integer-8 option");
463       gfc_default_integer_kind = 8;
464
465       /* Even if the user specified that the default integer kind be 8,
466          the numeric storage size isn't 64.  In this case, a warning will
467          be issued when NUMERIC_STORAGE_SIZE is used.  */
468       gfc_numeric_storage_size = 4 * 8;
469     }
470   else if (saw_i4)
471     {
472       gfc_default_integer_kind = 4;
473       gfc_numeric_storage_size = 4 * 8;
474     }
475   else
476     {
477       gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
478       gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
479     }
480
481   /* Choose the default real kind.  Again, we choose 4 when possible.  */
482   if (gfc_option.flag_default_real)
483     {
484       if (!saw_r8)
485         fatal_error ("real kind=8 not available for -fdefault-real-8 option");
486       gfc_default_real_kind = 8;
487     }
488   else if (saw_r4)
489     gfc_default_real_kind = 4;
490   else
491     gfc_default_real_kind = gfc_real_kinds[0].kind;
492
493   /* Choose the default double kind.  If -fdefault-real and -fdefault-double 
494      are specified, we use kind=8, if it's available.  If -fdefault-real is
495      specified without -fdefault-double, we use kind=16, if it's available.
496      Otherwise we do not change anything.  */
497   if (gfc_option.flag_default_double && !gfc_option.flag_default_real)
498     fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8");
499
500   if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8)
501     gfc_default_double_kind = 8;
502   else if (gfc_option.flag_default_real && saw_r16)
503     gfc_default_double_kind = 16;
504   else if (saw_r4 && saw_r8)
505     gfc_default_double_kind = 8;
506   else
507     {
508       /* F95 14.6.3.1: A nonpointer scalar object of type double precision
509          real ... occupies two contiguous numeric storage units.
510
511          Therefore we must be supplied a kind twice as large as we chose
512          for single precision.  There are loopholes, in that double
513          precision must *occupy* two storage units, though it doesn't have
514          to *use* two storage units.  Which means that you can make this
515          kind artificially wide by padding it.  But at present there are
516          no GCC targets for which a two-word type does not exist, so we
517          just let gfc_validate_kind abort and tell us if something breaks.  */
518
519       gfc_default_double_kind
520         = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
521     }
522
523   /* The default logical kind is constrained to be the same as the
524      default integer kind.  Similarly with complex and real.  */
525   gfc_default_logical_kind = gfc_default_integer_kind;
526   gfc_default_complex_kind = gfc_default_real_kind;
527
528   /* We only have two character kinds: ASCII and UCS-4.
529      ASCII corresponds to a 8-bit integer type, if one is available.
530      UCS-4 corresponds to a 32-bit integer type, if one is available. */
531   i_index = 0;
532   if ((kind = get_int_kind_from_width (8)) > 0)
533     {
534       gfc_character_kinds[i_index].kind = kind;
535       gfc_character_kinds[i_index].bit_size = 8;
536       gfc_character_kinds[i_index].name = "ascii";
537       i_index++;
538     }
539   if ((kind = get_int_kind_from_width (32)) > 0)
540     {
541       gfc_character_kinds[i_index].kind = kind;
542       gfc_character_kinds[i_index].bit_size = 32;
543       gfc_character_kinds[i_index].name = "iso_10646";
544       i_index++;
545     }
546
547   /* Choose the smallest integer kind for our default character.  */
548   gfc_default_character_kind = gfc_character_kinds[0].kind;
549   gfc_character_storage_size = gfc_default_character_kind * 8;
550
551   /* Choose the integer kind the same size as "void*" for our index kind.  */
552   gfc_index_integer_kind = POINTER_SIZE / 8;
553   /* Pick a kind the same size as the C "int" type.  */
554   gfc_c_int_kind = INT_TYPE_SIZE / 8;
555
556   /* initialize the C interoperable kinds  */
557   init_c_interop_kinds();
558 }
559
560 /* Make sure that a valid kind is present.  Returns an index into the
561    associated kinds array, -1 if the kind is not present.  */
562
563 static int
564 validate_integer (int kind)
565 {
566   int i;
567
568   for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
569     if (gfc_integer_kinds[i].kind == kind)
570       return i;
571
572   return -1;
573 }
574
575 static int
576 validate_real (int kind)
577 {
578   int i;
579
580   for (i = 0; gfc_real_kinds[i].kind != 0; i++)
581     if (gfc_real_kinds[i].kind == kind)
582       return i;
583
584   return -1;
585 }
586
587 static int
588 validate_logical (int kind)
589 {
590   int i;
591
592   for (i = 0; gfc_logical_kinds[i].kind; i++)
593     if (gfc_logical_kinds[i].kind == kind)
594       return i;
595
596   return -1;
597 }
598
599 static int
600 validate_character (int kind)
601 {
602   int i;
603
604   for (i = 0; gfc_character_kinds[i].kind; i++)
605     if (gfc_character_kinds[i].kind == kind)
606       return i;
607
608   return -1;
609 }
610
611 /* Validate a kind given a basic type.  The return value is the same
612    for the child functions, with -1 indicating nonexistence of the
613    type.  If MAY_FAIL is false, then -1 is never returned, and we ICE.  */
614
615 int
616 gfc_validate_kind (bt type, int kind, bool may_fail)
617 {
618   int rc;
619
620   switch (type)
621     {
622     case BT_REAL:               /* Fall through */
623     case BT_COMPLEX:
624       rc = validate_real (kind);
625       break;
626     case BT_INTEGER:
627       rc = validate_integer (kind);
628       break;
629     case BT_LOGICAL:
630       rc = validate_logical (kind);
631       break;
632     case BT_CHARACTER:
633       rc = validate_character (kind);
634       break;
635
636     default:
637       gfc_internal_error ("gfc_validate_kind(): Got bad type");
638     }
639
640   if (rc < 0 && !may_fail)
641     gfc_internal_error ("gfc_validate_kind(): Got bad kind");
642
643   return rc;
644 }
645
646
647 /* Four subroutines of gfc_init_types.  Create type nodes for the given kind.
648    Reuse common type nodes where possible.  Recognize if the kind matches up
649    with a C type.  This will be used later in determining which routines may
650    be scarfed from libm.  */
651
652 static tree
653 gfc_build_int_type (gfc_integer_info *info)
654 {
655   int mode_precision = info->bit_size;
656
657   if (mode_precision == CHAR_TYPE_SIZE)
658     info->c_char = 1;
659   if (mode_precision == SHORT_TYPE_SIZE)
660     info->c_short = 1;
661   if (mode_precision == INT_TYPE_SIZE)
662     info->c_int = 1;
663   if (mode_precision == LONG_TYPE_SIZE)
664     info->c_long = 1;
665   if (mode_precision == LONG_LONG_TYPE_SIZE)
666     info->c_long_long = 1;
667
668   if (TYPE_PRECISION (intQI_type_node) == mode_precision)
669     return intQI_type_node;
670   if (TYPE_PRECISION (intHI_type_node) == mode_precision)
671     return intHI_type_node;
672   if (TYPE_PRECISION (intSI_type_node) == mode_precision)
673     return intSI_type_node;
674   if (TYPE_PRECISION (intDI_type_node) == mode_precision)
675     return intDI_type_node;
676   if (TYPE_PRECISION (intTI_type_node) == mode_precision)
677     return intTI_type_node;
678
679   return make_signed_type (mode_precision);
680 }
681
682 tree
683 gfc_build_uint_type (int size)
684 {
685   if (size == CHAR_TYPE_SIZE)
686     return unsigned_char_type_node;
687   if (size == SHORT_TYPE_SIZE)
688     return short_unsigned_type_node;
689   if (size == INT_TYPE_SIZE)
690     return unsigned_type_node;
691   if (size == LONG_TYPE_SIZE)
692     return long_unsigned_type_node;
693   if (size == LONG_LONG_TYPE_SIZE)
694     return long_long_unsigned_type_node;
695
696   return make_unsigned_type (size);
697 }
698
699
700 static tree
701 gfc_build_real_type (gfc_real_info *info)
702 {
703   int mode_precision = info->mode_precision;
704   tree new_type;
705
706   if (mode_precision == FLOAT_TYPE_SIZE)
707     info->c_float = 1;
708   if (mode_precision == DOUBLE_TYPE_SIZE)
709     info->c_double = 1;
710   if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
711     info->c_long_double = 1;
712
713   if (TYPE_PRECISION (float_type_node) == mode_precision)
714     return float_type_node;
715   if (TYPE_PRECISION (double_type_node) == mode_precision)
716     return double_type_node;
717   if (TYPE_PRECISION (long_double_type_node) == mode_precision)
718     return long_double_type_node;
719
720   new_type = make_node (REAL_TYPE);
721   TYPE_PRECISION (new_type) = mode_precision;
722   layout_type (new_type);
723   return new_type;
724 }
725
726 static tree
727 gfc_build_complex_type (tree scalar_type)
728 {
729   tree new_type;
730
731   if (scalar_type == NULL)
732     return NULL;
733   if (scalar_type == float_type_node)
734     return complex_float_type_node;
735   if (scalar_type == double_type_node)
736     return complex_double_type_node;
737   if (scalar_type == long_double_type_node)
738     return complex_long_double_type_node;
739
740   new_type = make_node (COMPLEX_TYPE);
741   TREE_TYPE (new_type) = scalar_type;
742   layout_type (new_type);
743   return new_type;
744 }
745
746 static tree
747 gfc_build_logical_type (gfc_logical_info *info)
748 {
749   int bit_size = info->bit_size;
750   tree new_type;
751
752   if (bit_size == BOOL_TYPE_SIZE)
753     {
754       info->c_bool = 1;
755       return boolean_type_node;
756     }
757
758   new_type = make_unsigned_type (bit_size);
759   TREE_SET_CODE (new_type, BOOLEAN_TYPE);
760   TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
761   TYPE_PRECISION (new_type) = 1;
762
763   return new_type;
764 }
765
766
767 #if 0
768 /* Return the bit size of the C "size_t".  */
769
770 static unsigned int
771 c_size_t_size (void)
772 {
773 #ifdef SIZE_TYPE  
774   if (strcmp (SIZE_TYPE, "unsigned int") == 0)
775     return INT_TYPE_SIZE;
776   if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
777     return LONG_TYPE_SIZE;
778   if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
779     return SHORT_TYPE_SIZE;
780   gcc_unreachable ();
781 #else
782   return LONG_TYPE_SIZE;
783 #endif
784 }
785 #endif
786
787 /* Create the backend type nodes. We map them to their
788    equivalent C type, at least for now.  We also give
789    names to the types here, and we push them in the
790    global binding level context.*/
791
792 void
793 gfc_init_types (void)
794 {
795   char name_buf[18];
796   int index;
797   tree type;
798   unsigned n;
799   unsigned HOST_WIDE_INT hi;
800   unsigned HOST_WIDE_INT lo;
801
802   /* Create and name the types.  */
803 #define PUSH_TYPE(name, node) \
804   pushdecl (build_decl (input_location, \
805                         TYPE_DECL, get_identifier (name), node))
806
807   for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
808     {
809       type = gfc_build_int_type (&gfc_integer_kinds[index]);
810       /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set.  */
811       if (TYPE_STRING_FLAG (type))
812         type = make_signed_type (gfc_integer_kinds[index].bit_size);
813       gfc_integer_types[index] = type;
814       snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
815                 gfc_integer_kinds[index].kind);
816       PUSH_TYPE (name_buf, type);
817     }
818
819   for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
820     {
821       type = gfc_build_logical_type (&gfc_logical_kinds[index]);
822       gfc_logical_types[index] = type;
823       snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
824                 gfc_logical_kinds[index].kind);
825       PUSH_TYPE (name_buf, type);
826     }
827
828   for (index = 0; gfc_real_kinds[index].kind != 0; index++)
829     {
830       type = gfc_build_real_type (&gfc_real_kinds[index]);
831       gfc_real_types[index] = type;
832       snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
833                 gfc_real_kinds[index].kind);
834       PUSH_TYPE (name_buf, type);
835
836       type = gfc_build_complex_type (type);
837       gfc_complex_types[index] = type;
838       snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
839                 gfc_real_kinds[index].kind);
840       PUSH_TYPE (name_buf, type);
841     }
842
843   for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
844     {
845       type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
846       type = build_qualified_type (type, TYPE_UNQUALIFIED);
847       snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
848                 gfc_character_kinds[index].kind);
849       PUSH_TYPE (name_buf, type);
850       gfc_character_types[index] = type;
851       gfc_pcharacter_types[index] = build_pointer_type (type);
852     }
853   gfc_character1_type_node = gfc_character_types[0];
854
855   PUSH_TYPE ("byte", unsigned_char_type_node);
856   PUSH_TYPE ("void", void_type_node);
857
858   /* DBX debugging output gets upset if these aren't set.  */
859   if (!TYPE_NAME (integer_type_node))
860     PUSH_TYPE ("c_integer", integer_type_node);
861   if (!TYPE_NAME (char_type_node))
862     PUSH_TYPE ("c_char", char_type_node);
863
864 #undef PUSH_TYPE
865
866   pvoid_type_node = build_pointer_type (void_type_node);
867   prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
868   ppvoid_type_node = build_pointer_type (pvoid_type_node);
869   pchar_type_node = build_pointer_type (gfc_character1_type_node);
870   pfunc_type_node
871     = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
872
873   gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
874   /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
875      since this function is called before gfc_init_constants.  */
876   gfc_array_range_type
877           = build_range_type (gfc_array_index_type,
878                               build_int_cst (gfc_array_index_type, 0),
879                               NULL_TREE);
880
881   /* The maximum array element size that can be handled is determined
882      by the number of bits available to store this field in the array
883      descriptor.  */
884
885   n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
886   lo = ~ (unsigned HOST_WIDE_INT) 0;
887   if (n > HOST_BITS_PER_WIDE_INT)
888     hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
889   else
890     hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
891   gfc_max_array_element_size
892     = build_int_cst_wide (long_unsigned_type_node, lo, hi);
893
894   size_type_node = gfc_array_index_type;
895
896   boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
897   boolean_true_node = build_int_cst (boolean_type_node, 1);
898   boolean_false_node = build_int_cst (boolean_type_node, 0);
899
900   /* ??? Shouldn't this be based on gfc_index_integer_kind or so?  */
901   gfc_charlen_int_kind = 4;
902   gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
903 }
904
905 /* Get the type node for the given type and kind.  */
906
907 tree
908 gfc_get_int_type (int kind)
909 {
910   int index = gfc_validate_kind (BT_INTEGER, kind, true);
911   return index < 0 ? 0 : gfc_integer_types[index];
912 }
913
914 tree
915 gfc_get_real_type (int kind)
916 {
917   int index = gfc_validate_kind (BT_REAL, kind, true);
918   return index < 0 ? 0 : gfc_real_types[index];
919 }
920
921 tree
922 gfc_get_complex_type (int kind)
923 {
924   int index = gfc_validate_kind (BT_COMPLEX, kind, true);
925   return index < 0 ? 0 : gfc_complex_types[index];
926 }
927
928 tree
929 gfc_get_logical_type (int kind)
930 {
931   int index = gfc_validate_kind (BT_LOGICAL, kind, true);
932   return index < 0 ? 0 : gfc_logical_types[index];
933 }
934
935 tree
936 gfc_get_char_type (int kind)
937 {
938   int index = gfc_validate_kind (BT_CHARACTER, kind, true);
939   return index < 0 ? 0 : gfc_character_types[index];
940 }
941
942 tree
943 gfc_get_pchar_type (int kind)
944 {
945   int index = gfc_validate_kind (BT_CHARACTER, kind, true);
946   return index < 0 ? 0 : gfc_pcharacter_types[index];
947 }
948
949 \f
950 /* Create a character type with the given kind and length.  */
951
952 tree
953 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
954 {
955   tree bounds, type;
956
957   bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
958   type = build_array_type (eltype, bounds);
959   TYPE_STRING_FLAG (type) = 1;
960
961   return type;
962 }
963
964 tree
965 gfc_get_character_type_len (int kind, tree len)
966 {
967   gfc_validate_kind (BT_CHARACTER, kind, false);
968   return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
969 }
970
971
972 /* Get a type node for a character kind.  */
973
974 tree
975 gfc_get_character_type (int kind, gfc_charlen * cl)
976 {
977   tree len;
978
979   len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
980
981   return gfc_get_character_type_len (kind, len);
982 }
983 \f
984 /* Covert a basic type.  This will be an array for character types.  */
985
986 tree
987 gfc_typenode_for_spec (gfc_typespec * spec)
988 {
989   tree basetype;
990
991   switch (spec->type)
992     {
993     case BT_UNKNOWN:
994       gcc_unreachable ();
995
996     case BT_INTEGER:
997       /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
998          has been resolved.  This is done so we can convert C_PTR and
999          C_FUNPTR to simple variables that get translated to (void *).  */
1000       if (spec->f90_type == BT_VOID)
1001         {
1002           if (spec->u.derived
1003               && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1004             basetype = ptr_type_node;
1005           else
1006             basetype = pfunc_type_node;
1007         }
1008       else
1009         basetype = gfc_get_int_type (spec->kind);
1010       break;
1011
1012     case BT_REAL:
1013       basetype = gfc_get_real_type (spec->kind);
1014       break;
1015
1016     case BT_COMPLEX:
1017       basetype = gfc_get_complex_type (spec->kind);
1018       break;
1019
1020     case BT_LOGICAL:
1021       basetype = gfc_get_logical_type (spec->kind);
1022       break;
1023
1024     case BT_CHARACTER:
1025       basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1026       break;
1027
1028     case BT_DERIVED:
1029     case BT_CLASS:
1030       basetype = gfc_get_derived_type (spec->u.derived);
1031
1032       /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1033          type and kind to fit a (void *) and the basetype returned was a
1034          ptr_type_node.  We need to pass up this new information to the
1035          symbol that was declared of type C_PTR or C_FUNPTR.  */
1036       if (spec->u.derived->attr.is_iso_c)
1037         {
1038           spec->type = spec->u.derived->ts.type;
1039           spec->kind = spec->u.derived->ts.kind;
1040           spec->f90_type = spec->u.derived->ts.f90_type;
1041         }
1042       break;
1043     case BT_VOID:
1044       /* This is for the second arg to c_f_pointer and c_f_procpointer
1045          of the iso_c_binding module, to accept any ptr type.  */
1046       basetype = ptr_type_node;
1047       if (spec->f90_type == BT_VOID)
1048         {
1049           if (spec->u.derived
1050               && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1051             basetype = ptr_type_node;
1052           else
1053             basetype = pfunc_type_node;
1054         }
1055        break;
1056     default:
1057       gcc_unreachable ();
1058     }
1059   return basetype;
1060 }
1061 \f
1062 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE.  */
1063
1064 static tree
1065 gfc_conv_array_bound (gfc_expr * expr)
1066 {
1067   /* If expr is an integer constant, return that.  */
1068   if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1069     return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1070
1071   /* Otherwise return NULL.  */
1072   return NULL_TREE;
1073 }
1074 \f
1075 tree
1076 gfc_get_element_type (tree type)
1077 {
1078   tree element;
1079
1080   if (GFC_ARRAY_TYPE_P (type))
1081     {
1082       if (TREE_CODE (type) == POINTER_TYPE)
1083         type = TREE_TYPE (type);
1084       gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1085       element = TREE_TYPE (type);
1086     }
1087   else
1088     {
1089       gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1090       element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1091
1092       gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1093       element = TREE_TYPE (element);
1094
1095       gcc_assert (TREE_CODE (element) == ARRAY_TYPE);
1096       element = TREE_TYPE (element);
1097     }
1098
1099   return element;
1100 }
1101 \f
1102 /* Build an array.  This function is called from gfc_sym_type().
1103    Actually returns array descriptor type.
1104
1105    Format of array descriptors is as follows:
1106
1107     struct gfc_array_descriptor
1108     {
1109       array *data
1110       index offset;
1111       index dtype;
1112       struct descriptor_dimension dimension[N_DIM];
1113     }
1114
1115     struct descriptor_dimension
1116     {
1117       index stride;
1118       index lbound;
1119       index ubound;
1120     }
1121
1122    Translation code should use gfc_conv_descriptor_* rather than
1123    accessing the descriptor directly.  Any changes to the array
1124    descriptor type will require changes in gfc_conv_descriptor_* and
1125    gfc_build_array_initializer.
1126
1127    This is represented internally as a RECORD_TYPE. The index nodes
1128    are gfc_array_index_type and the data node is a pointer to the
1129    data.  See below for the handling of character types.
1130
1131    The dtype member is formatted as follows:
1132     rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1133     type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1134     size = dtype >> GFC_DTYPE_SIZE_SHIFT
1135
1136    I originally used nested ARRAY_TYPE nodes to represent arrays, but
1137    this generated poor code for assumed/deferred size arrays.  These
1138    require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1139    of the GENERIC grammar.  Also, there is no way to explicitly set
1140    the array stride, so all data must be packed(1).  I've tried to
1141    mark all the functions which would require modification with a GCC
1142    ARRAYS comment.
1143
1144    The data component points to the first element in the array.  The
1145    offset field is the position of the origin of the array (i.e. element
1146    (0, 0 ...)).  This may be outside the bounds of the array.
1147
1148    An element is accessed by
1149     data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1150    This gives good performance as the computation does not involve the
1151    bounds of the array.  For packed arrays, this is optimized further
1152    by substituting the known strides.
1153
1154    This system has one problem: all array bounds must be within 2^31
1155    elements of the origin (2^63 on 64-bit machines).  For example
1156     integer, dimension (80000:90000, 80000:90000, 2) :: array
1157    may not work properly on 32-bit machines because 80000*80000 >
1158    2^31, so the calculation for stride2 would overflow.  This may
1159    still work, but I haven't checked, and it relies on the overflow
1160    doing the right thing.
1161
1162    The way to fix this problem is to access elements as follows:
1163     data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1164    Obviously this is much slower.  I will make this a compile time
1165    option, something like -fsmall-array-offsets.  Mixing code compiled
1166    with and without this switch will work.
1167
1168    (1) This can be worked around by modifying the upper bound of the
1169    previous dimension.  This requires extra fields in the descriptor
1170    (both real_ubound and fake_ubound).  */
1171
1172
1173 /* Returns true if the array sym does not require a descriptor.  */
1174
1175 int
1176 gfc_is_nodesc_array (gfc_symbol * sym)
1177 {
1178   gcc_assert (sym->attr.dimension);
1179
1180   /* We only want local arrays.  */
1181   if (sym->attr.pointer || sym->attr.allocatable)
1182     return 0;
1183
1184   if (sym->attr.dummy)
1185     {
1186       if (sym->as->type != AS_ASSUMED_SHAPE)
1187         return 1;
1188       else
1189         return 0;
1190     }
1191
1192   if (sym->attr.result || sym->attr.function)
1193     return 0;
1194
1195   gcc_assert (sym->as->type == AS_EXPLICIT || sym->as->cp_was_assumed);
1196
1197   return 1;
1198 }
1199
1200
1201 /* Create an array descriptor type.  */
1202
1203 static tree
1204 gfc_build_array_type (tree type, gfc_array_spec * as,
1205                       enum gfc_array_kind akind, bool restricted)
1206 {
1207   tree lbound[GFC_MAX_DIMENSIONS];
1208   tree ubound[GFC_MAX_DIMENSIONS];
1209   int n;
1210
1211   for (n = 0; n < as->rank; n++)
1212     {
1213       /* Create expressions for the known bounds of the array.  */
1214       if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1215         lbound[n] = gfc_index_one_node;
1216       else
1217         lbound[n] = gfc_conv_array_bound (as->lower[n]);
1218       ubound[n] = gfc_conv_array_bound (as->upper[n]);
1219     }
1220
1221   if (as->type == AS_ASSUMED_SHAPE)
1222     akind = GFC_ARRAY_ASSUMED_SHAPE;
1223   return gfc_get_array_type_bounds (type, as->rank, as->corank, lbound,
1224                                     ubound, 0, akind, restricted);
1225 }
1226 \f
1227 /* Returns the struct descriptor_dimension type.  */
1228
1229 static tree
1230 gfc_get_desc_dim_type (void)
1231 {
1232   tree type;
1233   tree decl;
1234   tree fieldlist;
1235
1236   if (gfc_desc_dim_type)
1237     return gfc_desc_dim_type;
1238
1239   /* Build the type node.  */
1240   type = make_node (RECORD_TYPE);
1241
1242   TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1243   TYPE_PACKED (type) = 1;
1244
1245   /* Consists of the stride, lbound and ubound members.  */
1246   decl = build_decl (input_location,
1247                      FIELD_DECL,
1248                      get_identifier ("stride"), gfc_array_index_type);
1249   DECL_CONTEXT (decl) = type;
1250   TREE_NO_WARNING (decl) = 1;
1251   fieldlist = decl;
1252
1253   decl = build_decl (input_location,
1254                      FIELD_DECL,
1255                      get_identifier ("lbound"), gfc_array_index_type);
1256   DECL_CONTEXT (decl) = type;
1257   TREE_NO_WARNING (decl) = 1;
1258   fieldlist = chainon (fieldlist, decl);
1259
1260   decl = build_decl (input_location,
1261                      FIELD_DECL,
1262                      get_identifier ("ubound"), gfc_array_index_type);
1263   DECL_CONTEXT (decl) = type;
1264   TREE_NO_WARNING (decl) = 1;
1265   fieldlist = chainon (fieldlist, decl);
1266
1267   /* Finish off the type.  */
1268   TYPE_FIELDS (type) = fieldlist;
1269
1270   gfc_finish_type (type);
1271   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1272
1273   gfc_desc_dim_type = type;
1274   return type;
1275 }
1276
1277
1278 /* Return the DTYPE for an array.  This describes the type and type parameters
1279    of the array.  */
1280 /* TODO: Only call this when the value is actually used, and make all the
1281    unknown cases abort.  */
1282
1283 tree
1284 gfc_get_dtype (tree type)
1285 {
1286   tree size;
1287   int n;
1288   HOST_WIDE_INT i;
1289   tree tmp;
1290   tree dtype;
1291   tree etype;
1292   int rank;
1293
1294   gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1295
1296   if (GFC_TYPE_ARRAY_DTYPE (type))
1297     return GFC_TYPE_ARRAY_DTYPE (type);
1298
1299   rank = GFC_TYPE_ARRAY_RANK (type);
1300   etype = gfc_get_element_type (type);
1301
1302   switch (TREE_CODE (etype))
1303     {
1304     case INTEGER_TYPE:
1305       n = GFC_DTYPE_INTEGER;
1306       break;
1307
1308     case BOOLEAN_TYPE:
1309       n = GFC_DTYPE_LOGICAL;
1310       break;
1311
1312     case REAL_TYPE:
1313       n = GFC_DTYPE_REAL;
1314       break;
1315
1316     case COMPLEX_TYPE:
1317       n = GFC_DTYPE_COMPLEX;
1318       break;
1319
1320     /* We will never have arrays of arrays.  */
1321     case RECORD_TYPE:
1322       n = GFC_DTYPE_DERIVED;
1323       break;
1324
1325     case ARRAY_TYPE:
1326       n = GFC_DTYPE_CHARACTER;
1327       break;
1328
1329     default:
1330       /* TODO: Don't do dtype for temporary descriptorless arrays.  */
1331       /* We can strange array types for temporary arrays.  */
1332       return gfc_index_zero_node;
1333     }
1334
1335   gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1336   size = TYPE_SIZE_UNIT (etype);
1337
1338   i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1339   if (size && INTEGER_CST_P (size))
1340     {
1341       if (tree_int_cst_lt (gfc_max_array_element_size, size))
1342         internal_error ("Array element size too big");
1343
1344       i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1345     }
1346   dtype = build_int_cst (gfc_array_index_type, i);
1347
1348   if (size && !INTEGER_CST_P (size))
1349     {
1350       tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1351       tmp  = fold_build2 (LSHIFT_EXPR, gfc_array_index_type,
1352                           fold_convert (gfc_array_index_type, size), tmp);
1353       dtype = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype);
1354     }
1355   /* If we don't know the size we leave it as zero.  This should never happen
1356      for anything that is actually used.  */
1357   /* TODO: Check this is actually true, particularly when repacking
1358      assumed size parameters.  */
1359
1360   GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1361   return dtype;
1362 }
1363
1364
1365 /* Build an array type for use without a descriptor, packed according
1366    to the value of PACKED.  */
1367
1368 tree
1369 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1370                            bool restricted)
1371 {
1372   tree range;
1373   tree type;
1374   tree tmp;
1375   int n;
1376   int known_stride;
1377   int known_offset;
1378   mpz_t offset;
1379   mpz_t stride;
1380   mpz_t delta;
1381   gfc_expr *expr;
1382
1383   mpz_init_set_ui (offset, 0);
1384   mpz_init_set_ui (stride, 1);
1385   mpz_init (delta);
1386
1387   /* We don't use build_array_type because this does not include include
1388      lang-specific information (i.e. the bounds of the array) when checking
1389      for duplicates.  */
1390   type = make_node (ARRAY_TYPE);
1391
1392   GFC_ARRAY_TYPE_P (type) = 1;
1393   TYPE_LANG_SPECIFIC (type)
1394       = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
1395
1396   known_stride = (packed != PACKED_NO);
1397   known_offset = 1;
1398   for (n = 0; n < as->rank; n++)
1399     {
1400       /* Fill in the stride and bound components of the type.  */
1401       if (known_stride)
1402         tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1403       else
1404         tmp = NULL_TREE;
1405       GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1406
1407       expr = as->lower[n];
1408       if (expr->expr_type == EXPR_CONSTANT)
1409         {
1410           tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1411                                       gfc_index_integer_kind);
1412         }
1413       else
1414         {
1415           known_stride = 0;
1416           tmp = NULL_TREE;
1417         }
1418       GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1419
1420       if (known_stride)
1421         {
1422           /* Calculate the offset.  */
1423           mpz_mul (delta, stride, as->lower[n]->value.integer);
1424           mpz_sub (offset, offset, delta);
1425         }
1426       else
1427         known_offset = 0;
1428
1429       expr = as->upper[n];
1430       if (expr && expr->expr_type == EXPR_CONSTANT)
1431         {
1432           tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1433                                   gfc_index_integer_kind);
1434         }
1435       else
1436         {
1437           tmp = NULL_TREE;
1438           known_stride = 0;
1439         }
1440       GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1441
1442       if (known_stride)
1443         {
1444           /* Calculate the stride.  */
1445           mpz_sub (delta, as->upper[n]->value.integer,
1446                    as->lower[n]->value.integer);
1447           mpz_add_ui (delta, delta, 1);
1448           mpz_mul (stride, stride, delta);
1449         }
1450
1451       /* Only the first stride is known for partial packed arrays.  */
1452       if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1453         known_stride = 0;
1454     }
1455
1456   if (known_offset)
1457     {
1458       GFC_TYPE_ARRAY_OFFSET (type) =
1459         gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1460     }
1461   else
1462     GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1463
1464   if (known_stride)
1465     {
1466       GFC_TYPE_ARRAY_SIZE (type) =
1467         gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1468     }
1469   else
1470     GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1471
1472   GFC_TYPE_ARRAY_RANK (type) = as->rank;
1473   GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1474   range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1475                             NULL_TREE);
1476   /* TODO: use main type if it is unbounded.  */
1477   GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1478     build_pointer_type (build_array_type (etype, range));
1479   if (restricted)
1480     GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1481       build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1482                             TYPE_QUAL_RESTRICT);
1483
1484   if (known_stride)
1485     {
1486       mpz_sub_ui (stride, stride, 1);
1487       range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1488     }
1489   else
1490     range = NULL_TREE;
1491
1492   range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1493   TYPE_DOMAIN (type) = range;
1494
1495   build_pointer_type (etype);
1496   TREE_TYPE (type) = etype;
1497
1498   layout_type (type);
1499
1500   mpz_clear (offset);
1501   mpz_clear (stride);
1502   mpz_clear (delta);
1503
1504   /* Represent packed arrays as multi-dimensional if they have rank >
1505      1 and with proper bounds, instead of flat arrays.  This makes for
1506      better debug info.  */
1507   if (known_offset)
1508     {
1509       tree gtype = etype, rtype, type_decl;
1510
1511       for (n = as->rank - 1; n >= 0; n--)
1512         {
1513           rtype = build_range_type (gfc_array_index_type,
1514                                     GFC_TYPE_ARRAY_LBOUND (type, n),
1515                                     GFC_TYPE_ARRAY_UBOUND (type, n));
1516           gtype = build_array_type (gtype, rtype);
1517         }
1518       TYPE_NAME (type) = type_decl = build_decl (input_location,
1519                                                  TYPE_DECL, NULL, gtype);
1520       DECL_ORIGINAL_TYPE (type_decl) = gtype;
1521     }
1522
1523   if (packed != PACKED_STATIC || !known_stride)
1524     {
1525       /* For dummy arrays and automatic (heap allocated) arrays we
1526          want a pointer to the array.  */
1527       type = build_pointer_type (type);
1528       if (restricted)
1529         type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1530       GFC_ARRAY_TYPE_P (type) = 1;
1531       TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1532     }
1533   return type;
1534 }
1535
1536 /* Return or create the base type for an array descriptor.  */
1537
1538 static tree
1539 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1540 {
1541   tree fat_type, fieldlist, decl, arraytype;
1542   char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1543   int idx = 2 * (codimen + dimen - 1) + restricted;
1544
1545   gcc_assert (dimen >= 1 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1546   if (gfc_array_descriptor_base[idx])
1547     return gfc_array_descriptor_base[idx];
1548
1549   /* Build the type node.  */
1550   fat_type = make_node (RECORD_TYPE);
1551
1552   sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1553   TYPE_NAME (fat_type) = get_identifier (name);
1554
1555   /* Add the data member as the first element of the descriptor.  */
1556   decl = build_decl (input_location,
1557                      FIELD_DECL, get_identifier ("data"),
1558                      restricted ? prvoid_type_node : ptr_type_node);
1559
1560   DECL_CONTEXT (decl) = fat_type;
1561   fieldlist = decl;
1562
1563   /* Add the base component.  */
1564   decl = build_decl (input_location,
1565                      FIELD_DECL, get_identifier ("offset"),
1566                      gfc_array_index_type);
1567   DECL_CONTEXT (decl) = fat_type;
1568   TREE_NO_WARNING (decl) = 1;
1569   fieldlist = chainon (fieldlist, decl);
1570
1571   /* Add the dtype component.  */
1572   decl = build_decl (input_location,
1573                      FIELD_DECL, get_identifier ("dtype"),
1574                      gfc_array_index_type);
1575   DECL_CONTEXT (decl) = fat_type;
1576   TREE_NO_WARNING (decl) = 1;
1577   fieldlist = chainon (fieldlist, decl);
1578
1579   /* Build the array type for the stride and bound components.  */
1580   arraytype =
1581     build_array_type (gfc_get_desc_dim_type (),
1582                       build_range_type (gfc_array_index_type,
1583                                         gfc_index_zero_node,
1584                                         gfc_rank_cst[codimen + dimen - 1]));
1585
1586   decl = build_decl (input_location,
1587                      FIELD_DECL, get_identifier ("dim"), arraytype);
1588   DECL_CONTEXT (decl) = fat_type;
1589   TREE_NO_WARNING (decl) = 1;
1590   fieldlist = chainon (fieldlist, decl);
1591
1592   /* Finish off the type.  */
1593   TYPE_FIELDS (fat_type) = fieldlist;
1594
1595   gfc_finish_type (fat_type);
1596   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1597
1598   gfc_array_descriptor_base[idx] = fat_type;
1599   return fat_type;
1600 }
1601
1602 /* Build an array (descriptor) type with given bounds.  */
1603
1604 tree
1605 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1606                            tree * ubound, int packed,
1607                            enum gfc_array_kind akind, bool restricted)
1608 {
1609   char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1610   tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1611   const char *type_name;
1612   int n;
1613
1614   base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1615   fat_type = build_distinct_type_copy (base_type);
1616   /* Make sure that nontarget and target array type have the same canonical
1617      type (and same stub decl for debug info).  */
1618   base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1619   TYPE_CANONICAL (fat_type) = base_type;
1620   TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1621
1622   tmp = TYPE_NAME (etype);
1623   if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1624     tmp = DECL_NAME (tmp);
1625   if (tmp)
1626     type_name = IDENTIFIER_POINTER (tmp);
1627   else
1628     type_name = "unknown";
1629   sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1630            GFC_MAX_SYMBOL_LEN, type_name);
1631   TYPE_NAME (fat_type) = get_identifier (name);
1632
1633   GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1634   TYPE_LANG_SPECIFIC (fat_type)
1635     = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
1636
1637   GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1638   GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1639   GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1640
1641   /* Build an array descriptor record type.  */
1642   if (packed != 0)
1643     stride = gfc_index_one_node;
1644   else
1645     stride = NULL_TREE;
1646   for (n = 0; n < dimen; n++)
1647     {
1648       GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1649
1650       if (lbound)
1651         lower = lbound[n];
1652       else
1653         lower = NULL_TREE;
1654
1655       if (lower != NULL_TREE)
1656         {
1657           if (INTEGER_CST_P (lower))
1658             GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1659           else
1660             lower = NULL_TREE;
1661         }
1662
1663       upper = ubound[n];
1664       if (upper != NULL_TREE)
1665         {
1666           if (INTEGER_CST_P (upper))
1667             GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1668           else
1669             upper = NULL_TREE;
1670         }
1671
1672       if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1673         {
1674           tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
1675           tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1676                              gfc_index_one_node);
1677           stride =
1678             fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, stride);
1679           /* Check the folding worked.  */
1680           gcc_assert (INTEGER_CST_P (stride));
1681         }
1682       else
1683         stride = NULL_TREE;
1684     }
1685   GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1686
1687   /* TODO: known offsets for descriptors.  */
1688   GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1689
1690   /* We define data as an array with the correct size if possible.
1691      Much better than doing pointer arithmetic.  */
1692   if (stride)
1693     rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1694                               int_const_binop (MINUS_EXPR, stride,
1695                                                integer_one_node, 0));
1696   else
1697     rtype = gfc_array_range_type;
1698   arraytype = build_array_type (etype, rtype);
1699   arraytype = build_pointer_type (arraytype);
1700   if (restricted)
1701     arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1702   GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1703
1704   /* This will generate the base declarations we need to emit debug
1705      information for this type.  FIXME: there must be a better way to
1706      avoid divergence between compilations with and without debug
1707      information.  */
1708   {
1709     struct array_descr_info info;
1710     gfc_get_array_descr_info (fat_type, &info);
1711     gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1712   }
1713
1714   return fat_type;
1715 }
1716 \f
1717 /* Build a pointer type. This function is called from gfc_sym_type().  */
1718
1719 static tree
1720 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1721 {
1722   /* Array pointer types aren't actually pointers.  */
1723   if (sym->attr.dimension)
1724     return type;
1725   else
1726     return build_pointer_type (type);
1727 }
1728 \f
1729 /* Return the type for a symbol.  Special handling is required for character
1730    types to get the correct level of indirection.
1731    For functions return the return type.
1732    For subroutines return void_type_node.
1733    Calling this multiple times for the same symbol should be avoided,
1734    especially for character and array types.  */
1735
1736 tree
1737 gfc_sym_type (gfc_symbol * sym)
1738 {
1739   tree type;
1740   int byref;
1741   bool restricted;
1742
1743   /* Procedure Pointers inside COMMON blocks.  */
1744   if (sym->attr.proc_pointer && sym->attr.in_common)
1745     {
1746       /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type.  */
1747       sym->attr.proc_pointer = 0;
1748       type = build_pointer_type (gfc_get_function_type (sym));
1749       sym->attr.proc_pointer = 1;
1750       return type;
1751     }
1752
1753   if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1754     return void_type_node;
1755
1756   /* In the case of a function the fake result variable may have a
1757      type different from the function type, so don't return early in
1758      that case.  */
1759   if (sym->backend_decl && !sym->attr.function)
1760     return TREE_TYPE (sym->backend_decl);
1761
1762   if (sym->ts.type == BT_CHARACTER
1763       && ((sym->attr.function && sym->attr.is_bind_c)
1764           || (sym->attr.result
1765               && sym->ns->proc_name
1766               && sym->ns->proc_name->attr.is_bind_c)))
1767     type = gfc_character1_type_node;
1768   else
1769     type = gfc_typenode_for_spec (&sym->ts);
1770
1771   if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
1772     byref = 1;
1773   else
1774     byref = 0;
1775
1776   restricted = !sym->attr.target && !sym->attr.pointer
1777                && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
1778   if (sym->attr.dimension)
1779     {
1780       if (gfc_is_nodesc_array (sym))
1781         {
1782           /* If this is a character argument of unknown length, just use the
1783              base type.  */
1784           if (sym->ts.type != BT_CHARACTER
1785               || !(sym->attr.dummy || sym->attr.function)
1786               || sym->ts.u.cl->backend_decl)
1787             {
1788               type = gfc_get_nodesc_array_type (type, sym->as,
1789                                                 byref ? PACKED_FULL
1790                                                       : PACKED_STATIC,
1791                                                 restricted);
1792               byref = 0;
1793             }
1794
1795           if (sym->attr.cray_pointee)
1796             GFC_POINTER_TYPE_P (type) = 1;
1797         }
1798       else
1799         {
1800           enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
1801           if (sym->attr.pointer)
1802             akind = GFC_ARRAY_POINTER;
1803           else if (sym->attr.allocatable)
1804             akind = GFC_ARRAY_ALLOCATABLE;
1805           type = gfc_build_array_type (type, sym->as, akind, restricted);
1806         }
1807     }
1808   else
1809     {
1810       if (sym->attr.allocatable || sym->attr.pointer)
1811         type = gfc_build_pointer_type (sym, type);
1812       if (sym->attr.pointer || sym->attr.cray_pointee)
1813         GFC_POINTER_TYPE_P (type) = 1;
1814     }
1815
1816   /* We currently pass all parameters by reference.
1817      See f95_get_function_decl.  For dummy function parameters return the
1818      function type.  */
1819   if (byref)
1820     {
1821       /* We must use pointer types for potentially absent variables.  The
1822          optimizers assume a reference type argument is never NULL.  */
1823       if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1824         type = build_pointer_type (type);
1825       else
1826         {
1827           type = build_reference_type (type);
1828           if (restricted)
1829             type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1830         }
1831     }
1832
1833   return (type);
1834 }
1835 \f
1836 /* Layout and output debug info for a record type.  */
1837
1838 void
1839 gfc_finish_type (tree type)
1840 {
1841   tree decl;
1842
1843   decl = build_decl (input_location,
1844                      TYPE_DECL, NULL_TREE, type);
1845   TYPE_STUB_DECL (type) = decl;
1846   layout_type (type);
1847   rest_of_type_compilation (type, 1);
1848   rest_of_decl_compilation (decl, 1, 0);
1849 }
1850 \f
1851 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1852    or RECORD_TYPE pointed to by STYPE.  The new field is chained
1853    to the fieldlist pointed to by FIELDLIST.
1854
1855    Returns a pointer to the new field.  */
1856
1857 tree
1858 gfc_add_field_to_struct (tree *fieldlist, tree context,
1859                          tree name, tree type)
1860 {
1861   tree decl;
1862
1863   decl = build_decl (input_location,
1864                      FIELD_DECL, name, type);
1865
1866   DECL_CONTEXT (decl) = context;
1867   DECL_INITIAL (decl) = 0;
1868   DECL_ALIGN (decl) = 0;
1869   DECL_USER_ALIGN (decl) = 0;
1870   TREE_CHAIN (decl) = NULL_TREE;
1871   *fieldlist = chainon (*fieldlist, decl);
1872
1873   return decl;
1874 }
1875
1876
1877 /* Copy the backend_decl and component backend_decls if
1878    the two derived type symbols are "equal", as described
1879    in 4.4.2 and resolved by gfc_compare_derived_types.  */
1880
1881 static int
1882 copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
1883                        bool from_gsym)
1884 {
1885   gfc_component *to_cm;
1886   gfc_component *from_cm;
1887
1888   if (from->backend_decl == NULL
1889         || !gfc_compare_derived_types (from, to))
1890     return 0;
1891
1892   to->backend_decl = from->backend_decl;
1893
1894   to_cm = to->components;
1895   from_cm = from->components;
1896
1897   /* Copy the component declarations.  If a component is itself
1898      a derived type, we need a copy of its component declarations.
1899      This is done by recursing into gfc_get_derived_type and
1900      ensures that the component's component declarations have
1901      been built.  If it is a character, we need the character 
1902      length, as well.  */
1903   for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
1904     {
1905       to_cm->backend_decl = from_cm->backend_decl;
1906       if ((!from_cm->attr.pointer || from_gsym)
1907               && from_cm->ts.type == BT_DERIVED)
1908         gfc_get_derived_type (to_cm->ts.u.derived);
1909
1910       else if (from_cm->ts.type == BT_CHARACTER)
1911         to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
1912     }
1913
1914   return 1;
1915 }
1916
1917
1918 /* Build a tree node for a procedure pointer component.  */
1919
1920 tree
1921 gfc_get_ppc_type (gfc_component* c)
1922 {
1923   tree t;
1924
1925   /* Explicit interface.  */
1926   if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
1927     return build_pointer_type (gfc_get_function_type (c->ts.interface));
1928
1929   /* Implicit interface (only return value may be known).  */
1930   if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
1931     t = gfc_typenode_for_spec (&c->ts);
1932   else
1933     t = void_type_node;
1934
1935   return build_pointer_type (build_function_type_list (t, NULL_TREE));
1936 }
1937
1938
1939 /* Build a tree node for a derived type.  If there are equal
1940    derived types, with different local names, these are built
1941    at the same time.  If an equal derived type has been built
1942    in a parent namespace, this is used.  */
1943
1944 tree
1945 gfc_get_derived_type (gfc_symbol * derived)
1946 {
1947   tree typenode = NULL, field = NULL, field_type = NULL, fieldlist = NULL;
1948   tree canonical = NULL_TREE;
1949   bool got_canonical = false;
1950   gfc_component *c;
1951   gfc_dt_list *dt;
1952   gfc_namespace *ns;
1953   gfc_gsymbol *gsym;
1954
1955   gcc_assert (derived && derived->attr.flavor == FL_DERIVED);
1956
1957   /* See if it's one of the iso_c_binding derived types.  */
1958   if (derived->attr.is_iso_c == 1)
1959     {
1960       if (derived->backend_decl)
1961         return derived->backend_decl;
1962
1963       if (derived->intmod_sym_id == ISOCBINDING_PTR)
1964         derived->backend_decl = ptr_type_node;
1965       else
1966         derived->backend_decl = pfunc_type_node;
1967
1968       /* Create a backend_decl for the __c_ptr_c_address field.  */
1969       derived->components->backend_decl =
1970         gfc_add_field_to_struct (&(derived->backend_decl->type.values),
1971                                  derived->backend_decl,
1972                                  get_identifier (derived->components->name),
1973                                  gfc_typenode_for_spec (
1974                                    &(derived->components->ts)));
1975
1976       derived->ts.kind = gfc_index_integer_kind;
1977       derived->ts.type = BT_INTEGER;
1978       /* Set the f90_type to BT_VOID as a way to recognize something of type
1979          BT_INTEGER that needs to fit a void * for the purpose of the
1980          iso_c_binding derived types.  */
1981       derived->ts.f90_type = BT_VOID;
1982       
1983       return derived->backend_decl;
1984     }
1985
1986 /* If use associated, use the module type for this one.  */
1987   if (gfc_option.flag_whole_file
1988         && derived->backend_decl == NULL
1989         && derived->attr.use_assoc
1990         && derived->module)
1991     {
1992       gsym =  gfc_find_gsymbol (gfc_gsym_root, derived->module);
1993       if (gsym && gsym->ns && gsym->type == GSYM_MODULE)
1994         {
1995           gfc_symbol *s;
1996           s = NULL;
1997           gfc_find_symbol (derived->name, gsym->ns, 0, &s);
1998           if (s && s->backend_decl)
1999             {
2000               copy_dt_decls_ifequal (s, derived, true);
2001               goto copy_derived_types;
2002             }
2003         }
2004     }
2005
2006   /* If a whole file compilation, the derived types from an earlier
2007      namespace can be used as the the canonical type.  */
2008   if (gfc_option.flag_whole_file
2009         && derived->backend_decl == NULL
2010         && !derived->attr.use_assoc
2011         && gfc_global_ns_list)
2012     {
2013       for (ns = gfc_global_ns_list;
2014            ns->translated && !got_canonical;
2015            ns = ns->sibling)
2016         {
2017           dt = ns->derived_types;
2018           for (; dt && !canonical; dt = dt->next)
2019             {
2020               copy_dt_decls_ifequal (dt->derived, derived, true);
2021               if (derived->backend_decl)
2022                 got_canonical = true;
2023             }
2024         }
2025     }
2026
2027   /* Store up the canonical type to be added to this one.  */
2028   if (got_canonical)
2029     {
2030       if (TYPE_CANONICAL (derived->backend_decl))
2031         canonical = TYPE_CANONICAL (derived->backend_decl);
2032       else
2033         canonical = derived->backend_decl;
2034
2035       derived->backend_decl = NULL_TREE;
2036     }
2037
2038   /* derived->backend_decl != 0 means we saw it before, but its
2039      components' backend_decl may have not been built.  */
2040   if (derived->backend_decl)
2041     {
2042       /* Its components' backend_decl have been built or we are
2043          seeing recursion through the formal arglist of a procedure
2044          pointer component.  */
2045       if (TYPE_FIELDS (derived->backend_decl)
2046             || derived->attr.proc_pointer_comp)
2047         return derived->backend_decl;
2048       else
2049         typenode = derived->backend_decl;
2050     }
2051   else
2052     {
2053       /* We see this derived type first time, so build the type node.  */
2054       typenode = make_node (RECORD_TYPE);
2055       TYPE_NAME (typenode) = get_identifier (derived->name);
2056       TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
2057       derived->backend_decl = typenode;
2058     }
2059
2060   /* Go through the derived type components, building them as
2061      necessary. The reason for doing this now is that it is
2062      possible to recurse back to this derived type through a
2063      pointer component (PR24092). If this happens, the fields
2064      will be built and so we can return the type.  */
2065   for (c = derived->components; c; c = c->next)
2066     {
2067       if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2068         continue;
2069
2070       if ((!c->attr.pointer && !c->attr.proc_pointer)
2071           || c->ts.u.derived->backend_decl == NULL)
2072         c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
2073
2074       if (c->ts.u.derived && c->ts.u.derived->attr.is_iso_c)
2075         {
2076           /* Need to copy the modified ts from the derived type.  The
2077              typespec was modified because C_PTR/C_FUNPTR are translated
2078              into (void *) from derived types.  */
2079           c->ts.type = c->ts.u.derived->ts.type;
2080           c->ts.kind = c->ts.u.derived->ts.kind;
2081           c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2082           if (c->initializer)
2083             {
2084               c->initializer->ts.type = c->ts.type;
2085               c->initializer->ts.kind = c->ts.kind;
2086               c->initializer->ts.f90_type = c->ts.f90_type;
2087               c->initializer->expr_type = EXPR_NULL;
2088             }
2089         }
2090     }
2091
2092   if (TYPE_FIELDS (derived->backend_decl))
2093     return derived->backend_decl;
2094
2095   /* Build the type member list. Install the newly created RECORD_TYPE
2096      node as DECL_CONTEXT of each FIELD_DECL.  */
2097   fieldlist = NULL_TREE;
2098   for (c = derived->components; c; c = c->next)
2099     {
2100       if (c->attr.proc_pointer)
2101         field_type = gfc_get_ppc_type (c);
2102       else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2103         field_type = c->ts.u.derived->backend_decl;
2104       else
2105         {
2106           if (c->ts.type == BT_CHARACTER)
2107             {
2108               /* Evaluate the string length.  */
2109               gfc_conv_const_charlen (c->ts.u.cl);
2110               gcc_assert (c->ts.u.cl->backend_decl);
2111             }
2112
2113           field_type = gfc_typenode_for_spec (&c->ts);
2114         }
2115
2116       /* This returns an array descriptor type.  Initialization may be
2117          required.  */
2118       if (c->attr.dimension && !c->attr.proc_pointer)
2119         {
2120           if (c->attr.pointer || c->attr.allocatable)
2121             {
2122               enum gfc_array_kind akind;
2123               if (c->attr.pointer)
2124                 akind = GFC_ARRAY_POINTER;
2125               else
2126                 akind = GFC_ARRAY_ALLOCATABLE;
2127               /* Pointers to arrays aren't actually pointer types.  The
2128                  descriptors are separate, but the data is common.  */
2129               field_type = gfc_build_array_type (field_type, c->as, akind,
2130                                                  !c->attr.target
2131                                                  && !c->attr.pointer);
2132             }
2133           else
2134             field_type = gfc_get_nodesc_array_type (field_type, c->as,
2135                                                     PACKED_STATIC,
2136                                                     !c->attr.target);
2137         }
2138       else if ((c->attr.pointer || c->attr.allocatable)
2139                && !c->attr.proc_pointer)
2140         field_type = build_pointer_type (field_type);
2141
2142       field = gfc_add_field_to_struct (&fieldlist, typenode,
2143                                        get_identifier (c->name), field_type);
2144       if (c->loc.lb)
2145         gfc_set_decl_location (field, &c->loc);
2146       else if (derived->declared_at.lb)
2147         gfc_set_decl_location (field, &derived->declared_at);
2148
2149       DECL_PACKED (field) |= TYPE_PACKED (typenode);
2150
2151       gcc_assert (field);
2152       if (!c->backend_decl)
2153         c->backend_decl = field;
2154     }
2155
2156   /* Now we have the final fieldlist.  Record it, then lay out the
2157      derived type, including the fields.  */
2158   TYPE_FIELDS (typenode) = fieldlist;
2159   if (canonical)
2160     TYPE_CANONICAL (typenode) = canonical;
2161
2162   gfc_finish_type (typenode);
2163   gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2164   if (derived->module && derived->ns->proc_name
2165       && derived->ns->proc_name->attr.flavor == FL_MODULE)
2166     {
2167       if (derived->ns->proc_name->backend_decl
2168           && TREE_CODE (derived->ns->proc_name->backend_decl)
2169              == NAMESPACE_DECL)
2170         {
2171           TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2172           DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2173             = derived->ns->proc_name->backend_decl;
2174         }
2175     }
2176
2177   derived->backend_decl = typenode;
2178
2179 copy_derived_types:
2180
2181   for (dt = gfc_derived_types; dt; dt = dt->next)
2182     copy_dt_decls_ifequal (derived, dt->derived, false);
2183
2184   return derived->backend_decl;
2185 }
2186
2187
2188 int
2189 gfc_return_by_reference (gfc_symbol * sym)
2190 {
2191   if (!sym->attr.function)
2192     return 0;
2193
2194   if (sym->attr.dimension)
2195     return 1;
2196
2197   if (sym->ts.type == BT_CHARACTER
2198       && !sym->attr.is_bind_c
2199       && (!sym->attr.result
2200           || !sym->ns->proc_name
2201           || !sym->ns->proc_name->attr.is_bind_c))
2202     return 1;
2203
2204   /* Possibly return complex numbers by reference for g77 compatibility.
2205      We don't do this for calls to intrinsics (as the library uses the
2206      -fno-f2c calling convention), nor for calls to functions which always
2207      require an explicit interface, as no compatibility problems can
2208      arise there.  */
2209   if (gfc_option.flag_f2c
2210       && sym->ts.type == BT_COMPLEX
2211       && !sym->attr.intrinsic && !sym->attr.always_explicit)
2212     return 1;
2213
2214   return 0;
2215 }
2216 \f
2217 static tree
2218 gfc_get_mixed_entry_union (gfc_namespace *ns)
2219 {
2220   tree type;
2221   tree decl;
2222   tree fieldlist;
2223   char name[GFC_MAX_SYMBOL_LEN + 1];
2224   gfc_entry_list *el, *el2;
2225
2226   gcc_assert (ns->proc_name->attr.mixed_entry_master);
2227   gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2228
2229   snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2230
2231   /* Build the type node.  */
2232   type = make_node (UNION_TYPE);
2233
2234   TYPE_NAME (type) = get_identifier (name);
2235   fieldlist = NULL;
2236
2237   for (el = ns->entries; el; el = el->next)
2238     {
2239       /* Search for duplicates.  */
2240       for (el2 = ns->entries; el2 != el; el2 = el2->next)
2241         if (el2->sym->result == el->sym->result)
2242           break;
2243
2244       if (el == el2)
2245         {
2246           decl = build_decl (input_location,
2247                              FIELD_DECL,
2248                              get_identifier (el->sym->result->name),
2249                              gfc_sym_type (el->sym->result));
2250           DECL_CONTEXT (decl) = type;
2251           fieldlist = chainon (fieldlist, decl);
2252         }
2253     }
2254
2255   /* Finish off the type.  */
2256   TYPE_FIELDS (type) = fieldlist;
2257
2258   gfc_finish_type (type);
2259   TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2260   return type;
2261 }
2262 \f
2263 tree
2264 gfc_get_function_type (gfc_symbol * sym)
2265 {
2266   tree type;
2267   tree typelist;
2268   gfc_formal_arglist *f;
2269   gfc_symbol *arg;
2270   int nstr;
2271   int alternate_return;
2272
2273   /* Make sure this symbol is a function, a subroutine or the main
2274      program.  */
2275   gcc_assert (sym->attr.flavor == FL_PROCEDURE
2276               || sym->attr.flavor == FL_PROGRAM);
2277
2278   if (sym->backend_decl)
2279     return TREE_TYPE (sym->backend_decl);
2280
2281   nstr = 0;
2282   alternate_return = 0;
2283   typelist = NULL_TREE;
2284
2285   if (sym->attr.entry_master)
2286     {
2287       /* Additional parameter for selecting an entry point.  */
2288       typelist = gfc_chainon_list (typelist, gfc_array_index_type);
2289     }
2290
2291   if (sym->result)
2292     arg = sym->result;
2293   else
2294     arg = sym;
2295
2296   if (arg->ts.type == BT_CHARACTER)
2297     gfc_conv_const_charlen (arg->ts.u.cl);
2298
2299   /* Some functions we use an extra parameter for the return value.  */
2300   if (gfc_return_by_reference (sym))
2301     {
2302       type = gfc_sym_type (arg);
2303       if (arg->ts.type == BT_COMPLEX
2304           || arg->attr.dimension
2305           || arg->ts.type == BT_CHARACTER)
2306         type = build_reference_type (type);
2307
2308       typelist = gfc_chainon_list (typelist, type);
2309       if (arg->ts.type == BT_CHARACTER)
2310         typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
2311     }
2312
2313   /* Build the argument types for the function.  */
2314   for (f = sym->formal; f; f = f->next)
2315     {
2316       arg = f->sym;
2317       if (arg)
2318         {
2319           /* Evaluate constant character lengths here so that they can be
2320              included in the type.  */
2321           if (arg->ts.type == BT_CHARACTER)
2322             gfc_conv_const_charlen (arg->ts.u.cl);
2323
2324           if (arg->attr.flavor == FL_PROCEDURE)
2325             {
2326               type = gfc_get_function_type (arg);
2327               type = build_pointer_type (type);
2328             }
2329           else
2330             type = gfc_sym_type (arg);
2331
2332           /* Parameter Passing Convention
2333
2334              We currently pass all parameters by reference.
2335              Parameters with INTENT(IN) could be passed by value.
2336              The problem arises if a function is called via an implicit
2337              prototype. In this situation the INTENT is not known.
2338              For this reason all parameters to global functions must be
2339              passed by reference.  Passing by value would potentially
2340              generate bad code.  Worse there would be no way of telling that
2341              this code was bad, except that it would give incorrect results.
2342
2343              Contained procedures could pass by value as these are never
2344              used without an explicit interface, and cannot be passed as
2345              actual parameters for a dummy procedure.  */
2346           if (arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
2347             nstr++;
2348           typelist = gfc_chainon_list (typelist, type);
2349         }
2350       else
2351         {
2352           if (sym->attr.subroutine)
2353             alternate_return = 1;
2354         }
2355     }
2356
2357   /* Add hidden string length parameters.  */
2358   while (nstr--)
2359     typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
2360
2361   if (typelist)
2362     typelist = gfc_chainon_list (typelist, void_type_node);
2363
2364   if (alternate_return)
2365     type = integer_type_node;
2366   else if (!sym->attr.function || gfc_return_by_reference (sym))
2367     type = void_type_node;
2368   else if (sym->attr.mixed_entry_master)
2369     type = gfc_get_mixed_entry_union (sym->ns);
2370   else if (gfc_option.flag_f2c
2371            && sym->ts.type == BT_REAL
2372            && sym->ts.kind == gfc_default_real_kind
2373            && !sym->attr.always_explicit)
2374     {
2375       /* Special case: f2c calling conventions require that (scalar) 
2376          default REAL functions return the C type double instead.  f2c
2377          compatibility is only an issue with functions that don't
2378          require an explicit interface, as only these could be
2379          implemented in Fortran 77.  */
2380       sym->ts.kind = gfc_default_double_kind;
2381       type = gfc_typenode_for_spec (&sym->ts);
2382       sym->ts.kind = gfc_default_real_kind;
2383     }
2384   else if (sym->result && sym->result->attr.proc_pointer)
2385     /* Procedure pointer return values.  */
2386     {
2387       if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
2388         {
2389           /* Unset proc_pointer as gfc_get_function_type
2390              is called recursively.  */
2391           sym->result->attr.proc_pointer = 0;
2392           type = build_pointer_type (gfc_get_function_type (sym->result));
2393           sym->result->attr.proc_pointer = 1;
2394         }
2395       else
2396        type = gfc_sym_type (sym->result);
2397     }
2398   else
2399     type = gfc_sym_type (sym);
2400
2401   type = build_function_type (type, typelist);
2402
2403   return type;
2404 }
2405 \f
2406 /* Language hooks for middle-end access to type nodes.  */
2407
2408 /* Return an integer type with BITS bits of precision,
2409    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
2410
2411 tree
2412 gfc_type_for_size (unsigned bits, int unsignedp)
2413 {
2414   if (!unsignedp)
2415     {
2416       int i;
2417       for (i = 0; i <= MAX_INT_KINDS; ++i)
2418         {
2419           tree type = gfc_integer_types[i];
2420           if (type && bits == TYPE_PRECISION (type))
2421             return type;
2422         }
2423
2424       /* Handle TImode as a special case because it is used by some backends
2425          (e.g. ARM) even though it is not available for normal use.  */
2426 #if HOST_BITS_PER_WIDE_INT >= 64
2427       if (bits == TYPE_PRECISION (intTI_type_node))
2428         return intTI_type_node;
2429 #endif
2430     }
2431   else
2432     {
2433       if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
2434         return unsigned_intQI_type_node;
2435       if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
2436         return unsigned_intHI_type_node;
2437       if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
2438         return unsigned_intSI_type_node;
2439       if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
2440         return unsigned_intDI_type_node;
2441       if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
2442         return unsigned_intTI_type_node;
2443     }
2444
2445   return NULL_TREE;
2446 }
2447
2448 /* Return a data type that has machine mode MODE.  If the mode is an
2449    integer, then UNSIGNEDP selects between signed and unsigned types.  */
2450
2451 tree
2452 gfc_type_for_mode (enum machine_mode mode, int unsignedp)
2453 {
2454   int i;
2455   tree *base;
2456
2457   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2458     base = gfc_real_types;
2459   else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2460     base = gfc_complex_types;
2461   else if (SCALAR_INT_MODE_P (mode))
2462     return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
2463   else if (VECTOR_MODE_P (mode))
2464     {
2465       enum machine_mode inner_mode = GET_MODE_INNER (mode);
2466       tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
2467       if (inner_type != NULL_TREE)
2468         return build_vector_type_for_mode (inner_type, mode);
2469       return NULL_TREE;
2470     }
2471   else
2472     return NULL_TREE;
2473
2474   for (i = 0; i <= MAX_REAL_KINDS; ++i)
2475     {
2476       tree type = base[i];
2477       if (type && mode == TYPE_MODE (type))
2478         return type;
2479     }
2480
2481   return NULL_TREE;
2482 }
2483
2484 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
2485    in that case.  */
2486
2487 bool
2488 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
2489 {
2490   int rank, dim;
2491   bool indirect = false;
2492   tree etype, ptype, field, t, base_decl;
2493   tree data_off, dim_off, dim_size, elem_size;
2494   tree lower_suboff, upper_suboff, stride_suboff;
2495
2496   if (! GFC_DESCRIPTOR_TYPE_P (type))
2497     {
2498       if (! POINTER_TYPE_P (type))
2499         return false;
2500       type = TREE_TYPE (type);
2501       if (! GFC_DESCRIPTOR_TYPE_P (type))
2502         return false;
2503       indirect = true;
2504     }
2505
2506   rank = GFC_TYPE_ARRAY_RANK (type);
2507   if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
2508     return false;
2509
2510   etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2511   gcc_assert (POINTER_TYPE_P (etype));
2512   etype = TREE_TYPE (etype);
2513   gcc_assert (TREE_CODE (etype) == ARRAY_TYPE);
2514   etype = TREE_TYPE (etype);
2515   /* Can't handle variable sized elements yet.  */
2516   if (int_size_in_bytes (etype) <= 0)
2517     return false;
2518   /* Nor non-constant lower bounds in assumed shape arrays.  */
2519   if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE)
2520     {
2521       for (dim = 0; dim < rank; dim++)
2522         if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
2523             || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
2524           return false;
2525     }
2526
2527   memset (info, '\0', sizeof (*info));
2528   info->ndimensions = rank;
2529   info->element_type = etype;
2530   ptype = build_pointer_type (gfc_array_index_type);
2531   base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
2532   if (!base_decl)
2533     {
2534       base_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
2535                               indirect ? build_pointer_type (ptype) : ptype);
2536       GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
2537     }
2538   info->base_decl = base_decl;
2539   if (indirect)
2540     base_decl = build1 (INDIRECT_REF, ptype, base_decl);
2541
2542   if (GFC_TYPE_ARRAY_SPAN (type))
2543     elem_size = GFC_TYPE_ARRAY_SPAN (type);
2544   else
2545     elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
2546   field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
2547   data_off = byte_position (field);
2548   field = TREE_CHAIN (field);
2549   field = TREE_CHAIN (field);
2550   field = TREE_CHAIN (field);
2551   dim_off = byte_position (field);
2552   dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
2553   field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
2554   stride_suboff = byte_position (field);
2555   field = TREE_CHAIN (field);
2556   lower_suboff = byte_position (field);
2557   field = TREE_CHAIN (field);
2558   upper_suboff = byte_position (field);
2559
2560   t = base_decl;
2561   if (!integer_zerop (data_off))
2562     t = build2 (POINTER_PLUS_EXPR, ptype, t, data_off);
2563   t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
2564   info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
2565   if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
2566     info->allocated = build2 (NE_EXPR, boolean_type_node,
2567                               info->data_location, null_pointer_node);
2568   else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER)
2569     info->associated = build2 (NE_EXPR, boolean_type_node,
2570                                info->data_location, null_pointer_node);
2571
2572   for (dim = 0; dim < rank; dim++)
2573     {
2574       t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2575                   size_binop (PLUS_EXPR, dim_off, lower_suboff));
2576       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2577       info->dimen[dim].lower_bound = t;
2578       t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2579                   size_binop (PLUS_EXPR, dim_off, upper_suboff));
2580       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2581       info->dimen[dim].upper_bound = t;
2582       if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE)
2583         {
2584           /* Assumed shape arrays have known lower bounds.  */
2585           info->dimen[dim].upper_bound
2586             = build2 (MINUS_EXPR, gfc_array_index_type,
2587                       info->dimen[dim].upper_bound,
2588                       info->dimen[dim].lower_bound);
2589           info->dimen[dim].lower_bound
2590             = fold_convert (gfc_array_index_type,
2591                             GFC_TYPE_ARRAY_LBOUND (type, dim));
2592           info->dimen[dim].upper_bound
2593             = build2 (PLUS_EXPR, gfc_array_index_type,
2594                       info->dimen[dim].lower_bound,
2595                       info->dimen[dim].upper_bound);
2596         }
2597       t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2598                   size_binop (PLUS_EXPR, dim_off, stride_suboff));
2599       t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2600       t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
2601       info->dimen[dim].stride = t;
2602       dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
2603     }
2604
2605   return true;
2606 }
2607
2608 #include "gt-fortran-trans-types.h"