OSDN Git Service

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