expr.c 83.9 KB
Newer Older
1
/* Routines for manipulation of expression nodes.
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3
   Free Software Foundation, Inc.
4 5
   Contributed by Andy Vaught

6
This file is part of GCC.
7

8 9
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 3, or (at your option) any later
11
version.
12

13 14 15 16
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
17 18

You should have received a copy of the GNU General Public License
19 20
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
21 22

#include "config.h"
23
#include "system.h"
24 25 26
#include "gfortran.h"
#include "arith.h"
#include "match.h"
27
#include "target-memory.h" /* for gfc_convert_boz */
28 29 30 31 32 33 34 35

/* Get a new expr node.  */

gfc_expr *
gfc_get_expr (void)
{
  gfc_expr *e;

36
  e = XCNEW (gfc_expr);
37 38 39 40
  gfc_clear_ts (&e->ts);
  e->shape = NULL;
  e->ref = NULL;
  e->symtree = NULL;
41
  e->con_by_offset = NULL;
42 43 44 45 46 47 48
  return e;
}


/* Free an argument list and everything below it.  */

void
49
gfc_free_actual_arglist (gfc_actual_arglist *a1)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
  gfc_actual_arglist *a2;

  while (a1)
    {
      a2 = a1->next;
      gfc_free_expr (a1->expr);
      gfc_free (a1);
      a1 = a2;
    }
}


/* Copy an arglist structure and all of the arguments.  */

gfc_actual_arglist *
66
gfc_copy_actual_arglist (gfc_actual_arglist *p)
67
{
68
  gfc_actual_arglist *head, *tail, *new_arg;
69 70 71 72 73

  head = tail = NULL;

  for (; p; p = p->next)
    {
74 75
      new_arg = gfc_get_actual_arglist ();
      *new_arg = *p;
76

77 78
      new_arg->expr = gfc_copy_expr (p->expr);
      new_arg->next = NULL;
79 80

      if (head == NULL)
81
	head = new_arg;
82
      else
83
	tail->next = new_arg;
84

85
      tail = new_arg;
86 87 88 89 90 91 92 93 94
    }

  return head;
}


/* Free a list of reference structures.  */

void
95
gfc_free_ref_list (gfc_ref *p)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
{
  gfc_ref *q;
  int i;

  for (; p; p = q)
    {
      q = p->next;

      switch (p->type)
	{
	case REF_ARRAY:
	  for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
	    {
	      gfc_free_expr (p->u.ar.start[i]);
	      gfc_free_expr (p->u.ar.end[i]);
	      gfc_free_expr (p->u.ar.stride[i]);
	    }

	  break;

	case REF_SUBSTRING:
	  gfc_free_expr (p->u.ss.start);
	  gfc_free_expr (p->u.ss.end);
	  break;

	case REF_COMPONENT:
	  break;
	}

      gfc_free (p);
    }
}


/* Workhorse function for gfc_free_expr() that frees everything
   beneath an expression node, but not the node itself.  This is
   useful when we want to simplify a node and replace it with
   something else or the expression node belongs to another structure.  */

static void
136
free_expr0 (gfc_expr *e)
137 138 139 140 141 142
{
  int n;

  switch (e->expr_type)
    {
    case EXPR_CONSTANT:
143
      /* Free any parts of the value that need freeing.  */
144 145 146 147 148 149 150
      switch (e->ts.type)
	{
	case BT_INTEGER:
	  mpz_clear (e->value.integer);
	  break;

	case BT_REAL:
151
	  mpfr_clear (e->value.real);
152 153 154 155 156 157 158
	  break;

	case BT_CHARACTER:
	  gfc_free (e->value.character.string);
	  break;

	case BT_COMPLEX:
159 160
	  mpfr_clear (e->value.complex.r);
	  mpfr_clear (e->value.complex.i);
161 162 163 164 165 166
	  break;

	default:
	  break;
	}

167 168
      /* Free the representation.  */
      if (e->representation.string)
169 170
	gfc_free (e->representation.string);

171 172 173
      break;

    case EXPR_OP:
174 175 176 177
      if (e->value.op.op1 != NULL)
	gfc_free_expr (e->value.op.op1);
      if (e->value.op.op2 != NULL)
	gfc_free_expr (e->value.op.op2);
178 179 180 181 182 183
      break;

    case EXPR_FUNCTION:
      gfc_free_actual_arglist (e->value.function.actual);
      break;

184 185 186 187
    case EXPR_COMPCALL:
      gfc_free_actual_arglist (e->value.compcall.actual);
      break;

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    case EXPR_VARIABLE:
      break;

    case EXPR_ARRAY:
    case EXPR_STRUCTURE:
      gfc_free_constructor (e->value.constructor);
      break;

    case EXPR_SUBSTRING:
      gfc_free (e->value.character.string);
      break;

    case EXPR_NULL:
      break;

    default:
      gfc_internal_error ("free_expr0(): Bad expr type");
    }

  /* Free a shape array.  */
  if (e->shape != NULL)
    {
      for (n = 0; n < e->rank; n++)
	mpz_clear (e->shape[n]);

      gfc_free (e->shape);
    }

  gfc_free_ref_list (e->ref);

  memset (e, '\0', sizeof (gfc_expr));
}


/* Free an expression node and everything beneath it.  */

void
225
gfc_free_expr (gfc_expr *e)
226 227 228
{
  if (e == NULL)
    return;
229 230
  if (e->con_by_offset)
    splay_tree_delete (e->con_by_offset); 
231 232 233 234 235 236 237 238
  free_expr0 (e);
  gfc_free (e);
}


/* Graft the *src expression onto the *dest subexpression.  */

void
239
gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
240 241 242 243 244 245 246 247 248 249 250 251 252
{
  free_expr0 (dest);
  *dest = *src;
  gfc_free (src);
}


/* Try to extract an integer constant from the passed expression node.
   Returns an error message or NULL if the result is set.  It is
   tempting to generate an error and return SUCCESS or FAILURE, but
   failure is OK for some callers.  */

const char *
253
gfc_extract_int (gfc_expr *expr, int *result)
254 255
{
  if (expr->expr_type != EXPR_CONSTANT)
256
    return _("Constant expression required at %C");
257 258

  if (expr->ts.type != BT_INTEGER)
259
    return _("Integer expression required at %C");
260 261 262 263

  if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
      || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
    {
264
      return _("Integer value too large in expression at %C");
265 266 267 268 269 270 271 272 273 274
    }

  *result = (int) mpz_get_si (expr->value.integer);

  return NULL;
}


/* Recursively copy a list of reference structures.  */

275 276
gfc_ref *
gfc_copy_ref (gfc_ref *src)
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
{
  gfc_array_ref *ar;
  gfc_ref *dest;

  if (src == NULL)
    return NULL;

  dest = gfc_get_ref ();
  dest->type = src->type;

  switch (src->type)
    {
    case REF_ARRAY:
      ar = gfc_copy_array_ref (&src->u.ar);
      dest->u.ar = *ar;
      gfc_free (ar);
      break;

    case REF_COMPONENT:
      dest->u.c = src->u.c;
      break;

    case REF_SUBSTRING:
      dest->u.ss = src->u.ss;
      dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
      dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
      break;
    }

306
  dest->next = gfc_copy_ref (src->next);
307 308 309 310 311

  return dest;
}


312
/* Detect whether an expression has any vector index array references.  */
313 314 315 316

int
gfc_has_vector_index (gfc_expr *e)
{
317
  gfc_ref *ref;
318 319 320 321 322 323 324 325 326 327
  int i;
  for (ref = e->ref; ref; ref = ref->next)
    if (ref->type == REF_ARRAY)
      for (i = 0; i < ref->u.ar.dimen; i++)
	if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
	  return 1;
  return 0;
}


328 329 330
/* Copy a shape array.  */

mpz_t *
331
gfc_copy_shape (mpz_t *shape, int rank)
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
  mpz_t *new_shape;
  int n;

  if (shape == NULL)
    return NULL;

  new_shape = gfc_get_shape (rank);

  for (n = 0; n < rank; n++)
    mpz_init_set (new_shape[n], shape[n]);

  return new_shape;
}


348 349 350 351 352 353 354 355 356 357
/* Copy a shape array excluding dimension N, where N is an integer
   constant expression.  Dimensions are numbered in fortran style --
   starting with ONE.

   So, if the original shape array contains R elements
      { s1 ... sN-1  sN  sN+1 ... sR-1 sR}
   the result contains R-1 elements:
      { s1 ... sN-1  sN+1    ...  sR-1}

   If anything goes wrong -- N is not a constant, its value is out
358
   of range -- or anything else, just returns NULL.  */
359 360

mpz_t *
361
gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
362 363 364 365 366 367 368 369 370 371 372 373
{
  mpz_t *new_shape, *s;
  int i, n;

  if (shape == NULL 
      || rank <= 1
      || dim == NULL
      || dim->expr_type != EXPR_CONSTANT 
      || dim->ts.type != BT_INTEGER)
    return NULL;

  n = mpz_get_si (dim->value.integer);
374
  n--; /* Convert to zero based index.  */
375
  if (n < 0 || n >= rank)
376 377
    return NULL;

378
  s = new_shape = gfc_get_shape (rank - 1);
379 380 381 382

  for (i = 0; i < rank; i++)
    {
      if (i == n)
383
	continue;
384 385 386 387 388 389 390
      mpz_init_set (*s, shape[i]);
      s++;
    }

  return new_shape;
}

391

392 393 394 395
/* Given an expression pointer, return a copy of the expression.  This
   subroutine is recursive.  */

gfc_expr *
396
gfc_copy_expr (gfc_expr *p)
397 398
{
  gfc_expr *q;
399 400
  gfc_char_t *s;
  char *c;
401 402 403 404 405 406 407 408 409 410

  if (p == NULL)
    return NULL;

  q = gfc_get_expr ();
  *q = *p;

  switch (q->expr_type)
    {
    case EXPR_SUBSTRING:
411
      s = gfc_get_wide_string (p->value.character.length + 1);
412
      q->value.character.string = s;
413 414
      memcpy (s, p->value.character.string,
	      (p->value.character.length + 1) * sizeof (gfc_char_t));
415 416 417
      break;

    case EXPR_CONSTANT:
418 419
      /* Copy target representation, if it exists.  */
      if (p->representation.string)
420
	{
421
	  c = XCNEWVEC (char, p->representation.length + 1);
422 423
	  q->representation.string = c;
	  memcpy (c, p->representation.string, (p->representation.length + 1));
424
	}
425 426

      /* Copy the values of any pointer components of p->value.  */
427 428 429 430 431 432 433
      switch (q->ts.type)
	{
	case BT_INTEGER:
	  mpz_init_set (q->value.integer, p->value.integer);
	  break;

	case BT_REAL:
434 435
	  gfc_set_model_kind (q->ts.kind);
	  mpfr_init (q->value.real);
436
	  mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
437 438 439
	  break;

	case BT_COMPLEX:
440 441 442
	  gfc_set_model_kind (q->ts.kind);
	  mpfr_init (q->value.complex.r);
	  mpfr_init (q->value.complex.i);
443 444
	  mpfr_set (q->value.complex.r, p->value.complex.r, GFC_RND_MODE);
	  mpfr_set (q->value.complex.i, p->value.complex.i, GFC_RND_MODE);
445 446 447
	  break;

	case BT_CHARACTER:
448
	  if (p->representation.string)
449 450
	    q->value.character.string
	      = gfc_char_to_widechar (q->representation.string);
451 452
	  else
	    {
453
	      s = gfc_get_wide_string (p->value.character.length + 1);
454
	      q->value.character.string = s;
455

456 457 458 459 460 461 462 463 464 465 466
	      /* This is the case for the C_NULL_CHAR named constant.  */
	      if (p->value.character.length == 0
		  && (p->ts.is_c_interop || p->ts.is_iso_c))
		{
		  *s = '\0';
		  /* Need to set the length to 1 to make sure the NUL
		     terminator is copied.  */
		  q->value.character.length = 1;
		}
	      else
		memcpy (s, p->value.character.string,
467
			(p->value.character.length + 1) * sizeof (gfc_char_t));
468
	    }
469 470
	  break;

471
	case BT_HOLLERITH:
472 473
	case BT_LOGICAL:
	case BT_DERIVED:
474
	  break;		/* Already done.  */
475 476

	case BT_PROCEDURE:
477 478
        case BT_VOID:
           /* Should never be reached.  */
479 480
	case BT_UNKNOWN:
	  gfc_internal_error ("gfc_copy_expr(): Bad expr node");
481
	  /* Not reached.  */
482 483 484 485 486
	}

      break;

    case EXPR_OP:
487
      switch (q->value.op.op)
488 489
	{
	case INTRINSIC_NOT:
490
	case INTRINSIC_PARENTHESES:
491 492
	case INTRINSIC_UPLUS:
	case INTRINSIC_UMINUS:
493
	  q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
494 495
	  break;

496
	default:		/* Binary operators.  */
497 498
	  q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
	  q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
499 500 501 502 503 504 505 506 507 508
	  break;
	}

      break;

    case EXPR_FUNCTION:
      q->value.function.actual =
	gfc_copy_actual_arglist (p->value.function.actual);
      break;

509 510 511 512 513 514
    case EXPR_COMPCALL:
      q->value.compcall.actual =
	gfc_copy_actual_arglist (p->value.compcall.actual);
      q->value.compcall.tbp = p->value.compcall.tbp;
      break;

515 516 517 518 519 520 521 522 523 524 525 526
    case EXPR_STRUCTURE:
    case EXPR_ARRAY:
      q->value.constructor = gfc_copy_constructor (p->value.constructor);
      break;

    case EXPR_VARIABLE:
    case EXPR_NULL:
      break;
    }

  q->shape = gfc_copy_shape (p->shape, p->rank);

527
  q->ref = gfc_copy_ref (p->ref);
528 529 530 531 532 533 534 535 536

  return q;
}


