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

7
This file is part of GCC.
8

9 10
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
11
Software Foundation; either version 3, or (at your option) any later
12
version.
13

14 15 16 17
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.
18 19

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

#include "config.h"
24
#include "system.h"
25 26 27
#include "gfortran.h"
#include "arith.h"
#include "match.h"
28
#include "target-memory.h" /* for gfc_convert_boz */
Jerry DeLisle committed
29
#include "constructor.h"
30

Jerry DeLisle committed
31 32 33 34 35 36 37 38 39 40 41

/* The following set of functions provide access to gfc_expr* of
   various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE.

   There are two functions available elsewhere that provide
   slightly different flavours of variables.  Namely:
     expr.c (gfc_get_variable_expr)
     symbol.c (gfc_lval_expr_from_sym)
   TODO: Merge these functions, if possible.  */

/* Get a new expression node.  */
42 43 44 45 46 47

gfc_expr *
gfc_get_expr (void)
{
  gfc_expr *e;

48
  e = XCNEW (gfc_expr);
49 50 51 52 53 54 55 56
  gfc_clear_ts (&e->ts);
  e->shape = NULL;
  e->ref = NULL;
  e->symtree = NULL;
  return e;
}


Jerry DeLisle committed
57 58
/* Get a new expression node that is an array constructor
   of given type and kind.  */
59

Jerry DeLisle committed
60 61
gfc_expr *
gfc_get_array_expr (bt type, int kind, locus *where)
62
{
Jerry DeLisle committed
63
  gfc_expr *e;
64

Jerry DeLisle committed
65 66 67 68 69 70 71 72 73 74 75 76
  e = gfc_get_expr ();
  e->expr_type = EXPR_ARRAY;
  e->value.constructor = NULL;
  e->rank = 1;
  e->shape = NULL;

  e->ts.type = type;
  e->ts.kind = kind;
  if (where)
    e->where = *where;

  return e;
77 78 79
}


Jerry DeLisle committed
80
/* Get a new expression node that is the NULL expression.  */
81

Jerry DeLisle committed
82 83
gfc_expr *
gfc_get_null_expr (locus *where)
84
{
Jerry DeLisle committed
85
  gfc_expr *e;
86

Jerry DeLisle committed
87 88 89
  e = gfc_get_expr ();
  e->expr_type = EXPR_NULL;
  e->ts.type = BT_UNKNOWN;
90

Jerry DeLisle committed
91 92 93 94 95 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  if (where)
    e->where = *where;

  return e;
}


/* Get a new expression node that is an operator expression node.  */

gfc_expr *
gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
                      gfc_expr *op1, gfc_expr *op2)
{
  gfc_expr *e;

  e = gfc_get_expr ();
  e->expr_type = EXPR_OP;
  e->value.op.op = op;
  e->value.op.op1 = op1;
  e->value.op.op2 = op2;

  if (where)
    e->where = *where;

  return e;
}


/* Get a new expression node that is an structure constructor
   of given type and kind.  */

gfc_expr *
gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
{
  gfc_expr *e;

  e = gfc_get_expr ();
  e->expr_type = EXPR_STRUCTURE;
  e->value.constructor = NULL;

  e->ts.type = type;
  e->ts.kind = kind;
  if (where)
    e->where = *where;

  return e;
}


/* Get a new expression node that is an constant of given type and kind.  */

gfc_expr *
gfc_get_constant_expr (bt type, int kind, locus *where)
{
  gfc_expr *e;

  if (!where)
    gfc_internal_error ("gfc_get_constant_expr(): locus 'where' cannot be NULL");

  e = gfc_get_expr ();

  e->expr_type = EXPR_CONSTANT;
  e->ts.type = type;
  e->ts.kind = kind;
  e->where = *where;

  switch (type)
158
    {
Jerry DeLisle committed
159 160 161
    case BT_INTEGER:
      mpz_init (e->value.integer);
      break;
162

Jerry DeLisle committed
163 164 165 166
    case BT_REAL:
      gfc_set_model_kind (kind);
      mpfr_init (e->value.real);
      break;
167

Jerry DeLisle committed
168 169 170 171
    case BT_COMPLEX:
      gfc_set_model_kind (kind);
      mpc_init2 (e->value.complex, mpfr_get_default_prec());
      break;
172

Jerry DeLisle committed
173 174
    default:
      break;
175 176
    }

Jerry DeLisle committed
177
  return e;
178 179 180
}


Jerry DeLisle committed
181 182 183
/* Get a new expression node that is an string constant.
   If no string is passed, a string of len is allocated,
   blanked and null-terminated.  */
184

Jerry DeLisle committed
185 186
gfc_expr *
gfc_get_character_expr (int kind, locus *where, const char *src, int len)
187
{
Jerry DeLisle committed
188 189
  gfc_expr *e;
  gfc_char_t *dest;
190

Jerry DeLisle committed
191
  if (!src)
192
    {
Jerry DeLisle committed
193 194 195 196 197 198
      dest = gfc_get_wide_string (len + 1);
      gfc_wide_memset (dest, ' ', len);
      dest[len] = '\0';
    }
  else
    dest = gfc_char_to_widechar (src);
199

Jerry DeLisle committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  e = gfc_get_constant_expr (BT_CHARACTER, kind,
                            where ? where : &gfc_current_locus);
  e->value.character.string = dest;
  e->value.character.length = len;

  return e;
}


/* Get a new expression node that is an integer constant.  */

gfc_expr *
gfc_get_int_expr (int kind, locus *where, int value)
{
  gfc_expr *p;
  p = gfc_get_constant_expr (BT_INTEGER, kind,
			     where ? where : &gfc_current_locus);

218
  mpz_set_si (p->value.integer, value);
Jerry DeLisle committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

  return p;
}


/* Get a new expression node that is a logical constant.  */

gfc_expr *
gfc_get_logical_expr (int kind, locus *where, bool value)
{
  gfc_expr *p;
  p = gfc_get_constant_expr (BT_LOGICAL, kind,
			     where ? where : &gfc_current_locus);

  p->value.logical = value;

  return p;
}


gfc_expr *
gfc_get_iokind_expr (locus *where, io_kind k)
{
  gfc_expr *e;

  /* Set the types to something compatible with iokind. This is needed to
     get through gfc_free_expr later since iokind really has no Basic Type,
     BT, of its own.  */

  e = gfc_get_expr ();
  e->expr_type = EXPR_CONSTANT;
  e->ts.type = BT_LOGICAL;
  e->value.iokind = k;
  e->where = *where;

  return e;
}


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

gfc_expr *
gfc_copy_expr (gfc_expr *p)
{
  gfc_expr *q;
  gfc_char_t *s;
  char *c;

  if (p == NULL)
    return NULL;

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

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

    case EXPR_CONSTANT:
      /* Copy target representation, if it exists.  */
      if (p->representation.string)
286
	{
Jerry DeLisle committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	  c = XCNEWVEC (char, p->representation.length + 1);
	  q->representation.string = c;
	  memcpy (c, p->representation.string, (p->representation.length + 1));
	}

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

	case BT_REAL:
	  gfc_set_model_kind (q->ts.kind);
	  mpfr_init (q->value.real);
	  mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
	  break;

	case BT_COMPLEX:
	  gfc_set_model_kind (q->ts.kind);
	  mpc_init2 (q->value.complex, mpfr_get_default_prec());
	  mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
	  break;

	case BT_CHARACTER:
	  if (p->representation.string)
	    q->value.character.string
	      = gfc_char_to_widechar (q->representation.string);
	  else
316
	    {
Jerry DeLisle committed
317 318
	      s = gfc_get_wide_string (p->value.character.length + 1);
	      q->value.character.string = s;
319

Jerry DeLisle committed
320 321 322 323 324 325 326 327 328 329 330 331 332
	      /* 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,
			(p->value.character.length + 1) * sizeof (gfc_char_t));
	    }
333 334
	  break;

Jerry DeLisle committed
335 336 337 338
	case BT_HOLLERITH:
	case BT_LOGICAL:
	case BT_DERIVED:
	case BT_CLASS:
339
	case BT_ASSUMED:
Jerry DeLisle committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	  break;		/* Already done.  */

	case BT_PROCEDURE:
        case BT_VOID:
           /* Should never be reached.  */
	case BT_UNKNOWN:
	  gfc_internal_error ("gfc_copy_expr(): Bad expr node");
	  /* Not reached.  */
	}

      break;

    case EXPR_OP:
      switch (q->value.op.op)
	{
	case INTRINSIC_NOT:
	case INTRINSIC_PARENTHESES:
	case INTRINSIC_UPLUS:
	case INTRINSIC_UMINUS:
	  q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
360 361
	  break;

Jerry DeLisle committed
362 363 364
	default:		/* Binary operators.  */
	  q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
	  q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
365 366 367
	  break;
	}

Jerry DeLisle committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
      break;

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

    case EXPR_COMPCALL:
    case EXPR_PPC:
      q->value.compcall.actual =
	gfc_copy_actual_arglist (p->value.compcall.actual);
      q->value.compcall.tbp = p->value.compcall.tbp;
      break;

    case EXPR_STRUCTURE:
    case EXPR_ARRAY:
      q->value.constructor = gfc_constructor_copy (p->value.constructor);
      break;

    case EXPR_VARIABLE:
    case EXPR_NULL:
      break;
390
    }
Jerry DeLisle committed
391 392 393 394 395 396

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

  q->ref = gfc_copy_ref (p->ref);

  return q;
397 398 399
}


400 401 402 403 404 405 406 407 408 409 410 411 412
void
gfc_clear_shape (mpz_t *shape, int rank)
{
  int i;

  for (i = 0; i < rank; i++)
    mpz_clear (shape[i]);
}


void
gfc_free_shape (mpz_t **shape, int rank)
{
413 414 415
  if (*shape == NULL)
    return;

416 417 418 419 420 421
  gfc_clear_shape (*shape, rank);
  free (*shape);
  *shape = NULL;
}


422 423 424 425 426 427
/* 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
428
free_expr0 (gfc_expr *e)
429 430 431 432
{
  switch (e->expr_type)
    {
    case EXPR_CONSTANT:
433
      /* Free any parts of the value that need freeing.  */
434 435 436 437 438 439 440
      switch (e->ts.type)
	{
	case BT_INTEGER:
	  mpz_clear (e->value.integer);
	  break;

	case BT_REAL:
441
	  mpfr_clear (e->value.real);
442 443 444
	  break;

	case BT_CHARACTER:
445
	  free (e->value.character.string);
446 447 448
	  break;

	case BT_COMPLEX:
449
	  mpc_clear (e->value.complex);
450 451 452 453 454 455
	  break;

	default:
	  break;
	}

456
      /* Free the representation.  */
457
      free (e->representation.string);
458

459 460 461
      break;

    case EXPR_OP:
462 463 464 465
      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);
466 467 468 469 470 471
      break;

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

472
    case EXPR_COMPCALL:
473
    case EXPR_PPC:
474 475 476
      gfc_free_actual_arglist (e->value.compcall.actual);
      break;

477 478 479 480 481
    case EXPR_VARIABLE:
      break;

    case EXPR_ARRAY:
    case EXPR_STRUCTURE:
Jerry DeLisle committed
482
      gfc_constructor_free (e->value.constructor);
483 484 485
      break;

    case EXPR_SUBSTRING:
486
      free (e->value.character.string);
487 488 489 490 491 492 493 494 495 496
      break;

    case EXPR_NULL:
      break;

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

  /* Free a shape array.  */
497
  gfc_free_shape (&e->shape, e->rank);
Jerry DeLisle committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

  gfc_free_ref_list (e->ref);

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


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

void
gfc_free_expr (gfc_expr *e)
{
  if (e == NULL)
    return;
  free_expr0 (e);
513
  free (e);
Jerry DeLisle committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527
}


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

void
gfc_free_actual_arglist (gfc_actual_arglist *a1)
{
  gfc_actual_arglist *a2;

  while (a1)
    {
      a2 = a1->next;
      gfc_free_expr (a1->expr);
528
      free (a1);
Jerry DeLisle committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
      a1 = a2;
    }
}


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

gfc_actual_arglist *
gfc_copy_actual_arglist (gfc_actual_arglist *p)
{
  gfc_actual_arglist *head, *tail, *new_arg;

  head = tail = NULL;

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

      new_arg->expr = gfc_copy_expr (p->expr);
      new_arg->next = NULL;

      if (head == NULL)
	head = new_arg;
      else
	tail->next = new_arg;

      tail = new_arg;
    }

  return head;
}


/* Free a list of reference structures.  */

void
gfc_free_ref_list (gfc_ref *p)
{
  gfc_ref *q;
  int i;

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

Jerry DeLisle committed
575 576 577 578 579 580 581 582 583
      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]);
	    }
584

Jerry DeLisle committed
585
	  break;
586

Jerry DeLisle committed
587 588 589 590
	case REF_SUBSTRING:
	  gfc_free_expr (p->u.ss.start);
	  gfc_free_expr (p->u.ss.end);
	  break;
591

Jerry DeLisle committed
592 593 594
	case REF_COMPONENT:
	  break;
	}
595

596
      free (p);
Jerry DeLisle committed
597
    }
598 599 600 601 602 603
}


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

void
604
gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
605 606 607
{
  free_expr0 (dest);
  *dest = *src;
608
  free (src);
609 610 611 612 613 614 615 616 617
}


/* 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 *
618
gfc_extract_int (gfc_expr *expr, int *result)
619 620
{
  if (expr->expr_type != EXPR_CONSTANT)
621
    return _("Constant expression required at %C");
622 623

  if (expr->ts.type != BT_INTEGER)
624
    return _("Integer expression required at %C");
625 626 627 628

  if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
      || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
    {
629
      return _("Integer value too large in expression at %C");
630 631 632 633 634 635 636 637 638 639
    }

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

  return NULL;
}


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

640 641
gfc_ref *
gfc_copy_ref (gfc_ref *src)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
{
  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;
657
      free (ar);
658 659 660 661 662 663 664 665 666 667 668 669 670
      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;
    }

671
  dest->next = gfc_copy_ref (src->next);
672 673 674 675 676

  return dest;
}


677
/* Detect whether an expression has any vector index array references.  */
678 679 680 681

