array.c 54.3 KB
Newer Older
1
/* Array things
2
   Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 4
   Contributed by Andy Vaught

5
This file is part of GCC.
6

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

12 13 14 15
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.
16 17

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

#include "config.h"
22
#include "system.h"
23
#include "coretypes.h"
24 25
#include "gfortran.h"
#include "match.h"
Jerry DeLisle committed
26
#include "constructor.h"
27 28 29 30 31 32

/**************** Array reference matching subroutines *****************/

/* Copy an array reference structure.  */

gfc_array_ref *
Steven G. Kargl committed
33
gfc_copy_array_ref (gfc_array_ref *src)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
  gfc_array_ref *dest;
  int i;

  if (src == NULL)
    return NULL;

  dest = gfc_get_array_ref ();

  *dest = *src;

  for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
    {
      dest->start[i] = gfc_copy_expr (src->start[i]);
      dest->end[i] = gfc_copy_expr (src->end[i]);
      dest->stride[i] = gfc_copy_expr (src->stride[i]);
    }

  return dest;
}


/* Match a single dimension of an array reference.  This can be a
   single element or an array section.  Any modifications we've made
   to the ar structure are cleaned up by the caller.  If the init
   is set, we require the subscript to be a valid initialization
   expression.  */

static match
63
match_subscript (gfc_array_ref *ar, int init, bool match_star)
64
{
65
  match m = MATCH_ERROR;
66
  bool star = false;
67 68
  int i;

69
  i = ar->dimen + ar->codimen;
70

71
  gfc_gobble_whitespace ();
72
  ar->c_where[i] = gfc_current_locus;
73 74 75 76 77 78 79 80 81 82 83 84
  ar->start[i] = ar->end[i] = ar->stride[i] = NULL;

  /* We can't be sure of the difference between DIMEN_ELEMENT and
     DIMEN_VECTOR until we know the type of the element itself at
     resolution time.  */

  ar->dimen_type[i] = DIMEN_UNKNOWN;

  if (gfc_match_char (':') == MATCH_YES)
    goto end_element;

  /* Get start element.  */
85 86 87 88
  if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
    star = true;

  if (!star && init)
89
    m = gfc_match_init_expr (&ar->start[i]);
90
  else if (!star)
91 92
    m = gfc_match_expr (&ar->start[i]);

93
  if (m == MATCH_NO)
94 95 96 97 98
    gfc_error ("Expected array subscript at %C");
  if (m != MATCH_YES)
    return MATCH_ERROR;

  if (gfc_match_char (':') == MATCH_NO)
99 100 101 102 103 104 105
    goto matched;

  if (star)
    {
      gfc_error ("Unexpected '*' in coarray subscript at %C");
      return MATCH_ERROR;
    }
106 107 108 109 110 111

  /* Get an optional end element.  Because we've seen the colon, we
     definitely have a range along this dimension.  */
end_element:
  ar->dimen_type[i] = DIMEN_RANGE;

112 113 114
  if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
    star = true;
  else if (init)
115 116 117 118 119 120 121 122 123 124
    m = gfc_match_init_expr (&ar->end[i]);
  else
    m = gfc_match_expr (&ar->end[i]);

  if (m == MATCH_ERROR)
    return MATCH_ERROR;

  /* See if we have an optional stride.  */
  if (gfc_match_char (':') == MATCH_YES)
    {
125 126 127 128 129 130
      if (star)
	{
	  gfc_error ("Strides not allowed in coarray subscript at %C");
	  return MATCH_ERROR;
	}

131
      m = init ? gfc_match_init_expr (&ar->stride[i])
Steven G. Kargl committed
132
	       : gfc_match_expr (&ar->stride[i]);
133 134 135 136 137 138 139

      if (m == MATCH_NO)
	gfc_error ("Expected array subscript stride at %C");
      if (m != MATCH_YES)
	return MATCH_ERROR;
    }

140 141 142 143
matched:
  if (star)
    ar->dimen_type[i] = DIMEN_STAR;

144 145 146 147 148 149 150 151 152
  return MATCH_YES;
}


/* Match an array reference, whether it is the whole array or a
   particular elements or a section. If init is set, the reference has
   to consist of init expressions.  */

match
153 154
gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
		     int corank)
155 156
{
  match m;
157
  bool matched_bracket = false;
158

159
  memset (ar, '\0', sizeof (*ar));
160

161
  ar->where = gfc_current_locus;
162
  ar->as = as;
163 164 165 166 167 168 169
  ar->type = AR_UNKNOWN;

  if (gfc_match_char ('[') == MATCH_YES)
    {
       matched_bracket = true;
       goto coarray;
    }
170 171 172 173 174 175 176 177 178 179

  if (gfc_match_char ('(') != MATCH_YES)
    {
      ar->type = AR_FULL;
      ar->dimen = 0;
      return MATCH_YES;
    }

  for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
    {
180
      m = match_subscript (ar, init, false);
181
      if (m == MATCH_ERROR)
182
	return MATCH_ERROR;
183 184

      if (gfc_match_char (')') == MATCH_YES)
185 186 187 188
	{
	  ar->dimen++;
	  goto coarray;
	}
189 190 191 192

      if (gfc_match_char (',') != MATCH_YES)
	{
	  gfc_error ("Invalid form of array reference at %C");
193
	  return MATCH_ERROR;
194 195 196
	}
    }

197 198
  gfc_error ("Array reference at %C cannot have more than %d dimensions",
	     GFC_MAX_DIMENSIONS);
199 200
  return MATCH_ERROR;

201 202 203 204 205 206 207 208 209 210 211
coarray:
  if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
    {
      if (ar->dimen > 0)
	return MATCH_YES;
      else
	return MATCH_ERROR;
    }

  if (gfc_option.coarray == GFC_FCOARRAY_NONE)
    {
212
      gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
213 214 215 216 217 218 219 220 221 222 223
      return MATCH_ERROR;
    }

  if (corank == 0)
    {
	gfc_error ("Unexpected coarray designator at %C");
	return MATCH_ERROR;
    }

  for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
    {
224
      m = match_subscript (ar, init, true);
225 226 227 228 229 230
      if (m == MATCH_ERROR)
	return MATCH_ERROR;

      if (gfc_match_char (']') == MATCH_YES)
	{
	  ar->codimen++;
231 232 233 234 235 236
	  if (ar->codimen < corank)
	    {
	      gfc_error ("Too few codimensions at %C, expected %d not %d",
			 corank, ar->codimen);
	      return MATCH_ERROR;
	    }
237 238 239 240 241 242
	  if (ar->codimen > corank)
	    {
	      gfc_error ("Too many codimensions at %C, expected %d not %d",
			 corank, ar->codimen);
	      return MATCH_ERROR;
	    }
243 244 245 246 247
	  return MATCH_YES;
	}

      if (gfc_match_char (',') != MATCH_YES)
	{
248 249 250 251 252 253 254
	  if (gfc_match_char ('*') == MATCH_YES)
	    gfc_error ("Unexpected '*' for codimension %d of %d at %C",
		       ar->codimen + 1, corank);
	  else
	    gfc_error ("Invalid form of coarray reference at %C");
	  return MATCH_ERROR;
	}
255 256 257 258 259 260 261
      else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
	{
	  gfc_error ("Unexpected '*' for codimension %d of %d at %C",
		     ar->codimen + 1, corank);
	  return MATCH_ERROR;
	}

262 263 264 265
      if (ar->codimen >= corank)
	{
	  gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
		     ar->codimen + 1, corank);
266 267 268 269 270 271 272
	  return MATCH_ERROR;
	}
    }

  gfc_error ("Array reference at %C cannot have more than %d dimensions",
	     GFC_MAX_DIMENSIONS);
  return MATCH_ERROR;
273 274 275 276 277 278 279 280 281 282

}


/************** Array specification matching subroutines ***************/

/* Free all of the expressions associated with array bounds
   specifications.  */

void
Steven G. Kargl committed
283
gfc_free_array_spec (gfc_array_spec *as)
284 285 286 287 288 289
{
  int i;

  if (as == NULL)
    return;

290
  for (i = 0; i < as->rank + as->corank; i++)
291 292 293 294 295
    {
      gfc_free_expr (as->lower[i]);
      gfc_free_expr (as->upper[i]);
    }

296
  free (as);
297 298 299 300 301 302
}


/* Take an array bound, resolves the expression, that make up the
   shape and check associated constraints.  */

