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