OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [trans-types.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Backend support for Fortran 95 basic types and derived types.
2
   Copyright (C) 2002, 2003, 2004, 2005 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 2, 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 COPYING.  If not, write to the Free
20
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21
02110-1301, USA.  */
22
 
23
/* trans-types.c -- gfortran backend types */
24
 
25
#include "config.h"
26
#include "system.h"
27
#include "coretypes.h"
28
#include "tree.h"
29
#include "tm.h"
30
#include "target.h"
31
#include "ggc.h"
32
#include "toplev.h"
33
#include "gfortran.h"
34
#include "trans.h"
35
#include "trans-types.h"
36
#include "trans-const.h"
37
#include "real.h"
38
 
39
 
40
#if (GFC_MAX_DIMENSIONS < 10)
41
#define GFC_RANK_DIGITS 1
42
#define GFC_RANK_PRINTF_FORMAT "%01d"
43
#elif (GFC_MAX_DIMENSIONS < 100)
44
#define GFC_RANK_DIGITS 2
45
#define GFC_RANK_PRINTF_FORMAT "%02d"
46
#else
47
#error If you really need >99 dimensions, continue the sequence above...
48
#endif
49
 
50
static tree gfc_get_derived_type (gfc_symbol * derived);
51
 
52
tree gfc_array_index_type;
53
tree gfc_array_range_type;
54
tree gfc_character1_type_node;
55
tree pvoid_type_node;
56
tree ppvoid_type_node;
57
tree pchar_type_node;
58
 
59
tree gfc_charlen_type_node;
60
 
61
static GTY(()) tree gfc_desc_dim_type;
62
static GTY(()) tree gfc_max_array_element_size;
63
static GTY(()) tree gfc_array_descriptor_base[GFC_MAX_DIMENSIONS];
64
 
65
/* Arrays for all integral and real kinds.  We'll fill this in at runtime
66
   after the target has a chance to process command-line options.  */
67
 
68
#define MAX_INT_KINDS 5
69
gfc_integer_info gfc_integer_kinds[MAX_INT_KINDS + 1];
70
gfc_logical_info gfc_logical_kinds[MAX_INT_KINDS + 1];
71
static GTY(()) tree gfc_integer_types[MAX_INT_KINDS + 1];
72
static GTY(()) tree gfc_logical_types[MAX_INT_KINDS + 1];
73
 
74
#define MAX_REAL_KINDS 5
75
gfc_real_info gfc_real_kinds[MAX_REAL_KINDS + 1];
76
static GTY(()) tree gfc_real_types[MAX_REAL_KINDS + 1];
77
static GTY(()) tree gfc_complex_types[MAX_REAL_KINDS + 1];
78
 
79
/* The integer kind to use for array indices.  This will be set to the
80
   proper value based on target information from the backend.  */
81
 
82
int gfc_index_integer_kind;
83
 
84
/* The default kinds of the various types.  */
85
 
86
int gfc_default_integer_kind;
87
int gfc_max_integer_kind;
88
int gfc_default_real_kind;
89
int gfc_default_double_kind;
90
int gfc_default_character_kind;
91
int gfc_default_logical_kind;
92
int gfc_default_complex_kind;
93
int gfc_c_int_kind;
94
 
95
/* Query the target to determine which machine modes are available for
96
   computation.  Choose KIND numbers for them.  */
97
 
98
void
99
gfc_init_kinds (void)
100
{
101
  enum machine_mode mode;
102
  int i_index, r_index;
103
  bool saw_i4 = false, saw_i8 = false;
104
  bool saw_r4 = false, saw_r8 = false, saw_r16 = false;
105
 
106
  for (i_index = 0, mode = MIN_MODE_INT; mode <= MAX_MODE_INT; mode++)
107
    {
108
      int kind, bitsize;
109
 
110
      if (!targetm.scalar_mode_supported_p (mode))
111
        continue;
112
 
113
      /* The middle end doesn't support constants larger than 2*HWI.
114
         Perhaps the target hook shouldn't have accepted these either,
115
         but just to be safe...  */
116
      bitsize = GET_MODE_BITSIZE (mode);
117
      if (bitsize > 2*HOST_BITS_PER_WIDE_INT)
118
        continue;
119
 
120
      gcc_assert (i_index != MAX_INT_KINDS);
121
 
122
      /* Let the kind equal the bit size divided by 8.  This insulates the
123
         programmer from the underlying byte size.  */
124
      kind = bitsize / 8;
125
 
126
      if (kind == 4)
127
        saw_i4 = true;
128
      if (kind == 8)
129
        saw_i8 = true;
130
 
131
      gfc_integer_kinds[i_index].kind = kind;
132
      gfc_integer_kinds[i_index].radix = 2;
133
      gfc_integer_kinds[i_index].digits = bitsize - 1;
134
      gfc_integer_kinds[i_index].bit_size = bitsize;
135
 
136
      gfc_logical_kinds[i_index].kind = kind;
137
      gfc_logical_kinds[i_index].bit_size = bitsize;
138
 
139
      i_index += 1;
140
    }
141
 
142
  /* Set the maximum integer kind.  Used with at least BOZ constants.  */
143
  gfc_max_integer_kind = gfc_integer_kinds[i_index - 1].kind;
144
 
145
  for (r_index = 0, mode = MIN_MODE_FLOAT; mode <= MAX_MODE_FLOAT; mode++)
146
    {
147
      const struct real_format *fmt = REAL_MODE_FORMAT (mode);
148
      int kind;
149
 
150
      if (fmt == NULL)
151
        continue;
152
      if (!targetm.scalar_mode_supported_p (mode))
153
        continue;
154
 
155
      /* Only let float/double/long double go through because the fortran
156
         library assumes these are the only floating point types.  */
157
 
158
      if (mode != TYPE_MODE (float_type_node)
159
          && (mode != TYPE_MODE (double_type_node))
160
          && (mode != TYPE_MODE (long_double_type_node)))
161
        continue;
162
 
163
      /* Let the kind equal the precision divided by 8, rounding up.  Again,
164
         this insulates the programmer from the underlying byte size.
165
 
166
         Also, it effectively deals with IEEE extended formats.  There, the
167
         total size of the type may equal 16, but it's got 6 bytes of padding
168
         and the increased size can get in the way of a real IEEE quad format
169
         which may also be supported by the target.
170
 
171
         We round up so as to handle IA-64 __floatreg (RFmode), which is an
172
         82 bit type.  Not to be confused with __float80 (XFmode), which is
173
         an 80 bit type also supported by IA-64.  So XFmode should come out
174
         to be kind=10, and RFmode should come out to be kind=11.  Egads.  */
175
 
176
      kind = (GET_MODE_PRECISION (mode) + 7) / 8;
177
 
178
      if (kind == 4)
179
        saw_r4 = true;
180
      if (kind == 8)
181
        saw_r8 = true;
182
      if (kind == 16)
183
        saw_r16 = true;
184
 
185
      /* Careful we don't stumble a wierd internal mode.  */
186
      gcc_assert (r_index <= 0 || gfc_real_kinds[r_index-1].kind != kind);
187
      /* Or have too many modes for the allocated space.  */
188
      gcc_assert (r_index != MAX_REAL_KINDS);
189
 
190
      gfc_real_kinds[r_index].kind = kind;
191
      gfc_real_kinds[r_index].radix = fmt->b;
192
      gfc_real_kinds[r_index].digits = fmt->p;
193
      gfc_real_kinds[r_index].min_exponent = fmt->emin;
194
      gfc_real_kinds[r_index].max_exponent = fmt->emax;
195
      if (fmt->pnan < fmt->p)
196
        /* This is an IBM extended double format (or the MIPS variant)
197
           made up of two IEEE doubles.  The value of the long double is
198
           the sum of the values of the two parts.  The most significant
199
           part is required to be the value of the long double rounded
200
           to the nearest double.  If we use emax of 1024 then we can't
201
           represent huge(x) = (1 - b**(-p)) * b**(emax-1) * b, because
202
           rounding will make the most significant part overflow.  */
203
        gfc_real_kinds[r_index].max_exponent = fmt->emax - 1;
204
      gfc_real_kinds[r_index].mode_precision = GET_MODE_PRECISION (mode);
205
      r_index += 1;
206
    }
207
 
208
  /* Choose the default integer kind.  We choose 4 unless the user
209
     directs us otherwise.  */
210
  if (gfc_option.flag_default_integer)
211
    {
212
      if (!saw_i8)
213
        fatal_error ("integer kind=8 not available for -fdefault-integer-8 option");
214
      gfc_default_integer_kind = 8;
215
    }
216
  else if (saw_i4)
217
    gfc_default_integer_kind = 4;
218
  else
219
    gfc_default_integer_kind = gfc_integer_kinds[i_index - 1].kind;
220
 
221
  /* Choose the default real kind.  Again, we choose 4 when possible.  */
222
  if (gfc_option.flag_default_real)
223
    {
224
      if (!saw_r8)
225
        fatal_error ("real kind=8 not available for -fdefault-real-8 option");
226
      gfc_default_real_kind = 8;
227
    }
228
  else if (saw_r4)
229
    gfc_default_real_kind = 4;
230
  else
231
    gfc_default_real_kind = gfc_real_kinds[0].kind;
232
 
233
  /* Choose the default double kind.  If -fdefault-real and -fdefault-double
234
     are specified, we use kind=8, if it's available.  If -fdefault-real is
235
     specified without -fdefault-double, we use kind=16, if it's available.
236
     Otherwise we do not change anything.  */
237
  if (gfc_option.flag_default_double && !gfc_option.flag_default_real)
238
    fatal_error ("Use of -fdefault-double-8 requires -fdefault-real-8");
239
 
240
  if (gfc_option.flag_default_real && gfc_option.flag_default_double && saw_r8)
241
    gfc_default_double_kind = 8;
242
  else if (gfc_option.flag_default_real && saw_r16)
243
    gfc_default_double_kind = 16;
244
  else if (saw_r4 && saw_r8)
245
    gfc_default_double_kind = 8;
246
  else
247
    {
248
      /* F95 14.6.3.1: A nonpointer scalar object of type double precision
249
         real ... occupies two contiguous numeric storage units.
250
 
251
         Therefore we must be supplied a kind twice as large as we chose
252
         for single precision.  There are loopholes, in that double
253
         precision must *occupy* two storage units, though it doesn't have
254
         to *use* two storage units.  Which means that you can make this
255
         kind artificially wide by padding it.  But at present there are
256
         no GCC targets for which a two-word type does not exist, so we
257
         just let gfc_validate_kind abort and tell us if something breaks.  */
258
 
259
      gfc_default_double_kind
260
        = gfc_validate_kind (BT_REAL, gfc_default_real_kind * 2, false);
261
    }
262
 
263
  /* The default logical kind is constrained to be the same as the
264
     default integer kind.  Similarly with complex and real.  */
265
  gfc_default_logical_kind = gfc_default_integer_kind;
266
  gfc_default_complex_kind = gfc_default_real_kind;
267
 
268
  /* Choose the smallest integer kind for our default character.  */
269
  gfc_default_character_kind = gfc_integer_kinds[0].kind;
270
 
271
  /* Choose the integer kind the same size as "void*" for our index kind.  */
272
  gfc_index_integer_kind = POINTER_SIZE / 8;
273
  /* Pick a kind the same size as the C "int" type.  */
274
  gfc_c_int_kind = INT_TYPE_SIZE / 8;
275
}
276
 
277
/* Make sure that a valid kind is present.  Returns an index into the
278
   associated kinds array, -1 if the kind is not present.  */
279
 
280
static int
281
validate_integer (int kind)
282
{
283
  int i;
284
 
285
  for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
286
    if (gfc_integer_kinds[i].kind == kind)
287
      return i;
288
 
289
  return -1;
290
}
291
 
292
static int
293
validate_real (int kind)
294
{
295
  int i;
296
 
297
  for (i = 0; gfc_real_kinds[i].kind != 0; i++)
298
    if (gfc_real_kinds[i].kind == kind)
299
      return i;
300
 
301
  return -1;
302
}
303
 
304
static int
305
validate_logical (int kind)
306
{
307
  int i;
308
 
309
  for (i = 0; gfc_logical_kinds[i].kind; i++)
310
    if (gfc_logical_kinds[i].kind == kind)
311
      return i;
312
 
313
  return -1;
314
}
315
 
316
static int
317
validate_character (int kind)
318
{
319
  return kind == gfc_default_character_kind ? 0 : -1;
320
}
321
 
322
/* Validate a kind given a basic type.  The return value is the same
323
   for the child functions, with -1 indicating nonexistence of the
324
   type.  If MAY_FAIL is false, then -1 is never returned, and we ICE.  */
325
 
326
int
327
gfc_validate_kind (bt type, int kind, bool may_fail)
328
{
329
  int rc;
330
 
331
  switch (type)
332
    {
333
    case BT_REAL:               /* Fall through */
334
    case BT_COMPLEX:
335
      rc = validate_real (kind);
336
      break;
337
    case BT_INTEGER:
338
      rc = validate_integer (kind);
339
      break;
340
    case BT_LOGICAL:
341
      rc = validate_logical (kind);
342
      break;
343
    case BT_CHARACTER:
344
      rc = validate_character (kind);
345
      break;
346
 
347
    default:
348
      gfc_internal_error ("gfc_validate_kind(): Got bad type");
349
    }
350
 
351
  if (rc < 0 && !may_fail)
352
    gfc_internal_error ("gfc_validate_kind(): Got bad kind");
353
 
354
  return rc;
355
}
356
 
357
 
358
/* Four subroutines of gfc_init_types.  Create type nodes for the given kind.
359
   Reuse common type nodes where possible.  Recognize if the kind matches up
360
   with a C type.  This will be used later in determining which routines may
361
   be scarfed from libm.  */
362
 
363
static tree
364
gfc_build_int_type (gfc_integer_info *info)
365
{
366
  int mode_precision = info->bit_size;
367
 
368
  if (mode_precision == CHAR_TYPE_SIZE)
369
    info->c_char = 1;
370
  if (mode_precision == SHORT_TYPE_SIZE)
371
    info->c_short = 1;
372
  if (mode_precision == INT_TYPE_SIZE)
373
    info->c_int = 1;
374
  if (mode_precision == LONG_TYPE_SIZE)
375
    info->c_long = 1;
376
  if (mode_precision == LONG_LONG_TYPE_SIZE)
377
    info->c_long_long = 1;
378
 
379
  if (TYPE_PRECISION (intQI_type_node) == mode_precision)
380
    return intQI_type_node;
381
  if (TYPE_PRECISION (intHI_type_node) == mode_precision)
382
    return intHI_type_node;
383
  if (TYPE_PRECISION (intSI_type_node) == mode_precision)
384
    return intSI_type_node;
385
  if (TYPE_PRECISION (intDI_type_node) == mode_precision)
386
    return intDI_type_node;
387
  if (TYPE_PRECISION (intTI_type_node) == mode_precision)
388
    return intTI_type_node;
389
 
390
  return make_signed_type (mode_precision);
391
}
392
 
393
static tree
394
gfc_build_real_type (gfc_real_info *info)
395
{
396
  int mode_precision = info->mode_precision;
397
  tree new_type;
398
 
399
  if (mode_precision == FLOAT_TYPE_SIZE)
400
    info->c_float = 1;
401
  if (mode_precision == DOUBLE_TYPE_SIZE)
402
    info->c_double = 1;
403
  if (mode_precision == LONG_DOUBLE_TYPE_SIZE)
404
    info->c_long_double = 1;
405
 
406
  if (TYPE_PRECISION (float_type_node) == mode_precision)
407
    return float_type_node;
408
  if (TYPE_PRECISION (double_type_node) == mode_precision)
409
    return double_type_node;
410
  if (TYPE_PRECISION (long_double_type_node) == mode_precision)
411
    return long_double_type_node;
412
 
413
  new_type = make_node (REAL_TYPE);
414
  TYPE_PRECISION (new_type) = mode_precision;
415
  layout_type (new_type);
416
  return new_type;
417
}
418
 
419
static tree
420
gfc_build_complex_type (tree scalar_type)
421
{
422
  tree new_type;
423
 
424
  if (scalar_type == NULL)
425
    return NULL;
426
  if (scalar_type == float_type_node)
427
    return complex_float_type_node;
428
  if (scalar_type == double_type_node)
429
    return complex_double_type_node;
430
  if (scalar_type == long_double_type_node)
431
    return complex_long_double_type_node;
432
 
433
  new_type = make_node (COMPLEX_TYPE);
434
  TREE_TYPE (new_type) = scalar_type;
435
  layout_type (new_type);
436
  return new_type;
437
}
438
 
439
static tree
440
gfc_build_logical_type (gfc_logical_info *info)
441
{
442
  int bit_size = info->bit_size;
443
  tree new_type;
444
 
445
  if (bit_size == BOOL_TYPE_SIZE)
446
    {
447
      info->c_bool = 1;
448
      return boolean_type_node;
449
    }
450
 
451
  new_type = make_unsigned_type (bit_size);
452
  TREE_SET_CODE (new_type, BOOLEAN_TYPE);
453
  TYPE_MAX_VALUE (new_type) = build_int_cst (new_type, 1);
454
  TYPE_PRECISION (new_type) = 1;
455
 
456
  return new_type;
457
}
458
 
459
#if 0
460
/* Return the bit size of the C "size_t".  */
461
 
462
static unsigned int
463
c_size_t_size (void)
464
{
465
#ifdef SIZE_TYPE  
466
  if (strcmp (SIZE_TYPE, "unsigned int") == 0)
467
    return INT_TYPE_SIZE;
468
  if (strcmp (SIZE_TYPE, "long unsigned int") == 0)
469
    return LONG_TYPE_SIZE;
470
  if (strcmp (SIZE_TYPE, "short unsigned int") == 0)
471
    return SHORT_TYPE_SIZE;
472
  gcc_unreachable ();
473
#else
474
  return LONG_TYPE_SIZE;
475
#endif
476
}
477
#endif
478
 
479
/* Create the backend type nodes. We map them to their
480
   equivalent C type, at least for now.  We also give
481
   names to the types here, and we push them in the
482
   global binding level context.*/
483
 
484
void
485
gfc_init_types (void)
486
{
487
  char name_buf[16];
488
  int index;
489
  tree type;
490
  unsigned n;
491
  unsigned HOST_WIDE_INT hi;
492
  unsigned HOST_WIDE_INT lo;
493
 
494
  /* Create and name the types.  */
495
#define PUSH_TYPE(name, node) \
496
  pushdecl (build_decl (TYPE_DECL, get_identifier (name), node))
497
 
498
  for (index = 0; gfc_integer_kinds[index].kind != 0; ++index)
499
    {
500
      type = gfc_build_int_type (&gfc_integer_kinds[index]);
501
      gfc_integer_types[index] = type;
502
      snprintf (name_buf, sizeof(name_buf), "int%d",
503
                gfc_integer_kinds[index].kind);
504
      PUSH_TYPE (name_buf, type);
505
    }
506
 
507
  for (index = 0; gfc_logical_kinds[index].kind != 0; ++index)
508
    {
509
      type = gfc_build_logical_type (&gfc_logical_kinds[index]);
510
      gfc_logical_types[index] = type;
511
      snprintf (name_buf, sizeof(name_buf), "logical%d",
512
                gfc_logical_kinds[index].kind);
513
      PUSH_TYPE (name_buf, type);
514
    }
515
 
516
  for (index = 0; gfc_real_kinds[index].kind != 0; index++)
517
    {
518
      type = gfc_build_real_type (&gfc_real_kinds[index]);
519
      gfc_real_types[index] = type;
520
      snprintf (name_buf, sizeof(name_buf), "real%d",
521
                gfc_real_kinds[index].kind);
522
      PUSH_TYPE (name_buf, type);
523
 
524
      type = gfc_build_complex_type (type);
525
      gfc_complex_types[index] = type;
526
      snprintf (name_buf, sizeof(name_buf), "complex%d",
527
                gfc_real_kinds[index].kind);
528
      PUSH_TYPE (name_buf, type);
529
    }
530
 
531
  gfc_character1_type_node = build_type_variant (unsigned_char_type_node,
532
                                                 0, 0);
533
  PUSH_TYPE ("char", gfc_character1_type_node);
534
 
535
  PUSH_TYPE ("byte", unsigned_char_type_node);
536
  PUSH_TYPE ("void", void_type_node);
537
 
538
  /* DBX debugging output gets upset if these aren't set.  */
539
  if (!TYPE_NAME (integer_type_node))
540
    PUSH_TYPE ("c_integer", integer_type_node);
541
  if (!TYPE_NAME (char_type_node))
542
    PUSH_TYPE ("c_char", char_type_node);
543
 
544
#undef PUSH_TYPE
545
 
546
  pvoid_type_node = build_pointer_type (void_type_node);
547
  ppvoid_type_node = build_pointer_type (pvoid_type_node);
548
  pchar_type_node = build_pointer_type (gfc_character1_type_node);
549
 
550
  gfc_array_index_type = gfc_get_int_type (gfc_index_integer_kind);
551
  /* We cannot use gfc_index_zero_node in definition of gfc_array_range_type,
552
     since this function is called before gfc_init_constants.  */
553
  gfc_array_range_type
554
          = build_range_type (gfc_array_index_type,
555
                              build_int_cst (gfc_array_index_type, 0),
556
                              NULL_TREE);
557
 
558
  /* The maximum array element size that can be handled is determined
559
     by the number of bits available to store this field in the array
560
     descriptor.  */
561
 
562
  n = TYPE_PRECISION (gfc_array_index_type) - GFC_DTYPE_SIZE_SHIFT;
563
  lo = ~ (unsigned HOST_WIDE_INT) 0;
564
  if (n > HOST_BITS_PER_WIDE_INT)
565
    hi = lo >> (2*HOST_BITS_PER_WIDE_INT - n);
566
  else
567
    hi = 0, lo >>= HOST_BITS_PER_WIDE_INT - n;
568
  gfc_max_array_element_size
569
    = build_int_cst_wide (long_unsigned_type_node, lo, hi);
570
 
571
  size_type_node = gfc_array_index_type;
572
 
573
  boolean_type_node = gfc_get_logical_type (gfc_default_logical_kind);
574
  boolean_true_node = build_int_cst (boolean_type_node, 1);
575
  boolean_false_node = build_int_cst (boolean_type_node, 0);
576
 
577
  /* ??? Shouldn't this be based on gfc_index_integer_kind or so?  */
578
  gfc_charlen_type_node = gfc_get_int_type (4);
579
}
580
 
581
/* Get the type node for the given type and kind.  */
582
 
583
tree
584
gfc_get_int_type (int kind)
585
{
586
  int index = gfc_validate_kind (BT_INTEGER, kind, true);
587
  return index < 0 ? 0 : gfc_integer_types[index];
588
}
589
 
590
tree
591
gfc_get_real_type (int kind)
592
{
593
  int index = gfc_validate_kind (BT_REAL, kind, true);
594
  return index < 0 ? 0 : gfc_real_types[index];
595
}
596
 
597
tree
598
gfc_get_complex_type (int kind)
599
{
600
  int index = gfc_validate_kind (BT_COMPLEX, kind, true);
601
  return index < 0 ? 0 : gfc_complex_types[index];
602
}
603
 
604
tree
605
gfc_get_logical_type (int kind)
606
{
607
  int index = gfc_validate_kind (BT_LOGICAL, kind, true);
608
  return index < 0 ? 0 : gfc_logical_types[index];
609
}
610
 
611
/* Create a character type with the given kind and length.  */
612
 
613
tree
614
gfc_get_character_type_len (int kind, tree len)
615
{
616
  tree bounds, type;
617
 
618
  gfc_validate_kind (BT_CHARACTER, kind, false);
619
 
620
  bounds = build_range_type (gfc_charlen_type_node, gfc_index_one_node, len);
621
  type = build_array_type (gfc_character1_type_node, bounds);
622
  TYPE_STRING_FLAG (type) = 1;
623
 
624
  return type;
625
}
626
 
627
 
628
/* Get a type node for a character kind.  */
629
 
630
tree
631
gfc_get_character_type (int kind, gfc_charlen * cl)
632
{
633
  tree len;
634
 
635
  len = (cl == NULL) ? NULL_TREE : cl->backend_decl;
636
 
637
  return gfc_get_character_type_len (kind, len);
638
}
639
 
640
/* Covert a basic type.  This will be an array for character types.  */
641
 
642
tree
643
gfc_typenode_for_spec (gfc_typespec * spec)
644
{
645
  tree basetype;
646
 
647
  switch (spec->type)
648
    {
649
    case BT_UNKNOWN:
650
      gcc_unreachable ();
651
 
652
    case BT_INTEGER:
653
      basetype = gfc_get_int_type (spec->kind);
654
      break;
655
 
656
    case BT_REAL:
657
      basetype = gfc_get_real_type (spec->kind);
658
      break;
659
 
660
    case BT_COMPLEX:
661
      basetype = gfc_get_complex_type (spec->kind);
662
      break;
663
 
664
    case BT_LOGICAL:
665
      basetype = gfc_get_logical_type (spec->kind);
666
      break;
667
 
668
    case BT_CHARACTER:
669
      basetype = gfc_get_character_type (spec->kind, spec->cl);
670
      break;
671
 
672
    case BT_DERIVED:
673
      basetype = gfc_get_derived_type (spec->derived);
674
      break;
675
 
676
    default:
677
      gcc_unreachable ();
678
    }
679
  return basetype;
680
}
681
 
682
/* Build an INT_CST for constant expressions, otherwise return NULL_TREE.  */
683
 
684
static tree
685
gfc_conv_array_bound (gfc_expr * expr)
686
{
687
  /* If expr is an integer constant, return that.  */
688
  if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
689
    return gfc_conv_mpz_to_tree (expr->value.integer, gfc_index_integer_kind);
690
 
691
  /* Otherwise return NULL.  */
692
  return NULL_TREE;
693
}
694
 
695
tree
696
gfc_get_element_type (tree type)
697
{
698
  tree element;
699
 
700
  if (GFC_ARRAY_TYPE_P (type))
701
    {
702
      if (TREE_CODE (type) == POINTER_TYPE)
703
        type = TREE_TYPE (type);
704
      gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
705
      element = TREE_TYPE (type);
706
    }
707
  else
708
    {
709
      gcc_assert (GFC_DESCRIPTOR_TYPE_P (type));
710
      element = GFC_TYPE_ARRAY_DATAPTR_TYPE (type);
711
 
712
      gcc_assert (TREE_CODE (element) == POINTER_TYPE);
713
      element = TREE_TYPE (element);
714
 
715
      gcc_assert (TREE_CODE (element) == ARRAY_TYPE);
716
      element = TREE_TYPE (element);
717
    }
718
 
719
  return element;
720
}
721
 
722
/* Build an array. This function is called from gfc_sym_type().
723
   Actually returns array descriptor type.
724
 
725
   Format of array descriptors is as follows:
726
 
727
    struct gfc_array_descriptor
728
    {
729
      array *data
730
      index offset;
731
      index dtype;
732
      struct descriptor_dimension dimension[N_DIM];
733
    }
734
 
735
    struct descriptor_dimension
736
    {
737
      index stride;
738
      index lbound;
739
      index ubound;
740
    }
741
 
742
   Translation code should use gfc_conv_descriptor_* rather than accessing
743
   the descriptor directly. Any changes to the array descriptor type will
744
   require changes in gfc_conv_descriptor_* and gfc_build_array_initializer.
745
 
746
   This is represented internally as a RECORD_TYPE. The index nodes are
747
   gfc_array_index_type and the data node is a pointer to the data. See below
748
   for the handling of character types.
749
 
750
   The dtype member is formatted as follows:
751
    rank = dtype & GFC_DTYPE_RANK_MASK // 3 bits
752
    type = (dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT // 3 bits
753
    size = dtype >> GFC_DTYPE_SIZE_SHIFT
754
 
755
   I originally used nested ARRAY_TYPE nodes to represent arrays, but this
756
   generated poor code for assumed/deferred size arrays.  These require
757
   use of PLACEHOLDER_EXPR/WITH_RECORD_EXPR, which isn't part of the GENERIC
758
   grammar.  Also, there is no way to explicitly set the array stride, so
759
   all data must be packed(1).  I've tried to mark all the functions which
760
   would require modification with a GCC ARRAYS comment.
761
 
762
   The data component points to the first element in the array.
763
   The offset field is the position of the origin of the array
764
   (ie element (0, 0 ...)).  This may be outsite the bounds of the array.
765
 
766
   An element is accessed by
767
   data[offset + index0*stride0 + index1*stride1 + index2*stride2]
768
   This gives good performance as the computation does not involve the
769
   bounds of the array.  For packed arrays, this is optimized further by
770
   substituting the known strides.
771
 
772
   This system has one problem: all array bounds must be withing 2^31 elements
773
   of the origin (2^63 on 64-bit machines).  For example
774
   integer, dimension (80000:90000, 80000:90000, 2) :: array
775
   may not work properly on 32-bit machines because 80000*80000 > 2^31, so
776
   the calculation for stride02 would overflow.  This may still work, but
777
   I haven't checked, and it relies on the overflow doing the right thing.
778
 
779
   The way to fix this problem is to access elements as follows:
780
   data[(index0-lbound0)*stride0 + (index1-lbound1)*stride1]
781
   Obviously this is much slower.  I will make this a compile time option,
782
   something like -fsmall-array-offsets.  Mixing code compiled with and without
783
   this switch will work.
784
 
785
   (1) This can be worked around by modifying the upper bound of the previous
786
   dimension.  This requires extra fields in the descriptor (both real_ubound
787
   and fake_ubound).  In tree.def there is mention of TYPE_SEP, which
788
   may allow us to do this.  However I can't find mention of this anywhere
789
   else.  */
790
 
791
 
792
/* Returns true if the array sym does not require a descriptor.  */
793
 
794
int
795
gfc_is_nodesc_array (gfc_symbol * sym)
796
{
797
  gcc_assert (sym->attr.dimension);
798
 
799
  /* We only want local arrays.  */
800
  if (sym->attr.pointer || sym->attr.allocatable)
801
    return 0;
802
 
803
  if (sym->attr.dummy)
804
    {
805
      if (sym->as->type != AS_ASSUMED_SHAPE)
806
        return 1;
807
      else
808
        return 0;
809
    }
810
 
811
  if (sym->attr.result || sym->attr.function)
812
    return 0;
813
 
814
  gcc_assert (sym->as->type == AS_EXPLICIT);
815
 
816
  return 1;
817
}
818
 
819
 
820
/* Create an array descriptor type.  */
821
 
822
static tree
823
gfc_build_array_type (tree type, gfc_array_spec * as)
824
{
825
  tree lbound[GFC_MAX_DIMENSIONS];
826
  tree ubound[GFC_MAX_DIMENSIONS];
827
  int n;
828
 
829
  for (n = 0; n < as->rank; n++)
830
    {
831
      /* Create expressions for the known bounds of the array.  */
832
      if (as->type == AS_ASSUMED_SHAPE && as->lower[n] == NULL)
833
        lbound[n] = gfc_index_one_node;
834
      else
835
        lbound[n] = gfc_conv_array_bound (as->lower[n]);
836
      ubound[n] = gfc_conv_array_bound (as->upper[n]);
837
    }
838
 
839
  return gfc_get_array_type_bounds (type, as->rank, lbound, ubound, 0);
840
}
841
 
842
/* Returns the struct descriptor_dimension type.  */
843
 
844
static tree
845
gfc_get_desc_dim_type (void)
846
{
847
  tree type;
848
  tree decl;
849
  tree fieldlist;
850
 
851
  if (gfc_desc_dim_type)
852
    return gfc_desc_dim_type;
853
 
854
  /* Build the type node.  */
855
  type = make_node (RECORD_TYPE);
856
 
857
  TYPE_NAME (type) = get_identifier ("descriptor_dimension");
858
  TYPE_PACKED (type) = 1;
859
 
860
  /* Consists of the stride, lbound and ubound members.  */
861
  decl = build_decl (FIELD_DECL,
862
                     get_identifier ("stride"), gfc_array_index_type);
863
  DECL_CONTEXT (decl) = type;
864
  fieldlist = decl;
865
 
866
  decl = build_decl (FIELD_DECL,
867
                     get_identifier ("lbound"), gfc_array_index_type);
868
  DECL_CONTEXT (decl) = type;
869
  fieldlist = chainon (fieldlist, decl);
870
 
871
  decl = build_decl (FIELD_DECL,
872
                     get_identifier ("ubound"), gfc_array_index_type);
873
  DECL_CONTEXT (decl) = type;
874
  fieldlist = chainon (fieldlist, decl);
875
 
876
  /* Finish off the type.  */
877
  TYPE_FIELDS (type) = fieldlist;
878
 
879
  gfc_finish_type (type);
880
 
881
  gfc_desc_dim_type = type;
882
  return type;
883
}
884
 
885
 
886
/* Return the DTYPE for an array.  This describes the type and type parameters
887
   of the array.  */
888
/* TODO: Only call this when the value is actually used, and make all the
889
   unknown cases abort.  */
890
 
891
tree
892
gfc_get_dtype (tree type)
893
{
894
  tree size;
895
  int n;
896
  HOST_WIDE_INT i;
897
  tree tmp;
898
  tree dtype;
899
  tree etype;
900
  int rank;
901
 
902
  gcc_assert (GFC_DESCRIPTOR_TYPE_P (type) || GFC_ARRAY_TYPE_P (type));
903
 
904
  if (GFC_TYPE_ARRAY_DTYPE (type))
905
    return GFC_TYPE_ARRAY_DTYPE (type);
906
 
907
  rank = GFC_TYPE_ARRAY_RANK (type);
908
  etype = gfc_get_element_type (type);
909
 
910
  switch (TREE_CODE (etype))
911
    {
912
    case INTEGER_TYPE:
913
      n = GFC_DTYPE_INTEGER;
914
      break;
915
 
916
    case BOOLEAN_TYPE:
917
      n = GFC_DTYPE_LOGICAL;
918
      break;
919
 
920
    case REAL_TYPE:
921
      n = GFC_DTYPE_REAL;
922
      break;
923
 
924
    case COMPLEX_TYPE:
925
      n = GFC_DTYPE_COMPLEX;
926
      break;
927
 
928
    /* We will never have arrays of arrays.  */
929
    case RECORD_TYPE:
930
      n = GFC_DTYPE_DERIVED;
931
      break;
932
 
933
    case ARRAY_TYPE:
934
      n = GFC_DTYPE_CHARACTER;
935
      break;
936
 
937
    default:
938
      /* TODO: Don't do dtype for temporary descriptorless arrays.  */
939
      /* We can strange array types for temporary arrays.  */
940
      return gfc_index_zero_node;
941
    }
942
 
943
  gcc_assert (rank <= GFC_DTYPE_RANK_MASK);
944
  size = TYPE_SIZE_UNIT (etype);
945
 
946
  i = rank | (n << GFC_DTYPE_TYPE_SHIFT);
947
  if (size && INTEGER_CST_P (size))
948
    {
949
      if (tree_int_cst_lt (gfc_max_array_element_size, size))
950
        internal_error ("Array element size too big");
951
 
952
      i += TREE_INT_CST_LOW (size) << GFC_DTYPE_SIZE_SHIFT;
953
    }
954
  dtype = build_int_cst (gfc_array_index_type, i);
955
 
956
  if (size && !INTEGER_CST_P (size))
957
    {
958
      tmp = build_int_cst (gfc_array_index_type, GFC_DTYPE_SIZE_SHIFT);
959
      tmp  = fold_build2 (LSHIFT_EXPR, gfc_array_index_type, size, tmp);
960
      dtype = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp, dtype);
961
    }
962
  /* If we don't know the size we leave it as zero.  This should never happen
963
     for anything that is actually used.  */
964
  /* TODO: Check this is actually true, particularly when repacking
965
     assumed size parameters.  */
966
 
967
  GFC_TYPE_ARRAY_DTYPE (type) = dtype;
968
  return dtype;
969
}
970
 
