OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [fortran/] [expr.c] - Blame information for rev 410

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

Line No. Rev Author Line
1 285 jeremybenn
/* Routines for manipulation of expression nodes.
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3
   2009, 2010
4
   Free Software Foundation, Inc.
5
   Contributed by Andy Vaught
6
 
7
This file is part of GCC.
8
 
9
GCC is free software; you can redistribute it and/or modify it under
10
the terms of the GNU General Public License as published by the Free
11
Software Foundation; either version 3, or (at your option) any later
12
version.
13
 
14
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15
WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17
for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with GCC; see the file COPYING3.  If not see
21
<http://www.gnu.org/licenses/>.  */
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "gfortran.h"
26
#include "arith.h"
27
#include "match.h"
28
#include "target-memory.h" /* for gfc_convert_boz */
29
 
30
/* Get a new expr node.  */
31
 
32
gfc_expr *
33
gfc_get_expr (void)
34
{
35
  gfc_expr *e;
36
 
37
  e = XCNEW (gfc_expr);
38
  gfc_clear_ts (&e->ts);
39
  e->shape = NULL;
40
  e->ref = NULL;
41
  e->symtree = NULL;
42
  e->con_by_offset = NULL;
43
  return e;
44
}
45
 
46
 
47
/* Free an argument list and everything below it.  */
48
 
49
void
50
gfc_free_actual_arglist (gfc_actual_arglist *a1)
51
{
52
  gfc_actual_arglist *a2;
53
 
54
  while (a1)
55
    {
56
      a2 = a1->next;
57
      gfc_free_expr (a1->expr);
58
      gfc_free (a1);
59
      a1 = a2;
60
    }
61
}
62
 
63
 
64
/* Copy an arglist structure and all of the arguments.  */
65
 
66
gfc_actual_arglist *
67
gfc_copy_actual_arglist (gfc_actual_arglist *p)
68
{
69
  gfc_actual_arglist *head, *tail, *new_arg;
70
 
71
  head = tail = NULL;
72
 
73
  for (; p; p = p->next)
74
    {
75
      new_arg = gfc_get_actual_arglist ();
76
      *new_arg = *p;
77
 
78
      new_arg->expr = gfc_copy_expr (p->expr);
79
      new_arg->next = NULL;
80
 
81
      if (head == NULL)
82
        head = new_arg;
83
      else
84
        tail->next = new_arg;
85
 
86
      tail = new_arg;
87
    }
88
 
89
  return head;
90
}
91
 
92
 
93
/* Free a list of reference structures.  */
94
 
95
void
96
gfc_free_ref_list (gfc_ref *p)
97
{
98
  gfc_ref *q;
99
  int i;
100
 
101
  for (; p; p = q)
102
    {
103
      q = p->next;
104
 
105
      switch (p->type)
106
        {
107
        case REF_ARRAY:
108
          for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
109
            {
110
              gfc_free_expr (p->u.ar.start[i]);
111
              gfc_free_expr (p->u.ar.end[i]);
112
              gfc_free_expr (p->u.ar.stride[i]);
113
            }
114
 
115
          break;
116
 
117
        case REF_SUBSTRING:
118
          gfc_free_expr (p->u.ss.start);
119
          gfc_free_expr (p->u.ss.end);
120
          break;
121
 
122
        case REF_COMPONENT:
123
          break;
124
        }
125
 
126
      gfc_free (p);
127
    }
128
}
129
 
130
 
131
/* Workhorse function for gfc_free_expr() that frees everything
132
   beneath an expression node, but not the node itself.  This is
133
   useful when we want to simplify a node and replace it with
134
   something else or the expression node belongs to another structure.  */
135
 
136
static void
137
free_expr0 (gfc_expr *e)
138
{
139
  int n;
140
 
141
  switch (e->expr_type)
142
    {
143
    case EXPR_CONSTANT:
144
      /* Free any parts of the value that need freeing.  */
145
      switch (e->ts.type)
146
        {
147
        case BT_INTEGER:
148
          mpz_clear (e->value.integer);
149
          break;
150
 
151
        case BT_REAL:
152
          mpfr_clear (e->value.real);
153
          break;
154
 
155
        case BT_CHARACTER:
156
          gfc_free (e->value.character.string);
157
          break;
158
 
159
        case BT_COMPLEX:
160
          mpc_clear (e->value.complex);
161
          break;
162
 
163
        default:
164
          break;
165
        }
166
 
167
      /* Free the representation.  */
168
      if (e->representation.string)
169
        gfc_free (e->representation.string);
170
 
171
      break;
172
 
173
    case EXPR_OP:
174
      if (e->value.op.op1 != NULL)
175
        gfc_free_expr (e->value.op.op1);
176
      if (e->value.op.op2 != NULL)
177
        gfc_free_expr (e->value.op.op2);
178
      break;
179
 
180
    case EXPR_FUNCTION:
181
      gfc_free_actual_arglist (e->value.function.actual);
182
      break;
183
 
184
    case EXPR_COMPCALL:
185
    case EXPR_PPC:
186
      gfc_free_actual_arglist (e->value.compcall.actual);
187
      break;
188
 
189
    case EXPR_VARIABLE:
190
      break;
191
 
192
    case EXPR_ARRAY:
193
    case EXPR_STRUCTURE:
194
      gfc_free_constructor (e->value.constructor);
195
      break;
196
 
197
    case EXPR_SUBSTRING:
198
      gfc_free (e->value.character.string);
199
      break;
200
 
201
    case EXPR_NULL:
202
      break;
203
 
204
    default:
205
      gfc_internal_error ("free_expr0(): Bad expr type");
206
    }
207
 
208
  /* Free a shape array.  */
209
  if (e->shape != NULL)
210
    {
211
      for (n = 0; n < e->rank; n++)
212
        mpz_clear (e->shape[n]);
213
 
214
      gfc_free (e->shape);
215
    }
216
 
217
  gfc_free_ref_list (e->ref);
218
 
219
  memset (e, '\0', sizeof (gfc_expr));
220
}
221
 
222
 
223
/* Free an expression node and everything beneath it.  */
224
 
225
void
226
gfc_free_expr (gfc_expr *e)
227
{
228
  if (e == NULL)
229
    return;
230
  if (e->con_by_offset)
231
    splay_tree_delete (e->con_by_offset);
232
  free_expr0 (e);
233
  gfc_free (e);
234
}
235
 
236
 
237
/* Graft the *src expression onto the *dest subexpression.  */
238
 
239
void
240
gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
241
{
242
  free_expr0 (dest);
243
  *dest = *src;
244
  gfc_free (src);
245
}
246
 
247
 
248
/* Try to extract an integer constant from the passed expression node.
249
   Returns an error message or NULL if the result is set.  It is
250
   tempting to generate an error and return SUCCESS or FAILURE, but
251
   failure is OK for some callers.  */
252
 
253
const char *
254
gfc_extract_int (gfc_expr *expr, int *result)
255
{
256
  if (expr->expr_type != EXPR_CONSTANT)
257
    return _("Constant expression required at %C");
258
 
259
  if (expr->ts.type != BT_INTEGER)
260
    return _("Integer expression required at %C");
261
 
262
  if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
263
      || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
264
    {
265
      return _("Integer value too large in expression at %C");
266
    }
267
 
268
  *result = (int) mpz_get_si (expr->value.integer);
269
 
270
  return NULL;
271
}
272
 
273
 
274
/* Recursively copy a list of reference structures.  */
275
 
276
gfc_ref *
277
gfc_copy_ref (gfc_ref *src)
278
{
279
  gfc_array_ref *ar;
280
  gfc_ref *dest;
281
 
282
  if (src == NULL)
283
    return NULL;
284
 
285
  dest = gfc_get_ref ();
286
  dest->type = src->type;
287
 
288
  switch (src->type)
289
    {
290
    case REF_ARRAY:
291
      ar = gfc_copy_array_ref (&src->u.ar);
292
      dest->u.ar = *ar;
293
      gfc_free (ar);
294
      break;
295
 
296
    case REF_COMPONENT:
297
      dest->u.c = src->u.c;
298
      break;
299
 
300
    case REF_SUBSTRING:
301
      dest->u.ss = src->u.ss;
302
      dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
303
      dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
304
      break;
305
    }
306
 
307
  dest->next = gfc_copy_ref (src->next);
308
 
309
  return dest;
310
}
311
 
312
 
313
/* Detect whether an expression has any vector index array references.  */
314
 
315
int
316
gfc_has_vector_index (gfc_expr *e)
317
{
318
  gfc_ref *ref;
319
  int i;
320
  for (ref = e->ref; ref; ref = ref->next)
321
    if (ref->type == REF_ARRAY)
322
      for (i = 0; i < ref->u.ar.dimen; i++)
323
        if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
324
          return 1;
325
  return 0;
326
}
327
 
328
 
329
/* Insert a reference to the component of the given name.
330
   Only to be used with CLASS containers.  */
331
 
332
void
333
gfc_add_component_ref (gfc_expr *e, const char *name)
334
{
335
  gfc_ref **tail = &(e->ref);
336
  gfc_ref *next = NULL;
337
  gfc_symbol *derived = e->symtree->n.sym->ts.u.derived;
338
  while (*tail != NULL)
339
    {
340
      if ((*tail)->type == REF_COMPONENT)
341
        derived = (*tail)->u.c.component->ts.u.derived;
342
      if ((*tail)->type == REF_ARRAY && (*tail)->next == NULL)
343
        break;
344
      tail = &((*tail)->next);
345
    }
346
  if (*tail != NULL && strcmp (name, "$data") == 0)
347
    next = *tail;
348
  (*tail) = gfc_get_ref();
349
  (*tail)->next = next;
350
  (*tail)->type = REF_COMPONENT;
351
  (*tail)->u.c.sym = derived;
352
  (*tail)->u.c.component = gfc_find_component (derived, name, true, true);
353
  gcc_assert((*tail)->u.c.component);
354
  if (!next)
355
    e->ts = (*tail)->u.c.component->ts;
356
}
357
 
358
 
359
/* Copy a shape array.  */
360
 
361
mpz_t *
362
gfc_copy_shape (mpz_t *shape, int rank)
363
{
364
  mpz_t *new_shape;
365
  int n;
366
 
367
  if (shape == NULL)
368
    return NULL;
369
 
370
  new_shape = gfc_get_shape (rank);
371
 
372
  for (n = 0; n < rank; n++)
373
    mpz_init_set (new_shape[n], shape[n]);
374
 
375
  return new_shape;
376
}
377
 
378
 
379
/* Copy a shape array excluding dimension N, where N is an integer
380
   constant expression.  Dimensions are numbered in fortran style --
381
   starting with ONE.
382
 
383
   So, if the original shape array contains R elements
384
      { s1 ... sN-1  sN  sN+1 ... sR-1 sR}
385
   the result contains R-1 elements:
386
      { s1 ... sN-1  sN+1    ...  sR-1}
387
 
388
   If anything goes wrong -- N is not a constant, its value is out
389
   of range -- or anything else, just returns NULL.  */
390
 
391
mpz_t *
392
gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
393
{
394
  mpz_t *new_shape, *s;
395
  int i, n;
396
 
397
  if (shape == NULL
398
      || rank <= 1
399
      || dim == NULL
400
      || dim->expr_type != EXPR_CONSTANT
401
      || dim->ts.type != BT_INTEGER)
402
    return NULL;
403
 
404
  n = mpz_get_si (dim->value.integer);
405
  n--; /* Convert to zero based index.  */
406
  if (n < 0 || n >= rank)
407
    return NULL;
408
 
409
  s = new_shape = gfc_get_shape (rank - 1);
410
 
411
  for (i = 0; i < rank; i++)
412
    {
413
      if (i == n)
414
        continue;
415
      mpz_init_set (*s, shape[i]);
416
      s++;
417
    }
418
 
419
  return new_shape;
420
}
421
 
422
 
423
/* Given an expression pointer, return a copy of the expression.  This
424
   subroutine is recursive.  */
425
 
426
gfc_expr *
427
gfc_copy_expr (gfc_expr *p)
428
{
429
  gfc_expr *q;
430
  gfc_char_t *s;
431
  char *c;
432
 
433
  if (p == NULL)
434
    return NULL;
435
 
436
  q = gfc_get_expr ();
437
  *q = *p;
438
 
439
  switch (q->expr_type)
440
    {
441
    case EXPR_SUBSTRING:
442
      s = gfc_get_wide_string (p->value.character.length + 1);
443
      q->value.character.string = s;
444
      memcpy (s, p->value.character.string,
445
              (p->value.character.length + 1) * sizeof (gfc_char_t));
446
      break;
447
 
448
    case EXPR_CONSTANT:
449
      /* Copy target representation, if it exists.  */
450
      if (p->representation.string)
451
        {
452
          c = XCNEWVEC (char, p->representation.length + 1);
453
          q->representation.string = c;
454
          memcpy (c, p->representation.string, (p->representation.length + 1));
455
        }
456
 
457
      /* Copy the values of any pointer components of p->value.  */
458
      switch (q->ts.type)
459
        {
460
        case BT_INTEGER:
461
          mpz_init_set (q->value.integer, p->value.integer);
462
          break;
463
 
464
        case BT_REAL:
465
          gfc_set_model_kind (q->ts.kind);
466
          mpfr_init (q->value.real);
467
          mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
468
          break;
469
 
470
        case BT_COMPLEX:
471
          gfc_set_model_kind (q->ts.kind);
472
          mpc_init2 (q->value.complex, mpfr_get_default_prec());
473
          mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
474
          break;
475
 
476
        case BT_CHARACTER:
477
          if (p->representation.string)
478
            q->value.character.string
479
              = gfc_char_to_widechar (q->representation.string);
480
          else
481
            {
482
              s = gfc_get_wide_string (p->value.character.length + 1);
483
              q->value.character.string = s;
484
 
485
              /* This is the case for the C_NULL_CHAR named constant.  */
486
              if (p->value.character.length == 0
487
                  && (p->ts.is_c_interop || p->ts.is_iso_c))
488
                {
489
                  *s = '\0';
490
                  /* Need to set the length to 1 to make sure the NUL
491
                     terminator is copied.  */
492
                  q->value.character.length = 1;
493
                }
494
              else
495
                memcpy (s, p->value.character.string,
496
                        (p->value.character.length + 1) * sizeof (gfc_char_t));
497
            }
498
          break;
499
 
500
        case BT_HOLLERITH:
501
        case BT_LOGICAL:
502
        case BT_DERIVED:
503
        case BT_CLASS:
504
          break;                /* Already done.  */
505
 
506
        case BT_PROCEDURE:
507
        case BT_VOID:
508
           /* Should never be reached.  */
509
        case BT_UNKNOWN:
510
          gfc_internal_error ("gfc_copy_expr(): Bad expr node");
511
          /* Not reached.  */
512
        }
513
 
514
      break;
515
 
516
    case EXPR_OP:
517
      switch (q->value.op.op)
518
        {
519
        case INTRINSIC_NOT:
520
        case INTRINSIC_PARENTHESES:
521
        case INTRINSIC_UPLUS:
522
        case INTRINSIC_UMINUS:
523
          q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
524
          break;
525
 
526
        default:                /* Binary operators.  */
527
          q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
528
          q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
529
          break;
530
        }
531
 
532
      break;
533
 
534
    case EXPR_FUNCTION:
535
      q->value.function.actual =
536
        gfc_copy_actual_arglist (p->value.function.actual);
537
      break;
538
 
539
    case EXPR_COMPCALL:
540
    case EXPR_PPC:
541
      q->value.compcall.actual =
542
        gfc_copy_actual_arglist (p->value.compcall.actual);
543
      q->value.compcall.tbp = p->value.compcall.tbp;
544
      break;
545
 
546
    case EXPR_STRUCTURE:
547
    case EXPR_ARRAY:
548
      q->value.constructor = gfc_copy_constructor (p->value.constructor);
549
      break;
550
 
551
    case EXPR_VARIABLE:
552
    case EXPR_NULL:
553
      break;
554
    }
555
 
556
  q->shape = gfc_copy_shape (p->shape, p->rank);
557
 
558
  q->ref = gfc_copy_ref (p->ref);
559
 
560
  return q;
561
}
562
 