/* Return the maximum kind of two expressions.  In general, higher
   kind numbers mean more precision for numeric types.  */

int
537
gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
{
  return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
}


/* Returns nonzero if the type is numeric, zero otherwise.  */

static int
numeric_type (bt type)
{
  return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER;
}


/* Returns nonzero if the typespec is a numeric type, zero otherwise.  */

int
555
gfc_numeric_ts (gfc_typespec *ts)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
{
  return numeric_type (ts->type);
}


/* Returns an expression node that is an integer constant.  */

gfc_expr *
gfc_int_expr (int i)
{
  gfc_expr *p;

  p = gfc_get_expr ();

  p->expr_type = EXPR_CONSTANT;
  p->ts.type = BT_INTEGER;
572
  p->ts.kind = gfc_default_integer_kind;
573

574
  p->where = gfc_current_locus;
575 576 577 578 579 580 581 582 583
  mpz_init_set_si (p->value.integer, i);

  return p;
}


/* Returns an expression node that is a logical constant.  */

gfc_expr *
584
gfc_logical_expr (int i, locus *where)
585 586 587 588 589 590 591
{
  gfc_expr *p;

  p = gfc_get_expr ();

  p->expr_type = EXPR_CONSTANT;
  p->ts.type = BT_LOGICAL;
592
  p->ts.kind = gfc_default_logical_kind;
593 594

  if (where == NULL)
595
    where = &gfc_current_locus;
596 597 598 599 600 601 602 603 604 605 606 607
  p->where = *where;
  p->value.logical = i;

  return p;
}


/* Return an expression node with an optional argument list attached.
   A variable number of gfc_expr pointers are strung together in an
   argument list with a NULL pointer terminating the list.  */

gfc_expr *
608
gfc_build_conversion (gfc_expr *e)
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
{
  gfc_expr *p;

  p = gfc_get_expr ();
  p->expr_type = EXPR_FUNCTION;
  p->symtree = NULL;
  p->value.function.actual = NULL;

  p->value.function.actual = gfc_get_actual_arglist ();
  p->value.function.actual->expr = e;

  return p;
}


/* Given an expression node with some sort of numeric binary
   expression, insert type conversions required to make the operands
   have the same type.

   The exception is that the operands of an exponential don't have to
   have the same type.  If possible, the base is promoted to the type
   of the exponent.  For example, 1**2.3 becomes 1.0**2.3, but
631
   1.0**2 stays as it is.  */
632 633

void
634
gfc_type_convert_binary (gfc_expr *e)
635 636 637
{
  gfc_expr *op1, *op2;

638 639
  op1 = e->value.op.op1;
  op2 = e->value.op.op2;
640 641 642 643 644 645 646 647 648 649 650 651

  if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
    {
      gfc_clear_ts (&e->ts);
      return;
    }

  /* Kind conversions of same type.  */
  if (op1->ts.type == op2->ts.type)
    {
      if (op1->ts.kind == op2->ts.kind)
	{
652
	  /* No type conversions.  */
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	  e->ts = op1->ts;
	  goto done;
	}

      if (op1->ts.kind > op2->ts.kind)
	gfc_convert_type (op2, &op1->ts, 2);
      else
	gfc_convert_type (op1, &op2->ts, 2);

      e->ts = op1->ts;
      goto done;
    }

  /* Integer combined with real or complex.  */
  if (op2->ts.type == BT_INTEGER)
    {
      e->ts = op1->ts;

671
      /* Special case for ** operator.  */
672
      if (e->value.op.op == INTRINSIC_POWER)
673 674
	goto done;

675
      gfc_convert_type (e->value.op.op2, &e->ts, 2);
676 677 678 679 680 681
      goto done;
    }

  if (op1->ts.type == BT_INTEGER)
    {
      e->ts = op2->ts;
682
      gfc_convert_type (e->value.op.op1, &e->ts, 2);
683 684 685 686 687 688 689 690 691 692
      goto done;
    }

  /* Real combined with complex.  */
  e->ts.type = BT_COMPLEX;
  if (op1->ts.kind > op2->ts.kind)
    e->ts.kind = op1->ts.kind;
  else
    e->ts.kind = op2->ts.kind;
  if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
693
    gfc_convert_type (e->value.op.op1, &e->ts, 2);
694
  if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
695
    gfc_convert_type (e->value.op.op2, &e->ts, 2);
696 697 698 699 700 701

done:
  return;
}


702 703 704 705
static match
check_specification_function (gfc_expr *e)
{
  gfc_symbol *sym;
706 707 708 709

  if (!e->symtree)
    return MATCH_NO;

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  sym = e->symtree->n.sym;

  /* F95, 7.1.6.2; F2003, 7.1.7  */
  if (sym
      && sym->attr.function
      && sym->attr.pure
      && !sym->attr.intrinsic
      && !sym->attr.recursive
      && sym->attr.proc != PROC_INTERNAL
      && sym->attr.proc != PROC_ST_FUNCTION
      && sym->attr.proc != PROC_UNKNOWN
      && sym->formal == NULL)
    return MATCH_YES;

  return MATCH_NO;
}

727 728 729 730
/* Function to determine if an expression is constant or not.  This
   function expects that the expression has already been simplified.  */

int
731
gfc_is_constant_expr (gfc_expr *e)
732 733 734 735 736 737 738 739 740 741 742
{
  gfc_constructor *c;
  gfc_actual_arglist *arg;
  int rv;

  if (e == NULL)
    return 1;

  switch (e->expr_type)
    {
    case EXPR_OP:
743 744 745
      rv = (gfc_is_constant_expr (e->value.op.op1)
	    && (e->value.op.op2 == NULL
		|| gfc_is_constant_expr (e->value.op.op2)));
746 747 748 749 750 751 752
      break;

    case EXPR_VARIABLE:
      rv = 0;
      break;

    case EXPR_FUNCTION:
753 754 755 756 757 758 759
      /* Specification functions are constant.  */
      if (check_specification_function (e) == MATCH_YES)
	{
	  rv = 1;
	  break;
	}

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
      /* Call to intrinsic with at least one argument.  */
      rv = 0;
      if (e->value.function.isym && e->value.function.actual)
	{
	  for (arg = e->value.function.actual; arg; arg = arg->next)
	    {
	      if (!gfc_is_constant_expr (arg->expr))
		break;
	    }
	  if (arg == NULL)
	    rv = 1;
	}
      break;

    case EXPR_CONSTANT:
    case EXPR_NULL:
      rv = 1;
      break;

    case EXPR_SUBSTRING:
780 781
      rv = e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
			      && gfc_is_constant_expr (e->ref->u.ss.end));
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
      break;

    case EXPR_STRUCTURE:
      rv = 0;
      for (c = e->value.constructor; c; c = c->next)
	if (!gfc_is_constant_expr (c->expr))
	  break;

      if (c == NULL)
	rv = 1;
      break;

    case EXPR_ARRAY:
      rv = gfc_constant_ac (e);
      break;

    default:
      gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
    }

  return rv;
}


806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
/* Is true if an array reference is followed by a component or substring
   reference.  */
bool
is_subref_array (gfc_expr * e)
{
  gfc_ref * ref;
  bool seen_array;

  if (e->expr_type != EXPR_VARIABLE)
    return false;

  if (e->symtree->n.sym->attr.subref_array_pointer)
    return true;

  seen_array = false;
  for (ref = e->ref; ref; ref = ref->next)
    {
      if (ref->type == REF_ARRAY
	    && ref->u.ar.type != AR_ELEMENT)
	seen_array = true;

      if (seen_array
	    && ref->type != REF_ARRAY)
	return seen_array;
    }
  return false;
}


835 836
/* Try to collapse intrinsic expressions.  */

837
static gfc_try
838
simplify_intrinsic_op (gfc_expr *p, int type)
839
{
840
  gfc_intrinsic_op op;
841 842
  gfc_expr *op1, *op2, *result;

843
  if (p->value.op.op == INTRINSIC_USER)
844 845
    return SUCCESS;

846 847
  op1 = p->value.op.op1;
  op2 = p->value.op.op2;
848
  op  = p->value.op.op;
849 850 851 852 853 854 855 856 857 858

  if (gfc_simplify_expr (op1, type) == FAILURE)
    return FAILURE;
  if (gfc_simplify_expr (op2, type) == FAILURE)
    return FAILURE;

  if (!gfc_is_constant_expr (op1)
      || (op2 != NULL && !gfc_is_constant_expr (op2)))
    return SUCCESS;

859
  /* Rip p apart.  */
860 861
  p->value.op.op1 = NULL;
  p->value.op.op2 = NULL;
862

863
  switch (op)
864
    {
865
    case INTRINSIC_PARENTHESES:
866 867 868 869
      result = gfc_parentheses (op1);
      break;

    case INTRINSIC_UPLUS:
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
      result = gfc_uplus (op1);
      break;

    case INTRINSIC_UMINUS:
      result = gfc_uminus (op1);
      break;

    case INTRINSIC_PLUS:
      result = gfc_add (op1, op2);
      break;

    case INTRINSIC_MINUS:
      result = gfc_subtract (op1, op2);
      break;

    case INTRINSIC_TIMES:
      result = gfc_multiply (op1, op2);
      break;

    case INTRINSIC_DIVIDE:
      result = gfc_divide (op1, op2);
      break;

    case INTRINSIC_POWER:
      result = gfc_power (op1, op2);
      break;

    case INTRINSIC_CONCAT:
      result = gfc_concat (op1, op2);
      break;

    case INTRINSIC_EQ:
902 903
    case INTRINSIC_EQ_OS:
      result = gfc_eq (op1, op2, op);
904 905 906
      break;

    case INTRINSIC_NE:
907 908
    case INTRINSIC_NE_OS:
      result = gfc_ne (op1, op2, op);
909 910 911
      break;

    case INTRINSIC_GT:
912 913
    case INTRINSIC_GT_OS:
      result = gfc_gt (op1, op2, op);
914 915 916
      break;

    case INTRINSIC_GE:
917 918
    case INTRINSIC_GE_OS:
      result = gfc_ge (op1, op2, op);
919 920 921
      break;

    case INTRINSIC_LT:
922 923
    case INTRINSIC_LT_OS:
      result = gfc_lt (op1, op2, op);
924 925 926
      break;

    case INTRINSIC_LE:
927 928
    case INTRINSIC_LE_OS:
      result = gfc_le (op1, op2, op);
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
      break;

    case INTRINSIC_NOT:
      result = gfc_not (op1);
      break;

    case INTRINSIC_AND:
      result = gfc_and (op1, op2);
      break;

    case INTRINSIC_OR:
      result = gfc_or (op1, op2);
      break;

    case INTRINSIC_EQV:
      result = gfc_eqv (op1, op2);
      break;

    case INTRINSIC_NEQV:
      result = gfc_neqv (op1, op2);
      break;

    default:
      gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
    }

  if (result == NULL)
    {
      gfc_free_expr (op1);
      gfc_free_expr (op2);
      return FAILURE;
    }

962 963
  result->rank = p->rank;
  result->where = p->where;
964 965 966 967 968 969 970 971 972
  gfc_replace_expr (p, result);

  return SUCCESS;
}


/* Subroutine to simplify constructor expressions.  Mutually recursive
   with gfc_simplify_expr().  */

973
static gfc_try
974
simplify_constructor (gfc_constructor *c, int type)
975
{
976 977
  gfc_expr *p;

978 979 980 981 982 983 984 985
  for (; c; c = c->next)
    {
      if (c->iterator
	  && (gfc_simplify_expr (c->iterator->start, type) == FAILURE
	      || gfc_simplify_expr (c->iterator->end, type) == FAILURE
	      || gfc_simplify_expr (c->iterator->step, type) == FAILURE))
	return FAILURE;

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
      if (c->expr)
	{
	  /* Try and simplify a copy.  Replace the original if successful
	     but keep going through the constructor at all costs.  Not
	     doing so can make a dog's dinner of complicated things.  */
	  p = gfc_copy_expr (c->expr);

	  if (gfc_simplify_expr (p, type) == FAILURE)
	    {
	      gfc_free_expr (p);
	      continue;
	    }

	  gfc_replace_expr (c->expr, p);
	}
1001 1002 1003 1004 1005 1006 1007 1008
    }

  return SUCCESS;
}


/* Pull a single array element out of an array constructor.  */

1009
static gfc_try
1010 1011
find_array_element (gfc_constructor *cons, gfc_array_ref *ar,
		    gfc_constructor **rval)
