]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/trans-types.c
tree-inline.c (eni_inlining_weights): Remove.
[thirdparty/gcc.git] / gcc / fortran / trans-types.c
CommitLineData
6de9cd9a 1/* Backend support for Fortran 95 basic types and derived types.
fa502cb2
PT
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010
835aac92 4 Free Software Foundation, Inc.
6de9cd9a
DN
5 Contributed by Paul Brook <paul@nowt.org>
6 and Steven Bosscher <s.bosscher@student.tudelft.nl>
7
9fc4d79b 8This file is part of GCC.
6de9cd9a 9
9fc4d79b
TS
10GCC is free software; you can redistribute it and/or modify it under
11the terms of the GNU General Public License as published by the Free
d234d788 12Software Foundation; either version 3, or (at your option) any later
9fc4d79b 13version.
6de9cd9a 14
9fc4d79b
TS
15GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18for more details.
6de9cd9a
DN
19
20You should have received a copy of the GNU General Public License
d234d788
NC
21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
23
24/* trans-types.c -- gfortran backend types */
25
26#include "config.h"
27#include "system.h"
28#include "coretypes.h"
29#include "tree.h"
a8b3b0b6 30#include "langhooks.h"
5e8e542f
RH
31#include "tm.h"
32#include "target.h"
6de9cd9a
DN
33#include "ggc.h"
34#include "toplev.h"
6de9cd9a
DN
35#include "gfortran.h"
36#include "trans.h"
37#include "trans-types.h"
38#include "trans-const.h"
5e8e542f 39#include "real.h"
08789087 40#include "flags.h"
fad0afd7 41#include "dwarf2out.h"
6de9cd9a
DN
42\f
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
a8b3b0b6
CR
54/* array of structs so we don't have to worry about xmalloc or free */
55CInteropKind_t c_interop_kinds_table[ISOCBINDING_NUMBER];
56
6de9cd9a 57tree gfc_array_index_type;
b4838d29 58tree gfc_array_range_type;
7e6de2a5 59tree gfc_character1_type_node;
6de9cd9a 60tree pvoid_type_node;
10174ddf 61tree prvoid_type_node;
6de9cd9a
DN
62tree ppvoid_type_node;
63tree pchar_type_node;
089db47d 64tree pfunc_type_node;
7e6de2a5 65
d7177ab2 66tree gfc_charlen_type_node;
6de9cd9a 67
e2cad04b 68static GTY(()) tree gfc_desc_dim_type;
6de9cd9a 69static GTY(()) tree gfc_max_array_element_size;
10174ddf 70static GTY(()) tree gfc_array_descriptor_base[2 * GFC_MAX_DIMENSIONS];
6de9cd9a 71
5e8e542f
RH
72/* Arrays for all integral and real kinds. We'll fill this in at runtime
73 after the target has a chance to process command-line options. */
74
75#define MAX_INT_KINDS 5
76gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
77gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
e2cad04b
RH
78static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
79static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
5e8e542f 80
e8f35d4d 81#define MAX_REAL_KINDS 5
5e8e542f 82gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
e2cad04b
RH
83static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
84static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
5e8e542f 85
374929b2
FXC
86#define MAX_CHARACTER_KINDS 2
87gfc_character_info gfc_character_kinds[MAX_CHARACTER_KINDS + 1];
88static GTY(()) tree gfc_character_types[MAX_CHARACTER_KINDS + 1];
89static GTY(()) tree gfc_pcharacter_types[MAX_CHARACTER_KINDS + 1];
90
66e4ab31 91
5e8e542f
RH
92/* The integer kind to use for array indices. This will be set to the
93 proper value based on target information from the backend. */
94
95int gfc_index_integer_kind;
96
97/* The default kinds of the various types. */
98
9d64df18 99int gfc_default_integer_kind;
f4e7375a 100int gfc_max_integer_kind;
9d64df18
TS
101int gfc_default_real_kind;
102int gfc_default_double_kind;
103int gfc_default_character_kind;
104int gfc_default_logical_kind;
105int gfc_default_complex_kind;
e8525382 106int gfc_c_int_kind;
5e8e542f 107
4fec64b0
JD
108/* The kind size used for record offsets. If the target system supports
109 kind=8, this will be set to 8, otherwise it is set to 4. */
014ec6ee 110int gfc_intio_kind;
4fec64b0 111
f1412ca5
FXC
112/* The integer kind used to store character lengths. */
113int gfc_charlen_int_kind;
114
39f87c03
FXC
115/* The size of the numeric storage unit and character storage unit. */
116int gfc_numeric_storage_size;
117int gfc_character_storage_size;
118
a8b3b0b6 119
17b1d2a0 120gfc_try
a8b3b0b6
CR
121gfc_check_any_c_kind (gfc_typespec *ts)
122{
123 int i;
124
125 for (i = 0; i < ISOCBINDING_NUMBER; i++)
126 {
127 /* Check for any C interoperable kind for the given type/kind in ts.
128 This can be used after verify_c_interop to make sure that the
129 Fortran kind being used exists in at least some form for C. */
130 if (c_interop_kinds_table[i].f90_type == ts->type &&
131 c_interop_kinds_table[i].value == ts->kind)
132 return SUCCESS;
133 }
134
135 return FAILURE;
136}
137
138
139static int
140get_real_kind_from_node (tree type)
141{
142 int i;
143
144 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
145 if (gfc_real_kinds[i].mode_precision == TYPE_PRECISION (type))
146 return gfc_real_kinds[i].kind;
147
148 return -4;
149}
150
151static int
152get_int_kind_from_node (tree type)
153{
154 int i;
155
156 if (!type)
157 return -2;
158
159 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
160 if (gfc_integer_kinds[i].bit_size == TYPE_PRECISION (type))
161 return gfc_integer_kinds[i].kind;
162
163 return -1;
164}
165
e0a6661b
FXC
166/* Return a typenode for the "standard" C type with a given name. */
167static tree
168get_typenode_from_name (const char *name)
169{
170 if (name == NULL || *name == '\0')
171 return NULL_TREE;
172
173 if (strcmp (name, "char") == 0)
174 return char_type_node;
175 if (strcmp (name, "unsigned char") == 0)
176 return unsigned_char_type_node;
177 if (strcmp (name, "signed char") == 0)
178 return signed_char_type_node;
179
180 if (strcmp (name, "short int") == 0)
181 return short_integer_type_node;
182 if (strcmp (name, "short unsigned int") == 0)
183 return short_unsigned_type_node;
184
185 if (strcmp (name, "int") == 0)
186 return integer_type_node;
187 if (strcmp (name, "unsigned int") == 0)
188 return unsigned_type_node;
189
190 if (strcmp (name, "long int") == 0)
191 return long_integer_type_node;
192 if (strcmp (name, "long unsigned int") == 0)
193 return long_unsigned_type_node;
194
195 if (strcmp (name, "long long int") == 0)
196 return long_long_integer_type_node;
197 if (strcmp (name, "long long unsigned int") == 0)
198 return long_long_unsigned_type_node;
199
200 gcc_unreachable ();
201}
202
203static int
204get_int_kind_from_name (const char *name)
205{
206 return get_int_kind_from_node (get_typenode_from_name (name));
207}
208
209
210/* Get the kind number corresponding to an integer of given size,
211 following the required return values for ISO_FORTRAN_ENV INT* constants:
212 -2 is returned if we support a kind of larger size, -1 otherwise. */
213int
214gfc_get_int_kind_from_width_isofortranenv (int size)
215{
216 int i;
217
218 /* Look for a kind with matching storage size. */
219 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
220 if (gfc_integer_kinds[i].bit_size == size)
221 return gfc_integer_kinds[i].kind;
222
223 /* Look for a kind with larger storage size. */
224 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
225 if (gfc_integer_kinds[i].bit_size > size)
226 return -2;
227
228 return -1;
229}
230
231/* Get the kind number corresponding to a real of given storage size,
232 following the required return values for ISO_FORTRAN_ENV REAL* constants:
233 -2 is returned if we support a kind of larger size, -1 otherwise. */
234int
235gfc_get_real_kind_from_width_isofortranenv (int size)
236{
237 int i;
238
239 size /= 8;
240
241 /* Look for a kind with matching storage size. */
242 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
243 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) == size)
244 return gfc_real_kinds[i].kind;
245
246 /* Look for a kind with larger storage size. */
247 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
248 if (int_size_in_bytes (gfc_get_real_type (gfc_real_kinds[i].kind)) > size)
249 return -2;
250
251 return -1;
252}
253
254
255
a8b3b0b6
CR
256static int
257get_int_kind_from_width (int size)
258{
259 int i;
260
261 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
262 if (gfc_integer_kinds[i].bit_size == size)
263 return gfc_integer_kinds[i].kind;
264
265 return -2;
266}
267
268static int
269get_int_kind_from_minimal_width (int size)
270{
271 int i;
272
273 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
274 if (gfc_integer_kinds[i].bit_size >= size)
275 return gfc_integer_kinds[i].kind;
276
277 return -2;
278}
279
280
281/* Generate the CInteropKind_t objects for the C interoperable
282 kinds. */
283
284static
285void init_c_interop_kinds (void)
286{
287 int i;
a8b3b0b6
CR
288
289 /* init all pointers in the list to NULL */
290 for (i = 0; i < ISOCBINDING_NUMBER; i++)
291 {
292 /* Initialize the name and value fields. */
293 c_interop_kinds_table[i].name[0] = '\0';
294 c_interop_kinds_table[i].value = -100;
295 c_interop_kinds_table[i].f90_type = BT_UNKNOWN;
296 }
297
21684705 298#define NAMED_INTCST(a,b,c,d) \
a8b3b0b6
CR
299 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
300 c_interop_kinds_table[a].f90_type = BT_INTEGER; \
301 c_interop_kinds_table[a].value = c;
302#define NAMED_REALCST(a,b,c) \
303 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
304 c_interop_kinds_table[a].f90_type = BT_REAL; \
305 c_interop_kinds_table[a].value = c;
306#define NAMED_CMPXCST(a,b,c) \
307 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
308 c_interop_kinds_table[a].f90_type = BT_COMPLEX; \
309 c_interop_kinds_table[a].value = c;
310#define NAMED_LOGCST(a,b,c) \
311 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
312 c_interop_kinds_table[a].f90_type = BT_LOGICAL; \
313 c_interop_kinds_table[a].value = c;
314#define NAMED_CHARKNDCST(a,b,c) \
315 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
316 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
317 c_interop_kinds_table[a].value = c;
318#define NAMED_CHARCST(a,b,c) \
319 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
320 c_interop_kinds_table[a].f90_type = BT_CHARACTER; \
321 c_interop_kinds_table[a].value = c;
322#define DERIVED_TYPE(a,b,c) \
323 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
324 c_interop_kinds_table[a].f90_type = BT_DERIVED; \
325 c_interop_kinds_table[a].value = c;
326#define PROCEDURE(a,b) \
327 strncpy (c_interop_kinds_table[a].name, b, strlen(b) + 1); \
328 c_interop_kinds_table[a].f90_type = BT_PROCEDURE; \
329 c_interop_kinds_table[a].value = 0;
330#include "iso-c-binding.def"
331}
332
333
5e8e542f
RH
334/* Query the target to determine which machine modes are available for
335 computation. Choose KIND numbers for them. */
336
337void
338gfc_init_kinds (void)
339{
09639a83 340 unsigned int mode;
374929b2 341 int i_index, r_index, kind;
5e8e542f
RH
342 bool saw_i4 = false, saw_i8 = false;
343 bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
344
345 for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
346 {
347 int kind, bitsize;
348
09639a83 349 if (!targetm.scalar_mode_supported_p ((enum machine_mode) mode))
5e8e542f
RH
350 continue;
351
04204c2f
RH
352 /* The middle end doesn't support constants larger than 2*HWI.
353 Perhaps the target hook shouldn't have accepted these either,
354 but just to be safe... */
355 bitsize = GET_MODE_BITSIZE (mode);
356 if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
357 continue;
358
6e45f57b 359 gcc_assert (i_index != MAX_INT_KINDS);
5e8e542f
RH
360
361 /* Let the kind equal the bit size divided by 8. This insulates the
362 programmer from the underlying byte size. */
5e8e542f
RH
363 kind = bitsize / 8;
364
365 if (kind == 4)
366 saw_i4 = true;
367 if (kind == 8)
368 saw_i8 = true;
369
370 gfc_integer_kinds[i_index].kind = kind;
371 gfc_integer_kinds[i_index].radix = 2;
372 gfc_integer_kinds[i_index].digits = bitsize - 1;
373 gfc_integer_kinds[i_index].bit_size = bitsize;
374
375 gfc_logical_kinds[i_index].kind = kind;
376 gfc_logical_kinds[i_index].bit_size = bitsize;
377
378 i_index += 1;
379 }
380
014ec6ee 381 /* Set the kind used to match GFC_INT_IO in libgfortran. This is
4fec64b0
JD
382 used for large file access. */
383
384 if (saw_i8)
014ec6ee 385 gfc_intio_kind = 8;
4fec64b0 386 else
014ec6ee 387 gfc_intio_kind = 4;
4fec64b0
JD
388
389 /* If we do not at least have kind = 4, everything is pointless. */
390 gcc_assert(saw_i4);
391
f4e7375a
SK
392 /* Set the maximum integer kind. Used with at least BOZ constants. */
393 gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
394
5e8e542f
RH
395 for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
396 {
09639a83
ILT
397 const struct real_format *fmt =
398 REAL_MODE_FORMAT ((enum machine_mode) mode);
5e8e542f
RH
399 int kind;
400
401 if (fmt == NULL)
402 continue;
09639a83 403 if (!targetm.scalar_mode_supported_p ((enum machine_mode) mode))
5e8e542f
RH
404 continue;
405
216ac520
SE
406 /* Only let float/double/long double go through because the fortran
407 library assumes these are the only floating point types. */
408
409 if (mode != TYPE_MODE (float_type_node)
410 && (mode != TYPE_MODE (double_type_node))
411 && (mode != TYPE_MODE (long_double_type_node)))
412 continue;
413
5e8e542f
RH
414 /* Let the kind equal the precision divided by 8, rounding up. Again,
415 this insulates the programmer from the underlying byte size.
416
417 Also, it effectively deals with IEEE extended formats. There, the
418 total size of the type may equal 16, but it's got 6 bytes of padding
419 and the increased size can get in the way of a real IEEE quad format
420 which may also be supported by the target.
421
422 We round up so as to handle IA-64 __floatreg (RFmode), which is an
423 82 bit type. Not to be confused with __float80 (XFmode), which is
424 an 80 bit type also supported by IA-64. So XFmode should come out
425 to be kind=10, and RFmode should come out to be kind=11. Egads. */
426
427 kind = (GET_MODE_PRECISION (mode) + 7) / 8;
428
429 if (kind == 4)
430 saw_r4 = true;
431 if (kind == 8)
432 saw_r8 = true;
433 if (kind == 16)
434 saw_r16 = true;
435
df2fba9e 436 /* Careful we don't stumble a weird internal mode. */
6e45f57b 437 gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
5e8e542f 438 /* Or have too many modes for the allocated space. */
6e45f57b 439 gcc_assert (r_index != MAX_REAL_KINDS);
5e8e542f
RH
440
441 gfc_real_kinds[r_index].kind = kind;
442 gfc_real_kinds[r_index].radix = fmt->b;
443 gfc_real_kinds[r_index].digits = fmt->p;
444 gfc_real_kinds[r_index].min_exponent = fmt->emin;
445 gfc_real_kinds[r_index].max_exponent = fmt->emax;
c69cb92f
AM
446 if (fmt->pnan < fmt->p)
447 /* This is an IBM extended double format (or the MIPS variant)
448 made up of two IEEE doubles. The value of the long double is
449 the sum of the values of the two parts. The most significant
450 part is required to be the value of the long double rounded
451 to the nearest double. If we use emax of 1024 then we can't
452 represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
453 rounding will make the most significant part overflow. */
454 gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
e2cad04b 455 gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
5e8e542f
RH
456 r_index += 1;
457 }
458
459 /* Choose the default integer kind. We choose 4 unless the user
460 directs us otherwise. */
3ae9eb27 461 if (gfc_option.flag_default_integer)
5e8e542f
RH
462 {
463 if (!saw_i8)
3ae9eb27 464 fatal_error ("integer kind=8 not available for -fdefault-integer-8 option");
9d64df18 465 gfc_default_integer_kind = 8;
39f87c03
FXC
466
467 /* Even if the user specified that the default integer kind be 8,
df2fba9e 468 the numeric storage size isn't 64. In this case, a warning will
39f87c03
FXC
469 be issued when NUMERIC_STORAGE_SIZE is used. */
470 gfc_numeric_storage_size = 4 * 8;
5e8e542f
RH
471 }
472 else if (saw_i4)
39f87c03
FXC
473 {
474 gfc_default_integer_kind = 4;
475 gfc_numeric_storage_size = 4 * 8;
476 }
5e8e542f 477 else
39f87c03
FXC
478 {
479 gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
480 gfc_numeric_storage_size = gfc_integer_kinds[i_index - 1].bit_size;
481 }
5e8e542f
RH
482
483 /* Choose the default real kind. Again, we choose 4 when possible. */
3ae9eb27 484 if (gfc_option.flag_default_real)
5e8e542f
RH
485 {
486 if (!saw_r8)
3ae9eb27 487 fatal_error ("real kind=8 not available for -fdefault-real-8 option");
9d64df18 488 gfc_default_real_kind = 8;
5e8e542f
RH
489 }
490 else if (saw_r4)
9d64df18 491 gfc_default_real_kind = 4;
5e8e542f 492 else
9d64df18 493 gfc_default_real_kind = gfc_real_kinds[0].kind;
5e8e542f 494
3ae9eb27
SK
495 /* Choose the default double kind. If -fdefault-real and -fdefault-double
496 are specified, we use kind=8, if it's available. If -fdefault-real is
497 specified without -fdefault-double, we use kind=16, if it's available.
498 Otherwise we do not change anything. */
499 if (gfc_option.flag_default_double && !gfc_option.flag_default_real)
500 fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8");
501
502 if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8)
503 gfc_default_double_kind = 8;
504 else if (gfc_option.flag_default_real && saw_r16)
9d64df18 505 gfc_default_double_kind = 16;
5e8e542f 506 else if (saw_r4 && saw_r8)
9d64df18 507 gfc_default_double_kind = 8;
5e8e542f
RH
508 else
509 {
510 /* F95 14.6.3.1: A nonpointer scalar object of type double precision
511 real ... occupies two contiguous numeric storage units.
512
513 Therefore we must be supplied a kind twice as large as we chose
514 for single precision. There are loopholes, in that double
515 precision must *occupy* two storage units, though it doesn't have
516 to *use* two storage units. Which means that you can make this
517 kind artificially wide by padding it. But at present there are
518 no GCC targets for which a two-word type does not exist, so we
519 just let gfc_validate_kind abort and tell us if something breaks. */
520
9d64df18
TS
521 gfc_default_double_kind
522 = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
5e8e542f
RH
523 }
524
525 /* The default logical kind is constrained to be the same as the
526 default integer kind. Similarly with complex and real. */
9d64df18
TS
527 gfc_default_logical_kind = gfc_default_integer_kind;
528 gfc_default_complex_kind = gfc_default_real_kind;
5e8e542f 529
374929b2
FXC
530 /* We only have two character kinds: ASCII and UCS-4.
531 ASCII corresponds to a 8-bit integer type, if one is available.
532 UCS-4 corresponds to a 32-bit integer type, if one is available. */
533 i_index = 0;
534 if ((kind = get_int_kind_from_width (8)) > 0)
535 {
536 gfc_character_kinds[i_index].kind = kind;
537 gfc_character_kinds[i_index].bit_size = 8;
538 gfc_character_kinds[i_index].name = "ascii";
539 i_index++;
540 }
541 if ((kind = get_int_kind_from_width (32)) > 0)
542 {
543 gfc_character_kinds[i_index].kind = kind;
544 gfc_character_kinds[i_index].bit_size = 32;
545 gfc_character_kinds[i_index].name = "iso_10646";
546 i_index++;
547 }
548
5e8e542f 549 /* Choose the smallest integer kind for our default character. */
374929b2 550 gfc_default_character_kind = gfc_character_kinds[0].kind;
39f87c03 551 gfc_character_storage_size = gfc_default_character_kind * 8;
5e8e542f
RH
552
553 /* Choose the integer kind the same size as "void*" for our index kind. */
554 gfc_index_integer_kind = POINTER_SIZE / 8;
e8525382
SK
555 /* Pick a kind the same size as the C "int" type. */
556 gfc_c_int_kind = INT_TYPE_SIZE / 8;
a8b3b0b6
CR
557
558 /* initialize the C interoperable kinds */
559 init_c_interop_kinds();
5e8e542f
RH
560}
561
5e8e542f
RH
562/* Make sure that a valid kind is present. Returns an index into the
563 associated kinds array, -1 if the kind is not present. */
564
565static int
566validate_integer (int kind)
567{
568 int i;
569
570 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
571 if (gfc_integer_kinds[i].kind == kind)
572 return i;
573
574 return -1;
575}
576
577static int
578validate_real (int kind)
579{
580 int i;
581
582 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
583 if (gfc_real_kinds[i].kind == kind)
584 return i;
585
586 return -1;
587}
588
589static int
590validate_logical (int kind)
591{
592 int i;
593
594 for (i = 0; gfc_logical_kinds[i].kind; i++)
595 if (gfc_logical_kinds[i].kind == kind)
596 return i;
597
598 return -1;
599}
600
601static int
602validate_character (int kind)
603{
374929b2
FXC
604 int i;
605
606 for (i = 0; gfc_character_kinds[i].kind; i++)
607 if (gfc_character_kinds[i].kind == kind)
608 return i;
609
610 return -1;
5e8e542f
RH
611}
612
613/* Validate a kind given a basic type. The return value is the same
614 for the child functions, with -1 indicating nonexistence of the
615 type. If MAY_FAIL is false, then -1 is never returned, and we ICE. */
616
617int
618gfc_validate_kind (bt type, int kind, bool may_fail)
619{
620 int rc;
621
622 switch (type)
623 {
624 case BT_REAL: /* Fall through */
625 case BT_COMPLEX:
626 rc = validate_real (kind);
627 break;
628 case BT_INTEGER:
629 rc = validate_integer (kind);
630 break;
631 case BT_LOGICAL:
632 rc = validate_logical (kind);
633 break;
634 case BT_CHARACTER:
635 rc = validate_character (kind);
636 break;
637
638 default:
639 gfc_internal_error ("gfc_validate_kind(): Got bad type");
640 }
641
642 if (rc < 0 && !may_fail)
643 gfc_internal_error ("gfc_validate_kind(): Got bad kind");
644
645 return rc;
646}
647
648
e2cad04b
RH
649/* Four subroutines of gfc_init_types. Create type nodes for the given kind.
650 Reuse common type nodes where possible. Recognize if the kind matches up
651 with a C type. This will be used later in determining which routines may
652 be scarfed from libm. */
653
654static tree
655gfc_build_int_type (gfc_integer_info *info)
656{
657 int mode_precision = info->bit_size;
658
659 if (mode_precision == CHAR_TYPE_SIZE)
660 info->c_char = 1;
661 if (mode_precision == SHORT_TYPE_SIZE)
662 info->c_short = 1;
663 if (mode_precision == INT_TYPE_SIZE)
664 info->c_int = 1;
665 if (mode_precision == LONG_TYPE_SIZE)
666 info->c_long = 1;
667 if (mode_precision == LONG_LONG_TYPE_SIZE)
668 info->c_long_long = 1;
669
670 if (TYPE_PRECISION (intQI_type_node) == mode_precision)
671 return intQI_type_node;
672 if (TYPE_PRECISION (intHI_type_node) == mode_precision)
673 return intHI_type_node;
674 if (TYPE_PRECISION (intSI_type_node) == mode_precision)
675 return intSI_type_node;
676 if (TYPE_PRECISION (intDI_type_node) == mode_precision)
677 return intDI_type_node;
678 if (TYPE_PRECISION (intTI_type_node) == mode_precision)
679 return intTI_type_node;
680
681 return make_signed_type (mode_precision);
682}
683
0a05c536 684tree
374929b2
FXC
685gfc_build_uint_type (int size)
686{
687 if (size == CHAR_TYPE_SIZE)
688 return unsigned_char_type_node;
689 if (size == SHORT_TYPE_SIZE)
690 return short_unsigned_type_node;
691 if (size == INT_TYPE_SIZE)
692 return unsigned_type_node;
693 if (size == LONG_TYPE_SIZE)
694 return long_unsigned_type_node;
695 if (size == LONG_LONG_TYPE_SIZE)
696 return long_long_unsigned_type_node;
697
698 return make_unsigned_type (size);
699}
700
701
e2cad04b
RH
702static tree
703gfc_build_real_type (gfc_real_info *info)
704{
705 int mode_precision = info->mode_precision;
706 tree new_type;
707
708 if (mode_precision == FLOAT_TYPE_SIZE)
709 info->c_float = 1;
710 if (mode_precision == DOUBLE_TYPE_SIZE)
711 info->c_double = 1;
712 if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
713 info->c_long_double = 1;
714
715 if (TYPE_PRECISION (float_type_node) == mode_precision)
716 return float_type_node;
717 if (TYPE_PRECISION (double_type_node) == mode_precision)
718 return double_type_node;
719 if (TYPE_PRECISION (long_double_type_node) == mode_precision)
720 return long_double_type_node;
721
722 new_type = make_node (REAL_TYPE);
723 TYPE_PRECISION (new_type) = mode_precision;
724 layout_type (new_type);
725 return new_type;
726}
727
728static tree
729gfc_build_complex_type (tree scalar_type)
730{
731 tree new_type;
732
733 if (scalar_type == NULL)
734 return NULL;
735 if (scalar_type == float_type_node)
736 return complex_float_type_node;
737 if (scalar_type == double_type_node)
738 return complex_double_type_node;
739 if (scalar_type == long_double_type_node)
740 return complex_long_double_type_node;
741
742 new_type = make_node (COMPLEX_TYPE);
743 TREE_TYPE (new_type) = scalar_type;
744 layout_type (new_type);
745 return new_type;
746}
747
748static tree
749gfc_build_logical_type (gfc_logical_info *info)
750{
751 int bit_size = info->bit_size;
752 tree new_type;
753
754 if (bit_size == BOOL_TYPE_SIZE)
755 {
756 info->c_bool = 1;
757 return boolean_type_node;
758 }
759
760 new_type = make_unsigned_type (bit_size);
761 TREE_SET_CODE (new_type, BOOLEAN_TYPE);
762 TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
763 TYPE_PRECISION (new_type) = 1;
764
765 return new_type;
766}
767
e0a6661b 768
e2cad04b
RH
769#if 0
770/* Return the bit size of the C "size_t". */
771
772static unsigned int
773c_size_t_size (void)
774{
775#ifdef SIZE_TYPE
776 if (strcmp (SIZE_TYPE, "unsigned int") == 0)
777 return INT_TYPE_SIZE;
778 if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
779 return LONG_TYPE_SIZE;
780 if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
781 return SHORT_TYPE_SIZE;
6e45f57b 782 gcc_unreachable ();
e2cad04b
RH
783#else
784 return LONG_TYPE_SIZE;
785#endif
786}
787#endif
788
6de9cd9a
DN
789/* Create the backend type nodes. We map them to their
790 equivalent C type, at least for now. We also give
791 names to the types here, and we push them in the
792 global binding level context.*/
c3e8c6b8 793
6de9cd9a
DN
794void
795gfc_init_types (void)
796{
e5008df7 797 char name_buf[18];
e2cad04b
RH
798 int index;
799 tree type;
6de9cd9a
DN
800 unsigned n;
801 unsigned HOST_WIDE_INT hi;
802 unsigned HOST_WIDE_INT lo;
803
e2cad04b 804 /* Create and name the types. */
6de9cd9a 805#define PUSH_TYPE(name, node) \
c2255bc4
AH
806 pushdecl (build_decl (input_location, \
807 TYPE_DECL, get_identifier (name), node))
6de9cd9a 808
e2cad04b
RH
809 for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
810 {
811 type = gfc_build_int_type (&gfc_integer_kinds[index]);
dead0bae
JJ
812 /* Ensure integer(kind=1) doesn't have TYPE_STRING_FLAG set. */
813 if (TYPE_STRING_FLAG (type))
814 type = make_signed_type (gfc_integer_kinds[index].bit_size);
e2cad04b 815 gfc_integer_types[index] = type;
40373aa6 816 snprintf (name_buf, sizeof(name_buf), "integer(kind=%d)",
e2cad04b
RH
817 gfc_integer_kinds[index].kind);
818 PUSH_TYPE (name_buf, type);
819 }
6de9cd9a 820
e2cad04b
RH
821 for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
822 {
823 type = gfc_build_logical_type (&gfc_logical_kinds[index]);
824 gfc_logical_types[index] = type;
40373aa6 825 snprintf (name_buf, sizeof(name_buf), "logical(kind=%d)",
e2cad04b
RH
826 gfc_logical_kinds[index].kind);
827 PUSH_TYPE (name_buf, type);
828 }
6de9cd9a 829
e2cad04b
RH
830 for (index = 0; gfc_real_kinds[index].kind != 0; index++)
831 {
832 type = gfc_build_real_type (&gfc_real_kinds[index]);
833 gfc_real_types[index] = type;
40373aa6 834 snprintf (name_buf, sizeof(name_buf), "real(kind=%d)",
e2cad04b
RH
835 gfc_real_kinds[index].kind);
836 PUSH_TYPE (name_buf, type);
837
838 type = gfc_build_complex_type (type);
839 gfc_complex_types[index] = type;
40373aa6 840 snprintf (name_buf, sizeof(name_buf), "complex(kind=%d)",
e2cad04b
RH
841 gfc_real_kinds[index].kind);
842 PUSH_TYPE (name_buf, type);
843 }
6de9cd9a 844
374929b2
FXC
845 for (index = 0; gfc_character_kinds[index].kind != 0; ++index)
846 {
847 type = gfc_build_uint_type (gfc_character_kinds[index].bit_size);
848 type = build_qualified_type (type, TYPE_UNQUALIFIED);
849 snprintf (name_buf, sizeof(name_buf), "character(kind=%d)",
850 gfc_character_kinds[index].kind);
851 PUSH_TYPE (name_buf, type);
852 gfc_character_types[index] = type;
853 gfc_pcharacter_types[index] = build_pointer_type (type);
854 }
855 gfc_character1_type_node = gfc_character_types[0];
6de9cd9a
DN
856
857 PUSH_TYPE ("byte", unsigned_char_type_node);
858 PUSH_TYPE ("void", void_type_node);
859
860 /* DBX debugging output gets upset if these aren't set. */
861 if (!TYPE_NAME (integer_type_node))
862 PUSH_TYPE ("c_integer", integer_type_node);
863 if (!TYPE_NAME (char_type_node))
864 PUSH_TYPE ("c_char", char_type_node);
e2cad04b 865
6de9cd9a
DN
866#undef PUSH_TYPE
867
868 pvoid_type_node = build_pointer_type (void_type_node);
10174ddf 869 prvoid_type_node = build_qualified_type (pvoid_type_node, TYPE_QUAL_RESTRICT);
6de9cd9a
DN
870 ppvoid_type_node = build_pointer_type (pvoid_type_node);
871 pchar_type_node = build_pointer_type (gfc_character1_type_node);
089db47d
CR
872 pfunc_type_node
873 = build_pointer_type (build_function_type (void_type_node, NULL_TREE));
6de9cd9a 874
6de9cd9a 875 gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
b4838d29
ZD
876 /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
877 since this function is called before gfc_init_constants. */
878 gfc_array_range_type
879 = build_range_type (gfc_array_index_type,
880 build_int_cst (gfc_array_index_type, 0),
881 NULL_TREE);
6de9cd9a
DN
882
883 /* The maximum array element size that can be handled is determined
884 by the number of bits available to store this field in the array
885 descriptor. */
886
e2cad04b
RH
887 n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
888 lo = ~ (unsigned HOST_WIDE_INT) 0;
889 if (n > HOST_BITS_PER_WIDE_INT)
890 hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
6de9cd9a 891 else
e2cad04b 892 hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
7d60be94
NS
893 gfc_max_array_element_size
894 = build_int_cst_wide (long_unsigned_type_node, lo, hi);
6de9cd9a
DN
895
896 size_type_node = gfc_array_index_type;
6de9cd9a 897
e2cad04b 898 boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
7d60be94
NS
899 boolean_true_node = build_int_cst (boolean_type_node, 1);
900 boolean_false_node = build_int_cst (boolean_type_node, 0);
e2cad04b
RH
901
902 /* ??? Shouldn't this be based on gfc_index_integer_kind or so? */
f1412ca5
FXC
903 gfc_charlen_int_kind = 4;
904 gfc_charlen_type_node = gfc_get_int_type (gfc_charlen_int_kind);
6de9cd9a
DN
905}
906
e2cad04b 907/* Get the type node for the given type and kind. */
c3e8c6b8 908
6de9cd9a
DN
909tree
910gfc_get_int_type (int kind)
911{
644cb69f
FXC
912 int index = gfc_validate_kind (BT_INTEGER, kind, true);
913 return index < 0 ? 0 : gfc_integer_types[index];
6de9cd9a
DN
914}
915
6de9cd9a
DN
916tree
917gfc_get_real_type (int kind)
918{
644cb69f
FXC
919 int index = gfc_validate_kind (BT_REAL, kind, true);
920 return index < 0 ? 0 : gfc_real_types[index];
6de9cd9a
DN
921}
922
6de9cd9a
DN
923tree
924gfc_get_complex_type (int kind)
925{
644cb69f
FXC
926 int index = gfc_validate_kind (BT_COMPLEX, kind, true);
927 return index < 0 ? 0 : gfc_complex_types[index];
6de9cd9a
DN
928}
929
6de9cd9a
DN
930tree
931gfc_get_logical_type (int kind)
932{
644cb69f
FXC
933 int index = gfc_validate_kind (BT_LOGICAL, kind, true);
934 return index < 0 ? 0 : gfc_logical_types[index];
6de9cd9a 935}
374929b2
FXC
936
937tree
938gfc_get_char_type (int kind)
939{
940 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
941 return index < 0 ? 0 : gfc_character_types[index];
942}
943
944tree
945gfc_get_pchar_type (int kind)
946{
947 int index = gfc_validate_kind (BT_CHARACTER, kind, true);
948 return index < 0 ? 0 : gfc_pcharacter_types[index];
949}
950
6de9cd9a 951\f
40f20186 952/* Create a character type with the given kind and length. */
c3e8c6b8 953
6de9cd9a 954tree
d393bbd7 955gfc_get_character_type_len_for_eltype (tree eltype, tree len)
6de9cd9a 956{
e2cad04b 957 tree bounds, type;
6de9cd9a 958
5e3b8727 959 bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
d393bbd7 960 type = build_array_type (eltype, bounds);
6de9cd9a
DN
961 TYPE_STRING_FLAG (type) = 1;
962
963 return type;
964}
40f20186 965
d393bbd7
FXC
966tree
967gfc_get_character_type_len (int kind, tree len)
968{
969 gfc_validate_kind (BT_CHARACTER, kind, false);
970 return gfc_get_character_type_len_for_eltype (gfc_get_char_type (kind), len);
971}
972
40f20186
PB
973
974/* Get a type node for a character kind. */
975
976tree
977gfc_get_character_type (int kind, gfc_charlen * cl)
978{
979 tree len;
980
981 len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
982
983 return gfc_get_character_type_len (kind, len);
984}
6de9cd9a
DN
985\f
986/* Covert a basic type. This will be an array for character types. */
c3e8c6b8 987
6de9cd9a
DN
988tree
989gfc_typenode_for_spec (gfc_typespec * spec)
990{
991 tree basetype;
992
993 switch (spec->type)
994 {
995 case BT_UNKNOWN:
6e45f57b 996 gcc_unreachable ();
6de9cd9a
DN
997
998 case BT_INTEGER:
a8b3b0b6
CR
999 /* We use INTEGER(c_intptr_t) for C_PTR and C_FUNPTR once the symbol
1000 has been resolved. This is done so we can convert C_PTR and
1001 C_FUNPTR to simple variables that get translated to (void *). */
1002 if (spec->f90_type == BT_VOID)
089db47d 1003 {
bc21d315
JW
1004 if (spec->u.derived
1005 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
089db47d
CR
1006 basetype = ptr_type_node;
1007 else
1008 basetype = pfunc_type_node;
1009 }
a8b3b0b6
CR
1010 else
1011 basetype = gfc_get_int_type (spec->kind);
6de9cd9a
DN
1012 break;
1013
1014 case BT_REAL:
1015 basetype = gfc_get_real_type (spec->kind);
1016 break;
1017
1018 case BT_COMPLEX:
1019 basetype = gfc_get_complex_type (spec->kind);
1020 break;
1021
1022 case BT_LOGICAL:
1023 basetype = gfc_get_logical_type (spec->kind);
1024 break;
1025
1026 case BT_CHARACTER:
bc21d315 1027 basetype = gfc_get_character_type (spec->kind, spec->u.cl);
6de9cd9a
DN
1028 break;
1029
1030 case BT_DERIVED:
cf2b3c22 1031 case BT_CLASS:
bc21d315 1032 basetype = gfc_get_derived_type (spec->u.derived);
6de9cd9a 1033
a8b3b0b6
CR
1034 /* If we're dealing with either C_PTR or C_FUNPTR, we modified the
1035 type and kind to fit a (void *) and the basetype returned was a
1036 ptr_type_node. We need to pass up this new information to the
1037 symbol that was declared of type C_PTR or C_FUNPTR. */
bc21d315 1038 if (spec->u.derived->attr.is_iso_c)
a8b3b0b6 1039 {
bc21d315
JW
1040 spec->type = spec->u.derived->ts.type;
1041 spec->kind = spec->u.derived->ts.kind;
1042 spec->f90_type = spec->u.derived->ts.f90_type;
a8b3b0b6
CR
1043 }
1044 break;
1045 case BT_VOID:
089db47d
CR
1046 /* This is for the second arg to c_f_pointer and c_f_procpointer
1047 of the iso_c_binding module, to accept any ptr type. */
1048 basetype = ptr_type_node;
1049 if (spec->f90_type == BT_VOID)
1050 {
bc21d315
JW
1051 if (spec->u.derived
1052 && spec->u.derived->intmod_sym_id == ISOCBINDING_PTR)
089db47d
CR
1053 basetype = ptr_type_node;
1054 else
1055 basetype = pfunc_type_node;
1056 }
a8b3b0b6 1057 break;
6de9cd9a 1058 default:
6e45f57b 1059 gcc_unreachable ();
6de9cd9a
DN
1060 }
1061 return basetype;
1062}
1063\f
1064/* Build an INT_CST for constant expressions, otherwise return NULL_TREE. */
c3e8c6b8 1065
6de9cd9a
DN
1066static tree
1067gfc_conv_array_bound (gfc_expr * expr)
1068{
1069 /* If expr is an integer constant, return that. */
1070 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
1071 return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
1072
1073 /* Otherwise return NULL. */
1074 return NULL_TREE;
1075}
1076\f
1077tree
1078gfc_get_element_type (tree type)
1079{
1080 tree element;
1081
1082 if (GFC_ARRAY_TYPE_P (type))
1083 {
1084 if (TREE_CODE (type) == POINTER_TYPE)
1085 type = TREE_TYPE (type);
6e45f57b 1086 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6de9cd9a
DN
1087 element = TREE_TYPE (type);
1088 }
1089 else
1090 {
6e45f57b 1091 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
4c73896d 1092 element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
6de9cd9a 1093
6e45f57b 1094 gcc_assert (TREE_CODE (element) == POINTER_TYPE);
6de9cd9a
DN
1095 element = TREE_TYPE (element);
1096
6e45f57b 1097 gcc_assert (TREE_CODE (element) == ARRAY_TYPE);
6de9cd9a
DN
1098 element = TREE_TYPE (element);
1099 }
1100
1101 return element;
1102}
1103\f
eb28fb7d 1104/* Build an array. This function is called from gfc_sym_type().
c3e8c6b8 1105 Actually returns array descriptor type.
6de9cd9a
DN
1106
1107 Format of array descriptors is as follows:
1108
1109 struct gfc_array_descriptor
1110 {
1111 array *data
1112 index offset;
1113 index dtype;
1114 struct descriptor_dimension dimension[N_DIM];
1115 }
1116
1117 struct descriptor_dimension
1118 {
1119 index stride;
1120 index lbound;
1121 index ubound;
1122 }
1123
eb28fb7d
TS
1124 Translation code should use gfc_conv_descriptor_* rather than
1125 accessing the descriptor directly. Any changes to the array
1126 descriptor type will require changes in gfc_conv_descriptor_* and
1127 gfc_build_array_initializer.
6de9cd9a 1128
eb28fb7d
TS
1129 This is represented internally as a RECORD_TYPE. The index nodes
1130 are gfc_array_index_type and the data node is a pointer to the
1131 data. See below for the handling of character types.
6de9cd9a
DN
1132
1133 The dtype member is formatted as follows:
1134 rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
1135 type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
1136 size = dtype >> GFC_DTYPE_SIZE_SHIFT
1137
eb28fb7d
TS
1138 I originally used nested ARRAY_TYPE nodes to represent arrays, but
1139 this generated poor code for assumed/deferred size arrays. These
1140 require use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part
1141 of the GENERIC grammar. Also, there is no way to explicitly set
1142 the array stride, so all data must be packed(1). I've tried to
1143 mark all the functions which would require modification with a GCC
1144 ARRAYS comment.
6de9cd9a 1145
eb28fb7d 1146 The data component points to the first element in the array. The
df2fba9e
RW
1147 offset field is the position of the origin of the array (i.e. element
1148 (0, 0 ...)). This may be outside the bounds of the array.
6de9cd9a
DN
1149
1150 An element is accessed by
eb28fb7d 1151 data[offset + index0*stride0 + index1*stride1 + index2*stride2]
c3e8c6b8 1152 This gives good performance as the computation does not involve the
eb28fb7d
TS
1153 bounds of the array. For packed arrays, this is optimized further
1154 by substituting the known strides.
6de9cd9a 1155
eb28fb7d
TS
1156 This system has one problem: all array bounds must be within 2^31
1157 elements of the origin (2^63 on 64-bit machines). For example
1158 integer, dimension (80000:90000, 80000:90000, 2) :: array
1159 may not work properly on 32-bit machines because 80000*80000 >
df2fba9e 1160 2^31, so the calculation for stride2 would overflow. This may
eb28fb7d
TS
1161 still work, but I haven't checked, and it relies on the overflow
1162 doing the right thing.
6de9cd9a 1163
1f2959f0 1164 The way to fix this problem is to access elements as follows:
eb28fb7d
TS
1165 data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
1166 Obviously this is much slower. I will make this a compile time
1167 option, something like -fsmall-array-offsets. Mixing code compiled
1168 with and without this switch will work.
1169
1170 (1) This can be worked around by modifying the upper bound of the
1171 previous dimension. This requires extra fields in the descriptor
1172 (both real_ubound and fake_ubound). */
6de9cd9a
DN
1173
1174
1175/* Returns true if the array sym does not require a descriptor. */
1176
1177int
1178gfc_is_nodesc_array (gfc_symbol * sym)
1179{
6e45f57b 1180 gcc_assert (sym->attr.dimension);
6de9cd9a
DN
1181
1182 /* We only want local arrays. */
1183 if (sym->attr.pointer || sym->attr.allocatable)
1184 return 0;
1185
1186 if (sym->attr.dummy)
1187 {
1188 if (sym->as->type != AS_ASSUMED_SHAPE)
1189 return 1;
1190 else
1191 return 0;
1192 }
1193
1194 if (sym->attr.result || sym->attr.function)
1195 return 0;
1196
b3aefde2 1197 gcc_assert (sym->as->type == AS_EXPLICIT || sym->as->cp_was_assumed);
6de9cd9a
DN
1198
1199 return 1;
1200}
1201
40f20186
PB
1202
1203/* Create an array descriptor type. */
1204
6de9cd9a 1205static tree
fad0afd7 1206gfc_build_array_type (tree type, gfc_array_spec * as,
10174ddf 1207 enum gfc_array_kind akind, bool restricted)
6de9cd9a
DN
1208{
1209 tree lbound[GFC_MAX_DIMENSIONS];
1210 tree ubound[GFC_MAX_DIMENSIONS];
1211 int n;
1212
1213 for (n = 0; n < as->rank; n++)
1214 {
1215 /* Create expressions for the known bounds of the array. */
1216 if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
7ab92584 1217 lbound[n] = gfc_index_one_node;
6de9cd9a
DN
1218 else
1219 lbound[n] = gfc_conv_array_bound (as->lower[n]);
1220 ubound[n] = gfc_conv_array_bound (as->upper[n]);
1221 }
1222
fad0afd7
JJ
1223 if (as->type == AS_ASSUMED_SHAPE)
1224 akind = GFC_ARRAY_ASSUMED_SHAPE;
10174ddf
MM
1225 return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0, akind,
1226 restricted);
6de9cd9a
DN
1227}
1228\f
1229/* Returns the struct descriptor_dimension type. */
c3e8c6b8 1230
6de9cd9a
DN
1231static tree
1232gfc_get_desc_dim_type (void)
1233{
1234 tree type;
1235 tree decl;
1236 tree fieldlist;
1237
1238 if (gfc_desc_dim_type)
1239 return gfc_desc_dim_type;
1240
1241 /* Build the type node. */
1242 type = make_node (RECORD_TYPE);
1243
1244 TYPE_NAME (type) = get_identifier ("descriptor_dimension");
1245 TYPE_PACKED (type) = 1;
1246
1247 /* Consists of the stride, lbound and ubound members. */
c2255bc4
AH
1248 decl = build_decl (input_location,
1249 FIELD_DECL,
6de9cd9a
DN
1250 get_identifier ("stride"), gfc_array_index_type);
1251 DECL_CONTEXT (decl) = type;
d8eff1b8 1252 TREE_NO_WARNING (decl) = 1;
6de9cd9a
DN
1253 fieldlist = decl;
1254
c2255bc4
AH
1255 decl = build_decl (input_location,
1256 FIELD_DECL,
6de9cd9a
DN
1257 get_identifier ("lbound"), gfc_array_index_type);
1258 DECL_CONTEXT (decl) = type;
d8eff1b8 1259 TREE_NO_WARNING (decl) = 1;
6de9cd9a
DN
1260 fieldlist = chainon (fieldlist, decl);
1261
c2255bc4
AH
1262 decl = build_decl (input_location,
1263 FIELD_DECL,
6de9cd9a
DN
1264 get_identifier ("ubound"), gfc_array_index_type);
1265 DECL_CONTEXT (decl) = type;
d8eff1b8 1266 TREE_NO_WARNING (decl) = 1;
6de9cd9a
DN
1267 fieldlist = chainon (fieldlist, decl);
1268
1269 /* Finish off the type. */
1270 TYPE_FIELDS (type) = fieldlist;
1271
1272 gfc_finish_type (type);
dfcf0b12 1273 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
6de9cd9a
DN
1274
1275 gfc_desc_dim_type = type;
1276 return type;
1277}
1278
40b026d8 1279
43a5ef69 1280/* Return the DTYPE for an array. This describes the type and type parameters
40b026d8
PB
1281 of the array. */
1282/* TODO: Only call this when the value is actually used, and make all the
1283 unknown cases abort. */
1284
1285tree
1286gfc_get_dtype (tree type)
6de9cd9a
DN
1287{
1288 tree size;
1289 int n;
1290 HOST_WIDE_INT i;
1291 tree tmp;
1292 tree dtype;
40b026d8
PB
1293 tree etype;
1294 int rank;
1295
1296 gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
1297
1298 if (GFC_TYPE_ARRAY_DTYPE (type))
1299 return GFC_TYPE_ARRAY_DTYPE (type);
6de9cd9a 1300
40b026d8
PB
1301 rank = GFC_TYPE_ARRAY_RANK (type);
1302 etype = gfc_get_element_type (type);
6de9cd9a 1303
40b026d8 1304 switch (TREE_CODE (etype))
6de9cd9a
DN
1305 {
1306 case INTEGER_TYPE:
1307 n = GFC_DTYPE_INTEGER;
1308 break;
1309
1310 case BOOLEAN_TYPE:
1311 n = GFC_DTYPE_LOGICAL;
1312 break;
1313
1314 case REAL_TYPE:
1315 n = GFC_DTYPE_REAL;
1316 break;
1317
1318 case COMPLEX_TYPE:
1319 n = GFC_DTYPE_COMPLEX;
1320 break;
1321
40b026d8 1322 /* We will never have arrays of arrays. */
6de9cd9a
DN
1323 case RECORD_TYPE:
1324 n = GFC_DTYPE_DERIVED;
1325 break;
1326
1327 case ARRAY_TYPE:
1328 n = GFC_DTYPE_CHARACTER;
1329 break;
1330
1331 default:
40f20186
PB
1332 /* TODO: Don't do dtype for temporary descriptorless arrays. */
1333 /* We can strange array types for temporary arrays. */
1334 return gfc_index_zero_node;
6de9cd9a
DN
1335 }
1336
6e45f57b 1337 gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
40b026d8 1338 size = TYPE_SIZE_UNIT (etype);
f676971a 1339
6de9cd9a
DN
1340 i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
1341 if (size && INTEGER_CST_P (size))
1342 {
1343 if (tree_int_cst_lt (gfc_max_array_element_size, size))
1344 internal_error ("Array element size too big");
1345
1346 i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
1347 }
7d60be94 1348 dtype = build_int_cst (gfc_array_index_type, i);
6de9cd9a
DN
1349
1350 if (size && !INTEGER_CST_P (size))
1351 {
7d60be94 1352 tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
ac816b02
RG
1353 tmp = fold_build2 (LSHIFT_EXPR, gfc_array_index_type,
1354 fold_convert (gfc_array_index_type, size), tmp);
10c7a96f 1355 dtype = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype);
6de9cd9a
DN
1356 }
1357 /* If we don't know the size we leave it as zero. This should never happen
1358 for anything that is actually used. */
1359 /* TODO: Check this is actually true, particularly when repacking
1360 assumed size parameters. */
1361
40b026d8 1362 GFC_TYPE_ARRAY_DTYPE (type) = dtype;
6de9cd9a
DN
1363 return dtype;
1364}
1365
1366
dcfef7d4
TS
1367/* Build an array type for use without a descriptor, packed according
1368 to the value of PACKED. */
6de9cd9a
DN
1369
1370tree
10174ddf
MM
1371gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, gfc_packed packed,
1372 bool restricted)
6de9cd9a
DN
1373{
1374 tree range;
1375 tree type;
1376 tree tmp;
1377 int n;
1378 int known_stride;
1379 int known_offset;
1380 mpz_t offset;
1381 mpz_t stride;
1382 mpz_t delta;
1383 gfc_expr *expr;
1384
1385 mpz_init_set_ui (offset, 0);
1386 mpz_init_set_ui (stride, 1);
1387 mpz_init (delta);
1388
1389 /* We don't use build_array_type because this does not include include
13795658 1390 lang-specific information (i.e. the bounds of the array) when checking
6de9cd9a
DN
1391 for duplicates. */
1392 type = make_node (ARRAY_TYPE);
1393
1394 GFC_ARRAY_TYPE_P (type) = 1;
1395 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
1396 ggc_alloc_cleared (sizeof (struct lang_type));
1397
dcfef7d4 1398 known_stride = (packed != PACKED_NO);
6de9cd9a
DN
1399 known_offset = 1;
1400 for (n = 0; n < as->rank; n++)
1401 {
1402 /* Fill in the stride and bound components of the type. */
1403 if (known_stride)
08789087 1404 tmp = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
6de9cd9a
DN
1405 else
1406 tmp = NULL_TREE;
1407 GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1408
1409 expr = as->lower[n];
1410 if (expr->expr_type == EXPR_CONSTANT)
1411 {
1412 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
fad0afd7 1413 gfc_index_integer_kind);
6de9cd9a
DN
1414 }
1415 else
1416 {
1417 known_stride = 0;
1418 tmp = NULL_TREE;
1419 }
1420 GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1421
1422 if (known_stride)
1423 {
1424 /* Calculate the offset. */
1425 mpz_mul (delta, stride, as->lower[n]->value.integer);
1426 mpz_sub (offset, offset, delta);
1427 }
1428 else
1429 known_offset = 0;
1430
1431 expr = as->upper[n];
1432 if (expr && expr->expr_type == EXPR_CONSTANT)
1433 {
1434 tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1435 gfc_index_integer_kind);
1436 }
1437 else
1438 {
1439 tmp = NULL_TREE;
1440 known_stride = 0;
1441 }
1442 GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1443
1444 if (known_stride)
1445 {
1446 /* Calculate the stride. */
1447 mpz_sub (delta, as->upper[n]->value.integer,
1448 as->lower[n]->value.integer);
1449 mpz_add_ui (delta, delta, 1);
1450 mpz_mul (stride, stride, delta);
1451 }
1452
1453 /* Only the first stride is known for partial packed arrays. */
dcfef7d4 1454 if (packed == PACKED_NO || packed == PACKED_PARTIAL)
6de9cd9a
DN
1455 known_stride = 0;
1456 }
1457
1458 if (known_offset)
1459 {
1460 GFC_TYPE_ARRAY_OFFSET (type) =
1461 gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1462 }
1463 else
1464 GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1465
1466 if (known_stride)
1467 {
1468 GFC_TYPE_ARRAY_SIZE (type) =
1469 gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1470 }
1471 else
1472 GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1473
6de9cd9a 1474 GFC_TYPE_ARRAY_RANK (type) = as->rank;
40b026d8 1475 GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
7ab92584 1476 range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
6de9cd9a
DN
1477 NULL_TREE);
1478 /* TODO: use main type if it is unbounded. */
1479 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1480 build_pointer_type (build_array_type (etype, range));
10174ddf
MM
1481 if (restricted)
1482 GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1483 build_qualified_type (GFC_TYPE_ARRAY_DATAPTR_TYPE (type),
1484 TYPE_QUAL_RESTRICT);
6de9cd9a
DN
1485
1486 if (known_stride)
1487 {
1488 mpz_sub_ui (stride, stride, 1);
1489 range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1490 }
1491 else
1492 range = NULL_TREE;
1493
7ab92584 1494 range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
6de9cd9a
DN
1495 TYPE_DOMAIN (type) = range;
1496
1497 build_pointer_type (etype);
1498 TREE_TYPE (type) = etype;
1499
1500 layout_type (type);
1501
1502 mpz_clear (offset);
1503 mpz_clear (stride);
1504 mpz_clear (delta);
1505
09775c40
AO
1506 /* Represent packed arrays as multi-dimensional if they have rank >
1507 1 and with proper bounds, instead of flat arrays. This makes for
1508 better debug info. */
1509 if (known_offset)
08789087
JJ
1510 {
1511 tree gtype = etype, rtype, type_decl;
1512
1513 for (n = as->rank - 1; n >= 0; n--)
1514 {
1515 rtype = build_range_type (gfc_array_index_type,
1516 GFC_TYPE_ARRAY_LBOUND (type, n),
1517 GFC_TYPE_ARRAY_UBOUND (type, n));
1518 gtype = build_array_type (gtype, rtype);
1519 }
c2255bc4
AH
1520 TYPE_NAME (type) = type_decl = build_decl (input_location,
1521 TYPE_DECL, NULL, gtype);
08789087
JJ
1522 DECL_ORIGINAL_TYPE (type_decl) = gtype;
1523 }
1524
dcfef7d4 1525 if (packed != PACKED_STATIC || !known_stride)
6de9cd9a 1526 {
841b0c1f
PB
1527 /* For dummy arrays and automatic (heap allocated) arrays we
1528 want a pointer to the array. */
6de9cd9a 1529 type = build_pointer_type (type);
10174ddf
MM
1530 if (restricted)
1531 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
6de9cd9a
DN
1532 GFC_ARRAY_TYPE_P (type) = 1;
1533 TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1534 }
1535 return type;
1536}
1537
4c73896d
RH
1538/* Return or create the base type for an array descriptor. */
1539
1540static tree
10174ddf 1541gfc_get_array_descriptor_base (int dimen, bool restricted)
4c73896d
RH
1542{
1543 tree fat_type, fieldlist, decl, arraytype;
1544 char name[16 + GFC_RANK_DIGITS + 1];
10174ddf 1545 int idx = 2 * (dimen - 1) + restricted;
4c73896d
RH
1546
1547 gcc_assert (dimen >= 1 && dimen <= GFC_MAX_DIMENSIONS);
10174ddf
MM
1548 if (gfc_array_descriptor_base[idx])
1549 return gfc_array_descriptor_base[idx];
4c73896d
RH
1550
1551 /* Build the type node. */
1552 fat_type = make_node (RECORD_TYPE);
1553
1554 sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen);
1555 TYPE_NAME (fat_type) = get_identifier (name);
1556
1557 /* Add the data member as the first element of the descriptor. */
c2255bc4 1558 decl = build_decl (input_location,
10174ddf
MM
1559 FIELD_DECL, get_identifier ("data"),
1560 restricted ? prvoid_type_node : ptr_type_node);
4c73896d
RH
1561
1562 DECL_CONTEXT (decl) = fat_type;
1563 fieldlist = decl;
1564
1565 /* Add the base component. */
c2255bc4
AH
1566 decl = build_decl (input_location,
1567 FIELD_DECL, get_identifier ("offset"),
4c73896d
RH
1568 gfc_array_index_type);
1569 DECL_CONTEXT (decl) = fat_type;
d8eff1b8 1570 TREE_NO_WARNING (decl) = 1;
4c73896d
RH
1571 fieldlist = chainon (fieldlist, decl);
1572
1573 /* Add the dtype component. */
c2255bc4
AH
1574 decl = build_decl (input_location,
1575 FIELD_DECL, get_identifier ("dtype"),
4c73896d
RH
1576 gfc_array_index_type);
1577 DECL_CONTEXT (decl) = fat_type;
d8eff1b8 1578 TREE_NO_WARNING (decl) = 1;
4c73896d
RH
1579 fieldlist = chainon (fieldlist, decl);
1580
1581 /* Build the array type for the stride and bound components. */
1582 arraytype =
1583 build_array_type (gfc_get_desc_dim_type (),
1584 build_range_type (gfc_array_index_type,
1585 gfc_index_zero_node,
1586 gfc_rank_cst[dimen - 1]));
1587
c2255bc4
AH
1588 decl = build_decl (input_location,
1589 FIELD_DECL, get_identifier ("dim"), arraytype);
4c73896d 1590 DECL_CONTEXT (decl) = fat_type;
d8eff1b8 1591 TREE_NO_WARNING (decl) = 1;
4c73896d
RH
1592 fieldlist = chainon (fieldlist, decl);
1593
1594 /* Finish off the type. */
1595 TYPE_FIELDS (fat_type) = fieldlist;
1596
1597 gfc_finish_type (fat_type);
dfcf0b12 1598 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (fat_type)) = 1;
4c73896d 1599
10174ddf 1600 gfc_array_descriptor_base[idx] = fat_type;
4c73896d
RH
1601 return fat_type;
1602}
6de9cd9a
DN
1603
1604/* Build an array (descriptor) type with given bounds. */
1605
1606tree
1607gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound,
fad0afd7 1608 tree * ubound, int packed,
10174ddf 1609 enum gfc_array_kind akind, bool restricted)
6de9cd9a 1610{
6de9cd9a 1611 char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN];
416a8af4 1612 tree fat_type, base_type, arraytype, lower, upper, stride, tmp, rtype;
9aa433c2 1613 const char *type_name;
4c73896d 1614 int n;
6de9cd9a 1615
10174ddf 1616 base_type = gfc_get_array_descriptor_base (dimen, restricted);
9618fb3c 1617 fat_type = build_distinct_type_copy (base_type);
10174ddf
MM
1618 /* Make sure that nontarget and target array type have the same canonical
1619 type (and same stub decl for debug info). */
1620 base_type = gfc_get_array_descriptor_base (dimen, false);
9618fb3c
RG
1621 TYPE_CANONICAL (fat_type) = base_type;
1622 TYPE_STUB_DECL (fat_type) = TYPE_STUB_DECL (base_type);
6de9cd9a
DN
1623
1624 tmp = TYPE_NAME (etype);
1625 if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1626 tmp = DECL_NAME (tmp);
1627 if (tmp)
9aa433c2 1628 type_name = IDENTIFIER_POINTER (tmp);
6de9cd9a 1629 else
9aa433c2 1630 type_name = "unknown";
6de9cd9a 1631 sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen,
9aa433c2 1632 GFC_MAX_SYMBOL_LEN, type_name);
6de9cd9a 1633 TYPE_NAME (fat_type) = get_identifier (name);
6de9cd9a 1634
4c73896d
RH
1635 GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1636 TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *)
1637 ggc_alloc_cleared (sizeof (struct lang_type));
1638
1639 GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1640 GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
fad0afd7 1641 GFC_TYPE_ARRAY_AKIND (fat_type) = akind;
6de9cd9a
DN
1642
1643 /* Build an array descriptor record type. */
1644 if (packed != 0)
7ab92584 1645 stride = gfc_index_one_node;
6de9cd9a
DN
1646 else
1647 stride = NULL_TREE;
6de9cd9a
DN
1648 for (n = 0; n < dimen; n++)
1649 {
1650 GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1651
1652 if (lbound)
1653 lower = lbound[n];
1654 else
1655 lower = NULL_TREE;
1656
1657 if (lower != NULL_TREE)
1658 {
1659 if (INTEGER_CST_P (lower))
1660 GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1661 else
1662 lower = NULL_TREE;
1663 }
1664
1665 upper = ubound[n];
1666 if (upper != NULL_TREE)
1667 {
1668 if (INTEGER_CST_P (upper))
1669 GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1670 else
1671 upper = NULL_TREE;
1672 }
1673
1674 if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1675 {
10c7a96f
SB
1676 tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
1677 tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1678 gfc_index_one_node);
6de9cd9a 1679 stride =
10c7a96f 1680 fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, stride);
6de9cd9a 1681 /* Check the folding worked. */
6e45f57b 1682 gcc_assert (INTEGER_CST_P (stride));
6de9cd9a
DN
1683 }
1684 else
1685 stride = NULL_TREE;
1686 }
1687 GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
4c73896d 1688
6de9cd9a
DN
1689 /* TODO: known offsets for descriptors. */
1690 GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1691
416a8af4
RG
1692 /* We define data as an array with the correct size if possible.
1693 Much better than doing pointer arithmetic. */
1694 if (stride)
1695 rtype = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1696 int_const_binop (MINUS_EXPR, stride,
1697 integer_one_node, 0));
1698 else
1699 rtype = gfc_array_range_type;
1700 arraytype = build_array_type (etype, rtype);
6de9cd9a 1701 arraytype = build_pointer_type (arraytype);
10174ddf
MM
1702 if (restricted)
1703 arraytype = build_qualified_type (arraytype, TYPE_QUAL_RESTRICT);
6de9cd9a
DN
1704 GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1705
d560566a
AO
1706 /* This will generate the base declarations we need to emit debug
1707 information for this type. FIXME: there must be a better way to
1708 avoid divergence between compilations with and without debug
1709 information. */
1710 {
1711 struct array_descr_info info;
1712 gfc_get_array_descr_info (fat_type, &info);
1713 gfc_get_array_descr_info (build_pointer_type (fat_type), &info);
1714 }
1715
6de9cd9a
DN
1716 return fat_type;
1717}
1718\f
1719/* Build a pointer type. This function is called from gfc_sym_type(). */
c3e8c6b8 1720
6de9cd9a
DN
1721static tree
1722gfc_build_pointer_type (gfc_symbol * sym, tree type)
1723{
436529ea 1724 /* Array pointer types aren't actually pointers. */
6de9cd9a
DN
1725 if (sym->attr.dimension)
1726 return type;
1727 else
1728 return build_pointer_type (type);
1729}
1730\f
1731/* Return the type for a symbol. Special handling is required for character
1732 types to get the correct level of indirection.
1733 For functions return the return type.
ad6e2a18
TS
1734 For subroutines return void_type_node.
1735 Calling this multiple times for the same symbol should be avoided,
1736 especially for character and array types. */
c3e8c6b8 1737
6de9cd9a
DN
1738tree
1739gfc_sym_type (gfc_symbol * sym)
1740{
1741 tree type;
1742 int byref;
10174ddf 1743 bool restricted;
6de9cd9a 1744
3070bab4
JW
1745 /* Procedure Pointers inside COMMON blocks. */
1746 if (sym->attr.proc_pointer && sym->attr.in_common)
00625fae
JW
1747 {
1748 /* Unset proc_pointer as gfc_get_function_type calls gfc_sym_type. */
1749 sym->attr.proc_pointer = 0;
1750 type = build_pointer_type (gfc_get_function_type (sym));
1751 sym->attr.proc_pointer = 1;
1752 return type;
1753 }
1754
6de9cd9a
DN
1755 if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1756 return void_type_node;
1757
da4c6ed8
TS
1758 /* In the case of a function the fake result variable may have a
1759 type different from the function type, so don't return early in
1760 that case. */
1761 if (sym->backend_decl && !sym->attr.function)
1762 return TREE_TYPE (sym->backend_decl);
6de9cd9a 1763
665733c1
JJ
1764 if (sym->ts.type == BT_CHARACTER
1765 && ((sym->attr.function && sym->attr.is_bind_c)
1766 || (sym->attr.result
1767 && sym->ns->proc_name
1768 && sym->ns->proc_name->attr.is_bind_c)))
06a54338
TB
1769 type = gfc_character1_type_node;
1770 else
1771 type = gfc_typenode_for_spec (&sym->ts);
6de9cd9a 1772
06469efd 1773 if (sym->attr.dummy && !sym->attr.function && !sym->attr.value)
6de9cd9a
DN
1774 byref = 1;
1775 else
1776 byref = 0;
1777
10174ddf 1778 restricted = !sym->attr.target && !sym->attr.pointer
b3aefde2 1779 && !sym->attr.proc_pointer && !sym->attr.cray_pointee;
6de9cd9a
DN
1780 if (sym->attr.dimension)
1781 {
1782 if (gfc_is_nodesc_array (sym))
1783 {
1784 /* If this is a character argument of unknown length, just use the
1785 base type. */
1786 if (sym->ts.type != BT_CHARACTER
b49a3de7 1787 || !(sym->attr.dummy || sym->attr.function)
bc21d315 1788 || sym->ts.u.cl->backend_decl)
6de9cd9a
DN
1789 {
1790 type = gfc_get_nodesc_array_type (type, sym->as,
dcfef7d4 1791 byref ? PACKED_FULL
10174ddf
MM
1792 : PACKED_STATIC,
1793 restricted);
6de9cd9a
DN
1794 byref = 0;
1795 }
1796 }
1797 else
fad0afd7
JJ
1798 {
1799 enum gfc_array_kind akind = GFC_ARRAY_UNKNOWN;
1800 if (sym->attr.pointer)
1801 akind = GFC_ARRAY_POINTER;
1802 else if (sym->attr.allocatable)
1803 akind = GFC_ARRAY_ALLOCATABLE;
10174ddf 1804 type = gfc_build_array_type (type, sym->as, akind, restricted);
fad0afd7 1805 }
a8b3b0b6 1806 }
6de9cd9a
DN
1807 else
1808 {
1809 if (sym->attr.allocatable || sym->attr.pointer)
1810 type = gfc_build_pointer_type (sym, type);
e1c82219
JJ
1811 if (sym->attr.pointer)
1812 GFC_POINTER_TYPE_P (type) = 1;
6de9cd9a
DN
1813 }
1814
1815 /* We currently pass all parameters by reference.
1816 See f95_get_function_decl. For dummy function parameters return the
1817 function type. */
1818 if (byref)
1619aa6f
PB
1819 {
1820 /* We must use pointer types for potentially absent variables. The
1821 optimizers assume a reference type argument is never NULL. */
1822 if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1823 type = build_pointer_type (type);
1824 else
10174ddf
MM
1825 {
1826 type = build_reference_type (type);
1827 if (restricted)
1828 type = build_qualified_type (type, TYPE_QUAL_RESTRICT);
1829 }
1619aa6f 1830 }
6de9cd9a
DN
1831
1832 return (type);
1833}
1834\f
1835/* Layout and output debug info for a record type. */
c3e8c6b8 1836
6de9cd9a
DN
1837void
1838gfc_finish_type (tree type)
1839{
1840 tree decl;
1841
c2255bc4
AH
1842 decl = build_decl (input_location,
1843 TYPE_DECL, NULL_TREE, type);
6de9cd9a
DN
1844 TYPE_STUB_DECL (type) = decl;
1845 layout_type (type);
1846 rest_of_type_compilation (type, 1);
0e6df31e 1847 rest_of_decl_compilation (decl, 1, 0);
6de9cd9a
DN
1848}
1849\f
1850/* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1851 or RECORD_TYPE pointed to by STYPE. The new field is chained
1852 to the fieldlist pointed to by FIELDLIST.
1853
1854 Returns a pointer to the new field. */
c3e8c6b8 1855
6de9cd9a
DN
1856tree
1857gfc_add_field_to_struct (tree *fieldlist, tree context,
1858 tree name, tree type)
1859{
1860 tree decl;
1861
c2255bc4
AH
1862 decl = build_decl (input_location,
1863 FIELD_DECL, name, type);
6de9cd9a
DN
1864
1865 DECL_CONTEXT (decl) = context;
1866 DECL_INITIAL (decl) = 0;
1867 DECL_ALIGN (decl) = 0;
1868 DECL_USER_ALIGN (decl) = 0;
1869 TREE_CHAIN (decl) = NULL_TREE;
1870 *fieldlist = chainon (*fieldlist, decl);
1871
1872 return decl;
1873}
1874
1875
6b887797
PT
1876/* Copy the backend_decl and component backend_decls if
1877 the two derived type symbols are "equal", as described
1878 in 4.4.2 and resolved by gfc_compare_derived_types. */
1879
1880static int
3af8d8cb
PT
1881copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to,
1882 bool from_gsym)
6b887797
PT
1883{
1884 gfc_component *to_cm;
1885 gfc_component *from_cm;
1886
1887 if (from->backend_decl == NULL
1888 || !gfc_compare_derived_types (from, to))
1889 return 0;
1890
1891 to->backend_decl = from->backend_decl;
1892
1893 to_cm = to->components;
1894 from_cm = from->components;
1895
1896 /* Copy the component declarations. If a component is itself
1897 a derived type, we need a copy of its component declarations.
1898 This is done by recursing into gfc_get_derived_type and
1899 ensures that the component's component declarations have
1900 been built. If it is a character, we need the character
1901 length, as well. */
1902 for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
1903 {
1904 to_cm->backend_decl = from_cm->backend_decl;
3af8d8cb
PT
1905 if ((!from_cm->attr.pointer || from_gsym)
1906 && from_cm->ts.type == BT_DERIVED)
bc21d315 1907 gfc_get_derived_type (to_cm->ts.u.derived);
6b887797
PT
1908
1909 else if (from_cm->ts.type == BT_CHARACTER)
bc21d315 1910 to_cm->ts.u.cl->backend_decl = from_cm->ts.u.cl->backend_decl;
6b887797
PT
1911 }
1912
1913 return 1;
1914}
1915
1916
713485cc
JW
1917/* Build a tree node for a procedure pointer component. */
1918
1919tree
1920gfc_get_ppc_type (gfc_component* c)
1921{
1922 tree t;
37513ce9
JW
1923
1924 /* Explicit interface. */
1925 if (c->attr.if_source != IFSRC_UNKNOWN && c->ts.interface)
1926 return build_pointer_type (gfc_get_function_type (c->ts.interface));
1927
1928 /* Implicit interface (only return value may be known). */
1929 if (c->attr.function && !c->attr.dimension && c->ts.type != BT_CHARACTER)
1930 t = gfc_typenode_for_spec (&c->ts);
713485cc
JW
1931 else
1932 t = void_type_node;
37513ce9 1933
713485cc
JW
1934 return build_pointer_type (build_function_type (t, NULL_TREE));
1935}
1936
1937
6b887797
PT
1938/* Build a tree node for a derived type. If there are equal
1939 derived types, with different local names, these are built
1940 at the same time. If an equal derived type has been built
1941 in a parent namespace, this is used. */
c3e8c6b8 1942
7c1dab0d 1943tree
6de9cd9a
DN
1944gfc_get_derived_type (gfc_symbol * derived)
1945{
a8b3b0b6 1946 tree typenode = NULL, field = NULL, field_type = NULL, fieldlist = NULL;
3af8d8cb
PT
1947 tree canonical = NULL_TREE;
1948 bool got_canonical = false;
6de9cd9a 1949 gfc_component *c;
6b887797 1950 gfc_dt_list *dt;
3af8d8cb
PT
1951 gfc_namespace *ns;
1952 gfc_gsymbol *gsym;
6de9cd9a 1953
6b887797 1954 gcc_assert (derived && derived->attr.flavor == FL_DERIVED);
6de9cd9a 1955
a8b3b0b6
CR
1956 /* See if it's one of the iso_c_binding derived types. */
1957 if (derived->attr.is_iso_c == 1)
1958 {
9dc35956
CR
1959 if (derived->backend_decl)
1960 return derived->backend_decl;
1961
089db47d
CR
1962 if (derived->intmod_sym_id == ISOCBINDING_PTR)
1963 derived->backend_decl = ptr_type_node;
1964 else
1965 derived->backend_decl = pfunc_type_node;
9dc35956
CR
1966
1967 /* Create a backend_decl for the __c_ptr_c_address field. */
1968 derived->components->backend_decl =
1969 gfc_add_field_to_struct (&(derived->backend_decl->type.values),
1970 derived->backend_decl,
1971 get_identifier (derived->components->name),
1972 gfc_typenode_for_spec (
1973 &(derived->components->ts)));
1974
a8b3b0b6
CR
1975 derived->ts.kind = gfc_index_integer_kind;
1976 derived->ts.type = BT_INTEGER;
1977 /* Set the f90_type to BT_VOID as a way to recognize something of type
1978 BT_INTEGER that needs to fit a void * for the purpose of the
1979 iso_c_binding derived types. */
1980 derived->ts.f90_type = BT_VOID;
9dc35956 1981
a8b3b0b6
CR
1982 return derived->backend_decl;
1983 }
3af8d8cb
PT
1984
1985/* If use associated, use the module type for this one. */
1986 if (gfc_option.flag_whole_file
1987 && derived->backend_decl == NULL
1988 && derived->attr.use_assoc
1989 && derived->module)
1990 {
1991 gsym = gfc_find_gsymbol (gfc_gsym_root, derived->module);
1992 if (gsym && gsym->ns && gsym->type == GSYM_MODULE)
1993 {
1994 gfc_symbol *s;
1995 s = NULL;
1996 gfc_find_symbol (derived->name, gsym->ns, 0, &s);
1997 if (s && s->backend_decl)
1998 {
1999 copy_dt_decls_ifequal (s, derived, true);
2000 goto copy_derived_types;
2001 }
2002 }
2003 }
2004
2005 /* If a whole file compilation, the derived types from an earlier
2006 namespace can be used as the the canonical type. */
2007 if (gfc_option.flag_whole_file
2008 && derived->backend_decl == NULL
2009 && !derived->attr.use_assoc
2010 && gfc_global_ns_list)
2011 {
2012 for (ns = gfc_global_ns_list;
2013 ns->translated && !got_canonical;
2014 ns = ns->sibling)
2015 {
2016 dt = ns->derived_types;
2017 for (; dt && !canonical; dt = dt->next)
2018 {
2019 copy_dt_decls_ifequal (dt->derived, derived, true);
2020 if (derived->backend_decl)
2021 got_canonical = true;
2022 }
2023 }
2024 }
2025
2026 /* Store up the canonical type to be added to this one. */
2027 if (got_canonical)
2028 {
2029 if (TYPE_CANONICAL (derived->backend_decl))
2030 canonical = TYPE_CANONICAL (derived->backend_decl);
2031 else
2032 canonical = derived->backend_decl;
2033
2034 derived->backend_decl = NULL_TREE;
2035 }
2036
6de9cd9a 2037 /* derived->backend_decl != 0 means we saw it before, but its
436529ea 2038 components' backend_decl may have not been built. */
6de9cd9a 2039 if (derived->backend_decl)
3e6d828d 2040 {
37513ce9
JW
2041 /* Its components' backend_decl have been built or we are
2042 seeing recursion through the formal arglist of a procedure
2043 pointer component. */
2044 if (TYPE_FIELDS (derived->backend_decl)
2045 || derived->attr.proc_pointer_comp)
3e6d828d
JW
2046 return derived->backend_decl;
2047 else
2048 typenode = derived->backend_decl;
2049 }
6de9cd9a
DN
2050 else
2051 {
2052 /* We see this derived type first time, so build the type node. */
2053 typenode = make_node (RECORD_TYPE);
2054 TYPE_NAME (typenode) = get_identifier (derived->name);
2055 TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
2056 derived->backend_decl = typenode;
2057 }
2058
bce71376
PT
2059 /* Go through the derived type components, building them as
2060 necessary. The reason for doing this now is that it is
2061 possible to recurse back to this derived type through a
2062 pointer component (PR24092). If this happens, the fields
2063 will be built and so we can return the type. */
2064 for (c = derived->components; c; c = c->next)
2065 {
cf2b3c22 2066 if (c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
bce71376
PT
2067 continue;
2068
c4984ab2 2069 if ((!c->attr.pointer && !c->attr.proc_pointer)
bc21d315
JW
2070 || c->ts.u.derived->backend_decl == NULL)
2071 c->ts.u.derived->backend_decl = gfc_get_derived_type (c->ts.u.derived);
a8b3b0b6 2072
bc21d315 2073 if (c->ts.u.derived && c->ts.u.derived->attr.is_iso_c)
a8b3b0b6
CR
2074 {
2075 /* Need to copy the modified ts from the derived type. The
2076 typespec was modified because C_PTR/C_FUNPTR are translated
2077 into (void *) from derived types. */
bc21d315
JW
2078 c->ts.type = c->ts.u.derived->ts.type;
2079 c->ts.kind = c->ts.u.derived->ts.kind;
2080 c->ts.f90_type = c->ts.u.derived->ts.f90_type;
9dc35956
CR
2081 if (c->initializer)
2082 {
2083 c->initializer->ts.type = c->ts.type;
2084 c->initializer->ts.kind = c->ts.kind;
2085 c->initializer->ts.f90_type = c->ts.f90_type;
2086 c->initializer->expr_type = EXPR_NULL;
2087 }
a8b3b0b6 2088 }
bce71376
PT
2089 }
2090
2091 if (TYPE_FIELDS (derived->backend_decl))
2092 return derived->backend_decl;
2093
6de9cd9a
DN
2094 /* Build the type member list. Install the newly created RECORD_TYPE
2095 node as DECL_CONTEXT of each FIELD_DECL. */
2096 fieldlist = NULL_TREE;
2097 for (c = derived->components; c; c = c->next)
2098 {
c4984ab2 2099 if (c->attr.proc_pointer)
713485cc 2100 field_type = gfc_get_ppc_type (c);
cf2b3c22 2101 else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
bc21d315 2102 field_type = c->ts.u.derived->backend_decl;
6de9cd9a
DN
2103 else
2104 {
2105 if (c->ts.type == BT_CHARACTER)
2106 {
2107 /* Evaluate the string length. */
bc21d315
JW
2108 gfc_conv_const_charlen (c->ts.u.cl);
2109 gcc_assert (c->ts.u.cl->backend_decl);
6de9cd9a
DN
2110 }
2111
2112 field_type = gfc_typenode_for_spec (&c->ts);
2113 }
2114
1f2959f0 2115 /* This returns an array descriptor type. Initialization may be
6de9cd9a 2116 required. */
c74b74a8 2117 if (c->attr.dimension && !c->attr.proc_pointer)
6de9cd9a 2118 {
d4b7d0f0 2119 if (c->attr.pointer || c->attr.allocatable)
6de9cd9a 2120 {
fad0afd7 2121 enum gfc_array_kind akind;
d4b7d0f0 2122 if (c->attr.pointer)
fad0afd7
JJ
2123 akind = GFC_ARRAY_POINTER;
2124 else
2125 akind = GFC_ARRAY_ALLOCATABLE;
1f2959f0 2126 /* Pointers to arrays aren't actually pointer types. The
e7dc5b4f 2127 descriptors are separate, but the data is common. */
10174ddf
MM
2128 field_type = gfc_build_array_type (field_type, c->as, akind,
2129 !c->attr.target
2130 && !c->attr.pointer);
6de9cd9a
DN
2131 }
2132 else
dcfef7d4 2133 field_type = gfc_get_nodesc_array_type (field_type, c->as,
10174ddf
MM
2134 PACKED_STATIC,
2135 !c->attr.target);
6de9cd9a 2136 }
cf2b3c22
TB
2137 else if ((c->attr.pointer || c->attr.allocatable)
2138 && !c->attr.proc_pointer)
6de9cd9a
DN
2139 field_type = build_pointer_type (field_type);
2140
2141 field = gfc_add_field_to_struct (&fieldlist, typenode,
50dbf0b4 2142 get_identifier (c->name), field_type);
dfcf0b12
FXC
2143 if (c->loc.lb)
2144 gfc_set_decl_location (field, &c->loc);
2145 else if (derived->declared_at.lb)
2146 gfc_set_decl_location (field, &derived->declared_at);
6de9cd9a
DN
2147
2148 DECL_PACKED (field) |= TYPE_PACKED (typenode);
2149
bce71376
PT
2150 gcc_assert (field);
2151 if (!c->backend_decl)
2152 c->backend_decl = field;
6de9cd9a
DN
2153 }
2154
2155 /* Now we have the final fieldlist. Record it, then lay out the
2156 derived type, including the fields. */
2157 TYPE_FIELDS (typenode) = fieldlist;
64754ed5
RG
2158 if (canonical)
2159 TYPE_CANONICAL (typenode) = canonical;
6de9cd9a
DN
2160
2161 gfc_finish_type (typenode);
dfcf0b12 2162 gfc_set_decl_location (TYPE_STUB_DECL (typenode), &derived->declared_at);
96ffc6cd
TK
2163 if (derived->module && derived->ns->proc_name
2164 && derived->ns->proc_name->attr.flavor == FL_MODULE)
a64f5186
JJ
2165 {
2166 if (derived->ns->proc_name->backend_decl
2167 && TREE_CODE (derived->ns->proc_name->backend_decl)
2168 == NAMESPACE_DECL)
2169 {
2170 TYPE_CONTEXT (typenode) = derived->ns->proc_name->backend_decl;
2171 DECL_CONTEXT (TYPE_STUB_DECL (typenode))
2172 = derived->ns->proc_name->backend_decl;
2173 }
2174 }
6de9cd9a
DN
2175
2176 derived->backend_decl = typenode;
2177
3af8d8cb
PT
2178copy_derived_types:
2179
a64f5186 2180 for (dt = gfc_derived_types; dt; dt = dt->next)
3af8d8cb 2181 copy_dt_decls_ifequal (derived, dt->derived, false);
6b887797 2182
e0e85e06 2183 return derived->backend_decl;
6de9cd9a 2184}
e0e85e06
PT
2185
2186
6de9cd9a
DN
2187int
2188gfc_return_by_reference (gfc_symbol * sym)
2189{
2190 if (!sym->attr.function)
2191 return 0;
2192
b49a3de7 2193 if (sym->attr.dimension)
6de9cd9a
DN
2194 return 1;
2195
665733c1
JJ
2196 if (sym->ts.type == BT_CHARACTER
2197 && !sym->attr.is_bind_c
2198 && (!sym->attr.result
2199 || !sym->ns->proc_name
2200 || !sym->ns->proc_name->attr.is_bind_c))
6de9cd9a
DN
2201 return 1;
2202
973ff4c0
TS
2203 /* Possibly return complex numbers by reference for g77 compatibility.
2204 We don't do this for calls to intrinsics (as the library uses the
2205 -fno-f2c calling convention), nor for calls to functions which always
2206 require an explicit interface, as no compatibility problems can
2207 arise there. */
2208 if (gfc_option.flag_f2c
b49a3de7 2209 && sym->ts.type == BT_COMPLEX
973ff4c0
TS
2210 && !sym->attr.intrinsic && !sym->attr.always_explicit)
2211 return 1;
66e4ab31 2212
6de9cd9a
DN
2213 return 0;
2214}
2215\f
d198b59a
JJ
2216static tree
2217gfc_get_mixed_entry_union (gfc_namespace *ns)
2218{
2219 tree type;
2220 tree decl;
2221 tree fieldlist;
2222 char name[GFC_MAX_SYMBOL_LEN + 1];
2223 gfc_entry_list *el, *el2;
2224
2225 gcc_assert (ns->proc_name->attr.mixed_entry_master);
2226 gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
2227
2228 snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
2229
2230 /* Build the type node. */
2231 type = make_node (UNION_TYPE);
2232
2233 TYPE_NAME (type) = get_identifier (name);
2234 fieldlist = NULL;
2235
2236 for (el = ns->entries; el; el = el->next)
2237 {
2238 /* Search for duplicates. */
2239 for (el2 = ns->entries; el2 != el; el2 = el2->next)
2240 if (el2->sym->result == el->sym->result)
2241 break;
2242
2243 if (el == el2)
2244 {
c2255bc4
AH
2245 decl = build_decl (input_location,
2246 FIELD_DECL,
d198b59a
JJ
2247 get_identifier (el->sym->result->name),
2248 gfc_sym_type (el->sym->result));
2249 DECL_CONTEXT (decl) = type;
2250 fieldlist = chainon (fieldlist, decl);
2251 }
2252 }
2253
2254 /* Finish off the type. */
2255 TYPE_FIELDS (type) = fieldlist;
2256
2257 gfc_finish_type (type);
dfcf0b12 2258 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
d198b59a
JJ
2259 return type;
2260}
2261\f
6de9cd9a
DN
2262tree
2263gfc_get_function_type (gfc_symbol * sym)
2264{
2265 tree type;
2266 tree typelist;
2267 gfc_formal_arglist *f;
2268 gfc_symbol *arg;
2269 int nstr;
2270 int alternate_return;
2271
ecf24057
FXC
2272 /* Make sure this symbol is a function, a subroutine or the main
2273 program. */
2274 gcc_assert (sym->attr.flavor == FL_PROCEDURE
2275 || sym->attr.flavor == FL_PROGRAM);
6de9cd9a
DN
2276
2277 if (sym->backend_decl)
2278 return TREE_TYPE (sym->backend_decl);
2279
2280 nstr = 0;
2281 alternate_return = 0;
2282 typelist = NULL_TREE;
3d79abbd
PB
2283
2284 if (sym->attr.entry_master)
2285 {
2286 /* Additional parameter for selecting an entry point. */
2287 typelist = gfc_chainon_list (typelist, gfc_array_index_type);
2288 }
2289
bfd61955
TB
2290 if (sym->result)
2291 arg = sym->result;
2292 else
2293 arg = sym;
2294
2295 if (arg->ts.type == BT_CHARACTER)
bc21d315 2296 gfc_conv_const_charlen (arg->ts.u.cl);
bfd61955 2297
6de9cd9a
DN
2298 /* Some functions we use an extra parameter for the return value. */
2299 if (gfc_return_by_reference (sym))
2300 {
6de9cd9a 2301 type = gfc_sym_type (arg);
973ff4c0 2302 if (arg->ts.type == BT_COMPLEX
6de9cd9a
DN
2303 || arg->attr.dimension
2304 || arg->ts.type == BT_CHARACTER)
2305 type = build_reference_type (type);
2306
2307 typelist = gfc_chainon_list (typelist, type);
2308 if (arg->ts.type == BT_CHARACTER)
d7177ab2 2309 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
6de9cd9a
DN
2310 }
2311
436529ea 2312 /* Build the argument types for the function. */
6de9cd9a
DN
2313 for (f = sym->formal; f; f = f->next)
2314 {
2315 arg = f->sym;
2316 if (arg)
2317 {
2318 /* Evaluate constant character lengths here so that they can be
2319 included in the type. */
2320 if (arg->ts.type == BT_CHARACTER)
bc21d315 2321 gfc_conv_const_charlen (arg->ts.u.cl);
6de9cd9a
DN
2322
2323 if (arg->attr.flavor == FL_PROCEDURE)
2324 {
2325 type = gfc_get_function_type (arg);
2326 type = build_pointer_type (type);
2327 }
2328 else
2329 type = gfc_sym_type (arg);
2330
2331 /* Parameter Passing Convention
2332
2333 We currently pass all parameters by reference.
2334 Parameters with INTENT(IN) could be passed by value.
2335 The problem arises if a function is called via an implicit
2336 prototype. In this situation the INTENT is not known.
2337 For this reason all parameters to global functions must be
aa9c57ec 2338 passed by reference. Passing by value would potentially
6de9cd9a 2339 generate bad code. Worse there would be no way of telling that
c3e8c6b8 2340 this code was bad, except that it would give incorrect results.
6de9cd9a
DN
2341
2342 Contained procedures could pass by value as these are never
e2ae1407 2343 used without an explicit interface, and cannot be passed as
c3e8c6b8 2344 actual parameters for a dummy procedure. */
33215bb3 2345 if (arg->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)
6de9cd9a
DN
2346 nstr++;
2347 typelist = gfc_chainon_list (typelist, type);
2348 }
2349 else
2350 {
2351 if (sym->attr.subroutine)
2352 alternate_return = 1;
2353 }
2354 }
2355
2356 /* Add hidden string length parameters. */
2357 while (nstr--)
d7177ab2 2358 typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
6de9cd9a 2359
a7b75755
TB
2360 if (typelist)
2361 typelist = gfc_chainon_list (typelist, void_type_node);
6de9cd9a
DN
2362
2363 if (alternate_return)
2364 type = integer_type_node;
2365 else if (!sym->attr.function || gfc_return_by_reference (sym))
2366 type = void_type_node;
d198b59a
JJ
2367 else if (sym->attr.mixed_entry_master)
2368 type = gfc_get_mixed_entry_union (sym->ns);
da4c6ed8
TS
2369 else if (gfc_option.flag_f2c
2370 && sym->ts.type == BT_REAL
2371 && sym->ts.kind == gfc_default_real_kind
2372 && !sym->attr.always_explicit)
2373 {
2374 /* Special case: f2c calling conventions require that (scalar)
2375 default REAL functions return the C type double instead. f2c
2376 compatibility is only an issue with functions that don't
2377 require an explicit interface, as only these could be
2378 implemented in Fortran 77. */
2379 sym->ts.kind = gfc_default_double_kind;
2380 type = gfc_typenode_for_spec (&sym->ts);
2381 sym->ts.kind = gfc_default_real_kind;
2382 }
726d8566
JW
2383 else if (sym->result && sym->result->attr.proc_pointer)
2384 /* Procedure pointer return values. */
3070bab4
JW
2385 {
2386 if (sym->result->attr.result && strcmp (sym->name,"ppr@") != 0)
2387 {
2388 /* Unset proc_pointer as gfc_get_function_type
2389 is called recursively. */
2390 sym->result->attr.proc_pointer = 0;
2391 type = build_pointer_type (gfc_get_function_type (sym->result));
2392 sym->result->attr.proc_pointer = 1;
2393 }
2394 else
2395 type = gfc_sym_type (sym->result);
2396 }
6de9cd9a
DN
2397 else
2398 type = gfc_sym_type (sym);
2399
2400 type = build_function_type (type, typelist);
2401
2402 return type;
2403}
2404\f
e2cad04b 2405/* Language hooks for middle-end access to type nodes. */
6de9cd9a
DN
2406
2407/* Return an integer type with BITS bits of precision,
2408 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2409
2410tree
2411gfc_type_for_size (unsigned bits, int unsignedp)
2412{
e2cad04b
RH
2413 if (!unsignedp)
2414 {
2415 int i;
2416 for (i = 0; i <= MAX_INT_KINDS; ++i)
2417 {
2418 tree type = gfc_integer_types[i];
2419 if (type && bits == TYPE_PRECISION (type))
2420 return type;
2421 }
5218394a
PB
2422
2423 /* Handle TImode as a special case because it is used by some backends
df2fba9e 2424 (e.g. ARM) even though it is not available for normal use. */
5218394a
PB
2425#if HOST_BITS_PER_WIDE_INT >= 64
2426 if (bits == TYPE_PRECISION (intTI_type_node))
2427 return intTI_type_node;
2428#endif
e2cad04b
RH
2429 }
2430 else
2431 {
2432 if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
2433 return unsigned_intQI_type_node;
2434 if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
2435 return unsigned_intHI_type_node;
2436 if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
2437 return unsigned_intSI_type_node;
2438 if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
2439 return unsigned_intDI_type_node;
2440 if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
2441 return unsigned_intTI_type_node;
2442 }
6de9cd9a 2443
e2cad04b 2444 return NULL_TREE;
6de9cd9a
DN
2445}
2446
e2cad04b
RH
2447/* Return a data type that has machine mode MODE. If the mode is an
2448 integer, then UNSIGNEDP selects between signed and unsigned types. */
6de9cd9a
DN
2449
2450tree
2451gfc_type_for_mode (enum machine_mode mode, int unsignedp)
2452{
e2cad04b
RH
2453 int i;
2454 tree *base;
2455
2456 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2457 base = gfc_real_types;
2458 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2459 base = gfc_complex_types;
2460 else if (SCALAR_INT_MODE_P (mode))
2461 return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
2462 else if (VECTOR_MODE_P (mode))
6de9cd9a 2463 {
f676971a
EC
2464 enum machine_mode inner_mode = GET_MODE_INNER (mode);
2465 tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
2466 if (inner_type != NULL_TREE)
2467 return build_vector_type_for_mode (inner_type, mode);
e2cad04b 2468 return NULL_TREE;
6de9cd9a 2469 }
e2cad04b 2470 else
1a5ffec4 2471 return NULL_TREE;
6de9cd9a 2472
e2cad04b
RH
2473 for (i = 0; i <= MAX_REAL_KINDS; ++i)
2474 {
2475 tree type = base[i];
2476 if (type && mode == TYPE_MODE (type))
2477 return type;
2478 }
2479
2480 return NULL_TREE;
2481}
2482
fad0afd7
JJ
2483/* Return TRUE if TYPE is a type with a hidden descriptor, fill in INFO
2484 in that case. */
2485
2486bool
2487gfc_get_array_descr_info (const_tree type, struct array_descr_info *info)
2488{
2489 int rank, dim;
2490 bool indirect = false;
2491 tree etype, ptype, field, t, base_decl;
c4fae39e 2492 tree data_off, dim_off, dim_size, elem_size;
fad0afd7
JJ
2493 tree lower_suboff, upper_suboff, stride_suboff;
2494
2495 if (! GFC_DESCRIPTOR_TYPE_P (type))
2496 {
2497 if (! POINTER_TYPE_P (type))
2498 return false;
2499 type = TREE_TYPE (type);
2500 if (! GFC_DESCRIPTOR_TYPE_P (type))
2501 return false;
2502 indirect = true;
2503 }
2504
2505 rank = GFC_TYPE_ARRAY_RANK (type);
2506 if (rank >= (int) (sizeof (info->dimen) / sizeof (info->dimen[0])))
2507 return false;
2508
2509 etype = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
2510 gcc_assert (POINTER_TYPE_P (etype));
2511 etype = TREE_TYPE (etype);
2512 gcc_assert (TREE_CODE (etype) == ARRAY_TYPE);
2513 etype = TREE_TYPE (etype);
2514 /* Can't handle variable sized elements yet. */
2515 if (int_size_in_bytes (etype) <= 0)
2516 return false;
2517 /* Nor non-constant lower bounds in assumed shape arrays. */
2518 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE)
2519 {
2520 for (dim = 0; dim < rank; dim++)
2521 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE
2522 || TREE_CODE (GFC_TYPE_ARRAY_LBOUND (type, dim)) != INTEGER_CST)
2523 return false;
2524 }
2525
2526 memset (info, '\0', sizeof (*info));
2527 info->ndimensions = rank;
2528 info->element_type = etype;
2529 ptype = build_pointer_type (gfc_array_index_type);
d560566a
AO
2530 base_decl = GFC_TYPE_ARRAY_BASE_DECL (type, indirect);
2531 if (!base_decl)
fad0afd7 2532 {
c2255bc4 2533 base_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
d560566a
AO
2534 indirect ? build_pointer_type (ptype) : ptype);
2535 GFC_TYPE_ARRAY_BASE_DECL (type, indirect) = base_decl;
fad0afd7 2536 }
d560566a
AO
2537 info->base_decl = base_decl;
2538 if (indirect)
2539 base_decl = build1 (INDIRECT_REF, ptype, base_decl);
fad0afd7 2540
de870512
JJ
2541 if (GFC_TYPE_ARRAY_SPAN (type))
2542 elem_size = GFC_TYPE_ARRAY_SPAN (type);
2543 else
2544 elem_size = fold_convert (gfc_array_index_type, TYPE_SIZE_UNIT (etype));
fad0afd7
JJ
2545 field = TYPE_FIELDS (TYPE_MAIN_VARIANT (type));
2546 data_off = byte_position (field);
2547 field = TREE_CHAIN (field);
fad0afd7
JJ
2548 field = TREE_CHAIN (field);
2549 field = TREE_CHAIN (field);
2550 dim_off = byte_position (field);
2551 dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (field)));
2552 field = TYPE_FIELDS (TREE_TYPE (TREE_TYPE (field)));
2553 stride_suboff = byte_position (field);
2554 field = TREE_CHAIN (field);
2555 lower_suboff = byte_position (field);
2556 field = TREE_CHAIN (field);
2557 upper_suboff = byte_position (field);
2558
2559 t = base_decl;
2560 if (!integer_zerop (data_off))
2561 t = build2 (POINTER_PLUS_EXPR, ptype, t, data_off);
2562 t = build1 (NOP_EXPR, build_pointer_type (ptr_type_node), t);
2563 info->data_location = build1 (INDIRECT_REF, ptr_type_node, t);
2564 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
2565 info->allocated = build2 (NE_EXPR, boolean_type_node,
2566 info->data_location, null_pointer_node);
2567 else if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER)
2568 info->associated = build2 (NE_EXPR, boolean_type_node,
2569 info->data_location, null_pointer_node);
2570
2571 for (dim = 0; dim < rank; dim++)
2572 {
2573 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2574 size_binop (PLUS_EXPR, dim_off, lower_suboff));
2575 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2576 info->dimen[dim].lower_bound = t;
2577 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2578 size_binop (PLUS_EXPR, dim_off, upper_suboff));
2579 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2580 info->dimen[dim].upper_bound = t;
2581 if (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_SHAPE)
2582 {
2583 /* Assumed shape arrays have known lower bounds. */
2584 info->dimen[dim].upper_bound
2585 = build2 (MINUS_EXPR, gfc_array_index_type,
2586 info->dimen[dim].upper_bound,
2587 info->dimen[dim].lower_bound);
2588 info->dimen[dim].lower_bound
2589 = fold_convert (gfc_array_index_type,
2590 GFC_TYPE_ARRAY_LBOUND (type, dim));
2591 info->dimen[dim].upper_bound
2592 = build2 (PLUS_EXPR, gfc_array_index_type,
2593 info->dimen[dim].lower_bound,
2594 info->dimen[dim].upper_bound);
2595 }
2596 t = build2 (POINTER_PLUS_EXPR, ptype, base_decl,
2597 size_binop (PLUS_EXPR, dim_off, stride_suboff));
2598 t = build1 (INDIRECT_REF, gfc_array_index_type, t);
2599 t = build2 (MULT_EXPR, gfc_array_index_type, t, elem_size);
2600 info->dimen[dim].stride = t;
2601 dim_off = size_binop (PLUS_EXPR, dim_off, dim_size);
2602 }
2603
2604 return true;
2605}
2606
6de9cd9a 2607#include "gt-fortran-trans-types.h"