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