OSDN Git Service

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