1012 1013 1014 1015 1016
{
  unsigned long nelemen;
  int i;
  mpz_t delta;
  mpz_t offset;
1017 1018
  mpz_t span;
  mpz_t tmp;
1019
  gfc_expr *e;
1020
  gfc_try t;
1021 1022 1023

  t = SUCCESS;
  e = NULL;
1024 1025 1026

  mpz_init_set_ui (offset, 0);
  mpz_init (delta);
1027 1028
  mpz_init (tmp);
  mpz_init_set_ui (span, 1);
1029 1030
  for (i = 0; i < ar->dimen; i++)
    {
1031 1032 1033 1034 1035 1036 1037 1038
      if (gfc_reduce_init_expr (ar->as->lower[i]) == FAILURE
	  || gfc_reduce_init_expr (ar->as->upper[i]) == FAILURE)
	{
	  t = FAILURE;
	  cons = NULL;
	  goto depart;
	}

1039 1040
      e = gfc_copy_expr (ar->start[i]);
      if (e->expr_type != EXPR_CONSTANT)
1041 1042
	{
	  cons = NULL;
1043
	  goto depart;
1044
	}
1045

1046 1047 1048
      gcc_assert (ar->as->upper[i]->expr_type == EXPR_CONSTANT
		  && ar->as->lower[i]->expr_type == EXPR_CONSTANT);

1049
      /* Check the bounds.  */
1050
      if ((ar->as->upper[i]
1051 1052
	   && mpz_cmp (e->value.integer,
		       ar->as->upper[i]->value.integer) > 0)
1053 1054
	  || (mpz_cmp (e->value.integer,
		       ar->as->lower[i]->value.integer) < 0))
1055
	{
1056
	  gfc_error ("Index in dimension %d is out of bounds "
1057 1058 1059 1060 1061 1062
		     "at %L", i + 1, &ar->c_where[i]);
	  cons = NULL;
	  t = FAILURE;
	  goto depart;
	}

1063
      mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1064
      mpz_mul (delta, delta, span);
1065
      mpz_add (offset, offset, delta);
1066 1067 1068 1069 1070

      mpz_set_ui (tmp, 1);
      mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
      mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
      mpz_mul (span, span, tmp);
1071 1072
    }

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
  for (nelemen = mpz_get_ui (offset); nelemen > 0; nelemen--)
    {
      if (cons)
	{
	  if (cons->iterator)
	    {
	      cons = NULL;
	      goto depart;
	    }
	  cons = cons->next;
	}
    }
1085

1086
depart:
1087 1088
  mpz_clear (delta);
  mpz_clear (offset);
1089 1090
  mpz_clear (span);
  mpz_clear (tmp);
1091 1092 1093 1094
  if (e)
    gfc_free_expr (e);
  *rval = cons;
  return t;
1095 1096 1097 1098 1099 1100
}


/* Find a component of a structure constructor.  */

static gfc_constructor *
1101
find_component_ref (gfc_constructor *cons, gfc_ref *ref)
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
{
  gfc_component *comp;
  gfc_component *pick;

  comp = ref->u.c.sym->components;
  pick = ref->u.c.component;
  while (comp != pick)
    {
      comp = comp->next;
      cons = cons->next;
    }

  return cons;
}


/* Replace an expression with the contents of a constructor, removing
   the subobject reference in the process.  */

static void
1122
remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
{
  gfc_expr *e;

  e = cons->expr;
  cons->expr = NULL;
  e->ref = p->ref->next;
  p->ref->next =  NULL;
  gfc_replace_expr (p, e);
}


1134 1135
/* Pull an array section out of an array constructor.  */

1136
static gfc_try
1137 1138 1139 1140 1141
find_array_section (gfc_expr *expr, gfc_ref *ref)
{
  int idx;
  int rank;
  int d;
1142
  int shape_i;
1143
  long unsigned one = 1;
1144
  bool incr_ctr;
1145
  mpz_t start[GFC_MAX_DIMENSIONS];
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  mpz_t end[GFC_MAX_DIMENSIONS];
  mpz_t stride[GFC_MAX_DIMENSIONS];
  mpz_t delta[GFC_MAX_DIMENSIONS];
  mpz_t ctr[GFC_MAX_DIMENSIONS];
  mpz_t delta_mpz;
  mpz_t tmp_mpz;
  mpz_t nelts;
  mpz_t ptr;
  mpz_t index;
  gfc_constructor *cons;
  gfc_constructor *base;
  gfc_expr *begin;
  gfc_expr *finish;
  gfc_expr *step;
  gfc_expr *upper;
  gfc_expr *lower;
1162
  gfc_constructor *vecsub[GFC_MAX_DIMENSIONS], *c;
1163
  gfc_try t;
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

  t = SUCCESS;

  base = expr->value.constructor;
  expr->value.constructor = NULL;

  rank = ref->u.ar.as->rank;

  if (expr->shape == NULL)
    expr->shape = gfc_get_shape (rank);

  mpz_init_set_ui (delta_mpz, one);
  mpz_init_set_ui (nelts, one);
  mpz_init (tmp_mpz);

  /* Do the initialization now, so that we can cleanup without
     keeping track of where we were.  */
  for (d = 0; d < rank; d++)
    {
      mpz_init (delta[d]);
1184
      mpz_init (start[d]);
1185 1186 1187
      mpz_init (end[d]);
      mpz_init (ctr[d]);
      mpz_init (stride[d]);
1188
      vecsub[d] = NULL;
1189 1190 1191
    }

  /* Build the counters to clock through the array reference.  */
1192
  shape_i = 0;
1193 1194 1195 1196 1197 1198 1199 1200 1201
  for (d = 0; d < rank; d++)
    {
      /* Make this stretch of code easier on the eye!  */
      begin = ref->u.ar.start[d];
      finish = ref->u.ar.end[d];
      step = ref->u.ar.stride[d];
      lower = ref->u.ar.as->lower[d];
      upper = ref->u.ar.as->upper[d];

1202
      if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR)  /* Vector subscript.  */
1203 1204
	{
	  gcc_assert (begin);
Tobias Burnus committed
1205

1206
	  if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
Tobias Burnus committed
1207 1208 1209 1210 1211
	    {
	      t = FAILURE;
	      goto cleanup;
	    }

1212
	  gcc_assert (begin->rank == 1);
1213 1214 1215 1216 1217 1218
	  /* Zero-sized arrays have no shape and no elements, stop early.  */
	  if (!begin->shape) 
	    {
	      mpz_init_set_ui (nelts, 0);
	      break;
	    }
1219

1220 1221 1222 1223
	  vecsub[d] = begin->value.constructor;
	  mpz_set (ctr[d], vecsub[d]->expr->value.integer);
	  mpz_mul (nelts, nelts, begin->shape[0]);
	  mpz_set (expr->shape[shape_i++], begin->shape[0]);
1224

1225 1226 1227 1228
	  /* Check bounds.  */
	  for (c = vecsub[d]; c; c = c->next)
	    {
	      if (mpz_cmp (c->expr->value.integer, upper->value.integer) > 0
1229 1230
		  || mpz_cmp (c->expr->value.integer,
			      lower->value.integer) < 0)
1231 1232 1233 1234 1235 1236 1237
		{
		  gfc_error ("index in dimension %d is out of bounds "
			     "at %L", d + 1, &ref->u.ar.c_where[d]);
		  t = FAILURE;
		  goto cleanup;
		}
	    }
1238
	}
1239
      else
1240
	{
1241
	  if ((begin && begin->expr_type != EXPR_CONSTANT)
1242 1243
	      || (finish && finish->expr_type != EXPR_CONSTANT)
	      || (step && step->expr_type != EXPR_CONSTANT))
1244 1245 1246 1247
	    {
	      t = FAILURE;
	      goto cleanup;
	    }
1248

1249 1250 1251 1252 1253
	  /* Obtain the stride.  */
	  if (step)
	    mpz_set (stride[d], step->value.integer);
	  else
	    mpz_set_ui (stride[d], one);
1254

1255 1256
	  if (mpz_cmp_ui (stride[d], 0) == 0)
	    mpz_set_ui (stride[d], one);
1257

1258 1259 1260 1261 1262
	  /* Obtain the start value for the index.  */
	  if (begin)
	    mpz_set (start[d], begin->value.integer);
	  else
	    mpz_set (start[d], lower->value.integer);
1263

1264
	  mpz_set (ctr[d], start[d]);
1265

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	  /* Obtain the end value for the index.  */
	  if (finish)
	    mpz_set (end[d], finish->value.integer);
	  else
	    mpz_set (end[d], upper->value.integer);

	  /* Separate 'if' because elements sometimes arrive with
	     non-null end.  */
	  if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
	    mpz_set (end [d], begin->value.integer);

	  /* Check the bounds.  */
	  if (mpz_cmp (ctr[d], upper->value.integer) > 0
	      || mpz_cmp (end[d], upper->value.integer) > 0
	      || mpz_cmp (ctr[d], lower->value.integer) < 0
	      || mpz_cmp (end[d], lower->value.integer) < 0)
	    {
	      gfc_error ("index in dimension %d is out of bounds "
			 "at %L", d + 1, &ref->u.ar.c_where[d]);
	      t = FAILURE;
	      goto cleanup;
	    }
1288

1289
	  /* Calculate the number of elements and the shape.  */
1290
	  mpz_set (tmp_mpz, stride[d]);
1291 1292 1293 1294 1295
	  mpz_add (tmp_mpz, end[d], tmp_mpz);
	  mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
	  mpz_div (tmp_mpz, tmp_mpz, stride[d]);
	  mpz_mul (nelts, nelts, tmp_mpz);

1296 1297
	  /* An element reference reduces the rank of the expression; don't
	     add anything to the shape array.  */
1298 1299 1300
	  if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT) 
	    mpz_set (expr->shape[shape_i++], tmp_mpz);
	}
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

      /* Calculate the 'stride' (=delta) for conversion of the
	 counter values into the index along the constructor.  */
      mpz_set (delta[d], delta_mpz);
      mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
      mpz_add_ui (tmp_mpz, tmp_mpz, one);
      mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
    }

  mpz_init (index);
  mpz_init (ptr);
  cons = base;

  /* Now clock through the array reference, calculating the index in
     the source constructor and transferring the elements to the new
     constructor.  */  
1317
  for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1318 1319 1320 1321 1322 1323
    {
      if (ref->u.ar.offset)
	mpz_set (ptr, ref->u.ar.offset->value.integer);
      else
	mpz_init_set_ui (ptr, 0);

1324
      incr_ctr = true;
1325 1326 1327
      for (d = 0; d < rank; d++)
	{
	  mpz_set (tmp_mpz, ctr[d]);
1328
	  mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1329 1330 1331
	  mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
	  mpz_add (ptr, ptr, tmp_mpz);

1332
	  if (!incr_ctr) continue;
1333

1334
	  if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript.  */
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	    {
	      gcc_assert(vecsub[d]);

	      if (!vecsub[d]->next)
		vecsub[d] = ref->u.ar.start[d]->value.constructor;
	      else
		{
		  vecsub[d] = vecsub[d]->next;
		  incr_ctr = false;
		}
	      mpz_set (ctr[d], vecsub[d]->expr->value.integer);
	    }
1347
	  else
1348 1349 1350
	    {
	      mpz_add (ctr[d], ctr[d], stride[d]); 

1351 1352 1353
	      if (mpz_cmp_ui (stride[d], 0) > 0
		  ? mpz_cmp (ctr[d], end[d]) > 0
		  : mpz_cmp (ctr[d], end[d]) < 0)
1354 1355 1356 1357
		mpz_set (ctr[d], start[d]);
	      else
		incr_ctr = false;
	    }
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
	}

      /* There must be a better way of dealing with negative strides
	 than resetting the index and the constructor pointer!  */ 
      if (mpz_cmp (ptr, index) < 0)
	{
	  mpz_set_ui (index, 0);
	  cons = base;
	}

1368
      while (cons && cons->next && mpz_cmp (ptr, index) > 0)
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
	{
	  mpz_add_ui (index, index, one);
	  cons = cons->next;
	}

      gfc_append_constructor (expr, gfc_copy_expr (cons->expr));
    }

  mpz_clear (ptr);
  mpz_clear (index);

cleanup:

  mpz_clear (delta_mpz);
  mpz_clear (tmp_mpz);
  mpz_clear (nelts);
  for (d = 0; d < rank; d++)
    {
      mpz_clear (delta[d]);
1388
      mpz_clear (start[d]);
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
      mpz_clear (end[d]);
      mpz_clear (ctr[d]);
      mpz_clear (stride[d]);
    }
  gfc_free_constructor (base);
  return t;
}

/* Pull a substring out of an expression.  */

1399
static gfc_try
1400 1401 1402 1403
find_substring_ref (gfc_expr *p, gfc_expr **newp)
{
  int end;
  int start;
1404
  int length;
1405
  gfc_char_t *chr;
1406 1407

  if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1408
      || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1409 1410 1411
    return FAILURE;

  *newp = gfc_copy_expr (p);
1412 1413
  gfc_free ((*newp)->value.character.string);

1414 1415
  end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
  start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
1416
  length = end - start + 1;
1417

1418
  chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1419
  (*newp)->value.character.length = length;
1420 1421
  memcpy (chr, &p->value.character.string[start - 1],
	  length * sizeof (gfc_char_t));
1422
  chr[length] = '\0';
1423 1424 1425 1426 1427
  return SUCCESS;
}



1428 1429 1430
/* Simplify a subobject reference of a constructor.  This occurs when
   parameter variable values are substituted.  */

