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