int
gfc_has_vector_index (gfc_expr *e)
{
682
  gfc_ref *ref;
683 684 685 686 687 688 689 690 691 692
  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;
}


693 694 695
/* Copy a shape array.  */

mpz_t *
696
gfc_copy_shape (mpz_t *shape, int rank)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
{
  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;
}


713 714 715 716 717 718 719 720 721 722
/* 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
723
   of range -- or anything else, just returns NULL.  */
724 725

mpz_t *
726
gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
727 728 729 730 731 732 733 734 735 736 737 738
{
  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);
739
  n--; /* Convert to zero based index.  */
740
  if (n < 0 || n >= rank)
741 742
    return NULL;

743
  s = new_shape = gfc_get_shape (rank - 1);
744 745 746 747

  for (i = 0; i < rank; i++)
    {
      if (i == n)
748
	continue;
749 750 751 752 753 754 755
      mpz_init_set (*s, shape[i]);
      s++;
    }

  return new_shape;
}

756

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

int
761
gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
{
  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
779
gfc_numeric_ts (gfc_typespec *ts)
780 781 782 783 784 785 786 787 788 789
{
  return numeric_type (ts->type);
}


/* 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 *
790
gfc_build_conversion (gfc_expr *e)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
{
  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
808 809
   have the same type. Conversion warnings are disabled if wconversion
   is set to 0.
810 811 812 813

   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
814
   1.0**2 stays as it is.  */
815 816

void
817
gfc_type_convert_binary (gfc_expr *e, int wconversion)
818 819 820
{
  gfc_expr *op1, *op2;

821 822
  op1 = e->value.op.op1;
  op2 = e->value.op.op2;
823 824 825 826 827 828 829 830 831 832 833 834

  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)
	{
835
	  /* No type conversions.  */
836 837 838 839 840
	  e->ts = op1->ts;
	  goto done;
	}

      if (op1->ts.kind > op2->ts.kind)
841
	gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
842
      else
843
	gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
844 845 846 847 848 849 850 851 852 853

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

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

854
      /* Special case for ** operator.  */
855
      if (e->value.op.op == INTRINSIC_POWER)
856 857
	goto done;

858
      gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
859 860 861 862 863 864
      goto done;
    }

  if (op1->ts.type == BT_INTEGER)
    {
      e->ts = op2->ts;
865
      gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
866 867 868 869 870 871 872 873 874 875
      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)
876
    gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
877
  if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
878
    gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
879 880 881 882 883 884 885 886 887 888

done:
  return;
}


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

int
889
gfc_is_constant_expr (gfc_expr *e)
890 891 892
{
  gfc_constructor *c;
  gfc_actual_arglist *arg;
893
  gfc_symbol *sym;
894 895 896 897 898 899 900

  if (e == NULL)
    return 1;

  switch (e->expr_type)
    {
    case EXPR_OP:
Jerry DeLisle committed
901 902 903
      return (gfc_is_constant_expr (e->value.op.op1)
	      && (e->value.op.op2 == NULL
		  || gfc_is_constant_expr (e->value.op.op2)));
904 905

    case EXPR_VARIABLE:
Jerry DeLisle committed
906
      return 0;
907 908

    case EXPR_FUNCTION:
909 910
    case EXPR_PPC:
    case EXPR_COMPCALL:
911 912 913
      gcc_assert (e->symtree || e->value.function.esym
		  || e->value.function.isym);

914 915 916 917
      /* Call to intrinsic with at least one argument.  */
      if (e->value.function.isym && e->value.function.actual)
	{
	  for (arg = e->value.function.actual; arg; arg = arg->next)
Jerry DeLisle committed
918 919
	    if (!gfc_is_constant_expr (arg->expr))
	      return 0;
920
	}
921 922 923

      /* Specification functions are constant.  */
      /* F95, 7.1.6.2; F2003, 7.1.7  */
924 925 926 927 928 929
      sym = NULL;
      if (e->symtree)
	sym = e->symtree->n.sym;
      if (e->value.function.esym)
	sym = e->value.function.esym;

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
      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 1;

      if (e->value.function.isym
	  && (e->value.function.isym->elemental
	      || e->value.function.isym->pure
	      || e->value.function.isym->inquiry
	      || e->value.function.isym->transformational))
	return 1;

      return 0;
949 950 951

    case EXPR_CONSTANT:
    case EXPR_NULL:
Jerry DeLisle committed
952
      return 1;
953 954

    case EXPR_SUBSTRING:
Jerry DeLisle committed
955 956
      return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
				&& gfc_is_constant_expr (e->ref->u.ss.end));
957

958
    case EXPR_ARRAY:
959
    case EXPR_STRUCTURE:
960 961 962 963 964
      c = gfc_constructor_first (e->value.constructor);
      if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
        return gfc_constant_ac (e);

      for (; c; c = gfc_constructor_next (c))
965
	if (!gfc_is_constant_expr (c->expr))
Jerry DeLisle committed
966
	  return 0;
967

Jerry DeLisle committed
968
      return 1;
969 970 971 972


    default:
      gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
Jerry DeLisle committed
973
      return 0;
974 975 976 977
    }
}


978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
/* 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;
}


1007 1008
/* Try to collapse intrinsic expressions.  */

1009
static gfc_try
1010
simplify_intrinsic_op (gfc_expr *p, int type)
1011
{
1012
  gfc_intrinsic_op op;
1013 1014
  gfc_expr *op1, *op2, *result;

1015
  if (p->value.op.op == INTRINSIC_USER)
1016 1017
    return SUCCESS;

1018 1019
  op1 = p->value.op.op1;
  op2 = p->value.op.op2;
1020
  op  = p->value.op.op;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

  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;

1031
  /* Rip p apart.  */
1032 1033
  p->value.op.op1 = NULL;
  p->value.op.op2 = NULL;
1034

1035
  switch (op)
1036
    {
1037
    case INTRINSIC_PARENTHESES:
1038 1039 1040 1041
      result = gfc_parentheses (op1);
      break;

    case INTRINSIC_UPLUS:
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
      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:
1074 1075
    case INTRINSIC_EQ_OS:
      result = gfc_eq (op1, op2, op);
1076 1077 1078
      break;

    case INTRINSIC_NE:
1079 1080
    case INTRINSIC_NE_OS:
      result = gfc_ne (op1, op2, op);
1081 1082 1083
      break;

    case INTRINSIC_GT:
1084 1085
    case INTRINSIC_GT_OS:
      result = gfc_gt (op1, op2, op);
1086 1087 1088
      break;

    case INTRINSIC_GE:
1089 1090
    case INTRINSIC_GE_OS:
      result = gfc_ge (op1, op2, op);
1091 1092 1093
      break;

    case INTRINSIC_LT:
1094 1095
    case INTRINSIC_LT_OS:
      result = gfc_lt (op1, op2, op);
1096 1097 1098
      break;

    case INTRINSIC_LE:
1099 1100
    case INTRINSIC_LE_OS:
      result = gfc_le (op1, op2, op);
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
      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;
    }

1134 1135
  result->rank = p->rank;
  result->where = p->where;
1136 1137 1138 1139 1140 1141 1142 1143 1144
  gfc_replace_expr (p, result);

  return SUCCESS;
}


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

1145
static gfc_try
Jerry DeLisle committed
1146
simplify_constructor (gfc_constructor_base base, int type)
1147
{
Jerry DeLisle committed
1148
  gfc_constructor *c;
1149 1150
  gfc_expr *p;

Jerry DeLisle committed
1151
  for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1152 1153 1154 1155 1156 1157 1158
    {
      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;

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
      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);
	}
1174 1175 1176 1177 1178 1179 1180 1181
    }

  return SUCCESS;
}


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

1182
static gfc_try
Jerry DeLisle committed
1183
find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
1184
		    gfc_constructor **rval)
1185 1186 1187 1188 1189
{
  unsigned long nelemen;
  int i;
  mpz_t delta;
  mpz_t offset;
1190 1191
  mpz_t span;
  mpz_t tmp;
Jerry DeLisle committed
1192
  gfc_constructor *cons;
1193
  gfc_expr *e;
1194
  gfc_try t;
1195 1196 1197

  t = SUCCESS;
  e = NULL;
1198 1199 1200

  mpz_init_set_ui (offset, 0);
  mpz_init (delta);
1201 1202
  mpz_init (tmp);
  mpz_init_set_ui (span, 1);
1203 1204
  for (i = 0; i < ar->dimen; i++)
    {
1205 1206 1207 1208 1209 1210 1211 1212
      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;
	}

1213 1214
      e = gfc_copy_expr (ar->start[i]);
      if (e->expr_type != EXPR_CONSTANT)
1215 1216
	{
	  cons = NULL;
1217
	  goto depart;
1218
	}
1219

1220 1221 1222
      gcc_assert (ar->as->upper[i]->expr_type == EXPR_CONSTANT
		  && ar->as->lower[i]->expr_type == EXPR_CONSTANT);

1223
      /* Check the bounds.  */
1224
      if ((ar->as->upper[i]
1225 1226
	   && mpz_cmp (e->value.integer,
		       ar->as->upper[i]->value.integer) > 0)
1227 1228
	  || (mpz_cmp (e->value.integer,
		       ar->as->lower[i]->value.integer) < 0))
1229
	{
1230
	  gfc_error ("Index in dimension %d is out of bounds "
1231 1232 1233 1234 1235 1236
		     "at %L", i + 1, &ar->c_where[i]);
	  cons = NULL;
	  t = FAILURE;
	  goto depart;
	}

1237
      mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
1238
      mpz_mul (delta, delta, span);
1239
      mpz_add (offset, offset, delta);
1240 1241 1242 1243 1244

      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);
1245 1246
    }

Jerry DeLisle committed
1247 1248
  for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
       cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
1249
    {
Jerry DeLisle committed
1250
      if (cons->iterator)
1251
	{
Jerry DeLisle committed
1252 1253
	  cons = NULL;
	  goto depart;
1254 1255
	}
    }
1256

1257
depart:
1258 1259
  mpz_clear (delta);
  mpz_clear (offset);
1260 1261
  mpz_clear (span);
  mpz_clear (tmp);
1262 1263 1264 1265
  if (e)
    gfc_free_expr (e);
  *rval = cons;
  return t;
1266 1267 1268 1269 1270 1271
}


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

static gfc_constructor *
Jerry DeLisle committed
1272
find_component_ref (gfc_constructor_base base, gfc_ref *ref)
1273 1274 1275
{
  gfc_component *comp;
  gfc_component *pick;
Jerry DeLisle committed
1276
  gfc_constructor *c = gfc_constructor_first (base);
1277 1278 1279 1280 1281 1282

  comp = ref->u.c.sym->components;
  pick = ref->u.c.component;
  while (comp != pick)
    {
      comp = comp->next;
Jerry DeLisle committed
1283
      c = gfc_constructor_next (c);
1284 1285
    }

Jerry DeLisle committed
1286
  return c;
1287 1288 1289 1290 1291 1292 1293
}


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

static void
1294
remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
1295 1296 1297
{
  gfc_expr *e;

1298 1299 1300 1301 1302 1303 1304
  if (cons)
    {
      e = cons->expr;
      cons->expr = NULL;
    }
  else
    e = gfc_copy_expr (p);
1305 1306 1307 1308 1309 1310
  e->ref = p->ref->next;
  p->ref->next =  NULL;
  gfc_replace_expr (p, e);
}


1311 1312
/* Pull an array section out of an array constructor.  */

1313
static gfc_try
1314 1315 1316 1317 1318
find_array_section (gfc_expr *expr, gfc_ref *ref)
{
  int idx;
  int rank;
  int d;
1319
  int shape_i;
1320
  int limit;
1321
  long unsigned one = 1;
1322
  bool incr_ctr;
1323
  mpz_t start[GFC_MAX_DIMENSIONS];
1324 1325 1326 1327 1328 1329 1330 1331
  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;
Jerry DeLisle committed
1332 1333
  gfc_constructor_base base;
  gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
1334 1335 1336 1337 1338
  gfc_expr *begin;
  gfc_expr *finish;
  gfc_expr *step;
  gfc_expr *upper;
  gfc_expr *lower;
1339
  gfc_try t;
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

  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]);
1360
      mpz_init (start[d]);
1361 1362 1363
      mpz_init (end[d]);
      mpz_init (ctr[d]);
      mpz_init (stride[d]);
1364
      vecsub[d] = NULL;
1365 1366 1367
    }

  /* Build the counters to clock through the array reference.  */
1368
  shape_i = 0;
1369 1370 1371 1372 1373 1374 1375 1376 1377
  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];

1378
      if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR)  /* Vector subscript.  */
1379
	{
Jerry DeLisle committed
1380
	  gfc_constructor *ci;
1381
	  gcc_assert (begin);
Tobias Burnus committed
1382

1383
	  if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
Tobias Burnus committed
1384 1385 1386 1387 1388
	    {
	      t = FAILURE;
	      goto cleanup;
	    }

1389
	  gcc_assert (begin->rank == 1);
1390 1391 1392 1393 1394 1395
	  /* Zero-sized arrays have no shape and no elements, stop early.  */
	  if (!begin->shape) 
	    {
	      mpz_init_set_ui (nelts, 0);
	      break;
	    }
1396

Jerry DeLisle committed
1397
	  vecsub[d] = gfc_constructor_first (begin->value.constructor);
1398 1399 1400
	  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]);
1401

1402
	  /* Check bounds.  */
Jerry DeLisle committed
1403
	  for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
1404
	    {
Jerry DeLisle committed
1405 1406
	      if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
		  || mpz_cmp (ci->expr->value.integer,
1407
			      lower->value.integer) < 0)