971
 
972
/* Build an array type for use without a descriptor.  Valid values of packed
973
   are 0=no, 1=partial, 2=full, 3=static.  */
974
 
975
tree
976
gfc_get_nodesc_array_type (tree etype, gfc_array_spec * as, int packed)
977
{
978
  tree range;
979
  tree type;
980
  tree tmp;
981
  int n;
982
  int known_stride;
983
  int known_offset;
984
  mpz_t offset;
985
  mpz_t stride;
986
  mpz_t delta;
987
  gfc_expr *expr;
988
 
989
  mpz_init_set_ui (offset, 0);
990
  mpz_init_set_ui (stride, 1);
991
  mpz_init (delta);
992
 
993
  /* We don't use build_array_type because this does not include include
994
     lang-specific information (i.e. the bounds of the array) when checking
995
     for duplicates.  */
996
  type = make_node (ARRAY_TYPE);
997
 
998
  GFC_ARRAY_TYPE_P (type) = 1;
999
  TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
1000
    ggc_alloc_cleared (sizeof (struct lang_type));
1001
 
1002
  known_stride = (packed != 0);
1003
  known_offset = 1;
1004
  for (n = 0; n < as->rank; n++)
1005
    {
1006
      /* Fill in the stride and bound components of the type.  */
1007
      if (known_stride)
1008
        tmp =  gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1009
      else
1010
        tmp = NULL_TREE;
1011
      GFC_TYPE_ARRAY_STRIDE (type, n) = tmp;
1012
 
1013
      expr = as->lower[n];
1014
      if (expr->expr_type == EXPR_CONSTANT)
1015
        {
1016
          tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1017
                                  gfc_index_integer_kind);
1018
        }
1019
      else
1020
        {
1021
          known_stride = 0;
1022
          tmp = NULL_TREE;
1023
        }
1024
      GFC_TYPE_ARRAY_LBOUND (type, n) = tmp;
1025
 
1026
      if (known_stride)
1027
        {
1028
          /* Calculate the offset.  */
1029
          mpz_mul (delta, stride, as->lower[n]->value.integer);
1030
          mpz_sub (offset, offset, delta);
1031
        }
1032
      else
1033
        known_offset = 0;
1034
 
1035
      expr = as->upper[n];
1036
      if (expr && expr->expr_type == EXPR_CONSTANT)
1037
        {
1038
          tmp = gfc_conv_mpz_to_tree (expr->value.integer,
1039
                                  gfc_index_integer_kind);
1040
        }
1041
      else
1042
        {
1043
          tmp = NULL_TREE;
1044
          known_stride = 0;
1045
        }
1046
      GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
1047
 
1048
      if (known_stride)
1049
        {
1050
          /* Calculate the stride.  */
1051
          mpz_sub (delta, as->upper[n]->value.integer,
1052
                   as->lower[n]->value.integer);
1053
          mpz_add_ui (delta, delta, 1);
1054
          mpz_mul (stride, stride, delta);
1055
        }
1056
 
1057
      /* Only the first stride is known for partial packed arrays.  */
1058
      if (packed < 2)
1059
        known_stride = 0;
1060
    }
