OSDN Git Service

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