1408 1409 1410 1411 1412 1413 1414
		{
		  gfc_error ("index in dimension %d is out of bounds "
			     "at %L", d + 1, &ref->u.ar.c_where[d]);
		  t = FAILURE;
		  goto cleanup;
		}
	    }
1415
	}
1416
      else
1417
	{
1418
	  if ((begin && begin->expr_type != EXPR_CONSTANT)
1419 1420
	      || (finish && finish->expr_type != EXPR_CONSTANT)
	      || (step && step->expr_type != EXPR_CONSTANT))
1421 1422 1423 1424
	    {
	      t = FAILURE;
	      goto cleanup;
	    }
1425

1426 1427 1428 1429 1430
	  /* Obtain the stride.  */
	  if (step)
	    mpz_set (stride[d], step->value.integer);
	  else
	    mpz_set_ui (stride[d], one);
1431

1432 1433
	  if (mpz_cmp_ui (stride[d], 0) == 0)
	    mpz_set_ui (stride[d], one);
1434

1435 1436 1437 1438 1439
	  /* Obtain the start value for the index.  */
	  if (begin)
	    mpz_set (start[d], begin->value.integer);
	  else
	    mpz_set (start[d], lower->value.integer);
1440

1441
	  mpz_set (ctr[d], start[d]);
1442

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
	  /* 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;
	    }
1465

1466
	  /* Calculate the number of elements and the shape.  */
1467
	  mpz_set (tmp_mpz, stride[d]);
1468 1469 1470 1471 1472
	  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);

1473 1474
	  /* An element reference reduces the rank of the expression; don't
	     add anything to the shape array.  */
1475 1476 1477
	  if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT) 
	    mpz_set (expr->shape[shape_i++], tmp_mpz);
	}
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

      /* 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 (ptr);
Jerry DeLisle committed
1488
  cons = gfc_constructor_first (base);
1489 1490 1491 1492

  /* Now clock through the array reference, calculating the index in
     the source constructor and transferring the elements to the new
     constructor.  */  
1493
  for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
1494 1495 1496 1497 1498 1499
    {
      if (ref->u.ar.offset)
	mpz_set (ptr, ref->u.ar.offset->value.integer);
      else
	mpz_init_set_ui (ptr, 0);

1500
      incr_ctr = true;
1501 1502 1503
      for (d = 0; d < rank; d++)
	{
	  mpz_set (tmp_mpz, ctr[d]);
1504
	  mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
1505 1506 1507
	  mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
	  mpz_add (ptr, ptr, tmp_mpz);

1508
	  if (!incr_ctr) continue;
1509

1510
	  if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript.  */
1511 1512 1513
	    {
	      gcc_assert(vecsub[d]);

Jerry DeLisle committed
1514 1515
	      if (!gfc_constructor_next (vecsub[d]))
		vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
1516 1517
	      else
		{
Jerry DeLisle committed
1518
		  vecsub[d] = gfc_constructor_next (vecsub[d]);
1519 1520 1521 1522
		  incr_ctr = false;
		}
	      mpz_set (ctr[d], vecsub[d]->expr->value.integer);
	    }
1523
	  else
1524 1525 1526
	    {
	      mpz_add (ctr[d], ctr[d], stride[d]); 

1527 1528 1529
	      if (mpz_cmp_ui (stride[d], 0) > 0
		  ? mpz_cmp (ctr[d], end[d]) > 0
		  : mpz_cmp (ctr[d], end[d]) < 0)
1530 1531 1532 1533
		mpz_set (ctr[d], start[d]);
	      else
		incr_ctr = false;
	    }
1534 1535
	}

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
      limit = mpz_get_ui (ptr);
      if (limit >= gfc_option.flag_max_array_constructor)
        {
	  gfc_error ("The number of elements in the array constructor "
		     "at %L requires an increase of the allowed %d "
		     "upper limit.   See -fmax-array-constructor "
		     "option", &expr->where,
		     gfc_option.flag_max_array_constructor);
	  return FAILURE;
	}

      cons = gfc_constructor_lookup (base, limit);
Jerry DeLisle committed
1548 1549 1550
      gcc_assert (cons);
      gfc_constructor_append_expr (&expr->value.constructor,
				   gfc_copy_expr (cons->expr), NULL);
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
    }

  mpz_clear (ptr);

cleanup:

  mpz_clear (delta_mpz);
  mpz_clear (tmp_mpz);
  mpz_clear (nelts);
  for (d = 0; d < rank; d++)
    {
      mpz_clear (delta[d]);
1563
      mpz_clear (start[d]);
1564 1565 1566 1567
      mpz_clear (end[d]);
      mpz_clear (ctr[d]);
      mpz_clear (stride[d]);
    }
Jerry DeLisle committed
1568
  gfc_constructor_free (base);
1569 1570 1571 1572 1573
  return t;
}

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

1574
static gfc_try
1575 1576 1577 1578
find_substring_ref (gfc_expr *p, gfc_expr **newp)
{
  int end;
  int start;
1579
  int length;
1580
  gfc_char_t *chr;
1581 1582

  if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
1583
      || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
1584 1585 1586
    return FAILURE;

  *newp = gfc_copy_expr (p);
1587
  free ((*newp)->value.character.string);
1588

1589 1590
  end = (int) mpz_get_ui (p->ref->u.ss.end->value.integer);
  start = (int) mpz_get_ui (p->ref->u.ss.start->value.integer);
1591
  length = end - start + 1;
1592

1593
  chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
1594
  (*newp)->value.character.length = length;
1595 1596
  memcpy (chr, &p->value.character.string[start - 1],
	  length * sizeof (gfc_char_t));
1597
  chr[length] = '\0';
1598 1599 1600 1601 1602
  return SUCCESS;
}



1603 1604 1605
/* Simplify a subobject reference of a constructor.  This occurs when
   parameter variable values are substituted.  */

1606
static gfc_try
1607
simplify_const_ref (gfc_expr *p)
1608
{
Jerry DeLisle committed
1609
  gfc_constructor *cons, *c;
1610
  gfc_expr *newp;
1611
  gfc_ref *last_ref;
1612 1613 1614 1615 1616 1617 1618 1619 1620

  while (p->ref)
    {
      switch (p->ref->type)
	{
	case REF_ARRAY:
	  switch (p->ref->u.ar.type)
	    {
	    case AR_ELEMENT:
1621 1622 1623 1624 1625 1626 1627
	      /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
		 will generate this.  */
	      if (p->expr_type != EXPR_ARRAY)
		{
		  remove_subobject_ref (p, NULL);
		  break;
		}
1628
	      if (find_array_element (p->value.constructor, &p->ref->u.ar,
1629 1630 1631
				      &cons) == FAILURE)
		return FAILURE;

1632 1633
	      if (!cons)
		return SUCCESS;
1634

1635 1636 1637
	      remove_subobject_ref (p, cons);
	      break;

1638 1639 1640 1641 1642
	    case AR_SECTION:
	      if (find_array_section (p, p->ref) == FAILURE)
		return FAILURE;
	      p->ref->u.ar.type = AR_FULL;

1643
	    /* Fall through.  */
1644

1645
	    case AR_FULL:
1646
	      if (p->ref->next != NULL
1647
		  && (p->ts.type == BT_CHARACTER || p->ts.type == BT_DERIVED))
1648
		{
Jerry DeLisle committed
1649 1650
		  for (c = gfc_constructor_first (p->value.constructor);
		       c; c = gfc_constructor_next (c))
1651
		    {
Jerry DeLisle committed
1652 1653
		      c->expr->ref = gfc_copy_ref (p->ref->next);
		      if (simplify_const_ref (c->expr) == FAILURE)
1654 1655 1656
			return FAILURE;
		    }

1657 1658
		  if (p->ts.type == BT_DERIVED
			&& p->ref->next
Jerry DeLisle committed
1659
			&& (c = gfc_constructor_first (p->value.constructor)))
1660
		    {
1661
		      /* There may have been component references.  */
Jerry DeLisle committed
1662
		      p->ts = c->expr->ts;
1663
		    }
1664

1665 1666
		  last_ref = p->ref;
		  for (; last_ref->next; last_ref = last_ref->next) {};
1667

1668 1669 1670 1671 1672 1673 1674 1675
		  if (p->ts.type == BT_CHARACTER
			&& last_ref->type == REF_SUBSTRING)
		    {
		      /* 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).  */
		      int string_len;
Jerry DeLisle committed
1676
		      if ((c = gfc_constructor_first (p->value.constructor)))
1677
			{
Jerry DeLisle committed
1678
			  const gfc_expr* first = c->expr;
1679 1680 1681 1682 1683 1684 1685
			  gcc_assert (first->expr_type == EXPR_CONSTANT);
			  gcc_assert (first->ts.type == BT_CHARACTER);
			  string_len = first->value.character.length;
			}
		      else
			string_len = 0;

1686
		      if (!p->ts.u.cl)
1687 1688 1689 1690 1691
			p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
						      NULL);
		      else
			gfc_free_expr (p->ts.u.cl->length);

Jerry DeLisle committed
1692 1693 1694
		      p->ts.u.cl->length
			= gfc_get_int_expr (gfc_default_integer_kind,
					    NULL, string_len);
1695
		    }
1696
		}
1697 1698
	      gfc_free_ref_list (p->ref);
	      p->ref = NULL;
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
	      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:
1713 1714 1715 1716 1717 1718 1719
  	  if (find_substring_ref (p, &newp) == FAILURE)
	    return FAILURE;

	  gfc_replace_expr (p, newp);
	  gfc_free_ref_list (p->ref);
	  p->ref = NULL;
	  break;
1720 1721 1722 1723 1724 1725 1726 1727 1728
	}
    }

  return SUCCESS;
}


/* Simplify a chain of references.  */