1061
 
1062
  if (known_offset)
1063
    {
1064
      GFC_TYPE_ARRAY_OFFSET (type) =
1065
        gfc_conv_mpz_to_tree (offset, gfc_index_integer_kind);
1066
    }
1067
  else
1068
    GFC_TYPE_ARRAY_OFFSET (type) = NULL_TREE;
1069
 
1070
  if (known_stride)
1071
    {
1072
      GFC_TYPE_ARRAY_SIZE (type) =
1073
        gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1074
    }
1075
  else
1076
    GFC_TYPE_ARRAY_SIZE (type) = NULL_TREE;
1077
 
1078
  GFC_TYPE_ARRAY_RANK (type) = as->rank;
1079
  GFC_TYPE_ARRAY_DTYPE (type) = NULL_TREE;
1080
  range = build_range_type (gfc_array_index_type, gfc_index_zero_node,
1081
                            NULL_TREE);
1082
  /* TODO: use main type if it is unbounded.  */
1083
  GFC_TYPE_ARRAY_DATAPTR_TYPE (type) =
1084
    build_pointer_type (build_array_type (etype, range));
1085
 
1086
  if (known_stride)
1087
    {
1088
      mpz_sub_ui (stride, stride, 1);
1089
      range = gfc_conv_mpz_to_tree (stride, gfc_index_integer_kind);
1090
    }