563
 
564
/* Return the maximum kind of two expressions.  In general, higher
565
   kind numbers mean more precision for numeric types.  */
566
 
567
int
568
gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
569
{
570
  return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
571
}
572
 
573
 
574
/* Returns nonzero if the type is numeric, zero otherwise.  */
575
 
576
static int
577
numeric_type (bt type)
578
{
579
  return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
580
}
581
 
582
 
583
/* Returns nonzero if the typespec is a numeric type, zero otherwise.  */
584
 
585
int
586
gfc_numeric_ts (gfc_typespec *ts)
587
{
588
  return numeric_type (ts->type);
589
}
590
 
591
 
592
/* Returns an expression node that is an integer constant.  */
593
 
594
gfc_expr *
595
gfc_int_expr (int i)
596
{
597
  gfc_expr *p;
598
 
599
  p = gfc_get_expr ();
600
 
601
  p->expr_type = EXPR_CONSTANT;
602
  p->ts.type = BT_INTEGER;
603
  p->ts.kind = gfc_default_integer_kind;
604
 
605
  p->where = gfc_current_locus;
606
  mpz_init_set_si (p->value.integer, i);
607
 
608
  return p;
609
}
610
 
611
 
612
/* Returns an expression node that is a logical constant.  */
613
 
614
gfc_expr *
615
gfc_logical_expr (int i, locus *where)
616
{
617
  gfc_expr *p;
618
 
619
  p = gfc_get_expr ();
620
 
621
  p->expr_type = EXPR_CONSTANT;
622
  p->ts.type = BT_LOGICAL;
623
  p->ts.kind = gfc_default_logical_kind;
624
 
625
  if (where == NULL)
626
    where = &gfc_current_locus;
627
  p->where = *where;
628
  p->value.logical = i;
629
 
630
  return p;
631
}
632
 
633
 
634
/* Return an expression node with an optional argument list attached.
635
   A variable number of gfc_expr pointers are strung together in an
636
   argument list with a NULL pointer terminating the list.  */
637
 
638
gfc_expr *
639
gfc_build_conversion (gfc_expr *e)
640
{
641
  gfc_expr *p;
642
 
643
  p = gfc_get_expr ();
644
  p->expr_type = EXPR_FUNCTION;
645
  p->symtree = NULL;
646
  p->value.function.actual = NULL;
647
 
648
  p->value.function.actual = gfc_get_actual_arglist ();
649
  p->value.function.actual->expr = e;
650
 
651
  return p;
652
}
653
 
654
 
655
/* Given an expression node with some sort of numeric binary
656
   expression, insert type conversions required to make the operands
657
   have the same type. Conversion warnings are disabled if wconversion
658
   is set to 0.
659
 
660
   The exception is that the operands of an exponential don't have to
661
   have the same type.  If possible, the base is promoted to the type
662
   of the exponent.  For example, 1**2.3 becomes 1.0**2.3, but
663
   1.0**2 stays as it is.  */
664
 
665
void
666
gfc_type_convert_binary (gfc_expr *e, int wconversion)
667
{
668
  gfc_expr *op1, *op2;
669
 
670
  op1 = e->value.op.op1;
671
  op2 = e->value.op.op2;
672
 
673
  if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
674
    {
675
      gfc_clear_ts (&e->ts);
676
      return;
677
    }
678
 
679
  /* Kind conversions of same type.  */
680
  if (op1->ts.type == op2->ts.type)
681
    {
682
      if (op1->ts.kind == op2->ts.kind)
683
        {
684
          /* No type conversions.  */
685
          e->ts = op1->ts;
686
          goto done;
687
        }
688
 
689
      if (op1->ts.kind > op2->ts.kind)
690
        gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
691
      else
692
        gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
693
 
694
      e->ts = op1->ts;
695
      goto done;
696
    }
697
 
698
  /* Integer combined with real or complex.  */
699
  if (op2->ts.type == BT_INTEGER)
700
    {
701
      e->ts = op1->ts;
702
 
703
      /* Special case for ** operator.  */
704
      if (e->value.op.op == INTRINSIC_POWER)
705
        goto done;
706
 
707
      gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
708
      goto done;
709
    }
710
 
711
  if (op1->ts.type == BT_INTEGER)
712
    {
713
      e->ts = op2->ts;
714
      gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
715
      goto done;
716
    }
717
 
718
  /* Real combined with complex.  */
719
  e->ts.type = BT_COMPLEX;
720
  if (op1->ts.kind > op2->ts.kind)
721
    e->ts.kind = op1->ts.kind;
722
  else
723
    e->ts.kind = op2->ts.kind;
724
  if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
725
    gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
726
  if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
727
    gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
728
 
729
done:
730
  return;
731
}
732
 
733
 
734
static match
735
check_specification_function (gfc_expr *e)
736
{
737
  gfc_symbol *sym;
738
 
739
  if (!e->symtree)
740
    return MATCH_NO;
741
 
742
  sym = e->symtree->n.sym;
743
 
744
  /* F95, 7.1.6.2; F2003, 7.1.7  */
745
  if (sym
746
      && sym->attr.function
747
      && sym->attr.pure
748
      && !sym->attr.intrinsic
749
      && !sym->attr.recursive
750
      && sym->attr.proc != PROC_INTERNAL
751
      && sym->attr.proc != PROC_ST_FUNCTION
752
      && sym->attr.proc != PROC_UNKNOWN
753
      && sym->formal == NULL)
754
    return MATCH_YES;
755
 
756
  return MATCH_NO;
757
}
758
 
759
/* Function to determine if an expression is constant or not.  This
760
   function expects that the expression has already been simplified.  */
761
 
762
int
763
gfc_is_constant_expr (gfc_expr *e)
764
{
765
  gfc_constructor *c;
766
  gfc_actual_arglist *arg;
767
  int rv;
768
 
769
  if (e == NULL)
770
    return 1;
771
 
772
  switch (e->expr_type)
773
    {
774
    case EXPR_OP:
775
      rv = (gfc_is_constant_expr (e->value.op.op1)
776
            && (e->value.op.op2 == NULL
777
                || gfc_is_constant_expr (e->value.op.op2)));
778
      break;
779
 
780
    case EXPR_VARIABLE:
781
      rv = 0;
782
      break;
783
 
784
    case EXPR_FUNCTION:
785
    case EXPR_PPC:
786
    case EXPR_COMPCALL:
787
      /* Specification functions are constant.  */
788
      if (check_specification_function (e) == MATCH_YES)
789
        {
790
          rv = 1;
791
          break;
792
        }
793
 
794
      /* Call to intrinsic with at least one argument.  */
795
      rv = 0;
796
      if (e->value.function.isym && e->value.function.actual)
797
        {
798
          for (arg = e->value.function.actual; arg; arg = arg->next)
799
            {
800
              if (!gfc_is_constant_expr (arg->expr))
801
                break;
802
            }
803
          if (arg == NULL)
804
            rv = 1;
805
        }
806
      break;
807
 
808
    case EXPR_CONSTANT:
809
    case EXPR_NULL:
810
      rv = 1;
811
      break;
812
 
813
    case EXPR_SUBSTRING:
814
      rv = e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
815
                              && gfc_is_constant_expr (e->ref->u.ss.end));
816
      break;
817
 
818
    case EXPR_STRUCTURE:
819
      rv = 0;
820
      for (c = e->value.constructor; c; c = c->next)
821
        if (!gfc_is_constant_expr (c->expr))
822
          break;
823
 
824
      if (c == NULL)
825
        rv = 1;
826
      break;
827
 
828
    case EXPR_ARRAY:
829
      rv = gfc_constant_ac (e);
830
      break;
831
 
832
    default:
833
      gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
834
    }
835
 
836
  return rv;
837
}
838
 
839
 
840
/* Is true if an array reference is followed by a component or substring
841
   reference.  */
842
bool
843
is_subref_array (gfc_expr * e)
844
{
845
  gfc_ref * ref;
846
  bool seen_array;
847
 
848
  if (e->expr_type != EXPR_VARIABLE)
849
    return false;
850
 
851
  if (e->symtree->n.sym->attr.subref_array_pointer)
852
    return true;
853
 
854
  seen_array = false;
855
  for (ref = e->ref; ref; ref = ref->next)
856
    {
857
      if (ref->type == REF_ARRAY
858
            && ref->u.ar.type != AR_ELEMENT)
859
        seen_array = true;
860
 
861
      if (seen_array
862
            && ref->type != REF_ARRAY)
863
        return seen_array;
864
    }
865
  return false;
866
}
867
 
868
 
869
/* Try to collapse intrinsic expressions.  */
870
 
871
static gfc_try
872
simplify_intrinsic_op (gfc_expr *p, int type)
873
{
874
  gfc_intrinsic_op op;
875
  gfc_expr *op1, *op2, *result;
876
 
877
  if (p->value.op.op == INTRINSIC_USER)
878
    return SUCCESS;
879
 
880
  op1 = p->value.op.op1;
881
  op2 = p->value.op.op2;
882
  op  = p->value.op.op;
883
 
884
  if (gfc_simplify_expr (op1, type) == FAILURE)
885
    return FAILURE;
886
  if (gfc_simplify_expr (op2, type) == FAILURE)
887
    return FAILURE;
888
 
889
  if (!gfc_is_constant_expr (op1)
890
      || (op2 != NULL && !gfc_is_constant_expr (op2)))
891
    return SUCCESS;
892
 
893
  /* Rip p apart.  */
894
  p->value.op.op1 = NULL;
895
  p->value.op.op2 = NULL;
896
 
897
  switch (op)
898
    {
899
    case INTRINSIC_PARENTHESES:
900
      result = gfc_parentheses (op1);
901
      break;
902
 
903
    case INTRINSIC_UPLUS:
904
      result = gfc_uplus (op1);
905
      break;
906
 
907
    case INTRINSIC_UMINUS:
908
      result = gfc_uminus (op1);
909
      break;
910
 
911
    case INTRINSIC_PLUS:
912
      result = gfc_add (op1, op2);
913
      break;
914
 
915
    case INTRINSIC_MINUS:
916
      result = gfc_subtract (op1, op2);
917
      break;
918
 
919
    case INTRINSIC_TIMES:
920
      result = gfc_multiply (op1, op2);
921
      break;
922
 
923
    case INTRINSIC_DIVIDE:
924
      result = gfc_divide (op1, op2);
925
      break;
926
 
927
    case INTRINSIC_POWER:
928
      result = gfc_power (op1, op2);
929
      break;
930
 
931
    case INTRINSIC_CONCAT:
932
      result = gfc_concat (op1, op2);
933
      break;
934
 
935
    case INTRINSIC_EQ:
936
    case INTRINSIC_EQ_OS:
937
      result = gfc_eq (op1, op2, op);
938
      break;
939
 
940
    case INTRINSIC_NE:
941
    case INTRINSIC_NE_OS:
942
      result = gfc_ne (op1, op2, op);
943
      break;
944
 
945
    case INTRINSIC_GT:
946
    case INTRINSIC_GT_OS:
947
      result = gfc_gt (op1, op2, op);
948
      break;
949
 
950
    case INTRINSIC_GE:
951
    case INTRINSIC_GE_OS:
952
      result = gfc_ge (op1, op2, op);
953
      break;
954
 
955
    case INTRINSIC_LT:
956
    case INTRINSIC_LT_OS:
957
      result = gfc_lt (op1, op2, op);
958
      break;
959
 
960
    case INTRINSIC_LE:
961
    case INTRINSIC_LE_OS:
962
      result = gfc_le (op1, op2, op);
963
      break;
964
 
965
    case INTRINSIC_NOT:
966
      result = gfc_not (op1);
967
      break;
968
 
969
    case INTRINSIC_AND:
970
      result = gfc_and (op1, op2);
971
      break;
972
 
973
    case INTRINSIC_OR:
974
      result = gfc_or (op1, op2);
975
      break;
976
 
977
    case INTRINSIC_EQV:
978
      result = gfc_eqv (op1, op2);
979
      break;
980
 
981
    case INTRINSIC_NEQV:
982
      result = gfc_neqv (op1, op2);
983
      break;
984
 
985
    default:
986
      gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
987
    }
988
 
989
  if (result == NULL)
990
    {
991
      gfc_free_expr (op1);
992
      gfc_free_expr (op2);
993
      return FAILURE;
994
    }
995
 
996
  result->rank = p->rank;
997
  result->where = p->where;
998
  gfc_replace_expr (p, result);
999
 
1000
  return SUCCESS;
1001
}
1002
 
1003
 
1004
/* Subroutine to simplify constructor expressions.  Mutually recursive
1005
   with gfc_simplify_expr().  */
1006
 
1007
static gfc_try
1008
simplify_constructor (gfc_constructor *c, int type)
1009
{
1010
  gfc_expr *p;
1011
 
1012
  for (; c; c = c->next)
1013
    {
1014
      if (c->iterator
1015
          && (gfc_simplify_expr (c->iterator->start, type) == FAILURE
1016
              || gfc_simplify_expr (c->iterator->end, type) == FAILURE
1017
              || gfc_simplify_expr (c->iterator->step, type) == FAILURE))
1018
        return FAILURE;
1019
 
1020
      if (c->expr)
1021
        {
1022
          /* Try and simplify a copy.  Replace the original if successful
1023
             but keep going through the constructor at all costs.  Not
1024
             doing so can make a dog's dinner of complicated things.  */
1025
          p = gfc_copy_expr (c->expr);
1026
 
1027
          if (gfc_simplify_expr (p, type) == FAILURE)
1028
            {
1029
              gfc_free_expr (p);
1030
              continue;
1031
            }
1032
 
1033
          gfc_replace_expr (c->expr, p);
1034
        }
1035
    }
1036
 
1037
  return SUCCESS;
1038
}
1039
 
1040
 
1041
/* Pull a single array element out of an array constructor.  */
1042
 
1043
static gfc_try
1044
find_array_element (gfc_constructor *cons, gfc_array_ref *ar,
1045
                    gfc_constructor **rval)
1046
{
1047
  unsigned long nelemen;
1048
  int i;
1049
  mpz_t delta;
1050
  mpz_t offset;
1051
  mpz_t span;
1052
  mpz_t tmp;
1053
  gfc_expr *e;
1054
  gfc_try t;
1055
 
1056
  t = SUCCESS;
1057
  e = NULL;
1058
 
1059
  mpz_init_set_ui (offset, 0);
1060
  mpz_init (delta);
1061
  mpz_init (tmp);
1062
  mpz_init_set_ui (span, 1);
1063
  for (i = 0; i < ar->dimen; i++)
1064
    {
1065
      if (gfc_reduce_init_expr (ar->as->lower[i]) == FAILURE
1066
          || gfc_reduce_init_expr (ar->as->upper[i]) == FAILURE)
1067
        {
1068
          t = FAILURE;
1069
          cons = NULL;
1070
          goto depart;
1071
        }
1072
 
1073
      e = gfc_copy_expr (ar->start[i]);
1074
      if (e->expr_type != EXPR_CONSTANT)
1075
        {
1076
          cons = NULL;
1077
          goto depart;
1078
        }
1079
 
1080
      gcc_assert (ar->as->upper[i]->expr_type == EXPR_CONSTANT
1081
                  && ar->as->lower[i]->expr_type == EXPR_CONSTANT);
1082
 
1083
      /* Check the bounds.  */
1084
      if ((ar->as->upper[i]
1085
           && mpz_cmp (e->value.integer,
1086
                       ar->as->upper[i]->value.integer) > 0)
1087
          || (mpz_cmp (e->value.integer,
1088
                       ar->as->lower[i]->value.integer) < 0))
1089
        {
1090
          gfc_error ("Index in dimension %d is out of bounds "
1091
                     "at %L", i + 1, &ar->c_where[i]);
1092
          cons = NULL;
1093
          t = FAILURE;
1094
          goto depart;
1095
        }
1096
 
1097
      mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1098
      mpz_mul (delta, delta, span);
1099
      mpz_add (offset, offset, delta);
1100
 
1101
      mpz_set_ui (tmp, 1);
1102
      mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
1103
      mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
1104
      mpz_mul (span, span, tmp);
1105
    }
1106
 
1107
  for (nelemen = mpz_get_ui (offset); nelemen > 0; nelemen--)
1108
    {
1109
      if (cons)
1110
        {
1111
          if (cons->iterator)
1112
            {
1113
              cons = NULL;
1114
              goto depart;
1115
            }
1116
          cons = cons->next;
1117
        }
1118
    }
1119
 
1120
depart:
1121
  mpz_clear (delta);
1122
  mpz_clear (offset);
1123
  mpz_clear (span);
1124
  mpz_clear (tmp);
1125
  if (e)
1126
    gfc_free_expr (e);
1127
  *rval = cons;
1128
  return t;
1129
}
1130
 