1431
static gfc_try
1432
simplify_const_ref (gfc_expr *p)
1433 1434
{
  gfc_constructor *cons;
1435
  gfc_expr *newp;
1436 1437 1438 1439 1440 1441 1442 1443 1444

  while (p->ref)
    {
      switch (p->ref->type)
	{
	case REF_ARRAY:
	  switch (p->ref->u.ar.type)
	    {
	    case AR_ELEMENT:
1445
	      if (find_array_element (p->value.constructor, &p->ref->u.ar,
1446 1447 1448
				      &cons) == FAILURE)
		return FAILURE;

1449 1450
	      if (!cons)
		return SUCCESS;
1451

1452 1453 1454
	      remove_subobject_ref (p, cons);
	      break;

1455 1456 1457 1458 1459
	    case AR_SECTION:
	      if (find_array_section (p, p->ref) == FAILURE)
		return FAILURE;
	      p->ref->u.ar.type = AR_FULL;

1460
	    /* Fall through.  */
1461

1462
	    case AR_FULL:
1463
	      if (p->ref->next != NULL
1464
		  && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
1465
		{
1466 1467 1468
		  cons = p->value.constructor;
		  for (; cons; cons = cons->next)
		    {
1469
		      cons->expr->ref = gfc_copy_ref (p->ref->next);
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
		      if (simplify_const_ref (cons->expr) == FAILURE)
			return FAILURE;
		    }

		  /* If this is a CHARACTER array and we possibly took a
		     substring out of it, update the type-spec's character
		     length according to the first element (as all should have
		     the same length).  */
		  if (p->ts.type == BT_CHARACTER)
		    {
		      int string_len;

		      gcc_assert (p->ref->next);
		      gcc_assert (!p->ref->next->next);
		      gcc_assert (p->ref->next->type == REF_SUBSTRING);

		      if (p->value.constructor)
			{
			  const gfc_expr* first = p->value.constructor->expr;
			  gcc_assert (first->expr_type == EXPR_CONSTANT);
			  gcc_assert (first->ts.type == BT_CHARACTER);
			  string_len = first->value.character.length;
			}
		      else
			string_len = 0;

		      if (!p->ts.cl)
			{
			  p->ts.cl = gfc_get_charlen ();
			  p->ts.cl->next = NULL;
			  p->ts.cl->length = NULL;
			}
		      gfc_free_expr (p->ts.cl->length);
		      p->ts.cl->length = gfc_int_expr (string_len);
1504
		    }
1505
		}
1506 1507
	      gfc_free_ref_list (p->ref);
	      p->ref = NULL;
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
	      break;

	    default:
	      return SUCCESS;
	    }

	  break;

	case REF_COMPONENT:
	  cons = find_component_ref (p->value.constructor, p->ref);
	  remove_subobject_ref (p, cons);
	  break;

	case REF_SUBSTRING:
1522 1523 1524 1525 1526 1527 1528
  	  if (find_substring_ref (p, &newp) == FAILURE)
	    return FAILURE;

	  gfc_replace_expr (p, newp);
	  gfc_free_ref_list (p->ref);
	  p->ref = NULL;
	  break;
1529 1530 1531 1532 1533 1534 1535 1536 1537
	}
    }

  return SUCCESS;
}


/* Simplify a chain of references.  */

1538
static gfc_try
1539
simplify_ref_chain (gfc_ref *ref, int type)
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
{
  int n;

  for (; ref; ref = ref->next)
    {
      switch (ref->type)
	{
	case REF_ARRAY:
	  for (n = 0; n < ref->u.ar.dimen; n++)
	    {
1550
	      if (gfc_simplify_expr (ref->u.ar.start[n], type) == FAILURE)
1551
		return FAILURE;
1552
	      if (gfc_simplify_expr (ref->u.ar.end[n], type) == FAILURE)
1553
		return FAILURE;
1554
	      if (gfc_simplify_expr (ref->u.ar.stride[n], type) == FAILURE)
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
		return FAILURE;
	    }
	  break;

	case REF_SUBSTRING:
	  if (gfc_simplify_expr (ref->u.ss.start, type) == FAILURE)
	    return FAILURE;
	  if (gfc_simplify_expr (ref->u.ss.end, type) == FAILURE)
	    return FAILURE;
	  break;

	default:
	  break;
	}
    }
  return SUCCESS;
}


/* Try to substitute the value of a parameter variable.  */
1575

1576
static gfc_try
1577
simplify_parameter_variable (gfc_expr *p, int type)
1578 1579
{
  gfc_expr *e;
1580
  gfc_try t;
1581 1582

  e = gfc_copy_expr (p->symtree->n.sym->value);
1583 1584 1585
  if (e == NULL)
    return FAILURE;

1586 1587
  e->rank = p->rank;

1588 1589
  /* Do not copy subobject refs for constant.  */
  if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
1590
    e->ref = gfc_copy_ref (p->ref);
1591 1592
  t = gfc_simplify_expr (e, type);

1593
  /* Only use the simplification if it eliminated all subobject references.  */
1594
  if (t == SUCCESS && !e->ref)
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
    gfc_replace_expr (p, e);
  else
    gfc_free_expr (e);

  return t;
}

/* Given an expression, simplify it by collapsing constant
   expressions.  Most simplification takes place when the expression
   tree is being constructed.  If an intrinsic function is simplified
   at some point, we get called again to collapse the result against
   other constants.

   We work by recursively simplifying expression nodes, simplifying
   intrinsic functions where possible, which can lead to further
   constant collapsing.  If an operator has constant operand(s), we
   rip the expression apart, and rebuild it, hoping that it becomes
   something simpler.

   The expression type is defined for:
     0   Basic expression parsing
     1   Simplifying array constructors -- will substitute
1617
	 iterator values.
1618 1619 1620
   Returns FAILURE on error, SUCCESS otherwise.
   NOTE: Will return SUCCESS even if the expression can not be simplified.  */

1621
gfc_try
1622
gfc_simplify_expr (gfc_expr *p, int type)
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
{
  gfc_actual_arglist *ap;

  if (p == NULL)
    return SUCCESS;

  switch (p->expr_type)
    {
    case EXPR_CONSTANT:
    case EXPR_NULL:
      break;

    case EXPR_FUNCTION:
      for (ap = p->value.function.actual; ap; ap = ap->next)
	if (gfc_simplify_expr (ap->expr, type) == FAILURE)
	  return FAILURE;

      if (p->value.function.isym != NULL
	  && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
	return FAILURE;

      break;

    case EXPR_SUBSTRING:
1647
      if (simplify_ref_chain (p->ref, type) == FAILURE)
1648 1649
	return FAILURE;

1650 1651
      if (gfc_is_constant_expr (p))
	{
1652
	  gfc_char_t *s;
1653 1654
	  int start, end;

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
	  if (p->ref && p->ref->u.ss.start)
	    {
	      gfc_extract_int (p->ref->u.ss.start, &start);
	      start--;  /* Convert from one-based to zero-based.  */
	    }
	  else
	    start = 0;

	  if (p->ref && p->ref->u.ss.end)
	    gfc_extract_int (p->ref->u.ss.end, &end);
	  else
	    end = p->value.character.length;

1668 1669 1670
	  s = gfc_get_wide_string (end - start + 2);
	  memcpy (s, p->value.character.string + start,
		  (end - start) * sizeof (gfc_char_t));
1671
	  s[end - start + 1] = '\0';  /* TODO: C-style string.  */
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	  gfc_free (p->value.character.string);
	  p->value.character.string = s;
	  p->value.character.length = end - start;
	  p->ts.cl = gfc_get_charlen ();
	  p->ts.cl->next = gfc_current_ns->cl_list;
	  gfc_current_ns->cl_list = p->ts.cl;
	  p->ts.cl->length = gfc_int_expr (p->value.character.length);
	  gfc_free_ref_list (p->ref);
	  p->ref = NULL;
	  p->expr_type = EXPR_CONSTANT;
	}
1683 1684 1685 1686 1687 1688 1689 1690 1691
      break;

    case EXPR_OP:
      if (simplify_intrinsic_op (p, type) == FAILURE)
	return FAILURE;
      break;

    case EXPR_VARIABLE:
      /* Only substitute array parameter variables if we are in an
1692
	 initialization expression, or we want a subsection.  */
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
      if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
	  && (gfc_init_expr || p->ref
	      || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
	{
	  if (simplify_parameter_variable (p, type) == FAILURE)
	    return FAILURE;
	  break;
	}

      if (type == 1)
	{
	  gfc_simplify_iterator_var (p);
	}

      /* Simplify subcomponent references.  */
      if (simplify_ref_chain (p->ref, type) == FAILURE)
	return FAILURE;

      break;

    case EXPR_STRUCTURE:
    case EXPR_ARRAY:
      if (simplify_ref_chain (p->ref, type) == FAILURE)
	return FAILURE;

      if (simplify_constructor (p->value.constructor, type) == FAILURE)
	return FAILURE;

1721 1722
      if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
	  && p->ref->u.ar.type == AR_FULL)
1723 1724 1725 1726 1727 1728
	  gfc_expand_constructor (p);

      if (simplify_const_ref (p) == FAILURE)
	return FAILURE;

      break;
1729 1730 1731 1732

    case EXPR_COMPCALL:
      gcc_unreachable ();
      break;
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    }

  return SUCCESS;
}


/* Returns the type of an expression with the exception that iterator
   variables are automatically integers no matter what else they may
   be declared as.  */

static bt
1744
et0 (gfc_expr *e)
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
{
  if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e) == SUCCESS)
    return BT_INTEGER;

  return e->ts.type;
}


/* Check an intrinsic arithmetic operation to see if it is consistent
   with some type of expression.  */

1756
static gfc_try check_init_expr (gfc_expr *);
1757

1758 1759 1760

/* Scalarize an expression for an elemental intrinsic call.  */

1761
static gfc_try
1762 1763 1764 1765 1766
scalarize_intrinsic_call (gfc_expr *e)
{
  gfc_actual_arglist *a, *b;
  gfc_constructor *args[5], *ctor, *new_ctor;
  gfc_expr *expr, *old;
Paul Thomas committed
1767
  int n, i, rank[5], array_arg;
1768

Paul Thomas committed
1769 1770 1771 1772
  /* Find which, if any, arguments are arrays.  Assume that the old
     expression carries the type information and that the first arg
     that is an array expression carries all the shape information.*/
  n = array_arg = 0;
1773
  a = e->value.function.actual;
Paul Thomas committed
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
  for (; a; a = a->next)
    {
      n++;
      if (a->expr->expr_type != EXPR_ARRAY)
	continue;
      array_arg = n;
      expr = gfc_copy_expr (a->expr);
      break;
    }

  if (!array_arg)
1785 1786 1787
    return FAILURE;

  old = gfc_copy_expr (e);
Paul Thomas committed
1788

1789 1790 1791 1792
  gfc_free_constructor (expr->value.constructor);
  expr->value.constructor = NULL;

  expr->ts = old->ts;
Paul Thomas committed
1793
  expr->where = old->where;
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
  expr->expr_type = EXPR_ARRAY;

  /* Copy the array argument constructors into an array, with nulls
     for the scalars.  */
  n = 0;
  a = old->value.function.actual;
  for (; a; a = a->next)
    {
      /* Check that this is OK for an initialization expression.  */
      if (a->expr && check_init_expr (a->expr) == FAILURE)
	goto cleanup;

      rank[n] = 0;
      if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
	{
	  rank[n] = a->expr->rank;
	  ctor = a->expr->symtree->n.sym->value->value.constructor;
	  args[n] = gfc_copy_constructor (ctor);
	}
      else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
	{
	  if (a->expr->rank)
	    rank[n] = a->expr->rank;
	  else
	    rank[n] = 1;
	  args[n] = gfc_copy_constructor (a->expr->value.constructor);
	}
      else
	args[n] = NULL;
      n++;
    }


1827
  /* Using the array argument as the master, step through the array
1828 1829
     calling the function for each element and advancing the array
     constructors together.  */
Paul Thomas committed
1830
  ctor = args[array_arg - 1];
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
  new_ctor = NULL;
  for (; ctor; ctor = ctor->next)
    {
	  if (expr->value.constructor == NULL)
	    expr->value.constructor
		= new_ctor = gfc_get_constructor ();
	  else
	    {
	      new_ctor->next = gfc_get_constructor ();
	      new_ctor = new_ctor->next;
	    }
	  new_ctor->expr = gfc_copy_expr (old);
	  gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
	  a = NULL;
	  b = old->value.function.actual;
	  for (i = 0; i < n; i++)
	    {
	      if (a == NULL)
		new_ctor->expr->value.function.actual
			= a = gfc_get_actual_arglist ();
	      else
		{
		  a->next = gfc_get_actual_arglist ();
		  a = a->next;
		}
	      if (args[i])
		a->expr = gfc_copy_expr (args[i]->expr);
	      else
		a->expr = gfc_copy_expr (b->expr);

	      b = b->next;
	    }

Paul Thomas committed
1864 1865 1866 1867
	  /* Simplify the function calls.  If the simplification fails, the
	     error will be flagged up down-stream or the library will deal
	     with it.  */
	  gfc_simplify_expr (new_ctor->expr, 0);
1868 1869 1870 1871 1872 1873

	  for (i = 0; i < n; i++)
	    if (args[i])
	      args[i] = args[i]->next;

	  for (i = 1; i < n; i++)
Paul Thomas committed
1874 1875
	    if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
			 || (args[i] == NULL && args[array_arg - 1] != NULL)))
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
	      goto compliance;
    }

  free_expr0 (e);
  *e = *expr;
  gfc_free_expr (old);
  return SUCCESS;

compliance:
  gfc_error_now ("elemental function arguments at %C are not compliant");

cleanup:
  gfc_free_expr (expr);
  gfc_free_expr (old);
  return FAILURE;
}


