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