2012-01-02 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / interface.c
blob773749d5ebcdd96982e7c85f66eea23b0beced9a
1 /* Deal with interfaces.
2 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
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.
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.
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/>. */
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.
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.
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.
39 Nameless interfaces:
40 Nameless interfaces create symbols with explicit interfaces within
41 the current namespace. They are otherwise unlinked.
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.
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.
54 Extended intrinsics and assignment:
55 The head of these interface lists are stored in the containing namespace.
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.
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. */
68 #include "config.h"
69 #include "system.h"
70 #include "gfortran.h"
71 #include "match.h"
72 #include "arith.h"
74 /* The current_interface structure holds information about the
75 interface currently being parsed. This structure is saved and
76 restored during recursive interfaces. */
78 gfc_interface_info current_interface;
81 /* Free a singly linked list of gfc_interface structures. */
83 void
84 gfc_free_interface (gfc_interface *intr)
86 gfc_interface *next;
88 for (; intr; intr = next)
90 next = intr->next;
91 free (intr);
96 /* Change the operators unary plus and minus into binary plus and
97 minus respectively, leaving the rest unchanged. */
99 static gfc_intrinsic_op
100 fold_unary_intrinsic (gfc_intrinsic_op op)
102 switch (op)
104 case INTRINSIC_UPLUS:
105 op = INTRINSIC_PLUS;
106 break;
107 case INTRINSIC_UMINUS:
108 op = INTRINSIC_MINUS;
109 break;
110 default:
111 break;
114 return op;
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. */
122 match
123 gfc_match_generic_spec (interface_type *type,
124 char *name,
125 gfc_intrinsic_op *op)
127 char buffer[GFC_MAX_SYMBOL_LEN + 1];
128 match m;
129 gfc_intrinsic_op i;
131 if (gfc_match (" assignment ( = )") == MATCH_YES)
133 *type = INTERFACE_INTRINSIC_OP;
134 *op = INTRINSIC_ASSIGN;
135 return MATCH_YES;
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;
145 *op = INTRINSIC_NONE;
146 if (gfc_match (" operator ( ") == MATCH_YES)
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;
154 m = gfc_match_char (')');
155 if (m == MATCH_NO)
156 goto syntax;
157 if (m != MATCH_YES)
158 return MATCH_ERROR;
160 strcpy (name, buffer);
161 *type = INTERFACE_USER_OP;
162 return MATCH_YES;
165 if (gfc_match_name (buffer) == MATCH_YES)
167 strcpy (name, buffer);
168 *type = INTERFACE_GENERIC;
169 return MATCH_YES;
172 *type = INTERFACE_NAMELESS;
173 return MATCH_YES;
175 syntax:
176 gfc_error ("Syntax error in generic specification at %C");
177 return MATCH_ERROR;
181 /* Match one of the five F95 forms of an interface statement. The
182 matcher for the abstract interface follows. */
184 match
185 gfc_match_interface (void)
187 char name[GFC_MAX_SYMBOL_LEN + 1];
188 interface_type type;
189 gfc_symbol *sym;
190 gfc_intrinsic_op op;
191 match m;
193 m = gfc_match_space ();
195 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
196 return MATCH_ERROR;
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))
203 gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
204 "at %C");
205 return MATCH_ERROR;
208 current_interface.type = type;
210 switch (type)
212 case INTERFACE_GENERIC:
213 if (gfc_get_symbol (name, NULL, &sym))
214 return MATCH_ERROR;
216 if (!sym->attr.generic
217 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
218 return MATCH_ERROR;
220 if (sym->attr.dummy)
222 gfc_error ("Dummy procedure '%s' at %C cannot have a "
223 "generic interface", sym->name);
224 return MATCH_ERROR;
227 current_interface.sym = gfc_new_block = sym;
228 break;
230 case INTERFACE_USER_OP:
231 current_interface.uop = gfc_get_uop (name);
232 break;
234 case INTERFACE_INTRINSIC_OP:
235 current_interface.op = op;
236 break;
238 case INTERFACE_NAMELESS:
239 case INTERFACE_ABSTRACT:
240 break;
243 return MATCH_YES;
248 /* Match a F2003 abstract interface. */
250 match
251 gfc_match_abstract_interface (void)
253 match m;
255 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT INTERFACE at %C")
256 == FAILURE)
257 return MATCH_ERROR;
259 m = gfc_match_eos ();
261 if (m != MATCH_YES)
263 gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
264 return MATCH_ERROR;
267 current_interface.type = INTERFACE_ABSTRACT;
269 return m;
273 /* Match the different sort of generic-specs that can be present after
274 the END INTERFACE itself. */
276 match
277 gfc_match_end_interface (void)
279 char name[GFC_MAX_SYMBOL_LEN + 1];
280 interface_type type;
281 gfc_intrinsic_op op;
282 match m;
284 m = gfc_match_space ();
286 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
287 return MATCH_ERROR;
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))
294 gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
295 "statement at %C");
296 return MATCH_ERROR;
299 m = MATCH_YES;
301 switch (current_interface.type)
303 case INTERFACE_NAMELESS:
304 case INTERFACE_ABSTRACT:
305 if (type != INTERFACE_NAMELESS)
307 gfc_error ("Expected a nameless interface at %C");
308 m = MATCH_ERROR;
311 break;
313 case INTERFACE_INTRINSIC_OP:
314 if (type != current_interface.type || op != current_interface.op)
317 if (current_interface.op == INTRINSIC_ASSIGN)
319 m = MATCH_ERROR;
320 gfc_error ("Expected 'END INTERFACE ASSIGNMENT (=)' at %C");
322 else
324 const char *s1, *s2;
325 s1 = gfc_op2string (current_interface.op);
326 s2 = gfc_op2string (op);
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;
349 m = MATCH_ERROR;
350 gfc_error ("Expecting 'END INTERFACE OPERATOR (%s)' at %C, "
351 "but got %s", s1, s2);
356 break;
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)
364 gfc_error ("Expecting 'END INTERFACE OPERATOR (.%s.)' at %C",
365 current_interface.uop->name);
366 m = MATCH_ERROR;
369 break;
371 case INTERFACE_GENERIC:
372 if (type != current_interface.type
373 || strcmp (current_interface.sym->name, name) != 0)
375 gfc_error ("Expecting 'END INTERFACE %s' at %C",
376 current_interface.sym->name);
377 m = MATCH_ERROR;
380 break;
383 return m;
387 /* Compare two derived types using the criteria in 4.4.2 of the standard,
388 recursing through gfc_compare_types for the components. */
391 gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
393 gfc_component *dt1, *dt2;
395 if (derived1 == derived2)
396 return 1;
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;
407 /* Compare type via the rules of the standard. Both types must have
408 the SEQUENCE or BIND(C) attribute to be equal. */
410 if (strcmp (derived1->name, derived2->name))
411 return 0;
413 if (derived1->component_access == ACCESS_PRIVATE
414 || derived2->component_access == ACCESS_PRIVATE)
415 return 0;
417 if (!(derived1->attr.sequence && derived2->attr.sequence)
418 && !(derived1->attr.is_bind_c && derived2->attr.is_bind_c))
419 return 0;
421 dt1 = derived1->components;
422 dt2 = derived2->components;
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 (;;)
429 if (strcmp (dt1->name, dt2->name) != 0)
430 return 0;
432 if (dt1->attr.access != dt2->attr.access)
433 return 0;
435 if (dt1->attr.pointer != dt2->attr.pointer)
436 return 0;
438 if (dt1->attr.dimension != dt2->attr.dimension)
439 return 0;
441 if (dt1->attr.allocatable != dt2->attr.allocatable)
442 return 0;
444 if (dt1->attr.dimension && gfc_compare_array_spec (dt1->as, dt2->as) == 0)
445 return 0;
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;
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;
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;
462 dt1 = dt1->next;
463 dt2 = dt2->next;
465 if (dt1 == NULL && dt2 == NULL)
466 break;
467 if (dt1 == NULL || dt2 == NULL)
468 return 0;
471 return 1;
475 /* Compare two typespecs, recursively if necessary. */
478 gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
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;
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);
494 /* Compare derived types. */
495 if (gfc_type_compatible (ts1, ts2))
496 return 1;
498 return gfc_compare_derived_types (ts1->u.derived ,ts2->u.derived);
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. */
506 static int
507 compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
509 int r1, r2;
511 r1 = (s1->as != NULL) ? s1->as->rank : 0;
512 r2 = (s2->as != NULL) ? s2->as->rank : 0;
514 if (r1 != r2)
515 return 0; /* Ranks differ. */
517 return gfc_compare_types (&s1->ts, &s2->ts);
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. */
525 static int
526 compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
528 if (s1 == NULL || s2 == NULL)
529 return s1 == s2 ? 1 : 0;
531 if (s1 == s2)
532 return 1;
534 if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
535 return compare_type_rank (s1, s2);
537 if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
538 return 0;
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;
547 if (s2->attr.function == 0 && s2->attr.subroutine == 0)
548 return s2->attr.external == 1 ? compare_type_rank (s1, s2) : 0;
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;
555 if (s1->attr.function && compare_type_rank (s1, s2) == 0)
556 return 0;
558 /* Originally, gfortran recursed here to check the interfaces of passed
559 procedures. This is explicitly not required by the standard. */
560 return 1;
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. */
568 static gfc_symbol *
569 find_keyword_arg (const char *name, gfc_formal_arglist *f)
571 for (; f; f = f->next)
572 if (strcmp (f->sym->name, name) == 0)
573 return f->sym;
575 return NULL;
579 /******** Interface checking subroutines **********/
582 /* Given an operator interface and the operator, make sure that all
583 interfaces for that operator are legal. */
585 bool
586 gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
587 locus opwhere)
589 gfc_formal_arglist *formal;
590 sym_intent i1, i2;
591 bt t1, t2;
592 int args, r1, r2, k1, k2;
594 gcc_assert (sym);
596 args = 0;
597 t1 = t2 = BT_UNKNOWN;
598 i1 = i2 = INTENT_UNKNOWN;
599 r1 = r2 = -1;
600 k1 = k2 = -1;
602 for (formal = sym->formal; formal; formal = formal->next)
604 gfc_symbol *fsym = formal->sym;
605 if (fsym == NULL)
607 gfc_error ("Alternate return cannot appear in operator "
608 "interface at %L", &sym->declared_at);
609 return false;
611 if (args == 0)
613 t1 = fsym->ts.type;
614 i1 = fsym->attr.intent;
615 r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
616 k1 = fsym->ts.kind;
618 if (args == 1)
620 t2 = fsym->ts.type;
621 i2 = fsym->attr.intent;
622 r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
623 k2 = fsym->ts.kind;
625 args++;
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))
635 gfc_error ("Operator interface at %L has the wrong number of arguments",
636 &sym->declared_at);
637 return false;
640 /* Check that intrinsics are mapped to functions, except
641 INTRINSIC_ASSIGN which should map to a subroutine. */
642 if (op == INTRINSIC_ASSIGN)
644 if (!sym->attr.subroutine)
646 gfc_error ("Assignment operator interface at %L must be "
647 "a SUBROUTINE", &sym->declared_at);
648 return false;
650 if (args != 2)
652 gfc_error ("Assignment operator interface at %L must have "
653 "two arguments", &sym->declared_at);
654 return false;
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))))
669 gfc_error ("Assignment operator interface at %L must not redefine "
670 "an INTRINSIC type assignment", &sym->declared_at);
671 return false;
674 else
676 if (!sym->attr.function)
678 gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
679 &sym->declared_at);
680 return false;
684 /* Check intents on operator interfaces. */
685 if (op == INTRINSIC_ASSIGN)
687 if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
689 gfc_error ("First argument of defined assignment at %L must be "
690 "INTENT(OUT) or INTENT(INOUT)", &sym->declared_at);
691 return false;
694 if (i2 != INTENT_IN)
696 gfc_error ("Second argument of defined assignment at %L must be "
697 "INTENT(IN)", &sym->declared_at);
698 return false;
701 else
703 if (i1 != INTENT_IN)
705 gfc_error ("First argument of operator interface at %L must be "
706 "INTENT(IN)", &sym->declared_at);
707 return false;
710 if (args == 2 && i2 != INTENT_IN)
712 gfc_error ("Second argument of operator interface at %L must be "
713 "INTENT(IN)", &sym->declared_at);
714 return false;
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:
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)." */
729 #define IS_NUMERIC_TYPE(t) \
730 ((t) == BT_INTEGER || (t) == BT_REAL || (t) == BT_COMPLEX)
732 /* Unary ops are easy, do them first. */
733 if (op == INTRINSIC_NOT)
735 if (t1 == BT_LOGICAL)
736 goto bad_repl;
737 else
738 return true;
741 if (args == 1 && (op == INTRINSIC_PLUS || op == INTRINSIC_MINUS))
743 if (IS_NUMERIC_TYPE (t1))
744 goto bad_repl;
745 else
746 return true;
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;
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;
761 switch (op)
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. */
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;
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;
795 case INTRINSIC_CONCAT:
796 if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
797 goto bad_repl;
798 break;
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;
808 default:
809 break;
812 return true;
814 #undef IS_NUMERIC_TYPE
816 bad_repl:
817 gfc_error ("Operator interface at %L conflicts with intrinsic interface",
818 &opwhere);
819 return false;
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. */
832 static int
833 count_types_test (gfc_formal_arglist *f1, gfc_formal_arglist *f2)
835 int rc, ac1, ac2, i, j, k, n1;
836 gfc_formal_arglist *f;
838 typedef struct
840 int flag;
841 gfc_symbol *sym;
843 arginfo;
845 arginfo *arg;
847 n1 = 0;
849 for (f = f1; f; f = f->next)
850 n1++;
852 /* Build an array of integers that gives the same integer to
853 arguments of the same type/rank. */
854 arg = XCNEWVEC (arginfo, n1);
856 f = f1;
857 for (i = 0; i < n1; i++, f = f->next)
859 arg[i].flag = -1;
860 arg[i].sym = f->sym;
863 k = 0;
865 for (i = 0; i < n1; i++)
867 if (arg[i].flag != -1)
868 continue;
870 if (arg[i].sym && arg[i].sym->attr.optional)
871 continue; /* Skip optional arguments. */
873 arg[i].flag = k;
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;
882 k++;
885 /* Now loop over each distinct type found in f1. */
886 k = 0;
887 rc = 0;
889 for (i = 0; i < n1; i++)
891 if (arg[i].flag != k)
892 continue;
894 ac1 = 1;
895 for (j = i + 1; j < n1; j++)
896 if (arg[j].flag == k)
897 ac1++;
899 /* Count the number of arguments in f2 with that type, including
900 those that are optional. */
901 ac2 = 0;
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++;
908 if (ac1 > ac2)
910 rc = 1;
911 break;
914 k++;
917 free (arg);
919 return rc;
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.
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:
931 INTERFACE FOO
932 SUBROUTINE F1(A, B)
933 INTEGER :: A ; REAL :: B
934 END SUBROUTINE F1
936 SUBROUTINE F2(B, A)
937 INTEGER :: A ; REAL :: B
938 END SUBROUTINE F1
939 END INTERFACE FOO
941 At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous. */
943 static int
944 generic_correspondence (gfc_formal_arglist *f1, gfc_formal_arglist *f2)
946 gfc_formal_arglist *f2_save, *g;
947 gfc_symbol *sym;
949 f2_save = f2;
951 while (f1)
953 if (f1->sym->attr.optional)
954 goto next;
956 if (f2 != NULL && (compare_type_rank (f1->sym, f2->sym)
957 || compare_type_rank (f2->sym, f1->sym)))
958 goto next;
960 /* Now search for a disambiguating keyword argument starting at
961 the current non-match. */
962 for (g = f1; g; g = g->next)
964 if (g->sym->attr.optional)
965 continue;
967 sym = find_keyword_arg (g->sym->name, f2_save);
968 if (sym == NULL || !compare_type_rank (g->sym, sym))
969 return 1;
972 next:
973 f1 = f1->next;
974 if (f2 != NULL)
975 f2 = f2->next;
978 return 0;
982 /* Check if the characteristics of two dummy arguments match,
983 cf. F08:12.3.2. */
985 static gfc_try
986 check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
987 bool type_must_agree, char *errmsg, int err_len)
989 /* Check type and rank. */
990 if (type_must_agree && !compare_type_rank (s2, s1))
992 if (errmsg != NULL)
993 snprintf (errmsg, err_len, "Type/rank mismatch in argument '%s'",
994 s1->name);
995 return FAILURE;
998 /* Check INTENT. */
999 if (s1->attr.intent != s2->attr.intent)
1001 snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
1002 s1->name);
1003 return FAILURE;
1006 /* Check OPTIONAL attribute. */
1007 if (s1->attr.optional != s2->attr.optional)
1009 snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
1010 s1->name);
1011 return FAILURE;
1014 /* Check ALLOCATABLE attribute. */
1015 if (s1->attr.allocatable != s2->attr.allocatable)
1017 snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
1018 s1->name);
1019 return FAILURE;
1022 /* Check POINTER attribute. */
1023 if (s1->attr.pointer != s2->attr.pointer)
1025 snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
1026 s1->name);
1027 return FAILURE;
1030 /* Check TARGET attribute. */
1031 if (s1->attr.target != s2->attr.target)
1033 snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
1034 s1->name);
1035 return FAILURE;
1038 /* FIXME: Do more comprehensive testing of attributes, like e.g.
1039 ASYNCHRONOUS, CONTIGUOUS, VALUE, VOLATILE, etc. */
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)
1046 int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
1047 s2->ts.u.cl->length);
1048 switch (compval)
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;
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;
1063 case 0:
1064 break;
1066 default:
1067 gfc_internal_error ("check_dummy_characteristics: Unexpected result "
1068 "%i of gfc_dep_compare_expr", compval);
1069 break;
1073 /* Check array shape. */
1074 if (s1->as && s2->as)
1076 int i, compval;
1077 gfc_expr *shape1, *shape2;
1079 if (s1->as->type != s2->as->type)
1081 snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
1082 s1->name);
1083 return FAILURE;
1086 if (s1->as->type == AS_EXPLICIT)
1087 for (i = 0; i < s1->as->rank + s1->as->corank; i++)
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)
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;
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;
1111 case 0:
1112 break;
1114 default:
1115 gfc_internal_error ("check_dummy_characteristics: Unexpected "
1116 "result %i of gfc_dep_compare_expr",
1117 compval);
1118 break;
1123 return SUCCESS;
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.*/
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)
1138 gfc_formal_arglist *f1, *f2;
1140 gcc_assert (name2 != NULL);
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)))
1146 if (errmsg != NULL)
1147 snprintf (errmsg, err_len, "'%s' is not a function", name2);
1148 return 0;
1151 if (s1->attr.subroutine && s2->attr.function)
1153 if (errmsg != NULL)
1154 snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
1155 return 0;
1158 /* Do strict checks on all characteristics
1159 (for dummy procedures and procedure pointer assignments). */
1160 if (!generic_flag && strict_flag)
1162 if (s1->attr.function && s2->attr.function)
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))
1169 if (errmsg != NULL)
1170 snprintf (errmsg, err_len, "Type/rank mismatch in return value "
1171 "of '%s'", name2);
1172 return 0;
1175 /* FIXME: Check array bounds and string length of result. */
1178 if (s1->attr.pure && !s2->attr.pure)
1180 snprintf (errmsg, err_len, "Mismatch in PURE attribute");
1181 return 0;
1183 if (s1->attr.elemental && !s2->attr.elemental)
1185 snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
1186 return 0;
1190 if (s1->attr.if_source == IFSRC_UNKNOWN
1191 || s2->attr.if_source == IFSRC_UNKNOWN)
1192 return 1;
1194 f1 = s1->formal;
1195 f2 = s2->formal;
1197 if (f1 == NULL && f2 == NULL)
1198 return 1; /* Special case: No arguments. */
1200 if (generic_flag)
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;
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. */
1213 for (;;)
1215 /* Check existence. */
1216 if (f1 == NULL && f2 == NULL)
1217 break;
1218 if (f1 == NULL || f2 == NULL)
1220 if (errmsg != NULL)
1221 snprintf (errmsg, err_len, "'%s' has the wrong number of "
1222 "arguments", name2);
1223 return 0;
1226 if (strict_flag)
1228 /* Check all characteristics. */
1229 if (check_dummy_characteristics (f1->sym, f2->sym,
1230 true, errmsg, err_len) == FAILURE)
1231 return 0;
1233 else if (!compare_type_rank (f2->sym, f1->sym))
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;
1242 f1 = f1->next;
1243 f2 = f2->next;
1246 return 1;
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. */
1255 static int
1256 check_interface0 (gfc_interface *p, const char *interface_name)
1258 gfc_interface *psave, *q, *qlast;
1260 psave = p;
1261 for (; p; p = p->next)
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 && p->sym->attr.flavor != FL_DERIVED)
1269 if (p->sym->attr.external)
1270 gfc_error ("Procedure '%s' in %s at %L has no explicit interface",
1271 p->sym->name, interface_name, &p->sym->declared_at);
1272 else
1273 gfc_error ("Procedure '%s' in %s at %L is neither function nor "
1274 "subroutine", p->sym->name, interface_name,
1275 &p->sym->declared_at);
1276 return 1;
1279 /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs. */
1280 if ((psave->sym->attr.function && !p->sym->attr.function
1281 && p->sym->attr.flavor != FL_DERIVED)
1282 || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
1284 if (p->sym->attr.flavor != FL_DERIVED)
1285 gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
1286 " or all FUNCTIONs", interface_name,
1287 &p->sym->declared_at);
1288 else
1289 gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
1290 "generic name is also the name of a derived type",
1291 interface_name, &p->sym->declared_at);
1292 return 1;
1295 if (p->sym->attr.proc == PROC_INTERNAL
1296 && gfc_notify_std (GFC_STD_GNU, "Extension: Internal procedure '%s' "
1297 "in %s at %L", p->sym->name, interface_name,
1298 &p->sym->declared_at) == FAILURE)
1299 return 1;
1301 p = psave;
1303 /* Remove duplicate interfaces in this interface list. */
1304 for (; p; p = p->next)
1306 qlast = p;
1308 for (q = p->next; q;)
1310 if (p->sym != q->sym)
1312 qlast = q;
1313 q = q->next;
1315 else
1317 /* Duplicate interface. */
1318 qlast->next = q->next;
1319 free (q);
1320 q = qlast->next;
1325 return 0;
1329 /* Check lists of interfaces to make sure that no two interfaces are
1330 ambiguous. Duplicate interfaces (from the same symbol) are OK here. */
1332 static int
1333 check_interface1 (gfc_interface *p, gfc_interface *q0,
1334 int generic_flag, const char *interface_name,
1335 bool referenced)
1337 gfc_interface *q;
1338 for (; p; p = p->next)
1339 for (q = q0; q; q = q->next)
1341 if (p->sym == q->sym)
1342 continue; /* Duplicates OK here. */
1344 if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
1345 continue;
1347 if (p->sym->attr.flavor != FL_DERIVED
1348 && q->sym->attr.flavor != FL_DERIVED
1349 && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
1350 generic_flag, 0, NULL, 0))
1352 if (referenced)
1353 gfc_error ("Ambiguous interfaces '%s' and '%s' in %s at %L",
1354 p->sym->name, q->sym->name, interface_name,
1355 &p->where);
1356 else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
1357 gfc_warning ("Ambiguous interfaces '%s' and '%s' in %s at %L",
1358 p->sym->name, q->sym->name, interface_name,
1359 &p->where);
1360 else
1361 gfc_warning ("Although not referenced, '%s' has ambiguous "
1362 "interfaces at %L", interface_name, &p->where);
1363 return 1;
1366 return 0;
1370 /* Check the generic and operator interfaces of symbols to make sure
1371 that none of the interfaces conflict. The check has to be done
1372 after all of the symbols are actually loaded. */
1374 static void
1375 check_sym_interfaces (gfc_symbol *sym)
1377 char interface_name[100];
1378 gfc_interface *p;
1380 if (sym->ns != gfc_current_ns)
1381 return;
1383 if (sym->generic != NULL)
1385 sprintf (interface_name, "generic interface '%s'", sym->name);
1386 if (check_interface0 (sym->generic, interface_name))
1387 return;
1389 for (p = sym->generic; p; p = p->next)
1391 if (p->sym->attr.mod_proc
1392 && (p->sym->attr.if_source != IFSRC_DECL
1393 || p->sym->attr.procedure))
1395 gfc_error ("'%s' at %L is not a module procedure",
1396 p->sym->name, &p->where);
1397 return;
1401 /* Originally, this test was applied to host interfaces too;
1402 this is incorrect since host associated symbols, from any
1403 source, cannot be ambiguous with local symbols. */
1404 check_interface1 (sym->generic, sym->generic, 1, interface_name,
1405 sym->attr.referenced || !sym->attr.use_assoc);
1410 static void
1411 check_uop_interfaces (gfc_user_op *uop)
1413 char interface_name[100];
1414 gfc_user_op *uop2;
1415 gfc_namespace *ns;
1417 sprintf (interface_name, "operator interface '%s'", uop->name);
1418 if (check_interface0 (uop->op, interface_name))
1419 return;
1421 for (ns = gfc_current_ns; ns; ns = ns->parent)
1423 uop2 = gfc_find_uop (uop->name, ns);
1424 if (uop2 == NULL)
1425 continue;
1427 check_interface1 (uop->op, uop2->op, 0,
1428 interface_name, true);
1432 /* Given an intrinsic op, return an equivalent op if one exists,
1433 or INTRINSIC_NONE otherwise. */
1435 gfc_intrinsic_op
1436 gfc_equivalent_op (gfc_intrinsic_op op)
1438 switch(op)
1440 case INTRINSIC_EQ:
1441 return INTRINSIC_EQ_OS;
1443 case INTRINSIC_EQ_OS:
1444 return INTRINSIC_EQ;
1446 case INTRINSIC_NE:
1447 return INTRINSIC_NE_OS;
1449 case INTRINSIC_NE_OS:
1450 return INTRINSIC_NE;
1452 case INTRINSIC_GT:
1453 return INTRINSIC_GT_OS;
1455 case INTRINSIC_GT_OS:
1456 return INTRINSIC_GT;
1458 case INTRINSIC_GE:
1459 return INTRINSIC_GE_OS;
1461 case INTRINSIC_GE_OS:
1462 return INTRINSIC_GE;
1464 case INTRINSIC_LT:
1465 return INTRINSIC_LT_OS;
1467 case INTRINSIC_LT_OS:
1468 return INTRINSIC_LT;
1470 case INTRINSIC_LE:
1471 return INTRINSIC_LE_OS;
1473 case INTRINSIC_LE_OS:
1474 return INTRINSIC_LE;
1476 default:
1477 return INTRINSIC_NONE;
1481 /* For the namespace, check generic, user operator and intrinsic
1482 operator interfaces for consistency and to remove duplicate
1483 interfaces. We traverse the whole namespace, counting on the fact
1484 that most symbols will not have generic or operator interfaces. */
1486 void
1487 gfc_check_interfaces (gfc_namespace *ns)
1489 gfc_namespace *old_ns, *ns2;
1490 char interface_name[100];
1491 int i;
1493 old_ns = gfc_current_ns;
1494 gfc_current_ns = ns;
1496 gfc_traverse_ns (ns, check_sym_interfaces);
1498 gfc_traverse_user_op (ns, check_uop_interfaces);
1500 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
1502 if (i == INTRINSIC_USER)
1503 continue;
1505 if (i == INTRINSIC_ASSIGN)
1506 strcpy (interface_name, "intrinsic assignment operator");
1507 else
1508 sprintf (interface_name, "intrinsic '%s' operator",
1509 gfc_op2string ((gfc_intrinsic_op) i));
1511 if (check_interface0 (ns->op[i], interface_name))
1512 continue;
1514 if (ns->op[i])
1515 gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
1516 ns->op[i]->where);
1518 for (ns2 = ns; ns2; ns2 = ns2->parent)
1520 gfc_intrinsic_op other_op;
1522 if (check_interface1 (ns->op[i], ns2->op[i], 0,
1523 interface_name, true))
1524 goto done;
1526 /* i should be gfc_intrinsic_op, but has to be int with this cast
1527 here for stupid C++ compatibility rules. */
1528 other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
1529 if (other_op != INTRINSIC_NONE
1530 && check_interface1 (ns->op[i], ns2->op[other_op],
1531 0, interface_name, true))
1532 goto done;
1536 done:
1537 gfc_current_ns = old_ns;
1541 static int
1542 symbol_rank (gfc_symbol *sym)
1544 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
1545 return CLASS_DATA (sym)->as->rank;
1547 return (sym->as == NULL) ? 0 : sym->as->rank;
1551 /* Given a symbol of a formal argument list and an expression, if the
1552 formal argument is allocatable, check that the actual argument is
1553 allocatable. Returns nonzero if compatible, zero if not compatible. */
1555 static int
1556 compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
1558 symbol_attribute attr;
1560 if (formal->attr.allocatable
1561 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
1563 attr = gfc_expr_attr (actual);
1564 if (!attr.allocatable)
1565 return 0;
1568 return 1;
1572 /* Given a symbol of a formal argument list and an expression, if the
1573 formal argument is a pointer, see if the actual argument is a
1574 pointer. Returns nonzero if compatible, zero if not compatible. */
1576 static int
1577 compare_pointer (gfc_symbol *formal, gfc_expr *actual)
1579 symbol_attribute attr;
1581 if (formal->attr.pointer)
1583 attr = gfc_expr_attr (actual);
1585 /* Fortran 2008 allows non-pointer actual arguments. */
1586 if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
1587 return 2;
1589 if (!attr.pointer)
1590 return 0;
1593 return 1;
1597 /* Emit clear error messages for rank mismatch. */
1599 static void
1600 argument_rank_mismatch (const char *name, locus *where,
1601 int rank1, int rank2)
1603 if (rank1 == 0)
1605 gfc_error ("Rank mismatch in argument '%s' at %L "
1606 "(scalar and rank-%d)", name, where, rank2);
1608 else if (rank2 == 0)
1610 gfc_error ("Rank mismatch in argument '%s' at %L "
1611 "(rank-%d and scalar)", name, where, rank1);
1613 else
1615 gfc_error ("Rank mismatch in argument '%s' at %L "
1616 "(rank-%d and rank-%d)", name, where, rank1, rank2);
1621 /* Given a symbol of a formal argument list and an expression, see if
1622 the two are compatible as arguments. Returns nonzero if
1623 compatible, zero if not compatible. */
1625 static int
1626 compare_parameter (gfc_symbol *formal, gfc_expr *actual,
1627 int ranks_must_agree, int is_elemental, locus *where)
1629 gfc_ref *ref;
1630 bool rank_check, is_pointer;
1632 /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
1633 procs c_f_pointer or c_f_procpointer, and we need to accept most
1634 pointers the user could give us. This should allow that. */
1635 if (formal->ts.type == BT_VOID)
1636 return 1;
1638 if (formal->ts.type == BT_DERIVED
1639 && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
1640 && actual->ts.type == BT_DERIVED
1641 && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
1642 return 1;
1644 if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
1645 /* Make sure the vtab symbol is present when
1646 the module variables are generated. */
1647 gfc_find_derived_vtab (actual->ts.u.derived);
1649 if (actual->ts.type == BT_PROCEDURE)
1651 char err[200];
1652 gfc_symbol *act_sym = actual->symtree->n.sym;
1654 if (formal->attr.flavor != FL_PROCEDURE)
1656 if (where)
1657 gfc_error ("Invalid procedure argument at %L", &actual->where);
1658 return 0;
1661 if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
1662 sizeof(err)))
1664 if (where)
1665 gfc_error ("Interface mismatch in dummy procedure '%s' at %L: %s",
1666 formal->name, &actual->where, err);
1667 return 0;
1670 if (formal->attr.function && !act_sym->attr.function)
1672 gfc_add_function (&act_sym->attr, act_sym->name,
1673 &act_sym->declared_at);
1674 if (act_sym->ts.type == BT_UNKNOWN
1675 && gfc_set_default_type (act_sym, 1, act_sym->ns) == FAILURE)
1676 return 0;
1678 else if (formal->attr.subroutine && !act_sym->attr.subroutine)
1679 gfc_add_subroutine (&act_sym->attr, act_sym->name,
1680 &act_sym->declared_at);
1682 return 1;
1685 /* F2008, C1241. */
1686 if (formal->attr.pointer && formal->attr.contiguous
1687 && !gfc_is_simply_contiguous (actual, true))
1689 if (where)
1690 gfc_error ("Actual argument to contiguous pointer dummy '%s' at %L "
1691 "must be simply contigous", formal->name, &actual->where);
1692 return 0;
1695 if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
1696 && actual->ts.type != BT_HOLLERITH
1697 && !gfc_compare_types (&formal->ts, &actual->ts)
1698 && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
1699 && gfc_compare_derived_types (formal->ts.u.derived,
1700 CLASS_DATA (actual)->ts.u.derived)))
1702 if (where)
1703 gfc_error ("Type mismatch in argument '%s' at %L; passed %s to %s",
1704 formal->name, &actual->where, gfc_typename (&actual->ts),
1705 gfc_typename (&formal->ts));
1706 return 0;
1709 /* F2003, 12.5.2.5. */
1710 if (formal->ts.type == BT_CLASS
1711 && (CLASS_DATA (formal)->attr.class_pointer
1712 || CLASS_DATA (formal)->attr.allocatable))
1714 if (actual->ts.type != BT_CLASS)
1716 if (where)
1717 gfc_error ("Actual argument to '%s' at %L must be polymorphic",
1718 formal->name, &actual->where);
1719 return 0;
1721 if (CLASS_DATA (actual)->ts.u.derived
1722 != CLASS_DATA (formal)->ts.u.derived)
1724 if (where)
1725 gfc_error ("Actual argument to '%s' at %L must have the same "
1726 "declared type", formal->name, &actual->where);
1727 return 0;
1731 if (formal->attr.codimension && !gfc_is_coarray (actual))
1733 if (where)
1734 gfc_error ("Actual argument to '%s' at %L must be a coarray",
1735 formal->name, &actual->where);
1736 return 0;
1739 if (formal->attr.codimension && formal->attr.allocatable)
1741 gfc_ref *last = NULL;
1743 for (ref = actual->ref; ref; ref = ref->next)
1744 if (ref->type == REF_COMPONENT)
1745 last = ref;
1747 /* F2008, 12.5.2.6. */
1748 if ((last && last->u.c.component->as->corank != formal->as->corank)
1749 || (!last
1750 && actual->symtree->n.sym->as->corank != formal->as->corank))
1752 if (where)
1753 gfc_error ("Corank mismatch in argument '%s' at %L (%d and %d)",
1754 formal->name, &actual->where, formal->as->corank,
1755 last ? last->u.c.component->as->corank
1756 : actual->symtree->n.sym->as->corank);
1757 return 0;
1761 if (formal->attr.codimension)
1763 /* F2008, 12.5.2.8. */
1764 if (formal->attr.dimension
1765 && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
1766 && gfc_expr_attr (actual).dimension
1767 && !gfc_is_simply_contiguous (actual, true))
1769 if (where)
1770 gfc_error ("Actual argument to '%s' at %L must be simply "
1771 "contiguous", formal->name, &actual->where);
1772 return 0;
1775 /* F2008, C1303 and C1304. */
1776 if (formal->attr.intent != INTENT_INOUT
1777 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
1778 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
1779 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
1780 || formal->attr.lock_comp))
1783 if (where)
1784 gfc_error ("Actual argument to non-INTENT(INOUT) dummy '%s' at %L, "
1785 "which is LOCK_TYPE or has a LOCK_TYPE component",
1786 formal->name, &actual->where);
1787 return 0;
1791 /* F2008, C1239/C1240. */
1792 if (actual->expr_type == EXPR_VARIABLE
1793 && (actual->symtree->n.sym->attr.asynchronous
1794 || actual->symtree->n.sym->attr.volatile_)
1795 && (formal->attr.asynchronous || formal->attr.volatile_)
1796 && actual->rank && !gfc_is_simply_contiguous (actual, true)
1797 && ((formal->as->type != AS_ASSUMED_SHAPE && !formal->attr.pointer)
1798 || formal->attr.contiguous))
1800 if (where)
1801 gfc_error ("Dummy argument '%s' has to be a pointer or assumed-shape "
1802 "array without CONTIGUOUS attribute - as actual argument at"
1803 " %L is not simply contiguous and both are ASYNCHRONOUS "
1804 "or VOLATILE", formal->name, &actual->where);
1805 return 0;
1808 if (formal->attr.allocatable && !formal->attr.codimension
1809 && gfc_expr_attr (actual).codimension)
1811 if (formal->attr.intent == INTENT_OUT)
1813 if (where)
1814 gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
1815 "INTENT(OUT) dummy argument '%s'", &actual->where,
1816 formal->name);
1817 return 0;
1819 else if (gfc_option.warn_surprising && where
1820 && formal->attr.intent != INTENT_IN)
1821 gfc_warning ("Passing coarray at %L to allocatable, noncoarray dummy "
1822 "argument '%s', which is invalid if the allocation status"
1823 " is modified", &actual->where, formal->name);
1826 if (symbol_rank (formal) == actual->rank)
1827 return 1;
1829 if (actual->ts.type == BT_CLASS && CLASS_DATA (actual)->as
1830 && CLASS_DATA (actual)->as->rank == symbol_rank (formal))
1831 return 1;
1833 rank_check = where != NULL && !is_elemental && formal->as
1834 && (formal->as->type == AS_ASSUMED_SHAPE
1835 || formal->as->type == AS_DEFERRED)
1836 && actual->expr_type != EXPR_NULL;
1838 /* Scalar & coindexed, see: F2008, Section 12.5.2.4. */
1839 if (rank_check || ranks_must_agree
1840 || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
1841 || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
1842 || (actual->rank == 0
1843 && ((formal->ts.type == BT_CLASS
1844 && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
1845 || (formal->ts.type != BT_CLASS
1846 && formal->as->type == AS_ASSUMED_SHAPE))
1847 && actual->expr_type != EXPR_NULL)
1848 || (actual->rank == 0 && formal->attr.dimension
1849 && gfc_is_coindexed (actual)))
1851 if (where)
1852 argument_rank_mismatch (formal->name, &actual->where,
1853 symbol_rank (formal), actual->rank);
1854 return 0;
1856 else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
1857 return 1;
1859 /* At this point, we are considering a scalar passed to an array. This
1860 is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
1861 - if the actual argument is (a substring of) an element of a
1862 non-assumed-shape/non-pointer/non-polymorphic array; or
1863 - (F2003) if the actual argument is of type character of default/c_char
1864 kind. */
1866 is_pointer = actual->expr_type == EXPR_VARIABLE
1867 ? actual->symtree->n.sym->attr.pointer : false;
1869 for (ref = actual->ref; ref; ref = ref->next)
1871 if (ref->type == REF_COMPONENT)
1872 is_pointer = ref->u.c.component->attr.pointer;
1873 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
1874 && ref->u.ar.dimen > 0
1875 && (!ref->next
1876 || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
1877 break;
1880 if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
1882 if (where)
1883 gfc_error ("Polymorphic scalar passed to array dummy argument '%s' "
1884 "at %L", formal->name, &actual->where);
1885 return 0;
1888 if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
1889 && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
1891 if (where)
1892 gfc_error ("Element of assumed-shaped or pointer "
1893 "array passed to array dummy argument '%s' at %L",
1894 formal->name, &actual->where);
1895 return 0;
1898 if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
1899 && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
1901 if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
1903 if (where)
1904 gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
1905 "CHARACTER actual argument with array dummy argument "
1906 "'%s' at %L", formal->name, &actual->where);
1907 return 0;
1910 if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
1912 gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
1913 "array dummy argument '%s' at %L",
1914 formal->name, &actual->where);
1915 return 0;
1917 else if ((gfc_option.allow_std & GFC_STD_F2003) == 0)
1918 return 0;
1919 else
1920 return 1;
1923 if (ref == NULL && actual->expr_type != EXPR_NULL)
1925 if (where)
1926 argument_rank_mismatch (formal->name, &actual->where,
1927 symbol_rank (formal), actual->rank);
1928 return 0;
1931 return 1;
1935 /* Returns the storage size of a symbol (formal argument) or
1936 zero if it cannot be determined. */
1938 static unsigned long
1939 get_sym_storage_size (gfc_symbol *sym)
1941 int i;
1942 unsigned long strlen, elements;
1944 if (sym->ts.type == BT_CHARACTER)
1946 if (sym->ts.u.cl && sym->ts.u.cl->length
1947 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1948 strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
1949 else
1950 return 0;
1952 else
1953 strlen = 1;
1955 if (symbol_rank (sym) == 0)
1956 return strlen;
1958 elements = 1;
1959 if (sym->as->type != AS_EXPLICIT)
1960 return 0;
1961 for (i = 0; i < sym->as->rank; i++)
1963 if (!sym->as || sym->as->upper[i]->expr_type != EXPR_CONSTANT
1964 || sym->as->lower[i]->expr_type != EXPR_CONSTANT)
1965 return 0;
1967 elements *= mpz_get_si (sym->as->upper[i]->value.integer)
1968 - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
1971 return strlen*elements;
1975 /* Returns the storage size of an expression (actual argument) or
1976 zero if it cannot be determined. For an array element, it returns
1977 the remaining size as the element sequence consists of all storage
1978 units of the actual argument up to the end of the array. */
1980 static unsigned long
1981 get_expr_storage_size (gfc_expr *e)
1983 int i;
1984 long int strlen, elements;
1985 long int substrlen = 0;
1986 bool is_str_storage = false;
1987 gfc_ref *ref;
1989 if (e == NULL)
1990 return 0;
1992 if (e->ts.type == BT_CHARACTER)
1994 if (e->ts.u.cl && e->ts.u.cl->length
1995 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1996 strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
1997 else if (e->expr_type == EXPR_CONSTANT
1998 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
1999 strlen = e->value.character.length;
2000 else
2001 return 0;
2003 else
2004 strlen = 1; /* Length per element. */
2006 if (e->rank == 0 && !e->ref)
2007 return strlen;
2009 elements = 1;
2010 if (!e->ref)
2012 if (!e->shape)
2013 return 0;
2014 for (i = 0; i < e->rank; i++)
2015 elements *= mpz_get_si (e->shape[i]);
2016 return elements*strlen;
2019 for (ref = e->ref; ref; ref = ref->next)
2021 if (ref->type == REF_SUBSTRING && ref->u.ss.start
2022 && ref->u.ss.start->expr_type == EXPR_CONSTANT)
2024 if (is_str_storage)
2026 /* The string length is the substring length.
2027 Set now to full string length. */
2028 if (!ref->u.ss.length || !ref->u.ss.length->length
2029 || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
2030 return 0;
2032 strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
2034 substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
2035 continue;
2038 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION
2039 && ref->u.ar.start && ref->u.ar.end && ref->u.ar.stride
2040 && ref->u.ar.as->upper)
2041 for (i = 0; i < ref->u.ar.dimen; i++)
2043 long int start, end, stride;
2044 stride = 1;
2046 if (ref->u.ar.stride[i])
2048 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT)
2049 stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
2050 else
2051 return 0;
2054 if (ref->u.ar.start[i])
2056 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT)
2057 start = mpz_get_si (ref->u.ar.start[i]->value.integer);
2058 else
2059 return 0;
2061 else if (ref->u.ar.as->lower[i]
2062 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT)
2063 start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
2064 else
2065 return 0;
2067 if (ref->u.ar.end[i])
2069 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT)
2070 end = mpz_get_si (ref->u.ar.end[i]->value.integer);
2071 else
2072 return 0;
2074 else if (ref->u.ar.as->upper[i]
2075 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
2076 end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
2077 else
2078 return 0;
2080 elements *= (end - start)/stride + 1L;
2082 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL
2083 && ref->u.ar.as->lower && ref->u.ar.as->upper)
2084 for (i = 0; i < ref->u.ar.as->rank; i++)
2086 if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
2087 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
2088 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
2089 elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2090 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2091 + 1L;
2092 else
2093 return 0;
2095 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2096 && e->expr_type == EXPR_VARIABLE)
2098 if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
2099 || e->symtree->n.sym->attr.pointer)
2101 elements = 1;
2102 continue;
2105 /* Determine the number of remaining elements in the element
2106 sequence for array element designators. */
2107 is_str_storage = true;
2108 for (i = ref->u.ar.dimen - 1; i >= 0; i--)
2110 if (ref->u.ar.start[i] == NULL
2111 || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
2112 || ref->u.ar.as->upper[i] == NULL
2113 || ref->u.ar.as->lower[i] == NULL
2114 || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
2115 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT)
2116 return 0;
2118 elements
2119 = elements
2120 * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2121 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2122 + 1L)
2123 - (mpz_get_si (ref->u.ar.start[i]->value.integer)
2124 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
2129 if (substrlen)
2130 return (is_str_storage) ? substrlen + (elements-1)*strlen
2131 : elements*strlen;
2132 else
2133 return elements*strlen;
2137 /* Given an expression, check whether it is an array section
2138 which has a vector subscript. If it has, one is returned,
2139 otherwise zero. */
2142 gfc_has_vector_subscript (gfc_expr *e)
2144 int i;
2145 gfc_ref *ref;
2147 if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
2148 return 0;
2150 for (ref = e->ref; ref; ref = ref->next)
2151 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2152 for (i = 0; i < ref->u.ar.dimen; i++)
2153 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
2154 return 1;
2156 return 0;
2160 /* Given formal and actual argument lists, see if they are compatible.
2161 If they are compatible, the actual argument list is sorted to
2162 correspond with the formal list, and elements for missing optional
2163 arguments are inserted. If WHERE pointer is nonnull, then we issue
2164 errors when things don't match instead of just returning the status
2165 code. */
2167 static int
2168 compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
2169 int ranks_must_agree, int is_elemental, locus *where)
2171 gfc_actual_arglist **new_arg, *a, *actual, temp;
2172 gfc_formal_arglist *f;
2173 int i, n, na;
2174 unsigned long actual_size, formal_size;
2175 bool full_array = false;
2177 actual = *ap;
2179 if (actual == NULL && formal == NULL)
2180 return 1;
2182 n = 0;
2183 for (f = formal; f; f = f->next)
2184 n++;
2186 new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
2188 for (i = 0; i < n; i++)
2189 new_arg[i] = NULL;
2191 na = 0;
2192 f = formal;
2193 i = 0;
2195 for (a = actual; a; a = a->next, f = f->next)
2197 /* Look for keywords but ignore g77 extensions like %VAL. */
2198 if (a->name != NULL && a->name[0] != '%')
2200 i = 0;
2201 for (f = formal; f; f = f->next, i++)
2203 if (f->sym == NULL)
2204 continue;
2205 if (strcmp (f->sym->name, a->name) == 0)
2206 break;
2209 if (f == NULL)
2211 if (where)
2212 gfc_error ("Keyword argument '%s' at %L is not in "
2213 "the procedure", a->name, &a->expr->where);
2214 return 0;
2217 if (new_arg[i] != NULL)
2219 if (where)
2220 gfc_error ("Keyword argument '%s' at %L is already associated "
2221 "with another actual argument", a->name,
2222 &a->expr->where);
2223 return 0;
2227 if (f == NULL)
2229 if (where)
2230 gfc_error ("More actual than formal arguments in procedure "
2231 "call at %L", where);
2233 return 0;
2236 if (f->sym == NULL && a->expr == NULL)
2237 goto match;
2239 if (f->sym == NULL)
2241 if (where)
2242 gfc_error ("Missing alternate return spec in subroutine call "
2243 "at %L", where);
2244 return 0;
2247 if (a->expr == NULL)
2249 if (where)
2250 gfc_error ("Unexpected alternate return spec in subroutine "
2251 "call at %L", where);
2252 return 0;
2255 if (a->expr->expr_type == EXPR_NULL && !f->sym->attr.pointer
2256 && (f->sym->attr.allocatable || !f->sym->attr.optional
2257 || (gfc_option.allow_std & GFC_STD_F2008) == 0))
2259 if (where && (f->sym->attr.allocatable || !f->sym->attr.optional))
2260 gfc_error ("Unexpected NULL() intrinsic at %L to dummy '%s'",
2261 where, f->sym->name);
2262 else if (where)
2263 gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
2264 "dummy '%s'", where, f->sym->name);
2266 return 0;
2269 if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
2270 is_elemental, where))
2271 return 0;
2273 /* Special case for character arguments. For allocatable, pointer
2274 and assumed-shape dummies, the string length needs to match
2275 exactly. */
2276 if (a->expr->ts.type == BT_CHARACTER
2277 && a->expr->ts.u.cl && a->expr->ts.u.cl->length
2278 && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
2279 && f->sym->ts.u.cl && f->sym->ts.u.cl && f->sym->ts.u.cl->length
2280 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2281 && (f->sym->attr.pointer || f->sym->attr.allocatable
2282 || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2283 && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
2284 f->sym->ts.u.cl->length->value.integer) != 0))
2286 if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
2287 gfc_warning ("Character length mismatch (%ld/%ld) between actual "
2288 "argument and pointer or allocatable dummy argument "
2289 "'%s' at %L",
2290 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
2291 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
2292 f->sym->name, &a->expr->where);
2293 else if (where)
2294 gfc_warning ("Character length mismatch (%ld/%ld) between actual "
2295 "argument and assumed-shape dummy argument '%s' "
2296 "at %L",
2297 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
2298 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
2299 f->sym->name, &a->expr->where);
2300 return 0;
2303 if ((f->sym->attr.pointer || f->sym->attr.allocatable)
2304 && f->sym->ts.deferred != a->expr->ts.deferred
2305 && a->expr->ts.type == BT_CHARACTER)
2307 if (where)
2308 gfc_error ("Actual argument argument at %L to allocatable or "
2309 "pointer dummy argument '%s' must have a deferred "
2310 "length type parameter if and only if the dummy has one",
2311 &a->expr->where, f->sym->name);
2312 return 0;
2315 if (f->sym->ts.type == BT_CLASS)
2316 goto skip_size_check;
2318 actual_size = get_expr_storage_size (a->expr);
2319 formal_size = get_sym_storage_size (f->sym);
2320 if (actual_size != 0 && actual_size < formal_size
2321 && a->expr->ts.type != BT_PROCEDURE
2322 && f->sym->attr.flavor != FL_PROCEDURE)
2324 if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
2325 gfc_warning ("Character length of actual argument shorter "
2326 "than of dummy argument '%s' (%lu/%lu) at %L",
2327 f->sym->name, actual_size, formal_size,
2328 &a->expr->where);
2329 else if (where)
2330 gfc_warning ("Actual argument contains too few "
2331 "elements for dummy argument '%s' (%lu/%lu) at %L",
2332 f->sym->name, actual_size, formal_size,
2333 &a->expr->where);
2334 return 0;
2337 skip_size_check:
2339 /* Satisfy 12.4.1.3 by ensuring that a procedure pointer actual argument
2340 is provided for a procedure pointer formal argument. */
2341 if (f->sym->attr.proc_pointer
2342 && !((a->expr->expr_type == EXPR_VARIABLE
2343 && a->expr->symtree->n.sym->attr.proc_pointer)
2344 || (a->expr->expr_type == EXPR_FUNCTION
2345 && a->expr->symtree->n.sym->result->attr.proc_pointer)
2346 || gfc_is_proc_ptr_comp (a->expr, NULL)))
2348 if (where)
2349 gfc_error ("Expected a procedure pointer for argument '%s' at %L",
2350 f->sym->name, &a->expr->where);
2351 return 0;
2354 /* Satisfy 12.4.1.2 by ensuring that a procedure actual argument is
2355 provided for a procedure formal argument. */
2356 if (a->expr->ts.type != BT_PROCEDURE && !gfc_is_proc_ptr_comp (a->expr, NULL)
2357 && a->expr->expr_type == EXPR_VARIABLE
2358 && f->sym->attr.flavor == FL_PROCEDURE)
2360 if (where)
2361 gfc_error ("Expected a procedure for argument '%s' at %L",
2362 f->sym->name, &a->expr->where);
2363 return 0;
2366 if (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE
2367 && a->expr->expr_type == EXPR_VARIABLE
2368 && a->expr->symtree->n.sym->as
2369 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
2370 && (a->expr->ref == NULL
2371 || (a->expr->ref->type == REF_ARRAY
2372 && a->expr->ref->u.ar.type == AR_FULL)))
2374 if (where)
2375 gfc_error ("Actual argument for '%s' cannot be an assumed-size"
2376 " array at %L", f->sym->name, where);
2377 return 0;
2380 if (a->expr->expr_type != EXPR_NULL
2381 && compare_pointer (f->sym, a->expr) == 0)
2383 if (where)
2384 gfc_error ("Actual argument for '%s' must be a pointer at %L",
2385 f->sym->name, &a->expr->where);
2386 return 0;
2389 if (a->expr->expr_type != EXPR_NULL
2390 && (gfc_option.allow_std & GFC_STD_F2008) == 0
2391 && compare_pointer (f->sym, a->expr) == 2)
2393 if (where)
2394 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
2395 "pointer dummy '%s'", &a->expr->where,f->sym->name);
2396 return 0;
2400 /* Fortran 2008, C1242. */
2401 if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
2403 if (where)
2404 gfc_error ("Coindexed actual argument at %L to pointer "
2405 "dummy '%s'",
2406 &a->expr->where, f->sym->name);
2407 return 0;
2410 /* Fortran 2008, 12.5.2.5 (no constraint). */
2411 if (a->expr->expr_type == EXPR_VARIABLE
2412 && f->sym->attr.intent != INTENT_IN
2413 && f->sym->attr.allocatable
2414 && gfc_is_coindexed (a->expr))
2416 if (where)
2417 gfc_error ("Coindexed actual argument at %L to allocatable "
2418 "dummy '%s' requires INTENT(IN)",
2419 &a->expr->where, f->sym->name);
2420 return 0;
2423 /* Fortran 2008, C1237. */
2424 if (a->expr->expr_type == EXPR_VARIABLE
2425 && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
2426 && gfc_is_coindexed (a->expr)
2427 && (a->expr->symtree->n.sym->attr.volatile_
2428 || a->expr->symtree->n.sym->attr.asynchronous))
2430 if (where)
2431 gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
2432 "at %L requires that dummy %s' has neither "
2433 "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
2434 f->sym->name);
2435 return 0;
2438 /* Fortran 2008, 12.5.2.4 (no constraint). */
2439 if (a->expr->expr_type == EXPR_VARIABLE
2440 && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
2441 && gfc_is_coindexed (a->expr)
2442 && gfc_has_ultimate_allocatable (a->expr))
2444 if (where)
2445 gfc_error ("Coindexed actual argument at %L with allocatable "
2446 "ultimate component to dummy '%s' requires either VALUE "
2447 "or INTENT(IN)", &a->expr->where, f->sym->name);
2448 return 0;
2451 if (f->sym->ts.type == BT_CLASS
2452 && CLASS_DATA (f->sym)->attr.allocatable
2453 && gfc_is_class_array_ref (a->expr, &full_array)
2454 && !full_array)
2456 if (where)
2457 gfc_error ("Actual CLASS array argument for '%s' must be a full "
2458 "array at %L", f->sym->name, &a->expr->where);
2459 return 0;
2463 if (a->expr->expr_type != EXPR_NULL
2464 && compare_allocatable (f->sym, a->expr) == 0)
2466 if (where)
2467 gfc_error ("Actual argument for '%s' must be ALLOCATABLE at %L",
2468 f->sym->name, &a->expr->where);
2469 return 0;
2472 /* Check intent = OUT/INOUT for definable actual argument. */
2473 if ((f->sym->attr.intent == INTENT_OUT
2474 || f->sym->attr.intent == INTENT_INOUT))
2476 const char* context = (where
2477 ? _("actual argument to INTENT = OUT/INOUT")
2478 : NULL);
2480 if (f->sym->attr.pointer
2481 && gfc_check_vardef_context (a->expr, true, false, context)
2482 == FAILURE)
2483 return 0;
2484 if (gfc_check_vardef_context (a->expr, false, false, context)
2485 == FAILURE)
2486 return 0;
2489 if ((f->sym->attr.intent == INTENT_OUT
2490 || f->sym->attr.intent == INTENT_INOUT
2491 || f->sym->attr.volatile_
2492 || f->sym->attr.asynchronous)
2493 && gfc_has_vector_subscript (a->expr))
2495 if (where)
2496 gfc_error ("Array-section actual argument with vector "
2497 "subscripts at %L is incompatible with INTENT(OUT), "
2498 "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
2499 "of the dummy argument '%s'",
2500 &a->expr->where, f->sym->name);
2501 return 0;
2504 /* C1232 (R1221) For an actual argument which is an array section or
2505 an assumed-shape array, the dummy argument shall be an assumed-
2506 shape array, if the dummy argument has the VOLATILE attribute. */
2508 if (f->sym->attr.volatile_
2509 && a->expr->symtree->n.sym->as
2510 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
2511 && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2513 if (where)
2514 gfc_error ("Assumed-shape actual argument at %L is "
2515 "incompatible with the non-assumed-shape "
2516 "dummy argument '%s' due to VOLATILE attribute",
2517 &a->expr->where,f->sym->name);
2518 return 0;
2521 if (f->sym->attr.volatile_
2522 && a->expr->ref && a->expr->ref->u.ar.type == AR_SECTION
2523 && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2525 if (where)
2526 gfc_error ("Array-section actual argument at %L is "
2527 "incompatible with the non-assumed-shape "
2528 "dummy argument '%s' due to VOLATILE attribute",
2529 &a->expr->where,f->sym->name);
2530 return 0;
2533 /* C1233 (R1221) For an actual argument which is a pointer array, the
2534 dummy argument shall be an assumed-shape or pointer array, if the
2535 dummy argument has the VOLATILE attribute. */
2537 if (f->sym->attr.volatile_
2538 && a->expr->symtree->n.sym->attr.pointer
2539 && a->expr->symtree->n.sym->as
2540 && !(f->sym->as
2541 && (f->sym->as->type == AS_ASSUMED_SHAPE
2542 || f->sym->attr.pointer)))
2544 if (where)
2545 gfc_error ("Pointer-array actual argument at %L requires "
2546 "an assumed-shape or pointer-array dummy "
2547 "argument '%s' due to VOLATILE attribute",
2548 &a->expr->where,f->sym->name);
2549 return 0;
2552 match:
2553 if (a == actual)
2554 na = i;
2556 new_arg[i++] = a;
2559 /* Make sure missing actual arguments are optional. */
2560 i = 0;
2561 for (f = formal; f; f = f->next, i++)
2563 if (new_arg[i] != NULL)
2564 continue;
2565 if (f->sym == NULL)
2567 if (where)
2568 gfc_error ("Missing alternate return spec in subroutine call "
2569 "at %L", where);
2570 return 0;
2572 if (!f->sym->attr.optional)
2574 if (where)
2575 gfc_error ("Missing actual argument for argument '%s' at %L",
2576 f->sym->name, where);
2577 return 0;
2581 /* The argument lists are compatible. We now relink a new actual
2582 argument list with null arguments in the right places. The head
2583 of the list remains the head. */
2584 for (i = 0; i < n; i++)
2585 if (new_arg[i] == NULL)
2586 new_arg[i] = gfc_get_actual_arglist ();
2588 if (na != 0)
2590 temp = *new_arg[0];
2591 *new_arg[0] = *actual;
2592 *actual = temp;
2594 a = new_arg[0];
2595 new_arg[0] = new_arg[na];
2596 new_arg[na] = a;
2599 for (i = 0; i < n - 1; i++)
2600 new_arg[i]->next = new_arg[i + 1];
2602 new_arg[i]->next = NULL;
2604 if (*ap == NULL && n > 0)
2605 *ap = new_arg[0];
2607 /* Note the types of omitted optional arguments. */
2608 for (a = *ap, f = formal; a; a = a->next, f = f->next)
2609 if (a->expr == NULL && a->label == NULL)
2610 a->missing_arg_type = f->sym->ts.type;
2612 return 1;
2616 typedef struct
2618 gfc_formal_arglist *f;
2619 gfc_actual_arglist *a;
2621 argpair;
2623 /* qsort comparison function for argument pairs, with the following
2624 order:
2625 - p->a->expr == NULL
2626 - p->a->expr->expr_type != EXPR_VARIABLE
2627 - growing p->a->expr->symbol. */
2629 static int
2630 pair_cmp (const void *p1, const void *p2)
2632 const gfc_actual_arglist *a1, *a2;
2634 /* *p1 and *p2 are elements of the to-be-sorted array. */
2635 a1 = ((const argpair *) p1)->a;
2636 a2 = ((const argpair *) p2)->a;
2637 if (!a1->expr)
2639 if (!a2->expr)
2640 return 0;
2641 return -1;
2643 if (!a2->expr)
2644 return 1;
2645 if (a1->expr->expr_type != EXPR_VARIABLE)
2647 if (a2->expr->expr_type != EXPR_VARIABLE)
2648 return 0;
2649 return -1;
2651 if (a2->expr->expr_type != EXPR_VARIABLE)
2652 return 1;
2653 return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
2657 /* Given two expressions from some actual arguments, test whether they
2658 refer to the same expression. The analysis is conservative.
2659 Returning FAILURE will produce no warning. */
2661 static gfc_try
2662 compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
2664 const gfc_ref *r1, *r2;
2666 if (!e1 || !e2
2667 || e1->expr_type != EXPR_VARIABLE
2668 || e2->expr_type != EXPR_VARIABLE
2669 || e1->symtree->n.sym != e2->symtree->n.sym)
2670 return FAILURE;
2672 /* TODO: improve comparison, see expr.c:show_ref(). */
2673 for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
2675 if (r1->type != r2->type)
2676 return FAILURE;
2677 switch (r1->type)
2679 case REF_ARRAY:
2680 if (r1->u.ar.type != r2->u.ar.type)
2681 return FAILURE;
2682 /* TODO: At the moment, consider only full arrays;
2683 we could do better. */
2684 if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
2685 return FAILURE;
2686 break;
2688 case REF_COMPONENT:
2689 if (r1->u.c.component != r2->u.c.component)
2690 return FAILURE;
2691 break;
2693 case REF_SUBSTRING:
2694 return FAILURE;
2696 default:
2697 gfc_internal_error ("compare_actual_expr(): Bad component code");
2700 if (!r1 && !r2)
2701 return SUCCESS;
2702 return FAILURE;
2706 /* Given formal and actual argument lists that correspond to one
2707 another, check that identical actual arguments aren't not
2708 associated with some incompatible INTENTs. */
2710 static gfc_try
2711 check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
2713 sym_intent f1_intent, f2_intent;
2714 gfc_formal_arglist *f1;
2715 gfc_actual_arglist *a1;
2716 size_t n, i, j;
2717 argpair *p;
2718 gfc_try t = SUCCESS;
2720 n = 0;
2721 for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
2723 if (f1 == NULL && a1 == NULL)
2724 break;
2725 if (f1 == NULL || a1 == NULL)
2726 gfc_internal_error ("check_some_aliasing(): List mismatch");
2727 n++;
2729 if (n == 0)
2730 return t;
2731 p = XALLOCAVEC (argpair, n);
2733 for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
2735 p[i].f = f1;
2736 p[i].a = a1;
2739 qsort (p, n, sizeof (argpair), pair_cmp);
2741 for (i = 0; i < n; i++)
2743 if (!p[i].a->expr
2744 || p[i].a->expr->expr_type != EXPR_VARIABLE
2745 || p[i].a->expr->ts.type == BT_PROCEDURE)
2746 continue;
2747 f1_intent = p[i].f->sym->attr.intent;
2748 for (j = i + 1; j < n; j++)
2750 /* Expected order after the sort. */
2751 if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
2752 gfc_internal_error ("check_some_aliasing(): corrupted data");
2754 /* Are the expression the same? */
2755 if (compare_actual_expr (p[i].a->expr, p[j].a->expr) == FAILURE)
2756 break;
2757 f2_intent = p[j].f->sym->attr.intent;
2758 if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
2759 || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN))
2761 gfc_warning ("Same actual argument associated with INTENT(%s) "
2762 "argument '%s' and INTENT(%s) argument '%s' at %L",
2763 gfc_intent_string (f1_intent), p[i].f->sym->name,
2764 gfc_intent_string (f2_intent), p[j].f->sym->name,
2765 &p[i].a->expr->where);
2766 t = FAILURE;
2771 return t;
2775 /* Given a symbol of a formal argument list and an expression,
2776 return nonzero if their intents are compatible, zero otherwise. */
2778 static int
2779 compare_parameter_intent (gfc_symbol *formal, gfc_expr *actual)
2781 if (actual->symtree->n.sym->attr.pointer && !formal->attr.pointer)
2782 return 1;
2784 if (actual->symtree->n.sym->attr.intent != INTENT_IN)
2785 return 1;
2787 if (formal->attr.intent == INTENT_INOUT || formal->attr.intent == INTENT_OUT)
2788 return 0;
2790 return 1;
2794 /* Given formal and actual argument lists that correspond to one
2795 another, check that they are compatible in the sense that intents
2796 are not mismatched. */
2798 static gfc_try
2799 check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
2801 sym_intent f_intent;
2803 for (;; f = f->next, a = a->next)
2805 if (f == NULL && a == NULL)
2806 break;
2807 if (f == NULL || a == NULL)
2808 gfc_internal_error ("check_intents(): List mismatch");
2810 if (a->expr == NULL || a->expr->expr_type != EXPR_VARIABLE)
2811 continue;
2813 f_intent = f->sym->attr.intent;
2815 if (!compare_parameter_intent(f->sym, a->expr))
2817 gfc_error ("Procedure argument at %L is INTENT(IN) while interface "
2818 "specifies INTENT(%s)", &a->expr->where,
2819 gfc_intent_string (f_intent));
2820 return FAILURE;
2823 if (gfc_pure (NULL) && gfc_impure_variable (a->expr->symtree->n.sym))
2825 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
2827 gfc_error ("Procedure argument at %L is local to a PURE "
2828 "procedure and is passed to an INTENT(%s) argument",
2829 &a->expr->where, gfc_intent_string (f_intent));
2830 return FAILURE;
2833 if (f->sym->attr.pointer)
2835 gfc_error ("Procedure argument at %L is local to a PURE "
2836 "procedure and has the POINTER attribute",
2837 &a->expr->where);
2838 return FAILURE;
2842 /* Fortran 2008, C1283. */
2843 if (gfc_pure (NULL) && gfc_is_coindexed (a->expr))
2845 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
2847 gfc_error ("Coindexed actual argument at %L in PURE procedure "
2848 "is passed to an INTENT(%s) argument",
2849 &a->expr->where, gfc_intent_string (f_intent));
2850 return FAILURE;
2853 if (f->sym->attr.pointer)
2855 gfc_error ("Coindexed actual argument at %L in PURE procedure "
2856 "is passed to a POINTER dummy argument",
2857 &a->expr->where);
2858 return FAILURE;
2862 /* F2008, Section 12.5.2.4. */
2863 if (a->expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
2864 && gfc_is_coindexed (a->expr))
2866 gfc_error ("Coindexed polymorphic actual argument at %L is passed "
2867 "polymorphic dummy argument '%s'",
2868 &a->expr->where, f->sym->name);
2869 return FAILURE;
2873 return SUCCESS;
2877 /* Check how a procedure is used against its interface. If all goes
2878 well, the actual argument list will also end up being properly
2879 sorted. */
2881 void
2882 gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
2885 /* Warn about calls with an implicit interface. Special case
2886 for calling a ISO_C_BINDING becase c_loc and c_funloc
2887 are pseudo-unknown. Additionally, warn about procedures not
2888 explicitly declared at all if requested. */
2889 if (sym->attr.if_source == IFSRC_UNKNOWN && ! sym->attr.is_iso_c)
2891 if (gfc_option.warn_implicit_interface)
2892 gfc_warning ("Procedure '%s' called with an implicit interface at %L",
2893 sym->name, where);
2894 else if (gfc_option.warn_implicit_procedure
2895 && sym->attr.proc == PROC_UNKNOWN)
2896 gfc_warning ("Procedure '%s' called at %L is not explicitly declared",
2897 sym->name, where);
2900 if (sym->attr.if_source == IFSRC_UNKNOWN)
2902 gfc_actual_arglist *a;
2904 if (sym->attr.pointer)
2906 gfc_error("The pointer object '%s' at %L must have an explicit "
2907 "function interface or be declared as array",
2908 sym->name, where);
2909 return;
2912 if (sym->attr.allocatable && !sym->attr.external)
2914 gfc_error("The allocatable object '%s' at %L must have an explicit "
2915 "function interface or be declared as array",
2916 sym->name, where);
2917 return;
2920 if (sym->attr.allocatable)
2922 gfc_error("Allocatable function '%s' at %L must have an explicit "
2923 "function interface", sym->name, where);
2924 return;
2927 for (a = *ap; a; a = a->next)
2929 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
2930 if (a->name != NULL && a->name[0] != '%')
2932 gfc_error("Keyword argument requires explicit interface "
2933 "for procedure '%s' at %L", sym->name, &a->expr->where);
2934 break;
2937 /* F2008, C1303 and C1304. */
2938 if (a->expr
2939 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
2940 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2941 && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2942 || gfc_expr_attr (a->expr).lock_comp))
2944 gfc_error("Actual argument of LOCK_TYPE or with LOCK_TYPE "
2945 "component at %L requires an explicit interface for "
2946 "procedure '%s'", &a->expr->where, sym->name);
2947 break;
2950 if (a->expr && a->expr->expr_type == EXPR_NULL
2951 && a->expr->ts.type == BT_UNKNOWN)
2953 gfc_error ("MOLD argument to NULL required at %L", &a->expr->where);
2954 return;
2958 return;
2961 if (!compare_actual_formal (ap, sym->formal, 0, sym->attr.elemental, where))
2962 return;
2964 check_intents (sym->formal, *ap);
2965 if (gfc_option.warn_aliasing)
2966 check_some_aliasing (sym->formal, *ap);
2970 /* Check how a procedure pointer component is used against its interface.
2971 If all goes well, the actual argument list will also end up being properly
2972 sorted. Completely analogous to gfc_procedure_use. */
2974 void
2975 gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
2978 /* Warn about calls with an implicit interface. Special case
2979 for calling a ISO_C_BINDING becase c_loc and c_funloc
2980 are pseudo-unknown. */
2981 if (gfc_option.warn_implicit_interface
2982 && comp->attr.if_source == IFSRC_UNKNOWN
2983 && !comp->attr.is_iso_c)
2984 gfc_warning ("Procedure pointer component '%s' called with an implicit "
2985 "interface at %L", comp->name, where);
2987 if (comp->attr.if_source == IFSRC_UNKNOWN)
2989 gfc_actual_arglist *a;
2990 for (a = *ap; a; a = a->next)
2992 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
2993 if (a->name != NULL && a->name[0] != '%')
2995 gfc_error("Keyword argument requires explicit interface "
2996 "for procedure pointer component '%s' at %L",
2997 comp->name, &a->expr->where);
2998 break;
3002 return;
3005 if (!compare_actual_formal (ap, comp->formal, 0, comp->attr.elemental, where))
3006 return;
3008 check_intents (comp->formal, *ap);
3009 if (gfc_option.warn_aliasing)
3010 check_some_aliasing (comp->formal, *ap);
3014 /* Try if an actual argument list matches the formal list of a symbol,
3015 respecting the symbol's attributes like ELEMENTAL. This is used for
3016 GENERIC resolution. */
3018 bool
3019 gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
3021 bool r;
3023 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
3025 r = !sym->attr.elemental;
3026 if (compare_actual_formal (args, sym->formal, r, !r, NULL))
3028 check_intents (sym->formal, *args);
3029 if (gfc_option.warn_aliasing)
3030 check_some_aliasing (sym->formal, *args);
3031 return true;
3034 return false;
3038 /* Given an interface pointer and an actual argument list, search for
3039 a formal argument list that matches the actual. If found, returns
3040 a pointer to the symbol of the correct interface. Returns NULL if
3041 not found. */
3043 gfc_symbol *
3044 gfc_search_interface (gfc_interface *intr, int sub_flag,
3045 gfc_actual_arglist **ap)
3047 gfc_symbol *elem_sym = NULL;
3048 gfc_symbol *null_sym = NULL;
3049 locus null_expr_loc;
3050 gfc_actual_arglist *a;
3051 bool has_null_arg = false;
3053 for (a = *ap; a; a = a->next)
3054 if (a->expr && a->expr->expr_type == EXPR_NULL
3055 && a->expr->ts.type == BT_UNKNOWN)
3057 has_null_arg = true;
3058 null_expr_loc = a->expr->where;
3059 break;
3062 for (; intr; intr = intr->next)
3064 if (intr->sym->attr.flavor == FL_DERIVED)
3065 continue;
3066 if (sub_flag && intr->sym->attr.function)
3067 continue;
3068 if (!sub_flag && intr->sym->attr.subroutine)
3069 continue;
3071 if (gfc_arglist_matches_symbol (ap, intr->sym))
3073 if (has_null_arg && null_sym)
3075 gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
3076 "between specific functions %s and %s",
3077 &null_expr_loc, null_sym->name, intr->sym->name);
3078 return NULL;
3080 else if (has_null_arg)
3082 null_sym = intr->sym;
3083 continue;
3086 /* Satisfy 12.4.4.1 such that an elemental match has lower
3087 weight than a non-elemental match. */
3088 if (intr->sym->attr.elemental)
3090 elem_sym = intr->sym;
3091 continue;
3093 return intr->sym;
3097 if (null_sym)
3098 return null_sym;
3100 return elem_sym ? elem_sym : NULL;
3104 /* Do a brute force recursive search for a symbol. */
3106 static gfc_symtree *
3107 find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
3109 gfc_symtree * st;
3111 if (root->n.sym == sym)
3112 return root;
3114 st = NULL;
3115 if (root->left)
3116 st = find_symtree0 (root->left, sym);
3117 if (root->right && ! st)
3118 st = find_symtree0 (root->right, sym);
3119 return st;
3123 /* Find a symtree for a symbol. */
3125 gfc_symtree *
3126 gfc_find_sym_in_symtree (gfc_symbol *sym)
3128 gfc_symtree *st;
3129 gfc_namespace *ns;
3131 /* First try to find it by name. */
3132 gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
3133 if (st && st->n.sym == sym)
3134 return st;
3136 /* If it's been renamed, resort to a brute-force search. */
3137 /* TODO: avoid having to do this search. If the symbol doesn't exist
3138 in the symtree for the current namespace, it should probably be added. */
3139 for (ns = gfc_current_ns; ns; ns = ns->parent)
3141 st = find_symtree0 (ns->sym_root, sym);
3142 if (st)
3143 return st;
3145 gfc_internal_error ("Unable to find symbol %s", sym->name);
3146 /* Not reached. */
3150 /* See if the arglist to an operator-call contains a derived-type argument
3151 with a matching type-bound operator. If so, return the matching specific
3152 procedure defined as operator-target as well as the base-object to use
3153 (which is the found derived-type argument with operator). The generic
3154 name, if any, is transmitted to the final expression via 'gname'. */
3156 static gfc_typebound_proc*
3157 matching_typebound_op (gfc_expr** tb_base,
3158 gfc_actual_arglist* args,
3159 gfc_intrinsic_op op, const char* uop,
3160 const char ** gname)
3162 gfc_actual_arglist* base;
3164 for (base = args; base; base = base->next)
3165 if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
3167 gfc_typebound_proc* tb;
3168 gfc_symbol* derived;
3169 gfc_try result;
3171 if (base->expr->ts.type == BT_CLASS)
3173 if (!gfc_expr_attr (base->expr).class_ok)
3174 continue;
3175 derived = CLASS_DATA (base->expr)->ts.u.derived;
3177 else
3178 derived = base->expr->ts.u.derived;
3180 if (op == INTRINSIC_USER)
3182 gfc_symtree* tb_uop;
3184 gcc_assert (uop);
3185 tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
3186 false, NULL);
3188 if (tb_uop)
3189 tb = tb_uop->n.tb;
3190 else
3191 tb = NULL;
3193 else
3194 tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
3195 false, NULL);
3197 /* This means we hit a PRIVATE operator which is use-associated and
3198 should thus not be seen. */
3199 if (result == FAILURE)
3200 tb = NULL;
3202 /* Look through the super-type hierarchy for a matching specific
3203 binding. */
3204 for (; tb; tb = tb->overridden)
3206 gfc_tbp_generic* g;
3208 gcc_assert (tb->is_generic);
3209 for (g = tb->u.generic; g; g = g->next)
3211 gfc_symbol* target;
3212 gfc_actual_arglist* argcopy;
3213 bool matches;
3215 gcc_assert (g->specific);
3216 if (g->specific->error)
3217 continue;
3219 target = g->specific->u.specific->n.sym;
3221 /* Check if this arglist matches the formal. */
3222 argcopy = gfc_copy_actual_arglist (args);
3223 matches = gfc_arglist_matches_symbol (&argcopy, target);
3224 gfc_free_actual_arglist (argcopy);
3226 /* Return if we found a match. */
3227 if (matches)
3229 *tb_base = base->expr;
3230 *gname = g->specific_st->name;
3231 return g->specific;
3237 return NULL;
3241 /* For the 'actual arglist' of an operator call and a specific typebound
3242 procedure that has been found the target of a type-bound operator, build the
3243 appropriate EXPR_COMPCALL and resolve it. We take this indirection over
3244 type-bound procedures rather than resolving type-bound operators 'directly'
3245 so that we can reuse the existing logic. */
3247 static void
3248 build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
3249 gfc_expr* base, gfc_typebound_proc* target,
3250 const char *gname)
3252 e->expr_type = EXPR_COMPCALL;
3253 e->value.compcall.tbp = target;
3254 e->value.compcall.name = gname ? gname : "$op";
3255 e->value.compcall.actual = actual;
3256 e->value.compcall.base_object = base;
3257 e->value.compcall.ignore_pass = 1;
3258 e->value.compcall.assign = 0;
3259 if (e->ts.type == BT_UNKNOWN
3260 && target->function)
3262 if (target->is_generic)
3263 e->ts = target->u.generic->specific->u.specific->n.sym->ts;
3264 else
3265 e->ts = target->u.specific->n.sym->ts;
3270 /* This subroutine is called when an expression is being resolved.
3271 The expression node in question is either a user defined operator
3272 or an intrinsic operator with arguments that aren't compatible
3273 with the operator. This subroutine builds an actual argument list
3274 corresponding to the operands, then searches for a compatible
3275 interface. If one is found, the expression node is replaced with
3276 the appropriate function call. We use the 'match' enum to specify
3277 whether a replacement has been made or not, or if an error occurred. */
3279 match
3280 gfc_extend_expr (gfc_expr *e)
3282 gfc_actual_arglist *actual;
3283 gfc_symbol *sym;
3284 gfc_namespace *ns;
3285 gfc_user_op *uop;
3286 gfc_intrinsic_op i;
3287 const char *gname;
3289 sym = NULL;
3291 actual = gfc_get_actual_arglist ();
3292 actual->expr = e->value.op.op1;
3294 gname = NULL;
3296 if (e->value.op.op2 != NULL)
3298 actual->next = gfc_get_actual_arglist ();
3299 actual->next->expr = e->value.op.op2;
3302 i = fold_unary_intrinsic (e->value.op.op);
3304 if (i == INTRINSIC_USER)
3306 for (ns = gfc_current_ns; ns; ns = ns->parent)
3308 uop = gfc_find_uop (e->value.op.uop->name, ns);
3309 if (uop == NULL)
3310 continue;
3312 sym = gfc_search_interface (uop->op, 0, &actual);
3313 if (sym != NULL)
3314 break;
3317 else
3319 for (ns = gfc_current_ns; ns; ns = ns->parent)
3321 /* Due to the distinction between '==' and '.eq.' and friends, one has
3322 to check if either is defined. */
3323 switch (i)
3325 #define CHECK_OS_COMPARISON(comp) \
3326 case INTRINSIC_##comp: \
3327 case INTRINSIC_##comp##_OS: \
3328 sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
3329 if (!sym) \
3330 sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
3331 break;
3332 CHECK_OS_COMPARISON(EQ)
3333 CHECK_OS_COMPARISON(NE)
3334 CHECK_OS_COMPARISON(GT)
3335 CHECK_OS_COMPARISON(GE)
3336 CHECK_OS_COMPARISON(LT)
3337 CHECK_OS_COMPARISON(LE)
3338 #undef CHECK_OS_COMPARISON
3340 default:
3341 sym = gfc_search_interface (ns->op[i], 0, &actual);
3344 if (sym != NULL)
3345 break;
3349 /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
3350 found rather than just taking the first one and not checking further. */
3352 if (sym == NULL)
3354 gfc_typebound_proc* tbo;
3355 gfc_expr* tb_base;
3357 /* See if we find a matching type-bound operator. */
3358 if (i == INTRINSIC_USER)
3359 tbo = matching_typebound_op (&tb_base, actual,
3360 i, e->value.op.uop->name, &gname);
3361 else
3362 switch (i)
3364 #define CHECK_OS_COMPARISON(comp) \
3365 case INTRINSIC_##comp: \
3366 case INTRINSIC_##comp##_OS: \
3367 tbo = matching_typebound_op (&tb_base, actual, \
3368 INTRINSIC_##comp, NULL, &gname); \
3369 if (!tbo) \
3370 tbo = matching_typebound_op (&tb_base, actual, \
3371 INTRINSIC_##comp##_OS, NULL, &gname); \
3372 break;
3373 CHECK_OS_COMPARISON(EQ)
3374 CHECK_OS_COMPARISON(NE)
3375 CHECK_OS_COMPARISON(GT)
3376 CHECK_OS_COMPARISON(GE)
3377 CHECK_OS_COMPARISON(LT)
3378 CHECK_OS_COMPARISON(LE)
3379 #undef CHECK_OS_COMPARISON
3381 default:
3382 tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
3383 break;
3386 /* If there is a matching typebound-operator, replace the expression with
3387 a call to it and succeed. */
3388 if (tbo)
3390 gfc_try result;
3392 gcc_assert (tb_base);
3393 build_compcall_for_operator (e, actual, tb_base, tbo, gname);
3395 result = gfc_resolve_expr (e);
3396 if (result == FAILURE)
3397 return MATCH_ERROR;
3399 return MATCH_YES;
3402 /* Don't use gfc_free_actual_arglist(). */
3403 free (actual->next);
3404 free (actual);
3406 return MATCH_NO;
3409 /* Change the expression node to a function call. */
3410 e->expr_type = EXPR_FUNCTION;
3411 e->symtree = gfc_find_sym_in_symtree (sym);
3412 e->value.function.actual = actual;
3413 e->value.function.esym = NULL;
3414 e->value.function.isym = NULL;
3415 e->value.function.name = NULL;
3416 e->user_operator = 1;
3418 if (gfc_resolve_expr (e) == FAILURE)
3419 return MATCH_ERROR;
3421 return MATCH_YES;
3425 /* Tries to replace an assignment code node with a subroutine call to
3426 the subroutine associated with the assignment operator. Return
3427 SUCCESS if the node was replaced. On FAILURE, no error is
3428 generated. */
3430 gfc_try
3431 gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
3433 gfc_actual_arglist *actual;
3434 gfc_expr *lhs, *rhs;
3435 gfc_symbol *sym;
3436 const char *gname;
3438 gname = NULL;
3440 lhs = c->expr1;
3441 rhs = c->expr2;
3443 /* Don't allow an intrinsic assignment to be replaced. */
3444 if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
3445 && (rhs->rank == 0 || rhs->rank == lhs->rank)
3446 && (lhs->ts.type == rhs->ts.type
3447 || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
3448 return FAILURE;
3450 actual = gfc_get_actual_arglist ();
3451 actual->expr = lhs;
3453 actual->next = gfc_get_actual_arglist ();
3454 actual->next->expr = rhs;
3456 sym = NULL;
3458 for (; ns; ns = ns->parent)
3460 sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
3461 if (sym != NULL)
3462 break;
3465 /* TODO: Ambiguity-check, see above for gfc_extend_expr. */
3467 if (sym == NULL)
3469 gfc_typebound_proc* tbo;
3470 gfc_expr* tb_base;
3472 /* See if we find a matching type-bound assignment. */
3473 tbo = matching_typebound_op (&tb_base, actual,
3474 INTRINSIC_ASSIGN, NULL, &gname);
3476 /* If there is one, replace the expression with a call to it and
3477 succeed. */
3478 if (tbo)
3480 gcc_assert (tb_base);
3481 c->expr1 = gfc_get_expr ();
3482 build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
3483 c->expr1->value.compcall.assign = 1;
3484 c->expr1->where = c->loc;
3485 c->expr2 = NULL;
3486 c->op = EXEC_COMPCALL;
3488 /* c is resolved from the caller, so no need to do it here. */
3490 return SUCCESS;
3493 free (actual->next);
3494 free (actual);
3495 return FAILURE;
3498 /* Replace the assignment with the call. */
3499 c->op = EXEC_ASSIGN_CALL;
3500 c->symtree = gfc_find_sym_in_symtree (sym);
3501 c->expr1 = NULL;
3502 c->expr2 = NULL;
3503 c->ext.actual = actual;
3505 return SUCCESS;
3509 /* Make sure that the interface just parsed is not already present in
3510 the given interface list. Ambiguity isn't checked yet since module
3511 procedures can be present without interfaces. */
3513 static gfc_try
3514 check_new_interface (gfc_interface *base, gfc_symbol *new_sym)
3516 gfc_interface *ip;
3518 for (ip = base; ip; ip = ip->next)
3520 if (ip->sym == new_sym)
3522 gfc_error ("Entity '%s' at %C is already present in the interface",
3523 new_sym->name);
3524 return FAILURE;
3528 return SUCCESS;
3532 /* Add a symbol to the current interface. */
3534 gfc_try
3535 gfc_add_interface (gfc_symbol *new_sym)
3537 gfc_interface **head, *intr;
3538 gfc_namespace *ns;
3539 gfc_symbol *sym;
3541 switch (current_interface.type)
3543 case INTERFACE_NAMELESS:
3544 case INTERFACE_ABSTRACT:
3545 return SUCCESS;
3547 case INTERFACE_INTRINSIC_OP:
3548 for (ns = current_interface.ns; ns; ns = ns->parent)
3549 switch (current_interface.op)
3551 case INTRINSIC_EQ:
3552 case INTRINSIC_EQ_OS:
3553 if (check_new_interface (ns->op[INTRINSIC_EQ], new_sym) == FAILURE ||
3554 check_new_interface (ns->op[INTRINSIC_EQ_OS], new_sym) == FAILURE)
3555 return FAILURE;
3556 break;
3558 case INTRINSIC_NE:
3559 case INTRINSIC_NE_OS:
3560 if (check_new_interface (ns->op[INTRINSIC_NE], new_sym) == FAILURE ||
3561 check_new_interface (ns->op[INTRINSIC_NE_OS], new_sym) == FAILURE)
3562 return FAILURE;
3563 break;
3565 case INTRINSIC_GT:
3566 case INTRINSIC_GT_OS:
3567 if (check_new_interface (ns->op[INTRINSIC_GT], new_sym) == FAILURE ||
3568 check_new_interface (ns->op[INTRINSIC_GT_OS], new_sym) == FAILURE)
3569 return FAILURE;
3570 break;
3572 case INTRINSIC_GE:
3573 case INTRINSIC_GE_OS:
3574 if (check_new_interface (ns->op[INTRINSIC_GE], new_sym) == FAILURE ||
3575 check_new_interface (ns->op[INTRINSIC_GE_OS], new_sym) == FAILURE)
3576 return FAILURE;
3577 break;
3579 case INTRINSIC_LT:
3580 case INTRINSIC_LT_OS:
3581 if (check_new_interface (ns->op[INTRINSIC_LT], new_sym) == FAILURE ||
3582 check_new_interface (ns->op[INTRINSIC_LT_OS], new_sym) == FAILURE)
3583 return FAILURE;
3584 break;
3586 case INTRINSIC_LE:
3587 case INTRINSIC_LE_OS:
3588 if (check_new_interface (ns->op[INTRINSIC_LE], new_sym) == FAILURE ||
3589 check_new_interface (ns->op[INTRINSIC_LE_OS], new_sym) == FAILURE)
3590 return FAILURE;
3591 break;
3593 default:
3594 if (check_new_interface (ns->op[current_interface.op], new_sym) == FAILURE)
3595 return FAILURE;
3598 head = &current_interface.ns->op[current_interface.op];
3599 break;
3601 case INTERFACE_GENERIC:
3602 for (ns = current_interface.ns; ns; ns = ns->parent)
3604 gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
3605 if (sym == NULL)
3606 continue;
3608 if (check_new_interface (sym->generic, new_sym) == FAILURE)
3609 return FAILURE;
3612 head = &current_interface.sym->generic;
3613 break;
3615 case INTERFACE_USER_OP:
3616 if (check_new_interface (current_interface.uop->op, new_sym)
3617 == FAILURE)
3618 return FAILURE;
3620 head = &current_interface.uop->op;
3621 break;
3623 default:
3624 gfc_internal_error ("gfc_add_interface(): Bad interface type");
3627 intr = gfc_get_interface ();
3628 intr->sym = new_sym;
3629 intr->where = gfc_current_locus;
3631 intr->next = *head;
3632 *head = intr;
3634 return SUCCESS;
3638 gfc_interface *
3639 gfc_current_interface_head (void)
3641 switch (current_interface.type)
3643 case INTERFACE_INTRINSIC_OP:
3644 return current_interface.ns->op[current_interface.op];
3645 break;
3647 case INTERFACE_GENERIC:
3648 return current_interface.sym->generic;
3649 break;
3651 case INTERFACE_USER_OP:
3652 return current_interface.uop->op;
3653 break;
3655 default:
3656 gcc_unreachable ();
3661 void
3662 gfc_set_current_interface_head (gfc_interface *i)
3664 switch (current_interface.type)
3666 case INTERFACE_INTRINSIC_OP:
3667 current_interface.ns->op[current_interface.op] = i;
3668 break;
3670 case INTERFACE_GENERIC:
3671 current_interface.sym->generic = i;
3672 break;
3674 case INTERFACE_USER_OP:
3675 current_interface.uop->op = i;
3676 break;
3678 default:
3679 gcc_unreachable ();
3684 /* Gets rid of a formal argument list. We do not free symbols.
3685 Symbols are freed when a namespace is freed. */
3687 void
3688 gfc_free_formal_arglist (gfc_formal_arglist *p)
3690 gfc_formal_arglist *q;
3692 for (; p; p = q)
3694 q = p->next;
3695 free (p);
3700 /* Check that it is ok for the type-bound procedure 'proc' to override the
3701 procedure 'old', cf. F08:4.5.7.3. */
3703 gfc_try
3704 gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
3706 locus where;
3707 const gfc_symbol *proc_target, *old_target;
3708 unsigned proc_pass_arg, old_pass_arg, argpos;
3709 gfc_formal_arglist *proc_formal, *old_formal;
3710 bool check_type;
3711 char err[200];
3713 /* This procedure should only be called for non-GENERIC proc. */
3714 gcc_assert (!proc->n.tb->is_generic);
3716 /* If the overwritten procedure is GENERIC, this is an error. */
3717 if (old->n.tb->is_generic)
3719 gfc_error ("Can't overwrite GENERIC '%s' at %L",
3720 old->name, &proc->n.tb->where);
3721 return FAILURE;
3724 where = proc->n.tb->where;
3725 proc_target = proc->n.tb->u.specific->n.sym;
3726 old_target = old->n.tb->u.specific->n.sym;
3728 /* Check that overridden binding is not NON_OVERRIDABLE. */
3729 if (old->n.tb->non_overridable)
3731 gfc_error ("'%s' at %L overrides a procedure binding declared"
3732 " NON_OVERRIDABLE", proc->name, &where);
3733 return FAILURE;
3736 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
3737 if (!old->n.tb->deferred && proc->n.tb->deferred)
3739 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
3740 " non-DEFERRED binding", proc->name, &where);
3741 return FAILURE;
3744 /* If the overridden binding is PURE, the overriding must be, too. */
3745 if (old_target->attr.pure && !proc_target->attr.pure)
3747 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
3748 proc->name, &where);
3749 return FAILURE;
3752 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
3753 is not, the overriding must not be either. */
3754 if (old_target->attr.elemental && !proc_target->attr.elemental)
3756 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
3757 " ELEMENTAL", proc->name, &where);
3758 return FAILURE;
3760 if (!old_target->attr.elemental && proc_target->attr.elemental)
3762 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
3763 " be ELEMENTAL, either", proc->name, &where);
3764 return FAILURE;
3767 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
3768 SUBROUTINE. */
3769 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
3771 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
3772 " SUBROUTINE", proc->name, &where);
3773 return FAILURE;
3776 /* If the overridden binding is a FUNCTION, the overriding must also be a
3777 FUNCTION and have the same characteristics. */
3778 if (old_target->attr.function)
3780 if (!proc_target->attr.function)
3782 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
3783 " FUNCTION", proc->name, &where);
3784 return FAILURE;
3787 /* FIXME: Do more comprehensive checking (including, for instance, the
3788 array-shape). */
3789 gcc_assert (proc_target->result && old_target->result);
3790 if (!compare_type_rank (proc_target->result, old_target->result))
3792 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
3793 " matching result types and ranks", proc->name, &where);
3794 return FAILURE;
3797 /* Check string length. */
3798 if (proc_target->result->ts.type == BT_CHARACTER
3799 && proc_target->result->ts.u.cl && old_target->result->ts.u.cl)
3801 int compval = gfc_dep_compare_expr (proc_target->result->ts.u.cl->length,
3802 old_target->result->ts.u.cl->length);
3803 switch (compval)
3805 case -1:
3806 case 1:
3807 case -3:
3808 gfc_error ("Character length mismatch between '%s' at '%L' and "
3809 "overridden FUNCTION", proc->name, &where);
3810 return FAILURE;
3812 case -2:
3813 gfc_warning ("Possible character length mismatch between '%s' at"
3814 " '%L' and overridden FUNCTION", proc->name, &where);
3815 break;
3817 case 0:
3818 break;
3820 default:
3821 gfc_internal_error ("gfc_check_typebound_override: Unexpected "
3822 "result %i of gfc_dep_compare_expr", compval);
3823 break;
3828 /* If the overridden binding is PUBLIC, the overriding one must not be
3829 PRIVATE. */
3830 if (old->n.tb->access == ACCESS_PUBLIC
3831 && proc->n.tb->access == ACCESS_PRIVATE)
3833 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
3834 " PRIVATE", proc->name, &where);
3835 return FAILURE;
3838 /* Compare the formal argument lists of both procedures. This is also abused
3839 to find the position of the passed-object dummy arguments of both
3840 bindings as at least the overridden one might not yet be resolved and we
3841 need those positions in the check below. */
3842 proc_pass_arg = old_pass_arg = 0;
3843 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
3844 proc_pass_arg = 1;
3845 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
3846 old_pass_arg = 1;
3847 argpos = 1;
3848 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
3849 proc_formal && old_formal;
3850 proc_formal = proc_formal->next, old_formal = old_formal->next)
3852 if (proc->n.tb->pass_arg
3853 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
3854 proc_pass_arg = argpos;
3855 if (old->n.tb->pass_arg
3856 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
3857 old_pass_arg = argpos;
3859 /* Check that the names correspond. */
3860 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
3862 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
3863 " to match the corresponding argument of the overridden"
3864 " procedure", proc_formal->sym->name, proc->name, &where,
3865 old_formal->sym->name);
3866 return FAILURE;
3869 check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
3870 if (check_dummy_characteristics (proc_formal->sym, old_formal->sym,
3871 check_type, err, sizeof(err)) == FAILURE)
3873 gfc_error ("Argument mismatch for the overriding procedure "
3874 "'%s' at %L: %s", proc->name, &where, err);
3875 return FAILURE;
3878 ++argpos;
3880 if (proc_formal || old_formal)
3882 gfc_error ("'%s' at %L must have the same number of formal arguments as"
3883 " the overridden procedure", proc->name, &where);
3884 return FAILURE;
3887 /* If the overridden binding is NOPASS, the overriding one must also be
3888 NOPASS. */
3889 if (old->n.tb->nopass && !proc->n.tb->nopass)
3891 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
3892 " NOPASS", proc->name, &where);
3893 return FAILURE;
3896 /* If the overridden binding is PASS(x), the overriding one must also be
3897 PASS and the passed-object dummy arguments must correspond. */
3898 if (!old->n.tb->nopass)
3900 if (proc->n.tb->nopass)
3902 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
3903 " PASS", proc->name, &where);
3904 return FAILURE;
3907 if (proc_pass_arg != old_pass_arg)
3909 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
3910 " the same position as the passed-object dummy argument of"
3911 " the overridden procedure", proc->name, &where);
3912 return FAILURE;
3916 return SUCCESS;