OSDN Git Service

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