303
static bool
Steven G. Kargl committed
304
resolve_array_bound (gfc_expr *e, int check_constant)
305 306
{
  if (e == NULL)
307
    return true;
308

309 310 311
  if (!gfc_resolve_expr (e)
      || !gfc_specification_expr (e))
    return false;
312

313
  if (check_constant && !gfc_is_constant_expr (e))
314
    {
315 316 317 318 319 320
      if (e->expr_type == EXPR_VARIABLE)
	gfc_error ("Variable '%s' at %L in this context must be constant",
		   e->symtree->n.sym->name, &e->where);
      else
	gfc_error ("Expression at %L in this context must be constant",
		   &e->where);
321
      return false;
322 323
    }

324
  return true;
325 326 327 328 329 330
}


/* Takes an array specification, resolves the expressions that make up
   the shape and make sure everything is integral.  */

331
bool
Steven G. Kargl committed
332
gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
333 334 335 336 337
{
  gfc_expr *e;
  int i;

  if (as == NULL)
338
    return true;
339

340
  for (i = 0; i < as->rank + as->corank; i++)
341 342
    {
      e = as->lower[i];
343 344
      if (!resolve_array_bound (e, check_constant))
	return false;
345 346

      e = as->upper[i];
347 348
      if (!resolve_array_bound (e, check_constant))
	return false;
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

      if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
	continue;

      /* If the size is negative in this dimension, set it to zero.  */
      if (as->lower[i]->expr_type == EXPR_CONSTANT
	    && as->upper[i]->expr_type == EXPR_CONSTANT
	    && mpz_cmp (as->upper[i]->value.integer,
			as->lower[i]->value.integer) < 0)
	{
	  gfc_free_expr (as->upper[i]);
	  as->upper[i] = gfc_copy_expr (as->lower[i]);
	  mpz_sub_ui (as->upper[i]->value.integer,
		      as->upper[i]->value.integer, 1);
	}
364 365
    }

366
  return true;
367 368 369 370 371 372 373 374 375
}


/* Match a single array element specification.  The return values as
   well as the upper and lower bounds of the array spec are filled
   in according to what we see on the input.  The caller makes sure
   individual specifications make sense as a whole.


Steven G. Kargl committed
376 377
	Parsed       Lower   Upper  Returned
	------------------------------------
378 379 380 381 382 383
	  :           NULL    NULL   AS_DEFERRED (*)
	  x            1       x     AS_EXPLICIT
	  x:           x      NULL   AS_ASSUMED_SHAPE
	  x:y          x       y     AS_EXPLICIT
	  x:*          x      NULL   AS_ASSUMED_SIZE
	  *            1      NULL   AS_ASSUMED_SIZE
384 385 386 387 388 389 390

  (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE.  This
  is fixed during the resolution of formal interfaces.

   Anything else AS_UNKNOWN.  */

static array_type
Steven G. Kargl committed
391
match_array_element_spec (gfc_array_spec *as)
392 393 394
{
  gfc_expr **upper, **lower;
  match m;
395
  int rank;
396

397 398 399
  rank = as->rank == -1 ? 0 : as->rank;
  lower = &as->lower[rank + as->corank - 1];
  upper = &as->upper[rank + as->corank - 1];
400 401 402

  if (gfc_match_char ('*') == MATCH_YES)
    {
Jerry DeLisle committed
403
      *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
404 405 406 407 408 409 410 411 412 413 414
      return AS_ASSUMED_SIZE;
    }

  if (gfc_match_char (':') == MATCH_YES)
    return AS_DEFERRED;

  m = gfc_match_expr (upper);
  if (m == MATCH_NO)
    gfc_error ("Expected expression in array specification at %C");
  if (m != MATCH_YES)
    return AS_UNKNOWN;
415
  if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
416
    return AS_UNKNOWN;
417 418 419

  if (gfc_match_char (':') == MATCH_NO)
    {
Jerry DeLisle committed
420
      *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
421 422 423 424 425 426 427 428 429 430 431 432 433 434
      return AS_EXPLICIT;
    }

  *lower = *upper;
  *upper = NULL;

  if (gfc_match_char ('*') == MATCH_YES)
    return AS_ASSUMED_SIZE;

  m = gfc_match_expr (upper);
  if (m == MATCH_ERROR)
    return AS_UNKNOWN;
  if (m == MATCH_NO)
    return AS_ASSUMED_SHAPE;
435
  if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
436
    return AS_UNKNOWN;
437 438 439 440 441 442

  return AS_EXPLICIT;
}


/* Matches an array specification, incidentally figuring out what sort
443 444
   it is. Match either a normal array specification, or a coarray spec
   or both. Optionally allow [:] for coarrays.  */
445 446

match
447
gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim)
448 449 450 451 452
{
  array_type current_type;
  gfc_array_spec *as;
  int i;

453
  as = gfc_get_array_spec ();
454

455 456 457 458 459 460 461 462 463
  if (!match_dim)
    goto coarray;

  if (gfc_match_char ('(') != MATCH_YES)
    {
      if (!match_codim)
	goto done;
      goto coarray;
    }
464

465 466 467 468 469
  if (gfc_match (" .. )") == MATCH_YES)
    {
      as->type = AS_ASSUMED_RANK;
      as->rank = -1;

470
      if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
471 472 473 474 475 476 477
	goto cleanup;

      if (!match_codim)
	goto done;
      goto coarray;
    }

478 479
  for (;;)
    {
480
      as->rank++;
481 482
      current_type = match_array_element_spec (as);

483 484 485 486 487 488
      /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
	 and implied-shape specifications.  If the rank is at least 2, we can
	 distinguish between them.  But for rank 1, we currently return
	 ASSUMED_SIZE; this gets adjusted later when we know for sure
	 whether the symbol parsed is a PARAMETER or not.  */

489 490 491 492 493 494 495 496
      if (as->rank == 1)
	{
	  if (current_type == AS_UNKNOWN)
	    goto cleanup;
	  as->type = current_type;
	}
      else
	switch (as->type)
Steven G. Kargl committed
497
	  {		/* See how current spec meshes with the existing.  */
498 499 500
	  case AS_UNKNOWN:
	    goto cleanup;

501 502 503 504 505 506 507 508 509
	  case AS_IMPLIED_SHAPE:
	    if (current_type != AS_ASSUMED_SHAPE)
	      {
		gfc_error ("Bad array specification for implied-shape"
			   " array at %C");
		goto cleanup;
	      }
	    break;

510 511 512 513 514 515 516 517 518 519
	  case AS_EXPLICIT:
	    if (current_type == AS_ASSUMED_SIZE)
	      {
		as->type = AS_ASSUMED_SIZE;
		break;
	      }

	    if (current_type == AS_EXPLICIT)
	      break;

Steven G. Kargl committed
520 521
	    gfc_error ("Bad array specification for an explicitly shaped "
		       "array at %C");
522 523 524 525 526 527 528 529

	    goto cleanup;

	  case AS_ASSUMED_SHAPE:
	    if ((current_type == AS_ASSUMED_SHAPE)
		|| (current_type == AS_DEFERRED))
	      break;

Steven G. Kargl committed
530 531
	    gfc_error ("Bad array specification for assumed shape "
		       "array at %C");
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	    goto cleanup;

	  case AS_DEFERRED:
	    if (current_type == AS_DEFERRED)
	      break;

	    if (current_type == AS_ASSUMED_SHAPE)
	      {
		as->type = AS_ASSUMED_SHAPE;
		break;
	      }

	    gfc_error ("Bad specification for deferred shape array at %C");
	    goto cleanup;

	  case AS_ASSUMED_SIZE:
548 549 550 551 552 553
	    if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
	      {
		as->type = AS_IMPLIED_SHAPE;
		break;
	      }

554 555
	    gfc_error ("Bad specification for assumed size array at %C");
	    goto cleanup;
556 557

	  case AS_ASSUMED_RANK:
558
	    gcc_unreachable ();
559 560 561 562 563 564 565 566 567 568 569
	  }

      if (gfc_match_char (')') == MATCH_YES)
	break;

      if (gfc_match_char (',') != MATCH_YES)
	{
	  gfc_error ("Expected another dimension in array declaration at %C");
	  goto cleanup;
	}

570
      if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
571
	{
572 573
	  gfc_error ("Array specification at %C has more than %d dimensions",
		     GFC_MAX_DIMENSIONS);
574 575 576
	  goto cleanup;
	}

577
      if (as->corank + as->rank >= 7
578 579
	  && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
			      "with more than 7 dimensions"))
580
	goto cleanup;
581
    }
582

583 584 585 586 587 588 589
  if (!match_codim)
    goto done;

coarray:
  if (gfc_match_char ('[')  != MATCH_YES)
    goto done;

590
  if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
591 592
    goto cleanup;

593 594
  if (gfc_option.coarray == GFC_FCOARRAY_NONE)
    {
595
      gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
596
      goto cleanup;
597 598
    }

599 600 601 602 603 604 605
  if (as->rank >= GFC_MAX_DIMENSIONS)
    {
      gfc_error ("Array specification at %C has more than %d "
		 "dimensions", GFC_MAX_DIMENSIONS);
      goto cleanup;
    }

