OSDN Git Service

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