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