]> git.ipfire.org Git - thirdparty/gcc.git/blame_incremental - gcc/fortran/interface.cc
c++: -Wtemplate-body and tentative parsing [PR120575]
[thirdparty/gcc.git] / gcc / fortran / interface.cc
... / ...
CommitLineData
1/* Deal with interfaces.
2 Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
78gfc_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
86static void
87free_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
101void
102gfc_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
113void
114gfc_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
128static gfc_intrinsic_op
129fold_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
151static gfc_intrinsic_op
152dtio_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
166match
167gfc_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
253syntax:
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
262match
263gfc_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
329match
330gfc_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
354match
355gfc_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
481static bool
482is_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
497static bool
498is_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
512static bool
513compare_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
581static bool
582compare_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
655bool
656gfc_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
741bool
742gfc_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
806static bool
807compare_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
816static bool
817compare_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
829static bool
830compare_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
859static bool
860compare_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
870static bool
871compare_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
913static gfc_symbol *
914find_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
930bool
931gfc_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
1162bad_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
1178static bool
1179count_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
1279static bool
1280compare_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
1313static bool
1314generic_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
1365static int
1366symbol_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
1382bool
1383gfc_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 /* Sometimes the ambiguity between deferred shape and assumed shape
1545 does not get resolved in module procedures, where the only explicit
1546 declaration of the dummy is in the interface. */
1547 if (s1->ns->proc_name && s1->ns->proc_name->attr.module_procedure
1548 && s1->as->type == AS_ASSUMED_SHAPE
1549 && s2->as->type == AS_DEFERRED)
1550 {
1551 s2->as->type = AS_ASSUMED_SHAPE;
1552 for (i = 0; i < s2->as->rank; i++)
1553 if (s1->as->lower[i] != NULL)
1554 s2->as->lower[i] = gfc_copy_expr (s1->as->lower[i]);
1555 }
1556
1557 if (s1->as->type != s2->as->type)
1558 {
1559 snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
1560 s1->name);
1561 return false;
1562 }
1563
1564 if (s1->as->corank != s2->as->corank)
1565 {
1566 snprintf (errmsg, err_len, "Corank mismatch in argument '%s' (%i/%i)",
1567 s1->name, s1->as->corank, s2->as->corank);
1568 return false;
1569 }
1570
1571 if (s1->as->type == AS_EXPLICIT)
1572 for (i = 0; i < s1->as->rank + MAX (0, s1->as->corank-1); i++)
1573 {
1574 shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
1575 gfc_copy_expr (s1->as->lower[i]));
1576 shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
1577 gfc_copy_expr (s2->as->lower[i]));
1578 compval = gfc_dep_compare_expr (shape1, shape2);
1579 gfc_free_expr (shape1);
1580 gfc_free_expr (shape2);
1581 switch (compval)
1582 {
1583 case -1:
1584 case 1:
1585 case -3:
1586 if (i < s1->as->rank)
1587 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of"
1588 " argument '%s'", i + 1, s1->name);
1589 else
1590 snprintf (errmsg, err_len, "Shape mismatch in codimension %i "
1591 "of argument '%s'", i - s1->as->rank + 1, s1->name);
1592 return false;
1593
1594 case -2:
1595 /* FIXME: Implement a warning for this case.
1596 gfc_warning (0, "Possible shape mismatch in argument %qs",
1597 s1->name);*/
1598 break;
1599
1600 case 0:
1601 break;
1602
1603 default:
1604 gfc_internal_error ("check_dummy_characteristics: Unexpected "
1605 "result %i of gfc_dep_compare_expr",
1606 compval);
1607 break;
1608 }
1609 }
1610 }
1611
1612 return true;
1613}
1614
1615
1616/* Check if the characteristics of two function results match,
1617 cf. F08:12.3.3. */
1618
1619bool
1620gfc_check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
1621 char *errmsg, int err_len)
1622{
1623 gfc_symbol *r1, *r2;
1624
1625 if (s1->ts.interface && s1->ts.interface->result)
1626 r1 = s1->ts.interface->result;
1627 else
1628 r1 = s1->result ? s1->result : s1;
1629
1630 if (s2->ts.interface && s2->ts.interface->result)
1631 r2 = s2->ts.interface->result;
1632 else
1633 r2 = s2->result ? s2->result : s2;
1634
1635 if (r1->ts.type == BT_UNKNOWN)
1636 return true;
1637
1638 /* Check type and rank. */
1639 if (!compare_type_characteristics (r1, r2))
1640 {
1641 snprintf (errmsg, err_len, "Type mismatch in function result (%s/%s)",
1642 gfc_typename (&r1->ts), gfc_typename (&r2->ts));
1643 return false;
1644 }
1645 if (!compare_rank (r1, r2))
1646 {
1647 snprintf (errmsg, err_len, "Rank mismatch in function result (%i/%i)",
1648 symbol_rank (r1), symbol_rank (r2));
1649 return false;
1650 }
1651
1652 /* Check ALLOCATABLE attribute. */
1653 if (r1->attr.allocatable != r2->attr.allocatable)
1654 {
1655 snprintf (errmsg, err_len, "ALLOCATABLE attribute mismatch in "
1656 "function result");
1657 return false;
1658 }
1659
1660 /* Check POINTER attribute. */
1661 if (r1->attr.pointer != r2->attr.pointer)
1662 {
1663 snprintf (errmsg, err_len, "POINTER attribute mismatch in "
1664 "function result");
1665 return false;
1666 }
1667
1668 /* Check CONTIGUOUS attribute. */
1669 if (r1->attr.contiguous != r2->attr.contiguous)
1670 {
1671 snprintf (errmsg, err_len, "CONTIGUOUS attribute mismatch in "
1672 "function result");
1673 return false;
1674 }
1675
1676 /* Check PROCEDURE POINTER attribute. */
1677 if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
1678 {
1679 snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
1680 "function result");
1681 return false;
1682 }
1683
1684 /* Check string length. */
1685 if (r1->ts.type == BT_CHARACTER && r1->ts.u.cl && r2->ts.u.cl)
1686 {
1687 if (r1->ts.deferred != r2->ts.deferred)
1688 {
1689 snprintf (errmsg, err_len, "Character length mismatch "
1690 "in function result");
1691 return false;
1692 }
1693
1694 if (r1->ts.u.cl->length && r2->ts.u.cl->length)
1695 {
1696 int compval = gfc_dep_compare_expr (r1->ts.u.cl->length,
1697 r2->ts.u.cl->length);
1698 switch (compval)
1699 {
1700 case -1:
1701 case 1:
1702 case -3:
1703 snprintf (errmsg, err_len, "Character length mismatch "
1704 "in function result");
1705 return false;
1706
1707 case -2:
1708 if (r1->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1709 {
1710 snprintf (errmsg, err_len,
1711 "Function declared with a non-constant character "
1712 "length referenced with a constant length");
1713 return false;
1714 }
1715 else if (r2->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1716 {
1717 snprintf (errmsg, err_len,
1718 "Function declared with a constant character "
1719 "length referenced with a non-constant length");
1720 return false;
1721 }
1722 /* Warn if length expression types are different, except for
1723 possibly false positives where complex expressions might have
1724 been used. */
1725 else if ((r1->ts.u.cl->length->expr_type
1726 != r2->ts.u.cl->length->expr_type)
1727 && (r1->ts.u.cl->length->expr_type != EXPR_OP
1728 || r2->ts.u.cl->length->expr_type != EXPR_OP))
1729 gfc_warning (0, "Possible character length mismatch in "
1730 "function result between %L and %L",
1731 &r1->declared_at, &r2->declared_at);
1732 break;
1733
1734 case 0:
1735 break;
1736
1737 default:
1738 gfc_internal_error ("check_result_characteristics (1): Unexpected "
1739 "result %i of gfc_dep_compare_expr", compval);
1740 break;
1741 }
1742 }
1743 }
1744
1745 /* Check array shape. */
1746 if (!r1->attr.allocatable && !r1->attr.pointer && r1->as && r2->as)
1747 {
1748 int i, compval;
1749 gfc_expr *shape1, *shape2;
1750
1751 if (r1->as->type != r2->as->type)
1752 {
1753 snprintf (errmsg, err_len, "Shape mismatch in function result");
1754 return false;
1755 }
1756
1757 if (r1->as->type == AS_EXPLICIT)
1758 for (i = 0; i < r1->as->rank + r1->as->corank; i++)
1759 {
1760 shape1 = gfc_subtract (gfc_copy_expr (r1->as->upper[i]),
1761 gfc_copy_expr (r1->as->lower[i]));
1762 shape2 = gfc_subtract (gfc_copy_expr (r2->as->upper[i]),
1763 gfc_copy_expr (r2->as->lower[i]));
1764 compval = gfc_dep_compare_expr (shape1, shape2);
1765 gfc_free_expr (shape1);
1766 gfc_free_expr (shape2);
1767 switch (compval)
1768 {
1769 case -1:
1770 case 1:
1771 case -3:
1772 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
1773 "function result", i + 1);
1774 return false;
1775
1776 case -2:
1777 /* FIXME: Implement a warning for this case.
1778 gfc_warning (0, "Possible shape mismatch in return value");*/
1779 break;
1780
1781 case 0:
1782 break;
1783
1784 default:
1785 gfc_internal_error ("check_result_characteristics (2): "
1786 "Unexpected result %i of "
1787 "gfc_dep_compare_expr", compval);
1788 break;
1789 }
1790 }
1791 }
1792
1793 return true;
1794}
1795
1796
1797/* 'Compare' two formal interfaces associated with a pair of symbols.
1798 We return true if there exists an actual argument list that
1799 would be ambiguous between the two interfaces, zero otherwise.
1800 'strict_flag' specifies whether all the characteristics are
1801 required to match, which is not the case for ambiguity checks.
1802 'p1' and 'p2' are the PASS arguments of both procedures (if applicable). */
1803
1804bool
1805gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
1806 int generic_flag, int strict_flag,
1807 char *errmsg, int err_len,
1808 const char *p1, const char *p2,
1809 bool *bad_result_characteristics)
1810{
1811 gfc_formal_arglist *f1, *f2;
1812
1813 gcc_assert (name2 != NULL);
1814
1815 if (bad_result_characteristics)
1816 *bad_result_characteristics = false;
1817
1818 if (s1->attr.function && (s2->attr.subroutine
1819 || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
1820 && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
1821 {
1822 if (errmsg != NULL)
1823 snprintf (errmsg, err_len, "'%s' is not a function", name2);
1824 return false;
1825 }
1826
1827 if (s1->attr.subroutine && s2->attr.function)
1828 {
1829 if (errmsg != NULL)
1830 snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
1831 return false;
1832 }
1833
1834 if (s2->attr.subroutine && s1->attr.flavor == FL_VARIABLE)
1835 {
1836 if (errmsg != NULL)
1837 snprintf (errmsg, err_len, "subroutine proc pointer '%s' passed "
1838 "to dummy variable '%s'", name2, s1->name);
1839 return false;
1840 }
1841
1842 /* Do strict checks on all characteristics
1843 (for dummy procedures and procedure pointer assignments). */
1844 if (!generic_flag && strict_flag)
1845 {
1846 if (s1->attr.function && s2->attr.function)
1847 {
1848 /* If both are functions, check result characteristics. */
1849 if (!gfc_check_result_characteristics (s1, s2, errmsg, err_len)
1850 || !gfc_check_result_characteristics (s2, s1, errmsg, err_len))
1851 {
1852 if (bad_result_characteristics)
1853 *bad_result_characteristics = true;
1854 return false;
1855 }
1856 }
1857
1858 if (s1->attr.pure && !s2->attr.pure)
1859 {
1860 snprintf (errmsg, err_len, "Mismatch in PURE attribute");
1861 return false;
1862 }
1863 if (s1->attr.elemental && !s2->attr.elemental)
1864 {
1865 snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
1866 return false;
1867 }
1868 }
1869
1870 if (s1->attr.if_source == IFSRC_UNKNOWN
1871 || s2->attr.if_source == IFSRC_UNKNOWN)
1872 return true;
1873
1874 f1 = gfc_sym_get_dummy_args (s1);
1875 f2 = gfc_sym_get_dummy_args (s2);
1876
1877 /* Special case: No arguments. */
1878 if (f1 == NULL && f2 == NULL)
1879 return true;
1880
1881 if (generic_flag)
1882 {
1883 if (count_types_test (f1, f2, p1, p2)
1884 || count_types_test (f2, f1, p2, p1))
1885 return false;
1886
1887 /* Special case: alternate returns. If both f1->sym and f2->sym are
1888 NULL, then the leading formal arguments are alternate returns.
1889 The previous conditional should catch argument lists with
1890 different number of argument. */
1891 if (f1 && f1->sym == NULL && f2 && f2->sym == NULL)
1892 return true;
1893
1894 if (generic_correspondence (f1, f2, p1, p2)
1895 || generic_correspondence (f2, f1, p2, p1))
1896 return false;
1897 }
1898 else
1899 /* Perform the abbreviated correspondence test for operators (the
1900 arguments cannot be optional and are always ordered correctly).
1901 This is also done when comparing interfaces for dummy procedures and in
1902 procedure pointer assignments. */
1903
1904 for (; f1 || f2; f1 = f1->next, f2 = f2->next)
1905 {
1906 /* Check existence. */
1907 if (f1 == NULL || f2 == NULL)
1908 {
1909 if (errmsg != NULL)
1910 snprintf (errmsg, err_len, "'%s' has the wrong number of "
1911 "arguments", name2);
1912 return false;
1913 }
1914
1915 if (strict_flag)
1916 {
1917 /* Check all characteristics. */
1918 if (!gfc_check_dummy_characteristics (f1->sym, f2->sym, true,
1919 errmsg, err_len))
1920 return false;
1921 }
1922 else
1923 {
1924 /* Operators: Only check type and rank of arguments. */
1925 if (!compare_type (f2->sym, f1->sym))
1926 {
1927 if (errmsg != NULL)
1928 snprintf (errmsg, err_len, "Type mismatch in argument '%s' "
1929 "(%s/%s)", f1->sym->name,
1930 gfc_typename (&f1->sym->ts),
1931 gfc_typename (&f2->sym->ts));
1932 return false;
1933 }
1934 if (!compare_rank (f2->sym, f1->sym))
1935 {
1936 if (errmsg != NULL)
1937 snprintf (errmsg, err_len, "Rank mismatch in argument "
1938 "'%s' (%i/%i)", f1->sym->name,
1939 symbol_rank (f1->sym), symbol_rank (f2->sym));
1940 return false;
1941 }
1942 if ((gfc_option.allow_std & GFC_STD_F2008)
1943 && (compare_ptr_alloc(f1->sym, f2->sym)
1944 || compare_ptr_alloc(f2->sym, f1->sym)))
1945 {
1946 if (errmsg != NULL)
1947 snprintf (errmsg, err_len, "Mismatching POINTER/ALLOCATABLE "
1948 "attribute in argument '%s' ", f1->sym->name);
1949 return false;
1950 }
1951 }
1952 }
1953
1954 return true;
1955}
1956
1957
1958/* Given a pointer to an interface pointer, remove duplicate
1959 interfaces and make sure that all symbols are either functions
1960 or subroutines, and all of the same kind. Returns true if
1961 something goes wrong. */
1962
1963static bool
1964check_interface0 (gfc_interface *p, const char *interface_name)
1965{
1966 gfc_interface *psave, *q, *qlast;
1967
1968 psave = p;
1969 for (; p; p = p->next)
1970 {
1971 /* Make sure all symbols in the interface have been defined as
1972 functions or subroutines. */
1973 if (((!p->sym->attr.function && !p->sym->attr.subroutine)
1974 || !p->sym->attr.if_source)
1975 && !gfc_fl_struct (p->sym->attr.flavor))
1976 {
1977 const char *guessed
1978 = gfc_lookup_function_fuzzy (p->sym->name, p->sym->ns->sym_root);
1979
1980 if (p->sym->attr.external)
1981 if (guessed)
1982 gfc_error ("Procedure %qs in %s at %L has no explicit interface"
1983 "; did you mean %qs?",
1984 p->sym->name, interface_name, &p->sym->declared_at,
1985 guessed);
1986 else
1987 gfc_error ("Procedure %qs in %s at %L has no explicit interface",
1988 p->sym->name, interface_name, &p->sym->declared_at);
1989 else
1990 if (guessed)
1991 gfc_error ("Procedure %qs in %s at %L is neither function nor "
1992 "subroutine; did you mean %qs?", p->sym->name,
1993 interface_name, &p->sym->declared_at, guessed);
1994 else
1995 gfc_error ("Procedure %qs in %s at %L is neither function nor "
1996 "subroutine", p->sym->name, interface_name,
1997 &p->sym->declared_at);
1998 return true;
1999 }
2000
2001 /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs. */
2002 if ((psave->sym->attr.function && !p->sym->attr.function
2003 && !gfc_fl_struct (p->sym->attr.flavor))
2004 || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
2005 {
2006 if (!gfc_fl_struct (p->sym->attr.flavor))
2007 gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
2008 " or all FUNCTIONs", interface_name,
2009 &p->sym->declared_at);
2010 else if (p->sym->attr.flavor == FL_DERIVED)
2011 gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
2012 "generic name is also the name of a derived type",
2013 interface_name, &p->sym->declared_at);
2014 return true;
2015 }
2016
2017 /* F2003, C1207. F2008, C1207. */
2018 if (p->sym->attr.proc == PROC_INTERNAL
2019 && !gfc_notify_std (GFC_STD_F2008, "Internal procedure "
2020 "%qs in %s at %L", p->sym->name,
2021 interface_name, &p->sym->declared_at))
2022 return true;
2023 }
2024 p = psave;
2025
2026 /* Remove duplicate interfaces in this interface list. */
2027 for (; p; p = p->next)
2028 {
2029 qlast = p;
2030
2031 for (q = p->next; q;)
2032 {
2033 if (p->sym != q->sym)
2034 {
2035 qlast = q;
2036 q = q->next;
2037 }
2038 else
2039 {
2040 /* Duplicate interface. */
2041 qlast->next = q->next;
2042 free (q);
2043 q = qlast->next;
2044 }
2045 }
2046 }
2047
2048 return false;
2049}
2050
2051
2052/* Check lists of interfaces to make sure that no two interfaces are
2053 ambiguous. Duplicate interfaces (from the same symbol) are OK here. */
2054
2055static bool
2056check_interface1 (gfc_interface *p, gfc_interface *q0,
2057 int generic_flag, const char *interface_name,
2058 bool referenced)
2059{
2060 gfc_interface *q;
2061 for (; p; p = p->next)
2062 for (q = q0; q; q = q->next)
2063 {
2064 if (p->sym == q->sym)
2065 continue; /* Duplicates OK here. */
2066
2067 if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
2068 continue;
2069
2070 if (!gfc_fl_struct (p->sym->attr.flavor)
2071 && !gfc_fl_struct (q->sym->attr.flavor)
2072 && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
2073 generic_flag, 0, NULL, 0, NULL, NULL))
2074 {
2075 if (referenced)
2076 gfc_error ("Ambiguous interfaces in %s for %qs at %L "
2077 "and %qs at %L", interface_name,
2078 q->sym->name, &q->sym->declared_at,
2079 p->sym->name, &p->sym->declared_at);
2080 else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
2081 gfc_warning (0, "Ambiguous interfaces in %s for %qs at %L "
2082 "and %qs at %L", interface_name,
2083 q->sym->name, &q->sym->declared_at,
2084 p->sym->name, &p->sym->declared_at);
2085 else
2086 gfc_warning (0, "Although not referenced, %qs has ambiguous "
2087 "interfaces at %L", interface_name, &p->where);
2088 return true;
2089 }
2090 }
2091 return false;
2092}
2093
2094
2095/* Check the generic and operator interfaces of symbols to make sure
2096 that none of the interfaces conflict. The check has to be done
2097 after all of the symbols are actually loaded. */
2098
2099static void
2100check_sym_interfaces (gfc_symbol *sym)
2101{
2102 /* Provide sufficient space to hold "generic interface 'symbol.symbol'". */
2103 char interface_name[2*GFC_MAX_SYMBOL_LEN+2 + sizeof("generic interface ''")];
2104 gfc_interface *p;
2105
2106 if (sym->ns != gfc_current_ns)
2107 return;
2108
2109 if (sym->generic != NULL)
2110 {
2111 size_t len = strlen (sym->name) + sizeof("generic interface ''");
2112 gcc_assert (len < sizeof (interface_name));
2113 sprintf (interface_name, "generic interface '%s'", sym->name);
2114 if (check_interface0 (sym->generic, interface_name))
2115 return;
2116
2117 for (p = sym->generic; p; p = p->next)
2118 {
2119 if (p->sym->attr.mod_proc
2120 && !p->sym->attr.module_procedure
2121 && (p->sym->attr.if_source != IFSRC_DECL
2122 || p->sym->attr.procedure))
2123 {
2124 gfc_error ("%qs at %L is not a module procedure",
2125 p->sym->name, &p->where);
2126 return;
2127 }
2128 }
2129
2130 /* Originally, this test was applied to host interfaces too;
2131 this is incorrect since host associated symbols, from any
2132 source, cannot be ambiguous with local symbols. */
2133 check_interface1 (sym->generic, sym->generic, 1, interface_name,
2134 sym->attr.referenced || !sym->attr.use_assoc);
2135 }
2136}
2137
2138
2139static void
2140check_uop_interfaces (gfc_user_op *uop)
2141{
2142 char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("operator interface ''")];
2143 gfc_user_op *uop2;
2144 gfc_namespace *ns;
2145
2146 sprintf (interface_name, "operator interface '%s'", uop->name);
2147 if (check_interface0 (uop->op, interface_name))
2148 return;
2149
2150 for (ns = gfc_current_ns; ns; ns = ns->parent)
2151 {
2152 uop2 = gfc_find_uop (uop->name, ns);
2153 if (uop2 == NULL)
2154 continue;
2155
2156 check_interface1 (uop->op, uop2->op, 0,
2157 interface_name, true);
2158 }
2159}
2160
2161/* Given an intrinsic op, return an equivalent op if one exists,
2162 or INTRINSIC_NONE otherwise. */
2163
2164gfc_intrinsic_op
2165gfc_equivalent_op (gfc_intrinsic_op op)
2166{
2167 switch(op)
2168 {
2169 case INTRINSIC_EQ:
2170 return INTRINSIC_EQ_OS;
2171
2172 case INTRINSIC_EQ_OS:
2173 return INTRINSIC_EQ;
2174
2175 case INTRINSIC_NE:
2176 return INTRINSIC_NE_OS;
2177
2178 case INTRINSIC_NE_OS:
2179 return INTRINSIC_NE;
2180
2181 case INTRINSIC_GT:
2182 return INTRINSIC_GT_OS;
2183
2184 case INTRINSIC_GT_OS:
2185 return INTRINSIC_GT;
2186
2187 case INTRINSIC_GE:
2188 return INTRINSIC_GE_OS;
2189
2190 case INTRINSIC_GE_OS:
2191 return INTRINSIC_GE;
2192
2193 case INTRINSIC_LT:
2194 return INTRINSIC_LT_OS;
2195
2196 case INTRINSIC_LT_OS:
2197 return INTRINSIC_LT;
2198
2199 case INTRINSIC_LE:
2200 return INTRINSIC_LE_OS;
2201
2202 case INTRINSIC_LE_OS:
2203 return INTRINSIC_LE;
2204
2205 default:
2206 return INTRINSIC_NONE;
2207 }
2208}
2209
2210/* For the namespace, check generic, user operator and intrinsic
2211 operator interfaces for consistency and to remove duplicate
2212 interfaces. We traverse the whole namespace, counting on the fact
2213 that most symbols will not have generic or operator interfaces. */
2214
2215void
2216gfc_check_interfaces (gfc_namespace *ns)
2217{
2218 gfc_namespace *old_ns, *ns2;
2219 char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("intrinsic '' operator")];
2220 int i;
2221
2222 old_ns = gfc_current_ns;
2223 gfc_current_ns = ns;
2224
2225 gfc_traverse_ns (ns, check_sym_interfaces);
2226
2227 gfc_traverse_user_op (ns, check_uop_interfaces);
2228
2229 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2230 {
2231 if (i == INTRINSIC_USER)
2232 continue;
2233
2234 if (i == INTRINSIC_ASSIGN)
2235 strcpy (interface_name, "intrinsic assignment operator");
2236 else
2237 sprintf (interface_name, "intrinsic '%s' operator",
2238 gfc_op2string ((gfc_intrinsic_op) i));
2239
2240 if (check_interface0 (ns->op[i], interface_name))
2241 continue;
2242
2243 if (ns->op[i])
2244 gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
2245 ns->op[i]->where);
2246
2247 for (ns2 = ns; ns2; ns2 = ns2->parent)
2248 {
2249 gfc_intrinsic_op other_op;
2250
2251 if (check_interface1 (ns->op[i], ns2->op[i], 0,
2252 interface_name, true))
2253 goto done;
2254
2255 /* i should be gfc_intrinsic_op, but has to be int with this cast
2256 here for stupid C++ compatibility rules. */
2257 other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
2258 if (other_op != INTRINSIC_NONE
2259 && check_interface1 (ns->op[i], ns2->op[other_op],
2260 0, interface_name, true))
2261 goto done;
2262 }
2263 }
2264
2265done:
2266 gfc_current_ns = old_ns;
2267}
2268
2269
2270/* Given a symbol of a formal argument list and an expression, if the
2271 formal argument is allocatable, check that the actual argument is
2272 allocatable. Returns true if compatible, zero if not compatible. */
2273
2274static bool
2275compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
2276{
2277 if (formal->attr.allocatable
2278 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
2279 {
2280 symbol_attribute attr = gfc_expr_attr (actual);
2281 if (actual->ts.type == BT_CLASS && !attr.class_ok)
2282 return true;
2283 else if (!attr.allocatable)
2284 return false;
2285 }
2286
2287 return true;
2288}
2289
2290
2291/* Given a symbol of a formal argument list and an expression, if the
2292 formal argument is a pointer, see if the actual argument is a
2293 pointer. Returns nonzero if compatible, zero if not compatible. */
2294
2295static int
2296compare_pointer (gfc_symbol *formal, gfc_expr *actual)
2297{
2298 symbol_attribute attr;
2299
2300 if (formal->attr.pointer
2301 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)
2302 && CLASS_DATA (formal)->attr.class_pointer))
2303 {
2304 attr = gfc_expr_attr (actual);
2305
2306 /* Fortran 2008 allows non-pointer actual arguments. */
2307 if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
2308 return 2;
2309
2310 if (!attr.pointer)
2311 return 0;
2312 }
2313
2314 return 1;
2315}
2316
2317
2318/* Emit clear error messages for rank mismatch. */
2319
2320static void
2321argument_rank_mismatch (const char *name, locus *where,
2322 int rank1, int rank2, locus *where_formal)
2323{
2324
2325 /* TS 29113, C407b. */
2326 if (where_formal == NULL)
2327 {
2328 if (rank2 == -1)
2329 gfc_error ("The assumed-rank array at %L requires that the dummy "
2330 "argument %qs has assumed-rank", where, name);
2331 else if (rank1 == 0)
2332 gfc_error_opt (0, "Rank mismatch in argument %qs "
2333 "at %L (scalar and rank-%d)", name, where, rank2);
2334 else if (rank2 == 0)
2335 gfc_error_opt (0, "Rank mismatch in argument %qs "
2336 "at %L (rank-%d and scalar)", name, where, rank1);
2337 else
2338 gfc_error_opt (0, "Rank mismatch in argument %qs "
2339 "at %L (rank-%d and rank-%d)", name, where, rank1,
2340 rank2);
2341 }
2342 else
2343 {
2344 if (rank2 == -1)
2345 /* This is an assumed rank-actual passed to a function without
2346 an explicit interface, which is already diagnosed in
2347 gfc_procedure_use. */
2348 return;
2349 if (rank1 == 0)
2350 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2351 "and actual argument at %L (scalar and rank-%d)",
2352 where, where_formal, rank2);
2353 else if (rank2 == 0)
2354 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2355 "and actual argument at %L (rank-%d and scalar)",
2356 where, where_formal, rank1);
2357 else
2358 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2359 "and actual argument at %L (rank-%d and rank-%d)", where,
2360 where_formal, rank1, rank2);
2361 }
2362}
2363
2364
2365/* Under certain conditions, a scalar actual argument can be passed
2366 to an array dummy argument - see F2018, 15.5.2.4, paragraph 14.
2367 This function returns true for these conditions so that an error
2368 or warning for this can be suppressed later. Always return false
2369 for expressions with rank > 0. */
2370
2371bool
2372maybe_dummy_array_arg (gfc_expr *e)
2373{
2374 gfc_symbol *s;
2375 gfc_ref *ref;
2376 bool array_pointer = false;
2377 bool assumed_shape = false;
2378 bool scalar_ref = true;
2379
2380 if (e->rank > 0)
2381 return false;
2382
2383 if (e->ts.type == BT_CHARACTER && e->ts.kind == 1)
2384 return true;
2385
2386 /* If this comes from a constructor, it has been an array element
2387 originally. */
2388
2389 if (e->expr_type == EXPR_CONSTANT)
2390 return e->from_constructor;
2391
2392 if (e->expr_type != EXPR_VARIABLE)
2393 return false;
2394
2395 s = e->symtree->n.sym;
2396
2397 if (s->attr.dimension)
2398 {
2399 scalar_ref = false;
2400 array_pointer = s->attr.pointer;
2401 }
2402
2403 if (s->as && s->as->type == AS_ASSUMED_SHAPE)
2404 assumed_shape = true;
2405
2406 for (ref=e->ref; ref; ref=ref->next)
2407 {
2408 if (ref->type == REF_COMPONENT)
2409 {
2410 symbol_attribute *attr;
2411 attr = &ref->u.c.component->attr;
2412 if (attr->dimension)
2413 {
2414 array_pointer = attr->pointer;
2415 assumed_shape = false;
2416 scalar_ref = false;
2417 }
2418 else
2419 scalar_ref = true;
2420 }
2421 }
2422
2423 return !(scalar_ref || array_pointer || assumed_shape);
2424}
2425
2426/* Given a symbol of a formal argument list and an expression, see if
2427 the two are compatible as arguments. Returns true if
2428 compatible, false if not compatible. */
2429
2430static bool
2431compare_parameter (gfc_symbol *formal, gfc_expr *actual,
2432 int ranks_must_agree, int is_elemental, locus *where)
2433{
2434 gfc_ref *ref;
2435 bool rank_check, is_pointer;
2436 char err[200];
2437 gfc_component *ppc;
2438 bool codimension = false;
2439 gfc_array_spec *formal_as;
2440 const char *actual_name;
2441
2442 /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
2443 procs c_f_pointer or c_f_procpointer, and we need to accept most
2444 pointers the user could give us. This should allow that. */
2445 if (formal->ts.type == BT_VOID)
2446 return true;
2447
2448 if (formal->ts.type == BT_DERIVED
2449 && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
2450 && actual->ts.type == BT_DERIVED
2451 && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
2452 {
2453 if (formal->ts.u.derived->intmod_sym_id
2454 != actual->ts.u.derived->intmod_sym_id)
2455 return false;
2456
2457 if (ranks_must_agree
2458 && symbol_rank (formal) != actual->rank
2459 && symbol_rank (formal) != -1)
2460 {
2461 if (where)
2462 argument_rank_mismatch (formal->name, &actual->where,
2463 symbol_rank (formal), actual->rank,
2464 NULL);
2465 return false;
2466 }
2467 return true;
2468 }
2469
2470 if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
2471 /* Make sure the vtab symbol is present when
2472 the module variables are generated. */
2473 gfc_find_derived_vtab (actual->ts.u.derived);
2474
2475 if (actual->ts.type == BT_PROCEDURE)
2476 {
2477 gfc_symbol *act_sym = actual->symtree->n.sym;
2478
2479 if (formal->attr.flavor != FL_PROCEDURE && !act_sym->ts.interface)
2480 {
2481 if (where)
2482 gfc_error ("Invalid procedure argument at %L", &actual->where);
2483 return false;
2484 }
2485 else if (act_sym->ts.interface
2486 && !gfc_compare_interfaces (formal, act_sym->ts.interface,
2487 act_sym->name, 0, 1, err,
2488 sizeof(err),NULL, NULL))
2489 {
2490 if (where)
2491 {
2492 /* Artificially generated symbol names would only confuse. */
2493 if (formal->attr.artificial)
2494 gfc_error_opt (0, "Interface mismatch in dummy procedure "
2495 "at %L conflicts with %L: %s", &actual->where,
2496 &formal->declared_at, err);
2497 else
2498 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs "
2499 "at %L: %s", formal->name, &actual->where, err);
2500 }
2501 return false;
2502 }
2503
2504 if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
2505 sizeof(err), NULL, NULL))
2506 {
2507 if (where)
2508 {
2509 if (formal->attr.artificial)
2510 gfc_error_opt (0, "Interface mismatch in dummy procedure "
2511 "at %L conflicts with %L: %s", &actual->where,
2512 &formal->declared_at, err);
2513 else
2514 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at "
2515 "%L: %s", formal->name, &actual->where, err);
2516
2517 }
2518 return false;
2519 }
2520
2521 /* The actual symbol may disagree with a global symbol. If so, issue an
2522 error, but only if no previous error has been reported on the formal
2523 argument. */
2524 actual_name = act_sym->name;
2525 if (!formal->error && actual_name)
2526 {
2527 gfc_gsymbol *gsym;
2528 gsym = gfc_find_gsymbol (gfc_gsym_root, actual_name);
2529 if (gsym != NULL)
2530 {
2531 if (gsym->type == GSYM_SUBROUTINE && formal->attr.function)
2532 {
2533 gfc_error ("Passing global subroutine %qs declared at %L "
2534 "as function at %L", actual_name, &gsym->where,
2535 &actual->where);
2536 return false;
2537 }
2538 if (gsym->type == GSYM_FUNCTION && formal->attr.subroutine)
2539 {
2540 gfc_error ("Passing global function %qs declared at %L "
2541 "as subroutine at %L", actual_name, &gsym->where,
2542 &actual->where);
2543 return false;
2544 }
2545 if (gsym->type == GSYM_FUNCTION)
2546 {
2547 gfc_symbol *global_asym;
2548 gfc_find_symbol (actual_name, gsym->ns, 0, &global_asym);
2549 if (global_asym != NULL)
2550 {
2551 if (formal->attr.subroutine)
2552 {
2553 gfc_error ("Mismatch between subroutine and "
2554 "function at %L", &actual->where);
2555 return false;
2556 }
2557 else if (formal->attr.function)
2558 {
2559 gfc_typespec ts;
2560
2561 if (global_asym->result)
2562 ts = global_asym->result->ts;
2563 else
2564 ts = global_asym->ts;
2565
2566 if (!gfc_compare_types (&ts,
2567 &formal->ts))
2568 {
2569 gfc_error ("Type mismatch at %L passing global "
2570 "function %qs declared at %L (%s/%s)",
2571 &actual->where, actual_name,
2572 &gsym->where,
2573 gfc_typename (&global_asym->ts),
2574 gfc_dummy_typename (&formal->ts));
2575 return false;
2576 }
2577 }
2578 else
2579 {
2580 /* The global symbol is a function. Set the formal
2581 argument acordingly. */
2582 formal->attr.function = 1;
2583 formal->ts = global_asym->ts;
2584 }
2585 }
2586 }
2587 }
2588 }
2589
2590 if (formal->attr.function && !act_sym->attr.function)
2591 {
2592 gfc_add_function (&act_sym->attr, act_sym->name,
2593 &act_sym->declared_at);
2594 if (act_sym->ts.type == BT_UNKNOWN
2595 && !gfc_set_default_type (act_sym, 1, act_sym->ns))
2596 return false;
2597 }
2598 else if (formal->attr.subroutine && !act_sym->attr.subroutine)
2599 gfc_add_subroutine (&act_sym->attr, act_sym->name,
2600 &act_sym->declared_at);
2601
2602 return true;
2603 }
2604 ppc = gfc_get_proc_ptr_comp (actual);
2605 if (ppc && ppc->ts.interface)
2606 {
2607 if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
2608 err, sizeof(err), NULL, NULL))
2609 {
2610 if (where)
2611 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2612 " %s", formal->name, &actual->where, err);
2613 return false;
2614 }
2615 }
2616
2617 /* F2008, C1241. */
2618 if (formal->attr.pointer && formal->attr.contiguous
2619 && !gfc_is_simply_contiguous (actual, true, false))
2620 {
2621 if (where)
2622 gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
2623 "must be simply contiguous", formal->name, &actual->where);
2624 return false;
2625 }
2626
2627 symbol_attribute actual_attr = gfc_expr_attr (actual);
2628 if (actual->ts.type == BT_CLASS && !actual_attr.class_ok)
2629 return true;
2630
2631 if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
2632 && actual->ts.type != BT_HOLLERITH
2633 && formal->ts.type != BT_ASSUMED
2634 && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2635 && !gfc_compare_types (&formal->ts, &actual->ts)
2636 && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
2637 && gfc_compare_derived_types (formal->ts.u.derived,
2638 CLASS_DATA (actual)->ts.u.derived)))
2639 {
2640 if (where)
2641 {
2642 if (formal->attr.artificial)
2643 {
2644 if (!flag_allow_argument_mismatch || !formal->error)
2645 gfc_error_opt (0, "Type mismatch between actual argument at %L "
2646 "and actual argument at %L (%s/%s).",
2647 &actual->where,
2648 &formal->declared_at,
2649 gfc_typename (actual),
2650 gfc_dummy_typename (&formal->ts));
2651
2652 formal->error = 1;
2653 }
2654 else
2655 gfc_error_opt (0, "Type mismatch in argument %qs at %L; passed %s "
2656 "to %s", formal->name, where, gfc_typename (actual),
2657 gfc_dummy_typename (&formal->ts));
2658 }
2659 return false;
2660 }
2661
2662 if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
2663 {
2664 if (where)
2665 gfc_error ("Assumed-type actual argument at %L requires that dummy "
2666 "argument %qs is of assumed type", &actual->where,
2667 formal->name);
2668 return false;
2669 }
2670
2671 /* TS29113 C407c; F2018 C711. */
2672 if (actual->ts.type == BT_ASSUMED
2673 && symbol_rank (formal) == -1
2674 && actual->rank != -1
2675 && !(actual->symtree->n.sym->as
2676 && actual->symtree->n.sym->as->type == AS_ASSUMED_SHAPE))
2677 {
2678 if (where)
2679 gfc_error ("Assumed-type actual argument at %L corresponding to "
2680 "assumed-rank dummy argument %qs must be "
2681 "assumed-shape or assumed-rank",
2682 &actual->where, formal->name);
2683 return false;
2684 }
2685
2686 /* F2008, 12.5.2.5; IR F08/0073. */
2687 if (formal->ts.type == BT_CLASS && formal->attr.class_ok
2688 && actual->expr_type != EXPR_NULL
2689 && ((CLASS_DATA (formal)->attr.class_pointer
2690 && formal->attr.intent != INTENT_IN)
2691 || CLASS_DATA (formal)->attr.allocatable))
2692 {
2693 if (actual->ts.type != BT_CLASS)
2694 {
2695 if (where)
2696 gfc_error ("Actual argument to %qs at %L must be polymorphic",
2697 formal->name, &actual->where);
2698 return false;
2699 }
2700
2701 if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
2702 && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
2703 CLASS_DATA (formal)->ts.u.derived))
2704 {
2705 if (where)
2706 gfc_error ("Actual argument to %qs at %L must have the same "
2707 "declared type", formal->name, &actual->where);
2708 return false;
2709 }
2710 }
2711
2712 /* F08: 12.5.2.5 Allocatable and pointer dummy variables. However, this
2713 is necessary also for F03, so retain error for both.
2714 NOTE: Other type/kind errors pre-empt this error. Since they are F03
2715 compatible, no attempt has been made to channel to this one. */
2716 if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
2717 && (CLASS_DATA (formal)->attr.allocatable
2718 ||CLASS_DATA (formal)->attr.class_pointer))
2719 {
2720 if (where)
2721 gfc_error ("Actual argument to %qs at %L must be unlimited "
2722 "polymorphic since the formal argument is a "
2723 "pointer or allocatable unlimited polymorphic "
2724 "entity [F2008: 12.5.2.5]", formal->name,
2725 &actual->where);
2726 return false;
2727 }
2728
2729 if (formal->ts.type == BT_CLASS && formal->attr.class_ok)
2730 codimension = CLASS_DATA (formal)->attr.codimension;
2731 else
2732 codimension = formal->attr.codimension;
2733
2734 if (codimension && !gfc_is_coarray (actual))
2735 {
2736 if (where)
2737 gfc_error ("Actual argument to %qs at %L must be a coarray",
2738 formal->name, &actual->where);
2739 return false;
2740 }
2741
2742 formal_as = (formal->ts.type == BT_CLASS
2743 ? CLASS_DATA (formal)->as : formal->as);
2744
2745 if (codimension && formal->attr.allocatable)
2746 {
2747 gfc_ref *last = NULL;
2748
2749 for (ref = actual->ref; ref; ref = ref->next)
2750 if (ref->type == REF_COMPONENT)
2751 last = ref;
2752
2753 /* F2008, 12.5.2.6. */
2754 if ((last && last->u.c.component->as->corank != formal->as->corank)
2755 || (!last
2756 && actual->symtree->n.sym->as->corank != formal->as->corank))
2757 {
2758 if (where)
2759 gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
2760 formal->name, &actual->where, formal->as->corank,
2761 last ? last->u.c.component->as->corank
2762 : actual->symtree->n.sym->as->corank);
2763 return false;
2764 }
2765 }
2766
2767 if (codimension)
2768 {
2769 /* F2008, 12.5.2.8 + Corrig 2 (IR F08/0048). */
2770 /* F2018, 12.5.2.8. */
2771 if (formal->attr.dimension
2772 && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
2773 && actual_attr.dimension
2774 && !gfc_is_simply_contiguous (actual, true, true))
2775 {
2776 if (where)
2777 gfc_error ("Actual argument to %qs at %L must be simply "
2778 "contiguous or an element of such an array",
2779 formal->name, &actual->where);
2780 return false;
2781 }
2782
2783 /* F2008, C1303 and C1304. */
2784 if (formal->attr.intent != INTENT_INOUT
2785 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2786 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2787 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2788 || formal->attr.lock_comp))
2789
2790 {
2791 if (where)
2792 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2793 "which is LOCK_TYPE or has a LOCK_TYPE component",
2794 formal->name, &actual->where);
2795 return false;
2796 }
2797
2798 /* TS18508, C702/C703. */
2799 if (formal->attr.intent != INTENT_INOUT
2800 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2801 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2802 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2803 || formal->attr.event_comp))
2804
2805 {
2806 if (where)
2807 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2808 "which is EVENT_TYPE or has a EVENT_TYPE component",
2809 formal->name, &actual->where);
2810 return false;
2811 }
2812 }
2813
2814 /* F2008, C1239/C1240. */
2815 if (actual->expr_type == EXPR_VARIABLE
2816 && (actual->symtree->n.sym->attr.asynchronous
2817 || actual->symtree->n.sym->attr.volatile_)
2818 && (formal->attr.asynchronous || formal->attr.volatile_)
2819 && actual->rank && formal->as
2820 && !gfc_is_simply_contiguous (actual, true, false)
2821 && ((formal->as->type != AS_ASSUMED_SHAPE
2822 && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
2823 || formal->attr.contiguous))
2824 {
2825 if (where)
2826 gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
2827 "assumed-rank array without CONTIGUOUS attribute - as actual"
2828 " argument at %L is not simply contiguous and both are "
2829 "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
2830 return false;
2831 }
2832
2833 if (formal->attr.allocatable && !codimension
2834 && actual_attr.codimension)
2835 {
2836 if (formal->attr.intent == INTENT_OUT)
2837 {
2838 if (where)
2839 gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
2840 "INTENT(OUT) dummy argument %qs", &actual->where,
2841 formal->name);
2842 return false;
2843 }
2844 else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
2845 gfc_warning (OPT_Wsurprising,
2846 "Passing coarray at %L to allocatable, noncoarray dummy "
2847 "argument %qs, which is invalid if the allocation status"
2848 " is modified", &actual->where, formal->name);
2849 }
2850
2851 /* If the rank is the same or the formal argument has assumed-rank. */
2852 if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
2853 return true;
2854
2855 rank_check = where != NULL && !is_elemental && formal_as
2856 && (formal_as->type == AS_ASSUMED_SHAPE
2857 || formal_as->type == AS_DEFERRED)
2858 && !(actual->expr_type == EXPR_NULL
2859 && actual->ts.type == BT_UNKNOWN);
2860
2861 /* Skip rank checks for NO_ARG_CHECK. */
2862 if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2863 return true;
2864
2865 /* Scalar & coindexed, see: F2008, Section 12.5.2.4. */
2866 if (rank_check || ranks_must_agree
2867 || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
2868 || (actual->rank != 0
2869 && !(is_elemental || formal->attr.dimension
2870 || (formal->ts.type == BT_CLASS
2871 && CLASS_DATA (formal)->attr.dimension)))
2872 || (actual->rank == 0
2873 && ((formal->ts.type == BT_CLASS
2874 && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
2875 || (formal->ts.type != BT_CLASS
2876 && formal->as->type == AS_ASSUMED_SHAPE))
2877 && actual->expr_type != EXPR_NULL)
2878 || (actual->rank == 0
2879 && (formal->attr.dimension
2880 || (formal->ts.type == BT_CLASS
2881 && CLASS_DATA (formal)->attr.dimension))
2882 && gfc_is_coindexed (actual))
2883 /* Assumed-rank actual argument; F2018 C838. */
2884 || actual->rank == -1)
2885 {
2886 if (where
2887 && (!formal->attr.artificial || (!formal->maybe_array
2888 && !maybe_dummy_array_arg (actual))))
2889 {
2890 locus *where_formal;
2891 if (formal->attr.artificial)
2892 where_formal = &formal->declared_at;
2893 else
2894 where_formal = NULL;
2895
2896 argument_rank_mismatch (formal->name, &actual->where,
2897 symbol_rank (formal), actual->rank,
2898 where_formal);
2899 }
2900 return false;
2901 }
2902 else if (actual->rank != 0
2903 && (is_elemental || formal->attr.dimension
2904 || (formal->ts.type == BT_CLASS
2905 && CLASS_DATA (formal)->attr.dimension)))
2906 return true;
2907
2908 /* At this point, we are considering a scalar passed to an array. This
2909 is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
2910 - if the actual argument is (a substring of) an element of a
2911 non-assumed-shape/non-pointer/non-polymorphic array; or
2912 - (F2003) if the actual argument is of type character of default/c_char
2913 kind.
2914 - (F2018) if the dummy argument is type(*). */
2915
2916 is_pointer = actual->expr_type == EXPR_VARIABLE
2917 ? actual->symtree->n.sym->attr.pointer : false;
2918
2919 for (ref = actual->ref; ref; ref = ref->next)
2920 {
2921 if (ref->type == REF_COMPONENT)
2922 is_pointer = ref->u.c.component->attr.pointer;
2923 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2924 && ref->u.ar.dimen > 0
2925 && (!ref->next
2926 || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
2927 break;
2928 }
2929
2930 if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
2931 {
2932 if (where)
2933 gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
2934 "at %L", formal->name, &actual->where);
2935 return false;
2936 }
2937
2938 if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
2939 && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2940 {
2941 if (where)
2942 {
2943 if (formal->attr.artificial)
2944 gfc_error ("Element of assumed-shape or pointer array "
2945 "as actual argument at %L cannot correspond to "
2946 "actual argument at %L",
2947 &actual->where, &formal->declared_at);
2948 else
2949 gfc_error ("Element of assumed-shape or pointer "
2950 "array passed to array dummy argument %qs at %L",
2951 formal->name, &actual->where);
2952 }
2953 return false;
2954 }
2955
2956 if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
2957 && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2958 {
2959 if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
2960 {
2961 if (where)
2962 gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
2963 "CHARACTER actual argument with array dummy argument "
2964 "%qs at %L", formal->name, &actual->where);
2965 return false;
2966 }
2967
2968 if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
2969 {
2970 gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
2971 "array dummy argument %qs at %L",
2972 formal->name, &actual->where);
2973 return false;
2974 }
2975 else
2976 return ((gfc_option.allow_std & GFC_STD_F2003) != 0);
2977 }
2978
2979 if (ref == NULL && actual->expr_type != EXPR_NULL)
2980 {
2981 if (actual->rank == 0
2982 && formal->ts.type == BT_ASSUMED
2983 && formal->as
2984 && formal->as->type == AS_ASSUMED_SIZE)
2985 /* This is new in F2018, type(*) is new in TS29113, but gfortran does
2986 not differentiate. Thus, if type(*) exists, it is valid;
2987 otherwise, type(*) is already rejected. */
2988 return true;
2989 if (where
2990 && (!formal->attr.artificial || (!formal->maybe_array
2991 && !maybe_dummy_array_arg (actual))))
2992 {
2993 locus *where_formal;
2994 if (formal->attr.artificial)
2995 where_formal = &formal->declared_at;
2996 else
2997 where_formal = NULL;
2998
2999 argument_rank_mismatch (formal->name, &actual->where,
3000 symbol_rank (formal), actual->rank,
3001 where_formal);
3002 }
3003 return false;
3004 }
3005
3006 return true;
3007}
3008
3009
3010/* Returns the storage size of a symbol (formal argument) or
3011 zero if it cannot be determined. */
3012
3013static unsigned long
3014get_sym_storage_size (gfc_symbol *sym)
3015{
3016 int i;
3017 unsigned long strlen, elements;
3018
3019 if (sym->ts.type == BT_CHARACTER)
3020 {
3021 if (sym->ts.u.cl && sym->ts.u.cl->length
3022 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
3023 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
3024 strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
3025 else
3026 return 0;
3027 }
3028 else
3029 strlen = 1;
3030
3031 if (symbol_rank (sym) == 0)
3032 return strlen;
3033
3034 elements = 1;
3035 if (sym->as->type != AS_EXPLICIT)
3036 return 0;
3037 for (i = 0; i < sym->as->rank; i++)
3038 {
3039 if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
3040 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
3041 || sym->as->upper[i]->ts.type != BT_INTEGER
3042 || sym->as->lower[i]->ts.type != BT_INTEGER)
3043 return 0;
3044
3045 elements *= mpz_get_si (sym->as->upper[i]->value.integer)
3046 - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
3047 }
3048
3049 return strlen*elements;
3050}
3051
3052
3053/* Returns the storage size of an expression (actual argument) or
3054 zero if it cannot be determined. For an array element, it returns
3055 the remaining size as the element sequence consists of all storage
3056 units of the actual argument up to the end of the array. */
3057
3058static unsigned long
3059get_expr_storage_size (gfc_expr *e)
3060{
3061 int i;
3062 long int strlen, elements;
3063 long int substrlen = 0;
3064 bool is_str_storage = false;
3065 gfc_ref *ref;
3066
3067 if (e == NULL)
3068 return 0;
3069
3070 if (e->ts.type == BT_CHARACTER)
3071 {
3072 if (e->ts.u.cl && e->ts.u.cl->length
3073 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
3074 && e->ts.u.cl->length->ts.type == BT_INTEGER)
3075 strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
3076 else if (e->expr_type == EXPR_CONSTANT
3077 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
3078 strlen = e->value.character.length;
3079 else
3080 return 0;
3081 }
3082 else
3083 strlen = 1; /* Length per element. */
3084
3085 if (e->rank == 0 && !e->ref)
3086 return strlen;
3087
3088 elements = 1;
3089 if (!e->ref)
3090 {
3091 if (!e->shape)
3092 return 0;
3093 for (i = 0; i < e->rank; i++)
3094 elements *= mpz_get_si (e->shape[i]);
3095 return elements*strlen;
3096 }
3097
3098 for (ref = e->ref; ref; ref = ref->next)
3099 {
3100 if (ref->type == REF_SUBSTRING && ref->u.ss.start
3101 && ref->u.ss.start->expr_type == EXPR_CONSTANT)
3102 {
3103 if (is_str_storage)
3104 {
3105 /* The string length is the substring length.
3106 Set now to full string length. */
3107 if (!ref->u.ss.length || !ref->u.ss.length->length
3108 || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
3109 return 0;
3110
3111 strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
3112 }
3113 substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
3114 continue;
3115 }
3116
3117 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3118 for (i = 0; i < ref->u.ar.dimen; i++)
3119 {
3120 long int start, end, stride;
3121 stride = 1;
3122
3123 if (ref->u.ar.stride[i])
3124 {
3125 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT
3126 && ref->u.ar.stride[i]->ts.type == BT_INTEGER)
3127 stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
3128 else
3129 return 0;
3130 }
3131
3132 if (ref->u.ar.start[i])
3133 {
3134 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT
3135 && ref->u.ar.start[i]->ts.type == BT_INTEGER)
3136 start = mpz_get_si (ref->u.ar.start[i]->value.integer);
3137 else
3138 return 0;
3139 }
3140 else if (ref->u.ar.as->lower[i]
3141 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
3142 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER)
3143 start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
3144 else
3145 return 0;
3146
3147 if (ref->u.ar.end[i])
3148 {
3149 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT
3150 && ref->u.ar.end[i]->ts.type == BT_INTEGER)
3151 end = mpz_get_si (ref->u.ar.end[i]->value.integer);
3152 else
3153 return 0;
3154 }
3155 else if (ref->u.ar.as->upper[i]
3156 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
3157 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
3158 end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
3159 else
3160 return 0;
3161
3162 elements *= (end - start)/stride + 1L;
3163 }
3164 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
3165 for (i = 0; i < ref->u.ar.as->rank; i++)
3166 {
3167 if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
3168 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
3169 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER
3170 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
3171 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
3172 elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
3173 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
3174 + 1L;
3175 else
3176 return 0;
3177 }
3178 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
3179 && e->expr_type == EXPR_VARIABLE)
3180 {
3181 if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
3182 || e->symtree->n.sym->attr.pointer)
3183 {
3184 elements = 1;
3185 continue;
3186 }
3187
3188 /* Determine the number of remaining elements in the element
3189 sequence for array element designators. */
3190 is_str_storage = true;
3191 for (i = ref->u.ar.dimen - 1; i >= 0; i--)
3192 {
3193 if (ref->u.ar.start[i] == NULL
3194 || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
3195 || ref->u.ar.as->upper[i] == NULL
3196 || ref->u.ar.as->lower[i] == NULL
3197 || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
3198 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT
3199 || ref->u.ar.as->upper[i]->ts.type != BT_INTEGER
3200 || ref->u.ar.as->lower[i]->ts.type != BT_INTEGER)
3201 return 0;
3202
3203 elements
3204 = elements
3205 * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
3206 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
3207 + 1L)
3208 - (mpz_get_si (ref->u.ar.start[i]->value.integer)
3209 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
3210 }
3211 }
3212 else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
3213 && ref->u.c.component->attr.proc_pointer
3214 && ref->u.c.component->attr.dimension)
3215 {
3216 /* Array-valued procedure-pointer components. */
3217 gfc_array_spec *as = ref->u.c.component->as;
3218 for (i = 0; i < as->rank; i++)
3219 {
3220 if (!as->upper[i] || !as->lower[i]
3221 || as->upper[i]->expr_type != EXPR_CONSTANT
3222 || as->lower[i]->expr_type != EXPR_CONSTANT
3223 || as->upper[i]->ts.type != BT_INTEGER
3224 || as->lower[i]->ts.type != BT_INTEGER)
3225 return 0;
3226
3227 elements = elements
3228 * (mpz_get_si (as->upper[i]->value.integer)
3229 - mpz_get_si (as->lower[i]->value.integer) + 1L);
3230 }
3231 }
3232 }
3233
3234 if (substrlen)
3235 return (is_str_storage) ? substrlen + (elements-1)*strlen
3236 : elements*strlen;
3237 else
3238 return elements*strlen;
3239}
3240
3241
3242/* Given an expression, check whether it is an array section
3243 which has a vector subscript. */
3244
3245bool
3246gfc_has_vector_subscript (gfc_expr *e)
3247{
3248 int i;
3249 gfc_ref *ref;
3250
3251 if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
3252 return false;
3253
3254 for (ref = e->ref; ref; ref = ref->next)
3255 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3256 for (i = 0; i < ref->u.ar.dimen; i++)
3257 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
3258 return true;
3259
3260 return false;
3261}
3262
3263
3264static bool
3265is_procptr_result (gfc_expr *expr)
3266{
3267 gfc_component *c = gfc_get_proc_ptr_comp (expr);
3268 if (c)
3269 return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
3270 else
3271 return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
3272 && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
3273}
3274
3275
3276/* Recursively append candidate argument ARG to CANDIDATES. Store the
3277 number of total candidates in CANDIDATES_LEN. */
3278
3279static void
3280lookup_arg_fuzzy_find_candidates (gfc_formal_arglist *arg,
3281 char **&candidates,
3282 size_t &candidates_len)
3283{
3284 for (gfc_formal_arglist *p = arg; p && p->sym; p = p->next)
3285 vec_push (candidates, candidates_len, p->sym->name);
3286}
3287
3288
3289/* Lookup argument ARG fuzzily, taking names in ARGUMENTS into account. */
3290
3291static const char*
3292lookup_arg_fuzzy (const char *arg, gfc_formal_arglist *arguments)
3293{
3294 char **candidates = NULL;
3295 size_t candidates_len = 0;
3296 lookup_arg_fuzzy_find_candidates (arguments, candidates, candidates_len);
3297 return gfc_closest_fuzzy_match (arg, candidates);
3298}
3299
3300
3301static gfc_dummy_arg *
3302get_nonintrinsic_dummy_arg (gfc_formal_arglist *formal)
3303{
3304 gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
3305
3306 dummy_arg->intrinsicness = GFC_NON_INTRINSIC_DUMMY_ARG;
3307 dummy_arg->u.non_intrinsic = formal;
3308
3309 return dummy_arg;
3310}
3311
3312
3313/* Given formal and actual argument lists, see if they are compatible.
3314 If they are compatible, the actual argument list is sorted to
3315 correspond with the formal list, and elements for missing optional
3316 arguments are inserted. If WHERE pointer is nonnull, then we issue
3317 errors when things don't match instead of just returning the status
3318 code. */
3319
3320bool
3321gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
3322 int ranks_must_agree, int is_elemental,
3323 bool in_statement_function, locus *where)
3324{
3325 gfc_actual_arglist **new_arg, *a, *actual;
3326 gfc_formal_arglist *f;
3327 int i, n, na;
3328 unsigned long actual_size, formal_size;
3329 bool full_array = false;
3330 gfc_array_ref *actual_arr_ref;
3331 gfc_array_spec *fas, *aas;
3332 bool pointer_dummy, pointer_arg, allocatable_arg;
3333 bool procptr_dummy, optional_dummy, allocatable_dummy;
3334
3335 bool ok = true;
3336
3337 actual = *ap;
3338
3339 if (actual == NULL && formal == NULL)
3340 return true;
3341
3342 n = 0;
3343 for (f = formal; f; f = f->next)
3344 n++;
3345
3346 new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
3347
3348 for (i = 0; i < n; i++)
3349 new_arg[i] = NULL;
3350
3351 na = 0;
3352 f = formal;
3353 i = 0;
3354
3355 for (a = actual; a; a = a->next, f = f->next)
3356 {
3357 if (a->name != NULL && in_statement_function)
3358 {
3359 gfc_error ("Keyword argument %qs at %L is invalid in "
3360 "a statement function", a->name, &a->expr->where);
3361 return false;
3362 }
3363
3364 /* Look for keywords but ignore g77 extensions like %VAL. */
3365 if (a->name != NULL && a->name[0] != '%')
3366 {
3367 i = 0;
3368 for (f = formal; f; f = f->next, i++)
3369 {
3370 if (f->sym == NULL)
3371 continue;
3372 if (strcmp (f->sym->name, a->name) == 0)
3373 break;
3374 }
3375
3376 if (f == NULL)
3377 {
3378 if (where)
3379 {
3380 const char *guessed = lookup_arg_fuzzy (a->name, formal);
3381 if (guessed)
3382 gfc_error ("Keyword argument %qs at %L is not in "
3383 "the procedure; did you mean %qs?",
3384 a->name, &a->expr->where, guessed);
3385 else
3386 gfc_error ("Keyword argument %qs at %L is not in "
3387 "the procedure", a->name, &a->expr->where);
3388 }
3389 return false;
3390 }
3391
3392 if (new_arg[i] != NULL)
3393 {
3394 if (where)
3395 gfc_error ("Keyword argument %qs at %L is already associated "
3396 "with another actual argument", a->name,
3397 &a->expr->where);
3398 return false;
3399 }
3400 }
3401
3402 if (f == NULL)
3403 {
3404 if (where)
3405 gfc_error ("More actual than formal arguments in procedure "
3406 "call at %L", where);
3407 return false;
3408 }
3409
3410 if (f->sym == NULL && a->expr == NULL)
3411 goto match;
3412
3413 if (f->sym == NULL)
3414 {
3415 /* These errors have to be issued, otherwise an ICE can occur.
3416 See PR 78865. */
3417 if (where)
3418 gfc_error_now ("Missing alternate return specifier in subroutine "
3419 "call at %L", where);
3420 return false;
3421 }
3422 else
3423 {
3424 if (a->associated_dummy)
3425 free (a->associated_dummy);
3426 a->associated_dummy = get_nonintrinsic_dummy_arg (f);
3427 }
3428
3429 if (a->expr == NULL)
3430 {
3431 if (f->sym->attr.optional)
3432 continue;
3433 else
3434 {
3435 if (where)
3436 gfc_error_now ("Unexpected alternate return specifier in "
3437 "subroutine call at %L", where);
3438 return false;
3439 }
3440 }
3441
3442 /* Make sure that intrinsic vtables exist for calls to unlimited
3443 polymorphic formal arguments. */
3444 if (UNLIMITED_POLY (f->sym)
3445 && a->expr->ts.type != BT_DERIVED
3446 && a->expr->ts.type != BT_CLASS
3447 && a->expr->ts.type != BT_ASSUMED)
3448 gfc_find_vtab (&a->expr->ts);
3449
3450 /* Interp J3/22-146:
3451 "If the context of the reference to NULL is an <actual argument>
3452 corresponding to an <assumed-rank> dummy argument, MOLD shall be
3453 present." */
3454 if (a->expr->expr_type == EXPR_NULL
3455 && a->expr->ts.type == BT_UNKNOWN
3456 && f->sym->as
3457 && f->sym->as->type == AS_ASSUMED_RANK)
3458 {
3459 gfc_error ("Intrinsic %<NULL()%> without %<MOLD%> argument at %L "
3460 "passed to assumed-rank dummy %qs",
3461 &a->expr->where, f->sym->name);
3462 ok = false;
3463 goto match;
3464 }
3465
3466 if (warn_surprising
3467 && a->expr->expr_type == EXPR_VARIABLE
3468 && a->expr->symtree->n.sym->as
3469 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
3470 && f->sym->as
3471 && f->sym->as->type == AS_ASSUMED_RANK)
3472 gfc_warning (0, "The assumed-size dummy %qs is being passed at %L to "
3473 "an assumed-rank dummy %qs", a->expr->symtree->name,
3474 &a->expr->where, f->sym->name);
3475
3476 if (a->expr->expr_type == EXPR_NULL
3477 && a->expr->ts.type == BT_UNKNOWN
3478 && f->sym->ts.type == BT_CHARACTER
3479 && !f->sym->ts.deferred
3480 && f->sym->ts.u.cl
3481 && f->sym->ts.u.cl->length == NULL)
3482 {
3483 gfc_error ("Intrinsic %<NULL()%> without %<MOLD%> argument at %L "
3484 "passed to assumed-length dummy %qs",
3485 &a->expr->where, f->sym->name);
3486 ok = false;
3487 goto match;
3488 }
3489
3490 /* Allow passing of NULL() as disassociated pointer, procedure
3491 pointer, or unallocated allocatable (F2008+) to a respective dummy
3492 argument. */
3493 pointer_dummy = ((f->sym->ts.type != BT_CLASS
3494 && f->sym->attr.pointer)
3495 || (f->sym->ts.type == BT_CLASS
3496 && CLASS_DATA (f->sym)->attr.class_pointer));
3497
3498 procptr_dummy = ((f->sym->ts.type != BT_CLASS
3499 && f->sym->attr.proc_pointer)
3500 || (f->sym->ts.type == BT_CLASS
3501 && CLASS_DATA (f->sym)->attr.proc_pointer));
3502
3503 optional_dummy = f->sym->attr.optional;
3504
3505 allocatable_dummy = ((f->sym->ts.type != BT_CLASS
3506 && f->sym->attr.allocatable)
3507 || (f->sym->ts.type == BT_CLASS
3508 && CLASS_DATA (f->sym)->attr.allocatable));
3509
3510 if (a->expr->expr_type == EXPR_NULL
3511 && !pointer_dummy
3512 && !procptr_dummy
3513 && !(optional_dummy
3514 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3515 && !(allocatable_dummy
3516 && (gfc_option.allow_std & GFC_STD_F2008) != 0))
3517 {
3518 if (where
3519 && (!f->sym->attr.optional
3520 || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
3521 || (f->sym->ts.type == BT_CLASS
3522 && CLASS_DATA (f->sym)->attr.allocatable)))
3523 gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
3524 where, f->sym->name);
3525 else if (where)
3526 gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
3527 "dummy %qs", where, f->sym->name);
3528 ok = false;
3529 goto match;
3530 }
3531
3532 if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
3533 is_elemental, where))
3534 {
3535 ok = false;
3536 goto match;
3537 }
3538
3539 /* TS 29113, 6.3p2; F2018 15.5.2.4. */
3540 if (f->sym->ts.type == BT_ASSUMED
3541 && (a->expr->ts.type == BT_DERIVED
3542 || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
3543 {
3544 gfc_symbol *derived = (a->expr->ts.type == BT_DERIVED
3545 ? a->expr->ts.u.derived
3546 : CLASS_DATA (a->expr)->ts.u.derived);
3547 gfc_namespace *f2k_derived = derived->f2k_derived;
3548 if (derived->attr.pdt_type
3549 || (f2k_derived
3550 && (f2k_derived->finalizers || f2k_derived->tb_sym_root)))
3551 {
3552 gfc_error ("Actual argument at %L to assumed-type dummy "
3553 "has type parameters or is of "
3554 "derived type with type-bound or FINAL procedures",
3555 &a->expr->where);
3556 ok = false;
3557 goto match;
3558 }
3559 }
3560
3561 if (UNLIMITED_POLY (a->expr)
3562 && !(f->sym->ts.type == BT_ASSUMED || UNLIMITED_POLY (f->sym)))
3563 {
3564 gfc_error ("Unlimited polymorphic actual argument at %L is not "
3565 "matched with either an unlimited polymorphic or "
3566 "assumed type dummy argument", &a->expr->where);
3567 ok = false;
3568 goto match;
3569 }
3570
3571 /* Special case for character arguments. For allocatable, pointer
3572 and assumed-shape dummies, the string length needs to match
3573 exactly. */
3574 if (a->expr->ts.type == BT_CHARACTER
3575 && a->expr->ts.u.cl && a->expr->ts.u.cl->length
3576 && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
3577 && a->expr->ts.u.cl->length->ts.type == BT_INTEGER
3578 && f->sym->ts.type == BT_CHARACTER && f->sym->ts.u.cl
3579 && f->sym->ts.u.cl->length
3580 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
3581 && f->sym->ts.u.cl->length->ts.type == BT_INTEGER
3582 && (f->sym->attr.pointer || f->sym->attr.allocatable
3583 || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
3584 && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
3585 f->sym->ts.u.cl->length->value.integer) != 0))
3586 {
3587 if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
3588 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
3589 "argument and pointer or allocatable dummy argument "
3590 "%qs at %L",
3591 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3592 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3593 f->sym->name, &a->expr->where);
3594 else if (where)
3595 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
3596 "argument and assumed-shape dummy argument %qs "
3597 "at %L",
3598 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3599 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3600 f->sym->name, &a->expr->where);
3601 ok = false;
3602 goto match;
3603 }
3604
3605 if ((f->sym->attr.pointer || f->sym->attr.allocatable)
3606 && f->sym->ts.deferred != a->expr->ts.deferred
3607 && a->expr->ts.type == BT_CHARACTER)
3608 {
3609 if (where)
3610 gfc_error ("Actual argument at %L to allocatable or "
3611 "pointer dummy argument %qs must have a deferred "
3612 "length type parameter if and only if the dummy has one",
3613 &a->expr->where, f->sym->name);
3614 ok = false;
3615 goto match;
3616 }
3617
3618 if (f->sym->ts.type == BT_CLASS)
3619 goto skip_size_check;
3620
3621 /* Skip size check for NULL() actual without MOLD argument. */
3622 if (a->expr->expr_type == EXPR_NULL && a->expr->ts.type == BT_UNKNOWN)
3623 goto skip_size_check;
3624
3625 actual_size = get_expr_storage_size (a->expr);
3626 formal_size = get_sym_storage_size (f->sym);
3627 if (actual_size != 0 && actual_size < formal_size
3628 && a->expr->ts.type != BT_PROCEDURE
3629 && f->sym->attr.flavor != FL_PROCEDURE)
3630 {
3631 if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
3632 {
3633 gfc_warning (0, "Character length of actual argument shorter "
3634 "than of dummy argument %qs (%lu/%lu) at %L",
3635 f->sym->name, actual_size, formal_size,
3636 &a->expr->where);
3637 goto skip_size_check;
3638 }
3639 else if (where)
3640 {
3641 /* Emit a warning for -std=legacy and an error otherwise. */
3642 if (gfc_option.warn_std == 0)
3643 gfc_warning (0, "Actual argument contains too few "
3644 "elements for dummy argument %qs (%lu/%lu) "
3645 "at %L", f->sym->name, actual_size,
3646 formal_size, &a->expr->where);
3647 else
3648 gfc_error_now ("Actual argument contains too few "
3649 "elements for dummy argument %qs (%lu/%lu) "
3650 "at %L", f->sym->name, actual_size,
3651 formal_size, &a->expr->where);
3652 }
3653 ok = false;
3654 goto match;
3655 }
3656
3657 skip_size_check:
3658
3659 /* Satisfy either: F03:12.4.1.3 by ensuring that a procedure pointer
3660 actual argument is provided for a procedure pointer formal argument;
3661 or: F08:12.5.2.9 (F18:15.5.2.10) by ensuring that the effective
3662 argument shall be an external, internal, module, or dummy procedure.
3663 The interfaces are checked elsewhere. */
3664 if (f->sym->attr.proc_pointer
3665 && !((a->expr->expr_type == EXPR_VARIABLE
3666 && (a->expr->symtree->n.sym->attr.proc_pointer
3667 || gfc_is_proc_ptr_comp (a->expr)))
3668 || (a->expr->ts.type == BT_PROCEDURE
3669 && f->sym->ts.interface)
3670 || (a->expr->expr_type == EXPR_FUNCTION
3671 && is_procptr_result (a->expr))))
3672 {
3673 if (where)
3674 gfc_error ("Expected a procedure pointer for argument %qs at %L",
3675 f->sym->name, &a->expr->where);
3676 ok = false;
3677 goto match;
3678 }
3679
3680 /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
3681 provided for a procedure formal argument. */
3682 if (f->sym->attr.flavor == FL_PROCEDURE
3683 && !((a->expr->expr_type == EXPR_VARIABLE
3684 && (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
3685 || a->expr->symtree->n.sym->attr.proc_pointer
3686 || gfc_is_proc_ptr_comp (a->expr)))
3687 || (a->expr->expr_type == EXPR_FUNCTION
3688 && is_procptr_result (a->expr))))
3689 {
3690 if (where)
3691 gfc_error ("Expected a procedure for argument %qs at %L",
3692 f->sym->name, &a->expr->where);
3693 ok = false;
3694 goto match;
3695 }
3696
3697 /* Class array variables and expressions store array info in a
3698 different place from non-class objects; consolidate the logic
3699 to access it here instead of repeating it below. Note that
3700 pointer_arg and allocatable_arg are not fully general and are
3701 only used in a specific situation below with an assumed-rank
3702 argument. */
3703 if (f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym))
3704 {
3705 gfc_component *classdata = CLASS_DATA (f->sym);
3706 fas = classdata->as;
3707 pointer_dummy = classdata->attr.class_pointer;
3708 }
3709 else
3710 {
3711 fas = f->sym->as;
3712 pointer_dummy = f->sym->attr.pointer;
3713 }
3714
3715 if (a->expr->expr_type != EXPR_VARIABLE
3716 && !(a->expr->expr_type == EXPR_NULL
3717 && a->expr->ts.type != BT_UNKNOWN))
3718 {
3719 aas = NULL;
3720 pointer_arg = false;
3721 allocatable_arg = false;
3722 }
3723 else if (a->expr->ts.type == BT_CLASS
3724 && a->expr->symtree->n.sym
3725 && CLASS_DATA (a->expr->symtree->n.sym))
3726 {
3727 gfc_component *classdata = CLASS_DATA (a->expr->symtree->n.sym);
3728 aas = classdata->as;
3729 pointer_arg = classdata->attr.class_pointer;
3730 allocatable_arg = classdata->attr.allocatable;
3731 }
3732 else
3733 {
3734 aas = a->expr->symtree->n.sym->as;
3735 pointer_arg = a->expr->symtree->n.sym->attr.pointer;
3736 allocatable_arg = a->expr->symtree->n.sym->attr.allocatable;
3737 }
3738
3739 /* F2018:9.5.2(2) permits assumed-size whole array expressions as
3740 actual arguments only if the shape is not required; thus it
3741 cannot be passed to an assumed-shape array dummy.
3742 F2018:15.5.2.(2) permits passing a nonpointer actual to an
3743 intent(in) pointer dummy argument and this is accepted by
3744 the compare_pointer check below, but this also requires shape
3745 information.
3746 There's more discussion of this in PR94110. */
3747 if (fas
3748 && (fas->type == AS_ASSUMED_SHAPE
3749 || fas->type == AS_DEFERRED
3750 || (fas->type == AS_ASSUMED_RANK && pointer_dummy))
3751 && aas
3752 && aas->type == AS_ASSUMED_SIZE
3753 && (a->expr->ref == NULL
3754 || (a->expr->ref->type == REF_ARRAY
3755 && a->expr->ref->u.ar.type == AR_FULL)))
3756 {
3757 if (where)
3758 gfc_error ("Actual argument for %qs cannot be an assumed-size"
3759 " array at %L", f->sym->name, where);
3760 ok = false;
3761 goto match;
3762 }
3763
3764 /* Diagnose F2018 C839 (TS29113 C535c). Here the problem is
3765 passing an assumed-size array to an INTENT(OUT) assumed-rank
3766 dummy when it doesn't have the size information needed to run
3767 initializers and finalizers. */
3768 if (f->sym->attr.intent == INTENT_OUT
3769 && fas
3770 && fas->type == AS_ASSUMED_RANK
3771 && aas
3772 && ((aas->type == AS_ASSUMED_SIZE
3773 && (a->expr->ref == NULL
3774 || (a->expr->ref->type == REF_ARRAY
3775 && a->expr->ref->u.ar.type == AR_FULL)))
3776 || (aas->type == AS_ASSUMED_RANK
3777 && !pointer_arg
3778 && !allocatable_arg))
3779 && (a->expr->ts.type == BT_CLASS
3780 || (a->expr->ts.type == BT_DERIVED
3781 && (gfc_is_finalizable (a->expr->ts.u.derived, NULL)
3782 || gfc_has_ultimate_allocatable (a->expr)
3783 || gfc_has_default_initializer
3784 (a->expr->ts.u.derived)))))
3785 {
3786 if (where)
3787 gfc_error ("Actual argument to assumed-rank INTENT(OUT) "
3788 "dummy %qs at %L cannot be of unknown size",
3789 f->sym->name, where);
3790 ok = false;
3791 goto match;
3792 }
3793
3794 if (a->expr->expr_type != EXPR_NULL)
3795 {
3796 int cmp = compare_pointer (f->sym, a->expr);
3797 bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);
3798
3799 if (pre2008 && cmp == 0)
3800 {
3801 if (where)
3802 gfc_error ("Actual argument for %qs at %L must be a pointer",
3803 f->sym->name, &a->expr->where);
3804 ok = false;
3805 goto match;
3806 }
3807
3808 if (pre2008 && cmp == 2)
3809 {
3810 if (where)
3811 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
3812 "pointer dummy %qs", &a->expr->where, f->sym->name);
3813 ok = false;
3814 goto match;
3815 }
3816
3817 if (!pre2008 && cmp == 0)
3818 {
3819 if (where)
3820 gfc_error ("Actual argument for %qs at %L must be a pointer "
3821 "or a valid target for the dummy pointer in a "
3822 "pointer assignment statement",
3823 f->sym->name, &a->expr->where);
3824 ok = false;
3825 goto match;
3826 }
3827 }
3828
3829
3830 /* Fortran 2008, C1242. */
3831 if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
3832 {
3833 if (where)
3834 gfc_error ("Coindexed actual argument at %L to pointer "
3835 "dummy %qs",
3836 &a->expr->where, f->sym->name);
3837 ok = false;
3838 goto match;
3839 }
3840
3841 /* Fortran 2008, 12.5.2.5 (no constraint). */
3842 if (a->expr->expr_type == EXPR_VARIABLE
3843 && f->sym->attr.intent != INTENT_IN
3844 && f->sym->attr.allocatable
3845 && gfc_is_coindexed (a->expr))
3846 {
3847 if (where)
3848 gfc_error ("Coindexed actual argument at %L to allocatable "
3849 "dummy %qs requires INTENT(IN)",
3850 &a->expr->where, f->sym->name);
3851 ok = false;
3852 goto match;
3853 }
3854
3855 /* Fortran 2008, C1237. */
3856 if (a->expr->expr_type == EXPR_VARIABLE
3857 && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
3858 && gfc_is_coindexed (a->expr)
3859 && (a->expr->symtree->n.sym->attr.volatile_
3860 || a->expr->symtree->n.sym->attr.asynchronous))
3861 {
3862 if (where)
3863 gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
3864 "%L requires that dummy %qs has neither "
3865 "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
3866 f->sym->name);
3867 ok = false;
3868 goto match;
3869 }
3870
3871 /* Fortran 2008, 12.5.2.4 (no constraint). */
3872 if (a->expr->expr_type == EXPR_VARIABLE
3873 && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
3874 && gfc_is_coindexed (a->expr)
3875 && gfc_has_ultimate_allocatable (a->expr))
3876 {
3877 if (where)
3878 gfc_error ("Coindexed actual argument at %L with allocatable "
3879 "ultimate component to dummy %qs requires either VALUE "
3880 "or INTENT(IN)", &a->expr->where, f->sym->name);
3881 ok = false;
3882 goto match;
3883 }
3884
3885 if (f->sym->ts.type == BT_CLASS
3886 && CLASS_DATA (f->sym)->attr.allocatable
3887 && gfc_is_class_array_ref (a->expr, &full_array)
3888 && !full_array)
3889 {
3890 if (where)
3891 gfc_error ("Actual CLASS array argument for %qs must be a full "
3892 "array at %L", f->sym->name, &a->expr->where);
3893 ok = false;
3894 goto match;
3895 }
3896
3897
3898 if (a->expr->expr_type != EXPR_NULL
3899 && !compare_allocatable (f->sym, a->expr))
3900 {
3901 if (where)
3902 gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
3903 f->sym->name, &a->expr->where);
3904 ok = false;
3905 goto match;
3906 }
3907
3908 if (a->expr->expr_type == EXPR_FUNCTION
3909 && a->expr->value.function.esym
3910 && f->sym->attr.allocatable)
3911 {
3912 if (where)
3913 gfc_error ("Actual argument for %qs at %L is a function result "
3914 "and the dummy argument is ALLOCATABLE",
3915 f->sym->name, &a->expr->where);
3916 ok = false;
3917 goto match;
3918 }
3919
3920 /* Check intent = OUT/INOUT for definable actual argument. */
3921 if (!in_statement_function
3922 && (f->sym->attr.intent == INTENT_OUT
3923 || f->sym->attr.intent == INTENT_INOUT))
3924 {
3925 const char* context = (where
3926 ? _("actual argument to INTENT = OUT/INOUT")
3927 : NULL);
3928
3929 if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3930 && CLASS_DATA (f->sym)->attr.class_pointer)
3931 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
3932 && !gfc_check_vardef_context (a->expr, true, false, false, context))
3933 {
3934 ok = false;
3935 goto match;
3936 }
3937 if (!gfc_check_vardef_context (a->expr, false, false, false, context))
3938 {
3939 ok = false;
3940 goto match;
3941 }
3942 }
3943
3944 if ((f->sym->attr.intent == INTENT_OUT
3945 || f->sym->attr.intent == INTENT_INOUT
3946 || f->sym->attr.volatile_
3947 || f->sym->attr.asynchronous)
3948 && gfc_has_vector_subscript (a->expr))
3949 {
3950 if (where)
3951 gfc_error ("Array-section actual argument with vector "
3952 "subscripts at %L is incompatible with INTENT(OUT), "
3953 "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
3954 "of the dummy argument %qs",
3955 &a->expr->where, f->sym->name);
3956 ok = false;
3957 goto match;
3958 }
3959
3960 /* C1232 (R1221) For an actual argument which is an array section or
3961 an assumed-shape array, the dummy argument shall be an assumed-
3962 shape array, if the dummy argument has the VOLATILE attribute. */
3963
3964 if (f->sym->attr.volatile_
3965 && a->expr->expr_type == EXPR_VARIABLE
3966 && a->expr->symtree->n.sym->as
3967 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
3968 && !(fas && fas->type == AS_ASSUMED_SHAPE))
3969 {
3970 if (where)
3971 gfc_error ("Assumed-shape actual argument at %L is "
3972 "incompatible with the non-assumed-shape "
3973 "dummy argument %qs due to VOLATILE attribute",
3974 &a->expr->where,f->sym->name);
3975 ok = false;
3976 goto match;
3977 }
3978
3979 /* Find the last array_ref. */
3980 actual_arr_ref = NULL;
3981 if (a->expr->ref)
3982 actual_arr_ref = gfc_find_array_ref (a->expr, true);
3983
3984 if (f->sym->attr.volatile_
3985 && actual_arr_ref && actual_arr_ref->type == AR_SECTION
3986 && !(fas && fas->type == AS_ASSUMED_SHAPE))
3987 {
3988 if (where)
3989 gfc_error ("Array-section actual argument at %L is "
3990 "incompatible with the non-assumed-shape "
3991 "dummy argument %qs due to VOLATILE attribute",
3992 &a->expr->where, f->sym->name);
3993 ok = false;
3994 goto match;
3995 }
3996
3997 /* C1233 (R1221) For an actual argument which is a pointer array, the
3998 dummy argument shall be an assumed-shape or pointer array, if the
3999 dummy argument has the VOLATILE attribute. */
4000
4001 if (f->sym->attr.volatile_
4002 && a->expr->expr_type == EXPR_VARIABLE
4003 && a->expr->symtree->n.sym->attr.pointer
4004 && a->expr->symtree->n.sym->as
4005 && !(fas
4006 && (fas->type == AS_ASSUMED_SHAPE
4007 || f->sym->attr.pointer)))
4008 {
4009 if (where)
4010 gfc_error ("Pointer-array actual argument at %L requires "
4011 "an assumed-shape or pointer-array dummy "
4012 "argument %qs due to VOLATILE attribute",
4013 &a->expr->where,f->sym->name);
4014 ok = false;
4015 goto match;
4016 }
4017
4018 match:
4019 if (a == actual)
4020 na = i;
4021
4022 new_arg[i++] = a;
4023 }
4024
4025 /* Give up now if we saw any bad argument. */
4026 if (!ok)
4027 return false;
4028
4029 /* Make sure missing actual arguments are optional. */
4030 i = 0;
4031 for (f = formal; f; f = f->next, i++)
4032 {
4033 if (new_arg[i] != NULL)
4034 continue;
4035 if (f->sym == NULL)
4036 {
4037 if (where)
4038 gfc_error ("Missing alternate return spec in subroutine call "
4039 "at %L", where);
4040 return false;
4041 }
4042 /* For CLASS, the optional attribute might be set at either location. */
4043 if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
4044 && !f->sym->attr.optional)
4045 || (in_statement_function
4046 && (f->sym->attr.optional
4047 || (f->sym->ts.type == BT_CLASS
4048 && CLASS_DATA (f->sym)->attr.optional))))
4049 {
4050 if (where)
4051 gfc_error ("Missing actual argument for argument %qs at %L",
4052 f->sym->name, where);
4053 return false;
4054 }
4055 }
4056
4057 /* We should have handled the cases where the formal arglist is null
4058 already. */
4059 gcc_assert (n > 0);
4060
4061 /* The argument lists are compatible. We now relink a new actual
4062 argument list with null arguments in the right places. The head
4063 of the list remains the head. */
4064 for (f = formal, i = 0; f; f = f->next, i++)
4065 if (new_arg[i] == NULL)
4066 {
4067 new_arg[i] = gfc_get_actual_arglist ();
4068 new_arg[i]->associated_dummy = get_nonintrinsic_dummy_arg (f);
4069 }
4070
4071 if (na != 0)
4072 {
4073 std::swap (*new_arg[0], *actual);
4074 std::swap (new_arg[0], new_arg[na]);
4075 }
4076
4077 for (i = 0; i < n - 1; i++)
4078 new_arg[i]->next = new_arg[i + 1];
4079
4080 new_arg[i]->next = NULL;
4081
4082 if (*ap == NULL && n > 0)
4083 *ap = new_arg[0];
4084
4085 return true;
4086}
4087
4088
4089typedef struct
4090{
4091 gfc_formal_arglist *f;
4092 gfc_actual_arglist *a;
4093}
4094argpair;
4095
4096/* qsort comparison function for argument pairs, with the following
4097 order:
4098 - p->a->expr == NULL
4099 - p->a->expr->expr_type != EXPR_VARIABLE
4100 - by gfc_symbol pointer value (larger first). */
4101
4102static int
4103pair_cmp (const void *p1, const void *p2)
4104{
4105 const gfc_actual_arglist *a1, *a2;
4106
4107 /* *p1 and *p2 are elements of the to-be-sorted array. */
4108 a1 = ((const argpair *) p1)->a;
4109 a2 = ((const argpair *) p2)->a;
4110 if (!a1->expr)
4111 {
4112 if (!a2->expr)
4113 return 0;
4114 return -1;
4115 }
4116 if (!a2->expr)
4117 return 1;
4118 if (a1->expr->expr_type != EXPR_VARIABLE)
4119 {
4120 if (a2->expr->expr_type != EXPR_VARIABLE)
4121 return 0;
4122 return -1;
4123 }
4124 if (a2->expr->expr_type != EXPR_VARIABLE)
4125 return 1;
4126 if (a1->expr->symtree->n.sym > a2->expr->symtree->n.sym)
4127 return -1;
4128 return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
4129}
4130
4131
4132/* Given two expressions from some actual arguments, test whether they
4133 refer to the same expression. The analysis is conservative.
4134 Returning false will produce no warning. */
4135
4136static bool
4137compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
4138{
4139 const gfc_ref *r1, *r2;
4140
4141 if (!e1 || !e2
4142 || e1->expr_type != EXPR_VARIABLE
4143 || e2->expr_type != EXPR_VARIABLE
4144 || e1->symtree->n.sym != e2->symtree->n.sym)
4145 return false;
4146
4147 /* TODO: improve comparison, see expr.cc:show_ref(). */
4148 for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
4149 {
4150 if (r1->type != r2->type)
4151 return false;
4152 switch (r1->type)
4153 {
4154 case REF_ARRAY:
4155 if (r1->u.ar.type != r2->u.ar.type)
4156 return false;
4157 /* TODO: At the moment, consider only full arrays;
4158 we could do better. */
4159 if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
4160 return false;
4161 break;
4162
4163 case REF_COMPONENT:
4164 if (r1->u.c.component != r2->u.c.component)
4165 return false;
4166 break;
4167
4168 case REF_SUBSTRING:
4169 return false;
4170
4171 case REF_INQUIRY:
4172 if (e1->symtree->n.sym->ts.type == BT_COMPLEX
4173 && e1->ts.type == BT_REAL && e2->ts.type == BT_REAL
4174 && r1->u.i != r2->u.i)
4175 return false;
4176 break;
4177
4178 default:
4179 gfc_internal_error ("compare_actual_expr(): Bad component code");
4180 }
4181 }
4182 if (!r1 && !r2)
4183 return true;
4184 return false;
4185}
4186
4187
4188/* Given formal and actual argument lists that correspond to one
4189 another, check that identical actual arguments aren't not
4190 associated with some incompatible INTENTs. */
4191
4192static bool
4193check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
4194{
4195 sym_intent f1_intent, f2_intent;
4196 gfc_formal_arglist *f1;
4197 gfc_actual_arglist *a1;
4198 size_t n, i, j;
4199 argpair *p;
4200 bool t = true;
4201
4202 n = 0;
4203 for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
4204 {
4205 if (f1 == NULL && a1 == NULL)
4206 break;
4207 if (f1 == NULL || a1 == NULL)
4208 gfc_internal_error ("check_some_aliasing(): List mismatch");
4209 n++;
4210 }
4211 if (n == 0)
4212 return t;
4213 p = XALLOCAVEC (argpair, n);
4214
4215 for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
4216 {
4217 p[i].f = f1;
4218 p[i].a = a1;
4219 }
4220
4221 qsort (p, n, sizeof (argpair), pair_cmp);
4222
4223 for (i = 0; i < n; i++)
4224 {
4225 if (!p[i].a->expr
4226 || p[i].a->expr->expr_type != EXPR_VARIABLE
4227 || p[i].a->expr->ts.type == BT_PROCEDURE)
4228 continue;
4229 f1_intent = p[i].f->sym->attr.intent;
4230 for (j = i + 1; j < n; j++)
4231 {
4232 /* Expected order after the sort. */
4233 if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
4234 gfc_internal_error ("check_some_aliasing(): corrupted data");
4235
4236 /* Are the expression the same? */
4237 if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
4238 break;
4239 f2_intent = p[j].f->sym->attr.intent;
4240 if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
4241 || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
4242 || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
4243 {
4244 gfc_warning (0, "Same actual argument associated with INTENT(%s) "
4245 "argument %qs and INTENT(%s) argument %qs at %L",
4246 gfc_intent_string (f1_intent), p[i].f->sym->name,
4247 gfc_intent_string (f2_intent), p[j].f->sym->name,
4248 &p[i].a->expr->where);
4249 t = false;
4250 }
4251 }
4252 }
4253
4254 return t;
4255}
4256
4257
4258/* Given formal and actual argument lists that correspond to one
4259 another, check that they are compatible in the sense that intents
4260 are not mismatched. */
4261
4262static bool
4263check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
4264{
4265 sym_intent f_intent;
4266
4267 for (;; f = f->next, a = a->next)
4268 {
4269 gfc_expr *expr;
4270
4271 if (f == NULL && a == NULL)
4272 break;
4273 if (f == NULL || a == NULL)
4274 gfc_internal_error ("check_intents(): List mismatch");
4275
4276 if (a->expr && a->expr->expr_type == EXPR_FUNCTION
4277 && a->expr->value.function.isym
4278 && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
4279 expr = a->expr->value.function.actual->expr;
4280 else
4281 expr = a->expr;
4282
4283 if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
4284 continue;
4285
4286 f_intent = f->sym->attr.intent;
4287
4288 if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
4289 {
4290 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
4291 && CLASS_DATA (f->sym)->attr.class_pointer)
4292 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
4293 {
4294 gfc_error ("Procedure argument at %L is local to a PURE "
4295 "procedure and has the POINTER attribute",
4296 &expr->where);
4297 return false;
4298 }
4299 }
4300
4301 /* Fortran 2008, C1283. */
4302 if (gfc_pure (NULL) && gfc_is_coindexed (expr))
4303 {
4304 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
4305 {
4306 gfc_error ("Coindexed actual argument at %L in PURE procedure "
4307 "is passed to an INTENT(%s) argument",
4308 &expr->where, gfc_intent_string (f_intent));
4309 return false;
4310 }
4311
4312 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
4313 && CLASS_DATA (f->sym)->attr.class_pointer)
4314 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
4315 {
4316 gfc_error ("Coindexed actual argument at %L in PURE procedure "
4317 "is passed to a POINTER dummy argument",
4318 &expr->where);
4319 return false;
4320 }
4321 }
4322
4323 /* F2008, Section 12.5.2.4. */
4324 if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
4325 && gfc_is_coindexed (expr))
4326 {
4327 gfc_error ("Coindexed polymorphic actual argument at %L is passed "
4328 "polymorphic dummy argument %qs",
4329 &expr->where, f->sym->name);
4330 return false;
4331 }
4332 }
4333
4334 return true;
4335}
4336
4337
4338/* Check how a procedure is used against its interface. If all goes
4339 well, the actual argument list will also end up being properly
4340 sorted. */
4341
4342bool
4343gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
4344{
4345 gfc_actual_arglist *a;
4346 gfc_formal_arglist *dummy_args;
4347 bool implicit = false;
4348
4349 /* Warn about calls with an implicit interface. Special case
4350 for calling a ISO_C_BINDING because c_loc and c_funloc
4351 are pseudo-unknown. Additionally, warn about procedures not
4352 explicitly declared at all if requested. */
4353 if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
4354 {
4355 bool has_implicit_none_export = false;
4356 implicit = true;
4357 if (sym->attr.proc == PROC_UNKNOWN)
4358 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
4359 if (ns->has_implicit_none_export)
4360 {
4361 has_implicit_none_export = true;
4362 break;
4363 }
4364 if (has_implicit_none_export)
4365 {
4366 const char *guessed
4367 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
4368 if (guessed)
4369 gfc_error ("Procedure %qs called at %L is not explicitly declared"
4370 "; did you mean %qs?",
4371 sym->name, where, guessed);
4372 else
4373 gfc_error ("Procedure %qs called at %L is not explicitly declared",
4374 sym->name, where);
4375 return false;
4376 }
4377 if (warn_implicit_interface)
4378 gfc_warning (OPT_Wimplicit_interface,
4379 "Procedure %qs called with an implicit interface at %L",
4380 sym->name, where);
4381 else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
4382 gfc_warning (OPT_Wimplicit_procedure,
4383 "Procedure %qs called at %L is not explicitly declared",
4384 sym->name, where);
4385 gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
4386 }
4387
4388 if (sym->attr.if_source == IFSRC_UNKNOWN)
4389 {
4390 if (sym->attr.pointer)
4391 {
4392 gfc_error ("The pointer object %qs at %L must have an explicit "
4393 "function interface or be declared as array",
4394 sym->name, where);
4395 return false;
4396 }
4397
4398 if (sym->attr.allocatable && !sym->attr.external)
4399 {
4400 gfc_error ("The allocatable object %qs at %L must have an explicit "
4401 "function interface or be declared as array",
4402 sym->name, where);
4403 return false;
4404 }
4405
4406 if (sym->attr.allocatable)
4407 {
4408 gfc_error ("Allocatable function %qs at %L must have an explicit "
4409 "function interface", sym->name, where);
4410 return false;
4411 }
4412
4413 for (a = *ap; a; a = a->next)
4414 {
4415 if (a->expr && a->expr->error)
4416 return false;
4417
4418 /* F2018, 15.4.2.2 Explicit interface is required for a
4419 polymorphic dummy argument, so there is no way to
4420 legally have a class appear in an argument with an
4421 implicit interface. */
4422
4423 if (implicit && a->expr && a->expr->ts.type == BT_CLASS)
4424 {
4425 gfc_error ("Explicit interface required for polymorphic "
4426 "argument at %L",&a->expr->where);
4427 a->expr->error = 1;
4428 break;
4429 }
4430
4431 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4432 if (a->name != NULL && a->name[0] != '%')
4433 {
4434 gfc_error ("Keyword argument requires explicit interface "
4435 "for procedure %qs at %L", sym->name, &a->expr->where);
4436 break;
4437 }
4438
4439 /* TS 29113, 6.2. */
4440 if (a->expr && a->expr->ts.type == BT_ASSUMED
4441 && sym->intmod_sym_id != ISOCBINDING_LOC)
4442 {
4443 gfc_error ("Assumed-type argument %s at %L requires an explicit "
4444 "interface", a->expr->symtree->n.sym->name,
4445 &a->expr->where);
4446 a->expr->error = 1;
4447 break;
4448 }
4449
4450 /* F2008, C1303 and C1304. */
4451 if (a->expr
4452 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
4453 && a->expr->ts.u.derived
4454 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4455 && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
4456 || gfc_expr_attr (a->expr).lock_comp))
4457 {
4458 gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
4459 "component at %L requires an explicit interface for "
4460 "procedure %qs", &a->expr->where, sym->name);
4461 a->expr->error = 1;
4462 break;
4463 }
4464
4465 if (a->expr
4466 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
4467 && a->expr->ts.u.derived
4468 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4469 && a->expr->ts.u.derived->intmod_sym_id
4470 == ISOFORTRAN_EVENT_TYPE)
4471 || gfc_expr_attr (a->expr).event_comp))
4472 {
4473 gfc_error ("Actual argument of EVENT_TYPE or with EVENT_TYPE "
4474 "component at %L requires an explicit interface for "
4475 "procedure %qs", &a->expr->where, sym->name);
4476 a->expr->error = 1;
4477 break;
4478 }
4479
4480 if (a->expr && a->expr->expr_type == EXPR_NULL
4481 && a->expr->ts.type == BT_UNKNOWN)
4482 {
4483 gfc_error ("MOLD argument to NULL required at %L",
4484 &a->expr->where);
4485 a->expr->error = 1;
4486 return false;
4487 }
4488
4489 if (a->expr && a->expr->expr_type == EXPR_NULL)
4490 {
4491 gfc_error ("Passing intrinsic NULL as actual argument at %L "
4492 "requires an explicit interface", &a->expr->where);
4493 a->expr->error = 1;
4494 return false;
4495 }
4496
4497 /* TS 29113, C407b. */
4498 if (a->expr && a->expr->expr_type == EXPR_VARIABLE
4499 && symbol_rank (a->expr->symtree->n.sym) == -1)
4500 {
4501 gfc_error ("Assumed-rank argument requires an explicit interface "
4502 "at %L", &a->expr->where);
4503 a->expr->error = 1;
4504 return false;
4505 }
4506 }
4507
4508 return true;
4509 }
4510
4511 dummy_args = gfc_sym_get_dummy_args (sym);
4512
4513 /* For a statement function, check that types and type parameters of actual
4514 arguments and dummy arguments match. */
4515 if (!gfc_compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
4516 sym->attr.proc == PROC_ST_FUNCTION, where))
4517 return false;
4518
4519 if (!check_intents (dummy_args, *ap))
4520 return false;
4521
4522 if (warn_aliasing)
4523 check_some_aliasing (dummy_args, *ap);
4524
4525 return true;
4526}
4527
4528
4529/* Check how a procedure pointer component is used against its interface.
4530 If all goes well, the actual argument list will also end up being properly
4531 sorted. Completely analogous to gfc_procedure_use. */
4532
4533void
4534gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
4535{
4536 /* Warn about calls with an implicit interface. Special case
4537 for calling a ISO_C_BINDING because c_loc and c_funloc
4538 are pseudo-unknown. */
4539 if (warn_implicit_interface
4540 && comp->attr.if_source == IFSRC_UNKNOWN
4541 && !comp->attr.is_iso_c)
4542 gfc_warning (OPT_Wimplicit_interface,
4543 "Procedure pointer component %qs called with an implicit "
4544 "interface at %L", comp->name, where);
4545
4546 if (comp->attr.if_source == IFSRC_UNKNOWN)
4547 {
4548 gfc_actual_arglist *a;
4549 for (a = *ap; a; a = a->next)
4550 {
4551 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4552 if (a->name != NULL && a->name[0] != '%')
4553 {
4554 gfc_error ("Keyword argument requires explicit interface "
4555 "for procedure pointer component %qs at %L",
4556 comp->name, &a->expr->where);
4557 break;
4558 }
4559 }
4560
4561 return;
4562 }
4563
4564 if (!gfc_compare_actual_formal (ap, comp->ts.interface->formal, 0,
4565 comp->attr.elemental, false, where))
4566 return;
4567
4568 check_intents (comp->ts.interface->formal, *ap);
4569 if (warn_aliasing)
4570 check_some_aliasing (comp->ts.interface->formal, *ap);
4571}
4572
4573
4574/* Try if an actual argument list matches the formal list of a symbol,
4575 respecting the symbol's attributes like ELEMENTAL. This is used for
4576 GENERIC resolution. */
4577
4578bool
4579gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
4580{
4581 gfc_formal_arglist *dummy_args;
4582 bool r;
4583
4584 if (sym->attr.flavor != FL_PROCEDURE)
4585 return false;
4586
4587 dummy_args = gfc_sym_get_dummy_args (sym);
4588
4589 r = !sym->attr.elemental;
4590 if (gfc_compare_actual_formal (args, dummy_args, r, !r, false, NULL))
4591 {
4592 check_intents (dummy_args, *args);
4593 if (warn_aliasing)
4594 check_some_aliasing (dummy_args, *args);
4595 return true;
4596 }
4597
4598 return false;
4599}
4600
4601
4602/* Given an interface pointer and an actual argument list, search for
4603 a formal argument list that matches the actual. If found, returns
4604 a pointer to the symbol of the correct interface. Returns NULL if
4605 not found. */
4606
4607gfc_symbol *
4608gfc_search_interface (gfc_interface *intr, int sub_flag,
4609 gfc_actual_arglist **ap)
4610{
4611 gfc_symbol *elem_sym = NULL;
4612 gfc_symbol *null_sym = NULL;
4613 locus null_expr_loc;
4614 gfc_actual_arglist *a;
4615 bool has_null_arg = false;
4616
4617 for (a = *ap; a; a = a->next)
4618 if (a->expr && a->expr->expr_type == EXPR_NULL
4619 && a->expr->ts.type == BT_UNKNOWN)
4620 {
4621 has_null_arg = true;
4622 null_expr_loc = a->expr->where;
4623 break;
4624 }
4625
4626 for (; intr; intr = intr->next)
4627 {
4628 if (gfc_fl_struct (intr->sym->attr.flavor))
4629 continue;
4630 if (sub_flag && intr->sym->attr.function)
4631 continue;
4632 if (!sub_flag && intr->sym->attr.subroutine)
4633 continue;
4634
4635 if (gfc_arglist_matches_symbol (ap, intr->sym))
4636 {
4637 if (has_null_arg && null_sym)
4638 {
4639 gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
4640 "between specific functions %s and %s",
4641 &null_expr_loc, null_sym->name, intr->sym->name);
4642 return NULL;
4643 }
4644 else if (has_null_arg)
4645 {
4646 null_sym = intr->sym;
4647 continue;
4648 }
4649
4650 /* Satisfy 12.4.4.1 such that an elemental match has lower
4651 weight than a non-elemental match. */
4652 if (intr->sym->attr.elemental)
4653 {
4654 elem_sym = intr->sym;
4655 continue;
4656 }
4657 return intr->sym;
4658 }
4659 }
4660
4661 if (null_sym)
4662 return null_sym;
4663
4664 return elem_sym ? elem_sym : NULL;
4665}
4666
4667
4668/* Do a brute force recursive search for a symbol. */
4669
4670static gfc_symtree *
4671find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
4672{
4673 gfc_symtree * st;
4674
4675 if (root->n.sym == sym)
4676 return root;
4677
4678 st = NULL;
4679 if (root->left)
4680 st = find_symtree0 (root->left, sym);
4681 if (root->right && ! st)
4682 st = find_symtree0 (root->right, sym);
4683 return st;
4684}
4685
4686
4687/* Find a symtree for a symbol. */
4688
4689gfc_symtree *
4690gfc_find_sym_in_symtree (gfc_symbol *sym)
4691{
4692 gfc_symtree *st;
4693 gfc_namespace *ns;
4694
4695 /* First try to find it by name. */
4696 gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
4697 if (st && st->n.sym == sym)
4698 return st;
4699
4700 /* If it's been renamed, resort to a brute-force search. */
4701 /* TODO: avoid having to do this search. If the symbol doesn't exist
4702 in the symtree for the current namespace, it should probably be added. */
4703 for (ns = gfc_current_ns; ns; ns = ns->parent)
4704 {
4705 st = find_symtree0 (ns->sym_root, sym);
4706 if (st)
4707 return st;
4708 }
4709 gfc_internal_error ("Unable to find symbol %qs", sym->name);
4710 /* Not reached. */
4711}
4712
4713
4714/* See if the arglist to an operator-call contains a derived-type argument
4715 with a matching type-bound operator. If so, return the matching specific
4716 procedure defined as operator-target as well as the base-object to use
4717 (which is the found derived-type argument with operator). The generic
4718 name, if any, is transmitted to the final expression via 'gname'. */
4719
4720static gfc_typebound_proc*
4721matching_typebound_op (gfc_expr** tb_base,
4722 gfc_actual_arglist* args,
4723 gfc_intrinsic_op op, const char* uop,
4724 const char ** gname)
4725{
4726 gfc_actual_arglist* base;
4727
4728 for (base = args; base; base = base->next)
4729 if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
4730 {
4731 gfc_typebound_proc* tb;
4732 gfc_symbol* derived;
4733 bool result;
4734
4735 while (base->expr->expr_type == EXPR_OP
4736 && base->expr->value.op.op == INTRINSIC_PARENTHESES)
4737 base->expr = base->expr->value.op.op1;
4738
4739 if (base->expr->ts.type == BT_CLASS)
4740 {
4741 if (!base->expr->ts.u.derived || CLASS_DATA (base->expr) == NULL
4742 || !gfc_expr_attr (base->expr).class_ok)
4743 continue;
4744 derived = CLASS_DATA (base->expr)->ts.u.derived;
4745 }
4746 else
4747 derived = base->expr->ts.u.derived;
4748
4749 if (op == INTRINSIC_USER)
4750 {
4751 gfc_symtree* tb_uop;
4752
4753 gcc_assert (uop);
4754 tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
4755 false, NULL);
4756
4757 if (tb_uop)
4758 tb = tb_uop->n.tb;
4759 else
4760 tb = NULL;
4761 }
4762 else
4763 tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
4764 false, NULL);
4765
4766 /* This means we hit a PRIVATE operator which is use-associated and
4767 should thus not be seen. */
4768 if (!result)
4769 tb = NULL;
4770
4771 /* Look through the super-type hierarchy for a matching specific
4772 binding. */
4773 for (; tb; tb = tb->overridden)
4774 {
4775 gfc_tbp_generic* g;
4776
4777 gcc_assert (tb->is_generic);
4778 for (g = tb->u.generic; g; g = g->next)
4779 {
4780 gfc_symbol* target;
4781 gfc_actual_arglist* argcopy;
4782 bool matches;
4783
4784 gcc_assert (g->specific);
4785 if (g->specific->error)
4786 continue;
4787
4788 target = g->specific->u.specific->n.sym;
4789
4790 /* Check if this arglist matches the formal. */
4791 argcopy = gfc_copy_actual_arglist (args);
4792 matches = gfc_arglist_matches_symbol (&argcopy, target);
4793 gfc_free_actual_arglist (argcopy);
4794
4795 /* Return if we found a match. */
4796 if (matches)
4797 {
4798 *tb_base = base->expr;
4799 *gname = g->specific_st->name;
4800 return g->specific;
4801 }
4802 }
4803 }
4804 }
4805
4806 return NULL;
4807}
4808
4809
4810/* For the 'actual arglist' of an operator call and a specific typebound
4811 procedure that has been found the target of a type-bound operator, build the
4812 appropriate EXPR_COMPCALL and resolve it. We take this indirection over
4813 type-bound procedures rather than resolving type-bound operators 'directly'
4814 so that we can reuse the existing logic. */
4815
4816static void
4817build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
4818 gfc_expr* base, gfc_typebound_proc* target,
4819 const char *gname)
4820{
4821 e->expr_type = EXPR_COMPCALL;
4822 e->value.compcall.tbp = target;
4823 e->value.compcall.name = gname ? gname : "$op";
4824 e->value.compcall.actual = actual;
4825 e->value.compcall.base_object = base;
4826 e->value.compcall.ignore_pass = 1;
4827 e->value.compcall.assign = 0;
4828 if (e->ts.type == BT_UNKNOWN
4829 && target->function)
4830 {
4831 if (target->is_generic)
4832 e->ts = target->u.generic->specific->u.specific->n.sym->ts;
4833 else
4834 e->ts = target->u.specific->n.sym->ts;
4835 }
4836}
4837
4838
4839/* This subroutine is called when an expression is being resolved.
4840 The expression node in question is either a user defined operator
4841 or an intrinsic operator with arguments that aren't compatible
4842 with the operator. This subroutine builds an actual argument list
4843 corresponding to the operands, then searches for a compatible
4844 interface. If one is found, the expression node is replaced with
4845 the appropriate function call. We use the 'match' enum to specify
4846 whether a replacement has been made or not, or if an error occurred. */
4847
4848match
4849gfc_extend_expr (gfc_expr *e)
4850{
4851 gfc_actual_arglist *actual;
4852 gfc_symbol *sym;
4853 gfc_namespace *ns;
4854 gfc_user_op *uop;
4855 gfc_intrinsic_op i;
4856 const char *gname;
4857 gfc_typebound_proc* tbo;
4858 gfc_expr* tb_base;
4859
4860 sym = NULL;
4861
4862 actual = gfc_get_actual_arglist ();
4863 actual->expr = e->value.op.op1;
4864
4865 gname = NULL;
4866
4867 if (e->value.op.op2 != NULL)
4868 {
4869 actual->next = gfc_get_actual_arglist ();
4870 actual->next->expr = e->value.op.op2;
4871 }
4872
4873 i = fold_unary_intrinsic (e->value.op.op);
4874
4875 /* See if we find a matching type-bound operator. */
4876 if (i == INTRINSIC_USER)
4877 tbo = matching_typebound_op (&tb_base, actual,
4878 i, e->value.op.uop->name, &gname);
4879 else
4880 switch (i)
4881 {
4882#define CHECK_OS_COMPARISON(comp) \
4883 case INTRINSIC_##comp: \
4884 case INTRINSIC_##comp##_OS: \
4885 tbo = matching_typebound_op (&tb_base, actual, \
4886 INTRINSIC_##comp, NULL, &gname); \
4887 if (!tbo) \
4888 tbo = matching_typebound_op (&tb_base, actual, \
4889 INTRINSIC_##comp##_OS, NULL, &gname); \
4890 break;
4891 CHECK_OS_COMPARISON(EQ)
4892 CHECK_OS_COMPARISON(NE)
4893 CHECK_OS_COMPARISON(GT)
4894 CHECK_OS_COMPARISON(GE)
4895 CHECK_OS_COMPARISON(LT)
4896 CHECK_OS_COMPARISON(LE)
4897#undef CHECK_OS_COMPARISON
4898
4899 default:
4900 tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
4901 break;
4902 }
4903
4904 /* If there is a matching typebound-operator, replace the expression with
4905 a call to it and succeed. */
4906 if (tbo)
4907 {
4908 gcc_assert (tb_base);
4909 build_compcall_for_operator (e, actual, tb_base, tbo, gname);
4910
4911 if (!gfc_resolve_expr (e))
4912 return MATCH_ERROR;
4913 else
4914 return MATCH_YES;
4915 }
4916
4917 if (i == INTRINSIC_USER)
4918 {
4919 for (ns = gfc_current_ns; ns; ns = ns->parent)
4920 {
4921 uop = gfc_find_uop (e->value.op.uop->name, ns);
4922 if (uop == NULL)
4923 continue;
4924
4925 sym = gfc_search_interface (uop->op, 0, &actual);
4926 if (sym != NULL)
4927 break;
4928 }
4929 }
4930 else
4931 {
4932 for (ns = gfc_current_ns; ns; ns = ns->parent)
4933 {
4934 /* Due to the distinction between '==' and '.eq.' and friends, one has
4935 to check if either is defined. */
4936 switch (i)
4937 {
4938#define CHECK_OS_COMPARISON(comp) \
4939 case INTRINSIC_##comp: \
4940 case INTRINSIC_##comp##_OS: \
4941 sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
4942 if (!sym) \
4943 sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
4944 break;
4945 CHECK_OS_COMPARISON(EQ)
4946 CHECK_OS_COMPARISON(NE)
4947 CHECK_OS_COMPARISON(GT)
4948 CHECK_OS_COMPARISON(GE)
4949 CHECK_OS_COMPARISON(LT)
4950 CHECK_OS_COMPARISON(LE)
4951#undef CHECK_OS_COMPARISON
4952
4953 default:
4954 sym = gfc_search_interface (ns->op[i], 0, &actual);
4955 }
4956
4957 if (sym != NULL)
4958 break;
4959 }
4960
4961 /* F2018(15.4.3.4.2) requires that the use of unlimited polymorphic
4962 formal arguments does not override the intrinsic uses. */
4963 gfc_push_suppress_errors ();
4964 if (sym
4965 && (UNLIMITED_POLY (sym->formal->sym)
4966 || (sym->formal->next
4967 && UNLIMITED_POLY (sym->formal->next->sym)))
4968 && !gfc_check_operator_interface (sym, e->value.op.op, e->where))
4969 sym = NULL;
4970 gfc_pop_suppress_errors ();
4971 }
4972
4973 /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
4974 found rather than just taking the first one and not checking further. */
4975
4976 if (sym == NULL)
4977 {
4978 /* Don't use gfc_free_actual_arglist(). */
4979 free (actual->next);
4980 free (actual);
4981 return MATCH_NO;
4982 }
4983
4984 /* Change the expression node to a function call. */
4985 e->expr_type = EXPR_FUNCTION;
4986 e->symtree = gfc_find_sym_in_symtree (sym);
4987 e->value.function.actual = actual;
4988 e->value.function.esym = NULL;
4989 e->value.function.isym = NULL;
4990 e->value.function.name = NULL;
4991 e->user_operator = 1;
4992
4993 if (!gfc_resolve_expr (e))
4994 return MATCH_ERROR;
4995
4996 return MATCH_YES;
4997}
4998
4999
5000/* Tries to replace an assignment code node with a subroutine call to the
5001 subroutine associated with the assignment operator. Return true if the node
5002 was replaced. On false, no error is generated. */
5003
5004bool
5005gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
5006{
5007 gfc_actual_arglist *actual;
5008 gfc_expr *lhs, *rhs, *tb_base;
5009 gfc_symbol *sym = NULL;
5010 const char *gname = NULL;
5011 gfc_typebound_proc* tbo;
5012
5013 lhs = c->expr1;
5014 rhs = c->expr2;
5015
5016 /* Don't allow an intrinsic assignment with a BOZ rhs to be replaced. */
5017 if (c->op == EXEC_ASSIGN
5018 && c->expr1->expr_type == EXPR_VARIABLE
5019 && c->expr2->expr_type == EXPR_CONSTANT && c->expr2->ts.type == BT_BOZ)
5020 return false;
5021
5022 /* Don't allow an intrinsic assignment to be replaced. */
5023 if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
5024 && (rhs->rank == 0 || rhs->rank == lhs->rank)
5025 && (lhs->ts.type == rhs->ts.type
5026 || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
5027 return false;
5028
5029 actual = gfc_get_actual_arglist ();
5030 actual->expr = lhs;
5031
5032 actual->next = gfc_get_actual_arglist ();
5033 actual->next->expr = rhs;
5034
5035 /* TODO: Ambiguity-check, see above for gfc_extend_expr. */
5036
5037 /* See if we find a matching type-bound assignment. */
5038 tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
5039 NULL, &gname);
5040
5041 if (tbo)
5042 {
5043 /* Success: Replace the expression with a type-bound call. */
5044 gcc_assert (tb_base);
5045 c->expr1 = gfc_get_expr ();
5046 build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
5047 c->expr1->value.compcall.assign = 1;
5048 c->expr1->where = c->loc;
5049 c->expr2 = NULL;
5050 c->op = EXEC_COMPCALL;
5051 return true;
5052 }
5053
5054 /* See if we find an 'ordinary' (non-typebound) assignment procedure. */
5055 for (; ns; ns = ns->parent)
5056 {
5057 sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
5058 if (sym != NULL)
5059 break;
5060 }
5061
5062 if (sym)
5063 {
5064 /* Success: Replace the assignment with the call. */
5065 c->op = EXEC_ASSIGN_CALL;
5066 c->symtree = gfc_find_sym_in_symtree (sym);
5067 c->expr1 = NULL;
5068 c->expr2 = NULL;
5069 c->ext.actual = actual;
5070 return true;
5071 }
5072
5073 /* Failure: No assignment procedure found. */
5074 free (actual->next);
5075 free (actual);
5076 return false;
5077}
5078
5079
5080/* Make sure that the interface just parsed is not already present in
5081 the given interface list. Ambiguity isn't checked yet since module
5082 procedures can be present without interfaces. */
5083
5084bool
5085gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
5086{
5087 gfc_interface *ip;
5088
5089 for (ip = base; ip; ip = ip->next)
5090 {
5091 if (ip->sym == new_sym)
5092 {
5093 gfc_error ("Entity %qs at %L is already present in the interface",
5094 new_sym->name, &loc);
5095 return false;
5096 }
5097 }
5098
5099 return true;
5100}
5101
5102
5103/* Add a symbol to the current interface. */
5104
5105bool
5106gfc_add_interface (gfc_symbol *new_sym)
5107{
5108 gfc_interface **head, *intr;
5109 gfc_namespace *ns;
5110 gfc_symbol *sym;
5111
5112 switch (current_interface.type)
5113 {
5114 case INTERFACE_NAMELESS:
5115 case INTERFACE_ABSTRACT:
5116 return true;
5117
5118 case INTERFACE_INTRINSIC_OP:
5119 for (ns = current_interface.ns; ns; ns = ns->parent)
5120 switch (current_interface.op)
5121 {
5122 case INTRINSIC_EQ:
5123 case INTRINSIC_EQ_OS:
5124 if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym,
5125 gfc_current_locus)
5126 || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS],
5127 new_sym, gfc_current_locus))
5128 return false;
5129 break;
5130
5131 case INTRINSIC_NE:
5132 case INTRINSIC_NE_OS:
5133 if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym,
5134 gfc_current_locus)
5135 || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS],
5136 new_sym, gfc_current_locus))
5137 return false;
5138 break;
5139
5140 case INTRINSIC_GT:
5141 case INTRINSIC_GT_OS:
5142 if (!gfc_check_new_interface (ns->op[INTRINSIC_GT],
5143 new_sym, gfc_current_locus)
5144 || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS],
5145 new_sym, gfc_current_locus))
5146 return false;
5147 break;
5148
5149 case INTRINSIC_GE:
5150 case INTRINSIC_GE_OS:
5151 if (!gfc_check_new_interface (ns->op[INTRINSIC_GE],
5152 new_sym, gfc_current_locus)
5153 || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS],
5154 new_sym, gfc_current_locus))
5155 return false;
5156 break;
5157
5158 case INTRINSIC_LT:
5159 case INTRINSIC_LT_OS:
5160 if (!gfc_check_new_interface (ns->op[INTRINSIC_LT],
5161 new_sym, gfc_current_locus)
5162 || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS],
5163 new_sym, gfc_current_locus))
5164 return false;
5165 break;
5166
5167 case INTRINSIC_LE:
5168 case INTRINSIC_LE_OS:
5169 if (!gfc_check_new_interface (ns->op[INTRINSIC_LE],
5170 new_sym, gfc_current_locus)
5171 || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS],
5172 new_sym, gfc_current_locus))
5173 return false;
5174 break;
5175
5176 default:
5177 if (!gfc_check_new_interface (ns->op[current_interface.op],
5178 new_sym, gfc_current_locus))
5179 return false;
5180 }
5181
5182 head = &current_interface.ns->op[current_interface.op];
5183 break;
5184
5185 case INTERFACE_GENERIC:
5186 case INTERFACE_DTIO:
5187 for (ns = current_interface.ns; ns; ns = ns->parent)
5188 {
5189 gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
5190 if (sym == NULL)
5191 continue;
5192
5193 if (!gfc_check_new_interface (sym->generic,
5194 new_sym, gfc_current_locus))
5195 return false;
5196 }
5197
5198 head = &current_interface.sym->generic;
5199 break;
5200
5201 case INTERFACE_USER_OP:
5202 if (!gfc_check_new_interface (current_interface.uop->op,
5203 new_sym, gfc_current_locus))
5204 return false;
5205
5206 head = &current_interface.uop->op;
5207 break;
5208
5209 default:
5210 gfc_internal_error ("gfc_add_interface(): Bad interface type");
5211 }
5212
5213 intr = gfc_get_interface ();
5214 intr->sym = new_sym;
5215 intr->where = gfc_current_locus;
5216
5217 intr->next = *head;
5218 *head = intr;
5219
5220 return true;
5221}
5222
5223
5224gfc_interface *&
5225gfc_current_interface_head (void)
5226{
5227 switch (current_interface.type)
5228 {
5229 case INTERFACE_INTRINSIC_OP:
5230 return current_interface.ns->op[current_interface.op];
5231
5232 case INTERFACE_GENERIC:
5233 case INTERFACE_DTIO:
5234 return current_interface.sym->generic;
5235
5236 case INTERFACE_USER_OP:
5237 return current_interface.uop->op;
5238
5239 default:
5240 gcc_unreachable ();
5241 }
5242}
5243
5244
5245void
5246gfc_set_current_interface_head (gfc_interface *i)
5247{
5248 switch (current_interface.type)
5249 {
5250 case INTERFACE_INTRINSIC_OP:
5251 current_interface.ns->op[current_interface.op] = i;
5252 break;
5253
5254 case INTERFACE_GENERIC:
5255 case INTERFACE_DTIO:
5256 current_interface.sym->generic = i;
5257 break;
5258
5259 case INTERFACE_USER_OP:
5260 current_interface.uop->op = i;
5261 break;
5262
5263 default:
5264 gcc_unreachable ();
5265 }
5266}
5267
5268
5269/* Gets rid of a formal argument list. We do not free symbols.
5270 Symbols are freed when a namespace is freed. */
5271
5272void
5273gfc_free_formal_arglist (gfc_formal_arglist *p)
5274{
5275 gfc_formal_arglist *q;
5276
5277 for (; p; p = q)
5278 {
5279 q = p->next;
5280 free (p);
5281 }
5282}
5283
5284
5285/* Check that it is ok for the type-bound procedure 'proc' to override the
5286 procedure 'old', cf. F08:4.5.7.3. */
5287
5288bool
5289gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
5290{
5291 locus where;
5292 gfc_symbol *proc_target, *old_target;
5293 unsigned proc_pass_arg, old_pass_arg, argpos;
5294 gfc_formal_arglist *proc_formal, *old_formal;
5295 bool check_type;
5296 char err[200];
5297
5298 /* This procedure should only be called for non-GENERIC proc. */
5299 gcc_assert (!proc->n.tb->is_generic);
5300
5301 /* If the overwritten procedure is GENERIC, this is an error. */
5302 if (old->n.tb->is_generic)
5303 {
5304 gfc_error ("Cannot overwrite GENERIC %qs at %L",
5305 old->name, &proc->n.tb->where);
5306 return false;
5307 }
5308
5309 where = proc->n.tb->where;
5310 proc_target = proc->n.tb->u.specific->n.sym;
5311 old_target = old->n.tb->u.specific->n.sym;
5312
5313 /* Check that overridden binding is not NON_OVERRIDABLE. */
5314 if (old->n.tb->non_overridable)
5315 {
5316 gfc_error ("%qs at %L overrides a procedure binding declared"
5317 " NON_OVERRIDABLE", proc->name, &where);
5318 return false;
5319 }
5320
5321 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
5322 if (!old->n.tb->deferred && proc->n.tb->deferred)
5323 {
5324 gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
5325 " non-DEFERRED binding", proc->name, &where);
5326 return false;
5327 }
5328
5329 /* If the overridden binding is PURE, the overriding must be, too. */
5330 if (old_target->attr.pure && !proc_target->attr.pure)
5331 {
5332 gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
5333 proc->name, &where);
5334 return false;
5335 }
5336
5337 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
5338 is not, the overriding must not be either. */
5339 if (old_target->attr.elemental && !proc_target->attr.elemental)
5340 {
5341 gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
5342 " ELEMENTAL", proc->name, &where);
5343 return false;
5344 }
5345 if (!old_target->attr.elemental && proc_target->attr.elemental)
5346 {
5347 gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
5348 " be ELEMENTAL, either", proc->name, &where);
5349 return false;
5350 }
5351
5352 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
5353 SUBROUTINE. */
5354 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
5355 {
5356 gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
5357 " SUBROUTINE", proc->name, &where);
5358 return false;
5359 }
5360
5361 /* If the overridden binding is a FUNCTION, the overriding must also be a
5362 FUNCTION and have the same characteristics. */
5363 if (old_target->attr.function)
5364 {
5365 if (!proc_target->attr.function)
5366 {
5367 gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
5368 " FUNCTION", proc->name, &where);
5369 return false;
5370 }
5371
5372 if (!gfc_check_result_characteristics (proc_target, old_target,
5373 err, sizeof(err)))
5374 {
5375 gfc_error ("Result mismatch for the overriding procedure "
5376 "%qs at %L: %s", proc->name, &where, err);
5377 return false;
5378 }
5379 }
5380
5381 /* If the overridden binding is PUBLIC, the overriding one must not be
5382 PRIVATE. */
5383 if (old->n.tb->access == ACCESS_PUBLIC
5384 && proc->n.tb->access == ACCESS_PRIVATE)
5385 {
5386 gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
5387 " PRIVATE", proc->name, &where);
5388 return false;
5389 }
5390
5391 /* Compare the formal argument lists of both procedures. This is also abused
5392 to find the position of the passed-object dummy arguments of both
5393 bindings as at least the overridden one might not yet be resolved and we
5394 need those positions in the check below. */
5395 proc_pass_arg = old_pass_arg = 0;
5396 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
5397 proc_pass_arg = 1;
5398 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
5399 old_pass_arg = 1;
5400 argpos = 1;
5401 proc_formal = gfc_sym_get_dummy_args (proc_target);
5402 old_formal = gfc_sym_get_dummy_args (old_target);
5403 for ( ; proc_formal && old_formal;
5404 proc_formal = proc_formal->next, old_formal = old_formal->next)
5405 {
5406 if (proc->n.tb->pass_arg
5407 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
5408 proc_pass_arg = argpos;
5409 if (old->n.tb->pass_arg
5410 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
5411 old_pass_arg = argpos;
5412
5413 /* Check that the names correspond. */
5414 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
5415 {
5416 gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
5417 " to match the corresponding argument of the overridden"
5418 " procedure", proc_formal->sym->name, proc->name, &where,
5419 old_formal->sym->name);
5420 return false;
5421 }
5422
5423 check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
5424 if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
5425 check_type, err, sizeof(err)))
5426 {
5427 gfc_error_opt (0, "Argument mismatch for the overriding procedure "
5428 "%qs at %L: %s", proc->name, &where, err);
5429 return false;
5430 }
5431
5432 ++argpos;
5433 }
5434 if (proc_formal || old_formal)
5435 {
5436 gfc_error ("%qs at %L must have the same number of formal arguments as"
5437 " the overridden procedure", proc->name, &where);
5438 return false;
5439 }
5440
5441 /* If the overridden binding is NOPASS, the overriding one must also be
5442 NOPASS. */
5443 if (old->n.tb->nopass && !proc->n.tb->nopass)
5444 {
5445 gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
5446 " NOPASS", proc->name, &where);
5447 return false;
5448 }
5449
5450 /* If the overridden binding is PASS(x), the overriding one must also be
5451 PASS and the passed-object dummy arguments must correspond. */
5452 if (!old->n.tb->nopass)
5453 {
5454 if (proc->n.tb->nopass)
5455 {
5456 gfc_error ("%qs at %L overrides a binding with PASS and must also be"
5457 " PASS", proc->name, &where);
5458 return false;
5459 }
5460
5461 if (proc_pass_arg != old_pass_arg)
5462 {
5463 gfc_error ("Passed-object dummy argument of %qs at %L must be at"
5464 " the same position as the passed-object dummy argument of"
5465 " the overridden procedure", proc->name, &where);
5466 return false;
5467 }
5468 }
5469
5470 return true;
5471}
5472
5473
5474/* The following three functions check that the formal arguments
5475 of user defined derived type IO procedures are compliant with
5476 the requirements of the standard, see F03:9.5.3.7.2 (F08:9.6.4.8.3). */
5477
5478static void
5479check_dtio_arg_TKR_intent (gfc_symbol *fsym, bool typebound, bt type,
5480 int kind, int rank, sym_intent intent)
5481{
5482 if (fsym->ts.type != type)
5483 {
5484 gfc_error ("DTIO dummy argument at %L must be of type %s",
5485 &fsym->declared_at, gfc_basic_typename (type));
5486 return;
5487 }
5488
5489 if (fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED
5490 && fsym->ts.kind != kind)
5491 gfc_error ("DTIO dummy argument at %L must be of KIND = %d",
5492 &fsym->declared_at, kind);
5493
5494 if (!typebound
5495 && rank == 0
5496 && (((type == BT_CLASS) && CLASS_DATA (fsym)->attr.dimension)
5497 || ((type != BT_CLASS) && fsym->attr.dimension)))
5498 gfc_error ("DTIO dummy argument at %L must be a scalar",
5499 &fsym->declared_at);
5500 else if (rank == 1
5501 && (fsym->as == NULL || fsym->as->type != AS_ASSUMED_SHAPE))
5502 gfc_error ("DTIO dummy argument at %L must be an "
5503 "ASSUMED SHAPE ARRAY", &fsym->declared_at);
5504
5505 if (type == BT_CHARACTER && fsym->ts.u.cl->length != NULL)
5506 gfc_error ("DTIO character argument at %L must have assumed length",
5507 &fsym->declared_at);
5508
5509 if (fsym->attr.intent != intent)
5510 gfc_error ("DTIO dummy argument at %L must have INTENT %s",
5511 &fsym->declared_at, gfc_code2string (intents, (int)intent));
5512 return;
5513}
5514
5515
5516static void
5517check_dtio_interface1 (gfc_symbol *derived, gfc_symtree *tb_io_st,
5518 bool typebound, bool formatted, int code)
5519{
5520 gfc_symbol *dtio_sub, *generic_proc, *fsym;
5521 gfc_typebound_proc *tb_io_proc, *specific_proc;
5522 gfc_interface *intr;
5523 gfc_formal_arglist *formal;
5524 int arg_num;
5525
5526 bool read = ((dtio_codes)code == DTIO_RF)
5527 || ((dtio_codes)code == DTIO_RUF);
5528 bt type;
5529 sym_intent intent;
5530 int kind;
5531
5532 dtio_sub = NULL;
5533 if (typebound)
5534 {
5535 /* Typebound DTIO binding. */
5536 tb_io_proc = tb_io_st->n.tb;
5537 if (tb_io_proc == NULL)
5538 return;
5539
5540 gcc_assert (tb_io_proc->is_generic);
5541
5542 specific_proc = tb_io_proc->u.generic->specific;
5543 if (specific_proc == NULL || specific_proc->is_generic)
5544 return;
5545
5546 dtio_sub = specific_proc->u.specific->n.sym;
5547 }
5548 else
5549 {
5550 generic_proc = tb_io_st->n.sym;
5551 if (generic_proc == NULL || generic_proc->generic == NULL)
5552 return;
5553
5554 for (intr = tb_io_st->n.sym->generic; intr; intr = intr->next)
5555 {
5556 if (intr->sym && intr->sym->formal && intr->sym->formal->sym
5557 && ((intr->sym->formal->sym->ts.type == BT_CLASS
5558 && CLASS_DATA (intr->sym->formal->sym)->ts.u.derived
5559 == derived)
5560 || (intr->sym->formal->sym->ts.type == BT_DERIVED
5561 && intr->sym->formal->sym->ts.u.derived == derived)))
5562 {
5563 dtio_sub = intr->sym;
5564 break;
5565 }
5566 else if (intr->sym && intr->sym->formal && !intr->sym->formal->sym)
5567 {
5568 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5569 "procedure", &intr->sym->declared_at);
5570 return;
5571 }
5572 }
5573
5574 if (dtio_sub == NULL)
5575 return;
5576 }
5577
5578 gcc_assert (dtio_sub);
5579 if (!dtio_sub->attr.subroutine)
5580 gfc_error ("DTIO procedure %qs at %L must be a subroutine",
5581 dtio_sub->name, &dtio_sub->declared_at);
5582
5583 if (!dtio_sub->resolve_symbol_called)
5584 gfc_resolve_formal_arglist (dtio_sub);
5585
5586 arg_num = 0;
5587 for (formal = dtio_sub->formal; formal; formal = formal->next)
5588 arg_num++;
5589
5590 if (arg_num < (formatted ? 6 : 4))
5591 {
5592 gfc_error ("Too few dummy arguments in DTIO procedure %qs at %L",
5593 dtio_sub->name, &dtio_sub->declared_at);
5594 return;
5595 }
5596
5597 if (arg_num > (formatted ? 6 : 4))
5598 {
5599 gfc_error ("Too many dummy arguments in DTIO procedure %qs at %L",
5600 dtio_sub->name, &dtio_sub->declared_at);
5601 return;
5602 }
5603
5604 /* Now go through the formal arglist. */
5605 arg_num = 1;
5606 for (formal = dtio_sub->formal; formal; formal = formal->next, arg_num++)
5607 {
5608 if (!formatted && arg_num == 3)
5609 arg_num = 5;
5610 fsym = formal->sym;
5611
5612 if (fsym == NULL)
5613 {
5614 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5615 "procedure", &dtio_sub->declared_at);
5616 return;
5617 }
5618
5619 switch (arg_num)
5620 {
5621 case(1): /* DTV */
5622 type = derived->attr.sequence || derived->attr.is_bind_c ?
5623 BT_DERIVED : BT_CLASS;
5624 kind = 0;
5625 intent = read ? INTENT_INOUT : INTENT_IN;
5626 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5627 0, intent);
5628 break;
5629
5630 case(2): /* UNIT */
5631 type = BT_INTEGER;
5632 kind = gfc_default_integer_kind;
5633 intent = INTENT_IN;
5634 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5635 0, intent);
5636 break;
5637 case(3): /* IOTYPE */
5638 type = BT_CHARACTER;
5639 kind = gfc_default_character_kind;
5640 intent = INTENT_IN;
5641 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5642 0, intent);
5643 break;
5644 case(4): /* VLIST */
5645 type = BT_INTEGER;
5646 kind = gfc_default_integer_kind;
5647 intent = INTENT_IN;
5648 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5649 1, intent);
5650 break;
5651 case(5): /* IOSTAT */
5652 type = BT_INTEGER;
5653 kind = gfc_default_integer_kind;
5654 intent = INTENT_OUT;
5655 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5656 0, intent);
5657 break;
5658 case(6): /* IOMSG */
5659 type = BT_CHARACTER;
5660 kind = gfc_default_character_kind;
5661 intent = INTENT_INOUT;
5662 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5663 0, intent);
5664 break;
5665 default:
5666 gcc_unreachable ();
5667 }
5668 }
5669 derived->attr.has_dtio_procs = 1;
5670 return;
5671}
5672
5673void
5674gfc_check_dtio_interfaces (gfc_symbol *derived)
5675{
5676 gfc_symtree *tb_io_st;
5677 bool t = false;
5678 int code;
5679 bool formatted;
5680
5681 if (derived->attr.is_class == 1 || derived->attr.vtype == 1)
5682 return;
5683
5684 /* Check typebound DTIO bindings. */
5685 for (code = 0; code < 4; code++)
5686 {
5687 formatted = ((dtio_codes)code == DTIO_RF)
5688 || ((dtio_codes)code == DTIO_WF);
5689
5690 tb_io_st = gfc_find_typebound_proc (derived, &t,
5691 gfc_code2string (dtio_procs, code),
5692 true, &derived->declared_at);
5693 if (tb_io_st != NULL)
5694 check_dtio_interface1 (derived, tb_io_st, true, formatted, code);
5695 }
5696
5697 /* Check generic DTIO interfaces. */
5698 for (code = 0; code < 4; code++)
5699 {
5700 formatted = ((dtio_codes)code == DTIO_RF)
5701 || ((dtio_codes)code == DTIO_WF);
5702
5703 tb_io_st = gfc_find_symtree (derived->ns->sym_root,
5704 gfc_code2string (dtio_procs, code));
5705 if (tb_io_st != NULL)
5706 check_dtio_interface1 (derived, tb_io_st, false, formatted, code);
5707 }
5708}
5709
5710
5711gfc_symtree*
5712gfc_find_typebound_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5713{
5714 gfc_symtree *tb_io_st = NULL;
5715 bool t = false;
5716
5717 if (!derived || !derived->resolve_symbol_called
5718 || derived->attr.flavor != FL_DERIVED)
5719 return NULL;
5720
5721 /* Try to find a typebound DTIO binding. */
5722 if (formatted == true)
5723 {
5724 if (write == true)
5725 tb_io_st = gfc_find_typebound_proc (derived, &t,
5726 gfc_code2string (dtio_procs,
5727 DTIO_WF),
5728 true,
5729 &derived->declared_at);
5730 else
5731 tb_io_st = gfc_find_typebound_proc (derived, &t,
5732 gfc_code2string (dtio_procs,
5733 DTIO_RF),
5734 true,
5735 &derived->declared_at);
5736 }
5737 else
5738 {
5739 if (write == true)
5740 tb_io_st = gfc_find_typebound_proc (derived, &t,
5741 gfc_code2string (dtio_procs,
5742 DTIO_WUF),
5743 true,
5744 &derived->declared_at);
5745 else
5746 tb_io_st = gfc_find_typebound_proc (derived, &t,
5747 gfc_code2string (dtio_procs,
5748 DTIO_RUF),
5749 true,
5750 &derived->declared_at);
5751 }
5752 return tb_io_st;
5753}
5754
5755
5756gfc_symbol *
5757gfc_find_specific_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5758{
5759 gfc_symtree *tb_io_st = NULL;
5760 gfc_symbol *dtio_sub = NULL;
5761 gfc_symbol *extended;
5762 gfc_typebound_proc *tb_io_proc, *specific_proc;
5763
5764 tb_io_st = gfc_find_typebound_dtio_proc (derived, write, formatted);
5765
5766 if (tb_io_st != NULL)
5767 {
5768 const char *genname;
5769 gfc_symtree *st;
5770
5771 tb_io_proc = tb_io_st->n.tb;
5772 gcc_assert (tb_io_proc != NULL);
5773 gcc_assert (tb_io_proc->is_generic);
5774 gcc_assert (tb_io_proc->u.generic->next == NULL);
5775
5776 specific_proc = tb_io_proc->u.generic->specific;
5777 gcc_assert (!specific_proc->is_generic);
5778
5779 /* Go back and make sure that we have the right specific procedure.
5780 Here we most likely have a procedure from the parent type, which
5781 can be overridden in extensions. */
5782 genname = tb_io_proc->u.generic->specific_st->name;
5783 st = gfc_find_typebound_proc (derived, NULL, genname,
5784 true, &tb_io_proc->where);
5785 if (st)
5786 dtio_sub = st->n.tb->u.specific->n.sym;
5787 else
5788 dtio_sub = specific_proc->u.specific->n.sym;
5789
5790 goto finish;
5791 }
5792
5793 /* If there is not a typebound binding, look for a generic
5794 DTIO interface. */
5795 for (extended = derived; extended;
5796 extended = gfc_get_derived_super_type (extended))
5797 {
5798 if (extended == NULL || extended->ns == NULL
5799 || extended->attr.flavor == FL_UNKNOWN)
5800 return NULL;
5801
5802 if (formatted == true)
5803 {
5804 if (write == true)
5805 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5806 gfc_code2string (dtio_procs,
5807 DTIO_WF));
5808 else
5809 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5810 gfc_code2string (dtio_procs,
5811 DTIO_RF));
5812 }
5813 else
5814 {
5815 if (write == true)
5816 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5817 gfc_code2string (dtio_procs,
5818 DTIO_WUF));
5819 else
5820 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5821 gfc_code2string (dtio_procs,
5822 DTIO_RUF));
5823 }
5824
5825 if (tb_io_st != NULL
5826 && tb_io_st->n.sym
5827 && tb_io_st->n.sym->generic)
5828 {
5829 for (gfc_interface *intr = tb_io_st->n.sym->generic;
5830 intr && intr->sym; intr = intr->next)
5831 {
5832 if (intr->sym->formal)
5833 {
5834 gfc_symbol *fsym = intr->sym->formal->sym;
5835 if ((fsym->ts.type == BT_CLASS
5836 && CLASS_DATA (fsym)->ts.u.derived == extended)
5837 || (fsym->ts.type == BT_DERIVED
5838 && fsym->ts.u.derived == extended))
5839 {
5840 dtio_sub = intr->sym;
5841 break;
5842 }
5843 }
5844 }
5845 }
5846 }
5847
5848finish:
5849 if (dtio_sub
5850 && dtio_sub->formal->sym->ts.type == BT_CLASS
5851 && derived != CLASS_DATA (dtio_sub->formal->sym)->ts.u.derived)
5852 gfc_find_derived_vtab (derived);
5853
5854 return dtio_sub;
5855}
5856
5857/* Helper function - if we do not find an interface for a procedure,
5858 construct it from the actual arglist. Luckily, this can only
5859 happen for call by reference, so the information we actually need
5860 to provide (and which would be impossible to guess from the call
5861 itself) is not actually needed. */
5862
5863void
5864gfc_get_formal_from_actual_arglist (gfc_symbol *sym,
5865 gfc_actual_arglist *actual_args)
5866{
5867 gfc_actual_arglist *a;
5868 gfc_formal_arglist **f;
5869 gfc_symbol *s;
5870 char name[GFC_MAX_SYMBOL_LEN + 1];
5871 static int var_num;
5872
5873 /* Do not infer the formal from actual arguments if we are dealing with
5874 classes. */
5875
5876 if (sym->ts.type == BT_CLASS)
5877 return;
5878
5879 f = &sym->formal;
5880 for (a = actual_args; a != NULL; a = a->next)
5881 {
5882 (*f) = gfc_get_formal_arglist ();
5883 if (a->expr)
5884 {
5885 snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
5886 gfc_get_symbol (name, gfc_current_ns, &s);
5887 if (a->expr->ts.type == BT_PROCEDURE)
5888 {
5889 gfc_symbol *asym = a->expr->symtree->n.sym;
5890 s->attr.flavor = FL_PROCEDURE;
5891 if (asym->attr.function)
5892 {
5893 s->attr.function = 1;
5894 s->ts = asym->ts;
5895 }
5896 s->attr.subroutine = asym->attr.subroutine;
5897 }
5898 else
5899 {
5900 s->ts = a->expr->ts;
5901
5902 if (s->ts.type == BT_CHARACTER)
5903 s->ts.u.cl = gfc_get_charlen ();
5904
5905 s->ts.deferred = 0;
5906 s->ts.is_iso_c = 0;
5907 s->ts.is_c_interop = 0;
5908 s->attr.flavor = FL_VARIABLE;
5909 if (a->expr->rank > 0)
5910 {
5911 s->attr.dimension = 1;
5912 s->as = gfc_get_array_spec ();
5913 s->as->rank = 1;
5914 s->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5915 &a->expr->where, 1);
5916 s->as->upper[0] = NULL;
5917 s->as->type = AS_ASSUMED_SIZE;
5918 }
5919 else
5920 s->maybe_array = maybe_dummy_array_arg (a->expr);
5921 }
5922 s->attr.dummy = 1;
5923 s->attr.artificial = 1;
5924 s->declared_at = a->expr->where;
5925 s->attr.intent = INTENT_UNKNOWN;
5926 (*f)->sym = s;
5927 gfc_commit_symbol (s);
5928 }
5929 else /* If a->expr is NULL, this is an alternate rerturn. */
5930 (*f)->sym = NULL;
5931
5932 f = &((*f)->next);
5933 }
5934
5935}
5936
5937
5938const char *
5939gfc_dummy_arg_get_name (gfc_dummy_arg & dummy_arg)
5940{
5941 switch (dummy_arg.intrinsicness)
5942 {
5943 case GFC_INTRINSIC_DUMMY_ARG:
5944 return dummy_arg.u.intrinsic->name;
5945
5946 case GFC_NON_INTRINSIC_DUMMY_ARG:
5947 return dummy_arg.u.non_intrinsic->sym->name;
5948
5949 default:
5950 gcc_unreachable ();
5951 }
5952}
5953
5954
5955const gfc_typespec &
5956gfc_dummy_arg_get_typespec (gfc_dummy_arg & dummy_arg)
5957{
5958 switch (dummy_arg.intrinsicness)
5959 {
5960 case GFC_INTRINSIC_DUMMY_ARG:
5961 return dummy_arg.u.intrinsic->ts;
5962
5963 case GFC_NON_INTRINSIC_DUMMY_ARG:
5964 return dummy_arg.u.non_intrinsic->sym->ts;
5965
5966 default:
5967 gcc_unreachable ();
5968 }
5969}
5970
5971
5972bool
5973gfc_dummy_arg_is_optional (gfc_dummy_arg & dummy_arg)
5974{
5975 switch (dummy_arg.intrinsicness)
5976 {
5977 case GFC_INTRINSIC_DUMMY_ARG:
5978 return dummy_arg.u.intrinsic->optional;
5979
5980 case GFC_NON_INTRINSIC_DUMMY_ARG:
5981 return dummy_arg.u.non_intrinsic->sym->attr.optional;
5982
5983 default:
5984 gcc_unreachable ();
5985 }
5986}