1131
 
1132
/* Find a component of a structure constructor.  */
1133
 
1134
static gfc_constructor *
1135
find_component_ref (gfc_constructor *cons, gfc_ref *ref)
1136
{
1137
  gfc_component *comp;
1138
  gfc_component *pick;
1139
 
1140
  comp = ref->u.c.sym->components;
1141
  pick = ref->u.c.component;
1142
  while (comp != pick)
1143
    {
1144
      comp = comp->next;
1145
      cons = cons->next;
1146
    }
1147
 
1148
  return cons;
1149
}
1150
 
1151
 
1152
/* Replace an expression with the contents of a constructor, removing
1153
   the subobject reference in the process.  */
1154
 
1155
static void
1156
remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1157
{
1158
  gfc_expr *e;
1159
 
1160
  if (cons)
1161
    {
1162
      e = cons->expr;
1163
      cons->expr = NULL;
1164
    }
1165
  else
1166
    e = gfc_copy_expr (p);
1167
  e->ref = p->ref->next;
1168
  p->ref->next =  NULL;
1169
  gfc_replace_expr (p, e);
1170
}
1171
 
1172
 
1173
/* Pull an array section out of an array constructor.  */
1174
 
1175
static gfc_try
1176
find_array_section (gfc_expr *expr, gfc_ref *ref)
1177
{
1178
  int idx;
1179
  int rank;
1180
  int d;
1181
  int shape_i;
1182
  long unsigned one = 1;
1183
  bool incr_ctr;
1184
  mpz_t start[GFC_MAX_DIMENSIONS];
1185
  mpz_t end[GFC_MAX_DIMENSIONS];
1186
  mpz_t stride[GFC_MAX_DIMENSIONS];
1187
  mpz_t delta[GFC_MAX_DIMENSIONS];
1188
  mpz_t ctr[GFC_MAX_DIMENSIONS];
1189
  mpz_t delta_mpz;
1190
  mpz_t tmp_mpz;
1191
  mpz_t nelts;
1192
  mpz_t ptr;
1193
  mpz_t index;
1194
  gfc_constructor *cons;
1195
  gfc_constructor *base;
1196
  gfc_expr *begin;
1197
  gfc_expr *finish;
1198
  gfc_expr *step;
1199
  gfc_expr *upper;
1200
  gfc_expr *lower;
1201
  gfc_constructor *vecsub[GFC_MAX_DIMENSIONS], *c;
1202
  gfc_try t;
1203
 
1204
  t = SUCCESS;
1205
 
1206
  base = expr->value.constructor;
1207
  expr->value.constructor = NULL;
1208
 
1209
  rank = ref->u.ar.as->rank;
1210
 
1211
  if (expr->shape == NULL)
1212
    expr->shape = gfc_get_shape (rank);
1213
 
1214
  mpz_init_set_ui (delta_mpz, one);
1215
  mpz_init_set_ui (nelts, one);
1216
  mpz_init (tmp_mpz);
1217
 
1218
  /* Do the initialization now, so that we can cleanup without
1219
     keeping track of where we were.  */
1220
  for (d = 0; d < rank; d++)
1221
    {
1222
      mpz_init (delta[d]);
1223
      mpz_init (start[d]);
1224
      mpz_init (end[d]);
1225
      mpz_init (ctr[d]);
1226
      mpz_init (stride[d]);
1227
      vecsub[d] = NULL;
1228
    }
1229
 
1230
  /* Build the counters to clock through the array reference.  */
1231
  shape_i = 0;
1232
  for (d = 0; d < rank; d++)
1233
    {
1234
      /* Make this stretch of code easier on the eye!  */
1235
      begin = ref->u.ar.start[d];
1236
      finish = ref->u.ar.end[d];
1237
      step = ref->u.ar.stride[d];
1238
      lower = ref->u.ar.as->lower[d];
1239
      upper = ref->u.ar.as->upper[d];
1240
 
1241
      if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR)  /* Vector subscript.  */
1242
        {
1243
          gcc_assert (begin);
1244
 
1245
          if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
1246
            {
1247
              t = FAILURE;
1248
              goto cleanup;
1249
            }
1250
 
1251
          gcc_assert (begin->rank == 1);
1252
          /* Zero-sized arrays have no shape and no elements, stop early.  */
1253
          if (!begin->shape)
1254
            {
1255
              mpz_init_set_ui (nelts, 0);
1256
              break;
1257
            }
1258
 
1259
          vecsub[d] = begin->value.constructor;
1260
          mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1261
          mpz_mul (nelts, nelts, begin->shape[0]);
1262
          mpz_set (expr->shape[shape_i++], begin->shape[0]);
1263
 
1264
          /* Check bounds.  */
1265
          for (c = vecsub[d]; c; c = c->next)
1266
            {
1267
              if (mpz_cmp (c->expr->value.integer, upper->value.integer) > 0
1268
                  || mpz_cmp (c->expr->value.integer,
1269
                              lower->value.integer) < 0)
1270
                {
1271
                  gfc_error ("index in dimension %d is out of bounds "
1272
                             "at %L", d + 1, &ref->u.ar.c_where[d]);
1273
                  t = FAILURE;
1274
                  goto cleanup;
1275
                }
1276
            }
1277
        }
1278
      else
1279
        {
1280
          if ((begin && begin->expr_type != EXPR_CONSTANT)
1281
              || (finish && finish->expr_type != EXPR_CONSTANT)
1282
              || (step && step->expr_type != EXPR_CONSTANT))
1283
            {
1284
              t = FAILURE;
1285
              goto cleanup;
1286
            }
1287
 
1288
          /* Obtain the stride.  */
1289
          if (step)
1290
            mpz_set (stride[d], step->value.integer);
1291
          else
1292
            mpz_set_ui (stride[d], one);
1293
 
1294
          if (mpz_cmp_ui (stride[d], 0) == 0)
1295
            mpz_set_ui (stride[d], one);
1296
 
1297
          /* Obtain the start value for the index.  */
1298
          if (begin)
1299
            mpz_set (start[d], begin->value.integer);
1300
          else
1301
            mpz_set (start[d], lower->value.integer);
1302
 
1303
          mpz_set (ctr[d], start[d]);
1304
 
1305
          /* Obtain the end value for the index.  */
1306
          if (finish)
1307
            mpz_set (end[d], finish->value.integer);
1308
          else
1309
            mpz_set (end[d], upper->value.integer);
1310
 
1311
          /* Separate 'if' because elements sometimes arrive with
1312
             non-null end.  */
1313
          if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
1314
            mpz_set (end [d], begin->value.integer);
1315
 
1316
          /* Check the bounds.  */
1317
          if (mpz_cmp (ctr[d], upper->value.integer) > 0
1318
              || mpz_cmp (end[d], upper->value.integer) > 0
1319
              || mpz_cmp (ctr[d], lower->value.integer) < 0
1320
              || mpz_cmp (end[d], lower->value.integer) < 0)
1321
            {
1322
              gfc_error ("index in dimension %d is out of bounds "
1323
                         "at %L", d + 1, &ref->u.ar.c_where[d]);
1324
              t = FAILURE;
1325
              goto cleanup;
1326
            }
1327
 
1328
          /* Calculate the number of elements and the shape.  */
1329
          mpz_set (tmp_mpz, stride[d]);
1330
          mpz_add (tmp_mpz, end[d], tmp_mpz);
1331
          mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
1332
          mpz_div (tmp_mpz, tmp_mpz, stride[d]);
1333
          mpz_mul (nelts, nelts, tmp_mpz);
1334
 
1335
          /* An element reference reduces the rank of the expression; don't
1336
             add anything to the shape array.  */
1337
          if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
1338
            mpz_set (expr->shape[shape_i++], tmp_mpz);
1339
        }
1340
 
1341
      /* Calculate the 'stride' (=delta) for conversion of the
1342
         counter values into the index along the constructor.  */
1343
      mpz_set (delta[d], delta_mpz);
1344
      mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
1345
      mpz_add_ui (tmp_mpz, tmp_mpz, one);
1346
      mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
1347
    }
1348
 
1349
  mpz_init (index);
1350
  mpz_init (ptr);
1351
  cons = base;
1352
 
1353
  /* Now clock through the array reference, calculating the index in
1354
     the source constructor and transferring the elements to the new
1355
     constructor.  */
1356
  for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1357
    {
1358
      if (ref->u.ar.offset)
1359
        mpz_set (ptr, ref->u.ar.offset->value.integer);
1360
      else
1361
        mpz_init_set_ui (ptr, 0);
1362
 
1363
      incr_ctr = true;
1364
      for (d = 0; d < rank; d++)
1365
        {
1366
          mpz_set (tmp_mpz, ctr[d]);
1367
          mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1368
          mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
1369
          mpz_add (ptr, ptr, tmp_mpz);
1370
 
1371
          if (!incr_ctr) continue;
1372
 
1373
          if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript.  */
1374
            {
1375
              gcc_assert(vecsub[d]);
1376
 
1377
              if (!vecsub[d]->next)
1378
                vecsub[d] = ref->u.ar.start[d]->value.constructor;
1379
              else
1380
                {
1381
                  vecsub[d] = vecsub[d]->next;
1382
                  incr_ctr = false;
1383
                }
1384
              mpz_set (ctr[d], vecsub[d]->expr->value.integer);
1385
            }
1386
          else
1387
            {
1388
              mpz_add (ctr[d], ctr[d], stride[d]);
1389
 
1390
              if (mpz_cmp_ui (stride[d], 0) > 0
1391
                  ? mpz_cmp (ctr[d], end[d]) > 0
1392
                  : mpz_cmp (ctr[d], end[d]) < 0)
1393
                mpz_set (ctr[d], start[d]);
1394
              else
1395
                incr_ctr = false;
1396
            }
1397
        }
1398
 
1399
      /* There must be a better way of dealing with negative strides
1400
         than resetting the index and the constructor pointer!  */
1401
      if (mpz_cmp (ptr, index) < 0)
1402
        {
1403
          mpz_set_ui (index, 0);
1404
          cons = base;
1405
        }
1406
 
1407
      while (cons && cons->next && mpz_cmp (ptr, index) > 0)
1408
        {
1409
          mpz_add_ui (index, index, one);
1410
          cons = cons->next;
1411
        }
1412
 
1413
      gfc_append_constructor (expr, gfc_copy_expr (cons->expr));
1414
    }
1415
 
1416
  mpz_clear (ptr);
1417
  mpz_clear (index);
1418
 
1419
cleanup:
1420
 
1421
  mpz_clear (delta_mpz);
1422
  mpz_clear (tmp_mpz);
1423
  mpz_clear (nelts);
1424
  for (d = 0; d < rank; d++)
1425
    {
1426
      mpz_clear (delta[d]);
1427
      mpz_clear (start[d]);
1428
      mpz_clear (end[d]);
1429
      mpz_clear (ctr[d]);
1430
      mpz_clear (stride[d]);
1431
    }
1432
  gfc_free_constructor (base);
1433
  return t;
1434
}
1435
 
1436
/* Pull a substring out of an expression.  */
1437
 
1438
static gfc_try
1439
find_substring_ref (gfc_expr *p, gfc_expr **newp)
1440
{
1441
  int end;
1442
  int start;
1443
  int length;
1444
  gfc_char_t *chr;
1445
 
1446
  if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1447
      || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1448
    return FAILURE;
1449
 
1450
  *newp = gfc_copy_expr (p);
1451
  gfc_free ((*newp)->value.character.string);
1452
 
1453
  end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
1454
  start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
1455
  length = end - start + 1;
1456
 
1457
  chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1458
  (*newp)->value.character.length = length;
1459
  memcpy (chr, &p->value.character.string[start - 1],
1460
          length * sizeof (gfc_char_t));
1461
  chr[length] = '\0';
1462
  return SUCCESS;
1463
}
1464
 
1465
 
1466
 
1467
/* Simplify a subobject reference of a constructor.  This occurs when
1468
   parameter variable values are substituted.  */
1469
 
1470
static gfc_try
1471
simplify_const_ref (gfc_expr *p)
1472
{
1473
  gfc_constructor *cons;
1474
  gfc_expr *newp;
1475
  gfc_ref *last_ref;
1476
 
1477
  while (p->ref)
1478
    {
1479
      switch (p->ref->type)
1480
        {
1481
        case REF_ARRAY:
1482
          switch (p->ref->u.ar.type)
1483
            {
1484
            case AR_ELEMENT:
1485
              /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
1486
                 will generate this.  */
1487
              if (p->expr_type != EXPR_ARRAY)
1488
                {
1489
                  remove_subobject_ref (p, NULL);
1490
                  break;
1491
                }
1492
              if (find_array_element (p->value.constructor, &p->ref->u.ar,
1493
                                      &cons) == FAILURE)
1494
                return FAILURE;
1495
 
1496
              if (!cons)
1497
                return SUCCESS;
1498
 
1499
              remove_subobject_ref (p, cons);
1500
              break;
1501
 
1502
            case AR_SECTION:
1503
              if (find_array_section (p, p->ref) == FAILURE)
1504
                return FAILURE;
1505
              p->ref->u.ar.type = AR_FULL;
1506
 
1507
            /* Fall through.  */
1508
 
1509
            case AR_FULL:
1510
              if (p->ref->next != NULL
1511
                  && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
1512
                {
1513
                  cons = p->value.constructor;
1514
                  for (; cons; cons = cons->next)
1515
                    {
1516
                      cons->expr->ref = gfc_copy_ref (p->ref->next);
1517
                      if (simplify_const_ref (cons->expr) == FAILURE)
1518
                        return FAILURE;
1519
                    }
1520
 
1521
                  if (p->ts.type == BT_DERIVED
1522
                        && p->ref->next
1523
                        && p->value.constructor)
1524
                    {
1525
                      /* There may have been component references.  */
1526
                      p->ts = p->value.constructor->expr->ts;
1527
                    }
1528
 
1529
                  last_ref = p->ref;
1530
                  for (; last_ref->next; last_ref = last_ref->next) {};
1531
 
1532
                  if (p->ts.type == BT_CHARACTER
1533
                        && last_ref->type == REF_SUBSTRING)
1534
                    {
1535
                      /* If this is a CHARACTER array and we possibly took
1536
                         a substring out of it, update the type-spec's
1537
                         character length according to the first element
1538
                         (as all should have the same length).  */
1539
                      int string_len;
1540
                      if (p->value.constructor)
1541
                        {
1542
                          const gfc_expr* first = p->value.constructor->expr;
1543
                          gcc_assert (first->expr_type == EXPR_CONSTANT);
1544
                          gcc_assert (first->ts.type == BT_CHARACTER);
1545
                          string_len = first->value.character.length;
1546
                        }
1547
                      else
1548
                        string_len = 0;
1549
 
1550
                      if (!p->ts.u.cl)
1551
                        p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
1552
                                                      NULL);
1553
                      else
1554
                        gfc_free_expr (p->ts.u.cl->length);
1555
 
1556
                      p->ts.u.cl->length = gfc_int_expr (string_len);
1557
                    }
1558
                }
1559
              gfc_free_ref_list (p->ref);
1560
              p->ref = NULL;
1561
              break;
1562
 
1563
            default:
1564
              return SUCCESS;
1565
            }
1566
 
1567
          break;
1568
 
1569
        case REF_COMPONENT:
1570
          cons = find_component_ref (p->value.constructor, p->ref);
1571
          remove_subobject_ref (p, cons);
1572
          break;
1573
 
1574
        case REF_SUBSTRING:
1575
          if (find_substring_ref (p, &newp) == FAILURE)
1576
            return FAILURE;
1577
 
1578
          gfc_replace_expr (p, newp);
1579
          gfc_free_ref_list (p->ref);
1580
          p->ref = NULL;
1581
          break;
1582
        }
1583
    }
1584
 
1585
  return SUCCESS;
1586
}
1587
 