1729
static gfc_try
1730
simplify_ref_chain (gfc_ref *ref, int type)
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
{
  int n;

  for (; ref; ref = ref->next)
    {
      switch (ref->type)
	{
	case REF_ARRAY:
	  for (n = 0; n < ref->u.ar.dimen; n++)
	    {
1741
	      if (gfc_simplify_expr (ref->u.ar.start[n], type) == FAILURE)
1742
		return FAILURE;
1743
	      if (gfc_simplify_expr (ref->u.ar.end[n], type) == FAILURE)
1744
		return FAILURE;
1745
	      if (gfc_simplify_expr (ref->u.ar.stride[n], type) == FAILURE)
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
		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.  */
1766

1767
static gfc_try
1768
simplify_parameter_variable (gfc_expr *p, int type)
1769 1770
{
  gfc_expr *e;
1771
  gfc_try t;
1772 1773

  e = gfc_copy_expr (p->symtree->n.sym->value);
1774 1775 1776
  if (e == NULL)
    return FAILURE;

1777 1778
  e->rank = p->rank;

1779 1780
  /* Do not copy subobject refs for constant.  */
  if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
1781
    e->ref = gfc_copy_ref (p->ref);
1782 1783
  t = gfc_simplify_expr (e, type);

1784
  /* Only use the simplification if it eliminated all subobject references.  */
1785
  if (t == SUCCESS && !e->ref)
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
    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
1808
	 iterator values.
1809 1810 1811
   Returns FAILURE on error, SUCCESS otherwise.
   NOTE: Will return SUCCESS even if the expression can not be simplified.  */

1812
gfc_try
1813
gfc_simplify_expr (gfc_expr *p, int type)
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
{
  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:
1838
      if (simplify_ref_chain (p->ref, type) == FAILURE)
1839 1840
	return FAILURE;

1841 1842
      if (gfc_is_constant_expr (p))
	{
1843
	  gfc_char_t *s;
1844 1845
	  int start, end;

1846
	  start = 0;
1847 1848 1849 1850 1851 1852
	  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.  */
	    }

1853
	  end = p->value.character.length;
1854 1855 1856
	  if (p->ref && p->ref->u.ss.end)
	    gfc_extract_int (p->ref->u.ss.end, &end);

1857 1858
	  if (end < start)
	    end = start;
1859

1860 1861 1862
	  s = gfc_get_wide_string (end - start + 2);
	  memcpy (s, p->value.character.string + start,
		  (end - start) * sizeof (gfc_char_t));
1863
	  s[end - start + 1] = '\0';  /* TODO: C-style string.  */
1864
	  free (p->value.character.string);
1865 1866
	  p->value.character.string = s;
	  p->value.character.length = end - start;
1867
	  p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
Jerry DeLisle committed
1868 1869 1870
	  p->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
						 NULL,
						 p->value.character.length);
1871 1872 1873 1874
	  gfc_free_ref_list (p->ref);
	  p->ref = NULL;
	  p->expr_type = EXPR_CONSTANT;
	}
1875 1876 1877 1878 1879 1880 1881 1882 1883
      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
1884
	 initialization expression, or we want a subsection.  */
1885
      if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
1886
	  && (gfc_init_expr_flag || p->ref
1887
	      || p->symtree->n.sym->value->expr_type != EXPR_ARRAY))
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
	{
	  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;

1913 1914
      if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
	  && p->ref->u.ar.type == AR_FULL)
1915
	  gfc_expand_constructor (p, false);
1916 1917 1918 1919 1920

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

      break;
1921 1922

    case EXPR_COMPCALL:
1923
    case EXPR_PPC:
1924 1925
      gcc_unreachable ();
      break;
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
    }

  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
1937
et0 (gfc_expr *e)
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
{
  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.  */

1949
static gfc_try check_init_expr (gfc_expr *);
1950

1951 1952 1953

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

1954
static gfc_try
1955 1956 1957
scalarize_intrinsic_call (gfc_expr *e)
{
  gfc_actual_arglist *a, *b;
Jerry DeLisle committed
1958 1959 1960
  gfc_constructor_base ctor;
  gfc_constructor *args[5];
  gfc_constructor *ci, *new_ctor;
1961
  gfc_expr *expr, *old;
Paul Thomas committed
1962
  int n, i, rank[5], array_arg;
Jerry DeLisle committed
1963
  
Paul Thomas committed
1964 1965 1966 1967
  /* 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;
1968
  a = e->value.function.actual;
Paul Thomas committed
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
  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)
1980 1981 1982
    return FAILURE;

  old = gfc_copy_expr (e);
Paul Thomas committed
1983

Jerry DeLisle committed
1984
  gfc_constructor_free (expr->value.constructor);
1985 1986
  expr->value.constructor = NULL;
  expr->ts = old->ts;
Paul Thomas committed
1987
  expr->where = old->where;
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
  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;
Jerry DeLisle committed
2005
	  args[n] = gfc_constructor_first (ctor);
2006 2007 2008 2009 2010 2011 2012
	}
      else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
	{
	  if (a->expr->rank)
	    rank[n] = a->expr->rank;
	  else
	    rank[n] = 1;
Jerry DeLisle committed
2013 2014
	  ctor = gfc_constructor_copy (a->expr->value.constructor);
	  args[n] = gfc_constructor_first (ctor);
2015 2016 2017
	}
      else
	args[n] = NULL;
Jerry DeLisle committed
2018

2019 2020 2021 2022
      n++;
    }


2023
  /* Using the array argument as the master, step through the array
2024 2025
     calling the function for each element and advancing the array
     constructors together.  */
Jerry DeLisle committed
2026
  for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
2027
    {
Jerry DeLisle committed
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
      new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
					      gfc_copy_expr (old), NULL);

      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 ();
2039 2040
	  else
	    {
Jerry DeLisle committed
2041 2042
	      a->next = gfc_get_actual_arglist ();
	      a = a->next;
2043 2044
	    }

Jerry DeLisle committed
2045 2046 2047 2048 2049 2050 2051
	  if (args[i])
	    a->expr = gfc_copy_expr (args[i]->expr);
	  else
	    a->expr = gfc_copy_expr (b->expr);

	  b = b->next;
	}
2052

Jerry DeLisle committed
2053 2054 2055 2056
      /* 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);
2057

Jerry DeLisle committed
2058 2059 2060
      for (i = 0; i < n; i++)
	if (args[i])
	  args[i] = gfc_constructor_next (args[i]);
2061

Jerry DeLisle committed
2062 2063 2064 2065
      for (i = 1; i < n; i++)
	if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
			|| (args[i] == NULL && args[array_arg - 1] != NULL)))
	  goto compliance;
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
    }

  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;
}


2083 2084
static gfc_try
check_intrinsic_op (gfc_expr *e, gfc_try (*check_function) (gfc_expr *))
2085
{
2086 2087
  gfc_expr *op1 = e->value.op.op1;
  gfc_expr *op2 = e->value.op.op2;
2088

2089
  if ((*check_function) (op1) == FAILURE)
2090 2091
    return FAILURE;

2092
  switch (e->value.op.op)
2093 2094 2095
    {
    case INTRINSIC_UPLUS:
    case INTRINSIC_UMINUS:
2096
      if (!numeric_type (et0 (op1)))
2097 2098 2099 2100
	goto not_numeric;
      break;

    case INTRINSIC_EQ:
2101
    case INTRINSIC_EQ_OS:
2102
    case INTRINSIC_NE:
2103
    case INTRINSIC_NE_OS:
2104
    case INTRINSIC_GT:
2105
    case INTRINSIC_GT_OS:
2106
    case INTRINSIC_GE:
2107
    case INTRINSIC_GE_OS:
2108
    case INTRINSIC_LT:
2109
    case INTRINSIC_LT_OS:
2110
    case INTRINSIC_LE:
2111
    case INTRINSIC_LE_OS:
2112
      if ((*check_function) (op2) == FAILURE)
2113 2114
	return FAILURE;
      
2115 2116
      if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
	  && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
2117 2118 2119
	{
	  gfc_error ("Numeric or CHARACTER operands are required in "
		     "expression at %L", &e->where);
2120
	 return FAILURE;
2121 2122
	}
      break;
2123 2124 2125 2126 2127 2128

    case INTRINSIC_PLUS:
    case INTRINSIC_MINUS:
    case INTRINSIC_TIMES:
    case INTRINSIC_DIVIDE:
    case INTRINSIC_POWER:
2129
      if ((*check_function) (op2) == FAILURE)
2130 2131
	return FAILURE;

2132
      if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
2133 2134 2135 2136 2137
	goto not_numeric;

      break;

    case INTRINSIC_CONCAT:
2138
      if ((*check_function) (op2) == FAILURE)
2139 2140
	return FAILURE;

2141
      if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
2142 2143
	{
	  gfc_error ("Concatenation operator in expression at %L "
2144
		     "must have two CHARACTER operands", &op1->where);
2145 2146 2147
	  return FAILURE;
	}

2148
      if (op1->ts.kind != op2->ts.kind)
2149 2150 2151 2152 2153 2154 2155 2156 2157
	{
	  gfc_error ("Concat operator at %L must concatenate strings of the "
		     "same kind", &e->where);
	  return FAILURE;
	}

      break;

    case INTRINSIC_NOT:
2158
      if (et0 (op1) != BT_LOGICAL)
2159 2160
	{
	  gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
2161
		     "operand", &op1->where);
2162 2163 2164 2165 2166 2167 2168 2169 2170
	  return FAILURE;
	}

      break;

    case INTRINSIC_AND:
    case INTRINSIC_OR:
    case INTRINSIC_EQV:
    case INTRINSIC_NEQV:
2171
      if ((*check_function) (op2) == FAILURE)
2172 2173
	return FAILURE;

2174
      if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
2175 2176 2177 2178 2179 2180 2181 2182
	{
	  gfc_error ("LOGICAL operands are required in expression at %L",
		     &e->where);
	  return FAILURE;
	}

      break;

2183 2184 2185
    case INTRINSIC_PARENTHESES:
      break;

2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    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;
}

2200 2201 2202 2203 2204
/* F2003, 7.1.7 (3): In init expression, allocatable components
   must not be data-initialized.  */
static gfc_try
check_alloc_comp_init (gfc_expr *e)
{
Jerry DeLisle committed
2205
  gfc_component *comp;
2206 2207 2208 2209 2210
  gfc_constructor *ctor;

  gcc_assert (e->expr_type == EXPR_STRUCTURE);
  gcc_assert (e->ts.type == BT_DERIVED);

Jerry DeLisle committed
2211 2212 2213
  for (comp = e->ts.u.derived->components,
       ctor = gfc_constructor_first (e->value.constructor);
       comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
2214
    {
Jerry DeLisle committed
2215
      if (comp->attr.allocatable
2216 2217 2218 2219
          && ctor->expr->expr_type != EXPR_NULL)
        {
	  gfc_error("Invalid initialization expression for ALLOCATABLE "
	            "component '%s' in structure constructor at %L",
Jerry DeLisle committed
2220
	            comp->name, &ctor->expr->where);
2221 2222 2223 2224 2225 2226
	  return FAILURE;
	}
    }

  return SUCCESS;
}
2227

2228 2229 2230 2231
static match
check_init_expr_arguments (gfc_expr *e)
{
  gfc_actual_arglist *ap;
2232

2233 2234 2235
  for (ap = e->value.function.actual; ap; ap = ap->next)
    if (check_init_expr (ap->expr) == FAILURE)
      return MATCH_ERROR;
2236

2237 2238 2239
  return MATCH_YES;
}

2240 2241
static gfc_try check_restricted (gfc_expr *);

2242 2243 2244 2245
/* F95, 7.1.6.1, Initialization expressions, (7)
   F2003, 7.1.7 Initialization expression, (8)  */

static match
2246
check_inquiry (gfc_expr *e, int not_restricted)
2247 2248
{
  const char *name;
2249 2250 2251 2252 2253 2254 2255 2256 2257
  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
  };
2258

2259 2260 2261 2262 2263 2264
  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
2265 2266 2267
  };

  int i;
2268 2269 2270 2271 2272
  gfc_actual_arglist *ap;

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

2274 2275
  /* An undeclared parameter will get us here (PR25018).  */
  if (e->symtree == NULL)
2276
    return MATCH_NO;
2277

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

2280 2281
  functions = (gfc_option.warn_std & GFC_STD_F2003) 
		? inquiry_func_f2003 : inquiry_func_f95;
2282

2283 2284 2285
  for (i = 0; functions[i]; i++)
    if (strcmp (functions[i], name) == 0)
      break;
2286

2287
  if (functions[i] == NULL)
2288
    return MATCH_ERROR;
2289

2290 2291
  /* 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
2292
     arguments of these functions are not allowed to be undefined.  */
2293

2294
  for (ap = e->value.function.actual; ap; ap = ap->next)
2295
    {
2296 2297 2298 2299 2300 2301 2302 2303 2304
      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;
2305

2306 2307 2308 2309 2310 2311 2312
	  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
Steven G. Kargl committed
2313 2314
	    && (ap->expr->symtree->n.sym->ts.u.cl->length == NULL
		|| ap->expr->symtree->n.sym->ts.deferred))
2315
	  {
Steven G. Kargl committed
2316 2317 2318 2319
	    gfc_error ("Assumed or deferred character length variable '%s' "
			" in constant expression at %L",
			ap->expr->symtree->n.sym->name,
			&ap->expr->where);
2320 2321 2322 2323
	      return MATCH_ERROR;
	  }
	else if (not_restricted && check_init_expr (ap->expr) == FAILURE)
	  return MATCH_ERROR;
2324 2325 2326 2327 2328

	if (not_restricted == 0
	      && ap->expr->expr_type != EXPR_VARIABLE
	      && check_restricted (ap->expr) == FAILURE)
	  return MATCH_ERROR;
2329 2330 2331 2332 2333 2334

	if (not_restricted == 0
	    && ap->expr->expr_type == EXPR_VARIABLE
	    && ap->expr->symtree->n.sym->attr.dummy
	    && ap->expr->symtree->n.sym->attr.optional)
	  return MATCH_NO;
2335 2336
    }

2337 2338 2339
  return MATCH_YES;
}

2340

2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
/* 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
  };

2352
  static const char * const trans_func_f2003[] =  {
2353 2354
    "all", "any", "count", "dot_product", "matmul", "null", "pack",
    "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
2355 2356
    "selected_real_kind", "spread", "sum", "transfer", "transpose",
    "trim", "unpack", NULL
2357 2358
  };

2359 2360
  int i;
  const char *name;
2361
  const char *const *functions;
2362 2363 2364 2365 2366 2367 2368

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

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

2369 2370 2371
  functions = (gfc_option.allow_std & GFC_STD_F2003) 
		? trans_func_f2003 : trans_func_f95;

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

2376 2377 2378
  for (i = 0; functions[i]; i++)
    if (strcmp (functions[i], name) == 0)
       break;
2379

2380
  if (functions[i] == NULL)
2381 2382 2383 2384 2385
    {
      gfc_error("transformational intrinsic '%s' at %L is not permitted "
		"in an initialization expression", name, &e->where);
      return MATCH_ERROR;
    }
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410

  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;

2411 2412
  if (e->ts.type != BT_INTEGER
      && e->ts.type != BT_CHARACTER
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
      && 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);
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
}


/* 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.  */

2440
static gfc_try
2441
check_init_expr (gfc_expr *e)
2442 2443
{
  match m;
2444
  gfc_try t;
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458

  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:
2459
      t = FAILURE;
2460

2461 2462 2463
      {
	gfc_intrinsic_sym* isym;
	gfc_symbol* sym;
2464

2465 2466 2467 2468 2469 2470 2471 2472 2473
	sym = e->symtree->n.sym;
	if (!gfc_is_intrinsic (sym, 0, e->where)
	    || (m = gfc_intrinsic_func_interface (e, 0)) != MATCH_YES)
	  {
	    gfc_error ("Function '%s' in initialization expression at %L "
		       "must be an intrinsic function",
		       e->symtree->n.sym->name, &e->where);
	    break;
	  }
2474

2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
	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;
	  }
2486

2487 2488 2489
	if (m == MATCH_ERROR)
	  return FAILURE;

2490 2491 2492 2493 2494 2495 2496
	/* Try to scalarize an elemental intrinsic function that has an
	   array argument.  */
	isym = gfc_find_function (e->symtree->n.sym->name);
	if (isym && isym->elemental
	    && (t = scalarize_intrinsic_call (e)) == SUCCESS)
	  break;
      }
2497

2498
      if (m == MATCH_YES)
2499
	t = gfc_simplify_expr (e, 0);
2500

2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
      break;

    case EXPR_VARIABLE:
      t = SUCCESS;

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

      if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
	{
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
	  /* 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);

2523 2524 2525
	  break;
	}

2526 2527 2528
      if (gfc_in_match_data ())
	break;

2529
      t = FAILURE;
2530 2531 2532 2533 2534 2535

      if (e->symtree->n.sym->as)
	{
	  switch (e->symtree->n.sym->as->type)
	    {
	      case AS_ASSUMED_SIZE:
2536
		gfc_error ("Assumed size array '%s' at %L is not permitted "
2537 2538
			   "in an initialization expression",
			   e->symtree->n.sym->name, &e->where);
2539
		break;
2540 2541

	      case AS_ASSUMED_SHAPE:
2542
		gfc_error ("Assumed shape array '%s' at %L is not permitted "
2543 2544
			   "in an initialization expression",
			   e->symtree->n.sym->name, &e->where);
2545
		break;
2546 2547

	      case AS_DEFERRED:
2548
		gfc_error ("Deferred array '%s' at %L is not permitted "
2549 2550
			   "in an initialization expression",
			   e->symtree->n.sym->name, &e->where);
2551
		break;
2552

2553 2554 2555 2556 2557 2558
	      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;

2559 2560 2561 2562 2563 2564 2565 2566 2567
	      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);

2568 2569 2570 2571 2572 2573 2574 2575
      break;

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

    case EXPR_SUBSTRING:
2576
      t = check_init_expr (e->ref->u.ss.start);
2577 2578 2579
      if (t == FAILURE)
	break;

2580
      t = check_init_expr (e->ref->u.ss.end);
2581 2582 2583 2584 2585 2586
      if (t == SUCCESS)
	t = gfc_simplify_expr (e, 0);

      break;

    case EXPR_STRUCTURE:
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
      t = e->ts.is_iso_c ? SUCCESS : FAILURE;
      if (t == SUCCESS)
	break;

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

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

2599 2600 2601 2602 2603 2604 2605
      break;

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

2606
      t = gfc_expand_constructor (e, true);
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
      if (t == FAILURE)
	break;

      t = gfc_check_constructor_type (e);
      break;

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

  return t;
}

2620 2621 2622
/* 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.  */
2623

2624 2625
gfc_try
gfc_reduce_init_expr (gfc_expr *expr)
2626
{
2627
  gfc_try t;
2628

2629
  gfc_init_expr_flag = true;
2630 2631 2632
  t = gfc_resolve_expr (expr);
  if (t == SUCCESS)
    t = check_init_expr (expr);
2633
  gfc_init_expr_flag = false;
2634 2635

  if (t == FAILURE)
2636
    return FAILURE;
2637

2638
  if (expr->expr_type == EXPR_ARRAY)
2639
    {
2640 2641
      if (gfc_check_constructor_type (expr) == FAILURE)
	return FAILURE;
2642
      if (gfc_expand_constructor (expr, true) == FAILURE)
2643
	return FAILURE;
2644 2645 2646 2647 2648 2649 2650
    }

  return SUCCESS;
}


/* Match an initialization expression.  We work by first matching an
2651
   expression, then reducing it to a constant.  */
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661

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

  expr = NULL;

2662
  gfc_init_expr_flag = true;
2663

2664 2665
  m = gfc_match_expr (&expr);
  if (m != MATCH_YES)
2666
    {
2667
      gfc_init_expr_flag = false;
2668 2669
      return m;
    }
2670 2671 2672 2673 2674

  t = gfc_reduce_init_expr (expr);
  if (t != SUCCESS)
    {
      gfc_free_expr (expr);
2675
      gfc_init_expr_flag = false;
2676 2677
      return MATCH_ERROR;
    }
2678 2679

  *result = expr;
2680
  gfc_init_expr_flag = false;
2681 2682 2683 2684 2685 2686 2687 2688 2689

  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.  */

2690
static gfc_try
2691
restricted_args (gfc_actual_arglist *a)
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
{
  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.  */

2708
static gfc_try
2709
external_spec_function (gfc_expr *e)
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
{
  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;
    }

2729
  if (!f->attr.pure && !f->attr.elemental)
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
    {
      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;
    }

2743
  return restricted_args (e->value.function.actual);
2744 2745 2746 2747
}


/* Check to see that a function reference to an intrinsic is a
2748
   restricted expression.  */
2749

2750
static gfc_try
2751
restricted_intrinsic (gfc_expr *e)
2752
{
2753
  /* TODO: Check constraints on inquiry functions.  7.1.6.2 (7).  */
2754
  if (check_inquiry (e, 0) == MATCH_YES)
2755
    return SUCCESS;
2756

2757
  return restricted_args (e->value.function.actual);
2758 2759 2760
}


2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
/* 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);
}


2819 2820 2821 2822
/* Verify that an expression is a restricted expression.  Like its
   cousin check_init_expr(), an error message is generated if we
   return FAILURE.  */

2823
static gfc_try
2824
check_restricted (gfc_expr *e)
2825
{
2826
  gfc_symbol* sym;
2827
  gfc_try t;
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841

  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:
2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
      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);
	}
2858 2859 2860 2861 2862 2863
      break;

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

2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
      /* 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;
	}

2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
      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;
	}

2891 2892 2893 2894
      /* Check reference chain if any.  */
      if (check_references (e->ref, &check_restricted) == FAILURE)
	break;

2895 2896 2897 2898 2899
      /* 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.  */
2900 2901 2902 2903 2904
      if (e->error
	    || sym->attr.in_common
	    || sym->attr.use_assoc
	    || sym->attr.dummy
	    || sym->attr.implied_index
2905
	    || sym->attr.flavor == FL_PARAMETER
2906 2907 2908 2909 2910 2911
	    || (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)))