1894 1895
static gfc_try
check_intrinsic_op (gfc_expr *e, gfc_try (*check_function) (gfc_expr *))
1896
{
1897 1898
  gfc_expr *op1 = e->value.op.op1;
  gfc_expr *op2 = e->value.op.op2;
1899

1900
  if ((*check_function) (op1) == FAILURE)
1901 1902
    return FAILURE;

1903
  switch (e->value.op.op)
1904 1905 1906
    {
    case INTRINSIC_UPLUS:
    case INTRINSIC_UMINUS:
1907
      if (!numeric_type (et0 (op1)))
1908 1909 1910 1911
	goto not_numeric;
      break;

    case INTRINSIC_EQ:
1912
    case INTRINSIC_EQ_OS:
1913
    case INTRINSIC_NE:
1914
    case INTRINSIC_NE_OS:
1915
    case INTRINSIC_GT:
1916
    case INTRINSIC_GT_OS:
1917
    case INTRINSIC_GE:
1918
    case INTRINSIC_GE_OS:
1919
    case INTRINSIC_LT:
1920
    case INTRINSIC_LT_OS:
1921
    case INTRINSIC_LE:
1922
    case INTRINSIC_LE_OS:
1923
      if ((*check_function) (op2) == FAILURE)
1924 1925
	return FAILURE;
      
1926 1927
      if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
	  && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
1928 1929 1930
	{
	  gfc_error ("Numeric or CHARACTER operands are required in "
		     "expression at %L", &e->where);
1931
	 return FAILURE;
1932 1933
	}
      break;
1934 1935 1936 1937 1938 1939

    case INTRINSIC_PLUS:
    case INTRINSIC_MINUS:
    case INTRINSIC_TIMES:
    case INTRINSIC_DIVIDE:
    case INTRINSIC_POWER:
1940
      if ((*check_function) (op2) == FAILURE)
1941 1942
	return FAILURE;

1943
      if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
1944 1945 1946 1947 1948
	goto not_numeric;

      break;

    case INTRINSIC_CONCAT:
1949
      if ((*check_function) (op2) == FAILURE)
1950 1951
	return FAILURE;

1952
      if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
1953 1954
	{
	  gfc_error ("Concatenation operator in expression at %L "
1955
		     "must have two CHARACTER operands", &op1->where);
1956 1957 1958
	  return FAILURE;
	}

1959
      if (op1->ts.kind != op2->ts.kind)
1960 1961 1962 1963 1964 1965 1966 1967 1968
	{
	  gfc_error ("Concat operator at %L must concatenate strings of the "
		     "same kind", &e->where);
	  return FAILURE;
	}

      break;

    case INTRINSIC_NOT:
1969
      if (et0 (op1) != BT_LOGICAL)
1970 1971
	{
	  gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
1972
		     "operand", &op1->where);
1973 1974 1975 1976 1977 1978 1979 1980 1981
	  return FAILURE;
	}

      break;

    case INTRINSIC_AND:
    case INTRINSIC_OR:
    case INTRINSIC_EQV:
    case INTRINSIC_NEQV:
1982
      if ((*check_function) (op2) == FAILURE)
1983 1984
	return FAILURE;

1985
      if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
1986 1987 1988 1989 1990 1991 1992 1993
	{
	  gfc_error ("LOGICAL operands are required in expression at %L",
		     &e->where);
	  return FAILURE;
	}

      break;

1994 1995 1996
    case INTRINSIC_PARENTHESES:
      break;

1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
    default:
      gfc_error ("Only intrinsic operators can be used in expression at %L",
		 &e->where);
      return FAILURE;
    }

  return SUCCESS;

not_numeric:
  gfc_error ("Numeric operands are required in expression at %L", &e->where);

  return FAILURE;
}


2012 2013 2014 2015
static match
check_init_expr_arguments (gfc_expr *e)
{
  gfc_actual_arglist *ap;
2016

2017 2018 2019
  for (ap = e->value.function.actual; ap; ap = ap->next)
    if (check_init_expr (ap->expr) == FAILURE)
      return MATCH_ERROR;
2020

2021 2022 2023
  return MATCH_YES;
}

2024 2025
static gfc_try check_restricted (gfc_expr *);

2026 2027 2028 2029
/* F95, 7.1.6.1, Initialization expressions, (7)
   F2003, 7.1.7 Initialization expression, (8)  */

static match
2030
check_inquiry (gfc_expr *e, int not_restricted)
2031 2032
{
  const char *name;
2033 2034 2035 2036 2037 2038 2039 2040 2041
  const char *const *functions;

  static const char *const inquiry_func_f95[] = {
    "lbound", "shape", "size", "ubound",
    "bit_size", "len", "kind",
    "digits", "epsilon", "huge", "maxexponent", "minexponent",
    "precision", "radix", "range", "tiny",
    NULL
  };
2042

2043 2044 2045 2046 2047 2048
  static const char *const inquiry_func_f2003[] = {
    "lbound", "shape", "size", "ubound",
    "bit_size", "len", "kind",
    "digits", "epsilon", "huge", "maxexponent", "minexponent",
    "precision", "radix", "range", "tiny",
    "new_line", NULL
2049 2050 2051
  };

  int i;
2052 2053 2054 2055 2056
  gfc_actual_arglist *ap;

  if (!e->value.function.isym
      || !e->value.function.isym->inquiry)
    return MATCH_NO;
2057

2058 2059
  /* An undeclared parameter will get us here (PR25018).  */
  if (e->symtree == NULL)
2060
    return MATCH_NO;
2061

2062 2063
  name = e->symtree->n.sym->name;

2064 2065
  functions = (gfc_option.warn_std & GFC_STD_F2003) 
		? inquiry_func_f2003 : inquiry_func_f95;
2066

2067 2068 2069
  for (i = 0; functions[i]; i++)
    if (strcmp (functions[i], name) == 0)
      break;
2070

2071
  if (functions[i] == NULL)
2072
    return MATCH_ERROR;
2073

2074 2075
  /* At this point we have an inquiry function with a variable argument.  The
     type of the variable might be undefined, but we need it now, because the
2076
     arguments of these functions are not allowed to be undefined.  */
2077

2078
  for (ap = e->value.function.actual; ap; ap = ap->next)
2079
    {
2080 2081 2082 2083 2084 2085 2086 2087 2088
      if (!ap->expr)
	continue;

      if (ap->expr->ts.type == BT_UNKNOWN)
	{
	  if (ap->expr->symtree->n.sym->ts.type == BT_UNKNOWN
	      && gfc_set_default_type (ap->expr->symtree->n.sym, 0, gfc_current_ns)
	      == FAILURE)
	    return MATCH_NO;
2089

2090 2091 2092 2093 2094 2095 2096 2097 2098
	  ap->expr->ts = ap->expr->symtree->n.sym->ts;
	}

	/* Assumed character length will not reduce to a constant expression
	   with LEN, as required by the standard.  */
	if (i == 5 && not_restricted
	    && ap->expr->symtree->n.sym->ts.type == BT_CHARACTER
	    && ap->expr->symtree->n.sym->ts.cl->length == NULL)
	  {
2099
	    gfc_error ("Assumed character length variable '%s' in constant "
2100
		       "expression at %L", e->symtree->n.sym->name, &e->where);
2101 2102 2103 2104
	      return MATCH_ERROR;
	  }
	else if (not_restricted && check_init_expr (ap->expr) == FAILURE)
	  return MATCH_ERROR;
2105 2106 2107 2108 2109

	if (not_restricted == 0
	      && ap->expr->expr_type != EXPR_VARIABLE
	      && check_restricted (ap->expr) == FAILURE)
	  return MATCH_ERROR;
2110 2111
    }

2112 2113 2114
  return MATCH_YES;
}

2115

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
/* F95, 7.1.6.1, Initialization expressions, (5)
   F2003, 7.1.7 Initialization expression, (5)  */

static match
check_transformational (gfc_expr *e)
{
  static const char * const trans_func_f95[] = {
    "repeat", "reshape", "selected_int_kind",
    "selected_real_kind", "transfer", "trim", NULL
  };

  int i;
  const char *name;

  if (!e->value.function.isym
      || !e->value.function.isym->transformational)
    return MATCH_NO;

  name = e->symtree->n.sym->name;

  /* NULL() is dealt with below.  */
  if (strcmp ("null", name) == 0)
    return MATCH_NO;

  for (i = 0; trans_func_f95[i]; i++)
    if (strcmp (trans_func_f95[i], name) == 0)
      break;

2144 2145 2146 2147
  /* FIXME, F2003: implement translation of initialization
     expressions before enabling this check. For F95, error
     out if the transformational function is not in the list.  */
#if 0
2148 2149 2150 2151 2152
  if (trans_func_f95[i] == NULL
      && gfc_notify_std (GFC_STD_F2003, 
			 "transformational intrinsic '%s' at %L is not permitted "
			 "in an initialization expression", name, &e->where) == FAILURE)
    return MATCH_ERROR;
2153 2154 2155 2156 2157 2158 2159 2160
#else
  if (trans_func_f95[i] == NULL)
    {
      gfc_error("transformational intrinsic '%s' at %L is not permitted "
		"in an initialization expression", name, &e->where);
      return MATCH_ERROR;
    }
#endif
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185

  return check_init_expr_arguments (e);
}


/* F95, 7.1.6.1, Initialization expressions, (6)
   F2003, 7.1.7 Initialization expression, (6)  */

static match
check_null (gfc_expr *e)
{
  if (strcmp ("null", e->symtree->n.sym->name) != 0)
    return MATCH_NO;

  return check_init_expr_arguments (e);
}


static match
check_elemental (gfc_expr *e)
{
  if (!e->value.function.isym
      || !e->value.function.isym->elemental)
    return MATCH_NO;

2186 2187
  if (e->ts.type != BT_INTEGER
      && e->ts.type != BT_CHARACTER
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
      && gfc_notify_std (GFC_STD_F2003, "Extension: Evaluation of "
			"nonstandard initialization expression at %L",
			&e->where) == FAILURE)
    return MATCH_ERROR;

  return check_init_expr_arguments (e);
}


static match
check_conversion (gfc_expr *e)
{
  if (!e->value.function.isym
      || !e->value.function.isym->conversion)
    return MATCH_NO;

  return check_init_expr_arguments (e);
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
}


/* Verify that an expression is an initialization expression.  A side
   effect is that the expression tree is reduced to a single constant
   node if all goes well.  This would normally happen when the
   expression is constructed but function references are assumed to be
   intrinsics in the context of initialization expressions.  If
   FAILURE is returned an error message has been generated.  */