1588
 
1589
/* Simplify a chain of references.  */
1590
 
1591
static gfc_try
1592
simplify_ref_chain (gfc_ref *ref, int type)
1593
{
1594
  int n;
1595
 
1596
  for (; ref; ref = ref->next)
1597
    {
1598
      switch (ref->type)
1599
        {
1600
        case REF_ARRAY:
1601
          for (n = 0; n < ref->u.ar.dimen; n++)
1602
            {
1603
              if (gfc_simplify_expr (ref->u.ar.start[n], type) == FAILURE)
1604
                return FAILURE;
1605
              if (gfc_simplify_expr (ref->u.ar.end[n], type) == FAILURE)
1606
                return FAILURE;
1607
              if (gfc_simplify_expr (ref->u.ar.stride[n], type) == FAILURE)
1608
                return FAILURE;
1609
            }
1610
          break;
1611
 
1612
        case REF_SUBSTRING:
1613
          if (gfc_simplify_expr (ref->u.ss.start, type) == FAILURE)
1614
            return FAILURE;
1615
          if (gfc_simplify_expr (ref->u.ss.end, type) == FAILURE)
1616
            return FAILURE;
1617
          break;
1618
 
1619
        default:
1620
          break;
1621
        }
1622
    }
1623
  return SUCCESS;
1624
}
1625
 
1626
 
1627
/* Try to substitute the value of a parameter variable.  */
1628
 
1629
static gfc_try
1630
simplify_parameter_variable (gfc_expr *p, int type)
1631
{
1632
  gfc_expr *e;
1633
  gfc_try t;
1634
 
1635
  e = gfc_copy_expr (p->symtree->n.sym->value);
1636
  if (e == NULL)
1637
    return FAILURE;
1638
 
1639
  e->rank = p->rank;
1640
 
1641
  /* Do not copy subobject refs for constant.  */
1642
  if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
1643
    e->ref = gfc_copy_ref (p->ref);
1644
  t = gfc_simplify_expr (e, type);
1645
 
1646
  /* Only use the simplification if it eliminated all subobject references.  */
1647
  if (t == SUCCESS && !e->ref)
1648
    gfc_replace_expr (p, e);
1649
  else
1650
    gfc_free_expr (e);
1651
 
1652
  return t;
1653
}
1654
 
1655
/* Given an expression, simplify it by collapsing constant
1656
   expressions.  Most simplification takes place when the expression
1657
   tree is being constructed.  If an intrinsic function is simplified
1658
   at some point, we get called again to collapse the result against
1659
   other constants.
1660
 
1661
   We work by recursively simplifying expression nodes, simplifying
1662
   intrinsic functions where possible, which can lead to further
1663
   constant collapsing.  If an operator has constant operand(s), we
1664
   rip the expression apart, and rebuild it, hoping that it becomes
1665
   something simpler.
1666
 
1667
   The expression type is defined for:
1668
 
1669
     1   Simplifying array constructors -- will substitute
1670
         iterator values.
1671
   Returns FAILURE on error, SUCCESS otherwise.
1672
   NOTE: Will return SUCCESS even if the expression can not be simplified.  */
1673
 
1674
gfc_try
1675
gfc_simplify_expr (gfc_expr *p, int type)
1676
{
1677
  gfc_actual_arglist *ap;
1678
 
1679
  if (p == NULL)
1680
    return SUCCESS;
1681
 
1682
  switch (p->expr_type)
1683
    {
1684
    case EXPR_CONSTANT:
1685
    case EXPR_NULL:
1686
      break;
1687
 
1688
    case EXPR_FUNCTION:
1689
      for (ap = p->value.function.actual; ap; ap = ap->next)
1690
        if (gfc_simplify_expr (ap->expr, type) == FAILURE)
1691
          return FAILURE;
1692
 
1693
      if (p->value.function.isym != NULL
1694
          && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
1695
        return FAILURE;
1696
 
1697
      break;
1698
 
1699
    case EXPR_SUBSTRING:
1700
      if (simplify_ref_chain (p->ref, type) == FAILURE)
1701
        return FAILURE;
1702
 
1703
      if (gfc_is_constant_expr (p))
1704
        {
1705
          gfc_char_t *s;
1706
          int start, end;
1707
 
1708
          start = 0;
1709
          if (p->ref && p->ref->u.ss.start)
1710
            {
1711
              gfc_extract_int (p->ref->u.ss.start, &start);
1712
              start--;  /* Convert from one-based to zero-based.  */
1713
            }
1714
 
1715
          end = p->value.character.length;
1716
          if (p->ref && p->ref->u.ss.end)
1717
            gfc_extract_int (p->ref->u.ss.end, &end);
1718
 
1719
          s = gfc_get_wide_string (end - start + 2);
1720
          memcpy (s, p->value.character.string + start,
1721
                  (end - start) * sizeof (gfc_char_t));
1722
          s[end - start + 1] = '\0';  /* TODO: C-style string.  */
1723
          gfc_free (p->value.character.string);
1724
          p->value.character.string = s;
1725
          p->value.character.length = end - start;
1726
          p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1727
          p->ts.u.cl->length = gfc_int_expr (p->value.character.length);
1728
          gfc_free_ref_list (p->ref);
1729
          p->ref = NULL;
1730
          p->expr_type = EXPR_CONSTANT;
1731
        }
1732
      break;
1733
 
1734
    case EXPR_OP:
1735
      if (simplify_intrinsic_op (p, type) == FAILURE)
1736
        return FAILURE;
1737
      break;
1738
 
1739
    case EXPR_VARIABLE:
1740
      /* Only substitute array parameter variables if we are in an
1741
         initialization expression, or we want a subsection.  */
1742
      if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
1743
          && (gfc_init_expr || p->ref
1744
              || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
1745
        {
1746
          if (simplify_parameter_variable (p, type) == FAILURE)
1747
            return FAILURE;
1748
          break;
1749
        }
1750
 
1751
      if (type == 1)
1752
        {
1753
          gfc_simplify_iterator_var (p);
1754
        }
1755
 
1756
      /* Simplify subcomponent references.  */
1757
      if (simplify_ref_chain (p->ref, type) == FAILURE)
1758
        return FAILURE;
1759
 
1760
      break;
1761
 
1762
    case EXPR_STRUCTURE:
1763
    case EXPR_ARRAY:
1764
      if (simplify_ref_chain (p->ref, type) == FAILURE)
1765
        return FAILURE;
1766
 
1767
      if (simplify_constructor (p->value.constructor, type) == FAILURE)
1768
        return FAILURE;
1769
 
1770
      if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
1771
          && p->ref->u.ar.type == AR_FULL)
1772
          gfc_expand_constructor (p);
1773
 
1774
      if (simplify_const_ref (p) == FAILURE)
1775
        return FAILURE;
1776
 
1777
      break;
1778
 
1779
    case EXPR_COMPCALL:
1780
    case EXPR_PPC:
1781
      gcc_unreachable ();
1782
      break;
1783
    }
1784
 
1785
  return SUCCESS;
1786
}
1787
 
1788
 
1789
/* Returns the type of an expression with the exception that iterator
1790
   variables are automatically integers no matter what else they may
1791
   be declared as.  */
1792
 
1793
static bt
1794
et0 (gfc_expr *e)
1795
{
1796
  if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e) == SUCCESS)
1797
    return BT_INTEGER;
1798
 
1799
  return e->ts.type;
1800
}
1801
 
1802
 
1803
/* Check an intrinsic arithmetic operation to see if it is consistent
1804
   with some type of expression.  */
1805
 
1806
static gfc_try check_init_expr (gfc_expr *);
1807
 
1808
 
1809
/* Scalarize an expression for an elemental intrinsic call.  */
1810
 
1811
static gfc_try
1812
scalarize_intrinsic_call (gfc_expr *e)
1813
{
1814
  gfc_actual_arglist *a, *b;
1815
  gfc_constructor *args[5], *ctor, *new_ctor;
1816
  gfc_expr *expr, *old;
1817
  int n, i, rank[5], array_arg;
1818
 
1819
  /* Find which, if any, arguments are arrays.  Assume that the old
1820
     expression carries the type information and that the first arg
1821
     that is an array expression carries all the shape information.*/
1822
  n = array_arg = 0;
1823
  a = e->value.function.actual;
1824
  for (; a; a = a->next)
1825
    {
1826
      n++;
1827
      if (a->expr->expr_type != EXPR_ARRAY)
1828
        continue;
1829
      array_arg = n;
1830
      expr = gfc_copy_expr (a->expr);
1831
      break;
1832
    }
1833
 
1834
  if (!array_arg)
1835
    return FAILURE;
1836
 
1837
  old = gfc_copy_expr (e);
1838
 
1839
  gfc_free_constructor (expr->value.constructor);
1840
  expr->value.constructor = NULL;
1841
 
1842
  expr->ts = old->ts;
1843
  expr->where = old->where;
1844
  expr->expr_type = EXPR_ARRAY;
1845
 
1846
  /* Copy the array argument constructors into an array, with nulls
1847
     for the scalars.  */
1848
  n = 0;
1849
  a = old->value.function.actual;
1850
  for (; a; a = a->next)
1851
    {
1852
      /* Check that this is OK for an initialization expression.  */
1853
      if (a->expr && check_init_expr (a->expr) == FAILURE)
1854
        goto cleanup;
1855
 
1856
      rank[n] = 0;
1857
      if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
1858
        {
1859
          rank[n] = a->expr->rank;
1860
          ctor = a->expr->symtree->n.sym->value->value.constructor;
1861
          args[n] = gfc_copy_constructor (ctor);
1862
        }
1863
      else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
1864
        {
1865
          if (a->expr->rank)
1866
            rank[n] = a->expr->rank;
1867
          else
1868
            rank[n] = 1;
1869
          args[n] = gfc_copy_constructor (a->expr->value.constructor);
1870
        }
1871
      else
1872
        args[n] = NULL;
1873
      n++;
1874
    }
1875
 
1876
 
1877
  /* Using the array argument as the master, step through the array
1878
     calling the function for each element and advancing the array
1879
     constructors together.  */
1880
  ctor = args[array_arg - 1];
1881
  new_ctor = NULL;
1882
  for (; ctor; ctor = ctor->next)
1883
    {
1884
          if (expr->value.constructor == NULL)
1885
            expr->value.constructor
1886
                = new_ctor = gfc_get_constructor ();
1887
          else
1888
            {
1889
              new_ctor->next = gfc_get_constructor ();
1890
              new_ctor = new_ctor->next;
1891
            }
1892
          new_ctor->expr = gfc_copy_expr (old);
1893
          gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
1894
          a = NULL;
1895
          b = old->value.function.actual;
1896
          for (i = 0; i < n; i++)
1897
            {
1898
              if (a == NULL)
1899
                new_ctor->expr->value.function.actual
1900
                        = a = gfc_get_actual_arglist ();
1901
              else
1902
                {
1903
                  a->next = gfc_get_actual_arglist ();
1904
                  a = a->next;
1905
                }
1906
              if (args[i])
1907
                a->expr = gfc_copy_expr (args[i]->expr);
1908
              else
1909
                a->expr = gfc_copy_expr (b->expr);
1910
 
1911
              b = b->next;
1912
            }
1913
 
1914
          /* Simplify the function calls.  If the simplification fails, the
1915
             error will be flagged up down-stream or the library will deal
1916
             with it.  */
1917
          gfc_simplify_expr (new_ctor->expr, 0);
1918
 
1919
          for (i = 0; i < n; i++)
1920
            if (args[i])
1921
              args[i] = args[i]->next;
1922
 
1923
          for (i = 1; i < n; i++)
1924
            if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
1925
                         || (args[i] == NULL && args[array_arg - 1] != NULL)))
1926
              goto compliance;
1927
    }
1928
 
1929
  free_expr0 (e);
1930
  *e = *expr;
1931
  gfc_free_expr (old);
1932
  return SUCCESS;
1933
 
1934
compliance:
1935
  gfc_error_now ("elemental function arguments at %C are not compliant");
1936
 
1937
cleanup:
1938
  gfc_free_expr (expr);
1939
  gfc_free_expr (old);
1940
  return FAILURE;
1941
}
1942
 
1943
 
1944
static gfc_try
1945
check_intrinsic_op (gfc_expr *e, gfc_try (*check_function) (gfc_expr *))
1946
{
1947
  gfc_expr *op1 = e->value.op.op1;
1948
  gfc_expr *op2 = e->value.op.op2;
1949
 
1950
  if ((*check_function) (op1) == FAILURE)
1951
    return FAILURE;
1952
 
1953
  switch (e->value.op.op)
1954
    {
1955
    case INTRINSIC_UPLUS:
1956
    case INTRINSIC_UMINUS:
1957
      if (!numeric_type (et0 (op1)))
1958
        goto not_numeric;
1959
      break;
1960
 
1961
    case INTRINSIC_EQ:
1962
    case INTRINSIC_EQ_OS:
1963
    case INTRINSIC_NE:
1964
    case INTRINSIC_NE_OS:
1965
    case INTRINSIC_GT:
1966
    case INTRINSIC_GT_OS:
1967
    case INTRINSIC_GE:
1968
    case INTRINSIC_GE_OS:
1969
    case INTRINSIC_LT:
1970
    case INTRINSIC_LT_OS:
1971
    case INTRINSIC_LE:
1972
    case INTRINSIC_LE_OS:
1973
      if ((*check_function) (op2) == FAILURE)
1974
        return FAILURE;
1975
 
1976
      if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
1977
          && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
1978
        {
1979
          gfc_error ("Numeric or CHARACTER operands are required in "
1980
                     "expression at %L", &e->where);
1981
         return FAILURE;
1982
        }
1983
      break;
1984
 
1985
    case INTRINSIC_PLUS:
1986
    case INTRINSIC_MINUS:
1987
    case INTRINSIC_TIMES:
1988
    case INTRINSIC_DIVIDE:
1989
    case INTRINSIC_POWER:
1990
      if ((*check_function) (op2) == FAILURE)
1991
        return FAILURE;
1992
 
1993
      if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
1994
        goto not_numeric;
1995
 
1996
      break;
1997
 
1998
    case INTRINSIC_CONCAT:
1999
      if ((*check_function) (op2) == FAILURE)
2000
        return FAILURE;
2001
 
2002
      if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
2003
        {
2004
          gfc_error ("Concatenation operator in expression at %L "
2005
                     "must have two CHARACTER operands", &op1->where);
2006
          return FAILURE;
2007
        }
2008
 
2009
      if (op1->ts.kind != op2->ts.kind)
2010
        {
2011
          gfc_error ("Concat operator at %L must concatenate strings of the "
2012
                     "same kind", &e->where);
2013
          return FAILURE;
2014
        }
2015
 
2016
      break;
2017
 
2018
    case INTRINSIC_NOT:
2019
      if (et0 (op1) != BT_LOGICAL)
2020
        {
2021
          gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2022
                     "operand", &op1->where);
2023
          return FAILURE;
2024
        }
2025
 
2026
      break;
2027
 
2028
    case INTRINSIC_AND:
2029
    case INTRINSIC_OR:
2030
    case INTRINSIC_EQV:
2031
    case INTRINSIC_NEQV:
2032
      if ((*check_function) (op2) == FAILURE)
2033
        return FAILURE;
2034
 
2035
      if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
2036
        {
2037
          gfc_error ("LOGICAL operands are required in expression at %L",
2038
                     &e->where);
2039
          return FAILURE;
2040
        }
2041
 
2042
      break;
2043
 
2044
    case INTRINSIC_PARENTHESES:
2045
      break;
2046
 
2047
    default:
2048
      gfc_error ("Only intrinsic operators can be used in expression at %L",
2049
                 &e->where);
2050
      return FAILURE;
2051
    }