2912 2913 2914 2915 2916 2917 2918
	{
	  t = SUCCESS;
	  break;
	}

      gfc_error ("Variable '%s' cannot appear in the expression at %L",
		 sym->name, &e->where);
2919 2920
      /* Prevent a repetition of the error.  */
      e->error = 1;
2921 2922 2923 2924 2925 2926 2927 2928
      break;

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

    case EXPR_SUBSTRING:
2929
      t = gfc_specification_expr (e->ref->u.ss.start);
2930 2931 2932
      if (t == FAILURE)
	break;

2933
      t = gfc_specification_expr (e->ref->u.ss.end);
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957
      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.  */

2958
gfc_try
2959
gfc_specification_expr (gfc_expr *e)
2960
{
2961
  gfc_component *comp;
2962

2963 2964
  if (e == NULL)
    return SUCCESS;
2965 2966 2967

  if (e->ts.type != BT_INTEGER)
    {
2968 2969
      gfc_error ("Expression at %L must be of INTEGER type, found %s",
		 &e->where, gfc_basic_typename (e->ts.type));
2970 2971 2972
      return FAILURE;
    }

2973 2974 2975
  if (e->expr_type == EXPR_FUNCTION
	  && !e->value.function.isym
	  && !e->value.function.esym
2976 2977 2978
	  && !gfc_pure (e->symtree->n.sym)
	  && (!gfc_is_proc_ptr_comp (e, &comp)
	      || !comp->attr.pure))
2979 2980 2981 2982 2983 2984 2985 2986
    {
      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;
    }

2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003
  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.  */

3004
gfc_try
3005
gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
3006 3007 3008
{
  int op1_flag, op2_flag, d;
  mpz_t op1_size, op2_size;
3009
  gfc_try t;
3010

3011 3012 3013
  va_list argp;
  char buffer[240];

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

3017 3018 3019 3020
  va_start (argp, optype_msgid);
  vsnprintf (buffer, 240, optype_msgid, argp);
  va_end (argp);

3021 3022
  if (op1->rank != op2->rank)
    {
3023
      gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
3024
		 op1->rank, op2->rank, &op1->where);
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
      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)
	{
3037
	  gfc_error ("Different shape for %s at %L on dimension %d "
3038
		     "(%d and %d)", _(buffer), &op1->where, d + 1,
3039
		     (int) mpz_get_si (op1_size),
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
		     (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.  */

3061
gfc_try
3062
gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform)
3063 3064
{
  gfc_symbol *sym;
3065 3066
  gfc_ref *ref;
  int has_pointer;
3067 3068 3069

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

3070
  /* See if this is the component or subcomponent of a pointer.  */
3071 3072
  has_pointer = sym->attr.pointer;
  for (ref = lvalue->ref; ref; ref = ref->next)
3073
    if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
3074 3075 3076 3077 3078
      {
	has_pointer = 1;
	break;
      }

3079 3080 3081 3082 3083
  /* 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:  */
3084 3085
  if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
      && !sym->attr.external)
Paul Thomas committed
3086
    {
3087 3088 3089
      bool bad_proc;
      bad_proc = false;

3090
      /* (i) Use associated;  */
3091 3092 3093
      if (sym->attr.use_assoc)
	bad_proc = true;

3094
      /* (ii) The assignment is in the main program; or  */
3095 3096 3097
      if (gfc_current_ns->proc_name->attr.is_main_program)
	bad_proc = true;

3098
      /* (iii) A module or internal procedure...  */
3099
      if ((gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
3100
	   || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
3101 3102
	  && gfc_current_ns->parent
	  && (!(gfc_current_ns->parent->proc_name->attr.function
3103
		|| gfc_current_ns->parent->proc_name->attr.subroutine)
3104 3105
	      || gfc_current_ns->parent->proc_name->attr.is_main_program))
	{
3106
	  /* ... that is not a function...  */ 
3107 3108 3109
	  if (!gfc_current_ns->proc_name->attr.function)
	    bad_proc = true;

3110
	  /* ... or is not an entry and has a different name.  */
3111 3112 3113
	  if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
	    bad_proc = true;
	}
Paul Thomas committed
3114

3115 3116 3117 3118 3119 3120 3121 3122 3123
      /* (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;

3124 3125 3126 3127 3128 3129
      if (bad_proc)
	{
	  gfc_error ("'%s' at %L is not a VALUE", sym->name, &lvalue->where);
	  return FAILURE;
	}
    }
Paul Thomas committed
3130

3131 3132
  if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
    {
3133 3134
      gfc_error ("Incompatible ranks %d and %d in assignment at %L",
		 lvalue->rank, rvalue->rank, &lvalue->where);
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
      return FAILURE;
    }

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

3145 3146
  if (rvalue->expr_type == EXPR_NULL)
    {  
3147
      if (has_pointer && (ref == NULL || ref->next == NULL)
3148 3149 3150 3151 3152 3153 3154 3155 3156
	  && 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;
	}
    }
3157

3158
  /* This is possibly a typo: x = f() instead of x => f().  */
3159 3160 3161 3162 3163 3164
  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);

3165 3166
  /* Check size of array assignments.  */
  if (lvalue->rank != 0 && rvalue->rank != 0
3167
      && gfc_check_conformance (lvalue, rvalue, "array assignment") != SUCCESS)
3168 3169
    return FAILURE;

3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
  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)
    {
3186
      int rc;
3187 3188 3189 3190
      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);
3191 3192
      if (!gfc_convert_boz (rvalue, &lvalue->ts))
	return FAILURE;
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
      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;
	}
3209 3210
    }

3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
  /*  Warn about type-changing conversions for REAL or COMPLEX constants.
      If lvalue and rvalue are mixed REAL and complex, gfc_compare_types
      will warn anyway, so there is no need to to so here.  */

  if (rvalue->expr_type == EXPR_CONSTANT && lvalue->ts.type == rvalue->ts.type
      && (lvalue->ts.type == BT_REAL || lvalue->ts.type == BT_COMPLEX))
    {
      if (lvalue->ts.kind < rvalue->ts.kind && gfc_option.gfc_warn_conversion)
	{
	  /* As a special bonus, don't warn about REAL rvalues which are not
	     changed by the conversion if -Wconversion is specified.  */
	  if (rvalue->ts.type == BT_REAL && mpfr_number_p (rvalue->value.real))
	    {
	      /* Calculate the difference between the constant and the rounded
		 value and check it against zero.  */
	      mpfr_t rv, diff;
	      gfc_set_model_kind (lvalue->ts.kind);
	      mpfr_init (rv);
	      gfc_set_model_kind (rvalue->ts.kind);
	      mpfr_init (diff);
	      
	      mpfr_set (rv, rvalue->value.real, GFC_RND_MODE);
	      mpfr_sub (diff, rv, rvalue->value.real, GFC_RND_MODE);
	  
	      if (!mpfr_zero_p (diff))
		gfc_warning ("Change of value in conversion from "
			     " %s to %s at %L", gfc_typename (&rvalue->ts),
			     gfc_typename (&lvalue->ts), &rvalue->where);
	      
	      mpfr_clear (rv);
	      mpfr_clear (diff);
	    }
	  else
	    gfc_warning ("Possible change of value in conversion from %s "
			 "to %s at %L",gfc_typename (&rvalue->ts),
			 gfc_typename (&lvalue->ts), &rvalue->where);

	}
      else if (gfc_option.warn_conversion_extra
	       && lvalue->ts.kind > rvalue->ts.kind)
	{
	  gfc_warning ("Conversion from %s to %s at %L",
		       gfc_typename (&rvalue->ts),
		       gfc_typename (&lvalue->ts), &rvalue->where);
	}
    }

3258 3259 3260
  if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
    return SUCCESS;

3261
  /* Only DATA Statements come here.  */
3262 3263
  if (!conform)
    {
3264 3265 3266 3267
      /* 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)
3268 3269
	return SUCCESS;

3270 3271 3272
      if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
	return SUCCESS;

3273 3274 3275
      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));
3276 3277 3278 3279

      return FAILURE;
    }

3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
  /* 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;
    }

3290 3291 3292 3293 3294 3295 3296 3297
  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.  */

3298
gfc_try
3299
gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue)
3300 3301
{
  symbol_attribute attr;
3302
  gfc_ref *ref;
3303
  bool is_pure, is_implicit_pure, rank_remap;
3304
  int proc_pointer;
3305

3306 3307
  if (lvalue->symtree->n.sym->ts.type == BT_UNKNOWN
      && !lvalue->symtree->n.sym->attr.proc_pointer)
3308 3309 3310 3311 3312 3313
    {
      gfc_error ("Pointer assignment target is not a POINTER at %L",
		 &lvalue->where);
      return FAILURE;
    }

Paul Thomas committed
3314
  if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
3315 3316
      && lvalue->symtree->n.sym->attr.use_assoc
      && !lvalue->symtree->n.sym->attr.proc_pointer)
Paul Thomas committed
3317 3318 3319 3320 3321 3322 3323
    {
      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;
    }

3324
  proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
3325

3326
  rank_remap = false;
3327 3328
  for (ref = lvalue->ref; ref; ref = ref->next)
    {
3329
      if (ref->type == REF_COMPONENT)
3330
	proc_pointer = ref->u.c.component->attr.proc_pointer;
3331 3332 3333

      if (ref->type == REF_ARRAY && ref->next == NULL)
	{
3334 3335
	  int dim;

3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
	  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 "
3348
			      "at %L", lvalue->symtree->n.sym->name,
3349
			      &lvalue->where) == FAILURE)
3350
	    return FAILURE;
3351

3352 3353 3354 3355 3356
	  /* When bounds are given, all lbounds are necessary and either all
	     or none of the upper bounds; no strides are allowed.  If the
	     upper bounds are present, we may do rank remapping.  */
	  for (dim = 0; dim < ref->u.ar.dimen; ++dim)
	    {
3357 3358
	      if (!ref->u.ar.start[dim]
		  || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
		{
		  gfc_error ("Lower bound has to be present at %L",
			     &lvalue->where);
		  return FAILURE;
		}
	      if (ref->u.ar.stride[dim])
		{
		  gfc_error ("Stride must not be present at %L",
			     &lvalue->where);
		  return FAILURE;
		}

	      if (dim == 0)
		rank_remap = (ref->u.ar.end[dim] != NULL);
	      else
		{
		  if ((rank_remap && !ref->u.ar.end[dim])
		      || (!rank_remap && ref->u.ar.end[dim]))
		    {
		      gfc_error ("Either all or none of the upper bounds"
				 " must be specified at %L", &lvalue->where);
		      return FAILURE;
		    }
		}
	    }
3384
	}
3385 3386
    }

