OSDN Git Service

2011-11-06 Janus Weil <janus@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / interface.c
1 /* Deal with interfaces.
2    Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009,
3    2010
4    Free Software Foundation, Inc.
5    Contributed by Andy Vaught
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
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 #include "arith.h"
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       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_intrinsic (gfc_intrinsic_op op)
101 {
102   switch (op)
103     {
104     case INTRINSIC_UPLUS:
105       op = INTRINSIC_PLUS;
106       break;
107     case INTRINSIC_UMINUS:
108       op = INTRINSIC_MINUS;
109       break;
110     default:
111       break;
112     }
113
114   return op;
115 }
116
117
118 /* Match a generic specification.  Depending on which type of
119    interface is found, the 'name' or 'op' pointers may be set.
120    This subroutine doesn't return MATCH_NO.  */
121
122 match
123 gfc_match_generic_spec (interface_type *type,
124                         char *name,
125                         gfc_intrinsic_op *op)
126 {
127   char buffer[GFC_MAX_SYMBOL_LEN + 1];
128   match m;
129   gfc_intrinsic_op i;
130
131   if (gfc_match (" assignment ( = )") == MATCH_YES)
132     {
133       *type = INTERFACE_INTRINSIC_OP;
134       *op = INTRINSIC_ASSIGN;
135       return MATCH_YES;
136     }
137
138   if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
139     {                           /* Operator i/f */
140       *type = INTERFACE_INTRINSIC_OP;
141       *op = fold_unary_intrinsic (i);
142       return MATCH_YES;
143     }
144
145   *op = INTRINSIC_NONE;
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 F95 forms of an interface statement.  The
182    matcher for the abstract interface follows.  */
183
184 match
185 gfc_match_interface (void)
186 {
187   char name[GFC_MAX_SYMBOL_LEN + 1];
188   interface_type type;
189   gfc_symbol *sym;
190   gfc_intrinsic_op op;
191   match m;
192
193   m = gfc_match_space ();
194
195   if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
196     return MATCH_ERROR;
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 && m != MATCH_YES))
202     {
203       gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
204                  "at %C");
205       return MATCH_ERROR;
206     }
207
208   current_interface.type = type;
209
210   switch (type)
211     {
212     case INTERFACE_GENERIC:
213       if (gfc_get_symbol (name, NULL, &sym))
214         return MATCH_ERROR;
215
216       if (!sym->attr.generic 
217           && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
218         return MATCH_ERROR;
219
220       if (sym->attr.dummy)
221         {
222           gfc_error ("Dummy procedure '%s' at %C cannot have a "
223                      "generic interface", sym->name);
224           return MATCH_ERROR;
225         }
226
227       current_interface.sym = gfc_new_block = sym;
228       break;
229
230     case INTERFACE_USER_OP:
231       current_interface.uop = gfc_get_uop (name);
232       break;
233
234     case INTERFACE_INTRINSIC_OP:
235       current_interface.op = op;
236       break;
237
238     case INTERFACE_NAMELESS:
239     case INTERFACE_ABSTRACT:
240       break;
241     }
242
243   return MATCH_YES;
244 }
245
246
247
248 /* Match a F2003 abstract interface.  */
249
250 match
251 gfc_match_abstract_interface (void)
252 {
253   match m;
254
255   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT INTERFACE at %C")
256                       == FAILURE)
257     return MATCH_ERROR;
258
259   m = gfc_match_eos ();
260
261   if (m != MATCH_YES)
262     {
263       gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
264       return MATCH_ERROR;
265     }
266
267   current_interface.type = INTERFACE_ABSTRACT;
268
269   return m;
270 }
271
272
273 /* Match the different sort of generic-specs that can be present after
274    the END INTERFACE itself.  */
275
276 match
277 gfc_match_end_interface (void)
278 {
279   char name[GFC_MAX_SYMBOL_LEN + 1];
280   interface_type type;
281   gfc_intrinsic_op op;
282   match m;
283
284   m = gfc_match_space ();
285
286   if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
287     return MATCH_ERROR;
288
289   /* If we're not looking at the end of the statement now, or if this
290      is not a nameless interface but we did not see a space, punt.  */
291   if (gfc_match_eos () != MATCH_YES
292       || (type != INTERFACE_NAMELESS && m != MATCH_YES))
293     {
294       gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
295                  "statement at %C");
296       return MATCH_ERROR;
297     }
298
299   m = MATCH_YES;
300
301   switch (current_interface.type)
302     {
303     case INTERFACE_NAMELESS:
304     case INTERFACE_ABSTRACT:
305       if (type != INTERFACE_NAMELESS)
306         {
307           gfc_error ("Expected a nameless interface at %C");
308           m = MATCH_ERROR;
309         }
310
311       break;
312
313     case INTERFACE_INTRINSIC_OP:
314       if (type != current_interface.type || op != current_interface.op)
315         {
316
317           if (current_interface.op == INTRINSIC_ASSIGN)
318             {
319               m = MATCH_ERROR;
320               gfc_error ("Expected 'END INTERFACE ASSIGNMENT (=)' at %C");
321             }
322           else
323             {
324               const char *s1, *s2;
325               s1 = gfc_op2string (current_interface.op);
326               s2 = gfc_op2string (op);
327
328               /* The following if-statements are used to enforce C1202
329                  from F2003.  */
330               if ((strcmp(s1, "==") == 0 && strcmp(s2, ".eq.") == 0)
331                   || (strcmp(s1, ".eq.") == 0 && strcmp(s2, "==") == 0))
332                 break;
333               if ((strcmp(s1, "/=") == 0 && strcmp(s2, ".ne.") == 0)
334                   || (strcmp(s1, ".ne.") == 0 && strcmp(s2, "/=") == 0))
335                 break;
336               if ((strcmp(s1, "<=") == 0 && strcmp(s2, ".le.") == 0)
337                   || (strcmp(s1, ".le.") == 0 && strcmp(s2, "<=") == 0))
338                 break;
339               if ((strcmp(s1, "<") == 0 && strcmp(s2, ".lt.") == 0)
340                   || (strcmp(s1, ".lt.") == 0 && strcmp(s2, "<") == 0))
341                 break;
342               if ((strcmp(s1, ">=") == 0 && strcmp(s2, ".ge.") == 0)
343                   || (strcmp(s1, ".ge.") == 0 && strcmp(s2, ">=") == 0))
344                 break;
345               if ((strcmp(s1, ">") == 0 && strcmp(s2, ".gt.") == 0)
346                   || (strcmp(s1, ".gt.") == 0 && strcmp(s2, ">") == 0))
347                 break;
348
349               m = MATCH_ERROR;
350               gfc_error ("Expecting 'END INTERFACE OPERATOR (%s)' at %C, "
351                          "but got %s", s1, s2);
352             }
353                 
354         }
355
356       break;
357
358     case INTERFACE_USER_OP:
359       /* Comparing the symbol node names is OK because only use-associated
360          symbols can be renamed.  */
361       if (type != current_interface.type
362           || strcmp (current_interface.uop->name, name) != 0)
363         {
364           gfc_error ("Expecting 'END INTERFACE OPERATOR (.%s.)' at %C",
365                      current_interface.uop->name);
366           m = MATCH_ERROR;
367         }
368
369       break;
370
371     case INTERFACE_GENERIC:
372       if (type != current_interface.type
373           || strcmp (current_interface.sym->name, name) != 0)
374         {
375           gfc_error ("Expecting 'END INTERFACE %s' at %C",
376                      current_interface.sym->name);
377           m = MATCH_ERROR;
378         }
379
380       break;
381     }
382
383   return m;
384 }
385
386
387 /* Compare two derived types using the criteria in 4.4.2 of the standard,
388    recursing through gfc_compare_types for the components.  */
389
390 int
391 gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
392 {
393   gfc_component *dt1, *dt2;
394
395   if (derived1 == derived2)
396     return 1;
397
398   /* Special case for comparing derived types across namespaces.  If the
399      true names and module names are the same and the module name is
400      nonnull, then they are equal.  */
401   if (derived1 != NULL && derived2 != NULL
402       && strcmp (derived1->name, derived2->name) == 0
403       && derived1->module != NULL && derived2->module != NULL
404       && strcmp (derived1->module, derived2->module) == 0)
405     return 1;
406
407   /* Compare type via the rules of the standard.  Both types must have
408      the SEQUENCE or BIND(C) attribute to be equal.  */
409
410   if (strcmp (derived1->name, derived2->name))
411     return 0;
412
413   if (derived1->component_access == ACCESS_PRIVATE
414       || derived2->component_access == ACCESS_PRIVATE)
415     return 0;
416
417   if (!(derived1->attr.sequence && derived2->attr.sequence)
418       && !(derived1->attr.is_bind_c && derived2->attr.is_bind_c))
419     return 0;
420
421   dt1 = derived1->components;
422   dt2 = derived2->components;
423
424   /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
425      simple test can speed things up.  Otherwise, lots of things have to
426      match.  */
427   for (;;)
428     {
429       if (strcmp (dt1->name, dt2->name) != 0)
430         return 0;
431
432       if (dt1->attr.access != dt2->attr.access)
433         return 0;
434
435       if (dt1->attr.pointer != dt2->attr.pointer)
436         return 0;
437
438       if (dt1->attr.dimension != dt2->attr.dimension)
439         return 0;
440
441      if (dt1->attr.allocatable != dt2->attr.allocatable)
442         return 0;
443
444       if (dt1->attr.dimension && gfc_compare_array_spec (dt1->as, dt2->as) == 0)
445         return 0;
446
447       /* Make sure that link lists do not put this function into an 
448          endless recursive loop!  */
449       if (!(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
450             && !(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
451             && gfc_compare_types (&dt1->ts, &dt2->ts) == 0)
452         return 0;
453
454       else if ((dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
455                 && !(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived))
456         return 0;
457
458       else if (!(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
459                 && (dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived))
460         return 0;
461
462       dt1 = dt1->next;
463       dt2 = dt2->next;
464
465       if (dt1 == NULL && dt2 == NULL)
466         break;
467       if (dt1 == NULL || dt2 == NULL)
468         return 0;
469     }
470
471   return 1;
472 }
473
474
475 /* Compare two typespecs, recursively if necessary.  */
476
477 int
478 gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
479 {
480   /* See if one of the typespecs is a BT_VOID, which is what is being used
481      to allow the funcs like c_f_pointer to accept any pointer type.
482      TODO: Possibly should narrow this to just the one typespec coming in
483      that is for the formal arg, but oh well.  */
484   if (ts1->type == BT_VOID || ts2->type == BT_VOID)
485     return 1;
486    
487   if (ts1->type != ts2->type
488       && ((ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
489           || (ts2->type != BT_DERIVED && ts2->type != BT_CLASS)))
490     return 0;
491   if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
492     return (ts1->kind == ts2->kind);
493
494   /* Compare derived types.  */
495   if (gfc_type_compatible (ts1, ts2))
496     return 1;
497
498   return gfc_compare_derived_types (ts1->u.derived ,ts2->u.derived);
499 }
500
501
502 /* Given two symbols that are formal arguments, compare their ranks
503    and types.  Returns nonzero if they have the same rank and type,
504    zero otherwise.  */
505
506 static int
507 compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
508 {
509   int r1, r2;
510
511   r1 = (s1->as != NULL) ? s1->as->rank : 0;
512   r2 = (s2->as != NULL) ? s2->as->rank : 0;
513
514   if (r1 != r2)
515     return 0;                   /* Ranks differ.  */
516
517   return gfc_compare_types (&s1->ts, &s2->ts);
518 }
519
520
521 /* Given two symbols that are formal arguments, compare their types
522    and rank and their formal interfaces if they are both dummy
523    procedures.  Returns nonzero if the same, zero if different.  */
524
525 static int
526 compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
527 {
528   if (s1 == NULL || s2 == NULL)
529     return s1 == s2 ? 1 : 0;
530
531   if (s1 == s2)
532     return 1;
533
534   if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
535     return compare_type_rank (s1, s2);
536
537   if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
538     return 0;
539
540   /* At this point, both symbols are procedures.  It can happen that
541      external procedures are compared, where one is identified by usage
542      to be a function or subroutine but the other is not.  Check TKR
543      nonetheless for these cases.  */
544   if (s1->attr.function == 0 && s1->attr.subroutine == 0)
545     return s1->attr.external == 1 ? compare_type_rank (s1, s2) : 0;
546
547   if (s2->attr.function == 0 && s2->attr.subroutine == 0)
548     return s2->attr.external == 1 ? compare_type_rank (s1, s2) : 0;
549
550   /* Now the type of procedure has been identified.  */
551   if (s1->attr.function != s2->attr.function
552       || s1->attr.subroutine != s2->attr.subroutine)
553     return 0;
554
555   if (s1->attr.function && compare_type_rank (s1, s2) == 0)
556     return 0;
557
558   /* Originally, gfortran recursed here to check the interfaces of passed
559      procedures.  This is explicitly not required by the standard.  */
560   return 1;
561 }
562
563
564 /* Given a formal argument list and a keyword name, search the list
565    for that keyword.  Returns the correct symbol node if found, NULL
566    if not found.  */
567
568 static gfc_symbol *
569 find_keyword_arg (const char *name, gfc_formal_arglist *f)
570 {
571   for (; f; f = f->next)
572     if (strcmp (f->sym->name, name) == 0)
573       return f->sym;
574
575   return NULL;
576 }
577
578
579 /******** Interface checking subroutines **********/
580
581
582 /* Given an operator interface and the operator, make sure that all
583    interfaces for that operator are legal.  */
584
585 bool
586 gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
587                               locus opwhere)
588 {
589   gfc_formal_arglist *formal;
590   sym_intent i1, i2;
591   bt t1, t2;
592   int args, r1, r2, k1, k2;
593
594   gcc_assert (sym);
595
596   args = 0;
597   t1 = t2 = BT_UNKNOWN;
598   i1 = i2 = INTENT_UNKNOWN;
599   r1 = r2 = -1;
600   k1 = k2 = -1;
601
602   for (formal = sym->formal; formal; formal = formal->next)
603     {
604       gfc_symbol *fsym = formal->sym;
605       if (fsym == NULL)
606         {
607           gfc_error ("Alternate return cannot appear in operator "
608                      "interface at %L", &sym->declared_at);
609           return false;
610         }
611       if (args == 0)
612         {
613           t1 = fsym->ts.type;
614           i1 = fsym->attr.intent;
615           r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
616           k1 = fsym->ts.kind;
617         }
618       if (args == 1)
619         {
620           t2 = fsym->ts.type;
621           i2 = fsym->attr.intent;
622           r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
623           k2 = fsym->ts.kind;
624         }
625       args++;
626     }
627
628   /* Only +, - and .not. can be unary operators.
629      .not. cannot be a binary operator.  */
630   if (args == 0 || args > 2 || (args == 1 && op != INTRINSIC_PLUS
631                                 && op != INTRINSIC_MINUS
632                                 && op != INTRINSIC_NOT)
633       || (args == 2 && op == INTRINSIC_NOT))
634     {
635       gfc_error ("Operator interface at %L has the wrong number of arguments",
636                  &sym->declared_at);
637       return false;
638     }
639
640   /* Check that intrinsics are mapped to functions, except
641      INTRINSIC_ASSIGN which should map to a subroutine.  */
642   if (op == INTRINSIC_ASSIGN)
643     {
644       if (!sym->attr.subroutine)
645         {
646           gfc_error ("Assignment operator interface at %L must be "
647                      "a SUBROUTINE", &sym->declared_at);
648           return false;
649         }
650       if (args != 2)
651         {
652           gfc_error ("Assignment operator interface at %L must have "
653                      "two arguments", &sym->declared_at);
654           return false;
655         }
656
657       /* Allowed are (per F2003, 12.3.2.1.2 Defined assignments):
658          - First argument an array with different rank than second,
659          - First argument is a scalar and second an array,
660          - Types and kinds do not conform, or
661          - First argument is of derived type.  */
662       if (sym->formal->sym->ts.type != BT_DERIVED
663           && sym->formal->sym->ts.type != BT_CLASS
664           && (r2 == 0 || r1 == r2)
665           && (sym->formal->sym->ts.type == sym->formal->next->sym->ts.type
666               || (gfc_numeric_ts (&sym->formal->sym->ts)
667                   && gfc_numeric_ts (&sym->formal->next->sym->ts))))
668         {
669           gfc_error ("Assignment operator interface at %L must not redefine "
670                      "an INTRINSIC type assignment", &sym->declared_at);
671           return false;
672         }
673     }
674   else
675     {
676       if (!sym->attr.function)
677         {
678           gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
679                      &sym->declared_at);
680           return false;
681         }
682     }
683
684   /* Check intents on operator interfaces.  */
685   if (op == INTRINSIC_ASSIGN)
686     {
687       if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
688         {
689           gfc_error ("First argument of defined assignment at %L must be "
690                      "INTENT(OUT) or INTENT(INOUT)", &sym->declared_at);
691           return false;
692         }
693
694       if (i2 != INTENT_IN)
695         {
696           gfc_error ("Second argument of defined assignment at %L must be "
697                      "INTENT(IN)", &sym->declared_at);
698           return false;
699         }
700     }
701   else
702     {
703       if (i1 != INTENT_IN)
704         {
705           gfc_error ("First argument of operator interface at %L must be "
706                      "INTENT(IN)", &sym->declared_at);
707           return false;
708         }
709
710       if (args == 2 && i2 != INTENT_IN)
711         {
712           gfc_error ("Second argument of operator interface at %L must be "
713                      "INTENT(IN)", &sym->declared_at);
714           return false;
715         }
716     }
717
718   /* From now on, all we have to do is check that the operator definition
719      doesn't conflict with an intrinsic operator. The rules for this
720      game are defined in 7.1.2 and 7.1.3 of both F95 and F2003 standards,
721      as well as 12.3.2.1.1 of Fortran 2003:
722
723      "If the operator is an intrinsic-operator (R310), the number of
724      function arguments shall be consistent with the intrinsic uses of
725      that operator, and the types, kind type parameters, or ranks of the
726      dummy arguments shall differ from those required for the intrinsic
727      operation (7.1.2)."  */
728
729 #define IS_NUMERIC_TYPE(t) \
730   ((t) == BT_INTEGER || (t) == BT_REAL || (t) == BT_COMPLEX)
731
732   /* Unary ops are easy, do them first.  */
733   if (op == INTRINSIC_NOT)
734     {
735       if (t1 == BT_LOGICAL)
736         goto bad_repl;
737       else
738         return true;
739     }
740
741   if (args == 1 && (op == INTRINSIC_PLUS || op == INTRINSIC_MINUS))
742     {
743       if (IS_NUMERIC_TYPE (t1))
744         goto bad_repl;
745       else
746         return true;
747     }
748
749   /* Character intrinsic operators have same character kind, thus
750      operator definitions with operands of different character kinds
751      are always safe.  */
752   if (t1 == BT_CHARACTER && t2 == BT_CHARACTER && k1 != k2)
753     return true;
754
755   /* Intrinsic operators always perform on arguments of same rank,
756      so different ranks is also always safe.  (rank == 0) is an exception
757      to that, because all intrinsic operators are elemental.  */
758   if (r1 != r2 && r1 != 0 && r2 != 0)
759     return true;
760
761   switch (op)
762   {
763     case INTRINSIC_EQ:
764     case INTRINSIC_EQ_OS:
765     case INTRINSIC_NE:
766     case INTRINSIC_NE_OS:
767       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
768         goto bad_repl;
769       /* Fall through.  */
770
771     case INTRINSIC_PLUS:
772     case INTRINSIC_MINUS:
773     case INTRINSIC_TIMES:
774     case INTRINSIC_DIVIDE:
775     case INTRINSIC_POWER:
776       if (IS_NUMERIC_TYPE (t1) && IS_NUMERIC_TYPE (t2))
777         goto bad_repl;
778       break;
779
780     case INTRINSIC_GT:
781     case INTRINSIC_GT_OS:
782     case INTRINSIC_GE:
783     case INTRINSIC_GE_OS:
784     case INTRINSIC_LT:
785     case INTRINSIC_LT_OS:
786     case INTRINSIC_LE:
787     case INTRINSIC_LE_OS:
788       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
789         goto bad_repl;
790       if ((t1 == BT_INTEGER || t1 == BT_REAL)
791           && (t2 == BT_INTEGER || t2 == BT_REAL))
792         goto bad_repl;
793       break;
794
795     case INTRINSIC_CONCAT:
796       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
797         goto bad_repl;
798       break;
799
800     case INTRINSIC_AND:
801     case INTRINSIC_OR:
802     case INTRINSIC_EQV:
803     case INTRINSIC_NEQV:
804       if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
805         goto bad_repl;
806       break;
807
808     default:
809       break;
810   }
811
812   return true;
813
814 #undef IS_NUMERIC_TYPE
815
816 bad_repl:
817   gfc_error ("Operator interface at %L conflicts with intrinsic interface",
818              &opwhere);
819   return false;
820 }
821
822
823 /* Given a pair of formal argument lists, we see if the two lists can
824    be distinguished by counting the number of nonoptional arguments of
825    a given type/rank in f1 and seeing if there are less then that
826    number of those arguments in f2 (including optional arguments).
827    Since this test is asymmetric, it has to be called twice to make it
828    symmetric.  Returns nonzero if the argument lists are incompatible
829    by this test.  This subroutine implements rule 1 of section
830    14.1.2.3 in the Fortran 95 standard.  */
831
832 static int
833 count_types_test (gfc_formal_arglist *f1, gfc_formal_arglist *f2)
834 {
835   int rc, ac1, ac2, i, j, k, n1;
836   gfc_formal_arglist *f;
837
838   typedef struct
839   {
840     int flag;
841     gfc_symbol *sym;
842   }
843   arginfo;
844
845   arginfo *arg;
846
847   n1 = 0;
848
849   for (f = f1; f; f = f->next)
850     n1++;
851
852   /* Build an array of integers that gives the same integer to
853      arguments of the same type/rank.  */
854   arg = XCNEWVEC (arginfo, n1);
855
856   f = f1;
857   for (i = 0; i < n1; i++, f = f->next)
858     {
859       arg[i].flag = -1;
860       arg[i].sym = f->sym;
861     }
862
863   k = 0;
864
865   for (i = 0; i < n1; i++)
866     {
867       if (arg[i].flag != -1)
868         continue;
869
870       if (arg[i].sym && arg[i].sym->attr.optional)
871         continue;               /* Skip optional arguments.  */
872
873       arg[i].flag = k;
874
875       /* Find other nonoptional arguments of the same type/rank.  */
876       for (j = i + 1; j < n1; j++)
877         if ((arg[j].sym == NULL || !arg[j].sym->attr.optional)
878             && (compare_type_rank_if (arg[i].sym, arg[j].sym)
879                 || compare_type_rank_if (arg[j].sym, arg[i].sym)))
880           arg[j].flag = k;
881
882       k++;
883     }
884
885   /* Now loop over each distinct type found in f1.  */
886   k = 0;
887   rc = 0;
888
889   for (i = 0; i < n1; i++)
890     {
891       if (arg[i].flag != k)
892         continue;
893
894       ac1 = 1;
895       for (j = i + 1; j < n1; j++)
896         if (arg[j].flag == k)
897           ac1++;
898
899       /* Count the number of arguments in f2 with that type, including
900          those that are optional.  */
901       ac2 = 0;
902
903       for (f = f2; f; f = f->next)
904         if (compare_type_rank_if (arg[i].sym, f->sym)
905             || compare_type_rank_if (f->sym, arg[i].sym))
906           ac2++;
907
908       if (ac1 > ac2)
909         {
910           rc = 1;
911           break;
912         }
913
914       k++;
915     }
916
917   free (arg);
918
919   return rc;
920 }
921
922
923 /* Perform the correspondence test in rule 2 of section 14.1.2.3.
924    Returns zero if no argument is found that satisfies rule 2, nonzero
925    otherwise.
926
927    This test is also not symmetric in f1 and f2 and must be called
928    twice.  This test finds problems caused by sorting the actual
929    argument list with keywords.  For example:
930
931    INTERFACE FOO
932        SUBROUTINE F1(A, B)
933            INTEGER :: A ; REAL :: B
934        END SUBROUTINE F1
935
936        SUBROUTINE F2(B, A)
937            INTEGER :: A ; REAL :: B
938        END SUBROUTINE F1
939    END INTERFACE FOO
940
941    At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous.  */
942
943 static int
944 generic_correspondence (gfc_formal_arglist *f1, gfc_formal_arglist *f2)
945 {
946   gfc_formal_arglist *f2_save, *g;
947   gfc_symbol *sym;
948
949   f2_save = f2;
950
951   while (f1)
952     {
953       if (f1->sym->attr.optional)
954         goto next;
955
956       if (f2 != NULL && (compare_type_rank (f1->sym, f2->sym)
957                          || compare_type_rank (f2->sym, f1->sym)))
958         goto next;
959
960       /* Now search for a disambiguating keyword argument starting at
961          the current non-match.  */
962       for (g = f1; g; g = g->next)
963         {
964           if (g->sym->attr.optional)
965             continue;
966
967           sym = find_keyword_arg (g->sym->name, f2_save);
968           if (sym == NULL || !compare_type_rank (g->sym, sym))
969             return 1;
970         }
971
972     next:
973       f1 = f1->next;
974       if (f2 != NULL)
975         f2 = f2->next;
976     }
977
978   return 0;
979 }
980
981
982 /* Check if the characteristics of two dummy arguments match,
983    cf. F08:12.3.2.  */
984
985 static gfc_try
986 check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
987                              bool type_must_agree, char *errmsg, int err_len)
988 {
989   /* Check type and rank.  */
990   if (type_must_agree && !compare_type_rank (s2, s1))
991     {
992       if (errmsg != NULL)
993         snprintf (errmsg, err_len, "Type/rank mismatch in argument '%s'",
994                   s1->name);
995       return FAILURE;
996     }
997
998   /* Check INTENT.  */
999   if (s1->attr.intent != s2->attr.intent)
1000     {
1001       snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
1002                 s1->name);
1003       return FAILURE;
1004     }
1005
1006   /* Check OPTIONAL attribute.  */
1007   if (s1->attr.optional != s2->attr.optional)
1008     {
1009       snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
1010                 s1->name);
1011       return FAILURE;
1012     }
1013
1014   /* Check ALLOCATABLE attribute.  */
1015   if (s1->attr.allocatable != s2->attr.allocatable)
1016     {
1017       snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
1018                 s1->name);
1019       return FAILURE;
1020     }
1021
1022   /* Check POINTER attribute.  */
1023   if (s1->attr.pointer != s2->attr.pointer)
1024     {
1025       snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
1026                 s1->name);
1027       return FAILURE;
1028     }
1029
1030   /* Check TARGET attribute.  */
1031   if (s1->attr.target != s2->attr.target)
1032     {
1033       snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
1034                 s1->name);
1035       return FAILURE;
1036     }
1037
1038   /* FIXME: Do more comprehensive testing of attributes, like e.g.
1039             ASYNCHRONOUS, CONTIGUOUS, VALUE, VOLATILE, etc.  */
1040
1041   /* Check string length.  */
1042   if (s1->ts.type == BT_CHARACTER
1043       && s1->ts.u.cl && s1->ts.u.cl->length
1044       && s2->ts.u.cl && s2->ts.u.cl->length)
1045     {
1046       int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
1047                                           s2->ts.u.cl->length);
1048       switch (compval)
1049       {
1050         case -1:
1051         case  1:
1052         case -3:
1053           snprintf (errmsg, err_len, "Character length mismatch "
1054                     "in argument '%s'", s1->name);
1055           return FAILURE;
1056
1057         case -2:
1058           /* FIXME: Implement a warning for this case.
1059           gfc_warning ("Possible character length mismatch in argument '%s'",
1060                        s1->name);*/
1061           break;
1062
1063         case 0:
1064           break;
1065
1066         default:
1067           gfc_internal_error ("check_dummy_characteristics: Unexpected result "
1068                               "%i of gfc_dep_compare_expr", compval);
1069           break;
1070       }
1071     }
1072
1073   /* Check array shape.  */
1074   if (s1->as && s2->as)
1075     {
1076       int i, compval;
1077       gfc_expr *shape1, *shape2;
1078
1079       if (s1->as->type != s2->as->type)
1080         {
1081           snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
1082                     s1->name);
1083           return FAILURE;
1084         }
1085
1086       if (s1->as->type == AS_EXPLICIT)
1087         for (i = 0; i < s1->as->rank + s1->as->corank; i++)
1088           {
1089             shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
1090                                   gfc_copy_expr (s1->as->lower[i]));
1091             shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
1092                                   gfc_copy_expr (s2->as->lower[i]));
1093             compval = gfc_dep_compare_expr (shape1, shape2);
1094             gfc_free_expr (shape1);
1095             gfc_free_expr (shape2);
1096             switch (compval)
1097             {
1098               case -1:
1099               case  1:
1100               case -3:
1101                 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
1102                           "argument '%s'", i + 1, s1->name);
1103                 return FAILURE;
1104
1105               case -2:
1106                 /* FIXME: Implement a warning for this case.
1107                 gfc_warning ("Possible shape mismatch in argument '%s'",
1108                             s1->name);*/
1109                 break;
1110
1111               case 0:
1112                 break;
1113
1114               default:
1115                 gfc_internal_error ("check_dummy_characteristics: Unexpected "
1116                                     "result %i of gfc_dep_compare_expr",
1117                                     compval);
1118                 break;
1119             }
1120           }
1121     }
1122     
1123   return SUCCESS;
1124 }
1125
1126
1127 /* 'Compare' two formal interfaces associated with a pair of symbols.
1128    We return nonzero if there exists an actual argument list that
1129    would be ambiguous between the two interfaces, zero otherwise.
1130    'strict_flag' specifies whether all the characteristics are
1131    required to match, which is not the case for ambiguity checks.*/
1132
1133 int
1134 gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
1135                         int generic_flag, int strict_flag,
1136                         char *errmsg, int err_len)
1137 {
1138   gfc_formal_arglist *f1, *f2;
1139
1140   gcc_assert (name2 != NULL);
1141
1142   if (s1->attr.function && (s2->attr.subroutine
1143       || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
1144           && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
1145     {
1146       if (errmsg != NULL)
1147         snprintf (errmsg, err_len, "'%s' is not a function", name2);
1148       return 0;
1149     }
1150
1151   if (s1->attr.subroutine && s2->attr.function)
1152     {
1153       if (errmsg != NULL)
1154         snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
1155       return 0;
1156     }
1157
1158   /* Do strict checks on all characteristics
1159      (for dummy procedures and procedure pointer assignments).  */
1160   if (!generic_flag && strict_flag)
1161     {
1162       if (s1->attr.function && s2->attr.function)
1163         {
1164           /* If both are functions, check result type.  */
1165           if (s1->ts.type == BT_UNKNOWN)
1166             return 1;
1167           if (!compare_type_rank (s1,s2))
1168             {
1169               if (errmsg != NULL)
1170                 snprintf (errmsg, err_len, "Type/rank mismatch in return value "
1171                           "of '%s'", name2);
1172               return 0;
1173             }
1174
1175           /* FIXME: Check array bounds and string length of result.  */
1176         }
1177
1178       if (s1->attr.pure && !s2->attr.pure)
1179         {
1180           snprintf (errmsg, err_len, "Mismatch in PURE attribute");
1181           return 0;
1182         }
1183       if (s1->attr.elemental && !s2->attr.elemental)
1184         {
1185           snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
1186           return 0;
1187         }
1188     }
1189
1190   if (s1->attr.if_source == IFSRC_UNKNOWN
1191       || s2->attr.if_source == IFSRC_UNKNOWN)
1192     return 1;
1193
1194   f1 = s1->formal;
1195   f2 = s2->formal;
1196
1197   if (f1 == NULL && f2 == NULL)
1198     return 1;                   /* Special case: No arguments.  */
1199
1200   if (generic_flag)
1201     {
1202       if (count_types_test (f1, f2) || count_types_test (f2, f1))
1203         return 0;
1204       if (generic_correspondence (f1, f2) || generic_correspondence (f2, f1))
1205         return 0;
1206     }
1207   else
1208     /* Perform the abbreviated correspondence test for operators (the
1209        arguments cannot be optional and are always ordered correctly).
1210        This is also done when comparing interfaces for dummy procedures and in
1211        procedure pointer assignments.  */
1212
1213     for (;;)
1214       {
1215         /* Check existence.  */
1216         if (f1 == NULL && f2 == NULL)
1217           break;
1218         if (f1 == NULL || f2 == NULL)
1219           {
1220             if (errmsg != NULL)
1221               snprintf (errmsg, err_len, "'%s' has the wrong number of "
1222                         "arguments", name2);
1223             return 0;
1224           }
1225
1226         if (strict_flag)
1227           {
1228             /* Check all characteristics.  */
1229             if (check_dummy_characteristics (f1->sym, f2->sym,
1230                                              true, errmsg, err_len) == FAILURE)
1231               return 0;
1232           }
1233         else if (!compare_type_rank (f2->sym, f1->sym))
1234           {
1235             /* Only check type and rank.  */
1236             if (errmsg != NULL)
1237               snprintf (errmsg, err_len, "Type/rank mismatch in argument '%s'",
1238                         f1->sym->name);
1239             return 0;
1240           }
1241
1242         f1 = f1->next;
1243         f2 = f2->next;
1244       }
1245
1246   return 1;
1247 }
1248
1249
1250 /* Given a pointer to an interface pointer, remove duplicate
1251    interfaces and make sure that all symbols are either functions
1252    or subroutines, and all of the same kind.  Returns nonzero if
1253    something goes wrong.  */
1254
1255 static int
1256 check_interface0 (gfc_interface *p, const char *interface_name)
1257 {
1258   gfc_interface *psave, *q, *qlast;
1259
1260   psave = p;
1261   for (; p; p = p->next)
1262     {
1263       /* Make sure all symbols in the interface have been defined as
1264          functions or subroutines.  */
1265       if ((!p->sym->attr.function && !p->sym->attr.subroutine)
1266           || !p->sym->attr.if_source)
1267         {
1268           if (p->sym->attr.external)
1269             gfc_error ("Procedure '%s' in %s at %L has no explicit interface",
1270                        p->sym->name, interface_name, &p->sym->declared_at);
1271           else
1272             gfc_error ("Procedure '%s' in %s at %L is neither function nor "
1273                        "subroutine", p->sym->name, interface_name,
1274                       &p->sym->declared_at);
1275           return 1;
1276         }
1277
1278       /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs.  */
1279       if ((psave->sym->attr.function && !p->sym->attr.function)
1280           || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
1281         {
1282           gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
1283                      " or all FUNCTIONs", interface_name, &p->sym->declared_at);
1284           return 1;
1285         }
1286
1287       if (p->sym->attr.proc == PROC_INTERNAL
1288           && gfc_notify_std (GFC_STD_GNU, "Extension: Internal procedure '%s' "
1289                              "in %s at %L", p->sym->name, interface_name,
1290                              &p->sym->declared_at) == FAILURE)
1291         return 1;
1292     }
1293   p = psave;
1294
1295   /* Remove duplicate interfaces in this interface list.  */
1296   for (; p; p = p->next)
1297     {
1298       qlast = p;
1299
1300       for (q = p->next; q;)
1301         {
1302           if (p->sym != q->sym)
1303             {
1304               qlast = q;
1305               q = q->next;
1306             }
1307           else
1308             {
1309               /* Duplicate interface.  */
1310               qlast->next = q->next;
1311               free (q);
1312               q = qlast->next;
1313             }
1314         }
1315     }
1316
1317   return 0;
1318 }
1319
1320
1321 /* Check lists of interfaces to make sure that no two interfaces are
1322    ambiguous.  Duplicate interfaces (from the same symbol) are OK here.  */
1323
1324 static int
1325 check_interface1 (gfc_interface *p, gfc_interface *q0,
1326                   int generic_flag, const char *interface_name,
1327                   bool referenced)
1328 {
1329   gfc_interface *q;
1330   for (; p; p = p->next)
1331     for (q = q0; q; q = q->next)
1332       {
1333         if (p->sym == q->sym)
1334           continue;             /* Duplicates OK here.  */
1335
1336         if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
1337           continue;
1338
1339         if (gfc_compare_interfaces (p->sym, q->sym, q->sym->name, generic_flag,
1340                                     0, NULL, 0))
1341           {
1342             if (referenced)
1343               gfc_error ("Ambiguous interfaces '%s' and '%s' in %s at %L",
1344                          p->sym->name, q->sym->name, interface_name,
1345                          &p->where);
1346             else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
1347               gfc_warning ("Ambiguous interfaces '%s' and '%s' in %s at %L",
1348                            p->sym->name, q->sym->name, interface_name,
1349                            &p->where);
1350             else
1351               gfc_warning ("Although not referenced, '%s' has ambiguous "
1352                            "interfaces at %L", interface_name, &p->where);
1353             return 1;
1354           }
1355       }
1356   return 0;
1357 }
1358
1359
1360 /* Check the generic and operator interfaces of symbols to make sure
1361    that none of the interfaces conflict.  The check has to be done
1362    after all of the symbols are actually loaded.  */
1363
1364 static void
1365 check_sym_interfaces (gfc_symbol *sym)
1366 {
1367   char interface_name[100];
1368   gfc_interface *p;
1369
1370   if (sym->ns != gfc_current_ns)
1371     return;
1372
1373   if (sym->generic != NULL)
1374     {
1375       sprintf (interface_name, "generic interface '%s'", sym->name);
1376       if (check_interface0 (sym->generic, interface_name))
1377         return;
1378
1379       for (p = sym->generic; p; p = p->next)
1380         {
1381           if (p->sym->attr.mod_proc
1382               && (p->sym->attr.if_source != IFSRC_DECL
1383                   || p->sym->attr.procedure))
1384             {
1385               gfc_error ("'%s' at %L is not a module procedure",
1386                          p->sym->name, &p->where);
1387               return;
1388             }
1389         }
1390
1391       /* Originally, this test was applied to host interfaces too;
1392          this is incorrect since host associated symbols, from any
1393          source, cannot be ambiguous with local symbols.  */
1394       check_interface1 (sym->generic, sym->generic, 1, interface_name,
1395                         sym->attr.referenced || !sym->attr.use_assoc);
1396     }
1397 }
1398
1399
1400 static void
1401 check_uop_interfaces (gfc_user_op *uop)
1402 {
1403   char interface_name[100];
1404   gfc_user_op *uop2;
1405   gfc_namespace *ns;
1406
1407   sprintf (interface_name, "operator interface '%s'", uop->name);
1408   if (check_interface0 (uop->op, interface_name))
1409     return;
1410
1411   for (ns = gfc_current_ns; ns; ns = ns->parent)
1412     {
1413       uop2 = gfc_find_uop (uop->name, ns);
1414       if (uop2 == NULL)
1415         continue;
1416
1417       check_interface1 (uop->op, uop2->op, 0,
1418                         interface_name, true);
1419     }
1420 }
1421
1422 /* Given an intrinsic op, return an equivalent op if one exists,
1423    or INTRINSIC_NONE otherwise.  */
1424
1425 gfc_intrinsic_op
1426 gfc_equivalent_op (gfc_intrinsic_op op)
1427 {
1428   switch(op)
1429     {
1430     case INTRINSIC_EQ:
1431       return INTRINSIC_EQ_OS;
1432
1433     case INTRINSIC_EQ_OS:
1434       return INTRINSIC_EQ;
1435
1436     case INTRINSIC_NE:
1437       return INTRINSIC_NE_OS;
1438
1439     case INTRINSIC_NE_OS:
1440       return INTRINSIC_NE;
1441
1442     case INTRINSIC_GT:
1443       return INTRINSIC_GT_OS;
1444
1445     case INTRINSIC_GT_OS:
1446       return INTRINSIC_GT;
1447
1448     case INTRINSIC_GE:
1449       return INTRINSIC_GE_OS;
1450
1451     case INTRINSIC_GE_OS:
1452       return INTRINSIC_GE;
1453
1454     case INTRINSIC_LT:
1455       return INTRINSIC_LT_OS;
1456
1457     case INTRINSIC_LT_OS:
1458       return INTRINSIC_LT;
1459
1460     case INTRINSIC_LE:
1461       return INTRINSIC_LE_OS;
1462
1463     case INTRINSIC_LE_OS:
1464       return INTRINSIC_LE;
1465
1466     default:
1467       return INTRINSIC_NONE;
1468     }
1469 }
1470
1471 /* For the namespace, check generic, user operator and intrinsic
1472    operator interfaces for consistency and to remove duplicate
1473    interfaces.  We traverse the whole namespace, counting on the fact
1474    that most symbols will not have generic or operator interfaces.  */
1475
1476 void
1477 gfc_check_interfaces (gfc_namespace *ns)
1478 {
1479   gfc_namespace *old_ns, *ns2;
1480   char interface_name[100];
1481   int i;
1482
1483   old_ns = gfc_current_ns;
1484   gfc_current_ns = ns;
1485
1486   gfc_traverse_ns (ns, check_sym_interfaces);
1487
1488   gfc_traverse_user_op (ns, check_uop_interfaces);
1489
1490   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
1491     {
1492       if (i == INTRINSIC_USER)
1493         continue;
1494
1495       if (i == INTRINSIC_ASSIGN)
1496         strcpy (interface_name, "intrinsic assignment operator");
1497       else
1498         sprintf (interface_name, "intrinsic '%s' operator",
1499                  gfc_op2string ((gfc_intrinsic_op) i));
1500
1501       if (check_interface0 (ns->op[i], interface_name))
1502         continue;
1503
1504       if (ns->op[i])
1505         gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
1506                                       ns->op[i]->where);
1507
1508       for (ns2 = ns; ns2; ns2 = ns2->parent)
1509         {
1510           gfc_intrinsic_op other_op;
1511           
1512           if (check_interface1 (ns->op[i], ns2->op[i], 0,
1513                                 interface_name, true))
1514             goto done;
1515
1516           /* i should be gfc_intrinsic_op, but has to be int with this cast
1517              here for stupid C++ compatibility rules.  */
1518           other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
1519           if (other_op != INTRINSIC_NONE
1520             &&  check_interface1 (ns->op[i], ns2->op[other_op],
1521                                   0, interface_name, true))
1522             goto done;
1523         }
1524     }
1525
1526 done:
1527   gfc_current_ns = old_ns;
1528 }
1529
1530
1531 static int
1532 symbol_rank (gfc_symbol *sym)
1533 {
1534   return (sym->as == NULL) ? 0 : sym->as->rank;
1535 }
1536
1537
1538 /* Given a symbol of a formal argument list and an expression, if the
1539    formal argument is allocatable, check that the actual argument is
1540    allocatable. Returns nonzero if compatible, zero if not compatible.  */
1541
1542 static int
1543 compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
1544 {
1545   symbol_attribute attr;
1546
1547   if (formal->attr.allocatable
1548       || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
1549     {
1550       attr = gfc_expr_attr (actual);
1551       if (!attr.allocatable)
1552         return 0;
1553     }
1554
1555   return 1;
1556 }
1557
1558
1559 /* Given a symbol of a formal argument list and an expression, if the
1560    formal argument is a pointer, see if the actual argument is a
1561    pointer. Returns nonzero if compatible, zero if not compatible.  */
1562
1563 static int
1564 compare_pointer (gfc_symbol *formal, gfc_expr *actual)
1565 {
1566   symbol_attribute attr;
1567
1568   if (formal->attr.pointer)
1569     {
1570       attr = gfc_expr_attr (actual);
1571
1572       /* Fortran 2008 allows non-pointer actual arguments.  */
1573       if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
1574         return 2;
1575
1576       if (!attr.pointer)
1577         return 0;
1578     }
1579
1580   return 1;
1581 }
1582
1583
1584 /* Emit clear error messages for rank mismatch.  */
1585
1586 static void
1587 argument_rank_mismatch (const char *name, locus *where,
1588                         int rank1, int rank2)
1589 {
1590   if (rank1 == 0)
1591     {
1592       gfc_error ("Rank mismatch in argument '%s' at %L "
1593                  "(scalar and rank-%d)", name, where, rank2);
1594     }
1595   else if (rank2 == 0)
1596     {
1597       gfc_error ("Rank mismatch in argument '%s' at %L "
1598                  "(rank-%d and scalar)", name, where, rank1);
1599     }
1600   else
1601     {    
1602       gfc_error ("Rank mismatch in argument '%s' at %L "
1603                  "(rank-%d and rank-%d)", name, where, rank1, rank2);
1604     }
1605 }
1606
1607
1608 /* Given a symbol of a formal argument list and an expression, see if
1609    the two are compatible as arguments.  Returns nonzero if
1610    compatible, zero if not compatible.  */
1611
1612 static int
1613 compare_parameter (gfc_symbol *formal, gfc_expr *actual,
1614                    int ranks_must_agree, int is_elemental, locus *where)
1615 {
1616   gfc_ref *ref;
1617   bool rank_check, is_pointer;
1618
1619   /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
1620      procs c_f_pointer or c_f_procpointer, and we need to accept most
1621      pointers the user could give us.  This should allow that.  */
1622   if (formal->ts.type == BT_VOID)
1623     return 1;
1624
1625   if (formal->ts.type == BT_DERIVED
1626       && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
1627       && actual->ts.type == BT_DERIVED
1628       && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
1629     return 1;
1630
1631   if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
1632     /* Make sure the vtab symbol is present when
1633        the module variables are generated.  */
1634     gfc_find_derived_vtab (actual->ts.u.derived);
1635
1636   if (actual->ts.type == BT_PROCEDURE)
1637     {
1638       char err[200];
1639       gfc_symbol *act_sym = actual->symtree->n.sym;
1640
1641       if (formal->attr.flavor != FL_PROCEDURE)
1642         {
1643           if (where)
1644             gfc_error ("Invalid procedure argument at %L", &actual->where);
1645           return 0;
1646         }
1647
1648       if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
1649                                    sizeof(err)))
1650         {
1651           if (where)
1652             gfc_error ("Interface mismatch in dummy procedure '%s' at %L: %s",
1653                        formal->name, &actual->where, err);
1654           return 0;
1655         }
1656
1657       if (formal->attr.function && !act_sym->attr.function)
1658         {
1659           gfc_add_function (&act_sym->attr, act_sym->name,
1660           &act_sym->declared_at);
1661           if (act_sym->ts.type == BT_UNKNOWN
1662               && gfc_set_default_type (act_sym, 1, act_sym->ns) == FAILURE)
1663             return 0;
1664         }
1665       else if (formal->attr.subroutine && !act_sym->attr.subroutine)
1666         gfc_add_subroutine (&act_sym->attr, act_sym->name,
1667                             &act_sym->declared_at);
1668
1669       return 1;
1670     }
1671
1672   /* F2008, C1241.  */
1673   if (formal->attr.pointer && formal->attr.contiguous
1674       && !gfc_is_simply_contiguous (actual, true))
1675     {
1676       if (where)
1677         gfc_error ("Actual argument to contiguous pointer dummy '%s' at %L "
1678                    "must be simply contigous", formal->name, &actual->where);
1679       return 0;
1680     }
1681
1682   if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
1683       && actual->ts.type != BT_HOLLERITH
1684       && !gfc_compare_types (&formal->ts, &actual->ts))
1685     {
1686       if (where)
1687         gfc_error ("Type mismatch in argument '%s' at %L; passed %s to %s",
1688                    formal->name, &actual->where, gfc_typename (&actual->ts),
1689                    gfc_typename (&formal->ts));
1690       return 0;
1691     }
1692     
1693   /* F2003, 12.5.2.5.  */
1694   if (formal->ts.type == BT_CLASS
1695       && (CLASS_DATA (formal)->attr.class_pointer
1696           || CLASS_DATA (formal)->attr.allocatable))
1697     {
1698       if (actual->ts.type != BT_CLASS)
1699         {
1700           if (where)
1701             gfc_error ("Actual argument to '%s' at %L must be polymorphic",
1702                         formal->name, &actual->where);
1703           return 0;
1704         }
1705       if (CLASS_DATA (actual)->ts.u.derived
1706           != CLASS_DATA (formal)->ts.u.derived)
1707         {
1708           if (where)
1709             gfc_error ("Actual argument to '%s' at %L must have the same "
1710                        "declared type", formal->name, &actual->where);
1711           return 0;
1712         }
1713     }
1714
1715   if (formal->attr.codimension && !gfc_is_coarray (actual))
1716     {
1717       if (where)
1718         gfc_error ("Actual argument to '%s' at %L must be a coarray",
1719                        formal->name, &actual->where);
1720       return 0;
1721     }
1722
1723   if (formal->attr.codimension && formal->attr.allocatable)
1724     {
1725       gfc_ref *last = NULL;
1726
1727       for (ref = actual->ref; ref; ref = ref->next)
1728         if (ref->type == REF_COMPONENT)
1729           last = ref;
1730
1731       /* F2008, 12.5.2.6.  */
1732       if ((last && last->u.c.component->as->corank != formal->as->corank)
1733           || (!last
1734               && actual->symtree->n.sym->as->corank != formal->as->corank))
1735         {
1736           if (where)
1737             gfc_error ("Corank mismatch in argument '%s' at %L (%d and %d)",
1738                    formal->name, &actual->where, formal->as->corank,
1739                    last ? last->u.c.component->as->corank
1740                         : actual->symtree->n.sym->as->corank);
1741           return 0;
1742         }
1743     }
1744
1745   if (formal->attr.codimension)
1746     {
1747       /* F2008, 12.5.2.8.  */
1748       if (formal->attr.dimension
1749           && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
1750           && gfc_expr_attr (actual).dimension
1751           && !gfc_is_simply_contiguous (actual, true))
1752         {
1753           if (where)
1754             gfc_error ("Actual argument to '%s' at %L must be simply "
1755                        "contiguous", formal->name, &actual->where);
1756           return 0;
1757         }
1758
1759       /* F2008, C1303 and C1304.  */
1760       if (formal->attr.intent != INTENT_INOUT
1761           && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
1762                && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
1763                && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
1764               || formal->attr.lock_comp))
1765
1766         {
1767           if (where)
1768             gfc_error ("Actual argument to non-INTENT(INOUT) dummy '%s' at %L, "
1769                        "which is LOCK_TYPE or has a LOCK_TYPE component",
1770                        formal->name, &actual->where);
1771           return 0;
1772         }
1773     }
1774
1775   /* F2008, C1239/C1240.  */
1776   if (actual->expr_type == EXPR_VARIABLE
1777       && (actual->symtree->n.sym->attr.asynchronous
1778          || actual->symtree->n.sym->attr.volatile_)
1779       &&  (formal->attr.asynchronous || formal->attr.volatile_)
1780       && actual->rank && !gfc_is_simply_contiguous (actual, true)
1781       && ((formal->as->type != AS_ASSUMED_SHAPE && !formal->attr.pointer)
1782           || formal->attr.contiguous))
1783     {
1784       if (where)
1785         gfc_error ("Dummy argument '%s' has to be a pointer or assumed-shape "
1786                    "array without CONTIGUOUS attribute - as actual argument at"
1787                    " %L is not simply contiguous and both are ASYNCHRONOUS "
1788                    "or VOLATILE", formal->name, &actual->where);
1789       return 0;
1790     }
1791
1792   if (formal->attr.allocatable && !formal->attr.codimension
1793       && gfc_expr_attr (actual).codimension)
1794     {
1795       if (formal->attr.intent == INTENT_OUT)
1796         {
1797           if (where)
1798             gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
1799                        "INTENT(OUT) dummy argument '%s'", &actual->where,
1800                        formal->name);
1801             return 0;
1802         }
1803       else if (gfc_option.warn_surprising && where
1804                && formal->attr.intent != INTENT_IN)
1805         gfc_warning ("Passing coarray at %L to allocatable, noncoarray dummy "
1806                      "argument '%s', which is invalid if the allocation status"
1807                      " is modified",  &actual->where, formal->name);
1808     }
1809
1810   if (symbol_rank (formal) == actual->rank)
1811     return 1;
1812
1813   rank_check = where != NULL && !is_elemental && formal->as
1814                && (formal->as->type == AS_ASSUMED_SHAPE
1815                    || formal->as->type == AS_DEFERRED)
1816                && actual->expr_type != EXPR_NULL;
1817
1818   /* Scalar & coindexed, see: F2008, Section 12.5.2.4.  */
1819   if (rank_check || ranks_must_agree
1820       || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
1821       || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
1822       || (actual->rank == 0 && formal->as->type == AS_ASSUMED_SHAPE
1823           && actual->expr_type != EXPR_NULL)
1824       || (actual->rank == 0 && formal->attr.dimension
1825           && gfc_is_coindexed (actual)))
1826     {
1827       if (where)
1828         argument_rank_mismatch (formal->name, &actual->where,
1829                                 symbol_rank (formal), actual->rank);
1830       return 0;
1831     }
1832   else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
1833     return 1;
1834
1835   /* At this point, we are considering a scalar passed to an array.   This
1836      is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
1837      - if the actual argument is (a substring of) an element of a
1838        non-assumed-shape/non-pointer/non-polymorphic array; or
1839      - (F2003) if the actual argument is of type character of default/c_char
1840        kind.  */
1841
1842   is_pointer = actual->expr_type == EXPR_VARIABLE
1843                ? actual->symtree->n.sym->attr.pointer : false;
1844
1845   for (ref = actual->ref; ref; ref = ref->next)
1846     {
1847       if (ref->type == REF_COMPONENT)
1848         is_pointer = ref->u.c.component->attr.pointer;
1849       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
1850                && ref->u.ar.dimen > 0
1851                && (!ref->next 
1852                    || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
1853         break;
1854     }
1855
1856   if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
1857     {
1858       if (where)
1859         gfc_error ("Polymorphic scalar passed to array dummy argument '%s' "
1860                    "at %L", formal->name, &actual->where);
1861       return 0;
1862     }
1863
1864   if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
1865       && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
1866     {
1867       if (where)
1868         gfc_error ("Element of assumed-shaped or pointer "
1869                    "array passed to array dummy argument '%s' at %L",
1870                    formal->name, &actual->where);
1871       return 0;
1872     }
1873
1874   if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
1875       && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
1876     {
1877       if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
1878         {
1879           if (where)
1880             gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
1881                        "CHARACTER actual argument with array dummy argument "
1882                        "'%s' at %L", formal->name, &actual->where);
1883           return 0;
1884         }
1885
1886       if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
1887         {
1888           gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
1889                      "array dummy argument '%s' at %L",
1890                      formal->name, &actual->where);
1891           return 0;
1892         }
1893       else if ((gfc_option.allow_std & GFC_STD_F2003) == 0)
1894         return 0;
1895       else
1896         return 1;
1897     }
1898
1899   if (ref == NULL && actual->expr_type != EXPR_NULL)
1900     {
1901       if (where)
1902         argument_rank_mismatch (formal->name, &actual->where,
1903                                 symbol_rank (formal), actual->rank);
1904       return 0;
1905     }
1906
1907   return 1;
1908 }
1909
1910
1911 /* Returns the storage size of a symbol (formal argument) or
1912    zero if it cannot be determined.  */
1913
1914 static unsigned long
1915 get_sym_storage_size (gfc_symbol *sym)
1916 {
1917   int i;
1918   unsigned long strlen, elements;
1919
1920   if (sym->ts.type == BT_CHARACTER)
1921     {
1922       if (sym->ts.u.cl && sym->ts.u.cl->length
1923           && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1924         strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
1925       else
1926         return 0;
1927     }
1928   else
1929     strlen = 1; 
1930
1931   if (symbol_rank (sym) == 0)
1932     return strlen;
1933
1934   elements = 1;
1935   if (sym->as->type != AS_EXPLICIT)
1936     return 0;
1937   for (i = 0; i < sym->as->rank; i++)
1938     {
1939       if (!sym->as || sym->as->upper[i]->expr_type != EXPR_CONSTANT
1940           || sym->as->lower[i]->expr_type != EXPR_CONSTANT)
1941         return 0;
1942
1943       elements *= mpz_get_si (sym->as->upper[i]->value.integer)
1944                   - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
1945     }
1946
1947   return strlen*elements;
1948 }
1949
1950
1951 /* Returns the storage size of an expression (actual argument) or
1952    zero if it cannot be determined. For an array element, it returns
1953    the remaining size as the element sequence consists of all storage
1954    units of the actual argument up to the end of the array.  */
1955
1956 static unsigned long
1957 get_expr_storage_size (gfc_expr *e)
1958 {
1959   int i;
1960   long int strlen, elements;
1961   long int substrlen = 0;
1962   bool is_str_storage = false;
1963   gfc_ref *ref;
1964
1965   if (e == NULL)
1966     return 0;
1967   
1968   if (e->ts.type == BT_CHARACTER)
1969     {
1970       if (e->ts.u.cl && e->ts.u.cl->length
1971           && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1972         strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
1973       else if (e->expr_type == EXPR_CONSTANT
1974                && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
1975         strlen = e->value.character.length;
1976       else
1977         return 0;
1978     }
1979   else
1980     strlen = 1; /* Length per element.  */
1981
1982   if (e->rank == 0 && !e->ref)
1983     return strlen;
1984
1985   elements = 1;
1986   if (!e->ref)
1987     {
1988       if (!e->shape)
1989         return 0;
1990       for (i = 0; i < e->rank; i++)
1991         elements *= mpz_get_si (e->shape[i]);
1992       return elements*strlen;
1993     }
1994
1995   for (ref = e->ref; ref; ref = ref->next)
1996     {
1997       if (ref->type == REF_SUBSTRING && ref->u.ss.start
1998           && ref->u.ss.start->expr_type == EXPR_CONSTANT)
1999         {
2000           if (is_str_storage)
2001             {
2002               /* The string length is the substring length.
2003                  Set now to full string length.  */
2004               if (!ref->u.ss.length || !ref->u.ss.length->length
2005                   || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
2006                 return 0;
2007
2008               strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
2009             }
2010           substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
2011           continue;
2012         }
2013
2014       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION
2015           && ref->u.ar.start && ref->u.ar.end && ref->u.ar.stride
2016           && ref->u.ar.as->upper)
2017         for (i = 0; i < ref->u.ar.dimen; i++)
2018           {
2019             long int start, end, stride;
2020             stride = 1;
2021
2022             if (ref->u.ar.stride[i])
2023               {
2024                 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT)
2025                   stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
2026                 else
2027                   return 0;
2028               }
2029
2030             if (ref->u.ar.start[i])
2031               {
2032                 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT)
2033                   start = mpz_get_si (ref->u.ar.start[i]->value.integer);
2034                 else
2035                   return 0;
2036               }
2037             else if (ref->u.ar.as->lower[i]
2038                      && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT)
2039               start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
2040             else
2041               return 0;
2042
2043             if (ref->u.ar.end[i])
2044               {
2045                 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT)
2046                   end = mpz_get_si (ref->u.ar.end[i]->value.integer);
2047                 else
2048                   return 0;
2049               }
2050             else if (ref->u.ar.as->upper[i]
2051                      && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
2052               end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
2053             else
2054               return 0;
2055
2056             elements *= (end - start)/stride + 1L;
2057           }
2058       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL
2059                && ref->u.ar.as->lower && ref->u.ar.as->upper)
2060         for (i = 0; i < ref->u.ar.as->rank; i++)
2061           {
2062             if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
2063                 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
2064                 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
2065               elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2066                           - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2067                           + 1L;
2068             else
2069               return 0;
2070           }
2071       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2072                && e->expr_type == EXPR_VARIABLE)
2073         {
2074           if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
2075               || e->symtree->n.sym->attr.pointer)
2076             {
2077               elements = 1;
2078               continue;
2079             }
2080
2081           /* Determine the number of remaining elements in the element
2082              sequence for array element designators.  */
2083           is_str_storage = true;
2084           for (i = ref->u.ar.dimen - 1; i >= 0; i--)
2085             {
2086               if (ref->u.ar.start[i] == NULL
2087                   || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
2088                   || ref->u.ar.as->upper[i] == NULL
2089                   || ref->u.ar.as->lower[i] == NULL
2090                   || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
2091                   || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT)
2092                 return 0;
2093
2094               elements
2095                    = elements
2096                      * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2097                         - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2098                         + 1L)
2099                      - (mpz_get_si (ref->u.ar.start[i]->value.integer)
2100                         - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
2101             }
2102         }
2103     }
2104
2105   if (substrlen)
2106     return (is_str_storage) ? substrlen + (elements-1)*strlen
2107                             : elements*strlen;
2108   else
2109     return elements*strlen;
2110 }
2111
2112
2113 /* Given an expression, check whether it is an array section
2114    which has a vector subscript. If it has, one is returned,
2115    otherwise zero.  */
2116
2117 int
2118 gfc_has_vector_subscript (gfc_expr *e)
2119 {
2120   int i;
2121   gfc_ref *ref;
2122
2123   if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
2124     return 0;
2125
2126   for (ref = e->ref; ref; ref = ref->next)
2127     if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2128       for (i = 0; i < ref->u.ar.dimen; i++)
2129         if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2130           return 1;
2131
2132   return 0;
2133 }
2134
2135
2136 /* Given formal and actual argument lists, see if they are compatible.
2137    If they are compatible, the actual argument list is sorted to
2138    correspond with the formal list, and elements for missing optional
2139    arguments are inserted. If WHERE pointer is nonnull, then we issue
2140    errors when things don't match instead of just returning the status
2141    code.  */
2142
2143 static int
2144 compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
2145                        int ranks_must_agree, int is_elemental, locus *where)
2146 {
2147   gfc_actual_arglist **new_arg, *a, *actual, temp;
2148   gfc_formal_arglist *f;
2149   int i, n, na;
2150   unsigned long actual_size, formal_size;
2151
2152   actual = *ap;
2153
2154   if (actual == NULL && formal == NULL)
2155     return 1;
2156
2157   n = 0;
2158   for (f = formal; f; f = f->next)
2159     n++;
2160
2161   new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
2162
2163   for (i = 0; i < n; i++)
2164     new_arg[i] = NULL;
2165
2166   na = 0;
2167   f = formal;
2168   i = 0;
2169
2170   for (a = actual; a; a = a->next, f = f->next)
2171     {
2172       /* Look for keywords but ignore g77 extensions like %VAL.  */
2173       if (a->name != NULL && a->name[0] != '%')
2174         {
2175           i = 0;
2176           for (f = formal; f; f = f->next, i++)
2177             {
2178               if (f->sym == NULL)
2179                 continue;
2180               if (strcmp (f->sym->name, a->name) == 0)
2181                 break;
2182             }
2183
2184           if (f == NULL)
2185             {
2186               if (where)
2187                 gfc_error ("Keyword argument '%s' at %L is not in "
2188                            "the procedure", a->name, &a->expr->where);
2189               return 0;
2190             }
2191
2192           if (new_arg[i] != NULL)
2193             {
2194               if (where)
2195                 gfc_error ("Keyword argument '%s' at %L is already associated "
2196                            "with another actual argument", a->name,
2197                            &a->expr->where);
2198               return 0;
2199             }
2200         }
2201
2202       if (f == NULL)
2203         {
2204           if (where)
2205             gfc_error ("More actual than formal arguments in procedure "
2206                        "call at %L", where);
2207
2208           return 0;
2209         }
2210
2211       if (f->sym == NULL && a->expr == NULL)
2212         goto match;
2213
2214       if (f->sym == NULL)
2215         {
2216           if (where)
2217             gfc_error ("Missing alternate return spec in subroutine call "
2218                        "at %L", where);
2219           return 0;
2220         }
2221
2222       if (a->expr == NULL)
2223         {
2224           if (where)
2225             gfc_error ("Unexpected alternate return spec in subroutine "
2226                        "call at %L", where);
2227           return 0;
2228         }
2229
2230       if (a->expr->expr_type == EXPR_NULL && !f->sym->attr.pointer
2231           && (f->sym->attr.allocatable || !f->sym->attr.optional
2232               || (gfc_option.allow_std & GFC_STD_F2008) == 0))
2233         {
2234           if (where && (f->sym->attr.allocatable || !f->sym->attr.optional))
2235             gfc_error ("Unexpected NULL() intrinsic at %L to dummy '%s'",
2236                        where, f->sym->name);
2237           else if (where)
2238             gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
2239                        "dummy '%s'", where, f->sym->name);
2240
2241           return 0;
2242         }
2243       
2244       if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
2245                               is_elemental, where))
2246         return 0;
2247
2248       /* Special case for character arguments.  For allocatable, pointer
2249          and assumed-shape dummies, the string length needs to match
2250          exactly.  */
2251       if (a->expr->ts.type == BT_CHARACTER
2252            && a->expr->ts.u.cl && a->expr->ts.u.cl->length
2253            && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
2254            && f->sym->ts.u.cl && f->sym->ts.u.cl && f->sym->ts.u.cl->length
2255            && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2256            && (f->sym->attr.pointer || f->sym->attr.allocatable
2257                || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2258            && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
2259                         f->sym->ts.u.cl->length->value.integer) != 0))
2260          {
2261            if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
2262              gfc_warning ("Character length mismatch (%ld/%ld) between actual "
2263                           "argument and pointer or allocatable dummy argument "
2264                           "'%s' at %L",
2265                           mpz_get_si (a->expr->ts.u.cl->length->value.integer),
2266                           mpz_get_si (f->sym->ts.u.cl->length->value.integer),
2267                           f->sym->name, &a->expr->where);
2268            else if (where)
2269              gfc_warning ("Character length mismatch (%ld/%ld) between actual "
2270                           "argument and assumed-shape dummy argument '%s' "
2271                           "at %L",
2272                           mpz_get_si (a->expr->ts.u.cl->length->value.integer),
2273                           mpz_get_si (f->sym->ts.u.cl->length->value.integer),
2274                           f->sym->name, &a->expr->where);
2275            return 0;
2276          }
2277
2278       if ((f->sym->attr.pointer || f->sym->attr.allocatable)
2279             && f->sym->ts.deferred != a->expr->ts.deferred
2280             && a->expr->ts.type == BT_CHARACTER)
2281         {
2282           if (where)
2283             gfc_error ("Actual argument argument at %L to allocatable or "
2284                        "pointer dummy argument '%s' must have a deferred "
2285                        "length type parameter if and only if the dummy has one",
2286                        &a->expr->where, f->sym->name);
2287           return 0;
2288         }
2289
2290       actual_size = get_expr_storage_size (a->expr);
2291       formal_size = get_sym_storage_size (f->sym);
2292       if (actual_size != 0 && actual_size < formal_size
2293           && a->expr->ts.type != BT_PROCEDURE
2294           && f->sym->attr.flavor != FL_PROCEDURE)
2295         {
2296           if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
2297             gfc_warning ("Character length of actual argument shorter "
2298                          "than of dummy argument '%s' (%lu/%lu) at %L",
2299                          f->sym->name, actual_size, formal_size,
2300                          &a->expr->where);
2301           else if (where)
2302             gfc_warning ("Actual argument contains too few "
2303                          "elements for dummy argument '%s' (%lu/%lu) at %L",
2304                          f->sym->name, actual_size, formal_size,
2305                          &a->expr->where);
2306           return  0;
2307         }
2308
2309       /* Satisfy 12.4.1.3 by ensuring that a procedure pointer actual argument
2310          is provided for a procedure pointer formal argument.  */
2311       if (f->sym->attr.proc_pointer
2312           && !((a->expr->expr_type == EXPR_VARIABLE
2313                 && a->expr->symtree->n.sym->attr.proc_pointer)
2314                || (a->expr->expr_type == EXPR_FUNCTION
2315                    && a->expr->symtree->n.sym->result->attr.proc_pointer)
2316                || gfc_is_proc_ptr_comp (a->expr, NULL)))
2317         {
2318           if (where)
2319             gfc_error ("Expected a procedure pointer for argument '%s' at %L",
2320                        f->sym->name, &a->expr->where);
2321           return 0;
2322         }
2323
2324       /* Satisfy 12.4.1.2 by ensuring that a procedure actual argument is
2325          provided for a procedure formal argument.  */
2326       if (a->expr->ts.type != BT_PROCEDURE && !gfc_is_proc_ptr_comp (a->expr, NULL)
2327           && a->expr->expr_type == EXPR_VARIABLE
2328           && f->sym->attr.flavor == FL_PROCEDURE)
2329         {
2330           if (where)
2331             gfc_error ("Expected a procedure for argument '%s' at %L",
2332                        f->sym->name, &a->expr->where);
2333           return 0;
2334         }
2335
2336       if (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE
2337           && a->expr->expr_type == EXPR_VARIABLE
2338           && a->expr->symtree->n.sym->as
2339           && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
2340           && (a->expr->ref == NULL
2341               || (a->expr->ref->type == REF_ARRAY
2342                   && a->expr->ref->u.ar.type == AR_FULL)))
2343         {
2344           if (where)
2345             gfc_error ("Actual argument for '%s' cannot be an assumed-size"
2346                        " array at %L", f->sym->name, where);
2347           return 0;
2348         }
2349
2350       if (a->expr->expr_type != EXPR_NULL
2351           && compare_pointer (f->sym, a->expr) == 0)
2352         {
2353           if (where)
2354             gfc_error ("Actual argument for '%s' must be a pointer at %L",
2355                        f->sym->name, &a->expr->where);
2356           return 0;
2357         }
2358
2359       if (a->expr->expr_type != EXPR_NULL
2360           && (gfc_option.allow_std & GFC_STD_F2008) == 0
2361           && compare_pointer (f->sym, a->expr) == 2)
2362         {
2363           if (where)
2364             gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
2365                        "pointer dummy '%s'", &a->expr->where,f->sym->name);
2366           return 0;
2367         }
2368         
2369
2370       /* Fortran 2008, C1242.  */
2371       if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
2372         {
2373           if (where)
2374             gfc_error ("Coindexed actual argument at %L to pointer "
2375                        "dummy '%s'",
2376                        &a->expr->where, f->sym->name);
2377           return 0;
2378         }
2379
2380       /* Fortran 2008, 12.5.2.5 (no constraint).  */
2381       if (a->expr->expr_type == EXPR_VARIABLE
2382           && f->sym->attr.intent != INTENT_IN
2383           && f->sym->attr.allocatable
2384           && gfc_is_coindexed (a->expr))
2385         {
2386           if (where)
2387             gfc_error ("Coindexed actual argument at %L to allocatable "
2388                        "dummy '%s' requires INTENT(IN)",
2389                        &a->expr->where, f->sym->name);
2390           return 0;
2391         }
2392
2393       /* Fortran 2008, C1237.  */
2394       if (a->expr->expr_type == EXPR_VARIABLE
2395           && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
2396           && gfc_is_coindexed (a->expr)
2397           && (a->expr->symtree->n.sym->attr.volatile_
2398               || a->expr->symtree->n.sym->attr.asynchronous))
2399         {
2400           if (where)
2401             gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
2402                        "at %L requires that dummy %s' has neither "
2403                        "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
2404                        f->sym->name);
2405           return 0;
2406         }
2407
2408       /* Fortran 2008, 12.5.2.4 (no constraint).  */
2409       if (a->expr->expr_type == EXPR_VARIABLE
2410           && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
2411           && gfc_is_coindexed (a->expr)
2412           && gfc_has_ultimate_allocatable (a->expr))
2413         {
2414           if (where)
2415             gfc_error ("Coindexed actual argument at %L with allocatable "
2416                        "ultimate component to dummy '%s' requires either VALUE "
2417                        "or INTENT(IN)", &a->expr->where, f->sym->name);
2418           return 0;
2419         }
2420
2421       if (a->expr->expr_type != EXPR_NULL
2422           && compare_allocatable (f->sym, a->expr) == 0)
2423         {
2424           if (where)
2425             gfc_error ("Actual argument for '%s' must be ALLOCATABLE at %L",
2426                        f->sym->name, &a->expr->where);
2427           return 0;
2428         }
2429
2430       /* Check intent = OUT/INOUT for definable actual argument.  */
2431       if ((f->sym->attr.intent == INTENT_OUT
2432           || f->sym->attr.intent == INTENT_INOUT))
2433         {
2434           const char* context = (where
2435                                  ? _("actual argument to INTENT = OUT/INOUT")
2436                                  : NULL);
2437
2438           if (f->sym->attr.pointer
2439               && gfc_check_vardef_context (a->expr, true, false, context)
2440                    == FAILURE)
2441             return 0;
2442           if (gfc_check_vardef_context (a->expr, false, false, context)
2443                 == FAILURE)
2444             return 0;
2445         }
2446
2447       if ((f->sym->attr.intent == INTENT_OUT
2448            || f->sym->attr.intent == INTENT_INOUT
2449            || f->sym->attr.volatile_
2450            || f->sym->attr.asynchronous)
2451           && gfc_has_vector_subscript (a->expr))
2452         {
2453           if (where)
2454             gfc_error ("Array-section actual argument with vector "
2455                        "subscripts at %L is incompatible with INTENT(OUT), "
2456                        "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
2457                        "of the dummy argument '%s'",
2458                        &a->expr->where, f->sym->name);
2459           return 0;
2460         }
2461
2462       /* C1232 (R1221) For an actual argument which is an array section or
2463          an assumed-shape array, the dummy argument shall be an assumed-
2464          shape array, if the dummy argument has the VOLATILE attribute.  */
2465
2466       if (f->sym->attr.volatile_
2467           && a->expr->symtree->n.sym->as
2468           && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
2469           && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2470         {
2471           if (where)
2472             gfc_error ("Assumed-shape actual argument at %L is "
2473                        "incompatible with the non-assumed-shape "
2474                        "dummy argument '%s' due to VOLATILE attribute",
2475                        &a->expr->where,f->sym->name);
2476           return 0;
2477         }
2478
2479       if (f->sym->attr.volatile_
2480           && a->expr->ref && a->expr->ref->u.ar.type == AR_SECTION
2481           && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2482         {
2483           if (where)
2484             gfc_error ("Array-section actual argument at %L is "
2485                        "incompatible with the non-assumed-shape "
2486                        "dummy argument '%s' due to VOLATILE attribute",
2487                        &a->expr->where,f->sym->name);
2488           return 0;
2489         }
2490
2491       /* C1233 (R1221) For an actual argument which is a pointer array, the
2492          dummy argument shall be an assumed-shape or pointer array, if the
2493          dummy argument has the VOLATILE attribute.  */
2494
2495       if (f->sym->attr.volatile_
2496           && a->expr->symtree->n.sym->attr.pointer
2497           && a->expr->symtree->n.sym->as
2498           && !(f->sym->as
2499                && (f->sym->as->type == AS_ASSUMED_SHAPE
2500                    || f->sym->attr.pointer)))
2501         {
2502           if (where)
2503             gfc_error ("Pointer-array actual argument at %L requires "
2504                        "an assumed-shape or pointer-array dummy "
2505                        "argument '%s' due to VOLATILE attribute",
2506                        &a->expr->where,f->sym->name);
2507           return 0;
2508         }
2509
2510     match:
2511       if (a == actual)
2512         na = i;
2513
2514       new_arg[i++] = a;
2515     }
2516
2517   /* Make sure missing actual arguments are optional.  */
2518   i = 0;
2519   for (f = formal; f; f = f->next, i++)
2520     {
2521       if (new_arg[i] != NULL)
2522         continue;
2523       if (f->sym == NULL)
2524         {
2525           if (where)
2526             gfc_error ("Missing alternate return spec in subroutine call "
2527                        "at %L", where);
2528           return 0;
2529         }
2530       if (!f->sym->attr.optional)
2531         {
2532           if (where)
2533             gfc_error ("Missing actual argument for argument '%s' at %L",
2534                        f->sym->name, where);
2535           return 0;
2536         }
2537     }
2538
2539   /* The argument lists are compatible.  We now relink a new actual
2540      argument list with null arguments in the right places.  The head
2541      of the list remains the head.  */
2542   for (i = 0; i < n; i++)
2543     if (new_arg[i] == NULL)
2544       new_arg[i] = gfc_get_actual_arglist ();
2545
2546   if (na != 0)
2547     {
2548       temp = *new_arg[0];
2549       *new_arg[0] = *actual;
2550       *actual = temp;
2551
2552       a = new_arg[0];
2553       new_arg[0] = new_arg[na];
2554       new_arg[na] = a;
2555     }
2556
2557   for (i = 0; i < n - 1; i++)
2558     new_arg[i]->next = new_arg[i + 1];
2559
2560   new_arg[i]->next = NULL;
2561
2562   if (*ap == NULL && n > 0)
2563     *ap = new_arg[0];
2564
2565   /* Note the types of omitted optional arguments.  */
2566   for (a = *ap, f = formal; a; a = a->next, f = f->next)
2567     if (a->expr == NULL && a->label == NULL)
2568       a->missing_arg_type = f->sym->ts.type;
2569
2570   return 1;
2571 }
2572
2573
2574 typedef struct
2575 {
2576   gfc_formal_arglist *f;
2577   gfc_actual_arglist *a;
2578 }
2579 argpair;
2580
2581 /* qsort comparison function for argument pairs, with the following
2582    order:
2583     - p->a->expr == NULL
2584     - p->a->expr->expr_type != EXPR_VARIABLE
2585     - growing p->a->expr->symbol.  */
2586
2587 static int
2588 pair_cmp (const void *p1, const void *p2)
2589 {
2590   const gfc_actual_arglist *a1, *a2;
2591
2592   /* *p1 and *p2 are elements of the to-be-sorted array.  */
2593   a1 = ((const argpair *) p1)->a;
2594   a2 = ((const argpair *) p2)->a;
2595   if (!a1->expr)
2596     {
2597       if (!a2->expr)
2598         return 0;
2599       return -1;
2600     }
2601   if (!a2->expr)
2602     return 1;
2603   if (a1->expr->expr_type != EXPR_VARIABLE)
2604     {
2605       if (a2->expr->expr_type != EXPR_VARIABLE)
2606         return 0;
2607       return -1;
2608     }
2609   if (a2->expr->expr_type != EXPR_VARIABLE)
2610     return 1;
2611   return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
2612 }
2613
2614
2615 /* Given two expressions from some actual arguments, test whether they
2616    refer to the same expression. The analysis is conservative.
2617    Returning FAILURE will produce no warning.  */
2618
2619 static gfc_try
2620 compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
2621 {
2622   const gfc_ref *r1, *r2;
2623
2624   if (!e1 || !e2
2625       || e1->expr_type != EXPR_VARIABLE
2626       || e2->expr_type != EXPR_VARIABLE
2627       || e1->symtree->n.sym != e2->symtree->n.sym)
2628     return FAILURE;
2629
2630   /* TODO: improve comparison, see expr.c:show_ref().  */
2631   for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
2632     {
2633       if (r1->type != r2->type)
2634         return FAILURE;
2635       switch (r1->type)
2636         {
2637         case REF_ARRAY:
2638           if (r1->u.ar.type != r2->u.ar.type)
2639             return FAILURE;
2640           /* TODO: At the moment, consider only full arrays;
2641              we could do better.  */
2642           if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
2643             return FAILURE;
2644           break;
2645
2646         case REF_COMPONENT:
2647           if (r1->u.c.component != r2->u.c.component)
2648             return FAILURE;
2649           break;
2650
2651         case REF_SUBSTRING:
2652           return FAILURE;
2653
2654         default:
2655           gfc_internal_error ("compare_actual_expr(): Bad component code");
2656         }
2657     }
2658   if (!r1 && !r2)
2659     return SUCCESS;
2660   return FAILURE;
2661 }
2662
2663
2664 /* Given formal and actual argument lists that correspond to one
2665    another, check that identical actual arguments aren't not
2666    associated with some incompatible INTENTs.  */
2667
2668 static gfc_try
2669 check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
2670 {
2671   sym_intent f1_intent, f2_intent;
2672   gfc_formal_arglist *f1;
2673   gfc_actual_arglist *a1;
2674   size_t n, i, j;
2675   argpair *p;
2676   gfc_try t = SUCCESS;
2677
2678   n = 0;
2679   for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
2680     {
2681       if (f1 == NULL && a1 == NULL)
2682         break;
2683       if (f1 == NULL || a1 == NULL)
2684         gfc_internal_error ("check_some_aliasing(): List mismatch");
2685       n++;
2686     }
2687   if (n == 0)
2688     return t;
2689   p = XALLOCAVEC (argpair, n);
2690
2691   for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
2692     {
2693       p[i].f = f1;
2694       p[i].a = a1;
2695     }
2696
2697   qsort (p, n, sizeof (argpair), pair_cmp);
2698
2699   for (i = 0; i < n; i++)
2700     {
2701       if (!p[i].a->expr
2702           || p[i].a->expr->expr_type != EXPR_VARIABLE
2703           || p[i].a->expr->ts.type == BT_PROCEDURE)
2704         continue;
2705       f1_intent = p[i].f->sym->attr.intent;
2706       for (j = i + 1; j < n; j++)
2707         {
2708           /* Expected order after the sort.  */
2709           if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
2710             gfc_internal_error ("check_some_aliasing(): corrupted data");
2711
2712           /* Are the expression the same?  */
2713           if (compare_actual_expr (p[i].a->expr, p[j].a->expr) == FAILURE)
2714             break;
2715           f2_intent = p[j].f->sym->attr.intent;
2716           if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
2717               || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN))
2718             {
2719               gfc_warning ("Same actual argument associated with INTENT(%s) "
2720                            "argument '%s' and INTENT(%s) argument '%s' at %L",
2721                            gfc_intent_string (f1_intent), p[i].f->sym->name,
2722                            gfc_intent_string (f2_intent), p[j].f->sym->name,
2723                            &p[i].a->expr->where);
2724               t = FAILURE;
2725             }
2726         }
2727     }
2728
2729   return t;
2730 }
2731
2732
2733 /* Given a symbol of a formal argument list and an expression,
2734    return nonzero if their intents are compatible, zero otherwise.  */
2735
2736 static int
2737 compare_parameter_intent (gfc_symbol *formal, gfc_expr *actual)
2738 {
2739   if (actual->symtree->n.sym->attr.pointer && !formal->attr.pointer)
2740     return 1;
2741
2742   if (actual->symtree->n.sym->attr.intent != INTENT_IN)
2743     return 1;
2744
2745   if (formal->attr.intent == INTENT_INOUT || formal->attr.intent == INTENT_OUT)
2746     return 0;
2747
2748   return 1;
2749 }
2750
2751
2752 /* Given formal and actual argument lists that correspond to one
2753    another, check that they are compatible in the sense that intents
2754    are not mismatched.  */
2755
2756 static gfc_try
2757 check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
2758 {
2759   sym_intent f_intent;
2760
2761   for (;; f = f->next, a = a->next)
2762     {
2763       if (f == NULL && a == NULL)
2764         break;
2765       if (f == NULL || a == NULL)
2766         gfc_internal_error ("check_intents(): List mismatch");
2767
2768       if (a->expr == NULL || a->expr->expr_type != EXPR_VARIABLE)
2769         continue;
2770
2771       f_intent = f->sym->attr.intent;
2772
2773       if (!compare_parameter_intent(f->sym, a->expr))
2774         {
2775           gfc_error ("Procedure argument at %L is INTENT(IN) while interface "
2776                      "specifies INTENT(%s)", &a->expr->where,
2777                      gfc_intent_string (f_intent));
2778           return FAILURE;
2779         }
2780
2781       if (gfc_pure (NULL) && gfc_impure_variable (a->expr->symtree->n.sym))
2782         {
2783           if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
2784             {
2785               gfc_error ("Procedure argument at %L is local to a PURE "
2786                          "procedure and is passed to an INTENT(%s) argument",
2787                          &a->expr->where, gfc_intent_string (f_intent));
2788               return FAILURE;
2789             }
2790
2791           if (f->sym->attr.pointer)
2792             {
2793               gfc_error ("Procedure argument at %L is local to a PURE "
2794                          "procedure and has the POINTER attribute",
2795                          &a->expr->where);
2796               return FAILURE;
2797             }
2798         }
2799
2800        /* Fortran 2008, C1283.  */
2801        if (gfc_pure (NULL) && gfc_is_coindexed (a->expr))
2802         {
2803           if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
2804             {
2805               gfc_error ("Coindexed actual argument at %L in PURE procedure "
2806                          "is passed to an INTENT(%s) argument",
2807                          &a->expr->where, gfc_intent_string (f_intent));
2808               return FAILURE;
2809             }
2810
2811           if (f->sym->attr.pointer)
2812             {
2813               gfc_error ("Coindexed actual argument at %L in PURE procedure "
2814                          "is passed to a POINTER dummy argument",
2815                          &a->expr->where);
2816               return FAILURE;
2817             }
2818         }
2819
2820        /* F2008, Section 12.5.2.4.  */
2821        if (a->expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
2822            && gfc_is_coindexed (a->expr))
2823          {
2824            gfc_error ("Coindexed polymorphic actual argument at %L is passed "
2825                       "polymorphic dummy argument '%s'",
2826                          &a->expr->where, f->sym->name);
2827            return FAILURE;
2828          }
2829     }
2830
2831   return SUCCESS;
2832 }
2833
2834
2835 /* Check how a procedure is used against its interface.  If all goes
2836    well, the actual argument list will also end up being properly
2837    sorted.  */
2838
2839 void
2840 gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
2841 {
2842
2843   /* Warn about calls with an implicit interface.  Special case
2844      for calling a ISO_C_BINDING becase c_loc and c_funloc
2845      are pseudo-unknown.  Additionally, warn about procedures not
2846      explicitly declared at all if requested.  */
2847   if (sym->attr.if_source == IFSRC_UNKNOWN && ! sym->attr.is_iso_c)
2848     {
2849       if (gfc_option.warn_implicit_interface)
2850         gfc_warning ("Procedure '%s' called with an implicit interface at %L",
2851                      sym->name, where);
2852       else if (gfc_option.warn_implicit_procedure
2853                && sym->attr.proc == PROC_UNKNOWN)
2854         gfc_warning ("Procedure '%s' called at %L is not explicitly declared",
2855                      sym->name, where);
2856     }
2857
2858   if (sym->attr.if_source == IFSRC_UNKNOWN)
2859     {
2860       gfc_actual_arglist *a;
2861
2862       if (sym->attr.pointer)
2863         {
2864           gfc_error("The pointer object '%s' at %L must have an explicit "
2865                     "function interface or be declared as array",
2866                     sym->name, where);
2867           return;
2868         }
2869
2870       if (sym->attr.allocatable && !sym->attr.external)
2871         {
2872           gfc_error("The allocatable object '%s' at %L must have an explicit "
2873                     "function interface or be declared as array",
2874                     sym->name, where);
2875           return;
2876         }
2877
2878       if (sym->attr.allocatable)
2879         {
2880           gfc_error("Allocatable function '%s' at %L must have an explicit "
2881                     "function interface", sym->name, where);
2882           return;
2883         }
2884
2885       for (a = *ap; a; a = a->next)
2886         {
2887           /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
2888           if (a->name != NULL && a->name[0] != '%')
2889             {
2890               gfc_error("Keyword argument requires explicit interface "
2891                         "for procedure '%s' at %L", sym->name, &a->expr->where);
2892               break;
2893             }
2894
2895           /* F2008, C1303 and C1304.  */
2896           if (a->expr
2897               && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
2898               && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2899                    && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2900                   || gfc_expr_attr (a->expr).lock_comp))
2901             {
2902               gfc_error("Actual argument of LOCK_TYPE or with LOCK_TYPE "
2903                         "component at %L requires an explicit interface for "
2904                         "procedure '%s'", &a->expr->where, sym->name);
2905               break;
2906             }
2907
2908           if (a->expr && a->expr->expr_type == EXPR_NULL
2909               && a->expr->ts.type == BT_UNKNOWN)
2910             {
2911               gfc_error ("MOLD argument to NULL required at %L", &a->expr->where);
2912               return;
2913             }
2914         }
2915
2916       return;
2917     }
2918
2919   if (!compare_actual_formal (ap, sym->formal, 0, sym->attr.elemental, where))
2920     return;
2921
2922   check_intents (sym->formal, *ap);
2923   if (gfc_option.warn_aliasing)
2924     check_some_aliasing (sym->formal, *ap);
2925 }
2926
2927
2928 /* Check how a procedure pointer component is used against its interface.
2929    If all goes well, the actual argument list will also end up being properly
2930    sorted. Completely analogous to gfc_procedure_use.  */
2931
2932 void
2933 gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
2934 {
2935
2936   /* Warn about calls with an implicit interface.  Special case
2937      for calling a ISO_C_BINDING becase c_loc and c_funloc
2938      are pseudo-unknown.  */
2939   if (gfc_option.warn_implicit_interface
2940       && comp->attr.if_source == IFSRC_UNKNOWN
2941       && !comp->attr.is_iso_c)
2942     gfc_warning ("Procedure pointer component '%s' called with an implicit "
2943                  "interface at %L", comp->name, where);
2944
2945   if (comp->attr.if_source == IFSRC_UNKNOWN)
2946     {
2947       gfc_actual_arglist *a;
2948       for (a = *ap; a; a = a->next)
2949         {
2950           /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
2951           if (a->name != NULL && a->name[0] != '%')
2952             {
2953               gfc_error("Keyword argument requires explicit interface "
2954                         "for procedure pointer component '%s' at %L",
2955                         comp->name, &a->expr->where);
2956               break;
2957             }
2958         }
2959
2960       return;
2961     }
2962
2963   if (!compare_actual_formal (ap, comp->formal, 0, comp->attr.elemental, where))
2964     return;
2965
2966   check_intents (comp->formal, *ap);
2967   if (gfc_option.warn_aliasing)
2968     check_some_aliasing (comp->formal, *ap);
2969 }
2970
2971
2972 /* Try if an actual argument list matches the formal list of a symbol,
2973    respecting the symbol's attributes like ELEMENTAL.  This is used for
2974    GENERIC resolution.  */
2975
2976 bool
2977 gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
2978 {
2979   bool r;
2980
2981   gcc_assert (sym->attr.flavor == FL_PROCEDURE);
2982
2983   r = !sym->attr.elemental;
2984   if (compare_actual_formal (args, sym->formal, r, !r, NULL))
2985     {
2986       check_intents (sym->formal, *args);
2987       if (gfc_option.warn_aliasing)
2988         check_some_aliasing (sym->formal, *args);
2989       return true;
2990     }
2991
2992   return false;
2993 }
2994
2995
2996 /* Given an interface pointer and an actual argument list, search for
2997    a formal argument list that matches the actual.  If found, returns
2998    a pointer to the symbol of the correct interface.  Returns NULL if
2999    not found.  */
3000
3001 gfc_symbol *
3002 gfc_search_interface (gfc_interface *intr, int sub_flag,
3003                       gfc_actual_arglist **ap)
3004 {
3005   gfc_symbol *elem_sym = NULL;
3006   gfc_symbol *null_sym = NULL;
3007   locus null_expr_loc;
3008   gfc_actual_arglist *a;
3009   bool has_null_arg = false;
3010
3011   for (a = *ap; a; a = a->next)
3012     if (a->expr && a->expr->expr_type == EXPR_NULL
3013         && a->expr->ts.type == BT_UNKNOWN)
3014       {
3015         has_null_arg = true;
3016         null_expr_loc = a->expr->where;
3017         break;
3018       } 
3019
3020   for (; intr; intr = intr->next)
3021     {
3022       if (sub_flag && intr->sym->attr.function)
3023         continue;
3024       if (!sub_flag && intr->sym->attr.subroutine)
3025         continue;
3026
3027       if (gfc_arglist_matches_symbol (ap, intr->sym))
3028         {
3029           if (has_null_arg && null_sym)
3030             {
3031               gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
3032                          "between specific functions %s and %s",
3033                          &null_expr_loc, null_sym->name, intr->sym->name);
3034               return NULL;
3035             }
3036           else if (has_null_arg)
3037             {
3038               null_sym = intr->sym;
3039               continue;
3040             }
3041
3042           /* Satisfy 12.4.4.1 such that an elemental match has lower
3043              weight than a non-elemental match.  */ 
3044           if (intr->sym->attr.elemental)
3045             {
3046               elem_sym = intr->sym;
3047               continue;
3048             }
3049           return intr->sym;
3050         }
3051     }
3052
3053   if (null_sym)
3054     return null_sym;
3055
3056   return elem_sym ? elem_sym : NULL;
3057 }
3058
3059
3060 /* Do a brute force recursive search for a symbol.  */
3061
3062 static gfc_symtree *
3063 find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
3064 {
3065   gfc_symtree * st;
3066
3067   if (root->n.sym == sym)
3068     return root;
3069
3070   st = NULL;
3071   if (root->left)
3072     st = find_symtree0 (root->left, sym);
3073   if (root->right && ! st)
3074     st = find_symtree0 (root->right, sym);
3075   return st;
3076 }
3077
3078
3079 /* Find a symtree for a symbol.  */
3080
3081 gfc_symtree *
3082 gfc_find_sym_in_symtree (gfc_symbol *sym)
3083 {
3084   gfc_symtree *st;
3085   gfc_namespace *ns;
3086
3087   /* First try to find it by name.  */
3088   gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
3089   if (st && st->n.sym == sym)
3090     return st;
3091
3092   /* If it's been renamed, resort to a brute-force search.  */
3093   /* TODO: avoid having to do this search.  If the symbol doesn't exist
3094      in the symtree for the current namespace, it should probably be added.  */
3095   for (ns = gfc_current_ns; ns; ns = ns->parent)
3096     {
3097       st = find_symtree0 (ns->sym_root, sym);
3098       if (st)
3099         return st;
3100     }
3101   gfc_internal_error ("Unable to find symbol %s", sym->name);
3102   /* Not reached.  */
3103 }
3104
3105
3106 /* See if the arglist to an operator-call contains a derived-type argument
3107    with a matching type-bound operator.  If so, return the matching specific
3108    procedure defined as operator-target as well as the base-object to use
3109    (which is the found derived-type argument with operator).  The generic
3110    name, if any, is transmitted to the final expression via 'gname'.  */
3111
3112 static gfc_typebound_proc*
3113 matching_typebound_op (gfc_expr** tb_base,
3114                        gfc_actual_arglist* args,
3115                        gfc_intrinsic_op op, const char* uop,
3116                        const char ** gname)
3117 {
3118   gfc_actual_arglist* base;
3119
3120   for (base = args; base; base = base->next)
3121     if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
3122       {
3123         gfc_typebound_proc* tb;
3124         gfc_symbol* derived;
3125         gfc_try result;
3126
3127         if (base->expr->ts.type == BT_CLASS)
3128           {
3129             if (!gfc_expr_attr (base->expr).class_ok)
3130               continue;
3131             derived = CLASS_DATA (base->expr)->ts.u.derived;
3132           }
3133         else
3134           derived = base->expr->ts.u.derived;
3135
3136         if (op == INTRINSIC_USER)
3137           {
3138             gfc_symtree* tb_uop;
3139
3140             gcc_assert (uop);
3141             tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
3142                                                  false, NULL);
3143
3144             if (tb_uop)
3145               tb = tb_uop->n.tb;
3146             else
3147               tb = NULL;
3148           }
3149         else
3150           tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
3151                                                 false, NULL);
3152
3153         /* This means we hit a PRIVATE operator which is use-associated and
3154            should thus not be seen.  */
3155         if (result == FAILURE)
3156           tb = NULL;
3157
3158         /* Look through the super-type hierarchy for a matching specific
3159            binding.  */
3160         for (; tb; tb = tb->overridden)
3161           {
3162             gfc_tbp_generic* g;
3163
3164             gcc_assert (tb->is_generic);
3165             for (g = tb->u.generic; g; g = g->next)
3166               {
3167                 gfc_symbol* target;
3168                 gfc_actual_arglist* argcopy;
3169                 bool matches;
3170
3171                 gcc_assert (g->specific);
3172                 if (g->specific->error)
3173                   continue;
3174
3175                 target = g->specific->u.specific->n.sym;
3176
3177                 /* Check if this arglist matches the formal.  */
3178                 argcopy = gfc_copy_actual_arglist (args);
3179                 matches = gfc_arglist_matches_symbol (&argcopy, target);
3180                 gfc_free_actual_arglist (argcopy);
3181
3182                 /* Return if we found a match.  */
3183                 if (matches)
3184                   {
3185                     *tb_base = base->expr;
3186                     *gname = g->specific_st->name;
3187                     return g->specific;
3188                   }
3189               }
3190           }
3191       }
3192
3193   return NULL;
3194 }
3195
3196
3197 /* For the 'actual arglist' of an operator call and a specific typebound
3198    procedure that has been found the target of a type-bound operator, build the
3199    appropriate EXPR_COMPCALL and resolve it.  We take this indirection over
3200    type-bound procedures rather than resolving type-bound operators 'directly'
3201    so that we can reuse the existing logic.  */
3202
3203 static void
3204 build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
3205                              gfc_expr* base, gfc_typebound_proc* target,
3206                              const char *gname)
3207 {
3208   e->expr_type = EXPR_COMPCALL;
3209   e->value.compcall.tbp = target;
3210   e->value.compcall.name = gname ? gname : "$op";
3211   e->value.compcall.actual = actual;
3212   e->value.compcall.base_object = base;
3213   e->value.compcall.ignore_pass = 1;
3214   e->value.compcall.assign = 0;
3215 }
3216
3217
3218 /* This subroutine is called when an expression is being resolved.
3219    The expression node in question is either a user defined operator
3220    or an intrinsic operator with arguments that aren't compatible
3221    with the operator.  This subroutine builds an actual argument list
3222    corresponding to the operands, then searches for a compatible
3223    interface.  If one is found, the expression node is replaced with
3224    the appropriate function call. We use the 'match' enum to specify
3225    whether a replacement has been made or not, or if an error occurred.  */
3226
3227 match
3228 gfc_extend_expr (gfc_expr *e)
3229 {
3230   gfc_actual_arglist *actual;
3231   gfc_symbol *sym;
3232   gfc_namespace *ns;
3233   gfc_user_op *uop;
3234   gfc_intrinsic_op i;
3235   const char *gname;
3236
3237   sym = NULL;
3238
3239   actual = gfc_get_actual_arglist ();
3240   actual->expr = e->value.op.op1;
3241
3242   gname = NULL;
3243
3244   if (e->value.op.op2 != NULL)
3245     {
3246       actual->next = gfc_get_actual_arglist ();
3247       actual->next->expr = e->value.op.op2;
3248     }
3249
3250   i = fold_unary_intrinsic (e->value.op.op);
3251
3252   if (i == INTRINSIC_USER)
3253     {
3254       for (ns = gfc_current_ns; ns; ns = ns->parent)
3255         {
3256           uop = gfc_find_uop (e->value.op.uop->name, ns);
3257           if (uop == NULL)
3258             continue;
3259
3260           sym = gfc_search_interface (uop->op, 0, &actual);
3261           if (sym != NULL)
3262             break;
3263         }
3264     }
3265   else
3266     {
3267       for (ns = gfc_current_ns; ns; ns = ns->parent)
3268         {
3269           /* Due to the distinction between '==' and '.eq.' and friends, one has
3270              to check if either is defined.  */
3271           switch (i)
3272             {
3273 #define CHECK_OS_COMPARISON(comp) \
3274   case INTRINSIC_##comp: \
3275   case INTRINSIC_##comp##_OS: \
3276     sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
3277     if (!sym) \
3278       sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
3279     break;
3280               CHECK_OS_COMPARISON(EQ)
3281               CHECK_OS_COMPARISON(NE)
3282               CHECK_OS_COMPARISON(GT)
3283               CHECK_OS_COMPARISON(GE)
3284               CHECK_OS_COMPARISON(LT)
3285               CHECK_OS_COMPARISON(LE)
3286 #undef CHECK_OS_COMPARISON
3287
3288               default:
3289                 sym = gfc_search_interface (ns->op[i], 0, &actual);
3290             }
3291
3292           if (sym != NULL)
3293             break;
3294         }
3295     }
3296
3297   /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
3298      found rather than just taking the first one and not checking further.  */
3299
3300   if (sym == NULL)
3301     {
3302       gfc_typebound_proc* tbo;
3303       gfc_expr* tb_base;
3304
3305       /* See if we find a matching type-bound operator.  */
3306       if (i == INTRINSIC_USER)
3307         tbo = matching_typebound_op (&tb_base, actual,
3308                                      i, e->value.op.uop->name, &gname);
3309       else
3310         switch (i)
3311           {
3312 #define CHECK_OS_COMPARISON(comp) \
3313   case INTRINSIC_##comp: \
3314   case INTRINSIC_##comp##_OS: \
3315     tbo = matching_typebound_op (&tb_base, actual, \
3316                                  INTRINSIC_##comp, NULL, &gname); \
3317     if (!tbo) \
3318       tbo = matching_typebound_op (&tb_base, actual, \
3319                                    INTRINSIC_##comp##_OS, NULL, &gname); \
3320     break;
3321             CHECK_OS_COMPARISON(EQ)
3322             CHECK_OS_COMPARISON(NE)
3323             CHECK_OS_COMPARISON(GT)
3324             CHECK_OS_COMPARISON(GE)
3325             CHECK_OS_COMPARISON(LT)
3326             CHECK_OS_COMPARISON(LE)
3327 #undef CHECK_OS_COMPARISON
3328
3329             default:
3330               tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
3331               break;
3332           }
3333               
3334       /* If there is a matching typebound-operator, replace the expression with
3335          a call to it and succeed.  */
3336       if (tbo)
3337         {
3338           gfc_try result;
3339
3340           gcc_assert (tb_base);
3341           build_compcall_for_operator (e, actual, tb_base, tbo, gname);
3342
3343           result = gfc_resolve_expr (e);
3344           if (result == FAILURE)
3345             return MATCH_ERROR;
3346
3347           return MATCH_YES;
3348         }
3349
3350       /* Don't use gfc_free_actual_arglist().  */
3351       free (actual->next);
3352       free (actual);
3353
3354       return MATCH_NO;
3355     }
3356
3357   /* Change the expression node to a function call.  */
3358   e->expr_type = EXPR_FUNCTION;
3359   e->symtree = gfc_find_sym_in_symtree (sym);
3360   e->value.function.actual = actual;
3361   e->value.function.esym = NULL;
3362   e->value.function.isym = NULL;
3363   e->value.function.name = NULL;
3364   e->user_operator = 1;
3365
3366   if (gfc_resolve_expr (e) == FAILURE)
3367     return MATCH_ERROR;
3368
3369   return MATCH_YES;
3370 }
3371
3372
3373 /* Tries to replace an assignment code node with a subroutine call to
3374    the subroutine associated with the assignment operator.  Return
3375    SUCCESS if the node was replaced.  On FAILURE, no error is
3376    generated.  */
3377
3378 gfc_try
3379 gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
3380 {
3381   gfc_actual_arglist *actual;
3382   gfc_expr *lhs, *rhs;
3383   gfc_symbol *sym;
3384   const char *gname;
3385
3386   gname = NULL;
3387
3388   lhs = c->expr1;
3389   rhs = c->expr2;
3390
3391   /* Don't allow an intrinsic assignment to be replaced.  */
3392   if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
3393       && (rhs->rank == 0 || rhs->rank == lhs->rank)
3394       && (lhs->ts.type == rhs->ts.type
3395           || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
3396     return FAILURE;
3397
3398   actual = gfc_get_actual_arglist ();
3399   actual->expr = lhs;
3400
3401   actual->next = gfc_get_actual_arglist ();
3402   actual->next->expr = rhs;
3403
3404   sym = NULL;
3405
3406   for (; ns; ns = ns->parent)
3407     {
3408       sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
3409       if (sym != NULL)
3410         break;
3411     }
3412
3413   /* TODO: Ambiguity-check, see above for gfc_extend_expr.  */
3414
3415   if (sym == NULL)
3416     {
3417       gfc_typebound_proc* tbo;
3418       gfc_expr* tb_base;
3419
3420       /* See if we find a matching type-bound assignment.  */
3421       tbo = matching_typebound_op (&tb_base, actual,
3422                                    INTRINSIC_ASSIGN, NULL, &gname);
3423               
3424       /* If there is one, replace the expression with a call to it and
3425          succeed.  */
3426       if (tbo)
3427         {
3428           gcc_assert (tb_base);
3429           c->expr1 = gfc_get_expr ();
3430           build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
3431           c->expr1->value.compcall.assign = 1;
3432           c->expr1->where = c->loc;
3433           c->expr2 = NULL;
3434           c->op = EXEC_COMPCALL;
3435
3436           /* c is resolved from the caller, so no need to do it here.  */
3437
3438           return SUCCESS;
3439         }
3440
3441       free (actual->next);
3442       free (actual);
3443       return FAILURE;
3444     }
3445
3446   /* Replace the assignment with the call.  */
3447   c->op = EXEC_ASSIGN_CALL;
3448   c->symtree = gfc_find_sym_in_symtree (sym);
3449   c->expr1 = NULL;
3450   c->expr2 = NULL;
3451   c->ext.actual = actual;
3452
3453   return SUCCESS;
3454 }
3455
3456
3457 /* Make sure that the interface just parsed is not already present in
3458    the given interface list.  Ambiguity isn't checked yet since module
3459    procedures can be present without interfaces.  */
3460
3461 static gfc_try
3462 check_new_interface (gfc_interface *base, gfc_symbol *new_sym)
3463 {
3464   gfc_interface *ip;
3465
3466   for (ip = base; ip; ip = ip->next)
3467     {
3468       if (ip->sym == new_sym)
3469         {
3470           gfc_error ("Entity '%s' at %C is already present in the interface",
3471                      new_sym->name);
3472           return FAILURE;
3473         }
3474     }
3475
3476   return SUCCESS;
3477 }
3478
3479
3480 /* Add a symbol to the current interface.  */
3481
3482 gfc_try
3483 gfc_add_interface (gfc_symbol *new_sym)
3484 {
3485   gfc_interface **head, *intr;
3486   gfc_namespace *ns;
3487   gfc_symbol *sym;
3488
3489   switch (current_interface.type)
3490     {
3491     case INTERFACE_NAMELESS:
3492     case INTERFACE_ABSTRACT:
3493       return SUCCESS;
3494
3495     case INTERFACE_INTRINSIC_OP:
3496       for (ns = current_interface.ns; ns; ns = ns->parent)
3497         switch (current_interface.op)
3498           {
3499             case INTRINSIC_EQ:
3500             case INTRINSIC_EQ_OS:
3501               if (check_new_interface (ns->op[INTRINSIC_EQ], new_sym) == FAILURE ||
3502                   check_new_interface (ns->op[INTRINSIC_EQ_OS], new_sym) == FAILURE)
3503                 return FAILURE;
3504               break;
3505
3506             case INTRINSIC_NE:
3507             case INTRINSIC_NE_OS:
3508               if (check_new_interface (ns->op[INTRINSIC_NE], new_sym) == FAILURE ||
3509                   check_new_interface (ns->op[INTRINSIC_NE_OS], new_sym) == FAILURE)
3510                 return FAILURE;
3511               break;
3512
3513             case INTRINSIC_GT:
3514             case INTRINSIC_GT_OS:
3515               if (check_new_interface (ns->op[INTRINSIC_GT], new_sym) == FAILURE ||
3516                   check_new_interface (ns->op[INTRINSIC_GT_OS], new_sym) == FAILURE)
3517                 return FAILURE;
3518               break;
3519
3520             case INTRINSIC_GE:
3521             case INTRINSIC_GE_OS:
3522               if (check_new_interface (ns->op[INTRINSIC_GE], new_sym) == FAILURE ||
3523                   check_new_interface (ns->op[INTRINSIC_GE_OS], new_sym) == FAILURE)
3524                 return FAILURE;
3525               break;
3526
3527             case INTRINSIC_LT:
3528             case INTRINSIC_LT_OS:
3529               if (check_new_interface (ns->op[INTRINSIC_LT], new_sym) == FAILURE ||
3530                   check_new_interface (ns->op[INTRINSIC_LT_OS], new_sym) == FAILURE)
3531                 return FAILURE;
3532               break;
3533
3534             case INTRINSIC_LE:
3535             case INTRINSIC_LE_OS:
3536               if (check_new_interface (ns->op[INTRINSIC_LE], new_sym) == FAILURE ||
3537                   check_new_interface (ns->op[INTRINSIC_LE_OS], new_sym) == FAILURE)
3538                 return FAILURE;
3539               break;
3540
3541             default:
3542               if (check_new_interface (ns->op[current_interface.op], new_sym) == FAILURE)
3543                 return FAILURE;
3544           }
3545
3546       head = &current_interface.ns->op[current_interface.op];
3547       break;
3548
3549     case INTERFACE_GENERIC:
3550       for (ns = current_interface.ns; ns; ns = ns->parent)
3551         {
3552           gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
3553           if (sym == NULL)
3554             continue;
3555
3556           if (check_new_interface (sym->generic, new_sym) == FAILURE)
3557             return FAILURE;
3558         }
3559
3560       head = &current_interface.sym->generic;
3561       break;
3562
3563     case INTERFACE_USER_OP:
3564       if (check_new_interface (current_interface.uop->op, new_sym)
3565           == FAILURE)
3566         return FAILURE;
3567
3568       head = &current_interface.uop->op;
3569       break;
3570
3571     default:
3572       gfc_internal_error ("gfc_add_interface(): Bad interface type");
3573     }
3574
3575   intr = gfc_get_interface ();
3576   intr->sym = new_sym;
3577   intr->where = gfc_current_locus;
3578
3579   intr->next = *head;
3580   *head = intr;
3581
3582   return SUCCESS;
3583 }
3584
3585
3586 gfc_interface *
3587 gfc_current_interface_head (void)
3588 {
3589   switch (current_interface.type)
3590     {
3591       case INTERFACE_INTRINSIC_OP:
3592         return current_interface.ns->op[current_interface.op];
3593         break;
3594
3595       case INTERFACE_GENERIC:
3596         return current_interface.sym->generic;
3597         break;
3598
3599       case INTERFACE_USER_OP:
3600         return current_interface.uop->op;
3601         break;
3602
3603       default:
3604         gcc_unreachable ();
3605     }
3606 }
3607
3608
3609 void
3610 gfc_set_current_interface_head (gfc_interface *i)
3611 {
3612   switch (current_interface.type)
3613     {
3614       case INTERFACE_INTRINSIC_OP:
3615         current_interface.ns->op[current_interface.op] = i;
3616         break;
3617
3618       case INTERFACE_GENERIC:
3619         current_interface.sym->generic = i;
3620         break;
3621
3622       case INTERFACE_USER_OP:
3623         current_interface.uop->op = i;
3624         break;
3625
3626       default:
3627         gcc_unreachable ();
3628     }
3629 }
3630
3631
3632 /* Gets rid of a formal argument list.  We do not free symbols.
3633    Symbols are freed when a namespace is freed.  */
3634
3635 void
3636 gfc_free_formal_arglist (gfc_formal_arglist *p)
3637 {
3638   gfc_formal_arglist *q;
3639
3640   for (; p; p = q)
3641     {
3642       q = p->next;
3643       free (p);
3644     }
3645 }
3646
3647
3648 /* Check that it is ok for the type-bound procedure 'proc' to override the
3649    procedure 'old', cf. F08:4.5.7.3.  */
3650
3651 gfc_try
3652 gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
3653 {
3654   locus where;
3655   const gfc_symbol *proc_target, *old_target;
3656   unsigned proc_pass_arg, old_pass_arg, argpos;
3657   gfc_formal_arglist *proc_formal, *old_formal;
3658   bool check_type;
3659   char err[200];
3660
3661   /* This procedure should only be called for non-GENERIC proc.  */
3662   gcc_assert (!proc->n.tb->is_generic);
3663
3664   /* If the overwritten procedure is GENERIC, this is an error.  */
3665   if (old->n.tb->is_generic)
3666     {
3667       gfc_error ("Can't overwrite GENERIC '%s' at %L",
3668                  old->name, &proc->n.tb->where);
3669       return FAILURE;
3670     }
3671
3672   where = proc->n.tb->where;
3673   proc_target = proc->n.tb->u.specific->n.sym;
3674   old_target = old->n.tb->u.specific->n.sym;
3675
3676   /* Check that overridden binding is not NON_OVERRIDABLE.  */
3677   if (old->n.tb->non_overridable)
3678     {
3679       gfc_error ("'%s' at %L overrides a procedure binding declared"
3680                  " NON_OVERRIDABLE", proc->name, &where);
3681       return FAILURE;
3682     }
3683
3684   /* It's an error to override a non-DEFERRED procedure with a DEFERRED one.  */
3685   if (!old->n.tb->deferred && proc->n.tb->deferred)
3686     {
3687       gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
3688                  " non-DEFERRED binding", proc->name, &where);
3689       return FAILURE;
3690     }
3691
3692   /* If the overridden binding is PURE, the overriding must be, too.  */
3693   if (old_target->attr.pure && !proc_target->attr.pure)
3694     {
3695       gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
3696                  proc->name, &where);
3697       return FAILURE;
3698     }
3699
3700   /* If the overridden binding is ELEMENTAL, the overriding must be, too.  If it
3701      is not, the overriding must not be either.  */
3702   if (old_target->attr.elemental && !proc_target->attr.elemental)
3703     {
3704       gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
3705                  " ELEMENTAL", proc->name, &where);
3706       return FAILURE;
3707     }
3708   if (!old_target->attr.elemental && proc_target->attr.elemental)
3709     {
3710       gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
3711                  " be ELEMENTAL, either", proc->name, &where);
3712       return FAILURE;
3713     }
3714
3715   /* If the overridden binding is a SUBROUTINE, the overriding must also be a
3716      SUBROUTINE.  */
3717   if (old_target->attr.subroutine && !proc_target->attr.subroutine)
3718     {
3719       gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
3720                  " SUBROUTINE", proc->name, &where);
3721       return FAILURE;
3722     }
3723
3724   /* If the overridden binding is a FUNCTION, the overriding must also be a
3725      FUNCTION and have the same characteristics.  */
3726   if (old_target->attr.function)
3727     {
3728       if (!proc_target->attr.function)
3729         {
3730           gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
3731                      " FUNCTION", proc->name, &where);
3732           return FAILURE;
3733         }
3734
3735       /* FIXME:  Do more comprehensive checking (including, for instance, the
3736          array-shape).  */
3737       gcc_assert (proc_target->result && old_target->result);
3738       if (!compare_type_rank (proc_target->result, old_target->result))
3739         {
3740           gfc_error ("'%s' at %L and the overridden FUNCTION should have"
3741                      " matching result types and ranks", proc->name, &where);
3742           return FAILURE;
3743         }
3744         
3745       /* Check string length.  */
3746       if (proc_target->result->ts.type == BT_CHARACTER
3747           && proc_target->result->ts.u.cl && old_target->result->ts.u.cl)
3748         {
3749           int compval = gfc_dep_compare_expr (proc_target->result->ts.u.cl->length,
3750                                               old_target->result->ts.u.cl->length);
3751           switch (compval)
3752           {
3753             case -1:
3754             case  1:
3755             case -3:
3756               gfc_error ("Character length mismatch between '%s' at '%L' and "
3757                          "overridden FUNCTION", proc->name, &where);
3758               return FAILURE;
3759
3760             case -2:
3761               gfc_warning ("Possible character length mismatch between '%s' at"
3762                            " '%L' and overridden FUNCTION", proc->name, &where);
3763               break;
3764
3765             case 0:
3766               break;
3767
3768             default:
3769               gfc_internal_error ("gfc_check_typebound_override: Unexpected "
3770                                   "result %i of gfc_dep_compare_expr", compval);
3771               break;
3772           }
3773         }
3774     }
3775
3776   /* If the overridden binding is PUBLIC, the overriding one must not be
3777      PRIVATE.  */
3778   if (old->n.tb->access == ACCESS_PUBLIC
3779       && proc->n.tb->access == ACCESS_PRIVATE)
3780     {
3781       gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
3782                  " PRIVATE", proc->name, &where);
3783       return FAILURE;
3784     }
3785
3786   /* Compare the formal argument lists of both procedures.  This is also abused
3787      to find the position of the passed-object dummy arguments of both
3788      bindings as at least the overridden one might not yet be resolved and we
3789      need those positions in the check below.  */
3790   proc_pass_arg = old_pass_arg = 0;
3791   if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
3792     proc_pass_arg = 1;
3793   if (!old->n.tb->nopass && !old->n.tb->pass_arg)
3794     old_pass_arg = 1;
3795   argpos = 1;
3796   for (proc_formal = proc_target->formal, old_formal = old_target->formal;
3797        proc_formal && old_formal;
3798        proc_formal = proc_formal->next, old_formal = old_formal->next)
3799     {
3800       if (proc->n.tb->pass_arg
3801           && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
3802         proc_pass_arg = argpos;
3803       if (old->n.tb->pass_arg
3804           && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
3805         old_pass_arg = argpos;
3806
3807       /* Check that the names correspond.  */
3808       if (strcmp (proc_formal->sym->name, old_formal->sym->name))
3809         {
3810           gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
3811                      " to match the corresponding argument of the overridden"
3812                      " procedure", proc_formal->sym->name, proc->name, &where,
3813                      old_formal->sym->name);
3814           return FAILURE;
3815         }
3816
3817       check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
3818       if (check_dummy_characteristics (proc_formal->sym, old_formal->sym,
3819                                        check_type, err, sizeof(err)) == FAILURE)
3820         {
3821           gfc_error ("Argument mismatch for the overriding procedure "
3822                      "'%s' at %L: %s", proc->name, &where, err);
3823           return FAILURE;
3824         }
3825
3826       ++argpos;
3827     }
3828   if (proc_formal || old_formal)
3829     {
3830       gfc_error ("'%s' at %L must have the same number of formal arguments as"
3831                  " the overridden procedure", proc->name, &where);
3832       return FAILURE;
3833     }
3834
3835   /* If the overridden binding is NOPASS, the overriding one must also be
3836      NOPASS.  */
3837   if (old->n.tb->nopass && !proc->n.tb->nopass)
3838     {
3839       gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
3840                  " NOPASS", proc->name, &where);
3841       return FAILURE;
3842     }
3843
3844   /* If the overridden binding is PASS(x), the overriding one must also be
3845      PASS and the passed-object dummy arguments must correspond.  */
3846   if (!old->n.tb->nopass)
3847     {
3848       if (proc->n.tb->nopass)
3849         {
3850           gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
3851                      " PASS", proc->name, &where);
3852           return FAILURE;
3853         }
3854
3855       if (proc_pass_arg != old_pass_arg)
3856         {
3857           gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
3858                      " the same position as the passed-object dummy argument of"
3859                      " the overridden procedure", proc->name, &where);
3860           return FAILURE;
3861         }
3862     }
3863
3864   return SUCCESS;
3865 }