2052
 
2053
  return SUCCESS;
2054
 
2055
not_numeric:
2056
  gfc_error ("Numeric operands are required in expression at %L", &e->where);
2057
 
2058
  return FAILURE;
2059
}
2060
 
2061
/* F2003, 7.1.7 (3): In init expression, allocatable components
2062
   must not be data-initialized.  */
2063
static gfc_try
2064
check_alloc_comp_init (gfc_expr *e)
2065
{
2066
  gfc_component *c;
2067
  gfc_constructor *ctor;
2068
 
2069
  gcc_assert (e->expr_type == EXPR_STRUCTURE);
2070
  gcc_assert (e->ts.type == BT_DERIVED);
2071
 
2072
  for (c = e->ts.u.derived->components, ctor = e->value.constructor;
2073
       c; c = c->next, ctor = ctor->next)
2074
    {
2075
      if (c->attr.allocatable
2076
          && ctor->expr->expr_type != EXPR_NULL)
2077
        {
2078
          gfc_error("Invalid initialization expression for ALLOCATABLE "
2079
                    "component '%s' in structure constructor at %L",
2080
                    c->name, &ctor->expr->where);
2081
          return FAILURE;
2082
        }
2083
    }
2084
 
2085
  return SUCCESS;
2086
}
2087
 
2088
static match
2089
check_init_expr_arguments (gfc_expr *e)
2090
{
2091
  gfc_actual_arglist *ap;
2092
 
2093
  for (ap = e->value.function.actual; ap; ap = ap->next)
2094
    if (check_init_expr (ap->expr) == FAILURE)
2095
      return MATCH_ERROR;
2096
 
2097
  return MATCH_YES;
2098
}
2099
 
2100
static gfc_try check_restricted (gfc_expr *);
2101
 
2102
/* F95, 7.1.6.1, Initialization expressions, (7)
2103
   F2003, 7.1.7 Initialization expression, (8)  */
2104
 
2105
static match
2106
check_inquiry (gfc_expr *e, int not_restricted)
2107
{
2108
  const char *name;
2109
  const char *const *functions;
2110
 
2111
  static const char *const inquiry_func_f95[] = {
2112
    "lbound", "shape", "size", "ubound",
2113
    "bit_size", "len", "kind",
2114
    "digits", "epsilon", "huge", "maxexponent", "minexponent",
2115
    "precision", "radix", "range", "tiny",
2116
    NULL
2117
  };
2118
 
2119
  static const char *const inquiry_func_f2003[] = {
2120
    "lbound", "shape", "size", "ubound",
2121
    "bit_size", "len", "kind",
2122
    "digits", "epsilon", "huge", "maxexponent", "minexponent",
2123
    "precision", "radix", "range", "tiny",
2124
    "new_line", NULL
2125
  };
2126
 
2127
  int i;
2128
  gfc_actual_arglist *ap;
2129
 
2130
  if (!e->value.function.isym
2131
      || !e->value.function.isym->inquiry)
2132
    return MATCH_NO;
2133
 
2134
  /* An undeclared parameter will get us here (PR25018).  */
2135
  if (e->symtree == NULL)
2136
    return MATCH_NO;
2137
 
2138
  name = e->symtree->n.sym->name;
2139
 
2140
  functions = (gfc_option.warn_std & GFC_STD_F2003)
2141
                ? inquiry_func_f2003 : inquiry_func_f95;
2142
 
2143
  for (i = 0; functions[i]; i++)
2144
    if (strcmp (functions[i], name) == 0)
2145
      break;
2146
 
2147
  if (functions[i] == NULL)
2148
    return MATCH_ERROR;
2149
 
2150
  /* At this point we have an inquiry function with a variable argument.  The
2151
     type of the variable might be undefined, but we need it now, because the
2152
     arguments of these functions are not allowed to be undefined.  */
2153
 
2154
  for (ap = e->value.function.actual; ap; ap = ap->next)
2155
    {
2156
      if (!ap->expr)
2157
        continue;
2158
 
2159
      if (ap->expr->ts.type == BT_UNKNOWN)
2160
        {
2161
          if (ap->expr->symtree->n.sym->ts.type == BT_UNKNOWN
2162
              && gfc_set_default_type (ap->expr->symtree->n.sym, 0, gfc_current_ns)
2163
              == FAILURE)
2164
            return MATCH_NO;
2165
 
2166
          ap->expr->ts = ap->expr->symtree->n.sym->ts;
2167
        }
2168
 
2169
        /* Assumed character length will not reduce to a constant expression
2170
           with LEN, as required by the standard.  */
2171
        if (i == 5 && not_restricted
2172
            && ap->expr->symtree->n.sym->ts.type == BT_CHARACTER
2173
            && ap->expr->symtree->n.sym->ts.u.cl->length == NULL)
2174
          {
2175
            gfc_error ("Assumed character length variable '%s' in constant "
2176
                       "expression at %L", e->symtree->n.sym->name, &e->where);
2177
              return MATCH_ERROR;
2178
          }
2179
        else if (not_restricted && check_init_expr (ap->expr) == FAILURE)
2180
          return MATCH_ERROR;
2181
 
2182
        if (not_restricted == 0
2183
              && ap->expr->expr_type != EXPR_VARIABLE
2184
              && check_restricted (ap->expr) == FAILURE)
2185
          return MATCH_ERROR;
2186
    }
2187
 
2188
  return MATCH_YES;
2189
}
2190
 
2191
 
2192
/* F95, 7.1.6.1, Initialization expressions, (5)
2193
   F2003, 7.1.7 Initialization expression, (5)  */
2194
 
2195
static match
2196
check_transformational (gfc_expr *e)
2197
{
2198
  static const char * const trans_func_f95[] = {
2199
    "repeat", "reshape", "selected_int_kind",
2200
    "selected_real_kind", "transfer", "trim", NULL
2201
  };
2202
 
2203
  static const char * const trans_func_f2003[] =  {
2204
    "all", "any", "count", "dot_product", "matmul", "null", "pack",
2205
    "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2206
    "selected_real_kind", "spread", "sum", "transfer", "transpose",
2207
    "trim", "unpack", NULL
2208
  };
2209
 
2210
  int i;
2211
  const char *name;
2212
  const char *const *functions;
2213
 
2214
  if (!e->value.function.isym
2215
      || !e->value.function.isym->transformational)
2216
    return MATCH_NO;
2217
 
2218
  name = e->symtree->n.sym->name;
2219
 
2220
  functions = (gfc_option.allow_std & GFC_STD_F2003)
2221
                ? trans_func_f2003 : trans_func_f95;
2222
 
2223
  /* NULL() is dealt with below.  */
2224
  if (strcmp ("null", name) == 0)
2225
    return MATCH_NO;
2226
 
2227
  for (i = 0; functions[i]; i++)
2228
    if (strcmp (functions[i], name) == 0)
2229
       break;
2230
 
2231
  if (functions[i] == NULL)
2232
    {
2233
      gfc_error("transformational intrinsic '%s' at %L is not permitted "
2234
                "in an initialization expression", name, &e->where);
2235
      return MATCH_ERROR;
2236
    }
2237
 
2238
  return check_init_expr_arguments (e);
2239
}
2240
 
2241
 
2242
/* F95, 7.1.6.1, Initialization expressions, (6)
2243
   F2003, 7.1.7 Initialization expression, (6)  */
2244
 
2245
static match
2246
check_null (gfc_expr *e)
2247
{
2248
  if (strcmp ("null", e->symtree->n.sym->name) != 0)
2249
    return MATCH_NO;
2250
 
2251
  return check_init_expr_arguments (e);
2252
}
2253
 
2254
 
2255
static match
2256
check_elemental (gfc_expr *e)
2257
{
2258
  if (!e->value.function.isym
2259
      || !e->value.function.isym->elemental)
2260
    return MATCH_NO;
2261
 
2262
  if (e->ts.type != BT_INTEGER
2263
      && e->ts.type != BT_CHARACTER
2264
      && gfc_notify_std (GFC_STD_F2003, "Extension: Evaluation of "
2265
                        "nonstandard initialization expression at %L",
2266
                        &e->where) == FAILURE)
2267
    return MATCH_ERROR;
2268
 
2269
  return check_init_expr_arguments (e);
2270
}
2271
 
2272
 
2273
static match
2274
check_conversion (gfc_expr *e)
2275
{
2276
  if (!e->value.function.isym
2277
      || !e->value.function.isym->conversion)
2278
    return MATCH_NO;
2279
 
2280
  return check_init_expr_arguments (e);
2281
}
2282
 
2283
 
2284
/* Verify that an expression is an initialization expression.  A side
2285
   effect is that the expression tree is reduced to a single constant
2286
   node if all goes well.  This would normally happen when the
2287
   expression is constructed but function references are assumed to be
2288
   intrinsics in the context of initialization expressions.  If
2289
   FAILURE is returned an error message has been generated.  */
2290
 