3387
  is_pure = gfc_pure (NULL);
3388
  is_implicit_pure = gfc_implicit_pure (NULL);
3389 3390 3391 3392

  /* 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.  */
3393
  if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
3394 3395
    return SUCCESS;

3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409
  /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283.  */
  if (lvalue->expr_type == EXPR_VARIABLE
      && gfc_is_coindexed (lvalue))
    {
      gfc_ref *ref;
      for (ref = lvalue->ref; ref; ref = ref->next)
	if (ref->type == REF_ARRAY && ref->u.ar.codimen)
	  {
	    gfc_error ("Pointer object at %L shall not have a coindex",
		       &lvalue->where);
	    return FAILURE;
	  }
    }

3410
  /* Checks on rvalue for procedure pointer assignments.  */
3411
  if (proc_pointer)
3412
    {
3413
      char err[200];
3414 3415 3416 3417
      gfc_symbol *s1,*s2;
      gfc_component *comp;
      const char *name;

3418 3419 3420
      attr = gfc_expr_attr (rvalue);
      if (!((rvalue->expr_type == EXPR_NULL)
	    || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
3421
	    || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
3422 3423 3424 3425 3426 3427 3428
	    || (rvalue->expr_type == EXPR_VARIABLE
		&& attr.flavor == FL_PROCEDURE)))
	{
	  gfc_error ("Invalid procedure pointer assignment at %L",
		     &rvalue->where);
	  return FAILURE;
	}
3429 3430 3431 3432 3433
      if (attr.abstract)
	{
	  gfc_error ("Abstract interface '%s' is invalid "
		     "in procedure pointer assignment at %L",
		     rvalue->symtree->name, &rvalue->where);
3434
	  return FAILURE;
3435
	}
3436
      /* Check for F08:C729.  */
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
      if (attr.flavor == FL_PROCEDURE)
	{
	  if (attr.proc == PROC_ST_FUNCTION)
	    {
	      gfc_error ("Statement function '%s' is invalid "
			 "in procedure pointer assignment at %L",
			 rvalue->symtree->name, &rvalue->where);
	      return FAILURE;
	    }
	  if (attr.proc == PROC_INTERNAL &&
	      gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is "
			      "invalid in procedure pointer assignment at %L",
			      rvalue->symtree->name, &rvalue->where) == FAILURE)
	    return FAILURE;
	}
3452 3453 3454 3455 3456 3457 3458 3459
      /* Check for F08:C730.  */
      if (attr.elemental && !attr.intrinsic)
	{
	  gfc_error ("Nonintrinsic elemental procedure '%s' is invalid "
		     "in procedure pointer assigment at %L",
		     rvalue->symtree->name, &rvalue->where);
	  return FAILURE;
	}
3460 3461 3462 3463 3464 3465 3466 3467

      /* Ensure that the calling convention is the same. As other attributes
	 such as DLLEXPORT may differ, one explicitly only tests for the
	 calling conventions.  */
      if (rvalue->expr_type == EXPR_VARIABLE
	  && lvalue->symtree->n.sym->attr.ext_attr
	       != rvalue->symtree->n.sym->attr.ext_attr)
	{
3468
	  symbol_attribute calls;
3469

3470 3471 3472 3473
	  calls.ext_attr = 0;
	  gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
	  gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
	  gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
3474

3475 3476
	  if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
	      != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
3477 3478 3479 3480 3481 3482 3483 3484
	    {
	      gfc_error ("Mismatch in the procedure pointer assignment "
			 "at %L: mismatch in the calling convention",
			 &rvalue->where);
	  return FAILURE;
	    }
	}

3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
      if (gfc_is_proc_ptr_comp (lvalue, &comp))
	s1 = comp->ts.interface;
      else
	s1 = lvalue->symtree->n.sym;

      if (gfc_is_proc_ptr_comp (rvalue, &comp))
	{
	  s2 = comp->ts.interface;
	  name = comp->name;
	}
      else if (rvalue->expr_type == EXPR_FUNCTION)
	{
	  s2 = rvalue->symtree->n.sym->result;
	  name = rvalue->symtree->n.sym->result->name;
	}
      else
	{
	  s2 = rvalue->symtree->n.sym;
	  name = rvalue->symtree->n.sym->name;
	}

      if (s1 && s2 && !gfc_compare_interfaces (s1, s2, name, 0, 1,
					       err, sizeof(err)))
3508
	{
3509 3510
	  gfc_error ("Interface mismatch in procedure pointer assignment "
		     "at %L: %s", &rvalue->where, err);
3511
	  return FAILURE;
3512
	}
3513

3514 3515
      return SUCCESS;
    }
3516

3517
  if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
3518
    {
3519 3520 3521
      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));
3522 3523
      return FAILURE;
    }
3524

3525
  if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
3526
    {
3527
      gfc_error ("Different kind type parameters in pointer "
3528 3529 3530
		 "assignment at %L", &lvalue->where);
      return FAILURE;
    }
3531

3532
  if (lvalue->rank != rvalue->rank && !rank_remap)
3533
    {
3534
      gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
3535 3536 3537
      return FAILURE;
    }

3538 3539 3540 3541
  if (lvalue->ts.type == BT_CLASS && rvalue->ts.type == BT_DERIVED)
    /* Make sure the vtab is present.  */
    gfc_find_derived_vtab (rvalue->ts.u.derived);

3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
  /* Check rank remapping.  */
  if (rank_remap)
    {
      mpz_t lsize, rsize;

      /* If this can be determined, check that the target must be at least as
	 large as the pointer assigned to it is.  */
      if (gfc_array_size (lvalue, &lsize) == SUCCESS
	  && gfc_array_size (rvalue, &rsize) == SUCCESS
	  && mpz_cmp (rsize, lsize) < 0)
	{
	  gfc_error ("Rank remapping target is smaller than size of the"
		     " pointer (%ld < %ld) at %L",
		     mpz_get_si (rsize), mpz_get_si (lsize),
		     &lvalue->where);
	  return FAILURE;
	}

      /* The target must be either rank one or it must be simply contiguous
	 and F2008 must be allowed.  */
      if (rvalue->rank != 1)
	{
	  if (!gfc_is_simply_contiguous (rvalue, true))
	    {
	      gfc_error ("Rank remapping target must be rank 1 or"
			 " simply contiguous at %L", &rvalue->where);
	      return FAILURE;
	    }
	  if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Rank remapping"
			      " target is not rank 1 at %L", &rvalue->where)
		== FAILURE)
	    return FAILURE;
	}
    }

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

3581
  if (lvalue->ts.type == BT_CHARACTER)
Paul Thomas committed
3582
    {
3583 3584 3585
      gfc_try t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
      if (t == FAILURE)
	return FAILURE;
Paul Thomas committed
3586 3587
    }

3588 3589 3590
  if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
    lvalue->symtree->n.sym->attr.subref_array_pointer = 1;

3591
  attr = gfc_expr_attr (rvalue);
3592 3593 3594 3595 3596 3597 3598 3599 3600

  if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
    {
      gfc_error ("Target expression in pointer assignment "
		 "at %L must deliver a pointer result",
		 &rvalue->where);
      return FAILURE;
    }

3601 3602
  if (!attr.target && !attr.pointer)
    {
3603
      gfc_error ("Pointer assignment target is neither TARGET "
3604 3605 3606
		 "nor POINTER at %L", &rvalue->where);
      return FAILURE;
    }
3607

3608 3609
  if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    {
3610
      gfc_error ("Bad target in pointer assignment in PURE "
3611 3612
		 "procedure at %L", &rvalue->where);
    }
3613

3614 3615 3616 3617
  if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    gfc_current_ns->proc_name->attr.implicit_pure = 0;
    

3618 3619 3620 3621 3622 3623 3624
  if (gfc_has_vector_index (rvalue))
    {
      gfc_error ("Pointer assignment with vector subscript "
		 "on rhs at %L", &rvalue->where);
      return FAILURE;
    }

3625 3626
  if (attr.is_protected && attr.use_assoc
      && !(attr.pointer || attr.proc_pointer))
3627
    {
3628
      gfc_error ("Pointer assignment target has PROTECTED "
3629
		 "attribute at %L", &rvalue->where);
3630 3631 3632
      return FAILURE;
    }

3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
  /* F2008, C725. For PURE also C1283.  */
  if (rvalue->expr_type == EXPR_VARIABLE
      && gfc_is_coindexed (rvalue))
    {
      gfc_ref *ref;
      for (ref = rvalue->ref; ref; ref = ref->next)
	if (ref->type == REF_ARRAY && ref->u.ar.codimen)
	  {
	    gfc_error ("Data target at %L shall not have a coindex",
		       &rvalue->where);
	    return FAILURE;
	  }
    }

3647 3648 3649 3650 3651
  return SUCCESS;
}


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

3654
gfc_try
3655
gfc_check_assign_symbol (gfc_symbol *sym, gfc_expr *rvalue)
3656 3657
{
  gfc_expr lvalue;
3658
  gfc_try r;
3659 3660 3661 3662 3663 3664 3665

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

  lvalue.expr_type = EXPR_VARIABLE;
  lvalue.ts = sym->ts;
  if (sym->as)
    lvalue.rank = sym->as->rank;
3666
  lvalue.symtree = XCNEW (gfc_symtree);
3667 3668 3669
  lvalue.symtree->n.sym = sym;
  lvalue.where = sym->declared_at;

3670
  if (sym->attr.pointer || sym->attr.proc_pointer
3671
      || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->attr.class_pointer
3672
	  && rvalue->expr_type == EXPR_NULL))
3673 3674 3675
    r = gfc_check_pointer_assign (&lvalue, rvalue);
  else
    r = gfc_check_assign (&lvalue, rvalue, 1);
3676

3677
  free (lvalue.symtree);
3678

3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
  if (r == FAILURE)
    return r;
  
  if (sym->attr.pointer && rvalue->expr_type != EXPR_NULL)
    {
      /* F08:C461. Additional checks for pointer initialization.  */
      symbol_attribute attr;
      attr = gfc_expr_attr (rvalue);
      if (attr.allocatable)
	{
	  gfc_error ("Pointer initialization target at %C "
	             "must not be ALLOCATABLE ");
	  return FAILURE;
	}
3693
      if (!attr.target || attr.pointer)
3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705
	{
	  gfc_error ("Pointer initialization target at %C "
		     "must have the TARGET attribute");
	  return FAILURE;
	}
      if (!attr.save)
	{
	  gfc_error ("Pointer initialization target at %C "
		     "must have the SAVE attribute");
	  return FAILURE;
	}
    }
3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
    
  if (sym->attr.proc_pointer && rvalue->expr_type != EXPR_NULL)
    {
      /* F08:C1220. Additional checks for procedure pointer initialization.  */
      symbol_attribute attr = gfc_expr_attr (rvalue);
      if (attr.proc_pointer)
	{
	  gfc_error ("Procedure pointer initialization target at %L "
		     "may not be a procedure pointer", &rvalue->where);
	  return FAILURE;
	}
    }
3718 3719

  return SUCCESS;
3720
}
3721 3722


3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737
/* Check for default initializer; sym->value is not enough
   as it is also set for EXPR_NULL of allocatables.  */

bool
gfc_has_default_initializer (gfc_symbol *der)
{
  gfc_component *c;

  gcc_assert (der->attr.flavor == FL_DERIVED);
  for (c = der->components; c; c = c->next)
    if (c->ts.type == BT_DERIVED)
      {
        if (!c->attr.pointer
	     && gfc_has_default_initializer (c->ts.u.derived))
	  return true;
3738 3739
	if (c->attr.pointer && c->initializer)
	  return true;
3740 3741 3742 3743 3744 3745 3746 3747 3748 3749
      }
    else
      {
        if (c->initializer)
	  return true;
      }

  return false;
}

3750

3751 3752 3753 3754 3755 3756
/* Get an expression for a default initializer.  */