2215
static gfc_try
2216
check_init_expr (gfc_expr *e)
2217 2218
{
  match m;
2219
  gfc_try t;
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233

  if (e == NULL)
    return SUCCESS;

  switch (e->expr_type)
    {
    case EXPR_OP:
      t = check_intrinsic_op (e, check_init_expr);
      if (t == SUCCESS)
	t = gfc_simplify_expr (e, 0);

      break;

    case EXPR_FUNCTION:
2234
      t = FAILURE;
2235

2236
      if ((m = check_specification_function (e)) != MATCH_YES)
2237
	{
2238 2239 2240 2241 2242 2243
	  gfc_intrinsic_sym* isym;
          gfc_symbol* sym;

          sym = e->symtree->n.sym;
	  if (!gfc_is_intrinsic (sym, 0, e->where)
              || (m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)
2244 2245 2246 2247 2248 2249
	    {
	      gfc_error ("Function '%s' in initialization expression at %L "
			 "must be an intrinsic or a specification function",
			 e->symtree->n.sym->name, &e->where);
	      break;
	    }
2250

2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
	  if ((m = check_conversion (e)) == MATCH_NO
	      && (m = check_inquiry (e, 1)) == MATCH_NO
	      && (m = check_null (e)) == MATCH_NO
	      && (m = check_transformational (e)) == MATCH_NO
	      && (m = check_elemental (e)) == MATCH_NO)
	    {
	      gfc_error ("Intrinsic function '%s' at %L is not permitted "
			 "in an initialization expression",
			 e->symtree->n.sym->name, &e->where);
	      m = MATCH_ERROR;
	    }
2262

2263 2264
	  /* Try to scalarize an elemental intrinsic function that has an
	     array argument.  */
2265
          isym = gfc_find_function (e->symtree->n.sym->name);
2266
	  if (isym && isym->elemental
Paul Thomas committed
2267 2268
		&& (t = scalarize_intrinsic_call (e)) == SUCCESS)
	    break;
2269 2270
	}

2271
      if (m == MATCH_YES)
2272
	t = gfc_simplify_expr (e, 0);
2273

2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
      break;

    case EXPR_VARIABLE:
      t = SUCCESS;

      if (gfc_check_iter_variable (e) == SUCCESS)
	break;

      if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
	{
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
	  /* A PARAMETER shall not be used to define itself, i.e.
		REAL, PARAMETER :: x = transfer(0, x)
	     is invalid.  */
	  if (!e->symtree->n.sym->value)
	    {
	      gfc_error("PARAMETER '%s' is used at %L before its definition "
			"is complete", e->symtree->n.sym->name, &e->where);
	      t = FAILURE;
	    }
	  else
	    t = simplify_parameter_variable (e, 0);

2296 2297 2298
	  break;
	}

2299 2300 2301
      if (gfc_in_match_data ())
	break;

2302
      t = FAILURE;
2303 2304 2305 2306 2307 2308

      if (e->symtree->n.sym->as)
	{
	  switch (e->symtree->n.sym->as->type)
	    {
	      case AS_ASSUMED_SIZE:
2309
		gfc_error ("Assumed size array '%s' at %L is not permitted "
2310 2311
			   "in an initialization expression",
			   e->symtree->n.sym->name, &e->where);
2312
		break;
2313 2314

	      case AS_ASSUMED_SHAPE:
2315
		gfc_error ("Assumed shape array '%s' at %L is not permitted "
2316 2317
			   "in an initialization expression",
			   e->symtree->n.sym->name, &e->where);
2318
		break;
2319 2320

	      case AS_DEFERRED:
2321
		gfc_error ("Deferred array '%s' at %L is not permitted "
2322 2323
			   "in an initialization expression",
			   e->symtree->n.sym->name, &e->where);
2324
		break;
2325

2326 2327 2328 2329 2330 2331
	      case AS_EXPLICIT:
		gfc_error ("Array '%s' at %L is a variable, which does "
			   "not reduce to a constant expression",
			   e->symtree->n.sym->name, &e->where);
		break;

2332 2333 2334 2335 2336 2337 2338 2339 2340
	      default:
		gcc_unreachable();
	  }
	}
      else
	gfc_error ("Parameter '%s' at %L has not been declared or is "
		   "a variable, which does not reduce to a constant "
		   "expression", e->symtree->n.sym->name, &e->where);

2341 2342 2343 2344 2345 2346 2347 2348
      break;

    case EXPR_CONSTANT:
    case EXPR_NULL:
      t = SUCCESS;
      break;

    case EXPR_SUBSTRING:
2349
      t = check_init_expr (e->ref->u.ss.start);
2350 2351 2352
      if (t == FAILURE)
	break;

2353
      t = check_init_expr (e->ref->u.ss.end);
2354 2355 2356 2357 2358 2359
      if (t == SUCCESS)
	t = gfc_simplify_expr (e, 0);

      break;

    case EXPR_STRUCTURE:
2360 2361 2362 2363
      if (e->ts.is_iso_c)
	t = SUCCESS;
      else
	t = gfc_check_constructor (e, check_init_expr);
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
      break;

    case EXPR_ARRAY:
      t = gfc_check_constructor (e, check_init_expr);
      if (t == FAILURE)
	break;

      t = gfc_expand_constructor (e);
      if (t == FAILURE)
	break;

      t = gfc_check_constructor_type (e);
      break;

    default:
      gfc_internal_error ("check_init_expr(): Unknown expression type");
    }

  return t;
}

2385 2386 2387
/* Reduces a general expression to an initialization expression (a constant).
   This used to be part of gfc_match_init_expr.
   Note that this function doesn't free the given expression on FAILURE.  */
2388

2389 2390
gfc_try
gfc_reduce_init_expr (gfc_expr *expr)
2391
{
2392
  gfc_try t;
2393 2394 2395 2396 2397 2398 2399 2400

  gfc_init_expr = 1;
  t = gfc_resolve_expr (expr);
  if (t == SUCCESS)
    t = check_init_expr (expr);
  gfc_init_expr = 0;

  if (t == FAILURE)
2401
    return FAILURE;
2402 2403 2404

  if (expr->expr_type == EXPR_ARRAY
      && (gfc_check_constructor_type (expr) == FAILURE
2405 2406
      || gfc_expand_constructor (expr) == FAILURE))
    return FAILURE;
2407

2408 2409
  /* Not all inquiry functions are simplified to constant expressions
     so it is necessary to call check_inquiry again.  */ 
2410
  if (!gfc_is_constant_expr (expr) && check_inquiry (expr, 1) != MATCH_YES
2411
      && !gfc_in_match_data ())
2412 2413
    {
      gfc_error ("Initialization expression didn't reduce %C");
2414 2415 2416 2417 2418 2419 2420 2421
      return FAILURE;
    }

  return SUCCESS;
}


/* Match an initialization expression.  We work by first matching an
2422 2423 2424 2425 2426
   expression, then reducing it to a constant.  The reducing it to 
   constant part requires a global variable to flag the prohibition
   of a non-integer exponent in -std=f95 mode.  */

bool init_flag = false;
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436

match
gfc_match_init_expr (gfc_expr **result)
{
  gfc_expr *expr;
  match m;
  gfc_try t;

  expr = NULL;

2437 2438
  init_flag = true;

2439 2440
  m = gfc_match_expr (&expr);
  if (m != MATCH_YES)
2441 2442 2443 2444
    {
      init_flag = false;
      return m;
    }
2445 2446 2447 2448 2449

  t = gfc_reduce_init_expr (expr);
  if (t != SUCCESS)
    {
      gfc_free_expr (expr);
2450
      init_flag = false;
2451 2452
      return MATCH_ERROR;
    }
2453 2454

  *result = expr;
2455
  init_flag = false;
2456 2457 2458 2459 2460 2461 2462 2463 2464

  return MATCH_YES;
}


/* Given an actual argument list, test to see that each argument is a
   restricted expression and optionally if the expression type is
   integer or character.  */

2465
static gfc_try
2466
restricted_args (gfc_actual_arglist *a)
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
{
  for (; a; a = a->next)
    {
      if (check_restricted (a->expr) == FAILURE)
	return FAILURE;
    }

  return SUCCESS;
}


/************* Restricted/specification expressions *************/


/* Make sure a non-intrinsic function is a specification function.  */

2483
static gfc_try
2484
external_spec_function (gfc_expr *e)
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
{
  gfc_symbol *f;

  f = e->value.function.esym;

  if (f->attr.proc == PROC_ST_FUNCTION)
    {
      gfc_error ("Specification function '%s' at %L cannot be a statement "
		 "function", f->name, &e->where);
      return FAILURE;
    }

  if (f->attr.proc == PROC_INTERNAL)
    {
      gfc_error ("Specification function '%s' at %L cannot be an internal "
		 "function", f->name, &e->where);
      return FAILURE;
    }

2504
  if (!f->attr.pure && !f->attr.elemental)
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    {
      gfc_error ("Specification function '%s' at %L must be PURE", f->name,
		 &e->where);
      return FAILURE;
    }

  if (f->attr.recursive)
    {
      gfc_error ("Specification function '%s' at %L cannot be RECURSIVE",
		 f->name, &e->where);
      return FAILURE;
    }

2518
  return restricted_args (e->value.function.actual);
2519 2520 2521 2522
}


/* Check to see that a function reference to an intrinsic is a
2523
   restricted expression.  */
2524

2525
static gfc_try
2526
restricted_intrinsic (gfc_expr *e)
2527
{
2528
  /* TODO: Check constraints on inquiry functions.  7.1.6.2 (7).  */
2529
  if (check_inquiry (e, 0) == MATCH_YES)
2530
    return SUCCESS;
2531

2532
  return restricted_args (e->value.function.actual);
2533 2534 2535
}


2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
/* Check the expressions of an actual arglist.  Used by check_restricted.  */

static gfc_try
check_arglist (gfc_actual_arglist* arg, gfc_try (*checker) (gfc_expr*))
{
  for (; arg; arg = arg->next)
    if (checker (arg->expr) == FAILURE)
      return FAILURE;

  return SUCCESS;
}


/* Check the subscription expressions of a reference chain with a checking
   function; used by check_restricted.  */

static gfc_try
check_references (gfc_ref* ref, gfc_try (*checker) (gfc_expr*))
{
  int dim;

  if (!ref)
    return SUCCESS;

  switch (ref->type)
    {
    case REF_ARRAY:
      for (dim = 0; dim != ref->u.ar.dimen; ++dim)
	{
	  if (checker (ref->u.ar.start[dim]) == FAILURE)
	    return FAILURE;
	  if (checker (ref->u.ar.end[dim]) == FAILURE)
	    return FAILURE;
	  if (checker (ref->u.ar.stride[dim]) == FAILURE)
	    return FAILURE;
	}
      break;

    case REF_COMPONENT:
      /* Nothing needed, just proceed to next reference.  */
      break;

    case REF_SUBSTRING:
      if (checker (ref->u.ss.start) == FAILURE)
	return FAILURE;
      if (checker (ref->u.ss.end) == FAILURE)
	return FAILURE;
      break;

    default:
      gcc_unreachable ();
      break;
    }

  return check_references (ref->next, checker);
}


2594 2595 2596 2597
/* Verify that an expression is a restricted expression.  Like its
   cousin check_init_expr(), an error message is generated if we
   return FAILURE.  */

2598
static gfc_try
2599
check_restricted (gfc_expr *e)
2600
{
2601
  gfc_symbol* sym;
2602
  gfc_try t;
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616

  if (e == NULL)
    return SUCCESS;

  switch (e->expr_type)
    {
    case EXPR_OP:
      t = check_intrinsic_op (e, check_restricted);
      if (t == SUCCESS)
	t = gfc_simplify_expr (e, 0);

      break;

    case EXPR_FUNCTION:
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
      if (e->value.function.esym)
	{
	  t = check_arglist (e->value.function.actual, &check_restricted);
	  if (t == SUCCESS)
	    t = external_spec_function (e);
	}
      else
	{
	  if (e->value.function.isym && e->value.function.isym->inquiry)
	    t = SUCCESS;
	  else
	    t = check_arglist (e->value.function.actual, &check_restricted);

	  if (t == SUCCESS)
	    t = restricted_intrinsic (e);
	}
2633 2634 2635 2636 2637 2638
      break;

    case EXPR_VARIABLE:
      sym = e->symtree->n.sym;
      t = FAILURE;

2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
      /* If a dummy argument appears in a context that is valid for a
	 restricted expression in an elemental procedure, it will have
	 already been simplified away once we get here.  Therefore we
	 don't need to jump through hoops to distinguish valid from
	 invalid cases.  */
      if (sym->attr.dummy && sym->ns == gfc_current_ns
	  && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
	{
	  gfc_error ("Dummy argument '%s' not allowed in expression at %L",
		     sym->name, &e->where);
	  break;
	}

2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
      if (sym->attr.optional)
	{
	  gfc_error ("Dummy argument '%s' at %L cannot be OPTIONAL",
		     sym->name, &e->where);
	  break;
	}

      if (sym->attr.intent == INTENT_OUT)
	{
	  gfc_error ("Dummy argument '%s' at %L cannot be INTENT(OUT)",
		     sym->name, &e->where);
	  break;
	}

2666 2667 2668 2669
      /* Check reference chain if any.  */
      if (check_references (e->ref, &check_restricted) == FAILURE)
	break;

2670 2671 2672 2673 2674
      /* gfc_is_formal_arg broadcasts that a formal argument list is being
	 processed in resolve.c(resolve_formal_arglist).  This is done so
	 that host associated dummy array indices are accepted (PR23446).
	 This mechanism also does the same for the specification expressions
	 of array-valued functions.  */
2675 2676 2677 2678 2679
      if (e->error
	    || sym->attr.in_common
	    || sym->attr.use_assoc
	    || sym->attr.dummy
	    || sym->attr.implied_index
2680
	    || sym->attr.flavor == FL_PARAMETER
2681 2682 2683 2684 2685 2686
	    || (sym->ns && sym->ns == gfc_current_ns->parent)
	    || (sym->ns && gfc_current_ns->parent
		  && sym->ns == gfc_current_ns->parent->parent)
	    || (sym->ns->proc_name != NULL
		  && sym->ns->proc_name->attr.flavor == FL_MODULE)
	    || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns)))
2687 2688 2689 2690 2691 2692 2693
	{
	  t = SUCCESS;
	  break;
	}

      gfc_error ("Variable '%s' cannot appear in the expression at %L",
		 sym->name, &e->where);
2694 2695
      /* Prevent a repetition of the error.  */
      e->error = 1;
2696 2697 2698 2699 2700 2701 2702 2703
      break;

    case EXPR_NULL:
    case EXPR_CONSTANT:
      t = SUCCESS;
      break;

    case EXPR_SUBSTRING:
2704
      t = gfc_specification_expr (e->ref->u.ss.start);
2705 2706 2707
      if (t == FAILURE)
	break;

2708
      t = gfc_specification_expr (e->ref->u.ss.end);
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
      if (t == SUCCESS)
	t = gfc_simplify_expr (e, 0);

      break;

    case EXPR_STRUCTURE:
      t = gfc_check_constructor (e, check_restricted);
      break;

    case EXPR_ARRAY:
      t = gfc_check_constructor (e, check_restricted);
      break;

    default:
      gfc_internal_error ("check_restricted(): Unknown expression type");
    }

  return t;
}


/* Check to see that an expression is a specification expression.  If
   we return FAILURE, an error has been generated.  */

2733
gfc_try
2734
gfc_specification_expr (gfc_expr *e)
2735
{
2736

2737 2738
  if (e == NULL)
    return SUCCESS;
2739 2740 2741

  if (e->ts.type != BT_INTEGER)
    {
2742 2743
      gfc_error ("Expression at %L must be of INTEGER type, found %s",
		 &e->where, gfc_basic_typename (e->ts.type));
2744 2745 2746
      return FAILURE;
    }

2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
  if (e->expr_type == EXPR_FUNCTION
	  && !e->value.function.isym
	  && !e->value.function.esym
	  && !gfc_pure (e->symtree->n.sym))
    {
      gfc_error ("Function '%s' at %L must be PURE",
		 e->symtree->n.sym->name, &e->where);
      /* Prevent repeat error messages.  */
      e->symtree->n.sym->attr.pure = 1;
      return FAILURE;
    }

2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
  if (e->rank != 0)
    {
      gfc_error ("Expression at %L must be scalar", &e->where);
      return FAILURE;
    }

  if (gfc_simplify_expr (e, 0) == FAILURE)
    return FAILURE;

  return check_restricted (e);
}


/************** Expression conformance checks.  *************/

/* Given two expressions, make sure that the arrays are conformable.  */

