ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / fortran / array.c
1 /* Array things
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 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 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "constructor.h"
27
28 /**************** Array reference matching subroutines *****************/
29
30 /* Copy an array reference structure. */
31
32 gfc_array_ref *
33 gfc_copy_array_ref (gfc_array_ref *src)
34 {
35 gfc_array_ref *dest;
36 int i;
37
38 if (src == NULL)
39 return NULL;
40
41 dest = gfc_get_array_ref ();
42
43 *dest = *src;
44
45 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
46 {
47 dest->start[i] = gfc_copy_expr (src->start[i]);
48 dest->end[i] = gfc_copy_expr (src->end[i]);
49 dest->stride[i] = gfc_copy_expr (src->stride[i]);
50 }
51
52 return dest;
53 }
54
55
56 /* Match a single dimension of an array reference. This can be a
57 single element or an array section. Any modifications we've made
58 to the ar structure are cleaned up by the caller. If the init
59 is set, we require the subscript to be a valid initialization
60 expression. */
61
62 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 int i;
68
69 i = ar->dimen + ar->codimen;
70
71 gfc_gobble_whitespace ();
72 ar->c_where[i] = gfc_current_locus;
73 ar->start[i] = ar->end[i] = ar->stride[i] = NULL;
74
75 /* We can't be sure of the difference between DIMEN_ELEMENT and
76 DIMEN_VECTOR until we know the type of the element itself at
77 resolution time. */
78
79 ar->dimen_type[i] = DIMEN_UNKNOWN;
80
81 if (gfc_match_char (':') == MATCH_YES)
82 goto end_element;
83
84 /* Get start element. */
85 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
86 star = true;
87
88 if (!star && init)
89 m = gfc_match_init_expr (&ar->start[i]);
90 else if (!star)
91 m = gfc_match_expr (&ar->start[i]);
92
93 if (m == MATCH_NO)
94 gfc_error ("Expected array subscript at %C");
95 if (m != MATCH_YES)
96 return MATCH_ERROR;
97
98 if (gfc_match_char (':') == MATCH_NO)
99 goto matched;
100
101 if (star)
102 {
103 gfc_error ("Unexpected '*' in coarray subscript at %C");
104 return MATCH_ERROR;
105 }
106
107 /* Get an optional end element. Because we've seen the colon, we
108 definitely have a range along this dimension. */
109 end_element:
110 ar->dimen_type[i] = DIMEN_RANGE;
111
112 if (match_star && (m = gfc_match_char ('*')) == MATCH_YES)
113 star = true;
114 else if (init)
115 m = gfc_match_init_expr (&ar->end[i]);
116 else
117 m = gfc_match_expr (&ar->end[i]);
118
119 if (m == MATCH_ERROR)
120 return MATCH_ERROR;
121
122 /* See if we have an optional stride. */
123 if (gfc_match_char (':') == MATCH_YES)
124 {
125 if (star)
126 {
127 gfc_error ("Strides not allowed in coarray subscript at %C");
128 return MATCH_ERROR;
129 }
130
131 m = init ? gfc_match_init_expr (&ar->stride[i])
132 : gfc_match_expr (&ar->stride[i]);
133
134 if (m == MATCH_NO)
135 gfc_error ("Expected array subscript stride at %C");
136 if (m != MATCH_YES)
137 return MATCH_ERROR;
138 }
139
140 matched:
141 if (star)
142 ar->dimen_type[i] = DIMEN_STAR;
143
144 return MATCH_YES;
145 }
146
147
148 /* Match an array reference, whether it is the whole array or a
149 particular elements or a section. If init is set, the reference has
150 to consist of init expressions. */
151
152 match
153 gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init,
154 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 ar->type = AR_UNKNOWN;
164
165 if (gfc_match_char ('[') == MATCH_YES)
166 {
167 matched_bracket = true;
168 goto coarray;
169 }
170
171 if (gfc_match_char ('(') != MATCH_YES)
172 {
173 ar->type = AR_FULL;
174 ar->dimen = 0;
175 return MATCH_YES;
176 }
177
178 for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++)
179 {
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 ar->dimen++;
187 goto coarray;
188 }
189
190 if (gfc_match_char (',') != MATCH_YES)
191 {
192 gfc_error ("Invalid form of array reference at %C");
193 return MATCH_ERROR;
194 }
195 }
196
197 gfc_error ("Array reference at %C cannot have more than %d dimensions",
198 GFC_MAX_DIMENSIONS);
199 return MATCH_ERROR;
200
201 coarray:
202 if (!matched_bracket && gfc_match_char ('[') != MATCH_YES)
203 {
204 if (ar->dimen > 0)
205 return MATCH_YES;
206 else
207 return MATCH_ERROR;
208 }
209
210 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
211 {
212 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
213 return MATCH_ERROR;
214 }
215
216 if (corank == 0)
217 {
218 gfc_error ("Unexpected coarray designator at %C");
219 return MATCH_ERROR;
220 }
221
222 for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; ar->codimen++)
223 {
224 m = match_subscript (ar, init, true);
225 if (m == MATCH_ERROR)
226 return MATCH_ERROR;
227
228 if (gfc_match_char (']') == MATCH_YES)
229 {
230 ar->codimen++;
231 if (ar->codimen < corank)
232 {
233 gfc_error ("Too few codimensions at %C, expected %d not %d",
234 corank, ar->codimen);
235 return MATCH_ERROR;
236 }
237 if (ar->codimen > corank)
238 {
239 gfc_error ("Too many codimensions at %C, expected %d not %d",
240 corank, ar->codimen);
241 return MATCH_ERROR;
242 }
243 return MATCH_YES;
244 }
245
246 if (gfc_match_char (',') != MATCH_YES)
247 {
248 if (gfc_match_char ('*') == MATCH_YES)
249 gfc_error ("Unexpected '*' for codimension %d of %d at %C",
250 ar->codimen + 1, corank);
251 else
252 gfc_error ("Invalid form of coarray reference at %C");
253 return MATCH_ERROR;
254 }
255 else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR)
256 {
257 gfc_error ("Unexpected '*' for codimension %d of %d at %C",
258 ar->codimen + 1, corank);
259 return MATCH_ERROR;
260 }
261
262 if (ar->codimen >= corank)
263 {
264 gfc_error ("Invalid codimension %d at %C, only %d codimensions exist",
265 ar->codimen + 1, corank);
266 return MATCH_ERROR;
267 }
268 }
269
270 gfc_error ("Array reference at %C cannot have more than %d dimensions",
271 GFC_MAX_DIMENSIONS);
272 return MATCH_ERROR;
273
274 }
275
276
277 /************** Array specification matching subroutines ***************/
278
279 /* Free all of the expressions associated with array bounds
280 specifications. */
281
282 void
283 gfc_free_array_spec (gfc_array_spec *as)
284 {
285 int i;
286
287 if (as == NULL)
288 return;
289
290 for (i = 0; i < as->rank + as->corank; i++)
291 {
292 gfc_free_expr (as->lower[i]);
293 gfc_free_expr (as->upper[i]);
294 }
295
296 free (as);
297 }
298
299
300 /* Take an array bound, resolves the expression, that make up the
301 shape and check associated constraints. */
302
303 static bool
304 resolve_array_bound (gfc_expr *e, int check_constant)
305 {
306 if (e == NULL)
307 return true;
308
309 if (!gfc_resolve_expr (e)
310 || !gfc_specification_expr (e))
311 return false;
312
313 if (check_constant && !gfc_is_constant_expr (e))
314 {
315 if (e->expr_type == EXPR_VARIABLE)
316 gfc_error ("Variable '%s' at %L in this context must be constant",
317 e->symtree->n.sym->name, &e->where);
318 else
319 gfc_error ("Expression at %L in this context must be constant",
320 &e->where);
321 return false;
322 }
323
324 return true;
325 }
326
327
328 /* Takes an array specification, resolves the expressions that make up
329 the shape and make sure everything is integral. */
330
331 bool
332 gfc_resolve_array_spec (gfc_array_spec *as, int check_constant)
333 {
334 gfc_expr *e;
335 int i;
336
337 if (as == NULL)
338 return true;
339
340 for (i = 0; i < as->rank + as->corank; i++)
341 {
342 e = as->lower[i];
343 if (!resolve_array_bound (e, check_constant))
344 return false;
345
346 e = as->upper[i];
347 if (!resolve_array_bound (e, check_constant))
348 return false;
349
350 if ((as->lower[i] == NULL) || (as->upper[i] == NULL))
351 continue;
352
353 /* If the size is negative in this dimension, set it to zero. */
354 if (as->lower[i]->expr_type == EXPR_CONSTANT
355 && as->upper[i]->expr_type == EXPR_CONSTANT
356 && mpz_cmp (as->upper[i]->value.integer,
357 as->lower[i]->value.integer) < 0)
358 {
359 gfc_free_expr (as->upper[i]);
360 as->upper[i] = gfc_copy_expr (as->lower[i]);
361 mpz_sub_ui (as->upper[i]->value.integer,
362 as->upper[i]->value.integer, 1);
363 }
364 }
365
366 return true;
367 }
368
369
370 /* Match a single array element specification. The return values as
371 well as the upper and lower bounds of the array spec are filled
372 in according to what we see on the input. The caller makes sure
373 individual specifications make sense as a whole.
374
375
376 Parsed Lower Upper Returned
377 ------------------------------------
378 : NULL NULL AS_DEFERRED (*)
379 x 1 x AS_EXPLICIT
380 x: x NULL AS_ASSUMED_SHAPE
381 x:y x y AS_EXPLICIT
382 x:* x NULL AS_ASSUMED_SIZE
383 * 1 NULL AS_ASSUMED_SIZE
384
385 (*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This
386 is fixed during the resolution of formal interfaces.
387
388 Anything else AS_UNKNOWN. */
389
390 static array_type
391 match_array_element_spec (gfc_array_spec *as)
392 {
393 gfc_expr **upper, **lower;
394 match m;
395 int rank;
396
397 rank = as->rank == -1 ? 0 : as->rank;
398 lower = &as->lower[rank + as->corank - 1];
399 upper = &as->upper[rank + as->corank - 1];
400
401 if (gfc_match_char ('*') == MATCH_YES)
402 {
403 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
404 return AS_ASSUMED_SIZE;
405 }
406
407 if (gfc_match_char (':') == MATCH_YES)
408 return AS_DEFERRED;
409
410 m = gfc_match_expr (upper);
411 if (m == MATCH_NO)
412 gfc_error ("Expected expression in array specification at %C");
413 if (m != MATCH_YES)
414 return AS_UNKNOWN;
415 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
416 return AS_UNKNOWN;
417
418 if (gfc_match_char (':') == MATCH_NO)
419 {
420 *lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
421 return AS_EXPLICIT;
422 }
423
424 *lower = *upper;
425 *upper = NULL;
426
427 if (gfc_match_char ('*') == MATCH_YES)
428 return AS_ASSUMED_SIZE;
429
430 m = gfc_match_expr (upper);
431 if (m == MATCH_ERROR)
432 return AS_UNKNOWN;
433 if (m == MATCH_NO)
434 return AS_ASSUMED_SHAPE;
435 if (!gfc_expr_check_typed (*upper, gfc_current_ns, false))
436 return AS_UNKNOWN;
437
438 return AS_EXPLICIT;
439 }
440
441
442 /* Matches an array specification, incidentally figuring out what sort
443 it is. Match either a normal array specification, or a coarray spec
444 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 array_type current_type;
450 gfc_array_spec *as;
451 int i;
452
453 as = gfc_get_array_spec ();
454
455 if (!match_dim)
456 goto coarray;
457
458 if (gfc_match_char ('(') != MATCH_YES)
459 {
460 if (!match_codim)
461 goto done;
462 goto coarray;
463 }
464
465 if (gfc_match (" .. )") == MATCH_YES)
466 {
467 as->type = AS_ASSUMED_RANK;
468 as->rank = -1;
469
470 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed-rank array at %C"))
471 goto cleanup;
472
473 if (!match_codim)
474 goto done;
475 goto coarray;
476 }
477
478 for (;;)
479 {
480 as->rank++;
481 current_type = match_array_element_spec (as);
482
483 /* Note that current_type == AS_ASSUMED_SIZE for both assumed-size
484 and implied-shape specifications. If the rank is at least 2, we can
485 distinguish between them. But for rank 1, we currently return
486 ASSUMED_SIZE; this gets adjusted later when we know for sure
487 whether the symbol parsed is a PARAMETER or not. */
488
489 if (as->rank == 1)
490 {
491 if (current_type == AS_UNKNOWN)
492 goto cleanup;
493 as->type = current_type;
494 }
495 else
496 switch (as->type)
497 { /* See how current spec meshes with the existing. */
498 case AS_UNKNOWN:
499 goto cleanup;
500
501 case AS_IMPLIED_SHAPE:
502 if (current_type != AS_ASSUMED_SHAPE)
503 {
504 gfc_error ("Bad array specification for implied-shape"
505 " array at %C");
506 goto cleanup;
507 }
508 break;
509
510 case AS_EXPLICIT:
511 if (current_type == AS_ASSUMED_SIZE)
512 {
513 as->type = AS_ASSUMED_SIZE;
514 break;
515 }
516
517 if (current_type == AS_EXPLICIT)
518 break;
519
520 gfc_error ("Bad array specification for an explicitly shaped "
521 "array at %C");
522
523 goto cleanup;
524
525 case AS_ASSUMED_SHAPE:
526 if ((current_type == AS_ASSUMED_SHAPE)
527 || (current_type == AS_DEFERRED))
528 break;
529
530 gfc_error ("Bad array specification for assumed shape "
531 "array at %C");
532 goto cleanup;
533
534 case AS_DEFERRED:
535 if (current_type == AS_DEFERRED)
536 break;
537
538 if (current_type == AS_ASSUMED_SHAPE)
539 {
540 as->type = AS_ASSUMED_SHAPE;
541 break;
542 }
543
544 gfc_error ("Bad specification for deferred shape array at %C");
545 goto cleanup;
546
547 case AS_ASSUMED_SIZE:
548 if (as->rank == 2 && current_type == AS_ASSUMED_SIZE)
549 {
550 as->type = AS_IMPLIED_SHAPE;
551 break;
552 }
553
554 gfc_error ("Bad specification for assumed size array at %C");
555 goto cleanup;
556
557 case AS_ASSUMED_RANK:
558 gcc_unreachable ();
559 }
560
561 if (gfc_match_char (')') == MATCH_YES)
562 break;
563
564 if (gfc_match_char (',') != MATCH_YES)
565 {
566 gfc_error ("Expected another dimension in array declaration at %C");
567 goto cleanup;
568 }
569
570 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
571 {
572 gfc_error ("Array specification at %C has more than %d dimensions",
573 GFC_MAX_DIMENSIONS);
574 goto cleanup;
575 }
576
577 if (as->corank + as->rank >= 7
578 && !gfc_notify_std (GFC_STD_F2008, "Array specification at %C "
579 "with more than 7 dimensions"))
580 goto cleanup;
581 }
582
583 if (!match_codim)
584 goto done;
585
586 coarray:
587 if (gfc_match_char ('[') != MATCH_YES)
588 goto done;
589
590 if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C"))
591 goto cleanup;
592
593 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
594 {
595 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
596 goto cleanup;
597 }
598
599 if (as->rank >= GFC_MAX_DIMENSIONS)
600 {
601 gfc_error ("Array specification at %C has more than %d "
602 "dimensions", GFC_MAX_DIMENSIONS);
603 goto cleanup;
604 }
605
606 for (;;)
607 {
608 as->corank++;
609 current_type = match_array_element_spec (as);
610
611 if (current_type == AS_UNKNOWN)
612 goto cleanup;
613
614 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 case AS_UNKNOWN:
621 goto cleanup;
622
623 case AS_EXPLICIT:
624 if (current_type == AS_ASSUMED_SIZE)
625 {
626 as->cotype = AS_ASSUMED_SIZE;
627 break;
628 }
629
630 if (current_type == AS_EXPLICIT)
631 break;
632
633 gfc_error ("Bad array specification for an explicitly "
634 "shaped array at %C");
635
636 goto cleanup;
637
638 case AS_ASSUMED_SHAPE:
639 if ((current_type == AS_ASSUMED_SHAPE)
640 || (current_type == AS_DEFERRED))
641 break;
642
643 gfc_error ("Bad array specification for assumed shape "
644 "array at %C");
645 goto cleanup;
646
647 case AS_DEFERRED:
648 if (current_type == AS_DEFERRED)
649 break;
650
651 if (current_type == AS_ASSUMED_SHAPE)
652 {
653 as->cotype = AS_ASSUMED_SHAPE;
654 break;
655 }
656
657 gfc_error ("Bad specification for deferred shape array at %C");
658 goto cleanup;
659
660 case AS_ASSUMED_SIZE:
661 gfc_error ("Bad specification for assumed size array at %C");
662 goto cleanup;
663
664 case AS_ASSUMED_RANK:
665 gcc_unreachable ();
666 }
667
668 if (gfc_match_char (']') == MATCH_YES)
669 break;
670
671 if (gfc_match_char (',') != MATCH_YES)
672 {
673 gfc_error ("Expected another dimension in array declaration at %C");
674 goto cleanup;
675 }
676
677 if (as->rank + as->corank >= GFC_MAX_DIMENSIONS)
678 {
679 gfc_error ("Array specification at %C has more than %d "
680 "dimensions", GFC_MAX_DIMENSIONS);
681 goto cleanup;
682 }
683 }
684
685 if (current_type == AS_EXPLICIT)
686 {
687 gfc_error ("Upper bound of last coarray dimension must be '*' at %C");
688 goto cleanup;
689 }
690
691 if (as->cotype == AS_ASSUMED_SIZE)
692 as->cotype = AS_EXPLICIT;
693
694 if (as->rank == 0)
695 as->type = as->cotype;
696
697 done:
698 if (as->rank == 0 && as->corank == 0)
699 {
700 *asp = NULL;
701 gfc_free_array_spec (as);
702 return MATCH_NO;
703 }
704
705 /* If a lower bounds of an assumed shape array is blank, put in one. */
706 if (as->type == AS_ASSUMED_SHAPE)
707 {
708 for (i = 0; i < as->rank + as->corank; i++)
709 {
710 if (as->lower[i] == NULL)
711 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
712 }
713 }
714
715 *asp = as;
716
717 return MATCH_YES;
718
719 cleanup:
720 /* Something went wrong. */
721 gfc_free_array_spec (as);
722 return MATCH_ERROR;
723 }
724
725
726 /* Given a symbol and an array specification, modify the symbol to
727 have that array specification. The error locus is needed in case
728 something goes wrong. On failure, the caller must free the spec. */
729
730 bool
731 gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc)
732 {
733 int i;
734
735 if (as == NULL)
736 return true;
737
738 if (as->rank
739 && !gfc_add_dimension (&sym->attr, sym->name, error_loc))
740 return false;
741
742 if (as->corank
743 && !gfc_add_codimension (&sym->attr, sym->name, error_loc))
744 return false;
745
746 if (sym->as == NULL)
747 {
748 sym->as = as;
749 return true;
750 }
751
752 if ((sym->as->type == AS_ASSUMED_RANK && as->corank)
753 || (as->type == AS_ASSUMED_RANK && sym->as->corank))
754 {
755 gfc_error ("The assumed-rank array '%s' at %L shall not have a "
756 "codimension", sym->name, error_loc);
757 return false;
758 }
759
760 if (as->corank)
761 {
762 /* The "sym" has no corank (checked via gfc_add_codimension). Thus
763 the codimension is simply added. */
764 gcc_assert (as->rank == 0 && sym->as->corank == 0);
765
766 sym->as->cotype = as->cotype;
767 sym->as->corank = as->corank;
768 for (i = 0; i < as->corank; i++)
769 {
770 sym->as->lower[sym->as->rank + i] = as->lower[i];
771 sym->as->upper[sym->as->rank + i] = as->upper[i];
772 }
773 }
774 else
775 {
776 /* The "sym" has no rank (checked via gfc_add_dimension). Thus
777 the dimension is added - but first the codimensions (if existing
778 need to be shifted to make space for the dimension. */
779 gcc_assert (as->corank == 0 && sym->as->rank == 0);
780
781 sym->as->rank = as->rank;
782 sym->as->type = as->type;
783 sym->as->cray_pointee = as->cray_pointee;
784 sym->as->cp_was_assumed = as->cp_was_assumed;
785
786 for (i = 0; i < sym->as->corank; i++)
787 {
788 sym->as->lower[as->rank + i] = sym->as->lower[i];
789 sym->as->upper[as->rank + i] = sym->as->upper[i];
790 }
791 for (i = 0; i < as->rank; i++)
792 {
793 sym->as->lower[i] = as->lower[i];
794 sym->as->upper[i] = as->upper[i];
795 }
796 }
797
798 free (as);
799 return true;
800 }
801
802
803 /* Copy an array specification. */
804
805 gfc_array_spec *
806 gfc_copy_array_spec (gfc_array_spec *src)
807 {
808 gfc_array_spec *dest;
809 int i;
810
811 if (src == NULL)
812 return NULL;
813
814 dest = gfc_get_array_spec ();
815
816 *dest = *src;
817
818 for (i = 0; i < dest->rank + dest->corank; i++)
819 {
820 dest->lower[i] = gfc_copy_expr (dest->lower[i]);
821 dest->upper[i] = gfc_copy_expr (dest->upper[i]);
822 }
823
824 return dest;
825 }
826
827
828 /* Returns nonzero if the two expressions are equal. Only handles integer
829 constants. */
830
831 static int
832 compare_bounds (gfc_expr *bound1, gfc_expr *bound2)
833 {
834 if (bound1 == NULL || bound2 == NULL
835 || bound1->expr_type != EXPR_CONSTANT
836 || bound2->expr_type != EXPR_CONSTANT
837 || bound1->ts.type != BT_INTEGER
838 || bound2->ts.type != BT_INTEGER)
839 gfc_internal_error ("gfc_compare_array_spec(): Array spec clobbered");
840
841 if (mpz_cmp (bound1->value.integer, bound2->value.integer) == 0)
842 return 1;
843 else
844 return 0;
845 }
846
847
848 /* Compares two array specifications. They must be constant or deferred
849 shape. */
850
851 int
852 gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2)
853 {
854 int i;
855
856 if (as1 == NULL && as2 == NULL)
857 return 1;
858
859 if (as1 == NULL || as2 == NULL)
860 return 0;
861
862 if (as1->rank != as2->rank)
863 return 0;
864
865 if (as1->corank != as2->corank)
866 return 0;
867
868 if (as1->rank == 0)
869 return 1;
870
871 if (as1->type != as2->type)
872 return 0;
873
874 if (as1->type == AS_EXPLICIT)
875 for (i = 0; i < as1->rank + as1->corank; i++)
876 {
877 if (compare_bounds (as1->lower[i], as2->lower[i]) == 0)
878 return 0;
879
880 if (compare_bounds (as1->upper[i], as2->upper[i]) == 0)
881 return 0;
882 }
883
884 return 1;
885 }
886
887
888 /****************** Array constructor functions ******************/
889
890
891 /* Given an expression node that might be an array constructor and a
892 symbol, make sure that no iterators in this or child constructors
893 use the symbol as an implied-DO iterator. Returns nonzero if a
894 duplicate was found. */
895
896 static int
897 check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master)
898 {
899 gfc_constructor *c;
900 gfc_expr *e;
901
902 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
903 {
904 e = c->expr;
905
906 if (e->expr_type == EXPR_ARRAY
907 && check_duplicate_iterator (e->value.constructor, master))
908 return 1;
909
910 if (c->iterator == NULL)
911 continue;
912
913 if (c->iterator->var->symtree->n.sym == master)
914 {
915 gfc_error ("DO-iterator '%s' at %L is inside iterator of the "
916 "same name", master->name, &c->where);
917
918 return 1;
919 }
920 }
921
922 return 0;
923 }
924
925
926 /* Forward declaration because these functions are mutually recursive. */
927 static match match_array_cons_element (gfc_constructor_base *);
928
929 /* Match a list of array elements. */
930
931 static match
932 match_array_list (gfc_constructor_base *result)
933 {
934 gfc_constructor_base head;
935 gfc_constructor *p;
936 gfc_iterator iter;
937 locus old_loc;
938 gfc_expr *e;
939 match m;
940 int n;
941
942 old_loc = gfc_current_locus;
943
944 if (gfc_match_char ('(') == MATCH_NO)
945 return MATCH_NO;
946
947 memset (&iter, '\0', sizeof (gfc_iterator));
948 head = NULL;
949
950 m = match_array_cons_element (&head);
951 if (m != MATCH_YES)
952 goto cleanup;
953
954 if (gfc_match_char (',') != MATCH_YES)
955 {
956 m = MATCH_NO;
957 goto cleanup;
958 }
959
960 for (n = 1;; n++)
961 {
962 m = gfc_match_iterator (&iter, 0);
963 if (m == MATCH_YES)
964 break;
965 if (m == MATCH_ERROR)
966 goto cleanup;
967
968 m = match_array_cons_element (&head);
969 if (m == MATCH_ERROR)
970 goto cleanup;
971 if (m == MATCH_NO)
972 {
973 if (n > 2)
974 goto syntax;
975 m = MATCH_NO;
976 goto cleanup; /* Could be a complex constant */
977 }
978
979 if (gfc_match_char (',') != MATCH_YES)
980 {
981 if (n > 2)
982 goto syntax;
983 m = MATCH_NO;
984 goto cleanup;
985 }
986 }
987
988 if (gfc_match_char (')') != MATCH_YES)
989 goto syntax;
990
991 if (check_duplicate_iterator (head, iter.var->symtree->n.sym))
992 {
993 m = MATCH_ERROR;
994 goto cleanup;
995 }
996
997 e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc);
998 e->value.constructor = head;
999
1000 p = gfc_constructor_append_expr (result, e, &gfc_current_locus);
1001 p->iterator = gfc_get_iterator ();
1002 *p->iterator = iter;
1003
1004 return MATCH_YES;
1005
1006 syntax:
1007 gfc_error ("Syntax error in array constructor at %C");
1008 m = MATCH_ERROR;
1009
1010 cleanup:
1011 gfc_constructor_free (head);
1012 gfc_free_iterator (&iter, 0);
1013 gfc_current_locus = old_loc;
1014 return m;
1015 }
1016
1017
1018 /* Match a single element of an array constructor, which can be a
1019 single expression or a list of elements. */
1020
1021 static match
1022 match_array_cons_element (gfc_constructor_base *result)
1023 {
1024 gfc_expr *expr;
1025 match m;
1026
1027 m = match_array_list (result);
1028 if (m != MATCH_NO)
1029 return m;
1030
1031 m = gfc_match_expr (&expr);
1032 if (m != MATCH_YES)
1033 return m;
1034
1035 gfc_constructor_append_expr (result, expr, &gfc_current_locus);
1036 return MATCH_YES;
1037 }
1038
1039
1040 /* Match an array constructor. */
1041
1042 match
1043 gfc_match_array_constructor (gfc_expr **result)
1044 {
1045 gfc_constructor_base head, new_cons;
1046 gfc_undo_change_set changed_syms;
1047 gfc_expr *expr;
1048 gfc_typespec ts;
1049 locus where;
1050 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)
1057 return MATCH_NO;
1058 else
1059 {
1060 if (!gfc_notify_std (GFC_STD_F2003, "[...] "
1061 "style array constructors at %C"))
1062 return MATCH_ERROR;
1063 end_delim = " ]";
1064 }
1065 }
1066 else
1067 end_delim = " /)";
1068
1069 where = gfc_current_locus;
1070 head = new_cons = NULL;
1071 seen_ts = false;
1072
1073 /* Try to match an optional "type-spec ::" */
1074 gfc_clear_ts (&ts);
1075 gfc_new_undo_checkpoint (changed_syms);
1076 if (gfc_match_type_spec (&ts) == MATCH_YES)
1077 {
1078 seen_ts = (gfc_match (" ::") == MATCH_YES);
1079
1080 if (seen_ts)
1081 {
1082 if (!gfc_notify_std (GFC_STD_F2003, "Array constructor "
1083 "including type specification at %C"))
1084 {
1085 gfc_restore_last_undo_checkpoint ();
1086 goto cleanup;
1087 }
1088
1089 if (ts.deferred)
1090 {
1091 gfc_error ("Type-spec at %L cannot contain a deferred "
1092 "type parameter", &where);
1093 gfc_restore_last_undo_checkpoint ();
1094 goto cleanup;
1095 }
1096 }
1097 }
1098
1099 if (seen_ts)
1100 gfc_drop_last_undo_checkpoint ();
1101 else
1102 {
1103 gfc_restore_last_undo_checkpoint ();
1104 gfc_current_locus = where;
1105 }
1106
1107 if (gfc_match (end_delim) == MATCH_YES)
1108 {
1109 if (seen_ts)
1110 goto done;
1111 else
1112 {
1113 gfc_error ("Empty array constructor at %C is not allowed");
1114 goto cleanup;
1115 }
1116 }
1117
1118 for (;;)
1119 {
1120 m = match_array_cons_element (&head);
1121 if (m == MATCH_ERROR)
1122 goto cleanup;
1123 if (m == MATCH_NO)
1124 goto syntax;
1125
1126 if (gfc_match_char (',') == MATCH_NO)
1127 break;
1128 }
1129
1130 if (gfc_match (end_delim) == MATCH_NO)
1131 goto syntax;
1132
1133 done:
1134 /* Size must be calculated at resolution time. */
1135 if (seen_ts)
1136 {
1137 expr = gfc_get_array_expr (ts.type, ts.kind, &where);
1138 expr->ts = ts;
1139 }
1140 else
1141 expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where);
1142
1143 expr->value.constructor = head;
1144 if (expr->ts.u.cl)
1145 expr->ts.u.cl->length_from_typespec = seen_ts;
1146
1147 *result = expr;
1148 return MATCH_YES;
1149
1150 syntax:
1151 gfc_error ("Syntax error in array constructor at %C");
1152
1153 cleanup:
1154 gfc_constructor_free (head);
1155 return MATCH_ERROR;
1156 }
1157
1158
1159
1160 /************** Check array constructors for correctness **************/
1161
1162 /* Given an expression, compare it's type with the type of the current
1163 constructor. Returns nonzero if an error was issued. The
1164 cons_state variable keeps track of whether the type of the
1165 constructor being read or resolved is known to be good, bad or just
1166 starting out. */
1167
1168 static gfc_typespec constructor_ts;
1169 static enum
1170 { CONS_START, CONS_GOOD, CONS_BAD }
1171 cons_state;
1172
1173 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 if (cons_state == CONS_START)
1180 {
1181 if (expr->ts.type == BT_UNKNOWN)
1182 cons_state = CONS_BAD;
1183 else
1184 {
1185 cons_state = CONS_GOOD;
1186 constructor_ts = expr->ts;
1187 }
1188
1189 return 0;
1190 }
1191
1192 if (gfc_compare_types (&constructor_ts, &expr->ts))
1193 return 0;
1194
1195 if (convert)
1196 return gfc_convert_type(expr, &constructor_ts, 1) ? 0 : 1;
1197
1198 gfc_error ("Element in %s array constructor at %L is %s",
1199 gfc_typename (&constructor_ts), &expr->where,
1200 gfc_typename (&expr->ts));
1201
1202 cons_state = CONS_BAD;
1203 return 1;
1204 }
1205
1206
1207 /* Recursive work function for gfc_check_constructor_type(). */
1208
1209 static bool
1210 check_constructor_type (gfc_constructor_base base, bool convert)
1211 {
1212 gfc_constructor *c;
1213 gfc_expr *e;
1214
1215 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1216 {
1217 e = c->expr;
1218
1219 if (e->expr_type == EXPR_ARRAY)
1220 {
1221 if (!check_constructor_type (e->value.constructor, convert))
1222 return false;
1223
1224 continue;
1225 }
1226
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
1239 gfc_check_constructor_type (gfc_expr *e)
1240 {
1241 bool t;
1242
1243 if (e->ts.type != BT_UNKNOWN)
1244 {
1245 cons_state = CONS_GOOD;
1246 constructor_ts = e->ts;
1247 }
1248 else
1249 {
1250 cons_state = CONS_START;
1251 gfc_clear_ts (&constructor_ts);
1252 }
1253
1254 /* If e->ts.type != BT_UNKNOWN, the array constructor included a
1255 typespec, and we will now convert the values on the fly. */
1256 t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN);
1257 if (t && e->ts.type == BT_UNKNOWN)
1258 e->ts = constructor_ts;
1259
1260 return t;
1261 }
1262
1263
1264
1265 typedef struct cons_stack
1266 {
1267 gfc_iterator *iterator;
1268 struct cons_stack *previous;
1269 }
1270 cons_stack;
1271
1272 static cons_stack *base;
1273
1274 static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *));
1275
1276 /* Check an EXPR_VARIABLE expression in a constructor to make sure
1277 that that variable is an iteration variables. */
1278
1279 bool
1280 gfc_check_iter_variable (gfc_expr *expr)
1281 {
1282 gfc_symbol *sym;
1283 cons_stack *c;
1284
1285 sym = expr->symtree->n.sym;
1286
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 /* Recursive work function for gfc_check_constructor(). This amounts
1296 to calling the check function for each expression in the
1297 constructor, giving variables with the names of iterators a pass. */
1298
1299 static bool
1300 check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *))
1301 {
1302 cons_stack element;
1303 gfc_expr *e;
1304 bool t;
1305 gfc_constructor *c;
1306
1307 for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c))
1308 {
1309 e = c->expr;
1310
1311 if (e->expr_type != EXPR_ARRAY)
1312 {
1313 if (!(*check_function)(e))
1314 return false;
1315 continue;
1316 }
1317
1318 element.previous = base;
1319 element.iterator = c->iterator;
1320
1321 base = &element;
1322 t = check_constructor (e->value.constructor, check_function);
1323 base = element.previous;
1324
1325 if (!t)
1326 return false;
1327 }
1328
1329 /* Nothing went wrong, so all OK. */
1330 return true;
1331 }
1332
1333
1334 /* Checks a constructor to see if it is a particular kind of
1335 expression -- specification, restricted, or initialization as
1336 determined by the check_function. */
1337
1338 bool
1339 gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *))
1340 {
1341 cons_stack *base_save;
1342 bool t;
1343
1344 base_save = base;
1345 base = NULL;
1346
1347 t = check_constructor (expr->value.constructor, check_function);
1348 base = base_save;
1349
1350 return t;
1351 }
1352
1353
1354
1355 /**************** Simplification of array constructors ****************/
1356
1357 iterator_stack *iter_stack;
1358
1359 typedef struct
1360 {
1361 gfc_constructor_base base;
1362 int extract_count, extract_n;
1363 gfc_expr *extracted;
1364 mpz_t *count;
1365
1366 mpz_t *offset;
1367 gfc_component *component;
1368 mpz_t *repeat;
1369
1370 bool (*expand_work_function) (gfc_expr *);
1371 }
1372 expand_info;
1373
1374 static expand_info current_expand;
1375
1376 static bool expand_constructor (gfc_constructor_base);
1377
1378
1379 /* Work function that counts the number of elements present in a
1380 constructor. */
1381
1382 static bool
1383 count_elements (gfc_expr *e)
1384 {
1385 mpz_t result;
1386
1387 if (e->rank == 0)
1388 mpz_add_ui (*current_expand.count, *current_expand.count, 1);
1389 else
1390 {
1391 if (!gfc_array_size (e, &result))
1392 {
1393 gfc_free_expr (e);
1394 return false;
1395 }
1396
1397 mpz_add (*current_expand.count, *current_expand.count, result);
1398 mpz_clear (result);
1399 }
1400
1401 gfc_free_expr (e);
1402 return true;
1403 }
1404
1405
1406 /* Work function that extracts a particular element from an array
1407 constructor, freeing the rest. */
1408
1409 static bool
1410 extract_element (gfc_expr *e)
1411 {
1412 if (e->rank != 0)
1413 { /* Something unextractable */
1414 gfc_free_expr (e);
1415 return false;
1416 }
1417
1418 if (current_expand.extract_count == current_expand.extract_n)
1419 current_expand.extracted = e;
1420 else
1421 gfc_free_expr (e);
1422
1423 current_expand.extract_count++;
1424
1425 return true;
1426 }
1427
1428
1429 /* Work function that constructs a new constructor out of the old one,
1430 stringing new elements together. */
1431
1432 static bool
1433 expand (gfc_expr *e)
1434 {
1435 gfc_constructor *c = gfc_constructor_append_expr (&current_expand.base,
1436 e, &e->where);
1437
1438 c->n.component = current_expand.component;
1439 return true;
1440 }
1441
1442
1443 /* Given an initialization expression that is a variable reference,
1444 substitute the current value of the iteration variable. */
1445
1446 void
1447 gfc_simplify_iterator_var (gfc_expr *e)
1448 {
1449 iterator_stack *p;
1450
1451 for (p = iter_stack; p; p = p->prev)
1452 if (e->symtree == p->variable)
1453 break;
1454
1455 if (p == NULL)
1456 return; /* Variable not found */
1457
1458 gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
1459
1460 mpz_set (e->value.integer, p->value);
1461
1462 return;
1463 }
1464
1465
1466 /* Expand an expression with that is inside of a constructor,
1467 recursing into other constructors if present. */
1468
1469 static bool
1470 expand_expr (gfc_expr *e)
1471 {
1472 if (e->expr_type == EXPR_ARRAY)
1473 return expand_constructor (e->value.constructor);
1474
1475 e = gfc_copy_expr (e);
1476
1477 if (!gfc_simplify_expr (e, 1))
1478 {
1479 gfc_free_expr (e);
1480 return false;
1481 }
1482
1483 return current_expand.expand_work_function (e);
1484 }
1485
1486
1487 static bool
1488 expand_iterator (gfc_constructor *c)
1489 {
1490 gfc_expr *start, *end, *step;
1491 iterator_stack frame;
1492 mpz_t trip;
1493 bool t;
1494
1495 end = step = NULL;
1496
1497 t = false;
1498
1499 mpz_init (trip);
1500 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 goto cleanup;
1506
1507 if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER)
1508 goto cleanup;
1509
1510 end = gfc_copy_expr (c->iterator->end);
1511 if (!gfc_simplify_expr (end, 1))
1512 goto cleanup;
1513
1514 if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER)
1515 goto cleanup;
1516
1517 step = gfc_copy_expr (c->iterator->step);
1518 if (!gfc_simplify_expr (step, 1))
1519 goto cleanup;
1520
1521 if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER)
1522 goto cleanup;
1523
1524 if (mpz_sgn (step->value.integer) == 0)
1525 {
1526 gfc_error ("Iterator step at %L cannot be zero", &step->where);
1527 goto cleanup;
1528 }
1529
1530 /* Calculate the trip count of the loop. */
1531 mpz_sub (trip, end->value.integer, start->value.integer);
1532 mpz_add (trip, trip, step->value.integer);
1533 mpz_tdiv_q (trip, trip, step->value.integer);
1534
1535 mpz_set (frame.value, start->value.integer);
1536
1537 frame.prev = iter_stack;
1538 frame.variable = c->iterator->var->symtree;
1539 iter_stack = &frame;
1540
1541 while (mpz_sgn (trip) > 0)
1542 {
1543 if (!expand_expr (c->expr))
1544 goto cleanup;
1545
1546 mpz_add (frame.value, frame.value, step->value.integer);
1547 mpz_sub_ui (trip, trip, 1);
1548 }
1549
1550 t = true;
1551
1552 cleanup:
1553 gfc_free_expr (start);
1554 gfc_free_expr (end);
1555 gfc_free_expr (step);
1556
1557 mpz_clear (trip);
1558 mpz_clear (frame.value);
1559
1560 iter_stack = frame.prev;
1561
1562 return t;
1563 }
1564
1565
1566 /* Expand a constructor into constant constructors without any
1567 iterators, calling the work function for each of the expanded
1568 expressions. The work function needs to either save or free the
1569 passed expression. */
1570
1571 static bool
1572 expand_constructor (gfc_constructor_base base)
1573 {
1574 gfc_constructor *c;
1575 gfc_expr *e;
1576
1577 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c))
1578 {
1579 if (c->iterator != NULL)
1580 {
1581 if (!expand_iterator (c))
1582 return false;
1583 continue;
1584 }
1585
1586 e = c->expr;
1587
1588 if (e->expr_type == EXPR_ARRAY)
1589 {
1590 if (!expand_constructor (e->value.constructor))
1591 return false;
1592
1593 continue;
1594 }
1595
1596 e = gfc_copy_expr (e);
1597 if (!gfc_simplify_expr (e, 1))
1598 {
1599 gfc_free_expr (e);
1600 return false;
1601 }
1602 current_expand.offset = &c->offset;
1603 current_expand.repeat = &c->repeat;
1604 current_expand.component = c->n.component;
1605 if (!current_expand.expand_work_function(e))
1606 return false;
1607 }
1608 return true;
1609 }
1610
1611
1612 /* Given an array expression and an element number (starting at zero),
1613 return a pointer to the array element. NULL is returned if the
1614 size of the array has been exceeded. The expression node returned
1615 remains a part of the array and should not be freed. Access is not
1616 efficient at all, but this is another place where things do not
1617 have to be particularly fast. */
1618
1619 static gfc_expr *
1620 gfc_get_array_element (gfc_expr *array, int element)
1621 {
1622 expand_info expand_save;
1623 gfc_expr *e;
1624 bool rc;
1625
1626 expand_save = current_expand;
1627 current_expand.extract_n = element;
1628 current_expand.expand_work_function = extract_element;
1629 current_expand.extracted = NULL;
1630 current_expand.extract_count = 0;
1631
1632 iter_stack = NULL;
1633
1634 rc = expand_constructor (array->value.constructor);
1635 e = current_expand.extracted;
1636 current_expand = expand_save;
1637
1638 if (!rc)
1639 return NULL;
1640
1641 return e;
1642 }
1643
1644
1645 /* Top level subroutine for expanding constructors. We only expand
1646 constructor if they are small enough. */
1647
1648 bool
1649 gfc_expand_constructor (gfc_expr *e, bool fatal)
1650 {
1651 expand_info expand_save;
1652 gfc_expr *f;
1653 bool rc;
1654
1655 /* If we can successfully get an array element at the max array size then
1656 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 if (f != NULL)
1659 {
1660 gfc_free_expr (f);
1661 if (fatal)
1662 {
1663 gfc_error ("The number of elements in the array constructor "
1664 "at %L requires an increase of the allowed %d "
1665 "upper limit. See -fmax-array-constructor "
1666 "option", &e->where,
1667 gfc_option.flag_max_array_constructor);
1668 return false;
1669 }
1670 return true;
1671 }
1672
1673 /* We now know the array is not too big so go ahead and try to expand it. */
1674 expand_save = current_expand;
1675 current_expand.base = NULL;
1676
1677 iter_stack = NULL;
1678
1679 current_expand.expand_work_function = expand;
1680
1681 if (!expand_constructor (e->value.constructor))
1682 {
1683 gfc_constructor_free (current_expand.base);
1684 rc = false;
1685 goto done;
1686 }
1687
1688 gfc_constructor_free (e->value.constructor);
1689 e->value.constructor = current_expand.base;
1690
1691 rc = true;
1692
1693 done:
1694 current_expand = expand_save;
1695
1696 return rc;
1697 }
1698
1699
1700 /* Work function for checking that an element of a constructor is a
1701 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 int rv;
1708
1709 rv = gfc_is_constant_expr (e);
1710 gfc_free_expr (e);
1711
1712 return rv ? true : false;
1713 }
1714
1715
1716 /* Given an array constructor, determine if the constructor is
1717 constant or not by expanding it and making sure that all elements
1718 are constants. This is a bit of a hack since something like (/ (i,
1719 i=1,100000000) /) will take a while as* opposed to a more clever
1720 function that traverses the expression tree. FIXME. */
1721
1722 int
1723 gfc_constant_ac (gfc_expr *e)
1724 {
1725 expand_info expand_save;
1726 bool rc;
1727
1728 iter_stack = NULL;
1729 expand_save = current_expand;
1730 current_expand.expand_work_function = is_constant_element;
1731
1732 rc = expand_constructor (e->value.constructor);
1733
1734 current_expand = expand_save;
1735 if (!rc)
1736 return 0;
1737
1738 return 1;
1739 }
1740
1741
1742 /* Returns nonzero if an array constructor has been completely
1743 expanded (no iterators) and zero if iterators are present. */
1744
1745 int
1746 gfc_expanded_ac (gfc_expr *e)
1747 {
1748 gfc_constructor *c;
1749
1750 if (e->expr_type == EXPR_ARRAY)
1751 for (c = gfc_constructor_first (e->value.constructor);
1752 c; c = gfc_constructor_next (c))
1753 if (c->iterator != NULL || !gfc_expanded_ac (c->expr))
1754 return 0;
1755
1756 return 1;
1757 }
1758
1759
1760 /*************** Type resolution of array constructors ***************/
1761
1762
1763 /* The symbol expr_is_sought_symbol_ref will try to find. */
1764 static const gfc_symbol *sought_symbol = NULL;
1765
1766
1767 /* Tells whether the expression E is a variable reference to the symbol
1768 in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE
1769 accordingly.
1770 To be used with gfc_expr_walker: if a reference is found we don't need
1771 to look further so we return 1 to skip any further walk. */
1772
1773 static int
1774 expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
1775 void *where)
1776 {
1777 gfc_expr *expr = *e;
1778 locus *sym_loc = (locus *)where;
1779
1780 if (expr->expr_type == EXPR_VARIABLE
1781 && expr->symtree->n.sym == sought_symbol)
1782 {
1783 *sym_loc = expr->where;
1784 return 1;
1785 }
1786
1787 return 0;
1788 }
1789
1790
1791 /* Tells whether the expression EXPR contains a reference to the symbol
1792 SYM and in that case sets the position SYM_LOC where the reference is. */
1793
1794 static bool
1795 find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc)
1796 {
1797 int ret;
1798
1799 sought_symbol = sym;
1800 ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc);
1801 sought_symbol = NULL;
1802 return ret;
1803 }
1804
1805
1806 /* Recursive array list resolution function. All of the elements must
1807 be of the same type. */
1808
1809 static bool
1810 resolve_array_list (gfc_constructor_base base)
1811 {
1812 bool t;
1813 gfc_constructor *c;
1814 gfc_iterator *iter;
1815
1816 t = true;
1817
1818 for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
1819 {
1820 iter = c->iterator;
1821 if (iter != NULL)
1822 {
1823 gfc_symbol *iter_var;
1824 locus iter_var_loc;
1825
1826 if (!gfc_resolve_iterator (iter, false, true))
1827 t = false;
1828
1829 /* Check for bounds referencing the iterator variable. */
1830 gcc_assert (iter->var->expr_type == EXPR_VARIABLE);
1831 iter_var = iter->var->symtree->n.sym;
1832 if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc))
1833 {
1834 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial "
1835 "expression references control variable "
1836 "at %L", &iter_var_loc))
1837 t = false;
1838 }
1839 if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc))
1840 {
1841 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final "
1842 "expression references control variable "
1843 "at %L", &iter_var_loc))
1844 t = false;
1845 }
1846 if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc))
1847 {
1848 if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step "
1849 "expression references control variable "
1850 "at %L", &iter_var_loc))
1851 t = false;
1852 }
1853 }
1854
1855 if (!gfc_resolve_expr (c->expr))
1856 t = false;
1857
1858 if (UNLIMITED_POLY (c->expr))
1859 {
1860 gfc_error ("Array constructor value at %L shall not be unlimited "
1861 "polymorphic [F2008: C4106]", &c->expr->where);
1862 t = false;
1863 }
1864 }
1865
1866 return t;
1867 }
1868
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 all elements are of compile-time known length, emit an error as this is
1872 invalid. */
1873
1874 bool
1875 gfc_resolve_character_array_constructor (gfc_expr *expr)
1876 {
1877 gfc_constructor *p;
1878 int found_length;
1879
1880 gcc_assert (expr->expr_type == EXPR_ARRAY);
1881 gcc_assert (expr->ts.type == BT_CHARACTER);
1882
1883 if (expr->ts.u.cl == NULL)
1884 {
1885 for (p = gfc_constructor_first (expr->value.constructor);
1886 p; p = gfc_constructor_next (p))
1887 if (p->expr->ts.u.cl != NULL)
1888 {
1889 /* Ensure that if there is a char_len around that it is
1890 used; otherwise the middle-end confuses them! */
1891 expr->ts.u.cl = p->expr->ts.u.cl;
1892 goto got_charlen;
1893 }
1894
1895 expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1896 }
1897
1898 got_charlen:
1899
1900 found_length = -1;
1901
1902 if (expr->ts.u.cl->length == NULL)
1903 {
1904 /* Check that all constant string elements have the same length until
1905 we reach the end or find a variable-length one. */
1906
1907 for (p = gfc_constructor_first (expr->value.constructor);
1908 p; p = gfc_constructor_next (p))
1909 {
1910 int current_length = -1;
1911 gfc_ref *ref;
1912 for (ref = p->expr->ref; ref; ref = ref->next)
1913 if (ref->type == REF_SUBSTRING
1914 && ref->u.ss.start->expr_type == EXPR_CONSTANT
1915 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
1916 break;
1917
1918 if (p->expr->expr_type == EXPR_CONSTANT)
1919 current_length = p->expr->value.character.length;
1920 else if (ref)
1921 {
1922 long j;
1923 j = mpz_get_ui (ref->u.ss.end->value.integer)
1924 - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
1925 current_length = (int) j;
1926 }
1927 else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length
1928 && p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1929 {
1930 long j;
1931 j = mpz_get_si (p->expr->ts.u.cl->length->value.integer);
1932 current_length = (int) j;
1933 }
1934 else
1935 return true;
1936
1937 gcc_assert (current_length != -1);
1938
1939 if (found_length == -1)
1940 found_length = current_length;
1941 else if (found_length != current_length)
1942 {
1943 gfc_error ("Different CHARACTER lengths (%d/%d) in array"
1944 " constructor at %L", found_length, current_length,
1945 &p->expr->where);
1946 return false;
1947 }
1948
1949 gcc_assert (found_length == current_length);
1950 }
1951
1952 gcc_assert (found_length != -1);
1953
1954 /* Update the character length of the array constructor. */
1955 expr->ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
1956 NULL, found_length);
1957 }
1958 else
1959 {
1960 /* We've got a character length specified. It should be an integer,
1961 otherwise an error is signalled elsewhere. */
1962 gcc_assert (expr->ts.u.cl->length);
1963
1964 /* If we've got a constant character length, pad according to this.
1965 gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets
1966 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 length. This is ok inside this conditional, as in the case above
1971 (without typespec) all elements are verified to have the same length
1972 anyway. */
1973 if (found_length != -1)
1974 for (p = gfc_constructor_first (expr->value.constructor);
1975 p; p = gfc_constructor_next (p))
1976 if (p->expr->expr_type == EXPR_CONSTANT)
1977 {
1978 gfc_expr *cl = NULL;
1979 int current_length = -1;
1980 bool has_ts;
1981
1982 if (p->expr->ts.u.cl && p->expr->ts.u.cl->length)
1983 {
1984 cl = p->expr->ts.u.cl->length;
1985 gfc_extract_int (cl, &current_length);
1986 }
1987
1988 /* If gfc_extract_int above set current_length, we implicitly
1989 know the type is BT_INTEGER and it's EXPR_CONSTANT. */
1990
1991 has_ts = expr->ts.u.cl->length_from_typespec;
1992
1993 if (! cl
1994 || (current_length != -1 && current_length != found_length))
1995 gfc_set_constant_character_len (found_length, p->expr,
1996 has_ts ? -1 : found_length);
1997 }
1998 }
1999
2000 return true;
2001 }
2002
2003
2004 /* Resolve all of the expressions in an array list. */
2005
2006 bool
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 /* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after
2016 the call to this function, so we don't need to call it here; if it was
2017 called twice, an error message there would be duplicated. */
2018
2019 return t;
2020 }
2021
2022
2023 /* Copy an iterator structure. */
2024
2025 gfc_iterator *
2026 gfc_copy_iterator (gfc_iterator *src)
2027 {
2028 gfc_iterator *dest;
2029
2030 if (src == NULL)
2031 return NULL;
2032
2033 dest = gfc_get_iterator ();
2034
2035 dest->var = gfc_copy_expr (src->var);
2036 dest->start = gfc_copy_expr (src->start);
2037 dest->end = gfc_copy_expr (src->end);
2038 dest->step = gfc_copy_expr (src->step);
2039
2040 return dest;
2041 }
2042
2043
2044 /********* Subroutines for determining the size of an array *********/
2045
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 /* Get the size of single dimension of an array specification. The
2052 array is guaranteed to be one dimensional. */
2053
2054 bool
2055 spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result)
2056 {
2057 if (as == NULL)
2058 return false;
2059
2060 if (dimen < 0 || dimen > as->rank - 1)
2061 gfc_internal_error ("spec_dimen_size(): Bad dimension");
2062
2063 if (as->type != AS_EXPLICIT
2064 || as->lower[dimen]->expr_type != EXPR_CONSTANT
2065 || as->upper[dimen]->expr_type != EXPR_CONSTANT
2066 || as->lower[dimen]->ts.type != BT_INTEGER
2067 || as->upper[dimen]->ts.type != BT_INTEGER)
2068 return false;
2069
2070 mpz_init (*result);
2071
2072 mpz_sub (*result, as->upper[dimen]->value.integer,
2073 as->lower[dimen]->value.integer);
2074
2075 mpz_add_ui (*result, *result, 1);
2076
2077 return true;
2078 }
2079
2080
2081 bool
2082 spec_size (gfc_array_spec *as, mpz_t *result)
2083 {
2084 mpz_t size;
2085 int d;
2086
2087 if (!as || as->type == AS_ASSUMED_RANK)
2088 return false;
2089
2090 mpz_init_set_ui (*result, 1);
2091
2092 for (d = 0; d < as->rank; d++)
2093 {
2094 if (!spec_dimen_size (as, d, &size))
2095 {
2096 mpz_clear (*result);
2097 return false;
2098 }
2099
2100 mpz_mul (*result, *result, size);
2101 mpz_clear (size);
2102 }
2103
2104 return true;
2105 }
2106
2107
2108 /* Get the number of elements in an array section. Optionally, also supply
2109 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 mpz_t diff;
2116 bool t;
2117
2118 if (dimen < 0 || ar == NULL || dimen > ar->dimen - 1)
2119 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension");
2120
2121 switch (ar->dimen_type[dimen])
2122 {
2123 case DIMEN_ELEMENT:
2124 mpz_init (*result);
2125 mpz_set_ui (*result, 1);
2126 t = true;
2127 break;
2128
2129 case DIMEN_VECTOR:
2130 t = gfc_array_size (ar->start[dimen], result); /* Recurse! */
2131 break;
2132
2133 case DIMEN_RANGE:
2134
2135 mpz_init (stride);
2136
2137 if (ar->stride[dimen] == NULL)
2138 mpz_set_ui (stride, 1);
2139 else
2140 {
2141 if (ar->stride[dimen]->expr_type != EXPR_CONSTANT)
2142 {
2143 mpz_clear (stride);
2144 return false;
2145 }
2146 mpz_set (stride, ar->stride[dimen]->value.integer);
2147 }
2148
2149 /* Calculate the number of elements via gfc_dep_differce, but only if
2150 start and end are both supplied in the reference or the array spec.
2151 This is to guard against strange but valid code like
2152
2153 subroutine foo(a,n)
2154 real a(1:n)
2155 n = 3
2156 print *,size(a(n-1:))
2157
2158 where the user changes the value of a variable. If we have to
2159 determine end as well, we cannot do this using gfc_dep_difference.
2160 Fall back to the constants-only code then. */
2161
2162 if (end == NULL)
2163 {
2164 bool use_dep;
2165
2166 use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen],
2167 &diff);
2168 if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL)
2169 use_dep = gfc_dep_difference (ar->as->upper[dimen],
2170 ar->as->lower[dimen], &diff);
2171
2172 if (use_dep)
2173 {
2174 mpz_init (*result);
2175 mpz_add (*result, diff, stride);
2176 mpz_div (*result, *result, stride);
2177 if (mpz_cmp_ui (*result, 0) < 0)
2178 mpz_set_ui (*result, 0);
2179
2180 mpz_clear (stride);
2181 mpz_clear (diff);
2182 return true;
2183 }
2184
2185 }
2186
2187 /* Constant-only code here, which covers more cases
2188 like a(:4) etc. */
2189 mpz_init (upper);
2190 mpz_init (lower);
2191 t = false;
2192
2193 if (ar->start[dimen] == NULL)
2194 {
2195 if (ar->as->lower[dimen] == NULL
2196 || ar->as->lower[dimen]->expr_type != EXPR_CONSTANT)
2197 goto cleanup;
2198 mpz_set (lower, ar->as->lower[dimen]->value.integer);
2199 }
2200 else
2201 {
2202 if (ar->start[dimen]->expr_type != EXPR_CONSTANT)
2203 goto cleanup;
2204 mpz_set (lower, ar->start[dimen]->value.integer);
2205 }
2206
2207 if (ar->end[dimen] == NULL)
2208 {
2209 if (ar->as->upper[dimen] == NULL
2210 || ar->as->upper[dimen]->expr_type != EXPR_CONSTANT)
2211 goto cleanup;
2212 mpz_set (upper, ar->as->upper[dimen]->value.integer);
2213 }
2214 else
2215 {
2216 if (ar->end[dimen]->expr_type != EXPR_CONSTANT)
2217 goto cleanup;
2218 mpz_set (upper, ar->end[dimen]->value.integer);
2219 }
2220
2221 mpz_init (*result);
2222 mpz_sub (*result, upper, lower);
2223 mpz_add (*result, *result, stride);
2224 mpz_div (*result, *result, stride);
2225
2226 /* Zero stride caught earlier. */
2227 if (mpz_cmp_ui (*result, 0) < 0)
2228 mpz_set_ui (*result, 0);
2229 t = true;
2230
2231 if (end)
2232 {
2233 mpz_init (*end);
2234
2235 mpz_sub_ui (*end, *result, 1UL);
2236 mpz_mul (*end, *end, stride);
2237 mpz_add (*end, *end, lower);
2238 }
2239
2240 cleanup:
2241 mpz_clear (upper);
2242 mpz_clear (lower);
2243 mpz_clear (stride);
2244 return t;
2245
2246 default:
2247 gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type");
2248 }
2249
2250 return t;
2251 }
2252
2253
2254 static bool
2255 ref_size (gfc_array_ref *ar, mpz_t *result)
2256 {
2257 mpz_t size;
2258 int d;
2259
2260 mpz_init_set_ui (*result, 1);
2261
2262 for (d = 0; d < ar->dimen; d++)
2263 {
2264 if (!gfc_ref_dimen_size (ar, d, &size, NULL))
2265 {
2266 mpz_clear (*result);
2267 return false;
2268 }
2269
2270 mpz_mul (*result, *result, size);
2271 mpz_clear (size);
2272 }
2273
2274 return true;
2275 }
2276
2277
2278 /* Given an array expression and a dimension, figure out how many
2279 elements it has along that dimension. Returns true if we were
2280 able to return a result in the 'result' variable, false
2281 otherwise. */
2282
2283 bool
2284 gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result)
2285 {
2286 gfc_ref *ref;
2287 int i;
2288
2289 gcc_assert (array != NULL);
2290
2291 if (array->ts.type == BT_CLASS)
2292 return false;
2293
2294 if (array->rank == -1)
2295 return false;
2296
2297 if (dimen < 0 || dimen > array->rank - 1)
2298 gfc_internal_error ("gfc_array_dimen_size(): Bad dimension");
2299
2300 switch (array->expr_type)
2301 {
2302 case EXPR_VARIABLE:
2303 case EXPR_FUNCTION:
2304 for (ref = array->ref; ref; ref = ref->next)
2305 {
2306 if (ref->type != REF_ARRAY)
2307 continue;
2308
2309 if (ref->u.ar.type == AR_FULL)
2310 return spec_dimen_size (ref->u.ar.as, dimen, result);
2311
2312 if (ref->u.ar.type == AR_SECTION)
2313 {
2314 for (i = 0; dimen >= 0; i++)
2315 if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
2316 dimen--;
2317
2318 return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL);
2319 }
2320 }
2321
2322 if (array->shape && array->shape[dimen])
2323 {
2324 mpz_init_set (*result, array->shape[dimen]);
2325 return true;
2326 }
2327
2328 if (array->symtree->n.sym->attr.generic
2329 && array->value.function.esym != NULL)
2330 {
2331 if (!spec_dimen_size (array->value.function.esym->as, dimen, result))
2332 return false;
2333 }
2334 else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result))
2335 return false;
2336
2337 break;
2338
2339 case EXPR_ARRAY:
2340 if (array->shape == NULL) {
2341 /* Expressions with rank > 1 should have "shape" properly set */
2342 if ( array->rank != 1 )
2343 gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr");
2344 return gfc_array_size(array, result);
2345 }
2346
2347 /* Fall through */
2348 default:
2349 if (array->shape == NULL)
2350 return false;
2351
2352 mpz_init_set (*result, array->shape[dimen]);
2353
2354 break;
2355 }
2356
2357 return true;
2358 }
2359
2360
2361 /* Given an array expression, figure out how many elements are in the
2362 array. Returns true if this is possible, and sets the 'result'
2363 variable. Otherwise returns false. */
2364
2365 bool
2366 gfc_array_size (gfc_expr *array, mpz_t *result)
2367 {
2368 expand_info expand_save;
2369 gfc_ref *ref;
2370 int i;
2371 bool t;
2372
2373 if (array->ts.type == BT_CLASS)
2374 return false;
2375
2376 switch (array->expr_type)
2377 {
2378 case EXPR_ARRAY:
2379 gfc_push_suppress_errors ();
2380
2381 expand_save = current_expand;
2382
2383 current_expand.count = result;
2384 mpz_init_set_ui (*result, 0);
2385
2386 current_expand.expand_work_function = count_elements;
2387 iter_stack = NULL;
2388
2389 t = expand_constructor (array->value.constructor);
2390
2391 gfc_pop_suppress_errors ();
2392
2393 if (!t)
2394 mpz_clear (*result);
2395 current_expand = expand_save;
2396 return t;
2397
2398 case EXPR_VARIABLE:
2399 for (ref = array->ref; ref; ref = ref->next)
2400 {
2401 if (ref->type != REF_ARRAY)
2402 continue;
2403
2404 if (ref->u.ar.type == AR_FULL)
2405 return spec_size (ref->u.ar.as, result);
2406
2407 if (ref->u.ar.type == AR_SECTION)
2408 return ref_size (&ref->u.ar, result);
2409 }
2410
2411 return spec_size (array->symtree->n.sym->as, result);
2412
2413
2414 default:
2415 if (array->rank == 0 || array->shape == NULL)
2416 return false;
2417
2418 mpz_init_set_ui (*result, 1);
2419
2420 for (i = 0; i < array->rank; i++)
2421 mpz_mul (*result, *result, array->shape[i]);
2422
2423 break;
2424 }
2425
2426 return true;
2427 }
2428
2429
2430 /* Given an array reference, return the shape of the reference in an
2431 array of mpz_t integers. */
2432
2433 bool
2434 gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape)
2435 {
2436 int d;
2437 int i;
2438
2439 d = 0;
2440
2441 switch (ar->type)
2442 {
2443 case AR_FULL:
2444 for (; d < ar->as->rank; d++)
2445 if (!spec_dimen_size (ar->as, d, &shape[d]))
2446 goto cleanup;
2447
2448 return true;
2449
2450 case AR_SECTION:
2451 for (i = 0; i < ar->dimen; i++)
2452 {
2453 if (ar->dimen_type[i] != DIMEN_ELEMENT)
2454 {
2455 if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL))
2456 goto cleanup;
2457 d++;
2458 }
2459 }
2460
2461 return true;
2462
2463 default:
2464 break;
2465 }
2466
2467 cleanup:
2468 gfc_clear_shape (shape, d);
2469 return false;
2470 }
2471
2472
2473 /* Given an array expression, find the array reference structure that
2474 characterizes the reference. */
2475
2476 gfc_array_ref *
2477 gfc_find_array_ref (gfc_expr *e)
2478 {
2479 gfc_ref *ref;
2480
2481 for (ref = e->ref; ref; ref = ref->next)
2482 if (ref->type == REF_ARRAY
2483 && (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION))
2484 break;
2485
2486 if (ref == NULL)
2487 gfc_internal_error ("gfc_find_array_ref(): No ref found");
2488
2489 return &ref->u.ar;
2490 }
2491
2492
2493 /* Find out if an array shape is known at compile time. */
2494
2495 int
2496 gfc_is_compile_time_shape (gfc_array_spec *as)
2497 {
2498 int i;
2499
2500 if (as->type != AS_EXPLICIT)
2501 return 0;
2502
2503 for (i = 0; i < as->rank; i++)
2504 if (!gfc_is_constant_expr (as->lower[i])
2505 || !gfc_is_constant_expr (as->upper[i]))
2506 return 0;
2507
2508 return 1;
2509 }