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