2776
gfc_try
2777
gfc_check_conformance (const char *optype_msgid, gfc_expr *op1, gfc_expr *op2)
2778 2779 2780
{
  int op1_flag, op2_flag, d;
  mpz_t op1_size, op2_size;
2781
  gfc_try t;
2782 2783 2784 2785 2786 2787

  if (op1->rank == 0 || op2->rank == 0)
    return SUCCESS;

  if (op1->rank != op2->rank)
    {
2788 2789
      gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(optype_msgid),
		 op1->rank, op2->rank, &op1->where);
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
      return FAILURE;
    }

  t = SUCCESS;

  for (d = 0; d < op1->rank; d++)
    {
      op1_flag = gfc_array_dimen_size (op1, d, &op1_size) == SUCCESS;
      op2_flag = gfc_array_dimen_size (op2, d, &op2_size) == SUCCESS;

      if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
	{
2802 2803
	  gfc_error ("Different shape for %s at %L on dimension %d "
		     "(%d and %d)", _(optype_msgid), &op1->where, d + 1,
2804
		     (int) mpz_get_si (op1_size),
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
		     (int) mpz_get_si (op2_size));

	  t = FAILURE;
	}

      if (op1_flag)
	mpz_clear (op1_size);
      if (op2_flag)
	mpz_clear (op2_size);

      if (t == FAILURE)
	return FAILURE;
    }

  return SUCCESS;
}


/* Given an assignable expression and an arbitrary expression, make
   sure that the assignment can take place.  */

2826
gfc_try
2827
gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
2828 2829
{
  gfc_symbol *sym;
2830 2831
  gfc_ref *ref;
  int has_pointer;
2832 2833 2834

  sym = lvalue->symtree->n.sym;

2835 2836 2837 2838 2839
  /* Check INTENT(IN), unless the object itself is the component or
     sub-component of a pointer.  */
  has_pointer = sym->attr.pointer;

  for (ref = lvalue->ref; ref; ref = ref->next)
2840
    if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
2841 2842 2843 2844 2845 2846
      {
	has_pointer = 1;
	break;
      }

  if (!has_pointer && sym->attr.intent == INTENT_IN)
2847
    {
2848
      gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
2849 2850 2851 2852
		 sym->name, &lvalue->where);
      return FAILURE;
    }

2853 2854 2855 2856 2857
  /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
     variable local to a function subprogram.  Its existence begins when
     execution of the function is initiated and ends when execution of the
     function is terminated...
     Therefore, the left hand side is no longer a variable, when it is:  */
2858 2859
  if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
      && !sym->attr.external)
Paul Thomas committed
2860
    {
2861 2862 2863
      bool bad_proc;
      bad_proc = false;

2864
      /* (i) Use associated;  */
2865 2866 2867
      if (sym->attr.use_assoc)
	bad_proc = true;

2868
      /* (ii) The assignment is in the main program; or  */
2869 2870 2871
      if (gfc_current_ns->proc_name->attr.is_main_program)
	bad_proc = true;

2872
      /* (iii) A module or internal procedure...  */
2873
      if ((gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
2874
	   || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
2875 2876
	  && gfc_current_ns->parent
	  && (!(gfc_current_ns->parent->proc_name->attr.function
2877
		|| gfc_current_ns->parent->proc_name->attr.subroutine)
2878 2879
	      || gfc_current_ns->parent->proc_name->attr.is_main_program))
	{
2880
	  /* ... that is not a function...  */ 
2881 2882 2883
	  if (!gfc_current_ns->proc_name->attr.function)
	    bad_proc = true;

2884
	  /* ... or is not an entry and has a different name.  */
2885 2886 2887
	  if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
	    bad_proc = true;
	}
Paul Thomas committed
2888

2889 2890 2891 2892 2893 2894 2895 2896 2897
      /* (iv) Host associated and not the function symbol or the
	      parent result.  This picks up sibling references, which
	      cannot be entries.  */
      if (!sym->attr.entry
	    && sym->ns == gfc_current_ns->parent
	    && sym != gfc_current_ns->proc_name
	    && sym != gfc_current_ns->parent->proc_name->result)
	bad_proc = true;

2898 2899 2900 2901 2902 2903
      if (bad_proc)
	{
	  gfc_error ("'%s' at %L is not a VALUE", sym->name, &lvalue->where);
	  return FAILURE;
	}
    }
Paul Thomas committed
2904

2905 2906
  if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
    {
2907 2908
      gfc_error ("Incompatible ranks %d and %d in assignment at %L",
		 lvalue->rank, rvalue->rank, &lvalue->where);
2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
      return FAILURE;
    }

  if (lvalue->ts.type == BT_UNKNOWN)
    {
      gfc_error ("Variable type is UNKNOWN in assignment at %L",
		 &lvalue->where);
      return FAILURE;
    }

2919 2920
  if (rvalue->expr_type == EXPR_NULL)
    {  
2921
      if (has_pointer && (ref == NULL || ref->next == NULL)
2922 2923 2924 2925 2926 2927 2928 2929 2930
	  && lvalue->symtree->n.sym->attr.data)
        return SUCCESS;
      else
	{
	  gfc_error ("NULL appears on right-hand side in assignment at %L",
		     &rvalue->where);
	  return FAILURE;
	}
    }
2931

Asher Langton committed
2932 2933
   if (sym->attr.cray_pointee
       && lvalue->ref != NULL
2934
       && lvalue->ref->u.ar.type == AR_FULL
Asher Langton committed
2935 2936
       && lvalue->ref->u.ar.as->cp_was_assumed)
     {
2937 2938
       gfc_error ("Vector assignment to assumed-size Cray Pointee at %L "
		  "is illegal", &lvalue->where);
Asher Langton committed
2939 2940 2941
       return FAILURE;
     }

2942
  /* This is possibly a typo: x = f() instead of x => f().  */
2943 2944 2945 2946 2947 2948
  if (gfc_option.warn_surprising 
      && rvalue->expr_type == EXPR_FUNCTION
      && rvalue->symtree->n.sym->attr.pointer)
    gfc_warning ("POINTER valued function appears on right-hand side of "
		 "assignment at %L", &rvalue->where);

2949 2950
  /* Check size of array assignments.  */
  if (lvalue->rank != 0 && rvalue->rank != 0
2951
      && gfc_check_conformance ("array assignment", lvalue, rvalue) != SUCCESS)
2952 2953
    return FAILURE;

2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
  if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER
      && lvalue->symtree->n.sym->attr.data
      && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L used to "
                         "initialize non-integer variable '%s'",
			 &rvalue->where, lvalue->symtree->n.sym->name)
	 == FAILURE)
    return FAILURE;
  else if (rvalue->is_boz && !lvalue->symtree->n.sym->attr.data
      && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
			 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
			 &rvalue->where) == FAILURE)
    return FAILURE;

  /* Handle the case of a BOZ literal on the RHS.  */
  if (rvalue->is_boz && lvalue->ts.type != BT_INTEGER)
    {
2970
      int rc;
2971 2972 2973 2974
      if (gfc_option.warn_surprising)
        gfc_warning ("BOZ literal at %L is bitwise transferred "
                     "non-integer symbol '%s'", &rvalue->where,
                     lvalue->symtree->n.sym->name);
2975 2976
      if (!gfc_convert_boz (rvalue, &lvalue->ts))
	return FAILURE;
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992
      if ((rc = gfc_range_check (rvalue)) != ARITH_OK)
	{
	  if (rc == ARITH_UNDERFLOW)
	    gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
		       ". This check can be disabled with the option "
		       "-fno-range-check", &rvalue->where);
	  else if (rc == ARITH_OVERFLOW)
	    gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
		       ". This check can be disabled with the option "
		       "-fno-range-check", &rvalue->where);
	  else if (rc == ARITH_NAN)
	    gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
		       ". This check can be disabled with the option "
		       "-fno-range-check", &rvalue->where);
	  return FAILURE;
	}
2993 2994
    }

2995 2996 2997
  if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
    return SUCCESS;

2998
  /* Only DATA Statements come here.  */
2999 3000
  if (!conform)
    {
3001 3002 3003 3004
      /* Numeric can be converted to any other numeric. And Hollerith can be
	 converted to any other type.  */
      if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
	  || rvalue->ts.type == BT_HOLLERITH)
3005 3006
	return SUCCESS;

3007 3008 3009
      if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
	return SUCCESS;

3010 3011 3012
      gfc_error ("Incompatible types in DATA statement at %L; attempted "
		 "conversion of %s to %s", &lvalue->where,
		 gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
3013 3014 3015 3016

      return FAILURE;
    }

3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
  /* Assignment is the only case where character variables of different
     kind values can be converted into one another.  */
  if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
    {
      if (lvalue->ts.kind != rvalue->ts.kind)
	gfc_convert_chartype (rvalue, &lvalue->ts);

      return SUCCESS;
    }

3027 3028 3029 3030 3031 3032 3033 3034
  return gfc_convert_type (rvalue, &lvalue->ts, 1);
}


/* Check that a pointer assignment is OK.  We first check lvalue, and
   we only check rvalue if it's not an assignment to NULL() or a
   NULLIFY statement.  */

3035
gfc_try
3036
gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
3037 3038
{
  symbol_attribute attr;
3039
  gfc_ref *ref;
3040
  int is_pure;
3041
  int pointer, check_intent_in;
3042

3043 3044
  if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN
      && !lvalue->symtree->n.sym->attr.proc_pointer)
3045 3046 3047 3048 3049 3050
    {
      gfc_error ("Pointer assignment target is not a POINTER at %L",
		 &lvalue->where);
      return FAILURE;
    }

Paul Thomas committed
3051
  if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
3052 3053
      && lvalue->symtree->n.sym->attr.use_assoc
      && !lvalue->symtree->n.sym->attr.proc_pointer)
Paul Thomas committed
3054 3055 3056 3057 3058 3059 3060
    {
      gfc_error ("'%s' in the pointer assignment at %L cannot be an "
		 "l-value since it is a procedure",
		 lvalue->symtree->n.sym->name, &lvalue->where);
      return FAILURE;
    }

3061 3062 3063 3064

  /* Check INTENT(IN), unless the object itself is the component or
     sub-component of a pointer.  */
  check_intent_in = 1;
3065 3066
  pointer = lvalue->symtree->n.sym->attr.pointer
	      | lvalue->symtree->n.sym->attr.proc_pointer;
3067 3068 3069 3070

  for (ref = lvalue->ref; ref; ref = ref->next)
    {
      if (pointer)
3071
	check_intent_in = 0;
3072

3073 3074
      if (ref->type == REF_COMPONENT)
	pointer = ref->u.c.component->attr.pointer;
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100

      if (ref->type == REF_ARRAY && ref->next == NULL)
	{
	  if (ref->u.ar.type == AR_FULL)
	    break;

	  if (ref->u.ar.type != AR_SECTION)
	    {
	      gfc_error ("Expected bounds specification for '%s' at %L",
			 lvalue->symtree->n.sym->name, &lvalue->where);
	      return FAILURE;
	    }

	  if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Bounds "
			      "specification for '%s' in pointer assignment "
                              "at %L", lvalue->symtree->n.sym->name,
			      &lvalue->where) == FAILURE)
            return FAILURE;

	  gfc_error ("Pointer bounds remapping at %L is not yet implemented "
		     "in gfortran", &lvalue->where);
	  /* TODO: See PR 29785. Add checks that all lbounds are specified and
	     either never or always the upper-bound; strides shall not be
	     present.  */
	  return FAILURE;
	}
3101 3102 3103 3104 3105
    }

  if (check_intent_in && lvalue->symtree->n.sym->attr.intent == INTENT_IN)
    {
      gfc_error ("Cannot assign to INTENT(IN) variable '%s' at %L",
3106
		 lvalue->symtree->n.sym->name, &lvalue->where);
3107 3108 3109 3110
      return FAILURE;
    }

  if (!pointer)
3111 3112 3113 3114 3115 3116 3117
    {
      gfc_error ("Pointer assignment to non-POINTER at %L", &lvalue->where);
      return FAILURE;
    }

  is_pure = gfc_pure (NULL);

3118 3119
  if (is_pure && gfc_impure_variable (lvalue->symtree->n.sym)
	&& lvalue->symtree->n.sym->value != rvalue)
3120
    {
3121
      gfc_error ("Bad pointer object in PURE procedure at %L", &lvalue->where);
3122 3123 3124 3125 3126 3127
      return FAILURE;
    }

  /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
     kind, etc for lvalue and rvalue must match, and rvalue must be a
     pure variable if we're in a pure function.  */
3128
  if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
3129 3130
    return SUCCESS;

3131
  /* Checks on rvalue for procedure pointer assignments.  */
3132
  if (lvalue->symtree->n.sym->attr.proc_pointer)
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
    {
      attr = gfc_expr_attr (rvalue);
      if (!((rvalue->expr_type == EXPR_NULL)
	    || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
	    || (rvalue->expr_type == EXPR_VARIABLE
		&& attr.flavor == FL_PROCEDURE)))
	{
	  gfc_error ("Invalid procedure pointer assignment at %L",
		     &rvalue->where);
	  return FAILURE;
	}
3144 3145 3146 3147 3148
      if (attr.abstract)
	{
	  gfc_error ("Abstract interface '%s' is invalid "
		     "in procedure pointer assignment at %L",
		     rvalue->symtree->name, &rvalue->where);
3149
	  return FAILURE;
3150
	}
3151 3152 3153 3154 3155 3156 3157
      if (rvalue->expr_type == EXPR_VARIABLE
	  && !gfc_compare_interfaces (lvalue->symtree->n.sym,
				      rvalue->symtree->n.sym, 0))
	{
	  gfc_error ("Interfaces don't match "
		     "in procedure pointer assignment at %L", &rvalue->where);
	  return FAILURE;
3158
	}
3159 3160
      return SUCCESS;
    }
3161

3162
  if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
