OSDN Git Service

* options.c, primary.c, resolve.c, trans-common.c: Fix typos
[pf3gnuchains/gcc-fork.git] / gcc / fortran / resolve.c
1 /* Perform type resolution on the various stuctures.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, 
3    Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA
21 02110-1301, USA.  */
22
23
24 #include "config.h"
25 #include "system.h"
26 #include "gfortran.h"
27 #include "arith.h"  /* For gfc_compare_expr().  */
28 #include "dependency.h"
29
30 /* Types used in equivalence statements.  */
31
32 typedef enum seq_type
33 {
34   SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
35 }
36 seq_type;
37
38 /* Stack to push the current if we descend into a block during
39    resolution.  See resolve_branch() and resolve_code().  */
40
41 typedef struct code_stack
42 {
43   struct gfc_code *head, *current;
44   struct code_stack *prev;
45 }
46 code_stack;
47
48 static code_stack *cs_base = NULL;
49
50
51 /* Nonzero if we're inside a FORALL block.  */
52
53 static int forall_flag;
54
55 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block.  */
56
57 static int omp_workshare_flag;
58
59 /* Nonzero if we are processing a formal arglist. The corresponding function
60    resets the flag each time that it is read.  */
61 static int formal_arg_flag = 0;
62
63 int
64 gfc_is_formal_arg (void)
65 {
66   return formal_arg_flag;
67 }
68
69 /* Resolve types of formal argument lists.  These have to be done early so that
70    the formal argument lists of module procedures can be copied to the
71    containing module before the individual procedures are resolved
72    individually.  We also resolve argument lists of procedures in interface
73    blocks because they are self-contained scoping units.
74
75    Since a dummy argument cannot be a non-dummy procedure, the only
76    resort left for untyped names are the IMPLICIT types.  */
77
78 static void
79 resolve_formal_arglist (gfc_symbol * proc)
80 {
81   gfc_formal_arglist *f;
82   gfc_symbol *sym;
83   int i;
84
85   /* TODO: Procedures whose return character length parameter is not constant
86      or assumed must also have explicit interfaces.  */
87   if (proc->result != NULL)
88     sym = proc->result;
89   else
90     sym = proc;
91
92   if (gfc_elemental (proc)
93       || sym->attr.pointer || sym->attr.allocatable
94       || (sym->as && sym->as->rank > 0))
95     proc->attr.always_explicit = 1;
96
97   formal_arg_flag = 1;
98
99   for (f = proc->formal; f; f = f->next)
100     {
101       sym = f->sym;
102
103       if (sym == NULL)
104         {
105           /* Alternate return placeholder.  */
106           if (gfc_elemental (proc))
107             gfc_error ("Alternate return specifier in elemental subroutine "
108                        "'%s' at %L is not allowed", proc->name,
109                        &proc->declared_at);
110           if (proc->attr.function)
111             gfc_error ("Alternate return specifier in function "
112                        "'%s' at %L is not allowed", proc->name,
113                        &proc->declared_at);
114           continue;
115         }
116
117       if (sym->attr.if_source != IFSRC_UNKNOWN)
118         resolve_formal_arglist (sym);
119
120       if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
121         {
122           if (gfc_pure (proc) && !gfc_pure (sym))
123             {
124               gfc_error
125                 ("Dummy procedure '%s' of PURE procedure at %L must also "
126                  "be PURE", sym->name, &sym->declared_at);
127               continue;
128             }
129
130           if (gfc_elemental (proc))
131             {
132               gfc_error
133                 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
134                  &sym->declared_at);
135               continue;
136             }
137
138           continue;
139         }
140
141       if (sym->ts.type == BT_UNKNOWN)
142         {
143           if (!sym->attr.function || sym->result == sym)
144             gfc_set_default_type (sym, 1, sym->ns);
145         }
146
147       gfc_resolve_array_spec (sym->as, 0);
148
149       /* We can't tell if an array with dimension (:) is assumed or deferred
150          shape until we know if it has the pointer or allocatable attributes.
151       */
152       if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
153           && !(sym->attr.pointer || sym->attr.allocatable))
154         {
155           sym->as->type = AS_ASSUMED_SHAPE;
156           for (i = 0; i < sym->as->rank; i++)
157             sym->as->lower[i] = gfc_int_expr (1);
158         }
159
160       if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
161           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
162           || sym->attr.optional)
163         proc->attr.always_explicit = 1;
164
165       /* If the flavor is unknown at this point, it has to be a variable.
166          A procedure specification would have already set the type.  */
167
168       if (sym->attr.flavor == FL_UNKNOWN)
169         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
170
171       if (gfc_pure (proc))
172         {
173           if (proc->attr.function && !sym->attr.pointer
174               && sym->attr.flavor != FL_PROCEDURE
175               && sym->attr.intent != INTENT_IN)
176
177             gfc_error ("Argument '%s' of pure function '%s' at %L must be "
178                        "INTENT(IN)", sym->name, proc->name,
179                        &sym->declared_at);
180
181           if (proc->attr.subroutine && !sym->attr.pointer
182               && sym->attr.intent == INTENT_UNKNOWN)
183
184             gfc_error
185               ("Argument '%s' of pure subroutine '%s' at %L must have "
186                "its INTENT specified", sym->name, proc->name,
187                &sym->declared_at);
188         }
189
190
191       if (gfc_elemental (proc))
192         {
193           if (sym->as != NULL)
194             {
195               gfc_error
196                 ("Argument '%s' of elemental procedure at %L must be scalar",
197                  sym->name, &sym->declared_at);
198               continue;
199             }
200
201           if (sym->attr.pointer)
202             {
203               gfc_error
204                 ("Argument '%s' of elemental procedure at %L cannot have "
205                  "the POINTER attribute", sym->name, &sym->declared_at);
206               continue;
207             }
208         }
209
210       /* Each dummy shall be specified to be scalar.  */
211       if (proc->attr.proc == PROC_ST_FUNCTION)
212         {
213           if (sym->as != NULL)
214             {
215               gfc_error
216                 ("Argument '%s' of statement function at %L must be scalar",
217                  sym->name, &sym->declared_at);
218               continue;
219             }
220
221           if (sym->ts.type == BT_CHARACTER)
222             {
223               gfc_charlen *cl = sym->ts.cl;
224               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
225                 {
226                   gfc_error
227                     ("Character-valued argument '%s' of statement function at "
228                      "%L must has constant length",
229                      sym->name, &sym->declared_at);
230                   continue;
231                 }
232             }
233         }
234     }
235   formal_arg_flag = 0;
236 }
237
238
239 /* Work function called when searching for symbols that have argument lists
240    associated with them.  */
241
242 static void
243 find_arglists (gfc_symbol * sym)
244 {
245
246   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
247     return;
248
249   resolve_formal_arglist (sym);
250 }
251
252
253 /* Given a namespace, resolve all formal argument lists within the namespace.
254  */
255
256 static void
257 resolve_formal_arglists (gfc_namespace * ns)
258 {
259
260   if (ns == NULL)
261     return;
262
263   gfc_traverse_ns (ns, find_arglists);
264 }
265
266
267 static void
268 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
269 {
270   try t;
271   
272   /* If this namespace is not a function, ignore it.  */
273   if (! sym
274       || !(sym->attr.function
275            || sym->attr.flavor == FL_VARIABLE))
276     return;
277
278   /* Try to find out of what the return type is.  */
279   if (sym->result != NULL)
280     sym = sym->result;
281
282   if (sym->ts.type == BT_UNKNOWN)
283     {
284       t = gfc_set_default_type (sym, 0, ns);
285
286       if (t == FAILURE && !sym->attr.untyped)
287         {
288           gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
289                      sym->name, &sym->declared_at); /* FIXME */
290           sym->attr.untyped = 1;
291         }
292     }
293
294   /*Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character type,
295     lists the only ways a character length value of * can be used: dummy arguments
296     of procedures, named constants, and function results in external functions.
297     Internal function results are not on that list; ergo, not permitted.  */
298
299   if (sym->ts.type == BT_CHARACTER)
300     {
301       gfc_charlen *cl = sym->ts.cl;
302       if (!cl || !cl->length)
303         gfc_error ("Character-valued internal function '%s' at %L must "
304                    "not be assumed length", sym->name, &sym->declared_at);
305     }
306 }
307
308
309 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
310    introduce duplicates.  */
311
312 static void
313 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
314 {
315   gfc_formal_arglist *f, *new_arglist;
316   gfc_symbol *new_sym;
317
318   for (; new_args != NULL; new_args = new_args->next)
319     {
320       new_sym = new_args->sym;
321       /* See if ths arg is already in the formal argument list.  */
322       for (f = proc->formal; f; f = f->next)
323         {
324           if (new_sym == f->sym)
325             break;
326         }
327
328       if (f)
329         continue;
330
331       /* Add a new argument.  Argument order is not important.  */
332       new_arglist = gfc_get_formal_arglist ();
333       new_arglist->sym = new_sym;
334       new_arglist->next = proc->formal;
335       proc->formal  = new_arglist;
336     }
337 }
338
339
340 /* Resolve alternate entry points.  If a symbol has multiple entry points we
341    create a new master symbol for the main routine, and turn the existing
342    symbol into an entry point.  */
343
344 static void
345 resolve_entries (gfc_namespace * ns)
346 {
347   gfc_namespace *old_ns;
348   gfc_code *c;
349   gfc_symbol *proc;
350   gfc_entry_list *el;
351   char name[GFC_MAX_SYMBOL_LEN + 1];
352   static int master_count = 0;
353
354   if (ns->proc_name == NULL)
355     return;
356
357   /* No need to do anything if this procedure doesn't have alternate entry
358      points.  */
359   if (!ns->entries)
360     return;
361
362   /* We may already have resolved alternate entry points.  */
363   if (ns->proc_name->attr.entry_master)
364     return;
365
366   /* If this isn't a procedure something has gone horribly wrong.  */
367   gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
368   
369   /* Remember the current namespace.  */
370   old_ns = gfc_current_ns;
371
372   gfc_current_ns = ns;
373
374   /* Add the main entry point to the list of entry points.  */
375   el = gfc_get_entry_list ();
376   el->sym = ns->proc_name;
377   el->id = 0;
378   el->next = ns->entries;
379   ns->entries = el;
380   ns->proc_name->attr.entry = 1;
381
382   /* Add an entry statement for it.  */
383   c = gfc_get_code ();
384   c->op = EXEC_ENTRY;
385   c->ext.entry = el;
386   c->next = ns->code;
387   ns->code = c;
388
389   /* Create a new symbol for the master function.  */
390   /* Give the internal function a unique name (within this file).
391      Also include the function name so the user has some hope of figuring
392      out what is going on.  */
393   snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
394             master_count++, ns->proc_name->name);
395   gfc_get_ha_symbol (name, &proc);
396   gcc_assert (proc != NULL);
397
398   gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
399   if (ns->proc_name->attr.subroutine)
400     gfc_add_subroutine (&proc->attr, proc->name, NULL);
401   else
402     {
403       gfc_symbol *sym;
404       gfc_typespec *ts, *fts;
405
406       gfc_add_function (&proc->attr, proc->name, NULL);
407       proc->result = proc;
408       fts = &ns->entries->sym->result->ts;
409       if (fts->type == BT_UNKNOWN)
410         fts = gfc_get_default_type (ns->entries->sym->result, NULL);
411       for (el = ns->entries->next; el; el = el->next)
412         {
413           ts = &el->sym->result->ts;
414           if (ts->type == BT_UNKNOWN)
415             ts = gfc_get_default_type (el->sym->result, NULL);
416           if (! gfc_compare_types (ts, fts)
417               || (el->sym->result->attr.dimension
418                   != ns->entries->sym->result->attr.dimension)
419               || (el->sym->result->attr.pointer
420                   != ns->entries->sym->result->attr.pointer))
421             break;
422         }
423
424       if (el == NULL)
425         {
426           sym = ns->entries->sym->result;
427           /* All result types the same.  */
428           proc->ts = *fts;
429           if (sym->attr.dimension)
430             gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
431           if (sym->attr.pointer)
432             gfc_add_pointer (&proc->attr, NULL);
433         }
434       else
435         {
436           /* Otherwise the result will be passed through a union by
437              reference.  */
438           proc->attr.mixed_entry_master = 1;
439           for (el = ns->entries; el; el = el->next)
440             {
441               sym = el->sym->result;
442               if (sym->attr.dimension)
443               {
444                 if (el == ns->entries)
445                   gfc_error
446                   ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
447                    sym->name, ns->entries->sym->name, &sym->declared_at);
448                 else
449                   gfc_error
450                     ("ENTRY result %s can't be an array in FUNCTION %s at %L",
451                      sym->name, ns->entries->sym->name, &sym->declared_at);
452               }
453               else if (sym->attr.pointer)
454               {
455                 if (el == ns->entries)
456                   gfc_error
457                   ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
458                    sym->name, ns->entries->sym->name, &sym->declared_at);
459                 else
460                   gfc_error
461                     ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
462                      sym->name, ns->entries->sym->name, &sym->declared_at);
463               }
464               else
465                 {
466                   ts = &sym->ts;
467                   if (ts->type == BT_UNKNOWN)
468                     ts = gfc_get_default_type (sym, NULL);
469                   switch (ts->type)
470                     {
471                     case BT_INTEGER:
472                       if (ts->kind == gfc_default_integer_kind)
473                         sym = NULL;
474                       break;
475                     case BT_REAL:
476                       if (ts->kind == gfc_default_real_kind
477                           || ts->kind == gfc_default_double_kind)
478                         sym = NULL;
479                       break;
480                     case BT_COMPLEX:
481                       if (ts->kind == gfc_default_complex_kind)
482                         sym = NULL;
483                       break;
484                     case BT_LOGICAL:
485                       if (ts->kind == gfc_default_logical_kind)
486                         sym = NULL;
487                       break;
488                     case BT_UNKNOWN:
489                       /* We will issue error elsewhere.  */
490                       sym = NULL;
491                       break;
492                     default:
493                       break;
494                     }
495                   if (sym)
496                   {
497                     if (el == ns->entries)
498                       gfc_error
499                         ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
500                          sym->name, gfc_typename (ts), ns->entries->sym->name,
501                          &sym->declared_at);
502                     else
503                       gfc_error
504                         ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
505                          sym->name, gfc_typename (ts), ns->entries->sym->name,
506                          &sym->declared_at);
507                   }
508                 }
509             }
510         }
511     }
512   proc->attr.access = ACCESS_PRIVATE;
513   proc->attr.entry_master = 1;
514
515   /* Merge all the entry point arguments.  */
516   for (el = ns->entries; el; el = el->next)
517     merge_argument_lists (proc, el->sym->formal);
518
519   /* Use the master function for the function body.  */
520   ns->proc_name = proc;
521
522   /* Finalize the new symbols.  */
523   gfc_commit_symbols ();
524
525   /* Restore the original namespace.  */
526   gfc_current_ns = old_ns;
527 }
528
529
530 /* Resolve contained function types.  Because contained functions can call one
531    another, they have to be worked out before any of the contained procedures
532    can be resolved.
533
534    The good news is that if a function doesn't already have a type, the only
535    way it can get one is through an IMPLICIT type or a RESULT variable, because
536    by definition contained functions are contained namespace they're contained
537    in, not in a sibling or parent namespace.  */
538
539 static void
540 resolve_contained_functions (gfc_namespace * ns)
541 {
542   gfc_namespace *child;
543   gfc_entry_list *el;
544
545   resolve_formal_arglists (ns);
546
547   for (child = ns->contained; child; child = child->sibling)
548     {
549       /* Resolve alternate entry points first.  */
550       resolve_entries (child); 
551
552       /* Then check function return types.  */
553       resolve_contained_fntype (child->proc_name, child);
554       for (el = child->entries; el; el = el->next)
555         resolve_contained_fntype (el->sym, child);
556     }
557 }
558
559
560 /* Resolve all of the elements of a structure constructor and make sure that
561    the types are correct.  */
562
563 static try
564 resolve_structure_cons (gfc_expr * expr)
565 {
566   gfc_constructor *cons;
567   gfc_component *comp;
568   try t;
569
570   t = SUCCESS;
571   cons = expr->value.constructor;
572   /* A constructor may have references if it is the result of substituting a
573      parameter variable.  In this case we just pull out the component we
574      want.  */
575   if (expr->ref)
576     comp = expr->ref->u.c.sym->components;
577   else
578     comp = expr->ts.derived->components;
579
580   for (; comp; comp = comp->next, cons = cons->next)
581     {
582       if (! cons->expr)
583         {
584           t = FAILURE;
585           continue;
586         }
587
588       if (gfc_resolve_expr (cons->expr) == FAILURE)
589         {
590           t = FAILURE;
591           continue;
592         }
593
594       /* If we don't have the right type, try to convert it.  */
595
596       if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
597         {
598           t = FAILURE;
599           if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
600             gfc_error ("The element in the derived type constructor at %L, "
601                        "for pointer component '%s', is %s but should be %s",
602                        &cons->expr->where, comp->name,
603                        gfc_basic_typename (cons->expr->ts.type),
604                        gfc_basic_typename (comp->ts.type));
605           else
606             t = gfc_convert_type (cons->expr, &comp->ts, 1);
607         }
608     }
609
610   return t;
611 }
612
613
614
615 /****************** Expression name resolution ******************/
616
617 /* Returns 0 if a symbol was not declared with a type or
618    attribute declaration statement, nonzero otherwise.  */
619
620 static int
621 was_declared (gfc_symbol * sym)
622 {
623   symbol_attribute a;
624
625   a = sym->attr;
626
627   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
628     return 1;
629
630   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
631       || a.optional || a.pointer || a.save || a.target
632       || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
633     return 1;
634
635   return 0;
636 }
637
638
639 /* Determine if a symbol is generic or not.  */
640
641 static int
642 generic_sym (gfc_symbol * sym)
643 {
644   gfc_symbol *s;
645
646   if (sym->attr.generic ||
647       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
648     return 1;
649
650   if (was_declared (sym) || sym->ns->parent == NULL)
651     return 0;
652
653   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
654
655   return (s == NULL) ? 0 : generic_sym (s);
656 }
657
658
659 /* Determine if a symbol is specific or not.  */
660
661 static int
662 specific_sym (gfc_symbol * sym)
663 {
664   gfc_symbol *s;
665
666   if (sym->attr.if_source == IFSRC_IFBODY
667       || sym->attr.proc == PROC_MODULE
668       || sym->attr.proc == PROC_INTERNAL
669       || sym->attr.proc == PROC_ST_FUNCTION
670       || (sym->attr.intrinsic &&
671           gfc_specific_intrinsic (sym->name))
672       || sym->attr.external)
673     return 1;
674
675   if (was_declared (sym) || sym->ns->parent == NULL)
676     return 0;
677
678   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
679
680   return (s == NULL) ? 0 : specific_sym (s);
681 }
682
683
684 /* Figure out if the procedure is specific, generic or unknown.  */
685
686 typedef enum
687 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
688 proc_type;
689
690 static proc_type
691 procedure_kind (gfc_symbol * sym)
692 {
693
694   if (generic_sym (sym))
695     return PTYPE_GENERIC;
696
697   if (specific_sym (sym))
698     return PTYPE_SPECIFIC;
699
700   return PTYPE_UNKNOWN;
701 }
702
703 /* Check references to assumed size arrays.  The flag need_full_assumed_size
704    is nonzero when matching actual arguments.  */
705
706 static int need_full_assumed_size = 0;
707
708 static bool
709 check_assumed_size_reference (gfc_symbol * sym, gfc_expr * e)
710 {
711   gfc_ref * ref;
712   int dim;
713   int last = 1;
714
715   if (need_full_assumed_size
716         || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
717       return false;
718
719   for (ref = e->ref; ref; ref = ref->next)
720     if (ref->type == REF_ARRAY)
721       for (dim = 0; dim < ref->u.ar.as->rank; dim++)
722         last = (ref->u.ar.end[dim] == NULL) && (ref->u.ar.type == DIMEN_ELEMENT);
723
724   if (last)
725     {
726       gfc_error ("The upper bound in the last dimension must "
727                  "appear in the reference to the assumed size "
728                  "array '%s' at %L.", sym->name, &e->where);
729       return true;
730     }
731   return false;
732 }
733
734
735 /* Look for bad assumed size array references in argument expressions
736   of elemental and array valued intrinsic procedures.  Since this is
737   called from procedure resolution functions, it only recurses at
738   operators.  */
739
740 static bool
741 resolve_assumed_size_actual (gfc_expr *e)
742 {
743   if (e == NULL)
744    return false;
745
746   switch (e->expr_type)
747     {
748     case EXPR_VARIABLE:
749       if (e->symtree
750             && check_assumed_size_reference (e->symtree->n.sym, e))
751         return true;
752       break;
753
754     case EXPR_OP:
755       if (resolve_assumed_size_actual (e->value.op.op1)
756             || resolve_assumed_size_actual (e->value.op.op2))
757         return true;
758       break;
759
760     default:
761       break;
762     }
763   return false;
764 }
765
766
767 /* Resolve an actual argument list.  Most of the time, this is just
768    resolving the expressions in the list.
769    The exception is that we sometimes have to decide whether arguments
770    that look like procedure arguments are really simple variable
771    references.  */
772
773 static try
774 resolve_actual_arglist (gfc_actual_arglist * arg)
775 {
776   gfc_symbol *sym;
777   gfc_symtree *parent_st;
778   gfc_expr *e;
779
780   for (; arg; arg = arg->next)
781     {
782
783       e = arg->expr;
784       if (e == NULL)
785         {
786           /* Check the label is a valid branching target.  */
787           if (arg->label)
788             {
789               if (arg->label->defined == ST_LABEL_UNKNOWN)
790                 {
791                   gfc_error ("Label %d referenced at %L is never defined",
792                              arg->label->value, &arg->label->where);
793                   return FAILURE;
794                 }
795             }
796           continue;
797         }
798
799       if (e->ts.type != BT_PROCEDURE)
800         {
801           if (gfc_resolve_expr (e) != SUCCESS)
802             return FAILURE;
803           continue;
804         }
805
806       /* See if the expression node should really be a variable
807          reference.  */
808
809       sym = e->symtree->n.sym;
810
811       if (sym->attr.flavor == FL_PROCEDURE
812           || sym->attr.intrinsic
813           || sym->attr.external)
814         {
815
816           if (sym->attr.proc == PROC_ST_FUNCTION)
817             {
818               gfc_error ("Statement function '%s' at %L is not allowed as an "
819                          "actual argument", sym->name, &e->where);
820             }
821
822           if (sym->attr.contained && !sym->attr.use_assoc
823               && sym->ns->proc_name->attr.flavor != FL_MODULE)
824             {
825               gfc_error ("Internal procedure '%s' is not allowed as an "
826                          "actual argument at %L", sym->name, &e->where);
827             }
828
829           if (sym->attr.elemental && !sym->attr.intrinsic)
830             {
831               gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
832                          "allowed as an actual argument at %L", sym->name,
833                          &e->where);
834             }
835
836           /* If the symbol is the function that names the current (or
837              parent) scope, then we really have a variable reference.  */
838
839           if (sym->attr.function && sym->result == sym
840               && (sym->ns->proc_name == sym
841                   || (sym->ns->parent != NULL
842                       && sym->ns->parent->proc_name == sym)))
843             goto got_variable;
844
845           continue;
846         }
847
848       /* See if the name is a module procedure in a parent unit.  */
849
850       if (was_declared (sym) || sym->ns->parent == NULL)
851         goto got_variable;
852
853       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
854         {
855           gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
856           return FAILURE;
857         }
858
859       if (parent_st == NULL)
860         goto got_variable;
861
862       sym = parent_st->n.sym;
863       e->symtree = parent_st;           /* Point to the right thing.  */
864
865       if (sym->attr.flavor == FL_PROCEDURE
866           || sym->attr.intrinsic
867           || sym->attr.external)
868         {
869           continue;
870         }
871
872     got_variable:
873       e->expr_type = EXPR_VARIABLE;
874       e->ts = sym->ts;
875       if (sym->as != NULL)
876         {
877           e->rank = sym->as->rank;
878           e->ref = gfc_get_ref ();
879           e->ref->type = REF_ARRAY;
880           e->ref->u.ar.type = AR_FULL;
881           e->ref->u.ar.as = sym->as;
882         }
883     }
884
885   return SUCCESS;
886 }
887
888
889 /* Go through each actual argument in ACTUAL and see if it can be
890    implemented as an inlined, non-copying intrinsic.  FNSYM is the
891    function being called, or NULL if not known.  */
892
893 static void
894 find_noncopying_intrinsics (gfc_symbol * fnsym, gfc_actual_arglist * actual)
895 {
896   gfc_actual_arglist *ap;
897   gfc_expr *expr;
898
899   for (ap = actual; ap; ap = ap->next)
900     if (ap->expr
901         && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
902         && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
903       ap->expr->inline_noncopying_intrinsic = 1;
904 }
905
906 /* This function does the checking of references to global procedures
907    as defined in sections 18.1 and 14.1, respectively, of the Fortran
908    77 and 95 standards.  It checks for a gsymbol for the name, making
909    one if it does not already exist.  If it already exists, then the
910    reference being resolved must correspond to the type of gsymbol.
911    Otherwise, the new symbol is equipped with the attributes of the 
912    reference.  The corresponding code that is called in creating
913    global entities is parse.c.  */
914
915 static void
916 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
917 {
918   gfc_gsymbol * gsym;
919   unsigned int type;
920
921   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
922
923   gsym = gfc_get_gsymbol (sym->name);
924
925   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
926     global_used (gsym, where);
927
928   if (gsym->type == GSYM_UNKNOWN)
929     {
930       gsym->type = type;
931       gsym->where = *where;
932     }
933
934   gsym->used = 1;
935 }
936
937 /************* Function resolution *************/
938
939 /* Resolve a function call known to be generic.
940    Section 14.1.2.4.1.  */
941
942 static match
943 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
944 {
945   gfc_symbol *s;
946
947   if (sym->attr.generic)
948     {
949       s =
950         gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
951       if (s != NULL)
952         {
953           expr->value.function.name = s->name;
954           expr->value.function.esym = s;
955
956           if (s->ts.type != BT_UNKNOWN)
957             expr->ts = s->ts;
958           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
959             expr->ts = s->result->ts;
960
961           if (s->as != NULL)
962             expr->rank = s->as->rank;
963           else if (s->result != NULL && s->result->as != NULL)
964             expr->rank = s->result->as->rank;
965
966           return MATCH_YES;
967         }
968
969       /* TODO: Need to search for elemental references in generic interface */
970     }
971
972   if (sym->attr.intrinsic)
973     return gfc_intrinsic_func_interface (expr, 0);
974
975   return MATCH_NO;
976 }
977
978
979 static try
980 resolve_generic_f (gfc_expr * expr)
981 {
982   gfc_symbol *sym;
983   match m;
984
985   sym = expr->symtree->n.sym;
986
987   for (;;)
988     {
989       m = resolve_generic_f0 (expr, sym);
990       if (m == MATCH_YES)
991         return SUCCESS;
992       else if (m == MATCH_ERROR)
993         return FAILURE;
994
995 generic:
996       if (sym->ns->parent == NULL)
997         break;
998       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
999
1000       if (sym == NULL)
1001         break;
1002       if (!generic_sym (sym))
1003         goto generic;
1004     }
1005
1006   /* Last ditch attempt.  */
1007
1008   if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
1009     {
1010       gfc_error ("Generic function '%s' at %L is not an intrinsic function",
1011                  expr->symtree->n.sym->name, &expr->where);
1012       return FAILURE;
1013     }
1014
1015   m = gfc_intrinsic_func_interface (expr, 0);
1016   if (m == MATCH_YES)
1017     return SUCCESS;
1018   if (m == MATCH_NO)
1019     gfc_error
1020       ("Generic function '%s' at %L is not consistent with a specific "
1021        "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
1022
1023   return FAILURE;
1024 }
1025
1026
1027 /* Resolve a function call known to be specific.  */
1028
1029 static match
1030 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
1031 {
1032   match m;
1033
1034   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1035     {
1036       if (sym->attr.dummy)
1037         {
1038           sym->attr.proc = PROC_DUMMY;
1039           goto found;
1040         }
1041
1042       sym->attr.proc = PROC_EXTERNAL;
1043       goto found;
1044     }
1045
1046   if (sym->attr.proc == PROC_MODULE
1047       || sym->attr.proc == PROC_ST_FUNCTION
1048       || sym->attr.proc == PROC_INTERNAL)
1049     goto found;
1050
1051   if (sym->attr.intrinsic)
1052     {
1053       m = gfc_intrinsic_func_interface (expr, 1);
1054       if (m == MATCH_YES)
1055         return MATCH_YES;
1056       if (m == MATCH_NO)
1057         gfc_error
1058           ("Function '%s' at %L is INTRINSIC but is not compatible with "
1059            "an intrinsic", sym->name, &expr->where);
1060
1061       return MATCH_ERROR;
1062     }
1063
1064   return MATCH_NO;
1065
1066 found:
1067   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1068
1069   expr->ts = sym->ts;
1070   expr->value.function.name = sym->name;
1071   expr->value.function.esym = sym;
1072   if (sym->as != NULL)
1073     expr->rank = sym->as->rank;
1074
1075   return MATCH_YES;
1076 }
1077
1078
1079 static try
1080 resolve_specific_f (gfc_expr * expr)
1081 {
1082   gfc_symbol *sym;
1083   match m;
1084
1085   sym = expr->symtree->n.sym;
1086
1087   for (;;)
1088     {
1089       m = resolve_specific_f0 (sym, expr);
1090       if (m == MATCH_YES)
1091         return SUCCESS;
1092       if (m == MATCH_ERROR)
1093         return FAILURE;
1094
1095       if (sym->ns->parent == NULL)
1096         break;
1097
1098       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1099
1100       if (sym == NULL)
1101         break;
1102     }
1103
1104   gfc_error ("Unable to resolve the specific function '%s' at %L",
1105              expr->symtree->n.sym->name, &expr->where);
1106
1107   return SUCCESS;
1108 }
1109
1110
1111 /* Resolve a procedure call not known to be generic nor specific.  */
1112
1113 static try
1114 resolve_unknown_f (gfc_expr * expr)
1115 {
1116   gfc_symbol *sym;
1117   gfc_typespec *ts;
1118
1119   sym = expr->symtree->n.sym;
1120
1121   if (sym->attr.dummy)
1122     {
1123       sym->attr.proc = PROC_DUMMY;
1124       expr->value.function.name = sym->name;
1125       goto set_type;
1126     }
1127
1128   /* See if we have an intrinsic function reference.  */
1129
1130   if (gfc_intrinsic_name (sym->name, 0))
1131     {
1132       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1133         return SUCCESS;
1134       return FAILURE;
1135     }
1136
1137   /* The reference is to an external name.  */
1138
1139   sym->attr.proc = PROC_EXTERNAL;
1140   expr->value.function.name = sym->name;
1141   expr->value.function.esym = expr->symtree->n.sym;
1142
1143   if (sym->as != NULL)
1144     expr->rank = sym->as->rank;
1145
1146   /* Type of the expression is either the type of the symbol or the
1147      default type of the symbol.  */
1148
1149 set_type:
1150   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1151
1152   if (sym->ts.type != BT_UNKNOWN)
1153     expr->ts = sym->ts;
1154   else
1155     {
1156       ts = gfc_get_default_type (sym, sym->ns);
1157
1158       if (ts->type == BT_UNKNOWN)
1159         {
1160           gfc_error ("Function '%s' at %L has no IMPLICIT type",
1161                      sym->name, &expr->where);
1162           return FAILURE;
1163         }
1164       else
1165         expr->ts = *ts;
1166     }
1167
1168   return SUCCESS;
1169 }
1170
1171
1172 /* Figure out if a function reference is pure or not.  Also set the name
1173    of the function for a potential error message.  Return nonzero if the
1174    function is PURE, zero if not.  */
1175
1176 static int
1177 pure_function (gfc_expr * e, const char **name)
1178 {
1179   int pure;
1180
1181   if (e->value.function.esym)
1182     {
1183       pure = gfc_pure (e->value.function.esym);
1184       *name = e->value.function.esym->name;
1185     }
1186   else if (e->value.function.isym)
1187     {
1188       pure = e->value.function.isym->pure
1189         || e->value.function.isym->elemental;
1190       *name = e->value.function.isym->name;
1191     }
1192   else
1193     {
1194       /* Implicit functions are not pure.  */
1195       pure = 0;
1196       *name = e->value.function.name;
1197     }
1198
1199   return pure;
1200 }
1201
1202
1203 /* Resolve a function call, which means resolving the arguments, then figuring
1204    out which entity the name refers to.  */
1205 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1206    to INTENT(OUT) or INTENT(INOUT).  */
1207
1208 static try
1209 resolve_function (gfc_expr * expr)
1210 {
1211   gfc_actual_arglist *arg;
1212   gfc_symbol * sym;
1213   const char *name;
1214   try t;
1215   int temp;
1216   int i;
1217
1218   sym = NULL;
1219   if (expr->symtree)
1220     sym = expr->symtree->n.sym;
1221
1222   /* If the procedure is not internal, a statement function or a module
1223      procedure,it must be external and should be checked for usage.  */
1224   if (sym && !sym->attr.dummy && !sym->attr.contained
1225         && sym->attr.proc != PROC_ST_FUNCTION
1226         && !sym->attr.use_assoc)
1227     resolve_global_procedure (sym, &expr->where, 0);
1228
1229   /* Switch off assumed size checking and do this again for certain kinds
1230      of procedure, once the procedure itself is resolved.  */
1231   need_full_assumed_size++;
1232
1233   if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
1234     return FAILURE;
1235
1236   /* Resume assumed_size checking. */
1237   need_full_assumed_size--;
1238
1239   if (sym && sym->ts.type == BT_CHARACTER
1240         && sym->ts.cl
1241         && sym->ts.cl->length == NULL
1242         && !sym->attr.dummy
1243         && !sym->attr.contained)
1244     {
1245       /* Internal procedures are taken care of in resolve_contained_fntype.  */
1246       gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
1247                  "be used at %L since it is not a dummy argument",
1248                  sym->name, &expr->where);
1249       return FAILURE;
1250     }
1251
1252 /* See if function is already resolved.  */
1253
1254   if (expr->value.function.name != NULL)
1255     {
1256       if (expr->ts.type == BT_UNKNOWN)
1257         expr->ts = sym->ts;
1258       t = SUCCESS;
1259     }
1260   else
1261     {
1262       /* Apply the rules of section 14.1.2.  */
1263
1264       switch (procedure_kind (sym))
1265         {
1266         case PTYPE_GENERIC:
1267           t = resolve_generic_f (expr);
1268           break;
1269
1270         case PTYPE_SPECIFIC:
1271           t = resolve_specific_f (expr);
1272           break;
1273
1274         case PTYPE_UNKNOWN:
1275           t = resolve_unknown_f (expr);
1276           break;
1277
1278         default:
1279           gfc_internal_error ("resolve_function(): bad function type");
1280         }
1281     }
1282
1283   /* If the expression is still a function (it might have simplified),
1284      then we check to see if we are calling an elemental function.  */
1285
1286   if (expr->expr_type != EXPR_FUNCTION)
1287     return t;
1288
1289   temp = need_full_assumed_size;
1290   need_full_assumed_size = 0;
1291
1292   if (expr->value.function.actual != NULL
1293       && ((expr->value.function.esym != NULL
1294            && expr->value.function.esym->attr.elemental)
1295           || (expr->value.function.isym != NULL
1296               && expr->value.function.isym->elemental)))
1297     {
1298       /* The rank of an elemental is the rank of its array argument(s).  */
1299       for (arg = expr->value.function.actual; arg; arg = arg->next)
1300         {
1301           if (arg->expr != NULL && arg->expr->rank > 0)
1302             {
1303               expr->rank = arg->expr->rank;
1304               if (!expr->shape && arg->expr->shape)
1305                 {
1306                   expr->shape = gfc_get_shape (expr->rank);
1307                   for (i = 0; i < expr->rank; i++)
1308                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1309                 }
1310               break;
1311             }
1312         }
1313
1314       /* Being elemental, the last upper bound of an assumed size array
1315          argument must be present.  */
1316       for (arg = expr->value.function.actual; arg; arg = arg->next)
1317         {
1318           if (arg->expr != NULL
1319                 && arg->expr->rank > 0
1320                 && resolve_assumed_size_actual (arg->expr))
1321             return FAILURE;
1322         }
1323     }
1324   if (omp_workshare_flag
1325       && expr->value.function.esym
1326       && ! gfc_elemental (expr->value.function.esym))
1327     {
1328       gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed"
1329                  " in WORKSHARE construct", expr->value.function.esym->name,
1330                  &expr->where);
1331       t = FAILURE;
1332     }
1333
1334   else if (expr->value.function.actual != NULL
1335              && expr->value.function.isym != NULL
1336              && expr->value.function.isym->generic_id != GFC_ISYM_LBOUND
1337              && expr->value.function.isym->generic_id != GFC_ISYM_LOC
1338              && expr->value.function.isym->generic_id != GFC_ISYM_PRESENT)
1339     {
1340       /* Array instrinsics must also have the last upper bound of an
1341          assumed size array argument.  UBOUND and SIZE have to be
1342          excluded from the check if the second argument is anything
1343          than a constant.  */
1344       int inquiry;
1345       inquiry = expr->value.function.isym->generic_id == GFC_ISYM_UBOUND
1346                   || expr->value.function.isym->generic_id == GFC_ISYM_SIZE;
1347             
1348       for (arg = expr->value.function.actual; arg; arg = arg->next)
1349         {
1350           if (inquiry && arg->next != NULL && arg->next->expr
1351                 && arg->next->expr->expr_type != EXPR_CONSTANT)
1352             break;
1353           
1354           if (arg->expr != NULL
1355                 && arg->expr->rank > 0
1356                 && resolve_assumed_size_actual (arg->expr))
1357             return FAILURE;
1358         }
1359     }
1360
1361   need_full_assumed_size = temp;
1362
1363   if (!pure_function (expr, &name) && name)
1364     {
1365       if (forall_flag)
1366         {
1367           gfc_error
1368             ("Function reference to '%s' at %L is inside a FORALL block",
1369              name, &expr->where);
1370           t = FAILURE;
1371         }
1372       else if (gfc_pure (NULL))
1373         {
1374           gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1375                      "procedure within a PURE procedure", name, &expr->where);
1376           t = FAILURE;
1377         }
1378     }
1379
1380   /* Functions without the RECURSIVE attribution are not allowed to
1381    * call themselves.  */
1382   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
1383     {
1384       gfc_symbol *esym, *proc;
1385       esym = expr->value.function.esym;
1386       proc = gfc_current_ns->proc_name;
1387       if (esym == proc)
1388       {
1389         gfc_error ("Function '%s' at %L cannot call itself, as it is not "
1390                    "RECURSIVE", name, &expr->where);
1391         t = FAILURE;
1392       }
1393
1394       if (esym->attr.entry && esym->ns->entries && proc->ns->entries
1395           && esym->ns->entries->sym == proc->ns->entries->sym)
1396       {
1397         gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
1398                    "'%s' is not declared as RECURSIVE",
1399                    esym->name, &expr->where, esym->ns->entries->sym->name);
1400         t = FAILURE;
1401       }
1402     }
1403
1404   /* Character lengths of use associated functions may contains references to
1405      symbols not referenced from the current program unit otherwise.  Make sure
1406      those symbols are marked as referenced.  */
1407
1408   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym 
1409       && expr->value.function.esym->attr.use_assoc)
1410     {
1411       gfc_expr_set_symbols_referenced (expr->ts.cl->length);
1412     }
1413
1414   if (t == SUCCESS)
1415     find_noncopying_intrinsics (expr->value.function.esym,
1416                                 expr->value.function.actual);
1417   return t;
1418 }
1419
1420
1421 /************* Subroutine resolution *************/
1422
1423 static void
1424 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1425 {
1426
1427   if (gfc_pure (sym))
1428     return;
1429
1430   if (forall_flag)
1431     gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1432                sym->name, &c->loc);
1433   else if (gfc_pure (NULL))
1434     gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1435                &c->loc);
1436 }
1437
1438
1439 static match
1440 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1441 {
1442   gfc_symbol *s;
1443
1444   if (sym->attr.generic)
1445     {
1446       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1447       if (s != NULL)
1448         {
1449           c->resolved_sym = s;
1450           pure_subroutine (c, s);
1451           return MATCH_YES;
1452         }
1453
1454       /* TODO: Need to search for elemental references in generic interface.  */
1455     }
1456
1457   if (sym->attr.intrinsic)
1458     return gfc_intrinsic_sub_interface (c, 0);
1459
1460   return MATCH_NO;
1461 }
1462
1463
1464 static try
1465 resolve_generic_s (gfc_code * c)
1466 {
1467   gfc_symbol *sym;
1468   match m;
1469
1470   sym = c->symtree->n.sym;
1471
1472   m = resolve_generic_s0 (c, sym);
1473   if (m == MATCH_YES)
1474     return SUCCESS;
1475   if (m == MATCH_ERROR)
1476     return FAILURE;
1477
1478   if (sym->ns->parent != NULL)
1479     {
1480       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1481       if (sym != NULL)
1482         {
1483           m = resolve_generic_s0 (c, sym);
1484           if (m == MATCH_YES)
1485             return SUCCESS;
1486           if (m == MATCH_ERROR)
1487             return FAILURE;
1488         }
1489     }
1490
1491   /* Last ditch attempt.  */
1492
1493   if (!gfc_generic_intrinsic (sym->name))
1494     {
1495       gfc_error
1496         ("Generic subroutine '%s' at %L is not an intrinsic subroutine",
1497          sym->name, &c->loc);
1498       return FAILURE;
1499     }
1500
1501   m = gfc_intrinsic_sub_interface (c, 0);
1502   if (m == MATCH_YES)
1503     return SUCCESS;
1504   if (m == MATCH_NO)
1505     gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1506                "intrinsic subroutine interface", sym->name, &c->loc);
1507
1508   return FAILURE;
1509 }
1510
1511
1512 /* Resolve a subroutine call known to be specific.  */
1513
1514 static match
1515 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1516 {
1517   match m;
1518
1519   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1520     {
1521       if (sym->attr.dummy)
1522         {
1523           sym->attr.proc = PROC_DUMMY;
1524           goto found;
1525         }
1526
1527       sym->attr.proc = PROC_EXTERNAL;
1528       goto found;
1529     }
1530
1531   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1532     goto found;
1533
1534   if (sym->attr.intrinsic)
1535     {
1536       m = gfc_intrinsic_sub_interface (c, 1);
1537       if (m == MATCH_YES)
1538         return MATCH_YES;
1539       if (m == MATCH_NO)
1540         gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1541                    "with an intrinsic", sym->name, &c->loc);
1542
1543       return MATCH_ERROR;
1544     }
1545
1546   return MATCH_NO;
1547
1548 found:
1549   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1550
1551   c->resolved_sym = sym;
1552   pure_subroutine (c, sym);
1553
1554   return MATCH_YES;
1555 }
1556
1557
1558 static try
1559 resolve_specific_s (gfc_code * c)
1560 {
1561   gfc_symbol *sym;
1562   match m;
1563
1564   sym = c->symtree->n.sym;
1565
1566   m = resolve_specific_s0 (c, sym);
1567   if (m == MATCH_YES)
1568     return SUCCESS;
1569   if (m == MATCH_ERROR)
1570     return FAILURE;
1571
1572   gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1573
1574   if (sym != NULL)
1575     {
1576       m = resolve_specific_s0 (c, sym);
1577       if (m == MATCH_YES)
1578         return SUCCESS;
1579       if (m == MATCH_ERROR)
1580         return FAILURE;
1581     }
1582
1583   gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1584              sym->name, &c->loc);
1585
1586   return FAILURE;
1587 }
1588
1589
1590 /* Resolve a subroutine call not known to be generic nor specific.  */
1591
1592 static try
1593 resolve_unknown_s (gfc_code * c)
1594 {
1595   gfc_symbol *sym;
1596
1597   sym = c->symtree->n.sym;
1598
1599   if (sym->attr.dummy)
1600     {
1601       sym->attr.proc = PROC_DUMMY;
1602       goto found;
1603     }
1604
1605   /* See if we have an intrinsic function reference.  */
1606
1607   if (gfc_intrinsic_name (sym->name, 1))
1608     {
1609       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1610         return SUCCESS;
1611       return FAILURE;
1612     }
1613
1614   /* The reference is to an external name.  */
1615
1616 found:
1617   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1618
1619   c->resolved_sym = sym;
1620
1621   pure_subroutine (c, sym);
1622
1623   return SUCCESS;
1624 }
1625
1626
1627 /* Resolve a subroutine call.  Although it was tempting to use the same code
1628    for functions, subroutines and functions are stored differently and this
1629    makes things awkward.  */
1630
1631 static try
1632 resolve_call (gfc_code * c)
1633 {
1634   try t;
1635
1636   if (c->symtree && c->symtree->n.sym
1637         && c->symtree->n.sym->ts.type != BT_UNKNOWN)
1638     {
1639       gfc_error ("'%s' at %L has a type, which is not consistent with "
1640                  "the CALL at %L", c->symtree->n.sym->name,
1641                  &c->symtree->n.sym->declared_at, &c->loc);
1642       return FAILURE;
1643     }
1644
1645   /* If the procedure is not internal or module, it must be external and
1646      should be checked for usage.  */
1647   if (c->symtree && c->symtree->n.sym
1648         && !c->symtree->n.sym->attr.dummy
1649         && !c->symtree->n.sym->attr.contained
1650         && !c->symtree->n.sym->attr.use_assoc)
1651     resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
1652
1653   /* Subroutines without the RECURSIVE attribution are not allowed to
1654    * call themselves.  */
1655   if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
1656     {
1657       gfc_symbol *csym, *proc;
1658       csym = c->symtree->n.sym;
1659       proc = gfc_current_ns->proc_name;
1660       if (csym == proc)
1661       {
1662         gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
1663                    "RECURSIVE", csym->name, &c->loc);
1664         t = FAILURE;
1665       }
1666
1667       if (csym->attr.entry && csym->ns->entries && proc->ns->entries
1668           && csym->ns->entries->sym == proc->ns->entries->sym)
1669       {
1670         gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
1671                    "'%s' is not declared as RECURSIVE",
1672                    csym->name, &c->loc, csym->ns->entries->sym->name);
1673         t = FAILURE;
1674       }
1675     }
1676
1677   /* Switch off assumed size checking and do this again for certain kinds
1678      of procedure, once the procedure itself is resolved.  */
1679   need_full_assumed_size++;
1680
1681   if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1682     return FAILURE;
1683
1684   /* Resume assumed_size checking. */
1685   need_full_assumed_size--;
1686
1687
1688   t = SUCCESS;
1689   if (c->resolved_sym == NULL)
1690     switch (procedure_kind (c->symtree->n.sym))
1691       {
1692       case PTYPE_GENERIC:
1693         t = resolve_generic_s (c);
1694         break;
1695
1696       case PTYPE_SPECIFIC:
1697         t = resolve_specific_s (c);
1698         break;
1699
1700       case PTYPE_UNKNOWN:
1701         t = resolve_unknown_s (c);
1702         break;
1703
1704       default:
1705         gfc_internal_error ("resolve_subroutine(): bad function type");
1706       }
1707
1708   /* Some checks of elemental subroutines.  */
1709   if (c->ext.actual != NULL
1710       && c->symtree->n.sym->attr.elemental)
1711     {
1712       gfc_actual_arglist * a;
1713       gfc_expr * e;
1714       e = NULL;
1715
1716       for (a = c->ext.actual; a; a = a->next)
1717         {
1718           if (a->expr == NULL || a->expr->rank == 0)
1719             continue;
1720
1721          /* The last upper bound of an assumed size array argument must
1722             be present.  */
1723           if (resolve_assumed_size_actual (a->expr))
1724             return FAILURE;
1725
1726           /* Array actual arguments must conform.  */
1727           if (e != NULL)
1728             {
1729               if (gfc_check_conformance ("elemental subroutine", a->expr, e)
1730                         == FAILURE)
1731                 return FAILURE;
1732             }
1733           else
1734             e = a->expr;
1735         }
1736     }
1737
1738   if (t == SUCCESS)
1739     find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
1740   return t;
1741 }
1742
1743 /* Compare the shapes of two arrays that have non-NULL shapes.  If both
1744    op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1745    match.  If both op1->shape and op2->shape are non-NULL return FAILURE
1746    if their shapes do not match.  If either op1->shape or op2->shape is
1747    NULL, return SUCCESS.  */
1748
1749 static try
1750 compare_shapes (gfc_expr * op1, gfc_expr * op2)
1751 {
1752   try t;
1753   int i;
1754
1755   t = SUCCESS;
1756                   
1757   if (op1->shape != NULL && op2->shape != NULL)
1758     {
1759       for (i = 0; i < op1->rank; i++)
1760         {
1761           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1762            {
1763              gfc_error ("Shapes for operands at %L and %L are not conformable",
1764                          &op1->where, &op2->where);
1765              t = FAILURE;
1766              break;
1767            }
1768         }
1769     }
1770
1771   return t;
1772 }
1773
1774 /* Resolve an operator expression node.  This can involve replacing the
1775    operation with a user defined function call.  */
1776
1777 static try
1778 resolve_operator (gfc_expr * e)
1779 {
1780   gfc_expr *op1, *op2;
1781   char msg[200];
1782   try t;
1783
1784   /* Resolve all subnodes-- give them types.  */
1785
1786   switch (e->value.op.operator)
1787     {
1788     default:
1789       if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1790         return FAILURE;
1791
1792     /* Fall through...  */
1793
1794     case INTRINSIC_NOT:
1795     case INTRINSIC_UPLUS:
1796     case INTRINSIC_UMINUS:
1797     case INTRINSIC_PARENTHESES:
1798       if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1799         return FAILURE;
1800       break;
1801     }
1802
1803   /* Typecheck the new node.  */
1804
1805   op1 = e->value.op.op1;
1806   op2 = e->value.op.op2;
1807
1808   switch (e->value.op.operator)
1809     {
1810     case INTRINSIC_UPLUS:
1811     case INTRINSIC_UMINUS:
1812       if (op1->ts.type == BT_INTEGER
1813           || op1->ts.type == BT_REAL
1814           || op1->ts.type == BT_COMPLEX)
1815         {
1816           e->ts = op1->ts;
1817           break;
1818         }
1819
1820       sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
1821                gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1822       goto bad_op;
1823
1824     case INTRINSIC_PLUS:
1825     case INTRINSIC_MINUS:
1826     case INTRINSIC_TIMES:
1827     case INTRINSIC_DIVIDE:
1828     case INTRINSIC_POWER:
1829       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1830         {
1831           gfc_type_convert_binary (e);
1832           break;
1833         }
1834
1835       sprintf (msg,
1836                _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
1837                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1838                gfc_typename (&op2->ts));
1839       goto bad_op;
1840
1841     case INTRINSIC_CONCAT:
1842       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1843         {
1844           e->ts.type = BT_CHARACTER;
1845           e->ts.kind = op1->ts.kind;
1846           break;
1847         }
1848
1849       sprintf (msg,
1850                _("Operands of string concatenation operator at %%L are %s/%s"),
1851                gfc_typename (&op1->ts), gfc_typename (&op2->ts));
1852       goto bad_op;
1853
1854     case INTRINSIC_AND:
1855     case INTRINSIC_OR:
1856     case INTRINSIC_EQV:
1857     case INTRINSIC_NEQV:
1858       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1859         {
1860           e->ts.type = BT_LOGICAL;
1861           e->ts.kind = gfc_kind_max (op1, op2);
1862           if (op1->ts.kind < e->ts.kind)
1863             gfc_convert_type (op1, &e->ts, 2);
1864           else if (op2->ts.kind < e->ts.kind)
1865             gfc_convert_type (op2, &e->ts, 2);
1866           break;
1867         }
1868
1869       sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
1870                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1871                gfc_typename (&op2->ts));
1872
1873       goto bad_op;
1874
1875     case INTRINSIC_NOT:
1876       if (op1->ts.type == BT_LOGICAL)
1877         {
1878           e->ts.type = BT_LOGICAL;
1879           e->ts.kind = op1->ts.kind;
1880           break;
1881         }
1882
1883       sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
1884                gfc_typename (&op1->ts));
1885       goto bad_op;
1886
1887     case INTRINSIC_GT:
1888     case INTRINSIC_GE:
1889     case INTRINSIC_LT:
1890     case INTRINSIC_LE:
1891       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1892         {
1893           strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
1894           goto bad_op;
1895         }
1896
1897       /* Fall through...  */
1898
1899     case INTRINSIC_EQ:
1900     case INTRINSIC_NE:
1901       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1902         {
1903           e->ts.type = BT_LOGICAL;
1904           e->ts.kind = gfc_default_logical_kind;
1905           break;
1906         }
1907
1908       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1909         {
1910           gfc_type_convert_binary (e);
1911
1912           e->ts.type = BT_LOGICAL;
1913           e->ts.kind = gfc_default_logical_kind;
1914           break;
1915         }
1916
1917       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
1918         sprintf (msg,
1919                  _("Logicals at %%L must be compared with %s instead of %s"),
1920                  e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
1921                  gfc_op2string (e->value.op.operator));
1922       else
1923         sprintf (msg,
1924                  _("Operands of comparison operator '%s' at %%L are %s/%s"),
1925                  gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1926                  gfc_typename (&op2->ts));
1927
1928       goto bad_op;
1929
1930     case INTRINSIC_USER:
1931       if (op2 == NULL)
1932         sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
1933                  e->value.op.uop->name, gfc_typename (&op1->ts));
1934       else
1935         sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
1936                  e->value.op.uop->name, gfc_typename (&op1->ts),
1937                  gfc_typename (&op2->ts));
1938
1939       goto bad_op;
1940
1941     case INTRINSIC_PARENTHESES:
1942       break;
1943
1944     default:
1945       gfc_internal_error ("resolve_operator(): Bad intrinsic");
1946     }
1947
1948   /* Deal with arrayness of an operand through an operator.  */
1949
1950   t = SUCCESS;
1951
1952   switch (e->value.op.operator)
1953     {
1954     case INTRINSIC_PLUS:
1955     case INTRINSIC_MINUS:
1956     case INTRINSIC_TIMES:
1957     case INTRINSIC_DIVIDE:
1958     case INTRINSIC_POWER:
1959     case INTRINSIC_CONCAT:
1960     case INTRINSIC_AND:
1961     case INTRINSIC_OR:
1962     case INTRINSIC_EQV:
1963     case INTRINSIC_NEQV:
1964     case INTRINSIC_EQ:
1965     case INTRINSIC_NE:
1966     case INTRINSIC_GT:
1967     case INTRINSIC_GE:
1968     case INTRINSIC_LT:
1969     case INTRINSIC_LE:
1970
1971       if (op1->rank == 0 && op2->rank == 0)
1972         e->rank = 0;
1973
1974       if (op1->rank == 0 && op2->rank != 0)
1975         {
1976           e->rank = op2->rank;
1977
1978           if (e->shape == NULL)
1979             e->shape = gfc_copy_shape (op2->shape, op2->rank);
1980         }
1981
1982       if (op1->rank != 0 && op2->rank == 0)
1983         {
1984           e->rank = op1->rank;
1985
1986           if (e->shape == NULL)
1987             e->shape = gfc_copy_shape (op1->shape, op1->rank);
1988         }
1989
1990       if (op1->rank != 0 && op2->rank != 0)
1991         {
1992           if (op1->rank == op2->rank)
1993             {
1994               e->rank = op1->rank;
1995               if (e->shape == NULL)
1996                 {
1997                   t = compare_shapes(op1, op2);
1998                   if (t == FAILURE)
1999                     e->shape = NULL;
2000                   else
2001                 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2002                 }
2003             }
2004           else
2005             {
2006               gfc_error ("Inconsistent ranks for operator at %L and %L",
2007                          &op1->where, &op2->where);
2008               t = FAILURE;
2009
2010               /* Allow higher level expressions to work.  */
2011               e->rank = 0;
2012             }
2013         }
2014
2015       break;
2016
2017     case INTRINSIC_NOT:
2018     case INTRINSIC_UPLUS:
2019     case INTRINSIC_UMINUS:
2020     case INTRINSIC_PARENTHESES:
2021       e->rank = op1->rank;
2022
2023       if (e->shape == NULL)
2024         e->shape = gfc_copy_shape (op1->shape, op1->rank);
2025
2026       /* Simply copy arrayness attribute */
2027       break;
2028
2029     default:
2030       break;
2031     }
2032
2033   /* Attempt to simplify the expression.  */
2034   if (t == SUCCESS)
2035     t = gfc_simplify_expr (e, 0);
2036   return t;
2037
2038 bad_op:
2039
2040   if (gfc_extend_expr (e) == SUCCESS)
2041     return SUCCESS;
2042
2043   gfc_error (msg, &e->where);
2044
2045   return FAILURE;
2046 }
2047
2048
2049 /************** Array resolution subroutines **************/
2050
2051
2052 typedef enum
2053 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
2054 comparison;
2055
2056 /* Compare two integer expressions.  */
2057
2058 static comparison
2059 compare_bound (gfc_expr * a, gfc_expr * b)
2060 {
2061   int i;
2062
2063   if (a == NULL || a->expr_type != EXPR_CONSTANT
2064       || b == NULL || b->expr_type != EXPR_CONSTANT)
2065     return CMP_UNKNOWN;
2066
2067   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
2068     gfc_internal_error ("compare_bound(): Bad expression");
2069
2070   i = mpz_cmp (a->value.integer, b->value.integer);
2071
2072   if (i < 0)
2073     return CMP_LT;
2074   if (i > 0)
2075     return CMP_GT;
2076   return CMP_EQ;
2077 }
2078
2079
2080 /* Compare an integer expression with an integer.  */
2081
2082 static comparison
2083 compare_bound_int (gfc_expr * a, int b)
2084 {
2085   int i;
2086
2087   if (a == NULL || a->expr_type != EXPR_CONSTANT)
2088     return CMP_UNKNOWN;
2089
2090   if (a->ts.type != BT_INTEGER)
2091     gfc_internal_error ("compare_bound_int(): Bad expression");
2092
2093   i = mpz_cmp_si (a->value.integer, b);
2094
2095   if (i < 0)
2096     return CMP_LT;
2097   if (i > 0)
2098     return CMP_GT;
2099   return CMP_EQ;
2100 }
2101
2102
2103 /* Compare a single dimension of an array reference to the array
2104    specification.  */
2105
2106 static try
2107 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
2108 {
2109
2110 /* Given start, end and stride values, calculate the minimum and
2111    maximum referenced indexes.  */
2112
2113   switch (ar->type)
2114     {
2115     case AR_FULL:
2116       break;
2117
2118     case AR_ELEMENT:
2119       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2120         goto bound;
2121       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2122         goto bound;
2123
2124       break;
2125
2126     case AR_SECTION:
2127       if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
2128         {
2129           gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
2130           return FAILURE;
2131         }
2132
2133       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2134         goto bound;
2135       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2136         goto bound;
2137
2138       /* TODO: Possibly, we could warn about end[i] being out-of-bound although
2139          it is legal (see 6.2.2.3.1).  */
2140
2141       break;
2142
2143     default:
2144       gfc_internal_error ("check_dimension(): Bad array reference");
2145     }
2146
2147   return SUCCESS;
2148
2149 bound:
2150   gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
2151   return SUCCESS;
2152 }
2153
2154
2155 /* Compare an array reference with an array specification.  */
2156
2157 static try
2158 compare_spec_to_ref (gfc_array_ref * ar)
2159 {
2160   gfc_array_spec *as;
2161   int i;
2162
2163   as = ar->as;
2164   i = as->rank - 1;
2165   /* TODO: Full array sections are only allowed as actual parameters.  */
2166   if (as->type == AS_ASSUMED_SIZE
2167       && (/*ar->type == AR_FULL
2168           ||*/ (ar->type == AR_SECTION
2169               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
2170     {
2171       gfc_error ("Rightmost upper bound of assumed size array section"
2172                  " not specified at %L", &ar->where);
2173       return FAILURE;
2174     }
2175
2176   if (ar->type == AR_FULL)
2177     return SUCCESS;
2178
2179   if (as->rank != ar->dimen)
2180     {
2181       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
2182                  &ar->where, ar->dimen, as->rank);
2183       return FAILURE;
2184     }
2185
2186   for (i = 0; i < as->rank; i++)
2187     if (check_dimension (i, ar, as) == FAILURE)
2188       return FAILURE;
2189
2190   return SUCCESS;
2191 }
2192
2193
2194 /* Resolve one part of an array index.  */
2195
2196 try
2197 gfc_resolve_index (gfc_expr * index, int check_scalar)
2198 {
2199   gfc_typespec ts;
2200
2201   if (index == NULL)
2202     return SUCCESS;
2203
2204   if (gfc_resolve_expr (index) == FAILURE)
2205     return FAILURE;
2206
2207   if (check_scalar && index->rank != 0)
2208     {
2209       gfc_error ("Array index at %L must be scalar", &index->where);
2210       return FAILURE;
2211     }
2212
2213   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
2214     {
2215       gfc_error ("Array index at %L must be of INTEGER type",
2216                  &index->where);
2217       return FAILURE;
2218     }
2219
2220   if (index->ts.type == BT_REAL)
2221     if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
2222                         &index->where) == FAILURE)
2223       return FAILURE;
2224
2225   if (index->ts.kind != gfc_index_integer_kind
2226       || index->ts.type != BT_INTEGER)
2227     {
2228       gfc_clear_ts (&ts);
2229       ts.type = BT_INTEGER;
2230       ts.kind = gfc_index_integer_kind;
2231
2232       gfc_convert_type_warn (index, &ts, 2, 0);
2233     }
2234
2235   return SUCCESS;
2236 }
2237
2238 /* Resolve a dim argument to an intrinsic function.  */
2239
2240 try
2241 gfc_resolve_dim_arg (gfc_expr *dim)
2242 {
2243   if (dim == NULL)
2244     return SUCCESS;
2245
2246   if (gfc_resolve_expr (dim) == FAILURE)
2247     return FAILURE;
2248
2249   if (dim->rank != 0)
2250     {
2251       gfc_error ("Argument dim at %L must be scalar", &dim->where);
2252       return FAILURE;
2253   
2254     }
2255   if (dim->ts.type != BT_INTEGER)
2256     {
2257       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
2258       return FAILURE;
2259     }
2260   if (dim->ts.kind != gfc_index_integer_kind)
2261     {
2262       gfc_typespec ts;
2263
2264       ts.type = BT_INTEGER;
2265       ts.kind = gfc_index_integer_kind;
2266
2267       gfc_convert_type_warn (dim, &ts, 2, 0);
2268     }
2269
2270   return SUCCESS;
2271 }
2272
2273 /* Given an expression that contains array references, update those array
2274    references to point to the right array specifications.  While this is
2275    filled in during matching, this information is difficult to save and load
2276    in a module, so we take care of it here.
2277
2278    The idea here is that the original array reference comes from the
2279    base symbol.  We traverse the list of reference structures, setting
2280    the stored reference to references.  Component references can
2281    provide an additional array specification.  */
2282
2283 static void
2284 find_array_spec (gfc_expr * e)
2285 {
2286   gfc_array_spec *as;
2287   gfc_component *c;
2288   gfc_symbol *derived;
2289   gfc_ref *ref;
2290
2291   as = e->symtree->n.sym->as;
2292   derived = NULL;
2293
2294   for (ref = e->ref; ref; ref = ref->next)
2295     switch (ref->type)
2296       {
2297       case REF_ARRAY:
2298         if (as == NULL)
2299           gfc_internal_error ("find_array_spec(): Missing spec");
2300
2301         ref->u.ar.as = as;
2302         as = NULL;
2303         break;
2304
2305       case REF_COMPONENT:
2306         if (derived == NULL)
2307           derived = e->symtree->n.sym->ts.derived;
2308
2309         c = derived->components;
2310
2311         for (; c; c = c->next)
2312           if (c == ref->u.c.component)
2313             {
2314               /* Track the sequence of component references.  */
2315               if (c->ts.type == BT_DERIVED)
2316                 derived = c->ts.derived;
2317               break;
2318             }
2319
2320         if (c == NULL)
2321           gfc_internal_error ("find_array_spec(): Component not found");
2322
2323         if (c->dimension)
2324           {
2325             if (as != NULL)
2326               gfc_internal_error ("find_array_spec(): unused as(1)");
2327             as = c->as;
2328           }
2329
2330         break;
2331
2332       case REF_SUBSTRING:
2333         break;
2334       }
2335
2336   if (as != NULL)
2337     gfc_internal_error ("find_array_spec(): unused as(2)");
2338 }
2339
2340
2341 /* Resolve an array reference.  */
2342
2343 static try
2344 resolve_array_ref (gfc_array_ref * ar)
2345 {
2346   int i, check_scalar;
2347   gfc_expr *e;
2348
2349   for (i = 0; i < ar->dimen; i++)
2350     {
2351       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
2352
2353       if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
2354         return FAILURE;
2355       if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
2356         return FAILURE;
2357       if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
2358         return FAILURE;
2359
2360       e = ar->start[i];
2361
2362       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
2363         switch (e->rank)
2364           {
2365           case 0:
2366             ar->dimen_type[i] = DIMEN_ELEMENT;
2367             break;
2368
2369           case 1:
2370             ar->dimen_type[i] = DIMEN_VECTOR;
2371             if (e->expr_type == EXPR_VARIABLE
2372                    && e->symtree->n.sym->ts.type == BT_DERIVED)
2373               ar->start[i] = gfc_get_parentheses (e);
2374             break;
2375
2376           default:
2377             gfc_error ("Array index at %L is an array of rank %d",
2378                        &ar->c_where[i], e->rank);
2379             return FAILURE;
2380           }
2381     }
2382
2383   /* If the reference type is unknown, figure out what kind it is.  */
2384
2385   if (ar->type == AR_UNKNOWN)
2386     {
2387       ar->type = AR_ELEMENT;
2388       for (i = 0; i < ar->dimen; i++)
2389         if (ar->dimen_type[i] == DIMEN_RANGE
2390             || ar->dimen_type[i] == DIMEN_VECTOR)
2391           {
2392             ar->type = AR_SECTION;
2393             break;
2394           }
2395     }
2396
2397   if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
2398     return FAILURE;
2399
2400   return SUCCESS;
2401 }
2402
2403
2404 static try
2405 resolve_substring (gfc_ref * ref)
2406 {
2407
2408   if (ref->u.ss.start != NULL)
2409     {
2410       if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2411         return FAILURE;
2412
2413       if (ref->u.ss.start->ts.type != BT_INTEGER)
2414         {
2415           gfc_error ("Substring start index at %L must be of type INTEGER",
2416                      &ref->u.ss.start->where);
2417           return FAILURE;
2418         }
2419
2420       if (ref->u.ss.start->rank != 0)
2421         {
2422           gfc_error ("Substring start index at %L must be scalar",
2423                      &ref->u.ss.start->where);
2424           return FAILURE;
2425         }
2426
2427       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT)
2428         {
2429           gfc_error ("Substring start index at %L is less than one",
2430                      &ref->u.ss.start->where);
2431           return FAILURE;
2432         }
2433     }
2434
2435   if (ref->u.ss.end != NULL)
2436     {
2437       if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2438         return FAILURE;
2439
2440       if (ref->u.ss.end->ts.type != BT_INTEGER)
2441         {
2442           gfc_error ("Substring end index at %L must be of type INTEGER",
2443                      &ref->u.ss.end->where);
2444           return FAILURE;
2445         }
2446
2447       if (ref->u.ss.end->rank != 0)
2448         {
2449           gfc_error ("Substring end index at %L must be scalar",
2450                      &ref->u.ss.end->where);
2451           return FAILURE;
2452         }
2453
2454       if (ref->u.ss.length != NULL
2455           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT)
2456         {
2457           gfc_error ("Substring end index at %L is out of bounds",
2458                      &ref->u.ss.start->where);
2459           return FAILURE;
2460         }
2461     }
2462
2463   return SUCCESS;
2464 }
2465
2466
2467 /* Resolve subtype references.  */
2468
2469 static try
2470 resolve_ref (gfc_expr * expr)
2471 {
2472   int current_part_dimension, n_components, seen_part_dimension;
2473   gfc_ref *ref;
2474
2475   for (ref = expr->ref; ref; ref = ref->next)
2476     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2477       {
2478         find_array_spec (expr);
2479         break;
2480       }
2481
2482   for (ref = expr->ref; ref; ref = ref->next)
2483     switch (ref->type)
2484       {
2485       case REF_ARRAY:
2486         if (resolve_array_ref (&ref->u.ar) == FAILURE)
2487           return FAILURE;
2488         break;
2489
2490       case REF_COMPONENT:
2491         break;
2492
2493       case REF_SUBSTRING:
2494         resolve_substring (ref);
2495         break;
2496       }
2497
2498   /* Check constraints on part references.  */
2499
2500   current_part_dimension = 0;
2501   seen_part_dimension = 0;
2502   n_components = 0;
2503
2504   for (ref = expr->ref; ref; ref = ref->next)
2505     {
2506       switch (ref->type)
2507         {
2508         case REF_ARRAY:
2509           switch (ref->u.ar.type)
2510             {
2511             case AR_FULL:
2512             case AR_SECTION:
2513               current_part_dimension = 1;
2514               break;
2515
2516             case AR_ELEMENT:
2517               current_part_dimension = 0;
2518               break;
2519
2520             case AR_UNKNOWN:
2521               gfc_internal_error ("resolve_ref(): Bad array reference");
2522             }
2523
2524           break;
2525
2526         case REF_COMPONENT:
2527           if ((current_part_dimension || seen_part_dimension)
2528               && ref->u.c.component->pointer)
2529             {
2530               gfc_error
2531                 ("Component to the right of a part reference with nonzero "
2532                  "rank must not have the POINTER attribute at %L",
2533                  &expr->where);
2534               return FAILURE;
2535             }
2536
2537           n_components++;
2538           break;
2539
2540         case REF_SUBSTRING:
2541           break;
2542         }
2543
2544       if (((ref->type == REF_COMPONENT && n_components > 1)
2545            || ref->next == NULL)
2546           && current_part_dimension
2547           && seen_part_dimension)
2548         {
2549
2550           gfc_error ("Two or more part references with nonzero rank must "
2551                      "not be specified at %L", &expr->where);
2552           return FAILURE;
2553         }
2554
2555       if (ref->type == REF_COMPONENT)
2556         {
2557           if (current_part_dimension)
2558             seen_part_dimension = 1;
2559
2560           /* reset to make sure */
2561           current_part_dimension = 0;
2562         }
2563     }
2564
2565   return SUCCESS;
2566 }
2567
2568
2569 /* Given an expression, determine its shape.  This is easier than it sounds.
2570    Leaves the shape array NULL if it is not possible to determine the shape.  */
2571
2572 static void
2573 expression_shape (gfc_expr * e)
2574 {
2575   mpz_t array[GFC_MAX_DIMENSIONS];
2576   int i;
2577
2578   if (e->rank == 0 || e->shape != NULL)
2579     return;
2580
2581   for (i = 0; i < e->rank; i++)
2582     if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2583       goto fail;
2584
2585   e->shape = gfc_get_shape (e->rank);
2586
2587   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2588
2589   return;
2590
2591 fail:
2592   for (i--; i >= 0; i--)
2593     mpz_clear (array[i]);
2594 }
2595
2596
2597 /* Given a variable expression node, compute the rank of the expression by
2598    examining the base symbol and any reference structures it may have.  */
2599
2600 static void
2601 expression_rank (gfc_expr * e)
2602 {
2603   gfc_ref *ref;
2604   int i, rank;
2605
2606   if (e->ref == NULL)
2607     {
2608       if (e->expr_type == EXPR_ARRAY)
2609         goto done;
2610       /* Constructors can have a rank different from one via RESHAPE().  */
2611
2612       if (e->symtree == NULL)
2613         {
2614           e->rank = 0;
2615           goto done;
2616         }
2617
2618       e->rank = (e->symtree->n.sym->as == NULL)
2619                   ? 0 : e->symtree->n.sym->as->rank;
2620       goto done;
2621     }
2622
2623   rank = 0;
2624
2625   for (ref = e->ref; ref; ref = ref->next)
2626     {
2627       if (ref->type != REF_ARRAY)
2628         continue;
2629
2630       if (ref->u.ar.type == AR_FULL)
2631         {
2632           rank = ref->u.ar.as->rank;
2633           break;
2634         }
2635
2636       if (ref->u.ar.type == AR_SECTION)
2637         {
2638           /* Figure out the rank of the section.  */
2639           if (rank != 0)
2640             gfc_internal_error ("expression_rank(): Two array specs");
2641
2642           for (i = 0; i < ref->u.ar.dimen; i++)
2643             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2644                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2645               rank++;
2646
2647           break;
2648         }
2649     }
2650
2651   e->rank = rank;
2652
2653 done:
2654   expression_shape (e);
2655 }
2656
2657
2658 /* Resolve a variable expression.  */
2659
2660 static try
2661 resolve_variable (gfc_expr * e)
2662 {
2663   gfc_symbol *sym;
2664
2665   if (e->ref && resolve_ref (e) == FAILURE)
2666     return FAILURE;
2667
2668   if (e->symtree == NULL)
2669     return FAILURE;
2670
2671   sym = e->symtree->n.sym;
2672   if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2673     {
2674       e->ts.type = BT_PROCEDURE;
2675       return SUCCESS;
2676     }
2677
2678   if (sym->ts.type != BT_UNKNOWN)
2679     gfc_variable_attr (e, &e->ts);
2680   else
2681     {
2682       /* Must be a simple variable reference.  */
2683       if (gfc_set_default_type (sym, 1, NULL) == FAILURE)
2684         return FAILURE;
2685       e->ts = sym->ts;
2686     }
2687
2688   if (check_assumed_size_reference (sym, e))
2689     return FAILURE;
2690
2691   return SUCCESS;
2692 }
2693
2694
2695 /* Resolve an expression.  That is, make sure that types of operands agree
2696    with their operators, intrinsic operators are converted to function calls
2697    for overloaded types and unresolved function references are resolved.  */
2698
2699 try
2700 gfc_resolve_expr (gfc_expr * e)
2701 {
2702   try t;
2703
2704   if (e == NULL)
2705     return SUCCESS;
2706
2707   switch (e->expr_type)
2708     {
2709     case EXPR_OP:
2710       t = resolve_operator (e);
2711       break;
2712
2713     case EXPR_FUNCTION:
2714       t = resolve_function (e);
2715       break;
2716
2717     case EXPR_VARIABLE:
2718       t = resolve_variable (e);
2719       if (t == SUCCESS)
2720         expression_rank (e);
2721       break;
2722
2723     case EXPR_SUBSTRING:
2724       t = resolve_ref (e);
2725       break;
2726
2727     case EXPR_CONSTANT:
2728     case EXPR_NULL:
2729       t = SUCCESS;
2730       break;
2731
2732     case EXPR_ARRAY:
2733       t = FAILURE;
2734       if (resolve_ref (e) == FAILURE)
2735         break;
2736
2737       t = gfc_resolve_array_constructor (e);
2738       /* Also try to expand a constructor.  */
2739       if (t == SUCCESS)
2740         {
2741           expression_rank (e);
2742           gfc_expand_constructor (e);
2743         }
2744
2745       break;
2746
2747     case EXPR_STRUCTURE:
2748       t = resolve_ref (e);
2749       if (t == FAILURE)
2750         break;
2751
2752       t = resolve_structure_cons (e);
2753       if (t == FAILURE)
2754         break;
2755
2756       t = gfc_simplify_expr (e, 0);
2757       break;
2758
2759     default:
2760       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
2761     }
2762
2763   return t;
2764 }
2765
2766
2767 /* Resolve an expression from an iterator.  They must be scalar and have
2768    INTEGER or (optionally) REAL type.  */
2769
2770 static try
2771 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
2772                            const char * name_msgid)
2773 {
2774   if (gfc_resolve_expr (expr) == FAILURE)
2775     return FAILURE;
2776
2777   if (expr->rank != 0)
2778     {
2779       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
2780       return FAILURE;
2781     }
2782
2783   if (!(expr->ts.type == BT_INTEGER
2784         || (expr->ts.type == BT_REAL && real_ok)))
2785     {
2786       if (real_ok)
2787         gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
2788                    &expr->where);
2789       else
2790         gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
2791       return FAILURE;
2792     }
2793   return SUCCESS;
2794 }
2795
2796
2797 /* Resolve the expressions in an iterator structure.  If REAL_OK is
2798    false allow only INTEGER type iterators, otherwise allow REAL types.  */
2799
2800 try
2801 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
2802 {
2803
2804   if (iter->var->ts.type == BT_REAL)
2805     gfc_notify_std (GFC_STD_F95_DEL,
2806                     "Obsolete: REAL DO loop iterator at %L",
2807                     &iter->var->where);
2808
2809   if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
2810       == FAILURE)
2811     return FAILURE;
2812
2813   if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
2814     {
2815       gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
2816                  &iter->var->where);
2817       return FAILURE;
2818     }
2819
2820   if (gfc_resolve_iterator_expr (iter->start, real_ok,
2821                                  "Start expression in DO loop") == FAILURE)
2822     return FAILURE;
2823
2824   if (gfc_resolve_iterator_expr (iter->end, real_ok,
2825                                  "End expression in DO loop") == FAILURE)
2826     return FAILURE;
2827
2828   if (gfc_resolve_iterator_expr (iter->step, real_ok,
2829                                  "Step expression in DO loop") == FAILURE)
2830     return FAILURE;
2831
2832   if (iter->step->expr_type == EXPR_CONSTANT)
2833     {
2834       if ((iter->step->ts.type == BT_INTEGER
2835            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
2836           || (iter->step->ts.type == BT_REAL
2837               && mpfr_sgn (iter->step->value.real) == 0))
2838         {
2839           gfc_error ("Step expression in DO loop at %L cannot be zero",
2840                      &iter->step->where);
2841           return FAILURE;
2842         }
2843     }
2844
2845   /* Convert start, end, and step to the same type as var.  */
2846   if (iter->start->ts.kind != iter->var->ts.kind
2847       || iter->start->ts.type != iter->var->ts.type)
2848     gfc_convert_type (iter->start, &iter->var->ts, 2);
2849
2850   if (iter->end->ts.kind != iter->var->ts.kind
2851       || iter->end->ts.type != iter->var->ts.type)
2852     gfc_convert_type (iter->end, &iter->var->ts, 2);
2853
2854   if (iter->step->ts.kind != iter->var->ts.kind
2855       || iter->step->ts.type != iter->var->ts.type)
2856     gfc_convert_type (iter->step, &iter->var->ts, 2);
2857
2858   return SUCCESS;
2859 }
2860
2861
2862 /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
2863    to be a scalar INTEGER variable.  The subscripts and stride are scalar
2864    INTEGERs, and if stride is a constant it must be nonzero.  */
2865
2866 static void
2867 resolve_forall_iterators (gfc_forall_iterator * iter)
2868 {
2869
2870   while (iter)
2871     {
2872       if (gfc_resolve_expr (iter->var) == SUCCESS
2873           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
2874         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
2875                    &iter->var->where);
2876
2877       if (gfc_resolve_expr (iter->start) == SUCCESS
2878           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
2879         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
2880                    &iter->start->where);
2881       if (iter->var->ts.kind != iter->start->ts.kind)
2882         gfc_convert_type (iter->start, &iter->var->ts, 2);
2883
2884       if (gfc_resolve_expr (iter->end) == SUCCESS
2885           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
2886         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
2887                    &iter->end->where);
2888       if (iter->var->ts.kind != iter->end->ts.kind)
2889         gfc_convert_type (iter->end, &iter->var->ts, 2);
2890
2891       if (gfc_resolve_expr (iter->stride) == SUCCESS)
2892         {
2893           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
2894             gfc_error ("FORALL stride expression at %L must be a scalar %s",
2895                         &iter->stride->where, "INTEGER");
2896
2897           if (iter->stride->expr_type == EXPR_CONSTANT
2898               && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
2899             gfc_error ("FORALL stride expression at %L cannot be zero",
2900                        &iter->stride->where);
2901         }
2902       if (iter->var->ts.kind != iter->stride->ts.kind)
2903         gfc_convert_type (iter->stride, &iter->var->ts, 2);
2904
2905       iter = iter->next;
2906     }
2907 }
2908
2909
2910 /* Given a pointer to a symbol that is a derived type, see if any components
2911    have the POINTER attribute.  The search is recursive if necessary.
2912    Returns zero if no pointer components are found, nonzero otherwise.  */
2913
2914 static int
2915 derived_pointer (gfc_symbol * sym)
2916 {
2917   gfc_component *c;
2918
2919   for (c = sym->components; c; c = c->next)
2920     {
2921       if (c->pointer)
2922         return 1;
2923
2924       if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
2925         return 1;
2926     }
2927
2928   return 0;
2929 }
2930
2931
2932 /* Given a pointer to a symbol that is a derived type, see if it's
2933    inaccessible, i.e. if it's defined in another module and the components are
2934    PRIVATE.  The search is recursive if necessary.  Returns zero if no
2935    inaccessible components are found, nonzero otherwise.  */
2936
2937 static int
2938 derived_inaccessible (gfc_symbol *sym)
2939 {
2940   gfc_component *c;
2941
2942   if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
2943     return 1;
2944
2945   for (c = sym->components; c; c = c->next)
2946     {
2947         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
2948           return 1;
2949     }
2950
2951   return 0;
2952 }
2953
2954
2955 /* Resolve the argument of a deallocate expression.  The expression must be
2956    a pointer or a full array.  */
2957
2958 static try
2959 resolve_deallocate_expr (gfc_expr * e)
2960 {
2961   symbol_attribute attr;
2962   int allocatable;
2963   gfc_ref *ref;
2964
2965   if (gfc_resolve_expr (e) == FAILURE)
2966     return FAILURE;
2967
2968   attr = gfc_expr_attr (e);
2969   if (attr.pointer)
2970     return SUCCESS;
2971
2972   if (e->expr_type != EXPR_VARIABLE)
2973     goto bad;
2974
2975   allocatable = e->symtree->n.sym->attr.allocatable;
2976   for (ref = e->ref; ref; ref = ref->next)
2977     switch (ref->type)
2978       {
2979       case REF_ARRAY:
2980         if (ref->u.ar.type != AR_FULL)
2981           allocatable = 0;
2982         break;
2983
2984       case REF_COMPONENT:
2985         allocatable = (ref->u.c.component->as != NULL
2986                        && ref->u.c.component->as->type == AS_DEFERRED);
2987         break;
2988
2989       case REF_SUBSTRING:
2990         allocatable = 0;
2991         break;
2992       }
2993
2994   if (allocatable == 0)
2995     {
2996     bad:
2997       gfc_error ("Expression in DEALLOCATE statement at %L must be "
2998                  "ALLOCATABLE or a POINTER", &e->where);
2999     }
3000
3001   if (e->symtree->n.sym->attr.intent == INTENT_IN)
3002     {
3003       gfc_error ("Can't deallocate INTENT(IN) variable '%s' at %L",
3004                  e->symtree->n.sym->name, &e->where);
3005       return FAILURE;
3006     }
3007
3008   return SUCCESS;
3009 }
3010
3011
3012 /* Given the expression node e for an allocatable/pointer of derived type to be
3013    allocated, get the expression node to be initialized afterwards (needed for
3014    derived types with default initializers).  */
3015
3016 static gfc_expr *
3017 expr_to_initialize (gfc_expr * e)
3018 {
3019   gfc_expr *result;
3020   gfc_ref *ref;
3021   int i;
3022
3023   result = gfc_copy_expr (e);
3024
3025   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
3026   for (ref = result->ref; ref; ref = ref->next)
3027     if (ref->type == REF_ARRAY && ref->next == NULL)
3028       {
3029         ref->u.ar.type = AR_FULL;
3030
3031         for (i = 0; i < ref->u.ar.dimen; i++)
3032           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
3033
3034         result->rank = ref->u.ar.dimen; 
3035         break;
3036       }
3037
3038   return result;
3039 }
3040
3041
3042 /* Resolve the expression in an ALLOCATE statement, doing the additional
3043    checks to see whether the expression is OK or not.  The expression must
3044    have a trailing array reference that gives the size of the array.  */
3045
3046 static try
3047 resolve_allocate_expr (gfc_expr * e, gfc_code * code)
3048 {
3049   int i, pointer, allocatable, dimension;
3050   symbol_attribute attr;
3051   gfc_ref *ref, *ref2;
3052   gfc_array_ref *ar;
3053   gfc_code *init_st;
3054   gfc_expr *init_e;
3055
3056   if (gfc_resolve_expr (e) == FAILURE)
3057     return FAILURE;
3058
3059   /* Make sure the expression is allocatable or a pointer.  If it is
3060      pointer, the next-to-last reference must be a pointer.  */
3061
3062   ref2 = NULL;
3063
3064   if (e->expr_type != EXPR_VARIABLE)
3065     {
3066       allocatable = 0;
3067
3068       attr = gfc_expr_attr (e);
3069       pointer = attr.pointer;
3070       dimension = attr.dimension;
3071
3072     }
3073   else
3074     {
3075       allocatable = e->symtree->n.sym->attr.allocatable;
3076       pointer = e->symtree->n.sym->attr.pointer;
3077       dimension = e->symtree->n.sym->attr.dimension;
3078
3079       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
3080         switch (ref->type)
3081           {
3082           case REF_ARRAY:
3083             if (ref->next != NULL)
3084               pointer = 0;
3085             break;
3086
3087           case REF_COMPONENT:
3088             allocatable = (ref->u.c.component->as != NULL
3089                            && ref->u.c.component->as->type == AS_DEFERRED);
3090
3091             pointer = ref->u.c.component->pointer;
3092             dimension = ref->u.c.component->dimension;
3093             break;
3094
3095           case REF_SUBSTRING:
3096             allocatable = 0;
3097             pointer = 0;
3098             break;
3099           }
3100     }
3101
3102   if (allocatable == 0 && pointer == 0)
3103     {
3104       gfc_error ("Expression in ALLOCATE statement at %L must be "
3105                  "ALLOCATABLE or a POINTER", &e->where);
3106       return FAILURE;
3107     }
3108
3109   if (e->symtree->n.sym->attr.intent == INTENT_IN)
3110     {
3111       gfc_error ("Can't allocate INTENT(IN) variable '%s' at %L",
3112                  e->symtree->n.sym->name, &e->where);
3113       return FAILURE;
3114     }
3115
3116   /* Add default initializer for those derived types that need them.  */
3117   if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
3118     {
3119         init_st = gfc_get_code ();
3120         init_st->loc = code->loc;
3121         init_st->op = EXEC_ASSIGN;
3122         init_st->expr = expr_to_initialize (e);
3123         init_st->expr2 = init_e;
3124
3125         init_st->next = code->next;
3126         code->next = init_st;
3127     }
3128
3129   if (pointer && dimension == 0)
3130     return SUCCESS;
3131
3132   /* Make sure the next-to-last reference node is an array specification.  */
3133
3134   if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
3135     {
3136       gfc_error ("Array specification required in ALLOCATE statement "
3137                  "at %L", &e->where);
3138       return FAILURE;
3139     }
3140
3141   if (ref2->u.ar.type == AR_ELEMENT)
3142     return SUCCESS;
3143
3144   /* Make sure that the array section reference makes sense in the
3145     context of an ALLOCATE specification.  */
3146
3147   ar = &ref2->u.ar;
3148
3149   for (i = 0; i < ar->dimen; i++)
3150     switch (ar->dimen_type[i])
3151       {
3152       case DIMEN_ELEMENT:
3153         break;
3154
3155       case DIMEN_RANGE:
3156         if (ar->start[i] != NULL
3157             && ar->end[i] != NULL
3158             && ar->stride[i] == NULL)
3159           break;
3160
3161         /* Fall Through...  */
3162
3163       case DIMEN_UNKNOWN:
3164       case DIMEN_VECTOR:
3165         gfc_error ("Bad array specification in ALLOCATE statement at %L",
3166                    &e->where);
3167         return FAILURE;
3168       }
3169
3170   return SUCCESS;
3171 }
3172
3173
3174 /************ SELECT CASE resolution subroutines ************/
3175
3176 /* Callback function for our mergesort variant.  Determines interval
3177    overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
3178    op1 > op2.  Assumes we're not dealing with the default case.  
3179    We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
3180    There are nine situations to check.  */
3181
3182 static int
3183 compare_cases (const gfc_case * op1, const gfc_case * op2)
3184 {
3185   int retval;
3186
3187   if (op1->low == NULL) /* op1 = (:L)  */
3188     {
3189       /* op2 = (:N), so overlap.  */
3190       retval = 0;
3191       /* op2 = (M:) or (M:N),  L < M  */
3192       if (op2->low != NULL
3193           && gfc_compare_expr (op1->high, op2->low) < 0)
3194         retval = -1;
3195     }
3196   else if (op1->high == NULL) /* op1 = (K:)  */
3197     {
3198       /* op2 = (M:), so overlap.  */
3199       retval = 0;
3200       /* op2 = (:N) or (M:N), K > N  */
3201       if (op2->high != NULL
3202           && gfc_compare_expr (op1->low, op2->high) > 0)
3203         retval = 1;
3204     }
3205   else /* op1 = (K:L)  */
3206     {
3207       if (op2->low == NULL)       /* op2 = (:N), K > N  */
3208         retval = (gfc_compare_expr (op1->low, op2->high) > 0) ? 1 : 0;
3209       else if (op2->high == NULL) /* op2 = (M:), L < M  */
3210         retval = (gfc_compare_expr (op1->high, op2->low) < 0) ? -1 : 0;
3211       else                        /* op2 = (M:N)  */
3212         {
3213           retval =  0;
3214           /* L < M  */
3215           if (gfc_compare_expr (op1->high, op2->low) < 0)
3216             retval =  -1;
3217           /* K > N  */
3218           else if (gfc_compare_expr (op1->low, op2->high) > 0)
3219             retval =  1;
3220         }
3221     }
3222
3223   return retval;
3224 }
3225
3226
3227 /* Merge-sort a double linked case list, detecting overlap in the
3228    process.  LIST is the head of the double linked case list before it
3229    is sorted.  Returns the head of the sorted list if we don't see any
3230    overlap, or NULL otherwise.  */
3231
3232 static gfc_case *
3233 check_case_overlap (gfc_case * list)
3234 {
3235   gfc_case *p, *q, *e, *tail;
3236   int insize, nmerges, psize, qsize, cmp, overlap_seen;
3237
3238   /* If the passed list was empty, return immediately.  */
3239   if (!list)
3240     return NULL;
3241
3242   overlap_seen = 0;
3243   insize = 1;
3244
3245   /* Loop unconditionally.  The only exit from this loop is a return
3246      statement, when we've finished sorting the case list.  */
3247   for (;;)
3248     {
3249       p = list;
3250       list = NULL;
3251       tail = NULL;
3252
3253       /* Count the number of merges we do in this pass.  */
3254       nmerges = 0;
3255
3256       /* Loop while there exists a merge to be done.  */
3257       while (p)
3258         {
3259           int i;
3260
3261           /* Count this merge.  */
3262           nmerges++;
3263
3264           /* Cut the list in two pieces by stepping INSIZE places
3265              forward in the list, starting from P.  */
3266           psize = 0;
3267           q = p;
3268           for (i = 0; i < insize; i++)
3269             {
3270               psize++;
3271               q = q->right;
3272               if (!q)
3273                 break;
3274             }
3275           qsize = insize;
3276
3277           /* Now we have two lists.  Merge them!  */
3278           while (psize > 0 || (qsize > 0 && q != NULL))
3279             {
3280
3281               /* See from which the next case to merge comes from.  */
3282               if (psize == 0)
3283                 {
3284                   /* P is empty so the next case must come from Q.  */
3285                   e = q;
3286                   q = q->right;
3287                   qsize--;
3288                 }
3289               else if (qsize == 0 || q == NULL)
3290                 {
3291                   /* Q is empty.  */
3292                   e = p;
3293                   p = p->right;
3294                   psize--;
3295                 }
3296               else
3297                 {
3298                   cmp = compare_cases (p, q);
3299                   if (cmp < 0)
3300                     {
3301                       /* The whole case range for P is less than the
3302                          one for Q.  */
3303                       e = p;
3304                       p = p->right;
3305                       psize--;
3306                     }
3307                   else if (cmp > 0)
3308                     {
3309                       /* The whole case range for Q is greater than
3310                          the case range for P.  */
3311                       e = q;
3312                       q = q->right;
3313                       qsize--;
3314                     }
3315                   else
3316                     {
3317                       /* The cases overlap, or they are the same
3318                          element in the list.  Either way, we must
3319                          issue an error and get the next case from P.  */
3320                       /* FIXME: Sort P and Q by line number.  */
3321                       gfc_error ("CASE label at %L overlaps with CASE "
3322                                  "label at %L", &p->where, &q->where);
3323                       overlap_seen = 1;
3324                       e = p;
3325                       p = p->right;
3326                       psize--;
3327                     }
3328                 }
3329
3330                 /* Add the next element to the merged list.  */
3331               if (tail)
3332                 tail->right = e;
3333               else
3334                 list = e;
3335               e->left = tail;
3336               tail = e;
3337             }
3338
3339           /* P has now stepped INSIZE places along, and so has Q.  So
3340              they're the same.  */
3341           p = q;
3342         }
3343       tail->right = NULL;
3344
3345       /* If we have done only one merge or none at all, we've
3346          finished sorting the cases.  */
3347       if (nmerges <= 1)
3348         {
3349           if (!overlap_seen)
3350             return list;
3351           else
3352             return NULL;
3353         }
3354
3355       /* Otherwise repeat, merging lists twice the size.  */
3356       insize *= 2;
3357     }
3358 }
3359
3360
3361 /* Check to see if an expression is suitable for use in a CASE statement.
3362    Makes sure that all case expressions are scalar constants of the same
3363    type.  Return FAILURE if anything is wrong.  */
3364
3365 static try
3366 validate_case_label_expr (gfc_expr * e, gfc_expr * case_expr)
3367 {
3368   if (e == NULL) return SUCCESS;
3369
3370   if (e->ts.type != case_expr->ts.type)
3371     {
3372       gfc_error ("Expression in CASE statement at %L must be of type %s",
3373                  &e->where, gfc_basic_typename (case_expr->ts.type));
3374       return FAILURE;
3375     }
3376
3377   /* C805 (R808) For a given case-construct, each case-value shall be of
3378      the same type as case-expr.  For character type, length differences
3379      are allowed, but the kind type parameters shall be the same.  */
3380
3381   if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
3382     {
3383       gfc_error("Expression in CASE statement at %L must be kind %d",
3384                 &e->where, case_expr->ts.kind);
3385       return FAILURE;
3386     }
3387
3388   /* Convert the case value kind to that of case expression kind, if needed.
3389      FIXME:  Should a warning be issued?  */
3390   if (e->ts.kind != case_expr->ts.kind)
3391     gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
3392
3393   if (e->rank != 0)
3394     {
3395       gfc_error ("Expression in CASE statement at %L must be scalar",
3396                  &e->where);
3397       return FAILURE;
3398     }
3399
3400   return SUCCESS;
3401 }
3402
3403
3404 /* Given a completely parsed select statement, we:
3405
3406      - Validate all expressions and code within the SELECT.
3407      - Make sure that the selection expression is not of the wrong type.
3408      - Make sure that no case ranges overlap.
3409      - Eliminate unreachable cases and unreachable code resulting from
3410        removing case labels.
3411
3412    The standard does allow unreachable cases, e.g. CASE (5:3).  But
3413    they are a hassle for code generation, and to prevent that, we just
3414    cut them out here.  This is not necessary for overlapping cases
3415    because they are illegal and we never even try to generate code.
3416
3417    We have the additional caveat that a SELECT construct could have
3418    been a computed GOTO in the source code. Fortunately we can fairly
3419    easily work around that here: The case_expr for a "real" SELECT CASE
3420    is in code->expr1, but for a computed GOTO it is in code->expr2. All
3421    we have to do is make sure that the case_expr is a scalar integer
3422    expression.  */
3423
3424 static void
3425 resolve_select (gfc_code * code)
3426 {
3427   gfc_code *body;
3428   gfc_expr *case_expr;
3429   gfc_case *cp, *default_case, *tail, *head;
3430   int seen_unreachable;
3431   int ncases;
3432   bt type;
3433   try t;
3434
3435   if (code->expr == NULL)
3436     {
3437       /* This was actually a computed GOTO statement.  */
3438       case_expr = code->expr2;
3439       if (case_expr->ts.type != BT_INTEGER
3440           || case_expr->rank != 0)
3441         gfc_error ("Selection expression in computed GOTO statement "
3442                    "at %L must be a scalar integer expression",
3443                    &case_expr->where);
3444
3445       /* Further checking is not necessary because this SELECT was built
3446          by the compiler, so it should always be OK.  Just move the
3447          case_expr from expr2 to expr so that we can handle computed
3448          GOTOs as normal SELECTs from here on.  */
3449       code->expr = code->expr2;
3450       code->expr2 = NULL;
3451       return;
3452     }
3453
3454   case_expr = code->expr;
3455
3456   type = case_expr->ts.type;
3457   if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
3458     {
3459       gfc_error ("Argument of SELECT statement at %L cannot be %s",
3460                  &case_expr->where, gfc_typename (&case_expr->ts));
3461
3462       /* Punt. Going on here just produce more garbage error messages.  */
3463       return;
3464     }
3465
3466   if (case_expr->rank != 0)
3467     {
3468       gfc_error ("Argument of SELECT statement at %L must be a scalar "
3469                  "expression", &case_expr->where);
3470
3471       /* Punt.  */
3472       return;
3473     }
3474
3475   /* PR 19168 has a long discussion concerning a mismatch of the kinds
3476      of the SELECT CASE expression and its CASE values.  Walk the lists
3477      of case values, and if we find a mismatch, promote case_expr to
3478      the appropriate kind.  */
3479
3480   if (type == BT_LOGICAL || type == BT_INTEGER)
3481     {
3482       for (body = code->block; body; body = body->block)
3483         {
3484           /* Walk the case label list.  */
3485           for (cp = body->ext.case_list; cp; cp = cp->next)
3486             {
3487               /* Intercept the DEFAULT case.  It does not have a kind.  */
3488               if (cp->low == NULL && cp->high == NULL)
3489                 continue;
3490
3491               /* Unreachable case ranges are discarded, so ignore.  */  
3492               if (cp->low != NULL && cp->high != NULL
3493                   && cp->low != cp->high
3494                   && gfc_compare_expr (cp->low, cp->high) > 0)
3495                 continue;
3496
3497               /* FIXME: Should a warning be issued?  */
3498               if (cp->low != NULL
3499                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
3500                 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
3501
3502               if (cp->high != NULL
3503                   && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
3504                 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
3505             }
3506          }
3507     }
3508
3509   /* Assume there is no DEFAULT case.  */
3510   default_case = NULL;
3511   head = tail = NULL;
3512   ncases = 0;
3513
3514   for (body = code->block; body; body = body->block)
3515     {
3516       /* Assume the CASE list is OK, and all CASE labels can be matched.  */
3517       t = SUCCESS;
3518       seen_unreachable = 0;
3519
3520       /* Walk the case label list, making sure that all case labels
3521          are legal.  */
3522       for (cp = body->ext.case_list; cp; cp = cp->next)
3523         {
3524           /* Count the number of cases in the whole construct.  */
3525           ncases++;
3526
3527           /* Intercept the DEFAULT case.  */
3528           if (cp->low == NULL && cp->high == NULL)
3529             {
3530               if (default_case != NULL)
3531                 {
3532                   gfc_error ("The DEFAULT CASE at %L cannot be followed "
3533                              "by a second DEFAULT CASE at %L",
3534                              &default_case->where, &cp->where);
3535                   t = FAILURE;
3536                   break;
3537                 }
3538               else
3539                 {
3540                   default_case = cp;
3541                   continue;
3542                 }
3543             }
3544
3545           /* Deal with single value cases and case ranges.  Errors are
3546              issued from the validation function.  */
3547           if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
3548              || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
3549             {
3550               t = FAILURE;
3551               break;
3552             }
3553
3554           if (type == BT_LOGICAL
3555               && ((cp->low == NULL || cp->high == NULL)
3556                   || cp->low != cp->high))
3557             {
3558               gfc_error
3559                 ("Logical range in CASE statement at %L is not allowed",
3560                  &cp->low->where);
3561               t = FAILURE;
3562               break;
3563             }
3564
3565           if (cp->low != NULL && cp->high != NULL
3566               && cp->low != cp->high
3567               && gfc_compare_expr (cp->low, cp->high) > 0)
3568             {
3569               if (gfc_option.warn_surprising)
3570                 gfc_warning ("Range specification at %L can never "
3571                              "be matched", &cp->where);
3572
3573               cp->unreachable = 1;
3574               seen_unreachable = 1;
3575             }
3576           else
3577             {
3578               /* If the case range can be matched, it can also overlap with
3579                  other cases.  To make sure it does not, we put it in a
3580                  double linked list here.  We sort that with a merge sort
3581                  later on to detect any overlapping cases.  */
3582               if (!head)
3583                 {
3584                   head = tail = cp;
3585                   head->right = head->left = NULL;
3586                 }
3587               else
3588                 {
3589                   tail->right = cp;
3590                   tail->right->left = tail;
3591                   tail = tail->right;
3592                   tail->right = NULL;
3593                 }
3594             }
3595         }
3596
3597       /* It there was a failure in the previous case label, give up
3598          for this case label list.  Continue with the next block.  */
3599       if (t == FAILURE)
3600         continue;
3601
3602       /* See if any case labels that are unreachable have been seen.
3603          If so, we eliminate them.  This is a bit of a kludge because
3604          the case lists for a single case statement (label) is a
3605          single forward linked lists.  */
3606       if (seen_unreachable)
3607       {
3608         /* Advance until the first case in the list is reachable.  */
3609         while (body->ext.case_list != NULL
3610                && body->ext.case_list->unreachable)
3611           {
3612             gfc_case *n = body->ext.case_list;
3613             body->ext.case_list = body->ext.case_list->next;
3614             n->next = NULL;
3615             gfc_free_case_list (n);
3616           }
3617
3618         /* Strip all other unreachable cases.  */
3619         if (body->ext.case_list)
3620           {
3621             for (cp = body->ext.case_list; cp->next; cp = cp->next)
3622               {
3623                 if (cp->next->unreachable)
3624                   {
3625                     gfc_case *n = cp->next;
3626                     cp->next = cp->next->next;
3627                     n->next = NULL;
3628                     gfc_free_case_list (n);
3629                   }
3630               }
3631           }
3632       }
3633     }
3634
3635   /* See if there were overlapping cases.  If the check returns NULL,
3636      there was overlap.  In that case we don't do anything.  If head
3637      is non-NULL, we prepend the DEFAULT case.  The sorted list can
3638      then used during code generation for SELECT CASE constructs with
3639      a case expression of a CHARACTER type.  */
3640   if (head)
3641     {
3642       head = check_case_overlap (head);
3643
3644       /* Prepend the default_case if it is there.  */
3645       if (head != NULL && default_case)
3646         {
3647           default_case->left = NULL;
3648           default_case->right = head;
3649           head->left = default_case;
3650         }
3651     }
3652
3653   /* Eliminate dead blocks that may be the result if we've seen
3654      unreachable case labels for a block.  */
3655   for (body = code; body && body->block; body = body->block)
3656     {
3657       if (body->block->ext.case_list == NULL)
3658         {
3659           /* Cut the unreachable block from the code chain.  */
3660           gfc_code *c = body->block;
3661           body->block = c->block;
3662
3663           /* Kill the dead block, but not the blocks below it.  */
3664           c->block = NULL;
3665           gfc_free_statements (c);
3666         }
3667     }
3668
3669   /* More than two cases is legal but insane for logical selects.
3670      Issue a warning for it.  */
3671   if (gfc_option.warn_surprising && type == BT_LOGICAL
3672       && ncases > 2)
3673     gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
3674                  &code->loc);
3675 }
3676
3677
3678 /* Resolve a transfer statement. This is making sure that:
3679    -- a derived type being transferred has only non-pointer components
3680    -- a derived type being transferred doesn't have private components, unless 
3681       it's being transferred from the module where the type was defined
3682    -- we're not trying to transfer a whole assumed size array.  */
3683
3684 static void
3685 resolve_transfer (gfc_code * code)
3686 {
3687   gfc_typespec *ts;
3688   gfc_symbol *sym;
3689   gfc_ref *ref;
3690   gfc_expr *exp;
3691
3692   exp = code->expr;
3693
3694   if (exp->expr_type != EXPR_VARIABLE)
3695     return;
3696
3697   sym = exp->symtree->n.sym;
3698   ts = &sym->ts;
3699
3700   /* Go to actual component transferred.  */
3701   for (ref = code->expr->ref; ref; ref = ref->next)
3702     if (ref->type == REF_COMPONENT)
3703       ts = &ref->u.c.component->ts;
3704
3705   if (ts->type == BT_DERIVED)
3706     {
3707       /* Check that transferred derived type doesn't contain POINTER
3708          components.  */
3709       if (derived_pointer (ts->derived))
3710         {
3711           gfc_error ("Data transfer element at %L cannot have "
3712                      "POINTER components", &code->loc);
3713           return;
3714         }
3715
3716       if (derived_inaccessible (ts->derived))
3717         {
3718           gfc_error ("Data transfer element at %L cannot have "
3719                      "PRIVATE components",&code->loc);
3720           return;
3721         }
3722     }
3723
3724   if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
3725       && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
3726     {
3727       gfc_error ("Data transfer element at %L cannot be a full reference to "
3728                  "an assumed-size array", &code->loc);
3729       return;
3730     }
3731 }
3732
3733
3734 /*********** Toplevel code resolution subroutines ***********/
3735
3736 /* Given a branch to a label and a namespace, if the branch is conforming.
3737    The code node described where the branch is located.  */
3738
3739 static void
3740 resolve_branch (gfc_st_label * label, gfc_code * code)
3741 {
3742   gfc_code *block, *found;
3743   code_stack *stack;
3744   gfc_st_label *lp;
3745
3746   if (label == NULL)
3747     return;
3748   lp = label;
3749
3750   /* Step one: is this a valid branching target?  */
3751
3752   if (lp->defined == ST_LABEL_UNKNOWN)
3753     {
3754       gfc_error ("Label %d referenced at %L is never defined", lp->value,
3755                  &lp->where);
3756       return;
3757     }
3758
3759   if (lp->defined != ST_LABEL_TARGET)
3760     {
3761       gfc_error ("Statement at %L is not a valid branch target statement "
3762                  "for the branch statement at %L", &lp->where, &code->loc);
3763       return;
3764     }
3765
3766   /* Step two: make sure this branch is not a branch to itself ;-)  */
3767
3768   if (code->here == label)
3769     {
3770       gfc_warning ("Branch at %L causes an infinite loop", &code->loc);
3771       return;
3772     }
3773
3774   /* Step three: Try to find the label in the parse tree. To do this,
3775      we traverse the tree block-by-block: first the block that
3776      contains this GOTO, then the block that it is nested in, etc.  We
3777      can ignore other blocks because branching into another block is
3778      not allowed.  */
3779
3780   found = NULL;
3781
3782   for (stack = cs_base; stack; stack = stack->prev)
3783     {
3784       for (block = stack->head; block; block = block->next)
3785         {
3786           if (block->here == label)
3787             {
3788               found = block;
3789               break;
3790             }
3791         }
3792
3793       if (found)
3794         break;
3795     }
3796
3797   if (found == NULL)
3798     {
3799       /* The label is not in an enclosing block, so illegal.  This was
3800          allowed in Fortran 66, so we allow it as extension.  We also 
3801          forego further checks if we run into this.  */
3802       gfc_notify_std (GFC_STD_LEGACY,
3803                       "Label at %L is not in the same block as the "
3804                       "GOTO statement at %L", &lp->where, &code->loc);
3805       return;
3806     }
3807
3808   /* Step four: Make sure that the branching target is legal if
3809      the statement is an END {SELECT,DO,IF}.  */
3810
3811   if (found->op == EXEC_NOP)
3812     {
3813       for (stack = cs_base; stack; stack = stack->prev)
3814         if (stack->current->next == found)
3815           break;
3816
3817       if (stack == NULL)
3818         gfc_notify_std (GFC_STD_F95_DEL,
3819                         "Obsolete: GOTO at %L jumps to END of construct at %L",
3820                         &code->loc, &found->loc);
3821     }
3822 }
3823
3824
3825 /* Check whether EXPR1 has the same shape as EXPR2.  */
3826
3827 static try
3828 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
3829 {
3830   mpz_t shape[GFC_MAX_DIMENSIONS];
3831   mpz_t shape2[GFC_MAX_DIMENSIONS];
3832   try result = FAILURE;
3833   int i;
3834
3835   /* Compare the rank.  */
3836   if (expr1->rank != expr2->rank)
3837     return result;
3838
3839   /* Compare the size of each dimension.  */
3840   for (i=0; i<expr1->rank; i++)
3841     {
3842       if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
3843         goto ignore;
3844
3845       if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
3846         goto ignore;
3847
3848       if (mpz_cmp (shape[i], shape2[i]))
3849         goto over;
3850     }
3851
3852   /* When either of the two expression is an assumed size array, we
3853      ignore the comparison of dimension sizes.  */
3854 ignore:
3855   result = SUCCESS;
3856
3857 over:
3858   for (i--; i>=0; i--)
3859     {
3860       mpz_clear (shape[i]);
3861       mpz_clear (shape2[i]);
3862     }
3863   return result;
3864 }
3865
3866
3867 /* Check whether a WHERE assignment target or a WHERE mask expression
3868    has the same shape as the outmost WHERE mask expression.  */
3869
3870 static void
3871 resolve_where (gfc_code *code, gfc_expr *mask)
3872 {
3873   gfc_code *cblock;
3874   gfc_code *cnext;
3875   gfc_expr *e = NULL;
3876
3877   cblock = code->block;
3878
3879   /* Store the first WHERE mask-expr of the WHERE statement or construct.
3880      In case of nested WHERE, only the outmost one is stored.  */
3881   if (mask == NULL) /* outmost WHERE */
3882     e = cblock->expr;
3883   else /* inner WHERE */
3884     e = mask;
3885
3886   while (cblock)
3887     {
3888       if (cblock->expr)
3889         {
3890           /* Check if the mask-expr has a consistent shape with the
3891              outmost WHERE mask-expr.  */
3892           if (resolve_where_shape (cblock->expr, e) == FAILURE)
3893             gfc_error ("WHERE mask at %L has inconsistent shape",
3894                        &cblock->expr->where);
3895          }
3896
3897       /* the assignment statement of a WHERE statement, or the first
3898          statement in where-body-construct of a WHERE construct */
3899       cnext = cblock->next;
3900       while (cnext)
3901         {
3902           switch (cnext->op)
3903             {
3904             /* WHERE assignment statement */
3905             case EXEC_ASSIGN:
3906
3907               /* Check shape consistent for WHERE assignment target.  */
3908               if (e && resolve_where_shape (cnext->expr, e) == FAILURE)
3909                gfc_error ("WHERE assignment target at %L has "
3910                           "inconsistent shape", &cnext->expr->where);
3911               break;
3912
3913             /* WHERE or WHERE construct is part of a where-body-construct */
3914             case EXEC_WHERE:
3915               resolve_where (cnext, e);
3916               break;
3917
3918             default:
3919               gfc_error ("Unsupported statement inside WHERE at %L",
3920                          &cnext->loc);
3921             }
3922          /* the next statement within the same where-body-construct */
3923          cnext = cnext->next;
3924        }
3925     /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
3926     cblock = cblock->block;
3927   }
3928 }
3929
3930
3931 /* Check whether the FORALL index appears in the expression or not.  */
3932
3933 static try
3934 gfc_find_forall_index (gfc_expr *expr, gfc_symbol *symbol)
3935 {
3936   gfc_array_ref ar;
3937   gfc_ref *tmp;
3938   gfc_actual_arglist *args;
3939   int i;
3940
3941   switch (expr->expr_type)
3942     {
3943     case EXPR_VARIABLE:
3944       gcc_assert (expr->symtree->n.sym);
3945
3946       /* A scalar assignment  */
3947       if (!expr->ref)
3948         {
3949           if (expr->symtree->n.sym == symbol)
3950             return SUCCESS;
3951           else
3952             return FAILURE;
3953         }
3954
3955       /* the expr is array ref, substring or struct component.  */
3956       tmp = expr->ref;
3957       while (tmp != NULL)
3958         {
3959           switch (tmp->type)
3960             {
3961             case  REF_ARRAY:
3962               /* Check if the symbol appears in the array subscript.  */
3963               ar = tmp->u.ar;
3964               for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3965                 {
3966                   if (ar.start[i])
3967                     if (gfc_find_forall_index (ar.start[i], symbol) == SUCCESS)
3968                       return SUCCESS;
3969
3970                   if (ar.end[i])
3971                     if (gfc_find_forall_index (ar.end[i], symbol) == SUCCESS)
3972                       return SUCCESS;
3973
3974                   if (ar.stride[i])
3975                     if (gfc_find_forall_index (ar.stride[i], symbol) == SUCCESS)
3976                       return SUCCESS;
3977                 }  /* end for  */
3978               break;
3979
3980             case REF_SUBSTRING:
3981               if (expr->symtree->n.sym == symbol)
3982                 return SUCCESS;
3983               tmp = expr->ref;
3984               /* Check if the symbol appears in the substring section.  */
3985               if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
3986                 return SUCCESS;
3987               if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
3988                 return SUCCESS;
3989               break;
3990
3991             case REF_COMPONENT:
3992               break;
3993
3994             default:
3995               gfc_error("expression reference type error at %L", &expr->where);
3996             }
3997           tmp = tmp->next;
3998         }
3999       break;
4000
4001     /* If the expression is a function call, then check if the symbol
4002        appears in the actual arglist of the function.  */
4003     case EXPR_FUNCTION:
4004       for (args = expr->value.function.actual; args; args = args->next)
4005         {
4006           if (gfc_find_forall_index(args->expr,symbol) == SUCCESS)
4007             return SUCCESS;
4008         }
4009       break;
4010
4011     /* It seems not to happen.  */
4012     case EXPR_SUBSTRING:
4013       if (expr->ref)
4014         {
4015           tmp = expr->ref;
4016           gcc_assert (expr->ref->type == REF_SUBSTRING);
4017           if (gfc_find_forall_index (tmp->u.ss.start, symbol) == SUCCESS)
4018             return SUCCESS;
4019           if (gfc_find_forall_index (tmp->u.ss.end, symbol) == SUCCESS)
4020             return SUCCESS;
4021         }
4022       break;
4023
4024     /* It seems not to happen.  */
4025     case EXPR_STRUCTURE:
4026     case EXPR_ARRAY:
4027       gfc_error ("Unsupported statement while finding forall index in "
4028                  "expression");
4029       break;
4030
4031     case EXPR_OP:
4032       /* Find the FORALL index in the first operand.  */
4033       if (expr->value.op.op1)
4034         {
4035           if (gfc_find_forall_index (expr->value.op.op1, symbol) == SUCCESS)
4036             return SUCCESS;
4037         }
4038
4039       /* Find the FORALL index in the second operand.  */
4040       if (expr->value.op.op2)
4041         {
4042           if (gfc_find_forall_index (expr->value.op.op2, symbol) == SUCCESS)
4043             return SUCCESS;
4044         }
4045       break;
4046
4047     default:
4048       break;
4049     }
4050
4051   return FAILURE;
4052 }
4053
4054
4055 /* Resolve assignment in FORALL construct.
4056    NVAR is the number of FORALL index variables, and VAR_EXPR records the
4057    FORALL index variables.  */
4058
4059 static void
4060 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
4061 {
4062   int n;
4063
4064   for (n = 0; n < nvar; n++)
4065     {
4066       gfc_symbol *forall_index;
4067
4068       forall_index = var_expr[n]->symtree->n.sym;
4069
4070       /* Check whether the assignment target is one of the FORALL index
4071          variable.  */
4072       if ((code->expr->expr_type == EXPR_VARIABLE)
4073           && (code->expr->symtree->n.sym == forall_index))
4074         gfc_error ("Assignment to a FORALL index variable at %L",
4075                    &code->expr->where);
4076       else
4077         {
4078           /* If one of the FORALL index variables doesn't appear in the
4079              assignment target, then there will be a many-to-one
4080              assignment.  */
4081           if (gfc_find_forall_index (code->expr, forall_index) == FAILURE)
4082             gfc_error ("The FORALL with index '%s' cause more than one "
4083                        "assignment to this object at %L",
4084                        var_expr[n]->symtree->name, &code->expr->where);
4085         }
4086     }
4087 }
4088
4089
4090 /* Resolve WHERE statement in FORALL construct.  */
4091
4092 static void
4093 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr){
4094   gfc_code *cblock;
4095   gfc_code *cnext;
4096
4097   cblock = code->block;
4098   while (cblock)
4099     {
4100       /* the assignment statement of a WHERE statement, or the first
4101          statement in where-body-construct of a WHERE construct */
4102       cnext = cblock->next;
4103       while (cnext)
4104         {
4105           switch (cnext->op)
4106             {
4107             /* WHERE assignment statement */
4108             case EXEC_ASSIGN:
4109               gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
4110               break;
4111
4112             /* WHERE or WHERE construct is part of a where-body-construct */
4113             case EXEC_WHERE:
4114               gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
4115               break;
4116
4117             default:
4118               gfc_error ("Unsupported statement inside WHERE at %L",
4119                          &cnext->loc);
4120             }
4121           /* the next statement within the same where-body-construct */
4122           cnext = cnext->next;
4123         }
4124       /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
4125       cblock = cblock->block;
4126     }
4127 }
4128
4129
4130 /* Traverse the FORALL body to check whether the following errors exist:
4131    1. For assignment, check if a many-to-one assignment happens.
4132    2. For WHERE statement, check the WHERE body to see if there is any
4133       many-to-one assignment.  */
4134
4135 static void
4136 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
4137 {
4138   gfc_code *c;
4139
4140   c = code->block->next;
4141   while (c)
4142     {
4143       switch (c->op)
4144         {
4145         case EXEC_ASSIGN:
4146         case EXEC_POINTER_ASSIGN:
4147           gfc_resolve_assign_in_forall (c, nvar, var_expr);
4148           break;
4149
4150         /* Because the gfc_resolve_blocks() will handle the nested FORALL,
4151            there is no need to handle it here.  */
4152         case EXEC_FORALL:
4153           break;
4154         case EXEC_WHERE:
4155           gfc_resolve_where_code_in_forall(c, nvar, var_expr);
4156           break;
4157         default:
4158           break;
4159         }
4160       /* The next statement in the FORALL body.  */
4161       c = c->next;
4162     }
4163 }
4164
4165
4166 /* Given a FORALL construct, first resolve the FORALL iterator, then call
4167    gfc_resolve_forall_body to resolve the FORALL body.  */
4168
4169 static void
4170 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
4171 {
4172   static gfc_expr **var_expr;
4173   static int total_var = 0;
4174   static int nvar = 0;
4175   gfc_forall_iterator *fa;
4176   gfc_symbol *forall_index;
4177   gfc_code *next;
4178   int i;
4179
4180   /* Start to resolve a FORALL construct   */
4181   if (forall_save == 0)
4182     {
4183       /* Count the total number of FORALL index in the nested FORALL
4184          construct in order to allocate the VAR_EXPR with proper size.  */
4185       next = code;
4186       while ((next != NULL) && (next->op == EXEC_FORALL))
4187         {
4188           for (fa = next->ext.forall_iterator; fa; fa = fa->next)
4189             total_var ++;
4190           next = next->block->next;
4191         }
4192
4193       /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements.  */
4194       var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
4195     }
4196
4197   /* The information about FORALL iterator, including FORALL index start, end
4198      and stride. The FORALL index can not appear in start, end or stride.  */
4199   for (fa = code->ext.forall_iterator; fa; fa = fa->next)
4200     {
4201       /* Check if any outer FORALL index name is the same as the current
4202          one.  */
4203       for (i = 0; i < nvar; i++)
4204         {
4205           if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
4206             {
4207               gfc_error ("An outer FORALL construct already has an index "
4208                          "with this name %L", &fa->var->where);
4209             }
4210         }
4211
4212       /* Record the current FORALL index.  */
4213       var_expr[nvar] = gfc_copy_expr (fa->var);
4214
4215       forall_index = fa->var->symtree->n.sym;
4216
4217       /* Check if the FORALL index appears in start, end or stride.  */
4218       if (gfc_find_forall_index (fa->start, forall_index) == SUCCESS)
4219         gfc_error ("A FORALL index must not appear in a limit or stride "
4220                    "expression in the same FORALL at %L", &fa->start->where);
4221       if (gfc_find_forall_index (fa->end, forall_index) == SUCCESS)
4222         gfc_error ("A FORALL index must not appear in a limit or stride "
4223                    "expression in the same FORALL at %L", &fa->end->where);
4224       if (gfc_find_forall_index (fa->stride, forall_index) == SUCCESS)
4225         gfc_error ("A FORALL index must not appear in a limit or stride "
4226                    "expression in the same FORALL at %L", &fa->stride->where);
4227       nvar++;
4228     }
4229
4230   /* Resolve the FORALL body.  */
4231   gfc_resolve_forall_body (code, nvar, var_expr);
4232
4233   /* May call gfc_resolve_forall to resolve the inner FORALL loop.  */
4234   gfc_resolve_blocks (code->block, ns);
4235
4236   /* Free VAR_EXPR after the whole FORALL construct resolved.  */
4237   for (i = 0; i < total_var; i++)
4238     gfc_free_expr (var_expr[i]);
4239
4240   /* Reset the counters.  */
4241   total_var = 0;
4242   nvar = 0;
4243 }
4244
4245
4246 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL ,GOTO and
4247    DO code nodes.  */
4248
4249 static void resolve_code (gfc_code *, gfc_namespace *);
4250
4251 void
4252 gfc_resolve_blocks (gfc_code * b, gfc_namespace * ns)
4253 {
4254   try t;
4255
4256   for (; b; b = b->block)
4257     {
4258       t = gfc_resolve_expr (b->expr);
4259       if (gfc_resolve_expr (b->expr2) == FAILURE)
4260         t = FAILURE;
4261
4262       switch (b->op)
4263         {
4264         case EXEC_IF:
4265           if (t == SUCCESS && b->expr != NULL
4266               && (b->expr->ts.type != BT_LOGICAL || b->expr->rank != 0))
4267             gfc_error
4268               ("ELSE IF clause at %L requires a scalar LOGICAL expression",
4269                &b->expr->where);
4270           break;
4271
4272         case EXEC_WHERE:
4273           if (t == SUCCESS
4274               && b->expr != NULL
4275               && (b->expr->ts.type != BT_LOGICAL
4276                   || b->expr->rank == 0))
4277             gfc_error
4278               ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
4279                &b->expr->where);
4280           break;
4281
4282         case EXEC_GOTO:
4283           resolve_branch (b->label, b);
4284           break;
4285
4286         case EXEC_SELECT:
4287         case EXEC_FORALL:
4288         case EXEC_DO:
4289         case EXEC_DO_WHILE:
4290         case EXEC_READ:
4291         case EXEC_WRITE:
4292         case EXEC_IOLENGTH:
4293           break;
4294
4295         case EXEC_OMP_ATOMIC:
4296         case EXEC_OMP_CRITICAL:
4297         case EXEC_OMP_DO:
4298         case EXEC_OMP_MASTER:
4299         case EXEC_OMP_ORDERED:
4300         case EXEC_OMP_PARALLEL:
4301         case EXEC_OMP_PARALLEL_DO:
4302         case EXEC_OMP_PARALLEL_SECTIONS:
4303         case EXEC_OMP_PARALLEL_WORKSHARE:
4304         case EXEC_OMP_SECTIONS:
4305         case EXEC_OMP_SINGLE:
4306         case EXEC_OMP_WORKSHARE:
4307           break;
4308
4309         default:
4310           gfc_internal_error ("resolve_block(): Bad block type");
4311         }
4312
4313       resolve_code (b->next, ns);
4314     }
4315 }
4316
4317
4318 /* Given a block of code, recursively resolve everything pointed to by this
4319    code block.  */
4320
4321 static void
4322 resolve_code (gfc_code * code, gfc_namespace * ns)
4323 {
4324   int omp_workshare_save;
4325   code_stack frame;
4326   gfc_alloc *a;
4327   try t;
4328
4329   frame.prev = cs_base;
4330   frame.head = code;
4331   cs_base = &frame;
4332
4333   for (; code; code = code->next)
4334     {
4335       frame.current = code;
4336
4337       if (code->op == EXEC_FORALL)
4338         {
4339           int forall_save = forall_flag;
4340
4341           forall_flag = 1;
4342           gfc_resolve_forall (code, ns, forall_save);
4343           forall_flag = forall_save;
4344         }
4345       else if (code->block)
4346         {
4347           omp_workshare_save = -1;
4348           switch (code->op)
4349             {
4350             case EXEC_OMP_PARALLEL_WORKSHARE:
4351               omp_workshare_save = omp_workshare_flag;
4352               omp_workshare_flag = 1;
4353               gfc_resolve_omp_parallel_blocks (code, ns);
4354               break;
4355             case EXEC_OMP_PARALLEL:
4356             case EXEC_OMP_PARALLEL_DO:
4357             case EXEC_OMP_PARALLEL_SECTIONS:
4358               omp_workshare_save = omp_workshare_flag;
4359               omp_workshare_flag = 0;
4360               gfc_resolve_omp_parallel_blocks (code, ns);
4361               break;
4362             case EXEC_OMP_DO:
4363               gfc_resolve_omp_do_blocks (code, ns);
4364               break;
4365             case EXEC_OMP_WORKSHARE:
4366               omp_workshare_save = omp_workshare_flag;
4367               omp_workshare_flag = 1;
4368               /* FALLTHROUGH */
4369             default:
4370               gfc_resolve_blocks (code->block, ns);
4371               break;
4372             }
4373
4374           if (omp_workshare_save != -1)
4375             omp_workshare_flag = omp_workshare_save;
4376         }
4377
4378       t = gfc_resolve_expr (code->expr);
4379       if (gfc_resolve_expr (code->expr2) == FAILURE)
4380         t = FAILURE;
4381
4382       switch (code->op)
4383         {
4384         case EXEC_NOP:
4385         case EXEC_CYCLE:
4386         case EXEC_PAUSE:
4387         case EXEC_STOP:
4388         case EXEC_EXIT:
4389         case EXEC_CONTINUE:
4390         case EXEC_DT_END:
4391         case EXEC_ENTRY:
4392           break;
4393
4394         case EXEC_WHERE:
4395           resolve_where (code, NULL);
4396           break;
4397
4398         case EXEC_GOTO:
4399           if (code->expr != NULL)
4400             {
4401               if (code->expr->ts.type != BT_INTEGER)
4402                 gfc_error ("ASSIGNED GOTO statement at %L requires an INTEGER "
4403                        "variable", &code->expr->where);
4404               else if (code->expr->symtree->n.sym->attr.assign != 1)
4405                 gfc_error ("Variable '%s' has not been assigned a target label "
4406                         "at %L", code->expr->symtree->n.sym->name,
4407                         &code->expr->where);
4408             }
4409           else
4410             resolve_branch (code->label, code);
4411           break;
4412
4413         case EXEC_RETURN:
4414           if (code->expr != NULL
4415                 && (code->expr->ts.type != BT_INTEGER || code->expr->rank))
4416             gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
4417                        "INTEGER return specifier", &code->expr->where);
4418           break;
4419
4420         case EXEC_ASSIGN:
4421           if (t == FAILURE)
4422             break;
4423
4424           if (gfc_extend_assign (code, ns) == SUCCESS)
4425             {
4426               if (gfc_pure (NULL) && !gfc_pure (code->symtree->n.sym))
4427                 {
4428                   gfc_error ("Subroutine '%s' called instead of assignment at "
4429                              "%L must be PURE", code->symtree->n.sym->name,
4430                              &code->loc);
4431                   break;
4432                 }
4433               goto call;
4434             }
4435
4436           if (gfc_pure (NULL))
4437             {
4438               if (gfc_impure_variable (code->expr->symtree->n.sym))
4439                 {
4440                   gfc_error
4441                     ("Cannot assign to variable '%s' in PURE procedure at %L",
4442                      code->expr->symtree->n.sym->name, &code->expr->where);
4443                   break;
4444                 }
4445
4446               if (code->expr2->ts.type == BT_DERIVED
4447                   && derived_pointer (code->expr2->ts.derived))
4448                 {
4449                   gfc_error
4450                     ("Right side of assignment at %L is a derived type "
4451                      "containing a POINTER in a PURE procedure",
4452                      &code->expr2->where);
4453                   break;
4454                 }
4455             }
4456
4457           gfc_check_assign (code->expr, code->expr2, 1);
4458           break;
4459
4460         case EXEC_LABEL_ASSIGN:
4461           if (code->label->defined == ST_LABEL_UNKNOWN)
4462             gfc_error ("Label %d referenced at %L is never defined",
4463                        code->label->value, &code->label->where);
4464           if (t == SUCCESS
4465               && (code->expr->expr_type != EXPR_VARIABLE
4466                   || code->expr->symtree->n.sym->ts.type != BT_INTEGER
4467                   || code->expr->symtree->n.sym->ts.kind 
4468                         != gfc_default_integer_kind
4469                   || code->expr->symtree->n.sym->as != NULL))
4470             gfc_error ("ASSIGN statement at %L requires a scalar "
4471                        "default INTEGER variable", &code->expr->where);
4472           break;
4473
4474         case EXEC_POINTER_ASSIGN:
4475           if (t == FAILURE)
4476             break;
4477
4478           gfc_check_pointer_assign (code->expr, code->expr2);
4479           break;
4480
4481         case EXEC_ARITHMETIC_IF:
4482           if (t == SUCCESS
4483               && code->expr->ts.type != BT_INTEGER
4484               && code->expr->ts.type != BT_REAL)
4485             gfc_error ("Arithmetic IF statement at %L requires a numeric "
4486                        "expression", &code->expr->where);
4487
4488           resolve_branch (code->label, code);
4489           resolve_branch (code->label2, code);
4490           resolve_branch (code->label3, code);
4491           break;
4492
4493         case EXEC_IF:
4494           if (t == SUCCESS && code->expr != NULL
4495               && (code->expr->ts.type != BT_LOGICAL
4496                   || code->expr->rank != 0))
4497             gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
4498                        &code->expr->where);
4499           break;
4500
4501         case EXEC_CALL:
4502         call:
4503           resolve_call (code);
4504           break;
4505
4506         case EXEC_SELECT:
4507           /* Select is complicated. Also, a SELECT construct could be
4508              a transformed computed GOTO.  */
4509           resolve_select (code);
4510           break;
4511
4512         case EXEC_DO:
4513           if (code->ext.iterator != NULL)
4514             {
4515               gfc_iterator *iter = code->ext.iterator;
4516               if (gfc_resolve_iterator (iter, true) != FAILURE)
4517                 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
4518             }
4519           break;
4520
4521         case EXEC_DO_WHILE:
4522           if (code->expr == NULL)
4523             gfc_internal_error ("resolve_code(): No expression on DO WHILE");
4524           if (t == SUCCESS
4525               && (code->expr->rank != 0
4526                   || code->expr->ts.type != BT_LOGICAL))
4527             gfc_error ("Exit condition of DO WHILE loop at %L must be "
4528                        "a scalar LOGICAL expression", &code->expr->where);
4529           break;
4530
4531         case EXEC_ALLOCATE:
4532           if (t == SUCCESS && code->expr != NULL
4533               && code->expr->ts.type != BT_INTEGER)
4534             gfc_error ("STAT tag in ALLOCATE statement at %L must be "
4535                        "of type INTEGER", &code->expr->where);
4536
4537           for (a = code->ext.alloc_list; a; a = a->next)
4538             resolve_allocate_expr (a->expr, code);
4539
4540           break;
4541
4542         case EXEC_DEALLOCATE:
4543           if (t == SUCCESS && code->expr != NULL
4544               && code->expr->ts.type != BT_INTEGER)
4545             gfc_error
4546               ("STAT tag in DEALLOCATE statement at %L must be of type "
4547                "INTEGER", &code->expr->where);
4548
4549           for (a = code->ext.alloc_list; a; a = a->next)
4550             resolve_deallocate_expr (a->expr);
4551
4552           break;
4553
4554         case EXEC_OPEN:
4555           if (gfc_resolve_open (code->ext.open) == FAILURE)
4556             break;
4557
4558           resolve_branch (code->ext.open->err, code);
4559           break;
4560
4561         case EXEC_CLOSE:
4562           if (gfc_resolve_close (code->ext.close) == FAILURE)
4563             break;
4564
4565           resolve_branch (code->ext.close->err, code);
4566           break;
4567
4568         case EXEC_BACKSPACE:
4569         case EXEC_ENDFILE:
4570         case EXEC_REWIND:
4571         case EXEC_FLUSH:
4572           if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
4573             break;
4574
4575           resolve_branch (code->ext.filepos->err, code);
4576           break;
4577
4578         case EXEC_INQUIRE:
4579           if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4580               break;
4581
4582           resolve_branch (code->ext.inquire->err, code);
4583           break;
4584
4585         case EXEC_IOLENGTH:
4586           gcc_assert (code->ext.inquire != NULL);
4587           if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
4588             break;
4589
4590           resolve_branch (code->ext.inquire->err, code);
4591           break;
4592
4593         case EXEC_READ:
4594         case EXEC_WRITE:
4595           if (gfc_resolve_dt (code->ext.dt) == FAILURE)
4596             break;
4597
4598           resolve_branch (code->ext.dt->err, code);
4599           resolve_branch (code->ext.dt->end, code);
4600           resolve_branch (code->ext.dt->eor, code);
4601           break;
4602
4603         case EXEC_TRANSFER:
4604           resolve_transfer (code);
4605           break;
4606
4607         case EXEC_FORALL:
4608           resolve_forall_iterators (code->ext.forall_iterator);
4609
4610           if (code->expr != NULL && code->expr->ts.type != BT_LOGICAL)
4611             gfc_error
4612               ("FORALL mask clause at %L requires a LOGICAL expression",
4613                &code->expr->where);
4614           break;
4615
4616         case EXEC_OMP_ATOMIC:
4617         case EXEC_OMP_BARRIER:
4618         case EXEC_OMP_CRITICAL:
4619         case EXEC_OMP_FLUSH:
4620         case EXEC_OMP_DO:
4621         case EXEC_OMP_MASTER:
4622         case EXEC_OMP_ORDERED:
4623         case EXEC_OMP_SECTIONS:
4624         case EXEC_OMP_SINGLE:
4625         case EXEC_OMP_WORKSHARE:
4626           gfc_resolve_omp_directive (code, ns);
4627           break;
4628
4629         case EXEC_OMP_PARALLEL:
4630         case EXEC_OMP_PARALLEL_DO:
4631         case EXEC_OMP_PARALLEL_SECTIONS:
4632         case EXEC_OMP_PARALLEL_WORKSHARE:
4633           omp_workshare_save = omp_workshare_flag;
4634           omp_workshare_flag = 0;
4635           gfc_resolve_omp_directive (code, ns);
4636           omp_workshare_flag = omp_workshare_save;
4637           break;
4638
4639         default:
4640           gfc_internal_error ("resolve_code(): Bad statement code");
4641         }
4642     }
4643
4644   cs_base = frame.prev;
4645 }
4646
4647
4648 /* Resolve initial values and make sure they are compatible with
4649    the variable.  */
4650
4651 static void
4652 resolve_values (gfc_symbol * sym)
4653 {
4654
4655   if (sym->value == NULL)
4656     return;
4657
4658   if (gfc_resolve_expr (sym->value) == FAILURE)
4659     return;
4660
4661   gfc_check_assign_symbol (sym, sym->value);
4662 }
4663
4664
4665 /* Resolve an index expression.  */
4666
4667 static try
4668 resolve_index_expr (gfc_expr * e)
4669 {
4670
4671   if (gfc_resolve_expr (e) == FAILURE)
4672     return FAILURE;
4673
4674   if (gfc_simplify_expr (e, 0) == FAILURE)
4675     return FAILURE;
4676
4677   if (gfc_specification_expr (e) == FAILURE)
4678     return FAILURE;
4679
4680   return SUCCESS;
4681 }
4682
4683 /* Resolve a charlen structure.  */
4684
4685 static try
4686 resolve_charlen (gfc_charlen *cl)
4687 {
4688   if (cl->resolved)
4689     return SUCCESS;
4690
4691   cl->resolved = 1;
4692
4693   if (resolve_index_expr (cl->length) == FAILURE)
4694     return FAILURE;
4695
4696   return SUCCESS;
4697 }
4698
4699
4700 /* Test for non-constant shape arrays. */
4701
4702 static bool
4703 is_non_constant_shape_array (gfc_symbol *sym)
4704 {
4705   gfc_expr *e;
4706   int i;
4707
4708   if (sym->as != NULL)
4709     {
4710       /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
4711          has not been simplified; parameter array references.  Do the
4712          simplification now.  */
4713       for (i = 0; i < sym->as->rank; i++)
4714         {
4715           e = sym->as->lower[i];
4716           if (e && (resolve_index_expr (e) == FAILURE
4717                 || !gfc_is_constant_expr (e)))
4718             return true;
4719
4720           e = sym->as->upper[i];
4721           if (e && (resolve_index_expr (e) == FAILURE
4722                 || !gfc_is_constant_expr (e)))
4723             return true;
4724         }
4725     }
4726   return false;
4727 }
4728
4729 /* Resolution of common features of flavors variable and procedure. */
4730
4731 static try
4732 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
4733 {
4734   /* Constraints on deferred shape variable.  */
4735   if (sym->as == NULL || sym->as->type != AS_DEFERRED)
4736     {
4737       if (sym->attr.allocatable)
4738         {
4739           if (sym->attr.dimension)
4740             gfc_error ("Allocatable array '%s' at %L must have "
4741                        "a deferred shape", sym->name, &sym->declared_at);
4742           else
4743             gfc_error ("Scalar object '%s' at %L may not be ALLOCATABLE",
4744                        sym->name, &sym->declared_at);
4745             return FAILURE;
4746         }
4747
4748       if (sym->attr.pointer && sym->attr.dimension)
4749         {
4750           gfc_error ("Array pointer '%s' at %L must have a deferred shape",
4751                      sym->name, &sym->declared_at);
4752           return FAILURE;
4753         }
4754
4755     }
4756   else
4757     {
4758       if (!mp_flag && !sym->attr.allocatable
4759              && !sym->attr.pointer && !sym->attr.dummy)
4760         {
4761           gfc_error ("Array '%s' at %L cannot have a deferred shape",
4762                      sym->name, &sym->declared_at);
4763           return FAILURE;
4764          }
4765     }
4766   return SUCCESS;
4767 }
4768
4769 /* Resolve symbols with flavor variable.  */
4770
4771 static try
4772 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
4773 {
4774   int flag;
4775   int i;
4776   gfc_expr *e;
4777   gfc_expr *constructor_expr;
4778
4779   if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
4780     return FAILURE;
4781
4782   /* The shape of a main program or module array needs to be constant.  */
4783   if (sym->ns->proc_name
4784         && (sym->ns->proc_name->attr.flavor == FL_MODULE
4785              || sym->ns->proc_name->attr.is_main_program)
4786         && !sym->attr.use_assoc
4787         && !sym->attr.allocatable
4788         && !sym->attr.pointer
4789         && is_non_constant_shape_array (sym))
4790     {
4791        gfc_error ("The module or main program array '%s' at %L must "
4792                      "have constant shape", sym->name, &sym->declared_at);
4793           return FAILURE;
4794     }
4795
4796   if (sym->ts.type == BT_CHARACTER)
4797     {
4798       /* Make sure that character string variables with assumed length are
4799          dummy arguments.  */
4800       e = sym->ts.cl->length;
4801       if (e == NULL && !sym->attr.dummy && !sym->attr.result)
4802         {
4803           gfc_error ("Entity with assumed character length at %L must be a "
4804                      "dummy argument or a PARAMETER", &sym->declared_at);
4805           return FAILURE;
4806         }
4807
4808       if (!gfc_is_constant_expr (e)
4809             && !(e->expr_type == EXPR_VARIABLE
4810             && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
4811             && sym->ns->proc_name
4812             && (sym->ns->proc_name->attr.flavor == FL_MODULE
4813                   || sym->ns->proc_name->attr.is_main_program)
4814             && !sym->attr.use_assoc)
4815         {
4816           gfc_error ("'%s' at %L must have constant character length "
4817                      "in this context", sym->name, &sym->declared_at);
4818           return FAILURE;
4819         }
4820     }
4821
4822   /* Can the symbol have an initializer?  */
4823   flag = 0;
4824   if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
4825         || sym->attr.intrinsic || sym->attr.result)
4826     flag = 1;
4827   else if (sym->attr.dimension && !sym->attr.pointer)
4828     {
4829       /* Don't allow initialization of automatic arrays.  */
4830       for (i = 0; i < sym->as->rank; i++)
4831         {
4832           if (sym->as->lower[i] == NULL
4833                 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
4834                 || sym->as->upper[i] == NULL
4835                 || sym->as->upper[i]->expr_type != EXPR_CONSTANT)
4836             {
4837               flag = 1;
4838               break;
4839             }
4840         }
4841   }
4842
4843   /* Reject illegal initializers.  */
4844   if (sym->value && flag)
4845     {
4846       if (sym->attr.allocatable)
4847         gfc_error ("Allocatable '%s' at %L cannot have an initializer",
4848                    sym->name, &sym->declared_at);
4849       else if (sym->attr.external)
4850         gfc_error ("External '%s' at %L cannot have an initializer",
4851                    sym->name, &sym->declared_at);
4852       else if (sym->attr.dummy)
4853         gfc_error ("Dummy '%s' at %L cannot have an initializer",
4854                    sym->name, &sym->declared_at);
4855       else if (sym->attr.intrinsic)
4856         gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
4857                    sym->name, &sym->declared_at);
4858       else if (sym->attr.result)
4859         gfc_error ("Function result '%s' at %L cannot have an initializer",
4860                    sym->name, &sym->declared_at);
4861       else
4862         gfc_error ("Automatic array '%s' at %L cannot have an initializer",
4863                    sym->name, &sym->declared_at);
4864       return FAILURE;
4865     }
4866
4867   /* 4th constraint in section 11.3:  "If an object of a type for which
4868      component-initialization is specified (R429) appears in the
4869      specification-part of a module and does not have the ALLOCATABLE
4870      or POINTER attribute, the object shall have the SAVE attribute."  */
4871
4872   constructor_expr = NULL;
4873   if (sym->ts.type == BT_DERIVED && !(sym->value || flag))
4874         constructor_expr = gfc_default_initializer (&sym->ts);
4875
4876   if (sym->ns->proc_name
4877         && sym->ns->proc_name->attr.flavor == FL_MODULE
4878         && constructor_expr
4879         && !sym->ns->save_all && !sym->attr.save
4880         && !sym->attr.pointer && !sym->attr.allocatable)
4881     {
4882       gfc_error("Object '%s' at %L must have the SAVE attribute %s",
4883                 sym->name, &sym->declared_at,
4884                 "for default initialization of a component");
4885       return FAILURE;
4886     }
4887
4888   /* Assign default initializer.  */
4889   if (sym->ts.type == BT_DERIVED && !(sym->value || flag)
4890         && !sym->attr.pointer)
4891     sym->value = gfc_default_initializer (&sym->ts);
4892
4893   return SUCCESS;
4894 }
4895
4896
4897 /* Resolve a procedure.  */
4898
4899 static try
4900 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
4901 {
4902   gfc_formal_arglist *arg;
4903
4904   if (sym->attr.function
4905         && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
4906     return FAILURE;
4907
4908   if (sym->attr.proc == PROC_ST_FUNCTION)
4909     {
4910       if (sym->ts.type == BT_CHARACTER)
4911         {
4912           gfc_charlen *cl = sym->ts.cl;
4913           if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
4914             {
4915               gfc_error ("Character-valued statement function '%s' at %L must "
4916                          "have constant length", sym->name, &sym->declared_at);
4917               return FAILURE;
4918             }
4919         }
4920     }
4921
4922   /* Ensure that derived type for are not of a private type.  Internal
4923      module procedures are excluded by 2.2.3.3 - ie. they are not
4924      externally accessible and can access all the objects accessible in
4925      the host. */
4926   if (!(sym->ns->parent
4927             && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
4928         && gfc_check_access(sym->attr.access, sym->ns->default_access))
4929     {
4930       for (arg = sym->formal; arg; arg = arg->next)
4931         {
4932           if (arg->sym
4933                 && arg->sym->ts.type == BT_DERIVED
4934                 && !arg->sym->ts.derived->attr.use_assoc
4935                 && !gfc_check_access(arg->sym->ts.derived->attr.access,
4936                         arg->sym->ts.derived->ns->default_access))
4937             {
4938               gfc_error_now ("'%s' is of a PRIVATE type and cannot be "
4939                              "a dummy argument of '%s', which is "
4940                              "PUBLIC at %L", arg->sym->name, sym->name,
4941                              &sym->declared_at);
4942               /* Stop this message from recurring.  */
4943               arg->sym->ts.derived->attr.access = ACCESS_PUBLIC;
4944               return FAILURE;
4945             }
4946         }
4947     }
4948
4949   /* An external symbol may not have an initializer because it is taken to be
4950      a procedure.  */
4951   if (sym->attr.external && sym->value)
4952     {
4953       gfc_error ("External object '%s' at %L may not have an initializer",
4954                  sym->name, &sym->declared_at);
4955       return FAILURE;
4956     }
4957
4958   /* 5.1.1.5 of the Standard: A function name declared with an asterisk
4959      char-len-param shall not be array-valued, pointer-valued, recursive
4960      or pure.  ....snip... A character value of * may only be used in the
4961      following ways: (i) Dummy arg of procedure - dummy associates with
4962      actual length; (ii) To declare a named constant; or (iii) External
4963      function - but length must be declared in calling scoping unit.  */
4964   if (sym->attr.function
4965         && sym->ts.type == BT_CHARACTER
4966         && sym->ts.cl && sym->ts.cl->length == NULL)
4967     {
4968       if ((sym->as && sym->as->rank) || (sym->attr.pointer)
4969              || (sym->attr.recursive) || (sym->attr.pure))
4970         {
4971           if (sym->as && sym->as->rank)
4972             gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4973                        "array-valued", sym->name, &sym->declared_at);
4974
4975           if (sym->attr.pointer)
4976             gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4977                        "pointer-valued", sym->name, &sym->declared_at);
4978
4979           if (sym->attr.pure)
4980             gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4981                        "pure", sym->name, &sym->declared_at);
4982
4983           if (sym->attr.recursive)
4984             gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
4985                        "recursive", sym->name, &sym->declared_at);
4986
4987           return FAILURE;
4988         }
4989
4990       /* Appendix B.2 of the standard.  Contained functions give an
4991          error anyway.  Fixed-form is likely to be F77/legacy.  */
4992       if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
4993         gfc_notify_std (GFC_STD_F95_OBS, "CHARACTER(*) function "
4994                         "'%s' at %L is obsolescent in fortran 95",
4995                         sym->name, &sym->declared_at);
4996     }
4997   return SUCCESS;
4998 }
4999
5000
5001 /* Resolve the components of a derived type.  */
5002
5003 static try
5004 resolve_fl_derived (gfc_symbol *sym)
5005 {
5006   gfc_component *c;
5007   gfc_dt_list * dt_list;
5008   int i;
5009
5010   for (c = sym->components; c != NULL; c = c->next)
5011     {
5012       if (c->ts.type == BT_CHARACTER)
5013         {
5014          if (c->ts.cl->length == NULL
5015              || (resolve_charlen (c->ts.cl) == FAILURE)
5016              || !gfc_is_constant_expr (c->ts.cl->length))
5017            {
5018              gfc_error ("Character length of component '%s' needs to "
5019                         "be a constant specification expression at %L.",
5020                         c->name,
5021                         c->ts.cl->length ? &c->ts.cl->length->where : &c->loc);
5022              return FAILURE;
5023            }
5024         }
5025
5026       if (c->ts.type == BT_DERIVED
5027             && sym->component_access != ACCESS_PRIVATE
5028             && gfc_check_access(sym->attr.access, sym->ns->default_access)
5029             && !c->ts.derived->attr.use_assoc
5030             && !gfc_check_access(c->ts.derived->attr.access,
5031                                  c->ts.derived->ns->default_access))
5032         {
5033           gfc_error ("The component '%s' is a PRIVATE type and cannot be "
5034                      "a component of '%s', which is PUBLIC at %L",
5035                       c->name, sym->name, &sym->declared_at);
5036           return FAILURE;
5037         }
5038
5039       if (c->pointer || c->as == NULL)
5040         continue;
5041
5042       for (i = 0; i < c->as->rank; i++)
5043         {
5044           if (c->as->lower[i] == NULL
5045                 || !gfc_is_constant_expr (c->as->lower[i])
5046                 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
5047                 || c->as->upper[i] == NULL
5048                 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
5049                 || !gfc_is_constant_expr (c->as->upper[i]))
5050             {
5051               gfc_error ("Component '%s' of '%s' at %L must have "
5052                          "constant array bounds.",
5053                          c->name, sym->name, &c->loc);
5054               return FAILURE;
5055             }
5056         }
5057     }
5058     
5059   /* Add derived type to the derived type list.  */
5060   dt_list = gfc_get_dt_list ();
5061   dt_list->next = sym->ns->derived_types;
5062   dt_list->derived = sym;
5063   sym->ns->derived_types = dt_list;
5064
5065   return SUCCESS;
5066 }
5067
5068
5069 static try
5070 resolve_fl_namelist (gfc_symbol *sym)
5071 {
5072   gfc_namelist *nl;
5073   gfc_symbol *nlsym;
5074
5075   /* Reject PRIVATE objects in a PUBLIC namelist.  */
5076   if (gfc_check_access(sym->attr.access, sym->ns->default_access))
5077     {
5078       for (nl = sym->namelist; nl; nl = nl->next)
5079         {
5080           if (!nl->sym->attr.use_assoc
5081                 && !(sym->ns->parent == nl->sym->ns)
5082                        && !gfc_check_access(nl->sym->attr.access,
5083                                             nl->sym->ns->default_access))
5084             {
5085               gfc_error ("PRIVATE symbol '%s' cannot be member of "
5086                          "PUBLIC namelist at %L", nl->sym->name,
5087                          &sym->declared_at);
5088               return FAILURE;
5089             }
5090         }
5091     }
5092
5093     /* Reject namelist arrays that are not constant shape.  */
5094     for (nl = sym->namelist; nl; nl = nl->next)
5095       {
5096         if (is_non_constant_shape_array (nl->sym))
5097           {
5098             gfc_error ("The array '%s' must have constant shape to be "
5099                        "a NAMELIST object at %L", nl->sym->name,
5100                        &sym->declared_at);
5101             return FAILURE;
5102           }
5103     }
5104
5105   /* 14.1.2 A module or internal procedure represent local entities
5106      of the same type as a namelist member and so are not allowed.
5107      Note that this is sometimes caught by check_conflict so the
5108      same message has been used.  */
5109   for (nl = sym->namelist; nl; nl = nl->next)
5110     {
5111       nlsym = NULL;
5112         if (sym->ns->parent && nl->sym && nl->sym->name)
5113           gfc_find_symbol (nl->sym->name, sym->ns->parent, 0, &nlsym);
5114         if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
5115           {
5116             gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
5117                        "attribute in '%s' at %L", nlsym->name,
5118                        &sym->declared_at);
5119             return FAILURE;
5120           }
5121     }
5122
5123   return SUCCESS;
5124 }
5125
5126
5127 static try
5128 resolve_fl_parameter (gfc_symbol *sym)
5129 {
5130   /* A parameter array's shape needs to be constant.  */
5131   if (sym->as != NULL && !gfc_is_compile_time_shape (sym->as))
5132     {
5133       gfc_error ("Parameter array '%s' at %L cannot be automatic "
5134                  "or assumed shape", sym->name, &sym->declared_at);
5135       return FAILURE;
5136     }
5137
5138   /* Make sure a parameter that has been implicitly typed still
5139      matches the implicit type, since PARAMETER statements can precede
5140      IMPLICIT statements.  */
5141   if (sym->attr.implicit_type
5142         && !gfc_compare_types (&sym->ts,
5143                                gfc_get_default_type (sym, sym->ns)))
5144     {
5145       gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
5146                  "later IMPLICIT type", sym->name, &sym->declared_at);
5147       return FAILURE;
5148     }
5149
5150   /* Make sure the types of derived parameters are consistent.  This
5151      type checking is deferred until resolution because the type may
5152      refer to a derived type from the host.  */
5153   if (sym->ts.type == BT_DERIVED
5154         && !gfc_compare_types (&sym->ts, &sym->value->ts))
5155     {
5156       gfc_error ("Incompatible derived type in PARAMETER at %L",
5157                  &sym->value->where);
5158       return FAILURE;
5159     }
5160   return SUCCESS;
5161 }
5162
5163
5164 /* Do anything necessary to resolve a symbol.  Right now, we just
5165    assume that an otherwise unknown symbol is a variable.  This sort
5166    of thing commonly happens for symbols in module.  */
5167
5168 static void
5169 resolve_symbol (gfc_symbol * sym)
5170 {
5171   /* Zero if we are checking a formal namespace.  */
5172   static int formal_ns_flag = 1;
5173   int formal_ns_save, check_constant, mp_flag;
5174   gfc_symtree *symtree;
5175   gfc_symtree *this_symtree;
5176   gfc_namespace *ns;
5177   gfc_component *c;
5178
5179   if (sym->attr.flavor == FL_UNKNOWN)
5180     {
5181
5182     /* If we find that a flavorless symbol is an interface in one of the
5183        parent namespaces, find its symtree in this namespace, free the
5184        symbol and set the symtree to point to the interface symbol.  */
5185       for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
5186         {
5187           symtree = gfc_find_symtree (ns->sym_root, sym->name);
5188           if (symtree && symtree->n.sym->generic)
5189             {
5190               this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
5191                                                sym->name);
5192               sym->refs--;
5193               if (!sym->refs)
5194                 gfc_free_symbol (sym);
5195               symtree->n.sym->refs++;
5196               this_symtree->n.sym = symtree->n.sym;
5197               return;
5198             }
5199         }
5200
5201       /* Otherwise give it a flavor according to such attributes as
5202          it has.  */
5203       if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
5204         sym->attr.flavor = FL_VARIABLE;
5205       else
5206         {
5207           sym->attr.flavor = FL_PROCEDURE;
5208           if (sym->attr.dimension)
5209             sym->attr.function = 1;
5210         }
5211     }
5212
5213   if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
5214     return;
5215
5216   /* Symbols that are module procedures with results (functions) have
5217      the types and array specification copied for type checking in
5218      procedures that call them, as well as for saving to a module
5219      file.  These symbols can't stand the scrutiny that their results
5220      can.  */
5221   mp_flag = (sym->result != NULL && sym->result != sym);
5222
5223   /* Assign default type to symbols that need one and don't have one.  */
5224   if (sym->ts.type == BT_UNKNOWN)
5225     {
5226       if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
5227         gfc_set_default_type (sym, 1, NULL);
5228
5229       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
5230         {
5231           /* The specific case of an external procedure should emit an error
5232              in the case that there is no implicit type.  */
5233           if (!mp_flag)
5234             gfc_set_default_type (sym, sym->attr.external, NULL);
5235           else
5236             {
5237               /* Result may be in another namespace.  */
5238               resolve_symbol (sym->result);
5239
5240               sym->ts = sym->result->ts;
5241               sym->as = gfc_copy_array_spec (sym->result->as);
5242               sym->attr.dimension = sym->result->attr.dimension;
5243               sym->attr.pointer = sym->result->attr.pointer;
5244               sym->attr.allocatable = sym->result->attr.allocatable;
5245             }
5246         }
5247     }
5248
5249   /* Assumed size arrays and assumed shape arrays must be dummy
5250      arguments.  */ 
5251
5252   if (sym->as != NULL
5253       && (sym->as->type == AS_ASSUMED_SIZE
5254           || sym->as->type == AS_ASSUMED_SHAPE)
5255       && sym->attr.dummy == 0)
5256     {
5257       if (sym->as->type == AS_ASSUMED_SIZE)
5258         gfc_error ("Assumed size array at %L must be a dummy argument",
5259                    &sym->declared_at);
5260       else
5261         gfc_error ("Assumed shape array at %L must be a dummy argument",
5262                    &sym->declared_at);
5263       return;
5264     }
5265
5266   /* Make sure symbols with known intent or optional are really dummy
5267      variable.  Because of ENTRY statement, this has to be deferred
5268      until resolution time.  */
5269
5270   if (!sym->attr.dummy
5271       && (sym->attr.optional
5272           || sym->attr.intent != INTENT_UNKNOWN))
5273     {
5274       gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
5275       return;
5276     }
5277
5278   /* If a derived type symbol has reached this point, without its
5279      type being declared, we have an error.  Notice that most
5280      conditions that produce undefined derived types have already
5281      been dealt with.  However, the likes of:
5282      implicit type(t) (t) ..... call foo (t) will get us here if
5283      the type is not declared in the scope of the implicit
5284      statement. Change the type to BT_UNKNOWN, both because it is so
5285      and to prevent an ICE.  */
5286   if (sym->ts.type == BT_DERIVED
5287         && sym->ts.derived->components == NULL)
5288     {
5289       gfc_error ("The derived type '%s' at %L is of type '%s', "
5290                  "which has not been defined.", sym->name,
5291                   &sym->declared_at, sym->ts.derived->name);
5292       sym->ts.type = BT_UNKNOWN;
5293       return;
5294     }
5295
5296   /* An assumed-size array with INTENT(OUT) shall not be of a type for which
5297      default initialization is defined (5.1.2.4.4).  */
5298   if (sym->ts.type == BT_DERIVED
5299         && sym->attr.dummy
5300         && sym->attr.intent == INTENT_OUT
5301         && sym->as
5302         && sym->as->type == AS_ASSUMED_SIZE)
5303     {
5304       for (c = sym->ts.derived->components; c; c = c->next)
5305         {
5306           if (c->initializer)
5307             {
5308               gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
5309                          "ASSUMED SIZE and so cannot have a default initializer",
5310                          sym->name, &sym->declared_at);
5311               return;
5312             }
5313         }
5314     }
5315
5316   switch (sym->attr.flavor)
5317     {
5318     case FL_VARIABLE:
5319       if (resolve_fl_variable (sym, mp_flag) == FAILURE)
5320         return;
5321       break;
5322
5323     case FL_PROCEDURE:
5324       if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
5325         return;
5326       break;
5327
5328     case FL_NAMELIST:
5329       if (resolve_fl_namelist (sym) == FAILURE)
5330         return;
5331       break;
5332
5333     case FL_PARAMETER:
5334       if (resolve_fl_parameter (sym) == FAILURE)
5335         return;
5336
5337       break;
5338
5339     default:
5340
5341       break;
5342     }
5343
5344   /* Make sure that intrinsic exist */
5345   if (sym->attr.intrinsic
5346       && ! gfc_intrinsic_name(sym->name, 0)
5347       && ! gfc_intrinsic_name(sym->name, 1))
5348     gfc_error("Intrinsic at %L does not exist", &sym->declared_at);
5349
5350   /* Resolve array specifier. Check as well some constraints
5351      on COMMON blocks.  */
5352
5353   check_constant = sym->attr.in_common && !sym->attr.pointer;
5354   gfc_resolve_array_spec (sym->as, check_constant);
5355
5356   /* Resolve formal namespaces.  */
5357
5358   if (formal_ns_flag && sym != NULL && sym->formal_ns != NULL)
5359     {
5360       formal_ns_save = formal_ns_flag;
5361       formal_ns_flag = 0;
5362       gfc_resolve (sym->formal_ns);
5363       formal_ns_flag = formal_ns_save;
5364     }
5365
5366   /* Check threadprivate restrictions.  */
5367   if (sym->attr.threadprivate && !sym->attr.save
5368       && (!sym->attr.in_common
5369           && sym->module == NULL
5370           && (sym->ns->proc_name == NULL
5371               || sym->ns->proc_name->attr.flavor != FL_MODULE)))
5372     gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
5373 }
5374
5375
5376
5377 /************* Resolve DATA statements *************/
5378
5379 static struct
5380 {
5381   gfc_data_value *vnode;
5382   unsigned int left;
5383 }
5384 values;
5385
5386
5387 /* Advance the values structure to point to the next value in the data list.  */
5388
5389 static try
5390 next_data_value (void)
5391 {
5392   while (values.left == 0)
5393     {
5394       if (values.vnode->next == NULL)
5395         return FAILURE;
5396
5397       values.vnode = values.vnode->next;
5398       values.left = values.vnode->repeat;
5399     }
5400
5401   return SUCCESS;
5402 }
5403
5404
5405 static try
5406 check_data_variable (gfc_data_variable * var, locus * where)
5407 {
5408   gfc_expr *e;
5409   mpz_t size;
5410   mpz_t offset;
5411   try t;
5412   ar_type mark = AR_UNKNOWN;
5413   int i;
5414   mpz_t section_index[GFC_MAX_DIMENSIONS];
5415   gfc_ref *ref;
5416   gfc_array_ref *ar;
5417
5418   if (gfc_resolve_expr (var->expr) == FAILURE)
5419     return FAILURE;
5420
5421   ar = NULL;
5422   mpz_init_set_si (offset, 0);
5423   e = var->expr;
5424
5425   if (e->expr_type != EXPR_VARIABLE)
5426     gfc_internal_error ("check_data_variable(): Bad expression");
5427
5428   if (e->symtree->n.sym->ns->is_block_data
5429         && !e->symtree->n.sym->attr.in_common)
5430     {
5431       gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
5432                  e->symtree->n.sym->name, &e->symtree->n.sym->declared_at);
5433     }
5434
5435   if (e->rank == 0)
5436     {
5437       mpz_init_set_ui (size, 1);
5438       ref = NULL;
5439     }
5440   else
5441     {
5442       ref = e->ref;
5443
5444       /* Find the array section reference.  */
5445       for (ref = e->ref; ref; ref = ref->next)
5446         {
5447           if (ref->type != REF_ARRAY)
5448             continue;
5449           if (ref->u.ar.type == AR_ELEMENT)
5450             continue;
5451           break;
5452         }
5453       gcc_assert (ref);
5454
5455       /* Set marks according to the reference pattern.  */
5456       switch (ref->u.ar.type)
5457         {
5458         case AR_FULL:
5459           mark = AR_FULL;
5460           break;
5461
5462         case AR_SECTION:
5463           ar = &ref->u.ar;
5464           /* Get the start position of array section.  */
5465           gfc_get_section_index (ar, section_index, &offset);
5466           mark = AR_SECTION;
5467           break;
5468
5469         default:
5470           gcc_unreachable ();
5471         }
5472
5473       if (gfc_array_size (e, &size) == FAILURE)
5474         {
5475           gfc_error ("Nonconstant array section at %L in DATA statement",
5476                      &e->where);
5477           mpz_clear (offset);
5478           return FAILURE;
5479         }
5480     }
5481
5482   t = SUCCESS;
5483
5484   while (mpz_cmp_ui (size, 0) > 0)
5485     {
5486       if (next_data_value () == FAILURE)
5487         {
5488           gfc_error ("DATA statement at %L has more variables than values",
5489                      where);
5490           t = FAILURE;
5491           break;
5492         }
5493
5494       t = gfc_check_assign (var->expr, values.vnode->expr, 0);
5495       if (t == FAILURE)
5496         break;
5497
5498       /* If we have more than one element left in the repeat count,
5499          and we have more than one element left in the target variable,
5500          then create a range assignment.  */
5501       /* ??? Only done for full arrays for now, since array sections
5502          seem tricky.  */
5503       if (mark == AR_FULL && ref && ref->next == NULL
5504           && values.left > 1 && mpz_cmp_ui (size, 1) > 0)
5505         {
5506           mpz_t range;
5507
5508           if (mpz_cmp_ui (size, values.left) >= 0)
5509             {
5510               mpz_init_set_ui (range, values.left);
5511               mpz_sub_ui (size, size, values.left);
5512               values.left = 0;
5513             }
5514           else
5515             {
5516               mpz_init_set (range, size);
5517               values.left -= mpz_get_ui (size);
5518               mpz_set_ui (size, 0);
5519             }
5520
5521           gfc_assign_data_value_range (var->expr, values.vnode->expr,
5522                                        offset, range);
5523
5524           mpz_add (offset, offset, range);
5525           mpz_clear (range);
5526         }
5527
5528       /* Assign initial value to symbol.  */
5529       else
5530         {
5531           values.left -= 1;
5532           mpz_sub_ui (size, size, 1);
5533
5534           gfc_assign_data_value (var->expr, values.vnode->expr, offset);
5535
5536           if (mark == AR_FULL)
5537             mpz_add_ui (offset, offset, 1);
5538
5539           /* Modify the array section indexes and recalculate the offset
5540              for next element.  */
5541           else if (mark == AR_SECTION)
5542             gfc_advance_section (section_index, ar, &offset);
5543         }
5544     }
5545
5546   if (mark == AR_SECTION)
5547     {
5548       for (i = 0; i < ar->dimen; i++)
5549         mpz_clear (section_index[i]);
5550     }
5551
5552   mpz_clear (size);
5553   mpz_clear (offset);
5554
5555   return t;
5556 }
5557
5558
5559 static try traverse_data_var (gfc_data_variable *, locus *);
5560
5561 /* Iterate over a list of elements in a DATA statement.  */
5562
5563 static try
5564 traverse_data_list (gfc_data_variable * var, locus * where)
5565 {
5566   mpz_t trip;
5567   iterator_stack frame;
5568   gfc_expr *e;
5569
5570   mpz_init (frame.value);
5571
5572   mpz_init_set (trip, var->iter.end->value.integer);
5573   mpz_sub (trip, trip, var->iter.start->value.integer);
5574   mpz_add (trip, trip, var->iter.step->value.integer);
5575
5576   mpz_div (trip, trip, var->iter.step->value.integer);
5577
5578   mpz_set (frame.value, var->iter.start->value.integer);
5579
5580   frame.prev = iter_stack;
5581   frame.variable = var->iter.var->symtree;
5582   iter_stack = &frame;
5583
5584   while (mpz_cmp_ui (trip, 0) > 0)
5585     {
5586       if (traverse_data_var (var->list, where) == FAILURE)
5587         {
5588           mpz_clear (trip);
5589           return FAILURE;
5590         }
5591
5592       e = gfc_copy_expr (var->expr);
5593       if (gfc_simplify_expr (e, 1) == FAILURE)
5594         {
5595           gfc_free_expr (e);
5596           return FAILURE;
5597         }
5598
5599       mpz_add (frame.value, frame.value, var->iter.step->value.integer);
5600
5601       mpz_sub_ui (trip, trip, 1);
5602     }
5603
5604   mpz_clear (trip);
5605   mpz_clear (frame.value);
5606
5607   iter_stack = frame.prev;
5608   return SUCCESS;
5609 }
5610
5611
5612 /* Type resolve variables in the variable list of a DATA statement.  */
5613
5614 static try
5615 traverse_data_var (gfc_data_variable * var, locus * where)
5616 {
5617   try t;
5618
5619   for (; var; var = var->next)
5620     {
5621       if (var->expr == NULL)
5622         t = traverse_data_list (var, where);
5623       else
5624         t = check_data_variable (var, where);
5625
5626       if (t == FAILURE)
5627         return FAILURE;
5628     }
5629
5630   return SUCCESS;
5631 }
5632
5633
5634 /* Resolve the expressions and iterators associated with a data statement.
5635    This is separate from the assignment checking because data lists should
5636    only be resolved once.  */
5637
5638 static try
5639 resolve_data_variables (gfc_data_variable * d)
5640 {
5641   for (; d; d = d->next)
5642     {
5643       if (d->list == NULL)
5644         {
5645           if (gfc_resolve_expr (d->expr) == FAILURE)
5646             return FAILURE;
5647         }
5648       else
5649         {
5650           if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
5651             return FAILURE;
5652
5653           if (d->iter.start->expr_type != EXPR_CONSTANT
5654               || d->iter.end->expr_type != EXPR_CONSTANT
5655               || d->iter.step->expr_type != EXPR_CONSTANT)
5656             gfc_internal_error ("resolve_data_variables(): Bad iterator");
5657
5658           if (resolve_data_variables (d->list) == FAILURE)
5659             return FAILURE;
5660         }
5661     }
5662
5663   return SUCCESS;
5664 }
5665
5666
5667 /* Resolve a single DATA statement.  We implement this by storing a pointer to
5668    the value list into static variables, and then recursively traversing the
5669    variables list, expanding iterators and such.  */
5670
5671 static void
5672 resolve_data (gfc_data * d)
5673 {
5674   if (resolve_data_variables (d->var) == FAILURE)
5675     return;
5676
5677   values.vnode = d->value;
5678   values.left = (d->value == NULL) ? 0 : d->value->repeat;
5679
5680   if (traverse_data_var (d->var, &d->where) == FAILURE)
5681     return;
5682
5683   /* At this point, we better not have any values left.  */
5684
5685   if (next_data_value () == SUCCESS)
5686     gfc_error ("DATA statement at %L has more values than variables",
5687                &d->where);
5688 }
5689
5690
5691 /* Determines if a variable is not 'pure', ie not assignable within a pure
5692    procedure.  Returns zero if assignment is OK, nonzero if there is a problem.
5693  */
5694
5695 int
5696 gfc_impure_variable (gfc_symbol * sym)
5697 {
5698   if (sym->attr.use_assoc || sym->attr.in_common)
5699     return 1;
5700
5701   if (sym->ns != gfc_current_ns)
5702     return !sym->attr.function;
5703
5704   /* TODO: Check storage association through EQUIVALENCE statements */
5705
5706   return 0;
5707 }
5708
5709
5710 /* Test whether a symbol is pure or not.  For a NULL pointer, checks the
5711    symbol of the current procedure.  */
5712
5713 int
5714 gfc_pure (gfc_symbol * sym)
5715 {
5716   symbol_attribute attr;
5717
5718   if (sym == NULL)
5719     sym = gfc_current_ns->proc_name;
5720   if (sym == NULL)
5721     return 0;
5722
5723   attr = sym->attr;
5724
5725   return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
5726 }
5727
5728
5729 /* Test whether the current procedure is elemental or not.  */
5730
5731 int
5732 gfc_elemental (gfc_symbol * sym)
5733 {
5734   symbol_attribute attr;
5735
5736   if (sym == NULL)
5737     sym = gfc_current_ns->proc_name;
5738   if (sym == NULL)
5739     return 0;
5740   attr = sym->attr;
5741
5742   return attr.flavor == FL_PROCEDURE && attr.elemental;
5743 }
5744
5745
5746 /* Warn about unused labels.  */
5747
5748 static void
5749 warn_unused_label (gfc_st_label * label)
5750 {
5751   if (label == NULL)
5752     return;
5753
5754   warn_unused_label (label->left);
5755
5756   if (label->defined == ST_LABEL_UNKNOWN)
5757     return;
5758
5759   switch (label->referenced)
5760     {
5761     case ST_LABEL_UNKNOWN:
5762       gfc_warning ("Label %d at %L defined but not used", label->value,
5763                    &label->where);
5764       break;
5765
5766     case ST_LABEL_BAD_TARGET:
5767       gfc_warning ("Label %d at %L defined but cannot be used",
5768                    label->value, &label->where);
5769       break;
5770
5771     default:
5772       break;
5773     }
5774
5775   warn_unused_label (label->right);
5776 }
5777
5778
5779 /* Returns the sequence type of a symbol or sequence.  */
5780
5781 static seq_type
5782 sequence_type (gfc_typespec ts)
5783 {
5784   seq_type result;
5785   gfc_component *c;
5786
5787   switch (ts.type)
5788   {
5789     case BT_DERIVED:
5790
5791       if (ts.derived->components == NULL)
5792         return SEQ_NONDEFAULT;
5793
5794       result = sequence_type (ts.derived->components->ts);
5795       for (c = ts.derived->components->next; c; c = c->next)
5796         if (sequence_type (c->ts) != result)
5797           return SEQ_MIXED;
5798
5799       return result;
5800
5801     case BT_CHARACTER:
5802       if (ts.kind != gfc_default_character_kind)
5803           return SEQ_NONDEFAULT;
5804
5805       return SEQ_CHARACTER;
5806
5807     case BT_INTEGER:
5808       if (ts.kind != gfc_default_integer_kind)
5809           return SEQ_NONDEFAULT;
5810
5811       return SEQ_NUMERIC;
5812
5813     case BT_REAL:
5814       if (!(ts.kind == gfc_default_real_kind
5815              || ts.kind == gfc_default_double_kind))
5816           return SEQ_NONDEFAULT;
5817
5818       return SEQ_NUMERIC;
5819
5820     case BT_COMPLEX:
5821       if (ts.kind != gfc_default_complex_kind)
5822           return SEQ_NONDEFAULT;
5823
5824       return SEQ_NUMERIC;
5825
5826     case BT_LOGICAL:
5827       if (ts.kind != gfc_default_logical_kind)
5828           return SEQ_NONDEFAULT;
5829
5830       return SEQ_NUMERIC;
5831
5832     default:
5833       return SEQ_NONDEFAULT;
5834   }
5835 }
5836
5837
5838 /* Resolve derived type EQUIVALENCE object.  */
5839
5840 static try
5841 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
5842 {
5843   gfc_symbol *d;
5844   gfc_component *c = derived->components;
5845
5846   if (!derived)
5847     return SUCCESS;
5848
5849   /* Shall not be an object of nonsequence derived type.  */
5850   if (!derived->attr.sequence)
5851     {
5852       gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
5853                  "attribute to be an EQUIVALENCE object", sym->name, &e->where);
5854       return FAILURE;
5855     }
5856
5857   for (; c ; c = c->next)
5858     {
5859       d = c->ts.derived;
5860       if (d && (resolve_equivalence_derived (c->ts.derived, sym, e) == FAILURE))
5861         return FAILURE;
5862         
5863       /* Shall not be an object of sequence derived type containing a pointer
5864          in the structure.  */
5865       if (c->pointer)
5866         {
5867           gfc_error ("Derived type variable '%s' at %L with pointer component(s) "
5868                      "cannot be an EQUIVALENCE object", sym->name, &e->where);
5869           return FAILURE;
5870         }
5871
5872       if (c->initializer)
5873         {
5874           gfc_error ("Derived type variable '%s' at %L with default initializer "
5875                      "cannot be an EQUIVALENCE object", sym->name, &e->where);
5876           return FAILURE;
5877         }
5878     }
5879   return SUCCESS;
5880 }
5881
5882
5883 /* Resolve equivalence object. 
5884    An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
5885    an allocatable array, an object of nonsequence derived type, an object of
5886    sequence derived type containing a pointer at any level of component
5887    selection, an automatic object, a function name, an entry name, a result
5888    name, a named constant, a structure component, or a subobject of any of
5889    the preceding objects.  A substring shall not have length zero.  A
5890    derived type shall not have components with default initialization nor
5891    shall two objects of an equivalence group be initialized.
5892    The simple constraints are done in symbol.c(check_conflict) and the rest
5893    are implemented here.  */
5894
5895 static void
5896 resolve_equivalence (gfc_equiv *eq)
5897 {
5898   gfc_symbol *sym;
5899   gfc_symbol *derived;
5900   gfc_symbol *first_sym;
5901   gfc_expr *e;
5902   gfc_ref *r;
5903   locus *last_where = NULL;
5904   seq_type eq_type, last_eq_type;
5905   gfc_typespec *last_ts;
5906   int object;
5907   const char *value_name;
5908   const char *msg;
5909
5910   value_name = NULL;
5911   last_ts = &eq->expr->symtree->n.sym->ts;
5912
5913   first_sym = eq->expr->symtree->n.sym;
5914
5915   for (object = 1; eq; eq = eq->eq, object++)
5916     {
5917       e = eq->expr;
5918
5919       e->ts = e->symtree->n.sym->ts;
5920       /* match_varspec might not know yet if it is seeing
5921          array reference or substring reference, as it doesn't
5922          know the types.  */
5923       if (e->ref && e->ref->type == REF_ARRAY)
5924         {
5925           gfc_ref *ref = e->ref;
5926           sym = e->symtree->n.sym;
5927
5928           if (sym->attr.dimension)
5929             {
5930               ref->u.ar.as = sym->as;
5931               ref = ref->next;
5932             }
5933
5934           /* For substrings, convert REF_ARRAY into REF_SUBSTRING.  */
5935           if (e->ts.type == BT_CHARACTER
5936               && ref
5937               && ref->type == REF_ARRAY
5938               && ref->u.ar.dimen == 1
5939               && ref->u.ar.dimen_type[0] == DIMEN_RANGE
5940               && ref->u.ar.stride[0] == NULL)
5941             {
5942               gfc_expr *start = ref->u.ar.start[0];
5943               gfc_expr *end = ref->u.ar.end[0];
5944               void *mem = NULL;
5945
5946               /* Optimize away the (:) reference.  */
5947               if (start == NULL && end == NULL)
5948                 {
5949                   if (e->ref == ref)
5950                     e->ref = ref->next;
5951                   else
5952                     e->ref->next = ref->next;
5953                   mem = ref;
5954                 }
5955               else
5956                 {
5957                   ref->type = REF_SUBSTRING;
5958                   if (start == NULL)
5959                     start = gfc_int_expr (1);
5960                   ref->u.ss.start = start;
5961                   if (end == NULL && e->ts.cl)
5962                     end = gfc_copy_expr (e->ts.cl->length);
5963                   ref->u.ss.end = end;
5964                   ref->u.ss.length = e->ts.cl;
5965                   e->ts.cl = NULL;
5966                 }
5967               ref = ref->next;
5968               gfc_free (mem);
5969             }
5970
5971           /* Any further ref is an error.  */
5972           if (ref)
5973             {
5974               gcc_assert (ref->type == REF_ARRAY);
5975               gfc_error ("Syntax error in EQUIVALENCE statement at %L",
5976                          &ref->u.ar.where);
5977               continue;
5978             }
5979         }
5980
5981       if (gfc_resolve_expr (e) == FAILURE)
5982         continue;
5983
5984       sym = e->symtree->n.sym;
5985
5986       /* An equivalence statement cannot have more than one initialized
5987          object.  */
5988       if (sym->value)
5989         {
5990           if (value_name != NULL)
5991             {
5992               gfc_error ("Initialized objects '%s' and '%s'  cannot both "
5993                          "be in the EQUIVALENCE statement at %L",
5994                          value_name, sym->name, &e->where);
5995               continue;
5996             }
5997           else
5998             value_name = sym->name;
5999         }
6000
6001       /* Shall not equivalence common block variables in a PURE procedure.  */
6002       if (sym->ns->proc_name 
6003             && sym->ns->proc_name->attr.pure
6004             && sym->attr.in_common)
6005         {
6006           gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
6007                      "object in the pure procedure '%s'",
6008                      sym->name, &e->where, sym->ns->proc_name->name);
6009           break;
6010         }
6011  
6012       /* Shall not be a named constant.  */      
6013       if (e->expr_type == EXPR_CONSTANT)
6014         {
6015           gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
6016                      "object", sym->name, &e->where);
6017           continue;
6018         }
6019
6020       derived = e->ts.derived;
6021       if (derived && resolve_equivalence_derived (derived, sym, e) == FAILURE)
6022         continue;
6023
6024       /* Check that the types correspond correctly:
6025          Note 5.28:
6026          A numeric sequence structure may be equivalenced to another sequence
6027          structure, an object of default integer type, default real type, double
6028          precision real type, default logical type such that components of the
6029          structure ultimately only become associated to objects of the same
6030          kind. A character sequence structure may be equivalenced to an object
6031          of default character kind or another character sequence structure.
6032          Other objects may be equivalenced only to objects of the same type and
6033          kind parameters.  */
6034
6035       /* Identical types are unconditionally OK.  */
6036       if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
6037         goto identical_types;
6038
6039       last_eq_type = sequence_type (*last_ts);
6040       eq_type = sequence_type (sym->ts);
6041
6042       /* Since the pair of objects is not of the same type, mixed or
6043          non-default sequences can be rejected.  */
6044
6045       msg = "Sequence %s with mixed components in EQUIVALENCE "
6046             "statement at %L with different type objects";
6047       if ((object ==2
6048                && last_eq_type == SEQ_MIXED
6049                && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
6050                                   last_where) == FAILURE)
6051            ||  (eq_type == SEQ_MIXED
6052                && gfc_notify_std (GFC_STD_GNU, msg,sym->name,
6053                                   &e->where) == FAILURE))
6054         continue;
6055
6056       msg = "Non-default type object or sequence %s in EQUIVALENCE "
6057             "statement at %L with objects of different type";
6058       if ((object ==2
6059                && last_eq_type == SEQ_NONDEFAULT
6060                && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
6061                                   last_where) == FAILURE)
6062            ||  (eq_type == SEQ_NONDEFAULT
6063                && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
6064                                   &e->where) == FAILURE))
6065         continue;
6066
6067       msg ="Non-CHARACTER object '%s' in default CHARACTER "
6068            "EQUIVALENCE statement at %L";
6069       if (last_eq_type == SEQ_CHARACTER
6070             && eq_type != SEQ_CHARACTER
6071             && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
6072                                   &e->where) == FAILURE)
6073                 continue;
6074
6075       msg ="Non-NUMERIC object '%s' in default NUMERIC "
6076            "EQUIVALENCE statement at %L";
6077       if (last_eq_type == SEQ_NUMERIC
6078             && eq_type != SEQ_NUMERIC
6079             && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
6080                                   &e->where) == FAILURE)
6081                 continue;
6082
6083   identical_types:
6084       last_ts =&sym->ts;
6085       last_where = &e->where;
6086
6087       if (!e->ref)
6088         continue;
6089
6090       /* Shall not be an automatic array.  */
6091       if (e->ref->type == REF_ARRAY
6092           && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
6093         {
6094           gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
6095                      "an EQUIVALENCE object", sym->name, &e->where);
6096           continue;
6097         }
6098
6099       r = e->ref;
6100       while (r)
6101         {
6102           /* Shall not be a structure component.  */
6103           if (r->type == REF_COMPONENT)
6104             {
6105               gfc_error ("Structure component '%s' at %L cannot be an "
6106                          "EQUIVALENCE object",
6107                          r->u.c.component->name, &e->where);
6108               break;
6109             }
6110
6111           /* A substring shall not have length zero.  */
6112           if (r->type == REF_SUBSTRING)
6113             {
6114               if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
6115                 {
6116                   gfc_error ("Substring at %L has length zero",
6117                              &r->u.ss.start->where);
6118                   break;
6119                 }
6120             }
6121           r = r->next;
6122         }
6123     }    
6124 }      
6125
6126
6127 /* Resolve function and ENTRY types, issue diagnostics if needed. */
6128
6129 static void
6130 resolve_fntype (gfc_namespace * ns)
6131 {
6132   gfc_entry_list *el;
6133   gfc_symbol *sym;
6134
6135   if (ns->proc_name == NULL || !ns->proc_name->attr.function)
6136     return;
6137
6138   /* If there are any entries, ns->proc_name is the entry master
6139      synthetic symbol and ns->entries->sym actual FUNCTION symbol.  */
6140   if (ns->entries)
6141     sym = ns->entries->sym;
6142   else
6143     sym = ns->proc_name;
6144   if (sym->result == sym
6145       && sym->ts.type == BT_UNKNOWN
6146       && gfc_set_default_type (sym, 0, NULL) == FAILURE
6147       && !sym->attr.untyped)
6148     {
6149       gfc_error ("Function '%s' at %L has no IMPLICIT type",
6150                  sym->name, &sym->declared_at);
6151       sym->attr.untyped = 1;
6152     }
6153
6154   if (sym->ts.type == BT_DERIVED && !sym->ts.derived->attr.use_assoc
6155       && !gfc_check_access (sym->ts.derived->attr.access,
6156                             sym->ts.derived->ns->default_access)
6157       && gfc_check_access (sym->attr.access, sym->ns->default_access))
6158     {
6159       gfc_error ("PUBLIC function '%s' at %L cannot be of PRIVATE type '%s'",
6160                  sym->name, &sym->declared_at, sym->ts.derived->name);
6161     }
6162
6163   if (ns->entries)
6164     for (el = ns->entries->next; el; el = el->next)
6165       {
6166         if (el->sym->result == el->sym
6167             && el->sym->ts.type == BT_UNKNOWN
6168             && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
6169             && !el->sym->attr.untyped)
6170           {
6171             gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
6172                        el->sym->name, &el->sym->declared_at);
6173             el->sym->attr.untyped = 1;
6174           }
6175       }
6176 }
6177
6178 /* 12.3.2.1.1 Defined operators.  */
6179
6180 static void
6181 gfc_resolve_uops(gfc_symtree *symtree)
6182 {
6183   gfc_interface *itr;
6184   gfc_symbol *sym;
6185   gfc_formal_arglist *formal;
6186
6187   if (symtree == NULL) 
6188     return; 
6189  
6190   gfc_resolve_uops (symtree->left);
6191   gfc_resolve_uops (symtree->right);
6192
6193   for (itr = symtree->n.uop->operator; itr; itr = itr->next)
6194     {
6195       sym = itr->sym;
6196       if (!sym->attr.function)
6197         gfc_error("User operator procedure '%s' at %L must be a FUNCTION",
6198                   sym->name, &sym->declared_at);
6199
6200       if (sym->ts.type == BT_CHARACTER
6201             && !(sym->ts.cl && sym->ts.cl->length)
6202             && !(sym->result && sym->result->ts.cl && sym->result->ts.cl->length))
6203         gfc_error("User operator procedure '%s' at %L cannot be assumed character "
6204                   "length", sym->name, &sym->declared_at);
6205
6206       formal = sym->formal;
6207       if (!formal || !formal->sym)
6208         {
6209           gfc_error("User operator procedure '%s' at %L must have at least "
6210                     "one argument", sym->name, &sym->declared_at);
6211           continue;
6212         }
6213
6214       if (formal->sym->attr.intent != INTENT_IN)
6215         gfc_error ("First argument of operator interface at %L must be "
6216                    "INTENT(IN)", &sym->declared_at);
6217
6218       if (formal->sym->attr.optional)
6219         gfc_error ("First argument of operator interface at %L cannot be "
6220                    "optional", &sym->declared_at);
6221
6222       formal = formal->next;
6223       if (!formal || !formal->sym)
6224         continue;
6225
6226       if (formal->sym->attr.intent != INTENT_IN)
6227         gfc_error ("Second argument of operator interface at %L must be "
6228                    "INTENT(IN)", &sym->declared_at);
6229
6230       if (formal->sym->attr.optional)
6231         gfc_error ("Second argument of operator interface at %L cannot be "
6232                    "optional", &sym->declared_at);
6233
6234       if (formal->next)
6235         gfc_error ("Operator interface at %L must have, at most, two "
6236                    "arguments", &sym->declared_at);
6237     }
6238 }
6239
6240
6241 /* Examine all of the expressions associated with a program unit,
6242    assign types to all intermediate expressions, make sure that all
6243    assignments are to compatible types and figure out which names
6244    refer to which functions or subroutines.  It doesn't check code
6245    block, which is handled by resolve_code.  */
6246
6247 static void
6248 resolve_types (gfc_namespace * ns)
6249 {
6250   gfc_namespace *n;
6251   gfc_charlen *cl;
6252   gfc_data *d;
6253   gfc_equiv *eq;
6254
6255   gfc_current_ns = ns;
6256
6257   resolve_entries (ns);
6258
6259   resolve_contained_functions (ns);
6260
6261   gfc_traverse_ns (ns, resolve_symbol);
6262
6263   resolve_fntype (ns);
6264
6265   for (n = ns->contained; n; n = n->sibling)
6266     {
6267       if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
6268         gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
6269                    "also be PURE", n->proc_name->name,
6270                    &n->proc_name->declared_at);
6271
6272       resolve_types (n);
6273     }
6274
6275   forall_flag = 0;
6276   gfc_check_interfaces (ns);
6277
6278   for (cl = ns->cl_list; cl; cl = cl->next)
6279     resolve_charlen (cl);
6280
6281   gfc_traverse_ns (ns, resolve_values);
6282
6283   if (ns->save_all)
6284     gfc_save_all (ns);
6285
6286   iter_stack = NULL;
6287   for (d = ns->data; d; d = d->next)
6288     resolve_data (d);
6289
6290   iter_stack = NULL;
6291   gfc_traverse_ns (ns, gfc_formalize_init_value);
6292
6293   for (eq = ns->equiv; eq; eq = eq->next)
6294     resolve_equivalence (eq);
6295
6296   /* Warn about unused labels.  */
6297   if (gfc_option.warn_unused_labels)
6298     warn_unused_label (ns->st_labels);
6299
6300   gfc_resolve_uops (ns->uop_root);
6301     
6302 }
6303
6304
6305 /* Call resolve_code recursively.  */
6306
6307 static void
6308 resolve_codes (gfc_namespace * ns)
6309 {
6310   gfc_namespace *n;
6311
6312   for (n = ns->contained; n; n = n->sibling)
6313     resolve_codes (n);
6314
6315   gfc_current_ns = ns;
6316   cs_base = NULL;
6317   resolve_code (ns->code, ns);
6318 }
6319
6320
6321 /* This function is called after a complete program unit has been compiled.
6322    Its purpose is to examine all of the expressions associated with a program
6323    unit, assign types to all intermediate expressions, make sure that all
6324    assignments are to compatible types and figure out which names refer to
6325    which functions or subroutines.  */
6326
6327 void
6328 gfc_resolve (gfc_namespace * ns)
6329 {
6330   gfc_namespace *old_ns;
6331
6332   old_ns = gfc_current_ns;
6333
6334   resolve_types (ns);
6335   resolve_codes (ns);
6336
6337   gfc_current_ns = old_ns;
6338 }