OSDN Git Service

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