2291
static gfc_try
2292
check_init_expr (gfc_expr *e)
2293
{
2294
  match m;
2295
  gfc_try t;
2296
 
2297
  if (e == NULL)
2298
    return SUCCESS;
2299
 
2300
  switch (e->expr_type)
2301
    {
2302
    case EXPR_OP:
2303
      t = check_intrinsic_op (e, check_init_expr);
2304
      if (t == SUCCESS)
2305
        t = gfc_simplify_expr (e, 0);
2306
 
2307
      break;
2308
 
2309
    case EXPR_FUNCTION:
2310
      t = FAILURE;
2311
 
2312
      {
2313
        gfc_intrinsic_sym* isym;
2314
        gfc_symbol* sym;
2315
 
2316
        sym = e->symtree->n.sym;
2317
        if (!gfc_is_intrinsic (sym, 0, e->where)
2318
            || (m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)
2319
          {
2320
            gfc_error ("Function '%s' in initialization expression at %L "
2321
                       "must be an intrinsic function",
2322
                       e->symtree->n.sym->name, &e->where);
2323
            break;
2324
          }
2325
 
2326
        if ((m = check_conversion (e)) == MATCH_NO
2327
            && (m = check_inquiry (e, 1)) == MATCH_NO
2328
            && (m = check_null (e)) == MATCH_NO
2329
            && (m = check_transformational (e)) == MATCH_NO
2330
            && (m = check_elemental (e)) == MATCH_NO)
2331
          {
2332
            gfc_error ("Intrinsic function '%s' at %L is not permitted "
2333
                       "in an initialization expression",
2334
                       e->symtree->n.sym->name, &e->where);
2335
            m = MATCH_ERROR;
2336
          }
2337
 
2338
        /* Try to scalarize an elemental intrinsic function that has an
2339
           array argument.  */
2340
        isym = gfc_find_function (e->symtree->n.sym->name);
2341
        if (isym && isym->elemental
2342
            && (t = scalarize_intrinsic_call (e)) == SUCCESS)
2343
          break;
2344
      }
2345
 
2346
      if (m == MATCH_YES)
2347
        t = gfc_simplify_expr (e, 0);
2348
 
2349
      break;
2350
 
2351
    case EXPR_VARIABLE:
2352
      t = SUCCESS;
2353
 
2354
      if (gfc_check_iter_variable (e) == SUCCESS)
2355
        break;
2356
 
2357
      if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
2358
        {
2359
          /* A PARAMETER shall not be used to define itself, i.e.
2360
                REAL, PARAMETER :: x = transfer(0, x)
2361
             is invalid.  */
2362
          if (!e->symtree->n.sym->value)
2363
            {
2364
              gfc_error("PARAMETER '%s' is used at %L before its definition "
2365
                        "is complete", e->symtree->n.sym->name, &e->where);
2366
              t = FAILURE;
2367
            }
2368
          else
2369
            t = simplify_parameter_variable (e, 0);
2370
 
2371
          break;
2372
        }
2373
 
2374
      if (gfc_in_match_data ())
2375
        break;
2376
 
2377
      t = FAILURE;
2378
 
2379
      if (e->symtree->n.sym->as)
2380
        {
2381
          switch (e->symtree->n.sym->as->type)
2382
            {
2383
              case AS_ASSUMED_SIZE:
2384
                gfc_error ("Assumed size array '%s' at %L is not permitted "
2385
                           "in an initialization expression",
2386
                           e->symtree->n.sym->name, &e->where);
2387
                break;
2388
 
2389
              case AS_ASSUMED_SHAPE:
2390
                gfc_error ("Assumed shape array '%s' at %L is not permitted "
2391
                           "in an initialization expression",
2392
                           e->symtree->n.sym->name, &e->where);
2393
                break;
2394
 
2395
              case AS_DEFERRED:
2396
                gfc_error ("Deferred array '%s' at %L is not permitted "
2397
                           "in an initialization expression",
2398
                           e->symtree->n.sym->name, &e->where);
2399
                break;
2400
 
2401
              case AS_EXPLICIT:
2402
                gfc_error ("Array '%s' at %L is a variable, which does "
2403
                           "not reduce to a constant expression",
2404
                           e->symtree->n.sym->name, &e->where);
2405
                break;
2406
 
2407
              default:
2408
                gcc_unreachable();
2409
          }
2410
        }
2411
      else
2412
        gfc_error ("Parameter '%s' at %L has not been declared or is "
2413
                   "a variable, which does not reduce to a constant "
2414
                   "expression", e->symtree->n.sym->name, &e->where);
2415
 
2416
      break;
2417
 
2418
    case EXPR_CONSTANT:
2419
    case EXPR_NULL:
2420
      t = SUCCESS;
2421
      break;
2422
 
2423
    case EXPR_SUBSTRING:
2424
      t = check_init_expr (e->ref->u.ss.start);
2425
      if (t == FAILURE)
2426
        break;
2427
 
2428
      t = check_init_expr (e->ref->u.ss.end);
2429
      if (t == SUCCESS)
2430
        t = gfc_simplify_expr (e, 0);
2431
 
2432
      break;
2433
 
2434
    case EXPR_STRUCTURE:
2435
      t = e->ts.is_iso_c ? SUCCESS : FAILURE;
2436
      if (t == SUCCESS)
2437
        break;
2438
 
2439
      t = check_alloc_comp_init (e);
2440
      if (t == FAILURE)
2441
        break;
2442
 
2443
      t = gfc_check_constructor (e, check_init_expr);
2444
      if (t == FAILURE)
2445
        break;
2446
 
2447
      break;
2448
 
2449
    case EXPR_ARRAY:
2450
      t = gfc_check_constructor (e, check_init_expr);
2451
      if (t == FAILURE)
2452
        break;
2453
 
2454
      t = gfc_expand_constructor (e);
2455
      if (t == FAILURE)
2456
        break;
2457
 
2458
      t = gfc_check_constructor_type (e);
2459
      break;
2460
 
2461
    default:
2462
      gfc_internal_error ("check_init_expr(): Unknown expression type");
2463
    }
2464
 
2465
  return t;
2466
}
2467
 
2468
/* Reduces a general expression to an initialization expression (a constant).
2469
   This used to be part of gfc_match_init_expr.
2470
   Note that this function doesn't free the given expression on FAILURE.  */
2471
 
2472
gfc_try
2473
gfc_reduce_init_expr (gfc_expr *expr)
2474
{
2475
  gfc_try t;
2476
 
2477
  gfc_init_expr = 1;
2478
  t = gfc_resolve_expr (expr);
2479
  if (t == SUCCESS)
2480
    t = check_init_expr (expr);
2481
  gfc_init_expr = 0;
2482
 
2483
  if (t == FAILURE)
2484
    return FAILURE;
2485
 
2486
  if (expr->expr_type == EXPR_ARRAY)
2487
    {
2488
      if (gfc_check_constructor_type (expr) == FAILURE)
2489
        return FAILURE;
2490
      if (gfc_expand_constructor (expr) == FAILURE)
2491
        return FAILURE;
2492
    }
2493
 
2494
  return SUCCESS;
2495
}
2496
 
2497
 
2498
/* Match an initialization expression.  We work by first matching an
2499
   expression, then reducing it to a constant.  The reducing it to
2500
   constant part requires a global variable to flag the prohibition
2501
   of a non-integer exponent in -std=f95 mode.  */
2502
 
2503
bool init_flag = false;
2504
 
2505
match
2506
gfc_match_init_expr (gfc_expr **result)
2507
{
2508
  gfc_expr *expr;
2509
  match m;
2510
  gfc_try t;
2511
 
2512
  expr = NULL;
2513
 
2514
  init_flag = true;
2515
 
2516
  m = gfc_match_expr (&expr);
2517
  if (m != MATCH_YES)
2518
    {
2519
      init_flag = false;
2520
      return m;
2521
    }
2522
 
2523
  t = gfc_reduce_init_expr (expr);
2524
  if (t != SUCCESS)
2525
    {
2526
      gfc_free_expr (expr);
2527
      init_flag = false;
2528
      return MATCH_ERROR;
2529
    }
2530
 
2531
  *result = expr;
2532
  init_flag = false;
2533
 
2534
  return MATCH_YES;
2535
}
2536
 
2537
 
2538
/* Given an actual argument list, test to see that each argument is a
2539
   restricted expression and optionally if the expression type is
2540
   integer or character.  */
2541
 
2542
static gfc_try
2543
restricted_args (gfc_actual_arglist *a)
2544
{
2545
  for (; a; a = a->next)
2546
    {
2547
      if (check_restricted (a->expr) == FAILURE)
2548
        return FAILURE;
2549
    }
2550
 
2551
  return SUCCESS;
2552
}
2553
 
2554
 
2555
/************* Restricted/specification expressions *************/
2556
 
2557
 
2558
/* Make sure a non-intrinsic function is a specification function.  */
2559
 
2560
static gfc_try
2561
external_spec_function (gfc_expr *e)
2562
{
2563
  gfc_symbol *f;
2564
 
2565
  f = e->value.function.esym;
2566
 
2567
  if (f->attr.proc == PROC_ST_FUNCTION)
2568
    {
2569
      gfc_error ("Specification function '%s' at %L cannot be a statement "
2570
                 "function", f->name, &e->where);
2571
      return FAILURE;
2572
    }
2573
 
2574
  if (f->attr.proc == PROC_INTERNAL)
2575
    {
2576
      gfc_error ("Specification function '%s' at %L cannot be an internal "
2577
                 "function", f->name, &e->where);
2578
      return FAILURE;
2579
    }
2580
 
2581
  if (!f->attr.pure && !f->attr.elemental)
2582
    {
2583
      gfc_error ("Specification function '%s' at %L must be PURE", f->name,
2584
                 &e->where);
2585
      return FAILURE;
2586
    }
2587
 
2588
  if (f->attr.recursive)
2589
    {
2590
      gfc_error ("Specification function '%s' at %L cannot be RECURSIVE",
2591
                 f->name, &e->where);
2592
      return FAILURE;
2593
    }
2594
 
2595
  return restricted_args (e->value.function.actual);
2596
}
2597
 
2598
 
2599
/* Check to see that a function reference to an intrinsic is a
2600
   restricted expression.  */
2601
 
2602
static gfc_try
2603
restricted_intrinsic (gfc_expr *e)
2604
{
2605
  /* TODO: Check constraints on inquiry functions.  7.1.6.2 (7).  */
2606
  if (check_inquiry (e, 0) == MATCH_YES)
2607
    return SUCCESS;
2608
 
2609
  return restricted_args (e->value.function.actual);
2610
}
2611
 
2612
 
2613
/* Check the expressions of an actual arglist.  Used by check_restricted.  */
2614
 
2615
static gfc_try
2616
check_arglist (gfc_actual_arglist* arg, gfc_try (*checker) (gfc_expr*))
2617
{
2618
  for (; arg; arg = arg->next)
2619
    if (checker (arg->expr) == FAILURE)
2620
      return FAILURE;
2621
 
2622
  return SUCCESS;
2623
}
2624
 
2625
 
2626
/* Check the subscription expressions of a reference chain with a checking
2627
   function; used by check_restricted.  */
2628
 
2629
static gfc_try
2630
check_references (gfc_ref* ref, gfc_try (*checker) (gfc_expr*))
2631
{
2632
  int dim;
2633
 
2634
  if (!ref)
2635
    return SUCCESS;
2636
 
2637
  switch (ref->type)
2638
    {
2639
    case REF_ARRAY:
2640
      for (dim = 0; dim != ref->u.ar.dimen; ++dim)
2641
        {
2642
          if (checker (ref->u.ar.start[dim]) == FAILURE)
2643
            return FAILURE;
2644
          if (checker (ref->u.ar.end[dim]) == FAILURE)
2645
            return FAILURE;
2646
          if (checker (ref->u.ar.stride[dim]) == FAILURE)
2647
            return FAILURE;
2648
        }
2649
      break;
2650
 
2651
    case REF_COMPONENT:
2652
      /* Nothing needed, just proceed to next reference.  */
2653
      break;
2654
 
2655
    case REF_SUBSTRING:
2656
      if (checker (ref->u.ss.start) == FAILURE)
2657
        return FAILURE;
2658
      if (checker (ref->u.ss.end) == FAILURE)
2659
        return FAILURE;
2660
      break;
2661
 
2662
    default:
2663
      gcc_unreachable ();
2664
      break;
2665
    }
2666
 
2667
  return check_references (ref->next, checker);
2668
}
2669
 
2670
 
2671
/* Verify that an expression is a restricted expression.  Like its
2672
   cousin check_init_expr(), an error message is generated if we
2673
   return FAILURE.  */
2674
 
2675
static gfc_try
2676
check_restricted (gfc_expr *e)
2677
{
2678
  gfc_symbol* sym;
2679
  gfc_try t;
2680
 
2681
  if (e == NULL)
2682
    return SUCCESS;
2683
 
2684
  switch (e->expr_type)
2685
    {
2686
    case EXPR_OP:
2687
      t = check_intrinsic_op (e, check_restricted);
2688
      if (t == SUCCESS)
2689
        t = gfc_simplify_expr (e, 0);
2690
 
2691
      break;
2692
 
2693
    case EXPR_FUNCTION:
2694
      if (e->value.function.esym)
2695
        {
2696
          t = check_arglist (e->value.function.actual, &check_restricted);
2697
          if (t == SUCCESS)
2698
            t = external_spec_function (e);
2699
        }
2700
      else
2701
        {
2702
          if (e->value.function.isym && e->value.function.isym->inquiry)
2703
            t = SUCCESS;
2704
          else
2705
            t = check_arglist (e->value.function.actual, &check_restricted);
2706
 
2707
          if (t == SUCCESS)
2708
            t = restricted_intrinsic (e);
2709
        }
2710
      break;
2711
 
2712
    case EXPR_VARIABLE:
2713
      sym = e->symtree->n.sym;
2714
      t = FAILURE;
2715
 
2716
      /* If a dummy argument appears in a context that is valid for a
2717
         restricted expression in an elemental procedure, it will have
2718
         already been simplified away once we get here.  Therefore we
2719
         don't need to jump through hoops to distinguish valid from
2720
         invalid cases.  */
2721
      if (sym->attr.dummy && sym->ns == gfc_current_ns
2722
          && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
2723
        {
2724
          gfc_error ("Dummy argument '%s' not allowed in expression at %L",
2725
                     sym->name, &e->where);
2726
          break;
2727
        }
2728
 
2729
      if (sym->attr.optional)
2730
        {
2731
          gfc_error ("Dummy argument '%s' at %L cannot be OPTIONAL",
2732
                     sym->name, &e->where);
2733
          break;
2734
        }
2735
 
2736
      if (sym->attr.intent == INTENT_OUT)
2737
        {
2738
          gfc_error ("Dummy argument '%s' at %L cannot be INTENT(OUT)",
2739
                     sym->name, &e->where);
2740
          break;
2741
        }
2742
 
2743
      /* Check reference chain if any.  */
2744
      if (check_references (e->ref, &check_restricted) == FAILURE)
2745
        break;
2746
 
2747
      /* gfc_is_formal_arg broadcasts that a formal argument list is being
2748
         processed in resolve.c(resolve_formal_arglist).  This is done so
2749
         that host associated dummy array indices are accepted (PR23446).
2750
         This mechanism also does the same for the specification expressions
2751
         of array-valued functions.  */
2752
      if (e->error
2753
            || sym->attr.in_common
2754
            || sym->attr.use_assoc
2755
            || sym->attr.dummy
2756
            || sym->attr.implied_index
2757
            || sym->attr.flavor == FL_PARAMETER
2758
            || (sym->ns && sym->ns == gfc_current_ns->parent)
2759
            || (sym->ns && gfc_current_ns->parent
2760
                  && sym->ns == gfc_current_ns->parent->parent)
2761
            || (sym->ns->proc_name != NULL
2762
                  && sym->ns->proc_name->attr.flavor == FL_MODULE)
2763
            || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
2764
        {
2765
          t = SUCCESS;
2766
          break;
2767
        }
2768
 
2769
      gfc_error ("Variable '%s' cannot appear in the expression at %L",
2770
                 sym->name, &e->where);
2771
      /* Prevent a repetition of the error.  */
2772
      e->error = 1;
2773
      break;
2774
 
2775
    case EXPR_NULL:
2776
    case EXPR_CONSTANT:
2777
      t = SUCCESS;
2778
      break;
2779
 
2780
    case EXPR_SUBSTRING:
2781
      t = gfc_specification_expr (e->ref->u.ss.start);
2782
      if (t == FAILURE)
2783
        break;
2784
 
2785
      t = gfc_specification_expr (e->ref->u.ss.end);
2786
      if (t == SUCCESS)
2787
        t = gfc_simplify_expr (e, 0);
2788
 
2789
      break;
2790
 
2791
    case EXPR_STRUCTURE:
2792
      t = gfc_check_constructor (e, check_restricted);
2793
      break;
2794
 
2795
    case EXPR_ARRAY:
2796
      t = gfc_check_constructor (e, check_restricted);
2797
      break;
2798
 
2799
    default:
2800
      gfc_internal_error ("check_restricted(): Unknown expression type");
2801
    }
2802
 
2803
  return t;
2804
}
2805
 
2806
 
2807
/* Check to see that an expression is a specification expression.  If
2808
   we return FAILURE, an error has been generated.  */
2809
 
2810
gfc_try
2811
gfc_specification_expr (gfc_expr *e)
2812
{
2813
  gfc_component *comp;
2814
 
2815
  if (e == NULL)
2816
    return SUCCESS;
2817
 
2818
  if (e->ts.type != BT_INTEGER)
2819
    {
2820
      gfc_error ("Expression at %L must be of INTEGER type, found %s",
2821
                 &e->where, gfc_basic_typename (e->ts.type));
2822
      return FAILURE;
2823
    }
2824
 
2825
  if (e->expr_type == EXPR_FUNCTION
2826
          && !e->value.function.isym
2827
          && !e->value.function.esym
2828
          && !gfc_pure (e->symtree->n.sym)
2829
          && (!gfc_is_proc_ptr_comp (e, &comp)
2830
              || !comp-> attr.pure))
2831
    {
2832
      gfc_error ("Function '%s' at %L must be PURE",
2833
                 e->symtree->n.sym->name, &e->where);
2834
      /* Prevent repeat error messages.  */
2835
      e->symtree->n.sym->attr.pure = 1;
2836
      return FAILURE;
2837
    }
2838
 
2839
  if (e->rank != 0)
2840
    {
2841
      gfc_error ("Expression at %L must be scalar", &e->where);
2842
      return FAILURE;
2843
    }
2844
 
2845
  if (gfc_simplify_expr (e, 0) == FAILURE)
2846
    return FAILURE;
2847
 
2848
  return check_restricted (e);
2849
}
2850
 
2851
 
2852
/************** Expression conformance checks.  *************/
2853
 
2854
/* Given two expressions, make sure that the arrays are conformable.  */
2855
 
2856
gfc_try
2857
gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
2858
{
2859
  int op1_flag, op2_flag, d;
2860
  mpz_t op1_size, op2_size;
2861
  gfc_try t;
2862
 
2863
  va_list argp;
2864
  char buffer[240];
2865
 
2866
  if (op1->rank == 0 || op2->rank == 0)
2867
    return SUCCESS;
2868
 
2869
  va_start (argp, optype_msgid);
2870
  vsnprintf (buffer, 240, optype_msgid, argp);
2871
  va_end (argp);
2872
 
2873
  if (op1->rank != op2->rank)
2874
    {
2875
      gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
2876
                 op1->rank, op2->rank, &op1->where);
2877
      return FAILURE;
2878
    }
2879
 
2880
  t = SUCCESS;
2881
 
2882
  for (d = 0; d < op1->rank; d++)
2883
    {
2884
      op1_flag = gfc_array_dimen_size (op1, d, &op1_size) == SUCCESS;
2885
      op2_flag = gfc_array_dimen_size (op2, d, &op2_size) == SUCCESS;
2886
 
2887
      if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
2888
        {
2889
          gfc_error ("Different shape for %s at %L on dimension %d "
2890
                     "(%d and %d)", _(buffer), &op1->where, d + 1,
2891
                     (int) mpz_get_si (op1_size),
2892
                     (int) mpz_get_si (op2_size));
2893
 
2894
          t = FAILURE;
2895
        }
2896
 
2897
      if (op1_flag)
2898
        mpz_clear (op1_size);
2899
      if (op2_flag)
2900
        mpz_clear (op2_size);
2901
 
2902
      if (t == FAILURE)
2903
        return FAILURE;
2904
    }
2905
 
2906
  return SUCCESS;
2907
}
2908
 
2909
 
2910
/* Given an assignable expression and an arbitrary expression, make
2911
   sure that the assignment can take place.  */
2912
 
2913
gfc_try
2914
gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
2915
{
2916
  gfc_symbol *sym;
2917
  gfc_ref *ref;
2918
  int has_pointer;
2919
 
2920
  sym = lvalue->symtree->n.sym;
2921
 
2922
  /* Check INTENT(IN), unless the object itself is the component or
2923
     sub-component of a pointer.  */
2924
  has_pointer = sym->attr.pointer;
2925
 
2926
  for (ref = lvalue->ref; ref; ref = ref->next)
2927
    if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
2928
      {
2929
        has_pointer = 1;
2930
        break;
2931
      }
2932
 
2933
  if (!has_pointer && sym->attr.intent == INTENT_IN)
2934
    {
2935
      gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
2936
                 sym->name, &lvalue->where);
2937
      return FAILURE;
2938
    }
2939
 
2940
  /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
2941
     variable local to a function subprogram.  Its existence begins when
2942
     execution of the function is initiated and ends when execution of the
2943
     function is terminated...
2944
     Therefore, the left hand side is no longer a variable, when it is:  */
2945
  if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
2946
      && !sym->attr.external)
2947
    {
2948
      bool bad_proc;
2949
      bad_proc = false;
2950
 
2951
      /* (i) Use associated;  */
2952
      if (sym->attr.use_assoc)
2953
        bad_proc = true;
2954
 
2955
      /* (ii) The assignment is in the main program; or  */
2956
      if (gfc_current_ns->proc_name->attr.is_main_program)
2957
        bad_proc = true;
2958
 
2959
      /* (iii) A module or internal procedure...  */
2960
      if ((gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
2961
           || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
2962
          && gfc_current_ns->parent
2963
          && (!(gfc_current_ns->parent->proc_name->attr.function
2964
                || gfc_current_ns->parent->proc_name->attr.subroutine)
2965
              || gfc_current_ns->parent->proc_name->attr.is_main_program))
2966
        {
2967
          /* ... that is not a function...  */
2968
          if (!gfc_current_ns->proc_name->attr.function)
2969
            bad_proc = true;
2970
 
2971
          /* ... or is not an entry and has a different name.  */
2972
          if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
2973
            bad_proc = true;
2974
        }
2975
 
2976
      /* (iv) Host associated and not the function symbol or the
2977
              parent result.  This picks up sibling references, which
2978
              cannot be entries.  */
2979
      if (!sym->attr.entry
2980
            && sym->ns == gfc_current_ns->parent
2981
            && sym != gfc_current_ns->proc_name
2982
            && sym != gfc_current_ns->parent->proc_name->result)
2983
        bad_proc = true;
2984
 
2985
      if (bad_proc)
2986
        {
2987
          gfc_error ("'%s' at %L is not a VALUE", sym->name, &lvalue->where);
2988
          return FAILURE;
2989
        }
2990
    }
2991
 
2992
  if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
2993
    {
2994
      gfc_error ("Incompatible ranks %d and %d in assignment at %L",
2995
                 lvalue->rank, rvalue->rank, &lvalue->where);
2996
      return FAILURE;
2997
    }
2998
 
2999
  if (lvalue->ts.type == BT_UNKNOWN)
3000
    {
3001
      gfc_error ("Variable type is UNKNOWN in assignment at %L",
3002
                 &lvalue->where);
3003
      return FAILURE;
3004
    }
3005
 
3006
  if (rvalue->expr_type == EXPR_NULL)
3007
    {
3008
      if (has_pointer && (ref == NULL || ref->next == NULL)
3009
          && lvalue->symtree->n.sym->attr.data)
3010
        return SUCCESS;
3011
      else
3012
        {
3013
          gfc_error ("NULL appears on right-hand side in assignment at %L",
3014
                     &rvalue->where);
3015
          return FAILURE;
3016
        }
3017
    }
3018
 
3019
  /* This is possibly a typo: x = f() instead of x => f().  */
3020
  if (gfc_option.warn_surprising
3021
      && rvalue->expr_type == EXPR_FUNCTION
3022
      && rvalue->symtree->n.sym->attr.pointer)
3023
    gfc_warning ("POINTER valued function appears on right-hand side of "
3024
                 "assignment at %L", &rvalue->where);
3025
 
3026
  /* Check size of array assignments.  */
3027
  if (lvalue->rank != 0 && rvalue->rank != 0
3028
      && gfc_check_conformance (lvalue, rvalue, "array assignment") != SUCCESS)
3029
    return FAILURE;
3030
 
3031
  if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER
3032
      && lvalue->symtree->n.sym->attr.data
3033
      && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L used to "
3034
                         "initialize non-integer variable '%s'",
3035
                         &rvalue->where, lvalue->symtree->n.sym->name)
3036
         == FAILURE)
