OSDN Git Service

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