606 607 608 609 610 611 612 613 614
  for (;;)
    {
      as->corank++;
      current_type = match_array_element_spec (as);

      if (current_type == AS_UNKNOWN)
	goto cleanup;

      if (as->corank == 1)
615
	as->cotype = current_type;
616
      else
617
	switch (as->cotype)
618
	  { /* See how current spec meshes with the existing.  */
619
	    case AS_IMPLIED_SHAPE:
620 621 622 623 624 625
	    case AS_UNKNOWN:
	      goto cleanup;

	    case AS_EXPLICIT:
	      if (current_type == AS_ASSUMED_SIZE)
		{
626
		  as->cotype = AS_ASSUMED_SIZE;
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		  break;
		}

	      if (current_type == AS_EXPLICIT)
		break;

	      gfc_error ("Bad array specification for an explicitly "
			 "shaped array at %C");

	      goto cleanup;

	    case AS_ASSUMED_SHAPE:
	      if ((current_type == AS_ASSUMED_SHAPE)
		  || (current_type == AS_DEFERRED))
		break;

	      gfc_error ("Bad array specification for assumed shape "
			 "array at %C");
	      goto cleanup;

	    case AS_DEFERRED:
	      if (current_type == AS_DEFERRED)
		break;

	      if (current_type == AS_ASSUMED_SHAPE)
		{
653
		  as->cotype = AS_ASSUMED_SHAPE;
654 655 656 657 658 659 660 661 662
		  break;
		}

	      gfc_error ("Bad specification for deferred shape array at %C");
	      goto cleanup;

	    case AS_ASSUMED_SIZE:
	      gfc_error ("Bad specification for assumed size array at %C");
	      goto cleanup;
663 664

	    case AS_ASSUMED_RANK:
665
	      gcc_unreachable ();
666 667 668 669 670 671 672 673 674 675 676
	  }

      if (gfc_match_char (']') == MATCH_YES)
	break;

      if (gfc_match_char (',') != MATCH_YES)
	{
	  gfc_error ("Expected another dimension in array declaration at %C");
	  goto cleanup;
	}

677
      if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
678 679 680 681 682 683 684 685 686 687 688 689 690
	{
	  gfc_error ("Array specification at %C has more than %d "
		     "dimensions", GFC_MAX_DIMENSIONS);
	  goto cleanup;
	}
    }

  if (current_type == AS_EXPLICIT)
    {
      gfc_error ("Upper bound of last coarray dimension must be '*' at %C");
      goto cleanup;
    }

691 692 693 694 695
  if (as->cotype == AS_ASSUMED_SIZE)
    as->cotype = AS_EXPLICIT;

  if (as->rank == 0)
    as->type = as->cotype;
696 697 698 699 700 701 702

done:
  if (as->rank == 0 && as->corank == 0)
    {
      *asp = NULL;
      gfc_free_array_spec (as);
      return MATCH_NO;
703 704 705 706 707
    }

  /* If a lower bounds of an assumed shape array is blank, put in one.  */
  if (as->type == AS_ASSUMED_SHAPE)
    {
708
      for (i = 0; i < as->rank + as->corank; i++)
709 710
	{
	  if (as->lower[i] == NULL)
Jerry DeLisle committed
711
	    as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
712 713
	}
    }
714

715
  *asp = as;
716

717 718 719 720 721 722 723 724 725 726 727 728 729
  return MATCH_YES;

cleanup:
  /* Something went wrong.  */
  gfc_free_array_spec (as);
  return MATCH_ERROR;
}


/* Given a symbol and an array specification, modify the symbol to
   have that array specification.  The error locus is needed in case
   something goes wrong.  On failure, the caller must free the spec.  */

