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