OSDN Git Service

fortran/
[pf3gnuchains/gcc-fork.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various stuctures.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330,Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "arith.h"  /* For gfc_compare_expr().  */
27
28
29 /* Stack to push the current if we descend into a block during
30    resolution.  See resolve_branch() and resolve_code().  */
31
32 typedef struct code_stack
33 {
34   struct gfc_code *head, *current;
35   struct code_stack *prev;
36 }
37 code_stack;
38
39 static code_stack *cs_base = NULL;
40
41
42 /* Nonzero if we're inside a FORALL block */
43
44 static int forall_flag;
45
46 /* Resolve types of formal argument lists.  These have to be done early so that
47    the formal argument lists of module procedures can be copied to the
48    containing module before the individual procedures are resolved
49    individually.  We also resolve argument lists of procedures in interface
50    blocks because they are self-contained scoping units.
51
52    Since a dummy argument cannot be a non-dummy procedure, the only
53    resort left for untyped names are the IMPLICIT types.  */
54
55 static void
56 resolve_formal_arglist (gfc_symbol * proc)
57 {
58   gfc_formal_arglist *f;
59   gfc_symbol *sym;
60   int i;
61
62   /* TODO: Procedures whose return character length parameter is not constant
63      or assumed must also have explicit interfaces.  */
64   if (proc->result != NULL)
65     sym = proc->result;
66   else
67     sym = proc;
68
69   if (gfc_elemental (proc)
70       || sym->attr.pointer || sym->attr.allocatable
71       || (sym->as && sym->as->rank > 0))
72     proc->attr.always_explicit = 1;
73
74   for (f = proc->formal; f; f = f->next)
75     {
76       sym = f->sym;
77
78       if (sym == NULL)
79         {
80           /* Alternate return placeholder.  */
81           if (gfc_elemental (proc))
82             gfc_error ("Alternate return specifier in elemental subroutine "
83                        "'%s' at %L is not allowed", proc->name,
84                        &proc->declared_at);
85           if (proc->attr.function)
86             gfc_error ("Alternate return specifier in function "
87                        "'%s' at %L is not allowed", proc->name,
88                        &proc->declared_at);
89           continue;
90         }
91
92       if (sym->attr.if_source != IFSRC_UNKNOWN)
93         resolve_formal_arglist (sym);
94
95       if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
96         {
97           if (gfc_pure (proc) && !gfc_pure (sym))
98             {
99               gfc_error
100                 ("Dummy procedure '%s' of PURE procedure at %L must also "
101                  "be PURE", sym->name, &sym->declared_at);
102               continue;
103             }
104
105           if (gfc_elemental (proc))
106             {
107               gfc_error
108                 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
109                  &sym->declared_at);
110               continue;
111             }
112
113           continue;
114         }
115
116       if (sym->ts.type == BT_UNKNOWN)
117         {
118           if (!sym->attr.function || sym->result == sym)
119             gfc_set_default_type (sym, 1, sym->ns);
120           else
121             {
122               /* Set the type of the RESULT, then copy.  */
123               if (sym->result->ts.type == BT_UNKNOWN)
124                 gfc_set_default_type (sym->result, 1, sym->result->ns);
125
126               sym->ts = sym->result->ts;
127               if (sym->as == NULL)
128                 sym->as = gfc_copy_array_spec (sym->result->as);
129             }
130         }
131
132       gfc_resolve_array_spec (sym->as, 0);
133
134       /* We can't tell if an array with dimension (:) is assumed or deferred
135          shape until we know if it has the pointer or allocatable attributes.
136       */
137       if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
138           && !(sym->attr.pointer || sym->attr.allocatable))
139         {
140           sym->as->type = AS_ASSUMED_SHAPE;
141           for (i = 0; i < sym->as->rank; i++)
142             sym->as->lower[i] = gfc_int_expr (1);
143         }
144
145       if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
146           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
147           || sym->attr.optional)
148         proc->attr.always_explicit = 1;
149
150       /* If the flavor is unknown at this point, it has to be a variable.
151          A procedure specification would have already set the type.  */
152
153       if (sym->attr.flavor == FL_UNKNOWN)
154         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
155
156       if (gfc_pure (proc))
157         {
158           if (proc->attr.function && !sym->attr.pointer
159               && sym->attr.flavor != FL_PROCEDURE
160               && sym->attr.intent != INTENT_IN)
161
162             gfc_error ("Argument '%s' of pure function '%s' at %L must be "
163                        "INTENT(IN)", sym->name, proc->name,
164                        &sym->declared_at);
165
166           if (proc->attr.subroutine && !sym->attr.pointer
167               && sym->attr.intent == INTENT_UNKNOWN)
168
169             gfc_error
170               ("Argument '%s' of pure subroutine '%s' at %L must have "
171                "its INTENT specified", sym->name, proc->name,
172                &sym->declared_at);
173         }
174
175
176       if (gfc_elemental (proc))
177         {
178           if (sym->as != NULL)
179             {
180               gfc_error
181                 ("Argument '%s' of elemental procedure at %L must be scalar",
182                  sym->name, &sym->declared_at);
183               continue;
184             }
185
186           if (sym->attr.pointer)
187             {
188               gfc_error
189                 ("Argument '%s' of elemental procedure at %L cannot have "
190                  "the POINTER attribute", sym->name, &sym->declared_at);
191               continue;
192             }
193         }
194
195       /* Each dummy shall be specified to be scalar.  */
196       if (proc->attr.proc == PROC_ST_FUNCTION)
197         {
198           if (sym->as != NULL)
199             {
200               gfc_error
201                 ("Argument '%s' of statement function at %L must be scalar",
202                  sym->name, &sym->declared_at);
203               continue;
204             }
205
206           if (sym->ts.type == BT_CHARACTER)
207             {
208               gfc_charlen *cl = sym->ts.cl;
209               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
210                 {
211                   gfc_error
212                     ("Character-valued argument '%s' of statement function at "
213                      "%L must has constant length",
214                      sym->name, &sym->declared_at);
215                   continue;
216                 }
217             }
218         }
219     }
220 }
221
222
223 /* Work function called when searching for symbols that have argument lists
224    associated with them.  */
225
226 static void
227 find_arglists (gfc_symbol * sym)
228 {
229
230   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
231     return;
232
233   resolve_formal_arglist (sym);
234 }
235
236
237 /* Given a namespace, resolve all formal argument lists within the namespace.
238  */
239
240 static void
241 resolve_formal_arglists (gfc_namespace * ns)
242 {
243
244   if (ns == NULL)
245     return;
246
247   gfc_traverse_ns (ns, find_arglists);
248 }
249
250
251 static void
252 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
253 {
254   try t;
255   
256   /* If this namespace is not a function, ignore it.  */
257   if (! sym
258       || !(sym->attr.function
259            || sym->attr.flavor == FL_VARIABLE))
260     return;
261
262   /* Try to find out of what the return type is.  */
263   if (sym->result != NULL)
264     sym = sym->result;
265
266   if (sym->ts.type == BT_UNKNOWN)
267     {
268       t = gfc_set_default_type (sym, 0, ns);
269
270       if (t == FAILURE)
271         gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
272                     sym->name, &sym->declared_at); /* FIXME */
273     }
274 }
275
276
277 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
278    introduce duplicates.  */
279
280 static void
281 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
282 {
283   gfc_formal_arglist *f, *new_arglist;
284   gfc_symbol *new_sym;
285
286   for (; new_args != NULL; new_args = new_args->next)
287     {
288       new_sym = new_args->sym;
289       /* See if ths arg is already in the formal argument list.  */
290       for (f = proc->formal; f; f = f->next)
291         {
292           if (new_sym == f->sym)
293             break;
294         }
295
296       if (f)
297         continue;
298
299       /* Add a new argument.  Argument order is not important.  */
300       new_arglist = gfc_get_formal_arglist ();
301       new_arglist->sym = new_sym;
302       new_arglist->next = proc->formal;
303       proc->formal  = new_arglist;
304     }
305 }
306
307
308 /* Resolve alternate entry points.  If a symbol has multiple entry points we
309    create a new master symbol for the main routine, and turn the existing
310    symbol into an entry point.  */
311
312 static void
313 resolve_entries (gfc_namespace * ns)
314 {
315   gfc_namespace *old_ns;
316   gfc_code *c;
317   gfc_symbol *proc;
318   gfc_entry_list *el;
319   char name[GFC_MAX_SYMBOL_LEN + 1];
320   static int master_count = 0;
321
322   if (ns->proc_name == NULL)
323     return;
324
325   /* No need to do anything if this procedure doesn't have alternate entry
326      points.  */
327   if (!ns->entries)
328     return;
329
330   /* We may already have resolved alternate entry points.  */
331   if (ns->proc_name->attr.entry_master)
332     return;
333
334   /* If this isn't a procedure something has gone horribly wrong.  */
335   gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
336   
337   /* Remember the current namespace.  */
338   old_ns = gfc_current_ns;
339
340   gfc_current_ns = ns;
341
342   /* Add the main entry point to the list of entry points.  */
343   el = gfc_get_entry_list ();
344   el->sym = ns->proc_name;
345   el->id = 0;
346   el->next = ns->entries;
347   ns->entries = el;
348   ns->proc_name->attr.entry = 1;
349
350   /* Add an entry statement for it.  */
351   c = gfc_get_code ();
352   c->op = EXEC_ENTRY;
353   c->ext.entry = el;
354   c->next = ns->code;
355   ns->code = c;
356
357   /* Create a new symbol for the master function.  */
358   /* Give the internal function a unique name (within this file).
359      Also include the function name so the user has some hope of figuring
360      out what is going on.  */
361   snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
362             master_count++, ns->proc_name->name);
363   name[GFC_MAX_SYMBOL_LEN] = '\0';
364   gfc_get_ha_symbol (name, &proc);
365   gcc_assert (proc != NULL);
366
367   gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
368   if (ns->proc_name->attr.subroutine)
369     gfc_add_subroutine (&proc->attr, proc->name, NULL);
370   else
371     {
372       gfc_add_function (&proc->attr, proc->name, NULL);
373       gfc_internal_error ("TODO: Functions with alternate entry points");
374     }
375   proc->attr.access = ACCESS_PRIVATE;
376   proc->attr.entry_master = 1;
377
378   /* Merge all the entry point arguments.  */
379   for (el = ns->entries; el; el = el->next)
380     merge_argument_lists (proc, el->sym->formal);
381
382   /* Use the master function for the function body.  */
383   ns->proc_name = proc;
384
385   /* Finalize the new symbols.  */
386   gfc_commit_symbols ();
387
388   /* Restore the original namespace.  */
389   gfc_current_ns = old_ns;
390 }
391
392
393 /* Resolve contained function types.  Because contained functions can call one
394    another, they have to be worked out before any of the contained procedures
395    can be resolved.
396
397    The good news is that if a function doesn't already have a type, the only
398    way it can get one is through an IMPLICIT type or a RESULT variable, because
399    by definition contained functions are contained namespace they're contained
400    in, not in a sibling or parent namespace.  */
401
402 static void
403 resolve_contained_functions (gfc_namespace * ns)
404 {
405   gfc_namespace *child;
406   gfc_entry_list *el;
407
408   resolve_formal_arglists (ns);
409
410   for (child = ns->contained; child; child = child->sibling)
411     {
412       /* Resolve alternate entry points first.  */
413       resolve_entries (child); 
414
415       /* Then check function return types.  */
416       resolve_contained_fntype (child->proc_name, child);
417       for (el = child->entries; el; el = el->next)
418         resolve_contained_fntype (el->sym, child);
419     }
420 }
421
422
423 /* Resolve all of the elements of a structure constructor and make sure that
424    the types are correct.  */
425
426 static try
427 resolve_structure_cons (gfc_expr * expr)
428 {
429   gfc_constructor *cons;
430   gfc_component *comp;
431   try t;
432
433   t = SUCCESS;
434   cons = expr->value.constructor;
435   /* A constructor may have references if it is the result of substituting a
436      parameter variable.  In this case we just pull out the component we
437      want.  */
438   if (expr->ref)
439     comp = expr->ref->u.c.sym->components;
440   else
441     comp = expr->ts.derived->components;
442
443   for (; comp; comp = comp->next, cons = cons->next)
444     {
445       if (! cons->expr)
446         {
447           t = FAILURE;
448           continue;
449         }
450
451       if (gfc_resolve_expr (cons->expr) == FAILURE)
452         {
453           t = FAILURE;
454           continue;
455         }
456
457       /* If we don't have the right type, try to convert it.  */
458
459       if (!gfc_compare_types (&cons->expr->ts, &comp->ts)
460           && gfc_convert_type (cons->expr, &comp->ts, 1) == FAILURE)
461         t = FAILURE;
462     }
463
464   return t;
465 }
466
467
468
469 /****************** Expression name resolution ******************/
470
471 /* Returns 0 if a symbol was not declared with a type or
472    attribute declaration statement, nonzero otherwise.  */
473
474 static int
475 was_declared (gfc_symbol * sym)
476 {
477   symbol_attribute a;
478
479   a = sym->attr;
480
481   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
482     return 1;
483
484   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
485       || a.optional || a.pointer || a.save || a.target
486       || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
487     return 1;
488
489   return 0;
490 }
491
492
493 /* Determine if a symbol is generic or not.  */
494
495 static int
496 generic_sym (gfc_symbol * sym)
497 {
498   gfc_symbol *s;
499
500   if (sym->attr.generic ||
501       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
502     return 1;
503
504   if (was_declared (sym) || sym->ns->parent == NULL)
505     return 0;
506
507   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
508
509   return (s == NULL) ? 0 : generic_sym (s);
510 }
511
512
513 /* Determine if a symbol is specific or not.  */
514
515 static int
516 specific_sym (gfc_symbol * sym)
517 {
518   gfc_symbol *s;
519
520   if (sym->attr.if_source == IFSRC_IFBODY
521       || sym->attr.proc == PROC_MODULE
522       || sym->attr.proc == PROC_INTERNAL
523       || sym->attr.proc == PROC_ST_FUNCTION
524       || (sym->attr.intrinsic &&
525           gfc_specific_intrinsic (sym->name))
526       || sym->attr.external)
527     return 1;
528
529   if (was_declared (sym) || sym->ns->parent == NULL)
530     return 0;
531
532   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
533
534   return (s == NULL) ? 0 : specific_sym (s);
535 }
536
537
538 /* Figure out if the procedure is specific, generic or unknown.  */
539
540 typedef enum
541 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
542 proc_type;
543
544 static proc_type
545 procedure_kind (gfc_symbol * sym)
546 {
547
548   if (generic_sym (sym))
549     return PTYPE_GENERIC;
550
551   if (specific_sym (sym))
552     return PTYPE_SPECIFIC;
553
554   return PTYPE_UNKNOWN;
555 }
556
557
558 /* Resolve an actual argument list.  Most of the time, this is just
559    resolving the expressions in the list.
560    The exception is that we sometimes have to decide whether arguments
561    that look like procedure arguments are really simple variable
562    references.  */
563
564 static try
565 resolve_actual_arglist (gfc_actual_arglist * arg)
566 {
567   gfc_symbol *sym;
568   gfc_symtree *parent_st;
569   gfc_expr *e;
570
571   for (; arg; arg = arg->next)
572     {
573
574       e = arg->expr;
575       if (e == NULL)
576         {
577           /* Check the label is a valid branching target.  */
578           if (arg->label)
579             {
580               if (arg->label->defined == ST_LABEL_UNKNOWN)
581                 {
582                   gfc_error ("Label %d referenced at %L is never defined",
583                              arg->label->value, &arg->label->where);
584                   return FAILURE;
585                 }
586             }
587           continue;
588         }
589
590       if (e->ts.type != BT_PROCEDURE)
591         {
592           if (gfc_resolve_expr (e) != SUCCESS)
593             return FAILURE;
594           continue;
595         }
596
597       /* See if the expression node should really be a variable
598          reference.  */
599
600       sym = e->symtree->n.sym;
601
602       if (sym->attr.flavor == FL_PROCEDURE
603           || sym->attr.intrinsic
604           || sym->attr.external)
605         {
606
607           /* If the symbol is the function that names the current (or
608              parent) scope, then we really have a variable reference.  */
609
610           if (sym->attr.function && sym->result == sym
611               && (sym->ns->proc_name == sym
612                   || (sym->ns->parent != NULL
613                       && sym->ns->parent->proc_name == sym)))
614             goto got_variable;
615
616           continue;
617         }
618
619       /* See if the name is a module procedure in a parent unit.  */
620
621       if (was_declared (sym) || sym->ns->parent == NULL)
622         goto got_variable;
623
624       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
625         {
626           gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
627           return FAILURE;
628         }
629
630       if (parent_st == NULL)
631         goto got_variable;
632
633       sym = parent_st->n.sym;
634       e->symtree = parent_st;           /* Point to the right thing.  */
635
636       if (sym->attr.flavor == FL_PROCEDURE
637           || sym->attr.intrinsic
638           || sym->attr.external)
639         {
640           continue;
641         }
642
643     got_variable:
644       e->expr_type = EXPR_VARIABLE;
645       e->ts = sym->ts;
646       if (sym->as != NULL)
647         {
648           e->rank = sym->as->rank;
649           e->ref = gfc_get_ref ();
650           e->ref->type = REF_ARRAY;
651           e->ref->u.ar.type = AR_FULL;
652           e->ref->u.ar.as = sym->as;
653         }
654     }
655
656   return SUCCESS;
657 }
658
659
660 /************* Function resolution *************/
661
662 /* Resolve a function call known to be generic.
663    Section 14.1.2.4.1.  */
664
665 static match
666 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
667 {
668   gfc_symbol *s;
669
670   if (sym->attr.generic)
671     {
672       s =
673         gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
674       if (s != NULL)
675         {
676           expr->value.function.name = s->name;
677           expr->value.function.esym = s;
678           expr->ts = s->ts;
679           if (s->as != NULL)
680             expr->rank = s->as->rank;
681           return MATCH_YES;
682         }
683
684       /* TODO: Need to search for elemental references in generic interface */
685     }
686
687   if (sym->attr.intrinsic)
688     return gfc_intrinsic_func_interface (expr, 0);
689
690   return MATCH_NO;
691 }
692
693
694 static try
695 resolve_generic_f (gfc_expr * expr)
696 {
697   gfc_symbol *sym;
698   match m;
699
700   sym = expr->symtree->n.sym;
701
702   for (;;)
703     {
704       m = resolve_generic_f0 (expr, sym);
705       if (m == MATCH_YES)
706         return SUCCESS;
707       else if (m == MATCH_ERROR)
708         return FAILURE;
709
710 generic:
711       if (sym->ns->parent == NULL)
712         break;
713       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
714
715       if (sym == NULL)
716         break;
717       if (!generic_sym (sym))
718         goto generic;
719     }
720
721   /* Last ditch attempt.  */
722
723   if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
724     {
725       gfc_error ("Generic function '%s' at %L is not an intrinsic function",
726                  expr->symtree->n.sym->name, &expr->where);
727       return FAILURE;
728     }
729
730   m = gfc_intrinsic_func_interface (expr, 0);
731   if (m == MATCH_YES)
732     return SUCCESS;
733   if (m == MATCH_NO)
734     gfc_error
735       ("Generic function '%s' at %L is not consistent with a specific "
736        "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
737
738   return FAILURE;
739 }
740
741
742 /* Resolve a function call known to be specific.  */
743
744 static match
745 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
746 {
747   match m;
748
749   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
750     {
751       if (sym->attr.dummy)
752         {
753           sym->attr.proc = PROC_DUMMY;
754           goto found;
755         }
756
757       sym->attr.proc = PROC_EXTERNAL;
758       goto found;
759     }
760
761   if (sym->attr.proc == PROC_MODULE
762       || sym->attr.proc == PROC_ST_FUNCTION
763       || sym->attr.proc == PROC_INTERNAL)
764     goto found;
765
766   if (sym->attr.intrinsic)
767     {
768       m = gfc_intrinsic_func_interface (expr, 1);
769       if (m == MATCH_YES)
770         return MATCH_YES;
771       if (m == MATCH_NO)
772         gfc_error
773           ("Function '%s' at %L is INTRINSIC but is not compatible with "
774            "an intrinsic", sym->name, &expr->where);
775
776       return MATCH_ERROR;
777     }
778
779   return MATCH_NO;
780
781 found:
782   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
783
784   expr->ts = sym->ts;
785   expr->value.function.name = sym->name;
786   expr->value.function.esym = sym;
787   if (sym->as != NULL)
788     expr->rank = sym->as->rank;
789
790   return MATCH_YES;
791 }
792
793
794 static try
795 resolve_specific_f (gfc_expr * expr)
796 {
797   gfc_symbol *sym;
798   match m;
799
800   sym = expr->symtree->n.sym;
801
802   for (;;)
803     {
804       m = resolve_specific_f0 (sym, expr);
805       if (m == MATCH_YES)
806         return SUCCESS;
807       if (m == MATCH_ERROR)
808         return FAILURE;
809
810       if (sym->ns->parent == NULL)
811         break;
812
813       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
814
815       if (sym == NULL)
816         break;
817     }
818
819   gfc_error ("Unable to resolve the specific function '%s' at %L",
820              expr->symtree->n.sym->name, &expr->where);
821
822   return SUCCESS;
823 }
824
825
826 /* Resolve a procedure call not known to be generic nor specific.  */
827
828 static try
829 resolve_unknown_f (gfc_expr * expr)
830 {
831   gfc_symbol *sym;
832   gfc_typespec *ts;
833
834   sym = expr->symtree->n.sym;
835
836   if (sym->attr.dummy)
837     {
838       sym->attr.proc = PROC_DUMMY;
839       expr->value.function.name = sym->name;
840       goto set_type;
841     }
842
843   /* See if we have an intrinsic function reference.  */
844
845   if (gfc_intrinsic_name (sym->name, 0))
846     {
847       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
848         return SUCCESS;
849       return FAILURE;
850     }
851
852   /* The reference is to an external name.  */
853
854   sym->attr.proc = PROC_EXTERNAL;
855   expr->value.function.name = sym->name;
856   expr->value.function.esym = expr->symtree->n.sym;
857
858   if (sym->as != NULL)
859     expr->rank = sym->as->rank;
860
861   /* Type of the expression is either the type of the symbol or the
862      default type of the symbol.  */
863
864 set_type:
865   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
866
867   if (sym->ts.type != BT_UNKNOWN)
868     expr->ts = sym->ts;
869   else
870     {
871       ts = gfc_get_default_type (sym, sym->ns);
872
873       if (ts->type == BT_UNKNOWN)
874         {
875           gfc_error ("Function '%s' at %L has no implicit type",
876                      sym->name, &expr->where);
877           return FAILURE;
878         }
879       else
880         expr->ts = *ts;
881     }
882
883   return SUCCESS;
884 }
885
886
887 /* Figure out if a function reference is pure or not.  Also set the name
888    of the function for a potential error message.  Return nonzero if the
889    function is PURE, zero if not.  */
890
891 static int
892 pure_function (gfc_expr * e, const char **name)
893 {
894   int pure;
895
896   if (e->value.function.esym)
897     {
898       pure = gfc_pure (e->value.function.esym);
899       *name = e->value.function.esym->name;
900     }
901   else if (e->value.function.isym)
902     {
903       pure = e->value.function.isym->pure
904         || e->value.function.isym->elemental;
905       *name = e->value.function.isym->name;
906     }
907   else
908     {
909       /* Implicit functions are not pure.  */
910       pure = 0;
911       *name = e->value.function.name;
912     }
913
914   return pure;
915 }
916
917
918 /* Resolve a function call, which means resolving the arguments, then figuring
919    out which entity the name refers to.  */
920 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
921    to INTENT(OUT) or INTENT(INOUT).  */
922
923 static try
924 resolve_function (gfc_expr * expr)
925 {
926   gfc_actual_arglist *arg;
927   const char *name;
928   try t;
929
930   if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
931     return FAILURE;
932
933 /* See if function is already resolved.  */
934
935   if (expr->value.function.name != NULL)
936     {
937       if (expr->ts.type == BT_UNKNOWN)
938         expr->ts = expr->symtree->n.sym->ts;
939       t = SUCCESS;
940     }
941   else
942     {
943       /* Apply the rules of section 14.1.2.  */
944
945       switch (procedure_kind (expr->symtree->n.sym))
946         {
947         case PTYPE_GENERIC:
948           t = resolve_generic_f (expr);
949           break;
950
951         case PTYPE_SPECIFIC:
952           t = resolve_specific_f (expr);
953           break;
954
955         case PTYPE_UNKNOWN:
956           t = resolve_unknown_f (expr);
957           break;
958
959         default:
960           gfc_internal_error ("resolve_function(): bad function type");
961         }
962     }
963
964   /* If the expression is still a function (it might have simplified),
965      then we check to see if we are calling an elemental function.  */
966
967   if (expr->expr_type != EXPR_FUNCTION)
968     return t;
969
970   if (expr->value.function.actual != NULL
971       && ((expr->value.function.esym != NULL
972            && expr->value.function.esym->attr.elemental)
973           || (expr->value.function.isym != NULL
974               && expr->value.function.isym->elemental)))
975     {
976
977       /* The rank of an elemental is the rank of its array argument(s).  */
978
979       for (arg = expr->value.function.actual; arg; arg = arg->next)
980         {
981           if (arg->expr != NULL && arg->expr->rank > 0)
982             {
983               expr->rank = arg->expr->rank;
984               break;
985             }
986         }
987     }
988
989   if (!pure_function (expr, &name))
990     {
991       if (forall_flag)
992         {
993           gfc_error
994             ("Function reference to '%s' at %L is inside a FORALL block",
995              name, &expr->where);
996           t = FAILURE;
997         }
998       else if (gfc_pure (NULL))
999         {
1000           gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1001                      "procedure within a PURE procedure", name, &expr->where);
1002           t = FAILURE;
1003         }
1004     }
1005
1006   return t;
1007 }
1008
1009
1010 /************* Subroutine resolution *************/
1011
1012 static void
1013 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1014 {
1015
1016   if (gfc_pure (sym))
1017     return;
1018
1019   if (forall_flag)
1020     gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1021                sym->name, &c->loc);
1022   else if (gfc_pure (NULL))
1023     gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1024                &c->loc);
1025 }
1026
1027
1028 static match
1029 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1030 {
1031   gfc_symbol *s;
1032
1033   if (sym->attr.generic)
1034     {
1035       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1036       if (s != NULL)
1037         {
1038           c->resolved_sym = s;
1039           pure_subroutine (c, s);
1040           return MATCH_YES;
1041         }
1042
1043       /* TODO: Need to search for elemental references in generic interface.  */
1044     }
1045
1046   if (sym->attr.intrinsic)
1047     return gfc_intrinsic_sub_interface (c, 0);
1048
1049   return MATCH_NO;
1050 }
1051
1052
1053 static try
1054 resolve_generic_s (gfc_code * c)
1055 {
1056   gfc_symbol *sym;
1057   match m;
1058
1059   sym = c->symtree->n.sym;
1060
1061   m = resolve_generic_s0 (c, sym);
1062   if (m == MATCH_YES)
1063     return SUCCESS;
1064   if (m == MATCH_ERROR)
1065     return FAILURE;
1066
1067   if (sym->ns->parent != NULL)
1068     {
1069       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1070       if (sym != NULL)
1071         {
1072           m = resolve_generic_s0 (c, sym);
1073           if (m == MATCH_YES)
1074             return SUCCESS;
1075           if (m == MATCH_ERROR)
1076             return FAILURE;
1077         }
1078     }
1079
1080   /* Last ditch attempt.  */
1081
1082   if (!gfc_generic_intrinsic (sym->name))
1083     {
1084       gfc_error
1085         ("Generic subroutine '%s' at %L is not an intrinsic subroutine",
1086          sym->name, &c->loc);
1087       return FAILURE;
1088     }
1089
1090   m = gfc_intrinsic_sub_interface (c, 0);
1091   if (m == MATCH_YES)
1092     return SUCCESS;
1093   if (m == MATCH_NO)
1094     gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1095                "intrinsic subroutine interface", sym->name, &c->loc);
1096
1097   return FAILURE;
1098 }
1099
1100
1101 /* Resolve a subroutine call known to be specific.  */
1102
1103 static match
1104 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1105 {
1106   match m;
1107
1108   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1109     {
1110       if (sym->attr.dummy)
1111         {
1112           sym->attr.proc = PROC_DUMMY;
1113           goto found;
1114         }
1115
1116       sym->attr.proc = PROC_EXTERNAL;
1117       goto found;
1118     }
1119
1120   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1121     goto found;
1122
1123   if (sym->attr.intrinsic)
1124     {
1125       m = gfc_intrinsic_sub_interface (c, 1);
1126       if (m == MATCH_YES)
1127         return MATCH_YES;
1128       if (m == MATCH_NO)
1129         gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1130                    "with an intrinsic", sym->name, &c->loc);
1131
1132       return MATCH_ERROR;
1133     }
1134
1135   return MATCH_NO;
1136
1137 found:
1138   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1139
1140   c->resolved_sym = sym;
1141   pure_subroutine (c, sym);
1142
1143   return MATCH_YES;
1144 }
1145
1146
1147 static try
1148 resolve_specific_s (gfc_code * c)
1149 {
1150   gfc_symbol *sym;
1151   match m;
1152
1153   sym = c->symtree->n.sym;
1154
1155   m = resolve_specific_s0 (c, sym);
1156   if (m == MATCH_YES)
1157     return SUCCESS;
1158   if (m == MATCH_ERROR)
1159     return FAILURE;
1160
1161   gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1162
1163   if (sym != NULL)
1164     {
1165       m = resolve_specific_s0 (c, sym);
1166       if (m == MATCH_YES)
1167         return SUCCESS;
1168       if (m == MATCH_ERROR)
1169         return FAILURE;
1170     }
1171
1172   gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1173              sym->name, &c->loc);
1174
1175   return FAILURE;
1176 }
1177
1178
1179 /* Resolve a subroutine call not known to be generic nor specific.  */
1180
1181 static try
1182 resolve_unknown_s (gfc_code * c)
1183 {
1184   gfc_symbol *sym;
1185
1186   sym = c->symtree->n.sym;
1187
1188   if (sym->attr.dummy)
1189     {
1190       sym->attr.proc = PROC_DUMMY;
1191       goto found;
1192     }
1193
1194   /* See if we have an intrinsic function reference.  */
1195
1196   if (gfc_intrinsic_name (sym->name, 1))
1197     {
1198       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1199         return SUCCESS;
1200       return FAILURE;
1201     }
1202
1203   /* The reference is to an external name.  */
1204
1205 found:
1206   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1207
1208   c->resolved_sym = sym;
1209
1210   pure_subroutine (c, sym);
1211
1212   return SUCCESS;
1213 }
1214
1215
1216 /* Resolve a subroutine call.  Although it was tempting to use the same code
1217    for functions, subroutines and functions are stored differently and this
1218    makes things awkward.  */
1219
1220 static try
1221 resolve_call (gfc_code * c)
1222 {
1223   try t;
1224
1225   if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1226     return FAILURE;
1227
1228   if (c->resolved_sym != NULL)
1229     return SUCCESS;
1230
1231   switch (procedure_kind (c->symtree->n.sym))
1232     {
1233     case PTYPE_GENERIC:
1234       t = resolve_generic_s (c);
1235       break;
1236
1237     case PTYPE_SPECIFIC:
1238       t = resolve_specific_s (c);
1239       break;
1240
1241     case PTYPE_UNKNOWN:
1242       t = resolve_unknown_s (c);
1243       break;
1244
1245     default:
1246       gfc_internal_error ("resolve_subroutine(): bad function type");
1247     }
1248
1249   return t;
1250 }
1251
1252 /* Compare the shapes of two arrays that have non-NULL shapes.  If both
1253    op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1254    match.  If both op1->shape and op2->shape are non-NULL return FAILURE
1255    if their shapes do not match.  If either op1->shape or op2->shape is
1256    NULL, return SUCCESS.  */
1257
1258 static try
1259 compare_shapes (gfc_expr * op1, gfc_expr * op2)
1260 {
1261   try t;
1262   int i;
1263
1264   t = SUCCESS;
1265                   
1266   if (op1->shape != NULL && op2->shape != NULL)
1267     {
1268       for (i = 0; i < op1->rank; i++)
1269         {
1270           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1271            {
1272              gfc_error ("Shapes for operands at %L and %L are not conformable",
1273                          &op1->where, &op2->where);
1274              t = FAILURE;
1275              break;
1276            }
1277         }
1278     }
1279
1280   return t;
1281 }
1282
1283 /* Resolve an operator expression node.  This can involve replacing the
1284    operation with a user defined function call.  */
1285
1286 static try
1287 resolve_operator (gfc_expr * e)
1288 {
1289   gfc_expr *op1, *op2;
1290   char msg[200];
1291   try t;
1292
1293   /* Resolve all subnodes-- give them types.  */
1294
1295   switch (e->value.op.operator)
1296     {
1297     default:
1298       if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1299         return FAILURE;
1300
1301     /* Fall through...  */
1302
1303     case INTRINSIC_NOT:
1304     case INTRINSIC_UPLUS:
1305     case INTRINSIC_UMINUS:
1306       if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1307         return FAILURE;
1308       break;
1309     }
1310
1311   /* Typecheck the new node.  */
1312
1313   op1 = e->value.op.op1;
1314   op2 = e->value.op.op2;
1315
1316   switch (e->value.op.operator)
1317     {
1318     case INTRINSIC_UPLUS:
1319     case INTRINSIC_UMINUS:
1320       if (op1->ts.type == BT_INTEGER
1321           || op1->ts.type == BT_REAL
1322           || op1->ts.type == BT_COMPLEX)
1323         {
1324           e->ts = op1->ts;
1325           break;
1326         }
1327
1328       sprintf (msg, "Operand of unary numeric operator '%s' at %%L is %s",
1329                gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1330       goto bad_op;
1331
1332     case INTRINSIC_PLUS:
1333     case INTRINSIC_MINUS:
1334     case INTRINSIC_TIMES:
1335     case INTRINSIC_DIVIDE:
1336     case INTRINSIC_POWER:
1337       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1338         {
1339           gfc_type_convert_binary (e);
1340           break;
1341         }
1342
1343       sprintf (msg,
1344                "Operands of binary numeric operator '%s' at %%L are %s/%s",
1345                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1346                gfc_typename (&op2->ts));
1347       goto bad_op;
1348
1349     case INTRINSIC_CONCAT:
1350       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1351         {
1352           e->ts.type = BT_CHARACTER;
1353           e->ts.kind = op1->ts.kind;
1354           break;
1355         }
1356
1357       sprintf (msg,
1358                "Operands of string concatenation operator at %%L are %s/%s",
1359                gfc_typename (&op1->ts), gfc_typename (&op2->ts));
1360       goto bad_op;
1361
1362     case INTRINSIC_AND:
1363     case INTRINSIC_OR:
1364     case INTRINSIC_EQV:
1365     case INTRINSIC_NEQV:
1366       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1367         {
1368           e->ts.type = BT_LOGICAL;
1369           e->ts.kind = gfc_kind_max (op1, op2);
1370           if (op1->ts.kind < e->ts.kind)
1371             gfc_convert_type (op1, &e->ts, 2);
1372           else if (op2->ts.kind < e->ts.kind)
1373             gfc_convert_type (op2, &e->ts, 2);
1374           break;
1375         }
1376
1377       sprintf (msg, "Operands of logical operator '%s' at %%L are %s/%s",
1378                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1379                gfc_typename (&op2->ts));
1380
1381       goto bad_op;
1382
1383     case INTRINSIC_NOT:
1384       if (op1->ts.type == BT_LOGICAL)
1385         {
1386           e->ts.type = BT_LOGICAL;
1387           e->ts.kind = op1->ts.kind;
1388           break;
1389         }
1390
1391       sprintf (msg, "Operand of .NOT. operator at %%L is %s",
1392                gfc_typename (&op1->ts));
1393       goto bad_op;
1394
1395     case INTRINSIC_GT:
1396     case INTRINSIC_GE:
1397     case INTRINSIC_LT:
1398     case INTRINSIC_LE:
1399       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1400         {
1401           strcpy (msg, "COMPLEX quantities cannot be compared at %L");
1402           goto bad_op;
1403         }
1404
1405       /* Fall through...  */
1406
1407     case INTRINSIC_EQ:
1408     case INTRINSIC_NE:
1409       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1410         {
1411           e->ts.type = BT_LOGICAL;
1412           e->ts.kind = gfc_default_logical_kind;
1413           break;
1414         }
1415
1416       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1417         {
1418           gfc_type_convert_binary (e);
1419
1420           e->ts.type = BT_LOGICAL;
1421           e->ts.kind = gfc_default_logical_kind;
1422           break;
1423         }
1424
1425       sprintf (msg, "Operands of comparison operator '%s' at %%L are %s/%s",
1426                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1427                gfc_typename (&op2->ts));
1428
1429       goto bad_op;
1430
1431     case INTRINSIC_USER:
1432       if (op2 == NULL)
1433         sprintf (msg, "Operand of user operator '%s' at %%L is %s",
1434                  e->value.op.uop->name, gfc_typename (&op1->ts));
1435       else
1436         sprintf (msg, "Operands of user operator '%s' at %%L are %s/%s",
1437                  e->value.op.uop->name, gfc_typename (&op1->ts),
1438                  gfc_typename (&op2->ts));
1439
1440       goto bad_op;
1441
1442     default:
1443       gfc_internal_error ("resolve_operator(): Bad intrinsic");
1444     }
1445
1446   /* Deal with arrayness of an operand through an operator.  */
1447
1448   t = SUCCESS;
1449
1450   switch (e->value.op.operator)
1451     {
1452     case INTRINSIC_PLUS:
1453     case INTRINSIC_MINUS:
1454     case INTRINSIC_TIMES:
1455     case INTRINSIC_DIVIDE:
1456     case INTRINSIC_POWER:
1457     case INTRINSIC_CONCAT:
1458     case INTRINSIC_AND:
1459     case INTRINSIC_OR:
1460     case INTRINSIC_EQV:
1461     case INTRINSIC_NEQV:
1462     case INTRINSIC_EQ:
1463     case INTRINSIC_NE:
1464     case INTRINSIC_GT:
1465     case INTRINSIC_GE:
1466     case INTRINSIC_LT:
1467     case INTRINSIC_LE:
1468
1469       if (op1->rank == 0 && op2->rank == 0)
1470         e->rank = 0;
1471
1472       if (op1->rank == 0 && op2->rank != 0)
1473         {
1474           e->rank = op2->rank;
1475
1476           if (e->shape == NULL)
1477             e->shape = gfc_copy_shape (op2->shape, op2->rank);
1478         }
1479
1480       if (op1->rank != 0 && op2->rank == 0)
1481         {
1482           e->rank = op1->rank;
1483
1484           if (e->shape == NULL)
1485             e->shape = gfc_copy_shape (op1->shape, op1->rank);
1486         }
1487
1488       if (op1->rank != 0 && op2->rank != 0)
1489         {
1490           if (op1->rank == op2->rank)
1491             {
1492               e->rank = op1->rank;
1493               if (e->shape == NULL)
1494                 {
1495                   t = compare_shapes(op1, op2);
1496                   if (t == FAILURE)
1497                     e->shape = NULL;
1498                   else
1499                 e->shape = gfc_copy_shape (op1->shape, op1->rank);
1500                 }
1501             }
1502           else
1503             {
1504               gfc_error ("Inconsistent ranks for operator at %L and %L",
1505                          &op1->where, &op2->where);
1506               t = FAILURE;
1507
1508               /* Allow higher level expressions to work.  */
1509               e->rank = 0;
1510             }
1511         }
1512
1513       break;
1514
1515     case INTRINSIC_NOT:
1516     case INTRINSIC_UPLUS:
1517     case INTRINSIC_UMINUS:
1518       e->rank = op1->rank;
1519
1520       if (e->shape == NULL)
1521         e->shape = gfc_copy_shape (op1->shape, op1->rank);
1522
1523       /* Simply copy arrayness attribute */
1524       break;
1525
1526     default:
1527       break;
1528     }
1529
1530   /* Attempt to simplify the expression.  */
1531   if (t == SUCCESS)
1532     t = gfc_simplify_expr (e, 0);
1533   return t;
1534
1535 bad_op:
1536
1537   if (gfc_extend_expr (e) == SUCCESS)
1538     return SUCCESS;
1539
1540   gfc_error (msg, &e->where);
1541
1542   return FAILURE;
1543 }
1544
1545
1546 /************** Array resolution subroutines **************/
1547
1548
1549 typedef enum
1550 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
1551 comparison;
1552
1553 /* Compare two integer expressions.  */
1554
1555 static comparison
1556 compare_bound (gfc_expr * a, gfc_expr * b)
1557 {
1558   int i;
1559
1560   if (a == NULL || a->expr_type != EXPR_CONSTANT
1561       || b == NULL || b->expr_type != EXPR_CONSTANT)
1562     return CMP_UNKNOWN;
1563
1564   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
1565     gfc_internal_error ("compare_bound(): Bad expression");
1566
1567   i = mpz_cmp (a->value.integer, b->value.integer);
1568
1569   if (i < 0)
1570     return CMP_LT;
1571   if (i > 0)
1572     return CMP_GT;
1573   return CMP_EQ;
1574 }
1575
1576
1577 /* Compare an integer expression with an integer.  */
1578
1579 static comparison
1580 compare_bound_int (gfc_expr * a, int b)
1581 {
1582   int i;
1583
1584   if (a == NULL || a->expr_type != EXPR_CONSTANT)
1585     return CMP_UNKNOWN;
1586
1587   if (a->ts.type != BT_INTEGER)
1588     gfc_internal_error ("compare_bound_int(): Bad expression");
1589
1590   i = mpz_cmp_si (a->value.integer, b);
1591
1592   if (i < 0)
1593     return CMP_LT;
1594   if (i > 0)
1595     return CMP_GT;
1596   return CMP_EQ;
1597 }
1598
1599
1600 /* Compare a single dimension of an array reference to the array
1601    specification.  */
1602
1603 static try
1604 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
1605 {
1606
1607 /* Given start, end and stride values, calculate the minimum and
1608    maximum referenced indexes.  */
1609
1610   switch (ar->type)
1611     {
1612     case AR_FULL:
1613       break;
1614
1615     case AR_ELEMENT:
1616       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1617         goto bound;
1618       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1619         goto bound;
1620
1621       break;
1622
1623     case AR_SECTION:
1624       if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
1625         {
1626           gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
1627           return FAILURE;
1628         }
1629
1630       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1631         goto bound;
1632       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1633         goto bound;
1634
1635       /* TODO: Possibly, we could warn about end[i] being out-of-bound although
1636          it is legal (see 6.2.2.3.1).  */
1637
1638       break;
1639
1640     default:
1641       gfc_internal_error ("check_dimension(): Bad array reference");
1642     }
1643
1644   return SUCCESS;
1645
1646 bound:
1647   gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
1648   return SUCCESS;
1649 }
1650
1651
1652 /* Compare an array reference with an array specification.  */
1653
1654 static try
1655 compare_spec_to_ref (gfc_array_ref * ar)
1656 {
1657   gfc_array_spec *as;
1658   int i;
1659
1660   as = ar->as;
1661   i = as->rank - 1;
1662   /* TODO: Full array sections are only allowed as actual parameters.  */
1663   if (as->type == AS_ASSUMED_SIZE
1664       && (/*ar->type == AR_FULL
1665           ||*/ (ar->type == AR_SECTION
1666               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
1667     {
1668       gfc_error ("Rightmost upper bound of assumed size array section"
1669                  " not specified at %L", &ar->where);
1670       return FAILURE;
1671     }
1672
1673   if (ar->type == AR_FULL)
1674     return SUCCESS;
1675
1676   if (as->rank != ar->dimen)
1677     {
1678       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
1679                  &ar->where, ar->dimen, as->rank);
1680       return FAILURE;
1681     }
1682
1683   for (i = 0; i < as->rank; i++)
1684     if (check_dimension (i, ar, as) == FAILURE)
1685       return FAILURE;
1686
1687   return SUCCESS;
1688 }
1689
1690
1691 /* Resolve one part of an array index.  */
1692
1693 try
1694 gfc_resolve_index (gfc_expr * index, int check_scalar)
1695 {
1696   gfc_typespec ts;
1697
1698   if (index == NULL)
1699     return SUCCESS;
1700
1701   if (gfc_resolve_expr (index) == FAILURE)
1702     return FAILURE;
1703
1704   if (check_scalar && index->rank != 0)
1705     {
1706       gfc_error ("Array index at %L must be scalar", &index->where);
1707       return FAILURE;
1708     }
1709
1710   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
1711     {
1712       gfc_error ("Array index at %L must be of INTEGER type",
1713                  &index->where);
1714       return FAILURE;
1715     }
1716
1717   if (index->ts.type == BT_REAL)
1718     if (gfc_notify_std (GFC_STD_GNU, "Extension: REAL array index at %L",
1719                         &index->where) == FAILURE)
1720       return FAILURE;
1721
1722   if (index->ts.kind != gfc_index_integer_kind
1723       || index->ts.type != BT_INTEGER)
1724     {
1725       ts.type = BT_INTEGER;
1726       ts.kind = gfc_index_integer_kind;
1727
1728       gfc_convert_type_warn (index, &ts, 2, 0);
1729     }
1730
1731   return SUCCESS;
1732 }
1733
1734
1735 /* Given an expression that contains array references, update those array
1736    references to point to the right array specifications.  While this is
1737    filled in during matching, this information is difficult to save and load
1738    in a module, so we take care of it here.
1739
1740    The idea here is that the original array reference comes from the
1741    base symbol.  We traverse the list of reference structures, setting
1742    the stored reference to references.  Component references can
1743    provide an additional array specification.  */
1744
1745 static void
1746 find_array_spec (gfc_expr * e)
1747 {
1748   gfc_array_spec *as;
1749   gfc_component *c;
1750   gfc_ref *ref;
1751
1752   as = e->symtree->n.sym->as;
1753   c = e->symtree->n.sym->components;
1754
1755   for (ref = e->ref; ref; ref = ref->next)
1756     switch (ref->type)
1757       {
1758       case REF_ARRAY:
1759         if (as == NULL)
1760           gfc_internal_error ("find_array_spec(): Missing spec");
1761
1762         ref->u.ar.as = as;
1763         as = NULL;
1764         break;
1765
1766       case REF_COMPONENT:
1767         for (; c; c = c->next)
1768           if (c == ref->u.c.component)
1769             break;
1770
1771         if (c == NULL)
1772           gfc_internal_error ("find_array_spec(): Component not found");
1773
1774         if (c->dimension)
1775           {
1776             if (as != NULL)
1777               gfc_internal_error ("find_array_spec(): unused as(1)");
1778             as = c->as;
1779           }
1780
1781         c = c->ts.derived->components;
1782         break;
1783
1784       case REF_SUBSTRING:
1785         break;
1786       }
1787
1788   if (as != NULL)
1789     gfc_internal_error ("find_array_spec(): unused as(2)");
1790 }
1791
1792
1793 /* Resolve an array reference.  */
1794
1795 static try
1796 resolve_array_ref (gfc_array_ref * ar)
1797 {
1798   int i, check_scalar;
1799
1800   for (i = 0; i < ar->dimen; i++)
1801     {
1802       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
1803
1804       if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
1805         return FAILURE;
1806       if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
1807         return FAILURE;
1808       if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
1809         return FAILURE;
1810
1811       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
1812         switch (ar->start[i]->rank)
1813           {
1814           case 0:
1815             ar->dimen_type[i] = DIMEN_ELEMENT;
1816             break;
1817
1818           case 1:
1819             ar->dimen_type[i] = DIMEN_VECTOR;
1820             break;
1821
1822           default:
1823             gfc_error ("Array index at %L is an array of rank %d",
1824                        &ar->c_where[i], ar->start[i]->rank);
1825             return FAILURE;
1826           }
1827     }
1828
1829   /* If the reference type is unknown, figure out what kind it is.  */
1830
1831   if (ar->type == AR_UNKNOWN)
1832     {
1833       ar->type = AR_ELEMENT;
1834       for (i = 0; i < ar->dimen; i++)
1835         if (ar->dimen_type[i] == DIMEN_RANGE
1836             || ar->dimen_type[i] == DIMEN_VECTOR)
1837           {
1838             ar->type = AR_SECTION;
1839             break;
1840           }
1841     }
1842
1843   if (compare_spec_to_ref (ar) == FAILURE)
1844     return FAILURE;
1845
1846   return SUCCESS;
1847 }
1848
1849
1850 static try
1851 resolve_substring (gfc_ref * ref)
1852 {
1853
1854   if (ref->u.ss.start != NULL)
1855     {
1856       if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
1857         return FAILURE;
1858
1859       if (ref->u.ss.start->ts.type != BT_INTEGER)
1860         {
1861           gfc_error ("Substring start index at %L must be of type INTEGER",
1862                      &ref->u.ss.start->where);
1863           return FAILURE;
1864         }
1865
1866       if (ref->u.ss.start->rank != 0)
1867         {
1868           gfc_error ("Substring start index at %L must be scalar",
1869                      &ref->u.ss.start->where);
1870           return FAILURE;
1871         }
1872
1873       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
1874         {
1875           gfc_error ("Substring start index at %L is less than one",
1876                      &ref->u.ss.start->where);
1877           return FAILURE;
1878         }
1879     }
1880
1881   if (ref->u.ss.end != NULL)
1882     {
1883       if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
1884         return FAILURE;
1885
1886       if (ref->u.ss.end->ts.type != BT_INTEGER)
1887         {
1888           gfc_error ("Substring end index at %L must be of type INTEGER",
1889                      &ref->u.ss.end->where);
1890           return FAILURE;
1891         }
1892
1893       if (ref->u.ss.end->rank != 0)
1894         {
1895           gfc_error ("Substring end index at %L must be scalar",
1896                      &ref->u.ss.end->where);
1897           return FAILURE;
1898         }
1899
1900       if (ref->u.ss.length != NULL
1901           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
1902         {
1903           gfc_error ("Substring end index at %L is out of bounds",
1904                      &ref->u.ss.start->where);
1905           return FAILURE;
1906         }
1907     }
1908
1909   return SUCCESS;
1910 }
1911
1912
1913 /* Resolve subtype references.  */
1914
1915 static try
1916 resolve_ref (gfc_expr * expr)
1917 {
1918   int current_part_dimension, n_components, seen_part_dimension;
1919   gfc_ref *ref;
1920
1921   for (ref = expr->ref; ref; ref = ref->next)
1922     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
1923       {
1924         find_array_spec (expr);
1925         break;
1926       }
1927
1928   for (ref = expr->ref; ref; ref = ref->next)
1929     switch (ref->type)
1930       {
1931       case REF_ARRAY:
1932         if (resolve_array_ref (&ref->u.ar) == FAILURE)
1933           return FAILURE;
1934         break;
1935
1936       case REF_COMPONENT:
1937         break;
1938
1939       case REF_SUBSTRING:
1940         resolve_substring (ref);
1941         break;
1942       }
1943
1944   /* Check constraints on part references.  */
1945
1946   current_part_dimension = 0;
1947   seen_part_dimension = 0;
1948   n_components = 0;
1949
1950   for (ref = expr->ref; ref; ref = ref->next)
1951     {
1952       switch (ref->type)
1953         {
1954         case REF_ARRAY:
1955           switch (ref->u.ar.type)
1956             {
1957             case AR_FULL:
1958             case AR_SECTION:
1959               current_part_dimension = 1;
1960               break;
1961
1962             case AR_ELEMENT:
1963               current_part_dimension = 0;
1964               break;
1965
1966             case AR_UNKNOWN:
1967               gfc_internal_error ("resolve_ref(): Bad array reference");
1968             }
1969
1970           break;
1971
1972         case REF_COMPONENT:
1973           if ((current_part_dimension || seen_part_dimension)
1974               && ref->u.c.component->pointer)
1975             {
1976               gfc_error
1977                 ("Component to the right of a part reference with nonzero "
1978                  "rank must not have the POINTER attribute at %L",
1979                  &expr->where);
1980               return FAILURE;
1981             }
1982
1983           n_components++;
1984           break;
1985
1986         case REF_SUBSTRING:
1987           break;
1988         }
1989
1990       if (((ref->type == REF_COMPONENT && n_components > 1)
1991            || ref->next == NULL)
1992           && current_part_dimension
1993           && seen_part_dimension)
1994         {
1995
1996           gfc_error ("Two or more part references with nonzero rank must "
1997                      "not be specified at %L", &expr->where);
1998           return FAILURE;
1999         }
2000
2001       if (ref->type == REF_COMPONENT)
2002         {
2003           if (current_part_dimension)
2004             seen_part_dimension = 1;
2005
2006           /* reset to make sure */
2007           current_part_dimension = 0;
2008         }
2009     }
2010
2011   return SUCCESS;
2012 }
2013
2014
2015 /* Given an expression, determine its shape.  This is easier than it sounds.
2016    Leaves the shape array NULL if it is not possible to determine the shape.  */
2017
2018 static void
2019 expression_shape (gfc_expr * e)
2020 {
2021   mpz_t array[GFC_MAX_DIMENSIONS];
2022   int i;
2023
2024   if (e->rank == 0 || e->shape != NULL)
2025     return;
2026
2027   for (i = 0; i < e->rank; i++)
2028     if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2029       goto fail;
2030
2031   e->shape = gfc_get_shape (e->rank);
2032
2033   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2034
2035   return;
2036
2037 fail:
2038   for (i--; i >= 0; i--)
2039     mpz_clear (array[i]);
2040 }
2041
2042
2043 /* Given a variable expression node, compute the rank of the expression by
2044    examining the base symbol and any reference structures it may have.  */
2045
2046 static void
2047 expression_rank (gfc_expr * e)
2048 {
2049   gfc_ref *ref;
2050   int i, rank;
2051
2052   if (e->ref == NULL)
2053     {
2054       if (e->expr_type == EXPR_ARRAY)
2055         goto done;
2056       /* Constructors can have a rank different from one via RESHAPE().  */
2057
2058       if (e->symtree == NULL)
2059         {
2060           e->rank = 0;
2061           goto done;
2062         }
2063
2064       e->rank = (e->symtree->n.sym->as == NULL)
2065                   ? 0 : e->symtree->n.sym->as->rank;
2066       goto done;
2067     }
2068
2069   rank = 0;
2070
2071   for (ref = e->ref; ref; ref = ref->next)
2072     {
2073       if (ref->type != REF_ARRAY)
2074         continue;
2075
2076       if (ref->u.ar.type == AR_FULL)
2077         {
2078           rank = ref->u.ar.as->rank;
2079           break;
2080         }
2081
2082       if (ref->u.ar.type == AR_SECTION)
2083         {
2084           /* Figure out the rank of the section.  */
2085           if (rank != 0)
2086             gfc_internal_error ("expression_rank(): Two array specs");
2087
2088           for (i = 0; i < ref->u.ar.dimen; i++)
2089             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2090                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2091               rank++;
2092
2093           break;
2094         }
2095     }
2096
2097   e->rank = rank;
2098
2099 done:
2100   expression_shape (e);
2101 }
2102
2103
2104 /* Resolve a variable expression.  */
2105
2106 static try
2107 resolve_variable (gfc_expr * e)
2108 {
2109   gfc_symbol *sym;
2110
2111   if (e->ref && resolve_ref (e) == FAILURE)
2112     return FAILURE;
2113
2114   sym = e->symtree->n.sym;
2115   if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2116     {
2117       e->ts.type = BT_PROCEDURE;
2118       return SUCCESS;
2119     }
2120
2121   if (sym->ts.type != BT_UNKNOWN)
2122     gfc_variable_attr (e, &e->ts);
2123   else
2124     {
2125       /* Must be a simple variable reference.  */
2126       if (gfc_set_default_type (sym, 1, NULL) == FAILURE)
2127         return FAILURE;
2128       e->ts = sym->ts;
2129     }
2130
2131   return SUCCESS;
2132 }
2133
2134
2135 /* Resolve an expression.  That is, make sure that types of operands agree
2136    with their operators, intrinsic operators are converted to function calls
2137    for overloaded types and unresolved function references are resolved.  */
2138
2139 try
2140 gfc_resolve_expr (gfc_expr * e)
2141 {
2142   try t;
2143
2144   if (e == NULL)
2145     return SUCCESS;
2146
2147   switch (e->expr_type)
2148     {
2149     case EXPR_OP:
2150       t = resolve_operator (e);
2151       break;
2152
2153     case EXPR_FUNCTION:
2154       t = resolve_function (e);
2155       break;
2156
2157     case EXPR_VARIABLE:
2158       t = resolve_variable (e);
2159       if (t == SUCCESS)
2160         expression_rank (e);
2161       break;
2162
2163     case EXPR_SUBSTRING:
2164       t = resolve_ref (e);
2165       break;
2166
2167     case EXPR_CONSTANT:
2168     case EXPR_NULL:
2169       t = SUCCESS;
2170       break;
2171
2172     case EXPR_ARRAY:
2173       t = FAILURE;
2174       if (resolve_ref (e) == FAILURE)
2175         break;
2176
2177       t = gfc_resolve_array_constructor (e);
2178       /* Also try to expand a constructor.  */
2179       if (t == SUCCESS)
2180         {
2181           expression_rank (e);
2182           gfc_expand_constructor (e);
2183         }
2184
2185       break;
2186
2187     case EXPR_STRUCTURE:
2188       t = resolve_ref (e);
2189       if (t == FAILURE)
2190         break;
2191
2192       t = resolve_structure_cons (e);
2193       if (t == FAILURE)
2194         break;
2195
2196       t = gfc_simplify_expr (e, 0);
2197       break;
2198
2199     default:
2200       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
2201     }
2202
2203   return t;
2204 }
2205
2206
2207 /* Resolve an expression from an iterator.  They must be scalar and have
2208    INTEGER or (optionally) REAL type.  */
2209
2210 static try
2211 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok, const char * name)
2212 {
2213   if (gfc_resolve_expr (expr) == FAILURE)
2214     return FAILURE;
2215
2216   if (expr->rank != 0)
2217     {
2218       gfc_error ("%s at %L must be a scalar", name, &expr->where);
2219       return FAILURE;
2220     }
2221
2222   if (!(expr->ts.type == BT_INTEGER
2223         || (expr->ts.type == BT_REAL && real_ok)))
2224     {
2225       gfc_error ("%s at %L must be INTEGER%s",
2226                  name,
2227                  &expr->where,
2228                  real_ok ? " or REAL" : "");
2229       return FAILURE;
2230     }
2231   return SUCCESS;
2232 }
2233
2234
2235 /* Resolve the expressions in an iterator structure.  If REAL_OK is
2236    false allow only INTEGER type iterators, otherwise allow REAL types.  */
2237
2238 try
2239 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
2240 {
2241
2242   if (iter->var->ts.type == BT_REAL)
2243     gfc_notify_std (GFC_STD_F95_DEL,
2244                     "Obsolete: REAL DO loop iterator at %L",
2245                     &iter->var->where);
2246
2247   if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
2248       == FAILURE)
2249     return FAILURE;
2250
2251   if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
2252     {
2253       gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
2254                  &iter->var->where);
2255       return FAILURE;
2256     }
2257
2258   if (gfc_resolve_iterator_expr (iter->start, real_ok,
2259                                  "Start expression in DO loop") == FAILURE)
2260     return FAILURE;
2261
2262   if (gfc_resolve_iterator_expr (iter->end, real_ok,
2263                                  "End expression in DO loop") == FAILURE)
2264     return FAILURE;
2265
2266   if (gfc_resolve_iterator_expr (iter->step, real_ok,
2267                                  "Step expression in DO loop") == FAILURE)
2268     return FAILURE;
2269
2270   if (iter->step->expr_type == EXPR_CONSTANT)
2271     {
2272       if ((iter->step->ts.type == BT_INTEGER
2273            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
2274           || (iter->step->ts.type == BT_REAL
2275               && mpfr_sgn (iter->step->value.real) == 0))
2276         {
2277           gfc_error ("Step expression in DO loop at %L cannot be zero",
2278                      &iter->step->where);
2279           return FAILURE;
2280         }
2281     }
2282
2283   /* Convert start, end, and step to the same type as var.  */
2284   if (iter->start->ts.kind != iter->var->ts.kind
2285       || iter->start->ts.type != iter->var->ts.type)
2286     gfc_convert_type (iter->start, &iter->var->ts, 2);
2287
2288   if (iter->end->ts.kind != iter->var->ts.kind
2289       || iter->end->ts.type != iter->var->ts.type)
2290     gfc_convert_type (iter->end, &iter->var->ts, 2);
2291
2292   if (iter->step->ts.kind != iter->var->ts.kind
2293       || iter->step->ts.type != iter->var->ts.type)
2294     gfc_convert_type (iter->step, &iter->var->ts, 2);
2295
2296   return SUCCESS;
2297 }
2298
2299
2300 /* Resolve a list of FORALL iterators.  */
2301
2302 static void
2303 resolve_forall_iterators (gfc_forall_iterator * iter)
2304 {
2305
2306   while (iter)
2307     {
2308       if (gfc_resolve_expr (iter->var) == SUCCESS
2309           && iter->var->ts.type != BT_INTEGER)
2310         gfc_error ("FORALL Iteration variable at %L must be INTEGER",
2311                    &iter->var->where);
2312
2313       if (gfc_resolve_expr (iter->start) == SUCCESS
2314           && iter->start->ts.type != BT_INTEGER)
2315         gfc_error ("FORALL start expression at %L must be INTEGER",
2316                    &iter->start->where);
2317       if (iter->var->ts.kind != iter->start->ts.kind)
2318         gfc_convert_type (iter->start, &iter->var->ts, 2);
2319
2320       if (gfc_resolve_expr (iter->end) == SUCCESS
2321           && iter->end->ts.type != BT_INTEGER)
2322         gfc_error ("FORALL end expression at %L must be INTEGER",
2323                    &iter->end->where);
2324       if (iter->var->ts.kind != iter->end->ts.kind)
2325         gfc_convert_type (iter->end, &iter->var->ts, 2);
2326
2327       if (gfc_resolve_expr (iter->stride) == SUCCESS
2328           && iter->stride->ts.type != BT_INTEGER)
2329         gfc_error ("FORALL Stride expression at %L must be INTEGER",
2330                    &iter->stride->where);
2331       if (iter->var->ts.kind != iter->stride->ts.kind)
2332         gfc_convert_type (iter->stride, &iter->var->ts, 2);
2333
2334       iter = iter->next;
2335     }
2336 }
2337
2338
2339 /* Given a pointer to a symbol that is a derived type, see if any components
2340    have the POINTER attribute.  The search is recursive if necessary.
2341    Returns zero if no pointer components are found, nonzero otherwise.  */
2342
2343 static int
2344 derived_pointer (gfc_symbol * sym)
2345 {
2346   gfc_component *c;
2347
2348   for (c = sym->components; c; c = c->next)
2349     {
2350       if (c->pointer)
2351         return 1;
2352
2353       if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
2354         return 1;
2355     }
2356
2357   return 0;
2358 }
2359
2360
2361 /* Resolve the argument of a deallocate expression.  The expression must be
2362    a pointer or a full array.  */
2363
2364 static try
2365 resolve_deallocate_expr (gfc_expr * e)
2366 {
2367   symbol_attribute attr;
2368   int allocatable;
2369   gfc_ref *ref;
2370
2371   if (gfc_resolve_expr (e) == FAILURE)
2372     return FAILURE;
2373
2374   attr = gfc_expr_attr (e);
2375   if (attr.pointer)
2376     return SUCCESS;
2377
2378   if (e->expr_type != EXPR_VARIABLE)
2379     goto bad;
2380
2381   allocatable = e->symtree->n.sym->attr.allocatable;
2382   for (ref = e->ref; ref; ref = ref->next)
2383     switch (ref->type)
2384       {
2385       case REF_ARRAY:
2386         if (ref->u.ar.type != AR_FULL)
2387           allocatable = 0;
2388         break;
2389
2390       case REF_COMPONENT:
2391         allocatable = (ref->u.c.component->as != NULL
2392                        && ref->u.c.component->as->type == AS_DEFERRED);
2393         break;
2394
2395       case REF_SUBSTRING:
2396         allocatable = 0;
2397         break;
2398       }
2399
2400   if (allocatable == 0)
2401     {
2402     bad:
2403       gfc_error ("Expression in DEALLOCATE statement at %L must be "
2404                  "ALLOCATABLE or a POINTER", &e->where);
2405     }
2406
2407   return SUCCESS;
2408 }
2409
2410
2411 /* Resolve the expression in an ALLOCATE statement, doing the additional
2412    checks to see whether the expression is OK or not.  The expression must
2413    have a trailing array reference that gives the size of the array.  */
2414
2415 static try
2416 resolve_allocate_expr (gfc_expr * e)
2417 {
2418   int i, pointer, allocatable, dimension;
2419   symbol_attribute attr;
2420   gfc_ref *ref, *ref2;
2421   gfc_array_ref *ar;
2422
2423   if (gfc_resolve_expr (e) == FAILURE)
2424     return FAILURE;
2425
2426   /* Make sure the expression is allocatable or a pointer.  If it is
2427      pointer, the next-to-last reference must be a pointer.  */
2428
2429   ref2 = NULL;
2430
2431   if (e->expr_type != EXPR_VARIABLE)
2432     {
2433       allocatable = 0;
2434
2435       attr = gfc_expr_attr (e);
2436       pointer = attr.pointer;
2437       dimension = attr.dimension;
2438
2439     }
2440   else
2441     {
2442       allocatable = e->symtree->n.sym->attr.allocatable;
2443       pointer = e->symtree->n.sym->attr.pointer;
2444       dimension = e->symtree->n.sym->attr.dimension;
2445
2446       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
2447         switch (ref->type)
2448           {
2449           case REF_ARRAY:
2450             if (ref->next != NULL)
2451               pointer = 0;
2452             break;
2453
2454           case REF_COMPONENT:
2455             allocatable = (ref->u.c.component->as != NULL
2456                            && ref->u.c.component->as->type == AS_DEFERRED);
2457
2458             pointer = ref->u.c.component->pointer;
2459             dimension = ref->u.c.component->dimension;
2460             break;
2461
2462           case REF_SUBSTRING:
2463             allocatable = 0;
2464             pointer = 0;
2465             break;
2466           }
2467     }
2468
2469   if (allocatable == 0 && pointer == 0)
2470     {
2471       gfc_error ("Expression in ALLOCATE statement at %L must be "
2472                  "ALLOCATABLE or a POINTER", &e->where);
2473       return FAILURE;
2474     }
2475
2476   if (pointer && dimension == 0)
2477     return SUCCESS;
2478
2479   /* Make sure the next-to-last reference node is an array specification.  */
2480
2481   if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
2482     {
2483       gfc_error ("Array specification required in ALLOCATE statement "
2484                  "at %L", &e->where);
2485       return FAILURE;
2486     }
2487
2488   if (ref2->u.ar.type == AR_ELEMENT)
2489     return SUCCESS;
2490
2491   /* Make sure that the array section reference makes sense in the
2492     context of an ALLOCATE specification.  */
2493
2494   ar = &ref2->u.ar;
2495
2496   for (i = 0; i < ar->dimen; i++)
2497     switch (ar->dimen_type[i])
2498       {
2499       case DIMEN_ELEMENT:
2500         break;
2501
2502       case DIMEN_RANGE:
2503         if (ar->start[i] != NULL
2504             && ar->end[i] != NULL
2505             && ar->stride[i] == NULL)
2506           break;
2507
2508         /* Fall Through...  */
2509
2510       case DIMEN_UNKNOWN:
2511       case DIMEN_VECTOR:
2512         gfc_error ("Bad array specification in ALLOCATE statement at %L",
2513                    &e->where);
2514         return FAILURE;
2515       }
2516
2517   return SUCCESS;
2518 }
2519
2520
2521 /************ SELECT CASE resolution subroutines ************/
2522
2523 /* Callback function for our mergesort variant.  Determines interval
2524    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
2525    op1 > op2.  Assumes we're not dealing with the default case.  
2526    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
2527    There are nine situations to check.  */
2528
2529 static int
2530 compare_cases (const gfc_case * op1, const gfc_case * op2)
2531 {
2532   int retval;
2533
2534   if (op1->low == NULL) /* op1 = (:L)  */
2535     {
2536       /* op2 = (:N), so overlap.  */
2537       retval = 0;
2538       /* op2 = (M:) or (M:N),  L < M  */
2539       if (op2->low != NULL
2540           && gfc_compare_expr (op1->high, op2->low) < 0)
2541         retval = -1;
2542     }
2543   else if (op1->high == NULL) /* op1 = (K:)  */
2544     {
2545       /* op2 = (M:), so overlap.  */
2546       retval = 0;
2547       /* op2 = (:N) or (M:N), K > N  */
2548       if (op2->high != NULL
2549           && gfc_compare_expr (op1->low, op2->high) > 0)
2550         retval = 1;
2551     }
2552   else /* op1 = (K:L)  */
2553     {
2554       if (op2->low == NULL)       /* op2 = (:N), K > N  */
2555         retval = (gfc_compare_expr (op1->low, op2->high) > 0) ? 1 : 0;
2556       else if (op2->high == NULL) /* op2 = (M:), L < M  */
2557         retval = (gfc_compare_expr (op1->high, op2->low) < 0) ? -1 : 0;
2558       else                        /* op2 = (M:N)  */
2559         {
2560           retval =  0;
2561           /* L < M  */
2562           if (gfc_compare_expr (op1->high, op2->low) < 0)
2563             retval =  -1;
2564           /* K > N  */
2565           else if (gfc_compare_expr (op1->low, op2->high) > 0)
2566             retval =  1;
2567         }
2568     }
2569
2570   return retval;
2571 }
2572
2573
2574 /* Merge-sort a double linked case list, detecting overlap in the
2575    process.  LIST is the head of the double linked case list before it
2576    is sorted.  Returns the head of the sorted list if we don't see any
2577    overlap, or NULL otherwise.  */
2578
2579 static gfc_case *
2580 check_case_overlap (gfc_case * list)
2581 {
2582   gfc_case *p, *q, *e, *tail;
2583   int insize, nmerges, psize, qsize, cmp, overlap_seen;
2584
2585   /* If the passed list was empty, return immediately.  */
2586   if (!list)
2587     return NULL;
2588
2589   overlap_seen = 0;
2590   insize = 1;
2591
2592   /* Loop unconditionally.  The only exit from this loop is a return
2593      statement, when we've finished sorting the case list.  */
2594   for (;;)
2595     {
2596       p = list;
2597       list = NULL;
2598       tail = NULL;
2599
2600       /* Count the number of merges we do in this pass.  */
2601       nmerges = 0;
2602
2603       /* Loop while there exists a merge to be done.  */
2604       while (p)
2605         {
2606           int i;
2607
2608           /* Count this merge.  */
2609           nmerges++;
2610
2611           /* Cut the list in two pieces by stepping INSIZE places
2612              forward in the list, starting from P.  */
2613           psize = 0;
2614           q = p;
2615           for (i = 0; i < insize; i++)
2616             {
2617               psize++;
2618               q = q->right;
2619               if (!q)
2620                 break;
2621             }
2622           qsize = insize;
2623
2624           /* Now we have two lists.  Merge them!  */
2625           while (psize > 0 || (qsize > 0 && q != NULL))
2626             {
2627
2628               /* See from which the next case to merge comes from.  */
2629               if (psize == 0)
2630                 {
2631                   /* P is empty so the next case must come from Q.  */
2632                   e = q;
2633                   q = q->right;
2634                   qsize--;
2635                 }
2636               else if (qsize == 0 || q == NULL)
2637                 {
2638                   /* Q is empty.  */
2639                   e = p;
2640                   p = p->right;
2641                   psize--;
2642                 }
2643               else
2644                 {
2645                   cmp = compare_cases (p, q);
2646                   if (cmp < 0)
2647                     {
2648                       /* The whole case range for P is less than the
2649                          one for Q.  */
2650                       e = p;
2651                       p = p->right;
2652                       psize--;
2653                     }
2654                   else if (cmp > 0)
2655                     {
2656                       /* The whole case range for Q is greater than
2657                          the case range for P.  */
2658                       e = q;
2659                       q = q->right;
2660                       qsize--;
2661                     }
2662                   else
2663                     {
2664                       /* The cases overlap, or they are the same
2665                          element in the list.  Either way, we must
2666                          issue an error and get the next case from P.  */
2667                       /* FIXME: Sort P and Q by line number.  */
2668                       gfc_error ("CASE label at %L overlaps with CASE "
2669                                  "label at %L", &p->where, &q->where);
2670                       overlap_seen = 1;
2671                       e = p;
2672                       p = p->right;
2673                       psize--;
2674                     }
2675                 }
2676
2677                 /* Add the next element to the merged list.  */
2678               if (tail)
2679                 tail->right = e;
2680               else
2681                 list = e;
2682               e->left = tail;
2683               tail = e;
2684             }
2685
2686           /* P has now stepped INSIZE places along, and so has Q.  So
2687              they're the same.  */
2688           p = q;
2689         }
2690       tail->right = NULL;
2691
2692       /* If we have done only one merge or none at all, we've
2693          finished sorting the cases.  */
2694       if (nmerges <= 1)
2695         {
2696           if (!overlap_seen)
2697             return list;
2698           else
2699             return NULL;
2700         }
2701
2702       /* Otherwise repeat, merging lists twice the size.  */
2703       insize *= 2;
2704     }
2705 }
2706
2707
2708 /* Check to see if an expression is suitable for use in a CASE statement.
2709    Makes sure that all case expressions are scalar constants of the same
2710    type.  Return FAILURE if anything is wrong.  */
2711
2712 static try
2713 validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
2714 {
2715   if (e == NULL) return SUCCESS;
2716
2717   if (e->ts.type != case_expr->ts.type)
2718     {
2719       gfc_error ("Expression in CASE statement at %L must be of type %s",
2720                  &e->where, gfc_basic_typename (case_expr->ts.type));
2721       return FAILURE;
2722     }
2723
2724   /* C805 (R808) For a given case-construct, each case-value shall be of
2725      the same type as case-expr.  For character type, length differences
2726      are allowed, but the kind type parameters shall be the same.  */
2727
2728   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
2729     {
2730       gfc_error("Expression in CASE statement at %L must be kind %d",
2731                 &e->where, case_expr->ts.kind);
2732       return FAILURE;
2733     }
2734
2735   /* Convert the case value kind to that of case expression kind, if needed.
2736      FIXME:  Should a warning be issued?  */
2737   if (e->ts.kind != case_expr->ts.kind)
2738     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
2739
2740   if (e->rank != 0)
2741     {
2742       gfc_error ("Expression in CASE statement at %L must be scalar",
2743                  &e->where);
2744       return FAILURE;
2745     }
2746
2747   return SUCCESS;
2748 }
2749
2750
2751 /* Given a completely parsed select statement, we:
2752
2753      - Validate all expressions and code within the SELECT.
2754      - Make sure that the selection expression is not of the wrong type.
2755      - Make sure that no case ranges overlap.
2756      - Eliminate unreachable cases and unreachable code resulting from
2757        removing case labels.
2758
2759    The standard does allow unreachable cases, e.g. CASE (5:3).  But
2760    they are a hassle for code generation, and to prevent that, we just
2761    cut them out here.  This is not necessary for overlapping cases
2762    because they are illegal and we never even try to generate code.
2763
2764    We have the additional caveat that a SELECT construct could have
2765    been a computed GOTO in the source code. Fortunately we can fairly
2766    easily work around that here: The case_expr for a "real" SELECT CASE
2767    is in code->expr1, but for a computed GOTO it is in code->expr2. All
2768    we have to do is make sure that the case_expr is a scalar integer
2769    expression.  */
2770
2771 static void
2772 resolve_select (gfc_code * code)
2773 {
2774   gfc_code *body;
2775   gfc_expr *case_expr;
2776   gfc_case *cp, *default_case, *tail, *head;
2777   int seen_unreachable;
2778   int ncases;
2779   bt type;
2780   try t;
2781
2782   if (code->expr == NULL)
2783     {
2784       /* This was actually a computed GOTO statement.  */
2785       case_expr = code->expr2;
2786       if (case_expr->ts.type != BT_INTEGER
2787           || case_expr->rank != 0)
2788         gfc_error ("Selection expression in computed GOTO statement "
2789                    "at %L must be a scalar integer expression",
2790                    &case_expr->where);
2791
2792       /* Further checking is not necessary because this SELECT was built
2793          by the compiler, so it should always be OK.  Just move the
2794          case_expr from expr2 to expr so that we can handle computed
2795          GOTOs as normal SELECTs from here on.  */
2796       code->expr = code->expr2;
2797       code->expr2 = NULL;
2798       return;
2799     }
2800
2801   case_expr = code->expr;
2802
2803   type = case_expr->ts.type;
2804   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
2805     {
2806       gfc_error ("Argument of SELECT statement at %L cannot be %s",
2807                  &case_expr->where, gfc_typename (&case_expr->ts));
2808
2809       /* Punt. Going on here just produce more garbage error messages.  */
2810       return;
2811     }
2812
2813   if (case_expr->rank != 0)
2814     {
2815       gfc_error ("Argument of SELECT statement at %L must be a scalar "
2816                  "expression", &case_expr->where);
2817
2818       /* Punt.  */
2819       return;
2820     }
2821
2822   /* PR 19168 has a long discussion concerning a mismatch of the kinds
2823      of the SELECT CASE expression and its CASE values.  Walk the lists
2824      of case values, and if we find a mismatch, promote case_expr to
2825      the appropriate kind.  */
2826
2827   if (type == BT_LOGICAL || type == BT_INTEGER)
2828     {
2829       for (body = code->block; body; body = body->block)
2830         {
2831           /* Walk the case label list.  */
2832           for (cp = body->ext.case_list; cp; cp = cp->next)
2833             {
2834               /* Intercept the DEFAULT case.  It does not have a kind.  */
2835               if (cp->low == NULL && cp->high == NULL)
2836                 continue;
2837
2838               /* Unreachable case ranges are discarded, so ignore.  */  
2839               if (cp->low != NULL && cp->high != NULL
2840                   && cp->low != cp->high
2841                   && gfc_compare_expr (cp->low, cp->high) > 0)
2842                 continue;
2843
2844               /* FIXME: Should a warning be issued?  */
2845               if (cp->low != NULL
2846                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
2847                 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
2848
2849               if (cp->high != NULL
2850                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
2851                 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
2852             }
2853          }
2854     }
2855
2856   /* Assume there is no DEFAULT case.  */
2857   default_case = NULL;
2858   head = tail = NULL;
2859   ncases = 0;
2860
2861   for (body = code->block; body; body = body->block)
2862     {
2863       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
2864       t = SUCCESS;
2865       seen_unreachable = 0;
2866
2867       /* Walk the case label list, making sure that all case labels
2868          are legal.  */
2869       for (cp = body->ext.case_list; cp; cp = cp->next)
2870         {
2871           /* Count the number of cases in the whole construct.  */
2872           ncases++;
2873
2874           /* Intercept the DEFAULT case.  */
2875           if (cp->low == NULL && cp->high == NULL)
2876             {
2877               if (default_case != NULL)
2878                 {
2879                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
2880                              "by a second DEFAULT CASE at %L",
2881                              &default_case->where, &cp->where);
2882                   t = FAILURE;
2883                   break;
2884                 }
2885               else
2886                 {
2887                   default_case = cp;
2888                   continue;
2889                 }
2890             }
2891
2892           /* Deal with single value cases and case ranges.  Errors are
2893              issued from the validation function.  */
2894           if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
2895              || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
2896             {
2897               t = FAILURE;
2898               break;
2899             }
2900
2901           if (type == BT_LOGICAL
2902               && ((cp->low == NULL || cp->high == NULL)
2903                   || cp->low != cp->high))
2904             {
2905               gfc_error
2906                 ("Logical range in CASE statement at %L is not allowed",
2907                  &cp->low->where);
2908               t = FAILURE;
2909               break;
2910             }
2911
2912           if (cp->low != NULL && cp->high != NULL
2913               && cp->low != cp->high
2914               && gfc_compare_expr (cp->low, cp->high) > 0)
2915             {
2916               if (gfc_option.warn_surprising)
2917                 gfc_warning ("Range specification at %L can never "
2918                              "be matched", &cp->where);
2919
2920               cp->unreachable = 1;
2921               seen_unreachable = 1;
2922             }
2923           else
2924             {
2925               /* If the case range can be matched, it can also overlap with
2926                  other cases.  To make sure it does not, we put it in a
2927                  double linked list here.  We sort that with a merge sort
2928                  later on to detect any overlapping cases.  */
2929               if (!head)
2930                 {
2931                   head = tail = cp;
2932                   head->right = head->left = NULL;
2933                 }
2934               else
2935                 {
2936                   tail->right = cp;
2937                   tail->right->left = tail;
2938                   tail = tail->right;
2939                   tail->right = NULL;
2940                 }
2941             }
2942         }
2943
2944       /* It there was a failure in the previous case label, give up
2945          for this case label list.  Continue with the next block.  */
2946       if (t == FAILURE)
2947         continue;
2948
2949       /* See if any case labels that are unreachable have been seen.
2950          If so, we eliminate them.  This is a bit of a kludge because
2951          the case lists for a single case statement (label) is a
2952          single forward linked lists.  */
2953       if (seen_unreachable)
2954       {
2955         /* Advance until the first case in the list is reachable.  */
2956         while (body->ext.case_list != NULL
2957                && body->ext.case_list->unreachable)
2958           {
2959             gfc_case *n = body->ext.case_list;
2960             body->ext.case_list = body->ext.case_list->next;
2961             n->next = NULL;
2962             gfc_free_case_list (n);
2963           }
2964
2965         /* Strip all other unreachable cases.  */
2966         if (body->ext.case_list)
2967           {
2968             for (cp = body->ext.case_list; cp->next; cp = cp->next)
2969               {
2970                 if (cp->next->unreachable)
2971                   {
2972                     gfc_case *n = cp->next;
2973                     cp->next = cp->next->next;
2974                     n->next = NULL;
2975                     gfc_free_case_list (n);
2976                   }
2977               }
2978           }
2979       }
2980     }
2981
2982   /* See if there were overlapping cases.  If the check returns NULL,
2983      there was overlap.  In that case we don't do anything.  If head
2984      is non-NULL, we prepend the DEFAULT case.  The sorted list can
2985      then used during code generation for SELECT CASE constructs with
2986      a case expression of a CHARACTER type.  */
2987   if (head)
2988     {
2989       head = check_case_overlap (head);
2990
2991       /* Prepend the default_case if it is there.  */
2992       if (head != NULL && default_case)
2993         {
2994           default_case->left = NULL;
2995           default_case->right = head;
2996           head->left = default_case;
2997         }
2998     }
2999
3000   /* Eliminate dead blocks that may be the result if we've seen
3001      unreachable case labels for a block.  */
3002   for (body = code; body && body->block; body = body->block)
3003     {
3004       if (body->block->ext.case_list == NULL)
3005         {
3006           /* Cut the unreachable block from the code chain.  */
3007           gfc_code *c = body->block;
3008           body->block = c->block;
3009
3010           /* Kill the dead block, but not the blocks below it.  */
3011           c->block = NULL;
3012           gfc_free_statements (c);
3013         }
3014     }
3015
3016   /* More than two cases is legal but insane for logical selects.
3017      Issue a warning for it.  */
3018   if (gfc_option.warn_surprising && type == BT_LOGICAL
3019       && ncases > 2)
3020     gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
3021                  &code->loc);
3022 }
3023
3024
3025 /* Resolve a transfer statement. This is making sure that:
3026    -- a derived type being transferred has only non-pointer components
3027    -- a derived type being transferred doesn't have private components
3028    -- we're not trying to transfer a whole assumed size array.  */
3029
3030 static void
3031 resolve_transfer (gfc_code * code)
3032 {
3033   gfc_typespec *ts;
3034   gfc_symbol *sym;
3035   gfc_ref *ref;
3036   gfc_expr *exp;
3037
3038   exp = code->expr;
3039
3040   if (exp->expr_type != EXPR_VARIABLE)
3041     return;
3042
3043   sym = exp->symtree->n.sym;
3044   ts = &sym->ts;
3045
3046   /* Go to actual component transferred.  */
3047   for (ref = code->expr->ref; ref; ref = ref->next)
3048     if (ref->type == REF_COMPONENT)
3049       ts = &ref->u.c.component->ts;
3050
3051   if (ts->type == BT_DERIVED)
3052     {
3053       /* Check that transferred derived type doesn't contain POINTER
3054          components.  */
3055       if (derived_pointer (ts->derived))
3056         {
3057           gfc_error ("Data transfer element at %L cannot have "
3058                      "POINTER components", &code->loc);
3059           return;
3060         }
3061
3062       if (ts->derived->component_access == ACCESS_PRIVATE)
3063         {
3064           gfc_error ("Data transfer element at %L cannot have "
3065                      "PRIVATE components",&code->loc);
3066           return;
3067         }
3068     }
3069
3070   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
3071       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
3072     {
3073       gfc_error ("Data transfer element at %L cannot be a full reference to "
3074                  "an assumed-size array", &code->loc);
3075       return;
3076     }
3077 }
3078
3079
3080 /*********** Toplevel code resolution subroutines ***********/
3081
3082 /* Given a branch to a label and a namespace, if the branch is conforming.
3083    The code node described where the branch is located.  */
3084
3085 static void
3086 resolve_branch (gfc_st_label * label, gfc_code * code)
3087 {
3088   gfc_code *block, *found;
3089   code_stack *stack;
3090   gfc_st_label *lp;
3091
3092   if (label == NULL)
3093     return;
3094   lp = label;
3095
3096   /* Step one: is this a valid branching target?  */
3097
3098   if (lp->defined == ST_LABEL_UNKNOWN)
3099     {
3100       gfc_error ("Label %d referenced at %L is never defined", lp->value,
3101                  &lp->where);
3102       return;
3103     }
3104
3105   if (lp->defined != ST_LABEL_TARGET)
3106     {
3107       gfc_error ("Statement at %L is not a valid branch target statement "
3108                  "for the branch statement at %L", &lp->where, &code->loc);
3109       return;
3110     }
3111
3112   /* Step two: make sure this branch is not a branch to itself ;-)  */
3113
3114   if (code->here == label)
3115     {
3116       gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
3117       return;
3118     }
3119
3120   /* Step three: Try to find the label in the parse tree. To do this,
3121      we traverse the tree block-by-block: first the block that
3122      contains this GOTO, then the block that it is nested in, etc.  We
3123      can ignore other blocks because branching into another block is
3124      not allowed.  */
3125
3126   found = NULL;
3127
3128   for (stack = cs_base; stack; stack = stack->prev)
3129     {
3130       for (block = stack->head; block; block = block->next)
3131         {
3132           if (block->here == label)
3133             {
3134               found = block;
3135               break;
3136             }
3137         }
3138
3139       if (found)
3140         break;
3141     }
3142
3143   if (found == NULL)
3144     {
3145       /* still nothing, so illegal.  */
3146       gfc_error_now ("Label at %L is not in the same block as the "
3147                      "GOTO statement at %L", &lp->where, &code->loc);
3148       return;
3149     }
3150
3151   /* Step four: Make sure that the branching target is legal if
3152      the statement is an END {SELECT,DO,IF}.  */
3153
3154   if (found->op == EXEC_NOP)
3155     {
3156       for (stack = cs_base; stack; stack = stack->prev)
3157         if (stack->current->next == found)
3158           break;
3159
3160       if (stack == NULL)
3161         gfc_notify_std (GFC_STD_F95_DEL,
3162                         "Obsolete: GOTO at %L jumps to END of construct at %L",
3163                         &code->loc, &found->loc);
3164     }
3165 }
3166
3167
3168 /* Check whether EXPR1 has the same shape as EXPR2.  */
3169
3170 static try
3171 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
3172 {
3173   mpz_t shape[GFC_MAX_DIMENSIONS];
3174   mpz_t shape2[GFC_MAX_DIMENSIONS];
3175   try result = FAILURE;
3176   int i;
3177
3178   /* Compare the rank.  */
3179   if (expr1->rank != expr2->rank)
3180     return result;
3181
3182   /* Compare the size of each dimension.  */
3183   for (i=0; i<expr1->rank; i++)
3184     {
3185       if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
3186         goto ignore;
3187
3188       if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
3189         goto ignore;
3190
3191       if (mpz_cmp (shape[i], shape2[i]))
3192         goto over;
3193     }
3194
3195   /* When either of the two expression is an assumed size array, we
3196      ignore the comparison of dimension sizes.  */
3197 ignore:
3198   result = SUCCESS;
3199
3200 over:
3201   for (i--; i>=0; i--)
3202     {
3203       mpz_clear (shape[i]);
3204       mpz_clear (shape2[i]);
3205     }
3206   return result;
3207 }
3208
3209
3210 /* Check whether a WHERE assignment target or a WHERE mask expression
3211    has the same shape as the outmost WHERE mask expression.  */
3212
3213 static void
3214 resolve_where (gfc_code *code, gfc_expr *mask)
3215 {
3216   gfc_code *cblock;
3217   gfc_code *cnext;
3218   gfc_expr *e = NULL;
3219
3220   cblock = code->block;
3221
3222   /* Store the first WHERE mask-expr of the WHERE statement or construct.
3223      In case of nested WHERE, only the outmost one is stored.  */
3224   if (mask == NULL) /* outmost WHERE */
3225     e = cblock->expr;
3226   else /* inner WHERE */
3227     e = mask;
3228
3229   while (cblock)
3230     {
3231       if (cblock->expr)
3232         {
3233           /* Check if the mask-expr has a consistent shape with the
3234              outmost WHERE mask-expr.  */
3235           if (resolve_where_shape (cblock->expr, e) == FAILURE)
3236             gfc_error ("WHERE mask at %L has inconsistent shape",
3237                        &cblock->expr->where);
3238          }
3239
3240       /* the assignment statement of a WHERE statement, or the first
3241          statement in where-body-construct of a WHERE construct */
3242       cnext = cblock->next;
3243       while (cnext)
3244         {
3245           switch (cnext->op)
3246             {
3247             /* WHERE assignment statement */
3248             case EXEC_ASSIGN:
3249
3250               /* Check shape consistent for WHERE assignment target.  */
3251               if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
3252                gfc_error ("WHERE assignment target at %L has "
3253                           "inconsistent shape", &cnext->expr->where);
3254               break;
3255
3256             /* WHERE or WHERE construct is part of a where-body-construct */
3257             case EXEC_WHERE:
3258               resolve_where (cnext, e);
3259               break;
3260
3261             default:
3262               gfc_error ("Unsupported statement inside WHERE at %L",
3263                          &cnext->loc);
3264             }
3265          /* the next statement within the same where-body-construct */
3266          cnext = cnext->next;
3267        }
3268     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3269     cblock = cblock->block;
3270   }
3271 }
3272
3273
3274 /* Check whether the FORALL index appears in the expression or not.  */
3275
3276 static try
3277 gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
3278 {
3279   gfc_array_ref ar;
3280   gfc_ref *tmp;
3281   gfc_actual_arglist *args;
3282   int i;
3283
3284   switch (expr->expr_type)
3285     {
3286     case EXPR_VARIABLE:
3287       gcc_assert (expr->symtree->n.sym);
3288
3289       /* A scalar assignment  */
3290       if (!expr->ref)
3291         {
3292           if (expr->symtree->n.sym == symbol)
3293             return SUCCESS;
3294           else
3295             return FAILURE;
3296         }
3297
3298       /* the expr is array ref, substring or struct component.  */
3299       tmp = expr->ref;
3300       while (tmp != NULL)
3301         {
3302           switch (tmp->type)
3303             {
3304             case  REF_ARRAY:
3305               /* Check if the symbol appears in the array subscript.  */
3306               ar = tmp->u.ar;
3307               for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3308                 {
3309                   if (ar.start[i])
3310                     if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
3311                       return SUCCESS;
3312
3313                   if (ar.end[i])
3314                     if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
3315                       return SUCCESS;
3316
3317                   if (ar.stride[i])
3318                     if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
3319                       return SUCCESS;
3320                 }  /* end for  */
3321               break;
3322
3323             case REF_SUBSTRING:
3324               if (expr->symtree->n.sym == symbol)
3325                 return SUCCESS;
3326               tmp = expr->ref;
3327               /* Check if the symbol appears in the substring section.  */
3328               if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3329                 return SUCCESS;
3330               if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3331                 return SUCCESS;
3332               break;
3333
3334             case REF_COMPONENT:
3335               break;
3336
3337             default:
3338               gfc_error("expresion reference type error at %L", &expr->where);
3339             }
3340           tmp = tmp->next;
3341         }
3342       break;
3343
3344     /* If the expression is a function call, then check if the symbol
3345        appears in the actual arglist of the function.  */
3346     case EXPR_FUNCTION:
3347       for (args = expr->value.function.actual; args; args = args->next)
3348         {
3349           if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
3350             return SUCCESS;
3351         }
3352       break;
3353
3354     /* It seems not to happen.  */
3355     case EXPR_SUBSTRING:
3356       if (expr->ref)
3357         {
3358           tmp = expr->ref;
3359           gcc_assert (expr->ref->type == REF_SUBSTRING);
3360           if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3361             return SUCCESS;
3362           if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3363             return SUCCESS;
3364         }
3365       break;
3366
3367     /* It seems not to happen.  */
3368     case EXPR_STRUCTURE:
3369     case EXPR_ARRAY:
3370       gfc_error ("Unsupported statement while finding forall index in "
3371                  "expression");
3372       break;
3373
3374     case EXPR_OP:
3375       /* Find the FORALL index in the first operand.  */
3376       if (expr->value.op.op1)
3377         {
3378           if (gfc_find_forall_index (expr->value.op.op1, symbol) == SUCCESS)
3379             return SUCCESS;
3380         }
3381
3382       /* Find the FORALL index in the second operand.  */
3383       if (expr->value.op.op2)
3384         {
3385           if (gfc_find_forall_index (expr->value.op.op2, symbol) == SUCCESS)
3386             return SUCCESS;
3387         }
3388       break;
3389
3390     default:
3391       break;
3392     }
3393
3394   return FAILURE;
3395 }
3396
3397
3398 /* Resolve assignment in FORALL construct.
3399    NVAR is the number of FORALL index variables, and VAR_EXPR records the
3400    FORALL index variables.  */
3401
3402 static void
3403 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
3404 {
3405   int n;
3406
3407   for (n = 0; n < nvar; n++)
3408     {
3409       gfc_symbol *forall_index;
3410
3411       forall_index = var_expr[n]->symtree->n.sym;
3412
3413       /* Check whether the assignment target is one of the FORALL index
3414          variable.  */
3415       if ((code->expr->expr_type == EXPR_VARIABLE)
3416           && (code->expr->symtree->n.sym == forall_index))
3417         gfc_error ("Assignment to a FORALL index variable at %L",
3418                    &code->expr->where);
3419       else
3420         {
3421           /* If one of the FORALL index variables doesn't appear in the
3422              assignment target, then there will be a many-to-one
3423              assignment.  */
3424           if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
3425             gfc_error ("The FORALL with index '%s' cause more than one "
3426                        "assignment to this object at %L",
3427                        var_expr[n]->symtree->name, &code->expr->where);
3428         }
3429     }
3430 }
3431
3432
3433 /* Resolve WHERE statement in FORALL construct.  */
3434
3435 static void
3436 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
3437   gfc_code *cblock;
3438   gfc_code *cnext;
3439
3440   cblock = code->block;
3441   while (cblock)
3442     {
3443       /* the assignment statement of a WHERE statement, or the first
3444          statement in where-body-construct of a WHERE construct */
3445       cnext = cblock->next;
3446       while (cnext)
3447         {
3448           switch (cnext->op)
3449             {
3450             /* WHERE assignment statement */
3451             case EXEC_ASSIGN:
3452               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
3453               break;
3454
3455             /* WHERE or WHERE construct is part of a where-body-construct */
3456             case EXEC_WHERE:
3457               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
3458               break;
3459
3460             default:
3461               gfc_error ("Unsupported statement inside WHERE at %L",
3462                          &cnext->loc);
3463             }
3464           /* the next statement within the same where-body-construct */
3465           cnext = cnext->next;
3466         }
3467       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3468       cblock = cblock->block;
3469     }
3470 }
3471
3472
3473 /* Traverse the FORALL body to check whether the following errors exist:
3474    1. For assignment, check if a many-to-one assignment happens.
3475    2. For WHERE statement, check the WHERE body to see if there is any
3476       many-to-one assignment.  */
3477
3478 static void
3479 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
3480 {
3481   gfc_code *c;
3482
3483   c = code->block->next;
3484   while (c)
3485     {
3486       switch (c->op)
3487         {
3488         case EXEC_ASSIGN:
3489         case EXEC_POINTER_ASSIGN:
3490           gfc_resolve_assign_in_forall (c, nvar, var_expr);
3491           break;
3492
3493         /* Because the resolve_blocks() will handle the nested FORALL,
3494            there is no need to handle it here.  */
3495         case EXEC_FORALL:
3496           break;
3497         case EXEC_WHERE:
3498           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
3499           break;
3500         default:
3501           break;
3502         }
3503       /* The next statement in the FORALL body.  */
3504       c = c->next;
3505     }
3506 }
3507
3508
3509 /* Given a FORALL construct, first resolve the FORALL iterator, then call
3510    gfc_resolve_forall_body to resolve the FORALL body.  */
3511
3512 static void resolve_blocks (gfc_code *, gfc_namespace *);
3513
3514 static void
3515 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
3516 {
3517   static gfc_expr **var_expr;
3518   static int total_var = 0;
3519   static int nvar = 0;
3520   gfc_forall_iterator *fa;
3521   gfc_symbol *forall_index;
3522   gfc_code *next;
3523   int i;
3524
3525   /* Start to resolve a FORALL construct   */
3526   if (forall_save == 0)
3527     {
3528       /* Count the total number of FORALL index in the nested FORALL
3529          construct in order to allocate the VAR_EXPR with proper size.  */
3530       next = code;
3531       while ((next != NULL) && (next->op == EXEC_FORALL))
3532         {
3533           for (fa = next->ext.forall_iterator; fa; fa = fa->next)
3534             total_var ++;
3535           next = next->block->next;
3536         }
3537
3538       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
3539       var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
3540     }
3541
3542   /* The information about FORALL iterator, including FORALL index start, end
3543      and stride. The FORALL index can not appear in start, end or stride.  */
3544   for (fa = code->ext.forall_iterator; fa; fa = fa->next)
3545     {
3546       /* Check if any outer FORALL index name is the same as the current
3547          one.  */
3548       for (i = 0; i < nvar; i++)
3549         {
3550           if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
3551             {
3552               gfc_error ("An outer FORALL construct already has an index "
3553                          "with this name %L", &fa->var->where);
3554             }
3555         }
3556
3557       /* Record the current FORALL index.  */
3558       var_expr[nvar] = gfc_copy_expr (fa->var);
3559
3560       forall_index = fa->var->symtree->n.sym;
3561
3562       /* Check if the FORALL index appears in start, end or stride.  */
3563       if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
3564         gfc_error ("A FORALL index must not appear in a limit or stride "
3565                    "expression in the same FORALL at %L", &fa->start->where);
3566       if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
3567         gfc_error ("A FORALL index must not appear in a limit or stride "
3568                    "expression in the same FORALL at %L", &fa->end->where);
3569       if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
3570         gfc_error ("A FORALL index must not appear in a limit or stride "
3571                    "expression in the same FORALL at %L", &fa->stride->where);
3572       nvar++;
3573     }
3574
3575   /* Resolve the FORALL body.  */
3576   gfc_resolve_forall_body (code, nvar, var_expr);
3577
3578   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
3579   resolve_blocks (code->block, ns);
3580
3581   /* Free VAR_EXPR after the whole FORALL construct resolved.  */
3582   for (i = 0; i < total_var; i++)
3583     gfc_free_expr (var_expr[i]);
3584
3585   /* Reset the counters.  */
3586   total_var = 0;
3587   nvar = 0;
3588 }
3589
3590
3591 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
3592    DO code nodes.  */
3593
3594 static void resolve_code (gfc_code *, gfc_namespace *);
3595
3596 static void
3597 resolve_blocks (gfc_code * b, gfc_namespace * ns)
3598 {
3599   try t;
3600
3601   for (; b; b = b->block)
3602     {
3603       t = gfc_resolve_expr (b->expr);
3604       if (gfc_resolve_expr (b->expr2) == FAILURE)
3605         t = FAILURE;
3606
3607       switch (b->op)
3608         {
3609         case EXEC_IF:
3610           if (t == SUCCESS && b->expr != NULL
3611               && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
3612             gfc_error
3613               ("ELSE IF clause at %L requires a scalar LOGICAL expression",
3614                &b->expr->where);
3615           break;
3616
3617         case EXEC_WHERE:
3618           if (t == SUCCESS
3619               && b->expr != NULL
3620               && (b->expr->ts.type != BT_LOGICAL
3621                   || b->expr->rank == 0))
3622             gfc_error
3623               ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
3624                &b->expr->where);
3625           break;
3626
3627         case EXEC_GOTO:
3628           resolve_branch (b->label, b);
3629           break;
3630
3631         case EXEC_SELECT:
3632         case EXEC_FORALL:
3633         case EXEC_DO:
3634         case EXEC_DO_WHILE:
3635           break;
3636
3637         default:
3638           gfc_internal_error ("resolve_block(): Bad block type");
3639         }
3640
3641       resolve_code (b->next, ns);
3642     }
3643 }
3644
3645
3646 /* Given a block of code, recursively resolve everything pointed to by this
3647    code block.  */
3648
3649 static void
3650 resolve_code (gfc_code * code, gfc_namespace * ns)
3651 {
3652   int forall_save = 0;
3653   code_stack frame;
3654   gfc_alloc *a;
3655   try t;
3656
3657   frame.prev = cs_base;
3658   frame.head = code;
3659   cs_base = &frame;
3660
3661   for (; code; code = code->next)
3662     {
3663       frame.current = code;
3664
3665       if (code->op == EXEC_FORALL)
3666         {
3667           forall_save = forall_flag;
3668           forall_flag = 1;
3669           gfc_resolve_forall (code, ns, forall_save);
3670         }
3671       else
3672         resolve_blocks (code->block, ns);
3673
3674       if (code->op == EXEC_FORALL)
3675         forall_flag = forall_save;
3676
3677       t = gfc_resolve_expr (code->expr);
3678       if (gfc_resolve_expr (code->expr2) == FAILURE)
3679         t = FAILURE;
3680
3681       switch (code->op)
3682         {
3683         case EXEC_NOP:
3684         case EXEC_CYCLE:
3685         case EXEC_PAUSE:
3686         case EXEC_STOP:
3687         case EXEC_EXIT:
3688         case EXEC_CONTINUE:
3689         case EXEC_DT_END:
3690         case EXEC_ENTRY:
3691           break;
3692
3693         case EXEC_WHERE:
3694           resolve_where (code, NULL);
3695           break;
3696
3697         case EXEC_GOTO:
3698           if (code->expr != NULL)
3699             {
3700               if (code->expr->ts.type != BT_INTEGER)
3701                 gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
3702                        "variable", &code->expr->where);
3703               else if (code->expr->symtree->n.sym->attr.assign != 1)
3704                 gfc_error ("Variable '%s' has not been assigned a target label "
3705                         "at %L", code->expr->symtree->n.sym->name,
3706                         &code->expr->where);
3707             }
3708           else
3709             resolve_branch (code->label, code);
3710           break;
3711
3712         case EXEC_RETURN:
3713           if (code->expr != NULL && code->expr->ts.type != BT_INTEGER)
3714             gfc_error ("Alternate RETURN statement at %L requires an INTEGER "
3715                        "return specifier", &code->expr->where);
3716           break;
3717
3718         case EXEC_ASSIGN:
3719           if (t == FAILURE)
3720             break;
3721
3722           if (gfc_extend_assign (code, ns) == SUCCESS)
3723             goto call;
3724
3725           if (gfc_pure (NULL))
3726             {
3727               if (gfc_impure_variable (code->expr->symtree->n.sym))
3728                 {
3729                   gfc_error
3730                     ("Cannot assign to variable '%s' in PURE procedure at %L",
3731                      code->expr->symtree->n.sym->name, &code->expr->where);
3732                   break;
3733                 }
3734
3735               if (code->expr2->ts.type == BT_DERIVED
3736                   && derived_pointer (code->expr2->ts.derived))
3737                 {
3738                   gfc_error
3739                     ("Right side of assignment at %L is a derived type "
3740                      "containing a POINTER in a PURE procedure",
3741                      &code->expr2->where);
3742                   break;
3743                 }
3744             }
3745
3746           gfc_check_assign (code->expr, code->expr2, 1);
3747           break;
3748
3749         case EXEC_LABEL_ASSIGN:
3750           if (code->label->defined == ST_LABEL_UNKNOWN)
3751             gfc_error ("Label %d referenced at %L is never defined",
3752                        code->label->value, &code->label->where);
3753           if (t == SUCCESS
3754               && (code->expr->expr_type != EXPR_VARIABLE
3755                   || code->expr->symtree->n.sym->ts.type != BT_INTEGER
3756                   || code->expr->symtree->n.sym->ts.kind 
3757                         != gfc_default_integer_kind
3758                   || code->expr->symtree->n.sym->as != NULL))
3759             gfc_error ("ASSIGN statement at %L requires a scalar "
3760                        "default INTEGER variable", &code->expr->where);
3761           break;
3762
3763         case EXEC_POINTER_ASSIGN:
3764           if (t == FAILURE)
3765             break;
3766
3767           gfc_check_pointer_assign (code->expr, code->expr2);
3768           break;
3769
3770         case EXEC_ARITHMETIC_IF:
3771           if (t == SUCCESS
3772               && code->expr->ts.type != BT_INTEGER
3773               && code->expr->ts.type != BT_REAL)
3774             gfc_error ("Arithmetic IF statement at %L requires a numeric "
3775                        "expression", &code->expr->where);
3776
3777           resolve_branch (code->label, code);
3778           resolve_branch (code->label2, code);
3779           resolve_branch (code->label3, code);
3780           break;
3781
3782         case EXEC_IF:
3783           if (t == SUCCESS && code->expr != NULL
3784               && (code->expr->ts.type != BT_LOGICAL
3785                   || code->expr->rank != 0))
3786             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
3787                        &code->expr->where);
3788           break;
3789
3790         case EXEC_CALL:
3791         call:
3792           resolve_call (code);
3793           break;
3794
3795         case EXEC_SELECT:
3796           /* Select is complicated. Also, a SELECT construct could be
3797              a transformed computed GOTO.  */
3798           resolve_select (code);
3799           break;
3800
3801         case EXEC_DO:
3802           if (code->ext.iterator != NULL)
3803             gfc_resolve_iterator (code->ext.iterator, true);
3804           break;
3805
3806         case EXEC_DO_WHILE:
3807           if (code->expr == NULL)
3808             gfc_internal_error ("resolve_code(): No expression on DO WHILE");
3809           if (t == SUCCESS
3810               && (code->expr->rank != 0
3811                   || code->expr->ts.type != BT_LOGICAL))
3812             gfc_error ("Exit condition of DO WHILE loop at %L must be "
3813                        "a scalar LOGICAL expression", &code->expr->where);
3814           break;
3815
3816         case EXEC_ALLOCATE:
3817           if (t == SUCCESS && code->expr != NULL
3818               && code->expr->ts.type != BT_INTEGER)
3819             gfc_error ("STAT tag in ALLOCATE statement at %L must be "
3820                        "of type INTEGER", &code->expr->where);
3821
3822           for (a = code->ext.alloc_list; a; a = a->next)
3823             resolve_allocate_expr (a->expr);
3824
3825           break;
3826
3827         case EXEC_DEALLOCATE:
3828           if (t == SUCCESS && code->expr != NULL
3829               && code->expr->ts.type != BT_INTEGER)
3830             gfc_error
3831               ("STAT tag in DEALLOCATE statement at %L must be of type "
3832                "INTEGER", &code->expr->where);
3833
3834           for (a = code->ext.alloc_list; a; a = a->next)
3835             resolve_deallocate_expr (a->expr);
3836
3837           break;
3838
3839         case EXEC_OPEN:
3840           if (gfc_resolve_open (code->ext.open) == FAILURE)
3841             break;
3842
3843           resolve_branch (code->ext.open->err, code);
3844           break;
3845
3846         case EXEC_CLOSE:
3847           if (gfc_resolve_close (code->ext.close) == FAILURE)
3848             break;
3849
3850           resolve_branch (code->ext.close->err, code);
3851           break;
3852
3853         case EXEC_BACKSPACE:
3854         case EXEC_ENDFILE:
3855         case EXEC_REWIND:
3856           if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
3857             break;
3858
3859           resolve_branch (code->ext.filepos->err, code);
3860           break;
3861
3862         case EXEC_INQUIRE:
3863           if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
3864               break;
3865
3866           resolve_branch (code->ext.inquire->err, code);
3867           break;
3868
3869         case EXEC_IOLENGTH:
3870           gcc_assert (code->ext.inquire != NULL);
3871           if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
3872             break;
3873
3874           resolve_branch (code->ext.inquire->err, code);
3875           break;
3876
3877         case EXEC_READ:
3878         case EXEC_WRITE:
3879           if (gfc_resolve_dt (code->ext.dt) == FAILURE)
3880             break;
3881
3882           resolve_branch (code->ext.dt->err, code);
3883           resolve_branch (code->ext.dt->end, code);
3884           resolve_branch (code->ext.dt->eor, code);
3885           break;
3886
3887         case EXEC_TRANSFER:
3888           resolve_transfer (code);
3889           break;
3890
3891         case EXEC_FORALL:
3892           resolve_forall_iterators (code->ext.forall_iterator);
3893
3894           if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
3895             gfc_error
3896               ("FORALL mask clause at %L requires a LOGICAL expression",
3897                &code->expr->where);
3898           break;
3899
3900         default:
3901           gfc_internal_error ("resolve_code(): Bad statement code");
3902         }
3903     }
3904
3905   cs_base = frame.prev;
3906 }
3907
3908
3909 /* Resolve initial values and make sure they are compatible with
3910    the variable.  */
3911
3912 static void
3913 resolve_values (gfc_symbol * sym)
3914 {
3915
3916   if (sym->value == NULL)
3917     return;
3918
3919   if (gfc_resolve_expr (sym->value) == FAILURE)
3920     return;
3921
3922   gfc_check_assign_symbol (sym, sym->value);
3923 }
3924
3925
3926 /* Do anything necessary to resolve a symbol.  Right now, we just
3927    assume that an otherwise unknown symbol is a variable.  This sort
3928    of thing commonly happens for symbols in module.  */
3929
3930 static void
3931 resolve_symbol (gfc_symbol * sym)
3932 {
3933   /* Zero if we are checking a formal namespace.  */
3934   static int formal_ns_flag = 1;
3935   int formal_ns_save, check_constant, mp_flag;
3936   int i;
3937   const char *whynot;
3938   gfc_namelist *nl;
3939
3940   if (sym->attr.flavor == FL_UNKNOWN)
3941     {
3942       if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
3943         sym->attr.flavor = FL_VARIABLE;
3944       else
3945         {
3946           sym->attr.flavor = FL_PROCEDURE;
3947           if (sym->attr.dimension)
3948             sym->attr.function = 1;
3949         }
3950     }
3951
3952   /* Symbols that are module procedures with results (functions) have
3953      the types and array specification copied for type checking in
3954      procedures that call them, as well as for saving to a module
3955      file.  These symbols can't stand the scrutiny that their results
3956      can.  */
3957   mp_flag = (sym->result != NULL && sym->result != sym);
3958
3959   /* Assign default type to symbols that need one and don't have one.  */
3960   if (sym->ts.type == BT_UNKNOWN)
3961     {
3962       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
3963         gfc_set_default_type (sym, 1, NULL);
3964
3965       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
3966         {
3967           if (!mp_flag)
3968             gfc_set_default_type (sym, 0, NULL);
3969           else
3970             {
3971               /* Result may be in another namespace.  */
3972               resolve_symbol (sym->result);
3973
3974               sym->ts = sym->result->ts;
3975               sym->as = gfc_copy_array_spec (sym->result->as);
3976             }
3977         }
3978     }
3979
3980   /* Assumed size arrays and assumed shape arrays must be dummy
3981      arguments.  */ 
3982
3983   if (sym->as != NULL
3984       && (sym->as->type == AS_ASSUMED_SIZE
3985           || sym->as->type == AS_ASSUMED_SHAPE)
3986       && sym->attr.dummy == 0)
3987     {
3988       gfc_error ("Assumed %s array at %L must be a dummy argument",
3989                  sym->as->type == AS_ASSUMED_SIZE ? "size" : "shape",
3990                  &sym->declared_at);
3991       return;
3992     }
3993
3994   /* A parameter array's shape needs to be constant.  */
3995
3996   if (sym->attr.flavor == FL_PARAMETER && sym->as != NULL 
3997       && !gfc_is_compile_time_shape (sym->as))
3998     {
3999       gfc_error ("Parameter array '%s' at %L cannot be automatic "
4000                  "or assumed shape", sym->name, &sym->declared_at);
4001           return;
4002     }
4003
4004   /* Make sure that character string variables with assumed length are
4005      dummy arguments.  */
4006
4007   if (sym->attr.flavor == FL_VARIABLE && !sym->attr.result
4008       && sym->ts.type == BT_CHARACTER
4009       && sym->ts.cl->length == NULL && sym->attr.dummy == 0)
4010     {
4011       gfc_error ("Entity with assumed character length at %L must be a "
4012                  "dummy argument or a PARAMETER", &sym->declared_at);
4013       return;
4014     }
4015
4016   /* Make sure a parameter that has been implicitly typed still
4017      matches the implicit type, since PARAMETER statements can precede
4018      IMPLICIT statements.  */
4019
4020   if (sym->attr.flavor == FL_PARAMETER
4021       && sym->attr.implicit_type
4022       && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym, sym->ns)))
4023     gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
4024                "later IMPLICIT type", sym->name, &sym->declared_at);
4025
4026   /* Make sure the types of derived parameters are consistent.  This
4027      type checking is deferred until resolution because the type may
4028      refer to a derived type from the host.  */
4029
4030   if (sym->attr.flavor == FL_PARAMETER
4031       && sym->ts.type == BT_DERIVED
4032       && !gfc_compare_types (&sym->ts, &sym->value->ts))
4033     gfc_error ("Incompatible derived type in PARAMETER at %L",
4034                &sym->value->where);
4035
4036   /* Make sure symbols with known intent or optional are really dummy
4037      variable.  Because of ENTRY statement, this has to be deferred
4038      until resolution time.  */
4039
4040   if (! sym->attr.dummy
4041       && (sym->attr.optional
4042           || sym->attr.intent != INTENT_UNKNOWN))
4043     {
4044       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
4045       return;
4046     }
4047
4048   if (sym->attr.proc == PROC_ST_FUNCTION)
4049     {
4050       if (sym->ts.type == BT_CHARACTER)
4051         {
4052           gfc_charlen *cl = sym->ts.cl;
4053           if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
4054             {
4055               gfc_error ("Character-valued statement function '%s' at %L must "
4056                          "have constant length", sym->name, &sym->declared_at);
4057               return;
4058             }
4059         }
4060     }
4061
4062   /* Constraints on deferred shape variable.  */
4063   if (sym->attr.flavor == FL_VARIABLE
4064       || (sym->attr.flavor == FL_PROCEDURE
4065           && sym->attr.function))
4066     {
4067       if (sym->as == NULL || sym->as->type != AS_DEFERRED)
4068         {
4069           if (sym->attr.allocatable)
4070             {
4071               if (sym->attr.dimension)
4072                 gfc_error ("Allocatable array at %L must have a deferred shape",
4073                            &sym->declared_at);
4074               else
4075                 gfc_error ("Object at %L may not be ALLOCATABLE",
4076                            &sym->declared_at);
4077               return;
4078             }
4079
4080           if (sym->attr.pointer && sym->attr.dimension)
4081             {
4082               gfc_error ("Pointer to array at %L must have a deferred shape",
4083                          &sym->declared_at);
4084               return;
4085             }
4086
4087         }
4088       else
4089         {
4090           if (!mp_flag && !sym->attr.allocatable
4091               && !sym->attr.pointer && !sym->attr.dummy)
4092             {
4093               gfc_error ("Array at %L cannot have a deferred shape",
4094                          &sym->declared_at);
4095               return;
4096             }
4097         }
4098     }
4099
4100   switch (sym->attr.flavor)
4101     {
4102     case FL_VARIABLE:
4103       /* Can the sybol have an initializer?  */
4104       whynot = NULL;
4105       if (sym->attr.allocatable)
4106         whynot = "Allocatable";
4107       else if (sym->attr.external)
4108         whynot = "External";
4109       else if (sym->attr.dummy)
4110         whynot = "Dummy";
4111       else if (sym->attr.intrinsic)
4112         whynot = "Intrinsic";
4113       else if (sym->attr.result)
4114         whynot = "Function Result";
4115       else if (sym->attr.dimension && !sym->attr.pointer)
4116         {
4117           /* Don't allow initialization of automatic arrays.  */
4118           for (i = 0; i < sym->as->rank; i++)
4119             {
4120               if (sym->as->lower[i] == NULL
4121                   || sym->as->lower[i]->expr_type != EXPR_CONSTANT
4122                   || sym->as->upper[i] == NULL
4123                   || sym->as->upper[i]->expr_type != EXPR_CONSTANT)
4124                 {
4125                   whynot = "Automatic array";
4126                   break;
4127                 }
4128             }
4129         }
4130
4131       /* Reject illegal initializers.  */
4132       if (sym->value && whynot)
4133         {
4134           gfc_error ("%s '%s' at %L cannot have an initializer",
4135                      whynot, sym->name, &sym->declared_at);
4136           return;
4137         }
4138
4139       /* Assign default initializer.  */
4140       if (sym->ts.type == BT_DERIVED && !(sym->value || whynot))
4141         sym->value = gfc_default_initializer (&sym->ts);
4142       break;
4143
4144     case FL_NAMELIST:
4145       /* Reject PRIVATE objects in a PUBLIC namelist.  */
4146       if (gfc_check_access(sym->attr.access, sym->ns->default_access))
4147         {
4148           for (nl = sym->namelist; nl; nl = nl->next)
4149             {
4150               if (!gfc_check_access(nl->sym->attr.access,
4151                                     nl->sym->ns->default_access))
4152                 gfc_error ("PRIVATE symbol '%s' cannot be member of "
4153                            "PUBLIC namelist at %L", nl->sym->name,
4154                            &sym->declared_at);
4155             }
4156         }
4157       break;
4158
4159     default:
4160       break;
4161     }
4162
4163
4164   /* Make sure that intrinsic exist */
4165   if (sym->attr.intrinsic
4166       && ! gfc_intrinsic_name(sym->name, 0)
4167       && ! gfc_intrinsic_name(sym->name, 1))
4168     gfc_error("Intrinsic at %L does not exist", &sym->declared_at);
4169
4170   /* Resolve array specifier. Check as well some constraints
4171      on COMMON blocks.  */
4172
4173   check_constant = sym->attr.in_common && !sym->attr.pointer;
4174   gfc_resolve_array_spec (sym->as, check_constant);
4175
4176   /* Resolve formal namespaces.  */
4177
4178   if (formal_ns_flag && sym != NULL && sym->formal_ns != NULL)
4179     {
4180       formal_ns_save = formal_ns_flag;
4181       formal_ns_flag = 0;
4182       gfc_resolve (sym->formal_ns);
4183       formal_ns_flag = formal_ns_save;
4184     }
4185 }
4186
4187
4188
4189 /************* Resolve DATA statements *************/
4190
4191 static struct
4192 {
4193   gfc_data_value *vnode;
4194   unsigned int left;
4195 }
4196 values;
4197
4198
4199 /* Advance the values structure to point to the next value in the data list.  */
4200
4201 static try
4202 next_data_value (void)
4203 {
4204   while (values.left == 0)
4205     {
4206       if (values.vnode->next == NULL)
4207         return FAILURE;
4208
4209       values.vnode = values.vnode->next;
4210       values.left = values.vnode->repeat;
4211     }
4212
4213   return SUCCESS;
4214 }
4215
4216
4217 static try
4218 check_data_variable (gfc_data_variable * var, locus * where)
4219 {
4220   gfc_expr *e;
4221   mpz_t size;
4222   mpz_t offset;
4223   try t;
4224   ar_type mark = AR_UNKNOWN;
4225   int i;
4226   mpz_t section_index[GFC_MAX_DIMENSIONS];
4227   gfc_ref *ref;
4228   gfc_array_ref *ar;
4229
4230   if (gfc_resolve_expr (var->expr) == FAILURE)
4231     return FAILURE;
4232
4233   ar = NULL;
4234   mpz_init_set_si (offset, 0);
4235   e = var->expr;
4236
4237   if (e->expr_type != EXPR_VARIABLE)
4238     gfc_internal_error ("check_data_variable(): Bad expression");
4239
4240   if (e->rank == 0)
4241     {
4242       mpz_init_set_ui (size, 1);
4243       ref = NULL;
4244     }
4245   else
4246     {
4247       ref = e->ref;
4248
4249       /* Find the array section reference.  */
4250       for (ref = e->ref; ref; ref = ref->next)
4251         {
4252           if (ref->type != REF_ARRAY)
4253             continue;
4254           if (ref->u.ar.type == AR_ELEMENT)
4255             continue;
4256           break;
4257         }
4258       gcc_assert (ref);
4259
4260       /* Set marks according to the reference pattern.  */
4261       switch (ref->u.ar.type)
4262         {
4263         case AR_FULL:
4264           mark = AR_FULL;
4265           break;
4266
4267         case AR_SECTION:
4268           ar = &ref->u.ar;
4269           /* Get the start position of array section.  */
4270           gfc_get_section_index (ar, section_index, &offset);
4271           mark = AR_SECTION;
4272           break;
4273
4274         default:
4275           gcc_unreachable ();
4276         }
4277
4278       if (gfc_array_size (e, &size) == FAILURE)
4279         {
4280           gfc_error ("Nonconstant array section at %L in DATA statement",
4281                      &e->where);
4282           mpz_clear (offset);
4283           return FAILURE;
4284         }
4285     }
4286
4287   t = SUCCESS;
4288
4289   while (mpz_cmp_ui (size, 0) > 0)
4290     {
4291       if (next_data_value () == FAILURE)
4292         {
4293           gfc_error ("DATA statement at %L has more variables than values",
4294                      where);
4295           t = FAILURE;
4296           break;
4297         }
4298
4299       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
4300       if (t == FAILURE)
4301         break;
4302
4303       /* If we have more than one element left in the repeat count,
4304          and we have more than one element left in the target variable,
4305          then create a range assignment.  */
4306       /* ??? Only done for full arrays for now, since array sections
4307          seem tricky.  */
4308       if (mark == AR_FULL && ref && ref->next == NULL
4309           && values.left > 1 && mpz_cmp_ui (size, 1) > 0)
4310         {
4311           mpz_t range;
4312
4313           if (mpz_cmp_ui (size, values.left) >= 0)
4314             {
4315               mpz_init_set_ui (range, values.left);
4316               mpz_sub_ui (size, size, values.left);
4317               values.left = 0;
4318             }
4319           else
4320             {
4321               mpz_init_set (range, size);
4322               values.left -= mpz_get_ui (size);
4323               mpz_set_ui (size, 0);
4324             }
4325
4326           gfc_assign_data_value_range (var->expr, values.vnode->expr,
4327                                        offset, range);
4328
4329           mpz_add (offset, offset, range);
4330           mpz_clear (range);
4331         }
4332
4333       /* Assign initial value to symbol.  */
4334       else
4335         {
4336           values.left -= 1;
4337           mpz_sub_ui (size, size, 1);
4338
4339           gfc_assign_data_value (var->expr, values.vnode->expr, offset);
4340
4341           if (mark == AR_FULL)
4342             mpz_add_ui (offset, offset, 1);
4343
4344           /* Modify the array section indexes and recalculate the offset
4345              for next element.  */
4346           else if (mark == AR_SECTION)
4347             gfc_advance_section (section_index, ar, &offset);
4348         }
4349     }
4350
4351   if (mark == AR_SECTION)
4352     {
4353       for (i = 0; i < ar->dimen; i++)
4354         mpz_clear (section_index[i]);
4355     }
4356
4357   mpz_clear (size);
4358   mpz_clear (offset);
4359
4360   return t;
4361 }
4362
4363
4364 static try traverse_data_var (gfc_data_variable *, locus *);
4365
4366 /* Iterate over a list of elements in a DATA statement.  */
4367
4368 static try
4369 traverse_data_list (gfc_data_variable * var, locus * where)
4370 {
4371   mpz_t trip;
4372   iterator_stack frame;
4373   gfc_expr *e;
4374
4375   mpz_init (frame.value);
4376
4377   mpz_init_set (trip, var->iter.end->value.integer);
4378   mpz_sub (trip, trip, var->iter.start->value.integer);
4379   mpz_add (trip, trip, var->iter.step->value.integer);
4380
4381   mpz_div (trip, trip, var->iter.step->value.integer);
4382
4383   mpz_set (frame.value, var->iter.start->value.integer);
4384
4385   frame.prev = iter_stack;
4386   frame.variable = var->iter.var->symtree;
4387   iter_stack = &frame;
4388
4389   while (mpz_cmp_ui (trip, 0) > 0)
4390     {
4391       if (traverse_data_var (var->list, where) == FAILURE)
4392         {
4393           mpz_clear (trip);
4394           return FAILURE;
4395         }
4396
4397       e = gfc_copy_expr (var->expr);
4398       if (gfc_simplify_expr (e, 1) == FAILURE)
4399         {
4400           gfc_free_expr (e);
4401           return FAILURE;
4402         }
4403
4404       mpz_add (frame.value, frame.value, var->iter.step->value.integer);
4405
4406       mpz_sub_ui (trip, trip, 1);
4407     }
4408
4409   mpz_clear (trip);
4410   mpz_clear (frame.value);
4411
4412   iter_stack = frame.prev;
4413   return SUCCESS;
4414 }
4415
4416
4417 /* Type resolve variables in the variable list of a DATA statement.  */
4418
4419 static try
4420 traverse_data_var (gfc_data_variable * var, locus * where)
4421 {
4422   try t;
4423
4424   for (; var; var = var->next)
4425     {
4426       if (var->expr == NULL)
4427         t = traverse_data_list (var, where);
4428       else
4429         t = check_data_variable (var, where);
4430
4431       if (t == FAILURE)
4432         return FAILURE;
4433     }
4434
4435   return SUCCESS;
4436 }
4437
4438
4439 /* Resolve the expressions and iterators associated with a data statement.
4440    This is separate from the assignment checking because data lists should
4441    only be resolved once.  */
4442
4443 static try
4444 resolve_data_variables (gfc_data_variable * d)
4445 {
4446   for (; d; d = d->next)
4447     {
4448       if (d->list == NULL)
4449         {
4450           if (gfc_resolve_expr (d->expr) == FAILURE)
4451             return FAILURE;
4452         }
4453       else
4454         {
4455           if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
4456             return FAILURE;
4457
4458           if (d->iter.start->expr_type != EXPR_CONSTANT
4459               || d->iter.end->expr_type != EXPR_CONSTANT
4460               || d->iter.step->expr_type != EXPR_CONSTANT)
4461             gfc_internal_error ("resolve_data_variables(): Bad iterator");
4462
4463           if (resolve_data_variables (d->list) == FAILURE)
4464             return FAILURE;
4465         }
4466     }
4467
4468   return SUCCESS;
4469 }
4470
4471
4472 /* Resolve a single DATA statement.  We implement this by storing a pointer to
4473    the value list into static variables, and then recursively traversing the
4474    variables list, expanding iterators and such.  */
4475
4476 static void
4477 resolve_data (gfc_data * d)
4478 {
4479   if (resolve_data_variables (d->var) == FAILURE)
4480     return;
4481
4482   values.vnode = d->value;
4483   values.left = (d->value == NULL) ? 0 : d->value->repeat;
4484
4485   if (traverse_data_var (d->var, &d->where) == FAILURE)
4486     return;
4487
4488   /* At this point, we better not have any values left.  */
4489
4490   if (next_data_value () == SUCCESS)
4491     gfc_error ("DATA statement at %L has more values than variables",
4492                &d->where);
4493 }
4494
4495
4496 /* Determines if a variable is not 'pure', ie not assignable within a pure
4497    procedure.  Returns zero if assignment is OK, nonzero if there is a problem.
4498  */
4499
4500 int
4501 gfc_impure_variable (gfc_symbol * sym)
4502 {
4503   if (sym->attr.use_assoc || sym->attr.in_common)
4504     return 1;
4505
4506   if (sym->ns != gfc_current_ns)
4507     return !sym->attr.function;
4508
4509   /* TODO: Check storage association through EQUIVALENCE statements */
4510
4511   return 0;
4512 }
4513
4514
4515 /* Test whether a symbol is pure or not.  For a NULL pointer, checks the
4516    symbol of the current procedure.  */
4517
4518 int
4519 gfc_pure (gfc_symbol * sym)
4520 {
4521   symbol_attribute attr;
4522
4523   if (sym == NULL)
4524     sym = gfc_current_ns->proc_name;
4525   if (sym == NULL)
4526     return 0;
4527
4528   attr = sym->attr;
4529
4530   return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
4531 }
4532
4533
4534 /* Test whether the current procedure is elemental or not.  */
4535
4536 int
4537 gfc_elemental (gfc_symbol * sym)
4538 {
4539   symbol_attribute attr;
4540
4541   if (sym == NULL)
4542     sym = gfc_current_ns->proc_name;
4543   if (sym == NULL)
4544     return 0;
4545   attr = sym->attr;
4546
4547   return attr.flavor == FL_PROCEDURE && attr.elemental;
4548 }
4549
4550
4551 /* Warn about unused labels.  */
4552
4553 static void
4554 warn_unused_label (gfc_namespace * ns)
4555 {
4556   gfc_st_label *l;
4557
4558   l = ns->st_labels;
4559   if (l == NULL)
4560     return;
4561
4562   while (l->next)
4563     l = l->next;
4564
4565   for (; l; l = l->prev)
4566     {
4567       if (l->defined == ST_LABEL_UNKNOWN)
4568         continue;
4569
4570       switch (l->referenced)
4571         {
4572         case ST_LABEL_UNKNOWN:
4573           gfc_warning ("Label %d at %L defined but not used", l->value,
4574                        &l->where);
4575           break;
4576
4577         case ST_LABEL_BAD_TARGET:
4578           gfc_warning ("Label %d at %L defined but cannot be used", l->value,
4579                        &l->where);
4580           break;
4581
4582         default:
4583           break;
4584         }
4585     }
4586 }
4587
4588
4589 /* Resolve derived type EQUIVALENCE object.  */
4590
4591 static try
4592 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
4593 {
4594   gfc_symbol *d;
4595   gfc_component *c = derived->components;
4596
4597   if (!derived)
4598     return SUCCESS;
4599
4600   /* Shall not be an object of nonsequence derived type.  */
4601   if (!derived->attr.sequence)
4602     {
4603       gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
4604                  "attribute to be an EQUIVALENCE object", sym->name, &e->where);
4605       return FAILURE;
4606     }
4607
4608   for (; c ; c = c->next)
4609     {
4610       d = c->ts.derived;
4611       if (d && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
4612         return FAILURE;
4613         
4614       /* Shall not be an object of sequence derived type containing a pointer
4615          in the structure.  */
4616       if (c->pointer)
4617         {
4618           gfc_error ("Derived type variable '%s' at %L has pointer componet(s) "
4619                      "cannot be an EQUIVALENCE object", sym->name, &e->where);
4620           return FAILURE;
4621         }
4622     }
4623   return SUCCESS;
4624 }
4625
4626
4627 /* Resolve equivalence object. 
4628    An EQUIVALENCE object shall not be a dummy argument, a pointer, an
4629    allocatable array, an object of nonsequence derived type, an object of
4630    sequence derived type containing a pointer at any level of component
4631    selection, an automatic object, a function name, an entry name, a result
4632    name, a named constant, a structure component, or a subobject of any of
4633    the preceding objects.  */
4634
4635 static void
4636 resolve_equivalence (gfc_equiv *eq)
4637 {
4638   gfc_symbol *sym;
4639   gfc_symbol *derived;
4640   gfc_expr *e;
4641   gfc_ref *r;
4642
4643   for (; eq; eq = eq->eq)
4644     {
4645       e = eq->expr;
4646       if (gfc_resolve_expr (e) == FAILURE)
4647         continue;
4648
4649       sym = e->symtree->n.sym;
4650      
4651       /* Shall not be a dummy argument.  */
4652       if (sym->attr.dummy)
4653         {
4654           gfc_error ("Dummy argument '%s' at %L cannot be an EQUIVALENCE "
4655                      "object", sym->name, &e->where);
4656           continue;
4657         }
4658
4659       /* Shall not be an allocatable array.  */
4660       if (sym->attr.allocatable)
4661         {
4662           gfc_error ("Allocatable array '%s' at %L cannot be an EQUIVALENCE "
4663                      "object", sym->name, &e->where);
4664           continue;
4665         }
4666
4667       /* Shall not be a pointer.  */
4668       if (sym->attr.pointer)
4669         {
4670           gfc_error ("Pointer '%s' at %L cannot be an EQUIVALENCE object",
4671                      sym->name, &e->where);
4672           continue;
4673         }
4674       
4675       /* Shall not be a function name, ...  */
4676       if (sym->attr.function || sym->attr.result || sym->attr.entry
4677           || sym->attr.subroutine)
4678         {
4679           gfc_error ("Entity '%s' at %L cannot be an EQUIVALENCE object",
4680                      sym->name, &e->where);
4681           continue;
4682         }
4683
4684       /* Shall not be a named constant.  */      
4685       if (e->expr_type == EXPR_CONSTANT)
4686         {
4687           gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
4688                      "object", sym->name, &e->where);
4689           continue;
4690         }
4691
4692       derived = e->ts.derived;
4693       if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
4694         continue;
4695
4696       if (!e->ref)
4697         continue;
4698
4699       /* Shall not be an automatic array.  */
4700       if (e->ref->type == REF_ARRAY
4701           && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
4702         {
4703           gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
4704                      "an EQUIVALENCE object", sym->name, &e->where);
4705           continue;
4706         }
4707
4708       /* Shall not be a structure component.  */
4709       r = e->ref;
4710       while (r)
4711         {
4712           if (r->type == REF_COMPONENT)
4713             {
4714               gfc_error ("Structure component '%s' at %L cannot be an "
4715                          "EQUIVALENCE object",
4716                          r->u.c.component->name, &e->where);
4717               break;
4718             }
4719           r = r->next;
4720         }
4721     }    
4722 }      
4723       
4724       
4725 /* This function is called after a complete program unit has been compiled.
4726    Its purpose is to examine all of the expressions associated with a program
4727    unit, assign types to all intermediate expressions, make sure that all
4728    assignments are to compatible types and figure out which names refer to
4729    which functions or subroutines.  */
4730
4731 void
4732 gfc_resolve (gfc_namespace * ns)
4733 {
4734   gfc_namespace *old_ns, *n;
4735   gfc_charlen *cl;
4736   gfc_data *d;
4737   gfc_equiv *eq;
4738
4739   old_ns = gfc_current_ns;
4740   gfc_current_ns = ns;
4741
4742   resolve_entries (ns);
4743
4744   resolve_contained_functions (ns);
4745
4746   gfc_traverse_ns (ns, resolve_symbol);
4747
4748   for (n = ns->contained; n; n = n->sibling)
4749     {
4750       if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
4751         gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
4752                    "also be PURE", n->proc_name->name,
4753                    &n->proc_name->declared_at);
4754
4755       gfc_resolve (n);
4756     }
4757
4758   forall_flag = 0;
4759   gfc_check_interfaces (ns);
4760
4761   for (cl = ns->cl_list; cl; cl = cl->next)
4762     {
4763       if (cl->length == NULL || gfc_resolve_expr (cl->length) == FAILURE)
4764         continue;
4765
4766       if (gfc_simplify_expr (cl->length, 0) == FAILURE)
4767         continue;
4768
4769       if (gfc_specification_expr (cl->length) == FAILURE)
4770         continue;
4771     }
4772
4773   gfc_traverse_ns (ns, resolve_values);
4774
4775   if (ns->save_all)
4776     gfc_save_all (ns);
4777
4778   iter_stack = NULL;
4779   for (d = ns->data; d; d = d->next)
4780     resolve_data (d);
4781
4782   iter_stack = NULL;
4783   gfc_traverse_ns (ns, gfc_formalize_init_value);
4784
4785   for (eq = ns->equiv; eq; eq = eq->next)
4786     resolve_equivalence (eq);
4787
4788   cs_base = NULL;
4789   resolve_code (ns->code, ns);
4790
4791   /* Warn about unused labels.  */
4792   if (gfc_option.warn_unused_labels)
4793     warn_unused_label (ns);
4794
4795   gfc_current_ns = old_ns;
4796 }