1091
  else
1092
    range = NULL_TREE;
1093
 
1094
  range = build_range_type (gfc_array_index_type, gfc_index_zero_node, range);
1095
  TYPE_DOMAIN (type) = range;
1096
 
1097
  build_pointer_type (etype);
1098
  TREE_TYPE (type) = etype;
1099
 
1100
  layout_type (type);
1101
 
1102
  mpz_clear (offset);
1103
  mpz_clear (stride);
1104
  mpz_clear (delta);
1105
 
1106
  if (packed < 3 || !known_stride)
1107
    {
1108
      /* For dummy arrays and automatic (heap allocated) arrays we
1109
         want a pointer to the array.  */
1110
      type = build_pointer_type (type);
1111
      GFC_ARRAY_TYPE_P (type) = 1;
1112
      TYPE_LANG_SPECIFIC (type) = TYPE_LANG_SPECIFIC (TREE_TYPE (type));
1113
    }
1114
  return type;
1115
}
1116
 
1117
/* Return or create the base type for an array descriptor.  */
1118
 
1119
static tree
1120
gfc_get_array_descriptor_base (int dimen)
1121
{
1122
  tree fat_type, fieldlist, decl, arraytype;
1123
  char name[16 + GFC_RANK_DIGITS + 1];
1124
 
1125
  gcc_assert (dimen >= 1 && dimen <= GFC_MAX_DIMENSIONS);
1126
  if (gfc_array_descriptor_base[dimen - 1])
1127
    return gfc_array_descriptor_base[dimen - 1];
1128
 
1129
  /* Build the type node.  */
1130
  fat_type = make_node (RECORD_TYPE);
1131
 
1132
  sprintf (name, "array_descriptor" GFC_RANK_PRINTF_FORMAT, dimen);
1133
  TYPE_NAME (fat_type) = get_identifier (name);
1134
 
1135
  /* Add the data member as the first element of the descriptor.  */
1136
  decl = build_decl (FIELD_DECL, get_identifier ("data"), ptr_type_node);
1137
 
1138
  DECL_CONTEXT (decl) = fat_type;
1139
  fieldlist = decl;
1140
 
1141
  /* Add the base component.  */
1142
  decl = build_decl (FIELD_DECL, get_identifier ("offset"),
1143
                     gfc_array_index_type);
1144
  DECL_CONTEXT (decl) = fat_type;
1145
  fieldlist = chainon (fieldlist, decl);
1146
 
1147
  /* Add the dtype component.  */
1148
  decl = build_decl (FIELD_DECL, get_identifier ("dtype"),
1149
                     gfc_array_index_type);
1150
  DECL_CONTEXT (decl) = fat_type;
1151
  fieldlist = chainon (fieldlist, decl);
1152
 
1153
  /* Build the array type for the stride and bound components.  */
1154
  arraytype =
1155
    build_array_type (gfc_get_desc_dim_type (),
1156
                      build_range_type (gfc_array_index_type,
1157
                                        gfc_index_zero_node,
1158
                                        gfc_rank_cst[dimen - 1]));
1159
 
1160
  decl = build_decl (FIELD_DECL, get_identifier ("dim"), arraytype);
1161
  DECL_CONTEXT (decl) = fat_type;
1162
  fieldlist = chainon (fieldlist, decl);
1163
 
1164
  /* Finish off the type.  */
1165
  TYPE_FIELDS (fat_type) = fieldlist;
1166
 
1167
  gfc_finish_type (fat_type);
1168
 
1169
  gfc_array_descriptor_base[dimen - 1] = fat_type;
1170
  return fat_type;
1171
}
1172
 