gfc_expr *
gfc_default_initializer (gfc_typespec *ts)
{
  gfc_expr *init;
Jerry DeLisle committed
3757
  gfc_component *comp;
3758

3759 3760
  /* See if we have a default initializer in this, but not in nested
     types (otherwise we could use gfc_has_default_initializer()).  */
Jerry DeLisle committed
3761
  for (comp = ts->u.derived->components; comp; comp = comp->next)
3762 3763
    if (comp->initializer || comp->attr.allocatable
	|| (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
3764
      break;
3765

Jerry DeLisle committed
3766
  if (!comp)
3767 3768
    return NULL;

Jerry DeLisle committed
3769 3770
  init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
					     &ts->u.derived->declared_at);
3771
  init->ts = *ts;
3772

Jerry DeLisle committed
3773
  for (comp = ts->u.derived->components; comp; comp = comp->next)
3774
    {
Jerry DeLisle committed
3775
      gfc_constructor *ctor = gfc_constructor_get();
3776

Jerry DeLisle committed
3777
      if (comp->initializer)
3778 3779 3780 3781 3782 3783 3784
	{
	  ctor->expr = gfc_copy_expr (comp->initializer);
	  if ((comp->ts.type != comp->initializer->ts.type
	       || comp->ts.kind != comp->initializer->ts.kind)
	      && !comp->attr.pointer && !comp->attr.proc_pointer)
	    gfc_convert_type_warn (ctor->expr, &comp->ts, 2, false);
	}
Paul Thomas committed
3785

3786 3787
      if (comp->attr.allocatable
	  || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
Paul Thomas committed
3788
	{
Jerry DeLisle committed
3789 3790 3791
	  ctor->expr = gfc_get_expr ();
	  ctor->expr->expr_type = EXPR_NULL;
	  ctor->expr->ts = comp->ts;
Paul Thomas committed
3792
	}
Jerry DeLisle committed
3793 3794

      gfc_constructor_append (&init->value.constructor, ctor);
3795
    }
Jerry DeLisle committed
3796

3797 3798
  return init;
}
3799 3800 3801 3802 3803 3804 3805


/* 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 *
3806
gfc_get_variable_expr (gfc_symtree *var)
3807 3808 3809 3810 3811 3812 3813 3814
{
  gfc_expr *e;

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

3815 3816 3817
  if ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
      || (var->n.sym->ts.type == BT_CLASS && CLASS_DATA (var->n.sym)
	  && CLASS_DATA (var->n.sym)->as))
3818
    {
3819 3820
      e->rank = var->n.sym->ts.type == BT_CLASS
		? CLASS_DATA (var->n.sym)->as->rank : var->n.sym->as->rank;
3821 3822 3823 3824 3825 3826 3827 3828
      e->ref = gfc_get_ref ();
      e->ref->type = REF_ARRAY;
      e->ref->u.ar.type = AR_FULL;
    }

  return e;
}

3829

3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
gfc_expr *
gfc_lval_expr_from_sym (gfc_symbol *sym)
{
  gfc_expr *lval;
  lval = gfc_get_expr ();
  lval->expr_type = EXPR_VARIABLE;
  lval->where = sym->declared_at;
  lval->ts = sym->ts;
  lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);

  /* It will always be a full array.  */
  lval->rank = sym->as ? sym->as->rank : 0;
  if (lval->rank)
    {
      lval->ref = gfc_get_ref ();
      lval->ref->type = REF_ARRAY;
      lval->ref->u.ar.type = AR_FULL;
      lval->ref->u.ar.dimen = lval->rank;
      lval->ref->u.ar.where = sym->declared_at;
3849 3850
      lval->ref->u.ar.as = sym->ts.type == BT_CLASS
			   ? CLASS_DATA (sym)->as : sym->as;
3851 3852 3853 3854 3855 3856
    }

  return lval;
}


3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908
/* Returns the array_spec of a full array expression.  A NULL is
   returned otherwise.  */
gfc_array_spec *
gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
{
  gfc_array_spec *as;
  gfc_ref *ref;

  if (expr->rank == 0)
    return NULL;

  /* Follow any component references.  */
  if (expr->expr_type == EXPR_VARIABLE
      || expr->expr_type == EXPR_CONSTANT)
    {
      as = expr->symtree->n.sym->as;
      for (ref = expr->ref; ref; ref = ref->next)
	{
	  switch (ref->type)
	    {
	    case REF_COMPONENT:
	      as = ref->u.c.component->as;
	      continue;

	    case REF_SUBSTRING:
	      continue;

	    case REF_ARRAY:
	      {
		switch (ref->u.ar.type)
		  {
		  case AR_ELEMENT:
		  case AR_SECTION:
		  case AR_UNKNOWN:
		    as = NULL;
		    continue;

		  case AR_FULL:
		    break;
		  }
		break;
	      }
	    }
	}
    }
  else
    as = NULL;

  return as;
}


Paul Thomas committed
3909
/* General expression traversal function.  */
3910

Paul Thomas committed
3911 3912 3913 3914
bool
gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
		   bool (*func)(gfc_expr *, gfc_symbol *, int*),
		   int f)
3915
{
Paul Thomas committed
3916
  gfc_array_ref ar;
3917
  gfc_ref *ref;
Paul Thomas committed
3918 3919
  gfc_actual_arglist *args;
  gfc_constructor *c;
3920 3921
  int i;

Paul Thomas committed
3922 3923
  if (!expr)
    return false;
3924

3925 3926
  if ((*func) (expr, sym, &f))
    return true;
3927

3928
  if (expr->ts.type == BT_CHARACTER
3929 3930 3931 3932
	&& expr->ts.u.cl
	&& expr->ts.u.cl->length
	&& expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
	&& gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
3933
    return true;
3934

3935 3936
  switch (expr->expr_type)
    {
3937 3938
    case EXPR_PPC:
    case EXPR_COMPCALL:
Paul Thomas committed
3939 3940 3941 3942 3943 3944
    case EXPR_FUNCTION:
      for (args = expr->value.function.actual; args; args = args->next)
	{
	  if (gfc_traverse_expr (args->expr, sym, func, f))
	    return true;
	}
3945 3946
      break;

3947
    case EXPR_VARIABLE:
3948 3949 3950 3951 3952 3953 3954
    case EXPR_CONSTANT:
    case EXPR_NULL:
    case EXPR_SUBSTRING:
      break;

    case EXPR_STRUCTURE:
    case EXPR_ARRAY:
Jerry DeLisle committed
3955 3956
      for (c = gfc_constructor_first (expr->value.constructor);
	   c; c = gfc_constructor_next (c))
3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971
	{
	  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;
	    }
	}
3972 3973
      break;

Paul Thomas committed
3974 3975 3976 3977 3978 3979 3980
    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;

3981 3982 3983 3984 3985
    default:
      gcc_unreachable ();
      break;
    }

Paul Thomas committed
3986 3987 3988
  ref = expr->ref;
  while (ref != NULL)
    {
3989
      switch (ref->type)
3990
	{
Paul Thomas committed
3991 3992 3993
	case  REF_ARRAY:
	  ar = ref->u.ar;
	  for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3994
	    {
Paul Thomas committed
3995 3996 3997 3998 3999 4000
	      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;
4001 4002
	    }
	  break;
Paul Thomas committed
4003

4004
	case REF_SUBSTRING:
Paul Thomas committed
4005 4006 4007 4008
	  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;
4009
	  break;
Paul Thomas committed
4010

4011 4012
	case REF_COMPONENT:
	  if (ref->u.c.component->ts.type == BT_CHARACTER
4013 4014 4015
		&& ref->u.c.component->ts.u.cl
		&& ref->u.c.component->ts.u.cl->length
		&& ref->u.c.component->ts.u.cl->length->expr_type
4016
		     != EXPR_CONSTANT
4017
		&& gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
4018 4019 4020 4021
				      sym, func, f))
	    return true;

	  if (ref->u.c.component->as)
4022 4023
	    for (i = 0; i < ref->u.c.component->as->rank
			    + ref->u.c.component->as->corank; i++)
4024 4025 4026 4027 4028 4029 4030 4031 4032
	      {
		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
4033

4034 4035 4036
	default:
	  gcc_unreachable ();
	}
Paul Thomas committed
4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048
      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)
{
4049 4050
  if (expr->expr_type != EXPR_VARIABLE)
    return false;
Paul Thomas committed
4051 4052 4053 4054 4055 4056 4057 4058
  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);
4059
}
4060 4061


4062 4063 4064 4065 4066
/* Determine if an expression is a procedure pointer component. If yes, the
   argument 'comp' will point to the component (provided that 'comp' was
   provided).  */

bool
4067
gfc_is_proc_ptr_comp (gfc_expr *expr, gfc_component **comp)
4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
{
  gfc_ref *ref;
  bool ppc = false;

  if (!expr || !expr->ref)
    return false;

  ref = expr->ref;
  while (ref->next)
    ref = ref->next;

  if (ref->type == REF_COMPONENT)
    {
      ppc = ref->u.c.component->attr.proc_pointer;
      if (ppc && comp)
	*comp = ref->u.c.component;
    }

  return ppc;
}


4090 4091
/* 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
4092 4093
   mode as is a basic arithmetic expression using those; this is for things in
   legacy-code like:
4094 4095

     INTEGER :: arr(n), n
4096
     INTEGER :: arr(n + 1), n
4097 4098 4099

   The namespace is needed for IMPLICIT typing.  */

4100 4101 4102 4103 4104
static gfc_namespace* check_typed_ns;

static bool
expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
                       int* f ATTRIBUTE_UNUSED)
4105 4106 4107
{
  gfc_try t;

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

4111 4112 4113
  gcc_assert (e->symtree);
  t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
                              true, e->where);
4114

4115 4116
  return (t == FAILURE);
}
4117

4118 4119 4120 4121
gfc_try
gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
{
  bool error_found;
4122

4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142
  /* 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;
	}
    }
4143

4144 4145 4146
  /* Otherwise, walk the expression and do it strictly.  */
  check_typed_ns = ns;
  error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
4147

4148
  return error_found ? FAILURE : SUCCESS;
4149
}
4150

4151 4152 4153

/* Walk an expression tree and replace all dummy symbols by the corresponding
   symbol in the formal_ns of "sym". Needed for copying interfaces in PROCEDURE
4154 4155 4156 4157 4158
   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)
{
4159 4160 4161
  if ((expr->expr_type == EXPR_VARIABLE 
       || (expr->expr_type == EXPR_FUNCTION
	   && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
4162 4163
      && expr->symtree->n.sym->ns == sym->ts.interface->formal_ns
      && expr->symtree->n.sym->attr.dummy)
4164
    {
4165 4166 4167
      gfc_symtree *root = sym->formal_ns ? sym->formal_ns->sym_root
					 : gfc_current_ns->sym_root;
      gfc_symtree *stree = gfc_find_symtree (root, expr->symtree->n.sym->name);
4168
      gcc_assert (stree);
4169
      stree->n.sym->attr = expr->symtree->n.sym->attr;
4170 4171 4172 4173 4174 4175 4176 4177 4178 4179
      expr->symtree = stree;
    }
  return false;
}

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

4181

4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216
/* The following is analogous to 'replace_symbol', and needed for copying
   interfaces for procedure pointer components. The argument 'sym' must formally
   be a gfc_symbol, so that the function can be passed to gfc_traverse_expr.
   However, it gets actually passed a gfc_component (i.e. the procedure pointer
   component in whose formal_ns the arguments have to be).  */

static bool
replace_comp (gfc_expr *expr, gfc_symbol *sym, int *i ATTRIBUTE_UNUSED)
{
  gfc_component *comp;
  comp = (gfc_component *)sym;
  if ((expr->expr_type == EXPR_VARIABLE 
       || (expr->expr_type == EXPR_FUNCTION
	   && !gfc_is_intrinsic (expr->symtree->n.sym, 0, expr->where)))
      && expr->symtree->n.sym->ns == comp->ts.interface->formal_ns)
    {
      gfc_symtree *stree;
      gfc_namespace *ns = comp->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);
      stree->n.sym->attr = expr->symtree->n.sym->attr;
      expr->symtree = stree;
    }
  return false;
}

void
gfc_expr_replace_comp (gfc_expr *expr, gfc_component *dest)
{
  gfc_traverse_expr (expr, (gfc_symbol *)dest, &replace_comp, 0);
}

4217 4218

bool
4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233
gfc_ref_this_image (gfc_ref *ref)
{
  int n;

  gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);

  for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
      return false;

  return true;
}


bool
4234 4235 4236 4237 4238 4239
gfc_is_coindexed (gfc_expr *e)
{
  gfc_ref *ref;

  for (ref = e->ref; ref; ref = ref->next)
    if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4240
      return !gfc_ref_this_image (ref);
4241 4242 4243 4244 4245

  return false;
}


4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
/* Coarrays are variables with a corank but not being coindexed. However, also
   the following is a coarray: A subobject of a coarray is a coarray if it does
   not have any cosubscripts, vector subscripts, allocatable component
   selection, or pointer component selection. (F2008, 2.4.7)  */

bool
gfc_is_coarray (gfc_expr *e)
{
  gfc_ref *ref;
  gfc_symbol *sym;
  gfc_component *comp;
  bool coindexed;
  bool coarray;
  int i;

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

  coindexed = false;
  sym = e->symtree->n.sym;

  if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    coarray = CLASS_DATA (sym)->attr.codimension;
  else
    coarray = sym->attr.codimension;

  for (ref = e->ref; ref; ref = ref->next)
    switch (ref->type)
    {
      case REF_COMPONENT:
	comp = ref->u.c.component;
4277 4278 4279
	if (comp->ts.type == BT_CLASS && comp->attr.class_ok
	    && (CLASS_DATA (comp)->attr.class_pointer
		|| CLASS_DATA (comp)->attr.allocatable))
4280 4281
	  {
	    coindexed = false;
4282 4283 4284 4285 4286 4287
	    coarray = CLASS_DATA (comp)->attr.codimension;
	  }
        else if (comp->attr.pointer || comp->attr.allocatable)
	  {
	    coindexed = false;
	    coarray = comp->attr.codimension;
4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316
	  }
        break;

     case REF_ARRAY:
	if (!coarray)
	  break;

	if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
	  {
	    coindexed = true;
	    break;
	  }

	for (i = 0; i < ref->u.ar.dimen; i++)
	  if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
	    {
	      coarray = false;
	      break;
	    }
	break;

     case REF_SUBSTRING:
	break;
    }

  return coarray && !coindexed;
}


4317
int
4318 4319 4320 4321
gfc_get_corank (gfc_expr *e)
{
  int corank;
  gfc_ref *ref;
4322 4323 4324 4325

  if (!gfc_is_coarray (e))
    return 0;

4326 4327 4328 4329 4330
  if (e->ts.type == BT_CLASS && e->ts.u.derived->components)
    corank = e->ts.u.derived->components->as
	     ? e->ts.u.derived->components->as->corank : 0;
  else 
    corank = e->symtree->n.sym->as ? e->symtree->n.sym->as->corank : 0;
4331

4332 4333 4334 4335 4336 4337
  for (ref = e->ref; ref; ref = ref->next)
    {
      if (ref->type == REF_ARRAY)
	corank = ref->u.ar.as->corank;
      gcc_assert (ref->type != REF_SUBSTRING);
    }
4338

4339 4340 4341 4342
  return corank;
}


