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