730
bool
Steven G. Kargl committed
731
gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
732
{
733 734
  int i;

735
  if (as == NULL)
736
    return true;
737

738
  if (as->rank
739 740
      && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
    return false;
741 742

  if (as->corank
743 744
      && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
    return false;
745

746 747 748
  if (sym->as == NULL)
    {
      sym->as = as;
749
      return true;
750
    }
751

752 753 754 755 756
  if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
      || (as->type == AS_ASSUMED_RANK && sym->as->corank))
    {
      gfc_error ("The assumed-rank array '%s' at %L shall not have a "
		 "codimension", sym->name, error_loc);
757
      return false;
758 759
    }

760 761 762 763 764 765
  if (as->corank)
    {
      /* The "sym" has no corank (checked via gfc_add_codimension). Thus
	 the codimension is simply added.  */
      gcc_assert (as->rank == 0 && sym->as->corank == 0);

766
      sym->as->cotype = as->cotype;
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
      sym->as->corank = as->corank;
      for (i = 0; i < as->corank; i++)
	{
	  sym->as->lower[sym->as->rank + i] = as->lower[i];
	  sym->as->upper[sym->as->rank + i] = as->upper[i];
	}
    }
  else
    {
      /* The "sym" has no rank (checked via gfc_add_dimension). Thus
	 the dimension is added - but first the codimensions (if existing
	 need to be shifted to make space for the dimension.  */
      gcc_assert (as->corank == 0 && sym->as->rank == 0);

      sym->as->rank = as->rank;
      sym->as->type = as->type;
      sym->as->cray_pointee = as->cray_pointee;
      sym->as->cp_was_assumed = as->cp_was_assumed;

      for (i = 0; i < sym->as->corank; i++)
	{
	  sym->as->lower[as->rank + i] = sym->as->lower[i];
	  sym->as->upper[as->rank + i] = sym->as->upper[i];
	}
      for (i = 0; i < as->rank; i++)
	{
	  sym->as->lower[i] = as->lower[i];
	  sym->as->upper[i] = as->upper[i];
	}
    }

798
  free (as);
799
  return true;
800 801 802 803 804 805
}


/* Copy an array specification.  */

gfc_array_spec *
Steven G. Kargl committed
806
gfc_copy_array_spec (gfc_array_spec *src)
807 808 809 810 811 812 813 814 815 816 817
{
  gfc_array_spec *dest;
  int i;

  if (src == NULL)
    return NULL;

  dest = gfc_get_array_spec ();

  *dest = *src;

818
  for (i = 0; i < dest->rank + dest->corank; i++)
819 820 821 822 823 824 825 826
    {
      dest->lower[i] = gfc_copy_expr (dest->lower[i]);
      dest->upper[i] = gfc_copy_expr (dest->upper[i]);
    }

  return dest;
}

Steven G. Kargl committed
827

828 829 830 831
/* Returns nonzero if the two expressions are equal.  Only handles integer
   constants.  */

static int
Steven G. Kargl committed
832
compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
833 834 835 836 837 838 839 840 841 842 843 844 845 846
{
  if (bound1 == NULL || bound2 == NULL
      || bound1->expr_type != EXPR_CONSTANT
      || bound2->expr_type != EXPR_CONSTANT
      || bound1->ts.type != BT_INTEGER
      || bound2->ts.type != BT_INTEGER)
    gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");

  if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
    return 1;
  else
    return 0;
}

Steven G. Kargl committed
847

848 849 850 851
/* Compares two array specifications.  They must be constant or deferred
   shape.  */

int
Steven G. Kargl committed
852
gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
853 854 855 856 857 858 859 860 861 862 863 864
{
  int i;

  if (as1 == NULL && as2 == NULL)
    return 1;

  if (as1 == NULL || as2 == NULL)
    return 0;

  if (as1->rank != as2->rank)
    return 0;

865 866 867
  if (as1->corank != as2->corank)
    return 0;

868 869 870 871 872 873 874
  if (as1->rank == 0)
    return 1;

  if (as1->type != as2->type)
    return 0;

  if (as1->type == AS_EXPLICIT)
875
    for (i = 0; i < as1->rank + as1->corank; i++)
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
      {
	if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
	  return 0;

	if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
	  return 0;
      }

  return 1;
}


/****************** Array constructor functions ******************/


/* Given an expression node that might be an array constructor and a
   symbol, make sure that no iterators in this or child constructors
   use the symbol as an implied-DO iterator.  Returns nonzero if a
   duplicate was found.  */

static int
Jerry DeLisle committed
897
check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
898
{
Jerry DeLisle committed
899
  gfc_constructor *c;
900 901
  gfc_expr *e;

Jerry DeLisle committed
902
  for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
903 904 905 906 907 908 909 910 911 912 913 914
    {
      e = c->expr;

      if (e->expr_type == EXPR_ARRAY
	  && check_duplicate_iterator (e->value.constructor, master))
	return 1;

      if (c->iterator == NULL)
	continue;

      if (c->iterator->var->symtree->n.sym == master)
	{
Steven G. Kargl committed
915 916
	  gfc_error ("DO-iterator '%s' at %L is inside iterator of the "
		     "same name", master->name, &c->where);
917 918 919 920 921 922 923 924 925 926

	  return 1;
	}
    }

  return 0;
}


/* Forward declaration because these functions are mutually recursive.  */
Jerry DeLisle committed
927
static match match_array_cons_element (gfc_constructor_base *);
928 929 930 931

/* Match a list of array elements.  */

static match
Jerry DeLisle committed
932
match_array_list (gfc_constructor_base *result)
933
{
Jerry DeLisle committed
934 935
  gfc_constructor_base head;
  gfc_constructor *p;
936 937 938 939 940 941
  gfc_iterator iter;
  locus old_loc;
  gfc_expr *e;
  match m;
  int n;

942
  old_loc = gfc_current_locus;
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

  if (gfc_match_char ('(') == MATCH_NO)
    return MATCH_NO;

  memset (&iter, '\0', sizeof (gfc_iterator));
  head = NULL;

  m = match_array_cons_element (&head);
  if (m != MATCH_YES)
    goto cleanup;

  if (gfc_match_char (',') != MATCH_YES)
    {
      m = MATCH_NO;
      goto cleanup;
    }

  for (n = 1;; n++)
    {
962
      m = gfc_match_iterator (&iter, 0);
963 964 965 966 967
      if (m == MATCH_YES)
	break;
      if (m == MATCH_ERROR)
	goto cleanup;

Jerry DeLisle committed
968
      m = match_array_cons_element (&head);
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
      if (m == MATCH_ERROR)
	goto cleanup;
      if (m == MATCH_NO)
	{
	  if (n > 2)
	    goto syntax;
	  m = MATCH_NO;
	  goto cleanup;		/* Could be a complex constant */
	}

      if (gfc_match_char (',') != MATCH_YES)
	{
	  if (n > 2)
	    goto syntax;
	  m = MATCH_NO;
	  goto cleanup;
	}
    }

  if (gfc_match_char (')') != MATCH_YES)
    goto syntax;

  if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
    {
      m = MATCH_ERROR;
      goto cleanup;
    }

Jerry DeLisle committed
997
  e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
998 999
  e->value.constructor = head;

Jerry DeLisle committed
1000
  p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  p->iterator = gfc_get_iterator ();
  *p->iterator = iter;

  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in array constructor at %C");
  m = MATCH_ERROR;

cleanup:
Jerry DeLisle committed
1011
  gfc_constructor_free (head);
1012
  gfc_free_iterator (&iter, 0);
1013
  gfc_current_locus = old_loc;
1014 1015 1016 1017 1018 1019 1020 1021
  return m;
}


/* Match a single element of an array constructor, which can be a
   single expression or a list of elements.  */

static match
Jerry DeLisle committed
1022
match_array_cons_element (gfc_constructor_base *result)
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
{
  gfc_expr *expr;
  match m;

  m = match_array_list (result);
  if (m != MATCH_NO)
    return m;

  m = gfc_match_expr (&expr);
  if (m != MATCH_YES)
    return m;

Jerry DeLisle committed
1035
  gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1036 1037 1038 1039 1040 1041 1042
  return MATCH_YES;
}


/* Match an array constructor.  */

match
Steven G. Kargl committed
1043
gfc_match_array_constructor (gfc_expr **result)
1044
{
Jerry DeLisle committed
1045
  gfc_constructor_base head, new_cons;
1046
  gfc_undo_change_set changed_syms;
1047
  gfc_expr *expr;
1048
  gfc_typespec ts;
1049 1050
  locus where;
  match m;
1051
  const char *end_delim;
1052
  bool seen_ts;
1053 1054

  if (gfc_match (" (/") == MATCH_NO)
1055 1056
    {
      if (gfc_match (" [") == MATCH_NO)
Steven G. Kargl committed
1057
	return MATCH_NO;
1058
      else
Steven G. Kargl committed
1059
	{
1060 1061
	  if (!gfc_notify_std (GFC_STD_F2003, "[...] "
			       "style array constructors at %C"))
Steven G. Kargl committed
1062 1063 1064
	    return MATCH_ERROR;
	  end_delim = " ]";
	}
1065 1066 1067
    }
  else
    end_delim = " /)";
1068

1069
  where = gfc_current_locus;
Jerry DeLisle committed
1070
  head = new_cons = NULL;
1071 1072 1073
  seen_ts = false;

  /* Try to match an optional "type-spec ::"  */
1074
  gfc_clear_ts (&ts);
1075
  gfc_new_undo_checkpoint (changed_syms);
1076
  if (gfc_match_decl_type_spec (&ts, 0) == MATCH_YES)
1077 1078 1079 1080 1081
    {
      seen_ts = (gfc_match (" ::") == MATCH_YES);

      if (seen_ts)
	{
1082 1083
	  if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
			       "including type specification at %C"))
1084 1085 1086 1087
	    {
	      gfc_restore_last_undo_checkpoint ();
	      goto cleanup;
	    }
Steven G. Kargl committed
1088 1089 1090 1091 1092

	  if (ts.deferred)
	    {
	      gfc_error ("Type-spec at %L cannot contain a deferred "
			 "type parameter", &where);
1093
	      gfc_restore_last_undo_checkpoint ();
Steven G. Kargl committed
1094 1095
	      goto cleanup;
	    }
1096 1097 1098
	}
    }

1099 1100 1101 1102 1103 1104 1105
  if (seen_ts)
    gfc_drop_last_undo_checkpoint ();
  else
    {
      gfc_restore_last_undo_checkpoint ();
      gfc_current_locus = where;
    }
1106

1107
  if (gfc_match (end_delim) == MATCH_YES)
1108
    {
1109 1110 1111 1112 1113 1114 1115
      if (seen_ts)
	goto done;
      else
	{
	  gfc_error ("Empty array constructor at %C is not allowed");
	  goto cleanup;
	}
1116
    }
1117 1118 1119

  for (;;)
    {
Jerry DeLisle committed
1120
      m = match_array_cons_element (&head);
1121 1122 1123 1124 1125 1126 1127 1128 1129
      if (m == MATCH_ERROR)
	goto cleanup;
      if (m == MATCH_NO)
	goto syntax;

      if (gfc_match_char (',') == MATCH_NO)
	break;
    }

1130
  if (gfc_match (end_delim) == MATCH_NO)
1131 1132
    goto syntax;

1133
done:
1134
  /* Size must be calculated at resolution time.  */
1135
  if (seen_ts)
Jerry DeLisle committed
1136 1137 1138 1139
    {
      expr = gfc_get_array_expr (ts.type, ts.kind, &where);
      expr->ts = ts;
    }
1140
  else
Jerry DeLisle committed
1141 1142 1143
    expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);

  expr->value.constructor = head;
1144 1145
  if (expr->ts.u.cl)
    expr->ts.u.cl->length_from_typespec = seen_ts;
1146

1147 1148 1149 1150 1151 1152 1153
  *result = expr;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in array constructor at %C");

cleanup:
Jerry DeLisle committed
1154
  gfc_constructor_free (head);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
  return MATCH_ERROR;
}



/************** Check array constructors for correctness **************/

/* Given an expression, compare it's type with the type of the current
   constructor.  Returns nonzero if an error was issued.  The
   cons_state variable keeps track of whether the type of the
   constructor being read or resolved is known to be good, bad or just
   starting out.  */

static gfc_typespec constructor_ts;
static enum
{ CONS_START, CONS_GOOD, CONS_BAD }
cons_state;

static int
1174
check_element_type (gfc_expr *expr, bool convert)
1175 1176
{
  if (cons_state == CONS_BAD)
1177
    return 0;			/* Suppress further errors */
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

  if (cons_state == CONS_START)
    {
      if (expr->ts.type == BT_UNKNOWN)
	cons_state = CONS_BAD;
      else
	{
	  cons_state = CONS_GOOD;
	  constructor_ts = expr->ts;
	}

      return 0;
    }

  if (gfc_compare_types (&constructor_ts, &expr->ts))
    return 0;

1195
  if (convert)
1196
    return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1197

1198 1199 1200 1201 1202 1203 1204 1205 1206
  gfc_error ("Element in %s array constructor at %L is %s",
	     gfc_typename (&constructor_ts), &expr->where,
	     gfc_typename (&expr->ts));

  cons_state = CONS_BAD;
  return 1;
}


1207
/* Recursive work function for gfc_check_constructor_type().  */
1208