1173
/* Build an array (descriptor) type with given bounds.  */
1174
 
1175
tree
1176
gfc_get_array_type_bounds (tree etype, int dimen, tree * lbound,
1177
                           tree * ubound, int packed)
1178
{
1179
  char name[8 + GFC_RANK_DIGITS + GFC_MAX_SYMBOL_LEN];
1180
  tree fat_type, base_type, arraytype, lower, upper, stride, tmp;
1181
  const char *typename;
1182
  int n;
1183
 
1184
  base_type = gfc_get_array_descriptor_base (dimen);
1185
  fat_type = build_variant_type_copy (base_type);
1186
 
1187
  tmp = TYPE_NAME (etype);
1188
  if (tmp && TREE_CODE (tmp) == TYPE_DECL)
1189
    tmp = DECL_NAME (tmp);
1190
  if (tmp)
1191
    typename = IDENTIFIER_POINTER (tmp);
1192
  else
1193
    typename = "unknown";
1194
  sprintf (name, "array" GFC_RANK_PRINTF_FORMAT "_%.*s", dimen,
1195
           GFC_MAX_SYMBOL_LEN, typename);
1196
  TYPE_NAME (fat_type) = get_identifier (name);
1197
 
1198
  GFC_DESCRIPTOR_TYPE_P (fat_type) = 1;
1199
  TYPE_LANG_SPECIFIC (fat_type) = (struct lang_type *)
1200
    ggc_alloc_cleared (sizeof (struct lang_type));
1201
 
1202
  GFC_TYPE_ARRAY_RANK (fat_type) = dimen;
1203
  GFC_TYPE_ARRAY_DTYPE (fat_type) = NULL_TREE;
1204
 
1205
  /* Build an array descriptor record type.  */
1206
  if (packed != 0)
1207
    stride = gfc_index_one_node;
1208
  else
1209
    stride = NULL_TREE;
1210
  for (n = 0; n < dimen; n++)
1211
    {
1212
      GFC_TYPE_ARRAY_STRIDE (fat_type, n) = stride;
1213
 
1214
      if (lbound)
1215
        lower = lbound[n];
1216
      else
1217
        lower = NULL_TREE;
1218
 
1219
      if (lower != NULL_TREE)
1220
        {
1221
          if (INTEGER_CST_P (lower))
1222
            GFC_TYPE_ARRAY_LBOUND (fat_type, n) = lower;
1223
          else
1224
            lower = NULL_TREE;
1225
        }
1226
 
1227
      upper = ubound[n];
1228
      if (upper != NULL_TREE)
1229
        {
1230
          if (INTEGER_CST_P (upper))
1231
            GFC_TYPE_ARRAY_UBOUND (fat_type, n) = upper;
1232
          else
1233
            upper = NULL_TREE;
1234
        }
1235
 
1236
      if (upper != NULL_TREE && lower != NULL_TREE && stride != NULL_TREE)
1237
        {
1238
          tmp = fold_build2 (MINUS_EXPR, gfc_array_index_type, upper, lower);
1239
          tmp = fold_build2 (PLUS_EXPR, gfc_array_index_type, tmp,
1240
                             gfc_index_one_node);
1241
          stride =
1242
            fold_build2 (MULT_EXPR, gfc_array_index_type, tmp, stride);
1243
          /* Check the folding worked.  */
1244
          gcc_assert (INTEGER_CST_P (stride));
1245
        }
1246
      else
1247
        stride = NULL_TREE;
1248
    }
1249
  GFC_TYPE_ARRAY_SIZE (fat_type) = stride;
1250
 
1251
  /* TODO: known offsets for descriptors.  */
1252
  GFC_TYPE_ARRAY_OFFSET (fat_type) = NULL_TREE;
1253
 
1254
  /* We define data as an unknown size array. Much better than doing
1255
     pointer arithmetic.  */
1256
  arraytype =
1257
    build_array_type (etype, gfc_array_range_type);
1258
  arraytype = build_pointer_type (arraytype);
1259
  GFC_TYPE_ARRAY_DATAPTR_TYPE (fat_type) = arraytype;
1260
 
1261
  return fat_type;
1262
}
1263
 
