OSDN Git Service

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