1209
static bool
Jerry DeLisle committed
1210
check_constructor_type (gfc_constructor_base base, bool convert)
1211
{
Jerry DeLisle committed
1212
  gfc_constructor *c;
1213 1214
  gfc_expr *e;

Jerry DeLisle committed
1215
  for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1216 1217 1218 1219 1220
    {
      e = c->expr;

      if (e->expr_type == EXPR_ARRAY)
	{
1221 1222
	  if (!check_constructor_type (e->value.constructor, convert))
	    return false;
1223 1224 1225 1226

	  continue;
	}

1227
      if (check_element_type (e, convert))
1228
	return false;
1229 1230
    }

1231
  return true;
1232 1233 1234 1235
}


/* Check that all elements of an array constructor are the same type.
1236
   On false, an error has been generated.  */
1237

1238
bool
Steven G. Kargl committed
1239
gfc_check_constructor_type (gfc_expr *e)
1240
{
1241
  bool t;
1242

1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
  if (e->ts.type != BT_UNKNOWN)
    {
      cons_state = CONS_GOOD;
      constructor_ts = e->ts;
    }
  else
    {
      cons_state = CONS_START;
      gfc_clear_ts (&constructor_ts);
    }
1253

1254 1255 1256
  /* If e->ts.type != BT_UNKNOWN, the array constructor included a
     typespec, and we will now convert the values on the fly.  */
  t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1257
  if (t && e->ts.type == BT_UNKNOWN)
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    e->ts = constructor_ts;

  return t;
}



typedef struct cons_stack
{
  gfc_iterator *iterator;
  struct cons_stack *previous;
}
cons_stack;

static cons_stack *base;

1274
static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1275 1276 1277 1278

/* Check an EXPR_VARIABLE expression in a constructor to make sure
   that that variable is an iteration variables.  */

1279
bool
Steven G. Kargl committed
1280
gfc_check_iter_variable (gfc_expr *expr)
1281 1282 1283 1284 1285 1286
{
  gfc_symbol *sym;
  cons_stack *c;

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

1287
  for (c = base; c && c->iterator; c = c->previous)
1288
    if (sym == c->iterator->var->symtree->n.sym)
1289
      return true;
1290

1291
  return false;
1292 1293 1294 1295 1296 1297 1298
}


/* Recursive work function for gfc_check_constructor().  This amounts
   to calling the check function for each expression in the
   constructor, giving variables with the names of iterators a pass.  */

1299 1300
static bool
check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1301 1302 1303
{
  cons_stack element;
  gfc_expr *e;
1304
  bool t;
Jerry DeLisle committed
1305
  gfc_constructor *c;
1306

Jerry DeLisle committed
1307
  for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1308 1309 1310 1311 1312
    {
      e = c->expr;

      if (e->expr_type != EXPR_ARRAY)
	{
1313 1314
	  if (!(*check_function)(e))
	    return false;
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
	  continue;
	}

      element.previous = base;
      element.iterator = c->iterator;

      base = &element;
      t = check_constructor (e->value.constructor, check_function);
      base = element.previous;

1325 1326
      if (!t)
	return false;
1327 1328 1329
    }

  /* Nothing went wrong, so all OK.  */
1330
  return true;
1331 1332 1333 1334 1335 1336 1337
}


/* Checks a constructor to see if it is a particular kind of
   expression -- specification, restricted, or initialization as
   determined by the check_function.  */

1338 1339
bool
gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1340 1341
{
  cons_stack *base_save;
1342
  bool t;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

  base_save = base;
  base = NULL;

  t = check_constructor (expr->value.constructor, check_function);
  base = base_save;

  return t;
}



/**************** Simplification of array constructors ****************/

iterator_stack *iter_stack;

typedef struct
{
Jerry DeLisle committed
1361
  gfc_constructor_base base;
1362 1363 1364 1365 1366 1367
  int extract_count, extract_n;
  gfc_expr *extracted;
  mpz_t *count;

  mpz_t *offset;
  gfc_component *component;
1368
  mpz_t *repeat;
1369

1370
  bool (*expand_work_function) (gfc_expr *);
1371 1372 1373 1374 1375
}
expand_info;

static expand_info current_expand;

1376
static bool expand_constructor (gfc_constructor_base);
1377 1378 1379 1380 1381


/* Work function that counts the number of elements present in a
   constructor.  */

1382
static bool
Steven G. Kargl committed
1383
count_elements (gfc_expr *e)
1384 1385 1386 1387 1388 1389 1390
{
  mpz_t result;

  if (e->rank == 0)
    mpz_add_ui (*current_expand.count, *current_expand.count, 1);
  else
    {
1391
      if (!gfc_array_size (e, &result))
1392 1393
	{
	  gfc_free_expr (e);
1394
	  return false;
1395 1396 1397 1398 1399 1400 1401
	}

      mpz_add (*current_expand.count, *current_expand.count, result);
      mpz_clear (result);
    }

  gfc_free_expr (e);
1402
  return true;
1403 1404 1405 1406 1407 1408
}


/* Work function that extracts a particular element from an array
   constructor, freeing the rest.  */

1409
static bool
Steven G. Kargl committed
1410
extract_element (gfc_expr *e)
1411 1412 1413 1414
{
  if (e->rank != 0)
    {				/* Something unextractable */
      gfc_free_expr (e);
1415
      return false;
1416 1417 1418 1419 1420 1421 1422 1423
    }

  if (current_expand.extract_count == current_expand.extract_n)
    current_expand.extracted = e;
  else
    gfc_free_expr (e);

  current_expand.extract_count++;
1424

1425
  return true;
1426 1427 1428 1429 1430 1431
}


/* Work function that constructs a new constructor out of the old one,
   stringing new elements together.  */

1432
static bool
Steven G. Kargl committed
1433
expand (gfc_expr *e)
1434
{
Jerry DeLisle committed
1435 1436
  gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
						    e, &e->where);
1437

Jerry DeLisle committed
1438
  c->n.component = current_expand.component;
1439
  return true;
1440 1441 1442 1443 1444 1445 1446
}


/* Given an initialization expression that is a variable reference,
   substitute the current value of the iteration variable.  */

void
Steven G. Kargl committed
1447
gfc_simplify_iterator_var (gfc_expr *e)
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
{
  iterator_stack *p;

  for (p = iter_stack; p; p = p->prev)
    if (e->symtree == p->variable)
      break;

  if (p == NULL)
    return;		/* Variable not found */

Jerry DeLisle committed
1458
  gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

  mpz_set (e->value.integer, p->value);

  return;
}


/* Expand an expression with that is inside of a constructor,
   recursing into other constructors if present.  */

1469
static bool
Steven G. Kargl committed
1470
expand_expr (gfc_expr *e)
1471 1472 1473 1474 1475 1476
{
  if (e->expr_type == EXPR_ARRAY)
    return expand_constructor (e->value.constructor);

  e = gfc_copy_expr (e);

1477
  if (!gfc_simplify_expr (e, 1))
1478 1479
    {
      gfc_free_expr (e);
1480
      return false;
1481 1482 1483 1484 1485 1486
    }

  return current_expand.expand_work_function (e);
}


1487
static bool
Steven G. Kargl committed
1488
expand_iterator (gfc_constructor *c)
1489 1490 1491 1492
{
  gfc_expr *start, *end, *step;
  iterator_stack frame;
  mpz_t trip;
1493
  bool t;
1494 1495 1496

  end = step = NULL;

1497
  t = false;
1498 1499 1500

  mpz_init (trip);
  mpz_init (frame.value);
1501
  frame.prev = NULL;
1502 1503

  start = gfc_copy_expr (c->iterator->start);
1504
  if (!gfc_simplify_expr (start, 1))
1505 1506 1507 1508 1509 1510
    goto cleanup;

  if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
    goto cleanup;

  end = gfc_copy_expr (c->iterator->end);
1511
  if (!gfc_simplify_expr (end, 1))
1512 1513 1514 1515 1516 1517
    goto cleanup;

  if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
    goto cleanup;

  step = gfc_copy_expr (c->iterator->step);
1518
  if (!gfc_simplify_expr (step, 1))
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    goto cleanup;

  if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
    goto cleanup;

  if (mpz_sgn (step->value.integer) == 0)
    {
      gfc_error ("Iterator step at %L cannot be zero", &step->where);
      goto cleanup;
    }

  /* Calculate the trip count of the loop.  */
  mpz_sub (trip, end->value.integer, start->value.integer);
  mpz_add (trip, trip, step->value.integer);
  mpz_tdiv_q (trip, trip, step->value.integer);

  mpz_set (frame.value, start->value.integer);

  frame.prev = iter_stack;
  frame.variable = c->iterator->var->symtree;
  iter_stack = &frame;

  while (mpz_sgn (trip) > 0)
    {
1543
      if (!expand_expr (c->expr))
1544 1545 1546 1547 1548 1549
	goto cleanup;

      mpz_add (frame.value, frame.value, step->value.integer);
      mpz_sub_ui (trip, trip, 1);
    }

1550
  t = true;
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570

cleanup:
  gfc_free_expr (start);
  gfc_free_expr (end);
  gfc_free_expr (step);

  mpz_clear (trip);
  mpz_clear (frame.value);

  iter_stack = frame.prev;

  return t;
}


