OSDN Git Service

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