1 /* Backend support for Fortran 95 basic types and derived types.
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4 and Steven Bosscher <s.bosscher@student.tudelft.nl>
6 This file is part of GCC.
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
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
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/>. */
22 /* trans-types.c -- gfortran backend types */
26 #include "coretypes.h"
31 #include "stringpool.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "langhooks.h" /* For iso-c-bindings.def. */
35 #include "toplev.h" /* For rest_of_decl_compilation. */
36 #include "trans-types.h"
37 #include "trans-const.h"
38 #include "trans-array.h"
39 #include "dwarf2out.h" /* For struct array_descr_info. */
43 #if (GFC_MAX_DIMENSIONS < 10)
44 #define GFC_RANK_DIGITS 1
45 #define GFC_RANK_PRINTF_FORMAT "%01d"
46 #elif (GFC_MAX_DIMENSIONS < 100)
47 #define GFC_RANK_DIGITS 2
48 #define GFC_RANK_PRINTF_FORMAT "%02d"
50 #error If you really need >99 dimensions, continue the sequence above...
53 /* array of structs so we don't have to worry about xmalloc or free */
54 CInteropKind_t c_interop_kinds_table
[ISOCBINDING_NUMBER
];
56 tree gfc_array_index_type
;
57 tree gfc_array_range_type
;
58 tree gfc_character1_type_node
;
60 tree prvoid_type_node
;
61 tree ppvoid_type_node
;
65 tree logical_type_node
;
66 tree logical_true_node
;
67 tree logical_false_node
;
68 tree gfc_charlen_type_node
;
70 tree gfc_float128_type_node
= NULL_TREE
;
71 tree gfc_complex_float128_type_node
= NULL_TREE
;
73 bool gfc_real16_is_float128
= false;
75 static GTY(()) tree gfc_desc_dim_type
;
76 static GTY(()) tree gfc_max_array_element_size
;
77 static GTY(()) tree gfc_array_descriptor_base
[2 * (GFC_MAX_DIMENSIONS
+1)];
78 static GTY(()) tree gfc_array_descriptor_base_caf
[2 * (GFC_MAX_DIMENSIONS
+1)];
80 /* Arrays for all integral and real kinds. We'll fill this in at runtime
81 after the target has a chance to process command-line options. */
83 #define MAX_INT_KINDS 5
84 gfc_integer_info gfc_integer_kinds
[MAX_INT_KINDS
+ 1];
85 gfc_logical_info gfc_logical_kinds
[MAX_INT_KINDS
+ 1];
86 static GTY(()) tree gfc_integer_types
[MAX_INT_KINDS
+ 1];
87 static GTY(()) tree gfc_logical_types
[MAX_INT_KINDS
+ 1];
89 #define MAX_REAL_KINDS 5
90 gfc_real_info gfc_real_kinds
[MAX_REAL_KINDS
+ 1];
91 static GTY(()) tree gfc_real_types
[MAX_REAL_KINDS
+ 1];
92 static GTY(()) tree gfc_complex_types
[MAX_REAL_KINDS
+ 1];
94 #define MAX_CHARACTER_KINDS 2
95 gfc_character_info gfc_character_kinds
[MAX_CHARACTER_KINDS
+ 1];
96 static GTY(()) tree gfc_character_types
[MAX_CHARACTER_KINDS
+ 1];
97 static GTY(()) tree gfc_pcharacter_types
[MAX_CHARACTER_KINDS
+ 1];
99 static tree
gfc_add_field_to_struct_1 (tree
, tree
, tree
, tree
**);
101 /* The integer kind to use for array indices. This will be set to the
102 proper value based on target information from the backend. */
104 int gfc_index_integer_kind
;
106 /* The default kinds of the various types. */
108 int gfc_default_integer_kind
;
109 int gfc_max_integer_kind
;
110 int gfc_default_real_kind
;
111 int gfc_default_double_kind
;
112 int gfc_default_character_kind
;
113 int gfc_default_logical_kind
;
114 int gfc_default_complex_kind
;
116 int gfc_atomic_int_kind
;
117 int gfc_atomic_logical_kind
;
119 /* The kind size used for record offsets. If the target system supports
120 kind=8, this will be set to 8, otherwise it is set to 4. */
123 /* The integer kind used to store character lengths. */
124 int gfc_charlen_int_kind
;
126 /* Kind of internal integer for storing object sizes. */
129 /* The size of the numeric storage unit and character storage unit. */
130 int gfc_numeric_storage_size
;
131 int gfc_character_storage_size
;
133 tree dtype_type_node
= NULL_TREE
;
136 /* Build the dtype_type_node if necessary. */
137 tree
get_dtype_type_node (void)
141 tree
*dtype_chain
= NULL
;
143 if (dtype_type_node
== NULL_TREE
)
145 dtype_node
= make_node (RECORD_TYPE
);
146 TYPE_NAME (dtype_node
) = get_identifier ("dtype_type");
147 TYPE_NAMELESS (dtype_node
) = 1;
148 field
= gfc_add_field_to_struct_1 (dtype_node
,
149 get_identifier ("elem_len"),
150 size_type_node
, &dtype_chain
);
151 TREE_NO_WARNING (field
) = 1;
152 field
= gfc_add_field_to_struct_1 (dtype_node
,
153 get_identifier ("version"),
154 integer_type_node
, &dtype_chain
);
155 TREE_NO_WARNING (field
) = 1;
156 field
= gfc_add_field_to_struct_1 (dtype_node
,
157 get_identifier ("rank"),
158 signed_char_type_node
, &dtype_chain
);
159 TREE_NO_WARNING (field
) = 1;
160 field
= gfc_add_field_to_struct_1 (dtype_node
,
161 get_identifier ("type"),
162 signed_char_type_node
, &dtype_chain
);
163 TREE_NO_WARNING (field
) = 1;
164 field
= gfc_add_field_to_struct_1 (dtype_node
,
165 get_identifier ("attribute"),
166 short_integer_type_node
, &dtype_chain
);
167 TREE_NO_WARNING (field
) = 1;
168 gfc_finish_type (dtype_node
);
169 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (dtype_node
)) = 1;
170 dtype_type_node
= dtype_node
;
172 return dtype_type_node
;
176 gfc_check_any_c_kind (gfc_typespec
*ts
)
180 for (i
= 0; i
< ISOCBINDING_NUMBER
; i
++)
182 /* Check for any C interoperable kind for the given type/kind in ts.
183 This can be used after verify_c_interop to make sure that the
184 Fortran kind being used exists in at least some form for C. */
185 if (c_interop_kinds_table
[i
].f90_type
== ts
->type
&&
186 c_interop_kinds_table
[i
].value
== ts
->kind
)
195 get_real_kind_from_node (tree type
)
199 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
200 if (gfc_real_kinds
[i
].mode_precision
== TYPE_PRECISION (type
))
201 return gfc_real_kinds
[i
].kind
;
207 get_int_kind_from_node (tree type
)
214 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
215 if (gfc_integer_kinds
[i
].bit_size
== TYPE_PRECISION (type
))
216 return gfc_integer_kinds
[i
].kind
;
221 /* Return a typenode for the "standard" C type with a given name. */
223 get_typenode_from_name (const char *name
)
225 if (name
== NULL
|| *name
== '\0')
228 if (strcmp (name
, "char") == 0)
229 return char_type_node
;
230 if (strcmp (name
, "unsigned char") == 0)
231 return unsigned_char_type_node
;
232 if (strcmp (name
, "signed char") == 0)
233 return signed_char_type_node
;
235 if (strcmp (name
, "short int") == 0)
236 return short_integer_type_node
;
237 if (strcmp (name
, "short unsigned int") == 0)
238 return short_unsigned_type_node
;
240 if (strcmp (name
, "int") == 0)
241 return integer_type_node
;
242 if (strcmp (name
, "unsigned int") == 0)
243 return unsigned_type_node
;
245 if (strcmp (name
, "long int") == 0)
246 return long_integer_type_node
;
247 if (strcmp (name
, "long unsigned int") == 0)
248 return long_unsigned_type_node
;
250 if (strcmp (name
, "long long int") == 0)
251 return long_long_integer_type_node
;
252 if (strcmp (name
, "long long unsigned int") == 0)
253 return long_long_unsigned_type_node
;
259 get_int_kind_from_name (const char *name
)
261 return get_int_kind_from_node (get_typenode_from_name (name
));
265 /* Get the kind number corresponding to an integer of given size,
266 following the required return values for ISO_FORTRAN_ENV INT* constants:
267 -2 is returned if we support a kind of larger size, -1 otherwise. */
269 gfc_get_int_kind_from_width_isofortranenv (int size
)
273 /* Look for a kind with matching storage size. */
274 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
275 if (gfc_integer_kinds
[i
].bit_size
== size
)
276 return gfc_integer_kinds
[i
].kind
;
278 /* Look for a kind with larger storage size. */
279 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
280 if (gfc_integer_kinds
[i
].bit_size
> size
)
287 /* Get the kind number corresponding to a real of a given storage size.
288 If two real's have the same storage size, then choose the real with
289 the largest precision. If a kind type is unavailable and a real
290 exists with wider storage, then return -2; otherwise, return -1. */
293 gfc_get_real_kind_from_width_isofortranenv (int size
)
302 /* Look for a kind with matching storage size. */
303 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
304 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds
[i
].kind
)) == size
)
306 if (gfc_real_kinds
[i
].digits
> digits
)
308 digits
= gfc_real_kinds
[i
].digits
;
309 kind
= gfc_real_kinds
[i
].kind
;
316 /* Look for a kind with larger storage size. */
317 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
318 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds
[i
].kind
)) > size
)
327 get_int_kind_from_width (int size
)
331 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
332 if (gfc_integer_kinds
[i
].bit_size
== size
)
333 return gfc_integer_kinds
[i
].kind
;
339 get_int_kind_from_minimal_width (int size
)
343 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
344 if (gfc_integer_kinds
[i
].bit_size
>= size
)
345 return gfc_integer_kinds
[i
].kind
;
351 /* Generate the CInteropKind_t objects for the C interoperable
355 gfc_init_c_interop_kinds (void)
359 /* init all pointers in the list to NULL */
360 for (i
= 0; i
< ISOCBINDING_NUMBER
; i
++)
362 /* Initialize the name and value fields. */
363 c_interop_kinds_table
[i
].name
[0] = '\0';
364 c_interop_kinds_table
[i
].value
= -100;
365 c_interop_kinds_table
[i
].f90_type
= BT_UNKNOWN
;
368 #define NAMED_INTCST(a,b,c,d) \
369 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
370 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
371 c_interop_kinds_table[a].value = c;
372 #define NAMED_REALCST(a,b,c,d) \
373 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
374 c_interop_kinds_table[a].f90_type = BT_REAL; \
375 c_interop_kinds_table[a].value = c;
376 #define NAMED_CMPXCST(a,b,c,d) \
377 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
378 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
379 c_interop_kinds_table[a].value = c;
380 #define NAMED_LOGCST(a,b,c) \
381 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
382 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
383 c_interop_kinds_table[a].value = c;
384 #define NAMED_CHARKNDCST(a,b,c) \
385 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
386 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
387 c_interop_kinds_table[a].value = c;
388 #define NAMED_CHARCST(a,b,c) \
389 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
390 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
391 c_interop_kinds_table[a].value = c;
392 #define DERIVED_TYPE(a,b,c) \
393 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
394 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
395 c_interop_kinds_table[a].value = c;
396 #define NAMED_FUNCTION(a,b,c,d) \
397 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
398 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
399 c_interop_kinds_table[a].value = c;
400 #define NAMED_SUBROUTINE(a,b,c,d) \
401 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
402 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
403 c_interop_kinds_table[a].value = c;
404 #include "iso-c-binding.def"
408 /* Query the target to determine which machine modes are available for
409 computation. Choose KIND numbers for them. */
412 gfc_init_kinds (void)
414 opt_scalar_int_mode int_mode_iter
;
415 opt_scalar_float_mode float_mode_iter
;
416 int i_index
, r_index
, kind
;
417 bool saw_i4
= false, saw_i8
= false;
418 bool saw_r4
= false, saw_r8
= false, saw_r10
= false, saw_r16
= false;
421 FOR_EACH_MODE_IN_CLASS (int_mode_iter
, MODE_INT
)
423 scalar_int_mode mode
= int_mode_iter
.require ();
426 if (!targetm
.scalar_mode_supported_p (mode
))
429 /* The middle end doesn't support constants larger than 2*HWI.
430 Perhaps the target hook shouldn't have accepted these either,
431 but just to be safe... */
432 bitsize
= GET_MODE_BITSIZE (mode
);
433 if (bitsize
> 2*HOST_BITS_PER_WIDE_INT
)
436 gcc_assert (i_index
!= MAX_INT_KINDS
);
438 /* Let the kind equal the bit size divided by 8. This insulates the
439 programmer from the underlying byte size. */
447 gfc_integer_kinds
[i_index
].kind
= kind
;
448 gfc_integer_kinds
[i_index
].radix
= 2;
449 gfc_integer_kinds
[i_index
].digits
= bitsize
- 1;
450 gfc_integer_kinds
[i_index
].bit_size
= bitsize
;
452 gfc_logical_kinds
[i_index
].kind
= kind
;
453 gfc_logical_kinds
[i_index
].bit_size
= bitsize
;
458 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
459 used for large file access. */
466 /* If we do not at least have kind = 4, everything is pointless. */
469 /* Set the maximum integer kind. Used with at least BOZ constants. */
470 gfc_max_integer_kind
= gfc_integer_kinds
[i_index
- 1].kind
;
473 FOR_EACH_MODE_IN_CLASS (float_mode_iter
, MODE_FLOAT
)
475 scalar_float_mode mode
= float_mode_iter
.require ();
476 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
481 if (!targetm
.scalar_mode_supported_p (mode
))
484 /* Only let float, double, long double and __float128 go through.
485 Runtime support for others is not provided, so they would be
487 if (!targetm
.libgcc_floating_mode_supported_p (mode
))
489 if (mode
!= TYPE_MODE (float_type_node
)
490 && (mode
!= TYPE_MODE (double_type_node
))
491 && (mode
!= TYPE_MODE (long_double_type_node
))
492 #if defined(HAVE_TFmode) && defined(ENABLE_LIBQUADMATH_SUPPORT)
498 /* Let the kind equal the precision divided by 8, rounding up. Again,
499 this insulates the programmer from the underlying byte size.
501 Also, it effectively deals with IEEE extended formats. There, the
502 total size of the type may equal 16, but it's got 6 bytes of padding
503 and the increased size can get in the way of a real IEEE quad format
504 which may also be supported by the target.
506 We round up so as to handle IA-64 __floatreg (RFmode), which is an
507 82 bit type. Not to be confused with __float80 (XFmode), which is
508 an 80 bit type also supported by IA-64. So XFmode should come out
509 to be kind=10, and RFmode should come out to be kind=11. Egads. */
511 kind
= (GET_MODE_PRECISION (mode
) + 7) / 8;
522 /* Careful we don't stumble a weird internal mode. */
523 gcc_assert (r_index
<= 0 || gfc_real_kinds
[r_index
-1].kind
!= kind
);
524 /* Or have too many modes for the allocated space. */
525 gcc_assert (r_index
!= MAX_REAL_KINDS
);
527 gfc_real_kinds
[r_index
].kind
= kind
;
528 gfc_real_kinds
[r_index
].radix
= fmt
->b
;
529 gfc_real_kinds
[r_index
].digits
= fmt
->p
;
530 gfc_real_kinds
[r_index
].min_exponent
= fmt
->emin
;
531 gfc_real_kinds
[r_index
].max_exponent
= fmt
->emax
;
532 if (fmt
->pnan
< fmt
->p
)
533 /* This is an IBM extended double format (or the MIPS variant)
534 made up of two IEEE doubles. The value of the long double is
535 the sum of the values of the two parts. The most significant
536 part is required to be the value of the long double rounded
537 to the nearest double. If we use emax of 1024 then we can't
538 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
539 rounding will make the most significant part overflow. */
540 gfc_real_kinds
[r_index
].max_exponent
= fmt
->emax
- 1;
541 gfc_real_kinds
[r_index
].mode_precision
= GET_MODE_PRECISION (mode
);
545 /* Choose the default integer kind. We choose 4 unless the user directs us
546 otherwise. Even if the user specified that the default integer kind is 8,
547 the numeric storage size is not 64 bits. In this case, a warning will be
548 issued when NUMERIC_STORAGE_SIZE is used. Set NUMERIC_STORAGE_SIZE to 32. */
550 gfc_numeric_storage_size
= 4 * 8;
552 if (flag_default_integer
)
555 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
556 "%<-fdefault-integer-8%> option");
558 gfc_default_integer_kind
= 8;
561 else if (flag_integer4_kind
== 8)
564 gfc_fatal_error ("INTEGER(KIND=8) is not available for "
565 "%<-finteger-4-integer-8%> option");
567 gfc_default_integer_kind
= 8;
571 gfc_default_integer_kind
= 4;
575 gfc_default_integer_kind
= gfc_integer_kinds
[i_index
- 1].kind
;
576 gfc_numeric_storage_size
= gfc_integer_kinds
[i_index
- 1].bit_size
;
579 /* Choose the default real kind. Again, we choose 4 when possible. */
580 if (flag_default_real_8
)
583 gfc_fatal_error ("REAL(KIND=8) is not available for "
584 "%<-fdefault-real-8%> option");
586 gfc_default_real_kind
= 8;
588 else if (flag_default_real_10
)
591 gfc_fatal_error ("REAL(KIND=10) is not available for "
592 "%<-fdefault-real-10%> option");
594 gfc_default_real_kind
= 10;
596 else if (flag_default_real_16
)
599 gfc_fatal_error ("REAL(KIND=16) is not available for "
600 "%<-fdefault-real-16%> option");
602 gfc_default_real_kind
= 16;
604 else if (flag_real4_kind
== 8)
607 gfc_fatal_error ("REAL(KIND=8) is not available for %<-freal-4-real-8%> "
610 gfc_default_real_kind
= 8;
612 else if (flag_real4_kind
== 10)
615 gfc_fatal_error ("REAL(KIND=10) is not available for "
616 "%<-freal-4-real-10%> option");
618 gfc_default_real_kind
= 10;
620 else if (flag_real4_kind
== 16)
623 gfc_fatal_error ("REAL(KIND=16) is not available for "
624 "%<-freal-4-real-16%> option");
626 gfc_default_real_kind
= 16;
629 gfc_default_real_kind
= 4;
631 gfc_default_real_kind
= gfc_real_kinds
[0].kind
;
633 /* Choose the default double kind. If -fdefault-real and -fdefault-double
634 are specified, we use kind=8, if it's available. If -fdefault-real is
635 specified without -fdefault-double, we use kind=16, if it's available.
636 Otherwise we do not change anything. */
637 if (flag_default_double
&& saw_r8
)
638 gfc_default_double_kind
= 8;
639 else if (flag_default_real_8
|| flag_default_real_10
|| flag_default_real_16
)
641 /* Use largest available kind. */
643 gfc_default_double_kind
= 16;
645 gfc_default_double_kind
= 10;
647 gfc_default_double_kind
= 8;
649 gfc_default_double_kind
= gfc_default_real_kind
;
651 else if (flag_real8_kind
== 4)
654 gfc_fatal_error ("REAL(KIND=4) is not available for "
655 "%<-freal-8-real-4%> option");
657 gfc_default_double_kind
= 4;
659 else if (flag_real8_kind
== 10 )
662 gfc_fatal_error ("REAL(KIND=10) is not available for "
663 "%<-freal-8-real-10%> option");
665 gfc_default_double_kind
= 10;
667 else if (flag_real8_kind
== 16 )
670 gfc_fatal_error ("REAL(KIND=10) is not available for "
671 "%<-freal-8-real-16%> option");
673 gfc_default_double_kind
= 16;
675 else if (saw_r4
&& saw_r8
)
676 gfc_default_double_kind
= 8;
679 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
680 real ... occupies two contiguous numeric storage units.
682 Therefore we must be supplied a kind twice as large as we chose
683 for single precision. There are loopholes, in that double
684 precision must *occupy* two storage units, though it doesn't have
685 to *use* two storage units. Which means that you can make this
686 kind artificially wide by padding it. But at present there are
687 no GCC targets for which a two-word type does not exist, so we
688 just let gfc_validate_kind abort and tell us if something breaks. */
690 gfc_default_double_kind
691 = gfc_validate_kind (BT_REAL
, gfc_default_real_kind
* 2, false);
694 /* The default logical kind is constrained to be the same as the
695 default integer kind. Similarly with complex and real. */
696 gfc_default_logical_kind
= gfc_default_integer_kind
;
697 gfc_default_complex_kind
= gfc_default_real_kind
;
699 /* We only have two character kinds: ASCII and UCS-4.
700 ASCII corresponds to a 8-bit integer type, if one is available.
701 UCS-4 corresponds to a 32-bit integer type, if one is available. */
703 if ((kind
= get_int_kind_from_width (8)) > 0)
705 gfc_character_kinds
[i_index
].kind
= kind
;
706 gfc_character_kinds
[i_index
].bit_size
= 8;
707 gfc_character_kinds
[i_index
].name
= "ascii";
710 if ((kind
= get_int_kind_from_width (32)) > 0)
712 gfc_character_kinds
[i_index
].kind
= kind
;
713 gfc_character_kinds
[i_index
].bit_size
= 32;
714 gfc_character_kinds
[i_index
].name
= "iso_10646";
718 /* Choose the smallest integer kind for our default character. */
719 gfc_default_character_kind
= gfc_character_kinds
[0].kind
;
720 gfc_character_storage_size
= gfc_default_character_kind
* 8;
722 gfc_index_integer_kind
= get_int_kind_from_name (PTRDIFF_TYPE
);
724 /* Pick a kind the same size as the C "int" type. */
725 gfc_c_int_kind
= INT_TYPE_SIZE
/ 8;
727 /* Choose atomic kinds to match C's int. */
728 gfc_atomic_int_kind
= gfc_c_int_kind
;
729 gfc_atomic_logical_kind
= gfc_c_int_kind
;
733 /* Make sure that a valid kind is present. Returns an index into the
734 associated kinds array, -1 if the kind is not present. */
737 validate_integer (int kind
)
741 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
742 if (gfc_integer_kinds
[i
].kind
== kind
)
749 validate_real (int kind
)
753 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
754 if (gfc_real_kinds
[i
].kind
== kind
)
761 validate_logical (int kind
)
765 for (i
= 0; gfc_logical_kinds
[i
].kind
; i
++)
766 if (gfc_logical_kinds
[i
].kind
== kind
)
773 validate_character (int kind
)
777 for (i
= 0; gfc_character_kinds
[i
].kind
; i
++)
778 if (gfc_character_kinds
[i
].kind
== kind
)
784 /* Validate a kind given a basic type. The return value is the same
785 for the child functions, with -1 indicating nonexistence of the
786 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
789 gfc_validate_kind (bt type
, int kind
, bool may_fail
)
795 case BT_REAL
: /* Fall through */
797 rc
= validate_real (kind
);
800 rc
= validate_integer (kind
);
803 rc
= validate_logical (kind
);
806 rc
= validate_character (kind
);
810 gfc_internal_error ("gfc_validate_kind(): Got bad type");
813 if (rc
< 0 && !may_fail
)
814 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
820 /* Four subroutines of gfc_init_types. Create type nodes for the given kind.
821 Reuse common type nodes where possible. Recognize if the kind matches up
822 with a C type. This will be used later in determining which routines may
823 be scarfed from libm. */
826 gfc_build_int_type (gfc_integer_info
*info
)
828 int mode_precision
= info
->bit_size
;
830 if (mode_precision
== CHAR_TYPE_SIZE
)
832 if (mode_precision
== SHORT_TYPE_SIZE
)
834 if (mode_precision
== INT_TYPE_SIZE
)
836 if (mode_precision
== LONG_TYPE_SIZE
)
838 if (mode_precision
== LONG_LONG_TYPE_SIZE
)
839 info
->c_long_long
= 1;
841 if (TYPE_PRECISION (intQI_type_node
) == mode_precision
)
842 return intQI_type_node
;
843 if (TYPE_PRECISION (intHI_type_node
) == mode_precision
)
844 return intHI_type_node
;
845 if (TYPE_PRECISION (intSI_type_node
) == mode_precision
)
846 return intSI_type_node
;
847 if (TYPE_PRECISION (intDI_type_node
) == mode_precision
)
848 return intDI_type_node
;
849 if (TYPE_PRECISION (intTI_type_node
) == mode_precision
)
850 return intTI_type_node
;
852 return make_signed_type (mode_precision
);
856 gfc_build_uint_type (int size
)
858 if (size
== CHAR_TYPE_SIZE
)
859 return unsigned_char_type_node
;
860 if (size
== SHORT_TYPE_SIZE
)
861 return short_unsigned_type_node
;
862 if (size
== INT_TYPE_SIZE
)
863 return unsigned_type_node
;
864 if (size
== LONG_TYPE_SIZE
)
865 return long_unsigned_type_node
;
866 if (size
== LONG_LONG_TYPE_SIZE
)
867 return long_long_unsigned_type_node
;
869 return make_unsigned_type (size
);
874 gfc_build_real_type (gfc_real_info
*info
)
876 int mode_precision
= info
->mode_precision
;
879 if (mode_precision
== FLOAT_TYPE_SIZE
)
881 if (mode_precision
== DOUBLE_TYPE_SIZE
)
883 if (mode_precision
== LONG_DOUBLE_TYPE_SIZE
)
884 info
->c_long_double
= 1;
885 if (mode_precision
!= LONG_DOUBLE_TYPE_SIZE
&& mode_precision
== 128)
887 info
->c_float128
= 1;
888 gfc_real16_is_float128
= true;
891 if (TYPE_PRECISION (float_type_node
) == mode_precision
)
892 return float_type_node
;
893 if (TYPE_PRECISION (double_type_node
) == mode_precision
)
894 return double_type_node
;
895 if (TYPE_PRECISION (long_double_type_node
) == mode_precision
)
896 return long_double_type_node
;
898 new_type
= make_node (REAL_TYPE
);
899 TYPE_PRECISION (new_type
) = mode_precision
;
900 layout_type (new_type
);
905 gfc_build_complex_type (tree scalar_type
)
909 if (scalar_type
== NULL
)
911 if (scalar_type
== float_type_node
)
912 return complex_float_type_node
;
913 if (scalar_type
== double_type_node
)
914 return complex_double_type_node
;
915 if (scalar_type
== long_double_type_node
)
916 return complex_long_double_type_node
;
918 new_type
= make_node (COMPLEX_TYPE
);
919 TREE_TYPE (new_type
) = scalar_type
;
920 layout_type (new_type
);
925 gfc_build_logical_type (gfc_logical_info
*info
)
927 int bit_size
= info
->bit_size
;
930 if (bit_size
== BOOL_TYPE_SIZE
)
933 return boolean_type_node
;
936 new_type
= make_unsigned_type (bit_size
);
937 TREE_SET_CODE (new_type
, BOOLEAN_TYPE
);
938 TYPE_MAX_VALUE (new_type
) = build_int_cst (new_type
, 1);
939 TYPE_PRECISION (new_type
) = 1;
945 /* Create the backend type nodes. We map them to their
946 equivalent C type, at least for now. We also give
947 names to the types here, and we push them in the
948 global binding level context.*/
951 gfc_init_types (void)
958 /* Create and name the types. */
959 #define PUSH_TYPE(name, node) \
960 pushdecl (build_decl (input_location, \
961 TYPE_DECL, get_identifier (name), node))
963 for (index
= 0; gfc_integer_kinds
[index
].kind
!= 0; ++index
)
965 type
= gfc_build_int_type (&gfc_integer_kinds
[index
]);
966 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
967 if (TYPE_STRING_FLAG (type
))
968 type
= make_signed_type (gfc_integer_kinds
[index
].bit_size
);
969 gfc_integer_types
[index
] = type
;
970 snprintf (name_buf
, sizeof(name_buf
), "integer(kind=%d)",
971 gfc_integer_kinds
[index
].kind
);
972 PUSH_TYPE (name_buf
, type
);
975 for (index
= 0; gfc_logical_kinds
[index
].kind
!= 0; ++index
)
977 type
= gfc_build_logical_type (&gfc_logical_kinds
[index
]);
978 gfc_logical_types
[index
] = type
;
979 snprintf (name_buf
, sizeof(name_buf
), "logical(kind=%d)",
980 gfc_logical_kinds
[index
].kind
);
981 PUSH_TYPE (name_buf
, type
);
984 for (index
= 0; gfc_real_kinds
[index
].kind
!= 0; index
++)
986 type
= gfc_build_real_type (&gfc_real_kinds
[index
]);
987 gfc_real_types
[index
] = type
;
988 snprintf (name_buf
, sizeof(name_buf
), "real(kind=%d)",
989 gfc_real_kinds
[index
].kind
);
990 PUSH_TYPE (name_buf
, type
);
992 if (gfc_real_kinds
[index
].c_float128
)
993 gfc_float128_type_node
= type
;
995 type
= gfc_build_complex_type (type
);
996 gfc_complex_types
[index
] = type
;
997 snprintf (name_buf
, sizeof(name_buf
), "complex(kind=%d)",
998 gfc_real_kinds
[index
].kind
);
999 PUSH_TYPE (name_buf
, type
);
1001 if (gfc_real_kinds
[index
].c_float128
)
1002 gfc_complex_float128_type_node
= type
;
1005 for (index
= 0; gfc_character_kinds
[index
].kind
!= 0; ++index
)
1007 type
= gfc_build_uint_type (gfc_character_kinds
[index
].bit_size
);
1008 type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
1009 snprintf (name_buf
, sizeof(name_buf
), "character(kind=%d)",
1010 gfc_character_kinds
[index
].kind
);
1011 PUSH_TYPE (name_buf
, type
);
1012 gfc_character_types
[index
] = type
;
1013 gfc_pcharacter_types
[index
] = build_pointer_type (type
);
1015 gfc_character1_type_node
= gfc_character_types
[0];
1017 PUSH_TYPE ("byte", unsigned_char_type_node
);
1018 PUSH_TYPE ("void", void_type_node
);
1020 /* DBX debugging output gets upset if these aren't set. */
1021 if (!TYPE_NAME (integer_type_node
))
1022 PUSH_TYPE ("c_integer", integer_type_node
);
1023 if (!TYPE_NAME (char_type_node
))
1024 PUSH_TYPE ("c_char", char_type_node
);
1028 pvoid_type_node
= build_pointer_type (void_type_node
);
1029 prvoid_type_node
= build_qualified_type (pvoid_type_node
, TYPE_QUAL_RESTRICT
);
1030 ppvoid_type_node
= build_pointer_type (pvoid_type_node
);
1031 pchar_type_node
= build_pointer_type (gfc_character1_type_node
);
1033 = build_pointer_type (build_function_type_list (void_type_node
, NULL_TREE
));
1035 gfc_array_index_type
= gfc_get_int_type (gfc_index_integer_kind
);
1036 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
1037 since this function is called before gfc_init_constants. */
1038 gfc_array_range_type
1039 = build_range_type (gfc_array_index_type
,
1040 build_int_cst (gfc_array_index_type
, 0),
1043 /* The maximum array element size that can be handled is determined
1044 by the number of bits available to store this field in the array
1047 n
= TYPE_PRECISION (size_type_node
);
1048 gfc_max_array_element_size
1049 = wide_int_to_tree (size_type_node
,
1050 wi::mask (n
, UNSIGNED
,
1051 TYPE_PRECISION (size_type_node
)));
1053 logical_type_node
= gfc_get_logical_type (gfc_default_logical_kind
);
1054 logical_true_node
= build_int_cst (logical_type_node
, 1);
1055 logical_false_node
= build_int_cst (logical_type_node
, 0);
1057 /* Character lengths are of type size_t, except signed. */
1058 gfc_charlen_int_kind
= get_int_kind_from_node (size_type_node
);
1059 gfc_charlen_type_node
= gfc_get_int_type (gfc_charlen_int_kind
);
1061 /* Fortran kind number of size_type_node (size_t). This is used for
1062 the _size member in vtables. */
1063 gfc_size_kind
= get_int_kind_from_node (size_type_node
);
1066 /* Get the type node for the given type and kind. */
1069 gfc_get_int_type (int kind
)
1071 int index
= gfc_validate_kind (BT_INTEGER
, kind
, true);
1072 return index
< 0 ? 0 : gfc_integer_types
[index
];
1076 gfc_get_real_type (int kind
)
1078 int index
= gfc_validate_kind (BT_REAL
, kind
, true);
1079 return index
< 0 ? 0 : gfc_real_types
[index
];
1083 gfc_get_complex_type (int kind
)
1085 int index
= gfc_validate_kind (BT_COMPLEX
, kind
, true);
1086 return index
< 0 ? 0 : gfc_complex_types
[index
];
1090 gfc_get_logical_type (int kind
)
1092 int index
= gfc_validate_kind (BT_LOGICAL
, kind
, true);
1093 return index
< 0 ? 0 : gfc_logical_types
[index
];
1097 gfc_get_char_type (int kind
)
1099 int index
= gfc_validate_kind (BT_CHARACTER
, kind
, true);
1100 return index
< 0 ? 0 : gfc_character_types
[index
];
1104 gfc_get_pchar_type (int kind
)
1106 int index
= gfc_validate_kind (BT_CHARACTER
, kind
, true);
1107 return index
< 0 ? 0 : gfc_pcharacter_types
[index
];
1111 /* Create a character type with the given kind and length. */
1114 gfc_get_character_type_len_for_eltype (tree eltype
, tree len
)
1118 bounds
= build_range_type (gfc_charlen_type_node
, gfc_index_one_node
, len
);
1119 type
= build_array_type (eltype
, bounds
);
1120 TYPE_STRING_FLAG (type
) = 1;
1126 gfc_get_character_type_len (int kind
, tree len
)
1128 gfc_validate_kind (BT_CHARACTER
, kind
, false);
1129 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind
), len
);
1133 /* Get a type node for a character kind. */
1136 gfc_get_character_type (int kind
, gfc_charlen
* cl
)
1140 len
= (cl
== NULL
) ? NULL_TREE
: cl
->backend_decl
;
1141 if (len
&& POINTER_TYPE_P (TREE_TYPE (len
)))
1142 len
= build_fold_indirect_ref (len
);
1144 return gfc_get_character_type_len (kind
, len
);
1147 /* Convert a basic type. This will be an array for character types. */
1150 gfc_typenode_for_spec (gfc_typespec
* spec
, int codim
)
1160 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1161 has been resolved. This is done so we can convert C_PTR and
1162 C_FUNPTR to simple variables that get translated to (void *). */
1163 if (spec
->f90_type
== BT_VOID
)
1166 && spec
->u
.derived
->intmod_sym_id
== ISOCBINDING_PTR
)
1167 basetype
= ptr_type_node
;
1169 basetype
= pfunc_type_node
;
1172 basetype
= gfc_get_int_type (spec
->kind
);
1176 basetype
= gfc_get_real_type (spec
->kind
);
1180 basetype
= gfc_get_complex_type (spec
->kind
);
1184 basetype
= gfc_get_logical_type (spec
->kind
);
1188 basetype
= gfc_get_character_type (spec
->kind
, spec
->u
.cl
);
1192 /* Since this cannot be used, return a length one character. */
1193 basetype
= gfc_get_character_type_len (gfc_default_character_kind
,
1194 gfc_index_one_node
);
1198 basetype
= gfc_get_union_type (spec
->u
.derived
);
1203 basetype
= gfc_get_derived_type (spec
->u
.derived
, codim
);
1205 if (spec
->type
== BT_CLASS
)
1206 GFC_CLASS_TYPE_P (basetype
) = 1;
1208 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1209 type and kind to fit a (void *) and the basetype returned was a
1210 ptr_type_node. We need to pass up this new information to the
1211 symbol that was declared of type C_PTR or C_FUNPTR. */
1212 if (spec
->u
.derived
->ts
.f90_type
== BT_VOID
)
1214 spec
->type
= BT_INTEGER
;
1215 spec
->kind
= gfc_index_integer_kind
;
1216 spec
->f90_type
= BT_VOID
;
1221 /* This is for the second arg to c_f_pointer and c_f_procpointer
1222 of the iso_c_binding module, to accept any ptr type. */
1223 basetype
= ptr_type_node
;
1224 if (spec
->f90_type
== BT_VOID
)
1227 && spec
->u
.derived
->intmod_sym_id
== ISOCBINDING_PTR
)
1228 basetype
= ptr_type_node
;
1230 basetype
= pfunc_type_node
;
1239 /* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
1242 gfc_conv_array_bound (gfc_expr
* expr
)
1244 /* If expr is an integer constant, return that. */
1245 if (expr
!= NULL
&& expr
->expr_type
== EXPR_CONSTANT
)
1246 return gfc_conv_mpz_to_tree (expr
->value
.integer
, gfc_index_integer_kind
);
1248 /* Otherwise return NULL. */
1252 /* Return the type of an element of the array. Note that scalar coarrays
1253 are special. In particular, for GFC_ARRAY_TYPE_P, the original argument
1254 (with POINTER_TYPE stripped) is returned. */
1257 gfc_get_element_type (tree type
)
1261 if (GFC_ARRAY_TYPE_P (type
))
1263 if (TREE_CODE (type
) == POINTER_TYPE
)
1264 type
= TREE_TYPE (type
);
1265 if (GFC_TYPE_ARRAY_RANK (type
) == 0)
1267 gcc_assert (GFC_TYPE_ARRAY_CORANK (type
) > 0);
1272 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1273 element
= TREE_TYPE (type
);
1278 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type
));
1279 element
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
1281 gcc_assert (TREE_CODE (element
) == POINTER_TYPE
);
1282 element
= TREE_TYPE (element
);
1284 /* For arrays, which are not scalar coarrays. */
1285 if (TREE_CODE (element
) == ARRAY_TYPE
&& !TYPE_STRING_FLAG (element
))
1286 element
= TREE_TYPE (element
);
1292 /* Build an array. This function is called from gfc_sym_type().
1293 Actually returns array descriptor type.
1295 Format of array descriptors is as follows:
1297 struct gfc_array_descriptor
1301 struct dtype_type dtype;
1302 struct descriptor_dimension dimension[N_DIM];
1311 signed short attribute;
1314 struct descriptor_dimension
1321 Translation code should use gfc_conv_descriptor_* rather than
1322 accessing the descriptor directly. Any changes to the array
1323 descriptor type will require changes in gfc_conv_descriptor_* and
1324 gfc_build_array_initializer.
1326 This is represented internally as a RECORD_TYPE. The index nodes
1327 are gfc_array_index_type and the data node is a pointer to the
1328 data. See below for the handling of character types.
1330 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1331 this generated poor code for assumed/deferred size arrays. These
1332 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1333 of the GENERIC grammar. Also, there is no way to explicitly set
1334 the array stride, so all data must be packed(1). I've tried to
1335 mark all the functions which would require modification with a GCC
1338 The data component points to the first element in the array. The
1339 offset field is the position of the origin of the array (i.e. element
1340 (0, 0 ...)). This may be outside the bounds of the array.
1342 An element is accessed by
1343 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
1344 This gives good performance as the computation does not involve the
1345 bounds of the array. For packed arrays, this is optimized further
1346 by substituting the known strides.
1348 This system has one problem: all array bounds must be within 2^31
1349 elements of the origin (2^63 on 64-bit machines). For example
1350 integer, dimension (80000:90000, 80000:90000, 2) :: array
1351 may not work properly on 32-bit machines because 80000*80000 >
1352 2^31, so the calculation for stride2 would overflow. This may
1353 still work, but I haven't checked, and it relies on the overflow
1354 doing the right thing.
1356 The way to fix this problem is to access elements as follows:
1357 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1358 Obviously this is much slower. I will make this a compile time
1359 option, something like -fsmall-array-offsets. Mixing code compiled
1360 with and without this switch will work.
1362 (1) This can be worked around by modifying the upper bound of the
1363 previous dimension. This requires extra fields in the descriptor
1364 (both real_ubound and fake_ubound). */
1367 /* Returns true if the array sym does not require a descriptor. */
1370 gfc_is_nodesc_array (gfc_symbol
* sym
)
1372 symbol_attribute
*array_attr
;
1374 bool is_classarray
= IS_CLASS_ARRAY (sym
);
1376 array_attr
= is_classarray
? &CLASS_DATA (sym
)->attr
: &sym
->attr
;
1377 as
= is_classarray
? CLASS_DATA (sym
)->as
: sym
->as
;
1379 gcc_assert (array_attr
->dimension
|| array_attr
->codimension
);
1381 /* We only want local arrays. */
1382 if ((sym
->ts
.type
!= BT_CLASS
&& sym
->attr
.pointer
)
1383 || (sym
->ts
.type
== BT_CLASS
&& CLASS_DATA (sym
)->attr
.class_pointer
)
1384 || array_attr
->allocatable
)
1387 /* We want a descriptor for associate-name arrays that do not have an
1388 explicitly known shape already. */
1389 if (sym
->assoc
&& as
->type
!= AS_EXPLICIT
)
1392 /* The dummy is stored in sym and not in the component. */
1393 if (sym
->attr
.dummy
)
1394 return as
->type
!= AS_ASSUMED_SHAPE
1395 && as
->type
!= AS_ASSUMED_RANK
;
1397 if (sym
->attr
.result
|| sym
->attr
.function
)
1400 gcc_assert (as
->type
== AS_EXPLICIT
|| as
->cp_was_assumed
);
1406 /* Create an array descriptor type. */
1409 gfc_build_array_type (tree type
, gfc_array_spec
* as
,
1410 enum gfc_array_kind akind
, bool restricted
,
1411 bool contiguous
, int codim
)
1413 tree lbound
[GFC_MAX_DIMENSIONS
];
1414 tree ubound
[GFC_MAX_DIMENSIONS
];
1417 /* Assumed-shape arrays do not have codimension information stored in the
1419 corank
= MAX (as
->corank
, codim
);
1420 if (as
->type
== AS_ASSUMED_SHAPE
||
1421 (as
->type
== AS_ASSUMED_RANK
&& akind
== GFC_ARRAY_ALLOCATABLE
))
1424 if (as
->type
== AS_ASSUMED_RANK
)
1425 for (n
= 0; n
< GFC_MAX_DIMENSIONS
; n
++)
1427 lbound
[n
] = NULL_TREE
;
1428 ubound
[n
] = NULL_TREE
;
1431 for (n
= 0; n
< as
->rank
; n
++)
1433 /* Create expressions for the known bounds of the array. */
1434 if (as
->type
== AS_ASSUMED_SHAPE
&& as
->lower
[n
] == NULL
)
1435 lbound
[n
] = gfc_index_one_node
;
1437 lbound
[n
] = gfc_conv_array_bound (as
->lower
[n
]);
1438 ubound
[n
] = gfc_conv_array_bound (as
->upper
[n
]);
1441 for (n
= as
->rank
; n
< as
->rank
+ corank
; n
++)
1443 if (as
->type
!= AS_DEFERRED
&& as
->lower
[n
] == NULL
)
1444 lbound
[n
] = gfc_index_one_node
;
1446 lbound
[n
] = gfc_conv_array_bound (as
->lower
[n
]);
1448 if (n
< as
->rank
+ corank
- 1)
1449 ubound
[n
] = gfc_conv_array_bound (as
->upper
[n
]);
1452 if (as
->type
== AS_ASSUMED_SHAPE
)
1453 akind
= contiguous
? GFC_ARRAY_ASSUMED_SHAPE_CONT
1454 : GFC_ARRAY_ASSUMED_SHAPE
;
1455 else if (as
->type
== AS_ASSUMED_RANK
)
1456 akind
= contiguous
? GFC_ARRAY_ASSUMED_RANK_CONT
1457 : GFC_ARRAY_ASSUMED_RANK
;
1458 return gfc_get_array_type_bounds (type
, as
->rank
== -1
1459 ? GFC_MAX_DIMENSIONS
: as
->rank
,
1460 corank
, lbound
, ubound
, 0, akind
,
1464 /* Returns the struct descriptor_dimension type. */
1467 gfc_get_desc_dim_type (void)
1470 tree decl
, *chain
= NULL
;
1472 if (gfc_desc_dim_type
)
1473 return gfc_desc_dim_type
;
1475 /* Build the type node. */
1476 type
= make_node (RECORD_TYPE
);
1478 TYPE_NAME (type
) = get_identifier ("descriptor_dimension");
1479 TYPE_PACKED (type
) = 1;
1481 /* Consists of the stride, lbound and ubound members. */
1482 decl
= gfc_add_field_to_struct_1 (type
,
1483 get_identifier ("stride"),
1484 gfc_array_index_type
, &chain
);
1485 TREE_NO_WARNING (decl
) = 1;
1487 decl
= gfc_add_field_to_struct_1 (type
,
1488 get_identifier ("lbound"),
1489 gfc_array_index_type
, &chain
);
1490 TREE_NO_WARNING (decl
) = 1;
1492 decl
= gfc_add_field_to_struct_1 (type
,
1493 get_identifier ("ubound"),
1494 gfc_array_index_type
, &chain
);
1495 TREE_NO_WARNING (decl
) = 1;
1497 /* Finish off the type. */
1498 gfc_finish_type (type
);
1499 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type
)) = 1;
1501 gfc_desc_dim_type
= type
;
1506 /* Return the DTYPE for an array. This describes the type and type parameters
1508 /* TODO: Only call this when the value is actually used, and make all the
1509 unknown cases abort. */
1512 gfc_get_dtype_rank_type (int rank
, tree etype
)
1519 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1521 size
= TYPE_SIZE_UNIT (etype
);
1523 switch (TREE_CODE (etype
))
1542 if (GFC_CLASS_TYPE_P (etype
))
1548 /* We will never have arrays of arrays. */
1551 if (size
== NULL_TREE
)
1552 size
= TYPE_SIZE_UNIT (TREE_TYPE (etype
));
1557 if (TREE_CODE (TREE_TYPE (etype
)) != VOID_TYPE
)
1558 size
= TYPE_SIZE_UNIT (TREE_TYPE (etype
));
1560 size
= build_int_cst (size_type_node
, 0);
1564 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1565 /* We can encounter strange array types for temporary arrays. */
1566 return gfc_index_zero_node
;
1569 tmp
= get_dtype_type_node ();
1570 field
= gfc_advance_chain (TYPE_FIELDS (tmp
),
1571 GFC_DTYPE_ELEM_LEN
);
1572 CONSTRUCTOR_APPEND_ELT (v
, field
,
1573 fold_convert (TREE_TYPE (field
), size
));
1575 field
= gfc_advance_chain (TYPE_FIELDS (dtype_type_node
),
1577 CONSTRUCTOR_APPEND_ELT (v
, field
,
1578 build_int_cst (TREE_TYPE (field
), rank
));
1580 field
= gfc_advance_chain (TYPE_FIELDS (dtype_type_node
),
1582 CONSTRUCTOR_APPEND_ELT (v
, field
,
1583 build_int_cst (TREE_TYPE (field
), n
));
1585 dtype
= build_constructor (tmp
, v
);
1592 gfc_get_dtype (tree type
)
1598 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type
) || GFC_ARRAY_TYPE_P (type
));
1600 rank
= GFC_TYPE_ARRAY_RANK (type
);
1601 etype
= gfc_get_element_type (type
);
1602 dtype
= gfc_get_dtype_rank_type (rank
, etype
);
1604 GFC_TYPE_ARRAY_DTYPE (type
) = dtype
;
1609 /* Build an array type for use without a descriptor, packed according
1610 to the value of PACKED. */
1613 gfc_get_nodesc_array_type (tree etype
, gfc_array_spec
* as
, gfc_packed packed
,
1627 mpz_init_set_ui (offset
, 0);
1628 mpz_init_set_ui (stride
, 1);
1631 /* We don't use build_array_type because this does not include include
1632 lang-specific information (i.e. the bounds of the array) when checking
1635 type
= make_node (ARRAY_TYPE
);
1637 type
= build_variant_type_copy (etype
);
1639 GFC_ARRAY_TYPE_P (type
) = 1;
1640 TYPE_LANG_SPECIFIC (type
) = ggc_cleared_alloc
<struct lang_type
> ();
1642 known_stride
= (packed
!= PACKED_NO
);
1644 for (n
= 0; n
< as
->rank
; n
++)
1646 /* Fill in the stride and bound components of the type. */
1648 tmp
= gfc_conv_mpz_to_tree (stride
, gfc_index_integer_kind
);
1651 GFC_TYPE_ARRAY_STRIDE (type
, n
) = tmp
;
1653 expr
= as
->lower
[n
];
1654 if (expr
->expr_type
== EXPR_CONSTANT
)
1656 tmp
= gfc_conv_mpz_to_tree (expr
->value
.integer
,
1657 gfc_index_integer_kind
);
1664 GFC_TYPE_ARRAY_LBOUND (type
, n
) = tmp
;
1668 /* Calculate the offset. */
1669 mpz_mul (delta
, stride
, as
->lower
[n
]->value
.integer
);
1670 mpz_sub (offset
, offset
, delta
);
1675 expr
= as
->upper
[n
];
1676 if (expr
&& expr
->expr_type
== EXPR_CONSTANT
)
1678 tmp
= gfc_conv_mpz_to_tree (expr
->value
.integer
,
1679 gfc_index_integer_kind
);
1686 GFC_TYPE_ARRAY_UBOUND (type
, n
) = tmp
;
1690 /* Calculate the stride. */
1691 mpz_sub (delta
, as
->upper
[n
]->value
.integer
,
1692 as
->lower
[n
]->value
.integer
);
1693 mpz_add_ui (delta
, delta
, 1);
1694 mpz_mul (stride
, stride
, delta
);
1697 /* Only the first stride is known for partial packed arrays. */
1698 if (packed
== PACKED_NO
|| packed
== PACKED_PARTIAL
)
1701 for (n
= as
->rank
; n
< as
->rank
+ as
->corank
; n
++)
1703 expr
= as
->lower
[n
];
1704 if (expr
->expr_type
== EXPR_CONSTANT
)
1705 tmp
= gfc_conv_mpz_to_tree (expr
->value
.integer
,
1706 gfc_index_integer_kind
);
1709 GFC_TYPE_ARRAY_LBOUND (type
, n
) = tmp
;
1711 expr
= as
->upper
[n
];
1712 if (expr
&& expr
->expr_type
== EXPR_CONSTANT
)
1713 tmp
= gfc_conv_mpz_to_tree (expr
->value
.integer
,
1714 gfc_index_integer_kind
);
1717 if (n
< as
->rank
+ as
->corank
- 1)
1718 GFC_TYPE_ARRAY_UBOUND (type
, n
) = tmp
;
1723 GFC_TYPE_ARRAY_OFFSET (type
) =
1724 gfc_conv_mpz_to_tree (offset
, gfc_index_integer_kind
);
1727 GFC_TYPE_ARRAY_OFFSET (type
) = NULL_TREE
;
1731 GFC_TYPE_ARRAY_SIZE (type
) =
1732 gfc_conv_mpz_to_tree (stride
, gfc_index_integer_kind
);
1735 GFC_TYPE_ARRAY_SIZE (type
) = NULL_TREE
;
1737 GFC_TYPE_ARRAY_RANK (type
) = as
->rank
;
1738 GFC_TYPE_ARRAY_CORANK (type
) = as
->corank
;
1739 GFC_TYPE_ARRAY_DTYPE (type
) = NULL_TREE
;
1740 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
1742 /* TODO: use main type if it is unbounded. */
1743 GFC_TYPE_ARRAY_DATAPTR_TYPE (type
) =
1744 build_pointer_type (build_array_type (etype
, range
));
1746 GFC_TYPE_ARRAY_DATAPTR_TYPE (type
) =
1747 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type
),
1748 TYPE_QUAL_RESTRICT
);
1752 if (packed
!= PACKED_STATIC
|| flag_coarray
== GFC_FCOARRAY_LIB
)
1754 type
= build_pointer_type (type
);
1757 type
= build_qualified_type (type
, TYPE_QUAL_RESTRICT
);
1759 GFC_ARRAY_TYPE_P (type
) = 1;
1760 TYPE_LANG_SPECIFIC (type
) = TYPE_LANG_SPECIFIC (TREE_TYPE (type
));
1768 mpz_sub_ui (stride
, stride
, 1);
1769 range
= gfc_conv_mpz_to_tree (stride
, gfc_index_integer_kind
);
1774 range
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
, range
);
1775 TYPE_DOMAIN (type
) = range
;
1777 build_pointer_type (etype
);
1778 TREE_TYPE (type
) = etype
;
1786 /* Represent packed arrays as multi-dimensional if they have rank >
1787 1 and with proper bounds, instead of flat arrays. This makes for
1788 better debug info. */
1791 tree gtype
= etype
, rtype
, type_decl
;
1793 for (n
= as
->rank
- 1; n
>= 0; n
--)
1795 rtype
= build_range_type (gfc_array_index_type
,
1796 GFC_TYPE_ARRAY_LBOUND (type
, n
),
1797 GFC_TYPE_ARRAY_UBOUND (type
, n
));
1798 gtype
= build_array_type (gtype
, rtype
);
1800 TYPE_NAME (type
) = type_decl
= build_decl (input_location
,
1801 TYPE_DECL
, NULL
, gtype
);
1802 DECL_ORIGINAL_TYPE (type_decl
) = gtype
;
1805 if (packed
!= PACKED_STATIC
|| !known_stride
1806 || (as
->corank
&& flag_coarray
== GFC_FCOARRAY_LIB
))
1808 /* For dummy arrays and automatic (heap allocated) arrays we
1809 want a pointer to the array. */
1810 type
= build_pointer_type (type
);
1812 type
= build_qualified_type (type
, TYPE_QUAL_RESTRICT
);
1813 GFC_ARRAY_TYPE_P (type
) = 1;
1814 TYPE_LANG_SPECIFIC (type
) = TYPE_LANG_SPECIFIC (TREE_TYPE (type
));
1820 /* Return or create the base type for an array descriptor. */
1823 gfc_get_array_descriptor_base (int dimen
, int codimen
, bool restricted
)
1825 tree fat_type
, decl
, arraytype
, *chain
= NULL
;
1826 char name
[16 + 2*GFC_RANK_DIGITS
+ 1 + 1];
1829 /* Assumed-rank array. */
1831 dimen
= GFC_MAX_DIMENSIONS
;
1833 idx
= 2 * (codimen
+ dimen
) + restricted
;
1835 gcc_assert (codimen
+ dimen
>= 0 && codimen
+ dimen
<= GFC_MAX_DIMENSIONS
);
1837 if (flag_coarray
== GFC_FCOARRAY_LIB
&& codimen
)
1839 if (gfc_array_descriptor_base_caf
[idx
])
1840 return gfc_array_descriptor_base_caf
[idx
];
1842 else if (gfc_array_descriptor_base
[idx
])
1843 return gfc_array_descriptor_base
[idx
];
1845 /* Build the type node. */
1846 fat_type
= make_node (RECORD_TYPE
);
1848 sprintf (name
, "array_descriptor" GFC_RANK_PRINTF_FORMAT
, dimen
+ codimen
);
1849 TYPE_NAME (fat_type
) = get_identifier (name
);
1850 TYPE_NAMELESS (fat_type
) = 1;
1852 /* Add the data member as the first element of the descriptor. */
1853 decl
= gfc_add_field_to_struct_1 (fat_type
,
1854 get_identifier ("data"),
1857 : ptr_type_node
), &chain
);
1859 /* Add the base component. */
1860 decl
= gfc_add_field_to_struct_1 (fat_type
,
1861 get_identifier ("offset"),
1862 gfc_array_index_type
, &chain
);
1863 TREE_NO_WARNING (decl
) = 1;
1865 /* Add the dtype component. */
1866 decl
= gfc_add_field_to_struct_1 (fat_type
,
1867 get_identifier ("dtype"),
1868 get_dtype_type_node (), &chain
);
1869 TREE_NO_WARNING (decl
) = 1;
1871 /* Add the span component. */
1872 decl
= gfc_add_field_to_struct_1 (fat_type
,
1873 get_identifier ("span"),
1874 gfc_array_index_type
, &chain
);
1875 TREE_NO_WARNING (decl
) = 1;
1877 /* Build the array type for the stride and bound components. */
1878 if (dimen
+ codimen
> 0)
1881 build_array_type (gfc_get_desc_dim_type (),
1882 build_range_type (gfc_array_index_type
,
1883 gfc_index_zero_node
,
1884 gfc_rank_cst
[codimen
+ dimen
- 1]));
1886 decl
= gfc_add_field_to_struct_1 (fat_type
, get_identifier ("dim"),
1888 TREE_NO_WARNING (decl
) = 1;
1891 if (flag_coarray
== GFC_FCOARRAY_LIB
)
1893 decl
= gfc_add_field_to_struct_1 (fat_type
,
1894 get_identifier ("token"),
1895 prvoid_type_node
, &chain
);
1896 TREE_NO_WARNING (decl
) = 1;
1899 /* Finish off the type. */
1900 gfc_finish_type (fat_type
);
1901 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type
)) = 1;
1903 if (flag_coarray
== GFC_FCOARRAY_LIB
&& codimen
)
1904 gfc_array_descriptor_base_caf
[idx
] = fat_type
;
1906 gfc_array_descriptor_base
[idx
] = fat_type
;
1912 /* Build an array (descriptor) type with given bounds. */
1915 gfc_get_array_type_bounds (tree etype
, int dimen
, int codimen
, tree
* lbound
,
1916 tree
* ubound
, int packed
,
1917 enum gfc_array_kind akind
, bool restricted
)
1919 char name
[8 + 2*GFC_RANK_DIGITS
+ 1 + GFC_MAX_SYMBOL_LEN
];
1920 tree fat_type
, base_type
, arraytype
, lower
, upper
, stride
, tmp
, rtype
;
1921 const char *type_name
;
1924 base_type
= gfc_get_array_descriptor_base (dimen
, codimen
, restricted
);
1925 fat_type
= build_distinct_type_copy (base_type
);
1926 /* Unshare TYPE_FIELDs. */
1927 for (tree
*tp
= &TYPE_FIELDS (fat_type
); *tp
; tp
= &DECL_CHAIN (*tp
))
1929 tree next
= DECL_CHAIN (*tp
);
1930 *tp
= copy_node (*tp
);
1931 DECL_CONTEXT (*tp
) = fat_type
;
1932 DECL_CHAIN (*tp
) = next
;
1934 /* Make sure that nontarget and target array type have the same canonical
1935 type (and same stub decl for debug info). */
1936 base_type
= gfc_get_array_descriptor_base (dimen
, codimen
, false);
1937 TYPE_CANONICAL (fat_type
) = base_type
;
1938 TYPE_STUB_DECL (fat_type
) = TYPE_STUB_DECL (base_type
);
1940 tmp
= TYPE_NAME (etype
);
1941 if (tmp
&& TREE_CODE (tmp
) == TYPE_DECL
)
1942 tmp
= DECL_NAME (tmp
);
1944 type_name
= IDENTIFIER_POINTER (tmp
);
1946 type_name
= "unknown";
1947 sprintf (name
, "array" GFC_RANK_PRINTF_FORMAT
"_%.*s", dimen
+ codimen
,
1948 GFC_MAX_SYMBOL_LEN
, type_name
);
1949 TYPE_NAME (fat_type
) = get_identifier (name
);
1950 TYPE_NAMELESS (fat_type
) = 1;
1952 GFC_DESCRIPTOR_TYPE_P (fat_type
) = 1;
1953 TYPE_LANG_SPECIFIC (fat_type
) = ggc_cleared_alloc
<struct lang_type
> ();
1955 GFC_TYPE_ARRAY_RANK (fat_type
) = dimen
;
1956 GFC_TYPE_ARRAY_CORANK (fat_type
) = codimen
;
1957 GFC_TYPE_ARRAY_DTYPE (fat_type
) = NULL_TREE
;
1958 GFC_TYPE_ARRAY_AKIND (fat_type
) = akind
;
1960 /* Build an array descriptor record type. */
1962 stride
= gfc_index_one_node
;
1965 for (n
= 0; n
< dimen
+ codimen
; n
++)
1968 GFC_TYPE_ARRAY_STRIDE (fat_type
, n
) = stride
;
1975 if (lower
!= NULL_TREE
)
1977 if (INTEGER_CST_P (lower
))
1978 GFC_TYPE_ARRAY_LBOUND (fat_type
, n
) = lower
;
1983 if (codimen
&& n
== dimen
+ codimen
- 1)
1987 if (upper
!= NULL_TREE
)
1989 if (INTEGER_CST_P (upper
))
1990 GFC_TYPE_ARRAY_UBOUND (fat_type
, n
) = upper
;
1998 if (upper
!= NULL_TREE
&& lower
!= NULL_TREE
&& stride
!= NULL_TREE
)
2000 tmp
= fold_build2_loc (input_location
, MINUS_EXPR
,
2001 gfc_array_index_type
, upper
, lower
);
2002 tmp
= fold_build2_loc (input_location
, PLUS_EXPR
,
2003 gfc_array_index_type
, tmp
,
2004 gfc_index_one_node
);
2005 stride
= fold_build2_loc (input_location
, MULT_EXPR
,
2006 gfc_array_index_type
, tmp
, stride
);
2007 /* Check the folding worked. */
2008 gcc_assert (INTEGER_CST_P (stride
));
2013 GFC_TYPE_ARRAY_SIZE (fat_type
) = stride
;
2015 /* TODO: known offsets for descriptors. */
2016 GFC_TYPE_ARRAY_OFFSET (fat_type
) = NULL_TREE
;
2020 arraytype
= build_pointer_type (etype
);
2022 arraytype
= build_qualified_type (arraytype
, TYPE_QUAL_RESTRICT
);
2024 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type
) = arraytype
;
2028 /* We define data as an array with the correct size if possible.
2029 Much better than doing pointer arithmetic. */
2031 rtype
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
2032 int_const_binop (MINUS_EXPR
, stride
,
2033 build_int_cst (TREE_TYPE (stride
), 1)));
2035 rtype
= gfc_array_range_type
;
2036 arraytype
= build_array_type (etype
, rtype
);
2037 arraytype
= build_pointer_type (arraytype
);
2039 arraytype
= build_qualified_type (arraytype
, TYPE_QUAL_RESTRICT
);
2040 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type
) = arraytype
;
2042 /* This will generate the base declarations we need to emit debug
2043 information for this type. FIXME: there must be a better way to
2044 avoid divergence between compilations with and without debug
2047 struct array_descr_info info
;
2048 gfc_get_array_descr_info (fat_type
, &info
);
2049 gfc_get_array_descr_info (build_pointer_type (fat_type
), &info
);
2055 /* Build a pointer type. This function is called from gfc_sym_type(). */
2058 gfc_build_pointer_type (gfc_symbol
* sym
, tree type
)
2060 /* Array pointer types aren't actually pointers. */
2061 if (sym
->attr
.dimension
)
2064 return build_pointer_type (type
);
2067 static tree
gfc_nonrestricted_type (tree t
);
2068 /* Given two record or union type nodes TO and FROM, ensure
2069 that all fields in FROM have a corresponding field in TO,
2070 their type being nonrestrict variants. This accepts a TO
2071 node that already has a prefix of the fields in FROM. */
2073 mirror_fields (tree to
, tree from
)
2078 /* Forward to the end of TOs fields. */
2079 fto
= TYPE_FIELDS (to
);
2080 ffrom
= TYPE_FIELDS (from
);
2081 chain
= &TYPE_FIELDS (to
);
2084 gcc_assert (ffrom
&& DECL_NAME (fto
) == DECL_NAME (ffrom
));
2085 chain
= &DECL_CHAIN (fto
);
2086 fto
= DECL_CHAIN (fto
);
2087 ffrom
= DECL_CHAIN (ffrom
);
2090 /* Now add all fields remaining in FROM (starting with ffrom). */
2091 for (; ffrom
; ffrom
= DECL_CHAIN (ffrom
))
2093 tree newfield
= copy_node (ffrom
);
2094 DECL_CONTEXT (newfield
) = to
;
2095 /* The store to DECL_CHAIN might seem redundant with the
2096 stores to *chain, but not clearing it here would mean
2097 leaving a chain into the old fields. If ever
2098 our called functions would look at them confusion
2100 DECL_CHAIN (newfield
) = NULL_TREE
;
2102 chain
= &DECL_CHAIN (newfield
);
2104 if (TREE_CODE (ffrom
) == FIELD_DECL
)
2106 tree elemtype
= gfc_nonrestricted_type (TREE_TYPE (ffrom
));
2107 TREE_TYPE (newfield
) = elemtype
;
2113 /* Given a type T, returns a different type of the same structure,
2114 except that all types it refers to (recursively) are always
2115 non-restrict qualified types. */
2117 gfc_nonrestricted_type (tree t
)
2121 /* If the type isn't laid out yet, don't copy it. If something
2122 needs it for real it should wait until the type got finished. */
2126 if (!TYPE_LANG_SPECIFIC (t
))
2127 TYPE_LANG_SPECIFIC (t
) = ggc_cleared_alloc
<struct lang_type
> ();
2128 /* If we're dealing with this very node already further up
2129 the call chain (recursion via pointers and struct members)
2130 we haven't yet determined if we really need a new type node.
2131 Assume we don't, return T itself. */
2132 if (TYPE_LANG_SPECIFIC (t
)->nonrestricted_type
== error_mark_node
)
2135 /* If we have calculated this all already, just return it. */
2136 if (TYPE_LANG_SPECIFIC (t
)->nonrestricted_type
)
2137 return TYPE_LANG_SPECIFIC (t
)->nonrestricted_type
;
2139 /* Mark this type. */
2140 TYPE_LANG_SPECIFIC (t
)->nonrestricted_type
= error_mark_node
;
2142 switch (TREE_CODE (t
))
2148 case REFERENCE_TYPE
:
2150 tree totype
= gfc_nonrestricted_type (TREE_TYPE (t
));
2151 if (totype
== TREE_TYPE (t
))
2153 else if (TREE_CODE (t
) == POINTER_TYPE
)
2154 ret
= build_pointer_type (totype
);
2156 ret
= build_reference_type (totype
);
2157 ret
= build_qualified_type (ret
,
2158 TYPE_QUALS (t
) & ~TYPE_QUAL_RESTRICT
);
2164 tree elemtype
= gfc_nonrestricted_type (TREE_TYPE (t
));
2165 if (elemtype
== TREE_TYPE (t
))
2169 ret
= build_variant_type_copy (t
);
2170 TREE_TYPE (ret
) = elemtype
;
2171 if (TYPE_LANG_SPECIFIC (t
)
2172 && GFC_TYPE_ARRAY_DATAPTR_TYPE (t
))
2174 tree dataptr_type
= GFC_TYPE_ARRAY_DATAPTR_TYPE (t
);
2175 dataptr_type
= gfc_nonrestricted_type (dataptr_type
);
2176 if (dataptr_type
!= GFC_TYPE_ARRAY_DATAPTR_TYPE (t
))
2178 TYPE_LANG_SPECIFIC (ret
)
2179 = ggc_cleared_alloc
<struct lang_type
> ();
2180 *TYPE_LANG_SPECIFIC (ret
) = *TYPE_LANG_SPECIFIC (t
);
2181 GFC_TYPE_ARRAY_DATAPTR_TYPE (ret
) = dataptr_type
;
2190 case QUAL_UNION_TYPE
:
2193 /* First determine if we need a new type at all.
2194 Careful, the two calls to gfc_nonrestricted_type per field
2195 might return different values. That happens exactly when
2196 one of the fields reaches back to this very record type
2197 (via pointers). The first calls will assume that we don't
2198 need to copy T (see the error_mark_node marking). If there
2199 are any reasons for copying T apart from having to copy T,
2200 we'll indeed copy it, and the second calls to
2201 gfc_nonrestricted_type will use that new node if they
2203 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2204 if (TREE_CODE (field
) == FIELD_DECL
)
2206 tree elemtype
= gfc_nonrestricted_type (TREE_TYPE (field
));
2207 if (elemtype
!= TREE_TYPE (field
))
2212 ret
= build_variant_type_copy (t
);
2213 TYPE_FIELDS (ret
) = NULL_TREE
;
2215 /* Here we make sure that as soon as we know we have to copy
2216 T, that also fields reaching back to us will use the new
2217 copy. It's okay if that copy still contains the old fields,
2218 we won't look at them. */
2219 TYPE_LANG_SPECIFIC (t
)->nonrestricted_type
= ret
;
2220 mirror_fields (ret
, t
);
2225 TYPE_LANG_SPECIFIC (t
)->nonrestricted_type
= ret
;
2230 /* Return the type for a symbol. Special handling is required for character
2231 types to get the correct level of indirection.
2232 For functions return the return type.
2233 For subroutines return void_type_node.
2234 Calling this multiple times for the same symbol should be avoided,
2235 especially for character and array types. */
2238 gfc_sym_type (gfc_symbol
* sym
)
2244 /* Procedure Pointers inside COMMON blocks. */
2245 if (sym
->attr
.proc_pointer
&& sym
->attr
.in_common
)
2247 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
2248 sym
->attr
.proc_pointer
= 0;
2249 type
= build_pointer_type (gfc_get_function_type (sym
));
2250 sym
->attr
.proc_pointer
= 1;
2254 if (sym
->attr
.flavor
== FL_PROCEDURE
&& !sym
->attr
.function
)
2255 return void_type_node
;
2257 /* In the case of a function the fake result variable may have a
2258 type different from the function type, so don't return early in
2260 if (sym
->backend_decl
&& !sym
->attr
.function
)
2261 return TREE_TYPE (sym
->backend_decl
);
2263 if (sym
->attr
.result
2264 && sym
->ts
.type
== BT_CHARACTER
2265 && sym
->ts
.u
.cl
->backend_decl
== NULL_TREE
2266 && sym
->ns
->proc_name
2267 && sym
->ns
->proc_name
->ts
.u
.cl
2268 && sym
->ns
->proc_name
->ts
.u
.cl
->backend_decl
!= NULL_TREE
)
2269 sym
->ts
.u
.cl
->backend_decl
= sym
->ns
->proc_name
->ts
.u
.cl
->backend_decl
;
2271 if (sym
->ts
.type
== BT_CHARACTER
2272 && ((sym
->attr
.function
&& sym
->attr
.is_bind_c
)
2273 || (sym
->attr
.result
2274 && sym
->ns
->proc_name
2275 && sym
->ns
->proc_name
->attr
.is_bind_c
)
2276 || (sym
->ts
.deferred
&& (!sym
->ts
.u
.cl
2277 || !sym
->ts
.u
.cl
->backend_decl
))))
2278 type
= gfc_character1_type_node
;
2280 type
= gfc_typenode_for_spec (&sym
->ts
, sym
->attr
.codimension
);
2282 if (sym
->attr
.dummy
&& !sym
->attr
.function
&& !sym
->attr
.value
)
2287 restricted
= !sym
->attr
.target
&& !sym
->attr
.pointer
2288 && !sym
->attr
.proc_pointer
&& !sym
->attr
.cray_pointee
;
2290 type
= gfc_nonrestricted_type (type
);
2292 if (sym
->attr
.dimension
|| sym
->attr
.codimension
)
2294 if (gfc_is_nodesc_array (sym
))
2296 /* If this is a character argument of unknown length, just use the
2298 if (sym
->ts
.type
!= BT_CHARACTER
2299 || !(sym
->attr
.dummy
|| sym
->attr
.function
)
2300 || sym
->ts
.u
.cl
->backend_decl
)
2302 type
= gfc_get_nodesc_array_type (type
, sym
->as
,
2311 enum gfc_array_kind akind
= GFC_ARRAY_UNKNOWN
;
2312 if (sym
->attr
.pointer
)
2313 akind
= sym
->attr
.contiguous
? GFC_ARRAY_POINTER_CONT
2314 : GFC_ARRAY_POINTER
;
2315 else if (sym
->attr
.allocatable
)
2316 akind
= GFC_ARRAY_ALLOCATABLE
;
2317 type
= gfc_build_array_type (type
, sym
->as
, akind
, restricted
,
2318 sym
->attr
.contiguous
, false);
2323 if (sym
->attr
.allocatable
|| sym
->attr
.pointer
2324 || gfc_is_associate_pointer (sym
))
2325 type
= gfc_build_pointer_type (sym
, type
);
2328 /* We currently pass all parameters by reference.
2329 See f95_get_function_decl. For dummy function parameters return the
2333 /* We must use pointer types for potentially absent variables. The
2334 optimizers assume a reference type argument is never NULL. */
2335 if (sym
->attr
.optional
2336 || (sym
->ns
->proc_name
&& sym
->ns
->proc_name
->attr
.entry_master
))
2337 type
= build_pointer_type (type
);
2340 type
= build_reference_type (type
);
2342 type
= build_qualified_type (type
, TYPE_QUAL_RESTRICT
);
2349 /* Layout and output debug info for a record type. */
2352 gfc_finish_type (tree type
)
2356 decl
= build_decl (input_location
,
2357 TYPE_DECL
, NULL_TREE
, type
);
2358 TYPE_STUB_DECL (type
) = decl
;
2360 rest_of_type_compilation (type
, 1);
2361 rest_of_decl_compilation (decl
, 1, 0);
2364 /* Add a field of given NAME and TYPE to the context of a UNION_TYPE
2365 or RECORD_TYPE pointed to by CONTEXT. The new field is chained
2366 to the end of the field list pointed to by *CHAIN.
2368 Returns a pointer to the new field. */
2371 gfc_add_field_to_struct_1 (tree context
, tree name
, tree type
, tree
**chain
)
2373 tree decl
= build_decl (input_location
, FIELD_DECL
, name
, type
);
2375 DECL_CONTEXT (decl
) = context
;
2376 DECL_CHAIN (decl
) = NULL_TREE
;
2377 if (TYPE_FIELDS (context
) == NULL_TREE
)
2378 TYPE_FIELDS (context
) = decl
;
2383 *chain
= &DECL_CHAIN (decl
);
2389 /* Like `gfc_add_field_to_struct_1', but adds alignment
2393 gfc_add_field_to_struct (tree context
, tree name
, tree type
, tree
**chain
)
2395 tree decl
= gfc_add_field_to_struct_1 (context
, name
, type
, chain
);
2397 DECL_INITIAL (decl
) = 0;
2398 SET_DECL_ALIGN (decl
, 0);
2399 DECL_USER_ALIGN (decl
) = 0;
2405 /* Copy the backend_decl and component backend_decls if
2406 the two derived type symbols are "equal", as described
2407 in 4.4.2 and resolved by gfc_compare_derived_types. */
2410 gfc_copy_dt_decls_ifequal (gfc_symbol
*from
, gfc_symbol
*to
,
2413 gfc_component
*to_cm
;
2414 gfc_component
*from_cm
;
2419 if (from
->backend_decl
== NULL
2420 || !gfc_compare_derived_types (from
, to
))
2423 to
->backend_decl
= from
->backend_decl
;
2425 to_cm
= to
->components
;
2426 from_cm
= from
->components
;
2428 /* Copy the component declarations. If a component is itself
2429 a derived type, we need a copy of its component declarations.
2430 This is done by recursing into gfc_get_derived_type and
2431 ensures that the component's component declarations have
2432 been built. If it is a character, we need the character
2434 for (; to_cm
; to_cm
= to_cm
->next
, from_cm
= from_cm
->next
)
2436 to_cm
->backend_decl
= from_cm
->backend_decl
;
2437 to_cm
->caf_token
= from_cm
->caf_token
;
2438 if (from_cm
->ts
.type
== BT_UNION
)
2439 gfc_get_union_type (to_cm
->ts
.u
.derived
);
2440 else if (from_cm
->ts
.type
== BT_DERIVED
2441 && (!from_cm
->attr
.pointer
|| from_gsym
))
2442 gfc_get_derived_type (to_cm
->ts
.u
.derived
);
2443 else if (from_cm
->ts
.type
== BT_CLASS
2444 && (!CLASS_DATA (from_cm
)->attr
.class_pointer
|| from_gsym
))
2445 gfc_get_derived_type (to_cm
->ts
.u
.derived
);
2446 else if (from_cm
->ts
.type
== BT_CHARACTER
)
2447 to_cm
->ts
.u
.cl
->backend_decl
= from_cm
->ts
.u
.cl
->backend_decl
;
2454 /* Build a tree node for a procedure pointer component. */
2457 gfc_get_ppc_type (gfc_component
* c
)
2461 /* Explicit interface. */
2462 if (c
->attr
.if_source
!= IFSRC_UNKNOWN
&& c
->ts
.interface
)
2463 return build_pointer_type (gfc_get_function_type (c
->ts
.interface
));
2465 /* Implicit interface (only return value may be known). */
2466 if (c
->attr
.function
&& !c
->attr
.dimension
&& c
->ts
.type
!= BT_CHARACTER
)
2467 t
= gfc_typenode_for_spec (&c
->ts
);
2471 return build_pointer_type (build_function_type_list (t
, NULL_TREE
));
2475 /* Build a tree node for a union type. Requires building each map
2476 structure which is an element of the union. */
2479 gfc_get_union_type (gfc_symbol
*un
)
2481 gfc_component
*map
= NULL
;
2482 tree typenode
= NULL
, map_type
= NULL
, map_field
= NULL
;
2485 if (un
->backend_decl
)
2487 if (TYPE_FIELDS (un
->backend_decl
) || un
->attr
.proc_pointer_comp
)
2488 return un
->backend_decl
;
2490 typenode
= un
->backend_decl
;
2494 typenode
= make_node (UNION_TYPE
);
2495 TYPE_NAME (typenode
) = get_identifier (un
->name
);
2498 /* Add each contained MAP as a field. */
2499 for (map
= un
->components
; map
; map
= map
->next
)
2501 gcc_assert (map
->ts
.type
== BT_DERIVED
);
2503 /* The map's type node, which is defined within this union's context. */
2504 map_type
= gfc_get_derived_type (map
->ts
.u
.derived
);
2505 TYPE_CONTEXT (map_type
) = typenode
;
2507 /* The map field's declaration. */
2508 map_field
= gfc_add_field_to_struct(typenode
, get_identifier(map
->name
),
2511 gfc_set_decl_location (map_field
, &map
->loc
);
2512 else if (un
->declared_at
.lb
)
2513 gfc_set_decl_location (map_field
, &un
->declared_at
);
2515 DECL_PACKED (map_field
) |= TYPE_PACKED (typenode
);
2516 DECL_NAMELESS(map_field
) = true;
2518 /* We should never clobber another backend declaration for this map,
2519 because each map component is unique. */
2520 if (!map
->backend_decl
)
2521 map
->backend_decl
= map_field
;
2524 un
->backend_decl
= typenode
;
2525 gfc_finish_type (typenode
);
2531 /* Build a tree node for a derived type. If there are equal
2532 derived types, with different local names, these are built
2533 at the same time. If an equal derived type has been built
2534 in a parent namespace, this is used. */
2537 gfc_get_derived_type (gfc_symbol
* derived
, int codimen
)
2539 tree typenode
= NULL
, field
= NULL
, field_type
= NULL
;
2540 tree canonical
= NULL_TREE
;
2542 bool got_canonical
= false;
2543 bool unlimited_entity
= false;
2550 coarray_flag
= flag_coarray
== GFC_FCOARRAY_LIB
2551 && derived
->module
&& !derived
->attr
.vtype
;
2553 gcc_assert (!derived
->attr
.pdt_template
);
2555 if (derived
->attr
.unlimited_polymorphic
2556 || (flag_coarray
== GFC_FCOARRAY_LIB
2557 && derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2558 && (derived
->intmod_sym_id
== ISOFORTRAN_LOCK_TYPE
2559 || derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
2560 || derived
->intmod_sym_id
== ISOFORTRAN_TEAM_TYPE
)))
2561 return ptr_type_node
;
2563 if (flag_coarray
!= GFC_FCOARRAY_LIB
2564 && derived
->from_intmod
== INTMOD_ISO_FORTRAN_ENV
2565 && (derived
->intmod_sym_id
== ISOFORTRAN_EVENT_TYPE
2566 || derived
->intmod_sym_id
== ISOFORTRAN_TEAM_TYPE
))
2567 return gfc_get_int_type (gfc_default_integer_kind
);
2569 if (derived
&& derived
->attr
.flavor
== FL_PROCEDURE
2570 && derived
->attr
.generic
)
2571 derived
= gfc_find_dt_in_generic (derived
);
2573 /* See if it's one of the iso_c_binding derived types. */
2574 if (derived
->attr
.is_iso_c
== 1 || derived
->ts
.f90_type
== BT_VOID
)
2576 if (derived
->backend_decl
)
2577 return derived
->backend_decl
;
2579 if (derived
->intmod_sym_id
== ISOCBINDING_PTR
)
2580 derived
->backend_decl
= ptr_type_node
;
2582 derived
->backend_decl
= pfunc_type_node
;
2584 derived
->ts
.kind
= gfc_index_integer_kind
;
2585 derived
->ts
.type
= BT_INTEGER
;
2586 /* Set the f90_type to BT_VOID as a way to recognize something of type
2587 BT_INTEGER that needs to fit a void * for the purpose of the
2588 iso_c_binding derived types. */
2589 derived
->ts
.f90_type
= BT_VOID
;
2591 return derived
->backend_decl
;
2594 /* If use associated, use the module type for this one. */
2595 if (derived
->backend_decl
== NULL
2596 && derived
->attr
.use_assoc
2598 && gfc_get_module_backend_decl (derived
))
2599 goto copy_derived_types
;
2601 /* The derived types from an earlier namespace can be used as the
2603 if (derived
->backend_decl
== NULL
&& !derived
->attr
.use_assoc
2604 && gfc_global_ns_list
)
2606 for (ns
= gfc_global_ns_list
;
2607 ns
->translated
&& !got_canonical
;
2610 dt
= ns
->derived_types
;
2611 for (; dt
&& !canonical
; dt
= dt
->next
)
2613 gfc_copy_dt_decls_ifequal (dt
->derived
, derived
, true);
2614 if (derived
->backend_decl
)
2615 got_canonical
= true;
2620 /* Store up the canonical type to be added to this one. */
2623 if (TYPE_CANONICAL (derived
->backend_decl
))
2624 canonical
= TYPE_CANONICAL (derived
->backend_decl
);
2626 canonical
= derived
->backend_decl
;
2628 derived
->backend_decl
= NULL_TREE
;
2631 /* derived->backend_decl != 0 means we saw it before, but its
2632 components' backend_decl may have not been built. */
2633 if (derived
->backend_decl
)
2635 /* Its components' backend_decl have been built or we are
2636 seeing recursion through the formal arglist of a procedure
2637 pointer component. */
2638 if (TYPE_FIELDS (derived
->backend_decl
))
2639 return derived
->backend_decl
;
2640 else if (derived
->attr
.abstract
2641 && derived
->attr
.proc_pointer_comp
)
2643 /* If an abstract derived type with procedure pointer
2644 components has no other type of component, return the
2645 backend_decl. Otherwise build the components if any of the
2646 non-procedure pointer components have no backend_decl. */
2647 for (c
= derived
->components
; c
; c
= c
->next
)
2649 bool same_alloc_type
= c
->attr
.allocatable
2650 && derived
== c
->ts
.u
.derived
;
2651 if (!c
->attr
.proc_pointer
2653 && c
->backend_decl
== NULL
)
2655 else if (c
->next
== NULL
)
2656 return derived
->backend_decl
;
2658 typenode
= derived
->backend_decl
;
2661 typenode
= derived
->backend_decl
;
2665 /* We see this derived type first time, so build the type node. */
2666 typenode
= make_node (RECORD_TYPE
);
2667 TYPE_NAME (typenode
) = get_identifier (derived
->name
);
2668 TYPE_PACKED (typenode
) = flag_pack_derived
;
2669 derived
->backend_decl
= typenode
;
2672 if (derived
->components
2673 && derived
->components
->ts
.type
== BT_DERIVED
2674 && strcmp (derived
->components
->name
, "_data") == 0
2675 && derived
->components
->ts
.u
.derived
->attr
.unlimited_polymorphic
)
2676 unlimited_entity
= true;
2678 /* Go through the derived type components, building them as
2679 necessary. The reason for doing this now is that it is
2680 possible to recurse back to this derived type through a
2681 pointer component (PR24092). If this happens, the fields
2682 will be built and so we can return the type. */
2683 for (c
= derived
->components
; c
; c
= c
->next
)
2685 bool same_alloc_type
= c
->attr
.allocatable
2686 && derived
== c
->ts
.u
.derived
;
2688 if (c
->ts
.type
== BT_UNION
&& c
->ts
.u
.derived
->backend_decl
== NULL
)
2689 c
->ts
.u
.derived
->backend_decl
= gfc_get_union_type (c
->ts
.u
.derived
);
2691 if (c
->ts
.type
!= BT_DERIVED
&& c
->ts
.type
!= BT_CLASS
)
2694 if ((!c
->attr
.pointer
&& !c
->attr
.proc_pointer
2695 && !same_alloc_type
)
2696 || c
->ts
.u
.derived
->backend_decl
== NULL
)
2698 int local_codim
= c
->attr
.codimension
? c
->as
->corank
: codimen
;
2699 c
->ts
.u
.derived
->backend_decl
= gfc_get_derived_type (c
->ts
.u
.derived
,
2703 if (c
->ts
.u
.derived
->attr
.is_iso_c
)
2705 /* Need to copy the modified ts from the derived type. The
2706 typespec was modified because C_PTR/C_FUNPTR are translated
2707 into (void *) from derived types. */
2708 c
->ts
.type
= c
->ts
.u
.derived
->ts
.type
;
2709 c
->ts
.kind
= c
->ts
.u
.derived
->ts
.kind
;
2710 c
->ts
.f90_type
= c
->ts
.u
.derived
->ts
.f90_type
;
2713 c
->initializer
->ts
.type
= c
->ts
.type
;
2714 c
->initializer
->ts
.kind
= c
->ts
.kind
;
2715 c
->initializer
->ts
.f90_type
= c
->ts
.f90_type
;
2716 c
->initializer
->expr_type
= EXPR_NULL
;
2721 if (TYPE_FIELDS (derived
->backend_decl
))
2722 return derived
->backend_decl
;
2724 /* Build the type member list. Install the newly created RECORD_TYPE
2725 node as DECL_CONTEXT of each FIELD_DECL. In this case we must go
2726 through only the top-level linked list of components so we correctly
2727 build UNION_TYPE nodes for BT_UNION components. MAPs and other nested
2728 types are built as part of gfc_get_union_type. */
2729 for (c
= derived
->components
; c
; c
= c
->next
)
2731 bool same_alloc_type
= c
->attr
.allocatable
2732 && derived
== c
->ts
.u
.derived
;
2733 /* Prevent infinite recursion, when the procedure pointer type is
2734 the same as derived, by forcing the procedure pointer component to
2735 be built as if the explicit interface does not exist. */
2736 if (c
->attr
.proc_pointer
2737 && (c
->ts
.type
!= BT_DERIVED
|| (c
->ts
.u
.derived
2738 && !gfc_compare_derived_types (derived
, c
->ts
.u
.derived
)))
2739 && (c
->ts
.type
!= BT_CLASS
|| (CLASS_DATA (c
)->ts
.u
.derived
2740 && !gfc_compare_derived_types (derived
, CLASS_DATA (c
)->ts
.u
.derived
))))
2741 field_type
= gfc_get_ppc_type (c
);
2742 else if (c
->attr
.proc_pointer
&& derived
->backend_decl
)
2744 tmp
= build_function_type_list (derived
->backend_decl
, NULL_TREE
);
2745 field_type
= build_pointer_type (tmp
);
2747 else if (c
->ts
.type
== BT_DERIVED
|| c
->ts
.type
== BT_CLASS
)
2748 field_type
= c
->ts
.u
.derived
->backend_decl
;
2749 else if (c
->attr
.caf_token
)
2750 field_type
= pvoid_type_node
;
2753 if (c
->ts
.type
== BT_CHARACTER
2754 && !c
->ts
.deferred
&& !c
->attr
.pdt_string
)
2756 /* Evaluate the string length. */
2757 gfc_conv_const_charlen (c
->ts
.u
.cl
);
2758 gcc_assert (c
->ts
.u
.cl
->backend_decl
);
2760 else if (c
->ts
.type
== BT_CHARACTER
)
2761 c
->ts
.u
.cl
->backend_decl
2762 = build_int_cst (gfc_charlen_type_node
, 0);
2764 field_type
= gfc_typenode_for_spec (&c
->ts
, codimen
);
2767 /* This returns an array descriptor type. Initialization may be
2769 if ((c
->attr
.dimension
|| c
->attr
.codimension
) && !c
->attr
.proc_pointer
)
2771 if (c
->attr
.pointer
|| c
->attr
.allocatable
|| c
->attr
.pdt_array
)
2773 enum gfc_array_kind akind
;
2774 if (c
->attr
.pointer
)
2775 akind
= c
->attr
.contiguous
? GFC_ARRAY_POINTER_CONT
2776 : GFC_ARRAY_POINTER
;
2778 akind
= GFC_ARRAY_ALLOCATABLE
;
2779 /* Pointers to arrays aren't actually pointer types. The
2780 descriptors are separate, but the data is common. */
2781 field_type
= gfc_build_array_type (field_type
, c
->as
, akind
,
2783 && !c
->attr
.pointer
,
2788 field_type
= gfc_get_nodesc_array_type (field_type
, c
->as
,
2792 else if ((c
->attr
.pointer
|| c
->attr
.allocatable
|| c
->attr
.pdt_string
)
2793 && !c
->attr
.proc_pointer
2794 && !(unlimited_entity
&& c
== derived
->components
))
2795 field_type
= build_pointer_type (field_type
);
2797 if (c
->attr
.pointer
|| same_alloc_type
)
2798 field_type
= gfc_nonrestricted_type (field_type
);
2800 /* vtype fields can point to different types to the base type. */
2801 if (c
->ts
.type
== BT_DERIVED
2802 && c
->ts
.u
.derived
&& c
->ts
.u
.derived
->attr
.vtype
)
2803 field_type
= build_pointer_type_for_mode (TREE_TYPE (field_type
),
2806 /* Ensure that the CLASS language specific flag is set. */
2807 if (c
->ts
.type
== BT_CLASS
)
2809 if (POINTER_TYPE_P (field_type
))
2810 GFC_CLASS_TYPE_P (TREE_TYPE (field_type
)) = 1;
2812 GFC_CLASS_TYPE_P (field_type
) = 1;
2815 field
= gfc_add_field_to_struct (typenode
,
2816 get_identifier (c
->name
),
2817 field_type
, &chain
);
2819 gfc_set_decl_location (field
, &c
->loc
);
2820 else if (derived
->declared_at
.lb
)
2821 gfc_set_decl_location (field
, &derived
->declared_at
);
2823 gfc_finish_decl_attrs (field
, &c
->attr
);
2825 DECL_PACKED (field
) |= TYPE_PACKED (typenode
);
2828 if (!c
->backend_decl
)
2829 c
->backend_decl
= field
;
2831 if (c
->attr
.pointer
&& c
->attr
.dimension
2832 && !(c
->ts
.type
== BT_DERIVED
2833 && strcmp (c
->name
, "_data") == 0))
2834 GFC_DECL_PTR_ARRAY_P (c
->backend_decl
) = 1;
2837 /* Now lay out the derived type, including the fields. */
2839 TYPE_CANONICAL (typenode
) = canonical
;
2841 gfc_finish_type (typenode
);
2842 gfc_set_decl_location (TYPE_STUB_DECL (typenode
), &derived
->declared_at
);
2843 if (derived
->module
&& derived
->ns
->proc_name
2844 && derived
->ns
->proc_name
->attr
.flavor
== FL_MODULE
)
2846 if (derived
->ns
->proc_name
->backend_decl
2847 && TREE_CODE (derived
->ns
->proc_name
->backend_decl
)
2850 TYPE_CONTEXT (typenode
) = derived
->ns
->proc_name
->backend_decl
;
2851 DECL_CONTEXT (TYPE_STUB_DECL (typenode
))
2852 = derived
->ns
->proc_name
->backend_decl
;
2856 derived
->backend_decl
= typenode
;
2860 for (c
= derived
->components
; c
; c
= c
->next
)
2862 /* Do not add a caf_token field for class container components. */
2863 if ((codimen
|| coarray_flag
)
2864 && !c
->attr
.dimension
&& !c
->attr
.codimension
2865 && (c
->attr
.allocatable
|| c
->attr
.pointer
)
2866 && !derived
->attr
.is_class
)
2868 char caf_name
[GFC_MAX_SYMBOL_LEN
];
2869 gfc_component
*token
;
2870 snprintf (caf_name
, GFC_MAX_SYMBOL_LEN
, "_caf_%s", c
->name
);
2871 token
= gfc_find_component (derived
, caf_name
, true, true, NULL
);
2873 c
->caf_token
= token
->backend_decl
;
2874 TREE_NO_WARNING (c
->caf_token
) = 1;
2878 for (dt
= gfc_derived_types
; dt
; dt
= dt
->next
)
2879 gfc_copy_dt_decls_ifequal (derived
, dt
->derived
, false);
2881 return derived
->backend_decl
;
2886 gfc_return_by_reference (gfc_symbol
* sym
)
2888 if (!sym
->attr
.function
)
2891 if (sym
->attr
.dimension
)
2894 if (sym
->ts
.type
== BT_CHARACTER
2895 && !sym
->attr
.is_bind_c
2896 && (!sym
->attr
.result
2897 || !sym
->ns
->proc_name
2898 || !sym
->ns
->proc_name
->attr
.is_bind_c
))
2901 /* Possibly return complex numbers by reference for g77 compatibility.
2902 We don't do this for calls to intrinsics (as the library uses the
2903 -fno-f2c calling convention), nor for calls to functions which always
2904 require an explicit interface, as no compatibility problems can
2906 if (flag_f2c
&& sym
->ts
.type
== BT_COMPLEX
2907 && !sym
->attr
.intrinsic
&& !sym
->attr
.always_explicit
)
2914 gfc_get_mixed_entry_union (gfc_namespace
*ns
)
2918 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2919 gfc_entry_list
*el
, *el2
;
2921 gcc_assert (ns
->proc_name
->attr
.mixed_entry_master
);
2922 gcc_assert (memcmp (ns
->proc_name
->name
, "master.", 7) == 0);
2924 snprintf (name
, GFC_MAX_SYMBOL_LEN
, "munion.%s", ns
->proc_name
->name
+ 7);
2926 /* Build the type node. */
2927 type
= make_node (UNION_TYPE
);
2929 TYPE_NAME (type
) = get_identifier (name
);
2931 for (el
= ns
->entries
; el
; el
= el
->next
)
2933 /* Search for duplicates. */
2934 for (el2
= ns
->entries
; el2
!= el
; el2
= el2
->next
)
2935 if (el2
->sym
->result
== el
->sym
->result
)
2939 gfc_add_field_to_struct_1 (type
,
2940 get_identifier (el
->sym
->result
->name
),
2941 gfc_sym_type (el
->sym
->result
), &chain
);
2944 /* Finish off the type. */
2945 gfc_finish_type (type
);
2946 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type
)) = 1;
2950 /* Create a "fn spec" based on the formal arguments;
2951 cf. create_function_arglist. */
2954 create_fn_spec (gfc_symbol
*sym
, tree fntype
)
2958 gfc_formal_arglist
*f
;
2961 memset (&spec
, 0, sizeof (spec
));
2965 if (sym
->attr
.entry_master
)
2966 spec
[spec_len
++] = 'R';
2967 if (gfc_return_by_reference (sym
))
2969 gfc_symbol
*result
= sym
->result
? sym
->result
: sym
;
2971 if (result
->attr
.pointer
|| sym
->attr
.proc_pointer
)
2972 spec
[spec_len
++] = '.';
2974 spec
[spec_len
++] = 'w';
2975 if (sym
->ts
.type
== BT_CHARACTER
)
2976 spec
[spec_len
++] = 'R';
2979 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
2980 if (spec_len
< sizeof (spec
))
2982 if (!f
->sym
|| f
->sym
->attr
.pointer
|| f
->sym
->attr
.target
2983 || f
->sym
->attr
.external
|| f
->sym
->attr
.cray_pointer
2984 || (f
->sym
->ts
.type
== BT_DERIVED
2985 && (f
->sym
->ts
.u
.derived
->attr
.proc_pointer_comp
2986 || f
->sym
->ts
.u
.derived
->attr
.pointer_comp
))
2987 || (f
->sym
->ts
.type
== BT_CLASS
2988 && (CLASS_DATA (f
->sym
)->ts
.u
.derived
->attr
.proc_pointer_comp
2989 || CLASS_DATA (f
->sym
)->ts
.u
.derived
->attr
.pointer_comp
)))
2990 spec
[spec_len
++] = '.';
2991 else if (f
->sym
->attr
.intent
== INTENT_IN
)
2992 spec
[spec_len
++] = 'r';
2994 spec
[spec_len
++] = 'w';
2997 tmp
= build_tree_list (NULL_TREE
, build_string (spec_len
, spec
));
2998 tmp
= tree_cons (get_identifier ("fn spec"), tmp
, TYPE_ATTRIBUTES (fntype
));
2999 return build_type_attribute_variant (fntype
, tmp
);
3004 gfc_get_function_type (gfc_symbol
* sym
)
3007 vec
<tree
, va_gc
> *typelist
= NULL
;
3008 gfc_formal_arglist
*f
;
3010 int alternate_return
= 0;
3011 bool is_varargs
= true;
3013 /* Make sure this symbol is a function, a subroutine or the main
3015 gcc_assert (sym
->attr
.flavor
== FL_PROCEDURE
3016 || sym
->attr
.flavor
== FL_PROGRAM
);
3018 /* To avoid recursing infinitely on recursive types, we use error_mark_node
3019 so that they can be detected here and handled further down. */
3020 if (sym
->backend_decl
== NULL
)
3021 sym
->backend_decl
= error_mark_node
;
3022 else if (sym
->backend_decl
== error_mark_node
)
3023 goto arg_type_list_done
;
3024 else if (sym
->attr
.proc_pointer
)
3025 return TREE_TYPE (TREE_TYPE (sym
->backend_decl
));
3027 return TREE_TYPE (sym
->backend_decl
);
3029 if (sym
->attr
.entry_master
)
3030 /* Additional parameter for selecting an entry point. */
3031 vec_safe_push (typelist
, gfc_array_index_type
);
3038 if (arg
->ts
.type
== BT_CHARACTER
)
3039 gfc_conv_const_charlen (arg
->ts
.u
.cl
);
3041 /* Some functions we use an extra parameter for the return value. */
3042 if (gfc_return_by_reference (sym
))
3044 type
= gfc_sym_type (arg
);
3045 if (arg
->ts
.type
== BT_COMPLEX
3046 || arg
->attr
.dimension
3047 || arg
->ts
.type
== BT_CHARACTER
)
3048 type
= build_reference_type (type
);
3050 vec_safe_push (typelist
, type
);
3051 if (arg
->ts
.type
== BT_CHARACTER
)
3053 if (!arg
->ts
.deferred
)
3054 /* Transfer by value. */
3055 vec_safe_push (typelist
, gfc_charlen_type_node
);
3057 /* Deferred character lengths are transferred by reference
3058 so that the value can be returned. */
3059 vec_safe_push (typelist
, build_pointer_type(gfc_charlen_type_node
));
3063 /* Build the argument types for the function. */
3064 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
3069 /* Evaluate constant character lengths here so that they can be
3070 included in the type. */
3071 if (arg
->ts
.type
== BT_CHARACTER
)
3072 gfc_conv_const_charlen (arg
->ts
.u
.cl
);
3074 if (arg
->attr
.flavor
== FL_PROCEDURE
)
3076 type
= gfc_get_function_type (arg
);
3077 type
= build_pointer_type (type
);
3080 type
= gfc_sym_type (arg
);
3082 /* Parameter Passing Convention
3084 We currently pass all parameters by reference.
3085 Parameters with INTENT(IN) could be passed by value.
3086 The problem arises if a function is called via an implicit
3087 prototype. In this situation the INTENT is not known.
3088 For this reason all parameters to global functions must be
3089 passed by reference. Passing by value would potentially
3090 generate bad code. Worse there would be no way of telling that
3091 this code was bad, except that it would give incorrect results.
3093 Contained procedures could pass by value as these are never
3094 used without an explicit interface, and cannot be passed as
3095 actual parameters for a dummy procedure. */
3097 vec_safe_push (typelist
, type
);
3101 if (sym
->attr
.subroutine
)
3102 alternate_return
= 1;
3106 /* Add hidden string length parameters. */
3107 for (f
= gfc_sym_get_dummy_args (sym
); f
; f
= f
->next
)
3110 if (arg
&& arg
->ts
.type
== BT_CHARACTER
&& !sym
->attr
.is_bind_c
)
3112 if (!arg
->ts
.deferred
)
3113 /* Transfer by value. */
3114 type
= gfc_charlen_type_node
;
3116 /* Deferred character lengths are transferred by reference
3117 so that the value can be returned. */
3118 type
= build_pointer_type (gfc_charlen_type_node
);
3120 vec_safe_push (typelist
, type
);
3124 if (!vec_safe_is_empty (typelist
)
3125 || sym
->attr
.is_main_program
3126 || sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
3129 if (sym
->backend_decl
== error_mark_node
)
3130 sym
->backend_decl
= NULL_TREE
;
3134 if (alternate_return
)
3135 type
= integer_type_node
;
3136 else if (!sym
->attr
.function
|| gfc_return_by_reference (sym
))
3137 type
= void_type_node
;
3138 else if (sym
->attr
.mixed_entry_master
)
3139 type
= gfc_get_mixed_entry_union (sym
->ns
);
3140 else if (flag_f2c
&& sym
->ts
.type
== BT_REAL
3141 && sym
->ts
.kind
== gfc_default_real_kind
3142 && !sym
->attr
.always_explicit
)
3144 /* Special case: f2c calling conventions require that (scalar)
3145 default REAL functions return the C type double instead. f2c
3146 compatibility is only an issue with functions that don't
3147 require an explicit interface, as only these could be
3148 implemented in Fortran 77. */
3149 sym
->ts
.kind
= gfc_default_double_kind
;
3150 type
= gfc_typenode_for_spec (&sym
->ts
);
3151 sym
->ts
.kind
= gfc_default_real_kind
;
3153 else if (sym
->result
&& sym
->result
->attr
.proc_pointer
)
3154 /* Procedure pointer return values. */
3156 if (sym
->result
->attr
.result
&& strcmp (sym
->name
,"ppr@") != 0)
3158 /* Unset proc_pointer as gfc_get_function_type
3159 is called recursively. */
3160 sym
->result
->attr
.proc_pointer
= 0;
3161 type
= build_pointer_type (gfc_get_function_type (sym
->result
));
3162 sym
->result
->attr
.proc_pointer
= 1;
3165 type
= gfc_sym_type (sym
->result
);
3168 type
= gfc_sym_type (sym
);
3171 type
= build_varargs_function_type_vec (type
, typelist
);
3173 type
= build_function_type_vec (type
, typelist
);
3174 type
= create_fn_spec (sym
, type
);
3179 /* Language hooks for middle-end access to type nodes. */
3181 /* Return an integer type with BITS bits of precision,
3182 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3185 gfc_type_for_size (unsigned bits
, int unsignedp
)
3190 for (i
= 0; i
<= MAX_INT_KINDS
; ++i
)
3192 tree type
= gfc_integer_types
[i
];
3193 if (type
&& bits
== TYPE_PRECISION (type
))
3197 /* Handle TImode as a special case because it is used by some backends
3198 (e.g. ARM) even though it is not available for normal use. */
3199 #if HOST_BITS_PER_WIDE_INT >= 64
3200 if (bits
== TYPE_PRECISION (intTI_type_node
))
3201 return intTI_type_node
;
3204 if (bits
<= TYPE_PRECISION (intQI_type_node
))
3205 return intQI_type_node
;
3206 if (bits
<= TYPE_PRECISION (intHI_type_node
))
3207 return intHI_type_node
;
3208 if (bits
<= TYPE_PRECISION (intSI_type_node
))
3209 return intSI_type_node
;
3210 if (bits
<= TYPE_PRECISION (intDI_type_node
))
3211 return intDI_type_node
;
3212 if (bits
<= TYPE_PRECISION (intTI_type_node
))
3213 return intTI_type_node
;
3217 if (bits
<= TYPE_PRECISION (unsigned_intQI_type_node
))
3218 return unsigned_intQI_type_node
;
3219 if (bits
<= TYPE_PRECISION (unsigned_intHI_type_node
))
3220 return unsigned_intHI_type_node
;
3221 if (bits
<= TYPE_PRECISION (unsigned_intSI_type_node
))
3222 return unsigned_intSI_type_node
;
3223 if (bits
<= TYPE_PRECISION (unsigned_intDI_type_node
))
3224 return unsigned_intDI_type_node
;
3225 if (bits
<= TYPE_PRECISION (unsigned_intTI_type_node
))
3226 return unsigned_intTI_type_node
;
3232 /* Return a data type that has machine mode MODE. If the mode is an
3233 integer, then UNSIGNEDP selects between signed and unsigned types. */
3236 gfc_type_for_mode (machine_mode mode
, int unsignedp
)
3240 scalar_int_mode int_mode
;
3242 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
)
3243 base
= gfc_real_types
;
3244 else if (GET_MODE_CLASS (mode
) == MODE_COMPLEX_FLOAT
)
3245 base
= gfc_complex_types
;
3246 else if (is_a
<scalar_int_mode
> (mode
, &int_mode
))
3248 tree type
= gfc_type_for_size (GET_MODE_PRECISION (int_mode
), unsignedp
);
3249 return type
!= NULL_TREE
&& mode
== TYPE_MODE (type
) ? type
: NULL_TREE
;
3251 else if (GET_MODE_CLASS (mode
) == MODE_VECTOR_BOOL
3252 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
3254 unsigned int elem_bits
= vector_element_size (GET_MODE_BITSIZE (mode
),
3255 GET_MODE_NUNITS (mode
));
3256 tree bool_type
= build_nonstandard_boolean_type (elem_bits
);
3257 return build_vector_type_for_mode (bool_type
, mode
);
3259 else if (VECTOR_MODE_P (mode
)
3260 && valid_vector_subparts_p (GET_MODE_NUNITS (mode
)))
3262 machine_mode inner_mode
= GET_MODE_INNER (mode
);
3263 tree inner_type
= gfc_type_for_mode (inner_mode
, unsignedp
);
3264 if (inner_type
!= NULL_TREE
)
3265 return build_vector_type_for_mode (inner_type
, mode
);
3271 for (i
= 0; i
<= MAX_REAL_KINDS
; ++i
)
3273 tree type
= base
[i
];
3274 if (type
&& mode
== TYPE_MODE (type
))
3281 /* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
3285 gfc_get_array_descr_info (const_tree type
, struct array_descr_info
*info
)
3288 bool indirect
= false;
3289 tree etype
, ptype
, t
, base_decl
;
3290 tree data_off
, dim_off
, dtype_off
, dim_size
, elem_size
;
3291 tree lower_suboff
, upper_suboff
, stride_suboff
;
3292 tree dtype
, field
, rank_off
;
3294 if (! GFC_DESCRIPTOR_TYPE_P (type
))
3296 if (! POINTER_TYPE_P (type
))
3298 type
= TREE_TYPE (type
);
3299 if (! GFC_DESCRIPTOR_TYPE_P (type
))
3304 rank
= GFC_TYPE_ARRAY_RANK (type
);
3305 if (rank
>= (int) (sizeof (info
->dimen
) / sizeof (info
->dimen
[0])))
3308 etype
= GFC_TYPE_ARRAY_DATAPTR_TYPE (type
);
3309 gcc_assert (POINTER_TYPE_P (etype
));
3310 etype
= TREE_TYPE (etype
);
3312 /* If the type is not a scalar coarray. */
3313 if (TREE_CODE (etype
) == ARRAY_TYPE
)
3314 etype
= TREE_TYPE (etype
);
3316 /* Can't handle variable sized elements yet. */
3317 if (int_size_in_bytes (etype
) <= 0)
3319 /* Nor non-constant lower bounds in assumed shape arrays. */
3320 if (GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ASSUMED_SHAPE
3321 || GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ASSUMED_SHAPE_CONT
)
3323 for (dim
= 0; dim
< rank
; dim
++)
3324 if (GFC_TYPE_ARRAY_LBOUND (type
, dim
) == NULL_TREE
3325 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type
, dim
)) != INTEGER_CST
)
3329 memset (info
, '\0', sizeof (*info
));
3330 info
->ndimensions
= rank
;
3331 info
->ordering
= array_descr_ordering_column_major
;
3332 info
->element_type
= etype
;
3333 ptype
= build_pointer_type (gfc_array_index_type
);
3334 base_decl
= GFC_TYPE_ARRAY_BASE_DECL (type
, indirect
);
3337 base_decl
= make_node (DEBUG_EXPR_DECL
);
3338 DECL_ARTIFICIAL (base_decl
) = 1;
3339 TREE_TYPE (base_decl
) = indirect
? build_pointer_type (ptype
) : ptype
;
3340 SET_DECL_MODE (base_decl
, TYPE_MODE (TREE_TYPE (base_decl
)));
3341 GFC_TYPE_ARRAY_BASE_DECL (type
, indirect
) = base_decl
;
3343 info
->base_decl
= base_decl
;
3345 base_decl
= build1 (INDIRECT_REF
, ptype
, base_decl
);
3347 elem_size
= fold_convert (gfc_array_index_type
, TYPE_SIZE_UNIT (etype
));
3349 gfc_get_descriptor_offsets_for_info (type
, &data_off
, &dtype_off
, &dim_off
,
3350 &dim_size
, &stride_suboff
,
3351 &lower_suboff
, &upper_suboff
);
3354 if (!integer_zerop (data_off
))
3355 t
= fold_build_pointer_plus (t
, data_off
);
3356 t
= build1 (NOP_EXPR
, build_pointer_type (ptr_type_node
), t
);
3357 info
->data_location
= build1 (INDIRECT_REF
, ptr_type_node
, t
);
3358 if (GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ALLOCATABLE
)
3359 info
->allocated
= build2 (NE_EXPR
, logical_type_node
,
3360 info
->data_location
, null_pointer_node
);
3361 else if (GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_POINTER
3362 || GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_POINTER_CONT
)
3363 info
->associated
= build2 (NE_EXPR
, logical_type_node
,
3364 info
->data_location
, null_pointer_node
);
3365 if ((GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ASSUMED_RANK
3366 || GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ASSUMED_RANK_CONT
)
3367 && dwarf_version
>= 5)
3370 info
->ndimensions
= 1;
3372 if (!integer_zerop (dtype_off
))
3373 t
= fold_build_pointer_plus (t
, dtype_off
);
3374 dtype
= TYPE_MAIN_VARIANT (get_dtype_type_node ());
3375 field
= gfc_advance_chain (TYPE_FIELDS (dtype
), GFC_DTYPE_RANK
);
3376 rank_off
= byte_position (field
);
3377 if (!integer_zerop (dtype_off
))
3378 t
= fold_build_pointer_plus (t
, rank_off
);
3380 t
= build1 (NOP_EXPR
, build_pointer_type (gfc_array_index_type
), t
);
3381 t
= build1 (INDIRECT_REF
, gfc_array_index_type
, t
);
3383 t
= build0 (PLACEHOLDER_EXPR
, TREE_TYPE (dim_off
));
3384 t
= size_binop (MULT_EXPR
, t
, dim_size
);
3385 dim_off
= build2 (PLUS_EXPR
, TREE_TYPE (dim_off
), t
, dim_off
);
3388 for (dim
= 0; dim
< rank
; dim
++)
3390 t
= fold_build_pointer_plus (base_decl
,
3391 size_binop (PLUS_EXPR
,
3392 dim_off
, lower_suboff
));
3393 t
= build1 (INDIRECT_REF
, gfc_array_index_type
, t
);
3394 info
->dimen
[dim
].lower_bound
= t
;
3395 t
= fold_build_pointer_plus (base_decl
,
3396 size_binop (PLUS_EXPR
,
3397 dim_off
, upper_suboff
));
3398 t
= build1 (INDIRECT_REF
, gfc_array_index_type
, t
);
3399 info
->dimen
[dim
].upper_bound
= t
;
3400 if (GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ASSUMED_SHAPE
3401 || GFC_TYPE_ARRAY_AKIND (type
) == GFC_ARRAY_ASSUMED_SHAPE_CONT
)
3403 /* Assumed shape arrays have known lower bounds. */
3404 info
->dimen
[dim
].upper_bound
3405 = build2 (MINUS_EXPR
, gfc_array_index_type
,
3406 info
->dimen
[dim
].upper_bound
,
3407 info
->dimen
[dim
].lower_bound
);
3408 info
->dimen
[dim
].lower_bound
3409 = fold_convert (gfc_array_index_type
,
3410 GFC_TYPE_ARRAY_LBOUND (type
, dim
));
3411 info
->dimen
[dim
].upper_bound
3412 = build2 (PLUS_EXPR
, gfc_array_index_type
,
3413 info
->dimen
[dim
].lower_bound
,
3414 info
->dimen
[dim
].upper_bound
);
3416 t
= fold_build_pointer_plus (base_decl
,
3417 size_binop (PLUS_EXPR
,
3418 dim_off
, stride_suboff
));
3419 t
= build1 (INDIRECT_REF
, gfc_array_index_type
, t
);
3420 t
= build2 (MULT_EXPR
, gfc_array_index_type
, t
, elem_size
);
3421 info
->dimen
[dim
].stride
= t
;
3423 dim_off
= size_binop (PLUS_EXPR
, dim_off
, dim_size
);
3430 /* Create a type to handle vector subscripts for coarray library calls. It
3432 struct caf_vector_t {
3433 size_t nvec; // size of the vector
3440 ptrdiff_t lower_bound;
3441 ptrdiff_t upper_bound;
3446 where nvec == 0 for DIMEN_ELEMENT or DIMEN_RANGE and nvec being the vector
3447 size in case of DIMEN_VECTOR, where kind is the integer type of the vector. */
3450 gfc_get_caf_vector_type (int dim
)
3452 static tree vector_types
[GFC_MAX_DIMENSIONS
];
3453 static tree vec_type
= NULL_TREE
;
3454 tree triplet_struct_type
, vect_struct_type
, union_type
, tmp
, *chain
;
3456 if (vector_types
[dim
-1] != NULL_TREE
)
3457 return vector_types
[dim
-1];
3459 if (vec_type
== NULL_TREE
)
3462 vect_struct_type
= make_node (RECORD_TYPE
);
3463 tmp
= gfc_add_field_to_struct_1 (vect_struct_type
,
3464 get_identifier ("vector"),
3465 pvoid_type_node
, &chain
);
3466 TREE_NO_WARNING (tmp
) = 1;
3467 tmp
= gfc_add_field_to_struct_1 (vect_struct_type
,
3468 get_identifier ("kind"),
3469 integer_type_node
, &chain
);
3470 TREE_NO_WARNING (tmp
) = 1;
3471 gfc_finish_type (vect_struct_type
);
3474 triplet_struct_type
= make_node (RECORD_TYPE
);
3475 tmp
= gfc_add_field_to_struct_1 (triplet_struct_type
,
3476 get_identifier ("lower_bound"),
3477 gfc_array_index_type
, &chain
);
3478 TREE_NO_WARNING (tmp
) = 1;
3479 tmp
= gfc_add_field_to_struct_1 (triplet_struct_type
,
3480 get_identifier ("upper_bound"),
3481 gfc_array_index_type
, &chain
);
3482 TREE_NO_WARNING (tmp
) = 1;
3483 tmp
= gfc_add_field_to_struct_1 (triplet_struct_type
, get_identifier ("stride"),
3484 gfc_array_index_type
, &chain
);
3485 TREE_NO_WARNING (tmp
) = 1;
3486 gfc_finish_type (triplet_struct_type
);
3489 union_type
= make_node (UNION_TYPE
);
3490 tmp
= gfc_add_field_to_struct_1 (union_type
, get_identifier ("v"),
3491 vect_struct_type
, &chain
);
3492 TREE_NO_WARNING (tmp
) = 1;
3493 tmp
= gfc_add_field_to_struct_1 (union_type
, get_identifier ("triplet"),
3494 triplet_struct_type
, &chain
);
3495 TREE_NO_WARNING (tmp
) = 1;
3496 gfc_finish_type (union_type
);
3499 vec_type
= make_node (RECORD_TYPE
);
3500 tmp
= gfc_add_field_to_struct_1 (vec_type
, get_identifier ("nvec"),
3501 size_type_node
, &chain
);
3502 TREE_NO_WARNING (tmp
) = 1;
3503 tmp
= gfc_add_field_to_struct_1 (vec_type
, get_identifier ("u"),
3504 union_type
, &chain
);
3505 TREE_NO_WARNING (tmp
) = 1;
3506 gfc_finish_type (vec_type
);
3507 TYPE_NAME (vec_type
) = get_identifier ("caf_vector_t");
3510 tmp
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
3511 gfc_rank_cst
[dim
-1]);
3512 vector_types
[dim
-1] = build_array_type (vec_type
, tmp
);
3513 return vector_types
[dim
-1];
3518 gfc_get_caf_reference_type ()
3520 static tree reference_type
= NULL_TREE
;
3521 tree c_struct_type
, s_struct_type
, v_struct_type
, union_type
, dim_union_type
,
3522 a_struct_type
, u_union_type
, tmp
, *chain
;
3524 if (reference_type
!= NULL_TREE
)
3525 return reference_type
;
3528 c_struct_type
= make_node (RECORD_TYPE
);
3529 tmp
= gfc_add_field_to_struct_1 (c_struct_type
,
3530 get_identifier ("offset"),
3531 gfc_array_index_type
, &chain
);
3532 TREE_NO_WARNING (tmp
) = 1;
3533 tmp
= gfc_add_field_to_struct_1 (c_struct_type
,
3534 get_identifier ("caf_token_offset"),
3535 gfc_array_index_type
, &chain
);
3536 TREE_NO_WARNING (tmp
) = 1;
3537 gfc_finish_type (c_struct_type
);
3540 s_struct_type
= make_node (RECORD_TYPE
);
3541 tmp
= gfc_add_field_to_struct_1 (s_struct_type
,
3542 get_identifier ("start"),
3543 gfc_array_index_type
, &chain
);
3544 TREE_NO_WARNING (tmp
) = 1;
3545 tmp
= gfc_add_field_to_struct_1 (s_struct_type
,
3546 get_identifier ("end"),
3547 gfc_array_index_type
, &chain
);
3548 TREE_NO_WARNING (tmp
) = 1;
3549 tmp
= gfc_add_field_to_struct_1 (s_struct_type
,
3550 get_identifier ("stride"),
3551 gfc_array_index_type
, &chain
);
3552 TREE_NO_WARNING (tmp
) = 1;
3553 gfc_finish_type (s_struct_type
);
3556 v_struct_type
= make_node (RECORD_TYPE
);
3557 tmp
= gfc_add_field_to_struct_1 (v_struct_type
,
3558 get_identifier ("vector"),
3559 pvoid_type_node
, &chain
);
3560 TREE_NO_WARNING (tmp
) = 1;
3561 tmp
= gfc_add_field_to_struct_1 (v_struct_type
,
3562 get_identifier ("nvec"),
3563 size_type_node
, &chain
);
3564 TREE_NO_WARNING (tmp
) = 1;
3565 tmp
= gfc_add_field_to_struct_1 (v_struct_type
,
3566 get_identifier ("kind"),
3567 integer_type_node
, &chain
);
3568 TREE_NO_WARNING (tmp
) = 1;
3569 gfc_finish_type (v_struct_type
);
3572 union_type
= make_node (UNION_TYPE
);
3573 tmp
= gfc_add_field_to_struct_1 (union_type
, get_identifier ("s"),
3574 s_struct_type
, &chain
);
3575 TREE_NO_WARNING (tmp
) = 1;
3576 tmp
= gfc_add_field_to_struct_1 (union_type
, get_identifier ("v"),
3577 v_struct_type
, &chain
);
3578 TREE_NO_WARNING (tmp
) = 1;
3579 gfc_finish_type (union_type
);
3581 tmp
= build_range_type (gfc_array_index_type
, gfc_index_zero_node
,
3582 gfc_rank_cst
[GFC_MAX_DIMENSIONS
- 1]);
3583 dim_union_type
= build_array_type (union_type
, tmp
);
3586 a_struct_type
= make_node (RECORD_TYPE
);
3587 tmp
= gfc_add_field_to_struct_1 (a_struct_type
, get_identifier ("mode"),
3588 build_array_type (unsigned_char_type_node
,
3589 build_range_type (gfc_array_index_type
,
3590 gfc_index_zero_node
,
3591 gfc_rank_cst
[GFC_MAX_DIMENSIONS
- 1])),
3593 TREE_NO_WARNING (tmp
) = 1;
3594 tmp
= gfc_add_field_to_struct_1 (a_struct_type
,
3595 get_identifier ("static_array_type"),
3596 integer_type_node
, &chain
);
3597 TREE_NO_WARNING (tmp
) = 1;
3598 tmp
= gfc_add_field_to_struct_1 (a_struct_type
, get_identifier ("dim"),
3599 dim_union_type
, &chain
);
3600 TREE_NO_WARNING (tmp
) = 1;
3601 gfc_finish_type (a_struct_type
);
3604 u_union_type
= make_node (UNION_TYPE
);
3605 tmp
= gfc_add_field_to_struct_1 (u_union_type
, get_identifier ("c"),
3606 c_struct_type
, &chain
);
3607 TREE_NO_WARNING (tmp
) = 1;
3608 tmp
= gfc_add_field_to_struct_1 (u_union_type
, get_identifier ("a"),
3609 a_struct_type
, &chain
);
3610 TREE_NO_WARNING (tmp
) = 1;
3611 gfc_finish_type (u_union_type
);
3614 reference_type
= make_node (RECORD_TYPE
);
3615 tmp
= gfc_add_field_to_struct_1 (reference_type
, get_identifier ("next"),
3616 build_pointer_type (reference_type
), &chain
);
3617 TREE_NO_WARNING (tmp
) = 1;
3618 tmp
= gfc_add_field_to_struct_1 (reference_type
, get_identifier ("type"),
3619 integer_type_node
, &chain
);
3620 TREE_NO_WARNING (tmp
) = 1;
3621 tmp
= gfc_add_field_to_struct_1 (reference_type
, get_identifier ("item_size"),
3622 size_type_node
, &chain
);
3623 TREE_NO_WARNING (tmp
) = 1;
3624 tmp
= gfc_add_field_to_struct_1 (reference_type
, get_identifier ("u"),
3625 u_union_type
, &chain
);
3626 TREE_NO_WARNING (tmp
) = 1;
3627 gfc_finish_type (reference_type
);
3628 TYPE_NAME (reference_type
) = get_identifier ("caf_reference_t");
3630 return reference_type
;
3633 #include "gt-fortran-trans-types.h"