/* Expand a constructor into constant constructors without any
   iterators, calling the work function for each of the expanded
   expressions.  The work function needs to either save or free the
   passed expression.  */

1571
static bool
Jerry DeLisle committed
1572
expand_constructor (gfc_constructor_base base)
1573
{
Jerry DeLisle committed
1574
  gfc_constructor *c;
1575 1576
  gfc_expr *e;

Jerry DeLisle committed
1577
  for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1578 1579 1580
    {
      if (c->iterator != NULL)
	{
1581 1582
	  if (!expand_iterator (c))
	    return false;
1583 1584 1585 1586 1587 1588 1589
	  continue;
	}

      e = c->expr;

      if (e->expr_type == EXPR_ARRAY)
	{
1590 1591
	  if (!expand_constructor (e->value.constructor))
	    return false;
1592 1593 1594 1595 1596

	  continue;
	}

      e = gfc_copy_expr (e);
1597
      if (!gfc_simplify_expr (e, 1))
1598 1599
	{
	  gfc_free_expr (e);
1600
	  return false;
1601
	}
Jerry DeLisle committed
1602
      current_expand.offset = &c->offset;
1603
      current_expand.repeat = &c->repeat;
Jerry DeLisle committed
1604
      current_expand.component = c->n.component;
1605 1606
      if (!current_expand.expand_work_function(e))
	return false;
1607
    }
1608
  return true;
1609 1610 1611
}


Jerry DeLisle committed
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
/* Given an array expression and an element number (starting at zero),
   return a pointer to the array element.  NULL is returned if the
   size of the array has been exceeded.  The expression node returned
   remains a part of the array and should not be freed.  Access is not
   efficient at all, but this is another place where things do not
   have to be particularly fast.  */

static gfc_expr *
gfc_get_array_element (gfc_expr *array, int element)
{
  expand_info expand_save;
  gfc_expr *e;
1624
  bool rc;
Jerry DeLisle committed
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637

  expand_save = current_expand;
  current_expand.extract_n = element;
  current_expand.expand_work_function = extract_element;
  current_expand.extracted = NULL;
  current_expand.extract_count = 0;

  iter_stack = NULL;

  rc = expand_constructor (array->value.constructor);
  e = current_expand.extracted;
  current_expand = expand_save;

1638
  if (!rc)
Jerry DeLisle committed
1639 1640 1641 1642 1643 1644
    return NULL;

  return e;
}


1645 1646 1647
/* Top level subroutine for expanding constructors.  We only expand
   constructor if they are small enough.  */

1648
bool
1649
gfc_expand_constructor (gfc_expr *e, bool fatal)
1650 1651 1652
{
  expand_info expand_save;
  gfc_expr *f;
1653
  bool rc;
1654

Jerry DeLisle committed
1655 1656
  /* If we can successfully get an array element at the max array size then
     the array is too big to expand, so we just return.  */
1657
  f = gfc_get_array_element (e, gfc_option.flag_max_array_constructor);
1658 1659 1660
  if (f != NULL)
    {
      gfc_free_expr (f);
1661 1662 1663 1664 1665 1666 1667
      if (fatal)
	{
	  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", &e->where,
		     gfc_option.flag_max_array_constructor);
1668
	  return false;
1669
	}
1670
      return true;
1671 1672
    }

Jerry DeLisle committed
1673
  /* We now know the array is not too big so go ahead and try to expand it.  */
1674
  expand_save = current_expand;
Jerry DeLisle committed
1675
  current_expand.base = NULL;
1676 1677 1678 1679 1680

  iter_stack = NULL;

  current_expand.expand_work_function = expand;

1681
  if (!expand_constructor (e->value.constructor))
1682
    {
Jerry DeLisle committed
1683
      gfc_constructor_free (current_expand.base);
1684
      rc = false;
1685 1686 1687
      goto done;
    }

Jerry DeLisle committed
1688 1689
  gfc_constructor_free (e->value.constructor);
  e->value.constructor = current_expand.base;
1690

1691
  rc = true;
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701

done:
  current_expand = expand_save;

  return rc;
}


/* Work function for checking that an element of a constructor is a
   constant, after removal of any iteration variables.  We return
1702
   false if not so.  */
1703

1704
static bool
1705
is_constant_element (gfc_expr *e)
1706 1707 1708 1709 1710 1711
{
  int rv;

  rv = gfc_is_constant_expr (e);
  gfc_free_expr (e);

1712
  return rv ? true : false;
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
}


/* Given an array constructor, determine if the constructor is
   constant or not by expanding it and making sure that all elements
   are constants.  This is a bit of a hack since something like (/ (i,
   i=1,100000000) /) will take a while as* opposed to a more clever
   function that traverses the expression tree. FIXME.  */

int
Steven G. Kargl committed
1723
gfc_constant_ac (gfc_expr *e)
1724 1725
{
  expand_info expand_save;
1726
  bool rc;
1727

Jerry DeLisle committed
1728 1729 1730
  iter_stack = NULL;
  expand_save = current_expand;
  current_expand.expand_work_function = is_constant_element;
1731

Jerry DeLisle committed
1732
  rc = expand_constructor (e->value.constructor);
1733

Jerry DeLisle committed
1734
  current_expand = expand_save;
1735
  if (!rc)
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
    return 0;

  return 1;
}


/* Returns nonzero if an array constructor has been completely
   expanded (no iterators) and zero if iterators are present.  */

int
Steven G. Kargl committed
1746
gfc_expanded_ac (gfc_expr *e)
1747
{
Jerry DeLisle committed
1748
  gfc_constructor *c;
1749 1750

  if (e->expr_type == EXPR_ARRAY)
Jerry DeLisle committed
1751 1752 1753
    for (c = gfc_constructor_first (e->value.constructor);
	 c; c = gfc_constructor_next (c))
      if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1754 1755 1756 1757 1758 1759 1760 1761
	return 0;

  return 1;
}


/*************** Type resolution of array constructors ***************/

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805

/* The symbol expr_is_sought_symbol_ref will try to find.  */
static const gfc_symbol *sought_symbol = NULL;


/* Tells whether the expression E is a variable reference to the symbol
   in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
   accordingly.
   To be used with gfc_expr_walker: if a reference is found we don't need
   to look further so we return 1 to skip any further walk.  */

static int
expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
			   void *where)
{
  gfc_expr *expr = *e;
  locus *sym_loc = (locus *)where;

  if (expr->expr_type == EXPR_VARIABLE
      && expr->symtree->n.sym == sought_symbol)
    {
      *sym_loc = expr->where;
      return 1;
    }

  return 0;
}


/* Tells whether the expression EXPR contains a reference to the symbol
   SYM and in that case sets the position SYM_LOC where the reference is.  */

static bool
find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
{
  int ret;

  sought_symbol = sym;
  ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
  sought_symbol = NULL;
  return ret;
}


1806 1807 1808
/* Recursive array list resolution function.  All of the elements must
   be of the same type.  */

1809
static bool
Jerry DeLisle committed
1810
resolve_array_list (gfc_constructor_base base)
1811
{
1812
  bool t;
Jerry DeLisle committed
1813
  gfc_constructor *c;
1814
  gfc_iterator *iter;
1815

1816
  t = true;
1817

Jerry DeLisle committed
1818
  for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1819
    {
1820 1821 1822 1823 1824
      iter = c->iterator;
      if (iter != NULL)
        {
	  gfc_symbol *iter_var;
	  locus iter_var_loc;
1825

1826 1827
	  if (!gfc_resolve_iterator (iter, false, true))
	    t = false;
1828 1829 1830 1831 1832 1833

	  /* Check for bounds referencing the iterator variable.  */
	  gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
	  iter_var = iter->var->symtree->n.sym;
	  if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
	    {
1834 1835 1836 1837
	      if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
				   "expression references control variable "
				   "at %L", &iter_var_loc))
	       t = false;
1838 1839 1840
	    }
	  if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
	    {
1841 1842 1843 1844
	      if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
				   "expression references control variable "
				   "at %L", &iter_var_loc))
	       t = false;
1845 1846 1847
	    }
	  if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
	    {
1848 1849 1850 1851
	      if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
				   "expression references control variable "
				   "at %L", &iter_var_loc))
	       t = false;
1852 1853
	    }
	}
1854

1855 1856
      if (!gfc_resolve_expr (c->expr))
	t = false;
1857 1858 1859 1860 1861

      if (UNLIMITED_POLY (c->expr))
	{
	  gfc_error ("Array constructor value at %L shall not be unlimited "
		     "polymorphic [F2008: C4106]", &c->expr->where);
1862
	  t = false;
1863
	}
