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