1264
/* Build a pointer type. This function is called from gfc_sym_type().  */
1265
 
1266
static tree
1267
gfc_build_pointer_type (gfc_symbol * sym, tree type)
1268
{
1269
  /* Array pointer types aren't actually pointers.  */
1270
  if (sym->attr.dimension)
1271
    return type;
1272
  else
1273
    return build_pointer_type (type);
1274
}
1275
 
1276
/* Return the type for a symbol.  Special handling is required for character
1277
   types to get the correct level of indirection.
1278
   For functions return the return type.
1279
   For subroutines return void_type_node.
1280
   Calling this multiple times for the same symbol should be avoided,
1281
   especially for character and array types.  */
1282
 
1283
tree
1284
gfc_sym_type (gfc_symbol * sym)
1285
{
1286
  tree type;
1287
  int byref;
1288
 
1289
  if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
1290
    return void_type_node;
1291
 
1292
  if (sym->backend_decl)
1293
    {
1294
      if (sym->attr.function)
1295
        return TREE_TYPE (TREE_TYPE (sym->backend_decl));
1296
      else
1297
        return TREE_TYPE (sym->backend_decl);
1298
    }
1299
 
1300
  type = gfc_typenode_for_spec (&sym->ts);
1301
  if (gfc_option.flag_f2c
1302
      && sym->attr.function
1303
      && sym->ts.type == BT_REAL
1304
      && sym->ts.kind == gfc_default_real_kind
1305
      && !sym->attr.always_explicit)
1306
    {
1307
      /* Special case: f2c calling conventions require that (scalar)
1308
         default REAL functions return the C type double instead.  */
1309
      sym->ts.kind = gfc_default_double_kind;
1310
      type = gfc_typenode_for_spec (&sym->ts);
1311
      sym->ts.kind = gfc_default_real_kind;
1312
    }
1313
 
1314
  if (sym->attr.dummy && !sym->attr.function)
1315
    byref = 1;
1316
  else
1317
    byref = 0;
1318
 
1319
  if (sym->attr.dimension)
1320
    {
1321
      if (gfc_is_nodesc_array (sym))
1322
        {
1323
          /* If this is a character argument of unknown length, just use the
1324
             base type.  */
1325
          if (sym->ts.type != BT_CHARACTER
1326
              || !(sym->attr.dummy || sym->attr.function)
1327
              || sym->ts.cl->backend_decl)
1328
            {
1329
              type = gfc_get_nodesc_array_type (type, sym->as,
1330
                                                byref ? 2 : 3);
1331
              byref = 0;
1332
            }
1333
        }
1334
      else
1335
        type = gfc_build_array_type (type, sym->as);
1336
    }
1337
  else
1338
    {
1339
      if (sym->attr.allocatable || sym->attr.pointer)
1340
        type = gfc_build_pointer_type (sym, type);
1341
    }
1342
 
1343
  /* We currently pass all parameters by reference.
1344
     See f95_get_function_decl.  For dummy function parameters return the
1345
     function type.  */
1346
  if (byref)
1347
    {
1348
      /* We must use pointer types for potentially absent variables.  The
1349
         optimizers assume a reference type argument is never NULL.  */
1350
      if (sym->attr.optional || sym->ns->proc_name->attr.entry_master)
1351
        type = build_pointer_type (type);
1352
      else
1353
        type = build_reference_type (type);
1354
    }
1355
 
1356
  return (type);
1357
}
1358
 
1359
/* Layout and output debug info for a record type.  */
1360
 
1361
void
1362
gfc_finish_type (tree type)
1363
{
1364
  tree decl;
1365
 
1366
  decl = build_decl (TYPE_DECL, NULL_TREE, type);
1367
  TYPE_STUB_DECL (type) = decl;
1368
  layout_type (type);
1369
  rest_of_type_compilation (type, 1);
1370
  rest_of_decl_compilation (decl, 1, 0);
1371
}
1372
 
1373
/* Add a field of given NAME and TYPE to the context of a UNION_TYPE
1374
   or RECORD_TYPE pointed to by STYPE.  The new field is chained
1375
   to the fieldlist pointed to by FIELDLIST.
1376
 
1377
   Returns a pointer to the new field.  */
1378
 
1379
tree
1380
gfc_add_field_to_struct (tree *fieldlist, tree context,
1381
                         tree name, tree type)
