]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/trans-types.c
re PR fortran/34640 (ICE when assigning item of a derived-component to a pointer)
[thirdparty/gcc.git] / gcc / fortran / trans-types.c
1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* trans-types.c -- gfortran backend types */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "tree.h"
29 #include "gfortran.h"
30 #include "trans.h"
31 #include "stringpool.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "langhooks.h" /* For iso-c-bindings.def. */
35 #include "toplev.h" /* For rest_of_decl_compilation. */
36 #include "trans-types.h"
37 #include "trans-const.h"
38 #include "trans-array.h"
39 #include "dwarf2out.h" /* For struct array_descr_info. */
40 #include "attribs.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 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 gfc_float128_type_node = NULL_TREE;
68 tree gfc_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+1)];
75 static GTY(()) tree gfc_array_descriptor_base_caf[2 * (GFC_MAX_DIMENSIONS+1)];
76
77 /* Arrays for all integral and real kinds. We'll fill this in at runtime
78 after the target has a chance to process command-line options. */
79
80 #define MAX_INT_KINDS 5
81 gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
82 gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
83 static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
84 static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
85
86 #define MAX_REAL_KINDS 5
87 gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
88 static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
89 static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
90
91 #define MAX_CHARACTER_KINDS 2
92 gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
93 static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
94 static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
95
96 static tree gfc_add_field_to_struct_1 (tree, tree, tree, tree **);
97
98 /* The integer kind to use for array indices. This will be set to the
99 proper value based on target information from the backend. */
100
101 int gfc_index_integer_kind;
102
103 /* The default kinds of the various types. */
104
105 int gfc_default_integer_kind;
106 int gfc_max_integer_kind;
107 int gfc_default_real_kind;
108 int gfc_default_double_kind;
109 int gfc_default_character_kind;
110 int gfc_default_logical_kind;
111 int gfc_default_complex_kind;
112 int gfc_c_int_kind;
113 int gfc_atomic_int_kind;
114 int gfc_atomic_logical_kind;
115
116 /* The kind size used for record offsets. If the target system supports
117 kind=8, this will be set to 8, otherwise it is set to 4. */
118 int gfc_intio_kind;
119
120 /* The integer kind used to store character lengths. */
121 int gfc_charlen_int_kind;
122
123 /* The size of the numeric storage unit and character storage unit. */
124 int gfc_numeric_storage_size;
125 int gfc_character_storage_size;
126
127
128 bool
129 gfc_check_any_c_kind (gfc_typespec *ts)
130 {
131 int i;
132
133 for (i = 0; i < ISOCBINDING_NUMBER; i++)
134 {
135 /* Check for any C interoperable kind for the given type/kind in ts.
136 This can be used after verify_c_interop to make sure that the
137 Fortran kind being used exists in at least some form for C. */
138 if (c_interop_kinds_table[i].f90_type == ts->type &&
139 c_interop_kinds_table[i].value == ts->kind)
140 return true;
141 }
142
143 return false;
144 }
145
146
147 static int
148 get_real_kind_from_node (tree type)
149 {
150 int i;
151
152 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
153 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
154 return gfc_real_kinds[i].kind;
155
156 return -4;
157 }
158
159 static int
160 get_int_kind_from_node (tree type)
161 {
162 int i;
163
164 if (!type)
165 return -2;
166
167 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
168 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
169 return gfc_integer_kinds[i].kind;
170
171 return -1;
172 }
173
174 /* Return a typenode for the "standard" C type with a given name. */
175 static tree
176 get_typenode_from_name (const char *name)
177 {
178 if (name == NULL || *name == '\0')
179 return NULL_TREE;
180
181 if (strcmp (name, "char") == 0)
182 return char_type_node;
183 if (strcmp (name, "unsigned char") == 0)
184 return unsigned_char_type_node;
185 if (strcmp (name, "signed char") == 0)
186 return signed_char_type_node;
187
188 if (strcmp (name, "short int") == 0)
189 return short_integer_type_node;
190 if (strcmp (name, "short unsigned int") == 0)
191 return short_unsigned_type_node;
192
193 if (strcmp (name, "int") == 0)
194 return integer_type_node;
195 if (strcmp (name, "unsigned int") == 0)
196 return unsigned_type_node;
197
198 if (strcmp (name, "long int") == 0)
199 return long_integer_type_node;
200 if (strcmp (name, "long unsigned int") == 0)
201 return long_unsigned_type_node;
202
203 if (strcmp (name, "long long int") == 0)
204 return long_long_integer_type_node;
205 if (strcmp (name, "long long unsigned int") == 0)
206 return long_long_unsigned_type_node;
207
208 gcc_unreachable ();
209 }
210
211 static int
212 get_int_kind_from_name (const char *name)
213 {
214 return get_int_kind_from_node (get_typenode_from_name (name));
215 }
216
217
218 /* Get the kind number corresponding to an integer of given size,
219 following the required return values for ISO_FORTRAN_ENV INT* constants:
220 -2 is returned if we support a kind of larger size, -1 otherwise. */
221 int
222 gfc_get_int_kind_from_width_isofortranenv (int size)
223 {
224 int i;
225
226 /* Look for a kind with matching storage size. */
227 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
228 if (gfc_integer_kinds[i].bit_size == size)
229 return gfc_integer_kinds[i].kind;
230
231 /* Look for a kind with larger storage size. */
232 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
233 if (gfc_integer_kinds[i].bit_size > size)
234 return -2;
235
236 return -1;
237 }
238
239
240 /* Get the kind number corresponding to a real of a given storage size.
241 If two real's have the same storage size, then choose the real with
242 the largest precision. If a kind type is unavailable and a real
243 exists with wider storage, then return -2; otherwise, return -1. */
244
245 int
246 gfc_get_real_kind_from_width_isofortranenv (int size)
247 {
248 int digits, i, kind;
249
250 size /= 8;
251
252 kind = -1;
253 digits = 0;
254
255 /* Look for a kind with matching storage size. */
256 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
257 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
258 {
259 if (gfc_real_kinds[i].digits > digits)
260 {
261 digits = gfc_real_kinds[i].digits;
262 kind = gfc_real_kinds[i].kind;
263 }
264 }
265
266 if (kind != -1)
267 return kind;
268
269 /* Look for a kind with larger storage size. */
270 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
271 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
272 kind = -2;
273
274 return kind;
275 }
276
277
278
279 static int
280 get_int_kind_from_width (int size)
281 {
282 int i;
283
284 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
285 if (gfc_integer_kinds[i].bit_size == size)
286 return gfc_integer_kinds[i].kind;
287
288 return -2;
289 }
290
291 static int
292 get_int_kind_from_minimal_width (int size)
293 {
294 int i;
295
296 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
297 if (gfc_integer_kinds[i].bit_size >= size)
298 return gfc_integer_kinds[i].kind;
299
300 return -2;
301 }
302
303
304 /* Generate the CInteropKind_t objects for the C interoperable
305 kinds. */
306
307 void
308 gfc_init_c_interop_kinds (void)
309 {
310 int i;
311
312 /* init all pointers in the list to NULL */
313 for (i = 0; i < ISOCBINDING_NUMBER; i++)
314 {
315 /* Initialize the name and value fields. */
316 c_interop_kinds_table[i].name[0] = '\0';
317 c_interop_kinds_table[i].value = -100;
318 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
319 }
320
321 #define NAMED_INTCST(a,b,c,d) \
322 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
323 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
324 c_interop_kinds_table[a].value = c;
325 #define NAMED_REALCST(a,b,c,d) \
326 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
327 c_interop_kinds_table[a].f90_type = BT_REAL; \
328 c_interop_kinds_table[a].value = c;
329 #define NAMED_CMPXCST(a,b,c,d) \
330 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
331 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
332 c_interop_kinds_table[a].value = c;
333 #define NAMED_LOGCST(a,b,c) \
334 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
335 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
336 c_interop_kinds_table[a].value = c;
337 #define NAMED_CHARKNDCST(a,b,c) \
338 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
339 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
340 c_interop_kinds_table[a].value = c;
341 #define NAMED_CHARCST(a,b,c) \
342 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
343 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
344 c_interop_kinds_table[a].value = c;
345 #define DERIVED_TYPE(a,b,c) \
346 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
347 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
348 c_interop_kinds_table[a].value = c;
349 #define NAMED_FUNCTION(a,b,c,d) \
350 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
351 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
352 c_interop_kinds_table[a].value = c;
353 #define NAMED_SUBROUTINE(a,b,c,d) \
354 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
355 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
356 c_interop_kinds_table[a].value = c;
357 #include "iso-c-binding.def"
358 }
359
360
361 /* Query the target to determine which machine modes are available for
362 computation. Choose KIND numbers for them. */
363
364 void
365 gfc_init_kinds (void)
366 {
367 opt_scalar_int_mode int_mode_iter;
368 opt_scalar_float_mode float_mode_iter;
369 int i_index, r_index, kind;
370 bool saw_i4 = false, saw_i8 = false;
371 bool saw_r4 = false, saw_r8 = false, saw_r10 = false, saw_r16 = false;
372
373 i_index = 0;
374 FOR_EACH_MODE_IN_CLASS (int_mode_iter, MODE_INT)
375 {
376 scalar_int_mode mode = int_mode_iter.require ();
377 int kind, bitsize;
378
379 if (!targetm.scalar_mode_supported_p (mode))
380 continue;
381
382 /* The middle end doesn't support constants larger than 2*HWI.
383 Perhaps the target hook shouldn't have accepted these either,
384 but just to be safe... */
385 bitsize = GET_MODE_BITSIZE (mode);
386 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
387 continue;
388
389 gcc_assert (i_index != MAX_INT_KINDS);
390
391 /* Let the kind equal the bit size divided by 8. This insulates the
392 programmer from the underlying byte size. */
393 kind = bitsize / 8;
394
395 if (kind == 4)
396 saw_i4 = true;
397 if (kind == 8)
398 saw_i8 = true;
399
400 gfc_integer_kinds[i_index].kind = kind;
401 gfc_integer_kinds[i_index].radix = 2;
402 gfc_integer_kinds[i_index].digits = bitsize - 1;
403 gfc_integer_kinds[i_index].bit_size = bitsize;
404
405 gfc_logical_kinds[i_index].kind = kind;
406 gfc_logical_kinds[i_index].bit_size = bitsize;
407
408 i_index += 1;
409 }
410
411 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
412 used for large file access. */
413
414 if (saw_i8)
415 gfc_intio_kind = 8;
416 else
417 gfc_intio_kind = 4;
418
419 /* If we do not at least have kind = 4, everything is pointless. */
420 gcc_assert(saw_i4);
421
422 /* Set the maximum integer kind. Used with at least BOZ constants. */
423 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
424
425 r_index = 0;
426 FOR_EACH_MODE_IN_CLASS (float_mode_iter, MODE_FLOAT)
427 {
428 scalar_float_mode mode = float_mode_iter.require ();
429 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
430 int kind;
431
432 if (fmt == NULL)
433 continue;
434 if (!targetm.scalar_mode_supported_p (mode))
435 continue;
436
437 /* Only let float, double, long double and __float128 go through.
438 Runtime support for others is not provided, so they would be
439 useless. */
440 if (!targetm.libgcc_floating_mode_supported_p (mode))
441 continue;
442 if (mode != TYPE_MODE (float_type_node)
443 && (mode != TYPE_MODE (double_type_node))
444 && (mode != TYPE_MODE (long_double_type_node))
445 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
446 && (mode != TFmode)
447 #endif
448 )
449 continue;
450
451 /* Let the kind equal the precision divided by 8, rounding up. Again,
452 this insulates the programmer from the underlying byte size.
453
454 Also, it effectively deals with IEEE extended formats. There, the
455 total size of the type may equal 16, but it's got 6 bytes of padding
456 and the increased size can get in the way of a real IEEE quad format
457 which may also be supported by the target.
458
459 We round up so as to handle IA-64 __floatreg (RFmode), which is an
460 82 bit type. Not to be confused with __float80 (XFmode), which is
461 an 80 bit type also supported by IA-64. So XFmode should come out
462 to be kind=10, and RFmode should come out to be kind=11. Egads. */
463
464 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
465
466 if (kind == 4)
467 saw_r4 = true;
468 if (kind == 8)
469 saw_r8 = true;
470 if (kind == 10)
471 saw_r10 = true;
472 if (kind == 16)
473 saw_r16 = true;
474
475 /* Careful we don't stumble a weird internal mode. */
476 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
477 /* Or have too many modes for the allocated space. */
478 gcc_assert (r_index != MAX_REAL_KINDS);
479
480 gfc_real_kinds[r_index].kind = kind;
481 gfc_real_kinds[r_index].radix = fmt->b;
482 gfc_real_kinds[r_index].digits = fmt->p;
483 gfc_real_kinds[r_index].min_exponent = fmt->emin;
484 gfc_real_kinds[r_index].max_exponent = fmt->emax;
485 if (fmt->pnan < fmt->p)
486 /* This is an IBM extended double format (or the MIPS variant)
487 made up of two IEEE doubles. The value of the long double is
488 the sum of the values of the two parts. The most significant
489 part is required to be the value of the long double rounded
490 to the nearest double. If we use emax of 1024 then we can't
491 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
492 rounding will make the most significant part overflow. */
493 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
494 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
495 r_index += 1;
496 }
497
498 /* Choose the default integer kind. We choose 4 unless the user directs us
499 otherwise. Even if the user specified that the default integer kind is 8,
500 the numeric storage size is not 64 bits. In this case, a warning will be
501 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
502
503 gfc_numeric_storage_size = 4 * 8;
504
505 if (flag_default_integer)
506 {
507 if (!saw_i8)
508 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
509 "%<-fdefault-integer-8%> option");
510
511 gfc_default_integer_kind = 8;
512
513 }
514 else if (flag_integer4_kind == 8)
515 {
516 if (!saw_i8)
517 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
518 "%<-finteger-4-integer-8%> option");
519
520 gfc_default_integer_kind = 8;
521 }
522 else if (saw_i4)
523 {
524 gfc_default_integer_kind = 4;
525 }
526 else
527 {
528 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
529 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
530 }
531
532 /* Choose the default real kind. Again, we choose 4 when possible. */
533 if (flag_default_real)
534 {
535 if (!saw_r8)
536 gfc_fatal_error ("REAL(KIND=8) is not available for "
537 "%<-fdefault-real-8%> option");
538
539 gfc_default_real_kind = 8;
540 }
541 else if (flag_real4_kind == 8)
542 {
543 if (!saw_r8)
544 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
545 "option");
546
547 gfc_default_real_kind = 8;
548 }
549 else if (flag_real4_kind == 10)
550 {
551 if (!saw_r10)
552 gfc_fatal_error ("REAL(KIND=10) is not available for "
553 "%<-freal-4-real-10%> option");
554
555 gfc_default_real_kind = 10;
556 }
557 else if (flag_real4_kind == 16)
558 {
559 if (!saw_r16)
560 gfc_fatal_error ("REAL(KIND=16) is not available for "
561 "%<-freal-4-real-16%> option");
562
563 gfc_default_real_kind = 16;
564 }
565 else if (saw_r4)
566 gfc_default_real_kind = 4;
567 else
568 gfc_default_real_kind = gfc_real_kinds[0].kind;
569
570 /* Choose the default double kind. If -fdefault-real and -fdefault-double
571 are specified, we use kind=8, if it's available. If -fdefault-real is
572 specified without -fdefault-double, we use kind=16, if it's available.
573 Otherwise we do not change anything. */
574 if (flag_default_double && !flag_default_real)
575 gfc_fatal_error ("Use of %<-fdefault-double-8%> requires "
576 "%<-fdefault-real-8%>");
577
578 if (flag_default_real && flag_default_double && saw_r8)
579 gfc_default_double_kind = 8;
580 else if (flag_default_real && saw_r16)
581 gfc_default_double_kind = 16;
582 else if (flag_real8_kind == 4)
583 {
584 if (!saw_r4)
585 gfc_fatal_error ("REAL(KIND=4) is not available for "
586 "%<-freal-8-real-4%> option");
587
588 gfc_default_double_kind = 4;
589 }
590 else if (flag_real8_kind == 10 )
591 {
592 if (!saw_r10)
593 gfc_fatal_error ("REAL(KIND=10) is not available for "
594 "%<-freal-8-real-10%> option");
595
596 gfc_default_double_kind = 10;
597 }
598 else if (flag_real8_kind == 16 )
599 {
600 if (!saw_r16)
601 gfc_fatal_error ("REAL(KIND=10) is not available for "
602 "%<-freal-8-real-16%> option");
603
604 gfc_default_double_kind = 16;
605 }
606 else if (saw_r4 && saw_r8)
607 gfc_default_double_kind = 8;
608 else
609 {
610 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
611 real ... occupies two contiguous numeric storage units.
612
613 Therefore we must be supplied a kind twice as large as we chose
614 for single precision. There are loopholes, in that double
615 precision must *occupy* two storage units, though it doesn't have
616 to *use* two storage units. Which means that you can make this
617 kind artificially wide by padding it. But at present there are
618 no GCC targets for which a two-word type does not exist, so we
619 just let gfc_validate_kind abort and tell us if something breaks. */
620
621 gfc_default_double_kind
622 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
623 }
624
625 /* The default logical kind is constrained to be the same as the
626 default integer kind. Similarly with complex and real. */
627 gfc_default_logical_kind = gfc_default_integer_kind;
628 gfc_default_complex_kind = gfc_default_real_kind;
629
630 /* We only have two character kinds: ASCII and UCS-4.
631 ASCII corresponds to a 8-bit integer type, if one is available.
632 UCS-4 corresponds to a 32-bit integer type, if one is available. */
633 i_index = 0;
634 if ((kind = get_int_kind_from_width (8)) > 0)
635 {
636 gfc_character_kinds[i_index].kind = kind;
637 gfc_character_kinds[i_index].bit_size = 8;
638 gfc_character_kinds[i_index].name = "ascii";
639 i_index++;
640 }
641 if ((kind = get_int_kind_from_width (32)) > 0)
642 {
643 gfc_character_kinds[i_index].kind = kind;
644 gfc_character_kinds[i_index].bit_size = 32;
645 gfc_character_kinds[i_index].name = "iso_10646";
646 i_index++;
647 }
648
649 /* Choose the smallest integer kind for our default character. */
650 gfc_default_character_kind = gfc_character_kinds[0].kind;
651 gfc_character_storage_size = gfc_default_character_kind * 8;
652
653 gfc_index_integer_kind = get_int_kind_from_name (PTRDIFF_TYPE);
654
655 /* Pick a kind the same size as the C "int" type. */
656 gfc_c_int_kind = INT_TYPE_SIZE / 8;
657
658 /* Choose atomic kinds to match C's int. */
659 gfc_atomic_int_kind = gfc_c_int_kind;
660 gfc_atomic_logical_kind = gfc_c_int_kind;
661 }
662
663
664 /* Make sure that a valid kind is present. Returns an index into the
665 associated kinds array, -1 if the kind is not present. */
666
667 static int
668 validate_integer (int kind)
669 {
670 int i;
671
672 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
673 if (gfc_integer_kinds[i].kind == kind)
674 return i;
675
676 return -1;
677 }
678
679 static int
680 validate_real (int kind)
681 {
682 int i;
683
684 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
685 if (gfc_real_kinds[i].kind == kind)
686 return i;
687
688 return -1;
689 }
690
691 static int
692 validate_logical (int kind)
693 {
694 int i;
695
696 for (i = 0; gfc_logical_kinds[i].kind; i++)
697 if (gfc_logical_kinds[i].kind == kind)
698 return i;
699
700 return -1;
701 }
702
703 static int
704 validate_character (int kind)
705 {
706 int i;
707
708 for (i = 0; gfc_character_kinds[i].kind; i++)
709 if (gfc_character_kinds[i].kind == kind)
710 return i;
711
712 return -1;
713 }
714
715 /* Validate a kind given a basic type. The return value is the same
716 for the child functions, with -1 indicating nonexistence of the
717 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
718
719 int
720 gfc_validate_kind (bt type, int kind, bool may_fail)
721 {
722 int rc;
723
724 switch (type)
725 {
726 case BT_REAL: /* Fall through */
727 case BT_COMPLEX:
728 rc = validate_real (kind);
729 break;
730 case BT_INTEGER:
731 rc = validate_integer (kind);
732 break;
733 case BT_LOGICAL:
734 rc = validate_logical (kind);
735 break;
736 case BT_CHARACTER:
737 rc = validate_character (kind);
738 break;
739
740 default:
741 gfc_internal_error ("gfc_validate_kind(): Got bad type");
742 }
743
744 if (rc < 0 && !may_fail)
745 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
746
747 return rc;
748 }
749
750
751 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
752 Reuse common type nodes where possible. Recognize if the kind matches up
753 with a C type. This will be used later in determining which routines may
754 be scarfed from libm. */
755
756 static tree
757 gfc_build_int_type (gfc_integer_info *info)
758 {
759 int mode_precision = info->bit_size;
760
761 if (mode_precision == CHAR_TYPE_SIZE)
762 info->c_char = 1;
763 if (mode_precision == SHORT_TYPE_SIZE)
764 info->c_short = 1;
765 if (mode_precision == INT_TYPE_SIZE)
766 info->c_int = 1;
767 if (mode_precision == LONG_TYPE_SIZE)
768 info->c_long = 1;
769 if (mode_precision == LONG_LONG_TYPE_SIZE)
770 info->c_long_long = 1;
771
772 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
773 return intQI_type_node;
774 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
775 return intHI_type_node;
776 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
777 return intSI_type_node;
778 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
779 return intDI_type_node;
780 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
781 return intTI_type_node;
782
783 return make_signed_type (mode_precision);
784 }
785
786 tree
787 gfc_build_uint_type (int size)
788 {
789 if (size == CHAR_TYPE_SIZE)
790 return unsigned_char_type_node;
791 if (size == SHORT_TYPE_SIZE)
792 return short_unsigned_type_node;
793 if (size == INT_TYPE_SIZE)
794 return unsigned_type_node;
795 if (size == LONG_TYPE_SIZE)
796 return long_unsigned_type_node;
797 if (size == LONG_LONG_TYPE_SIZE)
798 return long_long_unsigned_type_node;
799
800 return make_unsigned_type (size);
801 }
802
803
804 static tree
805 gfc_build_real_type (gfc_real_info *info)
806 {
807 int mode_precision = info->mode_precision;
808 tree new_type;
809
810 if (mode_precision == FLOAT_TYPE_SIZE)
811 info->c_float = 1;
812 if (mode_precision == DOUBLE_TYPE_SIZE)
813 info->c_double = 1;
814 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
815 info->c_long_double = 1;
816 if (mode_precision != LONG_DOUBLE_TYPE_SIZE && mode_precision == 128)
817 {
818 info->c_float128 = 1;
819 gfc_real16_is_float128 = true;
820 }
821
822 if (TYPE_PRECISION (float_type_node) == mode_precision)
823 return float_type_node;
824 if (TYPE_PRECISION (double_type_node) == mode_precision)
825 return double_type_node;
826 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
827 return long_double_type_node;
828
829 new_type = make_node (REAL_TYPE);
830 TYPE_PRECISION (new_type) = mode_precision;
831 layout_type (new_type);
832 return new_type;
833 }
834
835 static tree
836 gfc_build_complex_type (tree scalar_type)
837 {
838 tree new_type;
839
840 if (scalar_type == NULL)
841 return NULL;
842 if (scalar_type == float_type_node)
843 return complex_float_type_node;
844 if (scalar_type == double_type_node)
845 return complex_double_type_node;
846 if (scalar_type == long_double_type_node)
847 return complex_long_double_type_node;
848
849 new_type = make_node (COMPLEX_TYPE);
850 TREE_TYPE (new_type) = scalar_type;
851 layout_type (new_type);
852 return new_type;
853 }
854
855 static tree
856 gfc_build_logical_type (gfc_logical_info *info)
857 {
858 int bit_size = info->bit_size;
859 tree new_type;
860
861 if (bit_size == BOOL_TYPE_SIZE)
862 {
863 info->c_bool = 1;
864 return boolean_type_node;
865 }
866
867 new_type = make_unsigned_type (bit_size);
868 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
869 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
870 TYPE_PRECISION (new_type) = 1;
871
872 return new_type;
873 }
874
875
876 /* Create the backend type nodes. We map them to their
877 equivalent C type, at least for now. We also give
878 names to the types here, and we push them in the
879 global binding level context.*/
880
881 void
882 gfc_init_types (void)
883 {
884 char name_buf[26];
885 int index;
886 tree type;
887 unsigned n;
888
889 /* Create and name the types. */
890 #define PUSH_TYPE(name, node) \
891 pushdecl (build_decl (input_location, \
892 TYPE_DECL, get_identifier (name), node))
893
894 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
895 {
896 type = gfc_build_int_type (&gfc_integer_kinds[index]);
897 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
898 if (TYPE_STRING_FLAG (type))
899 type = make_signed_type (gfc_integer_kinds[index].bit_size);
900 gfc_integer_types[index] = type;
901 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
902 gfc_integer_kinds[index].kind);
903 PUSH_TYPE (name_buf, type);
904 }
905
906 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
907 {
908 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
909 gfc_logical_types[index] = type;
910 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
911 gfc_logical_kinds[index].kind);
912 PUSH_TYPE (name_buf, type);
913 }
914
915 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
916 {
917 type = gfc_build_real_type (&gfc_real_kinds[index]);
918 gfc_real_types[index] = type;
919 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
920 gfc_real_kinds[index].kind);
921 PUSH_TYPE (name_buf, type);
922
923 if (gfc_real_kinds[index].c_float128)
924 gfc_float128_type_node = type;
925
926 type = gfc_build_complex_type (type);
927 gfc_complex_types[index] = type;
928 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
929 gfc_real_kinds[index].kind);
930 PUSH_TYPE (name_buf, type);
931
932 if (gfc_real_kinds[index].c_float128)
933 gfc_complex_float128_type_node = type;
934 }
935
936 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
937 {
938 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
939 type = build_qualified_type (type, TYPE_UNQUALIFIED);
940 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
941 gfc_character_kinds[index].kind);
942 PUSH_TYPE (name_buf, type);
943 gfc_character_types[index] = type;
944 gfc_pcharacter_types[index] = build_pointer_type (type);
945 }
946 gfc_character1_type_node = gfc_character_types[0];
947
948 PUSH_TYPE ("byte", unsigned_char_type_node);
949 PUSH_TYPE ("void", void_type_node);
950
951 /* DBX debugging output gets upset if these aren't set. */
952 if (!TYPE_NAME (integer_type_node))
953 PUSH_TYPE ("c_integer", integer_type_node);
954 if (!TYPE_NAME (char_type_node))
955 PUSH_TYPE ("c_char", char_type_node);
956
957 #undef PUSH_TYPE
958
959 pvoid_type_node = build_pointer_type (void_type_node);
960 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
961 ppvoid_type_node = build_pointer_type (pvoid_type_node);
962 pchar_type_node = build_pointer_type (gfc_character1_type_node);
963 pfunc_type_node
964 = build_pointer_type (build_function_type_list (void_type_node, NULL_TREE));
965
966 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
967 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
968 since this function is called before gfc_init_constants. */
969 gfc_array_range_type
970 = build_range_type (gfc_array_index_type,
971 build_int_cst (gfc_array_index_type, 0),
972 NULL_TREE);
973
974 /* The maximum array element size that can be handled is determined
975 by the number of bits available to store this field in the array
976 descriptor. */
977
978 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
979 gfc_max_array_element_size
980 = wide_int_to_tree (size_type_node,
981 wi::mask (n, UNSIGNED,
982 TYPE_PRECISION (size_type_node)));
983
984 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
985 gfc_charlen_int_kind = 4;
986 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
987 }
988
989 /* Get the type node for the given type and kind. */
990
991 tree
992 gfc_get_int_type (int kind)
993 {
994 int index = gfc_validate_kind (BT_INTEGER, kind, true);
995 return index < 0 ? 0 : gfc_integer_types[index];
996 }
997
998 tree
999 gfc_get_real_type (int kind)
1000 {
1001 int index = gfc_validate_kind (BT_REAL, kind, true);
1002 return index < 0 ? 0 : gfc_real_types[index];
1003 }
1004
1005 tree
1006 gfc_get_complex_type (int kind)
1007 {
1008 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
1009 return index < 0 ? 0 : gfc_complex_types[index];
1010 }
1011
1012 tree
1013 gfc_get_logical_type (int kind)
1014 {
1015 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
1016 return index < 0 ? 0 : gfc_logical_types[index];
1017 }
1018
1019 tree
1020 gfc_get_char_type (int kind)
1021 {
1022 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1023 return index < 0 ? 0 : gfc_character_types[index];
1024 }
1025
1026 tree
1027 gfc_get_pchar_type (int kind)
1028 {
1029 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
1030 return index < 0 ? 0 : gfc_pcharacter_types[index];
1031 }
1032
1033 \f
1034 /* Create a character type with the given kind and length. */
1035
1036 tree
1037 gfc_get_character_type_len_for_eltype (tree eltype, tree len)
1038 {
1039 tree bounds, type;
1040
1041 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
1042 type = build_array_type (eltype, bounds);
1043 TYPE_STRING_FLAG (type) = 1;
1044
1045 return type;
1046 }
1047
1048 tree
1049 gfc_get_character_type_len (int kind, tree len)
1050 {
1051 gfc_validate_kind (BT_CHARACTER, kind, false);
1052 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
1053 }
1054
1055
1056 /* Get a type node for a character kind. */
1057
1058 tree
1059 gfc_get_character_type (int kind, gfc_charlen * cl)
1060 {
1061 tree len;
1062
1063 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
1064 if (len && POINTER_TYPE_P (TREE_TYPE (len)))
1065 len = build_fold_indirect_ref (len);
1066
1067 return gfc_get_character_type_len (kind, len);
1068 }
1069 \f
1070 /* Convert a basic type. This will be an array for character types. */
1071
1072 tree
1073 gfc_typenode_for_spec (gfc_typespec * spec, int codim)
1074 {
1075 tree basetype;
1076
1077 switch (spec->type)
1078 {
1079 case BT_UNKNOWN:
1080 gcc_unreachable ();
1081
1082 case BT_INTEGER:
1083 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1084 has been resolved. This is done so we can convert C_PTR and
1085 C_FUNPTR to simple variables that get translated to (void *). */
1086 if (spec->f90_type == BT_VOID)
1087 {
1088 if (spec->u.derived
1089 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1090 basetype = ptr_type_node;
1091 else
1092 basetype = pfunc_type_node;
1093 }
1094 else
1095 basetype = gfc_get_int_type (spec->kind);
1096 break;
1097
1098 case BT_REAL:
1099 basetype = gfc_get_real_type (spec->kind);
1100 break;
1101
1102 case BT_COMPLEX:
1103 basetype = gfc_get_complex_type (spec->kind);
1104 break;
1105
1106 case BT_LOGICAL:
1107 basetype = gfc_get_logical_type (spec->kind);
1108 break;
1109
1110 case BT_CHARACTER:
1111 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
1112 break;
1113
1114 case BT_HOLLERITH:
1115 /* Since this cannot be used, return a length one character. */
1116 basetype = gfc_get_character_type_len (gfc_default_character_kind,
1117 gfc_index_one_node);
1118 break;
1119
1120 case BT_UNION:
1121 basetype = gfc_get_union_type (spec->u.derived);
1122 break;
1123
1124 case BT_DERIVED:
1125 case BT_CLASS:
1126 basetype = gfc_get_derived_type (spec->u.derived, codim);
1127
1128 if (spec->type == BT_CLASS)
1129 GFC_CLASS_TYPE_P (basetype) = 1;
1130
1131 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1132 type and kind to fit a (void *) and the basetype returned was a
1133 ptr_type_node. We need to pass up this new information to the
1134 symbol that was declared of type C_PTR or C_FUNPTR. */
1135 if (spec->u.derived->ts.f90_type == BT_VOID)
1136 {
1137 spec->type = BT_INTEGER;
1138 spec->kind = gfc_index_integer_kind;
1139 spec->f90_type = BT_VOID;
1140 }
1141 break;
1142 case BT_VOID:
1143 case BT_ASSUMED:
1144 /* This is for the second arg to c_f_pointer and c_f_procpointer
1145 of the iso_c_binding module, to accept any ptr type. */
1146 basetype = ptr_type_node;
1147 if (spec->f90_type == BT_VOID)
1148 {
1149 if (spec->u.derived
1150 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
1151 basetype = ptr_type_node;
1152 else
1153 basetype = pfunc_type_node;
1154 }
1155 break;
1156 default:
1157 gcc_unreachable ();
1158 }
1159 return basetype;
1160 }
1161 \f
1162 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1163
1164 static tree
1165 gfc_conv_array_bound (gfc_expr * expr)
1166 {
1167 /* If expr is an integer constant, return that. */
1168 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1169 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1170
1171 /* Otherwise return NULL. */
1172 return NULL_TREE;
1173 }
1174 \f
1175 /* Return the type of an element of the array. Note that scalar coarrays
1176 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1177 (with POINTER_TYPE stripped) is returned. */
1178
1179 tree
1180 gfc_get_element_type (tree type)
1181 {
1182 tree element;
1183
1184 if (GFC_ARRAY_TYPE_P (type))
1185 {
1186 if (TREE_CODE (type) == POINTER_TYPE)
1187 type = TREE_TYPE (type);
1188 if (GFC_TYPE_ARRAY_RANK (type) == 0)
1189 {
1190 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
1191 element = type;
1192 }
1193 else
1194 {
1195 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1196 element = TREE_TYPE (type);
1197 }
1198 }
1199 else
1200 {
1201 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
1202 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
1203
1204 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
1205 element = TREE_TYPE (element);
1206
1207 /* For arrays, which are not scalar coarrays. */
1208 if (TREE_CODE (element) == ARRAY_TYPE && !TYPE_STRING_FLAG (element))
1209 element = TREE_TYPE (element);
1210 }
1211
1212 return element;
1213 }
1214 \f
1215 /* Build an array. This function is called from gfc_sym_type().
1216 Actually returns array descriptor type.
1217
1218 Format of array descriptors is as follows:
1219
1220 struct gfc_array_descriptor
1221 {
1222 array *data
1223 index offset;
1224 index dtype;
1225 struct descriptor_dimension dimension[N_DIM];
1226 }
1227
1228 struct descriptor_dimension
1229 {
1230 index stride;
1231 index lbound;
1232 index ubound;
1233 }
1234
1235 Translation code should use gfc_conv_descriptor_* rather than
1236 accessing the descriptor directly. Any changes to the array
1237 descriptor type will require changes in gfc_conv_descriptor_* and
1238 gfc_build_array_initializer.
1239
1240 This is represented internally as a RECORD_TYPE. The index nodes
1241 are gfc_array_index_type and the data node is a pointer to the
1242 data. See below for the handling of character types.
1243
1244 The dtype member is formatted as follows:
1245 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1246 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1247 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1248
1249 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1250 this generated poor code for assumed/deferred size arrays. These
1251 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1252 of the GENERIC grammar. Also, there is no way to explicitly set
1253 the array stride, so all data must be packed(1). I've tried to
1254 mark all the functions which would require modification with a GCC
1255 ARRAYS comment.
1256
1257 The data component points to the first element in the array. The
1258 offset field is the position of the origin of the array (i.e. element
1259 (0, 0 ...)). This may be outside the bounds of the array.
1260
1261 An element is accessed by
1262 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1263 This gives good performance as the computation does not involve the
1264 bounds of the array. For packed arrays, this is optimized further
1265 by substituting the known strides.
1266
1267 This system has one problem: all array bounds must be within 2^31
1268 elements of the origin (2^63 on 64-bit machines). For example
1269 integer, dimension (80000:90000, 80000:90000, 2) :: array
1270 may not work properly on 32-bit machines because 80000*80000 >
1271 2^31, so the calculation for stride2 would overflow. This may
1272 still work, but I haven't checked, and it relies on the overflow
1273 doing the right thing.
1274
1275 The way to fix this problem is to access elements as follows:
1276 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1277 Obviously this is much slower. I will make this a compile time
1278 option, something like -fsmall-array-offsets. Mixing code compiled
1279 with and without this switch will work.
1280
1281 (1) This can be worked around by modifying the upper bound of the
1282 previous dimension. This requires extra fields in the descriptor
1283 (both real_ubound and fake_ubound). */
1284
1285
1286 /* Returns true if the array sym does not require a descriptor. */
1287
1288 int
1289 gfc_is_nodesc_array (gfc_symbol * sym)
1290 {
1291 symbol_attribute *array_attr;
1292 gfc_array_spec *as;
1293 bool is_classarray = IS_CLASS_ARRAY (sym);
1294
1295 array_attr = is_classarray ? &CLASS_DATA (sym)->attr : &sym->attr;
1296 as = is_classarray ? CLASS_DATA (sym)->as : sym->as;
1297
1298 gcc_assert (array_attr->dimension || array_attr->codimension);
1299
1300 /* We only want local arrays. */
1301 if ((sym->ts.type != BT_CLASS && sym->attr.pointer)
1302 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer)
1303 || array_attr->allocatable)
1304 return 0;
1305
1306 /* We want a descriptor for associate-name arrays that do not have an
1307 explicitly known shape already. */
1308 if (sym->assoc && as->type != AS_EXPLICIT)
1309 return 0;
1310
1311 /* The dummy is stored in sym and not in the component. */
1312 if (sym->attr.dummy)
1313 return as->type != AS_ASSUMED_SHAPE
1314 && as->type != AS_ASSUMED_RANK;
1315
1316 if (sym->attr.result || sym->attr.function)
1317 return 0;
1318
1319 gcc_assert (as->type == AS_EXPLICIT || as->cp_was_assumed);
1320
1321 return 1;
1322 }
1323
1324
1325 /* Create an array descriptor type. */
1326
1327 static tree
1328 gfc_build_array_type (tree type, gfc_array_spec * as,
1329 enum gfc_array_kind akind, bool restricted,
1330 bool contiguous, int codim)
1331 {
1332 tree lbound[GFC_MAX_DIMENSIONS];
1333 tree ubound[GFC_MAX_DIMENSIONS];
1334 int n, corank;
1335
1336 /* Assumed-shape arrays do not have codimension information stored in the
1337 descriptor. */
1338 corank = MAX (as->corank, codim);
1339 if (as->type == AS_ASSUMED_SHAPE ||
1340 (as->type == AS_ASSUMED_RANK && akind == GFC_ARRAY_ALLOCATABLE))
1341 corank = codim;
1342
1343 if (as->type == AS_ASSUMED_RANK)
1344 for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
1345 {
1346 lbound[n] = NULL_TREE;
1347 ubound[n] = NULL_TREE;
1348 }
1349
1350 for (n = 0; n < as->rank; n++)
1351 {
1352 /* Create expressions for the known bounds of the array. */
1353 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
1354 lbound[n] = gfc_index_one_node;
1355 else
1356 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1357 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1358 }
1359
1360 for (n = as->rank; n < as->rank + corank; n++)
1361 {
1362 if (as->type != AS_DEFERRED && as->lower[n] == NULL)
1363 lbound[n] = gfc_index_one_node;
1364 else
1365 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1366
1367 if (n < as->rank + corank - 1)
1368 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1369 }
1370
1371 if (as->type == AS_ASSUMED_SHAPE)
1372 akind = contiguous ? GFC_ARRAY_ASSUMED_SHAPE_CONT
1373 : GFC_ARRAY_ASSUMED_SHAPE;
1374 else if (as->type == AS_ASSUMED_RANK)
1375 akind = contiguous ? GFC_ARRAY_ASSUMED_RANK_CONT
1376 : GFC_ARRAY_ASSUMED_RANK;
1377 return gfc_get_array_type_bounds (type, as->rank == -1
1378 ? GFC_MAX_DIMENSIONS : as->rank,
1379 corank, lbound, ubound, 0, akind,
1380 restricted);
1381 }
1382 \f
1383 /* Returns the struct descriptor_dimension type. */
1384
1385 static tree
1386 gfc_get_desc_dim_type (void)
1387 {
1388 tree type;
1389 tree decl, *chain = NULL;
1390
1391 if (gfc_desc_dim_type)
1392 return gfc_desc_dim_type;
1393
1394 /* Build the type node. */
1395 type = make_node (RECORD_TYPE);
1396
1397 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1398 TYPE_PACKED (type) = 1;
1399
1400 /* Consists of the stride, lbound and ubound members. */
1401 decl = gfc_add_field_to_struct_1 (type,
1402 get_identifier ("stride"),
1403 gfc_array_index_type, &chain);
1404 TREE_NO_WARNING (decl) = 1;
1405
1406 decl = gfc_add_field_to_struct_1 (type,
1407 get_identifier ("lbound"),
1408 gfc_array_index_type, &chain);
1409 TREE_NO_WARNING (decl) = 1;
1410
1411 decl = gfc_add_field_to_struct_1 (type,
1412 get_identifier ("ubound"),
1413 gfc_array_index_type, &chain);
1414 TREE_NO_WARNING (decl) = 1;
1415
1416 /* Finish off the type. */
1417 gfc_finish_type (type);
1418 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
1419
1420 gfc_desc_dim_type = type;
1421 return type;
1422 }
1423
1424
1425 /* Return the DTYPE for an array. This describes the type and type parameters
1426 of the array. */
1427 /* TODO: Only call this when the value is actually used, and make all the
1428 unknown cases abort. */
1429
1430 tree
1431 gfc_get_dtype_rank_type (int rank, tree etype)
1432 {
1433 tree size;
1434 int n;
1435 HOST_WIDE_INT i;
1436 tree tmp;
1437 tree dtype;
1438
1439 switch (TREE_CODE (etype))
1440 {
1441 case INTEGER_TYPE:
1442 n = BT_INTEGER;
1443 break;
1444
1445 case BOOLEAN_TYPE:
1446 n = BT_LOGICAL;
1447 break;
1448
1449 case REAL_TYPE:
1450 n = BT_REAL;
1451 break;
1452
1453 case COMPLEX_TYPE:
1454 n = BT_COMPLEX;
1455 break;
1456
1457 /* We will never have arrays of arrays. */
1458 case RECORD_TYPE:
1459 n = BT_DERIVED;
1460 break;
1461
1462 case ARRAY_TYPE:
1463 n = BT_CHARACTER;
1464 break;
1465
1466 case POINTER_TYPE:
1467 n = BT_ASSUMED;
1468 break;
1469
1470 default:
1471 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1472 /* We can strange array types for temporary arrays. */
1473 return gfc_index_zero_node;
1474 }
1475
1476 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
1477 size = TYPE_SIZE_UNIT (etype);
1478
1479 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1480 if (size && INTEGER_CST_P (size))
1481 {
1482 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1483 gfc_fatal_error ("Array element size too big at %C");
1484
1485 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1486 }
1487 dtype = build_int_cst (gfc_array_index_type, i);
1488
1489 if (size && !INTEGER_CST_P (size))
1490 {
1491 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
1492 tmp = fold_build2_loc (input_location, LSHIFT_EXPR,
1493 gfc_array_index_type,
1494 fold_convert (gfc_array_index_type, size), tmp);
1495 dtype = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
1496 tmp, dtype);
1497 }
1498 /* If we don't know the size we leave it as zero. This should never happen
1499 for anything that is actually used. */
1500 /* TODO: Check this is actually true, particularly when repacking
1501 assumed size parameters. */
1502
1503 return dtype;
1504 }
1505
1506
1507 tree
1508 gfc_get_dtype (tree type)
1509 {
1510 tree dtype;
1511 tree etype;
1512 int rank;
1513
1514 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1515
1516 if (GFC_TYPE_ARRAY_DTYPE (type))
1517 return GFC_TYPE_ARRAY_DTYPE (type);
1518
1519 rank = GFC_TYPE_ARRAY_RANK (type);
1520 etype = gfc_get_element_type (type);
1521 dtype = gfc_get_dtype_rank_type (rank, etype);
1522
1523 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
1524 return dtype;
1525 }
1526
1527
1528 /* Build an array type for use without a descriptor, packed according
1529 to the value of PACKED. */
1530
1531 tree
1532 gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1533 bool restricted)
1534 {
1535 tree range;
1536 tree type;
1537 tree tmp;
1538 int n;
1539 int known_stride;
1540 int known_offset;
1541 mpz_t offset;
1542 mpz_t stride;
1543 mpz_t delta;
1544 gfc_expr *expr;
1545
1546 mpz_init_set_ui (offset, 0);
1547 mpz_init_set_ui (stride, 1);
1548 mpz_init (delta);
1549
1550 /* We don't use build_array_type because this does not include include
1551 lang-specific information (i.e. the bounds of the array) when checking
1552 for duplicates. */
1553 if (as->rank)
1554 type = make_node (ARRAY_TYPE);
1555 else
1556 type = build_variant_type_copy (etype);
1557
1558 GFC_ARRAY_TYPE_P (type) = 1;
1559 TYPE_LANG_SPECIFIC (type) = ggc_cleared_alloc<struct lang_type> ();
1560
1561 known_stride = (packed != PACKED_NO);
1562 known_offset = 1;
1563 for (n = 0; n < as->rank; n++)
1564 {
1565 /* Fill in the stride and bound components of the type. */
1566 if (known_stride)
1567 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1568 else
1569 tmp = NULL_TREE;
1570 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1571
1572 expr = as->lower[n];
1573 if (expr->expr_type == EXPR_CONSTANT)
1574 {
1575 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1576 gfc_index_integer_kind);
1577 }
1578 else
1579 {
1580 known_stride = 0;
1581 tmp = NULL_TREE;
1582 }
1583 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1584
1585 if (known_stride)
1586 {
1587 /* Calculate the offset. */
1588 mpz_mul (delta, stride, as->lower[n]->value.integer);
1589 mpz_sub (offset, offset, delta);
1590 }
1591 else
1592 known_offset = 0;
1593
1594 expr = as->upper[n];
1595 if (expr && expr->expr_type == EXPR_CONSTANT)
1596 {
1597 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1598 gfc_index_integer_kind);
1599 }
1600 else
1601 {
1602 tmp = NULL_TREE;
1603 known_stride = 0;
1604 }
1605 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1606
1607 if (known_stride)
1608 {
1609 /* Calculate the stride. */
1610 mpz_sub (delta, as->upper[n]->value.integer,
1611 as->lower[n]->value.integer);
1612 mpz_add_ui (delta, delta, 1);
1613 mpz_mul (stride, stride, delta);
1614 }
1615
1616 /* Only the first stride is known for partial packed arrays. */
1617 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
1618 known_stride = 0;
1619 }
1620 for (n = as->rank; n < as->rank + as->corank; n++)
1621 {
1622 expr = as->lower[n];
1623 if (expr->expr_type == EXPR_CONSTANT)
1624 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1625 gfc_index_integer_kind);
1626 else
1627 tmp = NULL_TREE;
1628 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1629
1630 expr = as->upper[n];
1631 if (expr && expr->expr_type == EXPR_CONSTANT)
1632 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1633 gfc_index_integer_kind);
1634 else
1635 tmp = NULL_TREE;
1636 if (n < as->rank + as->corank - 1)
1637 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1638 }
1639
1640 if (known_offset)
1641 {
1642 GFC_TYPE_ARRAY_OFFSET (type) =
1643 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1644 }
1645 else
1646 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1647
1648 if (known_stride)
1649 {
1650 GFC_TYPE_ARRAY_SIZE (type) =
1651 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1652 }
1653 else
1654 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1655
1656 GFC_TYPE_ARRAY_RANK (type) = as->rank;
1657 GFC_TYPE_ARRAY_CORANK (type) = as->corank;
1658 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1659 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1660 NULL_TREE);
1661 /* TODO: use main type if it is unbounded. */
1662 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1663 build_pointer_type (build_array_type (etype, range));
1664 if (restricted)
1665 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1666 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1667 TYPE_QUAL_RESTRICT);
1668
1669 if (as->rank == 0)
1670 {
1671 if (packed != PACKED_STATIC || flag_coarray == GFC_FCOARRAY_LIB)
1672 {
1673 type = build_pointer_type (type);
1674
1675 if (restricted)
1676 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1677
1678 GFC_ARRAY_TYPE_P (type) = 1;
1679 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1680 }
1681
1682 return type;
1683 }
1684
1685 if (known_stride)
1686 {
1687 mpz_sub_ui (stride, stride, 1);
1688 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1689 }
1690 else
1691 range = NULL_TREE;
1692
1693 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1694 TYPE_DOMAIN (type) = range;
1695
1696 build_pointer_type (etype);
1697 TREE_TYPE (type) = etype;
1698
1699 layout_type (type);
1700
1701 mpz_clear (offset);
1702 mpz_clear (stride);
1703 mpz_clear (delta);
1704
1705 /* Represent packed arrays as multi-dimensional if they have rank >
1706 1 and with proper bounds, instead of flat arrays. This makes for
1707 better debug info. */
1708 if (known_offset)
1709 {
1710 tree gtype = etype, rtype, type_decl;
1711
1712 for (n = as->rank - 1; n >= 0; n--)
1713 {
1714 rtype = build_range_type (gfc_array_index_type,
1715 GFC_TYPE_ARRAY_LBOUND (type, n),
1716 GFC_TYPE_ARRAY_UBOUND (type, n));
1717 gtype = build_array_type (gtype, rtype);
1718 }
1719 TYPE_NAME (type) = type_decl = build_decl (input_location,
1720 TYPE_DECL, NULL, gtype);
1721 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1722 }
1723
1724 if (packed != PACKED_STATIC || !known_stride
1725 || (as->corank && flag_coarray == GFC_FCOARRAY_LIB))
1726 {
1727 /* For dummy arrays and automatic (heap allocated) arrays we
1728 want a pointer to the array. */
1729 type = build_pointer_type (type);
1730 if (restricted)
1731 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1732 GFC_ARRAY_TYPE_P (type) = 1;
1733 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1734 }
1735 return type;
1736 }
1737
1738
1739 /* Return or create the base type for an array descriptor. */
1740
1741 static tree
1742 gfc_get_array_descriptor_base (int dimen, int codimen, bool restricted)
1743 {
1744 tree fat_type, decl, arraytype, *chain = NULL;
1745 char name[16 + 2*GFC_RANK_DIGITS + 1 + 1];
1746 int idx;
1747
1748 /* Assumed-rank array. */
1749 if (dimen == -1)
1750 dimen = GFC_MAX_DIMENSIONS;
1751
1752 idx = 2 * (codimen + dimen) + restricted;
1753
1754 gcc_assert (codimen + dimen >= 0 && codimen + dimen <= GFC_MAX_DIMENSIONS);
1755
1756 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1757 {
1758 if (gfc_array_descriptor_base_caf[idx])
1759 return gfc_array_descriptor_base_caf[idx];
1760 }
1761 else if (gfc_array_descriptor_base[idx])
1762 return gfc_array_descriptor_base[idx];
1763
1764 /* Build the type node. */
1765 fat_type = make_node (RECORD_TYPE);
1766
1767 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen + codimen);
1768 TYPE_NAME (fat_type) = get_identifier (name);
1769 TYPE_NAMELESS (fat_type) = 1;
1770
1771 /* Add the data member as the first element of the descriptor. */
1772 decl = gfc_add_field_to_struct_1 (fat_type,
1773 get_identifier ("data"),
1774 (restricted
1775 ? prvoid_type_node
1776 : ptr_type_node), &chain);
1777
1778 /* Add the base component. */
1779 decl = gfc_add_field_to_struct_1 (fat_type,
1780 get_identifier ("offset"),
1781 gfc_array_index_type, &chain);
1782 TREE_NO_WARNING (decl) = 1;
1783
1784 /* Add the dtype component. */
1785 decl = gfc_add_field_to_struct_1 (fat_type,
1786 get_identifier ("dtype"),
1787 gfc_array_index_type, &chain);
1788 TREE_NO_WARNING (decl) = 1;
1789
1790 /* Add the span component. */
1791 decl = gfc_add_field_to_struct_1 (fat_type,
1792 get_identifier ("span"),
1793 gfc_array_index_type, &chain);
1794 TREE_NO_WARNING (decl) = 1;
1795
1796 /* Build the array type for the stride and bound components. */
1797 if (dimen + codimen > 0)
1798 {
1799 arraytype =
1800 build_array_type (gfc_get_desc_dim_type (),
1801 build_range_type (gfc_array_index_type,
1802 gfc_index_zero_node,
1803 gfc_rank_cst[codimen + dimen - 1]));
1804
1805 decl = gfc_add_field_to_struct_1 (fat_type, get_identifier ("dim"),
1806 arraytype, &chain);
1807 TREE_NO_WARNING (decl) = 1;
1808 }
1809
1810 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1811 {
1812 decl = gfc_add_field_to_struct_1 (fat_type,
1813 get_identifier ("token"),
1814 prvoid_type_node, &chain);
1815 TREE_NO_WARNING (decl) = 1;
1816 }
1817
1818 /* Finish off the type. */
1819 gfc_finish_type (fat_type);
1820 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
1821
1822 if (flag_coarray == GFC_FCOARRAY_LIB && codimen)
1823 gfc_array_descriptor_base_caf[idx] = fat_type;
1824 else
1825 gfc_array_descriptor_base[idx] = fat_type;
1826
1827 return fat_type;
1828 }
1829
1830
1831 /* Build an array (descriptor) type with given bounds. */
1832
1833 tree
1834 gfc_get_array_type_bounds (tree etype, int dimen, int codimen, tree * lbound,
1835 tree * ubound, int packed,
1836 enum gfc_array_kind akind, bool restricted)
1837 {
1838 char name[8 + 2*GFC_RANK_DIGITS + 1 + GFC_MAX_SYMBOL_LEN];
1839 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
1840 const char *type_name;
1841 int n;
1842
1843 base_type = gfc_get_array_descriptor_base (dimen, codimen, restricted);
1844 fat_type = build_distinct_type_copy (base_type);
1845 /* Make sure that nontarget and target array type have the same canonical
1846 type (and same stub decl for debug info). */
1847 base_type = gfc_get_array_descriptor_base (dimen, codimen, false);
1848 TYPE_CANONICAL (fat_type) = base_type;
1849 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
1850
1851 tmp = TYPE_NAME (etype);
1852 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1853 tmp = DECL_NAME (tmp);
1854 if (tmp)
1855 type_name = IDENTIFIER_POINTER (tmp);
1856 else
1857 type_name = "unknown";
1858 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen + codimen,
1859 GFC_MAX_SYMBOL_LEN, type_name);
1860 TYPE_NAME (fat_type) = get_identifier (name);
1861 TYPE_NAMELESS (fat_type) = 1;
1862
1863 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1864 TYPE_LANG_SPECIFIC (fat_type) = ggc_cleared_alloc<struct lang_type> ();
1865
1866 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1867 GFC_TYPE_ARRAY_CORANK (fat_type) = codimen;
1868 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1869 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
1870
1871 /* Build an array descriptor record type. */
1872 if (packed != 0)
1873 stride = gfc_index_one_node;
1874 else
1875 stride = NULL_TREE;
1876 for (n = 0; n < dimen + codimen; n++)
1877 {
1878 if (n < dimen)
1879 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1880
1881 if (lbound)
1882 lower = lbound[n];
1883 else
1884 lower = NULL_TREE;
1885
1886 if (lower != NULL_TREE)
1887 {
1888 if (INTEGER_CST_P (lower))
1889 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1890 else
1891 lower = NULL_TREE;
1892 }
1893
1894 if (codimen && n == dimen + codimen - 1)
1895 break;
1896
1897 upper = ubound[n];
1898 if (upper != NULL_TREE)
1899 {
1900 if (INTEGER_CST_P (upper))
1901 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1902 else
1903 upper = NULL_TREE;
1904 }
1905
1906 if (n >= dimen)
1907 continue;
1908
1909 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1910 {
1911 tmp = fold_build2_loc (input_location, MINUS_EXPR,
1912 gfc_array_index_type, upper, lower);
1913 tmp = fold_build2_loc (input_location, PLUS_EXPR,
1914 gfc_array_index_type, tmp,
1915 gfc_index_one_node);
1916 stride = fold_build2_loc (input_location, MULT_EXPR,
1917 gfc_array_index_type, tmp, stride);
1918 /* Check the folding worked. */
1919 gcc_assert (INTEGER_CST_P (stride));
1920 }
1921 else
1922 stride = NULL_TREE;
1923 }
1924 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1925
1926 /* TODO: known offsets for descriptors. */
1927 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1928
1929 if (dimen == 0)
1930 {
1931 arraytype = build_pointer_type (etype);
1932 if (restricted)
1933 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1934
1935 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1936 return fat_type;
1937 }
1938
1939 /* We define data as an array with the correct size if possible.
1940 Much better than doing pointer arithmetic. */
1941 if (stride)
1942 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1943 int_const_binop (MINUS_EXPR, stride,
1944 build_int_cst (TREE_TYPE (stride), 1)));
1945 else
1946 rtype = gfc_array_range_type;
1947 arraytype = build_array_type (etype, rtype);
1948 arraytype = build_pointer_type (arraytype);
1949 if (restricted)
1950 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
1951 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1952
1953 /* This will generate the base declarations we need to emit debug
1954 information for this type. FIXME: there must be a better way to
1955 avoid divergence between compilations with and without debug
1956 information. */
1957 {
1958 struct array_descr_info info;
1959 gfc_get_array_descr_info (fat_type, &info);
1960 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1961 }
1962
1963 return fat_type;
1964 }
1965 \f
1966 /* Build a pointer type. This function is called from gfc_sym_type(). */
1967
1968 static tree
1969 gfc_build_pointer_type (gfc_symbol * sym, tree type)
1970 {
1971 /* Array pointer types aren't actually pointers. */
1972 if (sym->attr.dimension)
1973 return type;
1974 else
1975 return build_pointer_type (type);
1976 }
1977
1978 static tree gfc_nonrestricted_type (tree t);
1979 /* Given two record or union type nodes TO and FROM, ensure
1980 that all fields in FROM have a corresponding field in TO,
1981 their type being nonrestrict variants. This accepts a TO
1982 node that already has a prefix of the fields in FROM. */
1983 static void
1984 mirror_fields (tree to, tree from)
1985 {
1986 tree fto, ffrom;
1987 tree *chain;
1988
1989 /* Forward to the end of TOs fields. */
1990 fto = TYPE_FIELDS (to);
1991 ffrom = TYPE_FIELDS (from);
1992 chain = &TYPE_FIELDS (to);
1993 while (fto)
1994 {
1995 gcc_assert (ffrom && DECL_NAME (fto) == DECL_NAME (ffrom));
1996 chain = &DECL_CHAIN (fto);
1997 fto = DECL_CHAIN (fto);
1998 ffrom = DECL_CHAIN (ffrom);
1999 }
2000
2001 /* Now add all fields remaining in FROM (starting with ffrom). */
2002 for (; ffrom; ffrom = DECL_CHAIN (ffrom))
2003 {
2004 tree newfield = copy_node (ffrom);
2005 DECL_CONTEXT (newfield) = to;
2006 /* The store to DECL_CHAIN might seem redundant with the
2007 stores to *chain, but not clearing it here would mean
2008 leaving a chain into the old fields. If ever
2009 our called functions would look at them confusion
2010 will arise. */
2011 DECL_CHAIN (newfield) = NULL_TREE;
2012 *chain = newfield;
2013 chain = &DECL_CHAIN (newfield);
2014
2015 if (TREE_CODE (ffrom) == FIELD_DECL)
2016 {
2017 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (ffrom));
2018 TREE_TYPE (newfield) = elemtype;
2019 }
2020 }
2021 *chain = NULL_TREE;
2022 }
2023
2024 /* Given a type T, returns a different type of the same structure,
2025 except that all types it refers to (recursively) are always
2026 non-restrict qualified types. */
2027 static tree
2028 gfc_nonrestricted_type (tree t)
2029 {
2030 tree ret = t;
2031
2032 /* If the type isn't laid out yet, don't copy it. If something
2033 needs it for real it should wait until the type got finished. */
2034 if (!TYPE_SIZE (t))
2035 return t;
2036
2037 if (!TYPE_LANG_SPECIFIC (t))
2038 TYPE_LANG_SPECIFIC (t) = ggc_cleared_alloc<struct lang_type> ();
2039 /* If we're dealing with this very node already further up
2040 the call chain (recursion via pointers and struct members)
2041 we haven't yet determined if we really need a new type node.
2042 Assume we don't, return T itself. */
2043 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type == error_mark_node)
2044 return t;
2045
2046 /* If we have calculated this all already, just return it. */
2047 if (TYPE_LANG_SPECIFIC (t)->nonrestricted_type)
2048 return TYPE_LANG_SPECIFIC (t)->nonrestricted_type;
2049
2050 /* Mark this type. */
2051 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = error_mark_node;
2052
2053 switch (TREE_CODE (t))
2054 {
2055 default:
2056 break;
2057
2058 case POINTER_TYPE:
2059 case REFERENCE_TYPE:
2060 {
2061 tree totype = gfc_nonrestricted_type (TREE_TYPE (t));
2062 if (totype == TREE_TYPE (t))
2063 ret = t;
2064 else if (TREE_CODE (t) == POINTER_TYPE)
2065 ret = build_pointer_type (totype);
2066 else
2067 ret = build_reference_type (totype);
2068 ret = build_qualified_type (ret,
2069 TYPE_QUALS (t) & ~TYPE_QUAL_RESTRICT);
2070 }
2071 break;
2072
2073 case ARRAY_TYPE:
2074 {
2075 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (t));
2076 if (elemtype == TREE_TYPE (t))
2077 ret = t;
2078 else
2079 {
2080 ret = build_variant_type_copy (t);
2081 TREE_TYPE (ret) = elemtype;
2082 if (TYPE_LANG_SPECIFIC (t)
2083 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2084 {
2085 tree dataptr_type = GFC_TYPE_ARRAY_DATAPTR_TYPE (t);
2086 dataptr_type = gfc_nonrestricted_type (dataptr_type);
2087 if (dataptr_type != GFC_TYPE_ARRAY_DATAPTR_TYPE (t))
2088 {
2089 TYPE_LANG_SPECIFIC (ret)
2090 = ggc_cleared_alloc<struct lang_type> ();
2091 *TYPE_LANG_SPECIFIC (ret) = *TYPE_LANG_SPECIFIC (t);
2092 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret) = dataptr_type;
2093 }
2094 }
2095 }
2096 }
2097 break;
2098
2099 case RECORD_TYPE:
2100 case UNION_TYPE:
2101 case QUAL_UNION_TYPE:
2102 {
2103 tree field;
2104 /* First determine if we need a new type at all.
2105 Careful, the two calls to gfc_nonrestricted_type per field
2106 might return different values. That happens exactly when
2107 one of the fields reaches back to this very record type
2108 (via pointers). The first calls will assume that we don't
2109 need to copy T (see the error_mark_node marking). If there
2110 are any reasons for copying T apart from having to copy T,
2111 we'll indeed copy it, and the second calls to
2112 gfc_nonrestricted_type will use that new node if they
2113 reach back to T. */
2114 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2115 if (TREE_CODE (field) == FIELD_DECL)
2116 {
2117 tree elemtype = gfc_nonrestricted_type (TREE_TYPE (field));
2118 if (elemtype != TREE_TYPE (field))
2119 break;
2120 }
2121 if (!field)
2122 break;
2123 ret = build_variant_type_copy (t);
2124 TYPE_FIELDS (ret) = NULL_TREE;
2125
2126 /* Here we make sure that as soon as we know we have to copy
2127 T, that also fields reaching back to us will use the new
2128 copy. It's okay if that copy still contains the old fields,
2129 we won't look at them. */
2130 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2131 mirror_fields (ret, t);
2132 }
2133 break;
2134 }
2135
2136 TYPE_LANG_SPECIFIC (t)->nonrestricted_type = ret;
2137 return ret;
2138 }
2139
2140 \f
2141 /* Return the type for a symbol. Special handling is required for character
2142 types to get the correct level of indirection.
2143 For functions return the return type.
2144 For subroutines return void_type_node.
2145 Calling this multiple times for the same symbol should be avoided,
2146 especially for character and array types. */
2147
2148 tree
2149 gfc_sym_type (gfc_symbol * sym)
2150 {
2151 tree type;
2152 int byref;
2153 bool restricted;
2154
2155 /* Procedure Pointers inside COMMON blocks. */
2156 if (sym->attr.proc_pointer && sym->attr.in_common)
2157 {
2158 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2159 sym->attr.proc_pointer = 0;
2160 type = build_pointer_type (gfc_get_function_type (sym));
2161 sym->attr.proc_pointer = 1;
2162 return type;
2163 }
2164
2165 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2166 return void_type_node;
2167
2168 /* In the case of a function the fake result variable may have a
2169 type different from the function type, so don't return early in
2170 that case. */
2171 if (sym->backend_decl && !sym->attr.function)
2172 return TREE_TYPE (sym->backend_decl);
2173
2174 if (sym->ts.type == BT_CHARACTER
2175 && ((sym->attr.function && sym->attr.is_bind_c)
2176 || (sym->attr.result
2177 && sym->ns->proc_name
2178 && sym->ns->proc_name->attr.is_bind_c)
2179 || (sym->ts.deferred && (!sym->ts.u.cl
2180 || !sym->ts.u.cl->backend_decl))))
2181 type = gfc_character1_type_node;
2182 else
2183 type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension);
2184
2185 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
2186 byref = 1;
2187 else
2188 byref = 0;
2189
2190 restricted = !sym->attr.target && !sym->attr.pointer
2191 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
2192 if (!restricted)
2193 type = gfc_nonrestricted_type (type);
2194
2195 if (sym->attr.dimension || sym->attr.codimension)
2196 {
2197 if (gfc_is_nodesc_array (sym))
2198 {
2199 /* If this is a character argument of unknown length, just use the
2200 base type. */
2201 if (sym->ts.type != BT_CHARACTER
2202 || !(sym->attr.dummy || sym->attr.function)
2203 || sym->ts.u.cl->backend_decl)
2204 {
2205 type = gfc_get_nodesc_array_type (type, sym->as,
2206 byref ? PACKED_FULL
2207 : PACKED_STATIC,
2208 restricted);
2209 byref = 0;
2210 }
2211 }
2212 else
2213 {
2214 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
2215 if (sym->attr.pointer)
2216 akind = sym->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2217 : GFC_ARRAY_POINTER;
2218 else if (sym->attr.allocatable)
2219 akind = GFC_ARRAY_ALLOCATABLE;
2220 type = gfc_build_array_type (type, sym->as, akind, restricted,
2221 sym->attr.contiguous, false);
2222 }
2223 }
2224 else
2225 {
2226 if (sym->attr.allocatable || sym->attr.pointer
2227 || gfc_is_associate_pointer (sym))
2228 type = gfc_build_pointer_type (sym, type);
2229 }
2230
2231 /* We currently pass all parameters by reference.
2232 See f95_get_function_decl. For dummy function parameters return the
2233 function type. */
2234 if (byref)
2235 {
2236 /* We must use pointer types for potentially absent variables. The
2237 optimizers assume a reference type argument is never NULL. */
2238 if (sym->attr.optional
2239 || (sym->ns->proc_name && sym->ns->proc_name->attr.entry_master))
2240 type = build_pointer_type (type);
2241 else
2242 {
2243 type = build_reference_type (type);
2244 if (restricted)
2245 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
2246 }
2247 }
2248
2249 return (type);
2250 }
2251 \f
2252 /* Layout and output debug info for a record type. */
2253
2254 void
2255 gfc_finish_type (tree type)
2256 {
2257 tree decl;
2258
2259 decl = build_decl (input_location,
2260 TYPE_DECL, NULL_TREE, type);
2261 TYPE_STUB_DECL (type) = decl;
2262 layout_type (type);
2263 rest_of_type_compilation (type, 1);
2264 rest_of_decl_compilation (decl, 1, 0);
2265 }
2266 \f
2267 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2268 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2269 to the end of the field list pointed to by *CHAIN.
2270
2271 Returns a pointer to the new field. */
2272
2273 static tree
2274 gfc_add_field_to_struct_1 (tree context, tree name, tree type, tree **chain)
2275 {
2276 tree decl = build_decl (input_location, FIELD_DECL, name, type);
2277
2278 DECL_CONTEXT (decl) = context;
2279 DECL_CHAIN (decl) = NULL_TREE;
2280 if (TYPE_FIELDS (context) == NULL_TREE)
2281 TYPE_FIELDS (context) = decl;
2282 if (chain != NULL)
2283 {
2284 if (*chain != NULL)
2285 **chain = decl;
2286 *chain = &DECL_CHAIN (decl);
2287 }
2288
2289 return decl;
2290 }
2291
2292 /* Like `gfc_add_field_to_struct_1', but adds alignment
2293 information. */
2294
2295 tree
2296 gfc_add_field_to_struct (tree context, tree name, tree type, tree **chain)
2297 {
2298 tree decl = gfc_add_field_to_struct_1 (context, name, type, chain);
2299
2300 DECL_INITIAL (decl) = 0;
2301 SET_DECL_ALIGN (decl, 0);
2302 DECL_USER_ALIGN (decl) = 0;
2303
2304 return decl;
2305 }
2306
2307
2308 /* Copy the backend_decl and component backend_decls if
2309 the two derived type symbols are "equal", as described
2310 in 4.4.2 and resolved by gfc_compare_derived_types. */
2311
2312 int
2313 gfc_copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
2314 bool from_gsym)
2315 {
2316 gfc_component *to_cm;
2317 gfc_component *from_cm;
2318
2319 if (from == to)
2320 return 1;
2321
2322 if (from->backend_decl == NULL
2323 || !gfc_compare_derived_types (from, to))
2324 return 0;
2325
2326 to->backend_decl = from->backend_decl;
2327
2328 to_cm = to->components;
2329 from_cm = from->components;
2330
2331 /* Copy the component declarations. If a component is itself
2332 a derived type, we need a copy of its component declarations.
2333 This is done by recursing into gfc_get_derived_type and
2334 ensures that the component's component declarations have
2335 been built. If it is a character, we need the character
2336 length, as well. */
2337 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
2338 {
2339 to_cm->backend_decl = from_cm->backend_decl;
2340 if (from_cm->ts.type == BT_UNION)
2341 gfc_get_union_type (to_cm->ts.u.derived);
2342 else if (from_cm->ts.type == BT_DERIVED
2343 && (!from_cm->attr.pointer || from_gsym))
2344 gfc_get_derived_type (to_cm->ts.u.derived);
2345 else if (from_cm->ts.type == BT_CLASS
2346 && (!CLASS_DATA (from_cm)->attr.class_pointer || from_gsym))
2347 gfc_get_derived_type (to_cm->ts.u.derived);
2348 else if (from_cm->ts.type == BT_CHARACTER)
2349 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
2350 }
2351
2352 return 1;
2353 }
2354
2355
2356 /* Build a tree node for a procedure pointer component. */
2357
2358 tree
2359 gfc_get_ppc_type (gfc_component* c)
2360 {
2361 tree t;
2362
2363 /* Explicit interface. */
2364 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
2365 return build_pointer_type (gfc_get_function_type (c->ts.interface));
2366
2367 /* Implicit interface (only return value may be known). */
2368 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
2369 t = gfc_typenode_for_spec (&c->ts);
2370 else
2371 t = void_type_node;
2372
2373 return build_pointer_type (build_function_type_list (t, NULL_TREE));
2374 }
2375
2376
2377 /* Build a tree node for a union type. Requires building each map
2378 structure which is an element of the union. */
2379
2380 tree
2381 gfc_get_union_type (gfc_symbol *un)
2382 {
2383 gfc_component *map = NULL;
2384 tree typenode = NULL, map_type = NULL, map_field = NULL;
2385 tree *chain = NULL;
2386
2387 if (un->backend_decl)
2388 {
2389 if (TYPE_FIELDS (un->backend_decl) || un->attr.proc_pointer_comp)
2390 return un->backend_decl;
2391 else
2392 typenode = un->backend_decl;
2393 }
2394 else
2395 {
2396 typenode = make_node (UNION_TYPE);
2397 TYPE_NAME (typenode) = get_identifier (un->name);
2398 }
2399
2400 /* Add each contained MAP as a field. */
2401 for (map = un->components; map; map = map->next)
2402 {
2403 gcc_assert (map->ts.type == BT_DERIVED);
2404
2405 /* The map's type node, which is defined within this union's context. */
2406 map_type = gfc_get_derived_type (map->ts.u.derived);
2407 TYPE_CONTEXT (map_type) = typenode;
2408
2409 /* The map field's declaration. */
2410 map_field = gfc_add_field_to_struct(typenode, get_identifier(map->name),
2411 map_type, &chain);
2412 if (map->loc.lb)
2413 gfc_set_decl_location (map_field, &map->loc);
2414 else if (un->declared_at.lb)
2415 gfc_set_decl_location (map_field, &un->declared_at);
2416
2417 DECL_PACKED (map_field) |= TYPE_PACKED (typenode);
2418 DECL_NAMELESS(map_field) = true;
2419
2420 /* We should never clobber another backend declaration for this map,
2421 because each map component is unique. */
2422 if (!map->backend_decl)
2423 map->backend_decl = map_field;
2424 }
2425
2426 un->backend_decl = typenode;
2427 gfc_finish_type (typenode);
2428
2429 return typenode;
2430 }
2431
2432
2433 /* Build a tree node for a derived type. If there are equal
2434 derived types, with different local names, these are built
2435 at the same time. If an equal derived type has been built
2436 in a parent namespace, this is used. */
2437
2438 tree
2439 gfc_get_derived_type (gfc_symbol * derived, int codimen)
2440 {
2441 tree typenode = NULL, field = NULL, field_type = NULL;
2442 tree canonical = NULL_TREE;
2443 tree *chain = NULL;
2444 bool got_canonical = false;
2445 bool unlimited_entity = false;
2446 gfc_component *c;
2447 gfc_dt_list *dt;
2448 gfc_namespace *ns;
2449 tree tmp;
2450
2451 gcc_assert (!derived->attr.pdt_template);
2452
2453 if (derived->attr.unlimited_polymorphic
2454 || (flag_coarray == GFC_FCOARRAY_LIB
2455 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2456 && (derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE
2457 || derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
2458 return ptr_type_node;
2459
2460 if (flag_coarray != GFC_FCOARRAY_LIB
2461 && derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2462 && derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2463 return gfc_get_int_type (gfc_default_integer_kind);
2464
2465 if (derived && derived->attr.flavor == FL_PROCEDURE
2466 && derived->attr.generic)
2467 derived = gfc_find_dt_in_generic (derived);
2468
2469 /* See if it's one of the iso_c_binding derived types. */
2470 if (derived->attr.is_iso_c == 1 || derived->ts.f90_type == BT_VOID)
2471 {
2472 if (derived->backend_decl)
2473 return derived->backend_decl;
2474
2475 if (derived->intmod_sym_id == ISOCBINDING_PTR)
2476 derived->backend_decl = ptr_type_node;
2477 else
2478 derived->backend_decl = pfunc_type_node;
2479
2480 derived->ts.kind = gfc_index_integer_kind;
2481 derived->ts.type = BT_INTEGER;
2482 /* Set the f90_type to BT_VOID as a way to recognize something of type
2483 BT_INTEGER that needs to fit a void * for the purpose of the
2484 iso_c_binding derived types. */
2485 derived->ts.f90_type = BT_VOID;
2486
2487 return derived->backend_decl;
2488 }
2489
2490 /* If use associated, use the module type for this one. */
2491 if (derived->backend_decl == NULL
2492 && derived->attr.use_assoc
2493 && derived->module
2494 && gfc_get_module_backend_decl (derived))
2495 goto copy_derived_types;
2496
2497 /* The derived types from an earlier namespace can be used as the
2498 canonical type. */
2499 if (derived->backend_decl == NULL && !derived->attr.use_assoc
2500 && gfc_global_ns_list)
2501 {
2502 for (ns = gfc_global_ns_list;
2503 ns->translated && !got_canonical;
2504 ns = ns->sibling)
2505 {
2506 dt = ns->derived_types;
2507 for (; dt && !canonical; dt = dt->next)
2508 {
2509 gfc_copy_dt_decls_ifequal (dt->derived, derived, true);
2510 if (derived->backend_decl)
2511 got_canonical = true;
2512 }
2513 }
2514 }
2515
2516 /* Store up the canonical type to be added to this one. */
2517 if (got_canonical)
2518 {
2519 if (TYPE_CANONICAL (derived->backend_decl))
2520 canonical = TYPE_CANONICAL (derived->backend_decl);
2521 else
2522 canonical = derived->backend_decl;
2523
2524 derived->backend_decl = NULL_TREE;
2525 }
2526
2527 /* derived->backend_decl != 0 means we saw it before, but its
2528 components' backend_decl may have not been built. */
2529 if (derived->backend_decl)
2530 {
2531 /* Its components' backend_decl have been built or we are
2532 seeing recursion through the formal arglist of a procedure
2533 pointer component. */
2534 if (TYPE_FIELDS (derived->backend_decl))
2535 return derived->backend_decl;
2536 else if (derived->attr.abstract
2537 && derived->attr.proc_pointer_comp)
2538 {
2539 /* If an abstract derived type with procedure pointer
2540 components has no other type of component, return the
2541 backend_decl. Otherwise build the components if any of the
2542 non-procedure pointer components have no backend_decl. */
2543 for (c = derived->components; c; c = c->next)
2544 {
2545 bool same_alloc_type = c->attr.allocatable
2546 && derived == c->ts.u.derived;
2547 if (!c->attr.proc_pointer
2548 && !same_alloc_type
2549 && c->backend_decl == NULL)
2550 break;
2551 else if (c->next == NULL)
2552 return derived->backend_decl;
2553 }
2554 typenode = derived->backend_decl;
2555 }
2556 else
2557 typenode = derived->backend_decl;
2558 }
2559 else
2560 {
2561 /* We see this derived type first time, so build the type node. */
2562 typenode = make_node (RECORD_TYPE);
2563 TYPE_NAME (typenode) = get_identifier (derived->name);
2564 TYPE_PACKED (typenode) = flag_pack_derived;
2565 derived->backend_decl = typenode;
2566 }
2567
2568 if (derived->components
2569 && derived->components->ts.type == BT_DERIVED
2570 && strcmp (derived->components->name, "_data") == 0
2571 && derived->components->ts.u.derived->attr.unlimited_polymorphic)
2572 unlimited_entity = true;
2573
2574 /* Go through the derived type components, building them as
2575 necessary. The reason for doing this now is that it is
2576 possible to recurse back to this derived type through a
2577 pointer component (PR24092). If this happens, the fields
2578 will be built and so we can return the type. */
2579 for (c = derived->components; c; c = c->next)
2580 {
2581 bool same_alloc_type = c->attr.allocatable
2582 && derived == c->ts.u.derived;
2583
2584 if (c->ts.type == BT_UNION && c->ts.u.derived->backend_decl == NULL)
2585 c->ts.u.derived->backend_decl = gfc_get_union_type (c->ts.u.derived);
2586
2587 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
2588 continue;
2589
2590 if ((!c->attr.pointer && !c->attr.proc_pointer
2591 && !same_alloc_type)
2592 || c->ts.u.derived->backend_decl == NULL)
2593 {
2594 int local_codim = c->attr.codimension ? c->as->corank: codimen;
2595 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived,
2596 local_codim);
2597 }
2598
2599 if (c->ts.u.derived->attr.is_iso_c)
2600 {
2601 /* Need to copy the modified ts from the derived type. The
2602 typespec was modified because C_PTR/C_FUNPTR are translated
2603 into (void *) from derived types. */
2604 c->ts.type = c->ts.u.derived->ts.type;
2605 c->ts.kind = c->ts.u.derived->ts.kind;
2606 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
2607 if (c->initializer)
2608 {
2609 c->initializer->ts.type = c->ts.type;
2610 c->initializer->ts.kind = c->ts.kind;
2611 c->initializer->ts.f90_type = c->ts.f90_type;
2612 c->initializer->expr_type = EXPR_NULL;
2613 }
2614 }
2615 }
2616
2617 if (TYPE_FIELDS (derived->backend_decl))
2618 return derived->backend_decl;
2619
2620 /* Build the type member list. Install the newly created RECORD_TYPE
2621 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2622 through only the top-level linked list of components so we correctly
2623 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2624 types are built as part of gfc_get_union_type. */
2625 for (c = derived->components; c; c = c->next)
2626 {
2627 bool same_alloc_type = c->attr.allocatable
2628 && derived == c->ts.u.derived;
2629 /* Prevent infinite recursion, when the procedure pointer type is
2630 the same as derived, by forcing the procedure pointer component to
2631 be built as if the explicit interface does not exist. */
2632 if (c->attr.proc_pointer
2633 && (c->ts.type != BT_DERIVED || (c->ts.u.derived
2634 && !gfc_compare_derived_types (derived, c->ts.u.derived)))
2635 && (c->ts.type != BT_CLASS || (CLASS_DATA (c)->ts.u.derived
2636 && !gfc_compare_derived_types (derived, CLASS_DATA (c)->ts.u.derived))))
2637 field_type = gfc_get_ppc_type (c);
2638 else if (c->attr.proc_pointer && derived->backend_decl)
2639 {
2640 tmp = build_function_type_list (derived->backend_decl, NULL_TREE);
2641 field_type = build_pointer_type (tmp);
2642 }
2643 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
2644 field_type = c->ts.u.derived->backend_decl;
2645 else
2646 {
2647 if (c->ts.type == BT_CHARACTER
2648 && !c->ts.deferred && !c->attr.pdt_string)
2649 {
2650 /* Evaluate the string length. */
2651 gfc_conv_const_charlen (c->ts.u.cl);
2652 gcc_assert (c->ts.u.cl->backend_decl);
2653 }
2654 else if (c->ts.type == BT_CHARACTER)
2655 c->ts.u.cl->backend_decl
2656 = build_int_cst (gfc_charlen_type_node, 0);
2657
2658 field_type = gfc_typenode_for_spec (&c->ts, codimen);
2659 }
2660
2661 /* This returns an array descriptor type. Initialization may be
2662 required. */
2663 if ((c->attr.dimension || c->attr.codimension) && !c->attr.proc_pointer )
2664 {
2665 if (c->attr.pointer || c->attr.allocatable || c->attr.pdt_array)
2666 {
2667 enum gfc_array_kind akind;
2668 if (c->attr.pointer)
2669 akind = c->attr.contiguous ? GFC_ARRAY_POINTER_CONT
2670 : GFC_ARRAY_POINTER;
2671 else
2672 akind = GFC_ARRAY_ALLOCATABLE;
2673 /* Pointers to arrays aren't actually pointer types. The
2674 descriptors are separate, but the data is common. */
2675 field_type = gfc_build_array_type (field_type, c->as, akind,
2676 !c->attr.target
2677 && !c->attr.pointer,
2678 c->attr.contiguous,
2679 codimen);
2680 }
2681 else
2682 field_type = gfc_get_nodesc_array_type (field_type, c->as,
2683 PACKED_STATIC,
2684 !c->attr.target);
2685 }
2686 else if ((c->attr.pointer || c->attr.allocatable || c->attr.pdt_string)
2687 && !c->attr.proc_pointer
2688 && !(unlimited_entity && c == derived->components))
2689 field_type = build_pointer_type (field_type);
2690
2691 if (c->attr.pointer || same_alloc_type)
2692 field_type = gfc_nonrestricted_type (field_type);
2693
2694 /* vtype fields can point to different types to the base type. */
2695 if (c->ts.type == BT_DERIVED
2696 && c->ts.u.derived && c->ts.u.derived->attr.vtype)
2697 field_type = build_pointer_type_for_mode (TREE_TYPE (field_type),
2698 ptr_mode, true);
2699
2700 /* Ensure that the CLASS language specific flag is set. */
2701 if (c->ts.type == BT_CLASS)
2702 {
2703 if (POINTER_TYPE_P (field_type))
2704 GFC_CLASS_TYPE_P (TREE_TYPE (field_type)) = 1;
2705 else
2706 GFC_CLASS_TYPE_P (field_type) = 1;
2707 }
2708
2709 field = gfc_add_field_to_struct (typenode,
2710 get_identifier (c->name),
2711 field_type, &chain);
2712 if (c->loc.lb)
2713 gfc_set_decl_location (field, &c->loc);
2714 else if (derived->declared_at.lb)
2715 gfc_set_decl_location (field, &derived->declared_at);
2716
2717 gfc_finish_decl_attrs (field, &c->attr);
2718
2719 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2720
2721 gcc_assert (field);
2722 if (!c->backend_decl)
2723 c->backend_decl = field;
2724
2725 if (c->attr.pointer && c->attr.dimension
2726 && !(c->ts.type == BT_DERIVED
2727 && strcmp (c->name, "_data") == 0))
2728 GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
2729
2730 /* Do not add a caf_token field for classes' data components. */
2731 if (codimen && !c->attr.dimension && !c->attr.codimension
2732 && (c->attr.allocatable || c->attr.pointer)
2733 && c->caf_token == NULL_TREE && strcmp ("_data", c->name) != 0)
2734 {
2735 char caf_name[GFC_MAX_SYMBOL_LEN];
2736 snprintf (caf_name, GFC_MAX_SYMBOL_LEN, "_caf_%s", c->name);
2737 c->caf_token = gfc_add_field_to_struct (typenode,
2738 get_identifier (caf_name),
2739 pvoid_type_node, &chain);
2740 TREE_NO_WARNING (c->caf_token) = 1;
2741 }
2742 }
2743
2744 /* Now lay out the derived type, including the fields. */
2745 if (canonical)
2746 TYPE_CANONICAL (typenode) = canonical;
2747
2748 gfc_finish_type (typenode);
2749 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
2750 if (derived->module && derived->ns->proc_name
2751 && derived->ns->proc_name->attr.flavor == FL_MODULE)
2752 {
2753 if (derived->ns->proc_name->backend_decl
2754 && TREE_CODE (derived->ns->proc_name->backend_decl)
2755 == NAMESPACE_DECL)
2756 {
2757 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2758 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2759 = derived->ns->proc_name->backend_decl;
2760 }
2761 }
2762
2763 derived->backend_decl = typenode;
2764
2765 copy_derived_types:
2766
2767 for (dt = gfc_derived_types; dt; dt = dt->next)
2768 gfc_copy_dt_decls_ifequal (derived, dt->derived, false);
2769
2770 return derived->backend_decl;
2771 }
2772
2773
2774 int
2775 gfc_return_by_reference (gfc_symbol * sym)
2776 {
2777 if (!sym->attr.function)
2778 return 0;
2779
2780 if (sym->attr.dimension)
2781 return 1;
2782
2783 if (sym->ts.type == BT_CHARACTER
2784 && !sym->attr.is_bind_c
2785 && (!sym->attr.result
2786 || !sym->ns->proc_name
2787 || !sym->ns->proc_name->attr.is_bind_c))
2788 return 1;
2789
2790 /* Possibly return complex numbers by reference for g77 compatibility.
2791 We don't do this for calls to intrinsics (as the library uses the
2792 -fno-f2c calling convention), nor for calls to functions which always
2793 require an explicit interface, as no compatibility problems can
2794 arise there. */
2795 if (flag_f2c && sym->ts.type == BT_COMPLEX
2796 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2797 return 1;
2798
2799 return 0;
2800 }
2801 \f
2802 static tree
2803 gfc_get_mixed_entry_union (gfc_namespace *ns)
2804 {
2805 tree type;
2806 tree *chain = NULL;
2807 char name[GFC_MAX_SYMBOL_LEN + 1];
2808 gfc_entry_list *el, *el2;
2809
2810 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2811 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2812
2813 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2814
2815 /* Build the type node. */
2816 type = make_node (UNION_TYPE);
2817
2818 TYPE_NAME (type) = get_identifier (name);
2819
2820 for (el = ns->entries; el; el = el->next)
2821 {
2822 /* Search for duplicates. */
2823 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2824 if (el2->sym->result == el->sym->result)
2825 break;
2826
2827 if (el == el2)
2828 gfc_add_field_to_struct_1 (type,
2829 get_identifier (el->sym->result->name),
2830 gfc_sym_type (el->sym->result), &chain);
2831 }
2832
2833 /* Finish off the type. */
2834 gfc_finish_type (type);
2835 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
2836 return type;
2837 }
2838 \f
2839 /* Create a "fn spec" based on the formal arguments;
2840 cf. create_function_arglist. */
2841
2842 static tree
2843 create_fn_spec (gfc_symbol *sym, tree fntype)
2844 {
2845 char spec[150];
2846 size_t spec_len;
2847 gfc_formal_arglist *f;
2848 tree tmp;
2849
2850 memset (&spec, 0, sizeof (spec));
2851 spec[0] = '.';
2852 spec_len = 1;
2853
2854 if (sym->attr.entry_master)
2855 spec[spec_len++] = 'R';
2856 if (gfc_return_by_reference (sym))
2857 {
2858 gfc_symbol *result = sym->result ? sym->result : sym;
2859
2860 if (result->attr.pointer || sym->attr.proc_pointer)
2861 spec[spec_len++] = '.';
2862 else
2863 spec[spec_len++] = 'w';
2864 if (sym->ts.type == BT_CHARACTER)
2865 spec[spec_len++] = 'R';
2866 }
2867
2868 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2869 if (spec_len < sizeof (spec))
2870 {
2871 if (!f->sym || f->sym->attr.pointer || f->sym->attr.target
2872 || f->sym->attr.external || f->sym->attr.cray_pointer
2873 || (f->sym->ts.type == BT_DERIVED
2874 && (f->sym->ts.u.derived->attr.proc_pointer_comp
2875 || f->sym->ts.u.derived->attr.pointer_comp))
2876 || (f->sym->ts.type == BT_CLASS
2877 && (CLASS_DATA (f->sym)->ts.u.derived->attr.proc_pointer_comp
2878 || CLASS_DATA (f->sym)->ts.u.derived->attr.pointer_comp)))
2879 spec[spec_len++] = '.';
2880 else if (f->sym->attr.intent == INTENT_IN)
2881 spec[spec_len++] = 'r';
2882 else if (f->sym)
2883 spec[spec_len++] = 'w';
2884 }
2885
2886 tmp = build_tree_list (NULL_TREE, build_string (spec_len, spec));
2887 tmp = tree_cons (get_identifier ("fn spec"), tmp, TYPE_ATTRIBUTES (fntype));
2888 return build_type_attribute_variant (fntype, tmp);
2889 }
2890
2891
2892 tree
2893 gfc_get_function_type (gfc_symbol * sym)
2894 {
2895 tree type;
2896 vec<tree, va_gc> *typelist = NULL;
2897 gfc_formal_arglist *f;
2898 gfc_symbol *arg;
2899 int alternate_return = 0;
2900 bool is_varargs = true;
2901
2902 /* Make sure this symbol is a function, a subroutine or the main
2903 program. */
2904 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2905 || sym->attr.flavor == FL_PROGRAM);
2906
2907 /* To avoid recursing infinitely on recursive types, we use error_mark_node
2908 so that they can be detected here and handled further down. */
2909 if (sym->backend_decl == NULL)
2910 sym->backend_decl = error_mark_node;
2911 else if (sym->backend_decl == error_mark_node)
2912 goto arg_type_list_done;
2913 else if (sym->attr.proc_pointer)
2914 return TREE_TYPE (TREE_TYPE (sym->backend_decl));
2915 else
2916 return TREE_TYPE (sym->backend_decl);
2917
2918 if (sym->attr.entry_master)
2919 /* Additional parameter for selecting an entry point. */
2920 vec_safe_push (typelist, gfc_array_index_type);
2921
2922 if (sym->result)
2923 arg = sym->result;
2924 else
2925 arg = sym;
2926
2927 if (arg->ts.type == BT_CHARACTER)
2928 gfc_conv_const_charlen (arg->ts.u.cl);
2929
2930 /* Some functions we use an extra parameter for the return value. */
2931 if (gfc_return_by_reference (sym))
2932 {
2933 type = gfc_sym_type (arg);
2934 if (arg->ts.type == BT_COMPLEX
2935 || arg->attr.dimension
2936 || arg->ts.type == BT_CHARACTER)
2937 type = build_reference_type (type);
2938
2939 vec_safe_push (typelist, type);
2940 if (arg->ts.type == BT_CHARACTER)
2941 {
2942 if (!arg->ts.deferred)
2943 /* Transfer by value. */
2944 vec_safe_push (typelist, gfc_charlen_type_node);
2945 else
2946 /* Deferred character lengths are transferred by reference
2947 so that the value can be returned. */
2948 vec_safe_push (typelist, build_pointer_type(gfc_charlen_type_node));
2949 }
2950 }
2951
2952 /* Build the argument types for the function. */
2953 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2954 {
2955 arg = f->sym;
2956 if (arg)
2957 {
2958 /* Evaluate constant character lengths here so that they can be
2959 included in the type. */
2960 if (arg->ts.type == BT_CHARACTER)
2961 gfc_conv_const_charlen (arg->ts.u.cl);
2962
2963 if (arg->attr.flavor == FL_PROCEDURE)
2964 {
2965 type = gfc_get_function_type (arg);
2966 type = build_pointer_type (type);
2967 }
2968 else
2969 type = gfc_sym_type (arg);
2970
2971 /* Parameter Passing Convention
2972
2973 We currently pass all parameters by reference.
2974 Parameters with INTENT(IN) could be passed by value.
2975 The problem arises if a function is called via an implicit
2976 prototype. In this situation the INTENT is not known.
2977 For this reason all parameters to global functions must be
2978 passed by reference. Passing by value would potentially
2979 generate bad code. Worse there would be no way of telling that
2980 this code was bad, except that it would give incorrect results.
2981
2982 Contained procedures could pass by value as these are never
2983 used without an explicit interface, and cannot be passed as
2984 actual parameters for a dummy procedure. */
2985
2986 vec_safe_push (typelist, type);
2987 }
2988 else
2989 {
2990 if (sym->attr.subroutine)
2991 alternate_return = 1;
2992 }
2993 }
2994
2995 /* Add hidden string length parameters. */
2996 for (f = gfc_sym_get_dummy_args (sym); f; f = f->next)
2997 {
2998 arg = f->sym;
2999 if (arg && arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
3000 {
3001 if (!arg->ts.deferred)
3002 /* Transfer by value. */
3003 type = gfc_charlen_type_node;
3004 else
3005 /* Deferred character lengths are transferred by reference
3006 so that the value can be returned. */
3007 type = build_pointer_type (gfc_charlen_type_node);
3008
3009 vec_safe_push (typelist, type);
3010 }
3011 }
3012
3013 if (!vec_safe_is_empty (typelist)
3014 || sym->attr.is_main_program
3015 || sym->attr.if_source != IFSRC_UNKNOWN)
3016 is_varargs = false;
3017
3018 if (sym->backend_decl == error_mark_node)
3019 sym->backend_decl = NULL_TREE;
3020
3021 arg_type_list_done:
3022
3023 if (alternate_return)
3024 type = integer_type_node;
3025 else if (!sym->attr.function || gfc_return_by_reference (sym))
3026 type = void_type_node;
3027 else if (sym->attr.mixed_entry_master)
3028 type = gfc_get_mixed_entry_union (sym->ns);
3029 else if (flag_f2c && sym->ts.type == BT_REAL
3030 && sym->ts.kind == gfc_default_real_kind
3031 && !sym->attr.always_explicit)
3032 {
3033 /* Special case: f2c calling conventions require that (scalar)
3034 default REAL functions return the C type double instead. f2c
3035 compatibility is only an issue with functions that don't
3036 require an explicit interface, as only these could be
3037 implemented in Fortran 77. */
3038 sym->ts.kind = gfc_default_double_kind;
3039 type = gfc_typenode_for_spec (&sym->ts);
3040 sym->ts.kind = gfc_default_real_kind;
3041 }
3042 else if (sym->result && sym->result->attr.proc_pointer)
3043 /* Procedure pointer return values. */
3044 {
3045 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
3046 {
3047 /* Unset proc_pointer as gfc_get_function_type
3048 is called recursively. */
3049 sym->result->attr.proc_pointer = 0;
3050 type = build_pointer_type (gfc_get_function_type (sym->result));
3051 sym->result->attr.proc_pointer = 1;
3052 }
3053 else
3054 type = gfc_sym_type (sym->result);
3055 }
3056 else
3057 type = gfc_sym_type (sym);
3058
3059 if (is_varargs)
3060 type = build_varargs_function_type_vec (type, typelist);
3061 else
3062 type = build_function_type_vec (type, typelist);
3063 type = create_fn_spec (sym, type);
3064
3065 return type;
3066 }
3067 \f
3068 /* Language hooks for middle-end access to type nodes. */
3069
3070 /* Return an integer type with BITS bits of precision,
3071 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3072
3073 tree
3074 gfc_type_for_size (unsigned bits, int unsignedp)
3075 {
3076 if (!unsignedp)
3077 {
3078 int i;
3079 for (i = 0; i <= MAX_INT_KINDS; ++i)
3080 {
3081 tree type = gfc_integer_types[i];
3082 if (type && bits == TYPE_PRECISION (type))
3083 return type;
3084 }
3085
3086 /* Handle TImode as a special case because it is used by some backends
3087 (e.g. ARM) even though it is not available for normal use. */
3088 #if HOST_BITS_PER_WIDE_INT >= 64
3089 if (bits == TYPE_PRECISION (intTI_type_node))
3090 return intTI_type_node;
3091 #endif
3092
3093 if (bits <= TYPE_PRECISION (intQI_type_node))
3094 return intQI_type_node;
3095 if (bits <= TYPE_PRECISION (intHI_type_node))
3096 return intHI_type_node;
3097 if (bits <= TYPE_PRECISION (intSI_type_node))
3098 return intSI_type_node;
3099 if (bits <= TYPE_PRECISION (intDI_type_node))
3100 return intDI_type_node;
3101 if (bits <= TYPE_PRECISION (intTI_type_node))
3102 return intTI_type_node;
3103 }
3104 else
3105 {
3106 if (bits <= TYPE_PRECISION (unsigned_intQI_type_node))
3107 return unsigned_intQI_type_node;
3108 if (bits <= TYPE_PRECISION (unsigned_intHI_type_node))
3109 return unsigned_intHI_type_node;
3110 if (bits <= TYPE_PRECISION (unsigned_intSI_type_node))
3111 return unsigned_intSI_type_node;
3112 if (bits <= TYPE_PRECISION (unsigned_intDI_type_node))
3113 return unsigned_intDI_type_node;
3114 if (bits <= TYPE_PRECISION (unsigned_intTI_type_node))
3115 return unsigned_intTI_type_node;
3116 }
3117
3118 return NULL_TREE;
3119 }
3120
3121 /* Return a data type that has machine mode MODE. If the mode is an
3122 integer, then UNSIGNEDP selects between signed and unsigned types. */
3123
3124 tree
3125 gfc_type_for_mode (machine_mode mode, int unsignedp)
3126 {
3127 int i;
3128 tree *base;
3129 scalar_int_mode int_mode;
3130
3131 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3132 base = gfc_real_types;
3133 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3134 base = gfc_complex_types;
3135 else if (is_a <scalar_int_mode> (mode, &int_mode))
3136 {
3137 tree type = gfc_type_for_size (GET_MODE_PRECISION (int_mode), unsignedp);
3138 return type != NULL_TREE && mode == TYPE_MODE (type) ? type : NULL_TREE;
3139 }
3140 else if (VECTOR_MODE_P (mode))
3141 {
3142 machine_mode inner_mode = GET_MODE_INNER (mode);
3143 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
3144 if (inner_type != NULL_TREE)
3145 return build_vector_type_for_mode (inner_type, mode);
3146 return NULL_TREE;
3147 }
3148 else
3149 return NULL_TREE;
3150
3151 for (i = 0; i <= MAX_REAL_KINDS; ++i)
3152 {
3153 tree type = base[i];
3154 if (type && mode == TYPE_MODE (type))
3155 return type;
3156 }
3157
3158 return NULL_TREE;
3159 }
3160
3161 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3162 in that case. */
3163
3164 bool
3165 gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
3166 {
3167 int rank, dim;
3168 bool indirect = false;
3169 tree etype, ptype, t, base_decl;
3170 tree data_off, dim_off, dtype_off, dim_size, elem_size;
3171 tree lower_suboff, upper_suboff, stride_suboff;
3172
3173 if (! GFC_DESCRIPTOR_TYPE_P (type))
3174 {
3175 if (! POINTER_TYPE_P (type))
3176 return false;
3177 type = TREE_TYPE (type);
3178 if (! GFC_DESCRIPTOR_TYPE_P (type))
3179 return false;
3180 indirect = true;
3181 }
3182
3183 rank = GFC_TYPE_ARRAY_RANK (type);
3184 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
3185 return false;
3186
3187 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
3188 gcc_assert (POINTER_TYPE_P (etype));
3189 etype = TREE_TYPE (etype);
3190
3191 /* If the type is not a scalar coarray. */
3192 if (TREE_CODE (etype) == ARRAY_TYPE)
3193 etype = TREE_TYPE (etype);
3194
3195 /* Can't handle variable sized elements yet. */
3196 if (int_size_in_bytes (etype) <= 0)
3197 return false;
3198 /* Nor non-constant lower bounds in assumed shape arrays. */
3199 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3200 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3201 {
3202 for (dim = 0; dim < rank; dim++)
3203 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
3204 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
3205 return false;
3206 }
3207
3208 memset (info, '\0', sizeof (*info));
3209 info->ndimensions = rank;
3210 info->ordering = array_descr_ordering_column_major;
3211 info->element_type = etype;
3212 ptype = build_pointer_type (gfc_array_index_type);
3213 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
3214 if (!base_decl)
3215 {
3216 base_decl = make_node (DEBUG_EXPR_DECL);
3217 DECL_ARTIFICIAL (base_decl) = 1;
3218 TREE_TYPE (base_decl) = indirect ? build_pointer_type (ptype) : ptype;
3219 SET_DECL_MODE (base_decl, TYPE_MODE (TREE_TYPE (base_decl)));
3220 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
3221 }
3222 info->base_decl = base_decl;
3223 if (indirect)
3224 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
3225
3226 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
3227
3228 gfc_get_descriptor_offsets_for_info (type, &data_off, &dtype_off, &dim_off,
3229 &dim_size, &stride_suboff,
3230 &lower_suboff, &upper_suboff);
3231
3232 t = base_decl;
3233 if (!integer_zerop (data_off))
3234 t = fold_build_pointer_plus (t, data_off);
3235 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
3236 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
3237 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
3238 info->allocated = build2 (NE_EXPR, boolean_type_node,
3239 info->data_location, null_pointer_node);
3240 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
3241 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT)
3242 info->associated = build2 (NE_EXPR, boolean_type_node,
3243 info->data_location, null_pointer_node);
3244 if ((GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK
3245 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_CONT)
3246 && dwarf_version >= 5)
3247 {
3248 rank = 1;
3249 info->ndimensions = 1;
3250 t = base_decl;
3251 if (!integer_zerop (dtype_off))
3252 t = fold_build_pointer_plus (t, dtype_off);
3253 t = build1 (NOP_EXPR, build_pointer_type (gfc_array_index_type), t);
3254 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3255 info->rank = build2 (BIT_AND_EXPR, gfc_array_index_type, t,
3256 build_int_cst (gfc_array_index_type,
3257 GFC_DTYPE_RANK_MASK));
3258 t = build0 (PLACEHOLDER_EXPR, TREE_TYPE (dim_off));
3259 t = size_binop (MULT_EXPR, t, dim_size);
3260 dim_off = build2 (PLUS_EXPR, TREE_TYPE (dim_off), t, dim_off);
3261 }
3262
3263 for (dim = 0; dim < rank; dim++)
3264 {
3265 t = fold_build_pointer_plus (base_decl,
3266 size_binop (PLUS_EXPR,
3267 dim_off, lower_suboff));
3268 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3269 info->dimen[dim].lower_bound = t;
3270 t = fold_build_pointer_plus (base_decl,
3271 size_binop (PLUS_EXPR,
3272 dim_off, upper_suboff));
3273 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3274 info->dimen[dim].upper_bound = t;
3275 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE
3276 || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE_CONT)
3277 {
3278 /* Assumed shape arrays have known lower bounds. */
3279 info->dimen[dim].upper_bound
3280 = build2 (MINUS_EXPR, gfc_array_index_type,
3281 info->dimen[dim].upper_bound,
3282 info->dimen[dim].lower_bound);
3283 info->dimen[dim].lower_bound
3284 = fold_convert (gfc_array_index_type,
3285 GFC_TYPE_ARRAY_LBOUND (type, dim));
3286 info->dimen[dim].upper_bound
3287 = build2 (PLUS_EXPR, gfc_array_index_type,
3288 info->dimen[dim].lower_bound,
3289 info->dimen[dim].upper_bound);
3290 }
3291 t = fold_build_pointer_plus (base_decl,
3292 size_binop (PLUS_EXPR,
3293 dim_off, stride_suboff));
3294 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
3295 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
3296 info->dimen[dim].stride = t;
3297 if (dim + 1 < rank)
3298 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
3299 }
3300
3301 return true;
3302 }
3303
3304
3305 /* Create a type to handle vector subscripts for coarray library calls. It
3306 has the form:
3307 struct caf_vector_t {
3308 size_t nvec; // size of the vector
3309 union {
3310 struct {
3311 void *vector;
3312 int kind;
3313 } v;
3314 struct {
3315 ptrdiff_t lower_bound;
3316 ptrdiff_t upper_bound;
3317 ptrdiff_t stride;
3318 } triplet;
3319 } u;
3320 }
3321 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3322 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3323
3324 tree
3325 gfc_get_caf_vector_type (int dim)
3326 {
3327 static tree vector_types[GFC_MAX_DIMENSIONS];
3328 static tree vec_type = NULL_TREE;
3329 tree triplet_struct_type, vect_struct_type, union_type, tmp, *chain;
3330
3331 if (vector_types[dim-1] != NULL_TREE)
3332 return vector_types[dim-1];
3333
3334 if (vec_type == NULL_TREE)
3335 {
3336 chain = 0;
3337 vect_struct_type = make_node (RECORD_TYPE);
3338 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3339 get_identifier ("vector"),
3340 pvoid_type_node, &chain);
3341 TREE_NO_WARNING (tmp) = 1;
3342 tmp = gfc_add_field_to_struct_1 (vect_struct_type,
3343 get_identifier ("kind"),
3344 integer_type_node, &chain);
3345 TREE_NO_WARNING (tmp) = 1;
3346 gfc_finish_type (vect_struct_type);
3347
3348 chain = 0;
3349 triplet_struct_type = make_node (RECORD_TYPE);
3350 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3351 get_identifier ("lower_bound"),
3352 gfc_array_index_type, &chain);
3353 TREE_NO_WARNING (tmp) = 1;
3354 tmp = gfc_add_field_to_struct_1 (triplet_struct_type,
3355 get_identifier ("upper_bound"),
3356 gfc_array_index_type, &chain);
3357 TREE_NO_WARNING (tmp) = 1;
3358 tmp = gfc_add_field_to_struct_1 (triplet_struct_type, get_identifier ("stride"),
3359 gfc_array_index_type, &chain);
3360 TREE_NO_WARNING (tmp) = 1;
3361 gfc_finish_type (triplet_struct_type);
3362
3363 chain = 0;
3364 union_type = make_node (UNION_TYPE);
3365 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3366 vect_struct_type, &chain);
3367 TREE_NO_WARNING (tmp) = 1;
3368 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("triplet"),
3369 triplet_struct_type, &chain);
3370 TREE_NO_WARNING (tmp) = 1;
3371 gfc_finish_type (union_type);
3372
3373 chain = 0;
3374 vec_type = make_node (RECORD_TYPE);
3375 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("nvec"),
3376 size_type_node, &chain);
3377 TREE_NO_WARNING (tmp) = 1;
3378 tmp = gfc_add_field_to_struct_1 (vec_type, get_identifier ("u"),
3379 union_type, &chain);
3380 TREE_NO_WARNING (tmp) = 1;
3381 gfc_finish_type (vec_type);
3382 TYPE_NAME (vec_type) = get_identifier ("caf_vector_t");
3383 }
3384
3385 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3386 gfc_rank_cst[dim-1]);
3387 vector_types[dim-1] = build_array_type (vec_type, tmp);
3388 return vector_types[dim-1];
3389 }
3390
3391
3392 tree
3393 gfc_get_caf_reference_type ()
3394 {
3395 static tree reference_type = NULL_TREE;
3396 tree c_struct_type, s_struct_type, v_struct_type, union_type, dim_union_type,
3397 a_struct_type, u_union_type, tmp, *chain;
3398
3399 if (reference_type != NULL_TREE)
3400 return reference_type;
3401
3402 chain = 0;
3403 c_struct_type = make_node (RECORD_TYPE);
3404 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3405 get_identifier ("offset"),
3406 gfc_array_index_type, &chain);
3407 TREE_NO_WARNING (tmp) = 1;
3408 tmp = gfc_add_field_to_struct_1 (c_struct_type,
3409 get_identifier ("caf_token_offset"),
3410 gfc_array_index_type, &chain);
3411 TREE_NO_WARNING (tmp) = 1;
3412 gfc_finish_type (c_struct_type);
3413
3414 chain = 0;
3415 s_struct_type = make_node (RECORD_TYPE);
3416 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3417 get_identifier ("start"),
3418 gfc_array_index_type, &chain);
3419 TREE_NO_WARNING (tmp) = 1;
3420 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3421 get_identifier ("end"),
3422 gfc_array_index_type, &chain);
3423 TREE_NO_WARNING (tmp) = 1;
3424 tmp = gfc_add_field_to_struct_1 (s_struct_type,
3425 get_identifier ("stride"),
3426 gfc_array_index_type, &chain);
3427 TREE_NO_WARNING (tmp) = 1;
3428 gfc_finish_type (s_struct_type);
3429
3430 chain = 0;
3431 v_struct_type = make_node (RECORD_TYPE);
3432 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3433 get_identifier ("vector"),
3434 pvoid_type_node, &chain);
3435 TREE_NO_WARNING (tmp) = 1;
3436 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3437 get_identifier ("nvec"),
3438 size_type_node, &chain);
3439 TREE_NO_WARNING (tmp) = 1;
3440 tmp = gfc_add_field_to_struct_1 (v_struct_type,
3441 get_identifier ("kind"),
3442 integer_type_node, &chain);
3443 TREE_NO_WARNING (tmp) = 1;
3444 gfc_finish_type (v_struct_type);
3445
3446 chain = 0;
3447 union_type = make_node (UNION_TYPE);
3448 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("s"),
3449 s_struct_type, &chain);
3450 TREE_NO_WARNING (tmp) = 1;
3451 tmp = gfc_add_field_to_struct_1 (union_type, get_identifier ("v"),
3452 v_struct_type, &chain);
3453 TREE_NO_WARNING (tmp) = 1;
3454 gfc_finish_type (union_type);
3455
3456 tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
3457 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1]);
3458 dim_union_type = build_array_type (union_type, tmp);
3459
3460 chain = 0;
3461 a_struct_type = make_node (RECORD_TYPE);
3462 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("mode"),
3463 build_array_type (unsigned_char_type_node,
3464 build_range_type (gfc_array_index_type,
3465 gfc_index_zero_node,
3466 gfc_rank_cst[GFC_MAX_DIMENSIONS - 1])),
3467 &chain);
3468 TREE_NO_WARNING (tmp) = 1;
3469 tmp = gfc_add_field_to_struct_1 (a_struct_type,
3470 get_identifier ("static_array_type"),
3471 integer_type_node, &chain);
3472 TREE_NO_WARNING (tmp) = 1;
3473 tmp = gfc_add_field_to_struct_1 (a_struct_type, get_identifier ("dim"),
3474 dim_union_type, &chain);
3475 TREE_NO_WARNING (tmp) = 1;
3476 gfc_finish_type (a_struct_type);
3477
3478 chain = 0;
3479 u_union_type = make_node (UNION_TYPE);
3480 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("c"),
3481 c_struct_type, &chain);
3482 TREE_NO_WARNING (tmp) = 1;
3483 tmp = gfc_add_field_to_struct_1 (u_union_type, get_identifier ("a"),
3484 a_struct_type, &chain);
3485 TREE_NO_WARNING (tmp) = 1;
3486 gfc_finish_type (u_union_type);
3487
3488 chain = 0;
3489 reference_type = make_node (RECORD_TYPE);
3490 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("next"),
3491 build_pointer_type (reference_type), &chain);
3492 TREE_NO_WARNING (tmp) = 1;
3493 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("type"),
3494 integer_type_node, &chain);
3495 TREE_NO_WARNING (tmp) = 1;
3496 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("item_size"),
3497 size_type_node, &chain);
3498 TREE_NO_WARNING (tmp) = 1;
3499 tmp = gfc_add_field_to_struct_1 (reference_type, get_identifier ("u"),
3500 u_union_type, &chain);
3501 TREE_NO_WARNING (tmp) = 1;
3502 gfc_finish_type (reference_type);
3503 TYPE_NAME (reference_type) = get_identifier ("caf_reference_t");
3504
3505 return reference_type;
3506 }
3507
3508 #include "gt-fortran-trans-types.h"