]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/interface.cc
c++: ICE with constexpr lambda [PR107280]
[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;
6de9cd9a 2352
a8b3b0b6
CR
2353 /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
2354 procs c_f_pointer or c_f_procpointer, and we need to accept most
2355 pointers the user could give us. This should allow that. */
2356 if (formal->ts.type == BT_VOID)
f3e1097b 2357 return true;
a8b3b0b6
CR
2358
2359 if (formal->ts.type == BT_DERIVED
bc21d315 2360 && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
a8b3b0b6 2361 && actual->ts.type == BT_DERIVED
bc21d315 2362 && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
f3e1097b 2363 return true;
a8b3b0b6 2364
7d58b9e7 2365 if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
e10f52d0
JW
2366 /* Make sure the vtab symbol is present when
2367 the module variables are generated. */
7d58b9e7 2368 gfc_find_derived_vtab (actual->ts.u.derived);
e10f52d0 2369
6de9cd9a
DN
2370 if (actual->ts.type == BT_PROCEDURE)
2371 {
9b63f282 2372 gfc_symbol *act_sym = actual->symtree->n.sym;
6de9cd9a 2373
8ad15a0a
JW
2374 if (formal->attr.flavor != FL_PROCEDURE)
2375 {
2376 if (where)
2377 gfc_error ("Invalid procedure argument at %L", &actual->where);
f3e1097b 2378 return false;
8ad15a0a 2379 }
6de9cd9a 2380
889dc035 2381 if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
6f3ab30d 2382 sizeof(err), NULL, NULL))
8ad15a0a
JW
2383 {
2384 if (where)
e0b9e5f9 2385 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2700d0e3 2386 " %s", formal->name, &actual->where, err);
f3e1097b 2387 return false;
8ad15a0a 2388 }
5ad6345e 2389
9b63f282 2390 if (formal->attr.function && !act_sym->attr.function)
03bd096b
JW
2391 {
2392 gfc_add_function (&act_sym->attr, act_sym->name,
2393 &act_sym->declared_at);
2394 if (act_sym->ts.type == BT_UNKNOWN
524af0d6 2395 && !gfc_set_default_type (act_sym, 1, act_sym->ns))
f3e1097b 2396 return false;
03bd096b
JW
2397 }
2398 else if (formal->attr.subroutine && !act_sym->attr.subroutine)
9b63f282
JW
2399 gfc_add_subroutine (&act_sym->attr, act_sym->name,
2400 &act_sym->declared_at);
2401
f3e1097b 2402 return true;
6de9cd9a
DN
2403 }
2404
5c0ba546 2405 ppc = gfc_get_proc_ptr_comp (actual);
228eb42a 2406 if (ppc && ppc->ts.interface)
5c0ba546
JW
2407 {
2408 if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
2409 err, sizeof(err), NULL, NULL))
2410 {
2411 if (where)
e0b9e5f9 2412 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2700d0e3 2413 " %s", formal->name, &actual->where, err);
f3e1097b 2414 return false;
5c0ba546
JW
2415 }
2416 }
2417
fe4e525c
TB
2418 /* F2008, C1241. */
2419 if (formal->attr.pointer && formal->attr.contiguous
460263d0 2420 && !gfc_is_simply_contiguous (actual, true, false))
fe4e525c
TB
2421 {
2422 if (where)
c4100eae 2423 gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
62732c30 2424 "must be simply contiguous", formal->name, &actual->where);
f3e1097b 2425 return false;
fe4e525c
TB
2426 }
2427
fec5ce24
JW
2428 symbol_attribute actual_attr = gfc_expr_attr (actual);
2429 if (actual->ts.type == BT_CLASS && !actual_attr.class_ok)
f3e1097b 2430 return true;
fec5ce24 2431
90aeadcb 2432 if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
df161b69 2433 && actual->ts.type != BT_HOLLERITH
45a69325 2434 && formal->ts.type != BT_ASSUMED
e7ac6a7c 2435 && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
c49ea23d
PT
2436 && !gfc_compare_types (&formal->ts, &actual->ts)
2437 && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
8b704316 2438 && gfc_compare_derived_types (formal->ts.u.derived,
c49ea23d 2439 CLASS_DATA (actual)->ts.u.derived)))
5ad6345e 2440 {
d68e117b 2441 if (where)
e0b9e5f9
TK
2442 {
2443 if (formal->attr.artificial)
2444 {
2445 if (!flag_allow_argument_mismatch || !formal->error)
2446 gfc_error_opt (0, "Type mismatch between actual argument at %L "
2447 "and actual argument at %L (%s/%s).",
2448 &actual->where,
2449 &formal->declared_at,
f61e54e5
ME
2450 gfc_typename (actual),
2451 gfc_dummy_typename (&formal->ts));
e0b9e5f9
TK
2452
2453 formal->error = 1;
2454 }
2455 else
2456 gfc_error_opt (0, "Type mismatch in argument %qs at %L; passed %s "
f61e54e5
ME
2457 "to %s", formal->name, where, gfc_typename (actual),
2458 gfc_dummy_typename (&formal->ts));
e0b9e5f9 2459 }
f3e1097b 2460 return false;
5ad6345e 2461 }
f18075ff 2462
3d54e576
TB
2463 if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
2464 {
2465 if (where)
2466 gfc_error ("Assumed-type actual argument at %L requires that dummy "
c4100eae 2467 "argument %qs is of assumed type", &actual->where,
3d54e576 2468 formal->name);
f3e1097b 2469 return false;
3d54e576
TB
2470 }
2471
2364250e
SL
2472 /* TS29113 C407c; F2018 C711. */
2473 if (actual->ts.type == BT_ASSUMED
2474 && symbol_rank (formal) == -1
2475 && actual->rank != -1
2476 && !(actual->symtree->n.sym->as
2477 && actual->symtree->n.sym->as->type == AS_ASSUMED_SHAPE))
2478 {
2479 if (where)
2480 gfc_error ("Assumed-type actual argument at %L corresponding to "
2481 "assumed-rank dummy argument %qs must be "
2482 "assumed-shape or assumed-rank",
2483 &actual->where, formal->name);
2484 return false;
2485 }
2486
f18075ff 2487 /* F2008, 12.5.2.5; IR F08/0073. */
67b1d004
JW
2488 if (formal->ts.type == BT_CLASS && formal->attr.class_ok
2489 && actual->expr_type != EXPR_NULL
f18075ff 2490 && ((CLASS_DATA (formal)->attr.class_pointer
86eb9e2f 2491 && formal->attr.intent != INTENT_IN)
5ac13b8e
JW
2492 || CLASS_DATA (formal)->attr.allocatable))
2493 {
2494 if (actual->ts.type != BT_CLASS)
2495 {
2496 if (where)
c4100eae 2497 gfc_error ("Actual argument to %qs at %L must be polymorphic",
5ac13b8e 2498 formal->name, &actual->where);
f3e1097b 2499 return false;
5ac13b8e 2500 }
67b1d004 2501
a8267f8d
TB
2502 if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
2503 && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
2504 CLASS_DATA (formal)->ts.u.derived))
5ac13b8e
JW
2505 {
2506 if (where)
c4100eae 2507 gfc_error ("Actual argument to %qs at %L must have the same "
5ac13b8e 2508 "declared type", formal->name, &actual->where);
f3e1097b 2509 return false;
5ac13b8e
JW
2510 }
2511 }
6de9cd9a 2512
8b704316
PT
2513 /* F08: 12.5.2.5 Allocatable and pointer dummy variables. However, this
2514 is necessary also for F03, so retain error for both.
2515 NOTE: Other type/kind errors pre-empt this error. Since they are F03
2516 compatible, no attempt has been made to channel to this one. */
2517 if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
2518 && (CLASS_DATA (formal)->attr.allocatable
2519 ||CLASS_DATA (formal)->attr.class_pointer))
2520 {
2521 if (where)
c4100eae 2522 gfc_error ("Actual argument to %qs at %L must be unlimited "
8b704316
PT
2523 "polymorphic since the formal argument is a "
2524 "pointer or allocatable unlimited polymorphic "
2525 "entity [F2008: 12.5.2.5]", formal->name,
2526 &actual->where);
f3e1097b 2527 return false;
8b704316
PT
2528 }
2529
fc27115d
PT
2530 if (formal->ts.type == BT_CLASS && formal->attr.class_ok)
2531 codimension = CLASS_DATA (formal)->attr.codimension;
2532 else
2533 codimension = formal->attr.codimension;
2534
2535 if (codimension && !gfc_is_coarray (actual))
d3a9eea2 2536 {
394d3a2e 2537 if (where)
c4100eae 2538 gfc_error ("Actual argument to %qs at %L must be a coarray",
d3a9eea2 2539 formal->name, &actual->where);
f3e1097b 2540 return false;
394d3a2e 2541 }
d3a9eea2 2542
fc27115d 2543 if (codimension && formal->attr.allocatable)
394d3a2e
TB
2544 {
2545 gfc_ref *last = NULL;
a3935ffc 2546
d3a9eea2 2547 for (ref = actual->ref; ref; ref = ref->next)
394d3a2e
TB
2548 if (ref->type == REF_COMPONENT)
2549 last = ref;
d3a9eea2 2550
d3a9eea2 2551 /* F2008, 12.5.2.6. */
394d3a2e
TB
2552 if ((last && last->u.c.component->as->corank != formal->as->corank)
2553 || (!last
2554 && actual->symtree->n.sym->as->corank != formal->as->corank))
d3a9eea2
TB
2555 {
2556 if (where)
c4100eae 2557 gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
d3a9eea2
TB
2558 formal->name, &actual->where, formal->as->corank,
2559 last ? last->u.c.component->as->corank
2560 : actual->symtree->n.sym->as->corank);
f3e1097b 2561 return false;
d3a9eea2 2562 }
394d3a2e 2563 }
fe4e525c 2564
fc27115d 2565 if (codimension)
394d3a2e 2566 {
460263d0 2567 /* F2008, 12.5.2.8 + Corrig 2 (IR F08/0048). */
8179b067 2568 /* F2018, 12.5.2.8. */
fe4e525c
TB
2569 if (formal->attr.dimension
2570 && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
fec5ce24 2571 && actual_attr.dimension
460263d0 2572 && !gfc_is_simply_contiguous (actual, true, true))
fe4e525c
TB
2573 {
2574 if (where)
c4100eae 2575 gfc_error ("Actual argument to %qs at %L must be simply "
460263d0
TB
2576 "contiguous or an element of such an array",
2577 formal->name, &actual->where);
f3e1097b 2578 return false;
fe4e525c 2579 }
fea54935
TB
2580
2581 /* F2008, C1303 and C1304. */
2582 if (formal->attr.intent != INTENT_INOUT
2583 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2584 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2585 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2586 || formal->attr.lock_comp))
2587
2588 {
2589 if (where)
c4100eae 2590 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
fea54935
TB
2591 "which is LOCK_TYPE or has a LOCK_TYPE component",
2592 formal->name, &actual->where);
f3e1097b 2593 return false;
fea54935 2594 }
5df445a2
TB
2595
2596 /* TS18508, C702/C703. */
2597 if (formal->attr.intent != INTENT_INOUT
2598 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2599 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2600 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2601 || formal->attr.event_comp))
2602
2603 {
2604 if (where)
2605 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2606 "which is EVENT_TYPE or has a EVENT_TYPE component",
2607 formal->name, &actual->where);
f3e1097b 2608 return false;
5df445a2 2609 }
394d3a2e 2610 }
fe4e525c
TB
2611
2612 /* F2008, C1239/C1240. */
2613 if (actual->expr_type == EXPR_VARIABLE
2614 && (actual->symtree->n.sym->attr.asynchronous
2615 || actual->symtree->n.sym->attr.volatile_)
2616 && (formal->attr.asynchronous || formal->attr.volatile_)
460263d0
TB
2617 && actual->rank && formal->as
2618 && !gfc_is_simply_contiguous (actual, true, false)
f188272d
TB
2619 && ((formal->as->type != AS_ASSUMED_SHAPE
2620 && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
fe4e525c
TB
2621 || formal->attr.contiguous))
2622 {
2623 if (where)
c4100eae 2624 gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
f188272d
TB
2625 "assumed-rank array without CONTIGUOUS attribute - as actual"
2626 " argument at %L is not simply contiguous and both are "
2627 "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
f3e1097b 2628 return false;
d3a9eea2
TB
2629 }
2630
fc27115d 2631 if (formal->attr.allocatable && !codimension
fec5ce24 2632 && actual_attr.codimension)
427180d2
TB
2633 {
2634 if (formal->attr.intent == INTENT_OUT)
2635 {
2636 if (where)
2637 gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
c4100eae 2638 "INTENT(OUT) dummy argument %qs", &actual->where,
427180d2 2639 formal->name);
f3e1097b 2640 return false;
427180d2 2641 }
73e42eef 2642 else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
48749dbc
MLI
2643 gfc_warning (OPT_Wsurprising,
2644 "Passing coarray at %L to allocatable, noncoarray dummy "
2645 "argument %qs, which is invalid if the allocation status"
427180d2
TB
2646 " is modified", &actual->where, formal->name);
2647 }
2648
c62c6622
TB
2649 /* If the rank is the same or the formal argument has assumed-rank. */
2650 if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
f3e1097b 2651 return true;
6de9cd9a 2652
5ad6345e
TB
2653 rank_check = where != NULL && !is_elemental && formal->as
2654 && (formal->as->type == AS_ASSUMED_SHAPE
d8a8dab3
TB
2655 || formal->as->type == AS_DEFERRED)
2656 && actual->expr_type != EXPR_NULL;
6de9cd9a 2657
e7ac6a7c
TB
2658 /* Skip rank checks for NO_ARG_CHECK. */
2659 if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
f3e1097b 2660 return true;
e7ac6a7c 2661
d3a9eea2 2662 /* Scalar & coindexed, see: F2008, Section 12.5.2.4. */
d8a8dab3
TB
2663 if (rank_check || ranks_must_agree
2664 || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
5ad6345e 2665 || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
c49ea23d
PT
2666 || (actual->rank == 0
2667 && ((formal->ts.type == BT_CLASS
2668 && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
2669 || (formal->ts.type != BT_CLASS
2670 && formal->as->type == AS_ASSUMED_SHAPE))
08857b61 2671 && actual->expr_type != EXPR_NULL)
d3a9eea2 2672 || (actual->rank == 0 && formal->attr.dimension
7a40f2e7
SL
2673 && gfc_is_coindexed (actual))
2674 /* Assumed-rank actual argument; F2018 C838. */
2675 || actual->rank == -1)
5ad6345e 2676 {
a5baf71b 2677 if (where
4a4fc7fe
TK
2678 && (!formal->attr.artificial || (!formal->maybe_array
2679 && !maybe_dummy_array_arg (actual))))
e0b9e5f9
TK
2680 {
2681 locus *where_formal;
2682 if (formal->attr.artificial)
2683 where_formal = &formal->declared_at;
2684 else
2685 where_formal = NULL;
2686
2687 argument_rank_mismatch (formal->name, &actual->where,
2688 symbol_rank (formal), actual->rank,
2689 where_formal);
2690 }
f3e1097b 2691 return false;
5ad6345e
TB
2692 }
2693 else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
f3e1097b 2694 return true;
5ad6345e
TB
2695
2696 /* At this point, we are considering a scalar passed to an array. This
975b975b 2697 is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
5ad6345e 2698 - if the actual argument is (a substring of) an element of a
975b975b
TB
2699 non-assumed-shape/non-pointer/non-polymorphic array; or
2700 - (F2003) if the actual argument is of type character of default/c_char
59f6dea9
TB
2701 kind.
2702 - (F2018) if the dummy argument is type(*). */
975b975b
TB
2703
2704 is_pointer = actual->expr_type == EXPR_VARIABLE
2705 ? actual->symtree->n.sym->attr.pointer : false;
6de9cd9a
DN
2706
2707 for (ref = actual->ref; ref; ref = ref->next)
975b975b
TB
2708 {
2709 if (ref->type == REF_COMPONENT)
2710 is_pointer = ref->u.c.component->attr.pointer;
2711 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2712 && ref->u.ar.dimen > 0
8b704316 2713 && (!ref->next
975b975b
TB
2714 || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
2715 break;
2716 }
2717
2718 if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
2719 {
2720 if (where)
c4100eae 2721 gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
975b975b 2722 "at %L", formal->name, &actual->where);
f3e1097b 2723 return false;
975b975b
TB
2724 }
2725
2726 if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
2727 && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2728 {
2729 if (where)
4a4fc7fe
TK
2730 {
2731 if (formal->attr.artificial)
2732 gfc_error ("Element of assumed-shape or pointer array "
7260547d
TK
2733 "as actual argument at %L cannot correspond to "
2734 "actual argument at %L",
4a4fc7fe
TK
2735 &actual->where, &formal->declared_at);
2736 else
2737 gfc_error ("Element of assumed-shape or pointer "
2738 "array passed to array dummy argument %qs at %L",
2739 formal->name, &actual->where);
2740 }
f3e1097b 2741 return false;
975b975b 2742 }
6de9cd9a 2743
975b975b
TB
2744 if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
2745 && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
5ad6345e 2746 {
975b975b
TB
2747 if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
2748 {
2749 if (where)
2750 gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
2751 "CHARACTER actual argument with array dummy argument "
c4100eae 2752 "%qs at %L", formal->name, &actual->where);
f3e1097b 2753 return false;
975b975b
TB
2754 }
2755
5ad6345e
TB
2756 if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
2757 {
2758 gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
c4100eae 2759 "array dummy argument %qs at %L",
5ad6345e 2760 formal->name, &actual->where);
f3e1097b 2761 return false;
5ad6345e 2762 }
5ad6345e 2763 else
f3e1097b 2764 return ((gfc_option.allow_std & GFC_STD_F2003) != 0);
5ad6345e 2765 }
975b975b
TB
2766
2767 if (ref == NULL && actual->expr_type != EXPR_NULL)
5ad6345e 2768 {
59f6dea9
TB
2769 if (actual->rank == 0
2770 && formal->ts.type == BT_ASSUMED
2771 && formal->as
2772 && formal->as->type == AS_ASSUMED_SIZE)
2773 /* This is new in F2018, type(*) is new in TS29113, but gfortran does
2774 not differentiate. Thus, if type(*) exists, it is valid;
2775 otherwise, type(*) is already rejected. */
2776 return true;
a5baf71b 2777 if (where
4a4fc7fe
TK
2778 && (!formal->attr.artificial || (!formal->maybe_array
2779 && !maybe_dummy_array_arg (actual))))
e0b9e5f9
TK
2780 {
2781 locus *where_formal;
2782 if (formal->attr.artificial)
2783 where_formal = &formal->declared_at;
2784 else
2785 where_formal = NULL;
2786
2787 argument_rank_mismatch (formal->name, &actual->where,
2788 symbol_rank (formal), actual->rank,
2789 where_formal);
2790 }
f3e1097b 2791 return false;
5ad6345e
TB
2792 }
2793
f3e1097b 2794 return true;
6de9cd9a
DN
2795}
2796
2797
2d5b90b2
TB
2798/* Returns the storage size of a symbol (formal argument) or
2799 zero if it cannot be determined. */
2800
2801static unsigned long
2802get_sym_storage_size (gfc_symbol *sym)
2803{
2804 int i;
2805 unsigned long strlen, elements;
2806
2807 if (sym->ts.type == BT_CHARACTER)
2808 {
bc21d315 2809 if (sym->ts.u.cl && sym->ts.u.cl->length
600956c8
HA
2810 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2811 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
bc21d315 2812 strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
2d5b90b2
TB
2813 else
2814 return 0;
2815 }
2816 else
8b704316 2817 strlen = 1;
2d5b90b2
TB
2818
2819 if (symbol_rank (sym) == 0)
2820 return strlen;
2821
2822 elements = 1;
2823 if (sym->as->type != AS_EXPLICIT)
2824 return 0;
2825 for (i = 0; i < sym->as->rank; i++)
2826 {
efb63364 2827 if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
600956c8
HA
2828 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
2829 || sym->as->upper[i]->ts.type != BT_INTEGER
2830 || sym->as->lower[i]->ts.type != BT_INTEGER)
2d5b90b2
TB
2831 return 0;
2832
c13af44b
SK
2833 elements *= mpz_get_si (sym->as->upper[i]->value.integer)
2834 - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
2d5b90b2
TB
2835 }
2836
2837 return strlen*elements;
2838}
2839
2840
2841/* Returns the storage size of an expression (actual argument) or
2842 zero if it cannot be determined. For an array element, it returns
1207ac67 2843 the remaining size as the element sequence consists of all storage
2d5b90b2
TB
2844 units of the actual argument up to the end of the array. */
2845
2846static unsigned long
2847get_expr_storage_size (gfc_expr *e)
2848{
2849 int i;
2850 long int strlen, elements;
6da0839a 2851 long int substrlen = 0;
a0710c29 2852 bool is_str_storage = false;
2d5b90b2
TB
2853 gfc_ref *ref;
2854
2855 if (e == NULL)
2856 return 0;
8b704316 2857
2d5b90b2
TB
2858 if (e->ts.type == BT_CHARACTER)
2859 {
bc21d315 2860 if (e->ts.u.cl && e->ts.u.cl->length
a7576037
HA
2861 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
2862 && e->ts.u.cl->length->ts.type == BT_INTEGER)
bc21d315 2863 strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
2d5b90b2 2864 else if (e->expr_type == EXPR_CONSTANT
bc21d315 2865 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
2d5b90b2
TB
2866 strlen = e->value.character.length;
2867 else
2868 return 0;
2869 }
2870 else
2871 strlen = 1; /* Length per element. */
2872
2873 if (e->rank == 0 && !e->ref)
2874 return strlen;
2875
2876 elements = 1;
2877 if (!e->ref)
2878 {
2879 if (!e->shape)
2880 return 0;
2881 for (i = 0; i < e->rank; i++)
2882 elements *= mpz_get_si (e->shape[i]);
2883 return elements*strlen;
2884 }
2885
2886 for (ref = e->ref; ref; ref = ref->next)
2887 {
6da0839a
TB
2888 if (ref->type == REF_SUBSTRING && ref->u.ss.start
2889 && ref->u.ss.start->expr_type == EXPR_CONSTANT)
2890 {
a0710c29
TB
2891 if (is_str_storage)
2892 {
2893 /* The string length is the substring length.
2894 Set now to full string length. */
e323640f 2895 if (!ref->u.ss.length || !ref->u.ss.length->length
a0710c29
TB
2896 || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
2897 return 0;
2898
2899 strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
2900 }
2901 substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
6da0839a
TB
2902 continue;
2903 }
2904
efb63364 2905 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2d5b90b2
TB
2906 for (i = 0; i < ref->u.ar.dimen; i++)
2907 {
2908 long int start, end, stride;
2909 stride = 1;
37639728 2910
2d5b90b2
TB
2911 if (ref->u.ar.stride[i])
2912 {
771d793d
HA
2913 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT
2914 && ref->u.ar.stride[i]->ts.type == BT_INTEGER)
2d5b90b2
TB
2915 stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
2916 else
2917 return 0;
2918 }
2919
2920 if (ref->u.ar.start[i])
2921 {
771d793d
HA
2922 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT
2923 && ref->u.ar.start[i]->ts.type == BT_INTEGER)
2d5b90b2
TB
2924 start = mpz_get_si (ref->u.ar.start[i]->value.integer);
2925 else
2926 return 0;
2927 }
37639728 2928 else if (ref->u.ar.as->lower[i]
771d793d
HA
2929 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
2930 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER)
37639728
TB
2931 start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
2932 else
2933 return 0;
2d5b90b2
TB
2934
2935 if (ref->u.ar.end[i])
2936 {
771d793d
HA
2937 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT
2938 && ref->u.ar.end[i]->ts.type == BT_INTEGER)
2d5b90b2
TB
2939 end = mpz_get_si (ref->u.ar.end[i]->value.integer);
2940 else
2941 return 0;
2942 }
2943 else if (ref->u.ar.as->upper[i]
771d793d
HA
2944 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
2945 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
2d5b90b2
TB
2946 end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
2947 else
2948 return 0;
2949
2950 elements *= (end - start)/stride + 1L;
2951 }
c6423ef3 2952 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
2d5b90b2
TB
2953 for (i = 0; i < ref->u.ar.as->rank; i++)
2954 {
2955 if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
2956 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
edcc76d5
SK
2957 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER
2958 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
2959 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
da9ad923
TB
2960 elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2961 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2d5b90b2
TB
2962 + 1L;
2963 else
2964 return 0;
2965 }
6da0839a 2966 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
a0710c29
TB
2967 && e->expr_type == EXPR_VARIABLE)
2968 {
93302a24 2969 if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
a0710c29
TB
2970 || e->symtree->n.sym->attr.pointer)
2971 {
2972 elements = 1;
2973 continue;
2974 }
2975
2976 /* Determine the number of remaining elements in the element
2977 sequence for array element designators. */
2978 is_str_storage = true;
2979 for (i = ref->u.ar.dimen - 1; i >= 0; i--)
2980 {
2981 if (ref->u.ar.start[i] == NULL
2982 || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
2983 || ref->u.ar.as->upper[i] == NULL
2984 || ref->u.ar.as->lower[i] == NULL
2985 || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
771d793d
HA
2986 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT
2987 || ref->u.ar.as->upper[i]->ts.type != BT_INTEGER
2988 || ref->u.ar.as->lower[i]->ts.type != BT_INTEGER)
a0710c29
TB
2989 return 0;
2990
2991 elements
2992 = elements
2993 * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2994 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2995 + 1L)
2996 - (mpz_get_si (ref->u.ar.start[i]->value.integer)
2997 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
2998 }
2999 }
3436db75
JW
3000 else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
3001 && ref->u.c.component->attr.proc_pointer
3002 && ref->u.c.component->attr.dimension)
3003 {
3004 /* Array-valued procedure-pointer components. */
3005 gfc_array_spec *as = ref->u.c.component->as;
3006 for (i = 0; i < as->rank; i++)
3007 {
3008 if (!as->upper[i] || !as->lower[i]
3009 || as->upper[i]->expr_type != EXPR_CONSTANT
771d793d
HA
3010 || as->lower[i]->expr_type != EXPR_CONSTANT
3011 || as->upper[i]->ts.type != BT_INTEGER
3012 || as->lower[i]->ts.type != BT_INTEGER)
3436db75
JW
3013 return 0;
3014
3015 elements = elements
3016 * (mpz_get_si (as->upper[i]->value.integer)
3017 - mpz_get_si (as->lower[i]->value.integer) + 1L);
3018 }
3019 }
2d5b90b2
TB
3020 }
3021
6da0839a 3022 if (substrlen)
a0710c29
TB
3023 return (is_str_storage) ? substrlen + (elements-1)*strlen
3024 : elements*strlen;
3025 else
3026 return elements*strlen;
2d5b90b2
TB
3027}
3028
3029
59be8071 3030/* Given an expression, check whether it is an array section
f3e1097b 3031 which has a vector subscript. */
59be8071 3032
f3e1097b 3033bool
03af1e4c 3034gfc_has_vector_subscript (gfc_expr *e)
59be8071
TB
3035{
3036 int i;
3037 gfc_ref *ref;
3038
3039 if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
f3e1097b 3040 return false;
59be8071
TB
3041
3042 for (ref = e->ref; ref; ref = ref->next)
3043 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3044 for (i = 0; i < ref->u.ar.dimen; i++)
3045 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
f3e1097b 3046 return true;
59be8071 3047
f3e1097b 3048 return false;
59be8071
TB
3049}
3050
3051
4294c093
JW
3052static bool
3053is_procptr_result (gfc_expr *expr)
3054{
3055 gfc_component *c = gfc_get_proc_ptr_comp (expr);
3056 if (c)
3057 return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
3058 else
3059 return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
3060 && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
3061}
3062
3063
bcc478b9
BRF
3064/* Recursively append candidate argument ARG to CANDIDATES. Store the
3065 number of total candidates in CANDIDATES_LEN. */
3066
3067static void
3068lookup_arg_fuzzy_find_candidates (gfc_formal_arglist *arg,
3069 char **&candidates,
3070 size_t &candidates_len)
3071{
3072 for (gfc_formal_arglist *p = arg; p && p->sym; p = p->next)
3073 vec_push (candidates, candidates_len, p->sym->name);
3074}
3075
3076
3077/* Lookup argument ARG fuzzily, taking names in ARGUMENTS into account. */
3078
3079static const char*
3080lookup_arg_fuzzy (const char *arg, gfc_formal_arglist *arguments)
3081{
3082 char **candidates = NULL;
3083 size_t candidates_len = 0;
3084 lookup_arg_fuzzy_find_candidates (arguments, candidates, candidates_len);
3085 return gfc_closest_fuzzy_match (arg, candidates);
3086}
3087
3088
5888512f
MM
3089static gfc_dummy_arg *
3090get_nonintrinsic_dummy_arg (gfc_formal_arglist *formal)
3091{
3092 gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
3093
3094 dummy_arg->intrinsicness = GFC_NON_INTRINSIC_DUMMY_ARG;
3095 dummy_arg->u.non_intrinsic = formal;
3096
3097 return dummy_arg;
3098}
3099
3100
6de9cd9a
DN
3101/* Given formal and actual argument lists, see if they are compatible.
3102 If they are compatible, the actual argument list is sorted to
3103 correspond with the formal list, and elements for missing optional
3104 arguments are inserted. If WHERE pointer is nonnull, then we issue
3105 errors when things don't match instead of just returning the status
3106 code. */
3107
e68a35ae
TK
3108bool
3109gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
3110 int ranks_must_agree, int is_elemental,
3111 bool in_statement_function, locus *where)
6de9cd9a 3112{
fab27f52 3113 gfc_actual_arglist **new_arg, *a, *actual;
6de9cd9a
DN
3114 gfc_formal_arglist *f;
3115 int i, n, na;
2d5b90b2 3116 unsigned long actual_size, formal_size;
c49ea23d 3117 bool full_array = false;
eb401400 3118 gfc_array_ref *actual_arr_ref;
7afb6108
SL
3119 gfc_array_spec *fas, *aas;
3120 bool pointer_dummy, pointer_arg, allocatable_arg;
6de9cd9a 3121
ee11be7f
SL
3122 bool ok = true;
3123
6de9cd9a
DN
3124 actual = *ap;
3125
3126 if (actual == NULL && formal == NULL)
f3e1097b 3127 return true;
6de9cd9a
DN
3128
3129 n = 0;
3130 for (f = formal; f; f = f->next)
3131 n++;
3132
1145e690 3133 new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
6de9cd9a
DN
3134
3135 for (i = 0; i < n; i++)
7b901ac4 3136 new_arg[i] = NULL;
6de9cd9a
DN
3137
3138 na = 0;
3139 f = formal;
3140 i = 0;
3141
3142 for (a = actual; a; a = a->next, f = f->next)
3143 {
3453b6aa
SK
3144 if (a->name != NULL && in_statement_function)
3145 {
3146 gfc_error ("Keyword argument %qs at %L is invalid in "
3147 "a statement function", a->name, &a->expr->where);
3148 return false;
3149 }
3150
7fcafa71
PT
3151 /* Look for keywords but ignore g77 extensions like %VAL. */
3152 if (a->name != NULL && a->name[0] != '%')
6de9cd9a
DN
3153 {
3154 i = 0;
3155 for (f = formal; f; f = f->next, i++)
3156 {
3157 if (f->sym == NULL)
3158 continue;
3159 if (strcmp (f->sym->name, a->name) == 0)
3160 break;
3161 }
3162
3163 if (f == NULL)
3164 {
3165 if (where)
bcc478b9
BRF
3166 {
3167 const char *guessed = lookup_arg_fuzzy (a->name, formal);
3168 if (guessed)
3169 gfc_error ("Keyword argument %qs at %L is not in "
3170 "the procedure; did you mean %qs?",
3171 a->name, &a->expr->where, guessed);
3172 else
3173 gfc_error ("Keyword argument %qs at %L is not in "
3174 "the procedure", a->name, &a->expr->where);
3175 }
f3e1097b 3176 return false;
6de9cd9a
DN
3177 }
3178
7b901ac4 3179 if (new_arg[i] != NULL)
6de9cd9a
DN
3180 {
3181 if (where)
c4100eae 3182 gfc_error ("Keyword argument %qs at %L is already associated "
b251af97
SK
3183 "with another actual argument", a->name,
3184 &a->expr->where);
f3e1097b 3185 return false;
6de9cd9a
DN
3186 }
3187 }
3188
3189 if (f == NULL)
3190 {
3191 if (where)
b251af97
SK
3192 gfc_error ("More actual than formal arguments in procedure "
3193 "call at %L", where);
f3e1097b 3194 return false;
6de9cd9a
DN
3195 }
3196
3197 if (f->sym == NULL && a->expr == NULL)
3198 goto match;
3199
3200 if (f->sym == NULL)
3201 {
866664a3
TK
3202 /* These errors have to be issued, otherwise an ICE can occur.
3203 See PR 78865. */
6de9cd9a 3204 if (where)
866664a3
TK
3205 gfc_error_now ("Missing alternate return specifier in subroutine "
3206 "call at %L", where);
f3e1097b 3207 return false;
6de9cd9a 3208 }
5888512f
MM
3209 else
3210 a->associated_dummy = get_nonintrinsic_dummy_arg (f);
6de9cd9a
DN
3211
3212 if (a->expr == NULL)
3213 {
fb078366
TK
3214 if (f->sym->attr.optional)
3215 continue;
3216 else
3217 {
3218 if (where)
3219 gfc_error_now ("Unexpected alternate return specifier in "
3220 "subroutine call at %L", where);
3221 return false;
3222 }
6de9cd9a 3223 }
08857b61 3224
8b704316
PT
3225 /* Make sure that intrinsic vtables exist for calls to unlimited
3226 polymorphic formal arguments. */
524af0d6 3227 if (UNLIMITED_POLY (f->sym)
8b704316 3228 && a->expr->ts.type != BT_DERIVED
15e5858f
TK
3229 && a->expr->ts.type != BT_CLASS
3230 && a->expr->ts.type != BT_ASSUMED)
7289d1c9 3231 gfc_find_vtab (&a->expr->ts);
8b704316 3232
99091b70
TB
3233 if (a->expr->expr_type == EXPR_NULL
3234 && ((f->sym->ts.type != BT_CLASS && !f->sym->attr.pointer
3235 && (f->sym->attr.allocatable || !f->sym->attr.optional
3236 || (gfc_option.allow_std & GFC_STD_F2008) == 0))
3237 || (f->sym->ts.type == BT_CLASS
3238 && !CLASS_DATA (f->sym)->attr.class_pointer
3239 && (CLASS_DATA (f->sym)->attr.allocatable
3240 || !f->sym->attr.optional
3241 || (gfc_option.allow_std & GFC_STD_F2008) == 0))))
08857b61 3242 {
99091b70
TB
3243 if (where
3244 && (!f->sym->attr.optional
3245 || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
3246 || (f->sym->ts.type == BT_CLASS
3247 && CLASS_DATA (f->sym)->attr.allocatable)))
c4100eae 3248 gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
08857b61
TB
3249 where, f->sym->name);
3250 else if (where)
3251 gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
c4100eae 3252 "dummy %qs", where, f->sym->name);
ee11be7f
SL
3253 ok = false;
3254 goto match;
08857b61 3255 }
8b704316 3256
5ad6345e
TB
3257 if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
3258 is_elemental, where))
ee11be7f
SL
3259 {
3260 ok = false;
3261 goto match;
3262 }
6de9cd9a 3263
5098e707 3264 /* TS 29113, 6.3p2; F2018 15.5.2.4. */
45a69325
TB
3265 if (f->sym->ts.type == BT_ASSUMED
3266 && (a->expr->ts.type == BT_DERIVED
3267 || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
3268 {
5098e707
SL
3269 gfc_symbol *derived = (a->expr->ts.type == BT_DERIVED
3270 ? a->expr->ts.u.derived
3271 : CLASS_DATA (a->expr)->ts.u.derived);
3272 gfc_namespace *f2k_derived = derived->f2k_derived;
3273 if (derived->attr.pdt_type
3274 || (f2k_derived
3275 && (f2k_derived->finalizers || f2k_derived->tb_sym_root)))
45a69325 3276 {
5098e707
SL
3277 gfc_error ("Actual argument at %L to assumed-type dummy "
3278 "has type parameters or is of "
45a69325
TB
3279 "derived type with type-bound or FINAL procedures",
3280 &a->expr->where);
ee11be7f
SL
3281 ok = false;
3282 goto match;
45a69325
TB
3283 }
3284 }
3285
a0710c29
TB
3286 /* Special case for character arguments. For allocatable, pointer
3287 and assumed-shape dummies, the string length needs to match
3288 exactly. */
2d5b90b2 3289 if (a->expr->ts.type == BT_CHARACTER
eb401400
AV
3290 && a->expr->ts.u.cl && a->expr->ts.u.cl->length
3291 && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
bdd784fc 3292 && a->expr->ts.u.cl->length->ts.type == BT_INTEGER
eb401400
AV
3293 && f->sym->ts.type == BT_CHARACTER && f->sym->ts.u.cl
3294 && f->sym->ts.u.cl->length
3295 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
bdd784fc 3296 && f->sym->ts.u.cl->length->ts.type == BT_INTEGER
eb401400
AV
3297 && (f->sym->attr.pointer || f->sym->attr.allocatable
3298 || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
3299 && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
3300 f->sym->ts.u.cl->length->value.integer) != 0))
3301 {
3302 if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
e0b9e5f9 3303 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
eb401400
AV
3304 "argument and pointer or allocatable dummy argument "
3305 "%qs at %L",
3306 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3307 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3308 f->sym->name, &a->expr->where);
3309 else if (where)
e0b9e5f9 3310 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
eb401400
AV
3311 "argument and assumed-shape dummy argument %qs "
3312 "at %L",
3313 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3314 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3315 f->sym->name, &a->expr->where);
ee11be7f
SL
3316 ok = false;
3317 goto match;
eb401400 3318 }
a0324f7b 3319
8d51f26f 3320 if ((f->sym->attr.pointer || f->sym->attr.allocatable)
eb401400
AV
3321 && f->sym->ts.deferred != a->expr->ts.deferred
3322 && a->expr->ts.type == BT_CHARACTER)
8d51f26f
PT
3323 {
3324 if (where)
0c133211 3325 gfc_error ("Actual argument at %L to allocatable or "
c4100eae 3326 "pointer dummy argument %qs must have a deferred "
8d51f26f
PT
3327 "length type parameter if and only if the dummy has one",
3328 &a->expr->where, f->sym->name);
ee11be7f
SL
3329 ok = false;
3330 goto match;
8d51f26f
PT
3331 }
3332
c49ea23d
PT
3333 if (f->sym->ts.type == BT_CLASS)
3334 goto skip_size_check;
3335
37639728
TB
3336 actual_size = get_expr_storage_size (a->expr);
3337 formal_size = get_sym_storage_size (f->sym);
93302a24
JW
3338 if (actual_size != 0 && actual_size < formal_size
3339 && a->expr->ts.type != BT_PROCEDURE
3340 && f->sym->attr.flavor != FL_PROCEDURE)
2d5b90b2
TB
3341 {
3342 if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
a8b79cc9
HA
3343 {
3344 gfc_warning (0, "Character length of actual argument shorter "
3345 "than of dummy argument %qs (%lu/%lu) at %L",
3346 f->sym->name, actual_size, formal_size,
3347 &a->expr->where);
3348 goto skip_size_check;
3349 }
2d5b90b2 3350 else if (where)
37d92a7e
DH
3351 {
3352 /* Emit a warning for -std=legacy and an error otherwise. */
3353 if (gfc_option.warn_std == 0)
e0b9e5f9 3354 gfc_warning (0, "Actual argument contains too few "
37d92a7e
DH
3355 "elements for dummy argument %qs (%lu/%lu) "
3356 "at %L", f->sym->name, actual_size,
3357 formal_size, &a->expr->where);
3358 else
3359 gfc_error_now ("Actual argument contains too few "
3360 "elements for dummy argument %qs (%lu/%lu) "
3361 "at %L", f->sym->name, actual_size,
3362 formal_size, &a->expr->where);
3363 }
ee11be7f
SL
3364 ok = false;
3365 goto match;
2d5b90b2
TB
3366 }
3367
c49ea23d
PT
3368 skip_size_check:
3369
e9355cc3
JW
3370 /* Satisfy F03:12.4.1.3 by ensuring that a procedure pointer actual
3371 argument is provided for a procedure pointer formal argument. */
8fb74da4 3372 if (f->sym->attr.proc_pointer
a7c0b11d 3373 && !((a->expr->expr_type == EXPR_VARIABLE
4294c093
JW
3374 && (a->expr->symtree->n.sym->attr.proc_pointer
3375 || gfc_is_proc_ptr_comp (a->expr)))
a7c0b11d 3376 || (a->expr->expr_type == EXPR_FUNCTION
4294c093 3377 && is_procptr_result (a->expr))))
8fb74da4
JW
3378 {
3379 if (where)
c4100eae 3380 gfc_error ("Expected a procedure pointer for argument %qs at %L",
8fb74da4 3381 f->sym->name, &a->expr->where);
ee11be7f
SL
3382 ok = false;
3383 goto match;
8fb74da4
JW
3384 }
3385
e9355cc3 3386 /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
699fa7aa 3387 provided for a procedure formal argument. */
e9355cc3 3388 if (f->sym->attr.flavor == FL_PROCEDURE
4294c093
JW
3389 && !((a->expr->expr_type == EXPR_VARIABLE
3390 && (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
3391 || a->expr->symtree->n.sym->attr.proc_pointer
3392 || gfc_is_proc_ptr_comp (a->expr)))
3393 || (a->expr->expr_type == EXPR_FUNCTION
3394 && is_procptr_result (a->expr))))
699fa7aa 3395 {
9914f8cf 3396 if (where)
c4100eae 3397 gfc_error ("Expected a procedure for argument %qs at %L",
9914f8cf 3398 f->sym->name, &a->expr->where);
ee11be7f
SL
3399 ok = false;
3400 goto match;
699fa7aa
PT
3401 }
3402
7afb6108
SL
3403 /* Class array variables and expressions store array info in a
3404 different place from non-class objects; consolidate the logic
3405 to access it here instead of repeating it below. Note that
3406 pointer_arg and allocatable_arg are not fully general and are
3407 only used in a specific situation below with an assumed-rank
3408 argument. */
3409 if (f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym))
3410 {
3411 gfc_component *classdata = CLASS_DATA (f->sym);
3412 fas = classdata->as;
3413 pointer_dummy = classdata->attr.class_pointer;
3414 }
3415 else
3416 {
3417 fas = f->sym->as;
3418 pointer_dummy = f->sym->attr.pointer;
3419 }
3420
3421 if (a->expr->expr_type != EXPR_VARIABLE)
3422 {
3423 aas = NULL;
3424 pointer_arg = false;
3425 allocatable_arg = false;
3426 }
3427 else if (a->expr->ts.type == BT_CLASS
3428 && a->expr->symtree->n.sym
3429 && CLASS_DATA (a->expr->symtree->n.sym))
3430 {
3431 gfc_component *classdata = CLASS_DATA (a->expr->symtree->n.sym);
3432 aas = classdata->as;
3433 pointer_arg = classdata->attr.class_pointer;
3434 allocatable_arg = classdata->attr.allocatable;
3435 }
3436 else
3437 {
3438 aas = a->expr->symtree->n.sym->as;
3439 pointer_arg = a->expr->symtree->n.sym->attr.pointer;
3440 allocatable_arg = a->expr->symtree->n.sym->attr.allocatable;
3441 }
3442
3443 /* F2018:9.5.2(2) permits assumed-size whole array expressions as
3444 actual arguments only if the shape is not required; thus it
3445 cannot be passed to an assumed-shape array dummy.
3446 F2018:15.5.2.(2) permits passing a nonpointer actual to an
3447 intent(in) pointer dummy argument and this is accepted by
3448 the compare_pointer check below, but this also requires shape
3449 information.
3450 There's more discussion of this in PR94110. */
3451 if (fas
3452 && (fas->type == AS_ASSUMED_SHAPE
3453 || fas->type == AS_DEFERRED
3454 || (fas->type == AS_ASSUMED_RANK && pointer_dummy))
3455 && aas
3456 && aas->type == AS_ASSUMED_SIZE
bf9d2177
JJ
3457 && (a->expr->ref == NULL
3458 || (a->expr->ref->type == REF_ARRAY
3459 && a->expr->ref->u.ar.type == AR_FULL)))
3460 {
3461 if (where)
c4100eae 3462 gfc_error ("Actual argument for %qs cannot be an assumed-size"
bf9d2177 3463 " array at %L", f->sym->name, where);
ee11be7f
SL
3464 ok = false;
3465 goto match;
bf9d2177
JJ
3466 }
3467
7afb6108
SL
3468 /* Diagnose F2018 C839 (TS29113 C535c). Here the problem is
3469 passing an assumed-size array to an INTENT(OUT) assumed-rank
3470 dummy when it doesn't have the size information needed to run
3471 initializers and finalizers. */
3472 if (f->sym->attr.intent == INTENT_OUT
3473 && fas
3474 && fas->type == AS_ASSUMED_RANK
3475 && aas
3476 && ((aas->type == AS_ASSUMED_SIZE
3477 && (a->expr->ref == NULL
3478 || (a->expr->ref->type == REF_ARRAY
3479 && a->expr->ref->u.ar.type == AR_FULL)))
3480 || (aas->type == AS_ASSUMED_RANK
3481 && !pointer_arg
3482 && !allocatable_arg))
3483 && (a->expr->ts.type == BT_CLASS
3484 || (a->expr->ts.type == BT_DERIVED
3485 && (gfc_is_finalizable (a->expr->ts.u.derived, NULL)
3486 || gfc_has_ultimate_allocatable (a->expr)
3487 || gfc_has_default_initializer
3488 (a->expr->ts.u.derived)))))
3489 {
3490 if (where)
3491 gfc_error ("Actual argument to assumed-rank INTENT(OUT) "
3492 "dummy %qs at %L cannot be of unknown size",
3493 f->sym->name, where);
ee11be7f
SL
3494 ok = false;
3495 goto match;
7afb6108
SL
3496 }
3497
58e7732a 3498 if (a->expr->expr_type != EXPR_NULL)
6de9cd9a 3499 {
58e7732a
JRFS
3500 int cmp = compare_pointer (f->sym, a->expr);
3501 bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);
6de9cd9a 3502
58e7732a
JRFS
3503 if (pre2008 && cmp == 0)
3504 {
3505 if (where)
3506 gfc_error ("Actual argument for %qs at %L must be a pointer",
3507 f->sym->name, &a->expr->where);
3508 ok = false;
3509 goto match;
3510 }
3511
3512 if (pre2008 && cmp == 2)
3513 {
3514 if (where)
3515 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
3516 "pointer dummy %qs", &a->expr->where, f->sym->name);
3517 ok = false;
3518 goto match;
3519 }
3520
3521 if (!pre2008 && cmp == 0)
3522 {
3523 if (where)
3524 gfc_error ("Actual argument for %qs at %L must be a pointer "
3525 "or a valid target for the dummy pointer in a "
3526 "pointer assignment statement",
3527 f->sym->name, &a->expr->where);
3528 ok = false;
3529 goto match;
3530 }
7d54ef80 3531 }
8b704316 3532
7d54ef80 3533
d3a9eea2
TB
3534 /* Fortran 2008, C1242. */
3535 if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
3536 {
3537 if (where)
3538 gfc_error ("Coindexed actual argument at %L to pointer "
c4100eae 3539 "dummy %qs",
d3a9eea2 3540 &a->expr->where, f->sym->name);
ee11be7f
SL
3541 ok = false;
3542 goto match;
d3a9eea2
TB
3543 }
3544
3545 /* Fortran 2008, 12.5.2.5 (no constraint). */
3546 if (a->expr->expr_type == EXPR_VARIABLE
3547 && f->sym->attr.intent != INTENT_IN
3548 && f->sym->attr.allocatable
3549 && gfc_is_coindexed (a->expr))
3550 {
3551 if (where)
3552 gfc_error ("Coindexed actual argument at %L to allocatable "
c4100eae 3553 "dummy %qs requires INTENT(IN)",
d3a9eea2 3554 &a->expr->where, f->sym->name);
ee11be7f
SL
3555 ok = false;
3556 goto match;
d3a9eea2
TB
3557 }
3558
3559 /* Fortran 2008, C1237. */
3560 if (a->expr->expr_type == EXPR_VARIABLE
3561 && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
3562 && gfc_is_coindexed (a->expr)
3563 && (a->expr->symtree->n.sym->attr.volatile_
3564 || a->expr->symtree->n.sym->attr.asynchronous))
3565 {
3566 if (where)
3567 gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
c4100eae 3568 "%L requires that dummy %qs has neither "
d3a9eea2
TB
3569 "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
3570 f->sym->name);
ee11be7f
SL
3571 ok = false;
3572 goto match;
d3a9eea2
TB
3573 }
3574
3575 /* Fortran 2008, 12.5.2.4 (no constraint). */
3576 if (a->expr->expr_type == EXPR_VARIABLE
3577 && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
3578 && gfc_is_coindexed (a->expr)
3579 && gfc_has_ultimate_allocatable (a->expr))
3580 {
3581 if (where)
3582 gfc_error ("Coindexed actual argument at %L with allocatable "
c4100eae 3583 "ultimate component to dummy %qs requires either VALUE "
d3a9eea2 3584 "or INTENT(IN)", &a->expr->where, f->sym->name);
ee11be7f
SL
3585 ok = false;
3586 goto match;
d3a9eea2
TB
3587 }
3588
c49ea23d
PT
3589 if (f->sym->ts.type == BT_CLASS
3590 && CLASS_DATA (f->sym)->attr.allocatable
3591 && gfc_is_class_array_ref (a->expr, &full_array)
3592 && !full_array)
3593 {
3594 if (where)
c4100eae 3595 gfc_error ("Actual CLASS array argument for %qs must be a full "
c49ea23d 3596 "array at %L", f->sym->name, &a->expr->where);
ee11be7f
SL
3597 ok = false;
3598 goto match;
c49ea23d
PT
3599 }
3600
3601
aa08038d 3602 if (a->expr->expr_type != EXPR_NULL
f3e1097b 3603 && !compare_allocatable (f->sym, a->expr))
aa08038d
EE
3604 {
3605 if (where)
c4100eae 3606 gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
aa08038d 3607 f->sym->name, &a->expr->where);
ee11be7f
SL
3608 ok = false;
3609 goto match;
aa08038d
EE
3610 }
3611
a920e94a 3612 /* Check intent = OUT/INOUT for definable actual argument. */
f3883269
SK
3613 if (!in_statement_function
3614 && (f->sym->attr.intent == INTENT_OUT
3615 || f->sym->attr.intent == INTENT_INOUT))
a920e94a 3616 {
8c91ab34
DK
3617 const char* context = (where
3618 ? _("actual argument to INTENT = OUT/INOUT")
3619 : NULL);
a920e94a 3620
bcb4ad36
TB
3621 if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3622 && CLASS_DATA (f->sym)->attr.class_pointer)
3623 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
524af0d6 3624 && !gfc_check_vardef_context (a->expr, true, false, false, context))
ee11be7f
SL
3625 {
3626 ok = false;
3627 goto match;
3628 }
524af0d6 3629 if (!gfc_check_vardef_context (a->expr, false, false, false, context))
ee11be7f
SL
3630 {
3631 ok = false;
3632 goto match;
3633 }
ee7e677f
TB
3634 }
3635
59be8071
TB
3636 if ((f->sym->attr.intent == INTENT_OUT
3637 || f->sym->attr.intent == INTENT_INOUT
84efddb2
DF
3638 || f->sym->attr.volatile_
3639 || f->sym->attr.asynchronous)
03af1e4c 3640 && gfc_has_vector_subscript (a->expr))
59be8071
TB
3641 {
3642 if (where)
84efddb2
DF
3643 gfc_error ("Array-section actual argument with vector "
3644 "subscripts at %L is incompatible with INTENT(OUT), "
3645 "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
c4100eae 3646 "of the dummy argument %qs",
59be8071 3647 &a->expr->where, f->sym->name);
ee11be7f
SL
3648 ok = false;
3649 goto match;
59be8071
TB
3650 }
3651
9bce3c1c
TB
3652 /* C1232 (R1221) For an actual argument which is an array section or
3653 an assumed-shape array, the dummy argument shall be an assumed-
3654 shape array, if the dummy argument has the VOLATILE attribute. */
3655
3656 if (f->sym->attr.volatile_
271dd55c 3657 && a->expr->expr_type == EXPR_VARIABLE
9bce3c1c
TB
3658 && a->expr->symtree->n.sym->as
3659 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
7afb6108 3660 && !(fas && fas->type == AS_ASSUMED_SHAPE))
9bce3c1c
TB
3661 {
3662 if (where)
3663 gfc_error ("Assumed-shape actual argument at %L is "
3664 "incompatible with the non-assumed-shape "
c4100eae 3665 "dummy argument %qs due to VOLATILE attribute",
9bce3c1c 3666 &a->expr->where,f->sym->name);
ee11be7f
SL
3667 ok = false;
3668 goto match;
9bce3c1c
TB
3669 }
3670
eb401400
AV
3671 /* Find the last array_ref. */
3672 actual_arr_ref = NULL;
3673 if (a->expr->ref)
3674 actual_arr_ref = gfc_find_array_ref (a->expr, true);
3675
9bce3c1c 3676 if (f->sym->attr.volatile_
eb401400 3677 && actual_arr_ref && actual_arr_ref->type == AR_SECTION
7afb6108 3678 && !(fas && fas->type == AS_ASSUMED_SHAPE))
9bce3c1c
TB
3679 {
3680 if (where)
3681 gfc_error ("Array-section actual argument at %L is "
3682 "incompatible with the non-assumed-shape "
c4100eae 3683 "dummy argument %qs due to VOLATILE attribute",
eb401400 3684 &a->expr->where, f->sym->name);
ee11be7f
SL
3685 ok = false;
3686 goto match;
9bce3c1c
TB
3687 }
3688
3689 /* C1233 (R1221) For an actual argument which is a pointer array, the
3690 dummy argument shall be an assumed-shape or pointer array, if the
3691 dummy argument has the VOLATILE attribute. */
3692
3693 if (f->sym->attr.volatile_
271dd55c 3694 && a->expr->expr_type == EXPR_VARIABLE
9bce3c1c
TB
3695 && a->expr->symtree->n.sym->attr.pointer
3696 && a->expr->symtree->n.sym->as
7afb6108
SL
3697 && !(fas
3698 && (fas->type == AS_ASSUMED_SHAPE
9bce3c1c
TB
3699 || f->sym->attr.pointer)))
3700 {
3701 if (where)
3702 gfc_error ("Pointer-array actual argument at %L requires "
3703 "an assumed-shape or pointer-array dummy "
c4100eae 3704 "argument %qs due to VOLATILE attribute",
9bce3c1c 3705 &a->expr->where,f->sym->name);
ee11be7f
SL
3706 ok = false;
3707 goto match;
9bce3c1c
TB
3708 }
3709
6de9cd9a
DN
3710 match:
3711 if (a == actual)
3712 na = i;
3713
7b901ac4 3714 new_arg[i++] = a;
6de9cd9a
DN
3715 }
3716
ee11be7f
SL
3717 /* Give up now if we saw any bad argument. */
3718 if (!ok)
3719 return false;
3720
6de9cd9a
DN
3721 /* Make sure missing actual arguments are optional. */
3722 i = 0;
3723 for (f = formal; f; f = f->next, i++)
3724 {
7b901ac4 3725 if (new_arg[i] != NULL)
6de9cd9a 3726 continue;
3ab7b3de
BM
3727 if (f->sym == NULL)
3728 {
3729 if (where)
b251af97
SK
3730 gfc_error ("Missing alternate return spec in subroutine call "
3731 "at %L", where);
f3e1097b 3732 return false;
3ab7b3de 3733 }
eb92cd57
TB
3734 /* For CLASS, the optional attribute might be set at either location. */
3735 if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
3736 && !f->sym->attr.optional)
3737 || (in_statement_function
3738 && (f->sym->attr.optional
3739 || (f->sym->ts.type == BT_CLASS
3740 && CLASS_DATA (f->sym)->attr.optional))))
6de9cd9a
DN
3741 {
3742 if (where)
c4100eae 3743 gfc_error ("Missing actual argument for argument %qs at %L",
6de9cd9a 3744 f->sym->name, where);
f3e1097b 3745 return false;
6de9cd9a
DN
3746 }
3747 }
3748
a85e5696
SL
3749 /* We should have handled the cases where the formal arglist is null
3750 already. */
3751 gcc_assert (n > 0);
3752
6de9cd9a
DN
3753 /* The argument lists are compatible. We now relink a new actual
3754 argument list with null arguments in the right places. The head
3755 of the list remains the head. */
5888512f 3756 for (f = formal, i = 0; f; f = f->next, i++)
7b901ac4 3757 if (new_arg[i] == NULL)
5888512f
MM
3758 {
3759 new_arg[i] = gfc_get_actual_arglist ();
3760 new_arg[i]->associated_dummy = get_nonintrinsic_dummy_arg (f);
3761 }
6de9cd9a
DN
3762
3763 if (na != 0)
3764 {
fab27f52
MM
3765 std::swap (*new_arg[0], *actual);
3766 std::swap (new_arg[0], new_arg[na]);
6de9cd9a
DN
3767 }
3768
3769 for (i = 0; i < n - 1; i++)
7b901ac4 3770 new_arg[i]->next = new_arg[i + 1];
6de9cd9a 3771
7b901ac4 3772 new_arg[i]->next = NULL;
6de9cd9a
DN
3773
3774 if (*ap == NULL && n > 0)
7b901ac4 3775 *ap = new_arg[0];
6de9cd9a 3776
f3e1097b 3777 return true;
6de9cd9a
DN
3778}
3779
3780
3781typedef struct
3782{
3783 gfc_formal_arglist *f;
3784 gfc_actual_arglist *a;
3785}
3786argpair;
3787
3788/* qsort comparison function for argument pairs, with the following
3789 order:
3790 - p->a->expr == NULL
3791 - p->a->expr->expr_type != EXPR_VARIABLE
c5014982 3792 - by gfc_symbol pointer value (larger first). */
6de9cd9a
DN
3793
3794static int
3795pair_cmp (const void *p1, const void *p2)
3796{
3797 const gfc_actual_arglist *a1, *a2;
3798
3799 /* *p1 and *p2 are elements of the to-be-sorted array. */
3800 a1 = ((const argpair *) p1)->a;
3801 a2 = ((const argpair *) p2)->a;
3802 if (!a1->expr)
3803 {
3804 if (!a2->expr)
3805 return 0;
3806 return -1;
3807 }
3808 if (!a2->expr)
3809 return 1;
3810 if (a1->expr->expr_type != EXPR_VARIABLE)
3811 {
3812 if (a2->expr->expr_type != EXPR_VARIABLE)
3813 return 0;
3814 return -1;
3815 }
3816 if (a2->expr->expr_type != EXPR_VARIABLE)
3817 return 1;
c5014982
AM
3818 if (a1->expr->symtree->n.sym > a2->expr->symtree->n.sym)
3819 return -1;
6de9cd9a
DN
3820 return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
3821}
3822
3823
3824/* Given two expressions from some actual arguments, test whether they
3825 refer to the same expression. The analysis is conservative.
524af0d6 3826 Returning false will produce no warning. */
6de9cd9a 3827
524af0d6 3828static bool
b251af97 3829compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
6de9cd9a
DN
3830{
3831 const gfc_ref *r1, *r2;
3832
3833 if (!e1 || !e2
3834 || e1->expr_type != EXPR_VARIABLE
3835 || e2->expr_type != EXPR_VARIABLE
3836 || e1->symtree->n.sym != e2->symtree->n.sym)
524af0d6 3837 return false;
6de9cd9a 3838
e53b6e56 3839 /* TODO: improve comparison, see expr.cc:show_ref(). */
6de9cd9a
DN
3840 for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
3841 {
3842 if (r1->type != r2->type)
524af0d6 3843 return false;
6de9cd9a
DN
3844 switch (r1->type)
3845 {
3846 case REF_ARRAY:
3847 if (r1->u.ar.type != r2->u.ar.type)
524af0d6 3848 return false;
6de9cd9a
DN
3849 /* TODO: At the moment, consider only full arrays;
3850 we could do better. */
3851 if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
524af0d6 3852 return false;
6de9cd9a
DN
3853 break;
3854
3855 case REF_COMPONENT:
3856 if (r1->u.c.component != r2->u.c.component)
524af0d6 3857 return false;
6de9cd9a
DN
3858 break;
3859
3860 case REF_SUBSTRING:
524af0d6 3861 return false;
6de9cd9a 3862
f16be16d
SK
3863 case REF_INQUIRY:
3864 if (e1->symtree->n.sym->ts.type == BT_COMPLEX
3865 && e1->ts.type == BT_REAL && e2->ts.type == BT_REAL
3866 && r1->u.i != r2->u.i)
3867 return false;
3868 break;
3869
6de9cd9a
DN
3870 default:
3871 gfc_internal_error ("compare_actual_expr(): Bad component code");
3872 }
3873 }
3874 if (!r1 && !r2)
524af0d6
JB
3875 return true;
3876 return false;
6de9cd9a
DN
3877}
3878
b251af97 3879
6de9cd9a
DN
3880/* Given formal and actual argument lists that correspond to one
3881 another, check that identical actual arguments aren't not
3882 associated with some incompatible INTENTs. */
3883
524af0d6 3884static bool
b251af97 3885check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
6de9cd9a
DN
3886{
3887 sym_intent f1_intent, f2_intent;
3888 gfc_formal_arglist *f1;
3889 gfc_actual_arglist *a1;
3890 size_t n, i, j;
3891 argpair *p;
524af0d6 3892 bool t = true;
6de9cd9a
DN
3893
3894 n = 0;
3895 for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
3896 {
3897 if (f1 == NULL && a1 == NULL)
3898 break;
3899 if (f1 == NULL || a1 == NULL)
3900 gfc_internal_error ("check_some_aliasing(): List mismatch");
3901 n++;
3902 }
3903 if (n == 0)
3904 return t;
1145e690 3905 p = XALLOCAVEC (argpair, n);
6de9cd9a
DN
3906
3907 for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
3908 {
3909 p[i].f = f1;
3910 p[i].a = a1;
3911 }
3912
3913 qsort (p, n, sizeof (argpair), pair_cmp);
3914
3915 for (i = 0; i < n; i++)
3916 {
3917 if (!p[i].a->expr
3918 || p[i].a->expr->expr_type != EXPR_VARIABLE
3919 || p[i].a->expr->ts.type == BT_PROCEDURE)
3920 continue;
3921 f1_intent = p[i].f->sym->attr.intent;
3922 for (j = i + 1; j < n; j++)
3923 {
3924 /* Expected order after the sort. */
3925 if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
3926 gfc_internal_error ("check_some_aliasing(): corrupted data");
3927
3928 /* Are the expression the same? */
524af0d6 3929 if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
6de9cd9a
DN
3930 break;
3931 f2_intent = p[j].f->sym->attr.intent;
3932 if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
9f1930be
TB
3933 || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
3934 || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
6de9cd9a 3935 {
db30e21c 3936 gfc_warning (0, "Same actual argument associated with INTENT(%s) "
48749dbc 3937 "argument %qs and INTENT(%s) argument %qs at %L",
6de9cd9a
DN
3938 gfc_intent_string (f1_intent), p[i].f->sym->name,
3939 gfc_intent_string (f2_intent), p[j].f->sym->name,
3940 &p[i].a->expr->where);
524af0d6 3941 t = false;
6de9cd9a
DN
3942 }
3943 }
3944 }
3945
3946 return t;
3947}
3948
3949
3950/* Given formal and actual argument lists that correspond to one
3951 another, check that they are compatible in the sense that intents
3952 are not mismatched. */
3953
524af0d6 3954static bool
b251af97 3955check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
6de9cd9a 3956{
f17facac 3957 sym_intent f_intent;
6de9cd9a
DN
3958
3959 for (;; f = f->next, a = a->next)
3960 {
99c39534
TB
3961 gfc_expr *expr;
3962
6de9cd9a
DN
3963 if (f == NULL && a == NULL)
3964 break;
3965 if (f == NULL || a == NULL)
3966 gfc_internal_error ("check_intents(): List mismatch");
3967
99c39534
TB
3968 if (a->expr && a->expr->expr_type == EXPR_FUNCTION
3969 && a->expr->value.function.isym
3970 && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
3971 expr = a->expr->value.function.actual->expr;
3972 else
3973 expr = a->expr;
3974
3975 if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
6de9cd9a
DN
3976 continue;
3977
6de9cd9a
DN
3978 f_intent = f->sym->attr.intent;
3979
99c39534 3980 if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
6de9cd9a 3981 {
bcb4ad36
TB
3982 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3983 && CLASS_DATA (f->sym)->attr.class_pointer)
3984 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
6de9cd9a 3985 {
b251af97
SK
3986 gfc_error ("Procedure argument at %L is local to a PURE "
3987 "procedure and has the POINTER attribute",
99c39534 3988 &expr->where);
524af0d6 3989 return false;
6de9cd9a
DN
3990 }
3991 }
d3a9eea2
TB
3992
3993 /* Fortran 2008, C1283. */
99c39534 3994 if (gfc_pure (NULL) && gfc_is_coindexed (expr))
d3a9eea2
TB
3995 {
3996 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
3997 {
3998 gfc_error ("Coindexed actual argument at %L in PURE procedure "
3999 "is passed to an INTENT(%s) argument",
99c39534 4000 &expr->where, gfc_intent_string (f_intent));
524af0d6 4001 return false;
d3a9eea2
TB
4002 }
4003
bcb4ad36
TB
4004 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
4005 && CLASS_DATA (f->sym)->attr.class_pointer)
4006 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
d3a9eea2
TB
4007 {
4008 gfc_error ("Coindexed actual argument at %L in PURE procedure "
4009 "is passed to a POINTER dummy argument",
99c39534 4010 &expr->where);
524af0d6 4011 return false;
d3a9eea2
TB
4012 }
4013 }
4014
4015 /* F2008, Section 12.5.2.4. */
99c39534
TB
4016 if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
4017 && gfc_is_coindexed (expr))
d3a9eea2
TB
4018 {
4019 gfc_error ("Coindexed polymorphic actual argument at %L is passed "
c4100eae 4020 "polymorphic dummy argument %qs",
99c39534 4021 &expr->where, f->sym->name);
524af0d6 4022 return false;
d3a9eea2 4023 }
6de9cd9a
DN
4024 }
4025
524af0d6 4026 return true;
6de9cd9a
DN
4027}
4028
4029
4030/* Check how a procedure is used against its interface. If all goes
4031 well, the actual argument list will also end up being properly
4032 sorted. */
4033
524af0d6 4034bool
b251af97 4035gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
6de9cd9a 4036{
f3883269 4037 gfc_actual_arglist *a;
4cbc9039 4038 gfc_formal_arglist *dummy_args;
4a4fc7fe 4039 bool implicit = false;
4cbc9039 4040
a9c5fe7e 4041 /* Warn about calls with an implicit interface. Special case
6bd2c800 4042 for calling a ISO_C_BINDING because c_loc and c_funloc
ca071303
FXC
4043 are pseudo-unknown. Additionally, warn about procedures not
4044 explicitly declared at all if requested. */
8b7a967e 4045 if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
ca071303 4046 {
b31f8023 4047 bool has_implicit_none_export = false;
4a4fc7fe 4048 implicit = true;
b31f8023
TB
4049 if (sym->attr.proc == PROC_UNKNOWN)
4050 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
4051 if (ns->has_implicit_none_export)
4052 {
4053 has_implicit_none_export = true;
4054 break;
4055 }
4056 if (has_implicit_none_export)
8b7a967e 4057 {
bcc478b9
BRF
4058 const char *guessed
4059 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
4060 if (guessed)
4061 gfc_error ("Procedure %qs called at %L is not explicitly declared"
4062 "; did you mean %qs?",
4063 sym->name, where, guessed);
4064 else
4065 gfc_error ("Procedure %qs called at %L is not explicitly declared",
4066 sym->name, where);
8b7a967e
TB
4067 return false;
4068 }
73e42eef 4069 if (warn_implicit_interface)
48749dbc
MLI
4070 gfc_warning (OPT_Wimplicit_interface,
4071 "Procedure %qs called with an implicit interface at %L",
ca071303 4072 sym->name, where);
73e42eef 4073 else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
48749dbc
MLI
4074 gfc_warning (OPT_Wimplicit_procedure,
4075 "Procedure %qs called at %L is not explicitly declared",
ca071303 4076 sym->name, where);
ffeebc4f 4077 gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
ca071303 4078 }
6de9cd9a 4079
e6895430 4080 if (sym->attr.if_source == IFSRC_UNKNOWN)
ac05557c 4081 {
86d7449c
TB
4082 if (sym->attr.pointer)
4083 {
c4100eae
MLI
4084 gfc_error ("The pointer object %qs at %L must have an explicit "
4085 "function interface or be declared as array",
4086 sym->name, where);
524af0d6 4087 return false;
86d7449c
TB
4088 }
4089
4090 if (sym->attr.allocatable && !sym->attr.external)
4091 {
c4100eae
MLI
4092 gfc_error ("The allocatable object %qs at %L must have an explicit "
4093 "function interface or be declared as array",
4094 sym->name, where);
524af0d6 4095 return false;
86d7449c
TB
4096 }
4097
4098 if (sym->attr.allocatable)
4099 {
c4100eae
MLI
4100 gfc_error ("Allocatable function %qs at %L must have an explicit "
4101 "function interface", sym->name, where);
524af0d6 4102 return false;
86d7449c
TB
4103 }
4104
ac05557c
DF
4105 for (a = *ap; a; a = a->next)
4106 {
fb078366
TK
4107 if (a->expr && a->expr->error)
4108 return false;
4109
4a4fc7fe
TK
4110 /* F2018, 15.4.2.2 Explicit interface is required for a
4111 polymorphic dummy argument, so there is no way to
4112 legally have a class appear in an argument with an
4113 implicit interface. */
4114
4115 if (implicit && a->expr && a->expr->ts.type == BT_CLASS)
4116 {
4117 gfc_error ("Explicit interface required for polymorphic "
4118 "argument at %L",&a->expr->where);
4119 a->expr->error = 1;
4120 break;
4121 }
4122
ac05557c
DF
4123 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4124 if (a->name != NULL && a->name[0] != '%')
4125 {
c4100eae
MLI
4126 gfc_error ("Keyword argument requires explicit interface "
4127 "for procedure %qs at %L", sym->name, &a->expr->where);
ac05557c
DF
4128 break;
4129 }
fea54935 4130
45a69325
TB
4131 /* TS 29113, 6.2. */
4132 if (a->expr && a->expr->ts.type == BT_ASSUMED
4133 && sym->intmod_sym_id != ISOCBINDING_LOC)
4134 {
4135 gfc_error ("Assumed-type argument %s at %L requires an explicit "
4136 "interface", a->expr->symtree->n.sym->name,
4137 &a->expr->where);
fb078366 4138 a->expr->error = 1;
45a69325
TB
4139 break;
4140 }
4141
fea54935
TB
4142 /* F2008, C1303 and C1304. */
4143 if (a->expr
4144 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
f477062c 4145 && a->expr->ts.u.derived
fea54935
TB
4146 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4147 && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
4148 || gfc_expr_attr (a->expr).lock_comp))
4149 {
c4100eae
MLI
4150 gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
4151 "component at %L requires an explicit interface for "
4152 "procedure %qs", &a->expr->where, sym->name);
fb078366 4153 a->expr->error = 1;
fea54935
TB
4154 break;
4155 }
ea8ad3e5 4156
5df445a2
TB
4157 if (a->expr
4158 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
f477062c 4159 && a->expr->ts.u.derived
5df445a2
TB
4160 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4161 && a->expr->ts.u.derived->intmod_sym_id
4162 == ISOFORTRAN_EVENT_TYPE)
4163 || gfc_expr_attr (a->expr).event_comp))
4164 {
4165 gfc_error ("Actual argument of EVENT_TYPE or with EVENT_TYPE "
4166 "component at %L requires an explicit interface for "
4167 "procedure %qs", &a->expr->where, sym->name);
fb078366 4168 a->expr->error = 1;
5df445a2
TB
4169 break;
4170 }
4171
ea8ad3e5
TB
4172 if (a->expr && a->expr->expr_type == EXPR_NULL
4173 && a->expr->ts.type == BT_UNKNOWN)
4174 {
fb078366
TK
4175 gfc_error ("MOLD argument to NULL required at %L",
4176 &a->expr->where);
4177 a->expr->error = 1;
524af0d6 4178 return false;
ea8ad3e5 4179 }
c62c6622 4180
820c25c8
HA
4181 if (a->expr && a->expr->expr_type == EXPR_NULL)
4182 {
4183 gfc_error ("Passing intrinsic NULL as actual argument at %L "
4184 "requires an explicit interface", &a->expr->where);
4185 a->expr->error = 1;
4186 return false;
4187 }
4188
c62c6622
TB
4189 /* TS 29113, C407b. */
4190 if (a->expr && a->expr->expr_type == EXPR_VARIABLE
4191 && symbol_rank (a->expr->symtree->n.sym) == -1)
4192 {
4193 gfc_error ("Assumed-rank argument requires an explicit interface "
4194 "at %L", &a->expr->where);
fb078366 4195 a->expr->error = 1;
524af0d6 4196 return false;
c62c6622 4197 }
ac05557c
DF
4198 }
4199
524af0d6 4200 return true;
ac05557c
DF
4201 }
4202
4cbc9039
JW
4203 dummy_args = gfc_sym_get_dummy_args (sym);
4204
f3883269
SK
4205 /* For a statement function, check that types and type parameters of actual
4206 arguments and dummy arguments match. */
e68a35ae
TK
4207 if (!gfc_compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
4208 sym->attr.proc == PROC_ST_FUNCTION, where))
524af0d6 4209 return false;
a5baf71b 4210
524af0d6
JB
4211 if (!check_intents (dummy_args, *ap))
4212 return false;
6de9cd9a 4213
73e42eef 4214 if (warn_aliasing)
4cbc9039 4215 check_some_aliasing (dummy_args, *ap);
f8552cd4 4216
524af0d6 4217 return true;
6de9cd9a
DN
4218}
4219
4220
7e196f89
JW
4221/* Check how a procedure pointer component is used against its interface.
4222 If all goes well, the actual argument list will also end up being properly
4223 sorted. Completely analogous to gfc_procedure_use. */
4224
4225void
4226gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
4227{
7e196f89 4228 /* Warn about calls with an implicit interface. Special case
6bd2c800 4229 for calling a ISO_C_BINDING because c_loc and c_funloc
7e196f89 4230 are pseudo-unknown. */
73e42eef 4231 if (warn_implicit_interface
7e196f89
JW
4232 && comp->attr.if_source == IFSRC_UNKNOWN
4233 && !comp->attr.is_iso_c)
48749dbc
MLI
4234 gfc_warning (OPT_Wimplicit_interface,
4235 "Procedure pointer component %qs called with an implicit "
7e196f89
JW
4236 "interface at %L", comp->name, where);
4237
4238 if (comp->attr.if_source == IFSRC_UNKNOWN)
4239 {
4240 gfc_actual_arglist *a;
4241 for (a = *ap; a; a = a->next)
4242 {
4243 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4244 if (a->name != NULL && a->name[0] != '%')
4245 {
c4100eae
MLI
4246 gfc_error ("Keyword argument requires explicit interface "
4247 "for procedure pointer component %qs at %L",
4248 comp->name, &a->expr->where);
7e196f89
JW
4249 break;
4250 }
4251 }
4252
4253 return;
4254 }
4255
e68a35ae 4256 if (!gfc_compare_actual_formal (ap, comp->ts.interface->formal, 0,
f3883269 4257 comp->attr.elemental, false, where))
7e196f89
JW
4258 return;
4259
4cbc9039 4260 check_intents (comp->ts.interface->formal, *ap);
73e42eef 4261 if (warn_aliasing)
4cbc9039 4262 check_some_aliasing (comp->ts.interface->formal, *ap);
7e196f89
JW
4263}
4264
4265
f0ac18b7
DK
4266/* Try if an actual argument list matches the formal list of a symbol,
4267 respecting the symbol's attributes like ELEMENTAL. This is used for
4268 GENERIC resolution. */
4269
4270bool
4271gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
4272{
4cbc9039 4273 gfc_formal_arglist *dummy_args;
f0ac18b7
DK
4274 bool r;
4275
1d101216
JD
4276 if (sym->attr.flavor != FL_PROCEDURE)
4277 return false;
f0ac18b7 4278
4cbc9039
JW
4279 dummy_args = gfc_sym_get_dummy_args (sym);
4280
f0ac18b7 4281 r = !sym->attr.elemental;
e68a35ae 4282 if (gfc_compare_actual_formal (args, dummy_args, r, !r, false, NULL))
f0ac18b7 4283 {
4cbc9039 4284 check_intents (dummy_args, *args);
73e42eef 4285 if (warn_aliasing)
4cbc9039 4286 check_some_aliasing (dummy_args, *args);
f0ac18b7
DK
4287 return true;
4288 }
4289
4290 return false;
4291}
4292
4293
6de9cd9a
DN
4294/* Given an interface pointer and an actual argument list, search for
4295 a formal argument list that matches the actual. If found, returns
4296 a pointer to the symbol of the correct interface. Returns NULL if
4297 not found. */
4298
4299gfc_symbol *
b251af97
SK
4300gfc_search_interface (gfc_interface *intr, int sub_flag,
4301 gfc_actual_arglist **ap)
6de9cd9a 4302{
22a0a780 4303 gfc_symbol *elem_sym = NULL;
ea8ad3e5
TB
4304 gfc_symbol *null_sym = NULL;
4305 locus null_expr_loc;
4306 gfc_actual_arglist *a;
4307 bool has_null_arg = false;
4308
4309 for (a = *ap; a; a = a->next)
4310 if (a->expr && a->expr->expr_type == EXPR_NULL
4311 && a->expr->ts.type == BT_UNKNOWN)
4312 {
4313 has_null_arg = true;
4314 null_expr_loc = a->expr->where;
4315 break;
8b704316 4316 }
ea8ad3e5 4317
6de9cd9a
DN
4318 for (; intr; intr = intr->next)
4319 {
f6288c24 4320 if (gfc_fl_struct (intr->sym->attr.flavor))
c3f34952 4321 continue;
6de9cd9a
DN
4322 if (sub_flag && intr->sym->attr.function)
4323 continue;
4324 if (!sub_flag && intr->sym->attr.subroutine)
4325 continue;
4326
f0ac18b7 4327 if (gfc_arglist_matches_symbol (ap, intr->sym))
22a0a780 4328 {
ea8ad3e5
TB
4329 if (has_null_arg && null_sym)
4330 {
4331 gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
4332 "between specific functions %s and %s",
4333 &null_expr_loc, null_sym->name, intr->sym->name);
4334 return NULL;
4335 }
4336 else if (has_null_arg)
4337 {
4338 null_sym = intr->sym;
4339 continue;
4340 }
4341
22a0a780 4342 /* Satisfy 12.4.4.1 such that an elemental match has lower
8b704316 4343 weight than a non-elemental match. */
22a0a780
PT
4344 if (intr->sym->attr.elemental)
4345 {
4346 elem_sym = intr->sym;
4347 continue;
4348 }
4349 return intr->sym;
4350 }
6de9cd9a
DN
4351 }
4352
ea8ad3e5
TB
4353 if (null_sym)
4354 return null_sym;
4355
22a0a780 4356 return elem_sym ? elem_sym : NULL;
6de9cd9a
DN
4357}
4358
4359
4360/* Do a brute force recursive search for a symbol. */
4361
4362static gfc_symtree *
b251af97 4363find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
6de9cd9a
DN
4364{
4365 gfc_symtree * st;
4366
4367 if (root->n.sym == sym)
4368 return root;
4369
4370 st = NULL;
4371 if (root->left)
4372 st = find_symtree0 (root->left, sym);
4373 if (root->right && ! st)
4374 st = find_symtree0 (root->right, sym);
4375 return st;
4376}
4377
4378
4379/* Find a symtree for a symbol. */
4380
f6fad28e
DK
4381gfc_symtree *
4382gfc_find_sym_in_symtree (gfc_symbol *sym)
6de9cd9a
DN
4383{
4384 gfc_symtree *st;
4385 gfc_namespace *ns;
4386
4387 /* First try to find it by name. */
4388 gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
4389 if (st && st->n.sym == sym)
4390 return st;
4391
66e4ab31 4392 /* If it's been renamed, resort to a brute-force search. */
6de9cd9a
DN
4393 /* TODO: avoid having to do this search. If the symbol doesn't exist
4394 in the symtree for the current namespace, it should probably be added. */
4395 for (ns = gfc_current_ns; ns; ns = ns->parent)
4396 {
4397 st = find_symtree0 (ns->sym_root, sym);
4398 if (st)
b251af97 4399 return st;
6de9cd9a 4400 }
17d5d49f 4401 gfc_internal_error ("Unable to find symbol %qs", sym->name);
66e4ab31 4402 /* Not reached. */
6de9cd9a
DN
4403}
4404
4405
4a44a72d
DK
4406/* See if the arglist to an operator-call contains a derived-type argument
4407 with a matching type-bound operator. If so, return the matching specific
4408 procedure defined as operator-target as well as the base-object to use
974df0f8
PT
4409 (which is the found derived-type argument with operator). The generic
4410 name, if any, is transmitted to the final expression via 'gname'. */
4a44a72d
DK
4411
4412static gfc_typebound_proc*
4413matching_typebound_op (gfc_expr** tb_base,
4414 gfc_actual_arglist* args,
974df0f8
PT
4415 gfc_intrinsic_op op, const char* uop,
4416 const char ** gname)
4a44a72d
DK
4417{
4418 gfc_actual_arglist* base;
4419
4420 for (base = args; base; base = base->next)
4b7dd692 4421 if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
4a44a72d
DK
4422 {
4423 gfc_typebound_proc* tb;
4424 gfc_symbol* derived;
524af0d6 4425 bool result;
4a44a72d 4426
efd2e969
PT
4427 while (base->expr->expr_type == EXPR_OP
4428 && base->expr->value.op.op == INTRINSIC_PARENTHESES)
4429 base->expr = base->expr->value.op.op1;
4430
4b7dd692 4431 if (base->expr->ts.type == BT_CLASS)
528622fd 4432 {
fba5a793 4433 if (!base->expr->ts.u.derived || CLASS_DATA (base->expr) == NULL
0a59e583 4434 || !gfc_expr_attr (base->expr).class_ok)
528622fd
JW
4435 continue;
4436 derived = CLASS_DATA (base->expr)->ts.u.derived;
4437 }
4b7dd692
JW
4438 else
4439 derived = base->expr->ts.u.derived;
4a44a72d
DK
4440
4441 if (op == INTRINSIC_USER)
4442 {
4443 gfc_symtree* tb_uop;
4444
4445 gcc_assert (uop);
4446 tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
4447 false, NULL);
4448
4449 if (tb_uop)
4450 tb = tb_uop->n.tb;
4451 else
4452 tb = NULL;
4453 }
4454 else
4455 tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
4456 false, NULL);
4457
4458 /* This means we hit a PRIVATE operator which is use-associated and
4459 should thus not be seen. */
524af0d6 4460 if (!result)
4a44a72d
DK
4461 tb = NULL;
4462
4463 /* Look through the super-type hierarchy for a matching specific
4464 binding. */
4465 for (; tb; tb = tb->overridden)
4466 {
4467 gfc_tbp_generic* g;
4468
4469 gcc_assert (tb->is_generic);
4470 for (g = tb->u.generic; g; g = g->next)
4471 {
4472 gfc_symbol* target;
4473 gfc_actual_arglist* argcopy;
4474 bool matches;
4475
4476 gcc_assert (g->specific);
4477 if (g->specific->error)
4478 continue;
4479
4480 target = g->specific->u.specific->n.sym;
4481
4482 /* Check if this arglist matches the formal. */
4483 argcopy = gfc_copy_actual_arglist (args);
4484 matches = gfc_arglist_matches_symbol (&argcopy, target);
4485 gfc_free_actual_arglist (argcopy);
4486
4487 /* Return if we found a match. */
4488 if (matches)
4489 {
4490 *tb_base = base->expr;
974df0f8 4491 *gname = g->specific_st->name;
4a44a72d
DK
4492 return g->specific;
4493 }
4494 }
4495 }
4496 }
4497
4498 return NULL;
4499}
4500
4501
4502/* For the 'actual arglist' of an operator call and a specific typebound
4503 procedure that has been found the target of a type-bound operator, build the
4504 appropriate EXPR_COMPCALL and resolve it. We take this indirection over
4505 type-bound procedures rather than resolving type-bound operators 'directly'
4506 so that we can reuse the existing logic. */
4507
4508static void
4509build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
974df0f8
PT
4510 gfc_expr* base, gfc_typebound_proc* target,
4511 const char *gname)
4a44a72d
DK
4512{
4513 e->expr_type = EXPR_COMPCALL;
4514 e->value.compcall.tbp = target;
974df0f8 4515 e->value.compcall.name = gname ? gname : "$op";
4a44a72d
DK
4516 e->value.compcall.actual = actual;
4517 e->value.compcall.base_object = base;
4518 e->value.compcall.ignore_pass = 1;
4519 e->value.compcall.assign = 0;
94fae14b
PT
4520 if (e->ts.type == BT_UNKNOWN
4521 && target->function)
4522 {
4523 if (target->is_generic)
4524 e->ts = target->u.generic->specific->u.specific->n.sym->ts;
4525 else
4526 e->ts = target->u.specific->n.sym->ts;
4527 }
4a44a72d
DK
4528}
4529
4530
6de9cd9a
DN
4531/* This subroutine is called when an expression is being resolved.
4532 The expression node in question is either a user defined operator
1f2959f0 4533 or an intrinsic operator with arguments that aren't compatible
6de9cd9a
DN
4534 with the operator. This subroutine builds an actual argument list
4535 corresponding to the operands, then searches for a compatible
4536 interface. If one is found, the expression node is replaced with
eaee02a5
JW
4537 the appropriate function call. We use the 'match' enum to specify
4538 whether a replacement has been made or not, or if an error occurred. */
6de9cd9a 4539
eaee02a5
JW
4540match
4541gfc_extend_expr (gfc_expr *e)
6de9cd9a
DN
4542{
4543 gfc_actual_arglist *actual;
4544 gfc_symbol *sym;
4545 gfc_namespace *ns;
4546 gfc_user_op *uop;
4547 gfc_intrinsic_op i;
974df0f8 4548 const char *gname;
517d78be
JW
4549 gfc_typebound_proc* tbo;
4550 gfc_expr* tb_base;
6de9cd9a
DN
4551
4552 sym = NULL;
4553
4554 actual = gfc_get_actual_arglist ();
58b03ab2 4555 actual->expr = e->value.op.op1;
6de9cd9a 4556
974df0f8 4557 gname = NULL;
4a44a72d 4558
58b03ab2 4559 if (e->value.op.op2 != NULL)
6de9cd9a
DN
4560 {
4561 actual->next = gfc_get_actual_arglist ();
58b03ab2 4562 actual->next->expr = e->value.op.op2;
6de9cd9a
DN
4563 }
4564
e8d4f3fc 4565 i = fold_unary_intrinsic (e->value.op.op);
6de9cd9a 4566
517d78be
JW
4567 /* See if we find a matching type-bound operator. */
4568 if (i == INTRINSIC_USER)
4569 tbo = matching_typebound_op (&tb_base, actual,
4570 i, e->value.op.uop->name, &gname);
4571 else
4572 switch (i)
4573 {
4574#define CHECK_OS_COMPARISON(comp) \
4575 case INTRINSIC_##comp: \
4576 case INTRINSIC_##comp##_OS: \
4577 tbo = matching_typebound_op (&tb_base, actual, \
4578 INTRINSIC_##comp, NULL, &gname); \
4579 if (!tbo) \
4580 tbo = matching_typebound_op (&tb_base, actual, \
4581 INTRINSIC_##comp##_OS, NULL, &gname); \
4582 break;
4583 CHECK_OS_COMPARISON(EQ)
4584 CHECK_OS_COMPARISON(NE)
4585 CHECK_OS_COMPARISON(GT)
4586 CHECK_OS_COMPARISON(GE)
4587 CHECK_OS_COMPARISON(LT)
4588 CHECK_OS_COMPARISON(LE)
4589#undef CHECK_OS_COMPARISON
4590
4591 default:
4592 tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
4593 break;
4594 }
4595
4596 /* If there is a matching typebound-operator, replace the expression with
4597 a call to it and succeed. */
4598 if (tbo)
4599 {
4600 gcc_assert (tb_base);
4601 build_compcall_for_operator (e, actual, tb_base, tbo, gname);
4602
4603 if (!gfc_resolve_expr (e))
4604 return MATCH_ERROR;
4605 else
4606 return MATCH_YES;
4607 }
e73d3ca6 4608
6de9cd9a
DN
4609 if (i == INTRINSIC_USER)
4610 {
4611 for (ns = gfc_current_ns; ns; ns = ns->parent)
4612 {
58b03ab2 4613 uop = gfc_find_uop (e->value.op.uop->name, ns);
6de9cd9a
DN
4614 if (uop == NULL)
4615 continue;
4616
a1ee985f 4617 sym = gfc_search_interface (uop->op, 0, &actual);
6de9cd9a
DN
4618 if (sym != NULL)
4619 break;
4620 }
4621 }
4622 else
4623 {
4624 for (ns = gfc_current_ns; ns; ns = ns->parent)
4625 {
3bed9dd0
DF
4626 /* Due to the distinction between '==' and '.eq.' and friends, one has
4627 to check if either is defined. */
4628 switch (i)
4629 {
4a44a72d
DK
4630#define CHECK_OS_COMPARISON(comp) \
4631 case INTRINSIC_##comp: \
4632 case INTRINSIC_##comp##_OS: \
4633 sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
4634 if (!sym) \
4635 sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
4636 break;
4637 CHECK_OS_COMPARISON(EQ)
4638 CHECK_OS_COMPARISON(NE)
4639 CHECK_OS_COMPARISON(GT)
4640 CHECK_OS_COMPARISON(GE)
4641 CHECK_OS_COMPARISON(LT)
4642 CHECK_OS_COMPARISON(LE)
4643#undef CHECK_OS_COMPARISON
3bed9dd0
DF
4644
4645 default:
a1ee985f 4646 sym = gfc_search_interface (ns->op[i], 0, &actual);
3bed9dd0
DF
4647 }
4648
6de9cd9a
DN
4649 if (sym != NULL)
4650 break;
4651 }
4652 }
4653
4a44a72d
DK
4654 /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
4655 found rather than just taking the first one and not checking further. */
4656
6de9cd9a
DN
4657 if (sym == NULL)
4658 {
66e4ab31 4659 /* Don't use gfc_free_actual_arglist(). */
04695783 4660 free (actual->next);
cede9502 4661 free (actual);
eaee02a5 4662 return MATCH_NO;
6de9cd9a
DN
4663 }
4664
4665 /* Change the expression node to a function call. */
4666 e->expr_type = EXPR_FUNCTION;
f6fad28e 4667 e->symtree = gfc_find_sym_in_symtree (sym);
6de9cd9a 4668 e->value.function.actual = actual;
58b03ab2
TS
4669 e->value.function.esym = NULL;
4670 e->value.function.isym = NULL;
cf013e9f 4671 e->value.function.name = NULL;
a1ab6660 4672 e->user_operator = 1;
6de9cd9a 4673
524af0d6 4674 if (!gfc_resolve_expr (e))
eaee02a5 4675 return MATCH_ERROR;
6de9cd9a 4676
eaee02a5 4677 return MATCH_YES;
6de9cd9a
DN
4678}
4679
4680
4f7395ff
JW
4681/* Tries to replace an assignment code node with a subroutine call to the
4682 subroutine associated with the assignment operator. Return true if the node
4683 was replaced. On false, no error is generated. */
6de9cd9a 4684
524af0d6 4685bool
b251af97 4686gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
6de9cd9a
DN
4687{
4688 gfc_actual_arglist *actual;
4f7395ff
JW
4689 gfc_expr *lhs, *rhs, *tb_base;
4690 gfc_symbol *sym = NULL;
4691 const char *gname = NULL;
4692 gfc_typebound_proc* tbo;
6de9cd9a 4693
a513927a 4694 lhs = c->expr1;
6de9cd9a
DN
4695 rhs = c->expr2;
4696
8dc63166
SK
4697 /* Don't allow an intrinsic assignment with a BOZ rhs to be replaced. */
4698 if (c->op == EXEC_ASSIGN
4699 && c->expr1->expr_type == EXPR_VARIABLE
4700 && c->expr2->expr_type == EXPR_CONSTANT && c->expr2->ts.type == BT_BOZ)
4701 return false;
4702
6de9cd9a 4703 /* Don't allow an intrinsic assignment to be replaced. */
4b7dd692 4704 if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
e19bb186 4705 && (rhs->rank == 0 || rhs->rank == lhs->rank)
6de9cd9a 4706 && (lhs->ts.type == rhs->ts.type
b251af97 4707 || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
524af0d6 4708 return false;
6de9cd9a
DN
4709
4710 actual = gfc_get_actual_arglist ();
4711 actual->expr = lhs;
4712
4713 actual->next = gfc_get_actual_arglist ();
4714 actual->next->expr = rhs;
4715
4f7395ff
JW
4716 /* TODO: Ambiguity-check, see above for gfc_extend_expr. */
4717
4718 /* See if we find a matching type-bound assignment. */
4719 tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
4720 NULL, &gname);
4721
4722 if (tbo)
4723 {
4724 /* Success: Replace the expression with a type-bound call. */
4725 gcc_assert (tb_base);
4726 c->expr1 = gfc_get_expr ();
4727 build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
4728 c->expr1->value.compcall.assign = 1;
4729 c->expr1->where = c->loc;
4730 c->expr2 = NULL;
4731 c->op = EXEC_COMPCALL;
4732 return true;
4733 }
6de9cd9a 4734
4f7395ff 4735 /* See if we find an 'ordinary' (non-typebound) assignment procedure. */
6de9cd9a
DN
4736 for (; ns; ns = ns->parent)
4737 {
a1ee985f 4738 sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
6de9cd9a
DN
4739 if (sym != NULL)
4740 break;
4741 }
4742
4f7395ff 4743 if (sym)
6de9cd9a 4744 {
4f7395ff
JW
4745 /* Success: Replace the assignment with the call. */
4746 c->op = EXEC_ASSIGN_CALL;
4747 c->symtree = gfc_find_sym_in_symtree (sym);
4748 c->expr1 = NULL;
4749 c->expr2 = NULL;
4750 c->ext.actual = actual;
4751 return true;
6de9cd9a
DN
4752 }
4753
4f7395ff
JW
4754 /* Failure: No assignment procedure found. */
4755 free (actual->next);
4756 free (actual);
4757 return false;
6de9cd9a
DN
4758}
4759
4760
4761/* Make sure that the interface just parsed is not already present in
4762 the given interface list. Ambiguity isn't checked yet since module
4763 procedures can be present without interfaces. */
4764
524af0d6 4765bool
362aa474 4766gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
6de9cd9a
DN
4767{
4768 gfc_interface *ip;
4769
4770 for (ip = base; ip; ip = ip->next)
4771 {
7b901ac4 4772 if (ip->sym == new_sym)
6de9cd9a 4773 {
c4100eae 4774 gfc_error ("Entity %qs at %L is already present in the interface",
362aa474 4775 new_sym->name, &loc);
524af0d6 4776 return false;
6de9cd9a
DN
4777 }
4778 }
4779
524af0d6 4780 return true;
6de9cd9a
DN
4781}
4782
4783
4784/* Add a symbol to the current interface. */
4785
524af0d6 4786bool
7b901ac4 4787gfc_add_interface (gfc_symbol *new_sym)
6de9cd9a
DN
4788{
4789 gfc_interface **head, *intr;
4790 gfc_namespace *ns;
4791 gfc_symbol *sym;
4792
4793 switch (current_interface.type)
4794 {
4795 case INTERFACE_NAMELESS:
9e1d712c 4796 case INTERFACE_ABSTRACT:
524af0d6 4797 return true;
6de9cd9a
DN
4798
4799 case INTERFACE_INTRINSIC_OP:
4800 for (ns = current_interface.ns; ns; ns = ns->parent)
3bed9dd0
DF
4801 switch (current_interface.op)
4802 {
4803 case INTRINSIC_EQ:
4804 case INTRINSIC_EQ_OS:
e73d3ca6 4805 if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym,
524af0d6 4806 gfc_current_locus)
e73d3ca6 4807 || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS],
524af0d6
JB
4808 new_sym, gfc_current_locus))
4809 return false;
3bed9dd0
DF
4810 break;
4811
4812 case INTRINSIC_NE:
4813 case INTRINSIC_NE_OS:
e73d3ca6 4814 if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym,
524af0d6 4815 gfc_current_locus)
e73d3ca6 4816 || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS],
524af0d6
JB
4817 new_sym, gfc_current_locus))
4818 return false;
3bed9dd0
DF
4819 break;
4820
4821 case INTRINSIC_GT:
4822 case INTRINSIC_GT_OS:
e73d3ca6 4823 if (!gfc_check_new_interface (ns->op[INTRINSIC_GT],
524af0d6 4824 new_sym, gfc_current_locus)
e73d3ca6 4825 || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS],
524af0d6
JB
4826 new_sym, gfc_current_locus))
4827 return false;
3bed9dd0
DF
4828 break;
4829
4830 case INTRINSIC_GE:
4831 case INTRINSIC_GE_OS:
e73d3ca6 4832 if (!gfc_check_new_interface (ns->op[INTRINSIC_GE],
524af0d6 4833 new_sym, gfc_current_locus)
e73d3ca6 4834 || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS],
524af0d6
JB
4835 new_sym, gfc_current_locus))
4836 return false;
3bed9dd0
DF
4837 break;
4838
4839 case INTRINSIC_LT:
4840 case INTRINSIC_LT_OS:
e73d3ca6 4841 if (!gfc_check_new_interface (ns->op[INTRINSIC_LT],
524af0d6 4842 new_sym, gfc_current_locus)
e73d3ca6 4843 || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS],
524af0d6
JB
4844 new_sym, gfc_current_locus))
4845 return false;
3bed9dd0
DF
4846 break;
4847
4848 case INTRINSIC_LE:
4849 case INTRINSIC_LE_OS:
e73d3ca6 4850 if (!gfc_check_new_interface (ns->op[INTRINSIC_LE],
524af0d6 4851 new_sym, gfc_current_locus)
e73d3ca6 4852 || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS],
524af0d6
JB
4853 new_sym, gfc_current_locus))
4854 return false;
3bed9dd0
DF
4855 break;
4856
4857 default:
e73d3ca6 4858 if (!gfc_check_new_interface (ns->op[current_interface.op],
524af0d6
JB
4859 new_sym, gfc_current_locus))
4860 return false;
3bed9dd0 4861 }
6de9cd9a 4862
a1ee985f 4863 head = &current_interface.ns->op[current_interface.op];
6de9cd9a
DN
4864 break;
4865
4866 case INTERFACE_GENERIC:
e73d3ca6 4867 case INTERFACE_DTIO:
6de9cd9a
DN
4868 for (ns = current_interface.ns; ns; ns = ns->parent)
4869 {
4870 gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
4871 if (sym == NULL)
4872 continue;
4873
e73d3ca6 4874 if (!gfc_check_new_interface (sym->generic,
524af0d6
JB
4875 new_sym, gfc_current_locus))
4876 return false;
6de9cd9a
DN
4877 }
4878
4879 head = &current_interface.sym->generic;
4880 break;
4881
4882 case INTERFACE_USER_OP:
e73d3ca6 4883 if (!gfc_check_new_interface (current_interface.uop->op,
524af0d6
JB
4884 new_sym, gfc_current_locus))
4885 return false;
6de9cd9a 4886
a1ee985f 4887 head = &current_interface.uop->op;
6de9cd9a
DN
4888 break;
4889
4890 default:
4891 gfc_internal_error ("gfc_add_interface(): Bad interface type");
4892 }
4893
4894 intr = gfc_get_interface ();
7b901ac4 4895 intr->sym = new_sym;
63645982 4896 intr->where = gfc_current_locus;
6de9cd9a
DN
4897
4898 intr->next = *head;
4899 *head = intr;
4900
524af0d6 4901 return true;
6de9cd9a
DN
4902}
4903
4904
2b77e908
FXC
4905gfc_interface *
4906gfc_current_interface_head (void)
4907{
4908 switch (current_interface.type)
4909 {
4910 case INTERFACE_INTRINSIC_OP:
a1ee985f 4911 return current_interface.ns->op[current_interface.op];
2b77e908
FXC
4912
4913 case INTERFACE_GENERIC:
e73d3ca6 4914 case INTERFACE_DTIO:
2b77e908 4915 return current_interface.sym->generic;
2b77e908
FXC
4916
4917 case INTERFACE_USER_OP:
a1ee985f 4918 return current_interface.uop->op;
2b77e908
FXC
4919
4920 default:
4921 gcc_unreachable ();
4922 }
4923}
4924
4925
4926void
4927gfc_set_current_interface_head (gfc_interface *i)
4928{
4929 switch (current_interface.type)
4930 {
4931 case INTERFACE_INTRINSIC_OP:
a1ee985f 4932 current_interface.ns->op[current_interface.op] = i;
2b77e908
FXC
4933 break;
4934
4935 case INTERFACE_GENERIC:
e73d3ca6 4936 case INTERFACE_DTIO:
2b77e908
FXC
4937 current_interface.sym->generic = i;
4938 break;
4939
4940 case INTERFACE_USER_OP:
a1ee985f 4941 current_interface.uop->op = i;
2b77e908
FXC
4942 break;
4943
4944 default:
4945 gcc_unreachable ();
4946 }
4947}
4948
4949
6de9cd9a
DN
4950/* Gets rid of a formal argument list. We do not free symbols.
4951 Symbols are freed when a namespace is freed. */
4952
4953void
b251af97 4954gfc_free_formal_arglist (gfc_formal_arglist *p)
6de9cd9a
DN
4955{
4956 gfc_formal_arglist *q;
4957
4958 for (; p; p = q)
4959 {
4960 q = p->next;
cede9502 4961 free (p);
6de9cd9a
DN
4962 }
4963}
99fc1b90
JW
4964
4965
9795c594
JW
4966/* Check that it is ok for the type-bound procedure 'proc' to override the
4967 procedure 'old', cf. F08:4.5.7.3. */
99fc1b90 4968
524af0d6 4969bool
99fc1b90
JW
4970gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
4971{
4972 locus where;
edc802c7 4973 gfc_symbol *proc_target, *old_target;
99fc1b90 4974 unsigned proc_pass_arg, old_pass_arg, argpos;
9795c594
JW
4975 gfc_formal_arglist *proc_formal, *old_formal;
4976 bool check_type;
4977 char err[200];
99fc1b90
JW
4978
4979 /* This procedure should only be called for non-GENERIC proc. */
4980 gcc_assert (!proc->n.tb->is_generic);
4981
4982 /* If the overwritten procedure is GENERIC, this is an error. */
4983 if (old->n.tb->is_generic)
4984 {
1fe61adf 4985 gfc_error ("Cannot overwrite GENERIC %qs at %L",
99fc1b90 4986 old->name, &proc->n.tb->where);
524af0d6 4987 return false;
99fc1b90
JW
4988 }
4989
4990 where = proc->n.tb->where;
4991 proc_target = proc->n.tb->u.specific->n.sym;
4992 old_target = old->n.tb->u.specific->n.sym;
4993
4994 /* Check that overridden binding is not NON_OVERRIDABLE. */
4995 if (old->n.tb->non_overridable)
4996 {
c4100eae 4997 gfc_error ("%qs at %L overrides a procedure binding declared"
99fc1b90 4998 " NON_OVERRIDABLE", proc->name, &where);
524af0d6 4999 return false;
99fc1b90
JW
5000 }
5001
5002 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
5003 if (!old->n.tb->deferred && proc->n.tb->deferred)
5004 {
c4100eae 5005 gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
99fc1b90 5006 " non-DEFERRED binding", proc->name, &where);
524af0d6 5007 return false;
99fc1b90
JW
5008 }
5009
5010 /* If the overridden binding is PURE, the overriding must be, too. */
5011 if (old_target->attr.pure && !proc_target->attr.pure)
5012 {
c4100eae 5013 gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
99fc1b90 5014 proc->name, &where);
524af0d6 5015 return false;
99fc1b90
JW
5016 }
5017
5018 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
5019 is not, the overriding must not be either. */
5020 if (old_target->attr.elemental && !proc_target->attr.elemental)
5021 {
c4100eae 5022 gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
99fc1b90 5023 " ELEMENTAL", proc->name, &where);
524af0d6 5024 return false;
99fc1b90
JW
5025 }
5026 if (!old_target->attr.elemental && proc_target->attr.elemental)
5027 {
c4100eae 5028 gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
99fc1b90 5029 " be ELEMENTAL, either", proc->name, &where);
524af0d6 5030 return false;
99fc1b90
JW
5031 }
5032
5033 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
5034 SUBROUTINE. */
5035 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
5036 {
c4100eae 5037 gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
99fc1b90 5038 " SUBROUTINE", proc->name, &where);
524af0d6 5039 return false;
99fc1b90
JW
5040 }
5041
5042 /* If the overridden binding is a FUNCTION, the overriding must also be a
5043 FUNCTION and have the same characteristics. */
5044 if (old_target->attr.function)
5045 {
5046 if (!proc_target->attr.function)
5047 {
c4100eae 5048 gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
99fc1b90 5049 " FUNCTION", proc->name, &where);
524af0d6 5050 return false;
99fc1b90 5051 }
8b704316 5052
4668d6f9
PT
5053 if (!gfc_check_result_characteristics (proc_target, old_target,
5054 err, sizeof(err)))
2240d1cf 5055 {
edc802c7 5056 gfc_error ("Result mismatch for the overriding procedure "
c4100eae 5057 "%qs at %L: %s", proc->name, &where, err);
524af0d6 5058 return false;
2240d1cf 5059 }
99fc1b90
JW
5060 }
5061
5062 /* If the overridden binding is PUBLIC, the overriding one must not be
5063 PRIVATE. */
5064 if (old->n.tb->access == ACCESS_PUBLIC
5065 && proc->n.tb->access == ACCESS_PRIVATE)
5066 {
c4100eae 5067 gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
99fc1b90 5068 " PRIVATE", proc->name, &where);
524af0d6 5069 return false;
99fc1b90
JW
5070 }
5071
5072 /* Compare the formal argument lists of both procedures. This is also abused
5073 to find the position of the passed-object dummy arguments of both
5074 bindings as at least the overridden one might not yet be resolved and we
5075 need those positions in the check below. */
5076 proc_pass_arg = old_pass_arg = 0;
5077 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
5078 proc_pass_arg = 1;
5079 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
5080 old_pass_arg = 1;
5081 argpos = 1;
4cbc9039
JW
5082 proc_formal = gfc_sym_get_dummy_args (proc_target);
5083 old_formal = gfc_sym_get_dummy_args (old_target);
5084 for ( ; proc_formal && old_formal;
99fc1b90
JW
5085 proc_formal = proc_formal->next, old_formal = old_formal->next)
5086 {
5087 if (proc->n.tb->pass_arg
5088 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
5089 proc_pass_arg = argpos;
5090 if (old->n.tb->pass_arg
5091 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
5092 old_pass_arg = argpos;
5093
5094 /* Check that the names correspond. */
5095 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
5096 {
c4100eae 5097 gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
99fc1b90
JW
5098 " to match the corresponding argument of the overridden"
5099 " procedure", proc_formal->sym->name, proc->name, &where,
5100 old_formal->sym->name);
524af0d6 5101 return false;
99fc1b90
JW
5102 }
5103
9795c594 5104 check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
4668d6f9 5105 if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
524af0d6 5106 check_type, err, sizeof(err)))
99fc1b90 5107 {
e0b9e5f9 5108 gfc_error_opt (0, "Argument mismatch for the overriding procedure "
2700d0e3 5109 "%qs at %L: %s", proc->name, &where, err);
524af0d6 5110 return false;
99fc1b90
JW
5111 }
5112
5113 ++argpos;
5114 }
5115 if (proc_formal || old_formal)
5116 {
c4100eae 5117 gfc_error ("%qs at %L must have the same number of formal arguments as"
99fc1b90 5118 " the overridden procedure", proc->name, &where);
524af0d6 5119 return false;
99fc1b90
JW
5120 }
5121
5122 /* If the overridden binding is NOPASS, the overriding one must also be
5123 NOPASS. */
5124 if (old->n.tb->nopass && !proc->n.tb->nopass)
5125 {
c4100eae 5126 gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
99fc1b90 5127 " NOPASS", proc->name, &where);
524af0d6 5128 return false;
99fc1b90
JW
5129 }
5130
5131 /* If the overridden binding is PASS(x), the overriding one must also be
5132 PASS and the passed-object dummy arguments must correspond. */
5133 if (!old->n.tb->nopass)
5134 {
5135 if (proc->n.tb->nopass)
5136 {
c4100eae 5137 gfc_error ("%qs at %L overrides a binding with PASS and must also be"
99fc1b90 5138 " PASS", proc->name, &where);
524af0d6 5139 return false;
99fc1b90
JW
5140 }
5141
5142 if (proc_pass_arg != old_pass_arg)
5143 {
c4100eae 5144 gfc_error ("Passed-object dummy argument of %qs at %L must be at"
99fc1b90
JW
5145 " the same position as the passed-object dummy argument of"
5146 " the overridden procedure", proc->name, &where);
524af0d6 5147 return false;
99fc1b90
JW
5148 }
5149 }
5150
524af0d6 5151 return true;
99fc1b90 5152}
e73d3ca6
PT
5153
5154
5155/* The following three functions check that the formal arguments
5156 of user defined derived type IO procedures are compliant with
f3ad8745 5157 the requirements of the standard, see F03:9.5.3.7.2 (F08:9.6.4.8.3). */
e73d3ca6
PT
5158
5159static void
5160check_dtio_arg_TKR_intent (gfc_symbol *fsym, bool typebound, bt type,
5161 int kind, int rank, sym_intent intent)
5162{
5163 if (fsym->ts.type != type)
739d9339
PT
5164 {
5165 gfc_error ("DTIO dummy argument at %L must be of type %s",
5166 &fsym->declared_at, gfc_basic_typename (type));
5167 return;
5168 }
e73d3ca6
PT
5169
5170 if (fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED
5171 && fsym->ts.kind != kind)
5172 gfc_error ("DTIO dummy argument at %L must be of KIND = %d",
5173 &fsym->declared_at, kind);
5174
5175 if (!typebound
5176 && rank == 0
5177 && (((type == BT_CLASS) && CLASS_DATA (fsym)->attr.dimension)
5178 || ((type != BT_CLASS) && fsym->attr.dimension)))
b93a9a15 5179 gfc_error ("DTIO dummy argument at %L must be a scalar",
e73d3ca6
PT
5180 &fsym->declared_at);
5181 else if (rank == 1
5182 && (fsym->as == NULL || fsym->as->type != AS_ASSUMED_SHAPE))
5183 gfc_error ("DTIO dummy argument at %L must be an "
5184 "ASSUMED SHAPE ARRAY", &fsym->declared_at);
5185
f3ad8745
JW
5186 if (type == BT_CHARACTER && fsym->ts.u.cl->length != NULL)
5187 gfc_error ("DTIO character argument at %L must have assumed length",
5188 &fsym->declared_at);
5189
e73d3ca6 5190 if (fsym->attr.intent != intent)
77be9417 5191 gfc_error ("DTIO dummy argument at %L must have INTENT %s",
e73d3ca6
PT
5192 &fsym->declared_at, gfc_code2string (intents, (int)intent));
5193 return;
5194}
5195
5196
5197static void
5198check_dtio_interface1 (gfc_symbol *derived, gfc_symtree *tb_io_st,
5199 bool typebound, bool formatted, int code)
5200{
5201 gfc_symbol *dtio_sub, *generic_proc, *fsym;
5202 gfc_typebound_proc *tb_io_proc, *specific_proc;
5203 gfc_interface *intr;
5204 gfc_formal_arglist *formal;
5205 int arg_num;
5206
5207 bool read = ((dtio_codes)code == DTIO_RF)
5208 || ((dtio_codes)code == DTIO_RUF);
5209 bt type;
5210 sym_intent intent;
5211 int kind;
5212
5213 dtio_sub = NULL;
5214 if (typebound)
5215 {
5216 /* Typebound DTIO binding. */
5217 tb_io_proc = tb_io_st->n.tb;
739d9339
PT
5218 if (tb_io_proc == NULL)
5219 return;
5220
e73d3ca6 5221 gcc_assert (tb_io_proc->is_generic);
e73d3ca6
PT
5222
5223 specific_proc = tb_io_proc->u.generic->specific;
739d9339
PT
5224 if (specific_proc == NULL || specific_proc->is_generic)
5225 return;
e73d3ca6
PT
5226
5227 dtio_sub = specific_proc->u.specific->n.sym;
5228 }
5229 else
5230 {
5231 generic_proc = tb_io_st->n.sym;
739d9339
PT
5232 if (generic_proc == NULL || generic_proc->generic == NULL)
5233 return;
e73d3ca6
PT
5234
5235 for (intr = tb_io_st->n.sym->generic; intr; intr = intr->next)
5236 {
a8de3002 5237 if (intr->sym && intr->sym->formal && intr->sym->formal->sym
e73d3ca6
PT
5238 && ((intr->sym->formal->sym->ts.type == BT_CLASS
5239 && CLASS_DATA (intr->sym->formal->sym)->ts.u.derived
5240 == derived)
5241 || (intr->sym->formal->sym->ts.type == BT_DERIVED
5242 && intr->sym->formal->sym->ts.u.derived == derived)))
5243 {
5244 dtio_sub = intr->sym;
5245 break;
5246 }
a8de3002
PT
5247 else if (intr->sym && intr->sym->formal && !intr->sym->formal->sym)
5248 {
5249 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5250 "procedure", &intr->sym->declared_at);
5251 return;
5252 }
e73d3ca6
PT
5253 }
5254
5255 if (dtio_sub == NULL)
5256 return;
5257 }
5258
5259 gcc_assert (dtio_sub);
5260 if (!dtio_sub->attr.subroutine)
2f029c08 5261 gfc_error ("DTIO procedure %qs at %L must be a subroutine",
e73d3ca6
PT
5262 dtio_sub->name, &dtio_sub->declared_at);
5263
dbeaa7ab 5264 if (!dtio_sub->resolve_symbol_called)
3ab216a4
TB
5265 gfc_resolve_formal_arglist (dtio_sub);
5266
a8de3002
PT
5267 arg_num = 0;
5268 for (formal = dtio_sub->formal; formal; formal = formal->next)
5269 arg_num++;
5270
5271 if (arg_num < (formatted ? 6 : 4))
5272 {
2f029c08 5273 gfc_error ("Too few dummy arguments in DTIO procedure %qs at %L",
a8de3002
PT
5274 dtio_sub->name, &dtio_sub->declared_at);
5275 return;
5276 }
5277
5278 if (arg_num > (formatted ? 6 : 4))
5279 {
2f029c08 5280 gfc_error ("Too many dummy arguments in DTIO procedure %qs at %L",
a8de3002
PT
5281 dtio_sub->name, &dtio_sub->declared_at);
5282 return;
5283 }
5284
e73d3ca6
PT
5285 /* Now go through the formal arglist. */
5286 arg_num = 1;
5287 for (formal = dtio_sub->formal; formal; formal = formal->next, arg_num++)
5288 {
5289 if (!formatted && arg_num == 3)
5290 arg_num = 5;
5291 fsym = formal->sym;
a8de3002
PT
5292
5293 if (fsym == NULL)
5294 {
5295 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5296 "procedure", &dtio_sub->declared_at);
5297 return;
5298 }
5299
e73d3ca6
PT
5300 switch (arg_num)
5301 {
5302 case(1): /* DTV */
5303 type = derived->attr.sequence || derived->attr.is_bind_c ?
5304 BT_DERIVED : BT_CLASS;
5305 kind = 0;
5306 intent = read ? INTENT_INOUT : INTENT_IN;
5307 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5308 0, intent);
5309 break;
5310
5311 case(2): /* UNIT */
5312 type = BT_INTEGER;
5313 kind = gfc_default_integer_kind;
5314 intent = INTENT_IN;
5315 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5316 0, intent);
5317 break;
5318 case(3): /* IOTYPE */
5319 type = BT_CHARACTER;
5320 kind = gfc_default_character_kind;
5321 intent = INTENT_IN;
5322 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5323 0, intent);
5324 break;
5325 case(4): /* VLIST */
5326 type = BT_INTEGER;
5327 kind = gfc_default_integer_kind;
5328 intent = INTENT_IN;
5329 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5330 1, intent);
5331 break;
5332 case(5): /* IOSTAT */
5333 type = BT_INTEGER;
5334 kind = gfc_default_integer_kind;
5335 intent = INTENT_OUT;
5336 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5337 0, intent);
5338 break;
5339 case(6): /* IOMSG */
5340 type = BT_CHARACTER;
5341 kind = gfc_default_character_kind;
5342 intent = INTENT_INOUT;
5343 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5344 0, intent);
5345 break;
5346 default:
5347 gcc_unreachable ();
5348 }
5349 }
5350 derived->attr.has_dtio_procs = 1;
5351 return;
5352}
5353
5354void
5355gfc_check_dtio_interfaces (gfc_symbol *derived)
5356{
5357 gfc_symtree *tb_io_st;
5358 bool t = false;
5359 int code;
5360 bool formatted;
5361
5362 if (derived->attr.is_class == 1 || derived->attr.vtype == 1)
5363 return;
5364
5365 /* Check typebound DTIO bindings. */
5366 for (code = 0; code < 4; code++)
5367 {
5368 formatted = ((dtio_codes)code == DTIO_RF)
5369 || ((dtio_codes)code == DTIO_WF);
5370
5371 tb_io_st = gfc_find_typebound_proc (derived, &t,
5372 gfc_code2string (dtio_procs, code),
5373 true, &derived->declared_at);
5374 if (tb_io_st != NULL)
5375 check_dtio_interface1 (derived, tb_io_st, true, formatted, code);
5376 }
5377
5378 /* Check generic DTIO interfaces. */
5379 for (code = 0; code < 4; code++)
5380 {
5381 formatted = ((dtio_codes)code == DTIO_RF)
5382 || ((dtio_codes)code == DTIO_WF);
5383
5384 tb_io_st = gfc_find_symtree (derived->ns->sym_root,
5385 gfc_code2string (dtio_procs, code));
5386 if (tb_io_st != NULL)
5387 check_dtio_interface1 (derived, tb_io_st, false, formatted, code);
5388 }
5389}
5390
5391
e4e659b9
JW
5392gfc_symtree*
5393gfc_find_typebound_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
e73d3ca6
PT
5394{
5395 gfc_symtree *tb_io_st = NULL;
e73d3ca6
PT
5396 bool t = false;
5397
dbeaa7ab
ME
5398 if (!derived || !derived->resolve_symbol_called
5399 || derived->attr.flavor != FL_DERIVED)
9beb81ed
PT
5400 return NULL;
5401
e73d3ca6
PT
5402 /* Try to find a typebound DTIO binding. */
5403 if (formatted == true)
5404 {
5405 if (write == true)
5406 tb_io_st = gfc_find_typebound_proc (derived, &t,
5407 gfc_code2string (dtio_procs,
5408 DTIO_WF),
5409 true,
5410 &derived->declared_at);
5411 else
5412 tb_io_st = gfc_find_typebound_proc (derived, &t,
5413 gfc_code2string (dtio_procs,
5414 DTIO_RF),
5415 true,
5416 &derived->declared_at);
5417 }
5418 else
5419 {
5420 if (write == true)
5421 tb_io_st = gfc_find_typebound_proc (derived, &t,
5422 gfc_code2string (dtio_procs,
5423 DTIO_WUF),
5424 true,
5425 &derived->declared_at);
5426 else
5427 tb_io_st = gfc_find_typebound_proc (derived, &t,
5428 gfc_code2string (dtio_procs,
5429 DTIO_RUF),
5430 true,
5431 &derived->declared_at);
5432 }
e4e659b9
JW
5433 return tb_io_st;
5434}
5435
5436
5437gfc_symbol *
5438gfc_find_specific_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5439{
5440 gfc_symtree *tb_io_st = NULL;
5441 gfc_symbol *dtio_sub = NULL;
5442 gfc_symbol *extended;
5443 gfc_typebound_proc *tb_io_proc, *specific_proc;
5444
5445 tb_io_st = gfc_find_typebound_dtio_proc (derived, write, formatted);
e73d3ca6
PT
5446
5447 if (tb_io_st != NULL)
5448 {
096506bb
PT
5449 const char *genname;
5450 gfc_symtree *st;
5451
e73d3ca6
PT
5452 tb_io_proc = tb_io_st->n.tb;
5453 gcc_assert (tb_io_proc != NULL);
5454 gcc_assert (tb_io_proc->is_generic);
5455 gcc_assert (tb_io_proc->u.generic->next == NULL);
5456
5457 specific_proc = tb_io_proc->u.generic->specific;
5458 gcc_assert (!specific_proc->is_generic);
5459
096506bb
PT
5460 /* Go back and make sure that we have the right specific procedure.
5461 Here we most likely have a procedure from the parent type, which
5462 can be overridden in extensions. */
5463 genname = tb_io_proc->u.generic->specific_st->name;
5464 st = gfc_find_typebound_proc (derived, NULL, genname,
5465 true, &tb_io_proc->where);
5466 if (st)
5467 dtio_sub = st->n.tb->u.specific->n.sym;
5468 else
5469 dtio_sub = specific_proc->u.specific->n.sym;
e73d3ca6 5470
e4e659b9
JW
5471 goto finish;
5472 }
e73d3ca6
PT
5473
5474 /* If there is not a typebound binding, look for a generic
5475 DTIO interface. */
5476 for (extended = derived; extended;
5477 extended = gfc_get_derived_super_type (extended))
5478 {
e4e659b9
JW
5479 if (extended == NULL || extended->ns == NULL
5480 || extended->attr.flavor == FL_UNKNOWN)
a8de3002
PT
5481 return NULL;
5482
e73d3ca6
PT
5483 if (formatted == true)
5484 {
5485 if (write == true)
5486 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5487 gfc_code2string (dtio_procs,
5488 DTIO_WF));
5489 else
5490 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5491 gfc_code2string (dtio_procs,
5492 DTIO_RF));
5493 }
5494 else
5495 {
5496 if (write == true)
5497 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5498 gfc_code2string (dtio_procs,
5499 DTIO_WUF));
5500 else
5501 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5502 gfc_code2string (dtio_procs,
5503 DTIO_RUF));
5504 }
5505
5506 if (tb_io_st != NULL
5507 && tb_io_st->n.sym
5508 && tb_io_st->n.sym->generic)
5509 {
40109581 5510 for (gfc_interface *intr = tb_io_st->n.sym->generic;
413e859c 5511 intr && intr->sym; intr = intr->next)
e73d3ca6 5512 {
413e859c 5513 if (intr->sym->formal)
e73d3ca6 5514 {
413e859c
JW
5515 gfc_symbol *fsym = intr->sym->formal->sym;
5516 if ((fsym->ts.type == BT_CLASS
5517 && CLASS_DATA (fsym)->ts.u.derived == extended)
5518 || (fsym->ts.type == BT_DERIVED
5519 && fsym->ts.u.derived == extended))
5520 {
5521 dtio_sub = intr->sym;
5522 break;
5523 }
e73d3ca6
PT
5524 }
5525 }
5526 }
5527 }
5528
5529finish:
72d91d6c
TB
5530 if (dtio_sub
5531 && dtio_sub->formal->sym->ts.type == BT_CLASS
5532 && derived != CLASS_DATA (dtio_sub->formal->sym)->ts.u.derived)
e73d3ca6
PT
5533 gfc_find_derived_vtab (derived);
5534
5535 return dtio_sub;
5536}
e68a35ae
TK
5537
5538/* Helper function - if we do not find an interface for a procedure,
5539 construct it from the actual arglist. Luckily, this can only
5540 happen for call by reference, so the information we actually need
5541 to provide (and which would be impossible to guess from the call
5542 itself) is not actually needed. */
5543
5544void
5545gfc_get_formal_from_actual_arglist (gfc_symbol *sym,
5546 gfc_actual_arglist *actual_args)
5547{
5548 gfc_actual_arglist *a;
5549 gfc_formal_arglist **f;
5550 gfc_symbol *s;
5551 char name[GFC_MAX_SYMBOL_LEN + 1];
5552 static int var_num;
5553
5554 f = &sym->formal;
5555 for (a = actual_args; a != NULL; a = a->next)
5556 {
5557 (*f) = gfc_get_formal_arglist ();
5558 if (a->expr)
5559 {
5560 snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
5561 gfc_get_symbol (name, gfc_current_ns, &s);
5562 if (a->expr->ts.type == BT_PROCEDURE)
5563 {
5564 s->attr.flavor = FL_PROCEDURE;
5565 }
5566 else
5567 {
5568 s->ts = a->expr->ts;
5569
5570 if (s->ts.type == BT_CHARACTER)
5571 s->ts.u.cl = gfc_get_charlen ();
5572
5573 s->ts.deferred = 0;
5574 s->ts.is_iso_c = 0;
5575 s->ts.is_c_interop = 0;
5576 s->attr.flavor = FL_VARIABLE;
e68a35ae
TK
5577 if (a->expr->rank > 0)
5578 {
5579 s->attr.dimension = 1;
5580 s->as = gfc_get_array_spec ();
5581 s->as->rank = 1;
5582 s->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5583 &a->expr->where, 1);
5584 s->as->upper[0] = NULL;
5585 s->as->type = AS_ASSUMED_SIZE;
5586 }
4a4fc7fe
TK
5587 else
5588 s->maybe_array = maybe_dummy_array_arg (a->expr);
e68a35ae
TK
5589 }
5590 s->attr.dummy = 1;
3b0e49a5 5591 s->attr.artificial = 1;
e0b9e5f9 5592 s->declared_at = a->expr->where;
e68a35ae
TK
5593 s->attr.intent = INTENT_UNKNOWN;
5594 (*f)->sym = s;
5595 }
5596 else /* If a->expr is NULL, this is an alternate rerturn. */
5597 (*f)->sym = NULL;
5598
5599 f = &((*f)->next);
5600 }
5601}
5d9d16db
MM
5602
5603
48a8c5be
MM
5604const char *
5605gfc_dummy_arg_get_name (gfc_dummy_arg & dummy_arg)
5606{
5607 switch (dummy_arg.intrinsicness)
5608 {
5609 case GFC_INTRINSIC_DUMMY_ARG:
5610 return dummy_arg.u.intrinsic->name;
5611
5612 case GFC_NON_INTRINSIC_DUMMY_ARG:
5613 return dummy_arg.u.non_intrinsic->sym->name;
5614
5615 default:
5616 gcc_unreachable ();
5617 }
5618}
5619
5620
5d9d16db
MM
5621const gfc_typespec &
5622gfc_dummy_arg_get_typespec (gfc_dummy_arg & dummy_arg)
5623{
5624 switch (dummy_arg.intrinsicness)
5625 {
5626 case GFC_INTRINSIC_DUMMY_ARG:
5627 return dummy_arg.u.intrinsic->ts;
5628
5629 case GFC_NON_INTRINSIC_DUMMY_ARG:
5630 return dummy_arg.u.non_intrinsic->sym->ts;
5631
5632 default:
5633 gcc_unreachable ();
5634 }
5635}
5636
5637
5638bool
5639gfc_dummy_arg_is_optional (gfc_dummy_arg & dummy_arg)
5640{
5641 switch (dummy_arg.intrinsicness)
5642 {
5643 case GFC_INTRINSIC_DUMMY_ARG:
5644 return dummy_arg.u.intrinsic->optional;
5645
5646 case GFC_NON_INTRINSIC_DUMMY_ARG:
5647 return dummy_arg.u.non_intrinsic->sym->attr.optional;
5648
5649 default:
5650 gcc_unreachable ();
5651 }
5652}