OSDN Git Service

2006-11-24 Paul Thomas <pault@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / interface.c
1 /* Deal with interfaces.
2    Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006 Free Software
3    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 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23
24 /* Deal with interfaces.  An explicit interface is represented as a
25    singly linked list of formal argument structures attached to the
26    relevant symbols.  For an implicit interface, the arguments don't
27    point to symbols.  Explicit interfaces point to namespaces that
28    contain the symbols within that interface.
29
30    Implicit interfaces are linked together in a singly linked list
31    along the next_if member of symbol nodes.  Since a particular
32    symbol can only have a single explicit interface, the symbol cannot
33    be part of multiple lists and a single next-member suffices.
34
35    This is not the case for general classes, though.  An operator
36    definition is independent of just about all other uses and has it's
37    own head pointer.
38
39    Nameless interfaces:
40      Nameless interfaces create symbols with explicit interfaces within
41      the current namespace.  They are otherwise unlinked.
42
43    Generic interfaces:
44      The generic name points to a linked list of symbols.  Each symbol
45      has an explicit interface.  Each explicit interface has its own
46      namespace containing the arguments.  Module procedures are symbols in
47      which the interface is added later when the module procedure is parsed.
48
49    User operators:
50      User-defined operators are stored in a their own set of symtrees
51      separate from regular symbols.  The symtrees point to gfc_user_op
52      structures which in turn head up a list of relevant interfaces.
53
54    Extended intrinsics and assignment:
55      The head of these interface lists are stored in the containing namespace.
56
57    Implicit interfaces:
58      An implicit interface is represented as a singly linked list of
59      formal argument list structures that don't point to any symbol
60      nodes -- they just contain types.
61
62
63    When a subprogram is defined, the program unit's name points to an
64    interface as usual, but the link to the namespace is NULL and the
65    formal argument list points to symbols within the same namespace as
66    the program unit name.  */
67
68 #include "config.h"
69 #include "system.h"
70 #include "gfortran.h"
71 #include "match.h"
72
73
74 /* The current_interface structure holds information about the
75    interface currently being parsed.  This structure is saved and
76    restored during recursive interfaces.  */
77
78 gfc_interface_info current_interface;
79
80
81 /* Free a singly linked list of gfc_interface structures.  */
82
83 void
84 gfc_free_interface (gfc_interface * intr)
85 {
86   gfc_interface *next;
87
88   for (; intr; intr = next)
89     {
90       next = intr->next;
91       gfc_free (intr);
92     }
93 }
94
95
96 /* Change the operators unary plus and minus into binary plus and
97    minus respectively, leaving the rest unchanged.  */
98
99 static gfc_intrinsic_op
100 fold_unary (gfc_intrinsic_op operator)
101 {
102
103   switch (operator)
104     {
105     case INTRINSIC_UPLUS:
106       operator = INTRINSIC_PLUS;
107       break;
108     case INTRINSIC_UMINUS:
109       operator = INTRINSIC_MINUS;
110       break;
111     default:
112       break;
113     }
114
115   return operator;
116 }
117
118
119 /* Match a generic specification.  Depending on which type of
120    interface is found, the 'name' or 'operator' pointers may be set.
121    This subroutine doesn't return MATCH_NO.  */
122
123 match
124 gfc_match_generic_spec (interface_type * type,
125                         char *name,
126                         gfc_intrinsic_op *operator)
127 {
128   char buffer[GFC_MAX_SYMBOL_LEN + 1];
129   match m;
130   gfc_intrinsic_op i;
131
132   if (gfc_match (" assignment ( = )") == MATCH_YES)
133     {
134       *type = INTERFACE_INTRINSIC_OP;
135       *operator = INTRINSIC_ASSIGN;
136       return MATCH_YES;
137     }
138
139   if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
140     {                           /* Operator i/f */
141       *type = INTERFACE_INTRINSIC_OP;
142       *operator = fold_unary (i);
143       return MATCH_YES;
144     }
145
146   if (gfc_match (" operator ( ") == MATCH_YES)
147     {
148       m = gfc_match_defined_op_name (buffer, 1);
149       if (m == MATCH_NO)
150         goto syntax;
151       if (m != MATCH_YES)
152         return MATCH_ERROR;
153
154       m = gfc_match_char (')');
155       if (m == MATCH_NO)
156         goto syntax;
157       if (m != MATCH_YES)
158         return MATCH_ERROR;
159
160       strcpy (name, buffer);
161       *type = INTERFACE_USER_OP;
162       return MATCH_YES;
163     }
164
165   if (gfc_match_name (buffer) == MATCH_YES)
166     {
167       strcpy (name, buffer);
168       *type = INTERFACE_GENERIC;
169       return MATCH_YES;
170     }
171
172   *type = INTERFACE_NAMELESS;
173   return MATCH_YES;
174
175 syntax:
176   gfc_error ("Syntax error in generic specification at %C");
177   return MATCH_ERROR;
178 }
179
180
181 /* Match one of the five forms of an interface statement.  */
182
183 match
184 gfc_match_interface (void)
185 {
186   char name[GFC_MAX_SYMBOL_LEN + 1];
187   interface_type type;
188   gfc_symbol *sym;
189   gfc_intrinsic_op operator;
190   match m;
191
192   m = gfc_match_space ();
193
194   if (gfc_match_generic_spec (&type, name, &operator) == MATCH_ERROR)
195     return MATCH_ERROR;
196
197
198   /* If we're not looking at the end of the statement now, or if this
199      is not a nameless interface but we did not see a space, punt.  */
200   if (gfc_match_eos () != MATCH_YES
201       || (type != INTERFACE_NAMELESS
202           && m != MATCH_YES))
203     {
204       gfc_error
205         ("Syntax error: Trailing garbage in INTERFACE statement at %C");
206       return MATCH_ERROR;
207     }
208
209   current_interface.type = type;
210
211   switch (type)
212     {
213     case INTERFACE_GENERIC:
214       if (gfc_get_symbol (name, NULL, &sym))
215         return MATCH_ERROR;
216
217       if (!sym->attr.generic 
218           && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
219         return MATCH_ERROR;
220
221       if (sym->attr.dummy)
222         {
223           gfc_error ("Dummy procedure '%s' at %C cannot have a "
224                      "generic interface", sym->name);
225           return MATCH_ERROR;
226         }
227
228       current_interface.sym = gfc_new_block = sym;
229       break;
230
231     case INTERFACE_USER_OP:
232       current_interface.uop = gfc_get_uop (name);
233       break;
234
235     case INTERFACE_INTRINSIC_OP:
236       current_interface.op = operator;
237       break;
238
239     case INTERFACE_NAMELESS:
240       break;
241     }
242
243   return MATCH_YES;
244 }
245
246
247 /* Match the different sort of generic-specs that can be present after
248    the END INTERFACE itself.  */
249
250 match
251 gfc_match_end_interface (void)
252 {
253   char name[GFC_MAX_SYMBOL_LEN + 1];
254   interface_type type;
255   gfc_intrinsic_op operator;
256   match m;
257
258   m = gfc_match_space ();
259
260   if (gfc_match_generic_spec (&type, name, &operator) == MATCH_ERROR)
261     return MATCH_ERROR;
262
263   /* If we're not looking at the end of the statement now, or if this
264      is not a nameless interface but we did not see a space, punt.  */
265   if (gfc_match_eos () != MATCH_YES
266       || (type != INTERFACE_NAMELESS
267           && m != MATCH_YES))
268     {
269       gfc_error
270         ("Syntax error: Trailing garbage in END INTERFACE statement at %C");
271       return MATCH_ERROR;
272     }
273
274   m = MATCH_YES;
275
276   switch (current_interface.type)
277     {
278     case INTERFACE_NAMELESS:
279       if (type != current_interface.type)
280         {
281           gfc_error ("Expected a nameless interface at %C");
282           m = MATCH_ERROR;
283         }
284
285       break;
286
287     case INTERFACE_INTRINSIC_OP:
288       if (type != current_interface.type || operator != current_interface.op)
289         {
290
291           if (current_interface.op == INTRINSIC_ASSIGN)
292             gfc_error ("Expected 'END INTERFACE ASSIGNMENT (=)' at %C");
293           else
294             gfc_error ("Expecting 'END INTERFACE OPERATOR (%s)' at %C",
295                        gfc_op2string (current_interface.op));
296
297           m = MATCH_ERROR;
298         }
299
300       break;
301
302     case INTERFACE_USER_OP:
303       /* Comparing the symbol node names is OK because only use-associated
304          symbols can be renamed.  */
305       if (type != current_interface.type
306           || strcmp (current_interface.uop->name, name) != 0)
307         {
308           gfc_error ("Expecting 'END INTERFACE OPERATOR (.%s.)' at %C",
309                      current_interface.uop->name);
310           m = MATCH_ERROR;
311         }
312
313       break;
314
315     case INTERFACE_GENERIC:
316       if (type != current_interface.type
317           || strcmp (current_interface.sym->name, name) != 0)
318         {
319           gfc_error ("Expecting 'END INTERFACE %s' at %C",
320                      current_interface.sym->name);
321           m = MATCH_ERROR;
322         }
323
324       break;
325     }
326
327   return m;
328 }
329
330
331 /* Compare two derived types using the criteria in 4.4.2 of the standard,
332    recursing through gfc_compare_types for the components.  */
333
334 int
335 gfc_compare_derived_types (gfc_symbol * derived1, gfc_symbol * derived2)
336 {
337   gfc_component *dt1, *dt2;
338
339   /* Special case for comparing derived types across namespaces.  If the
340      true names and module names are the same and the module name is
341      nonnull, then they are equal.  */
342   if (strcmp (derived1->name, derived2->name) == 0
343         && derived1 != NULL && derived2 != NULL
344         && derived1->module != NULL && derived2->module != NULL
345         && strcmp (derived1->module, derived2->module) == 0)
346     return 1;
347
348   /* Compare type via the rules of the standard.  Both types must have
349      the SEQUENCE attribute to be equal.  */
350
351   if (strcmp (derived1->name, derived2->name))
352     return 0;
353
354   if (derived1->component_access == ACCESS_PRIVATE
355         || derived2->component_access == ACCESS_PRIVATE)
356     return 0;
357
358   if (derived1->attr.sequence == 0 || derived2->attr.sequence == 0)
359     return 0;
360
361   dt1 = derived1->components;
362   dt2 = derived2->components;
363
364   /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
365      simple test can speed things up.  Otherwise, lots of things have to
366      match.  */
367   for (;;)
368     {
369       if (strcmp (dt1->name, dt2->name) != 0)
370         return 0;
371
372       if (dt1->pointer != dt2->pointer)
373         return 0;
374
375       if (dt1->dimension != dt2->dimension)
376         return 0;
377
378      if (dt1->allocatable != dt2->allocatable)
379         return 0;
380
381       if (dt1->dimension && gfc_compare_array_spec (dt1->as, dt2->as) == 0)
382         return 0;
383
384       if (gfc_compare_types (&dt1->ts, &dt2->ts) == 0)
385         return 0;
386
387       dt1 = dt1->next;
388       dt2 = dt2->next;
389
390       if (dt1 == NULL && dt2 == NULL)
391         break;
392       if (dt1 == NULL || dt2 == NULL)
393         return 0;
394     }
395
396   return 1;
397 }
398
399 /* Compare two typespecs, recursively if necessary.  */
400
401 int
402 gfc_compare_types (gfc_typespec * ts1, gfc_typespec * ts2)
403 {
404
405   if (ts1->type != ts2->type)
406     return 0;
407   if (ts1->type != BT_DERIVED)
408     return (ts1->kind == ts2->kind);
409
410   /* Compare derived types.  */
411   if (ts1->derived == ts2->derived)
412     return 1;
413
414   return gfc_compare_derived_types (ts1->derived ,ts2->derived);
415 }
416
417
418 /* Given two symbols that are formal arguments, compare their ranks
419    and types.  Returns nonzero if they have the same rank and type,
420    zero otherwise.  */
421
422 static int
423 compare_type_rank (gfc_symbol * s1, gfc_symbol * s2)
424 {
425   int r1, r2;
426
427   r1 = (s1->as != NULL) ? s1->as->rank : 0;
428   r2 = (s2->as != NULL) ? s2->as->rank : 0;
429
430   if (r1 != r2)
431     return 0;                   /* Ranks differ */
432
433   return gfc_compare_types (&s1->ts, &s2->ts);
434 }
435
436
437 static int compare_interfaces (gfc_symbol *, gfc_symbol *, int);
438
439 /* Given two symbols that are formal arguments, compare their types
440    and rank and their formal interfaces if they are both dummy
441    procedures.  Returns nonzero if the same, zero if different.  */
442
443 static int
444 compare_type_rank_if (gfc_symbol * s1, gfc_symbol * s2)
445 {
446
447   if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
448     return compare_type_rank (s1, s2);
449
450   if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
451     return 0;
452
453   /* At this point, both symbols are procedures.  */
454   if ((s1->attr.function == 0 && s1->attr.subroutine == 0)
455       || (s2->attr.function == 0 && s2->attr.subroutine == 0))
456     return 0;
457
458   if (s1->attr.function != s2->attr.function
459       || s1->attr.subroutine != s2->attr.subroutine)
460     return 0;
461
462   if (s1->attr.function && compare_type_rank (s1, s2) == 0)
463     return 0;
464
465   return compare_interfaces (s1, s2, 0);        /* Recurse! */
466 }
467
468
469 /* Given a formal argument list and a keyword name, search the list
470    for that keyword.  Returns the correct symbol node if found, NULL
471    if not found.  */
472
473 static gfc_symbol *
474 find_keyword_arg (const char *name, gfc_formal_arglist * f)
475 {
476
477   for (; f; f = f->next)
478     if (strcmp (f->sym->name, name) == 0)
479       return f->sym;
480
481   return NULL;
482 }
483
484
485 /******** Interface checking subroutines **********/
486
487
488 /* Given an operator interface and the operator, make sure that all
489    interfaces for that operator are legal.  */
490
491 static void
492 check_operator_interface (gfc_interface * intr, gfc_intrinsic_op operator)
493 {
494   gfc_formal_arglist *formal;
495   sym_intent i1, i2;
496   gfc_symbol *sym;
497   bt t1, t2;
498   int args;
499
500   if (intr == NULL)
501     return;
502
503   args = 0;
504   t1 = t2 = BT_UNKNOWN;
505   i1 = i2 = INTENT_UNKNOWN;
506
507   for (formal = intr->sym->formal; formal; formal = formal->next)
508     {
509       sym = formal->sym;
510       if (sym == NULL)
511         {
512           gfc_error ("Alternate return cannot appear in operator "
513                      "interface at %L", &intr->where);
514           return;
515         }
516       if (args == 0)
517         {
518           t1 = sym->ts.type;
519           i1 = sym->attr.intent;
520         }
521       if (args == 1)
522         {
523           t2 = sym->ts.type;
524           i2 = sym->attr.intent;
525         }
526       args++;
527     }
528
529   if (args == 0 || args > 2)
530     goto num_args;
531
532   sym = intr->sym;
533
534   if (operator == INTRINSIC_ASSIGN)
535     {
536       if (!sym->attr.subroutine)
537         {
538           gfc_error
539             ("Assignment operator interface at %L must be a SUBROUTINE",
540              &intr->where);
541           return;
542         }
543       if (args != 2)
544         {
545           gfc_error
546             ("Assignment operator interface at %L must have two arguments",
547              &intr->where);
548           return;
549         }
550       if (sym->formal->sym->ts.type != BT_DERIVED
551             && sym->formal->next->sym->ts.type != BT_DERIVED
552             && (sym->formal->sym->ts.type == sym->formal->next->sym->ts.type
553                   || (gfc_numeric_ts (&sym->formal->sym->ts)
554                         && gfc_numeric_ts (&sym->formal->next->sym->ts))))
555         {
556           gfc_error
557             ("Assignment operator interface at %L must not redefine "
558              "an INTRINSIC type assignment", &intr->where);
559           return;
560         }
561     }
562   else
563     {
564       if (!sym->attr.function)
565         {
566           gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
567                      &intr->where);
568           return;
569         }
570     }
571
572   switch (operator)
573     {
574     case INTRINSIC_PLUS:        /* Numeric unary or binary */
575     case INTRINSIC_MINUS:
576       if ((args == 1)
577           && (t1 == BT_INTEGER
578               || t1 == BT_REAL
579               || t1 == BT_COMPLEX))
580         goto bad_repl;
581
582       if ((args == 2)
583           && (t1 == BT_INTEGER || t1 == BT_REAL || t1 == BT_COMPLEX)
584           && (t2 == BT_INTEGER || t2 == BT_REAL || t2 == BT_COMPLEX))
585         goto bad_repl;
586
587       break;
588
589     case INTRINSIC_POWER:       /* Binary numeric */
590     case INTRINSIC_TIMES:
591     case INTRINSIC_DIVIDE:
592
593     case INTRINSIC_EQ:
594     case INTRINSIC_NE:
595       if (args == 1)
596         goto num_args;
597
598       if ((t1 == BT_INTEGER || t1 == BT_REAL || t1 == BT_COMPLEX)
599           && (t2 == BT_INTEGER || t2 == BT_REAL || t2 == BT_COMPLEX))
600         goto bad_repl;
601
602       break;
603
604     case INTRINSIC_GE:          /* Binary numeric operators that do not support */
605     case INTRINSIC_LE:          /* complex numbers */
606     case INTRINSIC_LT:
607     case INTRINSIC_GT:
608       if (args == 1)
609         goto num_args;
610
611       if ((t1 == BT_INTEGER || t1 == BT_REAL)
612           && (t2 == BT_INTEGER || t2 == BT_REAL))
613         goto bad_repl;
614
615       break;
616
617     case INTRINSIC_OR:          /* Binary logical */
618     case INTRINSIC_AND:
619     case INTRINSIC_EQV:
620     case INTRINSIC_NEQV:
621       if (args == 1)
622         goto num_args;
623       if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
624         goto bad_repl;
625       break;
626
627     case INTRINSIC_NOT: /* Unary logical */
628       if (args != 1)
629         goto num_args;
630       if (t1 == BT_LOGICAL)
631         goto bad_repl;
632       break;
633
634     case INTRINSIC_CONCAT:      /* Binary string */
635       if (args != 2)
636         goto num_args;
637       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
638         goto bad_repl;
639       break;
640
641     case INTRINSIC_ASSIGN:      /* Class by itself */
642       if (args != 2)
643         goto num_args;
644       break;
645     default:
646       gfc_internal_error ("check_operator_interface(): Bad operator");
647     }
648
649   /* Check intents on operator interfaces.  */
650   if (operator == INTRINSIC_ASSIGN)
651     {
652       if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
653         gfc_error ("First argument of defined assignment at %L must be "
654                    "INTENT(IN) or INTENT(INOUT)", &intr->where);
655
656       if (i2 != INTENT_IN)
657         gfc_error ("Second argument of defined assignment at %L must be "
658                    "INTENT(IN)", &intr->where);
659     }
660   else
661     {
662       if (i1 != INTENT_IN)
663         gfc_error ("First argument of operator interface at %L must be "
664                    "INTENT(IN)", &intr->where);
665
666       if (args == 2 && i2 != INTENT_IN)
667         gfc_error ("Second argument of operator interface at %L must be "
668                    "INTENT(IN)", &intr->where);
669     }
670
671   return;
672
673 bad_repl:
674   gfc_error ("Operator interface at %L conflicts with intrinsic interface",
675              &intr->where);
676   return;
677
678 num_args:
679   gfc_error ("Operator interface at %L has the wrong number of arguments",
680              &intr->where);
681   return;
682 }
683
684
685 /* Given a pair of formal argument lists, we see if the two lists can
686    be distinguished by counting the number of nonoptional arguments of
687    a given type/rank in f1 and seeing if there are less then that
688    number of those arguments in f2 (including optional arguments).
689    Since this test is asymmetric, it has to be called twice to make it
690    symmetric.  Returns nonzero if the argument lists are incompatible
691    by this test.  This subroutine implements rule 1 of section
692    14.1.2.3.  */
693
694 static int
695 count_types_test (gfc_formal_arglist * f1, gfc_formal_arglist * f2)
696 {
697   int rc, ac1, ac2, i, j, k, n1;
698   gfc_formal_arglist *f;
699
700   typedef struct
701   {
702     int flag;
703     gfc_symbol *sym;
704   }
705   arginfo;
706
707   arginfo *arg;
708
709   n1 = 0;
710
711   for (f = f1; f; f = f->next)
712     n1++;
713
714   /* Build an array of integers that gives the same integer to
715      arguments of the same type/rank.  */
716   arg = gfc_getmem (n1 * sizeof (arginfo));
717
718   f = f1;
719   for (i = 0; i < n1; i++, f = f->next)
720     {
721       arg[i].flag = -1;
722       arg[i].sym = f->sym;
723     }
724
725   k = 0;
726
727   for (i = 0; i < n1; i++)
728     {
729       if (arg[i].flag != -1)
730         continue;
731
732       if (arg[i].sym->attr.optional)
733         continue;               /* Skip optional arguments */
734
735       arg[i].flag = k;
736
737       /* Find other nonoptional arguments of the same type/rank.  */
738       for (j = i + 1; j < n1; j++)
739         if (!arg[j].sym->attr.optional
740             && compare_type_rank_if (arg[i].sym, arg[j].sym))
741           arg[j].flag = k;
742
743       k++;
744     }
745
746   /* Now loop over each distinct type found in f1.  */
747   k = 0;
748   rc = 0;
749
750   for (i = 0; i < n1; i++)
751     {
752       if (arg[i].flag != k)
753         continue;
754
755       ac1 = 1;
756       for (j = i + 1; j < n1; j++)
757         if (arg[j].flag == k)
758           ac1++;
759
760       /* Count the number of arguments in f2 with that type, including
761          those that are optional.  */
762       ac2 = 0;
763
764       for (f = f2; f; f = f->next)
765         if (compare_type_rank_if (arg[i].sym, f->sym))
766           ac2++;
767
768       if (ac1 > ac2)
769         {
770           rc = 1;
771           break;
772         }
773
774       k++;
775     }
776
777   gfc_free (arg);
778
779   return rc;
780 }
781
782
783 /* Perform the abbreviated correspondence test for operators.  The
784    arguments cannot be optional and are always ordered correctly,
785    which makes this test much easier than that for generic tests.
786
787    This subroutine is also used when comparing a formal and actual
788    argument list when an actual parameter is a dummy procedure.  At
789    that point, two formal interfaces must be compared for equality
790    which is what happens here.  */
791
792 static int
793 operator_correspondence (gfc_formal_arglist * f1, gfc_formal_arglist * f2)
794 {
795   for (;;)
796     {
797       if (f1 == NULL && f2 == NULL)
798         break;
799       if (f1 == NULL || f2 == NULL)
800         return 1;
801
802       if (!compare_type_rank (f1->sym, f2->sym))
803         return 1;
804
805       f1 = f1->next;
806       f2 = f2->next;
807     }
808
809   return 0;
810 }
811
812
813 /* Perform the correspondence test in rule 2 of section 14.1.2.3.
814    Returns zero if no argument is found that satisfies rule 2, nonzero
815    otherwise.
816
817    This test is also not symmetric in f1 and f2 and must be called
818    twice.  This test finds problems caused by sorting the actual
819    argument list with keywords.  For example:
820
821    INTERFACE FOO
822        SUBROUTINE F1(A, B)
823            INTEGER :: A ; REAL :: B
824        END SUBROUTINE F1
825
826        SUBROUTINE F2(B, A)
827            INTEGER :: A ; REAL :: B
828        END SUBROUTINE F1
829    END INTERFACE FOO
830
831    At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous.  */
832
833 static int
834 generic_correspondence (gfc_formal_arglist * f1, gfc_formal_arglist * f2)
835 {
836
837   gfc_formal_arglist *f2_save, *g;
838   gfc_symbol *sym;
839
840   f2_save = f2;
841
842   while (f1)
843     {
844       if (f1->sym->attr.optional)
845         goto next;
846
847       if (f2 != NULL && compare_type_rank (f1->sym, f2->sym))
848         goto next;
849
850       /* Now search for a disambiguating keyword argument starting at
851          the current non-match.  */
852       for (g = f1; g; g = g->next)
853         {
854           if (g->sym->attr.optional)
855             continue;
856
857           sym = find_keyword_arg (g->sym->name, f2_save);
858           if (sym == NULL || !compare_type_rank (g->sym, sym))
859             return 1;
860         }
861
862     next:
863       f1 = f1->next;
864       if (f2 != NULL)
865         f2 = f2->next;
866     }
867
868   return 0;
869 }
870
871
872 /* 'Compare' two formal interfaces associated with a pair of symbols.
873    We return nonzero if there exists an actual argument list that
874    would be ambiguous between the two interfaces, zero otherwise.  */
875
876 static int
877 compare_interfaces (gfc_symbol * s1, gfc_symbol * s2, int generic_flag)
878 {
879   gfc_formal_arglist *f1, *f2;
880
881   if (s1->attr.function != s2->attr.function
882       && s1->attr.subroutine != s2->attr.subroutine)
883     return 0;                   /* disagreement between function/subroutine */
884
885   f1 = s1->formal;
886   f2 = s2->formal;
887
888   if (f1 == NULL && f2 == NULL)
889     return 1;                   /* Special case */
890
891   if (count_types_test (f1, f2))
892     return 0;
893   if (count_types_test (f2, f1))
894     return 0;
895
896   if (generic_flag)
897     {
898       if (generic_correspondence (f1, f2))
899         return 0;
900       if (generic_correspondence (f2, f1))
901         return 0;
902     }
903   else
904     {
905       if (operator_correspondence (f1, f2))
906         return 0;
907     }
908
909   return 1;
910 }
911
912
913 /* Given a pointer to an interface pointer, remove duplicate
914    interfaces and make sure that all symbols are either functions or
915    subroutines.  Returns nonzero if something goes wrong.  */
916
917 static int
918 check_interface0 (gfc_interface * p, const char *interface_name)
919 {
920   gfc_interface *psave, *q, *qlast;
921
922   psave = p;
923   /* Make sure all symbols in the interface have been defined as
924      functions or subroutines.  */
925   for (; p; p = p->next)
926     if (!p->sym->attr.function && !p->sym->attr.subroutine)
927       {
928         gfc_error ("Procedure '%s' in %s at %L is neither function nor "
929                    "subroutine", p->sym->name, interface_name,
930                    &p->sym->declared_at);
931         return 1;
932       }
933   p = psave;
934
935   /* Remove duplicate interfaces in this interface list.  */
936   for (; p; p = p->next)
937     {
938       qlast = p;
939
940       for (q = p->next; q;)
941         {
942           if (p->sym != q->sym)
943             {
944               qlast = q;
945               q = q->next;
946
947             }
948           else
949             {
950               /* Duplicate interface */
951               qlast->next = q->next;
952               gfc_free (q);
953               q = qlast->next;
954             }
955         }
956     }
957
958   return 0;
959 }
960
961
962 /* Check lists of interfaces to make sure that no two interfaces are
963    ambiguous.  Duplicate interfaces (from the same symbol) are OK
964    here.  */
965
966 static int
967 check_interface1 (gfc_interface * p, gfc_interface * q0,
968                   int generic_flag, const char *interface_name)
969 {
970   gfc_interface * q;
971   for (; p; p = p->next)
972     for (q = q0; q; q = q->next)
973       {
974         if (p->sym == q->sym)
975           continue;             /* Duplicates OK here */
976
977         if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
978           continue;
979
980         if (compare_interfaces (p->sym, q->sym, generic_flag))
981           {
982             gfc_error ("Ambiguous interfaces '%s' and '%s' in %s at %L",
983                        p->sym->name, q->sym->name, interface_name, &p->where);
984             return 1;
985           }
986       }
987
988   return 0;
989 }
990
991
992 /* Check the generic and operator interfaces of symbols to make sure
993    that none of the interfaces conflict.  The check has to be done
994    after all of the symbols are actually loaded.  */
995
996 static void
997 check_sym_interfaces (gfc_symbol * sym)
998 {
999   char interface_name[100];
1000   gfc_symbol *s2;
1001
1002   if (sym->ns != gfc_current_ns)
1003     return;
1004
1005   if (sym->generic != NULL)
1006     {
1007       sprintf (interface_name, "generic interface '%s'", sym->name);
1008       if (check_interface0 (sym->generic, interface_name))
1009         return;
1010
1011       s2 = sym;
1012       while (s2 != NULL)
1013         {
1014           if (check_interface1 (sym->generic, s2->generic, 1, interface_name))
1015             return;
1016
1017           if (s2->ns->parent == NULL)
1018             break;
1019           if (gfc_find_symbol (sym->name, s2->ns->parent, 1, &s2))
1020             break;
1021         }
1022     }
1023 }
1024
1025
1026 static void
1027 check_uop_interfaces (gfc_user_op * uop)
1028 {
1029   char interface_name[100];
1030   gfc_user_op *uop2;
1031   gfc_namespace *ns;
1032
1033   sprintf (interface_name, "operator interface '%s'", uop->name);
1034   if (check_interface0 (uop->operator, interface_name))
1035     return;
1036
1037   for (ns = gfc_current_ns; ns; ns = ns->parent)
1038     {
1039       uop2 = gfc_find_uop (uop->name, ns);
1040       if (uop2 == NULL)
1041         continue;
1042
1043       check_interface1 (uop->operator, uop2->operator, 0, interface_name);
1044     }
1045 }
1046
1047
1048 /* For the namespace, check generic, user operator and intrinsic
1049    operator interfaces for consistency and to remove duplicate
1050    interfaces.  We traverse the whole namespace, counting on the fact
1051    that most symbols will not have generic or operator interfaces.  */
1052
1053 void
1054 gfc_check_interfaces (gfc_namespace * ns)
1055 {
1056   gfc_namespace *old_ns, *ns2;
1057   char interface_name[100];
1058   gfc_intrinsic_op i;
1059
1060   old_ns = gfc_current_ns;
1061   gfc_current_ns = ns;
1062
1063   gfc_traverse_ns (ns, check_sym_interfaces);
1064
1065   gfc_traverse_user_op (ns, check_uop_interfaces);
1066
1067   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
1068     {
1069       if (i == INTRINSIC_USER)
1070         continue;
1071
1072       if (i == INTRINSIC_ASSIGN)
1073         strcpy (interface_name, "intrinsic assignment operator");
1074       else
1075         sprintf (interface_name, "intrinsic '%s' operator",
1076                  gfc_op2string (i));
1077
1078       if (check_interface0 (ns->operator[i], interface_name))
1079         continue;
1080
1081       check_operator_interface (ns->operator[i], i);
1082
1083       for (ns2 = ns->parent; ns2; ns2 = ns2->parent)
1084         if (check_interface1 (ns->operator[i], ns2->operator[i], 0,
1085                               interface_name))
1086           break;
1087     }
1088
1089   gfc_current_ns = old_ns;
1090 }
1091
1092
1093 static int
1094 symbol_rank (gfc_symbol * sym)
1095 {
1096
1097   return (sym->as == NULL) ? 0 : sym->as->rank;
1098 }
1099
1100
1101 /* Given a symbol of a formal argument list and an expression, if the
1102    formal argument is allocatable, check that the actual argument is
1103    allocatable. Returns nonzero if compatible, zero if not compatible.  */
1104
1105 static int
1106 compare_allocatable (gfc_symbol * formal, gfc_expr * actual)
1107 {
1108   symbol_attribute attr;
1109
1110   if (formal->attr.allocatable)
1111     {
1112       attr = gfc_expr_attr (actual);
1113       if (!attr.allocatable)
1114         return 0;
1115     }
1116
1117   return 1;
1118 }
1119
1120
1121 /* Given a symbol of a formal argument list and an expression, if the
1122    formal argument is a pointer, see if the actual argument is a
1123    pointer. Returns nonzero if compatible, zero if not compatible.  */
1124
1125 static int
1126 compare_pointer (gfc_symbol * formal, gfc_expr * actual)
1127 {
1128   symbol_attribute attr;
1129
1130   if (formal->attr.pointer)
1131     {
1132       attr = gfc_expr_attr (actual);
1133       if (!attr.pointer)
1134         return 0;
1135     }
1136
1137   return 1;
1138 }
1139
1140
1141 /* Given a symbol of a formal argument list and an expression, see if
1142    the two are compatible as arguments.  Returns nonzero if
1143    compatible, zero if not compatible.  */
1144
1145 static int
1146 compare_parameter (gfc_symbol * formal, gfc_expr * actual,
1147                    int ranks_must_agree, int is_elemental)
1148 {
1149   gfc_ref *ref;
1150
1151   if (actual->ts.type == BT_PROCEDURE)
1152     {
1153       if (formal->attr.flavor != FL_PROCEDURE)
1154         return 0;
1155
1156       if (formal->attr.function
1157           && !compare_type_rank (formal, actual->symtree->n.sym))
1158         return 0;
1159
1160       if (formal->attr.if_source == IFSRC_UNKNOWN
1161             || actual->symtree->n.sym->attr.external)
1162         return 1;               /* Assume match */
1163
1164       return compare_interfaces (formal, actual->symtree->n.sym, 0);
1165     }
1166
1167   if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
1168       && !gfc_compare_types (&formal->ts, &actual->ts))
1169     return 0;
1170
1171   if (symbol_rank (formal) == actual->rank)
1172     return 1;
1173
1174   /* At this point the ranks didn't agree.  */
1175   if (ranks_must_agree || formal->attr.pointer)
1176     return 0;
1177
1178   if (actual->rank != 0)
1179     return is_elemental || formal->attr.dimension;
1180
1181   /* At this point, we are considering a scalar passed to an array.
1182      This is legal if the scalar is an array element of the right sort.  */
1183   if (formal->as->type == AS_ASSUMED_SHAPE)
1184     return 0;
1185
1186   for (ref = actual->ref; ref; ref = ref->next)
1187     if (ref->type == REF_SUBSTRING)
1188       return 0;
1189
1190   for (ref = actual->ref; ref; ref = ref->next)
1191     if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT)
1192       break;
1193
1194   if (ref == NULL)
1195     return 0;                   /* Not an array element */
1196
1197   return 1;
1198 }
1199
1200
1201 /* Given formal and actual argument lists, see if they are compatible.
1202    If they are compatible, the actual argument list is sorted to
1203    correspond with the formal list, and elements for missing optional
1204    arguments are inserted. If WHERE pointer is nonnull, then we issue
1205    errors when things don't match instead of just returning the status
1206    code.  */
1207
1208 static int
1209 compare_actual_formal (gfc_actual_arglist ** ap,
1210                        gfc_formal_arglist * formal,
1211                        int ranks_must_agree, int is_elemental, locus * where)
1212 {
1213   gfc_actual_arglist **new, *a, *actual, temp;
1214   gfc_formal_arglist *f;
1215   gfc_gsymbol *gsym;
1216   int i, n, na;
1217   bool rank_check;
1218
1219   actual = *ap;
1220
1221   if (actual == NULL && formal == NULL)
1222     return 1;
1223
1224   n = 0;
1225   for (f = formal; f; f = f->next)
1226     n++;
1227
1228   new = (gfc_actual_arglist **) alloca (n * sizeof (gfc_actual_arglist *));
1229
1230   for (i = 0; i < n; i++)
1231     new[i] = NULL;
1232
1233   na = 0;
1234   f = formal;
1235   i = 0;
1236
1237   for (a = actual; a; a = a->next, f = f->next)
1238     {
1239       if (a->name != NULL)
1240         {
1241           i = 0;
1242           for (f = formal; f; f = f->next, i++)
1243             {
1244               if (f->sym == NULL)
1245                 continue;
1246               if (strcmp (f->sym->name, a->name) == 0)
1247                 break;
1248             }
1249
1250           if (f == NULL)
1251             {
1252               if (where)
1253                 gfc_error
1254                   ("Keyword argument '%s' at %L is not in the procedure",
1255                    a->name, &a->expr->where);
1256               return 0;
1257             }
1258
1259           if (new[i] != NULL)
1260             {
1261               if (where)
1262                 gfc_error
1263                   ("Keyword argument '%s' at %L is already associated "
1264                    "with another actual argument", a->name, &a->expr->where);
1265               return 0;
1266             }
1267         }
1268
1269       if (f == NULL)
1270         {
1271           if (where)
1272             gfc_error
1273               ("More actual than formal arguments in procedure call at %L",
1274                where);
1275
1276           return 0;
1277         }
1278
1279       if (f->sym == NULL && a->expr == NULL)
1280         goto match;
1281
1282       if (f->sym == NULL)
1283         {
1284           if (where)
1285             gfc_error
1286               ("Missing alternate return spec in subroutine call at %L",
1287                where);
1288           return 0;
1289         }
1290
1291       if (a->expr == NULL)
1292         {
1293           if (where)
1294             gfc_error
1295               ("Unexpected alternate return spec in subroutine call at %L",
1296                where);
1297           return 0;
1298         }
1299
1300       rank_check = where != NULL
1301                      && !is_elemental
1302                      && f->sym->as
1303                      && (f->sym->as->type == AS_ASSUMED_SHAPE
1304                            || f->sym->as->type == AS_DEFERRED);
1305
1306       if (!compare_parameter
1307           (f->sym, a->expr, ranks_must_agree || rank_check, is_elemental))
1308         {
1309           if (where)
1310             gfc_error ("Type/rank mismatch in argument '%s' at %L",
1311                        f->sym->name, &a->expr->where);
1312           return 0;
1313         }
1314
1315       /* Satisfy 12.4.1.2 by ensuring that a procedure actual argument is
1316          provided for a procedure formal argument.  */
1317       if (a->expr->ts.type != BT_PROCEDURE
1318           && a->expr->expr_type == EXPR_VARIABLE
1319           && f->sym->attr.flavor == FL_PROCEDURE)
1320         {
1321           gsym = gfc_find_gsymbol (gfc_gsym_root,
1322                                    a->expr->symtree->n.sym->name);
1323           if (gsym == NULL || (gsym->type != GSYM_FUNCTION
1324                 && gsym->type != GSYM_SUBROUTINE))
1325             {
1326               if (where)
1327                 gfc_error ("Expected a procedure for argument '%s' at %L",
1328                            f->sym->name, &a->expr->where);
1329               return 0;
1330             }
1331         }
1332
1333       if (f->sym->attr.flavor == FL_PROCEDURE
1334             && f->sym->attr.pure
1335             && a->expr->ts.type == BT_PROCEDURE
1336             && !a->expr->symtree->n.sym->attr.pure)
1337         {
1338           if (where)
1339             gfc_error ("Expected a PURE procedure for argument '%s' at %L",
1340                        f->sym->name, &a->expr->where);
1341           return 0;
1342         }
1343
1344       if (f->sym->as
1345           && f->sym->as->type == AS_ASSUMED_SHAPE
1346           && a->expr->expr_type == EXPR_VARIABLE
1347           && a->expr->symtree->n.sym->as
1348           && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
1349           && (a->expr->ref == NULL
1350               || (a->expr->ref->type == REF_ARRAY
1351                   && a->expr->ref->u.ar.type == AR_FULL)))
1352         {
1353           if (where)
1354             gfc_error ("Actual argument for '%s' cannot be an assumed-size"
1355                        " array at %L", f->sym->name, where);
1356           return 0;
1357         }
1358
1359       if (a->expr->expr_type != EXPR_NULL
1360           && compare_pointer (f->sym, a->expr) == 0)
1361         {
1362           if (where)
1363             gfc_error ("Actual argument for '%s' must be a pointer at %L",
1364                        f->sym->name, &a->expr->where);
1365           return 0;
1366         }
1367
1368       if (a->expr->expr_type != EXPR_NULL
1369           && compare_allocatable (f->sym, a->expr) == 0)
1370         {
1371           if (where)
1372             gfc_error ("Actual argument for '%s' must be ALLOCATABLE at %L",
1373                        f->sym->name, &a->expr->where);
1374           return 0;
1375         }
1376
1377       /* Check intent = OUT/INOUT for definable actual argument.  */
1378       if (a->expr->expr_type != EXPR_VARIABLE
1379              && (f->sym->attr.intent == INTENT_OUT
1380                    || f->sym->attr.intent == INTENT_INOUT))
1381         {
1382           if (where)
1383             gfc_error ("Actual argument at %L must be definable to "
1384                        "match dummy INTENT = OUT/INOUT", &a->expr->where);
1385           return 0;
1386         }
1387
1388     match:
1389       if (a == actual)
1390         na = i;
1391
1392       new[i++] = a;
1393     }
1394
1395   /* Make sure missing actual arguments are optional.  */
1396   i = 0;
1397   for (f = formal; f; f = f->next, i++)
1398     {
1399       if (new[i] != NULL)
1400         continue;
1401       if (!f->sym->attr.optional)
1402         {
1403           if (where)
1404             gfc_error ("Missing actual argument for argument '%s' at %L",
1405                        f->sym->name, where);
1406           return 0;
1407         }
1408     }
1409
1410   /* The argument lists are compatible.  We now relink a new actual
1411      argument list with null arguments in the right places.  The head
1412      of the list remains the head.  */
1413   for (i = 0; i < n; i++)
1414     if (new[i] == NULL)
1415       new[i] = gfc_get_actual_arglist ();
1416
1417   if (na != 0)
1418     {
1419       temp = *new[0];
1420       *new[0] = *actual;
1421       *actual = temp;
1422
1423       a = new[0];
1424       new[0] = new[na];
1425       new[na] = a;
1426     }
1427
1428   for (i = 0; i < n - 1; i++)
1429     new[i]->next = new[i + 1];
1430
1431   new[i]->next = NULL;
1432
1433   if (*ap == NULL && n > 0)
1434     *ap = new[0];
1435
1436   /* Note the types of omitted optional arguments.  */
1437   for (a = actual, f = formal; a; a = a->next, f = f->next)
1438     if (a->expr == NULL && a->label == NULL)
1439       a->missing_arg_type = f->sym->ts.type;
1440
1441   return 1;
1442 }
1443
1444
1445 typedef struct
1446 {
1447   gfc_formal_arglist *f;
1448   gfc_actual_arglist *a;
1449 }
1450 argpair;
1451
1452 /* qsort comparison function for argument pairs, with the following
1453    order:
1454     - p->a->expr == NULL
1455     - p->a->expr->expr_type != EXPR_VARIABLE
1456     - growing p->a->expr->symbol.  */
1457
1458 static int
1459 pair_cmp (const void *p1, const void *p2)
1460 {
1461   const gfc_actual_arglist *a1, *a2;
1462
1463   /* *p1 and *p2 are elements of the to-be-sorted array.  */
1464   a1 = ((const argpair *) p1)->a;
1465   a2 = ((const argpair *) p2)->a;
1466   if (!a1->expr)
1467     {
1468       if (!a2->expr)
1469         return 0;
1470       return -1;
1471     }
1472   if (!a2->expr)
1473     return 1;
1474   if (a1->expr->expr_type != EXPR_VARIABLE)
1475     {
1476       if (a2->expr->expr_type != EXPR_VARIABLE)
1477         return 0;
1478       return -1;
1479     }
1480   if (a2->expr->expr_type != EXPR_VARIABLE)
1481     return 1;
1482   return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
1483 }
1484
1485
1486 /* Given two expressions from some actual arguments, test whether they
1487    refer to the same expression. The analysis is conservative.
1488    Returning FAILURE will produce no warning.  */
1489
1490 static try
1491 compare_actual_expr (gfc_expr * e1, gfc_expr * e2)
1492 {
1493   const gfc_ref *r1, *r2;
1494
1495   if (!e1 || !e2
1496       || e1->expr_type != EXPR_VARIABLE
1497       || e2->expr_type != EXPR_VARIABLE
1498       || e1->symtree->n.sym != e2->symtree->n.sym)
1499     return FAILURE;
1500
1501   /* TODO: improve comparison, see expr.c:show_ref().  */
1502   for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
1503     {
1504       if (r1->type != r2->type)
1505         return FAILURE;
1506       switch (r1->type)
1507         {
1508         case REF_ARRAY:
1509           if (r1->u.ar.type != r2->u.ar.type)
1510             return FAILURE;
1511           /* TODO: At the moment, consider only full arrays;
1512              we could do better.  */
1513           if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
1514             return FAILURE;
1515           break;
1516
1517         case REF_COMPONENT:
1518           if (r1->u.c.component != r2->u.c.component)
1519             return FAILURE;
1520           break;
1521
1522         case REF_SUBSTRING:
1523           return FAILURE;
1524
1525         default:
1526           gfc_internal_error ("compare_actual_expr(): Bad component code");
1527         }
1528     }
1529   if (!r1 && !r2)
1530     return SUCCESS;
1531   return FAILURE;
1532 }
1533
1534 /* Given formal and actual argument lists that correspond to one
1535    another, check that identical actual arguments aren't not
1536    associated with some incompatible INTENTs.  */
1537
1538 static try
1539 check_some_aliasing (gfc_formal_arglist * f, gfc_actual_arglist * a)
1540 {
1541   sym_intent f1_intent, f2_intent;
1542   gfc_formal_arglist *f1;
1543   gfc_actual_arglist *a1;
1544   size_t n, i, j;
1545   argpair *p;
1546   try t = SUCCESS;
1547
1548   n = 0;
1549   for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
1550     {
1551       if (f1 == NULL && a1 == NULL)
1552         break;
1553       if (f1 == NULL || a1 == NULL)
1554         gfc_internal_error ("check_some_aliasing(): List mismatch");
1555       n++;
1556     }
1557   if (n == 0)
1558     return t;
1559   p = (argpair *) alloca (n * sizeof (argpair));
1560
1561   for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
1562     {
1563       p[i].f = f1;
1564       p[i].a = a1;
1565     }
1566
1567   qsort (p, n, sizeof (argpair), pair_cmp);
1568
1569   for (i = 0; i < n; i++)
1570     {
1571       if (!p[i].a->expr
1572           || p[i].a->expr->expr_type != EXPR_VARIABLE
1573           || p[i].a->expr->ts.type == BT_PROCEDURE)
1574         continue;
1575       f1_intent = p[i].f->sym->attr.intent;
1576       for (j = i + 1; j < n; j++)
1577         {
1578           /* Expected order after the sort.  */
1579           if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
1580             gfc_internal_error ("check_some_aliasing(): corrupted data");
1581
1582           /* Are the expression the same?  */
1583           if (compare_actual_expr (p[i].a->expr, p[j].a->expr) == FAILURE)
1584             break;
1585           f2_intent = p[j].f->sym->attr.intent;
1586           if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
1587               || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN))
1588             {
1589               gfc_warning ("Same actual argument associated with INTENT(%s) "
1590                            "argument '%s' and INTENT(%s) argument '%s' at %L",
1591                            gfc_intent_string (f1_intent), p[i].f->sym->name,
1592                            gfc_intent_string (f2_intent), p[j].f->sym->name,
1593                            &p[i].a->expr->where);
1594               t = FAILURE;
1595             }
1596         }
1597     }
1598
1599   return t;
1600 }
1601
1602
1603 /* Given formal and actual argument lists that correspond to one
1604    another, check that they are compatible in the sense that intents
1605    are not mismatched.  */
1606
1607 static try
1608 check_intents (gfc_formal_arglist * f, gfc_actual_arglist * a)
1609 {
1610   sym_intent a_intent, f_intent;
1611
1612   for (;; f = f->next, a = a->next)
1613     {
1614       if (f == NULL && a == NULL)
1615         break;
1616       if (f == NULL || a == NULL)
1617         gfc_internal_error ("check_intents(): List mismatch");
1618
1619       if (a->expr == NULL || a->expr->expr_type != EXPR_VARIABLE)
1620         continue;
1621
1622       a_intent = a->expr->symtree->n.sym->attr.intent;
1623       f_intent = f->sym->attr.intent;
1624
1625       if (a_intent == INTENT_IN
1626           && (f_intent == INTENT_INOUT
1627               || f_intent == INTENT_OUT))
1628         {
1629
1630           gfc_error ("Procedure argument at %L is INTENT(IN) while interface "
1631                      "specifies INTENT(%s)", &a->expr->where,
1632                      gfc_intent_string (f_intent));
1633           return FAILURE;
1634         }
1635
1636       if (gfc_pure (NULL) && gfc_impure_variable (a->expr->symtree->n.sym))
1637         {
1638           if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
1639             {
1640               gfc_error
1641                 ("Procedure argument at %L is local to a PURE procedure and "
1642                  "is passed to an INTENT(%s) argument", &a->expr->where,
1643                  gfc_intent_string (f_intent));
1644               return FAILURE;
1645             }
1646
1647           if (a->expr->symtree->n.sym->attr.pointer)
1648             {
1649               gfc_error
1650                 ("Procedure argument at %L is local to a PURE procedure and "
1651                  "has the POINTER attribute", &a->expr->where);
1652               return FAILURE;
1653             }
1654         }
1655     }
1656
1657   return SUCCESS;
1658 }
1659
1660
1661 /* Check how a procedure is used against its interface.  If all goes
1662    well, the actual argument list will also end up being properly
1663    sorted.  */
1664
1665 void
1666 gfc_procedure_use (gfc_symbol * sym, gfc_actual_arglist ** ap, locus * where)
1667 {
1668
1669   /* Warn about calls with an implicit interface.  */
1670   if (gfc_option.warn_implicit_interface
1671       && sym->attr.if_source == IFSRC_UNKNOWN)
1672     gfc_warning ("Procedure '%s' called with an implicit interface at %L",
1673                  sym->name, where);
1674
1675   if (sym->attr.if_source == IFSRC_UNKNOWN
1676       || !compare_actual_formal (ap, sym->formal, 0,
1677                                  sym->attr.elemental, where))
1678     return;
1679
1680   check_intents (sym->formal, *ap);
1681   if (gfc_option.warn_aliasing)
1682     check_some_aliasing (sym->formal, *ap);
1683 }
1684
1685
1686 /* Given an interface pointer and an actual argument list, search for
1687    a formal argument list that matches the actual.  If found, returns
1688    a pointer to the symbol of the correct interface.  Returns NULL if
1689    not found.  */
1690
1691 gfc_symbol *
1692 gfc_search_interface (gfc_interface * intr, int sub_flag,
1693                       gfc_actual_arglist ** ap)
1694 {
1695   int r;
1696
1697   for (; intr; intr = intr->next)
1698     {
1699       if (sub_flag && intr->sym->attr.function)
1700         continue;
1701       if (!sub_flag && intr->sym->attr.subroutine)
1702         continue;
1703
1704       r = !intr->sym->attr.elemental;
1705
1706       if (compare_actual_formal (ap, intr->sym->formal, r, !r, NULL))
1707         {
1708           check_intents (intr->sym->formal, *ap);
1709           if (gfc_option.warn_aliasing)
1710             check_some_aliasing (intr->sym->formal, *ap);
1711           return intr->sym;
1712         }
1713     }
1714
1715   return NULL;
1716 }
1717
1718
1719 /* Do a brute force recursive search for a symbol.  */
1720
1721 static gfc_symtree *
1722 find_symtree0 (gfc_symtree * root, gfc_symbol * sym)
1723 {
1724   gfc_symtree * st;
1725
1726   if (root->n.sym == sym)
1727     return root;
1728
1729   st = NULL;
1730   if (root->left)
1731     st = find_symtree0 (root->left, sym);
1732   if (root->right && ! st)
1733     st = find_symtree0 (root->right, sym);
1734   return st;
1735 }
1736
1737
1738 /* Find a symtree for a symbol.  */
1739
1740 static gfc_symtree *
1741 find_sym_in_symtree (gfc_symbol * sym)
1742 {
1743   gfc_symtree *st;
1744   gfc_namespace *ns;
1745
1746   /* First try to find it by name.  */
1747   gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
1748   if (st && st->n.sym == sym)
1749     return st;
1750
1751   /* if it's been renamed, resort to a brute-force search.  */
1752   /* TODO: avoid having to do this search.  If the symbol doesn't exist
1753      in the symtree for the current namespace, it should probably be added.  */
1754   for (ns = gfc_current_ns; ns; ns = ns->parent)
1755     {
1756       st = find_symtree0 (ns->sym_root, sym);
1757       if (st)
1758         return st;
1759     }
1760   gfc_internal_error ("Unable to find symbol %s", sym->name);
1761   /* Not reached */
1762 }
1763
1764
1765 /* This subroutine is called when an expression is being resolved.
1766    The expression node in question is either a user defined operator
1767    or an intrinsic operator with arguments that aren't compatible
1768    with the operator.  This subroutine builds an actual argument list
1769    corresponding to the operands, then searches for a compatible
1770    interface.  If one is found, the expression node is replaced with
1771    the appropriate function call.  */
1772
1773 try
1774 gfc_extend_expr (gfc_expr * e)
1775 {
1776   gfc_actual_arglist *actual;
1777   gfc_symbol *sym;
1778   gfc_namespace *ns;
1779   gfc_user_op *uop;
1780   gfc_intrinsic_op i;
1781
1782   sym = NULL;
1783
1784   actual = gfc_get_actual_arglist ();
1785   actual->expr = e->value.op.op1;
1786
1787   if (e->value.op.op2 != NULL)
1788     {
1789       actual->next = gfc_get_actual_arglist ();
1790       actual->next->expr = e->value.op.op2;
1791     }
1792
1793   i = fold_unary (e->value.op.operator);
1794
1795   if (i == INTRINSIC_USER)
1796     {
1797       for (ns = gfc_current_ns; ns; ns = ns->parent)
1798         {
1799           uop = gfc_find_uop (e->value.op.uop->name, ns);
1800           if (uop == NULL)
1801             continue;
1802
1803           sym = gfc_search_interface (uop->operator, 0, &actual);
1804           if (sym != NULL)
1805             break;
1806         }
1807     }
1808   else
1809     {
1810       for (ns = gfc_current_ns; ns; ns = ns->parent)
1811         {
1812           sym = gfc_search_interface (ns->operator[i], 0, &actual);
1813           if (sym != NULL)
1814             break;
1815         }
1816     }
1817
1818   if (sym == NULL)
1819     {
1820       /* Don't use gfc_free_actual_arglist() */
1821       if (actual->next != NULL)
1822         gfc_free (actual->next);
1823       gfc_free (actual);
1824
1825       return FAILURE;
1826     }
1827
1828   /* Change the expression node to a function call.  */
1829   e->expr_type = EXPR_FUNCTION;
1830   e->symtree = find_sym_in_symtree (sym);
1831   e->value.function.actual = actual;
1832   e->value.function.esym = NULL;
1833   e->value.function.isym = NULL;
1834   e->value.function.name = NULL;
1835
1836   if (gfc_pure (NULL) && !gfc_pure (sym))
1837     {
1838       gfc_error
1839         ("Function '%s' called in lieu of an operator at %L must be PURE",
1840          sym->name, &e->where);
1841       return FAILURE;
1842     }
1843
1844   if (gfc_resolve_expr (e) == FAILURE)
1845     return FAILURE;
1846
1847   return SUCCESS;
1848 }
1849
1850
1851 /* Tries to replace an assignment code node with a subroutine call to
1852    the subroutine associated with the assignment operator.  Return
1853    SUCCESS if the node was replaced.  On FAILURE, no error is
1854    generated.  */
1855
1856 try
1857 gfc_extend_assign (gfc_code * c, gfc_namespace * ns)
1858 {
1859   gfc_actual_arglist *actual;
1860   gfc_expr *lhs, *rhs;
1861   gfc_symbol *sym;
1862
1863   lhs = c->expr;
1864   rhs = c->expr2;
1865
1866   /* Don't allow an intrinsic assignment to be replaced.  */
1867   if (lhs->ts.type != BT_DERIVED && rhs->ts.type != BT_DERIVED
1868       && (lhs->ts.type == rhs->ts.type
1869           || (gfc_numeric_ts (&lhs->ts)
1870               && gfc_numeric_ts (&rhs->ts))))
1871     return FAILURE;
1872
1873   actual = gfc_get_actual_arglist ();
1874   actual->expr = lhs;
1875
1876   actual->next = gfc_get_actual_arglist ();
1877   actual->next->expr = rhs;
1878
1879   sym = NULL;
1880
1881   for (; ns; ns = ns->parent)
1882     {
1883       sym = gfc_search_interface (ns->operator[INTRINSIC_ASSIGN], 1, &actual);
1884       if (sym != NULL)
1885         break;
1886     }
1887
1888   if (sym == NULL)
1889     {
1890       gfc_free (actual->next);
1891       gfc_free (actual);
1892       return FAILURE;
1893     }
1894
1895   /* Replace the assignment with the call.  */
1896   c->op = EXEC_ASSIGN_CALL;
1897   c->symtree = find_sym_in_symtree (sym);
1898   c->expr = NULL;
1899   c->expr2 = NULL;
1900   c->ext.actual = actual;
1901
1902   return SUCCESS;
1903 }
1904
1905
1906 /* Make sure that the interface just parsed is not already present in
1907    the given interface list.  Ambiguity isn't checked yet since module
1908    procedures can be present without interfaces.  */
1909
1910 static try
1911 check_new_interface (gfc_interface * base, gfc_symbol * new)
1912 {
1913   gfc_interface *ip;
1914
1915   for (ip = base; ip; ip = ip->next)
1916     {
1917       if (ip->sym == new)
1918         {
1919           gfc_error ("Entity '%s' at %C is already present in the interface",
1920                      new->name);
1921           return FAILURE;
1922         }
1923     }
1924
1925   return SUCCESS;
1926 }
1927
1928
1929 /* Add a symbol to the current interface.  */
1930
1931 try
1932 gfc_add_interface (gfc_symbol * new)
1933 {
1934   gfc_interface **head, *intr;
1935   gfc_namespace *ns;
1936   gfc_symbol *sym;
1937
1938   switch (current_interface.type)
1939     {
1940     case INTERFACE_NAMELESS:
1941       return SUCCESS;
1942
1943     case INTERFACE_INTRINSIC_OP:
1944       for (ns = current_interface.ns; ns; ns = ns->parent)
1945         if (check_new_interface (ns->operator[current_interface.op], new)
1946             == FAILURE)
1947           return FAILURE;
1948
1949       head = &current_interface.ns->operator[current_interface.op];
1950       break;
1951
1952     case INTERFACE_GENERIC:
1953       for (ns = current_interface.ns; ns; ns = ns->parent)
1954         {
1955           gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
1956           if (sym == NULL)
1957             continue;
1958
1959           if (check_new_interface (sym->generic, new) == FAILURE)
1960             return FAILURE;
1961         }
1962
1963       head = &current_interface.sym->generic;
1964       break;
1965
1966     case INTERFACE_USER_OP:
1967       if (check_new_interface (current_interface.uop->operator, new) ==
1968           FAILURE)
1969         return FAILURE;
1970
1971       head = &current_interface.uop->operator;
1972       break;
1973
1974     default:
1975       gfc_internal_error ("gfc_add_interface(): Bad interface type");
1976     }
1977
1978   intr = gfc_get_interface ();
1979   intr->sym = new;
1980   intr->where = gfc_current_locus;
1981
1982   intr->next = *head;
1983   *head = intr;
1984
1985   return SUCCESS;
1986 }
1987
1988
1989 /* Gets rid of a formal argument list.  We do not free symbols.
1990    Symbols are freed when a namespace is freed.  */
1991
1992 void
1993 gfc_free_formal_arglist (gfc_formal_arglist * p)
1994 {
1995   gfc_formal_arglist *q;
1996
1997   for (; p; p = q)
1998     {
1999       q = p->next;
2000       gfc_free (p);
2001     }
2002 }