OSDN Git Service

2006-12-03 Paul Thomas <pault@gcc.gnu.org>
[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 "flags.h"
27 #include "gfortran.h"
28 #include "arith.h"  /* For gfc_compare_expr().  */
29 #include "dependency.h"
30
31 /* Types used in equivalence statements.  */
32
33 typedef enum seq_type
34 {
35   SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
36 }
37 seq_type;
38
39 /* Stack to push the current if we descend into a block during
40    resolution.  See resolve_branch() and resolve_code().  */
41
42 typedef struct code_stack
43 {
44   struct gfc_code *head, *current;
45   struct code_stack *prev;
46 }
47 code_stack;
48
49 static code_stack *cs_base = NULL;
50
51
52 /* Nonzero if we're inside a FORALL block.  */
53
54 static int forall_flag;
55
56 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block.  */
57
58 static int omp_workshare_flag;
59
60 /* Nonzero if we are processing a formal arglist. The corresponding function
61    resets the flag each time that it is read.  */
62 static int formal_arg_flag = 0;
63
64 /* True if we are resolving a specification expression.  */
65 static int specification_expr = 0;
66
67 /* The id of the last entry seen.  */
68 static int current_entry_id;
69
70 int
71 gfc_is_formal_arg (void)
72 {
73   return formal_arg_flag;
74 }
75
76 /* Resolve types of formal argument lists.  These have to be done early so that
77    the formal argument lists of module procedures can be copied to the
78    containing module before the individual procedures are resolved
79    individually.  We also resolve argument lists of procedures in interface
80    blocks because they are self-contained scoping units.
81
82    Since a dummy argument cannot be a non-dummy procedure, the only
83    resort left for untyped names are the IMPLICIT types.  */
84
85 static void
86 resolve_formal_arglist (gfc_symbol * proc)
87 {
88   gfc_formal_arglist *f;
89   gfc_symbol *sym;
90   int i;
91
92   if (proc->result != NULL)
93     sym = proc->result;
94   else
95     sym = proc;
96
97   if (gfc_elemental (proc)
98       || sym->attr.pointer || sym->attr.allocatable
99       || (sym->as && sym->as->rank > 0))
100     proc->attr.always_explicit = 1;
101
102   formal_arg_flag = 1;
103
104   for (f = proc->formal; f; f = f->next)
105     {
106       sym = f->sym;
107
108       if (sym == NULL)
109         {
110           /* Alternate return placeholder.  */
111           if (gfc_elemental (proc))
112             gfc_error ("Alternate return specifier in elemental subroutine "
113                        "'%s' at %L is not allowed", proc->name,
114                        &proc->declared_at);
115           if (proc->attr.function)
116             gfc_error ("Alternate return specifier in function "
117                        "'%s' at %L is not allowed", proc->name,
118                        &proc->declared_at);
119           continue;
120         }
121
122       if (sym->attr.if_source != IFSRC_UNKNOWN)
123         resolve_formal_arglist (sym);
124
125       if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
126         {
127           if (gfc_pure (proc) && !gfc_pure (sym))
128             {
129               gfc_error
130                 ("Dummy procedure '%s' of PURE procedure at %L must also "
131                  "be PURE", sym->name, &sym->declared_at);
132               continue;
133             }
134
135           if (gfc_elemental (proc))
136             {
137               gfc_error
138                 ("Dummy procedure at %L not allowed in ELEMENTAL procedure",
139                  &sym->declared_at);
140               continue;
141             }
142
143           continue;
144         }
145
146       if (sym->ts.type == BT_UNKNOWN)
147         {
148           if (!sym->attr.function || sym->result == sym)
149             gfc_set_default_type (sym, 1, sym->ns);
150         }
151
152       gfc_resolve_array_spec (sym->as, 0);
153
154       /* We can't tell if an array with dimension (:) is assumed or deferred
155          shape until we know if it has the pointer or allocatable attributes.
156       */
157       if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
158           && !(sym->attr.pointer || sym->attr.allocatable))
159         {
160           sym->as->type = AS_ASSUMED_SHAPE;
161           for (i = 0; i < sym->as->rank; i++)
162             sym->as->lower[i] = gfc_int_expr (1);
163         }
164
165       if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
166           || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
167           || sym->attr.optional)
168         proc->attr.always_explicit = 1;
169
170       /* If the flavor is unknown at this point, it has to be a variable.
171          A procedure specification would have already set the type.  */
172
173       if (sym->attr.flavor == FL_UNKNOWN)
174         gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
175
176       if (gfc_pure (proc))
177         {
178           if (proc->attr.function && !sym->attr.pointer
179               && sym->attr.flavor != FL_PROCEDURE
180               && sym->attr.intent != INTENT_IN)
181
182             gfc_error ("Argument '%s' of pure function '%s' at %L must be "
183                        "INTENT(IN)", sym->name, proc->name,
184                        &sym->declared_at);
185
186           if (proc->attr.subroutine && !sym->attr.pointer
187               && sym->attr.intent == INTENT_UNKNOWN)
188
189             gfc_error
190               ("Argument '%s' of pure subroutine '%s' at %L must have "
191                "its INTENT specified", sym->name, proc->name,
192                &sym->declared_at);
193         }
194
195
196       if (gfc_elemental (proc))
197         {
198           if (sym->as != NULL)
199             {
200               gfc_error
201                 ("Argument '%s' of elemental procedure at %L must be scalar",
202                  sym->name, &sym->declared_at);
203               continue;
204             }
205
206           if (sym->attr.pointer)
207             {
208               gfc_error
209                 ("Argument '%s' of elemental procedure at %L cannot have "
210                  "the POINTER attribute", sym->name, &sym->declared_at);
211               continue;
212             }
213         }
214
215       /* Each dummy shall be specified to be scalar.  */
216       if (proc->attr.proc == PROC_ST_FUNCTION)
217         {
218           if (sym->as != NULL)
219             {
220               gfc_error
221                 ("Argument '%s' of statement function at %L must be scalar",
222                  sym->name, &sym->declared_at);
223               continue;
224             }
225
226           if (sym->ts.type == BT_CHARACTER)
227             {
228               gfc_charlen *cl = sym->ts.cl;
229               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
230                 {
231                   gfc_error
232                     ("Character-valued argument '%s' of statement function at "
233                      "%L must have constant length",
234                      sym->name, &sym->declared_at);
235                   continue;
236                 }
237             }
238         }
239     }
240   formal_arg_flag = 0;
241 }
242
243
244 /* Work function called when searching for symbols that have argument lists
245    associated with them.  */
246
247 static void
248 find_arglists (gfc_symbol * sym)
249 {
250
251   if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
252     return;
253
254   resolve_formal_arglist (sym);
255 }
256
257
258 /* Given a namespace, resolve all formal argument lists within the namespace.
259  */
260
261 static void
262 resolve_formal_arglists (gfc_namespace * ns)
263 {
264
265   if (ns == NULL)
266     return;
267
268   gfc_traverse_ns (ns, find_arglists);
269 }
270
271
272 static void
273 resolve_contained_fntype (gfc_symbol * sym, gfc_namespace * ns)
274 {
275   try t;
276
277   /* If this namespace is not a function, ignore it.  */
278   if (! sym
279       || !(sym->attr.function
280            || sym->attr.flavor == FL_VARIABLE))
281     return;
282
283   /* Try to find out of what the return type is.  */
284   if (sym->result != NULL)
285     sym = sym->result;
286
287   if (sym->ts.type == BT_UNKNOWN)
288     {
289       t = gfc_set_default_type (sym, 0, ns);
290
291       if (t == FAILURE && !sym->attr.untyped)
292         {
293           gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
294                      sym->name, &sym->declared_at); /* FIXME */
295           sym->attr.untyped = 1;
296         }
297     }
298
299   /*Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character type,
300     lists the only ways a character length value of * can be used: dummy arguments
301     of procedures, named constants, and function results in external functions.
302     Internal function results are not on that list; ergo, not permitted.  */
303
304   if (sym->ts.type == BT_CHARACTER)
305     {
306       gfc_charlen *cl = sym->ts.cl;
307       if (!cl || !cl->length)
308         gfc_error ("Character-valued internal function '%s' at %L must "
309                    "not be assumed length", sym->name, &sym->declared_at);
310     }
311 }
312
313
314 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
315    introduce duplicates.  */
316
317 static void
318 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
319 {
320   gfc_formal_arglist *f, *new_arglist;
321   gfc_symbol *new_sym;
322
323   for (; new_args != NULL; new_args = new_args->next)
324     {
325       new_sym = new_args->sym;
326       /* See if this arg is already in the formal argument list.  */
327       for (f = proc->formal; f; f = f->next)
328         {
329           if (new_sym == f->sym)
330             break;
331         }
332
333       if (f)
334         continue;
335
336       /* Add a new argument.  Argument order is not important.  */
337       new_arglist = gfc_get_formal_arglist ();
338       new_arglist->sym = new_sym;
339       new_arglist->next = proc->formal;
340       proc->formal  = new_arglist;
341     }
342 }
343
344
345 /* Resolve alternate entry points.  If a symbol has multiple entry points we
346    create a new master symbol for the main routine, and turn the existing
347    symbol into an entry point.  */
348
349 static void
350 resolve_entries (gfc_namespace * ns)
351 {
352   gfc_namespace *old_ns;
353   gfc_code *c;
354   gfc_symbol *proc;
355   gfc_entry_list *el;
356   char name[GFC_MAX_SYMBOL_LEN + 1];
357   static int master_count = 0;
358
359   if (ns->proc_name == NULL)
360     return;
361
362   /* No need to do anything if this procedure doesn't have alternate entry
363      points.  */
364   if (!ns->entries)
365     return;
366
367   /* We may already have resolved alternate entry points.  */
368   if (ns->proc_name->attr.entry_master)
369     return;
370
371   /* If this isn't a procedure something has gone horribly wrong.  */
372   gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
373
374   /* Remember the current namespace.  */
375   old_ns = gfc_current_ns;
376
377   gfc_current_ns = ns;
378
379   /* Add the main entry point to the list of entry points.  */
380   el = gfc_get_entry_list ();
381   el->sym = ns->proc_name;
382   el->id = 0;
383   el->next = ns->entries;
384   ns->entries = el;
385   ns->proc_name->attr.entry = 1;
386
387   /* If it is a module function, it needs to be in the right namespace
388      so that gfc_get_fake_result_decl can gather up the results. The
389      need for this arose in get_proc_name, where these beasts were
390      left in their own namespace, to keep prior references linked to
391      the entry declaration.*/
392   if (ns->proc_name->attr.function
393         && ns->parent
394         && ns->parent->proc_name->attr.flavor == FL_MODULE)
395     el->sym->ns = ns;
396
397   /* Add an entry statement for it.  */
398   c = gfc_get_code ();
399   c->op = EXEC_ENTRY;
400   c->ext.entry = el;
401   c->next = ns->code;
402   ns->code = c;
403
404   /* Create a new symbol for the master function.  */
405   /* Give the internal function a unique name (within this file).
406      Also include the function name so the user has some hope of figuring
407      out what is going on.  */
408   snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
409             master_count++, ns->proc_name->name);
410   gfc_get_ha_symbol (name, &proc);
411   gcc_assert (proc != NULL);
412
413   gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
414   if (ns->proc_name->attr.subroutine)
415     gfc_add_subroutine (&proc->attr, proc->name, NULL);
416   else
417     {
418       gfc_symbol *sym;
419       gfc_typespec *ts, *fts;
420       gfc_array_spec *as, *fas;
421       gfc_add_function (&proc->attr, proc->name, NULL);
422       proc->result = proc;
423       fas = ns->entries->sym->as;
424       fas = fas ? fas : ns->entries->sym->result->as;
425       fts = &ns->entries->sym->result->ts;
426       if (fts->type == BT_UNKNOWN)
427         fts = gfc_get_default_type (ns->entries->sym->result, NULL);
428       for (el = ns->entries->next; el; el = el->next)
429         {
430           ts = &el->sym->result->ts;
431           as = el->sym->as;
432           as = as ? as : el->sym->result->as;
433           if (ts->type == BT_UNKNOWN)
434             ts = gfc_get_default_type (el->sym->result, NULL);
435
436           if (! gfc_compare_types (ts, fts)
437               || (el->sym->result->attr.dimension
438                   != ns->entries->sym->result->attr.dimension)
439               || (el->sym->result->attr.pointer
440                   != ns->entries->sym->result->attr.pointer))
441             break;
442
443           else if (as && fas && gfc_compare_array_spec (as, fas) == 0)
444             gfc_error ("Procedure %s at %L has entries with mismatched "
445                        "array specifications", ns->entries->sym->name,
446                        &ns->entries->sym->declared_at);
447         }
448
449       if (el == NULL)
450         {
451           sym = ns->entries->sym->result;
452           /* All result types the same.  */
453           proc->ts = *fts;
454           if (sym->attr.dimension)
455             gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
456           if (sym->attr.pointer)
457             gfc_add_pointer (&proc->attr, NULL);
458         }
459       else
460         {
461           /* Otherwise the result will be passed through a union by
462              reference.  */
463           proc->attr.mixed_entry_master = 1;
464           for (el = ns->entries; el; el = el->next)
465             {
466               sym = el->sym->result;
467               if (sym->attr.dimension)
468               {
469                 if (el == ns->entries)
470                   gfc_error
471                   ("FUNCTION result %s can't be an array in FUNCTION %s at %L",
472                    sym->name, ns->entries->sym->name, &sym->declared_at);
473                 else
474                   gfc_error
475                     ("ENTRY result %s can't be an array in FUNCTION %s at %L",
476                      sym->name, ns->entries->sym->name, &sym->declared_at);
477               }
478               else if (sym->attr.pointer)
479               {
480                 if (el == ns->entries)
481                   gfc_error
482                   ("FUNCTION result %s can't be a POINTER in FUNCTION %s at %L",
483                    sym->name, ns->entries->sym->name, &sym->declared_at);
484                 else
485                   gfc_error
486                     ("ENTRY result %s can't be a POINTER in FUNCTION %s at %L",
487                      sym->name, ns->entries->sym->name, &sym->declared_at);
488               }
489               else
490                 {
491                   ts = &sym->ts;
492                   if (ts->type == BT_UNKNOWN)
493                     ts = gfc_get_default_type (sym, NULL);
494                   switch (ts->type)
495                     {
496                     case BT_INTEGER:
497                       if (ts->kind == gfc_default_integer_kind)
498                         sym = NULL;
499                       break;
500                     case BT_REAL:
501                       if (ts->kind == gfc_default_real_kind
502                           || ts->kind == gfc_default_double_kind)
503                         sym = NULL;
504                       break;
505                     case BT_COMPLEX:
506                       if (ts->kind == gfc_default_complex_kind)
507                         sym = NULL;
508                       break;
509                     case BT_LOGICAL:
510                       if (ts->kind == gfc_default_logical_kind)
511                         sym = NULL;
512                       break;
513                     case BT_UNKNOWN:
514                       /* We will issue error elsewhere.  */
515                       sym = NULL;
516                       break;
517                     default:
518                       break;
519                     }
520                   if (sym)
521                   {
522                     if (el == ns->entries)
523                       gfc_error
524                         ("FUNCTION result %s can't be of type %s in FUNCTION %s at %L",
525                          sym->name, gfc_typename (ts), ns->entries->sym->name,
526                          &sym->declared_at);
527                     else
528                       gfc_error
529                         ("ENTRY result %s can't be of type %s in FUNCTION %s at %L",
530                          sym->name, gfc_typename (ts), ns->entries->sym->name,
531                          &sym->declared_at);
532                   }
533                 }
534             }
535         }
536     }
537   proc->attr.access = ACCESS_PRIVATE;
538   proc->attr.entry_master = 1;
539
540   /* Merge all the entry point arguments.  */
541   for (el = ns->entries; el; el = el->next)
542     merge_argument_lists (proc, el->sym->formal);
543
544   /* Use the master function for the function body.  */
545   ns->proc_name = proc;
546
547   /* Finalize the new symbols.  */
548   gfc_commit_symbols ();
549
550   /* Restore the original namespace.  */
551   gfc_current_ns = old_ns;
552 }
553
554
555 /* Resolve contained function types.  Because contained functions can call one
556    another, they have to be worked out before any of the contained procedures
557    can be resolved.
558
559    The good news is that if a function doesn't already have a type, the only
560    way it can get one is through an IMPLICIT type or a RESULT variable, because
561    by definition contained functions are contained namespace they're contained
562    in, not in a sibling or parent namespace.  */
563
564 static void
565 resolve_contained_functions (gfc_namespace * ns)
566 {
567   gfc_namespace *child;
568   gfc_entry_list *el;
569
570   resolve_formal_arglists (ns);
571
572   for (child = ns->contained; child; child = child->sibling)
573     {
574       /* Resolve alternate entry points first.  */
575       resolve_entries (child);
576
577       /* Then check function return types.  */
578       resolve_contained_fntype (child->proc_name, child);
579       for (el = child->entries; el; el = el->next)
580         resolve_contained_fntype (el->sym, child);
581     }
582 }
583
584
585 /* Resolve all of the elements of a structure constructor and make sure that
586    the types are correct.  */
587
588 static try
589 resolve_structure_cons (gfc_expr * expr)
590 {
591   gfc_constructor *cons;
592   gfc_component *comp;
593   try t;
594   symbol_attribute a;
595
596   t = SUCCESS;
597   cons = expr->value.constructor;
598   /* A constructor may have references if it is the result of substituting a
599      parameter variable.  In this case we just pull out the component we
600      want.  */
601   if (expr->ref)
602     comp = expr->ref->u.c.sym->components;
603   else
604     comp = expr->ts.derived->components;
605
606   for (; comp; comp = comp->next, cons = cons->next)
607     {
608       if (! cons->expr)
609         continue;
610
611       if (gfc_resolve_expr (cons->expr) == FAILURE)
612         {
613           t = FAILURE;
614           continue;
615         }
616
617       if (cons->expr->expr_type != EXPR_NULL
618             && comp->as && comp->as->rank != cons->expr->rank
619             && (comp->allocatable || cons->expr->rank))
620         {
621           gfc_error ("The rank of the element in the derived type "
622                      "constructor at %L does not match that of the "
623                      "component (%d/%d)", &cons->expr->where,
624                      cons->expr->rank, comp->as ? comp->as->rank : 0);
625           t = FAILURE;
626         }
627
628       /* If we don't have the right type, try to convert it.  */
629
630       if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
631         {
632           t = FAILURE;
633           if (comp->pointer && cons->expr->ts.type != BT_UNKNOWN)
634             gfc_error ("The element in the derived type constructor at %L, "
635                        "for pointer component '%s', is %s but should be %s",
636                        &cons->expr->where, comp->name,
637                        gfc_basic_typename (cons->expr->ts.type),
638                        gfc_basic_typename (comp->ts.type));
639           else
640             t = gfc_convert_type (cons->expr, &comp->ts, 1);
641         }
642
643       if (!comp->pointer || cons->expr->expr_type == EXPR_NULL)
644         continue;
645
646       a = gfc_expr_attr (cons->expr);
647
648       if (!a.pointer && !a.target)
649         {
650           t = FAILURE;
651           gfc_error ("The element in the derived type constructor at %L, "
652                      "for pointer component '%s' should be a POINTER or "
653                      "a TARGET", &cons->expr->where, comp->name);
654         }
655     }
656
657   return t;
658 }
659
660
661
662 /****************** Expression name resolution ******************/
663
664 /* Returns 0 if a symbol was not declared with a type or
665    attribute declaration statement, nonzero otherwise.  */
666
667 static int
668 was_declared (gfc_symbol * sym)
669 {
670   symbol_attribute a;
671
672   a = sym->attr;
673
674   if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
675     return 1;
676
677   if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
678       || a.optional || a.pointer || a.save || a.target || a.volatile_ || a.value
679       || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
680     return 1;
681
682   return 0;
683 }
684
685
686 /* Determine if a symbol is generic or not.  */
687
688 static int
689 generic_sym (gfc_symbol * sym)
690 {
691   gfc_symbol *s;
692
693   if (sym->attr.generic ||
694       (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
695     return 1;
696
697   if (was_declared (sym) || sym->ns->parent == NULL)
698     return 0;
699
700   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
701
702   return (s == NULL) ? 0 : generic_sym (s);
703 }
704
705
706 /* Determine if a symbol is specific or not.  */
707
708 static int
709 specific_sym (gfc_symbol * sym)
710 {
711   gfc_symbol *s;
712
713   if (sym->attr.if_source == IFSRC_IFBODY
714       || sym->attr.proc == PROC_MODULE
715       || sym->attr.proc == PROC_INTERNAL
716       || sym->attr.proc == PROC_ST_FUNCTION
717       || (sym->attr.intrinsic &&
718           gfc_specific_intrinsic (sym->name))
719       || sym->attr.external)
720     return 1;
721
722   if (was_declared (sym) || sym->ns->parent == NULL)
723     return 0;
724
725   gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
726
727   return (s == NULL) ? 0 : specific_sym (s);
728 }
729
730
731 /* Figure out if the procedure is specific, generic or unknown.  */
732
733 typedef enum
734 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
735 proc_type;
736
737 static proc_type
738 procedure_kind (gfc_symbol * sym)
739 {
740
741   if (generic_sym (sym))
742     return PTYPE_GENERIC;
743
744   if (specific_sym (sym))
745     return PTYPE_SPECIFIC;
746
747   return PTYPE_UNKNOWN;
748 }
749
750 /* Check references to assumed size arrays.  The flag need_full_assumed_size
751    is nonzero when matching actual arguments.  */
752
753 static int need_full_assumed_size = 0;
754
755 static bool
756 check_assumed_size_reference (gfc_symbol * sym, gfc_expr * e)
757 {
758   gfc_ref * ref;
759   int dim;
760   int last = 1;
761
762   if (need_full_assumed_size
763         || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
764       return false;
765
766   for (ref = e->ref; ref; ref = ref->next)
767     if (ref->type == REF_ARRAY)
768       for (dim = 0; dim < ref->u.ar.as->rank; dim++)
769         last = (ref->u.ar.end[dim] == NULL) && (ref->u.ar.type == DIMEN_ELEMENT);
770
771   if (last)
772     {
773       gfc_error ("The upper bound in the last dimension must "
774                  "appear in the reference to the assumed size "
775                  "array '%s' at %L", sym->name, &e->where);
776       return true;
777     }
778   return false;
779 }
780
781
782 /* Look for bad assumed size array references in argument expressions
783   of elemental and array valued intrinsic procedures.  Since this is
784   called from procedure resolution functions, it only recurses at
785   operators.  */
786
787 static bool
788 resolve_assumed_size_actual (gfc_expr *e)
789 {
790   if (e == NULL)
791    return false;
792
793   switch (e->expr_type)
794     {
795     case EXPR_VARIABLE:
796       if (e->symtree
797             && check_assumed_size_reference (e->symtree->n.sym, e))
798         return true;
799       break;
800
801     case EXPR_OP:
802       if (resolve_assumed_size_actual (e->value.op.op1)
803             || resolve_assumed_size_actual (e->value.op.op2))
804         return true;
805       break;
806
807     default:
808       break;
809     }
810   return false;
811 }
812
813
814 /* Resolve an actual argument list.  Most of the time, this is just
815    resolving the expressions in the list.
816    The exception is that we sometimes have to decide whether arguments
817    that look like procedure arguments are really simple variable
818    references.  */
819
820 static try
821 resolve_actual_arglist (gfc_actual_arglist * arg)
822 {
823   gfc_symbol *sym;
824   gfc_symtree *parent_st;
825   gfc_expr *e;
826
827   for (; arg; arg = arg->next)
828     {
829
830       e = arg->expr;
831       if (e == NULL)
832         {
833           /* Check the label is a valid branching target.  */
834           if (arg->label)
835             {
836               if (arg->label->defined == ST_LABEL_UNKNOWN)
837                 {
838                   gfc_error ("Label %d referenced at %L is never defined",
839                              arg->label->value, &arg->label->where);
840                   return FAILURE;
841                 }
842             }
843           continue;
844         }
845
846       if (e->ts.type != BT_PROCEDURE)
847         {
848           if (gfc_resolve_expr (e) != SUCCESS)
849             return FAILURE;
850           continue;
851         }
852
853       /* See if the expression node should really be a variable
854          reference.  */
855
856       sym = e->symtree->n.sym;
857
858       if (sym->attr.flavor == FL_PROCEDURE
859           || sym->attr.intrinsic
860           || sym->attr.external)
861         {
862           int actual_ok;
863
864           /* If a procedure is not already determined to be something else
865              check if it is intrinsic.  */
866           if (!sym->attr.intrinsic
867                 && !(sym->attr.external || sym->attr.use_assoc
868                        || sym->attr.if_source == IFSRC_IFBODY)
869                 && gfc_intrinsic_name (sym->name, sym->attr.subroutine))
870             sym->attr.intrinsic = 1;
871
872           if (sym->attr.proc == PROC_ST_FUNCTION)
873             {
874               gfc_error ("Statement function '%s' at %L is not allowed as an "
875                          "actual argument", sym->name, &e->where);
876             }
877
878           actual_ok = gfc_intrinsic_actual_ok (sym->name, sym->attr.subroutine);
879           if (sym->attr.intrinsic && actual_ok == 0)
880             {
881               gfc_error ("Intrinsic '%s' at %L is not allowed as an "
882                          "actual argument", sym->name, &e->where);
883             }
884
885           if (sym->attr.contained && !sym->attr.use_assoc
886               && sym->ns->proc_name->attr.flavor != FL_MODULE)
887             {
888               gfc_error ("Internal procedure '%s' is not allowed as an "
889                          "actual argument at %L", sym->name, &e->where);
890             }
891
892           if (sym->attr.elemental && !sym->attr.intrinsic)
893             {
894               gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
895                          "allowed as an actual argument at %L", sym->name,
896                          &e->where);
897             }
898
899           if (sym->attr.generic)
900             {
901               gfc_error ("GENERIC non-INTRINSIC procedure '%s' is not "
902                          "allowed as an actual argument at %L", sym->name,
903                          &e->where);
904             }
905
906           /* If the symbol is the function that names the current (or
907              parent) scope, then we really have a variable reference.  */
908
909           if (sym->attr.function && sym->result == sym
910               && (sym->ns->proc_name == sym
911                   || (sym->ns->parent != NULL
912                       && sym->ns->parent->proc_name == sym)))
913             goto got_variable;
914
915           continue;
916         }
917
918       /* See if the name is a module procedure in a parent unit.  */
919
920       if (was_declared (sym) || sym->ns->parent == NULL)
921         goto got_variable;
922
923       if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
924         {
925           gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
926           return FAILURE;
927         }
928
929       if (parent_st == NULL)
930         goto got_variable;
931
932       sym = parent_st->n.sym;
933       e->symtree = parent_st;           /* Point to the right thing.  */
934
935       if (sym->attr.flavor == FL_PROCEDURE
936           || sym->attr.intrinsic
937           || sym->attr.external)
938         {
939           continue;
940         }
941
942     got_variable:
943       e->expr_type = EXPR_VARIABLE;
944       e->ts = sym->ts;
945       if (sym->as != NULL)
946         {
947           e->rank = sym->as->rank;
948           e->ref = gfc_get_ref ();
949           e->ref->type = REF_ARRAY;
950           e->ref->u.ar.type = AR_FULL;
951           e->ref->u.ar.as = sym->as;
952         }
953     }
954
955   return SUCCESS;
956 }
957
958
959 /* Do the checks of the actual argument list that are specific to elemental
960    procedures.  If called with c == NULL, we have a function, otherwise if
961    expr == NULL, we have a subroutine.  */
962 static try
963 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
964 {
965   gfc_actual_arglist *arg0;
966   gfc_actual_arglist *arg;
967   gfc_symbol *esym = NULL;
968   gfc_intrinsic_sym *isym = NULL;
969   gfc_expr *e = NULL;
970   gfc_intrinsic_arg *iformal = NULL;
971   gfc_formal_arglist *eformal = NULL;
972   bool formal_optional = false;
973   bool set_by_optional = false;
974   int i;
975   int rank = 0;
976
977   /* Is this an elemental procedure?  */
978   if (expr && expr->value.function.actual != NULL)
979     {
980       if (expr->value.function.esym != NULL
981             && expr->value.function.esym->attr.elemental)
982         {
983           arg0 = expr->value.function.actual;
984           esym = expr->value.function.esym;
985         }
986       else if (expr->value.function.isym != NULL
987                  && expr->value.function.isym->elemental)
988         {
989           arg0 = expr->value.function.actual;
990           isym = expr->value.function.isym;
991         }
992       else
993         return SUCCESS;
994     }
995   else if (c && c->ext.actual != NULL
996              && c->symtree->n.sym->attr.elemental)
997     {
998       arg0 = c->ext.actual;
999       esym = c->symtree->n.sym;
1000     }
1001   else
1002     return SUCCESS;
1003
1004   /* The rank of an elemental is the rank of its array argument(s).  */
1005   for (arg = arg0; arg; arg = arg->next)
1006     {
1007       if (arg->expr != NULL && arg->expr->rank > 0)
1008         {
1009           rank = arg->expr->rank;
1010           if (arg->expr->expr_type == EXPR_VARIABLE
1011                 && arg->expr->symtree->n.sym->attr.optional)
1012             set_by_optional = true;
1013
1014           /* Function specific; set the result rank and shape.  */
1015           if (expr)
1016             {
1017               expr->rank = rank;
1018               if (!expr->shape && arg->expr->shape)
1019                 {
1020                   expr->shape = gfc_get_shape (rank);
1021                   for (i = 0; i < rank; i++)
1022                     mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1023                 }
1024             }
1025           break;
1026         }
1027     }
1028
1029   /* If it is an array, it shall not be supplied as an actual argument
1030      to an elemental procedure unless an array of the same rank is supplied
1031      as an actual argument corresponding to a nonoptional dummy argument of
1032      that elemental procedure(12.4.1.5).  */
1033   formal_optional = false;
1034   if (isym)
1035     iformal = isym->formal;
1036   else
1037     eformal = esym->formal;
1038
1039   for (arg = arg0; arg; arg = arg->next)
1040     {
1041       if (eformal)
1042         {
1043           if (eformal->sym && eformal->sym->attr.optional)
1044             formal_optional = true;
1045           eformal = eformal->next;
1046         }
1047       else if (isym && iformal)
1048         {
1049           if (iformal->optional)
1050             formal_optional = true;
1051           iformal = iformal->next;
1052         }
1053       else if (isym)
1054         formal_optional = true;
1055
1056       if (pedantic && arg->expr != NULL
1057             && arg->expr->expr_type == EXPR_VARIABLE
1058             && arg->expr->symtree->n.sym->attr.optional
1059             && formal_optional
1060             && arg->expr->rank
1061             && (set_by_optional || arg->expr->rank != rank)
1062             && !(isym && isym->generic_id == GFC_ISYM_CONVERSION))
1063         {
1064           gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1065                        "MISSING, it cannot be the actual argument of an "
1066                        "ELEMENTAL procedure unless there is a non-optional"
1067                        "argument with the same rank (12.4.1.5)",
1068                        arg->expr->symtree->n.sym->name, &arg->expr->where);
1069           return FAILURE;
1070         }
1071     }
1072
1073   for (arg = arg0; arg; arg = arg->next)
1074     {
1075       if (arg->expr == NULL || arg->expr->rank == 0)
1076         continue;
1077
1078       /* Being elemental, the last upper bound of an assumed size array
1079          argument must be present.  */
1080       if (resolve_assumed_size_actual (arg->expr))
1081         return FAILURE;
1082
1083       if (expr)
1084         continue;
1085
1086       /* Elemental subroutine array actual arguments must conform.  */
1087       if (e != NULL)
1088         {
1089           if (gfc_check_conformance ("elemental subroutine", arg->expr, e)
1090                 == FAILURE)
1091             return FAILURE;
1092         }
1093       else
1094         e = arg->expr;
1095     }
1096
1097   return SUCCESS;
1098 }
1099
1100
1101 /* Go through each actual argument in ACTUAL and see if it can be
1102    implemented as an inlined, non-copying intrinsic.  FNSYM is the
1103    function being called, or NULL if not known.  */
1104
1105 static void
1106 find_noncopying_intrinsics (gfc_symbol * fnsym, gfc_actual_arglist * actual)
1107 {
1108   gfc_actual_arglist *ap;
1109   gfc_expr *expr;
1110
1111   for (ap = actual; ap; ap = ap->next)
1112     if (ap->expr
1113         && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
1114         && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual))
1115       ap->expr->inline_noncopying_intrinsic = 1;
1116 }
1117
1118 /* This function does the checking of references to global procedures
1119    as defined in sections 18.1 and 14.1, respectively, of the Fortran
1120    77 and 95 standards.  It checks for a gsymbol for the name, making
1121    one if it does not already exist.  If it already exists, then the
1122    reference being resolved must correspond to the type of gsymbol.
1123    Otherwise, the new symbol is equipped with the attributes of the
1124    reference.  The corresponding code that is called in creating
1125    global entities is parse.c.  */
1126
1127 static void
1128 resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
1129 {
1130   gfc_gsymbol * gsym;
1131   unsigned int type;
1132
1133   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1134
1135   gsym = gfc_get_gsymbol (sym->name);
1136
1137   if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1138     global_used (gsym, where);
1139
1140   if (gsym->type == GSYM_UNKNOWN)
1141     {
1142       gsym->type = type;
1143       gsym->where = *where;
1144     }
1145
1146   gsym->used = 1;
1147 }
1148
1149 /************* Function resolution *************/
1150
1151 /* Resolve a function call known to be generic.
1152    Section 14.1.2.4.1.  */
1153
1154 static match
1155 resolve_generic_f0 (gfc_expr * expr, gfc_symbol * sym)
1156 {
1157   gfc_symbol *s;
1158
1159   if (sym->attr.generic)
1160     {
1161       s =
1162         gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
1163       if (s != NULL)
1164         {
1165           expr->value.function.name = s->name;
1166           expr->value.function.esym = s;
1167
1168           if (s->ts.type != BT_UNKNOWN)
1169             expr->ts = s->ts;
1170           else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1171             expr->ts = s->result->ts;
1172
1173           if (s->as != NULL)
1174             expr->rank = s->as->rank;
1175           else if (s->result != NULL && s->result->as != NULL)
1176             expr->rank = s->result->as->rank;
1177
1178           return MATCH_YES;
1179         }
1180
1181       /* TODO: Need to search for elemental references in generic interface */
1182     }
1183
1184   if (sym->attr.intrinsic)
1185     return gfc_intrinsic_func_interface (expr, 0);
1186
1187   return MATCH_NO;
1188 }
1189
1190
1191 static try
1192 resolve_generic_f (gfc_expr * expr)
1193 {
1194   gfc_symbol *sym;
1195   match m;
1196
1197   sym = expr->symtree->n.sym;
1198
1199   for (;;)
1200     {
1201       m = resolve_generic_f0 (expr, sym);
1202       if (m == MATCH_YES)
1203         return SUCCESS;
1204       else if (m == MATCH_ERROR)
1205         return FAILURE;
1206
1207 generic:
1208       if (sym->ns->parent == NULL)
1209         break;
1210       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1211
1212       if (sym == NULL)
1213         break;
1214       if (!generic_sym (sym))
1215         goto generic;
1216     }
1217
1218   /* Last ditch attempt.  */
1219
1220   if (!gfc_generic_intrinsic (expr->symtree->n.sym->name))
1221     {
1222       gfc_error ("There is no specific function for the generic '%s' at %L",
1223                  expr->symtree->n.sym->name, &expr->where);
1224       return FAILURE;
1225     }
1226
1227   m = gfc_intrinsic_func_interface (expr, 0);
1228   if (m == MATCH_YES)
1229     return SUCCESS;
1230   if (m == MATCH_NO)
1231     gfc_error
1232       ("Generic function '%s' at %L is not consistent with a specific "
1233        "intrinsic interface", expr->symtree->n.sym->name, &expr->where);
1234
1235   return FAILURE;
1236 }
1237
1238
1239 /* Resolve a function call known to be specific.  */
1240
1241 static match
1242 resolve_specific_f0 (gfc_symbol * sym, gfc_expr * expr)
1243 {
1244   match m;
1245
1246   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1247     {
1248       if (sym->attr.dummy)
1249         {
1250           sym->attr.proc = PROC_DUMMY;
1251           goto found;
1252         }
1253
1254       sym->attr.proc = PROC_EXTERNAL;
1255       goto found;
1256     }
1257
1258   if (sym->attr.proc == PROC_MODULE
1259       || sym->attr.proc == PROC_ST_FUNCTION
1260       || sym->attr.proc == PROC_INTERNAL)
1261     goto found;
1262
1263   if (sym->attr.intrinsic)
1264     {
1265       m = gfc_intrinsic_func_interface (expr, 1);
1266       if (m == MATCH_YES)
1267         return MATCH_YES;
1268       if (m == MATCH_NO)
1269         gfc_error
1270           ("Function '%s' at %L is INTRINSIC but is not compatible with "
1271            "an intrinsic", sym->name, &expr->where);
1272
1273       return MATCH_ERROR;
1274     }
1275
1276   return MATCH_NO;
1277
1278 found:
1279   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1280
1281   expr->ts = sym->ts;
1282   expr->value.function.name = sym->name;
1283   expr->value.function.esym = sym;
1284   if (sym->as != NULL)
1285     expr->rank = sym->as->rank;
1286
1287   return MATCH_YES;
1288 }
1289
1290
1291 static try
1292 resolve_specific_f (gfc_expr * expr)
1293 {
1294   gfc_symbol *sym;
1295   match m;
1296
1297   sym = expr->symtree->n.sym;
1298
1299   for (;;)
1300     {
1301       m = resolve_specific_f0 (sym, expr);
1302       if (m == MATCH_YES)
1303         return SUCCESS;
1304       if (m == MATCH_ERROR)
1305         return FAILURE;
1306
1307       if (sym->ns->parent == NULL)
1308         break;
1309
1310       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1311
1312       if (sym == NULL)
1313         break;
1314     }
1315
1316   gfc_error ("Unable to resolve the specific function '%s' at %L",
1317              expr->symtree->n.sym->name, &expr->where);
1318
1319   return SUCCESS;
1320 }
1321
1322
1323 /* Resolve a procedure call not known to be generic nor specific.  */
1324
1325 static try
1326 resolve_unknown_f (gfc_expr * expr)
1327 {
1328   gfc_symbol *sym;
1329   gfc_typespec *ts;
1330
1331   sym = expr->symtree->n.sym;
1332
1333   if (sym->attr.dummy)
1334     {
1335       sym->attr.proc = PROC_DUMMY;
1336       expr->value.function.name = sym->name;
1337       goto set_type;
1338     }
1339
1340   /* See if we have an intrinsic function reference.  */
1341
1342   if (gfc_intrinsic_name (sym->name, 0))
1343     {
1344       if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
1345         return SUCCESS;
1346       return FAILURE;
1347     }
1348
1349   /* The reference is to an external name.  */
1350
1351   sym->attr.proc = PROC_EXTERNAL;
1352   expr->value.function.name = sym->name;
1353   expr->value.function.esym = expr->symtree->n.sym;
1354
1355   if (sym->as != NULL)
1356     expr->rank = sym->as->rank;
1357
1358   /* Type of the expression is either the type of the symbol or the
1359      default type of the symbol.  */
1360
1361 set_type:
1362   gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1363
1364   if (sym->ts.type != BT_UNKNOWN)
1365     expr->ts = sym->ts;
1366   else
1367     {
1368       ts = gfc_get_default_type (sym, sym->ns);
1369
1370       if (ts->type == BT_UNKNOWN)
1371         {
1372           gfc_error ("Function '%s' at %L has no IMPLICIT type",
1373                      sym->name, &expr->where);
1374           return FAILURE;
1375         }
1376       else
1377         expr->ts = *ts;
1378     }
1379
1380   return SUCCESS;
1381 }
1382
1383
1384 /* Figure out if a function reference is pure or not.  Also set the name
1385    of the function for a potential error message.  Return nonzero if the
1386    function is PURE, zero if not.  */
1387
1388 static int
1389 pure_function (gfc_expr * e, const char **name)
1390 {
1391   int pure;
1392
1393   if (e->value.function.esym)
1394     {
1395       pure = gfc_pure (e->value.function.esym);
1396       *name = e->value.function.esym->name;
1397     }
1398   else if (e->value.function.isym)
1399     {
1400       pure = e->value.function.isym->pure
1401         || e->value.function.isym->elemental;
1402       *name = e->value.function.isym->name;
1403     }
1404   else
1405     {
1406       /* Implicit functions are not pure.  */
1407       pure = 0;
1408       *name = e->value.function.name;
1409     }
1410
1411   return pure;
1412 }
1413
1414
1415 /* Resolve a function call, which means resolving the arguments, then figuring
1416    out which entity the name refers to.  */
1417 /* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
1418    to INTENT(OUT) or INTENT(INOUT).  */
1419
1420 static try
1421 resolve_function (gfc_expr * expr)
1422 {
1423   gfc_actual_arglist *arg;
1424   gfc_symbol * sym;
1425   const char *name;
1426   try t;
1427   int temp;
1428
1429   sym = NULL;
1430   if (expr->symtree)
1431     sym = expr->symtree->n.sym;
1432
1433   /* If the procedure is not internal, a statement function or a module
1434      procedure,it must be external and should be checked for usage.  */
1435   if (sym && !sym->attr.dummy && !sym->attr.contained
1436         && sym->attr.proc != PROC_ST_FUNCTION
1437         && !sym->attr.use_assoc)
1438     resolve_global_procedure (sym, &expr->where, 0);
1439
1440   /* Switch off assumed size checking and do this again for certain kinds
1441      of procedure, once the procedure itself is resolved.  */
1442   need_full_assumed_size++;
1443
1444   if (resolve_actual_arglist (expr->value.function.actual) == FAILURE)
1445     return FAILURE;
1446
1447   /* Resume assumed_size checking. */
1448   need_full_assumed_size--;
1449
1450   if (sym && sym->ts.type == BT_CHARACTER
1451         && sym->ts.cl
1452         && sym->ts.cl->length == NULL
1453         && !sym->attr.dummy
1454         && expr->value.function.esym == NULL
1455         && !sym->attr.contained)
1456     {
1457       /* Internal procedures are taken care of in resolve_contained_fntype.  */
1458       gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
1459                  "be used at %L since it is not a dummy argument",
1460                  sym->name, &expr->where);
1461       return FAILURE;
1462     }
1463
1464 /* See if function is already resolved.  */
1465
1466   if (expr->value.function.name != NULL)
1467     {
1468       if (expr->ts.type == BT_UNKNOWN)
1469         expr->ts = sym->ts;
1470       t = SUCCESS;
1471     }
1472   else
1473     {
1474       /* Apply the rules of section 14.1.2.  */
1475
1476       switch (procedure_kind (sym))
1477         {
1478         case PTYPE_GENERIC:
1479           t = resolve_generic_f (expr);
1480           break;
1481
1482         case PTYPE_SPECIFIC:
1483           t = resolve_specific_f (expr);
1484           break;
1485
1486         case PTYPE_UNKNOWN:
1487           t = resolve_unknown_f (expr);
1488           break;
1489
1490         default:
1491           gfc_internal_error ("resolve_function(): bad function type");
1492         }
1493     }
1494
1495   /* If the expression is still a function (it might have simplified),
1496      then we check to see if we are calling an elemental function.  */
1497
1498   if (expr->expr_type != EXPR_FUNCTION)
1499     return t;
1500
1501   temp = need_full_assumed_size;
1502   need_full_assumed_size = 0;
1503
1504   if (resolve_elemental_actual (expr, NULL) == FAILURE)
1505     return FAILURE;
1506
1507   if (omp_workshare_flag
1508       && expr->value.function.esym
1509       && ! gfc_elemental (expr->value.function.esym))
1510     {
1511       gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed"
1512                  " in WORKSHARE construct", expr->value.function.esym->name,
1513                  &expr->where);
1514       t = FAILURE;
1515     }
1516
1517   else if (expr->value.function.actual != NULL
1518              && expr->value.function.isym != NULL
1519              && expr->value.function.isym->generic_id != GFC_ISYM_LBOUND
1520              && expr->value.function.isym->generic_id != GFC_ISYM_LOC
1521              && expr->value.function.isym->generic_id != GFC_ISYM_PRESENT)
1522     {
1523       /* Array intrinsics must also have the last upper bound of an
1524          assumed size array argument.  UBOUND and SIZE have to be
1525          excluded from the check if the second argument is anything
1526          than a constant.  */
1527       int inquiry;
1528       inquiry = expr->value.function.isym->generic_id == GFC_ISYM_UBOUND
1529                   || expr->value.function.isym->generic_id == GFC_ISYM_SIZE;
1530
1531       for (arg = expr->value.function.actual; arg; arg = arg->next)
1532         {
1533           if (inquiry && arg->next != NULL && arg->next->expr
1534                 && arg->next->expr->expr_type != EXPR_CONSTANT)
1535             break;
1536
1537           if (arg->expr != NULL
1538                 && arg->expr->rank > 0
1539                 && resolve_assumed_size_actual (arg->expr))
1540             return FAILURE;
1541         }
1542     }
1543
1544   need_full_assumed_size = temp;
1545
1546   if (!pure_function (expr, &name) && name)
1547     {
1548       if (forall_flag)
1549         {
1550           gfc_error
1551             ("reference to non-PURE function '%s' at %L inside a "
1552              "FORALL %s", name, &expr->where, forall_flag == 2 ?
1553              "mask" : "block");
1554           t = FAILURE;
1555         }
1556       else if (gfc_pure (NULL))
1557         {
1558           gfc_error ("Function reference to '%s' at %L is to a non-PURE "
1559                      "procedure within a PURE procedure", name, &expr->where);
1560           t = FAILURE;
1561         }
1562     }
1563
1564   /* Functions without the RECURSIVE attribution are not allowed to
1565    * call themselves.  */
1566   if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
1567     {
1568       gfc_symbol *esym, *proc;
1569       esym = expr->value.function.esym;
1570       proc = gfc_current_ns->proc_name;
1571       if (esym == proc)
1572       {
1573         gfc_error ("Function '%s' at %L cannot call itself, as it is not "
1574                    "RECURSIVE", name, &expr->where);
1575         t = FAILURE;
1576       }
1577
1578       if (esym->attr.entry && esym->ns->entries && proc->ns->entries
1579           && esym->ns->entries->sym == proc->ns->entries->sym)
1580       {
1581         gfc_error ("Call to ENTRY '%s' at %L is recursive, but function "
1582                    "'%s' is not declared as RECURSIVE",
1583                    esym->name, &expr->where, esym->ns->entries->sym->name);
1584         t = FAILURE;
1585       }
1586     }
1587
1588   /* Character lengths of use associated functions may contains references to
1589      symbols not referenced from the current program unit otherwise.  Make sure
1590      those symbols are marked as referenced.  */
1591
1592   if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
1593       && expr->value.function.esym->attr.use_assoc)
1594     {
1595       gfc_expr_set_symbols_referenced (expr->ts.cl->length);
1596     }
1597
1598   if (t == SUCCESS)
1599     find_noncopying_intrinsics (expr->value.function.esym,
1600                                 expr->value.function.actual);
1601   return t;
1602 }
1603
1604
1605 /************* Subroutine resolution *************/
1606
1607 static void
1608 pure_subroutine (gfc_code * c, gfc_symbol * sym)
1609 {
1610
1611   if (gfc_pure (sym))
1612     return;
1613
1614   if (forall_flag)
1615     gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
1616                sym->name, &c->loc);
1617   else if (gfc_pure (NULL))
1618     gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
1619                &c->loc);
1620 }
1621
1622
1623 static match
1624 resolve_generic_s0 (gfc_code * c, gfc_symbol * sym)
1625 {
1626   gfc_symbol *s;
1627
1628   if (sym->attr.generic)
1629     {
1630       s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
1631       if (s != NULL)
1632         {
1633           c->resolved_sym = s;
1634           pure_subroutine (c, s);
1635           return MATCH_YES;
1636         }
1637
1638       /* TODO: Need to search for elemental references in generic interface.  */
1639     }
1640
1641   if (sym->attr.intrinsic)
1642     return gfc_intrinsic_sub_interface (c, 0);
1643
1644   return MATCH_NO;
1645 }
1646
1647
1648 static try
1649 resolve_generic_s (gfc_code * c)
1650 {
1651   gfc_symbol *sym;
1652   match m;
1653
1654   sym = c->symtree->n.sym;
1655
1656   for (;;)
1657     {
1658       m = resolve_generic_s0 (c, sym);
1659       if (m == MATCH_YES)
1660         return SUCCESS;
1661       else if (m == MATCH_ERROR)
1662         return FAILURE;
1663
1664 generic:
1665       if (sym->ns->parent == NULL)
1666         break;
1667       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1668
1669       if (sym == NULL)
1670         break;
1671       if (!generic_sym (sym))
1672         goto generic;
1673     }
1674
1675   /* Last ditch attempt.  */
1676   sym = c->symtree->n.sym;
1677   if (!gfc_generic_intrinsic (sym->name))
1678     {
1679       gfc_error
1680         ("There is no specific subroutine for the generic '%s' at %L",
1681          sym->name, &c->loc);
1682       return FAILURE;
1683     }
1684
1685   m = gfc_intrinsic_sub_interface (c, 0);
1686   if (m == MATCH_YES)
1687     return SUCCESS;
1688   if (m == MATCH_NO)
1689     gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
1690                "intrinsic subroutine interface", sym->name, &c->loc);
1691
1692   return FAILURE;
1693 }
1694
1695
1696 /* Resolve a subroutine call known to be specific.  */
1697
1698 static match
1699 resolve_specific_s0 (gfc_code * c, gfc_symbol * sym)
1700 {
1701   match m;
1702
1703   if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1704     {
1705       if (sym->attr.dummy)
1706         {
1707           sym->attr.proc = PROC_DUMMY;
1708           goto found;
1709         }
1710
1711       sym->attr.proc = PROC_EXTERNAL;
1712       goto found;
1713     }
1714
1715   if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
1716     goto found;
1717
1718   if (sym->attr.intrinsic)
1719     {
1720       m = gfc_intrinsic_sub_interface (c, 1);
1721       if (m == MATCH_YES)
1722         return MATCH_YES;
1723       if (m == MATCH_NO)
1724         gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
1725                    "with an intrinsic", sym->name, &c->loc);
1726
1727       return MATCH_ERROR;
1728     }
1729
1730   return MATCH_NO;
1731
1732 found:
1733   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1734
1735   c->resolved_sym = sym;
1736   pure_subroutine (c, sym);
1737
1738   return MATCH_YES;
1739 }
1740
1741
1742 static try
1743 resolve_specific_s (gfc_code * c)
1744 {
1745   gfc_symbol *sym;
1746   match m;
1747
1748   sym = c->symtree->n.sym;
1749
1750   for (;;)
1751     {
1752       m = resolve_specific_s0 (c, sym);
1753       if (m == MATCH_YES)
1754         return SUCCESS;
1755       if (m == MATCH_ERROR)
1756         return FAILURE;
1757
1758       if (sym->ns->parent == NULL)
1759         break;
1760
1761       gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1762
1763       if (sym == NULL)
1764         break;
1765     }
1766
1767   sym = c->symtree->n.sym;
1768   gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
1769              sym->name, &c->loc);
1770
1771   return FAILURE;
1772 }
1773
1774
1775 /* Resolve a subroutine call not known to be generic nor specific.  */
1776
1777 static try
1778 resolve_unknown_s (gfc_code * c)
1779 {
1780   gfc_symbol *sym;
1781
1782   sym = c->symtree->n.sym;
1783
1784   if (sym->attr.dummy)
1785     {
1786       sym->attr.proc = PROC_DUMMY;
1787       goto found;
1788     }
1789
1790   /* See if we have an intrinsic function reference.  */
1791
1792   if (gfc_intrinsic_name (sym->name, 1))
1793     {
1794       if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
1795         return SUCCESS;
1796       return FAILURE;
1797     }
1798
1799   /* The reference is to an external name.  */
1800
1801 found:
1802   gfc_procedure_use (sym, &c->ext.actual, &c->loc);
1803
1804   c->resolved_sym = sym;
1805
1806   pure_subroutine (c, sym);
1807
1808   return SUCCESS;
1809 }
1810
1811
1812 /* Resolve a subroutine call.  Although it was tempting to use the same code
1813    for functions, subroutines and functions are stored differently and this
1814    makes things awkward.  */
1815
1816 static try
1817 resolve_call (gfc_code * c)
1818 {
1819   try t;
1820
1821   if (c->symtree && c->symtree->n.sym
1822         && c->symtree->n.sym->ts.type != BT_UNKNOWN)
1823     {
1824       gfc_error ("'%s' at %L has a type, which is not consistent with "
1825                  "the CALL at %L", c->symtree->n.sym->name,
1826                  &c->symtree->n.sym->declared_at, &c->loc);
1827       return FAILURE;
1828     }
1829
1830   /* If the procedure is not internal or module, it must be external and
1831      should be checked for usage.  */
1832   if (c->symtree && c->symtree->n.sym
1833         && !c->symtree->n.sym->attr.dummy
1834         && !c->symtree->n.sym->attr.contained
1835         && !c->symtree->n.sym->attr.use_assoc)
1836     resolve_global_procedure (c->symtree->n.sym, &c->loc, 1);
1837
1838   /* Subroutines without the RECURSIVE attribution are not allowed to
1839    * call themselves.  */
1840   if (c->symtree && c->symtree->n.sym && !c->symtree->n.sym->attr.recursive)
1841     {
1842       gfc_symbol *csym, *proc;
1843       csym = c->symtree->n.sym;
1844       proc = gfc_current_ns->proc_name;
1845       if (csym == proc)
1846       {
1847         gfc_error ("SUBROUTINE '%s' at %L cannot call itself, as it is not "
1848                    "RECURSIVE", csym->name, &c->loc);
1849         t = FAILURE;
1850       }
1851
1852       if (csym->attr.entry && csym->ns->entries && proc->ns->entries
1853           && csym->ns->entries->sym == proc->ns->entries->sym)
1854       {
1855         gfc_error ("Call to ENTRY '%s' at %L is recursive, but subroutine "
1856                    "'%s' is not declared as RECURSIVE",
1857                    csym->name, &c->loc, csym->ns->entries->sym->name);
1858         t = FAILURE;
1859       }
1860     }
1861
1862   /* Switch off assumed size checking and do this again for certain kinds
1863      of procedure, once the procedure itself is resolved.  */
1864   need_full_assumed_size++;
1865
1866   if (resolve_actual_arglist (c->ext.actual) == FAILURE)
1867     return FAILURE;
1868
1869   /* Resume assumed_size checking. */
1870   need_full_assumed_size--;
1871
1872
1873   t = SUCCESS;
1874   if (c->resolved_sym == NULL)
1875     switch (procedure_kind (c->symtree->n.sym))
1876       {
1877       case PTYPE_GENERIC:
1878         t = resolve_generic_s (c);
1879         break;
1880
1881       case PTYPE_SPECIFIC:
1882         t = resolve_specific_s (c);
1883         break;
1884
1885       case PTYPE_UNKNOWN:
1886         t = resolve_unknown_s (c);
1887         break;
1888
1889       default:
1890         gfc_internal_error ("resolve_subroutine(): bad function type");
1891       }
1892
1893   /* Some checks of elemental subroutine actual arguments.  */
1894   if (resolve_elemental_actual (NULL, c) == FAILURE)
1895     return FAILURE;
1896
1897   if (t == SUCCESS)
1898     find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
1899   return t;
1900 }
1901
1902 /* Compare the shapes of two arrays that have non-NULL shapes.  If both
1903    op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
1904    match.  If both op1->shape and op2->shape are non-NULL return FAILURE
1905    if their shapes do not match.  If either op1->shape or op2->shape is
1906    NULL, return SUCCESS.  */
1907
1908 static try
1909 compare_shapes (gfc_expr * op1, gfc_expr * op2)
1910 {
1911   try t;
1912   int i;
1913
1914   t = SUCCESS;
1915
1916   if (op1->shape != NULL && op2->shape != NULL)
1917     {
1918       for (i = 0; i < op1->rank; i++)
1919         {
1920           if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
1921            {
1922              gfc_error ("Shapes for operands at %L and %L are not conformable",
1923                          &op1->where, &op2->where);
1924              t = FAILURE;
1925              break;
1926            }
1927         }
1928     }
1929
1930   return t;
1931 }
1932
1933 /* Resolve an operator expression node.  This can involve replacing the
1934    operation with a user defined function call.  */
1935
1936 static try
1937 resolve_operator (gfc_expr * e)
1938 {
1939   gfc_expr *op1, *op2;
1940   char msg[200];
1941   try t;
1942
1943   /* Resolve all subnodes-- give them types.  */
1944
1945   switch (e->value.op.operator)
1946     {
1947     default:
1948       if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
1949         return FAILURE;
1950
1951     /* Fall through...  */
1952
1953     case INTRINSIC_NOT:
1954     case INTRINSIC_UPLUS:
1955     case INTRINSIC_UMINUS:
1956     case INTRINSIC_PARENTHESES:
1957       if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
1958         return FAILURE;
1959       break;
1960     }
1961
1962   /* Typecheck the new node.  */
1963
1964   op1 = e->value.op.op1;
1965   op2 = e->value.op.op2;
1966
1967   switch (e->value.op.operator)
1968     {
1969     case INTRINSIC_UPLUS:
1970     case INTRINSIC_UMINUS:
1971       if (op1->ts.type == BT_INTEGER
1972           || op1->ts.type == BT_REAL
1973           || op1->ts.type == BT_COMPLEX)
1974         {
1975           e->ts = op1->ts;
1976           break;
1977         }
1978
1979       sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
1980                gfc_op2string (e->value.op.operator), gfc_typename (&e->ts));
1981       goto bad_op;
1982
1983     case INTRINSIC_PLUS:
1984     case INTRINSIC_MINUS:
1985     case INTRINSIC_TIMES:
1986     case INTRINSIC_DIVIDE:
1987     case INTRINSIC_POWER:
1988       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
1989         {
1990           gfc_type_convert_binary (e);
1991           break;
1992         }
1993
1994       sprintf (msg,
1995                _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
1996                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
1997                gfc_typename (&op2->ts));
1998       goto bad_op;
1999
2000     case INTRINSIC_CONCAT:
2001       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2002         {
2003           e->ts.type = BT_CHARACTER;
2004           e->ts.kind = op1->ts.kind;
2005           break;
2006         }
2007
2008       sprintf (msg,
2009                _("Operands of string concatenation operator at %%L are %s/%s"),
2010                gfc_typename (&op1->ts), gfc_typename (&op2->ts));
2011       goto bad_op;
2012
2013     case INTRINSIC_AND:
2014     case INTRINSIC_OR:
2015     case INTRINSIC_EQV:
2016     case INTRINSIC_NEQV:
2017       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2018         {
2019           e->ts.type = BT_LOGICAL;
2020           e->ts.kind = gfc_kind_max (op1, op2);
2021           if (op1->ts.kind < e->ts.kind)
2022             gfc_convert_type (op1, &e->ts, 2);
2023           else if (op2->ts.kind < e->ts.kind)
2024             gfc_convert_type (op2, &e->ts, 2);
2025           break;
2026         }
2027
2028       sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
2029                gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2030                gfc_typename (&op2->ts));
2031
2032       goto bad_op;
2033
2034     case INTRINSIC_NOT:
2035       if (op1->ts.type == BT_LOGICAL)
2036         {
2037           e->ts.type = BT_LOGICAL;
2038           e->ts.kind = op1->ts.kind;
2039           break;
2040         }
2041
2042       sprintf (msg, _("Operand of .NOT. operator at %%L is %s"),
2043                gfc_typename (&op1->ts));
2044       goto bad_op;
2045
2046     case INTRINSIC_GT:
2047     case INTRINSIC_GE:
2048     case INTRINSIC_LT:
2049     case INTRINSIC_LE:
2050       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2051         {
2052           strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
2053           goto bad_op;
2054         }
2055
2056       /* Fall through...  */
2057
2058     case INTRINSIC_EQ:
2059     case INTRINSIC_NE:
2060       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2061         {
2062           e->ts.type = BT_LOGICAL;
2063           e->ts.kind = gfc_default_logical_kind;
2064           break;
2065         }
2066
2067       if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
2068         {
2069           gfc_type_convert_binary (e);
2070
2071           e->ts.type = BT_LOGICAL;
2072           e->ts.kind = gfc_default_logical_kind;
2073           break;
2074         }
2075
2076       if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
2077         sprintf (msg,
2078                  _("Logicals at %%L must be compared with %s instead of %s"),
2079                  e->value.op.operator == INTRINSIC_EQ ? ".EQV." : ".NEQV.",
2080                  gfc_op2string (e->value.op.operator));
2081       else
2082         sprintf (msg,
2083                  _("Operands of comparison operator '%s' at %%L are %s/%s"),
2084                  gfc_op2string (e->value.op.operator), gfc_typename (&op1->ts),
2085                  gfc_typename (&op2->ts));
2086
2087       goto bad_op;
2088
2089     case INTRINSIC_USER:
2090       if (op2 == NULL)
2091         sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
2092                  e->value.op.uop->name, gfc_typename (&op1->ts));
2093       else
2094         sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
2095                  e->value.op.uop->name, gfc_typename (&op1->ts),
2096                  gfc_typename (&op2->ts));
2097
2098       goto bad_op;
2099
2100     case INTRINSIC_PARENTHESES:
2101       break;
2102
2103     default:
2104       gfc_internal_error ("resolve_operator(): Bad intrinsic");
2105     }
2106
2107   /* Deal with arrayness of an operand through an operator.  */
2108
2109   t = SUCCESS;
2110
2111   switch (e->value.op.operator)
2112     {
2113     case INTRINSIC_PLUS:
2114     case INTRINSIC_MINUS:
2115     case INTRINSIC_TIMES:
2116     case INTRINSIC_DIVIDE:
2117     case INTRINSIC_POWER:
2118     case INTRINSIC_CONCAT:
2119     case INTRINSIC_AND:
2120     case INTRINSIC_OR:
2121     case INTRINSIC_EQV:
2122     case INTRINSIC_NEQV:
2123     case INTRINSIC_EQ:
2124     case INTRINSIC_NE:
2125     case INTRINSIC_GT:
2126     case INTRINSIC_GE:
2127     case INTRINSIC_LT:
2128     case INTRINSIC_LE:
2129
2130       if (op1->rank == 0 && op2->rank == 0)
2131         e->rank = 0;
2132
2133       if (op1->rank == 0 && op2->rank != 0)
2134         {
2135           e->rank = op2->rank;
2136
2137           if (e->shape == NULL)
2138             e->shape = gfc_copy_shape (op2->shape, op2->rank);
2139         }
2140
2141       if (op1->rank != 0 && op2->rank == 0)
2142         {
2143           e->rank = op1->rank;
2144
2145           if (e->shape == NULL)
2146             e->shape = gfc_copy_shape (op1->shape, op1->rank);
2147         }
2148
2149       if (op1->rank != 0 && op2->rank != 0)
2150         {
2151           if (op1->rank == op2->rank)
2152             {
2153               e->rank = op1->rank;
2154               if (e->shape == NULL)
2155                 {
2156                   t = compare_shapes(op1, op2);
2157                   if (t == FAILURE)
2158                     e->shape = NULL;
2159                   else
2160                 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2161                 }
2162             }
2163           else
2164             {
2165               gfc_error ("Inconsistent ranks for operator at %L and %L",
2166                          &op1->where, &op2->where);
2167               t = FAILURE;
2168
2169               /* Allow higher level expressions to work.  */
2170               e->rank = 0;
2171             }
2172         }
2173
2174       break;
2175
2176     case INTRINSIC_NOT:
2177     case INTRINSIC_UPLUS:
2178     case INTRINSIC_UMINUS:
2179     case INTRINSIC_PARENTHESES:
2180       e->rank = op1->rank;
2181
2182       if (e->shape == NULL)
2183         e->shape = gfc_copy_shape (op1->shape, op1->rank);
2184
2185       /* Simply copy arrayness attribute */
2186       break;
2187
2188     default:
2189       break;
2190     }
2191
2192   /* Attempt to simplify the expression.  */
2193   if (t == SUCCESS)
2194     t = gfc_simplify_expr (e, 0);
2195   return t;
2196
2197 bad_op:
2198
2199   if (gfc_extend_expr (e) == SUCCESS)
2200     return SUCCESS;
2201
2202   gfc_error (msg, &e->where);
2203
2204   return FAILURE;
2205 }
2206
2207
2208 /************** Array resolution subroutines **************/
2209
2210
2211 typedef enum
2212 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
2213 comparison;
2214
2215 /* Compare two integer expressions.  */
2216
2217 static comparison
2218 compare_bound (gfc_expr * a, gfc_expr * b)
2219 {
2220   int i;
2221
2222   if (a == NULL || a->expr_type != EXPR_CONSTANT
2223       || b == NULL || b->expr_type != EXPR_CONSTANT)
2224     return CMP_UNKNOWN;
2225
2226   if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
2227     gfc_internal_error ("compare_bound(): Bad expression");
2228
2229   i = mpz_cmp (a->value.integer, b->value.integer);
2230
2231   if (i < 0)
2232     return CMP_LT;
2233   if (i > 0)
2234     return CMP_GT;
2235   return CMP_EQ;
2236 }
2237
2238
2239 /* Compare an integer expression with an integer.  */
2240
2241 static comparison
2242 compare_bound_int (gfc_expr * a, int b)
2243 {
2244   int i;
2245
2246   if (a == NULL || a->expr_type != EXPR_CONSTANT)
2247     return CMP_UNKNOWN;
2248
2249   if (a->ts.type != BT_INTEGER)
2250     gfc_internal_error ("compare_bound_int(): Bad expression");
2251
2252   i = mpz_cmp_si (a->value.integer, b);
2253
2254   if (i < 0)
2255     return CMP_LT;
2256   if (i > 0)
2257     return CMP_GT;
2258   return CMP_EQ;
2259 }
2260
2261
2262 /* Compare an integer expression with a mpz_t.  */
2263
2264 static comparison
2265 compare_bound_mpz_t (gfc_expr * a, mpz_t b)
2266 {
2267   int i;
2268
2269   if (a == NULL || a->expr_type != EXPR_CONSTANT)
2270     return CMP_UNKNOWN;
2271
2272   if (a->ts.type != BT_INTEGER)
2273     gfc_internal_error ("compare_bound_int(): Bad expression");
2274
2275   i = mpz_cmp (a->value.integer, b);
2276
2277   if (i < 0)
2278     return CMP_LT;
2279   if (i > 0)
2280     return CMP_GT;
2281   return CMP_EQ;
2282 }
2283
2284
2285 /* Compute the last value of a sequence given by a triplet.  
2286    Return 0 if it wasn't able to compute the last value, or if the
2287    sequence if empty, and 1 otherwise.  */
2288
2289 static int
2290 compute_last_value_for_triplet (gfc_expr * start, gfc_expr * end,
2291                                 gfc_expr * stride, mpz_t last)
2292 {
2293   mpz_t rem;
2294
2295   if (start == NULL || start->expr_type != EXPR_CONSTANT
2296       || end == NULL || end->expr_type != EXPR_CONSTANT
2297       || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
2298     return 0;
2299
2300   if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
2301       || (stride != NULL && stride->ts.type != BT_INTEGER))
2302     return 0;
2303
2304   if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
2305     {
2306       if (compare_bound (start, end) == CMP_GT)
2307         return 0;
2308       mpz_set (last, end->value.integer);
2309       return 1;
2310     }
2311
2312   if (compare_bound_int (stride, 0) == CMP_GT)
2313     {
2314       /* Stride is positive */
2315       if (mpz_cmp (start->value.integer, end->value.integer) > 0)
2316         return 0;
2317     }
2318   else
2319     {
2320       /* Stride is negative */
2321       if (mpz_cmp (start->value.integer, end->value.integer) < 0)
2322         return 0;
2323     }
2324
2325   mpz_init (rem);
2326   mpz_sub (rem, end->value.integer, start->value.integer);
2327   mpz_tdiv_r (rem, rem, stride->value.integer);
2328   mpz_sub (last, end->value.integer, rem);
2329   mpz_clear (rem);
2330
2331   return 1;
2332 }
2333
2334
2335 /* Compare a single dimension of an array reference to the array
2336    specification.  */
2337
2338 static try
2339 check_dimension (int i, gfc_array_ref * ar, gfc_array_spec * as)
2340 {
2341   mpz_t last_value;
2342
2343 /* Given start, end and stride values, calculate the minimum and
2344    maximum referenced indexes.  */
2345
2346   switch (ar->type)
2347     {
2348     case AR_FULL:
2349       break;
2350
2351     case AR_ELEMENT:
2352       if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
2353         goto bound;
2354       if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
2355         goto bound;
2356
2357       break;
2358
2359     case AR_SECTION:
2360       if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
2361         {
2362           gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
2363           return FAILURE;
2364         }
2365
2366 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
2367 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
2368
2369       if (compare_bound (AR_START, AR_END) == CMP_EQ
2370           && (compare_bound (AR_START, as->lower[i]) == CMP_LT
2371               || compare_bound (AR_START, as->upper[i]) == CMP_GT))
2372         goto bound;
2373
2374       if (((compare_bound_int (ar->stride[i], 0) == CMP_GT
2375             || ar->stride[i] == NULL)
2376            && compare_bound (AR_START, AR_END) != CMP_GT)
2377           || (compare_bound_int (ar->stride[i], 0) == CMP_LT
2378               && compare_bound (AR_START, AR_END) != CMP_LT))
2379         {
2380           if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
2381             goto bound;
2382           if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
2383             goto bound;
2384         }
2385
2386       mpz_init (last_value);
2387       if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
2388                                           last_value))
2389         {
2390           if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT
2391               || compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
2392             {
2393               mpz_clear (last_value);
2394               goto bound;
2395             }
2396         }
2397       mpz_clear (last_value);
2398
2399 #undef AR_START
2400 #undef AR_END
2401
2402       break;
2403
2404     default:
2405       gfc_internal_error ("check_dimension(): Bad array reference");
2406     }
2407
2408   return SUCCESS;
2409
2410 bound:
2411   gfc_warning ("Array reference at %L is out of bounds", &ar->c_where[i]);
2412   return SUCCESS;
2413 }
2414
2415
2416 /* Compare an array reference with an array specification.  */
2417
2418 static try
2419 compare_spec_to_ref (gfc_array_ref * ar)
2420 {
2421   gfc_array_spec *as;
2422   int i;
2423
2424   as = ar->as;
2425   i = as->rank - 1;
2426   /* TODO: Full array sections are only allowed as actual parameters.  */
2427   if (as->type == AS_ASSUMED_SIZE
2428       && (/*ar->type == AR_FULL
2429           ||*/ (ar->type == AR_SECTION
2430               && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
2431     {
2432       gfc_error ("Rightmost upper bound of assumed size array section"
2433                  " not specified at %L", &ar->where);
2434       return FAILURE;
2435     }
2436
2437   if (ar->type == AR_FULL)
2438     return SUCCESS;
2439
2440   if (as->rank != ar->dimen)
2441     {
2442       gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
2443                  &ar->where, ar->dimen, as->rank);
2444       return FAILURE;
2445     }
2446
2447   for (i = 0; i < as->rank; i++)
2448     if (check_dimension (i, ar, as) == FAILURE)
2449       return FAILURE;
2450
2451   return SUCCESS;
2452 }
2453
2454
2455 /* Resolve one part of an array index.  */
2456
2457 try
2458 gfc_resolve_index (gfc_expr * index, int check_scalar)
2459 {
2460   gfc_typespec ts;
2461
2462   if (index == NULL)
2463     return SUCCESS;
2464
2465   if (gfc_resolve_expr (index) == FAILURE)
2466     return FAILURE;
2467
2468   if (check_scalar && index->rank != 0)
2469     {
2470       gfc_error ("Array index at %L must be scalar", &index->where);
2471       return FAILURE;
2472     }
2473
2474   if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
2475     {
2476       gfc_error ("Array index at %L must be of INTEGER type",
2477                  &index->where);
2478       return FAILURE;
2479     }
2480
2481   if (index->ts.type == BT_REAL)
2482     if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
2483                         &index->where) == FAILURE)
2484       return FAILURE;
2485
2486   if (index->ts.kind != gfc_index_integer_kind
2487       || index->ts.type != BT_INTEGER)
2488     {
2489       gfc_clear_ts (&ts);
2490       ts.type = BT_INTEGER;
2491       ts.kind = gfc_index_integer_kind;
2492
2493       gfc_convert_type_warn (index, &ts, 2, 0);
2494     }
2495
2496   return SUCCESS;
2497 }
2498
2499 /* Resolve a dim argument to an intrinsic function.  */
2500
2501 try
2502 gfc_resolve_dim_arg (gfc_expr *dim)
2503 {
2504   if (dim == NULL)
2505     return SUCCESS;
2506
2507   if (gfc_resolve_expr (dim) == FAILURE)
2508     return FAILURE;
2509
2510   if (dim->rank != 0)
2511     {
2512       gfc_error ("Argument dim at %L must be scalar", &dim->where);
2513       return FAILURE;
2514
2515     }
2516   if (dim->ts.type != BT_INTEGER)
2517     {
2518       gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
2519       return FAILURE;
2520     }
2521   if (dim->ts.kind != gfc_index_integer_kind)
2522     {
2523       gfc_typespec ts;
2524
2525       ts.type = BT_INTEGER;
2526       ts.kind = gfc_index_integer_kind;
2527
2528       gfc_convert_type_warn (dim, &ts, 2, 0);
2529     }
2530
2531   return SUCCESS;
2532 }
2533
2534 /* Given an expression that contains array references, update those array
2535    references to point to the right array specifications.  While this is
2536    filled in during matching, this information is difficult to save and load
2537    in a module, so we take care of it here.
2538
2539    The idea here is that the original array reference comes from the
2540    base symbol.  We traverse the list of reference structures, setting
2541    the stored reference to references.  Component references can
2542    provide an additional array specification.  */
2543
2544 static void
2545 find_array_spec (gfc_expr * e)
2546 {
2547   gfc_array_spec *as;
2548   gfc_component *c;
2549   gfc_symbol *derived;
2550   gfc_ref *ref;
2551
2552   as = e->symtree->n.sym->as;
2553   derived = NULL;
2554
2555   for (ref = e->ref; ref; ref = ref->next)
2556     switch (ref->type)
2557       {
2558       case REF_ARRAY:
2559         if (as == NULL)
2560           gfc_internal_error ("find_array_spec(): Missing spec");
2561
2562         ref->u.ar.as = as;
2563         as = NULL;
2564         break;
2565
2566       case REF_COMPONENT:
2567         if (derived == NULL)
2568           derived = e->symtree->n.sym->ts.derived;
2569
2570         c = derived->components;
2571
2572         for (; c; c = c->next)
2573           if (c == ref->u.c.component)
2574             {
2575               /* Track the sequence of component references.  */
2576               if (c->ts.type == BT_DERIVED)
2577                 derived = c->ts.derived;
2578               break;
2579             }
2580
2581         if (c == NULL)
2582           gfc_internal_error ("find_array_spec(): Component not found");
2583
2584         if (c->dimension)
2585           {
2586             if (as != NULL)
2587               gfc_internal_error ("find_array_spec(): unused as(1)");
2588             as = c->as;
2589           }
2590
2591         break;
2592
2593       case REF_SUBSTRING:
2594         break;
2595       }
2596
2597   if (as != NULL)
2598     gfc_internal_error ("find_array_spec(): unused as(2)");
2599 }
2600
2601
2602 /* Resolve an array reference.  */
2603
2604 static try
2605 resolve_array_ref (gfc_array_ref * ar)
2606 {
2607   int i, check_scalar;
2608   gfc_expr *e;
2609
2610   for (i = 0; i < ar->dimen; i++)
2611     {
2612       check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
2613
2614       if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
2615         return FAILURE;
2616       if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
2617         return FAILURE;
2618       if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
2619         return FAILURE;
2620
2621       e = ar->start[i];
2622
2623       if (ar->dimen_type[i] == DIMEN_UNKNOWN)
2624         switch (e->rank)
2625           {
2626           case 0:
2627             ar->dimen_type[i] = DIMEN_ELEMENT;
2628             break;
2629
2630           case 1:
2631             ar->dimen_type[i] = DIMEN_VECTOR;
2632             if (e->expr_type == EXPR_VARIABLE
2633                    && e->symtree->n.sym->ts.type == BT_DERIVED)
2634               ar->start[i] = gfc_get_parentheses (e);
2635             break;
2636
2637           default:
2638             gfc_error ("Array index at %L is an array of rank %d",
2639                        &ar->c_where[i], e->rank);
2640             return FAILURE;
2641           }
2642     }
2643
2644   /* If the reference type is unknown, figure out what kind it is.  */
2645
2646   if (ar->type == AR_UNKNOWN)
2647     {
2648       ar->type = AR_ELEMENT;
2649       for (i = 0; i < ar->dimen; i++)
2650         if (ar->dimen_type[i] == DIMEN_RANGE
2651             || ar->dimen_type[i] == DIMEN_VECTOR)
2652           {
2653             ar->type = AR_SECTION;
2654             break;
2655           }
2656     }
2657
2658   if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
2659     return FAILURE;
2660
2661   return SUCCESS;
2662 }
2663
2664
2665 static try
2666 resolve_substring (gfc_ref * ref)
2667 {
2668
2669   if (ref->u.ss.start != NULL)
2670     {
2671       if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
2672         return FAILURE;
2673
2674       if (ref->u.ss.start->ts.type != BT_INTEGER)
2675         {
2676           gfc_error ("Substring start index at %L must be of type INTEGER",
2677                      &ref->u.ss.start->where);
2678           return FAILURE;
2679         }
2680
2681       if (ref->u.ss.start->rank != 0)
2682         {
2683           gfc_error ("Substring start index at %L must be scalar",
2684                      &ref->u.ss.start->where);
2685           return FAILURE;
2686         }
2687
2688       if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
2689           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
2690               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
2691         {
2692           gfc_error ("Substring start index at %L is less than one",
2693                      &ref->u.ss.start->where);
2694           return FAILURE;
2695         }
2696     }
2697
2698   if (ref->u.ss.end != NULL)
2699     {
2700       if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
2701         return FAILURE;
2702
2703       if (ref->u.ss.end->ts.type != BT_INTEGER)
2704         {
2705           gfc_error ("Substring end index at %L must be of type INTEGER",
2706                      &ref->u.ss.end->where);
2707           return FAILURE;
2708         }
2709
2710       if (ref->u.ss.end->rank != 0)
2711         {
2712           gfc_error ("Substring end index at %L must be scalar",
2713                      &ref->u.ss.end->where);
2714           return FAILURE;
2715         }
2716
2717       if (ref->u.ss.length != NULL
2718           && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
2719           && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
2720               || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
2721         {
2722           gfc_error ("Substring end index at %L exceeds the string length",
2723                      &ref->u.ss.start->where);
2724           return FAILURE;
2725         }
2726     }
2727
2728   return SUCCESS;
2729 }
2730
2731
2732 /* Resolve subtype references.  */
2733
2734 static try
2735 resolve_ref (gfc_expr * expr)
2736 {
2737   int current_part_dimension, n_components, seen_part_dimension;
2738   gfc_ref *ref;
2739
2740   for (ref = expr->ref; ref; ref = ref->next)
2741     if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
2742       {
2743         find_array_spec (expr);
2744         break;
2745       }
2746
2747   for (ref = expr->ref; ref; ref = ref->next)
2748     switch (ref->type)
2749       {
2750       case REF_ARRAY:
2751         if (resolve_array_ref (&ref->u.ar) == FAILURE)
2752           return FAILURE;
2753         break;
2754
2755       case REF_COMPONENT:
2756         break;
2757
2758       case REF_SUBSTRING:
2759         resolve_substring (ref);
2760         break;
2761       }
2762
2763   /* Check constraints on part references.  */
2764
2765   current_part_dimension = 0;
2766   seen_part_dimension = 0;
2767   n_components = 0;
2768
2769   for (ref = expr->ref; ref; ref = ref->next)
2770     {
2771       switch (ref->type)
2772         {
2773         case REF_ARRAY:
2774           switch (ref->u.ar.type)
2775             {
2776             case AR_FULL:
2777             case AR_SECTION:
2778               current_part_dimension = 1;
2779               break;
2780
2781             case AR_ELEMENT:
2782               current_part_dimension = 0;
2783               break;
2784
2785             case AR_UNKNOWN:
2786               gfc_internal_error ("resolve_ref(): Bad array reference");
2787             }
2788
2789           break;
2790
2791         case REF_COMPONENT:
2792           if (current_part_dimension || seen_part_dimension)
2793             {
2794               if (ref->u.c.component->pointer)
2795                 {
2796                   gfc_error
2797                     ("Component to the right of a part reference with nonzero "
2798                      "rank must not have the POINTER attribute at %L",
2799                      &expr->where);
2800                   return FAILURE;
2801                 }
2802               else if (ref->u.c.component->allocatable)
2803                 {
2804                   gfc_error
2805                     ("Component to the right of a part reference with nonzero "
2806                      "rank must not have the ALLOCATABLE attribute at %L",
2807                      &expr->where);
2808                   return FAILURE;
2809                 }
2810             }
2811
2812           n_components++;
2813           break;
2814
2815         case REF_SUBSTRING:
2816           break;
2817         }
2818
2819       if (((ref->type == REF_COMPONENT && n_components > 1)
2820            || ref->next == NULL)
2821           && current_part_dimension
2822           && seen_part_dimension)
2823         {
2824
2825           gfc_error ("Two or more part references with nonzero rank must "
2826                      "not be specified at %L", &expr->where);
2827           return FAILURE;
2828         }
2829
2830       if (ref->type == REF_COMPONENT)
2831         {
2832           if (current_part_dimension)
2833             seen_part_dimension = 1;
2834
2835           /* reset to make sure */
2836           current_part_dimension = 0;
2837         }
2838     }
2839
2840   return SUCCESS;
2841 }
2842
2843
2844 /* Given an expression, determine its shape.  This is easier than it sounds.
2845    Leaves the shape array NULL if it is not possible to determine the shape.  */
2846
2847 static void
2848 expression_shape (gfc_expr * e)
2849 {
2850   mpz_t array[GFC_MAX_DIMENSIONS];
2851   int i;
2852
2853   if (e->rank == 0 || e->shape != NULL)
2854     return;
2855
2856   for (i = 0; i < e->rank; i++)
2857     if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
2858       goto fail;
2859
2860   e->shape = gfc_get_shape (e->rank);
2861
2862   memcpy (e->shape, array, e->rank * sizeof (mpz_t));
2863
2864   return;
2865
2866 fail:
2867   for (i--; i >= 0; i--)
2868     mpz_clear (array[i]);
2869 }
2870
2871
2872 /* Given a variable expression node, compute the rank of the expression by
2873    examining the base symbol and any reference structures it may have.  */
2874
2875 static void
2876 expression_rank (gfc_expr * e)
2877 {
2878   gfc_ref *ref;
2879   int i, rank;
2880
2881   if (e->ref == NULL)
2882     {
2883       if (e->expr_type == EXPR_ARRAY)
2884         goto done;
2885       /* Constructors can have a rank different from one via RESHAPE().  */
2886
2887       if (e->symtree == NULL)
2888         {
2889           e->rank = 0;
2890           goto done;
2891         }
2892
2893       e->rank = (e->symtree->n.sym->as == NULL)
2894                   ? 0 : e->symtree->n.sym->as->rank;
2895       goto done;
2896     }
2897
2898   rank = 0;
2899
2900   for (ref = e->ref; ref; ref = ref->next)
2901     {
2902       if (ref->type != REF_ARRAY)
2903         continue;
2904
2905       if (ref->u.ar.type == AR_FULL)
2906         {
2907           rank = ref->u.ar.as->rank;
2908           break;
2909         }
2910
2911       if (ref->u.ar.type == AR_SECTION)
2912         {
2913           /* Figure out the rank of the section.  */
2914           if (rank != 0)
2915             gfc_internal_error ("expression_rank(): Two array specs");
2916
2917           for (i = 0; i < ref->u.ar.dimen; i++)
2918             if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
2919                 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2920               rank++;
2921
2922           break;
2923         }
2924     }
2925
2926   e->rank = rank;
2927
2928 done:
2929   expression_shape (e);
2930 }
2931
2932
2933 /* Resolve a variable expression.  */
2934
2935 static try
2936 resolve_variable (gfc_expr * e)
2937 {
2938   gfc_symbol *sym;
2939   try t;
2940
2941   t = SUCCESS;
2942
2943   if (e->symtree == NULL)
2944     return FAILURE;
2945
2946   if (e->ref && resolve_ref (e) == FAILURE)
2947     return FAILURE;
2948
2949   sym = e->symtree->n.sym;
2950   if (sym->attr.flavor == FL_PROCEDURE && !sym->attr.function)
2951     {
2952       e->ts.type = BT_PROCEDURE;
2953       return SUCCESS;
2954     }
2955
2956   if (sym->ts.type != BT_UNKNOWN)
2957     gfc_variable_attr (e, &e->ts);
2958   else
2959     {
2960       /* Must be a simple variable reference.  */
2961       if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
2962         return FAILURE;
2963       e->ts = sym->ts;
2964     }
2965
2966   if (check_assumed_size_reference (sym, e))
2967     return FAILURE;
2968
2969   /* Deal with forward references to entries during resolve_code, to
2970      satisfy, at least partially, 12.5.2.5.  */
2971   if (gfc_current_ns->entries
2972         && current_entry_id == sym->entry_id
2973         && cs_base
2974         && cs_base->current
2975         && cs_base->current->op != EXEC_ENTRY)
2976     {
2977       gfc_entry_list *entry;
2978       gfc_formal_arglist *formal;
2979       int n;
2980       bool seen;
2981
2982       /* If the symbol is a dummy...  */
2983       if (sym->attr.dummy)
2984         {
2985           entry = gfc_current_ns->entries;
2986           seen = false;
2987
2988           /* ...test if the symbol is a parameter of previous entries.  */
2989           for (; entry && entry->id <= current_entry_id; entry = entry->next)
2990             for (formal = entry->sym->formal; formal; formal = formal->next)
2991               {
2992                 if (formal->sym && sym->name == formal->sym->name)
2993                   seen = true;
2994               }
2995
2996           /*  If it has not been seen as a dummy, this is an error.  */
2997           if (!seen)
2998             {
2999               if (specification_expr)
3000                 gfc_error ("Variable '%s',used in a specification expression, "
3001                            "is referenced at %L before the ENTRY statement "
3002                            "in which it is a parameter",
3003                            sym->name, &cs_base->current->loc);
3004               else
3005                 gfc_error ("Variable '%s' is used at %L before the ENTRY "
3006                            "statement in which it is a parameter",
3007                            sym->name, &cs_base->current->loc);
3008               t = FAILURE;
3009             }
3010         }
3011
3012       /* Now do the same check on the specification expressions.  */
3013       specification_expr = 1;
3014       if (sym->ts.type == BT_CHARACTER
3015             && gfc_resolve_expr (sym->ts.cl->length) == FAILURE)
3016         t = FAILURE;
3017
3018       if (sym->as)
3019         for (n = 0; n < sym->as->rank; n++)
3020           {
3021              specification_expr = 1;
3022              if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
3023                t = FAILURE;
3024              specification_expr = 1;
3025              if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
3026                t = FAILURE;
3027           }
3028       specification_expr = 0;
3029
3030       if (t == SUCCESS)
3031         /* Update the symbol's entry level.  */
3032         sym->entry_id = current_entry_id + 1;
3033     }
3034
3035   return t;
3036 }
3037
3038
3039 /* Resolve an expression.  That is, make sure that types of operands agree
3040    with their operators, intrinsic operators are converted to function calls
3041    for overloaded types and unresolved function references are resolved.  */
3042
3043 try
3044 gfc_resolve_expr (gfc_expr * e)
3045 {
3046   try t;
3047
3048   if (e == NULL)
3049     return SUCCESS;
3050
3051   switch (e->expr_type)
3052     {
3053     case EXPR_OP:
3054       t = resolve_operator (e);
3055       break;
3056
3057     case EXPR_FUNCTION:
3058       t = resolve_function (e);
3059       break;
3060
3061     case EXPR_VARIABLE:
3062       t = resolve_variable (e);
3063       if (t == SUCCESS)
3064         expression_rank (e);
3065       break;
3066
3067     case EXPR_SUBSTRING:
3068       t = resolve_ref (e);
3069       break;
3070
3071     case EXPR_CONSTANT:
3072     case EXPR_NULL:
3073       t = SUCCESS;
3074       break;
3075
3076     case EXPR_ARRAY:
3077       t = FAILURE;
3078       if (resolve_ref (e) == FAILURE)
3079         break;
3080
3081       t = gfc_resolve_array_constructor (e);
3082       /* Also try to expand a constructor.  */
3083       if (t == SUCCESS)
3084         {
3085           expression_rank (e);
3086           gfc_expand_constructor (e);
3087         }
3088
3089       /* This provides the opportunity for the length of constructors with character
3090         valued function elements to propogate the string length to the expression.  */
3091       if (e->ts.type == BT_CHARACTER)
3092         gfc_resolve_character_array_constructor (e);
3093
3094       break;
3095
3096     case EXPR_STRUCTURE:
3097       t = resolve_ref (e);
3098       if (t == FAILURE)
3099         break;
3100
3101       t = resolve_structure_cons (e);
3102       if (t == FAILURE)
3103         break;
3104
3105       t = gfc_simplify_expr (e, 0);
3106       break;
3107
3108     default:
3109       gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
3110     }
3111
3112   return t;
3113 }
3114
3115
3116 /* Resolve an expression from an iterator.  They must be scalar and have
3117    INTEGER or (optionally) REAL type.  */
3118
3119 static try
3120 gfc_resolve_iterator_expr (gfc_expr * expr, bool real_ok,
3121                            const char * name_msgid)
3122 {
3123   if (gfc_resolve_expr (expr) == FAILURE)
3124     return FAILURE;
3125
3126   if (expr->rank != 0)
3127     {
3128       gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
3129       return FAILURE;
3130     }
3131
3132   if (!(expr->ts.type == BT_INTEGER
3133         || (expr->ts.type == BT_REAL && real_ok)))
3134     {
3135       if (real_ok)
3136         gfc_error ("%s at %L must be INTEGER or REAL", _(name_msgid),
3137                    &expr->where);
3138       else
3139         gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
3140       return FAILURE;
3141     }
3142   return SUCCESS;
3143 }
3144
3145
3146 /* Resolve the expressions in an iterator structure.  If REAL_OK is
3147    false allow only INTEGER type iterators, otherwise allow REAL types.  */
3148
3149 try
3150 gfc_resolve_iterator (gfc_iterator * iter, bool real_ok)
3151 {
3152
3153   if (iter->var->ts.type == BT_REAL)
3154     gfc_notify_std (GFC_STD_F95_DEL,
3155                     "Obsolete: REAL DO loop iterator at %L",
3156                     &iter->var->where);
3157
3158   if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
3159       == FAILURE)
3160     return FAILURE;
3161
3162   if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
3163     {
3164       gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
3165                  &iter->var->where);
3166       return FAILURE;
3167     }
3168
3169   if (gfc_resolve_iterator_expr (iter->start, real_ok,
3170                                  "Start expression in DO loop") == FAILURE)
3171     return FAILURE;
3172
3173   if (gfc_resolve_iterator_expr (iter->end, real_ok,
3174                                  "End expression in DO loop") == FAILURE)
3175     return FAILURE;
3176
3177   if (gfc_resolve_iterator_expr (iter->step, real_ok,
3178                                  "Step expression in DO loop") == FAILURE)
3179     return FAILURE;
3180
3181   if (iter->step->expr_type == EXPR_CONSTANT)
3182     {
3183       if ((iter->step->ts.type == BT_INTEGER
3184            && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
3185           || (iter->step->ts.type == BT_REAL
3186               && mpfr_sgn (iter->step->value.real) == 0))
3187         {
3188           gfc_error ("Step expression in DO loop at %L cannot be zero",
3189                      &iter->step->where);
3190           return FAILURE;
3191         }
3192     }
3193
3194   /* Convert start, end, and step to the same type as var.  */
3195   if (iter->start->ts.kind != iter->var->ts.kind
3196       || iter->start->ts.type != iter->var->ts.type)
3197     gfc_convert_type (iter->start, &iter->var->ts, 2);
3198
3199   if (iter->end->ts.kind != iter->var->ts.kind
3200       || iter->end->ts.type != iter->var->ts.type)
3201     gfc_convert_type (iter->end, &iter->var->ts, 2);
3202
3203   if (iter->step->ts.kind != iter->var->ts.kind
3204       || iter->step->ts.type != iter->var->ts.type)
3205     gfc_convert_type (iter->step, &iter->var->ts, 2);
3206
3207   return SUCCESS;
3208 }
3209
3210
3211 /* Resolve a list of FORALL iterators.  The FORALL index-name is constrained
3212    to be a scalar INTEGER variable.  The subscripts and stride are scalar
3213    INTEGERs, and if stride is a constant it must be nonzero.  */
3214
3215 static void
3216 resolve_forall_iterators (gfc_forall_iterator * iter)
3217 {
3218
3219   while (iter)
3220     {
3221       if (gfc_resolve_expr (iter->var) == SUCCESS
3222           && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
3223         gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
3224                    &iter->var->where);
3225
3226       if (gfc_resolve_expr (iter->start) == SUCCESS
3227           && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
3228         gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
3229                    &iter->start->where);
3230       if (iter->var->ts.kind != iter->start->ts.kind)
3231         gfc_convert_type (iter->start, &iter->var->ts, 2);
3232
3233       if (gfc_resolve_expr (iter->end) == SUCCESS
3234           && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
3235         gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
3236                    &iter->end->where);
3237       if (iter->var->ts.kind != iter->end->ts.kind)
3238         gfc_convert_type (iter->end, &iter->var->ts, 2);
3239
3240       if (gfc_resolve_expr (iter->stride) == SUCCESS)
3241         {
3242           if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
3243             gfc_error ("FORALL stride expression at %L must be a scalar %s",
3244                         &iter->stride->where, "INTEGER");
3245
3246           if (iter->stride->expr_type == EXPR_CONSTANT
3247               && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
3248             gfc_error ("FORALL stride expression at %L cannot be zero",
3249                        &iter->stride->where);
3250         }
3251       if (iter->var->ts.kind != iter->stride->ts.kind)
3252         gfc_convert_type (iter->stride, &iter->var->ts, 2);
3253
3254       iter = iter->next;
3255     }
3256 }
3257
3258
3259 /* Given a pointer to a symbol that is a derived type, see if any components
3260    have the POINTER attribute.  The search is recursive if necessary.
3261    Returns zero if no pointer components are found, nonzero otherwise.  */
3262
3263 static int
3264 derived_pointer (gfc_symbol * sym)
3265 {
3266   gfc_component *c;
3267
3268   for (c = sym->components; c; c = c->next)
3269     {
3270       if (c->pointer)
3271         return 1;
3272
3273       if (c->ts.type == BT_DERIVED && derived_pointer (c->ts.derived))
3274         return 1;
3275     }
3276
3277   return 0;
3278 }
3279
3280
3281 /* Given a pointer to a symbol that is a derived type, see if it's
3282    inaccessible, i.e. if it's defined in another module and the components are
3283    PRIVATE.  The search is recursive if necessary.  Returns zero if no
3284    inaccessible components are found, nonzero otherwise.  */
3285
3286 static int
3287 derived_inaccessible (gfc_symbol *sym)
3288 {
3289   gfc_component *c;
3290
3291   if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
3292     return 1;
3293
3294   for (c = sym->components; c; c = c->next)
3295     {
3296         if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.derived))
3297           return 1;
3298     }
3299
3300   return 0;
3301 }
3302
3303
3304 /* Resolve the argument of a deallocate expression.  The expression must be
3305    a pointer or a full array.  */
3306
3307 static try
3308 resolve_deallocate_expr (gfc_expr * e)
3309 {
3310   symbol_attribute attr;
3311   int allocatable;
3312   gfc_ref *ref;
3313
3314   if (gfc_resolve_expr (e) == FAILURE)
3315     return FAILURE;
3316
3317   attr = gfc_expr_attr (e);
3318   if (attr.pointer)
3319     return SUCCESS;
3320
3321   if (e->expr_type != EXPR_VARIABLE)
3322     goto bad;
3323
3324   allocatable = e->symtree->n.sym->attr.allocatable;
3325   for (ref = e->ref; ref; ref = ref->next)
3326     switch (ref->type)
3327       {
3328       case REF_ARRAY:
3329         if (ref->u.ar.type != AR_FULL)
3330           allocatable = 0;
3331         break;
3332
3333       case REF_COMPONENT:
3334         allocatable = (ref->u.c.component->as != NULL
3335                        && ref->u.c.component->as->type == AS_DEFERRED);
3336         break;
3337
3338       case REF_SUBSTRING:
3339         allocatable = 0;
3340         break;
3341       }
3342
3343   if (allocatable == 0)
3344     {
3345     bad:
3346       gfc_error ("Expression in DEALLOCATE statement at %L must be "
3347                  "ALLOCATABLE or a POINTER", &e->where);
3348     }
3349
3350   if (e->symtree->n.sym->attr.intent == INTENT_IN)
3351     {
3352       gfc_error ("Can't deallocate INTENT(IN) variable '%s' at %L",
3353                  e->symtree->n.sym->name, &e->where);
3354       return FAILURE;
3355     }
3356
3357   return SUCCESS;
3358 }
3359
3360 /* Returns true if the expression e contains a reference the symbol sym.  */
3361 static bool
3362 find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
3363 {
3364   gfc_actual_arglist *arg;
3365   gfc_ref *ref;
3366   int i;
3367   bool rv = false;
3368
3369   if (e == NULL)
3370     return rv;
3371
3372   switch (e->expr_type)
3373     {
3374     case EXPR_FUNCTION:
3375       for (arg = e->value.function.actual; arg; arg = arg->next)
3376         rv = rv || find_sym_in_expr (sym, arg->expr);
3377       break;
3378
3379     /* If the variable is not the same as the dependent, 'sym', and
3380        it is not marked as being declared and it is in the same
3381        namespace as 'sym', add it to the local declarations.  */
3382     case EXPR_VARIABLE:
3383       if (sym == e->symtree->n.sym)
3384         return true;
3385       break;
3386
3387     case EXPR_OP:
3388       rv = rv || find_sym_in_expr (sym, e->value.op.op1);
3389       rv = rv || find_sym_in_expr (sym, e->value.op.op2);
3390       break;
3391
3392     default:
3393       break;
3394     }
3395
3396   if (e->ref)
3397     {
3398       for (ref = e->ref; ref; ref = ref->next)
3399         {
3400           switch (ref->type)
3401             {
3402             case REF_ARRAY:
3403               for (i = 0; i < ref->u.ar.dimen; i++)
3404                 {
3405                   rv = rv || find_sym_in_expr (sym, ref->u.ar.start[i]);
3406                   rv = rv || find_sym_in_expr (sym, ref->u.ar.end[i]);
3407                   rv = rv || find_sym_in_expr (sym, ref->u.ar.stride[i]);
3408                 }
3409               break;
3410
3411             case REF_SUBSTRING:
3412               rv = rv || find_sym_in_expr (sym, ref->u.ss.start);
3413               rv = rv || find_sym_in_expr (sym, ref->u.ss.end);
3414               break;
3415
3416             case REF_COMPONENT:
3417               if (ref->u.c.component->ts.type == BT_CHARACTER
3418                     && ref->u.c.component->ts.cl->length->expr_type
3419                                                 != EXPR_CONSTANT)
3420                 rv = rv || find_sym_in_expr (sym, ref->u.c.component->ts.cl->length);
3421
3422               if (ref->u.c.component->as)
3423                 for (i = 0; i < ref->u.c.component->as->rank; i++)
3424                   {
3425                     rv = rv || find_sym_in_expr (sym, ref->u.c.component->as->lower[i]);
3426                     rv = rv || find_sym_in_expr (sym, ref->u.c.component->as->upper[i]);
3427                   }
3428               break;
3429             }
3430         }
3431     }
3432   return rv;
3433 }
3434
3435
3436 /* Given the expression node e for an allocatable/pointer of derived type to be
3437    allocated, get the expression node to be initialized afterwards (needed for
3438    derived types with default initializers, and derived types with allocatable
3439    components that need nullification.)  */
3440
3441 static gfc_expr *
3442 expr_to_initialize (gfc_expr * e)
3443 {
3444   gfc_expr *result;
3445   gfc_ref *ref;
3446   int i;
3447
3448   result = gfc_copy_expr (e);
3449
3450   /* Change the last array reference from AR_ELEMENT to AR_FULL.  */
3451   for (ref = result->ref; ref; ref = ref->next)
3452     if (ref->type == REF_ARRAY && ref->next == NULL)
3453       {
3454         ref->u.ar.type = AR_FULL;
3455
3456         for (i = 0; i < ref->u.ar.dimen; i++)
3457           ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
3458
3459         result->rank = ref->u.ar.dimen;
3460         break;
3461       }
3462
3463   return result;
3464 }
3465
3466
3467 /* Resolve the expression in an ALLOCATE statement, doing the additional
3468    checks to see whether the expression is OK or not.  The expression must
3469    have a trailing array reference that gives the size of the array.  */
3470
3471 static try
3472 resolve_allocate_expr (gfc_expr * e, gfc_code * code)
3473 {
3474   int i, pointer, allocatable, dimension;
3475   symbol_attribute attr;
3476   gfc_ref *ref, *ref2;
3477   gfc_array_ref *ar;
3478   gfc_code *init_st;
3479   gfc_expr *init_e;
3480   gfc_symbol *sym;
3481   gfc_alloc *a;
3482
3483   if (gfc_resolve_expr (e) == FAILURE)
3484     return FAILURE;
3485
3486   if (code->expr && code->expr->expr_type == EXPR_VARIABLE)
3487     sym = code->expr->symtree->n.sym;
3488   else
3489     sym = NULL;
3490
3491   /* Make sure the expression is allocatable or a pointer.  If it is
3492      pointer, the next-to-last reference must be a pointer.  */
3493
3494   ref2 = NULL;
3495
3496   if (e->expr_type != EXPR_VARIABLE)
3497     {
3498       allocatable = 0;
3499
3500       attr = gfc_expr_attr (e);
3501       pointer = attr.pointer;
3502       dimension = attr.dimension;
3503
3504     }
3505   else
3506     {
3507       allocatable = e->symtree->n.sym->attr.allocatable;
3508       pointer = e->symtree->n.sym->attr.pointer;
3509       dimension = e->symtree->n.sym->attr.dimension;
3510
3511       if (sym == e->symtree->n.sym && sym->ts.type != BT_DERIVED)
3512         {
3513           gfc_error ("The STAT variable '%s' in an ALLOCATE statement must "
3514                      "not be allocated in the same statement at %L",
3515                       sym->name, &e->where);
3516           return FAILURE;
3517         }
3518
3519       for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
3520         switch (ref->type)
3521           {
3522           case REF_ARRAY:
3523             if (ref->next != NULL)
3524               pointer = 0;
3525             break;
3526
3527           case REF_COMPONENT:
3528             allocatable = (ref->u.c.component->as != NULL
3529                            && ref->u.c.component->as->type == AS_DEFERRED);
3530
3531             pointer = ref->u.c.component->pointer;
3532             dimension = ref->u.c.component->dimension;
3533             break;
3534
3535           case REF_SUBSTRING:
3536             allocatable = 0;
3537             pointer = 0;
3538             break;
3539           }
3540     }
3541
3542   if (allocatable == 0 && pointer == 0)
3543     {
3544       gfc_error ("Expression in ALLOCATE statement at %L must be "
3545                  "ALLOCATABLE or a POINTER", &e->where);
3546       return FAILURE;
3547     }
3548
3549   if (e->symtree->n.sym->attr.intent == INTENT_IN)
3550     {
3551       gfc_error ("Can't allocate INTENT(IN) variable '%s' at %L",
3552                  e->symtree->n.sym->name, &e->where);
3553       return FAILURE;
3554     }
3555
3556   /* Add default initializer for those derived types that need them.  */
3557   if (e->ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&e->ts)))
3558     {
3559         init_st = gfc_get_code ();
3560         init_st->loc = code->loc;
3561         init_st->op = EXEC_INIT_ASSIGN;
3562         init_st->expr = expr_to_initialize (e);
3563         init_st->expr2 = init_e;
3564         init_st->next = code->next;
3565         code->next = init_st;