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