3163
    {
3164 3165 3166
      gfc_error ("Different types in pointer assignment at %L; attempted "
		 "assignment of %s to %s", &lvalue->where, 
		 gfc_typename (&rvalue->ts), gfc_typename (&lvalue->ts));
3167 3168
      return FAILURE;
    }
3169

3170 3171
  if (lvalue->ts.kind != rvalue->ts.kind)
    {
3172
      gfc_error ("Different kind type parameters in pointer "
3173 3174 3175
		 "assignment at %L", &lvalue->where);
      return FAILURE;
    }
3176

3177 3178 3179
  if (lvalue->rank != rvalue->rank)
    {
      gfc_error ("Different ranks in pointer assignment at %L",
3180
		 &lvalue->where);
3181 3182 3183 3184 3185 3186 3187
      return FAILURE;
    }

  /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X).  */
  if (rvalue->expr_type == EXPR_NULL)
    return SUCCESS;

3188
  if (lvalue->ts.type == BT_CHARACTER)
Paul Thomas committed
3189
    {
3190 3191 3192
      gfc_try t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
      if (t == FAILURE)
	return FAILURE;
Paul Thomas committed
3193 3194
    }

3195 3196 3197
  if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
    lvalue->symtree->n.sym->attr.subref_array_pointer = 1;

3198 3199 3200
  attr = gfc_expr_attr (rvalue);
  if (!attr.target && !attr.pointer)
    {
3201
      gfc_error ("Pointer assignment target is neither TARGET "
3202 3203 3204
		 "nor POINTER at %L", &rvalue->where);
      return FAILURE;
    }
3205

3206 3207
  if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    {
3208
      gfc_error ("Bad target in pointer assignment in PURE "
3209 3210
		 "procedure at %L", &rvalue->where);
    }
3211

3212 3213 3214 3215 3216 3217 3218
  if (gfc_has_vector_index (rvalue))
    {
      gfc_error ("Pointer assignment with vector subscript "
		 "on rhs at %L", &rvalue->where);
      return FAILURE;
    }

3219 3220
  if (attr.is_protected && attr.use_assoc
      && !(attr.pointer || attr.proc_pointer))
3221
    {
3222
      gfc_error ("Pointer assignment target has PROTECTED "
3223
		 "attribute at %L", &rvalue->where);
3224 3225 3226
      return FAILURE;
    }

3227 3228 3229 3230 3231
  return SUCCESS;
}


/* Relative of gfc_check_assign() except that the lvalue is a single
3232
   symbol.  Used for initialization assignments.  */
3233

3234
gfc_try
3235
gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
3236 3237
{
  gfc_expr lvalue;
3238
  gfc_try r;
3239 3240 3241 3242 3243 3244 3245

  memset (&lvalue, '\0', sizeof (gfc_expr));

  lvalue.expr_type = EXPR_VARIABLE;
  lvalue.ts = sym->ts;
  if (sym->as)
    lvalue.rank = sym->as->rank;
3246
  lvalue.symtree = (gfc_symtree *) gfc_getmem (sizeof (gfc_symtree));
3247 3248 3249
  lvalue.symtree->n.sym = sym;
  lvalue.where = sym->declared_at;

3250
  if (sym->attr.pointer || sym->attr.proc_pointer)
3251 3252 3253
    r = gfc_check_pointer_assign (&lvalue, rvalue);
  else
    r = gfc_check_assign (&lvalue, rvalue, 1);
3254 3255 3256 3257 3258

  gfc_free (lvalue.symtree);

  return r;
}
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271


/* Get an expression for a default initializer.  */

gfc_expr *
gfc_default_initializer (gfc_typespec *ts)
{
  gfc_constructor *tail;
  gfc_expr *init;
  gfc_component *c;

  /* See if we have a default initializer.  */
  for (c = ts->derived->components; c; c = c->next)
3272
    if (c->initializer || c->attr.allocatable)
3273
      break;
3274

3275
  if (!c)
3276 3277 3278
    return NULL;

  /* Build the constructor.  */
3279
  init = gfc_get_expr ();
3280 3281 3282
  init->expr_type = EXPR_STRUCTURE;
  init->ts = *ts;
  init->where = ts->derived->declared_at;
3283

3284 3285 3286 3287
  tail = NULL;
  for (c = ts->derived->components; c; c = c->next)
    {
      if (tail == NULL)
3288
	init->value.constructor = tail = gfc_get_constructor ();
3289
      else
3290 3291 3292 3293
	{
	  tail->next = gfc_get_constructor ();
	  tail = tail->next;
	}
3294 3295

      if (c->initializer)
3296
	tail->expr = gfc_copy_expr (c->initializer);
Paul Thomas committed
3297

3298
      if (c->attr.allocatable)
Paul Thomas committed
3299 3300 3301 3302 3303
	{
	  tail->expr = gfc_get_expr ();
	  tail->expr->expr_type = EXPR_NULL;
	  tail->expr->ts = c->ts;
	}
3304 3305 3306
    }
  return init;
}
3307 3308 3309 3310 3311 3312 3313


/* Given a symbol, create an expression node with that symbol as a
   variable. If the symbol is array valued, setup a reference of the
   whole array.  */

gfc_expr *
3314
gfc_get_variable_expr (gfc_symtree *var)
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333
{
  gfc_expr *e;

  e = gfc_get_expr ();
  e->expr_type = EXPR_VARIABLE;
  e->symtree = var;
  e->ts = var->n.sym->ts;

  if (var->n.sym->as != NULL)
    {
      e->rank = var->n.sym->as->rank;
      e->ref = gfc_get_ref ();
      e->ref->type = REF_ARRAY;
      e->ref->u.ar.type = AR_FULL;
    }

  return e;
}

3334

Paul Thomas committed
3335
/* General expression traversal function.  */
3336

Paul Thomas committed
3337 3338 3339 3340
bool
gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
		   bool (*func)(gfc_expr *, gfc_symbol *, int*),
		   int f)
3341
{
Paul Thomas committed
3342
  gfc_array_ref ar;
3343
  gfc_ref *ref;
Paul Thomas committed
3344 3345
  gfc_actual_arglist *args;
  gfc_constructor *c;
3346 3347
  int i;

Paul Thomas committed
3348 3349
  if (!expr)
    return false;
3350

3351 3352
  if ((*func) (expr, sym, &f))
    return true;
3353

3354 3355 3356 3357 3358 3359
  if (expr->ts.type == BT_CHARACTER
	&& expr->ts.cl
	&& expr->ts.cl->length
	&& expr->ts.cl->length->expr_type != EXPR_CONSTANT
	&& gfc_traverse_expr (expr->ts.cl->length, sym, func, f))
    return true;
3360

3361 3362
  switch (expr->expr_type)
    {
Paul Thomas committed
3363 3364 3365 3366 3367 3368
    case EXPR_FUNCTION:
      for (args = expr->value.function.actual; args; args = args->next)
	{
	  if (gfc_traverse_expr (args->expr, sym, func, f))
	    return true;
	}
3369 3370
      break;

3371
    case EXPR_VARIABLE:
3372 3373 3374 3375 3376 3377 3378 3379
    case EXPR_CONSTANT:
    case EXPR_NULL:
    case EXPR_SUBSTRING:
      break;

    case EXPR_STRUCTURE:
    case EXPR_ARRAY:
      for (c = expr->value.constructor; c; c = c->next)
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394
	{
	  if (gfc_traverse_expr (c->expr, sym, func, f))
	    return true;
	  if (c->iterator)
	    {
	      if (gfc_traverse_expr (c->iterator->var, sym, func, f))
		return true;
	      if (gfc_traverse_expr (c->iterator->start, sym, func, f))
		return true;
	      if (gfc_traverse_expr (c->iterator->end, sym, func, f))
		return true;
	      if (gfc_traverse_expr (c->iterator->step, sym, func, f))
		return true;
	    }
	}
3395 3396
      break;

Paul Thomas committed
3397 3398 3399 3400 3401 3402 3403
    case EXPR_OP:
      if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
	return true;
      if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
	return true;
      break;

3404 3405 3406 3407 3408
    default:
      gcc_unreachable ();
      break;
    }

Paul Thomas committed
3409 3410 3411
  ref = expr->ref;
  while (ref != NULL)
    {
3412
      switch (ref->type)
3413
	{
Paul Thomas committed
3414 3415 3416
	case  REF_ARRAY:
	  ar = ref->u.ar;
	  for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3417
	    {
Paul Thomas committed
3418 3419 3420 3421 3422 3423
	      if (gfc_traverse_expr (ar.start[i], sym, func, f))
		return true;
	      if (gfc_traverse_expr (ar.end[i], sym, func, f))
		return true;
	      if (gfc_traverse_expr (ar.stride[i], sym, func, f))
		return true;
3424 3425
	    }
	  break;
Paul Thomas committed
3426

3427
	case REF_SUBSTRING:
Paul Thomas committed
3428 3429 3430 3431
	  if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
	    return true;
	  if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
	    return true;
3432
	  break;
Paul Thomas committed
3433

3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454
	case REF_COMPONENT:
	  if (ref->u.c.component->ts.type == BT_CHARACTER
		&& ref->u.c.component->ts.cl
		&& ref->u.c.component->ts.cl->length
		&& ref->u.c.component->ts.cl->length->expr_type
		     != EXPR_CONSTANT
		&& gfc_traverse_expr (ref->u.c.component->ts.cl->length,
				      sym, func, f))
	    return true;

	  if (ref->u.c.component->as)
	    for (i = 0; i < ref->u.c.component->as->rank; i++)
	      {
		if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
				       sym, func, f))
		  return true;
		if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
				       sym, func, f))
		  return true;
	      }
	  break;
Paul Thomas committed
3455

3456 3457 3458
	default:
	  gcc_unreachable ();
	}
Paul Thomas committed
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
      ref = ref->next;
    }
  return false;
}

/* Traverse expr, marking all EXPR_VARIABLE symbols referenced.  */

static bool
expr_set_symbols_referenced (gfc_expr *expr,
			     gfc_symbol *sym ATTRIBUTE_UNUSED,
			     int *f ATTRIBUTE_UNUSED)
{
3471 3472
  if (expr->expr_type != EXPR_VARIABLE)
    return false;
Paul Thomas committed
3473 3474 3475 3476 3477 3478 3479 3480
  gfc_set_sym_referenced (expr->symtree->n.sym);
  return false;
}

void
gfc_expr_set_symbols_referenced (gfc_expr *expr)
{
  gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
3481
}
3482 3483 3484 3485


/* Walk an expression tree and check each variable encountered for being typed.
   If strict is not set, a top-level variable is tolerated untyped in -std=gnu
3486 3487
   mode as is a basic arithmetic expression using those; this is for things in
   legacy-code like:
3488 3489

     INTEGER :: arr(n), n
3490
     INTEGER :: arr(n + 1), n
3491 3492 3493

   The namespace is needed for IMPLICIT typing.  */

3494 3495 3496 3497 3498
static gfc_namespace* check_typed_ns;

static bool
expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
                       int* f ATTRIBUTE_UNUSED)
3499 3500 3501
{
  gfc_try t;

3502 3503
  if (e->expr_type != EXPR_VARIABLE)
    return false;
3504

3505 3506 3507
  gcc_assert (e->symtree);
  t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
                              true, e->where);
3508

3509 3510
  return (t == FAILURE);
}
3511

3512 3513 3514 3515
gfc_try
gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
{
  bool error_found;
3516

3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
  /* If this is a top-level variable or EXPR_OP, do the check with strict given
     to us.  */
  if (!strict)
    {
      if (e->expr_type == EXPR_VARIABLE && !e->ref)
	return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);

      if (e->expr_type == EXPR_OP)
	{
	  gfc_try t = SUCCESS;

	  gcc_assert (e->value.op.op1);
	  t = gfc_expr_check_typed (e->value.op.op1, ns, strict);

	  if (t == SUCCESS && e->value.op.op2)
	    t = gfc_expr_check_typed (e->value.op.op2, ns, strict);

	  return t;
	}
    }
3537

3538 3539 3540
  /* Otherwise, walk the expression and do it strictly.  */
  check_typed_ns = ns;
  error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
3541

3542
  return error_found ? FAILURE : SUCCESS;
3543
}
3544 3545 3546 3547 3548 3549 3550 3551

/* Walk an expression tree and replace all symbols with a corresponding symbol
   in the formal_ns of "sym". Needed for copying interfaces in PROCEDURE
   statements. The boolean return value is required by gfc_traverse_expr.  */

static bool
replace_symbol (gfc_expr *expr, gfc_symbol *sym, int *i ATTRIBUTE_UNUSED)
{
3552 3553 3554
  if ((expr->expr_type == EXPR_VARIABLE 
       || (expr->expr_type == EXPR_FUNCTION
	   && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
3555
      && expr->symtree->n.sym->ns == sym->ts.interface->formal_ns)
3556 3557
    {
      gfc_symtree *stree;
3558 3559 3560 3561 3562 3563
      gfc_namespace *ns = sym->formal_ns;
      /* Don't use gfc_get_symtree as we prefer to fail badly if we don't find
	 the symtree rather than create a new one (and probably fail later).  */
      stree = gfc_find_symtree (ns ? ns->sym_root : gfc_current_ns->sym_root,
		      		expr->symtree->n.sym->name);
      gcc_assert (stree);
3564
      stree->n.sym->attr = expr->symtree->n.sym->attr;
3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
      expr->symtree = stree;
    }
  return false;
}

void
gfc_expr_replace_symbols (gfc_expr *expr, gfc_symbol *dest)
{
  gfc_traverse_expr (expr, dest, &replace_symbol, 0);
}