4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
/* Check whether the expression has an ultimate allocatable component.
   Being itself allocatable does not count.  */
bool
gfc_has_ultimate_allocatable (gfc_expr *e)
{
  gfc_ref *ref, *last = NULL;

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

  for (ref = e->ref; ref; ref = ref->next)
    if (ref->type == REF_COMPONENT)
      last = ref;

  if (last && last->u.c.component->ts.type == BT_CLASS)
4358
    return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
4359 4360 4361 4362 4363 4364
  else if (last && last->u.c.component->ts.type == BT_DERIVED)
    return last->u.c.component->ts.u.derived->attr.alloc_comp;
  else if (last)
    return false;

  if (e->ts.type == BT_CLASS)
4365
    return CLASS_DATA (e)->attr.alloc_comp;
4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387
  else if (e->ts.type == BT_DERIVED)
    return e->ts.u.derived->attr.alloc_comp;
  else
    return false;
}


/* Check whether the expression has an pointer component.
   Being itself a pointer does not count.  */
bool
gfc_has_ultimate_pointer (gfc_expr *e)
{
  gfc_ref *ref, *last = NULL;

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

  for (ref = e->ref; ref; ref = ref->next)
    if (ref->type == REF_COMPONENT)
      last = ref;
 
  if (last && last->u.c.component->ts.type == BT_CLASS)
4388
    return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
4389 4390 4391 4392 4393 4394
  else if (last && last->u.c.component->ts.type == BT_DERIVED)
    return last->u.c.component->ts.u.derived->attr.pointer_comp;
  else if (last)
    return false;

  if (e->ts.type == BT_CLASS)
4395
    return CLASS_DATA (e)->attr.pointer_comp;
4396 4397 4398 4399 4400
  else if (e->ts.type == BT_DERIVED)
    return e->ts.u.derived->attr.pointer_comp;
  else
    return false;
}
4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414


/* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
   Note: A scalar is not regarded as "simply contiguous" by the standard.
   if bool is not strict, some futher checks are done - for instance,
   a "(::1)" is accepted.  */

bool
gfc_is_simply_contiguous (gfc_expr *expr, bool strict)
{
  bool colon;
  int i;
  gfc_array_ref *ar = NULL;
  gfc_ref *ref, *part_ref = NULL;
4415
  gfc_symbol *sym;
4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438

  if (expr->expr_type == EXPR_FUNCTION)
    return expr->value.function.esym
	   ? expr->value.function.esym->result->attr.contiguous : false;
  else if (expr->expr_type != EXPR_VARIABLE)
    return false;

  if (expr->rank == 0)
    return false;

  for (ref = expr->ref; ref; ref = ref->next)
    {
      if (ar)
	return false; /* Array shall be last part-ref. */

      if (ref->type == REF_COMPONENT)
	part_ref  = ref;
      else if (ref->type == REF_SUBSTRING)
	return false;
      else if (ref->u.ar.type != AR_ELEMENT)
	ar = &ref->u.ar;
    }

4439 4440 4441 4442 4443 4444 4445 4446 4447
  sym = expr->symtree->n.sym;
  if (expr->ts.type != BT_CLASS
	&& ((part_ref
		&& !part_ref->u.c.component->attr.contiguous
		&& part_ref->u.c.component->attr.pointer)
	    || (!part_ref
		&& !sym->attr.contiguous
		&& (sym->attr.pointer
		      || sym->as->type == AS_ASSUMED_SHAPE))))
4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
    return false;

  if (!ar || ar->type == AR_FULL)
    return true;

  gcc_assert (ar->type == AR_SECTION);

  /* Check for simply contiguous array */
  colon = true;
  for (i = 0; i < ar->dimen; i++)
    {
      if (ar->dimen_type[i] == DIMEN_VECTOR)
	return false;

      if (ar->dimen_type[i] == DIMEN_ELEMENT)
	{
	  colon = false;
	  continue;
	}

      gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);


      /* If the previous section was not contiguous, that's an error,
	 unless we have effective only one element and checking is not
	 strict.  */
      if (!colon && (strict || !ar->start[i] || !ar->end[i]
		     || ar->start[i]->expr_type != EXPR_CONSTANT
		     || ar->end[i]->expr_type != EXPR_CONSTANT
		     || mpz_cmp (ar->start[i]->value.integer,
				 ar->end[i]->value.integer) != 0))
	return false;

      /* Following the standard, "(::1)" or - if known at compile time -
	 "(lbound:ubound)" are not simply contigous; if strict
	 is false, they are regarded as simply contiguous.  */
      if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
			    || ar->stride[i]->ts.type != BT_INTEGER
			    || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
	return false;

      if (ar->start[i]
	  && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
	      || !ar->as->lower[i]
	      || ar->as->lower[i]->expr_type != EXPR_CONSTANT
	      || mpz_cmp (ar->start[i]->value.integer,
			  ar->as->lower[i]->value.integer) != 0))
	colon = false;

      if (ar->end[i]
	  && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
	      || !ar->as->upper[i]
	      || ar->as->upper[i]->expr_type != EXPR_CONSTANT
	      || mpz_cmp (ar->end[i]->value.integer,
			  ar->as->upper[i]->value.integer) != 0))
	colon = false;
    }
  
  return true;
}
4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532


/* Build call to an intrinsic procedure.  The number of arguments has to be
   passed (rather than ending the list with a NULL value) because we may
   want to add arguments but with a NULL-expression.  */

gfc_expr*
gfc_build_intrinsic_call (const char* name, locus where, unsigned numarg, ...)
{
  gfc_expr* result;
  gfc_actual_arglist* atail;
  gfc_intrinsic_sym* isym;
  va_list ap;
  unsigned i;

  isym = gfc_find_function (name);
  gcc_assert (isym);
  
  result = gfc_get_expr ();
  result->expr_type = EXPR_FUNCTION;
  result->ts = isym->ts;
  result->where = where;
  result->value.function.name = name;
  result->value.function.isym = isym;

4533 4534 4535 4536 4537
  result->symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
  gcc_assert (result->symtree
	      && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
		  || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));

4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555
  va_start (ap, numarg);
  atail = NULL;
  for (i = 0; i < numarg; ++i)
    {
      if (atail)
	{
	  atail->next = gfc_get_actual_arglist ();
	  atail = atail->next;
	}
      else
	atail = result->value.function.actual = gfc_get_actual_arglist ();

      atail->expr = va_arg (ap, gfc_expr*);
    }
  va_end (ap);

  return result;
}
4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566


/* Check if an expression may appear in a variable definition context
   (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
   This is called from the various places when resolving
   the pieces that make up such a context.

   Optionally, a possible error message can be suppressed if context is NULL
   and just the return status (SUCCESS / FAILURE) be requested.  */

gfc_try
4567 4568
gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
			  const char* context)
4569
{
4570
  gfc_symbol* sym = NULL;
4571 4572 4573 4574 4575 4576
  bool is_pointer;
  bool check_intentin;
  bool ptr_component;
  symbol_attribute attr;
  gfc_ref* ref;

4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
  if (e->expr_type == EXPR_VARIABLE)
    {
      gcc_assert (e->symtree);
      sym = e->symtree->n.sym;
    }
  else if (e->expr_type == EXPR_FUNCTION)
    {
      gcc_assert (e->symtree);
      sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
    }

4588 4589
  attr = gfc_expr_attr (e);
  if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
4590 4591 4592 4593 4594 4595 4596 4597 4598 4599
    {
      if (!(gfc_option.allow_std & GFC_STD_F2008))
	{
	  if (context)
	    gfc_error ("Fortran 2008: Pointer functions in variable definition"
		       " context (%s) at %L", context, &e->where);
	  return FAILURE;
	}
    }
  else if (e->expr_type != EXPR_VARIABLE)
4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
    {
      if (context)
	gfc_error ("Non-variable expression in variable definition context (%s)"
		   " at %L", context, &e->where);
      return FAILURE;
    }

  if (!pointer && sym->attr.flavor == FL_PARAMETER)
    {
      if (context)
	gfc_error ("Named constant '%s' in variable definition context (%s)"
		   " at %L", sym->name, context, &e->where);
      return FAILURE;
    }
  if (!pointer && sym->attr.flavor != FL_VARIABLE
      && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
      && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
    {
      if (context)
	gfc_error ("'%s' in variable definition context (%s) at %L is not"
		   " a variable", sym->name, context, &e->where);
      return FAILURE;
    }

  /* Find out whether the expr is a pointer; this also means following
     component references to the last one.  */
  is_pointer = (attr.pointer || attr.proc_pointer);
  if (pointer && !is_pointer)
    {
      if (context)
	gfc_error ("Non-POINTER in pointer association context (%s)"
		   " at %L", context, &e->where);
      return FAILURE;
    }

4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647
  /* F2008, C1303.  */
  if (!alloc_obj
      && (attr.lock_comp
	  || (e->ts.type == BT_DERIVED
	      && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
	      && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
    {
      if (context)
	gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
		   context, &e->where);
      return FAILURE;
    }

4648 4649 4650 4651
  /* INTENT(IN) dummy argument.  Check this, unless the object itself is
     the component of sub-component of a pointer.  Obviously,
     procedure pointers are of no interest here.  */
  check_intentin = true;
4652 4653
  ptr_component = (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
		  ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670
  for (ref = e->ref; ref && check_intentin; ref = ref->next)
    {
      if (ptr_component && ref->type == REF_COMPONENT)
	check_intentin = false;
      if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
	ptr_component = true;
    }
  if (check_intentin && sym->attr.intent == INTENT_IN)
    {
      if (pointer && is_pointer)
	{
	  if (context)
	    gfc_error ("Dummy argument '%s' with INTENT(IN) in pointer"
		       " association context (%s) at %L",
		       sym->name, context, &e->where);
	  return FAILURE;
	}
4671
      if (!pointer && !is_pointer && !sym->attr.pointer)
4672 4673 4674 4675 4676 4677 4678 4679 4680 4681
	{
	  if (context)
	    gfc_error ("Dummy argument '%s' with INTENT(IN) in variable"
		       " definition context (%s) at %L",
		       sym->name, context, &e->where);
	  return FAILURE;
	}
    }

  /* PROTECTED and use-associated.  */
4682
  if (sym->attr.is_protected && sym->attr.use_assoc  && check_intentin)
4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712
    {
      if (pointer && is_pointer)
	{
	  if (context)
	    gfc_error ("Variable '%s' is PROTECTED and can not appear in a"
		       " pointer association context (%s) at %L",
		       sym->name, context, &e->where);
	  return FAILURE;
	}
      if (!pointer && !is_pointer)
	{
	  if (context)
	    gfc_error ("Variable '%s' is PROTECTED and can not appear in a"
		       " variable definition context (%s) at %L",
		       sym->name, context, &e->where);
	  return FAILURE;
	}
    }

  /* Variable not assignable from a PURE procedure but appears in
     variable definition context.  */
  if (!pointer && gfc_pure (NULL) && gfc_impure_variable (sym))
    {
      if (context)
	gfc_error ("Variable '%s' can not appear in a variable definition"
		   " context (%s) at %L in PURE procedure",
		   sym->name, context, &e->where);
      return FAILURE;
    }

4713 4714 4715 4716 4717
  if (!pointer && context && gfc_implicit_pure (NULL)
      && gfc_impure_variable (sym))
    {
      gfc_namespace *ns;
      gfc_symbol *sym;
4718

4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
      for (ns = gfc_current_ns; ns; ns = ns->parent)
	{
	  sym = ns->proc_name;
	  if (sym == NULL)
	    break;
	  if (sym->attr.flavor == FL_PROCEDURE)
	    {
	      sym->attr.implicit_pure = 0;
	      break;
	    }
	}
    }
4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777
  /* Check variable definition context for associate-names.  */
  if (!pointer && sym->assoc)
    {
      const char* name;
      gfc_association_list* assoc;

      gcc_assert (sym->assoc->target);

      /* If this is a SELECT TYPE temporary (the association is used internally
	 for SELECT TYPE), silently go over to the target.  */
      if (sym->attr.select_type_temporary)
	{
	  gfc_expr* t = sym->assoc->target;

	  gcc_assert (t->expr_type == EXPR_VARIABLE);
	  name = t->symtree->name;

	  if (t->symtree->n.sym->assoc)
	    assoc = t->symtree->n.sym->assoc;
	  else
	    assoc = sym->assoc;
	}
      else
	{
	  name = sym->name;
	  assoc = sym->assoc;
	}
      gcc_assert (name && assoc);

      /* Is association to a valid variable?  */
      if (!assoc->variable)
	{
	  if (context)
	    {
	      if (assoc->target->expr_type == EXPR_VARIABLE)
		gfc_error ("'%s' at %L associated to vector-indexed target can"
			   " not be used in a variable definition context (%s)",
			   name, &e->where, context);
	      else
		gfc_error ("'%s' at %L associated to expression can"
			   " not be used in a variable definition context (%s)",
			   name, &e->where, context);
	    }
	  return FAILURE;
	}

      /* Target must be allowed to appear in a variable definition context.  */
4778 4779
      if (gfc_check_vardef_context (assoc->target, pointer, false, NULL)
	  == FAILURE)
4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792
	{
	  if (context)
	    gfc_error ("Associate-name '%s' can not appear in a variable"
		       " definition context (%s) at %L because its target"
		       " at %L can not, either",
		       name, context, &e->where,
		       &assoc->target->where);
	  return FAILURE;
	}
    }

  return SUCCESS;
}