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