3037
    return FAILURE;
3038
  else if (rvalue->is_boz && !lvalue->symtree->n.sym->attr.data
3039
      && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
3040
                         "a DATA statement and outside INT/REAL/DBLE/CMPLX",
3041
                         &rvalue->where) == FAILURE)
3042
    return FAILURE;
3043
 
3044
  /* Handle the case of a BOZ literal on the RHS.  */
3045
  if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER)
3046
    {
3047
      int rc;
3048
      if (gfc_option.warn_surprising)
3049
        gfc_warning ("BOZ literal at %L is bitwise transferred "
3050
                     "non-integer symbol '%s'", &rvalue->where,
3051
                     lvalue->symtree->n.sym->name);
3052
      if (!gfc_convert_boz (rvalue, &lvalue->ts))
3053
        return FAILURE;
3054
      if ((rc = gfc_range_check (rvalue)) != ARITH_OK)
3055
        {
3056
          if (rc == ARITH_UNDERFLOW)
3057
            gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
3058
                       ". This check can be disabled with the option "
3059
                       "-fno-range-check", &rvalue->where);
3060
          else if (rc == ARITH_OVERFLOW)
3061
            gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
3062
                       ". This check can be disabled with the option "
3063
                       "-fno-range-check", &rvalue->where);
3064
          else if (rc == ARITH_NAN)
3065
            gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
3066
                       ". This check can be disabled with the option "
3067
                       "-fno-range-check", &rvalue->where);
3068
          return FAILURE;
3069
        }
3070
    }
3071
 
3072
  if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
3073
    return SUCCESS;
3074
 
3075
  /* Only DATA Statements come here.  */
3076
  if (!conform)
3077
    {
3078
      /* Numeric can be converted to any other numeric. And Hollerith can be
3079
         converted to any other type.  */
3080
      if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
3081
          || rvalue->ts.type == BT_HOLLERITH)
3082
        return SUCCESS;
3083
 
3084
      if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
3085
        return SUCCESS;
3086
 
3087
      gfc_error ("Incompatible types in DATA statement at %L; attempted "
3088
                 "conversion of %s to %s", &lvalue->where,
3089
                 gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
3090
 
3091
      return FAILURE;
3092
    }
3093
 
3094
  /* Assignment is the only case where character variables of different
3095
     kind values can be converted into one another.  */
3096
  if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
3097
    {
3098
      if (lvalue->ts.kind != rvalue->ts.kind)
3099
        gfc_convert_chartype (rvalue, &lvalue->ts);
3100
 
3101
      return SUCCESS;
3102
    }
3103
 
3104
  return gfc_convert_type (rvalue, &lvalue->ts, 1);
3105
}
3106
 
3107
 
3108
/* Check that a pointer assignment is OK.  We first check lvalue, and
3109
   we only check rvalue if it's not an assignment to NULL() or a
3110
   NULLIFY statement.  */
3111
 
3112
gfc_try
3113
gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
3114
{
3115
  symbol_attribute attr;
3116
  gfc_ref *ref;
3117
  int is_pure;
3118
  int pointer, check_intent_in, proc_pointer;
3119
 
3120
  if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN
3121
      && !lvalue->symtree->n.sym->attr.proc_pointer)
3122
    {
3123
      gfc_error ("Pointer assignment target is not a POINTER at %L",
3124
                 &lvalue->where);
3125
      return FAILURE;
3126
    }
3127
 
3128
  if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
3129
      && lvalue->symtree->n.sym->attr.use_assoc
3130
      && !lvalue->symtree->n.sym->attr.proc_pointer)
3131
    {
3132
      gfc_error ("'%s' in the pointer assignment at %L cannot be an "
3133
                 "l-value since it is a procedure",
3134
                 lvalue->symtree->n.sym->name, &lvalue->where);
3135
      return FAILURE;
3136
    }
3137
 
3138
 
3139
  /* Check INTENT(IN), unless the object itself is the component or
3140
     sub-component of a pointer.  */
3141
  check_intent_in = 1;
3142
  pointer = lvalue->symtree->n.sym->attr.pointer;
3143
  proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
3144
 
3145
  for (ref = lvalue->ref; ref; ref = ref->next)
3146
    {
3147
      if (pointer)
3148
        check_intent_in = 0;
3149
 
3150
      if (ref->type == REF_COMPONENT)
3151
        {
3152
          pointer = ref->u.c.component->attr.pointer;
3153
          proc_pointer = ref->u.c.component->attr.proc_pointer;
3154
        }
3155
 
3156
      if (ref->type == REF_ARRAY && ref->next == NULL)
3157
        {
3158
          if (ref->u.ar.type == AR_FULL)
3159
            break;
3160
 
3161
          if (ref->u.ar.type != AR_SECTION)
3162
            {
3163
              gfc_error ("Expected bounds specification for '%s' at %L",
3164
                         lvalue->symtree->n.sym->name, &lvalue->where);
3165
              return FAILURE;
3166
            }
3167
 
3168
          if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Bounds "
3169
                              "specification for '%s' in pointer assignment "
3170
                              "at %L", lvalue->symtree->n.sym->name,
3171
                              &lvalue->where) == FAILURE)
3172
            return FAILURE;
3173
 
3174
          gfc_error ("Pointer bounds remapping at %L is not yet implemented "
3175
                     "in gfortran", &lvalue->where);
3176
          /* TODO: See PR 29785. Add checks that all lbounds are specified and
3177
             either never or always the upper-bound; strides shall not be
3178
             present.  */
3179
          return FAILURE;
3180
        }
3181
    }
3182
 
3183
  if (check_intent_in && lvalue->symtree->n.sym->attr.intent == INTENT_IN)
3184
    {
3185
      gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
3186
                 lvalue->symtree->n.sym->name, &lvalue->where);
3187
      return FAILURE;
3188
    }
3189
 
3190
  if (!pointer && !proc_pointer
3191
        && !(lvalue->ts.type == BT_CLASS
3192
                && lvalue->ts.u.derived->components->attr.pointer))
3193
    {
3194
      gfc_error ("Pointer assignment to non-POINTER at %L", &lvalue->where);
3195
      return FAILURE;
3196
    }
3197
 
3198
  is_pure = gfc_pure (NULL);
3199
 
3200
  if (is_pure && gfc_impure_variable (lvalue->symtree->n.sym)
3201
        && lvalue->symtree->n.sym->value != rvalue)
3202
    {
3203
      gfc_error ("Bad pointer object in PURE procedure at %L", &lvalue->where);
3204
      return FAILURE;
3205
    }
3206
 
3207
  /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
3208
     kind, etc for lvalue and rvalue must match, and rvalue must be a
3209
     pure variable if we're in a pure function.  */
3210
  if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
3211
    return SUCCESS;
3212
 
3213
  /* Checks on rvalue for procedure pointer assignments.  */
3214
  if (proc_pointer)
3215
    {
3216
      char err[200];
3217
      gfc_symbol *s1,*s2;
3218
      gfc_component *comp;
3219
      const char *name;
3220
 
3221
      attr = gfc_expr_attr (rvalue);
3222
      if (!((rvalue->expr_type == EXPR_NULL)
3223
            || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
3224
            || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
3225
            || (rvalue->expr_type == EXPR_VARIABLE
3226
                && attr.flavor == FL_PROCEDURE)))
3227
        {
3228
          gfc_error ("Invalid procedure pointer assignment at %L",
3229
                     &rvalue->where);
3230
          return FAILURE;
3231
        }
3232
      if (attr.abstract)
3233
        {
3234
          gfc_error ("Abstract interface '%s' is invalid "
3235
                     "in procedure pointer assignment at %L",
3236
                     rvalue->symtree->name, &rvalue->where);
3237
          return FAILURE;
3238
        }
3239
      /* Check for C727.  */
3240
      if (attr.flavor == FL_PROCEDURE)
3241
        {
3242
          if (attr.proc == PROC_ST_FUNCTION)
3243
            {
3244
              gfc_error ("Statement function '%s' is invalid "
3245
                         "in procedure pointer assignment at %L",
3246
                         rvalue->symtree->name, &rvalue->where);
3247
              return FAILURE;
3248
            }
3249
          if (attr.proc == PROC_INTERNAL &&
3250
              gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is "
3251
                              "invalid in procedure pointer assignment at %L",
3252
                              rvalue->symtree->name, &rvalue->where) == FAILURE)
3253
            return FAILURE;
3254
        }
3255
 
3256
      /* Ensure that the calling convention is the same. As other attributes
3257
         such as DLLEXPORT may differ, one explicitly only tests for the
3258
         calling conventions.  */
3259
      if (rvalue->expr_type == EXPR_VARIABLE
3260
          && lvalue->symtree->n.sym->attr.ext_attr
3261
               != rvalue->symtree->n.sym->attr.ext_attr)
3262
        {
3263
          symbol_attribute calls;
3264
 
3265
          calls.ext_attr = 0;
3266
          gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
3267
          gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
3268
          gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
3269
 
3270
          if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
3271
              != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
3272
            {
3273
              gfc_error ("Mismatch in the procedure pointer assignment "
3274
                         "at %L: mismatch in the calling convention",
3275
                         &rvalue->where);
3276
          return FAILURE;
3277
            }
3278
        }
3279
 
3280
      if (gfc_is_proc_ptr_comp (lvalue, &comp))
3281
        s1 = comp->ts.interface;
3282
      else
3283
        s1 = lvalue->symtree->n.sym;
3284
 
3285
      if (gfc_is_proc_ptr_comp (rvalue, &comp))
3286
        {
3287
          s2 = comp->ts.interface;
3288
          name = comp->name;
3289
        }
3290
      else if (rvalue->expr_type == EXPR_FUNCTION)
3291
        {
3292
          s2 = rvalue->symtree->n.sym->result;
3293
          name = rvalue->symtree->n.sym->result->name;
3294
        }
3295
      else
3296
        {
3297
          s2 = rvalue->symtree->n.sym;
3298
          name = rvalue->symtree->n.sym->name;
3299
        }
3300
 
3301
      if (s1 && s2 && !gfc_compare_interfaces (s1, s2, name, 0, 1,
3302
                                               err, sizeof(err)))
3303
        {
3304
          gfc_error ("Interface mismatch in procedure pointer assignment "
3305
                     "at %L: %s", &rvalue->where, err);
3306
          return FAILURE;
3307
        }
3308
 
3309
      return SUCCESS;
3310
    }
3311
 
3312
  if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
3313
    {
3314
      gfc_error ("Different types in pointer assignment at %L; attempted "
3315
                 "assignment of %s to %s", &lvalue->where,
3316
                 gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
3317
      return FAILURE;
3318
    }
3319
 
3320
  if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
3321
    {
3322
      gfc_error ("Different kind type parameters in pointer "
3323
                 "assignment at %L", &lvalue->where);
3324
      return FAILURE;
3325
    }
3326
 
3327
  if (lvalue->rank != rvalue->rank)
3328
    {
3329
      gfc_error ("Different ranks in pointer assignment at %L",
3330
                 &lvalue->where);
3331
      return FAILURE;
3332
    }
3333
 
3334
  /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X).  */
3335
  if (rvalue->expr_type == EXPR_NULL)
3336
    return SUCCESS;
3337
 
3338
  if (lvalue->ts.type == BT_CHARACTER)
3339
    {
3340
      gfc_try t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
3341
      if (t == FAILURE)
3342
        return FAILURE;
3343
    }
3344
 
3345
  if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
3346
    lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
3347
 
3348
  attr = gfc_expr_attr (rvalue);
3349
  if (!attr.target && !attr.pointer)
3350
    {
3351
      gfc_error ("Pointer assignment target is neither TARGET "
3352
                 "nor POINTER at %L", &rvalue->where);
3353
      return FAILURE;
3354
    }
3355
 
3356
  if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
3357
    {
3358
      gfc_error ("Bad target in pointer assignment in PURE "
3359
                 "procedure at %L", &rvalue->where);
3360
    }
3361
 
3362
  if (gfc_has_vector_index (rvalue))
3363
    {
3364
      gfc_error ("Pointer assignment with vector subscript "
3365
                 "on rhs at %L", &rvalue->where);
3366
      return FAILURE;
3367
    }
3368
 
3369
  if (attr.is_protected && attr.use_assoc
3370
      && !(attr.pointer || attr.proc_pointer))
3371
    {
3372
      gfc_error ("Pointer assignment target has PROTECTED "
3373
                 "attribute at %L", &rvalue->where);
3374
      return FAILURE;
3375
    }