1382
{
1383
  tree decl;
1384
 
1385
  decl = build_decl (FIELD_DECL, name, type);
1386
 
1387
  DECL_CONTEXT (decl) = context;
1388
  DECL_INITIAL (decl) = 0;
1389
  DECL_ALIGN (decl) = 0;
1390
  DECL_USER_ALIGN (decl) = 0;
1391
  TREE_CHAIN (decl) = NULL_TREE;
1392
  *fieldlist = chainon (*fieldlist, decl);
1393
 
1394
  return decl;
1395
}
1396
 
1397
 
1398
/* Copy the backend_decl and component backend_decls if
1399
   the two derived type symbols are "equal", as described
1400
   in 4.4.2 and resolved by gfc_compare_derived_types.  */
1401
 
1402
static int
1403
copy_dt_decls_ifequal (gfc_symbol *from, gfc_symbol *to)
1404
{
1405
  gfc_component *to_cm;
1406
  gfc_component *from_cm;
1407
 
1408
  if (from->backend_decl == NULL
1409
        || !gfc_compare_derived_types (from, to))
1410
    return 0;
1411
 
1412
  to->backend_decl = from->backend_decl;
1413
 
1414
  to_cm = to->components;
1415
  from_cm = from->components;
1416
 
1417
  /* Copy the component declarations.  If a component is itself
1418
     a derived type, we need a copy of its component declarations.
1419
     This is done by recursing into gfc_get_derived_type and
1420
     ensures that the component's component declarations have
1421
     been built.  If it is a character, we need the character
1422
     length, as well.  */
1423
  for (; to_cm; to_cm = to_cm->next, from_cm = from_cm->next)
1424
    {
1425
      to_cm->backend_decl = from_cm->backend_decl;
1426
      if (from_cm->ts.type == BT_DERIVED)
1427
        gfc_get_derived_type (to_cm->ts.derived);
1428
 
1429
      else if (from_cm->ts.type == BT_CHARACTER)
1430
        to_cm->ts.cl->backend_decl = from_cm->ts.cl->backend_decl;
1431
    }
1432
 
1433
  return 1;
1434
}
1435
 
1436
 
1437
/* Build a tree node for a derived type.  If there are equal
1438
   derived types, with different local names, these are built
1439
   at the same time.  If an equal derived type has been built
1440
   in a parent namespace, this is used.  */
1441
 
1442
static tree
1443
gfc_get_derived_type (gfc_symbol * derived)
1444
{
1445
  tree typenode, field, field_type, fieldlist;
1446
  gfc_component *c;
1447
  gfc_dt_list *dt;
1448
  gfc_namespace * ns;
1449
 
1450
  gcc_assert (derived && derived->attr.flavor == FL_DERIVED);
1451
 
1452
  /* derived->backend_decl != 0 means we saw it before, but its
1453
     components' backend_decl may have not been built.  */
1454
  if (derived->backend_decl)
1455
    {
1456
      /* Its components' backend_decl have been built.  */
1457
      if (TYPE_FIELDS (derived->backend_decl))
1458
        return derived->backend_decl;
1459
      else
1460
        typenode = derived->backend_decl;
1461
    }
1462
  else
1463
    {
1464
      /* In a module, if an equal derived type is already available in the
1465
         specification block, use its backend declaration and those of its
1466
         components, rather than building anew so that potential dummy and
1467
         actual arguments use the same TREE_TYPE.  Non-module structures,
1468
         need to be built, if found, because the order of visits to the
1469
         namespaces is different.  */
1470
 
1471
      for (ns = derived->ns->parent; ns; ns = ns->parent)
1472
        {
1473
          for (dt = ns->derived_types; dt; dt = dt->next)
1474
            {
1475
              if (derived->module == NULL
1476
                    && dt->derived->backend_decl == NULL
1477
                    && gfc_compare_derived_types (dt->derived, derived))
1478
                gfc_get_derived_type (dt->derived);
1479
 
1480
              if (copy_dt_decls_ifequal (dt->derived, derived))
1481
                break;
1482
            }
1483
          if (derived->backend_decl)
1484
            goto other_equal_dts;
1485
        }
1486
 
1487
      /* We see this derived type first time, so build the type node.  */
1488
      typenode = make_node (RECORD_TYPE);
1489
      TYPE_NAME (typenode) = get_identifier (derived->name);
1490
      TYPE_PACKED (typenode) = gfc_option.flag_pack_derived;
1491
      derived->backend_decl = typenode;
1492
    }
1493
 
1494
  /* Go through the derived type components, building them as
1495
     necessary. The reason for doing this now is that it is
1496
     possible to recurse back to this derived type through a
1497
     pointer component (PR24092). If this happens, the fields
1498
     will be built and so we can return the type.  */
1499
  for (c = derived->components; c; c = c->next)
1500
    {
1501
      if (c->ts.type != BT_DERIVED)
1502
        continue;
1503
 
1504
      if (!c->pointer || c->ts.derived->backend_decl == NULL)
1505
        c->ts.derived->backend_decl = gfc_get_derived_type (c->ts.derived);
1506
    }
1507
 
1508
  if (TYPE_FIELDS (derived->backend_decl))
1509
    return derived->backend_decl;
1510
 
1511
  /* Build the type member list. Install the newly created RECORD_TYPE
1512
     node as DECL_CONTEXT of each FIELD_DECL.  */
1513
  fieldlist = NULL_TREE;
1514
  for (c = derived->components; c; c = c->next)
1515
    {
1516
      if (c->ts.type == BT_DERIVED)
1517
        field_type = c->ts.derived->backend_decl;
1518
      else
1519
        {
1520
          if (c->ts.type == BT_CHARACTER)
1521
            {
1522
              /* Evaluate the string length.  */
1523
              gfc_conv_const_charlen (c->ts.cl);
1524
              gcc_assert (c->ts.cl->backend_decl);
1525
            }
1526
 
1527
          field_type = gfc_typenode_for_spec (&c->ts);
1528
        }
1529
 
1530
      /* This returns an array descriptor type.  Initialization may be
1531
         required.  */
1532
      if (c->dimension)
1533
        {
1534
          if (c->pointer)
1535
            {
1536
              /* Pointers to arrays aren't actually pointer types.  The
1537
                 descriptors are separate, but the data is common.  */
1538
              field_type = gfc_build_array_type (field_type, c->as);
1539
            }
1540
          else
1541
            field_type = gfc_get_nodesc_array_type (field_type, c->as, 3);
1542
        }
1543
      else if (c->pointer)
1544
        field_type = build_pointer_type (field_type);
1545
 
1546
      field = gfc_add_field_to_struct (&fieldlist, typenode,
1547
                                       get_identifier (c->name),
1548
                                       field_type);
1549
 
1550
      DECL_PACKED (field) |= TYPE_PACKED (typenode);
1551
 
1552
      gcc_assert (field);
1553
      if (!c->backend_decl)
1554
        c->backend_decl = field;
1555
    }
1556
 
1557
  /* Now we have the final fieldlist.  Record it, then lay out the
1558
     derived type, including the fields.  */
1559
  TYPE_FIELDS (typenode) = fieldlist;
1560
 
1561
  gfc_finish_type (typenode);
1562
 
1563
  derived->backend_decl = typenode;
1564
 
1565
other_equal_dts:
1566
  /* Add this backend_decl to all the other, equal derived types and
1567
     their components in this namespace.  */
1568
  for (dt = derived->ns->derived_types; dt; dt = dt->next)
1569
    copy_dt_decls_ifequal (derived, dt->derived);
1570
 
1571
  return derived->backend_decl;
1572
}
1573
 
1574
 
1575
int
1576
gfc_return_by_reference (gfc_symbol * sym)
1577
{
1578
  if (!sym->attr.function)
1579
    return 0;
1580
 
1581
  if (sym->attr.dimension)
1582
    return 1;
1583
 
1584
  if (sym->ts.type == BT_CHARACTER)
1585
    return 1;
1586
 
1587
  /* Possibly return complex numbers by reference for g77 compatibility.
1588
     We don't do this for calls to intrinsics (as the library uses the
1589
     -fno-f2c calling convention), nor for calls to functions which always
1590
     require an explicit interface, as no compatibility problems can
1591
     arise there.  */
1592
  if (gfc_option.flag_f2c
1593
      && sym->ts.type == BT_COMPLEX
1594
      && !sym->attr.intrinsic && !sym->attr.always_explicit)
1595
    return 1;
1596
 
1597
  return 0;
1598
}
1599
 
