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