1864 1865 1866 1867 1868
    }

  return t;
}

1869
/* Resolve character array constructor. If it has a specified constant character
1870
   length, pad/truncate the elements here; if the length is not specified and
1871 1872
   all elements are of compile-time known length, emit an error as this is
   invalid.  */
1873

1874
bool
Steven G. Kargl committed
1875
gfc_resolve_character_array_constructor (gfc_expr *expr)
1876
{
Steven G. Kargl committed
1877
  gfc_constructor *p;
1878
  int found_length;
1879 1880 1881 1882

  gcc_assert (expr->expr_type == EXPR_ARRAY);
  gcc_assert (expr->ts.type == BT_CHARACTER);

1883
  if (expr->ts.u.cl == NULL)
Feng Wang committed
1884
    {
Jerry DeLisle committed
1885 1886
      for (p = gfc_constructor_first (expr->value.constructor);
	   p; p = gfc_constructor_next (p))
1887
	if (p->expr->ts.u.cl != NULL)
1888 1889 1890
	  {
	    /* Ensure that if there is a char_len around that it is
	       used; otherwise the middle-end confuses them!  */
1891
	    expr->ts.u.cl = p->expr->ts.u.cl;
1892 1893 1894
	    goto got_charlen;
	  }

1895
      expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
Feng Wang committed
1896 1897
    }

1898 1899
got_charlen:

1900 1901
  found_length = -1;

1902
  if (expr->ts.u.cl->length == NULL)
1903
    {
1904 1905
      /* Check that all constant string elements have the same length until
	 we reach the end or find a variable-length one.  */
1906

Jerry DeLisle committed
1907 1908
      for (p = gfc_constructor_first (expr->value.constructor);
	   p; p = gfc_constructor_next (p))
1909
	{
1910
	  int current_length = -1;
1911 1912 1913
	  gfc_ref *ref;
	  for (ref = p->expr->ref; ref; ref = ref->next)
	    if (ref->type == REF_SUBSTRING
Steven G. Kargl committed
1914 1915
		&& ref->u.ss.start->expr_type == EXPR_CONSTANT
		&& ref->u.ss.end->expr_type == EXPR_CONSTANT)
1916 1917 1918
	      break;

	  if (p->expr->expr_type == EXPR_CONSTANT)
1919
	    current_length = p->expr->value.character.length;
1920
	  else if (ref)
Steven G. Kargl committed
1921 1922 1923 1924
	    {
	      long j;
	      j = mpz_get_ui (ref->u.ss.end->value.integer)
		- mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1925
	      current_length = (int) j;
Steven G. Kargl committed
1926
	    }
1927 1928
	  else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
		   && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
Steven G. Kargl committed
1929 1930
	    {
	      long j;
1931
	      j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1932
	      current_length = (int) j;
Steven G. Kargl committed
1933
	    }
1934
	  else
1935
	    return true;
1936

1937 1938 1939 1940 1941 1942 1943 1944 1945
	  gcc_assert (current_length != -1);

	  if (found_length == -1)
	    found_length = current_length;
	  else if (found_length != current_length)
	    {
	      gfc_error ("Different CHARACTER lengths (%d/%d) in array"
			 " constructor at %L", found_length, current_length,
			 &p->expr->where);
1946
	      return false;
1947 1948 1949
	    }

	  gcc_assert (found_length == current_length);
1950
	}
1951 1952 1953 1954

      gcc_assert (found_length != -1);

      /* Update the character length of the array constructor.  */
Jerry DeLisle committed
1955 1956
      expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
						NULL, found_length);
1957
    }
1958
  else
1959 1960 1961
    {
      /* We've got a character length specified.  It should be an integer,
	 otherwise an error is signalled elsewhere.  */
1962
      gcc_assert (expr->ts.u.cl->length);
1963 1964 1965 1966

      /* If we've got a constant character length, pad according to this.
	 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
	 max_length only if they pass.  */
1967
      gfc_extract_int (expr->ts.u.cl->length, &found_length);
1968

1969
      /* Now pad/truncate the elements accordingly to the specified character
1970 1971 1972 1973
	 length.  This is ok inside this conditional, as in the case above
	 (without typespec) all elements are verified to have the same length
	 anyway.  */
      if (found_length != -1)
Jerry DeLisle committed
1974 1975
	for (p = gfc_constructor_first (expr->value.constructor);
	     p; p = gfc_constructor_next (p))
1976 1977 1978 1979 1980 1981
	  if (p->expr->expr_type == EXPR_CONSTANT)
	    {
	      gfc_expr *cl = NULL;
	      int current_length = -1;
	      bool has_ts;

1982
	      if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1983
	      {
1984
		cl = p->expr->ts.u.cl->length;
1985 1986 1987 1988 1989 1990
		gfc_extract_int (cl, &current_length);
	      }

	      /* If gfc_extract_int above set current_length, we implicitly
		 know the type is BT_INTEGER and it's EXPR_CONSTANT.  */

1991
	      has_ts = expr->ts.u.cl->length_from_typespec;
1992 1993

	      if (! cl
1994
		  || (current_length != -1 && current_length != found_length))
1995 1996 1997
		gfc_set_constant_character_len (found_length, p->expr,
						has_ts ? -1 : found_length);
	    }
1998 1999
    }

2000
  return true;
2001 2002
}

Steven G. Kargl committed
2003

2004
/* Resolve all of the expressions in an array list.  */
2005

2006
bool
Steven G. Kargl committed
2007
gfc_resolve_array_constructor (gfc_expr *expr)
2008
{
2009
  bool t;
2010 2011

  t = resolve_array_list (expr->value.constructor);
2012
  if (t)
2013
    t = gfc_check_constructor_type (expr);
2014 2015 2016 2017

  /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
     the call to this function, so we don't need to call it here; if it was
     called twice, an error message there would be duplicated.  */
2018 2019 2020 2021 2022 2023 2024

  return t;
}


/* Copy an iterator structure.  */

Jerry DeLisle committed
2025 2026
gfc_iterator *
gfc_copy_iterator (gfc_iterator *src)
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
{
  gfc_iterator *dest;

  if (src == NULL)
    return NULL;

  dest = gfc_get_iterator ();

  dest->var = gfc_copy_expr (src->var);
  dest->start = gfc_copy_expr (src->start);
  dest->end = gfc_copy_expr (src->end);
  dest->step = gfc_copy_expr (src->step);

  return dest;
}


/********* Subroutines for determining the size of an array *********/

2046
/* These are needed just to accommodate RESHAPE().  There are no
2047
   diagnostics here, we just return a negative number if something
2048
   goes wrong.  */
2049 2050 2051 2052 2053


/* Get the size of single dimension of an array specification.  The
   array is guaranteed to be one dimensional.  */

2054
bool
Steven G. Kargl committed
2055
spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2056 2057
{
  if (as == NULL)
2058
    return false;
2059 2060 2061 2062 2063 2064

  if (dimen < 0 || dimen > as->rank - 1)
    gfc_internal_error ("spec_dimen_size(): Bad dimension");

  if (as->type != AS_EXPLICIT
      || as->lower[dimen]->expr_type != EXPR_CONSTANT
2065 2066 2067
      || as->upper[dimen]->expr_type != EXPR_CONSTANT
      || as->lower[dimen]->ts.type != BT_INTEGER
      || as->upper[dimen]->ts.type != BT_INTEGER)
2068
    return false;
2069 2070 2071 2072 2073 2074 2075 2076

  mpz_init (*result);

  mpz_sub (*result, as->upper[dimen]->value.integer,
	   as->lower[dimen]->value.integer);

  mpz_add_ui (*result, *result, 1);

2077
  return true;
2078 2079 2080
}


2081
bool
Steven G. Kargl committed
2082
spec_size (gfc_array_spec *as, mpz_t *result)
2083 2084 2085 2086
{
  mpz_t size;
  int d;

2087
  if (as->type == AS_ASSUMED_RANK)
2088
    return false;
2089

2090 2091 2092 2093
  mpz_init_set_ui (*result, 1);

  for (d = 0; d < as->rank; d++)
    {
2094
      if (!spec_dimen_size (as, d, &size))
2095 2096
	{
	  mpz_clear (*result);
2097
	  return false;
2098 2099 2100 2101 2102 2103
	}

      mpz_mul (*result, *result, size);
      mpz_clear (size);
    }

2104
  return true;
2105 2106 2107
}


2108 2109
/* Get the number of elements in an array section. Optionally, also supply
   the end value.  */
2110

