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