OSDN Git Service

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