2111
bool
2112
gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end)
2113 2114
{
  mpz_t upper, lower, stride;
2115
  bool t;
2116 2117

  if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2118
    gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2119 2120 2121 2122 2123 2124

  switch (ar->dimen_type[dimen])
    {
    case DIMEN_ELEMENT:
      mpz_init (*result);
      mpz_set_ui (*result, 1);
2125
      t = true;
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
      break;

    case DIMEN_VECTOR:
      t = gfc_array_size (ar->start[dimen], result);	/* Recurse! */
      break;

    case DIMEN_RANGE:
      mpz_init (upper);
      mpz_init (lower);
      mpz_init (stride);
2136
      t = false;
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182

      if (ar->start[dimen] == NULL)
	{
	  if (ar->as->lower[dimen] == NULL
	      || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
	    goto cleanup;
	  mpz_set (lower, ar->as->lower[dimen]->value.integer);
	}
      else
	{
	  if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
	    goto cleanup;
	  mpz_set (lower, ar->start[dimen]->value.integer);
	}

      if (ar->end[dimen] == NULL)
	{
	  if (ar->as->upper[dimen] == NULL
	      || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
	    goto cleanup;
	  mpz_set (upper, ar->as->upper[dimen]->value.integer);
	}
      else
	{
	  if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
	    goto cleanup;
	  mpz_set (upper, ar->end[dimen]->value.integer);
	}

      if (ar->stride[dimen] == NULL)
	mpz_set_ui (stride, 1);
      else
	{
	  if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
	    goto cleanup;
	  mpz_set (stride, ar->stride[dimen]->value.integer);
	}

      mpz_init (*result);
      mpz_sub (*result, upper, lower);
      mpz_add (*result, *result, stride);
      mpz_div (*result, *result, stride);

      /* Zero stride caught earlier.  */
      if (mpz_cmp_ui (*result, 0) < 0)
	mpz_set_ui (*result, 0);
2183
      t = true;
2184

2185 2186 2187 2188 2189 2190 2191 2192 2193
      if (end)
	{
	  mpz_init (*end);

	  mpz_sub_ui (*end, *result, 1UL);
	  mpz_mul (*end, *end, stride);
	  mpz_add (*end, *end, lower);
	}

2194 2195 2196 2197 2198 2199 2200
    cleanup:
      mpz_clear (upper);
      mpz_clear (lower);
      mpz_clear (stride);
      return t;

    default:
2201
      gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2202 2203 2204 2205 2206 2207
    }

  return t;
}


2208
static bool
Steven G. Kargl committed
2209
ref_size (gfc_array_ref *ar, mpz_t *result)
2210 2211 2212 2213 2214 2215 2216 2217
{
  mpz_t size;
  int d;

  mpz_init_set_ui (*result, 1);

  for (d = 0; d < ar->dimen; d++)
    {
2218
      if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2219 2220
	{
	  mpz_clear (*result);
2221
	  return false;
2222 2223 2224 2225 2226 2227
	}

      mpz_mul (*result, *result, size);
      mpz_clear (size);
    }

2228
  return true;
2229 2230 2231 2232
}


/* Given an array expression and a dimension, figure out how many
2233 2234
   elements it has along that dimension.  Returns true if we were
   able to return a result in the 'result' variable, false
2235 2236
   otherwise.  */

2237
bool
Steven G. Kargl committed
2238
gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2239 2240 2241 2242
{
  gfc_ref *ref;
  int i;

2243 2244
  gcc_assert (array != NULL);

2245
  if (array->ts.type == BT_CLASS)
2246
    return false;
2247

2248
  if (array->rank == -1)
2249
    return false;
2250

2251
  if (dimen < 0 || dimen > array->rank - 1)
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
    gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");

  switch (array->expr_type)
    {
    case EXPR_VARIABLE:
    case EXPR_FUNCTION:
      for (ref = array->ref; ref; ref = ref->next)
	{
	  if (ref->type != REF_ARRAY)
	    continue;

	  if (ref->u.ar.type == AR_FULL)
	    return spec_dimen_size (ref->u.ar.as, dimen, result);

	  if (ref->u.ar.type == AR_SECTION)
	    {
	      for (i = 0; dimen >= 0; i++)
		if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
		  dimen--;

2272
	      return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2273 2274 2275
	    }
	}

2276 2277 2278
      if (array->shape && array->shape[dimen])
	{
	  mpz_init_set (*result, array->shape[dimen]);
2279
	  return true;
2280 2281
	}

2282
      if (array->symtree->n.sym->attr.generic
2283
	  && array->value.function.esym != NULL)
2284
	{
2285 2286
	  if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
	    return false;
2287
	}
2288 2289
      else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
	return false;
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303

      break;

    case EXPR_ARRAY:
      if (array->shape == NULL) {
	/* Expressions with rank > 1 should have "shape" properly set */
	if ( array->rank != 1 )
	  gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
	return gfc_array_size(array, result);
      }

      /* Fall through */
    default:
      if (array->shape == NULL)
2304
	return false;
2305 2306 2307 2308 2309 2310

      mpz_init_set (*result, array->shape[dimen]);

      break;
    }

2311
  return true;
2312 2313 2314 2315
}


/* Given an array expression, figure out how many elements are in the
2316 2317
   array.  Returns true if this is possible, and sets the 'result'
   variable.  Otherwise returns false.  */
2318

2319
bool
Steven G. Kargl committed
2320
gfc_array_size (gfc_expr *array, mpz_t *result)
2321 2322 2323
{
  expand_info expand_save;
  gfc_ref *ref;
2324
  int i;
2325
  bool t;
2326

2327
  if (array->ts.type == BT_CLASS)
2328
    return false;
2329

2330 2331 2332
  switch (array->expr_type)
    {
    case EXPR_ARRAY:
2333
      gfc_push_suppress_errors ();
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343

      expand_save = current_expand;

      current_expand.count = result;
      mpz_init_set_ui (*result, 0);

      current_expand.expand_work_function = count_elements;
      iter_stack = NULL;

      t = expand_constructor (array->value.constructor);
2344 2345

      gfc_pop_suppress_errors ();
2346

2347
      if (!t)
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
	mpz_clear (*result);
      current_expand = expand_save;
      return t;

    case EXPR_VARIABLE:
      for (ref = array->ref; ref; ref = ref->next)
	{
	  if (ref->type != REF_ARRAY)
	    continue;

	  if (ref->u.ar.type == AR_FULL)
	    return spec_size (ref->u.ar.as, result);

	  if (ref->u.ar.type == AR_SECTION)
	    return ref_size (&ref->u.ar, result);
	}

      return spec_size (array->symtree->n.sym->as, result);


    default:
      if (array->rank == 0 || array->shape == NULL)
2370
	return false;
2371 2372 2373 2374 2375 2376 2377 2378 2379

      mpz_init_set_ui (*result, 1);

      for (i = 0; i < array->rank; i++)
	mpz_mul (*result, *result, array->shape[i]);

      break;
    }

2380
  return true;
2381 2382 2383 2384 2385 2386
}


/* Given an array reference, return the shape of the reference in an
   array of mpz_t integers.  */

2387
bool
Steven G. Kargl committed
2388
gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
{
  int d;
  int i;

  d = 0;

  switch (ar->type)
    {
    case AR_FULL:
      for (; d < ar->as->rank; d++)
2399
	if (!spec_dimen_size (ar->as, d, &shape[d]))
2400 2401
	  goto cleanup;

2402
      return true;
2403 2404 2405 2406 2407 2408

    case AR_SECTION:
      for (i = 0; i < ar->dimen; i++)
	{
	  if (ar->dimen_type[i] != DIMEN_ELEMENT)
	    {
2409
	      if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2410 2411 2412 2413 2414
		goto cleanup;
	      d++;
	    }
	}

2415
      return true;
2416 2417 2418 2419 2420 2421

    default:
      break;
    }

cleanup:
2422
  gfc_clear_shape (shape, d);
2423
  return false;
2424 2425 2426 2427 2428 2429 2430
}


/* Given an array expression, find the array reference structure that
   characterizes the reference.  */

gfc_array_ref *
Steven G. Kargl committed
2431
gfc_find_array_ref (gfc_expr *e)
2432 2433 2434 2435 2436
{
  gfc_ref *ref;

  for (ref = e->ref; ref; ref = ref->next)
    if (ref->type == REF_ARRAY
2437
	&& (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2438 2439 2440 2441 2442 2443 2444
      break;

  if (ref == NULL)
    gfc_internal_error ("gfc_find_array_ref(): No ref found");

  return &ref->u.ar;
}
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463


/* Find out if an array shape is known at compile time.  */

int
gfc_is_compile_time_shape (gfc_array_spec *as)
{
  int i;

  if (as->type != AS_EXPLICIT)
    return 0;

  for (i = 0; i < as->rank; i++)
    if (!gfc_is_constant_expr (as->lower[i])
	|| !gfc_is_constant_expr (as->upper[i]))
      return 0;

  return 1;
}