1600
static tree
1601
gfc_get_mixed_entry_union (gfc_namespace *ns)
1602
{
1603
  tree type;
1604
  tree decl;
1605
  tree fieldlist;
1606
  char name[GFC_MAX_SYMBOL_LEN + 1];
1607
  gfc_entry_list *el, *el2;
1608
 
1609
  gcc_assert (ns->proc_name->attr.mixed_entry_master);
1610
  gcc_assert (memcmp (ns->proc_name->name, "master.", 7) == 0);
1611
 
1612
  snprintf (name, GFC_MAX_SYMBOL_LEN, "munion.%s", ns->proc_name->name + 7);
1613
 
1614
  /* Build the type node.  */
1615
  type = make_node (UNION_TYPE);
1616
 
1617
  TYPE_NAME (type) = get_identifier (name);
1618
  fieldlist = NULL;
1619
 
1620
  for (el = ns->entries; el; el = el->next)
1621
    {
1622
      /* Search for duplicates.  */
1623
      for (el2 = ns->entries; el2 != el; el2 = el2->next)
1624
        if (el2->sym->result == el->sym->result)
1625
          break;
1626
 
1627
      if (el == el2)
1628
        {
1629
          decl = build_decl (FIELD_DECL,
1630
                             get_identifier (el->sym->result->name),
1631
                             gfc_sym_type (el->sym->result));
1632
          DECL_CONTEXT (decl) = type;
1633
          fieldlist = chainon (fieldlist, decl);
1634
        }
1635
    }
1636
 
1637
  /* Finish off the type.  */
1638
  TYPE_FIELDS (type) = fieldlist;
1639
 
1640
  gfc_finish_type (type);
1641
  return type;
1642
}
1643
 
1644
tree
1645
gfc_get_function_type (gfc_symbol * sym)
1646
{
1647
  tree type;
1648
  tree typelist;
1649
  gfc_formal_arglist *f;
1650
  gfc_symbol *arg;
1651
  int nstr;
1652
  int alternate_return;
1653
 
1654
  /* Make sure this symbol is a function or a subroutine.  */
1655
  gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1656
 
1657
  if (sym->backend_decl)
1658
    return TREE_TYPE (sym->backend_decl);
1659
 
1660
  nstr = 0;
1661
  alternate_return = 0;
1662
  typelist = NULL_TREE;
1663
 
1664
  if (sym->attr.entry_master)
1665
    {
1666
      /* Additional parameter for selecting an entry point.  */
1667
      typelist = gfc_chainon_list (typelist, gfc_array_index_type);
1668
    }
1669
 
1670
  /* Some functions we use an extra parameter for the return value.  */
1671
  if (gfc_return_by_reference (sym))
1672
    {
1673
      if (sym->result)
1674
        arg = sym->result;
1675
      else
1676
        arg = sym;
1677
 
1678
      if (arg->ts.type == BT_CHARACTER)
1679
        gfc_conv_const_charlen (arg->ts.cl);
1680
 
1681
      type = gfc_sym_type (arg);
1682
      if (arg->ts.type == BT_COMPLEX
1683
          || arg->attr.dimension
1684
          || arg->ts.type == BT_CHARACTER)
1685
        type = build_reference_type (type);
1686
 
1687
      typelist = gfc_chainon_list (typelist, type);
1688
      if (arg->ts.type == BT_CHARACTER)
1689
        typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1690
    }
1691
 
1692
  /* Build the argument types for the function.  */
1693
  for (f = sym->formal; f; f = f->next)
1694
    {
1695
      arg = f->sym;
1696
      if (arg)
1697
        {
1698
          /* Evaluate constant character lengths here so that they can be
1699
             included in the type.  */
1700
          if (arg->ts.type == BT_CHARACTER)
1701
            gfc_conv_const_charlen (arg->ts.cl);
1702
 
1703
          if (arg->attr.flavor == FL_PROCEDURE)
1704
            {
1705
              type = gfc_get_function_type (arg);
1706
              type = build_pointer_type (type);
1707
            }
1708
          else
1709
            type = gfc_sym_type (arg);
1710
 
1711
          /* Parameter Passing Convention
1712
 
1713
             We currently pass all parameters by reference.
1714
             Parameters with INTENT(IN) could be passed by value.
1715
             The problem arises if a function is called via an implicit
1716
             prototype. In this situation the INTENT is not known.
1717
             For this reason all parameters to global functions must be
1718
             passed by reference.  Passing by value would potentially
1719
             generate bad code.  Worse there would be no way of telling that
1720
             this code was bad, except that it would give incorrect results.
1721
 
1722
             Contained procedures could pass by value as these are never
1723
             used without an explicit interface, and connot be passed as
1724
             actual parameters for a dummy procedure.  */
1725
          if (arg->ts.type == BT_CHARACTER)
1726
            nstr++;
1727
          typelist = gfc_chainon_list (typelist, type);
1728
        }
1729
      else
1730
        {
1731
          if (sym->attr.subroutine)
1732
            alternate_return = 1;
1733
        }
1734
    }
1735
 
1736
  /* Add hidden string length parameters.  */
1737
  while (nstr--)
1738
    typelist = gfc_chainon_list (typelist, gfc_charlen_type_node);
1739
 
1740
  typelist = gfc_chainon_list (typelist, void_type_node);
1741
 
1742
  if (alternate_return)
1743
    type = integer_type_node;
1744
  else if (!sym->attr.function || gfc_return_by_reference (sym))
1745
    type = void_type_node;
1746
  else if (sym->attr.mixed_entry_master)
1747
    type = gfc_get_mixed_entry_union (sym->ns);
1748
  else
1749
    type = gfc_sym_type (sym);
1750
 
1751
  type = build_function_type (type, typelist);
1752
 
1753
  return type;
1754
}
1755
 
1756
/* Language hooks for middle-end access to type nodes.  */
1757
 
1758
/* Return an integer type with BITS bits of precision,
1759
   that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
1760
 
1761
tree
1762
gfc_type_for_size (unsigned bits, int unsignedp)
1763
{
1764
  if (!unsignedp)
1765
    {
1766
      int i;
1767
      for (i = 0; i <= MAX_INT_KINDS; ++i)
1768
        {
1769
          tree type = gfc_integer_types[i];
1770
          if (type && bits == TYPE_PRECISION (type))
1771
            return type;
1772
        }
1773
    }
1774
  else
1775
    {
1776
      if (bits == TYPE_PRECISION (unsigned_intQI_type_node))
1777
        return unsigned_intQI_type_node;
1778
      if (bits == TYPE_PRECISION (unsigned_intHI_type_node))
1779
        return unsigned_intHI_type_node;
1780
      if (bits == TYPE_PRECISION (unsigned_intSI_type_node))
1781
        return unsigned_intSI_type_node;
1782
      if (bits == TYPE_PRECISION (unsigned_intDI_type_node))
1783
        return unsigned_intDI_type_node;
1784
      if (bits == TYPE_PRECISION (unsigned_intTI_type_node))
1785
        return unsigned_intTI_type_node;
1786
    }
1787
 
1788
  return NULL_TREE;
1789
}
1790
 
1791
/* Return a data type that has machine mode MODE.  If the mode is an
1792
   integer, then UNSIGNEDP selects between signed and unsigned types.  */
1793
 
1794
tree
1795
gfc_type_for_mode (enum machine_mode mode, int unsignedp)
1796
{
1797
  int i;
1798
  tree *base;
1799
 
1800
  if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1801
    base = gfc_real_types;
1802
  else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
1803
    base = gfc_complex_types;
1804
  else if (SCALAR_INT_MODE_P (mode))
1805
    return gfc_type_for_size (GET_MODE_PRECISION (mode), unsignedp);
1806
  else if (VECTOR_MODE_P (mode))
1807
    {
1808
      enum machine_mode inner_mode = GET_MODE_INNER (mode);
1809
      tree inner_type = gfc_type_for_mode (inner_mode, unsignedp);
1810
      if (inner_type != NULL_TREE)
1811
        return build_vector_type_for_mode (inner_type, mode);
1812
      return NULL_TREE;
1813
    }
1814
  else
1815
    return NULL_TREE;
1816
 
1817
  for (i = 0; i <= MAX_REAL_KINDS; ++i)
1818
    {
1819
      tree type = base[i];
1820
      if (type && mode == TYPE_MODE (type))
1821
        return type;
1822
    }
1823
 
1824
  return NULL_TREE;
1825
}
1826
 
1827
/* Return a type the same as TYPE except unsigned or
1828
   signed according to UNSIGNEDP.  */
1829
 
1830
tree
1831
gfc_signed_or_unsigned_type (int unsignedp, tree type)
1832
{
1833
  if (TREE_CODE (type) != INTEGER_TYPE || TYPE_UNSIGNED (type) == unsignedp)
1834
    return type;
1835
  else
1836
    return gfc_type_for_size (TYPE_PRECISION (type), unsignedp);
1837
}
1838
 
1839
/* Return an unsigned type the same as TYPE in other respects.  */
1840
 
1841
tree
1842
gfc_unsigned_type (tree type)
1843
{
1844
  return gfc_signed_or_unsigned_type (1, type);
1845
}
1846
 
1847
/* Return a signed type the same as TYPE in other respects.  */
1848
 
1849
tree
1850
gfc_signed_type (tree type)
1851
{
1852
  return gfc_signed_or_unsigned_type (0, type);
1853
}
1854
 
1855
#include "gt-fortran-trans-types.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.