Debian Codegraph @

gcc/fortran/interface.cc

raw
1 /* Deal with interfaces.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 /* Deal with interfaces. An explicit interface is represented as a
23 singly linked list of formal argument structures attached to the
24 relevant symbols. For an implicit interface, the arguments don't
25 point to symbols. Explicit interfaces point to namespaces that
26 contain the symbols within that interface.
27
28 Implicit interfaces are linked together in a singly linked list
29 along the next_if member of symbol nodes. Since a particular
30 symbol can only have a single explicit interface, the symbol cannot
31 be part of multiple lists and a single next-member suffices.
32
33 This is not the case for general classes, though. An operator
34 definition is independent of just about all other uses and has it's
35 own head pointer.
36
37 Nameless interfaces:
38 Nameless interfaces create symbols with explicit interfaces within
39 the current namespace. They are otherwise unlinked.
40
41 Generic interfaces:
42 The generic name points to a linked list of symbols. Each symbol
43 has an explicit interface. Each explicit interface has its own
44 namespace containing the arguments. Module procedures are symbols in
45 which the interface is added later when the module procedure is parsed.
46
47 User operators:
48 User-defined operators are stored in a their own set of symtrees
49 separate from regular symbols. The symtrees point to gfc_user_op
50 structures which in turn head up a list of relevant interfaces.
51
52 Extended intrinsics and assignment:
53 The head of these interface lists are stored in the containing namespace.
54
55 Implicit interfaces:
56 An implicit interface is represented as a singly linked list of
57 formal argument list structures that don't point to any symbol
58 nodes -- they just contain types.
59
60
61 When a subprogram is defined, the program unit's name points to an
62 interface as usual, but the link to the namespace is NULL and the
63 formal argument list points to symbols within the same namespace as
64 the program unit name. */
65
66 #include "config.h"
67 #include "system.h"
68 #include "coretypes.h"
69 #include "options.h"
70 #include "gfortran.h"
71 #include "match.h"
72 #include "arith.h"
73
74 /* The current_interface structure holds information about the
75 interface currently being parsed. This structure is saved and
76 restored during recursive interfaces. */
77
78 gfc_interface_info current_interface;
79
80
81 /* Free the leading members of the gfc_interface linked list given in INTR
82 up to the END element (exclusive: the END element is not freed).
83 If END is not nullptr, it is assumed that END is in the linked list starting
84 with INTR. */
85
86 static void
87 free_interface_elements_until (gfc_interface *intr, gfc_interface *end)
88 {
89 gfc_interface *next;
90
91 for (; intr != end; intr = next)
92 {
93 next = intr->next;
94 free (intr);
95 }
96 }
97
98
99 /* Free a singly linked list of gfc_interface structures. */
100
101 void
102 gfc_free_interface (gfc_interface *intr)
103 {
104 free_interface_elements_until (intr, nullptr);
105 }
106
107
108 /* Update the interface pointer given by IFC_PTR to make it point to TAIL.
109 It is expected that TAIL (if non-null) is in the list pointed to by
110 IFC_PTR, hence the tail of it. The members of the list before TAIL are
111 freed before the pointer reassignment. */
112
113 void
114 gfc_drop_interface_elements_before (gfc_interface **ifc_ptr,
115 gfc_interface *tail)
116 {
117 if (ifc_ptr == nullptr)
118 return;
119
120 free_interface_elements_until (*ifc_ptr, tail);
121 *ifc_ptr = tail;
122 }
123
124
125 /* Change the operators unary plus and minus into binary plus and
126 minus respectively, leaving the rest unchanged. */
127
128 static gfc_intrinsic_op
129 fold_unary_intrinsic (gfc_intrinsic_op op)
130 {
131 switch (op)
132 {
133 case INTRINSIC_UPLUS:
134 op = INTRINSIC_PLUS;
135 break;
136 case INTRINSIC_UMINUS:
137 op = INTRINSIC_MINUS;
138 break;
139 default:
140 break;
141 }
142
143 return op;
144 }
145
146
147 /* Return the operator depending on the DTIO moded string. Note that
148 these are not operators in the normal sense and so have been placed
149 beyond GFC_INTRINSIC_END in gfortran.h:enum gfc_intrinsic_op. */
150
151 static gfc_intrinsic_op
152 dtio_op (char* mode)
153 {
154 if (strcmp (mode, "formatted") == 0)
155 return INTRINSIC_FORMATTED;
156 if (strcmp (mode, "unformatted") == 0)
157 return INTRINSIC_UNFORMATTED;
158 return INTRINSIC_NONE;
159 }
160
161
162 /* Match a generic specification. Depending on which type of
163 interface is found, the 'name' or 'op' pointers may be set.
164 This subroutine doesn't return MATCH_NO. */
165
166 match
167 gfc_match_generic_spec (interface_type *type,
168 char *name,
169 gfc_intrinsic_op *op)
170 {
171 char buffer[GFC_MAX_SYMBOL_LEN + 1];
172 match m;
173 gfc_intrinsic_op i;
174
175 if (gfc_match (" assignment ( = )") == MATCH_YES)
176 {
177 *type = INTERFACE_INTRINSIC_OP;
178 *op = INTRINSIC_ASSIGN;
179 return MATCH_YES;
180 }
181
182 if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
183 { /* Operator i/f */
184 *type = INTERFACE_INTRINSIC_OP;
185 *op = fold_unary_intrinsic (i);
186 return MATCH_YES;
187 }
188
189 *op = INTRINSIC_NONE;
190 if (gfc_match (" operator ( ") == MATCH_YES)
191 {
192 m = gfc_match_defined_op_name (buffer, 1);
193 if (m == MATCH_NO)
194 goto syntax;
195 if (m != MATCH_YES)
196 return MATCH_ERROR;
197
198 m = gfc_match_char (')');
199 if (m == MATCH_NO)
200 goto syntax;
201 if (m != MATCH_YES)
202 return MATCH_ERROR;
203
204 strcpy (name, buffer);
205 *type = INTERFACE_USER_OP;
206 return MATCH_YES;
207 }
208
209 if (gfc_match (" read ( %n )", buffer) == MATCH_YES)
210 {
211 *op = dtio_op (buffer);
212 if (*op == INTRINSIC_FORMATTED)
213 {
214 strcpy (name, gfc_code2string (dtio_procs, DTIO_RF));
215 *type = INTERFACE_DTIO;
216 }
217 if (*op == INTRINSIC_UNFORMATTED)
218 {
219 strcpy (name, gfc_code2string (dtio_procs, DTIO_RUF));
220 *type = INTERFACE_DTIO;
221 }
222 if (*op != INTRINSIC_NONE)
223 return MATCH_YES;
224 }
225
226 if (gfc_match (" write ( %n )", buffer) == MATCH_YES)
227 {
228 *op = dtio_op (buffer);
229 if (*op == INTRINSIC_FORMATTED)
230 {
231 strcpy (name, gfc_code2string (dtio_procs, DTIO_WF));
232 *type = INTERFACE_DTIO;
233 }
234 if (*op == INTRINSIC_UNFORMATTED)
235 {
236 strcpy (name, gfc_code2string (dtio_procs, DTIO_WUF));
237 *type = INTERFACE_DTIO;
238 }
239 if (*op != INTRINSIC_NONE)
240 return MATCH_YES;
241 }
242
243 if (gfc_match_name (buffer) == MATCH_YES)
244 {
245 strcpy (name, buffer);
246 *type = INTERFACE_GENERIC;
247 return MATCH_YES;
248 }
249
250 *type = INTERFACE_NAMELESS;
251 return MATCH_YES;
252
253 syntax:
254 gfc_error ("Syntax error in generic specification at %C");
255 return MATCH_ERROR;
256 }
257
258
259 /* Match one of the five F95 forms of an interface statement. The
260 matcher for the abstract interface follows. */
261
262 match
263 gfc_match_interface (void)
264 {
265 char name[GFC_MAX_SYMBOL_LEN + 1];
266 interface_type type;
267 gfc_symbol *sym;
268 gfc_intrinsic_op op;
269 match m;
270
271 m = gfc_match_space ();
272
273 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
274 return MATCH_ERROR;
275
276 /* If we're not looking at the end of the statement now, or if this
277 is not a nameless interface but we did not see a space, punt. */
278 if (gfc_match_eos () != MATCH_YES
279 || (type != INTERFACE_NAMELESS && m != MATCH_YES))
280 {
281 gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
282 "at %C");
283 return MATCH_ERROR;
284 }
285
286 current_interface.type = type;
287
288 switch (type)
289 {
290 case INTERFACE_DTIO:
291 case INTERFACE_GENERIC:
292 if (gfc_get_symbol (name, NULL, &sym))
293 return MATCH_ERROR;
294
295 if (!sym->attr.generic
296 && !gfc_add_generic (&sym->attr, sym->name, NULL))
297 return MATCH_ERROR;
298
299 if (sym->attr.dummy)
300 {
301 gfc_error ("Dummy procedure %qs at %C cannot have a "
302 "generic interface", sym->name);
303 return MATCH_ERROR;
304 }
305
306 current_interface.sym = gfc_new_block = sym;
307 break;
308
309 case INTERFACE_USER_OP:
310 current_interface.uop = gfc_get_uop (name);
311 break;
312
313 case INTERFACE_INTRINSIC_OP:
314 current_interface.op = op;
315 break;
316
317 case INTERFACE_NAMELESS:
318 case INTERFACE_ABSTRACT:
319 break;
320 }
321
322 return MATCH_YES;
323 }
324
325
326
327 /* Match a F2003 abstract interface. */
328
329 match
330 gfc_match_abstract_interface (void)
331 {
332 match m;
333
334 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT INTERFACE at %C"))
335 return MATCH_ERROR;
336
337 m = gfc_match_eos ();
338
339 if (m != MATCH_YES)
340 {
341 gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
342 return MATCH_ERROR;
343 }
344
345 current_interface.type = INTERFACE_ABSTRACT;
346
347 return m;
348 }
349
350
351 /* Match the different sort of generic-specs that can be present after
352 the END INTERFACE itself. */
353
354 match
355 gfc_match_end_interface (void)
356 {
357 char name[GFC_MAX_SYMBOL_LEN + 1];
358 interface_type type;
359 gfc_intrinsic_op op;
360 match m;
361
362 m = gfc_match_space ();
363
364 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
365 return MATCH_ERROR;
366
367 /* If we're not looking at the end of the statement now, or if this
368 is not a nameless interface but we did not see a space, punt. */
369 if (gfc_match_eos () != MATCH_YES
370 || (type != INTERFACE_NAMELESS && m != MATCH_YES))
371 {
372 gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
373 "statement at %C");
374 return MATCH_ERROR;
375 }
376
377 m = MATCH_YES;
378
379 switch (current_interface.type)
380 {
381 case INTERFACE_NAMELESS:
382 case INTERFACE_ABSTRACT:
383 if (type != INTERFACE_NAMELESS)
384 {
385 gfc_error ("Expected a nameless interface at %C");
386 m = MATCH_ERROR;
387 }
388
389 break;
390
391 case INTERFACE_INTRINSIC_OP:
392 if (type != current_interface.type || op != current_interface.op)
393 {
394
395 if (current_interface.op == INTRINSIC_ASSIGN)
396 {
397 m = MATCH_ERROR;
398 gfc_error ("Expected %<END INTERFACE ASSIGNMENT (=)%> at %C");
399 }
400 else
401 {
402 const char *s1, *s2;
403 s1 = gfc_op2string (current_interface.op);
404 s2 = gfc_op2string (op);
405
406 /* The following if-statements are used to enforce C1202
407 from F2003. */
408 if ((strcmp(s1, "==") == 0 && strcmp (s2, ".eq.") == 0)
409 || (strcmp(s1, ".eq.") == 0 && strcmp (s2, "==") == 0))
410 break;
411 if ((strcmp(s1, "/=") == 0 && strcmp (s2, ".ne.") == 0)
412 || (strcmp(s1, ".ne.") == 0 && strcmp (s2, "/=") == 0))
413 break;
414 if ((strcmp(s1, "<=") == 0 && strcmp (s2, ".le.") == 0)
415 || (strcmp(s1, ".le.") == 0 && strcmp (s2, "<=") == 0))
416 break;
417 if ((strcmp(s1, "<") == 0 && strcmp (s2, ".lt.") == 0)
418 || (strcmp(s1, ".lt.") == 0 && strcmp (s2, "<") == 0))
419 break;
420 if ((strcmp(s1, ">=") == 0 && strcmp (s2, ".ge.") == 0)
421 || (strcmp(s1, ".ge.") == 0 && strcmp (s2, ">=") == 0))
422 break;
423 if ((strcmp(s1, ">") == 0 && strcmp (s2, ".gt.") == 0)
424 || (strcmp(s1, ".gt.") == 0 && strcmp (s2, ">") == 0))
425 break;
426
427 m = MATCH_ERROR;
428 if (strcmp(s2, "none") == 0)
429 gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> "
430 "at %C", s1);
431 else
432 gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> at %C, "
433 "but got %qs", s1, s2);
434 }
435
436 }
437
438 break;
439
440 case INTERFACE_USER_OP:
441 /* Comparing the symbol node names is OK because only use-associated
442 symbols can be renamed. */
443 if (type != current_interface.type
444 || strcmp (current_interface.uop->name, name) != 0)
445 {
446 gfc_error ("Expecting %<END INTERFACE OPERATOR (.%s.)%> at %C",
447 current_interface.uop->name);
448 m = MATCH_ERROR;
449 }
450
451 break;
452
453 case INTERFACE_DTIO:
454 case INTERFACE_GENERIC:
455 /* If a use-associated symbol is renamed, check the local_name. */
456 const char *local_name = current_interface.sym->name;
457
458 if (current_interface.sym->attr.use_assoc
459 && current_interface.sym->attr.use_rename
460 && current_interface.sym->ns->use_stmts->rename
461 && (current_interface.sym->ns->use_stmts->rename->local_name[0]
462 != '\0'))
463 local_name = current_interface.sym->ns->use_stmts->rename->local_name;
464
465 if (type != current_interface.type
466 || strcmp (local_name, name) != 0)
467 {
468 gfc_error ("Expecting %<END INTERFACE %s%> at %C", local_name);
469 m = MATCH_ERROR;
470 }
471
472 break;
473 }
474
475 return m;
476 }
477
478
479 /* Return whether the component was defined anonymously. */
480
481 static bool
482 is_anonymous_component (gfc_component *cmp)
483 {
484 /* Only UNION and MAP components are anonymous. In the case of a MAP,
485 the derived type symbol is FL_STRUCT and the component name looks like mM*.
486 This is the only case in which the second character of a component name is
487 uppercase. */
488 return cmp->ts.type == BT_UNION
489 || (cmp->ts.type == BT_DERIVED
490 && cmp->ts.u.derived->attr.flavor == FL_STRUCT
491 && cmp->name[0] && cmp->name[1] && ISUPPER (cmp->name[1]));
492 }
493
494
495 /* Return whether the derived type was defined anonymously. */
496
497 static bool
498 is_anonymous_dt (gfc_symbol *derived)
499 {
500 /* UNION and MAP types are always anonymous. Otherwise, only nested STRUCTURE
501 types can be anonymous. For anonymous MAP/STRUCTURE, we have FL_STRUCT
502 and the type name looks like XX*. This is the only case in which the
503 second character of a type name is uppercase. */
504 return derived->attr.flavor == FL_UNION
505 || (derived->attr.flavor == FL_STRUCT
506 && derived->name[0] && derived->name[1] && ISUPPER (derived->name[1]));
507 }
508
509
510 /* Compare components according to 4.4.2 of the Fortran standard. */
511
512 static bool
513 compare_components (gfc_component *cmp1, gfc_component *cmp2,
514 gfc_symbol *derived1, gfc_symbol *derived2)
515 {
516 /* Compare names, but not for anonymous components such as UNION or MAP. */
517 if (!is_anonymous_component (cmp1) && !is_anonymous_component (cmp2)
518 && strcmp (cmp1->name, cmp2->name) != 0)
519 return false;
520
521 if (cmp1->attr.access != cmp2->attr.access)
522 return false;
523
524 if (cmp1->attr.pointer != cmp2->attr.pointer)
525 return false;
526
527 if (cmp1->attr.dimension != cmp2->attr.dimension)
528 return false;
529
530 if (cmp1->attr.codimension != cmp2->attr.codimension)
531 return false;
532
533 if (cmp1->attr.allocatable != cmp2->attr.allocatable)
534 return false;
535
536 if (cmp1->attr.dimension && gfc_compare_array_spec (cmp1->as, cmp2->as) == 0)
537 return false;
538
539 if (cmp1->attr.codimension
540 && gfc_compare_array_spec (cmp1->as, cmp2->as) == 0)
541 return false;
542
543 if (cmp1->ts.type == BT_CHARACTER && cmp2->ts.type == BT_CHARACTER)
544 {
545 gfc_charlen *l1 = cmp1->ts.u.cl;
546 gfc_charlen *l2 = cmp2->ts.u.cl;
547 if (l1 && l2 && l1->length && l2->length
548 && l1->length->expr_type == EXPR_CONSTANT
549 && l2->length->expr_type == EXPR_CONSTANT
550 && gfc_dep_compare_expr (l1->length, l2->length) != 0)
551 return false;
552 }
553
554 /* Make sure that link lists do not put this function into an
555 endless recursive loop! */
556 if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
557 && !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived)
558 && !gfc_compare_types (&cmp1->ts, &cmp2->ts))
559 return false;
560
561 else if ( (cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
562 && !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
563 return false;
564
565 else if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
566 && (cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
567 return false;
568
569 return true;
570 }
571
572
573 /* Compare two union types by comparing the components of their maps.
574 Because unions and maps are anonymous their types get special internal
575 names; therefore the usual derived type comparison will fail on them.
576
577 Returns nonzero if equal, as with gfc_compare_derived_types. Also as with
578 gfc_compare_derived_types, 'equal' is closer to meaning 'duplicate
579 definitions' than 'equivalent structure'. */
580
581 static bool
582 compare_union_types (gfc_symbol *un1, gfc_symbol *un2)
583 {
584 gfc_component *map1, *map2, *cmp1, *cmp2;
585 gfc_symbol *map1_t, *map2_t;
586
587 if (un1->attr.flavor != FL_UNION || un2->attr.flavor != FL_UNION)
588 return false;
589
590 if (un1->attr.zero_comp != un2->attr.zero_comp)
591 return false;
592
593 if (un1->attr.zero_comp)
594 return true;
595
596 map1 = un1->components;
597 map2 = un2->components;
598
599 /* In terms of 'equality' here we are worried about types which are
600 declared the same in two places, not types that represent equivalent
601 structures. (This is common because of FORTRAN's weird scoping rules.)
602 Though two unions with their maps in different orders could be equivalent,
603 we will say they are not equal for the purposes of this test; therefore
604 we compare the maps sequentially. */
605 for (;;)
606 {
607 map1_t = map1->ts.u.derived;
608 map2_t = map2->ts.u.derived;
609
610 cmp1 = map1_t->components;
611 cmp2 = map2_t->components;
612
613 /* Protect against null components. */
614 if (map1_t->attr.zero_comp != map2_t->attr.zero_comp)
615 return false;
616
617 if (map1_t->attr.zero_comp)
618 return true;
619
620 for (;;)
621 {
622 /* No two fields will ever point to the same map type unless they are
623 the same component, because one map field is created with its type
624 declaration. Therefore don't worry about recursion here. */
625 /* TODO: worry about recursion into parent types of the unions? */
626 if (!compare_components (cmp1, cmp2, map1_t, map2_t))
627 return false;
628
629 cmp1 = cmp1->next;
630 cmp2 = cmp2->next;
631
632 if (cmp1 == NULL && cmp2 == NULL)
633 break;
634 if (cmp1 == NULL || cmp2 == NULL)
635 return false;
636 }
637
638 map1 = map1->next;
639 map2 = map2->next;
640
641 if (map1 == NULL && map2 == NULL)
642 break;
643 if (map1 == NULL || map2 == NULL)
644 return false;
645 }
646
647 return true;
648 }
649
650
651
652 /* Compare two derived types using the criteria in 4.4.2 of the standard,
653 recursing through gfc_compare_types for the components. */
654
655 bool
656 gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
657 {
658 gfc_component *cmp1, *cmp2;
659
660 if (derived1 == derived2)
661 return true;
662
663 if (!derived1 || !derived2)
664 gfc_internal_error ("gfc_compare_derived_types: invalid derived type");
665
666 if (derived1->attr.unlimited_polymorphic
667 && derived2->attr.unlimited_polymorphic)
668 return true;
669
670 if (derived1->attr.unlimited_polymorphic
671 != derived2->attr.unlimited_polymorphic)
672 return false;
673
674 /* Compare UNION types specially. */
675 if (derived1->attr.flavor == FL_UNION || derived2->attr.flavor == FL_UNION)
676 return compare_union_types (derived1, derived2);
677
678 /* Special case for comparing derived types across namespaces. If the
679 true names and module names are the same and the module name is
680 nonnull, then they are equal. */
681 if (strcmp (derived1->name, derived2->name) == 0
682 && derived1->module != NULL && derived2->module != NULL
683 && strcmp (derived1->module, derived2->module) == 0)
684 return true;
685
686 /* Compare type via the rules of the standard. Both types must have the
687 SEQUENCE or BIND(C) attribute to be equal. We also compare types
688 recursively if they are class descriptors types or virtual tables types.
689 STRUCTUREs are special because they can be anonymous; therefore two
690 structures with different names may be equal. */
691
692 /* Compare names, but not for anonymous types such as UNION or MAP. */
693 if (!is_anonymous_dt (derived1) && !is_anonymous_dt (derived2)
694 && strcmp (derived1->name, derived2->name) != 0)
695 return false;
696
697 if (derived1->component_access == ACCESS_PRIVATE
698 || derived2->component_access == ACCESS_PRIVATE)
699 return false;
700
701 if (!(derived1->attr.sequence && derived2->attr.sequence)
702 && !(derived1->attr.is_bind_c && derived2->attr.is_bind_c)
703 && !(derived1->attr.is_class && derived2->attr.is_class)
704 && !(derived1->attr.vtype && derived2->attr.vtype)
705 && !(derived1->attr.pdt_type && derived2->attr.pdt_type))
706 return false;
707
708 /* Protect against null components. */
709 if (derived1->attr.zero_comp != derived2->attr.zero_comp)
710 return false;
711
712 if (derived1->attr.zero_comp)
713 return true;
714
715 cmp1 = derived1->components;
716 cmp2 = derived2->components;
717
718 /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
719 simple test can speed things up. Otherwise, lots of things have to
720 match. */
721 for (;;)
722 {
723 if (!compare_components (cmp1, cmp2, derived1, derived2))
724 return false;
725
726 cmp1 = cmp1->next;
727 cmp2 = cmp2->next;
728
729 if (cmp1 == NULL && cmp2 == NULL)
730 break;
731 if (cmp1 == NULL || cmp2 == NULL)
732 return false;
733 }
734
735 return true;
736 }
737
738
739 /* Compare two typespecs, recursively if necessary. */
740
741 bool
742 gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
743 {
744 /* See if one of the typespecs is a BT_VOID, which is what is being used
745 to allow the funcs like c_f_pointer to accept any pointer type.
746 TODO: Possibly should narrow this to just the one typespec coming in
747 that is for the formal arg, but oh well. */
748 if (ts1->type == BT_VOID || ts2->type == BT_VOID)
749 return true;
750
751 /* Special case for our C interop types. FIXME: There should be a
752 better way of doing this. When ISO C binding is cleared up,
753 this can probably be removed. See PR 57048. */
754
755 if ((ts1->type == BT_INTEGER
756 && ts2->type == BT_DERIVED
757 && ts1->f90_type == BT_VOID
758 && ts2->u.derived->from_intmod == INTMOD_ISO_C_BINDING
759 && ts1->u.derived
760 && strcmp (ts1->u.derived->name, ts2->u.derived->name) == 0)
761 || (ts2->type == BT_INTEGER
762 && ts1->type == BT_DERIVED
763 && ts2->f90_type == BT_VOID
764 && ts1->u.derived->from_intmod == INTMOD_ISO_C_BINDING
765 && ts2->u.derived
766 && strcmp (ts1->u.derived->name, ts2->u.derived->name) == 0))
767 return true;
768
769 /* The _data component is not always present, therefore check for its
770 presence before assuming, that its derived->attr is available.
771 When the _data component is not present, then nevertheless the
772 unlimited_polymorphic flag may be set in the derived type's attr. */
773 if (ts1->type == BT_CLASS && ts1->u.derived->components
774 && ((ts1->u.derived->attr.is_class
775 && ts1->u.derived->components->ts.u.derived->attr
776 .unlimited_polymorphic)
777 || ts1->u.derived->attr.unlimited_polymorphic))
778 return true;
779
780 /* F2003: C717 */
781 if (ts2->type == BT_CLASS && ts1->type == BT_DERIVED
782 && ts2->u.derived->components
783 && ((ts2->u.derived->attr.is_class
784 && ts2->u.derived->components->ts.u.derived->attr
785 .unlimited_polymorphic)
786 || ts2->u.derived->attr.unlimited_polymorphic)
787 && (ts1->u.derived->attr.sequence || ts1->u.derived->attr.is_bind_c))
788 return true;
789
790 if (ts1->type != ts2->type
791 && ((ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
792 || (ts2->type != BT_DERIVED && ts2->type != BT_CLASS)))
793 return false;
794
795 if (ts1->type == BT_UNION)
796 return compare_union_types (ts1->u.derived, ts2->u.derived);
797
798 if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
799 return (ts1->kind == ts2->kind);
800
801 /* Compare derived types. */
802 return gfc_type_compatible (ts1, ts2);
803 }
804
805
806 static bool
807 compare_type (gfc_symbol *s1, gfc_symbol *s2)
808 {
809 if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
810 return true;
811
812 return gfc_compare_types (&s1->ts, &s2->ts) || s2->ts.type == BT_ASSUMED;
813 }
814
815
816 static bool
817 compare_type_characteristics (gfc_symbol *s1, gfc_symbol *s2)
818 {
819 /* TYPE and CLASS of the same declared type are type compatible,
820 but have different characteristics. */
821 if ((s1->ts.type == BT_CLASS && s2->ts.type == BT_DERIVED)
822 || (s1->ts.type == BT_DERIVED && s2->ts.type == BT_CLASS))
823 return false;
824
825 return compare_type (s1, s2);
826 }
827
828
829 static bool
830 compare_rank (gfc_symbol *s1, gfc_symbol *s2)
831 {
832 gfc_array_spec *as1, *as2;
833 int r1, r2;
834
835 if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
836 return true;
837
838 as1 = (s1->ts.type == BT_CLASS
839 && !s1->ts.u.derived->attr.unlimited_polymorphic)
840 ? CLASS_DATA (s1)->as : s1->as;
841 as2 = (s2->ts.type == BT_CLASS
842 && !s2->ts.u.derived->attr.unlimited_polymorphic)
843 ? CLASS_DATA (s2)->as : s2->as;
844
845 r1 = as1 ? as1->rank : 0;
846 r2 = as2 ? as2->rank : 0;
847
848 if (r1 != r2 && (!as2 || as2->type != AS_ASSUMED_RANK))
849 return false; /* Ranks differ. */
850
851 return true;
852 }
853
854
855 /* Given two symbols that are formal arguments, compare their ranks
856 and types. Returns true if they have the same rank and type,
857 false otherwise. */
858
859 static bool
860 compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
861 {
862 return compare_type (s1, s2) && compare_rank (s1, s2);
863 }
864
865
866 /* Given two symbols that are formal arguments, compare their types
867 and rank and their formal interfaces if they are both dummy
868 procedures. Returns true if the same, false if different. */
869
870 static bool
871 compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
872 {
873 if (s1 == NULL || s2 == NULL)
874 return (s1 == s2);
875
876 if (s1 == s2)
877 return true;
878
879 if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
880 return compare_type_rank (s1, s2);
881
882 if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
883 return false;
884
885 /* At this point, both symbols are procedures. It can happen that
886 external procedures are compared, where one is identified by usage
887 to be a function or subroutine but the other is not. Check TKR
888 nonetheless for these cases. */
889 if (s1->attr.function == 0 && s1->attr.subroutine == 0)
890 return s1->attr.external ? compare_type_rank (s1, s2) : false;
891
892 if (s2->attr.function == 0 && s2->attr.subroutine == 0)
893 return s2->attr.external ? compare_type_rank (s1, s2) : false;
894
895 /* Now the type of procedure has been identified. */
896 if (s1->attr.function != s2->attr.function
897 || s1->attr.subroutine != s2->attr.subroutine)
898 return false;
899
900 if (s1->attr.function && !compare_type_rank (s1, s2))
901 return false;
902
903 /* Originally, gfortran recursed here to check the interfaces of passed
904 procedures. This is explicitly not required by the standard. */
905 return true;
906 }
907
908
909 /* Given a formal argument list and a keyword name, search the list
910 for that keyword. Returns the correct symbol node if found, NULL
911 if not found. */
912
913 static gfc_symbol *
914 find_keyword_arg (const char *name, gfc_formal_arglist *f)
915 {
916 for (; f; f = f->next)
917 if (strcmp (f->sym->name, name) == 0)
918 return f->sym;
919
920 return NULL;
921 }
922
923
924 /******** Interface checking subroutines **********/
925
926
927 /* Given an operator interface and the operator, make sure that all
928 interfaces for that operator are legal. */
929
930 bool
931 gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
932 locus opwhere)
933 {
934 gfc_formal_arglist *formal;
935 sym_intent i1, i2;
936 bt t1, t2;
937 int args, r1, r2, k1, k2;
938
939 gcc_assert (sym);
940
941 args = 0;
942 t1 = t2 = BT_UNKNOWN;
943 i1 = i2 = INTENT_UNKNOWN;
944 r1 = r2 = -1;
945 k1 = k2 = -1;
946
947 for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
948 {
949 gfc_symbol *fsym = formal->sym;
950 if (fsym == NULL)
951 {
952 gfc_error ("Alternate return cannot appear in operator "
953 "interface at %L", &sym->declared_at);
954 return false;
955 }
956 if (args == 0)
957 {
958 t1 = fsym->ts.type;
959 i1 = fsym->attr.intent;
960 r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
961 k1 = fsym->ts.kind;
962 }
963 if (args == 1)
964 {
965 t2 = fsym->ts.type;
966 i2 = fsym->attr.intent;
967 r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
968 k2 = fsym->ts.kind;
969 }
970 args++;
971 }
972
973 /* Only +, - and .not. can be unary operators.
974 .not. cannot be a binary operator. */
975 if (args == 0 || args > 2 || (args == 1 && op != INTRINSIC_PLUS
976 && op != INTRINSIC_MINUS
977 && op != INTRINSIC_NOT)
978 || (args == 2 && op == INTRINSIC_NOT))
979 {
980 if (op == INTRINSIC_ASSIGN)
981 gfc_error ("Assignment operator interface at %L must have "
982 "two arguments", &sym->declared_at);
983 else
984 gfc_error ("Operator interface at %L has the wrong number of arguments",
985 &sym->declared_at);
986 return false;
987 }
988
989 /* Check that intrinsics are mapped to functions, except
990 INTRINSIC_ASSIGN which should map to a subroutine. */
991 if (op == INTRINSIC_ASSIGN)
992 {
993 gfc_formal_arglist *dummy_args;
994
995 if (!sym->attr.subroutine)
996 {
997 gfc_error ("Assignment operator interface at %L must be "
998 "a SUBROUTINE", &sym->declared_at);
999 return false;
1000 }
1001
1002 /* Allowed are (per F2003, 12.3.2.1.2 Defined assignments):
1003 - First argument an array with different rank than second,
1004 - First argument is a scalar and second an array,
1005 - Types and kinds do not conform, or
1006 - First argument is of derived type. */
1007 dummy_args = gfc_sym_get_dummy_args (sym);
1008 if (dummy_args->sym->ts.type != BT_DERIVED
1009 && dummy_args->sym->ts.type != BT_CLASS
1010 && (r2 == 0 || r1 == r2)
1011 && (dummy_args->sym->ts.type == dummy_args->next->sym->ts.type
1012 || (gfc_numeric_ts (&dummy_args->sym->ts)
1013 && gfc_numeric_ts (&dummy_args->next->sym->ts))))
1014 {
1015 gfc_error ("Assignment operator interface at %L must not redefine "
1016 "an INTRINSIC type assignment", &sym->declared_at);
1017 return false;
1018 }
1019 }
1020 else
1021 {
1022 if (!sym->attr.function)
1023 {
1024 gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
1025 &sym->declared_at);
1026 return false;
1027 }
1028 }
1029
1030 /* Check intents on operator interfaces. */
1031 if (op == INTRINSIC_ASSIGN)
1032 {
1033 if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
1034 {
1035 gfc_error ("First argument of defined assignment at %L must be "
1036 "INTENT(OUT) or INTENT(INOUT)", &sym->declared_at);
1037 return false;
1038 }
1039
1040 if (i2 != INTENT_IN)
1041 {
1042 gfc_error ("Second argument of defined assignment at %L must be "
1043 "INTENT(IN)", &sym->declared_at);
1044 return false;
1045 }
1046 }
1047 else
1048 {
1049 if (i1 != INTENT_IN)
1050 {
1051 gfc_error ("First argument of operator interface at %L must be "
1052 "INTENT(IN)", &sym->declared_at);
1053 return false;
1054 }
1055
1056 if (args == 2 && i2 != INTENT_IN)
1057 {
1058 gfc_error ("Second argument of operator interface at %L must be "
1059 "INTENT(IN)", &sym->declared_at);
1060 return false;
1061 }
1062 }
1063
1064 /* From now on, all we have to do is check that the operator definition
1065 doesn't conflict with an intrinsic operator. The rules for this
1066 game are defined in 7.1.2 and 7.1.3 of both F95 and F2003 standards,
1067 as well as 12.3.2.1.1 of Fortran 2003:
1068
1069 "If the operator is an intrinsic-operator (R310), the number of
1070 function arguments shall be consistent with the intrinsic uses of
1071 that operator, and the types, kind type parameters, or ranks of the
1072 dummy arguments shall differ from those required for the intrinsic
1073 operation (7.1.2)." */
1074
1075 #define IS_NUMERIC_TYPE(t) \
1076 ((t) == BT_INTEGER || (t) == BT_REAL || (t) == BT_COMPLEX)
1077
1078 /* Unary ops are easy, do them first. */
1079 if (op == INTRINSIC_NOT)
1080 {
1081 if (t1 == BT_LOGICAL)
1082 goto bad_repl;
1083 else
1084 return true;
1085 }
1086
1087 if (args == 1 && (op == INTRINSIC_PLUS || op == INTRINSIC_MINUS))
1088 {
1089 if (IS_NUMERIC_TYPE (t1))
1090 goto bad_repl;
1091 else
1092 return true;
1093 }
1094
1095 /* Character intrinsic operators have same character kind, thus
1096 operator definitions with operands of different character kinds
1097 are always safe. */
1098 if (t1 == BT_CHARACTER && t2 == BT_CHARACTER && k1 != k2)
1099 return true;
1100
1101 /* Intrinsic operators always perform on arguments of same rank,
1102 so different ranks is also always safe. (rank == 0) is an exception
1103 to that, because all intrinsic operators are elemental. */
1104 if (r1 != r2 && r1 != 0 && r2 != 0)
1105 return true;
1106
1107 switch (op)
1108 {
1109 case INTRINSIC_EQ:
1110 case INTRINSIC_EQ_OS:
1111 case INTRINSIC_NE:
1112 case INTRINSIC_NE_OS:
1113 if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
1114 goto bad_repl;
1115 /* Fall through. */
1116
1117 case INTRINSIC_PLUS:
1118 case INTRINSIC_MINUS:
1119 case INTRINSIC_TIMES:
1120 case INTRINSIC_DIVIDE:
1121 case INTRINSIC_POWER:
1122 if (IS_NUMERIC_TYPE (t1) && IS_NUMERIC_TYPE (t2))
1123 goto bad_repl;
1124 break;
1125
1126 case INTRINSIC_GT:
1127 case INTRINSIC_GT_OS:
1128 case INTRINSIC_GE:
1129 case INTRINSIC_GE_OS:
1130 case INTRINSIC_LT:
1131 case INTRINSIC_LT_OS:
1132 case INTRINSIC_LE:
1133 case INTRINSIC_LE_OS:
1134 if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
1135 goto bad_repl;
1136 if ((t1 == BT_INTEGER || t1 == BT_REAL)
1137 && (t2 == BT_INTEGER || t2 == BT_REAL))
1138 goto bad_repl;
1139 break;
1140
1141 case INTRINSIC_CONCAT:
1142 if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
1143 goto bad_repl;
1144 break;
1145
1146 case INTRINSIC_AND:
1147 case INTRINSIC_OR:
1148 case INTRINSIC_EQV:
1149 case INTRINSIC_NEQV:
1150 if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
1151 goto bad_repl;
1152 break;
1153
1154 default:
1155 break;
1156 }
1157
1158 return true;
1159
1160 #undef IS_NUMERIC_TYPE
1161
1162 bad_repl:
1163 gfc_error ("Operator interface at %L conflicts with intrinsic interface",
1164 &opwhere);
1165 return false;
1166 }
1167
1168
1169 /* Given a pair of formal argument lists, we see if the two lists can
1170 be distinguished by counting the number of nonoptional arguments of
1171 a given type/rank in f1 and seeing if there are less then that
1172 number of those arguments in f2 (including optional arguments).
1173 Since this test is asymmetric, it has to be called twice to make it
1174 symmetric. Returns nonzero if the argument lists are incompatible
1175 by this test. This subroutine implements rule 1 of section F03:16.2.3.
1176 'p1' and 'p2' are the PASS arguments of both procedures (if applicable). */
1177
1178 static bool
1179 count_types_test (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
1180 const char *p1, const char *p2)
1181 {
1182 int ac1, ac2, i, j, k, n1;
1183 gfc_formal_arglist *f;
1184
1185 typedef struct
1186 {
1187 int flag;
1188 gfc_symbol *sym;
1189 }
1190 arginfo;
1191
1192 arginfo *arg;
1193
1194 n1 = 0;
1195
1196 for (f = f1; f; f = f->next)
1197 n1++;
1198
1199 /* Build an array of integers that gives the same integer to
1200 arguments of the same type/rank. */
1201 arg = XCNEWVEC (arginfo, n1);
1202
1203 f = f1;
1204 for (i = 0; i < n1; i++, f = f->next)
1205 {
1206 arg[i].flag = -1;
1207 arg[i].sym = f->sym;
1208 }
1209
1210 k = 0;
1211
1212 for (i = 0; i < n1; i++)
1213 {
1214 if (arg[i].flag != -1)
1215 continue;
1216
1217 if (arg[i].sym && (arg[i].sym->attr.optional
1218 || (p1 && strcmp (arg[i].sym->name, p1) == 0)))
1219 continue; /* Skip OPTIONAL and PASS arguments. */
1220
1221 arg[i].flag = k;
1222
1223 /* Find other non-optional, non-pass arguments of the same type/rank. */
1224 for (j = i + 1; j < n1; j++)
1225 if ((arg[j].sym == NULL
1226 || !(arg[j].sym->attr.optional
1227 || (p1 && strcmp (arg[j].sym->name, p1) == 0)))
1228 && (compare_type_rank_if (arg[i].sym, arg[j].sym)
1229 || compare_type_rank_if (arg[j].sym, arg[i].sym)))
1230 arg[j].flag = k;
1231
1232 k++;
1233 }
1234
1235 /* Now loop over each distinct type found in f1. */
1236 k = 0;
1237 bool rc = false;
1238
1239 for (i = 0; i < n1; i++)
1240 {
1241 if (arg[i].flag != k)
1242 continue;
1243
1244 ac1 = 1;
1245 for (j = i + 1; j < n1; j++)
1246 if (arg[j].flag == k)
1247 ac1++;
1248
1249 /* Count the number of non-pass arguments in f2 with that type,
1250 including those that are optional. */
1251 ac2 = 0;
1252
1253 for (f = f2; f; f = f->next)
1254 if ((!p2 || strcmp (f->sym->name, p2) != 0)
1255 && (compare_type_rank_if (arg[i].sym, f->sym)
1256 || compare_type_rank_if (f->sym, arg[i].sym)))
1257 ac2++;
1258
1259 if (ac1 > ac2)
1260 {
1261 rc = true;
1262 break;
1263 }
1264
1265 k++;
1266 }
1267
1268 free (arg);
1269
1270 return rc;
1271 }
1272
1273
1274 /* Returns true if two dummy arguments are distinguishable due to their POINTER
1275 and ALLOCATABLE attributes according to F2018 section 15.4.3.4.5 (3).
1276 The function is asymmetric wrt to the arguments s1 and s2 and should always
1277 be called twice (with flipped arguments in the second call). */
1278
1279 static bool
1280 compare_ptr_alloc(gfc_symbol *s1, gfc_symbol *s2)
1281 {
1282 /* Is s1 allocatable? */
1283 const bool a1 = s1->ts.type == BT_CLASS ?
1284 CLASS_DATA(s1)->attr.allocatable : s1->attr.allocatable;
1285 /* Is s2 a pointer? */
1286 const bool p2 = s2->ts.type == BT_CLASS ?
1287 CLASS_DATA(s2)->attr.class_pointer : s2->attr.pointer;
1288 return a1 && p2 && (s2->attr.intent != INTENT_IN);
1289 }
1290
1291
1292 /* Perform the correspondence test in rule (3) of F08:C1215.
1293 Returns zero if no argument is found that satisfies this rule,
1294 nonzero otherwise. 'p1' and 'p2' are the PASS arguments of both procedures
1295 (if applicable).
1296
1297 This test is also not symmetric in f1 and f2 and must be called
1298 twice. This test finds problems caused by sorting the actual
1299 argument list with keywords. For example:
1300
1301 INTERFACE FOO
1302 SUBROUTINE F1(A, B)
1303 INTEGER :: A ; REAL :: B
1304 END SUBROUTINE F1
1305
1306 SUBROUTINE F2(B, A)
1307 INTEGER :: A ; REAL :: B
1308 END SUBROUTINE F1
1309 END INTERFACE FOO
1310
1311 At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous. */
1312
1313 static bool
1314 generic_correspondence (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
1315 const char *p1, const char *p2)
1316 {
1317 gfc_formal_arglist *f2_save, *g;
1318 gfc_symbol *sym;
1319
1320 f2_save = f2;
1321
1322 while (f1)
1323 {
1324 if (!f1->sym || f1->sym->attr.optional)
1325 goto next;
1326
1327 if (p1 && strcmp (f1->sym->name, p1) == 0)
1328 f1 = f1->next;
1329 if (f2 && p2 && strcmp (f2->sym->name, p2) == 0)
1330 f2 = f2->next;
1331
1332 if (f2 != NULL && (compare_type_rank (f1->sym, f2->sym)
1333 || compare_type_rank (f2->sym, f1->sym))
1334 && !((gfc_option.allow_std & GFC_STD_F2008)
1335 && (compare_ptr_alloc(f1->sym, f2->sym)
1336 || compare_ptr_alloc(f2->sym, f1->sym))))
1337 goto next;
1338
1339 /* Now search for a disambiguating keyword argument starting at
1340 the current non-match. */
1341 for (g = f1; g; g = g->next)
1342 {
1343 if (g->sym->attr.optional || (p1 && strcmp (g->sym->name, p1) == 0))
1344 continue;
1345
1346 sym = find_keyword_arg (g->sym->name, f2_save);
1347 if (sym == NULL || !compare_type_rank (g->sym, sym)
1348 || ((gfc_option.allow_std & GFC_STD_F2008)
1349 && (compare_ptr_alloc(sym, g->sym)
1350 || compare_ptr_alloc(g->sym, sym))))
1351 return true;
1352 }
1353
1354 next:
1355 if (f1 != NULL)
1356 f1 = f1->next;
1357 if (f2 != NULL)
1358 f2 = f2->next;
1359 }
1360
1361 return false;
1362 }
1363
1364
1365 static int
1366 symbol_rank (gfc_symbol *sym)
1367 {
1368 gfc_array_spec *as = NULL;
1369
1370 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
1371 as = CLASS_DATA (sym)->as;
1372 else
1373 as = sym->as;
1374
1375 return as ? as->rank : 0;
1376 }
1377
1378
1379 /* Check if the characteristics of two dummy arguments match,
1380 cf. F08:12.3.2. */
1381
1382 bool
1383 gfc_check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
1384 bool type_must_agree, char *errmsg,
1385 int err_len)
1386 {
1387 if (s1 == NULL || s2 == NULL)
1388 return s1 == s2 ? true : false;
1389
1390 if (s1->attr.proc == PROC_ST_FUNCTION || s2->attr.proc == PROC_ST_FUNCTION)
1391 {
1392 strncpy (errmsg, "Statement function", err_len);
1393 return false;
1394 }
1395
1396 /* Check type and rank. */
1397 if (type_must_agree)
1398 {
1399 if (!compare_type_characteristics (s1, s2)
1400 || !compare_type_characteristics (s2, s1))
1401 {
1402 snprintf (errmsg, err_len, "Type mismatch in argument '%s' (%s/%s)",
1403 s1->name, gfc_dummy_typename (&s1->ts),
1404 gfc_dummy_typename (&s2->ts));
1405 return false;
1406 }
1407 if (!compare_rank (s1, s2))
1408 {
1409 snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
1410 s1->name, symbol_rank (s1), symbol_rank (s2));
1411 return false;
1412 }
1413 }
1414
1415 /* A lot of information is missing for artificially generated
1416 formal arguments, let's not look into that. */
1417
1418 if (!s1->attr.artificial && !s2->attr.artificial)
1419 {
1420 /* Check INTENT. */
1421 if (s1->attr.intent != s2->attr.intent)
1422 {
1423 snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
1424 s1->name);
1425 return false;
1426 }
1427
1428 /* Check OPTIONAL attribute. */
1429 if (s1->attr.optional != s2->attr.optional)
1430 {
1431 snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
1432 s1->name);
1433 return false;
1434 }
1435
1436 /* Check ALLOCATABLE attribute. */
1437 if (s1->attr.allocatable != s2->attr.allocatable)
1438 {
1439 snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
1440 s1->name);
1441 return false;
1442 }
1443
1444 /* Check POINTER attribute. */
1445 if (s1->attr.pointer != s2->attr.pointer)
1446 {
1447 snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
1448 s1->name);
1449 return false;
1450 }
1451
1452 /* Check TARGET attribute. */
1453 if (s1->attr.target != s2->attr.target)
1454 {
1455 snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
1456 s1->name);
1457 return false;
1458 }
1459
1460 /* Check ASYNCHRONOUS attribute. */
1461 if (s1->attr.asynchronous != s2->attr.asynchronous)
1462 {
1463 snprintf (errmsg, err_len, "ASYNCHRONOUS mismatch in argument '%s'",
1464 s1->name);
1465 return false;
1466 }
1467
1468 /* Check CONTIGUOUS attribute. */
1469 if (s1->attr.contiguous != s2->attr.contiguous)
1470 {
1471 snprintf (errmsg, err_len, "CONTIGUOUS mismatch in argument '%s'",
1472 s1->name);
1473 return false;
1474 }
1475
1476 /* Check VALUE attribute. */
1477 if (s1->attr.value != s2->attr.value)
1478 {
1479 snprintf (errmsg, err_len, "VALUE mismatch in argument '%s'",
1480 s1->name);
1481 return false;
1482 }
1483
1484 /* Check VOLATILE attribute. */
1485 if (s1->attr.volatile_ != s2->attr.volatile_)
1486 {
1487 snprintf (errmsg, err_len, "VOLATILE mismatch in argument '%s'",
1488 s1->name);
1489 return false;
1490 }
1491 }
1492
1493 /* Check interface of dummy procedures. */
1494 if (s1->attr.flavor == FL_PROCEDURE)
1495 {
1496 char err[200];
1497 if (!gfc_compare_interfaces (s1, s2, s2->name, 0, 1, err, sizeof(err),
1498 NULL, NULL))
1499 {
1500 snprintf (errmsg, err_len, "Interface mismatch in dummy procedure "
1501 "'%s': %s", s1->name, err);
1502 return false;
1503 }
1504 }
1505
1506 /* Check string length. */
1507 if (s1->ts.type == BT_CHARACTER
1508 && s1->ts.u.cl && s1->ts.u.cl->length
1509 && s2->ts.u.cl && s2->ts.u.cl->length)
1510 {
1511 int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
1512 s2->ts.u.cl->length);
1513 switch (compval)
1514 {
1515 case -1:
1516 case 1:
1517 case -3:
1518 snprintf (errmsg, err_len, "Character length mismatch "
1519 "in argument '%s'", s1->name);
1520 return false;
1521
1522 case -2:
1523 /* FIXME: Implement a warning for this case.
1524 gfc_warning (0, "Possible character length mismatch in argument %qs",
1525 s1->name);*/
1526 break;
1527
1528 case 0:
1529 break;
1530
1531 default:
1532 gfc_internal_error ("check_dummy_characteristics: Unexpected result "
1533 "%i of gfc_dep_compare_expr", compval);
1534 break;
1535 }
1536 }
1537
1538 /* Check array shape. */
1539 if (s1->as && s2->as)
1540 {
1541 int i, compval;
1542 gfc_expr *shape1, *shape2;
1543
1544 if (s1->as->rank != s2->as->rank)
1545 {
1546 snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
1547 s1->name, s1->as->rank, s2->as->rank);
1548 return false;
1549 }
1550
1551 /* Sometimes the ambiguity between deferred shape and assumed shape
1552 does not get resolved in module procedures, where the only explicit
1553 declaration of the dummy is in the interface. */
1554 if (s1->ns->proc_name && s1->ns->proc_name->attr.module_procedure
1555 && s1->as->type == AS_ASSUMED_SHAPE
1556 && s2->as->type == AS_DEFERRED)
1557 {
1558 s2->as->type = AS_ASSUMED_SHAPE;
1559 for (i = 0; i < s2->as->rank; i++)
1560 if (s1->as->lower[i] != NULL)
1561 s2->as->lower[i] = gfc_copy_expr (s1->as->lower[i]);
1562 }
1563
1564 if (s1->as->type != s2->as->type
1565 && !(s1->as->type == AS_DEFERRED
1566 && s2->as->type == AS_ASSUMED_SHAPE))
1567 {
1568 snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
1569 s1->name);
1570 return false;
1571 }
1572
1573 if (s1->as->corank != s2->as->corank)
1574 {
1575 snprintf (errmsg, err_len, "Corank mismatch in argument '%s' (%i/%i)",
1576 s1->name, s1->as->corank, s2->as->corank);
1577 return false;
1578 }
1579
1580 if (s1->as->type == AS_EXPLICIT)
1581 for (i = 0; i < s1->as->rank + MAX (0, s1->as->corank-1); i++)
1582 {
1583 shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
1584 gfc_copy_expr (s1->as->lower[i]));
1585 shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
1586 gfc_copy_expr (s2->as->lower[i]));
1587 compval = gfc_dep_compare_expr (shape1, shape2);
1588 gfc_free_expr (shape1);
1589 gfc_free_expr (shape2);
1590 switch (compval)
1591 {
1592 case -1:
1593 case 1:
1594 case -3:
1595 if (i < s1->as->rank)
1596 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of"
1597 " argument '%s'", i + 1, s1->name);
1598 else
1599 snprintf (errmsg, err_len, "Shape mismatch in codimension %i "
1600 "of argument '%s'", i - s1->as->rank + 1, s1->name);
1601 return false;
1602
1603 case -2:
1604 /* FIXME: Implement a warning for this case.
1605 gfc_warning (0, "Possible shape mismatch in argument %qs",
1606 s1->name);*/
1607 break;
1608
1609 case 0:
1610 break;
1611
1612 default:
1613 gfc_internal_error ("check_dummy_characteristics: Unexpected "
1614 "result %i of gfc_dep_compare_expr",
1615 compval);
1616 break;
1617 }
1618 }
1619 }
1620
1621 return true;
1622 }
1623
1624
1625 /* Check if the characteristics of two function results match,
1626 cf. F08:12.3.3. */
1627
1628 bool
1629 gfc_check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
1630 char *errmsg, int err_len)
1631 {
1632 gfc_symbol *r1, *r2;
1633
1634 if (s1->ts.interface && s1->ts.interface->result)
1635 r1 = s1->ts.interface->result;
1636 else
1637 r1 = s1->result ? s1->result : s1;
1638
1639 if (s2->ts.interface && s2->ts.interface->result)
1640 r2 = s2->ts.interface->result;
1641 else
1642 r2 = s2->result ? s2->result : s2;
1643
1644 if (r1->ts.type == BT_UNKNOWN)
1645 return true;
1646
1647 /* Check type and rank. */
1648 if (!compare_type_characteristics (r1, r2))
1649 {
1650 snprintf (errmsg, err_len, "Type mismatch in function result (%s/%s)",
1651 gfc_typename (&r1->ts), gfc_typename (&r2->ts));
1652 return false;
1653 }
1654 if (!compare_rank (r1, r2))
1655 {
1656 snprintf (errmsg, err_len, "Rank mismatch in function result (%i/%i)",
1657 symbol_rank (r1), symbol_rank (r2));
1658 return false;
1659 }
1660
1661 /* Check ALLOCATABLE attribute. */
1662 if (r1->attr.allocatable != r2->attr.allocatable)
1663 {
1664 snprintf (errmsg, err_len, "ALLOCATABLE attribute mismatch in "
1665 "function result");
1666 return false;
1667 }
1668
1669 /* Check POINTER attribute. */
1670 if (r1->attr.pointer != r2->attr.pointer)
1671 {
1672 snprintf (errmsg, err_len, "POINTER attribute mismatch in "
1673 "function result");
1674 return false;
1675 }
1676
1677 /* Check CONTIGUOUS attribute. */
1678 if (r1->attr.contiguous != r2->attr.contiguous)
1679 {
1680 snprintf (errmsg, err_len, "CONTIGUOUS attribute mismatch in "
1681 "function result");
1682 return false;
1683 }
1684
1685 /* Check PROCEDURE POINTER attribute. */
1686 if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
1687 {
1688 snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
1689 "function result");
1690 return false;
1691 }
1692
1693 /* Check string length. */
1694 if (r1->ts.type == BT_CHARACTER && r1->ts.u.cl && r2->ts.u.cl)
1695 {
1696 if (r1->ts.deferred != r2->ts.deferred)
1697 {
1698 snprintf (errmsg, err_len, "Character length mismatch "
1699 "in function result");
1700 return false;
1701 }
1702
1703 if (r1->ts.u.cl->length && r2->ts.u.cl->length)
1704 {
1705 int compval = gfc_dep_compare_expr (r1->ts.u.cl->length,
1706 r2->ts.u.cl->length);
1707 switch (compval)
1708 {
1709 case -1:
1710 case 1:
1711 case -3:
1712 snprintf (errmsg, err_len, "Character length mismatch "
1713 "in function result");
1714 return false;
1715
1716 case -2:
1717 if (r1->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1718 {
1719 snprintf (errmsg, err_len,
1720 "Function declared with a non-constant character "
1721 "length referenced with a constant length");
1722 return false;
1723 }
1724 else if (r2->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1725 {
1726 snprintf (errmsg, err_len,
1727 "Function declared with a constant character "
1728 "length referenced with a non-constant length");
1729 return false;
1730 }
1731 /* Warn if length expression types are different, except for
1732 possibly false positives where complex expressions might have
1733 been used. */
1734 else if ((r1->ts.u.cl->length->expr_type
1735 != r2->ts.u.cl->length->expr_type)
1736 && (r1->ts.u.cl->length->expr_type != EXPR_OP
1737 || r2->ts.u.cl->length->expr_type != EXPR_OP))
1738 gfc_warning (0, "Possible character length mismatch in "
1739 "function result between %L and %L",
1740 &r1->declared_at, &r2->declared_at);
1741 break;
1742
1743 case 0:
1744 break;
1745
1746 default:
1747 gfc_internal_error ("check_result_characteristics (1): Unexpected "
1748 "result %i of gfc_dep_compare_expr", compval);
1749 break;
1750 }
1751 }
1752 }
1753
1754 /* Check array shape. */
1755 if (!r1->attr.allocatable && !r1->attr.pointer && r1->as && r2->as)
1756 {
1757 int i, compval;
1758 gfc_expr *shape1, *shape2;
1759
1760 if (r1->as->type != r2->as->type)
1761 {
1762 snprintf (errmsg, err_len, "Shape mismatch in function result");
1763 return false;
1764 }
1765
1766 if (r1->as->type == AS_EXPLICIT)
1767 for (i = 0; i < r1->as->rank + r1->as->corank; i++)
1768 {
1769 shape1 = gfc_subtract (gfc_copy_expr (r1->as->upper[i]),
1770 gfc_copy_expr (r1->as->lower[i]));
1771 shape2 = gfc_subtract (gfc_copy_expr (r2->as->upper[i]),
1772 gfc_copy_expr (r2->as->lower[i]));
1773 compval = gfc_dep_compare_expr (shape1, shape2);
1774 gfc_free_expr (shape1);
1775 gfc_free_expr (shape2);
1776 switch (compval)
1777 {
1778 case -1:
1779 case 1:
1780 case -3:
1781 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
1782 "function result", i + 1);
1783 return false;
1784
1785 case -2:
1786 /* FIXME: Implement a warning for this case.
1787 gfc_warning (0, "Possible shape mismatch in return value");*/
1788 break;
1789
1790 case 0:
1791 break;
1792
1793 default:
1794 gfc_internal_error ("check_result_characteristics (2): "
1795 "Unexpected result %i of "
1796 "gfc_dep_compare_expr", compval);
1797 break;
1798 }
1799 }
1800 }
1801
1802 return true;
1803 }
1804
1805
1806 /* 'Compare' two formal interfaces associated with a pair of symbols.
1807 We return true if there exists an actual argument list that
1808 would be ambiguous between the two interfaces, zero otherwise.
1809 'strict_flag' specifies whether all the characteristics are
1810 required to match, which is not the case for ambiguity checks.
1811 'p1' and 'p2' are the PASS arguments of both procedures (if applicable). */
1812
1813 bool
1814 gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
1815 int generic_flag, int strict_flag,
1816 char *errmsg, int err_len,
1817 const char *p1, const char *p2,
1818 bool *bad_result_characteristics)
1819 {
1820 gfc_formal_arglist *f1, *f2;
1821
1822 gcc_assert (name2 != NULL);
1823
1824 if (bad_result_characteristics)
1825 *bad_result_characteristics = false;
1826
1827 if (s1->attr.function && (s2->attr.subroutine
1828 || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
1829 && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
1830 {
1831 if (errmsg != NULL)
1832 snprintf (errmsg, err_len, "'%s' is not a function", name2);
1833 return false;
1834 }
1835
1836 if (s1->attr.subroutine && s2->attr.function)
1837 {
1838 if (errmsg != NULL)
1839 snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
1840 return false;
1841 }
1842
1843 if (s2->attr.subroutine && s1->attr.flavor == FL_VARIABLE)
1844 {
1845 if (errmsg != NULL)
1846 snprintf (errmsg, err_len, "subroutine proc pointer '%s' passed "
1847 "to dummy variable '%s'", name2, s1->name);
1848 return false;
1849 }
1850
1851 /* Do strict checks on all characteristics
1852 (for dummy procedures and procedure pointer assignments). */
1853 if (!generic_flag && strict_flag)
1854 {
1855 if (s1->attr.function && s2->attr.function)
1856 {
1857 /* If both are functions, check result characteristics. */
1858 if (!gfc_check_result_characteristics (s1, s2, errmsg, err_len)
1859 || !gfc_check_result_characteristics (s2, s1, errmsg, err_len))
1860 {
1861 if (bad_result_characteristics)
1862 *bad_result_characteristics = true;
1863 return false;
1864 }
1865 }
1866
1867 if (s1->attr.pure && !s2->attr.pure)
1868 {
1869 snprintf (errmsg, err_len, "Mismatch in PURE attribute");
1870 return false;
1871 }
1872 if (s1->attr.elemental && !s2->attr.elemental)
1873 {
1874 snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
1875 return false;
1876 }
1877 }
1878
1879 if (s1->attr.if_source == IFSRC_UNKNOWN
1880 || s2->attr.if_source == IFSRC_UNKNOWN)
1881 return true;
1882
1883 f1 = gfc_sym_get_dummy_args (s1);
1884 f2 = gfc_sym_get_dummy_args (s2);
1885
1886 /* Special case: No arguments. */
1887 if (f1 == NULL && f2 == NULL)
1888 return true;
1889
1890 if (generic_flag)
1891 {
1892 if (count_types_test (f1, f2, p1, p2)
1893 || count_types_test (f2, f1, p2, p1))
1894 return false;
1895
1896 /* Special case: alternate returns. If both f1->sym and f2->sym are
1897 NULL, then the leading formal arguments are alternate returns.
1898 The previous conditional should catch argument lists with
1899 different number of argument. */
1900 if (f1 && f1->sym == NULL && f2 && f2->sym == NULL)
1901 return true;
1902
1903 if (generic_correspondence (f1, f2, p1, p2)
1904 || generic_correspondence (f2, f1, p2, p1))
1905 return false;
1906 }
1907 else
1908 /* Perform the abbreviated correspondence test for operators (the
1909 arguments cannot be optional and are always ordered correctly).
1910 This is also done when comparing interfaces for dummy procedures and in
1911 procedure pointer assignments. */
1912
1913 for (; f1 || f2; f1 = f1->next, f2 = f2->next)
1914 {
1915 /* Check existence. */
1916 if (f1 == NULL || f2 == NULL)
1917 {
1918 if (errmsg != NULL)
1919 snprintf (errmsg, err_len, "'%s' has the wrong number of "
1920 "arguments", name2);
1921 return false;
1922 }
1923
1924 if (strict_flag)
1925 {
1926 /* Check all characteristics. */
1927 if (!gfc_check_dummy_characteristics (f1->sym, f2->sym, true,
1928 errmsg, err_len))
1929 return false;
1930 }
1931 else
1932 {
1933 /* Operators: Only check type and rank of arguments. */
1934 if (!compare_type (f2->sym, f1->sym))
1935 {
1936 if (errmsg != NULL)
1937 snprintf (errmsg, err_len, "Type mismatch in argument '%s' "
1938 "(%s/%s)", f1->sym->name,
1939 gfc_typename (&f1->sym->ts),
1940 gfc_typename (&f2->sym->ts));
1941 return false;
1942 }
1943 if (!compare_rank (f2->sym, f1->sym))
1944 {
1945 if (errmsg != NULL)
1946 snprintf (errmsg, err_len, "Rank mismatch in argument "
1947 "'%s' (%i/%i)", f1->sym->name,
1948 symbol_rank (f1->sym), symbol_rank (f2->sym));
1949 return false;
1950 }
1951 if ((gfc_option.allow_std & GFC_STD_F2008)
1952 && (compare_ptr_alloc(f1->sym, f2->sym)
1953 || compare_ptr_alloc(f2->sym, f1->sym)))
1954 {
1955 if (errmsg != NULL)
1956 snprintf (errmsg, err_len, "Mismatching POINTER/ALLOCATABLE "
1957 "attribute in argument '%s' ", f1->sym->name);
1958 return false;
1959 }
1960 }
1961 }
1962
1963 return true;
1964 }
1965
1966
1967 /* Given a pointer to an interface pointer, remove duplicate
1968 interfaces and make sure that all symbols are either functions
1969 or subroutines, and all of the same kind. Returns true if
1970 something goes wrong. */
1971
1972 static bool
1973 check_interface0 (gfc_interface *p, const char *interface_name)
1974 {
1975 gfc_interface *psave, *q, *qlast;
1976
1977 psave = p;
1978 for (; p; p = p->next)
1979 {
1980 /* Make sure all symbols in the interface have been defined as
1981 functions or subroutines. */
1982 if (((!p->sym->attr.function && !p->sym->attr.subroutine)
1983 || !p->sym->attr.if_source)
1984 && !gfc_fl_struct (p->sym->attr.flavor))
1985 {
1986 const char *guessed
1987 = gfc_lookup_function_fuzzy (p->sym->name, p->sym->ns->sym_root);
1988
1989 if (p->sym->attr.external)
1990 if (guessed)
1991 gfc_error ("Procedure %qs in %s at %L has no explicit interface"
1992 "; did you mean %qs?",
1993 p->sym->name, interface_name, &p->sym->declared_at,
1994 guessed);
1995 else
1996 gfc_error ("Procedure %qs in %s at %L has no explicit interface",
1997 p->sym->name, interface_name, &p->sym->declared_at);
1998 else
1999 if (guessed)
2000 gfc_error ("Procedure %qs in %s at %L is neither function nor "
2001 "subroutine; did you mean %qs?", p->sym->name,
2002 interface_name, &p->sym->declared_at, guessed);
2003 else
2004 gfc_error ("Procedure %qs in %s at %L is neither function nor "
2005 "subroutine", p->sym->name, interface_name,
2006 &p->sym->declared_at);
2007 return true;
2008 }
2009
2010 /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs. */
2011 if ((psave->sym->attr.function && !p->sym->attr.function
2012 && !gfc_fl_struct (p->sym->attr.flavor))
2013 || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
2014 {
2015 if (!gfc_fl_struct (p->sym->attr.flavor))
2016 gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
2017 " or all FUNCTIONs", interface_name,
2018 &p->sym->declared_at);
2019 else if (p->sym->attr.flavor == FL_DERIVED)
2020 gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
2021 "generic name is also the name of a derived type",
2022 interface_name, &p->sym->declared_at);
2023 return true;
2024 }
2025
2026 /* F2003, C1207. F2008, C1207. */
2027 if (p->sym->attr.proc == PROC_INTERNAL
2028 && !gfc_notify_std (GFC_STD_F2008, "Internal procedure "
2029 "%qs in %s at %L", p->sym->name,
2030 interface_name, &p->sym->declared_at))
2031 return true;
2032 }
2033 p = psave;
2034
2035 /* Remove duplicate interfaces in this interface list. */
2036 for (; p; p = p->next)
2037 {
2038 qlast = p;
2039
2040 for (q = p->next; q;)
2041 {
2042 if (p->sym != q->sym)
2043 {
2044 qlast = q;
2045 q = q->next;
2046 }
2047 else
2048 {
2049 /* Duplicate interface. */
2050 qlast->next = q->next;
2051 free (q);
2052 q = qlast->next;
2053 }
2054 }
2055 }
2056
2057 return false;
2058 }
2059
2060
2061 /* Check lists of interfaces to make sure that no two interfaces are
2062 ambiguous. Duplicate interfaces (from the same symbol) are OK here. */
2063
2064 static bool
2065 check_interface1 (gfc_interface *p, gfc_interface *q0,
2066 int generic_flag, const char *interface_name,
2067 bool referenced)
2068 {
2069 gfc_interface *q;
2070 for (; p; p = p->next)
2071 for (q = q0; q; q = q->next)
2072 {
2073 if (p->sym == q->sym)
2074 continue; /* Duplicates OK here. */
2075
2076 if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
2077 continue;
2078
2079 if (!gfc_fl_struct (p->sym->attr.flavor)
2080 && !gfc_fl_struct (q->sym->attr.flavor)
2081 && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
2082 generic_flag, 0, NULL, 0, NULL, NULL))
2083 {
2084 if (referenced)
2085 gfc_error ("Ambiguous interfaces in %s for %qs at %L "
2086 "and %qs at %L", interface_name,
2087 q->sym->name, &q->sym->declared_at,
2088 p->sym->name, &p->sym->declared_at);
2089 else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
2090 gfc_warning (0, "Ambiguous interfaces in %s for %qs at %L "
2091 "and %qs at %L", interface_name,
2092 q->sym->name, &q->sym->declared_at,
2093 p->sym->name, &p->sym->declared_at);
2094 else
2095 gfc_warning (0, "Although not referenced, %qs has ambiguous "
2096 "interfaces at %L", interface_name, &p->where);
2097 return true;
2098 }
2099 }
2100 return false;
2101 }
2102
2103
2104 /* Check the generic and operator interfaces of symbols to make sure
2105 that none of the interfaces conflict. The check has to be done
2106 after all of the symbols are actually loaded. */
2107
2108 static void
2109 check_sym_interfaces (gfc_symbol *sym)
2110 {
2111 /* Provide sufficient space to hold "generic interface 'symbol.symbol'". */
2112 char interface_name[2*GFC_MAX_SYMBOL_LEN+2 + sizeof("generic interface ''")];
2113 gfc_interface *p;
2114
2115 if (sym->ns != gfc_current_ns)
2116 return;
2117
2118 if (sym->generic != NULL)
2119 {
2120 size_t len = strlen (sym->name) + sizeof("generic interface ''");
2121 gcc_assert (len < sizeof (interface_name));
2122 sprintf (interface_name, "generic interface '%s'", sym->name);
2123 if (check_interface0 (sym->generic, interface_name))
2124 return;
2125
2126 for (p = sym->generic; p; p = p->next)
2127 {
2128 if (p->sym->attr.mod_proc
2129 && !p->sym->attr.module_procedure
2130 && (p->sym->attr.if_source != IFSRC_DECL
2131 || p->sym->attr.procedure))
2132 {
2133 gfc_error ("%qs at %L is not a module procedure",
2134 p->sym->name, &p->where);
2135 return;
2136 }
2137 }
2138
2139 /* Originally, this test was applied to host interfaces too;
2140 this is incorrect since host associated symbols, from any
2141 source, cannot be ambiguous with local symbols. */
2142 check_interface1 (sym->generic, sym->generic, 1, interface_name,
2143 sym->attr.referenced || !sym->attr.use_assoc);
2144 }
2145 }
2146
2147
2148 static void
2149 check_uop_interfaces (gfc_user_op *uop)
2150 {
2151 char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("operator interface ''")];
2152 gfc_user_op *uop2;
2153 gfc_namespace *ns;
2154
2155 sprintf (interface_name, "operator interface '%s'", uop->name);
2156 if (check_interface0 (uop->op, interface_name))
2157 return;
2158
2159 for (ns = gfc_current_ns; ns; ns = ns->parent)
2160 {
2161 uop2 = gfc_find_uop (uop->name, ns);
2162 if (uop2 == NULL)
2163 continue;
2164
2165 check_interface1 (uop->op, uop2->op, 0,
2166 interface_name, true);
2167 }
2168 }
2169
2170 /* Given an intrinsic op, return an equivalent op if one exists,
2171 or INTRINSIC_NONE otherwise. */
2172
2173 gfc_intrinsic_op
2174 gfc_equivalent_op (gfc_intrinsic_op op)
2175 {
2176 switch(op)
2177 {
2178 case INTRINSIC_EQ:
2179 return INTRINSIC_EQ_OS;
2180
2181 case INTRINSIC_EQ_OS:
2182 return INTRINSIC_EQ;
2183
2184 case INTRINSIC_NE:
2185 return INTRINSIC_NE_OS;
2186
2187 case INTRINSIC_NE_OS:
2188 return INTRINSIC_NE;
2189
2190 case INTRINSIC_GT:
2191 return INTRINSIC_GT_OS;
2192
2193 case INTRINSIC_GT_OS:
2194 return INTRINSIC_GT;
2195
2196 case INTRINSIC_GE:
2197 return INTRINSIC_GE_OS;
2198
2199 case INTRINSIC_GE_OS:
2200 return INTRINSIC_GE;
2201
2202 case INTRINSIC_LT:
2203 return INTRINSIC_LT_OS;
2204
2205 case INTRINSIC_LT_OS:
2206 return INTRINSIC_LT;
2207
2208 case INTRINSIC_LE:
2209 return INTRINSIC_LE_OS;
2210
2211 case INTRINSIC_LE_OS:
2212 return INTRINSIC_LE;
2213
2214 default:
2215 return INTRINSIC_NONE;
2216 }
2217 }
2218
2219 /* For the namespace, check generic, user operator and intrinsic
2220 operator interfaces for consistency and to remove duplicate
2221 interfaces. We traverse the whole namespace, counting on the fact
2222 that most symbols will not have generic or operator interfaces. */
2223
2224 void
2225 gfc_check_interfaces (gfc_namespace *ns)
2226 {
2227 gfc_namespace *old_ns, *ns2;
2228 char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("intrinsic '' operator")];
2229 int i;
2230
2231 old_ns = gfc_current_ns;
2232 gfc_current_ns = ns;
2233
2234 gfc_traverse_ns (ns, check_sym_interfaces);
2235
2236 gfc_traverse_user_op (ns, check_uop_interfaces);
2237
2238 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2239 {
2240 if (i == INTRINSIC_USER)
2241 continue;
2242
2243 if (i == INTRINSIC_ASSIGN)
2244 strcpy (interface_name, "intrinsic assignment operator");
2245 else
2246 sprintf (interface_name, "intrinsic '%s' operator",
2247 gfc_op2string ((gfc_intrinsic_op) i));
2248
2249 if (check_interface0 (ns->op[i], interface_name))
2250 continue;
2251
2252 if (ns->op[i])
2253 gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
2254 ns->op[i]->where);
2255
2256 for (ns2 = ns; ns2; ns2 = ns2->parent)
2257 {
2258 gfc_intrinsic_op other_op;
2259
2260 if (check_interface1 (ns->op[i], ns2->op[i], 0,
2261 interface_name, true))
2262 goto done;
2263
2264 /* i should be gfc_intrinsic_op, but has to be int with this cast
2265 here for stupid C++ compatibility rules. */
2266 other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
2267 if (other_op != INTRINSIC_NONE
2268 && check_interface1 (ns->op[i], ns2->op[other_op],
2269 0, interface_name, true))
2270 goto done;
2271 }
2272 }
2273
2274 done:
2275 gfc_current_ns = old_ns;
2276 }
2277
2278
2279 /* Given a symbol of a formal argument list and an expression, if the
2280 formal argument is allocatable, check that the actual argument is
2281 allocatable. Returns true if compatible, zero if not compatible. */
2282
2283 static bool
2284 compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
2285 {
2286 if (formal->attr.allocatable
2287 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
2288 {
2289 symbol_attribute attr = gfc_expr_attr (actual);
2290 if (actual->ts.type == BT_CLASS && !attr.class_ok)
2291 return true;
2292 else if (!attr.allocatable)
2293 return false;
2294 }
2295
2296 return true;
2297 }
2298
2299
2300 /* Given a symbol of a formal argument list and an expression, if the
2301 formal argument is a pointer, see if the actual argument is a
2302 pointer. Returns nonzero if compatible, zero if not compatible. */
2303
2304 static int
2305 compare_pointer (gfc_symbol *formal, gfc_expr *actual)
2306 {
2307 symbol_attribute attr;
2308
2309 if (formal->attr.pointer
2310 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)
2311 && CLASS_DATA (formal)->attr.class_pointer))
2312 {
2313 attr = gfc_expr_attr (actual);
2314
2315 /* Fortran 2008 allows non-pointer actual arguments. */
2316 if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
2317 return 2;
2318
2319 if (!attr.pointer)
2320 return 0;
2321 }
2322
2323 return 1;
2324 }
2325
2326
2327 /* Emit clear error messages for rank mismatch. */
2328
2329 static void
2330 argument_rank_mismatch (const char *name, locus *where,
2331 int rank1, int rank2, locus *where_formal)
2332 {
2333
2334 /* TS 29113, C407b. */
2335 if (where_formal == NULL)
2336 {
2337 if (rank2 == -1)
2338 gfc_error ("The assumed-rank array at %L requires that the dummy "
2339 "argument %qs has assumed-rank", where, name);
2340 else if (rank1 == 0)
2341 gfc_error_opt (0, "Rank mismatch in argument %qs "
2342 "at %L (scalar and rank-%d)", name, where, rank2);
2343 else if (rank2 == 0)
2344 gfc_error_opt (0, "Rank mismatch in argument %qs "
2345 "at %L (rank-%d and scalar)", name, where, rank1);
2346 else
2347 gfc_error_opt (0, "Rank mismatch in argument %qs "
2348 "at %L (rank-%d and rank-%d)", name, where, rank1,
2349 rank2);
2350 }
2351 else
2352 {
2353 if (rank2 == -1)
2354 /* This is an assumed rank-actual passed to a function without
2355 an explicit interface, which is already diagnosed in
2356 gfc_procedure_use. */
2357 return;
2358 if (rank1 == 0)
2359 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2360 "and actual argument at %L (scalar and rank-%d)",
2361 where, where_formal, rank2);
2362 else if (rank2 == 0)
2363 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2364 "and actual argument at %L (rank-%d and scalar)",
2365 where, where_formal, rank1);
2366 else
2367 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2368 "and actual argument at %L (rank-%d and rank-%d)", where,
2369 where_formal, rank1, rank2);
2370 }
2371 }
2372
2373
2374 /* Under certain conditions, a scalar actual argument can be passed
2375 to an array dummy argument - see F2018, 15.5.2.4, paragraph 14.
2376 This function returns true for these conditions so that an error
2377 or warning for this can be suppressed later. Always return false
2378 for expressions with rank > 0. */
2379
2380 bool
2381 maybe_dummy_array_arg (gfc_expr *e)
2382 {
2383 gfc_symbol *s;
2384 gfc_ref *ref;
2385 bool array_pointer = false;
2386 bool assumed_shape = false;
2387 bool scalar_ref = true;
2388
2389 if (e->rank > 0)
2390 return false;
2391
2392 if (e->ts.type == BT_CHARACTER && e->ts.kind == 1)
2393 return true;
2394
2395 /* If this comes from a constructor, it has been an array element
2396 originally. */
2397
2398 if (e->expr_type == EXPR_CONSTANT)
2399 return e->from_constructor;
2400
2401 if (e->expr_type != EXPR_VARIABLE)
2402 return false;
2403
2404 s = e->symtree->n.sym;
2405
2406 if (s->attr.dimension)
2407 {
2408 scalar_ref = false;
2409 array_pointer = s->attr.pointer;
2410 }
2411
2412 if (s->as && s->as->type == AS_ASSUMED_SHAPE)
2413 assumed_shape = true;
2414
2415 for (ref=e->ref; ref; ref=ref->next)
2416 {
2417 if (ref->type == REF_COMPONENT)
2418 {
2419 symbol_attribute *attr;
2420 attr = &ref->u.c.component->attr;
2421 if (attr->dimension)
2422 {
2423 array_pointer = attr->pointer;
2424 assumed_shape = false;
2425 scalar_ref = false;
2426 }
2427 else
2428 scalar_ref = true;
2429 }
2430 }
2431
2432 return !(scalar_ref || array_pointer || assumed_shape);
2433 }
2434
2435 /* Given a symbol of a formal argument list and an expression, see if
2436 the two are compatible as arguments. Returns true if
2437 compatible, false if not compatible. */
2438
2439 static bool
2440 compare_parameter (gfc_symbol *formal, gfc_expr *actual,
2441 int ranks_must_agree, int is_elemental, locus *where)
2442 {
2443 gfc_ref *ref;
2444 bool rank_check, is_pointer;
2445 char err[200];
2446 gfc_component *ppc;
2447 bool codimension = false;
2448 gfc_array_spec *formal_as;
2449 const char *actual_name;
2450
2451 /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
2452 procs c_f_pointer or c_f_procpointer, and we need to accept most
2453 pointers the user could give us. This should allow that. */
2454 if (formal->ts.type == BT_VOID)
2455 return true;
2456
2457 if (formal->ts.type == BT_DERIVED
2458 && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
2459 && actual->ts.type == BT_DERIVED
2460 && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
2461 {
2462 if (formal->ts.u.derived->intmod_sym_id
2463 != actual->ts.u.derived->intmod_sym_id)
2464 return false;
2465
2466 if (ranks_must_agree
2467 && symbol_rank (formal) != actual->rank
2468 && symbol_rank (formal) != -1)
2469 {
2470 if (where)
2471 argument_rank_mismatch (formal->name, &actual->where,
2472 symbol_rank (formal), actual->rank,
2473 NULL);
2474 return false;
2475 }
2476 return true;
2477 }
2478
2479 if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
2480 /* Make sure the vtab symbol is present when
2481 the module variables are generated. */
2482 gfc_find_derived_vtab (actual->ts.u.derived);
2483
2484 if (actual->ts.type == BT_PROCEDURE)
2485 {
2486 gfc_symbol *act_sym = actual->symtree->n.sym;
2487
2488 if (formal->attr.flavor != FL_PROCEDURE && !act_sym->ts.interface)
2489 {
2490 if (where)
2491 gfc_error ("Invalid procedure argument at %L", &actual->where);
2492 return false;
2493 }
2494 else if (act_sym->ts.interface
2495 && !gfc_compare_interfaces (formal, act_sym->ts.interface,
2496 act_sym->name, 0, 1, err,
2497 sizeof(err),NULL, NULL))
2498 {
2499 if (where)
2500 {
2501 /* Artificially generated symbol names would only confuse. */
2502 if (formal->attr.artificial)
2503 gfc_error_opt (0, "Interface mismatch in dummy procedure "
2504 "at %L conflicts with %L: %s", &actual->where,
2505 &formal->declared_at, err);
2506 else
2507 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs "
2508 "at %L: %s", formal->name, &actual->where, err);
2509 }
2510 return false;
2511 }
2512
2513 if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
2514 sizeof(err), NULL, NULL))
2515 {
2516 if (where)
2517 {
2518 if (formal->attr.artificial)
2519 gfc_error_opt (0, "Interface mismatch in dummy procedure "
2520 "at %L conflicts with %L: %s", &actual->where,
2521 &formal->declared_at, err);
2522 else
2523 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at "
2524 "%L: %s", formal->name, &actual->where, err);
2525
2526 }
2527 return false;
2528 }
2529
2530 /* The actual symbol may disagree with a global symbol. If so, issue an
2531 error, but only if no previous error has been reported on the formal
2532 argument. */
2533 actual_name = act_sym->name;
2534 if (!formal->error && actual_name)
2535 {
2536 gfc_gsymbol *gsym;
2537 gsym = gfc_find_gsymbol (gfc_gsym_root, actual_name);
2538 if (gsym != NULL)
2539 {
2540 if (gsym->type == GSYM_SUBROUTINE && formal->attr.function)
2541 {
2542 gfc_error ("Passing global subroutine %qs declared at %L "
2543 "as function at %L", actual_name, &gsym->where,
2544 &actual->where);
2545 return false;
2546 }
2547 if (gsym->type == GSYM_FUNCTION && formal->attr.subroutine)
2548 {
2549 gfc_error ("Passing global function %qs declared at %L "
2550 "as subroutine at %L", actual_name, &gsym->where,
2551 &actual->where);
2552 return false;
2553 }
2554 if (gsym->type == GSYM_FUNCTION)
2555 {
2556 gfc_symbol *global_asym;
2557 gfc_find_symbol (actual_name, gsym->ns, 0, &global_asym);
2558 if (global_asym != NULL)
2559 {
2560 if (formal->attr.subroutine)
2561 {
2562 gfc_error ("Mismatch between subroutine and "
2563 "function at %L", &actual->where);
2564 return false;
2565 }
2566 else if (formal->attr.function)
2567 {
2568 gfc_typespec ts;
2569
2570 if (global_asym->result)
2571 ts = global_asym->result->ts;
2572 else
2573 ts = global_asym->ts;
2574
2575 if (!gfc_compare_types (&ts,
2576 &formal->ts))
2577 {
2578 gfc_error ("Type mismatch at %L passing global "
2579 "function %qs declared at %L (%s/%s)",
2580 &actual->where, actual_name,
2581 &gsym->where,
2582 gfc_typename (&global_asym->ts),
2583 gfc_dummy_typename (&formal->ts));
2584 return false;
2585 }
2586 }
2587 else
2588 {
2589 /* The global symbol is a function. Set the formal
2590 argument acordingly. */
2591 formal->attr.function = 1;
2592 formal->ts = global_asym->ts;
2593 }
2594 }
2595 }
2596 }
2597 }
2598
2599 if (formal->attr.function && !act_sym->attr.function)
2600 {
2601 gfc_add_function (&act_sym->attr, act_sym->name,
2602 &act_sym->declared_at);
2603 if (act_sym->ts.type == BT_UNKNOWN
2604 && !gfc_set_default_type (act_sym, 1, act_sym->ns))
2605 return false;
2606 }
2607 else if (formal->attr.subroutine && !act_sym->attr.subroutine)
2608 gfc_add_subroutine (&act_sym->attr, act_sym->name,
2609 &act_sym->declared_at);
2610
2611 return true;
2612 }
2613 ppc = gfc_get_proc_ptr_comp (actual);
2614 if (ppc && ppc->ts.interface)
2615 {
2616 if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
2617 err, sizeof(err), NULL, NULL))
2618 {
2619 if (where)
2620 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2621 " %s", formal->name, &actual->where, err);
2622 return false;
2623 }
2624 }
2625
2626 /* F2008, C1241. */
2627 if (formal->attr.pointer && formal->attr.contiguous
2628 && !gfc_is_simply_contiguous (actual, true, false))
2629 {
2630 if (where)
2631 gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
2632 "must be simply contiguous", formal->name, &actual->where);
2633 return false;
2634 }
2635
2636 symbol_attribute actual_attr = gfc_expr_attr (actual);
2637 if (actual->ts.type == BT_CLASS && !actual_attr.class_ok)
2638 return true;
2639
2640 if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
2641 && actual->ts.type != BT_HOLLERITH
2642 && formal->ts.type != BT_ASSUMED
2643 && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2644 && !gfc_compare_types (&formal->ts, &actual->ts)
2645 && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
2646 && gfc_compare_derived_types (formal->ts.u.derived,
2647 CLASS_DATA (actual)->ts.u.derived)))
2648 {
2649 if (where)
2650 {
2651 if (formal->attr.artificial)
2652 {
2653 if (!flag_allow_argument_mismatch || !formal->error)
2654 gfc_error_opt (0, "Type mismatch between actual argument at %L "
2655 "and actual argument at %L (%s/%s).",
2656 &actual->where,
2657 &formal->declared_at,
2658 gfc_typename (actual),
2659 gfc_dummy_typename (&formal->ts));
2660
2661 formal->error = 1;
2662 }
2663 else
2664 gfc_error_opt (0, "Type mismatch in argument %qs at %L; passed %s "
2665 "to %s", formal->name, where, gfc_typename (actual),
2666 gfc_dummy_typename (&formal->ts));
2667 }
2668 return false;
2669 }
2670
2671 if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
2672 {
2673 if (where)
2674 gfc_error ("Assumed-type actual argument at %L requires that dummy "
2675 "argument %qs is of assumed type", &actual->where,
2676 formal->name);
2677 return false;
2678 }
2679
2680 /* TS29113 C407c; F2018 C711. */
2681 if (actual->ts.type == BT_ASSUMED
2682 && symbol_rank (formal) == -1
2683 && actual->rank != -1
2684 && !(actual->symtree->n.sym->as
2685 && actual->symtree->n.sym->as->type == AS_ASSUMED_SHAPE))
2686 {
2687 if (where)
2688 gfc_error ("Assumed-type actual argument at %L corresponding to "
2689 "assumed-rank dummy argument %qs must be "
2690 "assumed-shape or assumed-rank",
2691 &actual->where, formal->name);
2692 return false;
2693 }
2694
2695 /* F2008, 12.5.2.5; IR F08/0073. */
2696 if (formal->ts.type == BT_CLASS && formal->attr.class_ok
2697 && actual->expr_type != EXPR_NULL
2698 && ((CLASS_DATA (formal)->attr.class_pointer
2699 && formal->attr.intent != INTENT_IN)
2700 || CLASS_DATA (formal)->attr.allocatable))
2701 {
2702 if (actual->ts.type != BT_CLASS)
2703 {
2704 if (where)
2705 gfc_error ("Actual argument to %qs at %L must be polymorphic",
2706 formal->name, &actual->where);
2707 return false;
2708 }
2709
2710 if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
2711 && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
2712 CLASS_DATA (formal)->ts.u.derived))
2713 {
2714 if (where)
2715 gfc_error ("Actual argument to %qs at %L must have the same "
2716 "declared type", formal->name, &actual->where);
2717 return false;
2718 }
2719 }
2720
2721 /* F08: 12.5.2.5 Allocatable and pointer dummy variables. However, this
2722 is necessary also for F03, so retain error for both.
2723 NOTE: Other type/kind errors pre-empt this error. Since they are F03
2724 compatible, no attempt has been made to channel to this one. */
2725 if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
2726 && (CLASS_DATA (formal)->attr.allocatable
2727 ||CLASS_DATA (formal)->attr.class_pointer))
2728 {
2729 if (where)
2730 gfc_error ("Actual argument to %qs at %L must be unlimited "
2731 "polymorphic since the formal argument is a "
2732 "pointer or allocatable unlimited polymorphic "
2733 "entity [F2008: 12.5.2.5]", formal->name,
2734 &actual->where);
2735 return false;
2736 }
2737
2738 if (formal->ts.type == BT_CLASS && formal->attr.class_ok)
2739 codimension = CLASS_DATA (formal)->attr.codimension;
2740 else
2741 codimension = formal->attr.codimension;
2742
2743 if (codimension && !gfc_is_coarray (actual))
2744 {
2745 if (where)
2746 gfc_error ("Actual argument to %qs at %L must be a coarray",
2747 formal->name, &actual->where);
2748 return false;
2749 }
2750
2751 formal_as = (formal->ts.type == BT_CLASS
2752 ? CLASS_DATA (formal)->as : formal->as);
2753
2754 if (codimension && formal->attr.allocatable)
2755 {
2756 gfc_ref *last = NULL;
2757
2758 for (ref = actual->ref; ref; ref = ref->next)
2759 if (ref->type == REF_COMPONENT)
2760 last = ref;
2761
2762 /* F2008, 12.5.2.6. */
2763 if ((last && last->u.c.component->as->corank != formal->as->corank)
2764 || (!last
2765 && actual->symtree->n.sym->as->corank != formal->as->corank))
2766 {
2767 if (where)
2768 gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
2769 formal->name, &actual->where, formal->as->corank,
2770 last ? last->u.c.component->as->corank
2771 : actual->symtree->n.sym->as->corank);
2772 return false;
2773 }
2774 }
2775
2776 if (codimension)
2777 {
2778 /* F2008, 12.5.2.8 + Corrig 2 (IR F08/0048). */
2779 /* F2018, 12.5.2.8. */
2780 if (formal->attr.dimension
2781 && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
2782 && actual_attr.dimension
2783 && !gfc_is_simply_contiguous (actual, true, true))
2784 {
2785 if (where)
2786 gfc_error ("Actual argument to %qs at %L must be simply "
2787 "contiguous or an element of such an array",
2788 formal->name, &actual->where);
2789 return false;
2790 }
2791
2792 /* F2008, C1303 and C1304. */
2793 if (formal->attr.intent != INTENT_INOUT
2794 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2795 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2796 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2797 || formal->attr.lock_comp))
2798
2799 {
2800 if (where)
2801 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2802 "which is LOCK_TYPE or has a LOCK_TYPE component",
2803 formal->name, &actual->where);
2804 return false;
2805 }
2806
2807 /* TS18508, C702/C703. */
2808 if (formal->attr.intent != INTENT_INOUT
2809 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2810 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2811 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2812 || formal->attr.event_comp))
2813
2814 {
2815 if (where)
2816 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2817 "which is EVENT_TYPE or has a EVENT_TYPE component",
2818 formal->name, &actual->where);
2819 return false;
2820 }
2821 }
2822
2823 /* F2008, C1239/C1240. */
2824 if (actual->expr_type == EXPR_VARIABLE
2825 && (actual->symtree->n.sym->attr.asynchronous
2826 || actual->symtree->n.sym->attr.volatile_)
2827 && (formal->attr.asynchronous || formal->attr.volatile_)
2828 && actual->rank && formal->as
2829 && !gfc_is_simply_contiguous (actual, true, false)
2830 && ((formal->as->type != AS_ASSUMED_SHAPE
2831 && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
2832 || formal->attr.contiguous))
2833 {
2834 if (where)
2835 gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
2836 "assumed-rank array without CONTIGUOUS attribute - as actual"
2837 " argument at %L is not simply contiguous and both are "
2838 "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
2839 return false;
2840 }
2841
2842 if (formal->attr.allocatable && !codimension
2843 && actual_attr.codimension)
2844 {
2845 if (formal->attr.intent == INTENT_OUT)
2846 {
2847 if (where)
2848 gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
2849 "INTENT(OUT) dummy argument %qs", &actual->where,
2850 formal->name);
2851 return false;
2852 }
2853 else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
2854 gfc_warning (OPT_Wsurprising,
2855 "Passing coarray at %L to allocatable, noncoarray dummy "
2856 "argument %qs, which is invalid if the allocation status"
2857 " is modified", &actual->where, formal->name);
2858 }
2859
2860 /* If the rank is the same or the formal argument has assumed-rank. */
2861 if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
2862 return true;
2863
2864 rank_check = where != NULL && !is_elemental && formal_as
2865 && (formal_as->type == AS_ASSUMED_SHAPE
2866 || formal_as->type == AS_DEFERRED)
2867 && !(actual->expr_type == EXPR_NULL
2868 && actual->ts.type == BT_UNKNOWN);
2869
2870 /* Skip rank checks for NO_ARG_CHECK. */
2871 if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2872 return true;
2873
2874 /* Scalar & coindexed, see: F2008, Section 12.5.2.4. */
2875 if (rank_check || ranks_must_agree
2876 || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
2877 || (actual->rank != 0
2878 && !(is_elemental || formal->attr.dimension
2879 || (formal->ts.type == BT_CLASS
2880 && CLASS_DATA (formal)->attr.dimension)))
2881 || (actual->rank == 0
2882 && ((formal->ts.type == BT_CLASS
2883 && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
2884 || (formal->ts.type != BT_CLASS
2885 && formal->as->type == AS_ASSUMED_SHAPE))
2886 && actual->expr_type != EXPR_NULL)
2887 || (actual->rank == 0
2888 && (formal->attr.dimension
2889 || (formal->ts.type == BT_CLASS
2890 && CLASS_DATA (formal)->attr.dimension))
2891 && gfc_is_coindexed (actual))
2892 /* Assumed-rank actual argument; F2018 C838. */
2893 || actual->rank == -1)
2894 {
2895 if (where
2896 && (!formal->attr.artificial || (!formal->maybe_array
2897 && !maybe_dummy_array_arg (actual))))
2898 {
2899 locus *where_formal;
2900 if (formal->attr.artificial)
2901 where_formal = &formal->declared_at;
2902 else
2903 where_formal = NULL;
2904
2905 argument_rank_mismatch (formal->name, &actual->where,
2906 symbol_rank (formal), actual->rank,
2907 where_formal);
2908 }
2909 return false;
2910 }
2911 else if (actual->rank != 0
2912 && (is_elemental || formal->attr.dimension
2913 || (formal->ts.type == BT_CLASS
2914 && CLASS_DATA (formal)->attr.dimension)))
2915 return true;
2916
2917 /* At this point, we are considering a scalar passed to an array. This
2918 is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
2919 - if the actual argument is (a substring of) an element of a
2920 non-assumed-shape/non-pointer/non-polymorphic array; or
2921 - (F2003) if the actual argument is of type character of default/c_char
2922 kind.
2923 - (F2018) if the dummy argument is type(*). */
2924
2925 is_pointer = actual->expr_type == EXPR_VARIABLE
2926 ? actual->symtree->n.sym->attr.pointer : false;
2927
2928 for (ref = actual->ref; ref; ref = ref->next)
2929 {
2930 if (ref->type == REF_COMPONENT)
2931 is_pointer = ref->u.c.component->attr.pointer;
2932 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2933 && ref->u.ar.dimen > 0
2934 && (!ref->next
2935 || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
2936 break;
2937 }
2938
2939 if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
2940 {
2941 if (where)
2942 gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
2943 "at %L", formal->name, &actual->where);
2944 return false;
2945 }
2946
2947 if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
2948 && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2949 {
2950 if (where)
2951 {
2952 if (formal->attr.artificial)
2953 gfc_error ("Element of assumed-shape or pointer array "
2954 "as actual argument at %L cannot correspond to "
2955 "actual argument at %L",
2956 &actual->where, &formal->declared_at);
2957 else
2958 gfc_error ("Element of assumed-shape or pointer "
2959 "array passed to array dummy argument %qs at %L",
2960 formal->name, &actual->where);
2961 }
2962 return false;
2963 }
2964
2965 if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
2966 && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2967 {
2968 if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
2969 {
2970 if (where)
2971 gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
2972 "CHARACTER actual argument with array dummy argument "
2973 "%qs at %L", formal->name, &actual->where);
2974 return false;
2975 }
2976
2977 if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
2978 {
2979 gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
2980 "array dummy argument %qs at %L",
2981 formal->name, &actual->where);
2982 return false;
2983 }
2984 else
2985 return ((gfc_option.allow_std & GFC_STD_F2003) != 0);
2986 }
2987
2988 if (ref == NULL && actual->expr_type != EXPR_NULL)
2989 {
2990 if (actual->rank == 0
2991 && formal->ts.type == BT_ASSUMED
2992 && formal->as
2993 && formal->as->type == AS_ASSUMED_SIZE)
2994 /* This is new in F2018, type(*) is new in TS29113, but gfortran does
2995 not differentiate. Thus, if type(*) exists, it is valid;
2996 otherwise, type(*) is already rejected. */
2997 return true;
2998 if (where
2999 && (!formal->attr.artificial || (!formal->maybe_array
3000 && !maybe_dummy_array_arg (actual))))
3001 {
3002 locus *where_formal;
3003 if (formal->attr.artificial)
3004 where_formal = &formal->declared_at;
3005 else
3006 where_formal = NULL;
3007
3008 argument_rank_mismatch (formal->name, &actual->where,
3009 symbol_rank (formal), actual->rank,
3010 where_formal);
3011 }
3012 return false;
3013 }
3014
3015 return true;
3016 }
3017
3018
3019 /* Returns the storage size of a symbol (formal argument) or
3020 zero if it cannot be determined. */
3021
3022 static unsigned long
3023 get_sym_storage_size (gfc_symbol *sym)
3024 {
3025 int i;
3026 unsigned long strlen, elements;
3027
3028 if (sym->ts.type == BT_CHARACTER)
3029 {
3030 if (sym->ts.u.cl && sym->ts.u.cl->length
3031 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
3032 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
3033 strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
3034 else
3035 return 0;
3036 }
3037 else
3038 strlen = 1;
3039
3040 if (symbol_rank (sym) == 0)
3041 return strlen;
3042
3043 elements = 1;
3044 if (sym->as->type != AS_EXPLICIT)
3045 return 0;
3046 for (i = 0; i < sym->as->rank; i++)
3047 {
3048 if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
3049 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
3050 || sym->as->upper[i]->ts.type != BT_INTEGER
3051 || sym->as->lower[i]->ts.type != BT_INTEGER)
3052 return 0;
3053
3054 elements *= mpz_get_si (sym->as->upper[i]->value.integer)
3055 - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
3056 }
3057
3058 return strlen*elements;
3059 }
3060
3061
3062 /* Returns the storage size of an expression (actual argument) or
3063 zero if it cannot be determined. For an array element, it returns
3064 the remaining size as the element sequence consists of all storage
3065 units of the actual argument up to the end of the array. */
3066
3067 static unsigned long
3068 get_expr_storage_size (gfc_expr *e)
3069 {
3070 int i;
3071 long int strlen, elements;
3072 long int substrlen = 0;
3073 bool is_str_storage = false;
3074 gfc_ref *ref;
3075
3076 if (e == NULL)
3077 return 0;
3078
3079 if (e->ts.type == BT_CHARACTER)
3080 {
3081 if (e->ts.u.cl && e->ts.u.cl->length
3082 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
3083 && e->ts.u.cl->length->ts.type == BT_INTEGER)
3084 strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
3085 else if (e->expr_type == EXPR_CONSTANT
3086 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
3087 strlen = e->value.character.length;
3088 else
3089 return 0;
3090 }
3091 else
3092 strlen = 1; /* Length per element. */
3093
3094 if (e->rank == 0 && !e->ref)
3095 return strlen;
3096
3097 elements = 1;
3098 if (!e->ref)
3099 {
3100 if (!e->shape)
3101 return 0;
3102 for (i = 0; i < e->rank; i++)
3103 elements *= mpz_get_si (e->shape[i]);
3104 return elements*strlen;
3105 }
3106
3107 for (ref = e->ref; ref; ref = ref->next)
3108 {
3109 if (ref->type == REF_SUBSTRING && ref->u.ss.start
3110 && ref->u.ss.start->expr_type == EXPR_CONSTANT)
3111 {
3112 if (is_str_storage)
3113 {
3114 /* The string length is the substring length.
3115 Set now to full string length. */
3116 if (!ref->u.ss.length || !ref->u.ss.length->length
3117 || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
3118 return 0;
3119
3120 strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
3121 }
3122 substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
3123 continue;
3124 }
3125
3126 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3127 for (i = 0; i < ref->u.ar.dimen; i++)
3128 {
3129 long int start, end, stride;
3130 stride = 1;
3131
3132 if (ref->u.ar.stride[i])
3133 {
3134 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT
3135 && ref->u.ar.stride[i]->ts.type == BT_INTEGER)
3136 stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
3137 else
3138 return 0;
3139 }
3140
3141 if (ref->u.ar.start[i])
3142 {
3143 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT
3144 && ref->u.ar.start[i]->ts.type == BT_INTEGER)
3145 start = mpz_get_si (ref->u.ar.start[i]->value.integer);
3146 else
3147 return 0;
3148 }
3149 else if (ref->u.ar.as->lower[i]
3150 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
3151 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER)
3152 start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
3153 else
3154 return 0;
3155
3156 if (ref->u.ar.end[i])
3157 {
3158 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT
3159 && ref->u.ar.end[i]->ts.type == BT_INTEGER)
3160 end = mpz_get_si (ref->u.ar.end[i]->value.integer);
3161 else
3162 return 0;
3163 }
3164 else if (ref->u.ar.as->upper[i]
3165 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
3166 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
3167 end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
3168 else
3169 return 0;
3170
3171 elements *= (end - start)/stride + 1L;
3172 }
3173 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
3174 for (i = 0; i < ref->u.ar.as->rank; i++)
3175 {
3176 if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
3177 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
3178 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER
3179 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
3180 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
3181 elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
3182 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
3183 + 1L;
3184 else
3185 return 0;
3186 }
3187 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
3188 && e->expr_type == EXPR_VARIABLE)
3189 {
3190 if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
3191 || e->symtree->n.sym->attr.pointer)
3192 {
3193 elements = 1;
3194 continue;
3195 }
3196
3197 /* Determine the number of remaining elements in the element
3198 sequence for array element designators. */
3199 is_str_storage = true;
3200 for (i = ref->u.ar.dimen - 1; i >= 0; i--)
3201 {
3202 if (ref->u.ar.start[i] == NULL
3203 || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
3204 || ref->u.ar.as->upper[i] == NULL
3205 || ref->u.ar.as->lower[i] == NULL
3206 || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
3207 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT
3208 || ref->u.ar.as->upper[i]->ts.type != BT_INTEGER
3209 || ref->u.ar.as->lower[i]->ts.type != BT_INTEGER)
3210 return 0;
3211
3212 elements
3213 = elements
3214 * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
3215 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
3216 + 1L)
3217 - (mpz_get_si (ref->u.ar.start[i]->value.integer)
3218 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
3219 }
3220 }
3221 else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
3222 && ref->u.c.component->attr.proc_pointer
3223 && ref->u.c.component->attr.dimension)
3224 {
3225 /* Array-valued procedure-pointer components. */
3226 gfc_array_spec *as = ref->u.c.component->as;
3227 for (i = 0; i < as->rank; i++)
3228 {
3229 if (!as->upper[i] || !as->lower[i]
3230 || as->upper[i]->expr_type != EXPR_CONSTANT
3231 || as->lower[i]->expr_type != EXPR_CONSTANT
3232 || as->upper[i]->ts.type != BT_INTEGER
3233 || as->lower[i]->ts.type != BT_INTEGER)
3234 return 0;
3235
3236 elements = elements
3237 * (mpz_get_si (as->upper[i]->value.integer)
3238 - mpz_get_si (as->lower[i]->value.integer) + 1L);
3239 }
3240 }
3241 }
3242
3243 if (substrlen)
3244 return (is_str_storage) ? substrlen + (elements-1)*strlen
3245 : elements*strlen;
3246 else
3247 return elements*strlen;
3248 }
3249
3250
3251 /* Given an expression, check whether it is an array section
3252 which has a vector subscript. */
3253
3254 bool
3255 gfc_has_vector_subscript (gfc_expr *e)
3256 {
3257 int i;
3258 gfc_ref *ref;
3259
3260 if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
3261 return false;
3262
3263 for (ref = e->ref; ref; ref = ref->next)
3264 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3265 for (i = 0; i < ref->u.ar.dimen; i++)
3266 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
3267 return true;
3268
3269 return false;
3270 }
3271
3272
3273 static bool
3274 is_procptr_result (gfc_expr *expr)
3275 {
3276 gfc_component *c = gfc_get_proc_ptr_comp (expr);
3277 if (c)
3278 return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
3279 else
3280 return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
3281 && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
3282 }
3283
3284
3285 /* Recursively append candidate argument ARG to CANDIDATES. Store the
3286 number of total candidates in CANDIDATES_LEN. */
3287
3288 static void
3289 lookup_arg_fuzzy_find_candidates (gfc_formal_arglist *arg,
3290 char **&candidates,
3291 size_t &candidates_len)
3292 {
3293 for (gfc_formal_arglist *p = arg; p && p->sym; p = p->next)
3294 vec_push (candidates, candidates_len, p->sym->name);
3295 }
3296
3297
3298 /* Lookup argument ARG fuzzily, taking names in ARGUMENTS into account. */
3299
3300 static const char*
3301 lookup_arg_fuzzy (const char *arg, gfc_formal_arglist *arguments)
3302 {
3303 char **candidates = NULL;
3304 size_t candidates_len = 0;
3305 lookup_arg_fuzzy_find_candidates (arguments, candidates, candidates_len);
3306 return gfc_closest_fuzzy_match (arg, candidates);
3307 }
3308
3309
3310 static gfc_dummy_arg *
3311 get_nonintrinsic_dummy_arg (gfc_formal_arglist *formal)
3312 {
3313 gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
3314
3315 dummy_arg->intrinsicness = GFC_NON_INTRINSIC_DUMMY_ARG;
3316 dummy_arg->u.non_intrinsic = formal;
3317
3318 return dummy_arg;
3319 }
3320
3321
3322 /* Given formal and actual argument lists, see if they are compatible.
3323 If they are compatible, the actual argument list is sorted to
3324 correspond with the formal list, and elements for missing optional
3325 arguments are inserted. If WHERE pointer is nonnull, then we issue
3326 errors when things don't match instead of just returning the status
3327 code. */
3328
3329 bool
3330 gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
3331 int ranks_must_agree, int is_elemental,
3332 bool in_statement_function, locus *where)
3333 {
3334 gfc_actual_arglist **new_arg, *a, *actual;
3335 gfc_formal_arglist *f;
3336 int i, n, na;
3337 unsigned long actual_size, formal_size;
3338 bool full_array = false;
3339 gfc_array_ref *actual_arr_ref;
3340 gfc_array_spec *fas, *aas;
3341 bool pointer_dummy, pointer_arg, allocatable_arg;
3342 bool procptr_dummy, optional_dummy, allocatable_dummy;
3343
3344 bool ok = true;
3345
3346 actual = *ap;
3347
3348 if (actual == NULL && formal == NULL)
3349 return true;
3350
3351 n = 0;
3352 for (f = formal; f; f = f->next)
3353 n++;
3354
3355 new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
3356
3357 for (i = 0; i < n; i++)
3358 new_arg[i] = NULL;
3359
3360 na = 0;
3361 f = formal;
3362 i = 0;
3363
3364 for (a = actual; a; a = a->next, f = f->next)
3365 {
3366 if (a->name != NULL && in_statement_function)
3367 {
3368 gfc_error ("Keyword argument %qs at %L is invalid in "
3369 "a statement function", a->name, &a->expr->where);
3370 return false;
3371 }
3372
3373 /* Look for keywords but ignore g77 extensions like %VAL. */
3374 if (a->name != NULL && a->name[0] != '%')
3375 {
3376 i = 0;
3377 for (f = formal; f; f = f->next, i++)
3378 {
3379 if (f->sym == NULL)
3380 continue;
3381 if (strcmp (f->sym->name, a->name) == 0)
3382 break;
3383 }
3384
3385 if (f == NULL)
3386 {
3387 if (where)
3388 {
3389 const char *guessed = lookup_arg_fuzzy (a->name, formal);
3390 if (guessed)
3391 gfc_error ("Keyword argument %qs at %L is not in "
3392 "the procedure; did you mean %qs?",
3393 a->name, &a->expr->where, guessed);
3394 else
3395 gfc_error ("Keyword argument %qs at %L is not in "
3396 "the procedure", a->name, &a->expr->where);
3397 }
3398 return false;
3399 }
3400
3401 if (new_arg[i] != NULL)
3402 {
3403 if (where)
3404 gfc_error ("Keyword argument %qs at %L is already associated "
3405 "with another actual argument", a->name,
3406 &a->expr->where);
3407 return false;
3408 }
3409 }
3410
3411 if (f == NULL)
3412 {
3413 if (where)
3414 gfc_error ("More actual than formal arguments in procedure "
3415 "call at %L", where);
3416 return false;
3417 }
3418
3419 if (f->sym == NULL && a->expr == NULL)
3420 goto match;
3421
3422 if (f->sym == NULL)
3423 {
3424 /* These errors have to be issued, otherwise an ICE can occur.
3425 See PR 78865. */
3426 if (where)
3427 gfc_error_now ("Missing alternate return specifier in subroutine "
3428 "call at %L", where);
3429 return false;
3430 }
3431 else
3432 {
3433 if (a->associated_dummy)
3434 free (a->associated_dummy);
3435 a->associated_dummy = get_nonintrinsic_dummy_arg (f);
3436 }
3437
3438 if (a->expr == NULL)
3439 {
3440 if (f->sym->attr.optional)
3441 continue;
3442 else
3443 {
3444 if (where)
3445 gfc_error_now ("Unexpected alternate return specifier in "
3446 "subroutine call at %L", where);
3447 return false;
3448 }
3449 }
3450
3451 /* Make sure that intrinsic vtables exist for calls to unlimited
3452 polymorphic formal arguments. */
3453 if (UNLIMITED_POLY (f->sym)
3454 && a->expr->ts.type != BT_DERIVED
3455 && a->expr->ts.type != BT_CLASS
3456 && a->expr->ts.type != BT_ASSUMED)
3457 gfc_find_vtab (&a->expr->ts);
3458
3459 /* Interp J3/22-146:
3460 "If the context of the reference to NULL is an <actual argument>
3461 corresponding to an <assumed-rank> dummy argument, MOLD shall be
3462 present." */
3463 if (a->expr->expr_type == EXPR_NULL
3464 && a->expr->ts.type == BT_UNKNOWN
3465 && f->sym->as
3466 && f->sym->as->type == AS_ASSUMED_RANK)
3467 {
3468 gfc_error ("Intrinsic %<NULL()%> without %<MOLD%> argument at %L "
3469 "passed to assumed-rank dummy %qs",
3470 &a->expr->where, f->sym->name);
3471 ok = false;
3472 goto match;
3473 }
3474
3475 if (warn_surprising
3476 && a->expr->expr_type == EXPR_VARIABLE
3477 && a->expr->symtree->n.sym->as
3478 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
3479 && f->sym->as
3480 && f->sym->as->type == AS_ASSUMED_RANK)
3481 gfc_warning (0, "The assumed-size dummy %qs is being passed at %L to "
3482 "an assumed-rank dummy %qs", a->expr->symtree->name,
3483 &a->expr->where, f->sym->name);
3484
3485 if (a->expr->expr_type == EXPR_NULL
3486 && a->expr->ts.type == BT_UNKNOWN
3487 && f->sym->ts.type == BT_CHARACTER
3488 && !f->sym->ts.deferred
3489 && f->sym->ts.u.cl
3490 && f->sym->ts.u.cl->length == NULL)
3491 {
3492 gfc_error ("Intrinsic %<NULL()%> without %<MOLD%> argument at %L "
3493 "passed to assumed-length dummy %qs",
3494 &a->expr->where, f->sym->name);
3495 ok = false;
3496 goto match;
3497 }
3498
3499 /* Allow passing of NULL() as disassociated pointer, procedure
3500 pointer, or unallocated allocatable (F2008+) to a respective dummy
3501 argument. */
3502 pointer_dummy = ((f->sym->ts.type != BT_CLASS
3503 && f->sym->attr.pointer)
3504 || (f->sym->ts.type == BT_CLASS
3505 && CLASS_DATA (f->sym)->attr.class_pointer));
3506
3507 procptr_dummy = ((f->sym->ts.type != BT_CLASS
3508 && f->sym->attr.proc_pointer)
3509 || (f->sym->ts.type == BT_CLASS
3510 && CLASS_DATA (f->sym)->attr.proc_pointer));
3511
3512 optional_dummy = f->sym->attr.optional;
3513
3514 allocatable_dummy = ((f->sym->ts.type != BT_CLASS
3515 && f->sym->attr.allocatable)
3516 || (f->sym->ts.type == BT_CLASS
3517 && CLASS_DATA (f->sym)->attr.allocatable));
3518
3519 if (a->expr->expr_type == EXPR_NULL
3520 && !pointer_dummy
3521 && !procptr_dummy
3522 && !(optional_dummy
3523 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3524 && !(allocatable_dummy
3525 && (gfc_option.allow_std & GFC_STD_F2008) != 0))
3526 {
3527 if (where
3528 && (!f->sym->attr.optional
3529 || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
3530 || (f->sym->ts.type == BT_CLASS
3531 && CLASS_DATA (f->sym)->attr.allocatable)))
3532 gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
3533 where, f->sym->name);
3534 else if (where)
3535 gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
3536 "dummy %qs", where, f->sym->name);
3537 ok = false;
3538 goto match;
3539 }
3540
3541 if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
3542 is_elemental, where))
3543 {
3544 ok = false;
3545 goto match;
3546 }
3547
3548 /* TS 29113, 6.3p2; F2018 15.5.2.4. */
3549 if (f->sym->ts.type == BT_ASSUMED
3550 && (a->expr->ts.type == BT_DERIVED
3551 || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
3552 {
3553 gfc_symbol *derived = (a->expr->ts.type == BT_DERIVED
3554 ? a->expr->ts.u.derived
3555 : CLASS_DATA (a->expr)->ts.u.derived);
3556 gfc_namespace *f2k_derived = derived->f2k_derived;
3557 if (derived->attr.pdt_type
3558 || (f2k_derived
3559 && (f2k_derived->finalizers || f2k_derived->tb_sym_root)))
3560 {
3561 gfc_error ("Actual argument at %L to assumed-type dummy "
3562 "has type parameters or is of "
3563 "derived type with type-bound or FINAL procedures",
3564 &a->expr->where);
3565 ok = false;
3566 goto match;
3567 }
3568 }
3569
3570 if (UNLIMITED_POLY (a->expr)
3571 && !(f->sym->ts.type == BT_ASSUMED || UNLIMITED_POLY (f->sym)))
3572 {
3573 gfc_error ("Unlimited polymorphic actual argument at %L is not "
3574 "matched with either an unlimited polymorphic or "
3575 "assumed type dummy argument", &a->expr->where);
3576 ok = false;
3577 goto match;
3578 }
3579
3580 /* Special case for character arguments. For allocatable, pointer
3581 and assumed-shape dummies, the string length needs to match
3582 exactly. */
3583 if (a->expr->ts.type == BT_CHARACTER
3584 && a->expr->ts.u.cl && a->expr->ts.u.cl->length
3585 && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
3586 && a->expr->ts.u.cl->length->ts.type == BT_INTEGER
3587 && f->sym->ts.type == BT_CHARACTER && f->sym->ts.u.cl
3588 && f->sym->ts.u.cl->length
3589 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
3590 && f->sym->ts.u.cl->length->ts.type == BT_INTEGER
3591 && (f->sym->attr.pointer || f->sym->attr.allocatable
3592 || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
3593 && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
3594 f->sym->ts.u.cl->length->value.integer) != 0))
3595 {
3596 if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
3597 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
3598 "argument and pointer or allocatable dummy argument "
3599 "%qs at %L",
3600 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3601 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3602 f->sym->name, &a->expr->where);
3603 else if (where)
3604 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
3605 "argument and assumed-shape dummy argument %qs "
3606 "at %L",
3607 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3608 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3609 f->sym->name, &a->expr->where);
3610 ok = false;
3611 goto match;
3612 }
3613
3614 if ((f->sym->attr.pointer || f->sym->attr.allocatable)
3615 && f->sym->ts.deferred != a->expr->ts.deferred
3616 && a->expr->ts.type == BT_CHARACTER)
3617 {
3618 if (where)
3619 gfc_error ("Actual argument at %L to allocatable or "
3620 "pointer dummy argument %qs must have a deferred "
3621 "length type parameter if and only if the dummy has one",
3622 &a->expr->where, f->sym->name);
3623 ok = false;
3624 goto match;
3625 }
3626
3627 if (f->sym->ts.type == BT_CLASS)
3628 goto skip_size_check;
3629
3630 /* Skip size check for NULL() actual without MOLD argument. */
3631 if (a->expr->expr_type == EXPR_NULL && a->expr->ts.type == BT_UNKNOWN)
3632 goto skip_size_check;
3633
3634 actual_size = get_expr_storage_size (a->expr);
3635 formal_size = get_sym_storage_size (f->sym);
3636 if (actual_size != 0 && actual_size < formal_size
3637 && a->expr->ts.type != BT_PROCEDURE
3638 && f->sym->attr.flavor != FL_PROCEDURE)
3639 {
3640 if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
3641 {
3642 gfc_warning (0, "Character length of actual argument shorter "
3643 "than of dummy argument %qs (%lu/%lu) at %L",
3644 f->sym->name, actual_size, formal_size,
3645 &a->expr->where);
3646 goto skip_size_check;
3647 }
3648 else if (where)
3649 {
3650 /* Emit a warning for -std=legacy and an error otherwise. */
3651 if (gfc_option.warn_std == 0)
3652 gfc_warning (0, "Actual argument contains too few "
3653 "elements for dummy argument %qs (%lu/%lu) "
3654 "at %L", f->sym->name, actual_size,
3655 formal_size, &a->expr->where);
3656 else
3657 gfc_error_now ("Actual argument contains too few "
3658 "elements for dummy argument %qs (%lu/%lu) "
3659 "at %L", f->sym->name, actual_size,
3660 formal_size, &a->expr->where);
3661 }
3662 ok = false;
3663 goto match;
3664 }
3665
3666 skip_size_check:
3667
3668 /* Satisfy either: F03:12.4.1.3 by ensuring that a procedure pointer
3669 actual argument is provided for a procedure pointer formal argument;
3670 or: F08:12.5.2.9 (F18:15.5.2.10) by ensuring that the effective
3671 argument shall be an external, internal, module, or dummy procedure.
3672 The interfaces are checked elsewhere. */
3673 if (f->sym->attr.proc_pointer
3674 && !((a->expr->expr_type == EXPR_VARIABLE
3675 && (a->expr->symtree->n.sym->attr.proc_pointer
3676 || gfc_is_proc_ptr_comp (a->expr)))
3677 || (a->expr->ts.type == BT_PROCEDURE
3678 && f->sym->ts.interface)
3679 || (a->expr->expr_type == EXPR_FUNCTION
3680 && is_procptr_result (a->expr))))
3681 {
3682 if (where)
3683 gfc_error ("Expected a procedure pointer for argument %qs at %L",
3684 f->sym->name, &a->expr->where);
3685 ok = false;
3686 goto match;
3687 }
3688
3689 /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
3690 provided for a procedure formal argument. */
3691 if (f->sym->attr.flavor == FL_PROCEDURE
3692 && !((a->expr->expr_type == EXPR_VARIABLE
3693 && (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
3694 || a->expr->symtree->n.sym->attr.proc_pointer
3695 || gfc_is_proc_ptr_comp (a->expr)))
3696 || (a->expr->expr_type == EXPR_FUNCTION
3697 && is_procptr_result (a->expr))))
3698 {
3699 if (where)
3700 gfc_error ("Expected a procedure for argument %qs at %L",
3701 f->sym->name, &a->expr->where);
3702 ok = false;
3703 goto match;
3704 }
3705
3706 /* Class array variables and expressions store array info in a
3707 different place from non-class objects; consolidate the logic
3708 to access it here instead of repeating it below. Note that
3709 pointer_arg and allocatable_arg are not fully general and are
3710 only used in a specific situation below with an assumed-rank
3711 argument. */
3712 if (f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym))
3713 {
3714 gfc_component *classdata = CLASS_DATA (f->sym);
3715 fas = classdata->as;
3716 pointer_dummy = classdata->attr.class_pointer;
3717 }
3718 else
3719 {
3720 fas = f->sym->as;
3721 pointer_dummy = f->sym->attr.pointer;
3722 }
3723
3724 if (a->expr->expr_type != EXPR_VARIABLE
3725 && !(a->expr->expr_type == EXPR_NULL
3726 && a->expr->ts.type != BT_UNKNOWN))
3727 {
3728 aas = NULL;
3729 pointer_arg = false;
3730 allocatable_arg = false;
3731 }
3732 else if (a->expr->ts.type == BT_CLASS
3733 && a->expr->symtree->n.sym
3734 && CLASS_DATA (a->expr->symtree->n.sym))
3735 {
3736 gfc_component *classdata = CLASS_DATA (a->expr->symtree->n.sym);
3737 aas = classdata->as;
3738 pointer_arg = classdata->attr.class_pointer;
3739 allocatable_arg = classdata->attr.allocatable;
3740 }
3741 else
3742 {
3743 aas = a->expr->symtree->n.sym->as;
3744 pointer_arg = a->expr->symtree->n.sym->attr.pointer;
3745 allocatable_arg = a->expr->symtree->n.sym->attr.allocatable;
3746 }
3747
3748 /* F2018:9.5.2(2) permits assumed-size whole array expressions as
3749 actual arguments only if the shape is not required; thus it
3750 cannot be passed to an assumed-shape array dummy.
3751 F2018:15.5.2.(2) permits passing a nonpointer actual to an
3752 intent(in) pointer dummy argument and this is accepted by
3753 the compare_pointer check below, but this also requires shape
3754 information.
3755 There's more discussion of this in PR94110. */
3756 if (fas
3757 && (fas->type == AS_ASSUMED_SHAPE
3758 || fas->type == AS_DEFERRED
3759 || (fas->type == AS_ASSUMED_RANK && pointer_dummy))
3760 && aas
3761 && aas->type == AS_ASSUMED_SIZE
3762 && (a->expr->ref == NULL
3763 || (a->expr->ref->type == REF_ARRAY
3764 && a->expr->ref->u.ar.type == AR_FULL)))
3765 {
3766 if (where)
3767 gfc_error ("Actual argument for %qs cannot be an assumed-size"
3768 " array at %L", f->sym->name, where);
3769 ok = false;
3770 goto match;
3771 }
3772
3773 /* Diagnose F2018 C839 (TS29113 C535c). Here the problem is
3774 passing an assumed-size array to an INTENT(OUT) assumed-rank
3775 dummy when it doesn't have the size information needed to run
3776 initializers and finalizers. */
3777 if (f->sym->attr.intent == INTENT_OUT
3778 && fas
3779 && fas->type == AS_ASSUMED_RANK
3780 && aas
3781 && ((aas->type == AS_ASSUMED_SIZE
3782 && (a->expr->ref == NULL
3783 || (a->expr->ref->type == REF_ARRAY
3784 && a->expr->ref->u.ar.type == AR_FULL)))
3785 || (aas->type == AS_ASSUMED_RANK
3786 && !pointer_arg
3787 && !allocatable_arg))
3788 && (a->expr->ts.type == BT_CLASS
3789 || (a->expr->ts.type == BT_DERIVED
3790 && (gfc_is_finalizable (a->expr->ts.u.derived, NULL)
3791 || gfc_has_ultimate_allocatable (a->expr)
3792 || gfc_has_default_initializer
3793 (a->expr->ts.u.derived)))))
3794 {
3795 if (where)
3796 gfc_error ("Actual argument to assumed-rank INTENT(OUT) "
3797 "dummy %qs at %L cannot be of unknown size",
3798 f->sym->name, where);
3799 ok = false;
3800 goto match;
3801 }
3802
3803 if (a->expr->expr_type != EXPR_NULL)
3804 {
3805 int cmp = compare_pointer (f->sym, a->expr);
3806 bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);
3807
3808 if (pre2008 && cmp == 0)
3809 {
3810 if (where)
3811 gfc_error ("Actual argument for %qs at %L must be a pointer",
3812 f->sym->name, &a->expr->where);
3813 ok = false;
3814 goto match;
3815 }
3816
3817 if (pre2008 && cmp == 2)
3818 {
3819 if (where)
3820 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
3821 "pointer dummy %qs", &a->expr->where, f->sym->name);
3822 ok = false;
3823 goto match;
3824 }
3825
3826 if (!pre2008 && cmp == 0)
3827 {
3828 if (where)
3829 gfc_error ("Actual argument for %qs at %L must be a pointer "
3830 "or a valid target for the dummy pointer in a "
3831 "pointer assignment statement",
3832 f->sym->name, &a->expr->where);
3833 ok = false;
3834 goto match;
3835 }
3836 }
3837
3838
3839 /* Fortran 2008, C1242. */
3840 if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
3841 {
3842 if (where)
3843 gfc_error ("Coindexed actual argument at %L to pointer "
3844 "dummy %qs",
3845 &a->expr->where, f->sym->name);
3846 ok = false;
3847 goto match;
3848 }
3849
3850 /* Fortran 2008, 12.5.2.5 (no constraint). */
3851 if (a->expr->expr_type == EXPR_VARIABLE
3852 && f->sym->attr.intent != INTENT_IN
3853 && f->sym->attr.allocatable
3854 && gfc_is_coindexed (a->expr))
3855 {
3856 if (where)
3857 gfc_error ("Coindexed actual argument at %L to allocatable "
3858 "dummy %qs requires INTENT(IN)",
3859 &a->expr->where, f->sym->name);
3860 ok = false;
3861 goto match;
3862 }
3863
3864 /* Fortran 2008, C1237. */
3865 if (a->expr->expr_type == EXPR_VARIABLE
3866 && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
3867 && gfc_is_coindexed (a->expr)
3868 && (a->expr->symtree->n.sym->attr.volatile_
3869 || a->expr->symtree->n.sym->attr.asynchronous))
3870 {
3871 if (where)
3872 gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
3873 "%L requires that dummy %qs has neither "
3874 "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
3875 f->sym->name);
3876 ok = false;
3877 goto match;
3878 }
3879
3880 /* Fortran 2008, 12.5.2.4 (no constraint). */
3881 if (a->expr->expr_type == EXPR_VARIABLE
3882 && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
3883 && gfc_is_coindexed (a->expr)
3884 && gfc_has_ultimate_allocatable (a->expr))
3885 {
3886 if (where)
3887 gfc_error ("Coindexed actual argument at %L with allocatable "
3888 "ultimate component to dummy %qs requires either VALUE "
3889 "or INTENT(IN)", &a->expr->where, f->sym->name);
3890 ok = false;
3891 goto match;
3892 }
3893
3894 if (f->sym->ts.type == BT_CLASS
3895 && CLASS_DATA (f->sym)->attr.allocatable
3896 && gfc_is_class_array_ref (a->expr, &full_array)
3897 && !full_array)
3898 {
3899 if (where)
3900 gfc_error ("Actual CLASS array argument for %qs must be a full "
3901 "array at %L", f->sym->name, &a->expr->where);
3902 ok = false;
3903 goto match;
3904 }
3905
3906
3907 if (a->expr->expr_type != EXPR_NULL
3908 && !compare_allocatable (f->sym, a->expr))
3909 {
3910 if (where)
3911 gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
3912 f->sym->name, &a->expr->where);
3913 ok = false;
3914 goto match;
3915 }
3916
3917 if (a->expr->expr_type == EXPR_FUNCTION
3918 && a->expr->value.function.esym
3919 && f->sym->attr.allocatable)
3920 {
3921 if (where)
3922 gfc_error ("Actual argument for %qs at %L is a function result "
3923 "and the dummy argument is ALLOCATABLE",
3924 f->sym->name, &a->expr->where);
3925 ok = false;
3926 goto match;
3927 }
3928
3929 /* Check intent = OUT/INOUT for definable actual argument. */
3930 if (!in_statement_function
3931 && (f->sym->attr.intent == INTENT_OUT
3932 || f->sym->attr.intent == INTENT_INOUT))
3933 {
3934 const char* context = (where
3935 ? _("actual argument to INTENT = OUT/INOUT")
3936 : NULL);
3937
3938 if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3939 && CLASS_DATA (f->sym)->attr.class_pointer)
3940 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
3941 && !gfc_check_vardef_context (a->expr, true, false, false, context))
3942 {
3943 ok = false;
3944 goto match;
3945 }
3946 if (!gfc_check_vardef_context (a->expr, false, false, false, context))
3947 {
3948 ok = false;
3949 goto match;
3950 }
3951 }
3952
3953 if ((f->sym->attr.intent == INTENT_OUT
3954 || f->sym->attr.intent == INTENT_INOUT
3955 || f->sym->attr.volatile_
3956 || f->sym->attr.asynchronous)
3957 && gfc_has_vector_subscript (a->expr))
3958 {
3959 if (where)
3960 gfc_error ("Array-section actual argument with vector "
3961 "subscripts at %L is incompatible with INTENT(OUT), "
3962 "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
3963 "of the dummy argument %qs",
3964 &a->expr->where, f->sym->name);
3965 ok = false;
3966 goto match;
3967 }
3968
3969 /* C1232 (R1221) For an actual argument which is an array section or
3970 an assumed-shape array, the dummy argument shall be an assumed-
3971 shape array, if the dummy argument has the VOLATILE attribute. */
3972
3973 if (f->sym->attr.volatile_
3974 && a->expr->expr_type == EXPR_VARIABLE
3975 && a->expr->symtree->n.sym->as
3976 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
3977 && !(fas && fas->type == AS_ASSUMED_SHAPE))
3978 {
3979 if (where)
3980 gfc_error ("Assumed-shape actual argument at %L is "
3981 "incompatible with the non-assumed-shape "
3982 "dummy argument %qs due to VOLATILE attribute",
3983 &a->expr->where,f->sym->name);
3984 ok = false;
3985 goto match;
3986 }
3987
3988 /* Find the last array_ref. */
3989 actual_arr_ref = NULL;
3990 if (a->expr->ref)
3991 actual_arr_ref = gfc_find_array_ref (a->expr, true);
3992
3993 if (f->sym->attr.volatile_
3994 && actual_arr_ref && actual_arr_ref->type == AR_SECTION
3995 && !(fas && fas->type == AS_ASSUMED_SHAPE))
3996 {
3997 if (where)
3998 gfc_error ("Array-section actual argument at %L is "
3999 "incompatible with the non-assumed-shape "
4000 "dummy argument %qs due to VOLATILE attribute",
4001 &a->expr->where, f->sym->name);
4002 ok = false;
4003 goto match;
4004 }
4005
4006 /* C1233 (R1221) For an actual argument which is a pointer array, the
4007 dummy argument shall be an assumed-shape or pointer array, if the
4008 dummy argument has the VOLATILE attribute. */
4009
4010 if (f->sym->attr.volatile_
4011 && a->expr->expr_type == EXPR_VARIABLE
4012 && a->expr->symtree->n.sym->attr.pointer
4013 && a->expr->symtree->n.sym->as
4014 && !(fas
4015 && (fas->type == AS_ASSUMED_SHAPE
4016 || f->sym->attr.pointer)))
4017 {
4018 if (where)
4019 gfc_error ("Pointer-array actual argument at %L requires "
4020 "an assumed-shape or pointer-array dummy "
4021 "argument %qs due to VOLATILE attribute",
4022 &a->expr->where,f->sym->name);
4023 ok = false;
4024 goto match;
4025 }
4026
4027 /* C_LOC/C_FUNLOC from ISO_C_BINDING as actual argument can only be
4028 passed to a dummy argument of matching type C_PTR/C_FUNPTR. */
4029 if (a->expr->expr_type == EXPR_FUNCTION
4030 && a->expr->ts.type == BT_VOID
4031 && a->expr->symtree->n.sym
4032 && a->expr->symtree->n.sym->from_intmod == INTMOD_ISO_C_BINDING
4033 && (f->sym->ts.type != BT_DERIVED
4034 || f->sym->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4035 || !((a->expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_FUNLOC
4036 && f->sym->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR)
4037 || (a->expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_LOC
4038 && f->sym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR))))
4039 {
4040 if (where)
4041 gfc_error ("ISO_C_BINDING function actual argument at %L "
4042 "requires dummy argument %qs to have a matching "
4043 "type from ISO_C_BINDING",
4044 &a->expr->where,f->sym->name);
4045 ok = false;
4046 goto match;
4047 }
4048
4049 match:
4050 if (a == actual)
4051 na = i;
4052
4053 new_arg[i++] = a;
4054 }
4055
4056 /* Give up now if we saw any bad argument. */
4057 if (!ok)
4058 return false;
4059
4060 /* Make sure missing actual arguments are optional. */
4061 i = 0;
4062 for (f = formal; f; f = f->next, i++)
4063 {
4064 if (new_arg[i] != NULL)
4065 continue;
4066 if (f->sym == NULL)
4067 {
4068 if (where)
4069 gfc_error ("Missing alternate return spec in subroutine call "
4070 "at %L", where);
4071 return false;
4072 }
4073 /* For CLASS, the optional attribute might be set at either location. */
4074 if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
4075 && !f->sym->attr.optional)
4076 || (in_statement_function
4077 && (f->sym->attr.optional
4078 || (f->sym->ts.type == BT_CLASS
4079 && CLASS_DATA (f->sym)->attr.optional))))
4080 {
4081 if (where)
4082 gfc_error ("Missing actual argument for argument %qs at %L",
4083 f->sym->name, where);
4084 return false;
4085 }
4086 }
4087
4088 /* We should have handled the cases where the formal arglist is null
4089 already. */
4090 gcc_assert (n > 0);
4091
4092 /* The argument lists are compatible. We now relink a new actual
4093 argument list with null arguments in the right places. The head
4094 of the list remains the head. */
4095 for (f = formal, i = 0; f; f = f->next, i++)
4096 if (new_arg[i] == NULL)
4097 {
4098 new_arg[i] = gfc_get_actual_arglist ();
4099 new_arg[i]->associated_dummy = get_nonintrinsic_dummy_arg (f);
4100 }
4101
4102 if (na != 0)
4103 {
4104 std::swap (*new_arg[0], *actual);
4105 std::swap (new_arg[0], new_arg[na]);
4106 }
4107
4108 for (i = 0; i < n - 1; i++)
4109 new_arg[i]->next = new_arg[i + 1];
4110
4111 new_arg[i]->next = NULL;
4112
4113 if (*ap == NULL && n > 0)
4114 *ap = new_arg[0];
4115
4116 return true;
4117 }
4118
4119
4120 typedef struct
4121 {
4122 gfc_formal_arglist *f;
4123 gfc_actual_arglist *a;
4124 }
4125 argpair;
4126
4127 /* qsort comparison function for argument pairs, with the following
4128 order:
4129 - p->a->expr == NULL
4130 - p->a->expr->expr_type != EXPR_VARIABLE
4131 - by gfc_symbol pointer value (larger first). */
4132
4133 static int
4134 pair_cmp (const void *p1, const void *p2)
4135 {
4136 const gfc_actual_arglist *a1, *a2;
4137
4138 /* *p1 and *p2 are elements of the to-be-sorted array. */
4139 a1 = ((const argpair *) p1)->a;
4140 a2 = ((const argpair *) p2)->a;
4141 if (!a1->expr)
4142 {
4143 if (!a2->expr)
4144 return 0;
4145 return -1;
4146 }
4147 if (!a2->expr)
4148 return 1;
4149 if (a1->expr->expr_type != EXPR_VARIABLE)
4150 {
4151 if (a2->expr->expr_type != EXPR_VARIABLE)
4152 return 0;
4153 return -1;
4154 }
4155 if (a2->expr->expr_type != EXPR_VARIABLE)
4156 return 1;
4157 if (a1->expr->symtree->n.sym > a2->expr->symtree->n.sym)
4158 return -1;
4159 return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
4160 }
4161
4162
4163 /* Given two expressions from some actual arguments, test whether they
4164 refer to the same expression. The analysis is conservative.
4165 Returning false will produce no warning. */
4166
4167 static bool
4168 compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
4169 {
4170 const gfc_ref *r1, *r2;
4171
4172 if (!e1 || !e2
4173 || e1->expr_type != EXPR_VARIABLE
4174 || e2->expr_type != EXPR_VARIABLE
4175 || e1->symtree->n.sym != e2->symtree->n.sym)
4176 return false;
4177
4178 /* TODO: improve comparison, see expr.cc:show_ref(). */
4179 for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
4180 {
4181 if (r1->type != r2->type)
4182 return false;
4183 switch (r1->type)
4184 {
4185 case REF_ARRAY:
4186 if (r1->u.ar.type != r2->u.ar.type)
4187 return false;
4188 /* TODO: At the moment, consider only full arrays;
4189 we could do better. */
4190 if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
4191 return false;
4192 break;
4193
4194 case REF_COMPONENT:
4195 if (r1->u.c.component != r2->u.c.component)
4196 return false;
4197 break;
4198
4199 case REF_SUBSTRING:
4200 return false;
4201
4202 case REF_INQUIRY:
4203 if (e1->symtree->n.sym->ts.type == BT_COMPLEX
4204 && e1->ts.type == BT_REAL && e2->ts.type == BT_REAL
4205 && r1->u.i != r2->u.i)
4206 return false;
4207 break;
4208
4209 default:
4210 gfc_internal_error ("compare_actual_expr(): Bad component code");
4211 }
4212 }
4213 if (!r1 && !r2)
4214 return true;
4215 return false;
4216 }
4217
4218
4219 /* Given formal and actual argument lists that correspond to one
4220 another, check that identical actual arguments aren't not
4221 associated with some incompatible INTENTs. */
4222
4223 static bool
4224 check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
4225 {
4226 sym_intent f1_intent, f2_intent;
4227 gfc_formal_arglist *f1;
4228 gfc_actual_arglist *a1;
4229 size_t n, i, j;
4230 argpair *p;
4231 bool t = true;
4232
4233 n = 0;
4234 for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
4235 {
4236 if (f1 == NULL && a1 == NULL)
4237 break;
4238 if (f1 == NULL || a1 == NULL)
4239 gfc_internal_error ("check_some_aliasing(): List mismatch");
4240 n++;
4241 }
4242 if (n == 0)
4243 return t;
4244 p = XALLOCAVEC (argpair, n);
4245
4246 for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
4247 {
4248 p[i].f = f1;
4249 p[i].a = a1;
4250 }
4251
4252 qsort (p, n, sizeof (argpair), pair_cmp);
4253
4254 for (i = 0; i < n; i++)
4255 {
4256 if (!p[i].a->expr
4257 || p[i].a->expr->expr_type != EXPR_VARIABLE
4258 || p[i].a->expr->ts.type == BT_PROCEDURE)
4259 continue;
4260 f1_intent = p[i].f->sym->attr.intent;
4261 for (j = i + 1; j < n; j++)
4262 {
4263 /* Expected order after the sort. */
4264 if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
4265 gfc_internal_error ("check_some_aliasing(): corrupted data");
4266
4267 /* Are the expression the same? */
4268 if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
4269 break;
4270 f2_intent = p[j].f->sym->attr.intent;
4271 if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
4272 || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
4273 || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
4274 {
4275 gfc_warning (0, "Same actual argument associated with INTENT(%s) "
4276 "argument %qs and INTENT(%s) argument %qs at %L",
4277 gfc_intent_string (f1_intent), p[i].f->sym->name,
4278 gfc_intent_string (f2_intent), p[j].f->sym->name,
4279 &p[i].a->expr->where);
4280 t = false;
4281 }
4282 }
4283 }
4284
4285 return t;
4286 }
4287
4288
4289 /* Given formal and actual argument lists that correspond to one
4290 another, check that they are compatible in the sense that intents
4291 are not mismatched. */
4292
4293 static bool
4294 check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
4295 {
4296 sym_intent f_intent;
4297
4298 for (;; f = f->next, a = a->next)
4299 {
4300 gfc_expr *expr;
4301
4302 if (f == NULL && a == NULL)
4303 break;
4304 if (f == NULL || a == NULL)
4305 gfc_internal_error ("check_intents(): List mismatch");
4306
4307 if (a->expr && a->expr->expr_type == EXPR_FUNCTION
4308 && a->expr->value.function.isym
4309 && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
4310 expr = a->expr->value.function.actual->expr;
4311 else
4312 expr = a->expr;
4313
4314 if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
4315 continue;
4316
4317 f_intent = f->sym->attr.intent;
4318
4319 if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
4320 {
4321 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
4322 && CLASS_DATA (f->sym)->attr.class_pointer)
4323 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
4324 {
4325 gfc_error ("Procedure argument at %L is local to a PURE "
4326 "procedure and has the POINTER attribute",
4327 &expr->where);
4328 return false;
4329 }
4330 }
4331
4332 /* Fortran 2008, C1283. */
4333 if (gfc_pure (NULL) && gfc_is_coindexed (expr))
4334 {
4335 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
4336 {
4337 gfc_error ("Coindexed actual argument at %L in PURE procedure "
4338 "is passed to an INTENT(%s) argument",
4339 &expr->where, gfc_intent_string (f_intent));
4340 return false;
4341 }
4342
4343 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
4344 && CLASS_DATA (f->sym)->attr.class_pointer)
4345 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
4346 {
4347 gfc_error ("Coindexed actual argument at %L in PURE procedure "
4348 "is passed to a POINTER dummy argument",
4349 &expr->where);
4350 return false;
4351 }
4352 }
4353
4354 /* F2008, Section 12.5.2.4. */
4355 if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
4356 && gfc_is_coindexed (expr))
4357 {
4358 gfc_error ("Coindexed polymorphic actual argument at %L is passed "
4359 "polymorphic dummy argument %qs",
4360 &expr->where, f->sym->name);
4361 return false;
4362 }
4363 }
4364
4365 return true;
4366 }
4367
4368
4369 /* Check how a procedure is used against its interface. If all goes
4370 well, the actual argument list will also end up being properly
4371 sorted. */
4372
4373 bool
4374 gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
4375 {
4376 gfc_actual_arglist *a;
4377 gfc_formal_arglist *dummy_args;
4378 bool implicit = false;
4379
4380 /* Warn about calls with an implicit interface. Special case
4381 for calling a ISO_C_BINDING because c_loc and c_funloc
4382 are pseudo-unknown. Additionally, warn about procedures not
4383 explicitly declared at all if requested. */
4384 if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
4385 {
4386 bool has_implicit_none_export = false;
4387 implicit = true;
4388 if (sym->attr.proc == PROC_UNKNOWN)
4389 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
4390 if (ns->has_implicit_none_export)
4391 {
4392 has_implicit_none_export = true;
4393 break;
4394 }
4395 if (has_implicit_none_export)
4396 {
4397 const char *guessed
4398 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
4399 if (guessed)
4400 gfc_error ("Procedure %qs called at %L is not explicitly declared"
4401 "; did you mean %qs?",
4402 sym->name, where, guessed);
4403 else
4404 gfc_error ("Procedure %qs called at %L is not explicitly declared",
4405 sym->name, where);
4406 return false;
4407 }
4408 if (warn_implicit_interface)
4409 gfc_warning (OPT_Wimplicit_interface,
4410 "Procedure %qs called with an implicit interface at %L",
4411 sym->name, where);
4412 else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
4413 gfc_warning (OPT_Wimplicit_procedure,
4414 "Procedure %qs called at %L is not explicitly declared",
4415 sym->name, where);
4416 gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
4417 }
4418
4419 if (sym->attr.if_source == IFSRC_UNKNOWN)
4420 {
4421 if (sym->attr.pointer)
4422 {
4423 gfc_error ("The pointer object %qs at %L must have an explicit "
4424 "function interface or be declared as array",
4425 sym->name, where);
4426 return false;
4427 }
4428
4429 if (sym->attr.allocatable && !sym->attr.external)
4430 {
4431 gfc_error ("The allocatable object %qs at %L must have an explicit "
4432 "function interface or be declared as array",
4433 sym->name, where);
4434 return false;
4435 }
4436
4437 if (sym->attr.allocatable)
4438 {
4439 gfc_error ("Allocatable function %qs at %L must have an explicit "
4440 "function interface", sym->name, where);
4441 return false;
4442 }
4443
4444 for (a = *ap; a; a = a->next)
4445 {
4446 if (a->expr && a->expr->error)
4447 return false;
4448
4449 /* F2018, 15.4.2.2 Explicit interface is required for a
4450 polymorphic dummy argument, so there is no way to
4451 legally have a class appear in an argument with an
4452 implicit interface. */
4453
4454 if (implicit && a->expr && a->expr->ts.type == BT_CLASS)
4455 {
4456 gfc_error ("Explicit interface required for polymorphic "
4457 "argument at %L",&a->expr->where);
4458 a->expr->error = 1;
4459 break;
4460 }
4461
4462 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4463 if (a->name != NULL && a->name[0] != '%')
4464 {
4465 gfc_error ("Keyword argument requires explicit interface "
4466 "for procedure %qs at %L", sym->name, &a->expr->where);
4467 break;
4468 }
4469
4470 /* TS 29113, 6.2. */
4471 if (a->expr && a->expr->ts.type == BT_ASSUMED
4472 && sym->intmod_sym_id != ISOCBINDING_LOC)
4473 {
4474 gfc_error ("Assumed-type argument %s at %L requires an explicit "
4475 "interface", a->expr->symtree->n.sym->name,
4476 &a->expr->where);
4477 a->expr->error = 1;
4478 break;
4479 }
4480
4481 /* F2008, C1303 and C1304. */
4482 if (a->expr
4483 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
4484 && a->expr->ts.u.derived
4485 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4486 && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
4487 || gfc_expr_attr (a->expr).lock_comp))
4488 {
4489 gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
4490 "component at %L requires an explicit interface for "
4491 "procedure %qs", &a->expr->where, sym->name);
4492 a->expr->error = 1;
4493 break;
4494 }
4495
4496 if (a->expr
4497 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
4498 && a->expr->ts.u.derived
4499 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4500 && a->expr->ts.u.derived->intmod_sym_id
4501 == ISOFORTRAN_EVENT_TYPE)
4502 || gfc_expr_attr (a->expr).event_comp))
4503 {
4504 gfc_error ("Actual argument of EVENT_TYPE or with EVENT_TYPE "
4505 "component at %L requires an explicit interface for "
4506 "procedure %qs", &a->expr->where, sym->name);
4507 a->expr->error = 1;
4508 break;
4509 }
4510
4511 if (a->expr && a->expr->expr_type == EXPR_NULL
4512 && a->expr->ts.type == BT_UNKNOWN)
4513 {
4514 gfc_error ("MOLD argument to NULL required at %L",
4515 &a->expr->where);
4516 a->expr->error = 1;
4517 return false;
4518 }
4519
4520 if (a->expr && a->expr->expr_type == EXPR_NULL)
4521 {
4522 gfc_error ("Passing intrinsic NULL as actual argument at %L "
4523 "requires an explicit interface", &a->expr->where);
4524 a->expr->error = 1;
4525 return false;
4526 }
4527
4528 /* TS 29113, C407b. */
4529 if (a->expr && a->expr->expr_type == EXPR_VARIABLE
4530 && symbol_rank (a->expr->symtree->n.sym) == -1)
4531 {
4532 gfc_error ("Assumed-rank argument requires an explicit interface "
4533 "at %L", &a->expr->where);
4534 a->expr->error = 1;
4535 return false;
4536 }
4537 }
4538
4539 return true;
4540 }
4541
4542 dummy_args = gfc_sym_get_dummy_args (sym);
4543
4544 /* For a statement function, check that types and type parameters of actual
4545 arguments and dummy arguments match. */
4546 if (!gfc_compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
4547 sym->attr.proc == PROC_ST_FUNCTION, where))
4548 return false;
4549
4550 if (!check_intents (dummy_args, *ap))
4551 return false;
4552
4553 if (warn_aliasing)
4554 check_some_aliasing (dummy_args, *ap);
4555
4556 return true;
4557 }
4558
4559
4560 /* Check how a procedure pointer component is used against its interface.
4561 If all goes well, the actual argument list will also end up being properly
4562 sorted. Completely analogous to gfc_procedure_use. */
4563
4564 void
4565 gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
4566 {
4567 /* Warn about calls with an implicit interface. Special case
4568 for calling a ISO_C_BINDING because c_loc and c_funloc
4569 are pseudo-unknown. */
4570 if (warn_implicit_interface
4571 && comp->attr.if_source == IFSRC_UNKNOWN
4572 && !comp->attr.is_iso_c)
4573 gfc_warning (OPT_Wimplicit_interface,
4574 "Procedure pointer component %qs called with an implicit "
4575 "interface at %L", comp->name, where);
4576
4577 if (comp->attr.if_source == IFSRC_UNKNOWN)
4578 {
4579 gfc_actual_arglist *a;
4580 for (a = *ap; a; a = a->next)
4581 {
4582 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4583 if (a->name != NULL && a->name[0] != '%')
4584 {
4585 gfc_error ("Keyword argument requires explicit interface "
4586 "for procedure pointer component %qs at %L",
4587 comp->name, &a->expr->where);
4588 break;
4589 }
4590 }
4591
4592 return;
4593 }
4594
4595 if (!gfc_compare_actual_formal (ap, comp->ts.interface->formal, 0,
4596 comp->attr.elemental, false, where))
4597 return;
4598
4599 check_intents (comp->ts.interface->formal, *ap);
4600 if (warn_aliasing)
4601 check_some_aliasing (comp->ts.interface->formal, *ap);
4602 }
4603
4604
4605 /* Try if an actual argument list matches the formal list of a symbol,
4606 respecting the symbol's attributes like ELEMENTAL. This is used for
4607 GENERIC resolution. */
4608
4609 bool
4610 gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
4611 {
4612 gfc_formal_arglist *dummy_args;
4613 bool r;
4614
4615 if (sym->attr.flavor != FL_PROCEDURE)
4616 return false;
4617
4618 dummy_args = gfc_sym_get_dummy_args (sym);
4619
4620 r = !sym->attr.elemental;
4621 if (gfc_compare_actual_formal (args, dummy_args, r, !r, false, NULL))
4622 {
4623 check_intents (dummy_args, *args);
4624 if (warn_aliasing)
4625 check_some_aliasing (dummy_args, *args);
4626 return true;
4627 }
4628
4629 return false;
4630 }
4631
4632
4633 /* Given an interface pointer and an actual argument list, search for
4634 a formal argument list that matches the actual. If found, returns
4635 a pointer to the symbol of the correct interface. Returns NULL if
4636 not found. */
4637
4638 gfc_symbol *
4639 gfc_search_interface (gfc_interface *intr, int sub_flag,
4640 gfc_actual_arglist **ap)
4641 {
4642 gfc_symbol *elem_sym = NULL;
4643 gfc_symbol *null_sym = NULL;
4644 locus null_expr_loc;
4645 gfc_actual_arglist *a;
4646 bool has_null_arg = false;
4647
4648 for (a = *ap; a; a = a->next)
4649 if (a->expr && a->expr->expr_type == EXPR_NULL
4650 && a->expr->ts.type == BT_UNKNOWN)
4651 {
4652 has_null_arg = true;
4653 null_expr_loc = a->expr->where;
4654 break;
4655 }
4656
4657 for (; intr; intr = intr->next)
4658 {
4659 if (gfc_fl_struct (intr->sym->attr.flavor))
4660 continue;
4661 if (sub_flag && intr->sym->attr.function)
4662 continue;
4663 if (!sub_flag && intr->sym->attr.subroutine)
4664 continue;
4665
4666 if (gfc_arglist_matches_symbol (ap, intr->sym))
4667 {
4668 if (has_null_arg && null_sym)
4669 {
4670 gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
4671 "between specific functions %s and %s",
4672 &null_expr_loc, null_sym->name, intr->sym->name);
4673 return NULL;
4674 }
4675 else if (has_null_arg)
4676 {
4677 null_sym = intr->sym;
4678 continue;
4679 }
4680
4681 /* Satisfy 12.4.4.1 such that an elemental match has lower
4682 weight than a non-elemental match. */
4683 if (intr->sym->attr.elemental)
4684 {
4685 elem_sym = intr->sym;
4686 continue;
4687 }
4688 return intr->sym;
4689 }
4690 }
4691
4692 if (null_sym)
4693 return null_sym;
4694
4695 return elem_sym ? elem_sym : NULL;
4696 }
4697
4698
4699 /* Do a brute force recursive search for a symbol. */
4700
4701 static gfc_symtree *
4702 find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
4703 {
4704 gfc_symtree * st;
4705
4706 if (root->n.sym == sym)
4707 return root;
4708
4709 st = NULL;
4710 if (root->left)
4711 st = find_symtree0 (root->left, sym);
4712 if (root->right && ! st)
4713 st = find_symtree0 (root->right, sym);
4714 return st;
4715 }
4716
4717
4718 /* Find a symtree for a symbol. */
4719
4720 gfc_symtree *
4721 gfc_find_sym_in_symtree (gfc_symbol *sym)
4722 {
4723 gfc_symtree *st;
4724 gfc_namespace *ns;
4725
4726 /* First try to find it by name. */
4727 gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
4728 if (st && st->n.sym == sym)
4729 return st;
4730
4731 /* If it's been renamed, resort to a brute-force search. */
4732 /* TODO: avoid having to do this search. If the symbol doesn't exist
4733 in the symtree for the current namespace, it should probably be added. */
4734 for (ns = gfc_current_ns; ns; ns = ns->parent)
4735 {
4736 st = find_symtree0 (ns->sym_root, sym);
4737 if (st)
4738 return st;
4739 }
4740 gfc_internal_error ("Unable to find symbol %qs", sym->name);
4741 /* Not reached. */
4742 }
4743
4744
4745 /* See if the arglist to an operator-call contains a derived-type argument
4746 with a matching type-bound operator. If so, return the matching specific
4747 procedure defined as operator-target as well as the base-object to use
4748 (which is the found derived-type argument with operator). The generic
4749 name, if any, is transmitted to the final expression via 'gname'. */
4750
4751 static gfc_typebound_proc*
4752 matching_typebound_op (gfc_expr** tb_base,
4753 gfc_actual_arglist* args,
4754 gfc_intrinsic_op op, const char* uop,
4755 const char ** gname)
4756 {
4757 gfc_actual_arglist* base;
4758
4759 for (base = args; base; base = base->next)
4760 if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
4761 {
4762 gfc_typebound_proc* tb;
4763 gfc_symbol* derived;
4764 bool result;
4765
4766 while (base->expr->expr_type == EXPR_OP
4767 && base->expr->value.op.op == INTRINSIC_PARENTHESES)
4768 base->expr = base->expr->value.op.op1;
4769
4770 if (base->expr->ts.type == BT_CLASS)
4771 {
4772 if (!base->expr->ts.u.derived || CLASS_DATA (base->expr) == NULL
4773 || !gfc_expr_attr (base->expr).class_ok)
4774 continue;
4775 derived = CLASS_DATA (base->expr)->ts.u.derived;
4776 }
4777 else
4778 derived = base->expr->ts.u.derived;
4779
4780 if (op == INTRINSIC_USER)
4781 {
4782 gfc_symtree* tb_uop;
4783
4784 gcc_assert (uop);
4785 tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
4786 false, NULL);
4787
4788 if (tb_uop)
4789 tb = tb_uop->n.tb;
4790 else
4791 tb = NULL;
4792 }
4793 else
4794 tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
4795 false, NULL);
4796
4797 /* This means we hit a PRIVATE operator which is use-associated and
4798 should thus not be seen. */
4799 if (!result)
4800 tb = NULL;
4801
4802 /* Look through the super-type hierarchy for a matching specific
4803 binding. */
4804 for (; tb; tb = tb->overridden)
4805 {
4806 gfc_tbp_generic* g;
4807
4808 gcc_assert (tb->is_generic);
4809 for (g = tb->u.generic; g; g = g->next)
4810 {
4811 gfc_symbol* target;
4812 gfc_actual_arglist* argcopy;
4813 bool matches;
4814
4815 /* If expression matching comes here during parsing, eg. when
4816 parsing ASSOCIATE, generic TBPs have not yet been resolved
4817 and g->specific will not have been set. Wait for expression
4818 resolution by returning NULL. */
4819 if (!g->specific && !gfc_current_ns->resolved)
4820 return NULL;
4821
4822 gcc_assert (g->specific);
4823 if (g->specific->error)
4824 continue;
4825
4826 target = g->specific->u.specific->n.sym;
4827
4828 /* Check if this arglist matches the formal. */
4829 argcopy = gfc_copy_actual_arglist (args);
4830 matches = gfc_arglist_matches_symbol (&argcopy, target);
4831 gfc_free_actual_arglist (argcopy);
4832
4833 /* Return if we found a match. */
4834 if (matches)
4835 {
4836 *tb_base = base->expr;
4837 *gname = g->specific_st->name;
4838 return g->specific;
4839 }
4840 }
4841 }
4842 }
4843
4844 return NULL;
4845 }
4846
4847
4848 /* For the 'actual arglist' of an operator call and a specific typebound
4849 procedure that has been found the target of a type-bound operator, build the
4850 appropriate EXPR_COMPCALL and resolve it. We take this indirection over
4851 type-bound procedures rather than resolving type-bound operators 'directly'
4852 so that we can reuse the existing logic. */
4853
4854 static void
4855 build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
4856 gfc_expr* base, gfc_typebound_proc* target,
4857 const char *gname)
4858 {
4859 e->expr_type = EXPR_COMPCALL;
4860 e->value.compcall.tbp = target;
4861 e->value.compcall.name = gname ? gname : "$op";
4862 e->value.compcall.actual = actual;
4863 e->value.compcall.base_object = base;
4864 e->value.compcall.ignore_pass = 1;
4865 e->value.compcall.assign = 0;
4866 if (e->ts.type == BT_UNKNOWN
4867 && target->function)
4868 {
4869 if (target->is_generic)
4870 e->ts = target->u.generic->specific->u.specific->n.sym->ts;
4871 else
4872 e->ts = target->u.specific->n.sym->ts;
4873 }
4874 }
4875
4876
4877 /* This subroutine is called when an expression is being resolved.
4878 The expression node in question is either a user defined operator
4879 or an intrinsic operator with arguments that aren't compatible
4880 with the operator. This subroutine builds an actual argument list
4881 corresponding to the operands, then searches for a compatible
4882 interface. If one is found, the expression node is replaced with
4883 the appropriate function call. We use the 'match' enum to specify
4884 whether a replacement has been made or not, or if an error occurred. */
4885
4886 match
4887 gfc_extend_expr (gfc_expr *e)
4888 {
4889 gfc_actual_arglist *actual;
4890 gfc_symbol *sym;
4891 gfc_namespace *ns;
4892 gfc_user_op *uop;
4893 gfc_intrinsic_op i;
4894 const char *gname;
4895 gfc_typebound_proc* tbo;
4896 gfc_expr* tb_base;
4897
4898 sym = NULL;
4899
4900 actual = gfc_get_actual_arglist ();
4901 actual->expr = e->value.op.op1;
4902
4903 gname = NULL;
4904
4905 if (e->value.op.op2 != NULL)
4906 {
4907 actual->next = gfc_get_actual_arglist ();
4908 actual->next->expr = e->value.op.op2;
4909 }
4910
4911 i = fold_unary_intrinsic (e->value.op.op);
4912
4913 /* See if we find a matching type-bound operator. */
4914 if (i == INTRINSIC_USER)
4915 tbo = matching_typebound_op (&tb_base, actual,
4916 i, e->value.op.uop->name, &gname);
4917 else
4918 switch (i)
4919 {
4920 #define CHECK_OS_COMPARISON(comp) \
4921 case INTRINSIC_##comp: \
4922 case INTRINSIC_##comp##_OS: \
4923 tbo = matching_typebound_op (&tb_base, actual, \
4924 INTRINSIC_##comp, NULL, &gname); \
4925 if (!tbo) \
4926 tbo = matching_typebound_op (&tb_base, actual, \
4927 INTRINSIC_##comp##_OS, NULL, &gname); \
4928 break;
4929 CHECK_OS_COMPARISON(EQ)
4930 CHECK_OS_COMPARISON(NE)
4931 CHECK_OS_COMPARISON(GT)
4932 CHECK_OS_COMPARISON(GE)
4933 CHECK_OS_COMPARISON(LT)
4934 CHECK_OS_COMPARISON(LE)
4935 #undef CHECK_OS_COMPARISON
4936
4937 default:
4938 tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
4939 break;
4940 }
4941
4942 /* If there is a matching typebound-operator, replace the expression with
4943 a call to it and succeed. */
4944 if (tbo)
4945 {
4946 gcc_assert (tb_base);
4947 build_compcall_for_operator (e, actual, tb_base, tbo, gname);
4948
4949 if (!gfc_resolve_expr (e))
4950 return MATCH_ERROR;
4951 else
4952 return MATCH_YES;
4953 }
4954
4955 if (i == INTRINSIC_USER)
4956 {
4957 for (ns = gfc_current_ns; ns; ns = ns->parent)
4958 {
4959 uop = gfc_find_uop (e->value.op.uop->name, ns);
4960 if (uop == NULL)
4961 continue;
4962
4963 sym = gfc_search_interface (uop->op, 0, &actual);
4964 if (sym != NULL)
4965 break;
4966 }
4967 }
4968 else
4969 {
4970 for (ns = gfc_current_ns; ns; ns = ns->parent)
4971 {
4972 /* Due to the distinction between '==' and '.eq.' and friends, one has
4973 to check if either is defined. */
4974 switch (i)
4975 {
4976 #define CHECK_OS_COMPARISON(comp) \
4977 case INTRINSIC_##comp: \
4978 case INTRINSIC_##comp##_OS: \
4979 sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
4980 if (!sym) \
4981 sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
4982 break;
4983 CHECK_OS_COMPARISON(EQ)
4984 CHECK_OS_COMPARISON(NE)
4985 CHECK_OS_COMPARISON(GT)
4986 CHECK_OS_COMPARISON(GE)
4987 CHECK_OS_COMPARISON(LT)
4988 CHECK_OS_COMPARISON(LE)
4989 #undef CHECK_OS_COMPARISON
4990
4991 default:
4992 sym = gfc_search_interface (ns->op[i], 0, &actual);
4993 }
4994
4995 if (sym != NULL)
4996 break;
4997 }
4998
4999 /* F2018(15.4.3.4.2) requires that the use of unlimited polymorphic
5000 formal arguments does not override the intrinsic uses. */
5001 gfc_push_suppress_errors ();
5002 if (sym
5003 && (UNLIMITED_POLY (sym->formal->sym)
5004 || (sym->formal->next
5005 && UNLIMITED_POLY (sym->formal->next->sym)))
5006 && !gfc_check_operator_interface (sym, e->value.op.op, e->where))
5007 sym = NULL;
5008 gfc_pop_suppress_errors ();
5009 }
5010
5011 /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
5012 found rather than just taking the first one and not checking further. */
5013
5014 if (sym == NULL)
5015 {
5016 /* Don't use gfc_free_actual_arglist(). */
5017 free (actual->next);
5018 free (actual);
5019 return MATCH_NO;
5020 }
5021
5022 /* Change the expression node to a function call. */
5023 e->expr_type = EXPR_FUNCTION;
5024 e->symtree = gfc_find_sym_in_symtree (sym);
5025 e->value.function.actual = actual;
5026 e->value.function.esym = NULL;
5027 e->value.function.isym = NULL;
5028 e->value.function.name = NULL;
5029 e->user_operator = 1;
5030
5031 if (!gfc_resolve_expr (e))
5032 return MATCH_ERROR;
5033
5034 return MATCH_YES;
5035 }
5036
5037
5038 /* Tries to replace an assignment code node with a subroutine call to the
5039 subroutine associated with the assignment operator. Return true if the node
5040 was replaced. On false, no error is generated. */
5041
5042 bool
5043 gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
5044 {
5045 gfc_actual_arglist *actual;
5046 gfc_expr *lhs, *rhs, *tb_base;
5047 gfc_symbol *sym = NULL;
5048 const char *gname = NULL;
5049 gfc_typebound_proc* tbo;
5050
5051 lhs = c->expr1;
5052 rhs = c->expr2;
5053
5054 /* Don't allow an intrinsic assignment with a BOZ rhs to be replaced. */
5055 if (c->op == EXEC_ASSIGN
5056 && c->expr1->expr_type == EXPR_VARIABLE
5057 && c->expr2->expr_type == EXPR_CONSTANT && c->expr2->ts.type == BT_BOZ)
5058 return false;
5059
5060 /* Don't allow an intrinsic assignment to be replaced. */
5061 if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
5062 && (rhs->rank == 0 || rhs->rank == lhs->rank)
5063 && (lhs->ts.type == rhs->ts.type
5064 || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
5065 return false;
5066
5067 actual = gfc_get_actual_arglist ();
5068 actual->expr = lhs;
5069
5070 actual->next = gfc_get_actual_arglist ();
5071 actual->next->expr = rhs;
5072
5073 /* TODO: Ambiguity-check, see above for gfc_extend_expr. */
5074
5075 /* See if we find a matching type-bound assignment. */
5076 tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
5077 NULL, &gname);
5078
5079 if (tbo)
5080 {
5081 /* Success: Replace the expression with a type-bound call. */
5082 gcc_assert (tb_base);
5083 c->expr1 = gfc_get_expr ();
5084 build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
5085 c->expr1->value.compcall.assign = 1;
5086 c->expr1->where = c->loc;
5087 c->expr2 = NULL;
5088 c->op = EXEC_COMPCALL;
5089 return true;
5090 }
5091
5092 /* See if we find an 'ordinary' (non-typebound) assignment procedure. */
5093 for (; ns; ns = ns->parent)
5094 {
5095 sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
5096 if (sym != NULL)
5097 break;
5098 }
5099
5100 if (sym)
5101 {
5102 /* Success: Replace the assignment with the call. */
5103 c->op = EXEC_ASSIGN_CALL;
5104 c->symtree = gfc_find_sym_in_symtree (sym);
5105 c->expr1 = NULL;
5106 c->expr2 = NULL;
5107 c->ext.actual = actual;
5108 return true;
5109 }
5110
5111 /* Failure: No assignment procedure found. */
5112 free (actual->next);
5113 free (actual);
5114 return false;
5115 }
5116
5117
5118 /* Make sure that the interface just parsed is not already present in
5119 the given interface list. Ambiguity isn't checked yet since module
5120 procedures can be present without interfaces. */
5121
5122 bool
5123 gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
5124 {
5125 gfc_interface *ip;
5126
5127 for (ip = base; ip; ip = ip->next)
5128 {
5129 if (ip->sym == new_sym)
5130 {
5131 gfc_error ("Entity %qs at %L is already present in the interface",
5132 new_sym->name, &loc);
5133 return false;
5134 }
5135 }
5136
5137 return true;
5138 }
5139
5140
5141 /* Add a symbol to the current interface. */
5142
5143 bool
5144 gfc_add_interface (gfc_symbol *new_sym)
5145 {
5146 gfc_interface **head, *intr;
5147 gfc_namespace *ns;
5148 gfc_symbol *sym;
5149
5150 switch (current_interface.type)
5151 {
5152 case INTERFACE_NAMELESS:
5153 case INTERFACE_ABSTRACT:
5154 return true;
5155
5156 case INTERFACE_INTRINSIC_OP:
5157 for (ns = current_interface.ns; ns; ns = ns->parent)
5158 switch (current_interface.op)
5159 {
5160 case INTRINSIC_EQ:
5161 case INTRINSIC_EQ_OS:
5162 if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym,
5163 gfc_current_locus)
5164 || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS],
5165 new_sym, gfc_current_locus))
5166 return false;
5167 break;
5168
5169 case INTRINSIC_NE:
5170 case INTRINSIC_NE_OS:
5171 if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym,
5172 gfc_current_locus)
5173 || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS],
5174 new_sym, gfc_current_locus))
5175 return false;
5176 break;
5177
5178 case INTRINSIC_GT:
5179 case INTRINSIC_GT_OS:
5180 if (!gfc_check_new_interface (ns->op[INTRINSIC_GT],
5181 new_sym, gfc_current_locus)
5182 || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS],
5183 new_sym, gfc_current_locus))
5184 return false;
5185 break;
5186
5187 case INTRINSIC_GE:
5188 case INTRINSIC_GE_OS:
5189 if (!gfc_check_new_interface (ns->op[INTRINSIC_GE],
5190 new_sym, gfc_current_locus)
5191 || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS],
5192 new_sym, gfc_current_locus))
5193 return false;
5194 break;
5195
5196 case INTRINSIC_LT:
5197 case INTRINSIC_LT_OS:
5198 if (!gfc_check_new_interface (ns->op[INTRINSIC_LT],
5199 new_sym, gfc_current_locus)
5200 || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS],
5201 new_sym, gfc_current_locus))
5202 return false;
5203 break;
5204
5205 case INTRINSIC_LE:
5206 case INTRINSIC_LE_OS:
5207 if (!gfc_check_new_interface (ns->op[INTRINSIC_LE],
5208 new_sym, gfc_current_locus)
5209 || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS],
5210 new_sym, gfc_current_locus))
5211 return false;
5212 break;
5213
5214 default:
5215 if (!gfc_check_new_interface (ns->op[current_interface.op],
5216 new_sym, gfc_current_locus))
5217 return false;
5218 }
5219
5220 head = &current_interface.ns->op[current_interface.op];
5221 break;
5222
5223 case INTERFACE_GENERIC:
5224 case INTERFACE_DTIO:
5225 for (ns = current_interface.ns; ns; ns = ns->parent)
5226 {
5227 gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
5228 if (sym == NULL)
5229 continue;
5230
5231 if (!gfc_check_new_interface (sym->generic,
5232 new_sym, gfc_current_locus))
5233 return false;
5234 }
5235
5236 head = &current_interface.sym->generic;
5237 break;
5238
5239 case INTERFACE_USER_OP:
5240 if (!gfc_check_new_interface (current_interface.uop->op,
5241 new_sym, gfc_current_locus))
5242 return false;
5243
5244 head = &current_interface.uop->op;
5245 break;
5246
5247 default:
5248 gfc_internal_error ("gfc_add_interface(): Bad interface type");
5249 }
5250
5251 intr = gfc_get_interface ();
5252 intr->sym = new_sym;
5253 intr->where = gfc_current_locus;
5254
5255 intr->next = *head;
5256 *head = intr;
5257
5258 return true;
5259 }
5260
5261
5262 gfc_interface *&
5263 gfc_current_interface_head (void)
5264 {
5265 switch (current_interface.type)
5266 {
5267 case INTERFACE_INTRINSIC_OP:
5268 return current_interface.ns->op[current_interface.op];
5269
5270 case INTERFACE_GENERIC:
5271 case INTERFACE_DTIO:
5272 return current_interface.sym->generic;
5273
5274 case INTERFACE_USER_OP:
5275 return current_interface.uop->op;
5276
5277 default:
5278 gcc_unreachable ();
5279 }
5280 }
5281
5282
5283 void
5284 gfc_set_current_interface_head (gfc_interface *i)
5285 {
5286 switch (current_interface.type)
5287 {
5288 case INTERFACE_INTRINSIC_OP:
5289 current_interface.ns->op[current_interface.op] = i;
5290 break;
5291
5292 case INTERFACE_GENERIC:
5293 case INTERFACE_DTIO:
5294 current_interface.sym->generic = i;
5295 break;
5296
5297 case INTERFACE_USER_OP:
5298 current_interface.uop->op = i;
5299 break;
5300
5301 default:
5302 gcc_unreachable ();
5303 }
5304 }
5305
5306
5307 /* Gets rid of a formal argument list. We do not free symbols.
5308 Symbols are freed when a namespace is freed. */
5309
5310 void
5311 gfc_free_formal_arglist (gfc_formal_arglist *p)
5312 {
5313 gfc_formal_arglist *q;
5314
5315 for (; p; p = q)
5316 {
5317 q = p->next;
5318 free (p);
5319 }
5320 }
5321
5322
5323 /* Check that it is ok for the type-bound procedure 'proc' to override the
5324 procedure 'old', cf. F08:4.5.7.3. */
5325
5326 bool
5327 gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
5328 {
5329 locus where;
5330 gfc_symbol *proc_target, *old_target;
5331 unsigned proc_pass_arg, old_pass_arg, argpos;
5332 gfc_formal_arglist *proc_formal, *old_formal;
5333 bool check_type;
5334 char err[200];
5335
5336 /* This procedure should only be called for non-GENERIC proc. */
5337 gcc_assert (!proc->n.tb->is_generic);
5338
5339 /* If the overwritten procedure is GENERIC, this is an error. */
5340 if (old->n.tb->is_generic)
5341 {
5342 gfc_error ("Cannot overwrite GENERIC %qs at %L",
5343 old->name, &proc->n.tb->where);
5344 return false;
5345 }
5346
5347 where = proc->n.tb->where;
5348 proc_target = proc->n.tb->u.specific->n.sym;
5349 old_target = old->n.tb->u.specific->n.sym;
5350
5351 /* Check that overridden binding is not NON_OVERRIDABLE. */
5352 if (old->n.tb->non_overridable)
5353 {
5354 gfc_error ("%qs at %L overrides a procedure binding declared"
5355 " NON_OVERRIDABLE", proc->name, &where);
5356 return false;
5357 }
5358
5359 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
5360 if (!old->n.tb->deferred && proc->n.tb->deferred)
5361 {
5362 gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
5363 " non-DEFERRED binding", proc->name, &where);
5364 return false;
5365 }
5366
5367 /* If the overridden binding is PURE, the overriding must be, too. */
5368 if (old_target->attr.pure && !proc_target->attr.pure)
5369 {
5370 gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
5371 proc->name, &where);
5372 return false;
5373 }
5374
5375 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
5376 is not, the overriding must not be either. */
5377 if (old_target->attr.elemental && !proc_target->attr.elemental)
5378 {
5379 gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
5380 " ELEMENTAL", proc->name, &where);
5381 return false;
5382 }
5383 if (!old_target->attr.elemental && proc_target->attr.elemental)
5384 {
5385 gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
5386 " be ELEMENTAL, either", proc->name, &where);
5387 return false;
5388 }
5389
5390 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
5391 SUBROUTINE. */
5392 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
5393 {
5394 gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
5395 " SUBROUTINE", proc->name, &where);
5396 return false;
5397 }
5398
5399 /* If the overridden binding is a FUNCTION, the overriding must also be a
5400 FUNCTION and have the same characteristics. */
5401 if (old_target->attr.function)
5402 {
5403 if (!proc_target->attr.function)
5404 {
5405 gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
5406 " FUNCTION", proc->name, &where);
5407 return false;
5408 }
5409
5410 if (!gfc_check_result_characteristics (proc_target, old_target,
5411 err, sizeof(err)))
5412 {
5413 gfc_error ("Result mismatch for the overriding procedure "
5414 "%qs at %L: %s", proc->name, &where, err);
5415 return false;
5416 }
5417 }
5418
5419 /* If the overridden binding is PUBLIC, the overriding one must not be
5420 PRIVATE. */
5421 if (old->n.tb->access == ACCESS_PUBLIC
5422 && proc->n.tb->access == ACCESS_PRIVATE)
5423 {
5424 gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
5425 " PRIVATE", proc->name, &where);
5426 return false;
5427 }
5428
5429 /* Compare the formal argument lists of both procedures. This is also abused
5430 to find the position of the passed-object dummy arguments of both
5431 bindings as at least the overridden one might not yet be resolved and we
5432 need those positions in the check below. */
5433 proc_pass_arg = old_pass_arg = 0;
5434 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
5435 proc_pass_arg = 1;
5436 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
5437 old_pass_arg = 1;
5438 argpos = 1;
5439 proc_formal = gfc_sym_get_dummy_args (proc_target);
5440 old_formal = gfc_sym_get_dummy_args (old_target);
5441 for ( ; proc_formal && old_formal;
5442 proc_formal = proc_formal->next, old_formal = old_formal->next)
5443 {
5444 if (proc->n.tb->pass_arg
5445 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
5446 proc_pass_arg = argpos;
5447 if (old->n.tb->pass_arg
5448 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
5449 old_pass_arg = argpos;
5450
5451 /* Check that the names correspond. */
5452 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
5453 {
5454 gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
5455 " to match the corresponding argument of the overridden"
5456 " procedure", proc_formal->sym->name, proc->name, &where,
5457 old_formal->sym->name);
5458 return false;
5459 }
5460
5461 check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
5462 if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
5463 check_type, err, sizeof(err)))
5464 {
5465 gfc_error_opt (0, "Argument mismatch for the overriding procedure "
5466 "%qs at %L: %s", proc->name, &where, err);
5467 return false;
5468 }
5469
5470 ++argpos;
5471 }
5472 if (proc_formal || old_formal)
5473 {
5474 gfc_error ("%qs at %L must have the same number of formal arguments as"
5475 " the overridden procedure", proc->name, &where);
5476 return false;
5477 }
5478
5479 /* If the overridden binding is NOPASS, the overriding one must also be
5480 NOPASS. */
5481 if (old->n.tb->nopass && !proc->n.tb->nopass)
5482 {
5483 gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
5484 " NOPASS", proc->name, &where);
5485 return false;
5486 }
5487
5488 /* If the overridden binding is PASS(x), the overriding one must also be
5489 PASS and the passed-object dummy arguments must correspond. */
5490 if (!old->n.tb->nopass)
5491 {
5492 if (proc->n.tb->nopass)
5493 {
5494 gfc_error ("%qs at %L overrides a binding with PASS and must also be"
5495 " PASS", proc->name, &where);
5496 return false;
5497 }
5498
5499 if (proc_pass_arg != old_pass_arg)
5500 {
5501 gfc_error ("Passed-object dummy argument of %qs at %L must be at"
5502 " the same position as the passed-object dummy argument of"
5503 " the overridden procedure", proc->name, &where);
5504 return false;
5505 }
5506 }
5507
5508 return true;
5509 }
5510
5511
5512 /* The following three functions check that the formal arguments
5513 of user defined derived type IO procedures are compliant with
5514 the requirements of the standard, see F03:9.5.3.7.2 (F08:9.6.4.8.3). */
5515
5516 static void
5517 check_dtio_arg_TKR_intent (gfc_symbol *fsym, bool typebound, bt type,
5518 int kind, int rank, sym_intent intent)
5519 {
5520 if (fsym->ts.type != type)
5521 {
5522 gfc_error ("DTIO dummy argument at %L must be of type %s",
5523 &fsym->declared_at, gfc_basic_typename (type));
5524 return;
5525 }
5526
5527 if (fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED
5528 && fsym->ts.kind != kind)
5529 gfc_error ("DTIO dummy argument at %L must be of KIND = %d",
5530 &fsym->declared_at, kind);
5531
5532 if (!typebound
5533 && rank == 0
5534 && (((type == BT_CLASS) && CLASS_DATA (fsym)->attr.dimension)
5535 || ((type != BT_CLASS) && fsym->attr.dimension)))
5536 gfc_error ("DTIO dummy argument at %L must be a scalar",
5537 &fsym->declared_at);
5538 else if (rank == 1
5539 && (fsym->as == NULL || fsym->as->type != AS_ASSUMED_SHAPE))
5540 gfc_error ("DTIO dummy argument at %L must be an "
5541 "ASSUMED SHAPE ARRAY", &fsym->declared_at);
5542
5543 if (type == BT_CHARACTER && fsym->ts.u.cl->length != NULL)
5544 gfc_error ("DTIO character argument at %L must have assumed length",
5545 &fsym->declared_at);
5546
5547 if (fsym->attr.intent != intent)
5548 gfc_error ("DTIO dummy argument at %L must have INTENT %s",
5549 &fsym->declared_at, gfc_code2string (intents, (int)intent));
5550 return;
5551 }
5552
5553
5554 static void
5555 check_dtio_interface1 (gfc_symbol *derived, gfc_symtree *tb_io_st,
5556 bool typebound, bool formatted, int code)
5557 {
5558 gfc_symbol *dtio_sub, *generic_proc, *fsym;
5559 gfc_typebound_proc *tb_io_proc, *specific_proc;
5560 gfc_interface *intr;
5561 gfc_formal_arglist *formal;
5562 int arg_num;
5563
5564 bool read = ((dtio_codes)code == DTIO_RF)
5565 || ((dtio_codes)code == DTIO_RUF);
5566 bt type;
5567 sym_intent intent;
5568 int kind;
5569
5570 dtio_sub = NULL;
5571 if (typebound)
5572 {
5573 /* Typebound DTIO binding. */
5574 tb_io_proc = tb_io_st->n.tb;
5575 if (tb_io_proc == NULL)
5576 return;
5577
5578 gcc_assert (tb_io_proc->is_generic);
5579
5580 specific_proc = tb_io_proc->u.generic->specific;
5581 if (specific_proc == NULL || specific_proc->is_generic)
5582 return;
5583
5584 dtio_sub = specific_proc->u.specific->n.sym;
5585 }
5586 else
5587 {
5588 generic_proc = tb_io_st->n.sym;
5589 if (generic_proc == NULL || generic_proc->generic == NULL)
5590 return;
5591
5592 for (intr = tb_io_st->n.sym->generic; intr; intr = intr->next)
5593 {
5594 if (intr->sym && intr->sym->formal && intr->sym->formal->sym
5595 && ((intr->sym->formal->sym->ts.type == BT_CLASS
5596 && CLASS_DATA (intr->sym->formal->sym)->ts.u.derived
5597 == derived)
5598 || (intr->sym->formal->sym->ts.type == BT_DERIVED
5599 && intr->sym->formal->sym->ts.u.derived == derived)))
5600 {
5601 dtio_sub = intr->sym;
5602 break;
5603 }
5604 else if (intr->sym && intr->sym->formal && !intr->sym->formal->sym)
5605 {
5606 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5607 "procedure", &intr->sym->declared_at);
5608 return;
5609 }
5610 }
5611
5612 if (dtio_sub == NULL)
5613 return;
5614 }
5615
5616 gcc_assert (dtio_sub);
5617 if (!dtio_sub->attr.subroutine)
5618 gfc_error ("DTIO procedure %qs at %L must be a subroutine",
5619 dtio_sub->name, &dtio_sub->declared_at);
5620
5621 if (!dtio_sub->resolve_symbol_called)
5622 gfc_resolve_formal_arglist (dtio_sub);
5623
5624 arg_num = 0;
5625 for (formal = dtio_sub->formal; formal; formal = formal->next)
5626 arg_num++;
5627
5628 if (arg_num < (formatted ? 6 : 4))
5629 {
5630 gfc_error ("Too few dummy arguments in DTIO procedure %qs at %L",
5631 dtio_sub->name, &dtio_sub->declared_at);
5632 return;
5633 }
5634
5635 if (arg_num > (formatted ? 6 : 4))
5636 {
5637 gfc_error ("Too many dummy arguments in DTIO procedure %qs at %L",
5638 dtio_sub->name, &dtio_sub->declared_at);
5639 return;
5640 }
5641
5642 /* Now go through the formal arglist. */
5643 arg_num = 1;
5644 for (formal = dtio_sub->formal; formal; formal = formal->next, arg_num++)
5645 {
5646 if (!formatted && arg_num == 3)
5647 arg_num = 5;
5648 fsym = formal->sym;
5649
5650 if (fsym == NULL)
5651 {
5652 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5653 "procedure", &dtio_sub->declared_at);
5654 return;
5655 }
5656
5657 switch (arg_num)
5658 {
5659 case(1): /* DTV */
5660 type = derived->attr.sequence || derived->attr.is_bind_c ?
5661 BT_DERIVED : BT_CLASS;
5662 kind = 0;
5663 intent = read ? INTENT_INOUT : INTENT_IN;
5664 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5665 0, intent);
5666 break;
5667
5668 case(2): /* UNIT */
5669 type = BT_INTEGER;
5670 kind = gfc_default_integer_kind;
5671 intent = INTENT_IN;
5672 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5673 0, intent);
5674 break;
5675 case(3): /* IOTYPE */
5676 type = BT_CHARACTER;
5677 kind = gfc_default_character_kind;
5678 intent = INTENT_IN;
5679 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5680 0, intent);
5681 break;
5682 case(4): /* VLIST */
5683 type = BT_INTEGER;
5684 kind = gfc_default_integer_kind;
5685 intent = INTENT_IN;
5686 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5687 1, intent);
5688 break;
5689 case(5): /* IOSTAT */
5690 type = BT_INTEGER;
5691 kind = gfc_default_integer_kind;
5692 intent = INTENT_OUT;
5693 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5694 0, intent);
5695 break;
5696 case(6): /* IOMSG */
5697 type = BT_CHARACTER;
5698 kind = gfc_default_character_kind;
5699 intent = INTENT_INOUT;
5700 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5701 0, intent);
5702 break;
5703 default:
5704 gcc_unreachable ();
5705 }
5706 }
5707 derived->attr.has_dtio_procs = 1;
5708 return;
5709 }
5710
5711 void
5712 gfc_check_dtio_interfaces (gfc_symbol *derived)
5713 {
5714 gfc_symtree *tb_io_st;
5715 bool t = false;
5716 int code;
5717 bool formatted;
5718
5719 if (derived->attr.is_class == 1 || derived->attr.vtype == 1)
5720 return;
5721
5722 /* Check typebound DTIO bindings. */
5723 for (code = 0; code < 4; code++)
5724 {
5725 formatted = ((dtio_codes)code == DTIO_RF)
5726 || ((dtio_codes)code == DTIO_WF);
5727
5728 tb_io_st = gfc_find_typebound_proc (derived, &t,
5729 gfc_code2string (dtio_procs, code),
5730 true, &derived->declared_at);
5731 if (tb_io_st != NULL)
5732 check_dtio_interface1 (derived, tb_io_st, true, formatted, code);
5733 }
5734
5735 /* Check generic DTIO interfaces. */
5736 for (code = 0; code < 4; code++)
5737 {
5738 formatted = ((dtio_codes)code == DTIO_RF)
5739 || ((dtio_codes)code == DTIO_WF);
5740
5741 tb_io_st = gfc_find_symtree (derived->ns->sym_root,
5742 gfc_code2string (dtio_procs, code));
5743 if (tb_io_st != NULL)
5744 check_dtio_interface1 (derived, tb_io_st, false, formatted, code);
5745 }
5746 }
5747
5748
5749 gfc_symtree*
5750 gfc_find_typebound_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5751 {
5752 gfc_symtree *tb_io_st = NULL;
5753 bool t = false;
5754
5755 if (!derived || !derived->resolve_symbol_called
5756 || derived->attr.flavor != FL_DERIVED)
5757 return NULL;
5758
5759 /* Try to find a typebound DTIO binding. */
5760 if (formatted == true)
5761 {
5762 if (write == true)
5763 tb_io_st = gfc_find_typebound_proc (derived, &t,
5764 gfc_code2string (dtio_procs,
5765 DTIO_WF),
5766 true,
5767 &derived->declared_at);
5768 else
5769 tb_io_st = gfc_find_typebound_proc (derived, &t,
5770 gfc_code2string (dtio_procs,
5771 DTIO_RF),
5772 true,
5773 &derived->declared_at);
5774 }
5775 else
5776 {
5777 if (write == true)
5778 tb_io_st = gfc_find_typebound_proc (derived, &t,
5779 gfc_code2string (dtio_procs,
5780 DTIO_WUF),
5781 true,
5782 &derived->declared_at);
5783 else
5784 tb_io_st = gfc_find_typebound_proc (derived, &t,
5785 gfc_code2string (dtio_procs,
5786 DTIO_RUF),
5787 true,
5788 &derived->declared_at);
5789 }
5790 return tb_io_st;
5791 }
5792
5793
5794 gfc_symbol *
5795 gfc_find_specific_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5796 {
5797 gfc_symtree *tb_io_st = NULL;
5798 gfc_symbol *dtio_sub = NULL;
5799 gfc_symbol *extended;
5800 gfc_typebound_proc *tb_io_proc, *specific_proc;
5801
5802 tb_io_st = gfc_find_typebound_dtio_proc (derived, write, formatted);
5803
5804 if (tb_io_st != NULL)
5805 {
5806 const char *genname;
5807 gfc_symtree *st;
5808
5809 tb_io_proc = tb_io_st->n.tb;
5810 gcc_assert (tb_io_proc != NULL);
5811 gcc_assert (tb_io_proc->is_generic);
5812 gcc_assert (tb_io_proc->u.generic->next == NULL);
5813
5814 specific_proc = tb_io_proc->u.generic->specific;
5815 gcc_assert (!specific_proc->is_generic);
5816
5817 /* Go back and make sure that we have the right specific procedure.
5818 Here we most likely have a procedure from the parent type, which
5819 can be overridden in extensions. */
5820 genname = tb_io_proc->u.generic->specific_st->name;
5821 st = gfc_find_typebound_proc (derived, NULL, genname,
5822 true, &tb_io_proc->where);
5823 if (st)
5824 dtio_sub = st->n.tb->u.specific->n.sym;
5825 else
5826 dtio_sub = specific_proc->u.specific->n.sym;
5827
5828 goto finish;
5829 }
5830
5831 /* If there is not a typebound binding, look for a generic
5832 DTIO interface. */
5833 for (extended = derived; extended;
5834 extended = gfc_get_derived_super_type (extended))
5835 {
5836 if (extended == NULL || extended->ns == NULL
5837 || extended->attr.flavor == FL_UNKNOWN)
5838 return NULL;
5839
5840 if (formatted == true)
5841 {
5842 if (write == true)
5843 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5844 gfc_code2string (dtio_procs,
5845 DTIO_WF));
5846 else
5847 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5848 gfc_code2string (dtio_procs,
5849 DTIO_RF));
5850 }
5851 else
5852 {
5853 if (write == true)
5854 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5855 gfc_code2string (dtio_procs,
5856 DTIO_WUF));
5857 else
5858 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5859 gfc_code2string (dtio_procs,
5860 DTIO_RUF));
5861 }
5862
5863 if (tb_io_st != NULL
5864 && tb_io_st->n.sym
5865 && tb_io_st->n.sym->generic)
5866 {
5867 for (gfc_interface *intr = tb_io_st->n.sym->generic;
5868 intr && intr->sym; intr = intr->next)
5869 {
5870 if (intr->sym->formal)
5871 {
5872 gfc_symbol *fsym = intr->sym->formal->sym;
5873 if ((fsym->ts.type == BT_CLASS
5874 && CLASS_DATA (fsym)->ts.u.derived == extended)
5875 || (fsym->ts.type == BT_DERIVED
5876 && fsym->ts.u.derived == extended))
5877 {
5878 dtio_sub = intr->sym;
5879 break;
5880 }
5881 }
5882 }
5883 }
5884 }
5885
5886 finish:
5887 if (dtio_sub
5888 && dtio_sub->formal->sym->ts.type == BT_CLASS
5889 && derived != CLASS_DATA (dtio_sub->formal->sym)->ts.u.derived)
5890 gfc_find_derived_vtab (derived);
5891
5892 return dtio_sub;
5893 }
5894
5895 /* Helper function - if we do not find an interface for a procedure,
5896 construct it from the actual arglist. Luckily, this can only
5897 happen for call by reference, so the information we actually need
5898 to provide (and which would be impossible to guess from the call
5899 itself) is not actually needed. */
5900
5901 void
5902 gfc_get_formal_from_actual_arglist (gfc_symbol *sym,
5903 gfc_actual_arglist *actual_args)
5904 {
5905 gfc_actual_arglist *a;
5906 gfc_formal_arglist **f;
5907 gfc_symbol *s;
5908 char name[GFC_MAX_SYMBOL_LEN + 1];
5909 static int var_num;
5910
5911 /* Do not infer the formal from actual arguments if we are dealing with
5912 classes. */
5913
5914 if (sym->ts.type == BT_CLASS)
5915 return;
5916
5917 f = &sym->formal;
5918 for (a = actual_args; a != NULL; a = a->next)
5919 {
5920 (*f) = gfc_get_formal_arglist ();
5921 if (a->expr)
5922 {
5923 snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
5924 gfc_get_symbol (name, gfc_current_ns, &s);
5925 if (a->expr->ts.type == BT_PROCEDURE)
5926 {
5927 gfc_symbol *asym = a->expr->symtree->n.sym;
5928 s->attr.flavor = FL_PROCEDURE;
5929 if (asym->attr.function)
5930 {
5931 s->attr.function = 1;
5932 s->ts = asym->ts;
5933 }
5934 s->attr.subroutine = asym->attr.subroutine;
5935 }
5936 else
5937 {
5938 s->ts = a->expr->ts;
5939
5940 if (s->ts.type == BT_CHARACTER)
5941 s->ts.u.cl = gfc_get_charlen ();
5942
5943 s->ts.deferred = 0;
5944 s->ts.is_iso_c = 0;
5945 s->ts.is_c_interop = 0;
5946 s->attr.flavor = FL_VARIABLE;
5947 if (a->expr->rank > 0)
5948 {
5949 s->attr.dimension = 1;
5950 s->as = gfc_get_array_spec ();
5951 s->as->rank = 1;
5952 s->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5953 &a->expr->where, 1);
5954 s->as->upper[0] = NULL;
5955 s->as->type = AS_ASSUMED_SIZE;
5956 }
5957 else
5958 s->maybe_array = maybe_dummy_array_arg (a->expr);
5959 }
5960 s->attr.dummy = 1;
5961 s->attr.artificial = 1;
5962 s->declared_at = a->expr->where;
5963 s->attr.intent = INTENT_UNKNOWN;
5964 (*f)->sym = s;
5965 gfc_commit_symbol (s);
5966 }
5967 else /* If a->expr is NULL, this is an alternate rerturn. */
5968 (*f)->sym = NULL;
5969
5970 f = &((*f)->next);
5971 }
5972
5973 }
5974
5975
5976 const char *
5977 gfc_dummy_arg_get_name (gfc_dummy_arg & dummy_arg)
5978 {
5979 switch (dummy_arg.intrinsicness)
5980 {
5981 case GFC_INTRINSIC_DUMMY_ARG:
5982 return dummy_arg.u.intrinsic->name;
5983
5984 case GFC_NON_INTRINSIC_DUMMY_ARG:
5985 return dummy_arg.u.non_intrinsic->sym->name;
5986
5987 default:
5988 gcc_unreachable ();
5989 }
5990 }
5991
5992
5993 const gfc_typespec &
5994 gfc_dummy_arg_get_typespec (gfc_dummy_arg & dummy_arg)
5995 {
5996 switch (dummy_arg.intrinsicness)
5997 {
5998 case GFC_INTRINSIC_DUMMY_ARG:
5999 return dummy_arg.u.intrinsic->ts;
6000
6001 case GFC_NON_INTRINSIC_DUMMY_ARG:
6002 return dummy_arg.u.non_intrinsic->sym->ts;
6003
6004 default:
6005 gcc_unreachable ();
6006 }
6007 }
6008
6009
6010 bool
6011 gfc_dummy_arg_is_optional (gfc_dummy_arg & dummy_arg)
6012 {
6013 switch (dummy_arg.intrinsicness)
6014 {
6015 case GFC_INTRINSIC_DUMMY_ARG:
6016 return dummy_arg.u.intrinsic->optional;
6017
6018 case GFC_NON_INTRINSIC_DUMMY_ARG:
6019 return dummy_arg.u.non_intrinsic->sym->attr.optional;
6020
6021 default:
6022 gcc_unreachable ();
6023 }
6024 }