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