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