3376
 
3377
  return SUCCESS;
3378
}
3379
 
3380
 
3381
/* Relative of gfc_check_assign() except that the lvalue is a single
3382
   symbol.  Used for initialization assignments.  */
3383
 
3384
gfc_try
3385
gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
3386
{
3387
  gfc_expr lvalue;
3388
  gfc_try r;
3389
 
3390
  memset (&lvalue, '\0', sizeof (gfc_expr));
3391
 
3392
  lvalue.expr_type = EXPR_VARIABLE;
3393
  lvalue.ts = sym->ts;
3394
  if (sym->as)
3395
    lvalue.rank = sym->as->rank;
3396
  lvalue.symtree = (gfc_symtree *) gfc_getmem (sizeof (gfc_symtree));
3397
  lvalue.symtree->n.sym = sym;
3398
  lvalue.where = sym->declared_at;
3399
 
3400
  if (sym->attr.pointer || sym->attr.proc_pointer
3401
      || (sym->ts.type == BT_CLASS
3402
          && sym->ts.u.derived->components->attr.pointer
3403
          && rvalue->expr_type == EXPR_NULL))
3404
    r = gfc_check_pointer_assign (&lvalue, rvalue);
3405
  else
3406
    r = gfc_check_assign (&lvalue, rvalue, 1);
3407
 
3408
  gfc_free (lvalue.symtree);
3409
 
3410
  return r;
3411
}
3412
 
3413
 
3414
/* Get an expression for a default initializer.  */
3415
 
3416
gfc_expr *
3417
gfc_default_initializer (gfc_typespec *ts)
3418
{
3419
  gfc_constructor *tail;
3420
  gfc_expr *init;
3421
  gfc_component *c;
3422
 
3423
  /* See if we have a default initializer.  */
3424
  for (c = ts->u.derived->components; c; c = c->next)
3425
    if (c->initializer || c->attr.allocatable)
3426
      break;
3427
 
3428
  if (!c)
3429
    return NULL;
3430
 
3431
  /* Build the constructor.  */
3432
  init = gfc_get_expr ();
3433
  init->expr_type = EXPR_STRUCTURE;
3434
  init->ts = *ts;
3435
  init->where = ts->u.derived->declared_at;
3436
 
3437
  tail = NULL;
3438
  for (c = ts->u.derived->components; c; c = c->next)
3439
    {
3440
      if (tail == NULL)
3441
        init->value.constructor = tail = gfc_get_constructor ();
3442
      else
3443
        {
3444
          tail->next = gfc_get_constructor ();
3445
          tail = tail->next;
3446
        }
3447
 
3448
      if (c->initializer)
3449
        tail->expr = gfc_copy_expr (c->initializer);
3450
 
3451
      if (c->attr.allocatable)
3452
        {
3453
          tail->expr = gfc_get_expr ();
3454
          tail->expr->expr_type = EXPR_NULL;
3455
          tail->expr->ts = c->ts;
3456
        }
3457
    }
3458
  return init;
3459
}
3460
 
3461
 
3462
/* Given a symbol, create an expression node with that symbol as a
3463
   variable. If the symbol is array valued, setup a reference of the
3464
   whole array.  */
3465
 
3466
gfc_expr *
3467
gfc_get_variable_expr (gfc_symtree *var)
3468
{
3469
  gfc_expr *e;
3470
 
3471
  e = gfc_get_expr ();
3472
  e->expr_type = EXPR_VARIABLE;
3473
  e->symtree = var;
3474
  e->ts = var->n.sym->ts;
3475
 
3476
  if (var->n.sym->as != NULL)
3477
    {
3478
      e->rank = var->n.sym->as->rank;
3479
      e->ref = gfc_get_ref ();
3480
      e->ref->type = REF_ARRAY;
3481
      e->ref->u.ar.type = AR_FULL;
3482
    }
3483
 
3484
  return e;
3485
}
3486
 
3487
 
3488
/* Returns the array_spec of a full array expression.  A NULL is
3489
   returned otherwise.  */
3490
gfc_array_spec *
3491
gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
3492
{
3493
  gfc_array_spec *as;
3494
  gfc_ref *ref;
3495
 
3496
  if (expr->rank == 0)
3497
    return NULL;
3498
 
3499
  /* Follow any component references.  */
3500
  if (expr->expr_type == EXPR_VARIABLE
3501
      || expr->expr_type == EXPR_CONSTANT)
3502
    {
3503
      as = expr->symtree->n.sym->as;
3504
      for (ref = expr->ref; ref; ref = ref->next)
3505
        {
3506
          switch (ref->type)
3507
            {
3508
            case REF_COMPONENT:
3509
              as = ref->u.c.component->as;
3510
              continue;
3511
 
3512
            case REF_SUBSTRING:
3513
              continue;
3514
 
3515
            case REF_ARRAY:
3516
              {
3517
                switch (ref->u.ar.type)
3518
                  {
3519
                  case AR_ELEMENT:
3520
                  case AR_SECTION:
3521
                  case AR_UNKNOWN:
3522
                    as = NULL;
3523
                    continue;
3524
 
3525
                  case AR_FULL:
3526
                    break;
3527
                  }
3528
                break;
3529
              }
3530
            }
3531
        }
3532
    }
3533
  else
3534
    as = NULL;
3535
 
3536
  return as;
3537
}
3538
 
3539
 
3540
/* General expression traversal function.  */
3541
 
3542
bool
3543
gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
3544
                   bool (*func)(gfc_expr *, gfc_symbol *, int*),
3545
                   int f)
3546
{
3547
  gfc_array_ref ar;
3548
  gfc_ref *ref;
3549
  gfc_actual_arglist *args;
3550
  gfc_constructor *c;
3551
  int i;
3552
 
3553
  if (!expr)
3554
    return false;
3555
 
3556
  if ((*func) (expr, sym, &f))
3557
    return true;
3558
 
3559
  if (expr->ts.type == BT_CHARACTER
3560
        && expr->ts.u.cl
3561
        && expr->ts.u.cl->length
3562
        && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
3563
        && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
3564
    return true;
3565
 
3566
  switch (expr->expr_type)
3567
    {
3568
    case EXPR_PPC:
3569
    case EXPR_COMPCALL:
3570
    case EXPR_FUNCTION:
3571
      for (args = expr->value.function.actual; args; args = args->next)
3572
        {
3573
          if (gfc_traverse_expr (args->expr, sym, func, f))
3574
            return true;
3575
        }
3576
      break;
3577
 
3578
    case EXPR_VARIABLE:
3579
    case EXPR_CONSTANT:
3580
    case EXPR_NULL:
3581
    case EXPR_SUBSTRING:
3582
      break;
3583
 
3584
    case EXPR_STRUCTURE:
3585
    case EXPR_ARRAY:
3586
      for (c = expr->value.constructor; c; c = c->next)
3587
        {
3588
          if (gfc_traverse_expr (c->expr, sym, func, f))
3589
            return true;
3590
          if (c->iterator)
3591
            {
3592
              if (gfc_traverse_expr (c->iterator->var, sym, func, f))
3593
                return true;
3594
              if (gfc_traverse_expr (c->iterator->start, sym, func, f))
3595
                return true;
3596
              if (gfc_traverse_expr (c->iterator->end, sym, func, f))
3597
                return true;
3598
              if (gfc_traverse_expr (c->iterator->step, sym, func, f))
3599
                return true;
3600
            }
3601
        }
3602
      break;
3603
 
3604
    case EXPR_OP:
3605
      if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
3606
        return true;
3607
      if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
3608
        return true;
3609
      break;
3610
 
3611
    default:
3612
      gcc_unreachable ();
3613
      break;
3614
    }
3615
 
3616
  ref = expr->ref;
3617
  while (ref != NULL)
3618
    {
3619
      switch (ref->type)
3620
        {
3621
        case  REF_ARRAY:
3622
          ar = ref->u.ar;
3623
          for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3624
            {
3625
              if (gfc_traverse_expr (ar.start[i], sym, func, f))
3626
                return true;
3627
              if (gfc_traverse_expr (ar.end[i], sym, func, f))
3628
                return true;
3629
              if (gfc_traverse_expr (ar.stride[i], sym, func, f))
3630
                return true;
3631
            }
3632
          break;
3633
 
3634
        case REF_SUBSTRING:
3635
          if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
3636
            return true;
3637
          if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
3638
            return true;
3639
          break;
3640
 
3641
        case REF_COMPONENT:
3642
          if (ref->u.c.component->ts.type == BT_CHARACTER
3643
                && ref->u.c.component->ts.u.cl
3644
                && ref->u.c.component->ts.u.cl->length
3645
                && ref->u.c.component->ts.u.cl->length->expr_type
3646
                     != EXPR_CONSTANT
3647
                && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
3648
                                      sym, func, f))
3649
            return true;
3650
 
3651
          if (ref->u.c.component->as)
3652
            for (i = 0; i < ref->u.c.component->as->rank; i++)
3653
              {
3654
                if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
3655
                                       sym, func, f))
3656
                  return true;
3657
                if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
3658
                                       sym, func, f))
3659
                  return true;
3660
              }
3661
          break;
3662
 
3663
        default:
3664
          gcc_unreachable ();
3665
        }
3666
      ref = ref->next;
3667
    }
3668
  return false;
3669
}
3670
 
3671
/* Traverse expr, marking all EXPR_VARIABLE symbols referenced.  */
3672
 
3673
static bool
3674
expr_set_symbols_referenced (gfc_expr *expr,
3675
                             gfc_symbol *sym ATTRIBUTE_UNUSED,
3676
                             int *f ATTRIBUTE_UNUSED)
3677
{
3678
  if (expr->expr_type != EXPR_VARIABLE)
3679
    return false;
3680
  gfc_set_sym_referenced (expr->symtree->n.sym);
3681
  return false;
3682
}
3683
 
3684
void
3685
gfc_expr_set_symbols_referenced (gfc_expr *expr)
3686
{
3687
  gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
3688
}
3689
 
3690
 
3691
/* Determine if an expression is a procedure pointer component. If yes, the
3692
   argument 'comp' will point to the component (provided that 'comp' was
3693
   provided).  */
3694
 
3695
bool
3696
gfc_is_proc_ptr_comp (gfc_expr *expr, gfc_component **comp)
3697
{
3698
  gfc_ref *ref;
3699
  bool ppc = false;
3700
 
3701
  if (!expr || !expr->ref)
3702
    return false;
3703
 
3704
  ref = expr->ref;
3705
  while (ref->next)
3706
    ref = ref->next;
3707
 
3708
  if (ref->type == REF_COMPONENT)
3709
    {
3710
      ppc = ref->u.c.component->attr.proc_pointer;
3711
      if (ppc && comp)
3712
        *comp = ref->u.c.component;
3713
    }
3714
 
3715
  return ppc;
3716
}
3717
 
3718
 
3719
/* Walk an expression tree and check each variable encountered for being typed.
3720
   If strict is not set, a top-level variable is tolerated untyped in -std=gnu
3721
   mode as is a basic arithmetic expression using those; this is for things in
3722
   legacy-code like:
3723
 
3724
     INTEGER :: arr(n), n
3725
     INTEGER :: arr(n + 1), n
3726
 
3727
   The namespace is needed for IMPLICIT typing.  */
3728
 
3729
static gfc_namespace* check_typed_ns;
3730
 
3731
static bool
3732
expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
3733
                       int* f ATTRIBUTE_UNUSED)
3734
{
3735
  gfc_try t;
3736
 
3737
  if (e->expr_type != EXPR_VARIABLE)
3738
    return false;
3739
 
3740
  gcc_assert (e->symtree);
3741
  t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
3742
                              true, e->where);
3743
 
3744
  return (t == FAILURE);
3745
}
3746
 
3747
gfc_try
3748
gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
3749
{
3750
  bool error_found;
3751
 
3752
  /* If this is a top-level variable or EXPR_OP, do the check with strict given
3753
     to us.  */
3754
  if (!strict)
3755
    {
3756
      if (e->expr_type == EXPR_VARIABLE && !e->ref)
3757
        return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
3758
 
3759
      if (e->expr_type == EXPR_OP)
3760
        {
3761
          gfc_try t = SUCCESS;
3762
 
3763
          gcc_assert (e->value.op.op1);
3764
          t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
3765
 
3766
          if (t == SUCCESS && e->value.op.op2)
3767
            t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
3768
 
3769
          return t;
3770
        }
3771
    }
3772
 
3773
  /* Otherwise, walk the expression and do it strictly.  */
3774
  check_typed_ns = ns;
3775
  error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
3776
 
3777
  return error_found ? FAILURE : SUCCESS;
3778
}
3779
 
3780
/* Walk an expression tree and replace all symbols with a corresponding symbol
3781
   in the formal_ns of "sym". Needed for copying interfaces in PROCEDURE
3782
   statements. The boolean return value is required by gfc_traverse_expr.  */
3783
 
3784
static bool
3785
replace_symbol (gfc_expr *expr, gfc_symbol *sym, int *i ATTRIBUTE_UNUSED)
3786
{
3787
  if ((expr->expr_type == EXPR_VARIABLE
3788
       || (expr->expr_type == EXPR_FUNCTION
3789
           && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
3790
      && expr->symtree->n.sym->ns == sym->ts.interface->formal_ns)
3791
    {
3792
      gfc_symtree *stree;
3793
      gfc_namespace *ns = sym->formal_ns;
3794
      /* Don't use gfc_get_symtree as we prefer to fail badly if we don't find
3795
         the symtree rather than create a new one (and probably fail later).  */
3796
      stree = gfc_find_symtree (ns ? ns->sym_root : gfc_current_ns->sym_root,
3797
                                expr->symtree->n.sym->name);
3798
      gcc_assert (stree);
3799
      stree->n.sym->attr = expr->symtree->n.sym->attr;
3800
      expr->symtree = stree;
3801
    }
3802
  return false;
3803
}
3804
 
3805
void
3806
gfc_expr_replace_symbols (gfc_expr *expr, gfc_symbol *dest)
3807
{
3808
  gfc_traverse_expr (expr, dest, &replace_symbol, 0);
3809
}
3810
 
3811
/* The following is analogous to 'replace_symbol', and needed for copying
3812
   interfaces for procedure pointer components. The argument 'sym' must formally
3813
   be a gfc_symbol, so that the function can be passed to gfc_traverse_expr.
3814
   However, it gets actually passed a gfc_component (i.e. the procedure pointer
3815
   component in whose formal_ns the arguments have to be).  */
3816
 
3817
static bool
3818
replace_comp (gfc_expr *expr, gfc_symbol *sym, int *i ATTRIBUTE_UNUSED)
3819
{
3820
  gfc_component *comp;
3821
  comp = (gfc_component *)sym;
3822
  if ((expr->expr_type == EXPR_VARIABLE
3823
       || (expr->expr_type == EXPR_FUNCTION
3824
           && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
3825
      && expr->symtree->n.sym->ns == comp->ts.interface->formal_ns)
3826
    {
3827
      gfc_symtree *stree;
3828
      gfc_namespace *ns = comp->formal_ns;
3829
      /* Don't use gfc_get_symtree as we prefer to fail badly if we don't find
3830
         the symtree rather than create a new one (and probably fail later).  */
3831
      stree = gfc_find_symtree (ns ? ns->sym_root : gfc_current_ns->sym_root,
3832
                                expr->symtree->n.sym->name);
3833
      gcc_assert (stree);
3834
      stree->n.sym->attr = expr->symtree->n.sym->attr;
3835
      expr->symtree = stree;
3836
    }
3837
  return false;
3838
}
3839
 
3840
void
3841
gfc_expr_replace_comp (gfc_expr *expr, gfc_component *dest)
3842
{
3843
  gfc_traverse_expr (expr, (gfc_symbol *)dest, &replace_comp, 0);
3844
}
3845
 

powered by: WebSVN 2.1.0

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