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