]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/interface.cc
c++: P2448 - Relaxing some constexpr restrictions [PR106649]
[thirdparty/gcc.git] / gcc / fortran / interface.cc
CommitLineData
6de9cd9a 1/* Deal with interfaces.
7adcbafe 2 Copyright (C) 2000-2022 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
9795c594 1337 /* Check type and rank. */
e7333b69 1338 if (type_must_agree)
9795c594 1339 {
0ce0e6e8
JW
1340 if (!compare_type_characteristics (s1, s2)
1341 || !compare_type_characteristics (s2, s1))
e7333b69
JW
1342 {
1343 snprintf (errmsg, err_len, "Type mismatch in argument '%s' (%s/%s)",
f61e54e5
ME
1344 s1->name, gfc_dummy_typename (&s1->ts),
1345 gfc_dummy_typename (&s2->ts));
e7333b69
JW
1346 return false;
1347 }
1348 if (!compare_rank (s1, s2))
1349 {
1350 snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
1351 s1->name, symbol_rank (s1), symbol_rank (s2));
1352 return false;
1353 }
9795c594
JW
1354 }
1355
1356 /* Check INTENT. */
90efb251
TK
1357 if (s1->attr.intent != s2->attr.intent && !s1->attr.artificial
1358 && !s2->attr.artificial)
9795c594
JW
1359 {
1360 snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
1361 s1->name);
524af0d6 1362 return false;
9795c594
JW
1363 }
1364
1365 /* Check OPTIONAL attribute. */
1366 if (s1->attr.optional != s2->attr.optional)
1367 {
1368 snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
1369 s1->name);
524af0d6 1370 return false;
9795c594
JW
1371 }
1372
1373 /* Check ALLOCATABLE attribute. */
1374 if (s1->attr.allocatable != s2->attr.allocatable)
1375 {
1376 snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
1377 s1->name);
524af0d6 1378 return false;
9795c594
JW
1379 }
1380
1381 /* Check POINTER attribute. */
1382 if (s1->attr.pointer != s2->attr.pointer)
1383 {
1384 snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
1385 s1->name);
524af0d6 1386 return false;
9795c594
JW
1387 }
1388
1389 /* Check TARGET attribute. */
1390 if (s1->attr.target != s2->attr.target)
1391 {
1392 snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
1393 s1->name);
524af0d6 1394 return false;
9795c594
JW
1395 }
1396
688974a3
JW
1397 /* Check ASYNCHRONOUS attribute. */
1398 if (s1->attr.asynchronous != s2->attr.asynchronous)
1399 {
1400 snprintf (errmsg, err_len, "ASYNCHRONOUS mismatch in argument '%s'",
1401 s1->name);
1402 return false;
1403 }
1404
1405 /* Check CONTIGUOUS attribute. */
1406 if (s1->attr.contiguous != s2->attr.contiguous)
1407 {
1408 snprintf (errmsg, err_len, "CONTIGUOUS mismatch in argument '%s'",
1409 s1->name);
1410 return false;
1411 }
1412
1413 /* Check VALUE attribute. */
1414 if (s1->attr.value != s2->attr.value)
1415 {
1416 snprintf (errmsg, err_len, "VALUE mismatch in argument '%s'",
1417 s1->name);
1418 return false;
1419 }
1420
1421 /* Check VOLATILE attribute. */
1422 if (s1->attr.volatile_ != s2->attr.volatile_)
1423 {
1424 snprintf (errmsg, err_len, "VOLATILE mismatch in argument '%s'",
1425 s1->name);
1426 return false;
1427 }
9795c594 1428
f2f8171f
JW
1429 /* Check interface of dummy procedures. */
1430 if (s1->attr.flavor == FL_PROCEDURE)
1431 {
1432 char err[200];
1433 if (!gfc_compare_interfaces (s1, s2, s2->name, 0, 1, err, sizeof(err),
1434 NULL, NULL))
1435 {
1436 snprintf (errmsg, err_len, "Interface mismatch in dummy procedure "
1437 "'%s': %s", s1->name, err);
524af0d6 1438 return false;
f2f8171f
JW
1439 }
1440 }
1441
9795c594
JW
1442 /* Check string length. */
1443 if (s1->ts.type == BT_CHARACTER
1444 && s1->ts.u.cl && s1->ts.u.cl->length
1445 && s2->ts.u.cl && s2->ts.u.cl->length)
1446 {
1447 int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
1448 s2->ts.u.cl->length);
1449 switch (compval)
1450 {
1451 case -1:
1452 case 1:
1453 case -3:
1454 snprintf (errmsg, err_len, "Character length mismatch "
1455 "in argument '%s'", s1->name);
524af0d6 1456 return false;
9795c594
JW
1457
1458 case -2:
1459 /* FIXME: Implement a warning for this case.
db30e21c 1460 gfc_warning (0, "Possible character length mismatch in argument %qs",
9795c594
JW
1461 s1->name);*/
1462 break;
1463
1464 case 0:
1465 break;
1466
1467 default:
1468 gfc_internal_error ("check_dummy_characteristics: Unexpected result "
1469 "%i of gfc_dep_compare_expr", compval);
1470 break;
1471 }
1472 }
1473
1474 /* Check array shape. */
1475 if (s1->as && s2->as)
1476 {
97f26732
JW
1477 int i, compval;
1478 gfc_expr *shape1, *shape2;
1479
a5baf71b
PT
1480 /* Sometimes the ambiguity between deferred shape and assumed shape
1481 does not get resolved in module procedures, where the only explicit
1482 declaration of the dummy is in the interface. */
1483 if (s1->ns->proc_name && s1->ns->proc_name->attr.module_procedure
1484 && s1->as->type == AS_ASSUMED_SHAPE
1485 && s2->as->type == AS_DEFERRED)
1486 {
1487 s2->as->type = AS_ASSUMED_SHAPE;
1488 for (i = 0; i < s2->as->rank; i++)
1489 if (s1->as->lower[i] != NULL)
1490 s2->as->lower[i] = gfc_copy_expr (s1->as->lower[i]);
1491 }
1492
9795c594
JW
1493 if (s1->as->type != s2->as->type)
1494 {
1495 snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
1496 s1->name);
524af0d6 1497 return false;
9795c594 1498 }
97f26732 1499
b25affbd
TB
1500 if (s1->as->corank != s2->as->corank)
1501 {
1502 snprintf (errmsg, err_len, "Corank mismatch in argument '%s' (%i/%i)",
1503 s1->name, s1->as->corank, s2->as->corank);
1504 return false;
1505 }
1506
97f26732 1507 if (s1->as->type == AS_EXPLICIT)
47da0bf6 1508 for (i = 0; i < s1->as->rank + MAX (0, s1->as->corank-1); i++)
97f26732
JW
1509 {
1510 shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
1511 gfc_copy_expr (s1->as->lower[i]));
1512 shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
1513 gfc_copy_expr (s2->as->lower[i]));
1514 compval = gfc_dep_compare_expr (shape1, shape2);
1515 gfc_free_expr (shape1);
1516 gfc_free_expr (shape2);
1517 switch (compval)
1518 {
1519 case -1:
1520 case 1:
1521 case -3:
b25affbd
TB
1522 if (i < s1->as->rank)
1523 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of"
1524 " argument '%s'", i + 1, s1->name);
1525 else
1526 snprintf (errmsg, err_len, "Shape mismatch in codimension %i "
1527 "of argument '%s'", i - s1->as->rank + 1, s1->name);
524af0d6 1528 return false;
97f26732
JW
1529
1530 case -2:
1531 /* FIXME: Implement a warning for this case.
db30e21c 1532 gfc_warning (0, "Possible shape mismatch in argument %qs",
97f26732
JW
1533 s1->name);*/
1534 break;
1535
1536 case 0:
1537 break;
1538
1539 default:
1540 gfc_internal_error ("check_dummy_characteristics: Unexpected "
1541 "result %i of gfc_dep_compare_expr",
1542 compval);
1543 break;
1544 }
1545 }
9795c594 1546 }
8b704316 1547
524af0d6 1548 return true;
9795c594
JW
1549}
1550
1551
edc802c7
JW
1552/* Check if the characteristics of two function results match,
1553 cf. F08:12.3.3. */
1554
4668d6f9
PT
1555bool
1556gfc_check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
2298af08 1557 char *errmsg, int err_len)
edc802c7
JW
1558{
1559 gfc_symbol *r1, *r2;
1560
82b541a1
JW
1561 if (s1->ts.interface && s1->ts.interface->result)
1562 r1 = s1->ts.interface->result;
1563 else
1564 r1 = s1->result ? s1->result : s1;
1565
1566 if (s2->ts.interface && s2->ts.interface->result)
1567 r2 = s2->ts.interface->result;
1568 else
1569 r2 = s2->result ? s2->result : s2;
edc802c7
JW
1570
1571 if (r1->ts.type == BT_UNKNOWN)
524af0d6 1572 return true;
edc802c7
JW
1573
1574 /* Check type and rank. */
0ce0e6e8 1575 if (!compare_type_characteristics (r1, r2))
edc802c7 1576 {
e7333b69
JW
1577 snprintf (errmsg, err_len, "Type mismatch in function result (%s/%s)",
1578 gfc_typename (&r1->ts), gfc_typename (&r2->ts));
1579 return false;
1580 }
1581 if (!compare_rank (r1, r2))
1582 {
1583 snprintf (errmsg, err_len, "Rank mismatch in function result (%i/%i)",
1584 symbol_rank (r1), symbol_rank (r2));
524af0d6 1585 return false;
edc802c7
JW
1586 }
1587
1588 /* Check ALLOCATABLE attribute. */
1589 if (r1->attr.allocatable != r2->attr.allocatable)
1590 {
1591 snprintf (errmsg, err_len, "ALLOCATABLE attribute mismatch in "
1592 "function result");
524af0d6 1593 return false;
edc802c7
JW
1594 }
1595
1596 /* Check POINTER attribute. */
1597 if (r1->attr.pointer != r2->attr.pointer)
1598 {
1599 snprintf (errmsg, err_len, "POINTER attribute mismatch in "
1600 "function result");
524af0d6 1601 return false;
edc802c7
JW
1602 }
1603
1604 /* Check CONTIGUOUS attribute. */
1605 if (r1->attr.contiguous != r2->attr.contiguous)
1606 {
1607 snprintf (errmsg, err_len, "CONTIGUOUS attribute mismatch in "
1608 "function result");
524af0d6 1609 return false;
edc802c7
JW
1610 }
1611
1612 /* Check PROCEDURE POINTER attribute. */
1613 if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
1614 {
1615 snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
1616 "function result");
524af0d6 1617 return false;
edc802c7
JW
1618 }
1619
1620 /* Check string length. */
1621 if (r1->ts.type == BT_CHARACTER && r1->ts.u.cl && r2->ts.u.cl)
1622 {
1623 if (r1->ts.deferred != r2->ts.deferred)
1624 {
1625 snprintf (errmsg, err_len, "Character length mismatch "
1626 "in function result");
524af0d6 1627 return false;
edc802c7
JW
1628 }
1629
96486998 1630 if (r1->ts.u.cl->length && r2->ts.u.cl->length)
edc802c7
JW
1631 {
1632 int compval = gfc_dep_compare_expr (r1->ts.u.cl->length,
1633 r2->ts.u.cl->length);
1634 switch (compval)
1635 {
1636 case -1:
1637 case 1:
1638 case -3:
1639 snprintf (errmsg, err_len, "Character length mismatch "
1640 "in function result");
524af0d6 1641 return false;
edc802c7
JW
1642
1643 case -2:
1644 /* FIXME: Implement a warning for this case.
1645 snprintf (errmsg, err_len, "Possible character length mismatch "
1646 "in function result");*/
1647 break;
1648
1649 case 0:
1650 break;
1651
1652 default:
1653 gfc_internal_error ("check_result_characteristics (1): Unexpected "
1654 "result %i of gfc_dep_compare_expr", compval);
1655 break;
1656 }
1657 }
1658 }
1659
1660 /* Check array shape. */
1661 if (!r1->attr.allocatable && !r1->attr.pointer && r1->as && r2->as)
1662 {
1663 int i, compval;
1664 gfc_expr *shape1, *shape2;
1665
1666 if (r1->as->type != r2->as->type)
1667 {
1668 snprintf (errmsg, err_len, "Shape mismatch in function result");
524af0d6 1669 return false;
edc802c7
JW
1670 }
1671
1672 if (r1->as->type == AS_EXPLICIT)
1673 for (i = 0; i < r1->as->rank + r1->as->corank; i++)
1674 {
1675 shape1 = gfc_subtract (gfc_copy_expr (r1->as->upper[i]),
1676 gfc_copy_expr (r1->as->lower[i]));
1677 shape2 = gfc_subtract (gfc_copy_expr (r2->as->upper[i]),
1678 gfc_copy_expr (r2->as->lower[i]));
1679 compval = gfc_dep_compare_expr (shape1, shape2);
1680 gfc_free_expr (shape1);
1681 gfc_free_expr (shape2);
1682 switch (compval)
1683 {
1684 case -1:
1685 case 1:
1686 case -3:
1687 snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
1688 "function result", i + 1);
524af0d6 1689 return false;
edc802c7
JW
1690
1691 case -2:
1692 /* FIXME: Implement a warning for this case.
db30e21c 1693 gfc_warning (0, "Possible shape mismatch in return value");*/
edc802c7
JW
1694 break;
1695
1696 case 0:
1697 break;
1698
1699 default:
1700 gfc_internal_error ("check_result_characteristics (2): "
1701 "Unexpected result %i of "
1702 "gfc_dep_compare_expr", compval);
1703 break;
1704 }
1705 }
1706 }
1707
524af0d6 1708 return true;
edc802c7
JW
1709}
1710
1711
6de9cd9a 1712/* 'Compare' two formal interfaces associated with a pair of symbols.
f3e1097b 1713 We return true if there exists an actual argument list that
8ad15a0a 1714 would be ambiguous between the two interfaces, zero otherwise.
58c1ae36 1715 'strict_flag' specifies whether all the characteristics are
6f3ab30d
JW
1716 required to match, which is not the case for ambiguity checks.
1717 'p1' and 'p2' are the PASS arguments of both procedures (if applicable). */
6de9cd9a 1718
f3e1097b 1719bool
889dc035 1720gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
58c1ae36 1721 int generic_flag, int strict_flag,
6f3ab30d 1722 char *errmsg, int err_len,
2298af08
TK
1723 const char *p1, const char *p2,
1724 bool *bad_result_characteristics)
6de9cd9a
DN
1725{
1726 gfc_formal_arglist *f1, *f2;
1727
0175478d
JD
1728 gcc_assert (name2 != NULL);
1729
2298af08
TK
1730 if (bad_result_characteristics)
1731 *bad_result_characteristics = false;
1732
9b63f282
JW
1733 if (s1->attr.function && (s2->attr.subroutine
1734 || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
889dc035 1735 && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
8ad15a0a
JW
1736 {
1737 if (errmsg != NULL)
889dc035 1738 snprintf (errmsg, err_len, "'%s' is not a function", name2);
f3e1097b 1739 return false;
8ad15a0a
JW
1740 }
1741
1742 if (s1->attr.subroutine && s2->attr.function)
1743 {
1744 if (errmsg != NULL)
889dc035 1745 snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
f3e1097b 1746 return false;
8ad15a0a 1747 }
3afadac3 1748
58c1ae36
JW
1749 /* Do strict checks on all characteristics
1750 (for dummy procedures and procedure pointer assignments). */
1751 if (!generic_flag && strict_flag)
6cc309c9 1752 {
58c1ae36 1753 if (s1->attr.function && s2->attr.function)
8ad15a0a 1754 {
edc802c7 1755 /* If both are functions, check result characteristics. */
4668d6f9
PT
1756 if (!gfc_check_result_characteristics (s1, s2, errmsg, err_len)
1757 || !gfc_check_result_characteristics (s2, s1, errmsg, err_len))
2298af08
TK
1758 {
1759 if (bad_result_characteristics)
1760 *bad_result_characteristics = true;
1761 return false;
1762 }
58c1ae36
JW
1763 }
1764
1765 if (s1->attr.pure && !s2->attr.pure)
1766 {
1767 snprintf (errmsg, err_len, "Mismatch in PURE attribute");
f3e1097b 1768 return false;
58c1ae36
JW
1769 }
1770 if (s1->attr.elemental && !s2->attr.elemental)
1771 {
1772 snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
f3e1097b 1773 return false;
8ad15a0a 1774 }
6cc309c9 1775 }
26033479 1776
8ad15a0a
JW
1777 if (s1->attr.if_source == IFSRC_UNKNOWN
1778 || s2->attr.if_source == IFSRC_UNKNOWN)
f3e1097b 1779 return true;
26033479 1780
4cbc9039
JW
1781 f1 = gfc_sym_get_dummy_args (s1);
1782 f2 = gfc_sym_get_dummy_args (s2);
26033479 1783
bd845c14 1784 /* Special case: No arguments. */
c73b6478 1785 if (f1 == NULL && f2 == NULL)
f3e1097b 1786 return true;
6cc309c9 1787
c73b6478 1788 if (generic_flag)
6cc309c9 1789 {
6f3ab30d
JW
1790 if (count_types_test (f1, f2, p1, p2)
1791 || count_types_test (f2, f1, p2, p1))
f3e1097b 1792 return false;
bd845c14
SK
1793
1794 /* Special case: alternate returns. If both f1->sym and f2->sym are
e4e659b9
JW
1795 NULL, then the leading formal arguments are alternate returns.
1796 The previous conditional should catch argument lists with
bd845c14
SK
1797 different number of argument. */
1798 if (f1 && f1->sym == NULL && f2 && f2->sym == NULL)
f3e1097b 1799 return true;
bd845c14 1800
6f3ab30d
JW
1801 if (generic_correspondence (f1, f2, p1, p2)
1802 || generic_correspondence (f2, f1, p2, p1))
f3e1097b 1803 return false;
6cc309c9 1804 }
c73b6478 1805 else
8ad15a0a
JW
1806 /* Perform the abbreviated correspondence test for operators (the
1807 arguments cannot be optional and are always ordered correctly).
1808 This is also done when comparing interfaces for dummy procedures and in
1809 procedure pointer assignments. */
1810
f76c4d97 1811 for (; f1 || f2; f1 = f1->next, f2 = f2->next)
8ad15a0a
JW
1812 {
1813 /* Check existence. */
8ad15a0a
JW
1814 if (f1 == NULL || f2 == NULL)
1815 {
1816 if (errmsg != NULL)
1817 snprintf (errmsg, err_len, "'%s' has the wrong number of "
889dc035 1818 "arguments", name2);
f3e1097b 1819 return false;
8ad15a0a
JW
1820 }
1821
58c1ae36 1822 if (strict_flag)
8ad15a0a 1823 {
9795c594 1824 /* Check all characteristics. */
4668d6f9 1825 if (!gfc_check_dummy_characteristics (f1->sym, f2->sym, true,
524af0d6 1826 errmsg, err_len))
f3e1097b 1827 return false;
9795c594 1828 }
e7333b69 1829 else
9795c594 1830 {
d50cd259 1831 /* Operators: Only check type and rank of arguments. */
e7333b69
JW
1832 if (!compare_type (f2->sym, f1->sym))
1833 {
1834 if (errmsg != NULL)
1835 snprintf (errmsg, err_len, "Type mismatch in argument '%s' "
1836 "(%s/%s)", f1->sym->name,
1837 gfc_typename (&f1->sym->ts),
1838 gfc_typename (&f2->sym->ts));
f3e1097b 1839 return false;
e7333b69
JW
1840 }
1841 if (!compare_rank (f2->sym, f1->sym))
1842 {
1843 if (errmsg != NULL)
e0b9e5f9
TK
1844 snprintf (errmsg, err_len, "Rank mismatch in argument "
1845 "'%s' (%i/%i)", f1->sym->name,
1846 symbol_rank (f1->sym), symbol_rank (f2->sym));
f3e1097b 1847 return false;
e7333b69 1848 }
d50cd259
JW
1849 if ((gfc_option.allow_std & GFC_STD_F2008)
1850 && (compare_ptr_alloc(f1->sym, f2->sym)
1851 || compare_ptr_alloc(f2->sym, f1->sym)))
1852 {
1853 if (errmsg != NULL)
1854 snprintf (errmsg, err_len, "Mismatching POINTER/ALLOCATABLE "
1855 "attribute in argument '%s' ", f1->sym->name);
1856 return false;
1857 }
8ad15a0a 1858 }
8ad15a0a
JW
1859 }
1860
f3e1097b 1861 return true;
6cc309c9
JD
1862}
1863
1864
6de9cd9a 1865/* Given a pointer to an interface pointer, remove duplicate
284d58f1 1866 interfaces and make sure that all symbols are either functions
f3e1097b 1867 or subroutines, and all of the same kind. Returns true if
284d58f1 1868 something goes wrong. */
6de9cd9a 1869
f3e1097b 1870static bool
b251af97 1871check_interface0 (gfc_interface *p, const char *interface_name)
6de9cd9a
DN
1872{
1873 gfc_interface *psave, *q, *qlast;
1874
1875 psave = p;
6de9cd9a 1876 for (; p; p = p->next)
284d58f1
DF
1877 {
1878 /* Make sure all symbols in the interface have been defined as
1879 functions or subroutines. */
c3f34952
TB
1880 if (((!p->sym->attr.function && !p->sym->attr.subroutine)
1881 || !p->sym->attr.if_source)
f6288c24 1882 && !gfc_fl_struct (p->sym->attr.flavor))
284d58f1 1883 {
bcc478b9
BRF
1884 const char *guessed
1885 = gfc_lookup_function_fuzzy (p->sym->name, p->sym->ns->sym_root);
1886
284d58f1 1887 if (p->sym->attr.external)
bcc478b9
BRF
1888 if (guessed)
1889 gfc_error ("Procedure %qs in %s at %L has no explicit interface"
1890 "; did you mean %qs?",
1891 p->sym->name, interface_name, &p->sym->declared_at,
1892 guessed);
1893 else
1894 gfc_error ("Procedure %qs in %s at %L has no explicit interface",
1895 p->sym->name, interface_name, &p->sym->declared_at);
284d58f1 1896 else
bcc478b9
BRF
1897 if (guessed)
1898 gfc_error ("Procedure %qs in %s at %L is neither function nor "
1899 "subroutine; did you mean %qs?", p->sym->name,
1900 interface_name, &p->sym->declared_at, guessed);
1901 else
1902 gfc_error ("Procedure %qs in %s at %L is neither function nor "
1903 "subroutine", p->sym->name, interface_name,
1904 &p->sym->declared_at);
f3e1097b 1905 return true;
284d58f1
DF
1906 }
1907
1908 /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs. */
c3f34952 1909 if ((psave->sym->attr.function && !p->sym->attr.function
f6288c24 1910 && !gfc_fl_struct (p->sym->attr.flavor))
284d58f1
DF
1911 || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
1912 {
f6288c24 1913 if (!gfc_fl_struct (p->sym->attr.flavor))
c3f34952
TB
1914 gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
1915 " or all FUNCTIONs", interface_name,
1916 &p->sym->declared_at);
f6288c24 1917 else if (p->sym->attr.flavor == FL_DERIVED)
c3f34952
TB
1918 gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
1919 "generic name is also the name of a derived type",
1920 interface_name, &p->sym->declared_at);
f3e1097b 1921 return true;
284d58f1 1922 }
a300121e 1923
d2c5dbf2 1924 /* F2003, C1207. F2008, C1207. */
a300121e 1925 if (p->sym->attr.proc == PROC_INTERNAL
524af0d6 1926 && !gfc_notify_std (GFC_STD_F2008, "Internal procedure "
a4d9b221 1927 "%qs in %s at %L", p->sym->name,
524af0d6 1928 interface_name, &p->sym->declared_at))
f3e1097b 1929 return true;
284d58f1 1930 }
6de9cd9a
DN
1931 p = psave;
1932
1933 /* Remove duplicate interfaces in this interface list. */
1934 for (; p; p = p->next)
1935 {
1936 qlast = p;
1937
1938 for (q = p->next; q;)
1939 {
1940 if (p->sym != q->sym)
1941 {
1942 qlast = q;
1943 q = q->next;
6de9cd9a
DN
1944 }
1945 else
1946 {
66e4ab31 1947 /* Duplicate interface. */
6de9cd9a 1948 qlast->next = q->next;
cede9502 1949 free (q);
6de9cd9a
DN
1950 q = qlast->next;
1951 }
1952 }
1953 }
1954
f3e1097b 1955 return false;
6de9cd9a
DN
1956}
1957
1958
1959/* Check lists of interfaces to make sure that no two interfaces are
66e4ab31 1960 ambiguous. Duplicate interfaces (from the same symbol) are OK here. */
6de9cd9a 1961
f3e1097b 1962static bool
b251af97 1963check_interface1 (gfc_interface *p, gfc_interface *q0,
993ef28f 1964 int generic_flag, const char *interface_name,
26f2ca2b 1965 bool referenced)
6de9cd9a 1966{
b251af97 1967 gfc_interface *q;
6de9cd9a 1968 for (; p; p = p->next)
991f3b12 1969 for (q = q0; q; q = q->next)
6de9cd9a
DN
1970 {
1971 if (p->sym == q->sym)
66e4ab31 1972 continue; /* Duplicates OK here. */
6de9cd9a 1973
312ae8f4 1974 if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
6de9cd9a
DN
1975 continue;
1976
f6288c24
FR
1977 if (!gfc_fl_struct (p->sym->attr.flavor)
1978 && !gfc_fl_struct (q->sym->attr.flavor)
c3f34952 1979 && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
6f3ab30d 1980 generic_flag, 0, NULL, 0, NULL, NULL))
6de9cd9a 1981 {
993ef28f 1982 if (referenced)
bd845c14
SK
1983 gfc_error ("Ambiguous interfaces in %s for %qs at %L "
1984 "and %qs at %L", interface_name,
1985 q->sym->name, &q->sym->declared_at,
1986 p->sym->name, &p->sym->declared_at);
ae7c61de 1987 else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
bd845c14
SK
1988 gfc_warning (0, "Ambiguous interfaces in %s for %qs at %L "
1989 "and %qs at %L", interface_name,
1990 q->sym->name, &q->sym->declared_at,
1991 p->sym->name, &p->sym->declared_at);
ae7c61de 1992 else
db30e21c 1993 gfc_warning (0, "Although not referenced, %qs has ambiguous "
ae7c61de 1994 "interfaces at %L", interface_name, &p->where);
f3e1097b 1995 return true;
6de9cd9a
DN
1996 }
1997 }
f3e1097b 1998 return false;
6de9cd9a
DN
1999}
2000
2001
2002/* Check the generic and operator interfaces of symbols to make sure
2003 that none of the interfaces conflict. The check has to be done
2004 after all of the symbols are actually loaded. */
2005
2006static void
b251af97 2007check_sym_interfaces (gfc_symbol *sym)
6de9cd9a 2008{
62c0c0ea
HA
2009 /* Provide sufficient space to hold "generic interface 'symbol.symbol'". */
2010 char interface_name[2*GFC_MAX_SYMBOL_LEN+2 + sizeof("generic interface ''")];
71f77fd7 2011 gfc_interface *p;
6de9cd9a
DN
2012
2013 if (sym->ns != gfc_current_ns)
2014 return;
2015
2016 if (sym->generic != NULL)
2017 {
62c0c0ea
HA
2018 size_t len = strlen (sym->name) + sizeof("generic interface ''");
2019 gcc_assert (len < sizeof (interface_name));
6de9cd9a
DN
2020 sprintf (interface_name, "generic interface '%s'", sym->name);
2021 if (check_interface0 (sym->generic, interface_name))
2022 return;
2023
71f77fd7
PT
2024 for (p = sym->generic; p; p = p->next)
2025 {
abf86978 2026 if (p->sym->attr.mod_proc
4668d6f9 2027 && !p->sym->attr.module_procedure
abf86978
TB
2028 && (p->sym->attr.if_source != IFSRC_DECL
2029 || p->sym->attr.procedure))
71f77fd7 2030 {
c4100eae 2031 gfc_error ("%qs at %L is not a module procedure",
e9f63ace 2032 p->sym->name, &p->where);
71f77fd7
PT
2033 return;
2034 }
2035 }
2036
4c256e34 2037 /* Originally, this test was applied to host interfaces too;
993ef28f
PT
2038 this is incorrect since host associated symbols, from any
2039 source, cannot be ambiguous with local symbols. */
ae7c61de
JW
2040 check_interface1 (sym->generic, sym->generic, 1, interface_name,
2041 sym->attr.referenced || !sym->attr.use_assoc);
6de9cd9a
DN
2042 }
2043}
2044
2045
2046static void
b251af97 2047check_uop_interfaces (gfc_user_op *uop)
6de9cd9a 2048{
439d2350 2049 char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("operator interface ''")];
6de9cd9a
DN
2050 gfc_user_op *uop2;
2051 gfc_namespace *ns;
2052
2053 sprintf (interface_name, "operator interface '%s'", uop->name);
a1ee985f 2054 if (check_interface0 (uop->op, interface_name))
6de9cd9a
DN
2055 return;
2056
2057 for (ns = gfc_current_ns; ns; ns = ns->parent)
2058 {
2059 uop2 = gfc_find_uop (uop->name, ns);
2060 if (uop2 == NULL)
2061 continue;
2062
a1ee985f 2063 check_interface1 (uop->op, uop2->op, 0,
26f2ca2b 2064 interface_name, true);
6de9cd9a
DN
2065 }
2066}
2067
fb03a37e
TK
2068/* Given an intrinsic op, return an equivalent op if one exists,
2069 or INTRINSIC_NONE otherwise. */
2070
2071gfc_intrinsic_op
2072gfc_equivalent_op (gfc_intrinsic_op op)
2073{
2074 switch(op)
2075 {
2076 case INTRINSIC_EQ:
2077 return INTRINSIC_EQ_OS;
2078
2079 case INTRINSIC_EQ_OS:
2080 return INTRINSIC_EQ;
2081
2082 case INTRINSIC_NE:
2083 return INTRINSIC_NE_OS;
2084
2085 case INTRINSIC_NE_OS:
2086 return INTRINSIC_NE;
2087
2088 case INTRINSIC_GT:
2089 return INTRINSIC_GT_OS;
2090
2091 case INTRINSIC_GT_OS:
2092 return INTRINSIC_GT;
2093
2094 case INTRINSIC_GE:
2095 return INTRINSIC_GE_OS;
2096
2097 case INTRINSIC_GE_OS:
2098 return INTRINSIC_GE;
2099
2100 case INTRINSIC_LT:
2101 return INTRINSIC_LT_OS;
2102
2103 case INTRINSIC_LT_OS:
2104 return INTRINSIC_LT;
2105
2106 case INTRINSIC_LE:
2107 return INTRINSIC_LE_OS;
2108
2109 case INTRINSIC_LE_OS:
2110 return INTRINSIC_LE;
2111
2112 default:
2113 return INTRINSIC_NONE;
2114 }
2115}
6de9cd9a
DN
2116
2117/* For the namespace, check generic, user operator and intrinsic
2118 operator interfaces for consistency and to remove duplicate
2119 interfaces. We traverse the whole namespace, counting on the fact
2120 that most symbols will not have generic or operator interfaces. */
2121
2122void
b251af97 2123gfc_check_interfaces (gfc_namespace *ns)
6de9cd9a
DN
2124{
2125 gfc_namespace *old_ns, *ns2;
439d2350 2126 char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("intrinsic '' operator")];
09639a83 2127 int i;
6de9cd9a
DN
2128
2129 old_ns = gfc_current_ns;
2130 gfc_current_ns = ns;
2131
2132 gfc_traverse_ns (ns, check_sym_interfaces);
2133
2134 gfc_traverse_user_op (ns, check_uop_interfaces);
2135
2136 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2137 {
2138 if (i == INTRINSIC_USER)
2139 continue;
2140
2141 if (i == INTRINSIC_ASSIGN)
2142 strcpy (interface_name, "intrinsic assignment operator");
2143 else
2144 sprintf (interface_name, "intrinsic '%s' operator",
09639a83 2145 gfc_op2string ((gfc_intrinsic_op) i));
6de9cd9a 2146
a1ee985f 2147 if (check_interface0 (ns->op[i], interface_name))
6de9cd9a
DN
2148 continue;
2149
94747289
DK
2150 if (ns->op[i])
2151 gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
2152 ns->op[i]->where);
6de9cd9a 2153
3bed9dd0
DF
2154 for (ns2 = ns; ns2; ns2 = ns2->parent)
2155 {
fb03a37e 2156 gfc_intrinsic_op other_op;
8b704316 2157
a1ee985f 2158 if (check_interface1 (ns->op[i], ns2->op[i], 0,
3bed9dd0
DF
2159 interface_name, true))
2160 goto done;
2161
fb03a37e
TK
2162 /* i should be gfc_intrinsic_op, but has to be int with this cast
2163 here for stupid C++ compatibility rules. */
2164 other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
2165 if (other_op != INTRINSIC_NONE
2166 && check_interface1 (ns->op[i], ns2->op[other_op],
2167 0, interface_name, true))
2168 goto done;
3bed9dd0 2169 }
6de9cd9a
DN
2170 }
2171
3bed9dd0 2172done:
6de9cd9a
DN
2173 gfc_current_ns = old_ns;
2174}
2175
2176
aa08038d
EE
2177/* Given a symbol of a formal argument list and an expression, if the
2178 formal argument is allocatable, check that the actual argument is
f3e1097b 2179 allocatable. Returns true if compatible, zero if not compatible. */
aa08038d 2180
f3e1097b 2181static bool
b251af97 2182compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
aa08038d 2183{
5ac13b8e
JW
2184 if (formal->attr.allocatable
2185 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
aa08038d 2186 {
fec5ce24
JW
2187 symbol_attribute attr = gfc_expr_attr (actual);
2188 if (actual->ts.type == BT_CLASS && !attr.class_ok)
f3e1097b 2189 return true;
fec5ce24 2190 else if (!attr.allocatable)
f3e1097b 2191 return false;
aa08038d
EE
2192 }
2193
f3e1097b 2194 return true;
aa08038d
EE
2195}
2196
2197
6de9cd9a
DN
2198/* Given a symbol of a formal argument list and an expression, if the
2199 formal argument is a pointer, see if the actual argument is a
2200 pointer. Returns nonzero if compatible, zero if not compatible. */
2201
2202static int
b251af97 2203compare_pointer (gfc_symbol *formal, gfc_expr *actual)
6de9cd9a
DN
2204{
2205 symbol_attribute attr;
2206
f18075ff
TB
2207 if (formal->attr.pointer
2208 || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)
2209 && CLASS_DATA (formal)->attr.class_pointer))
6de9cd9a
DN
2210 {
2211 attr = gfc_expr_attr (actual);
7d54ef80
TB
2212
2213 /* Fortran 2008 allows non-pointer actual arguments. */
2214 if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
2215 return 2;
2216
6de9cd9a
DN
2217 if (!attr.pointer)
2218 return 0;
2219 }
2220
2221 return 1;
2222}
2223
2224
a516520c
PT
2225/* Emit clear error messages for rank mismatch. */
2226
2227static void
2228argument_rank_mismatch (const char *name, locus *where,
e0b9e5f9 2229 int rank1, int rank2, locus *where_formal)
a516520c 2230{
c62c6622
TB
2231
2232 /* TS 29113, C407b. */
e0b9e5f9
TK
2233 if (where_formal == NULL)
2234 {
2235 if (rank2 == -1)
2236 gfc_error ("The assumed-rank array at %L requires that the dummy "
2237 "argument %qs has assumed-rank", where, name);
2238 else if (rank1 == 0)
2239 gfc_error_opt (0, "Rank mismatch in argument %qs "
2240 "at %L (scalar and rank-%d)", name, where, rank2);
2241 else if (rank2 == 0)
2242 gfc_error_opt (0, "Rank mismatch in argument %qs "
2243 "at %L (rank-%d and scalar)", name, where, rank1);
2244 else
2245 gfc_error_opt (0, "Rank mismatch in argument %qs "
2246 "at %L (rank-%d and rank-%d)", name, where, rank1,
2247 rank2);
2248 }
a516520c 2249 else
e0b9e5f9 2250 {
8e277106
SL
2251 if (rank2 == -1)
2252 /* This is an assumed rank-actual passed to a function without
2253 an explicit interface, which is already diagnosed in
2254 gfc_procedure_use. */
2255 return;
e0b9e5f9
TK
2256 if (rank1 == 0)
2257 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2258 "and actual argument at %L (scalar and rank-%d)",
2259 where, where_formal, rank2);
2260 else if (rank2 == 0)
2261 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2262 "and actual argument at %L (rank-%d and scalar)",
2263 where, where_formal, rank1);
2264 else
2265 gfc_error_opt (0, "Rank mismatch between actual argument at %L "
92e8508e 2266 "and actual argument at %L (rank-%d and rank-%d)", where,
e0b9e5f9
TK
2267 where_formal, rank1, rank2);
2268 }
a516520c
PT
2269}
2270
2271
4a4fc7fe
TK
2272/* Under certain conditions, a scalar actual argument can be passed
2273 to an array dummy argument - see F2018, 15.5.2.4, paragraph 14.
2274 This function returns true for these conditions so that an error
2275 or warning for this can be suppressed later. Always return false
2276 for expressions with rank > 0. */
2277
2278bool
2279maybe_dummy_array_arg (gfc_expr *e)
2280{
2281 gfc_symbol *s;
2282 gfc_ref *ref;
2283 bool array_pointer = false;
2284 bool assumed_shape = false;
2285 bool scalar_ref = true;
2286
2287 if (e->rank > 0)
2288 return false;
2289
2290 if (e->ts.type == BT_CHARACTER && e->ts.kind == 1)
2291 return true;
2292
2293 /* If this comes from a constructor, it has been an array element
2294 originally. */
2295
2296 if (e->expr_type == EXPR_CONSTANT)
2297 return e->from_constructor;
2298
2299 if (e->expr_type != EXPR_VARIABLE)
2300 return false;
2301
2302 s = e->symtree->n.sym;
2303
2304 if (s->attr.dimension)
2305 {
2306 scalar_ref = false;
2307 array_pointer = s->attr.pointer;
2308 }
2309
2310 if (s->as && s->as->type == AS_ASSUMED_SHAPE)
2311 assumed_shape = true;
2312
2313 for (ref=e->ref; ref; ref=ref->next)
2314 {
2315 if (ref->type == REF_COMPONENT)
2316 {
2317 symbol_attribute *attr;
2318 attr = &ref->u.c.component->attr;
2319 if (attr->dimension)
2320 {
2321 array_pointer = attr->pointer;
2322 assumed_shape = false;
2323 scalar_ref = false;
2324 }
2325 else
2326 scalar_ref = true;
2327 }
2328 }
2329
2330 return !(scalar_ref || array_pointer || assumed_shape);
2331}
2332
6de9cd9a 2333/* Given a symbol of a formal argument list and an expression, see if
f3e1097b
JW
2334 the two are compatible as arguments. Returns true if
2335 compatible, false if not compatible. */
6de9cd9a 2336
f3e1097b 2337static bool
b251af97 2338compare_parameter (gfc_symbol *formal, gfc_expr *actual,
5ad6345e 2339 int ranks_must_agree, int is_elemental, locus *where)
6de9cd9a
DN
2340{
2341 gfc_ref *ref;
975b975b 2342 bool rank_check, is_pointer;
5c0ba546
JW
2343 char err[200];
2344 gfc_component *ppc;
fc27115d 2345 bool codimension = false;
6de9cd9a 2346
a8b3b0b6
CR
2347 /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
2348 procs c_f_pointer or c_f_procpointer, and we need to accept most
2349 pointers the user could give us. This should allow that. */
2350 if (formal->ts.type == BT_VOID)
f3e1097b 2351 return true;
a8b3b0b6
CR
2352
2353 if (formal->ts.type == BT_DERIVED
bc21d315 2354 && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
a8b3b0b6 2355 && actual->ts.type == BT_DERIVED
bc21d315 2356 && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
f3e1097b 2357 return true;
a8b3b0b6 2358
7d58b9e7 2359 if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
e10f52d0
JW
2360 /* Make sure the vtab symbol is present when
2361 the module variables are generated. */
7d58b9e7 2362 gfc_find_derived_vtab (actual->ts.u.derived);
e10f52d0 2363
6de9cd9a
DN
2364 if (actual->ts.type == BT_PROCEDURE)
2365 {
9b63f282 2366 gfc_symbol *act_sym = actual->symtree->n.sym;
6de9cd9a 2367
8ad15a0a
JW
2368 if (formal->attr.flavor != FL_PROCEDURE)
2369 {
2370 if (where)
2371 gfc_error ("Invalid procedure argument at %L", &actual->where);
f3e1097b 2372 return false;
8ad15a0a 2373 }
6de9cd9a 2374
889dc035 2375 if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
6f3ab30d 2376 sizeof(err), NULL, NULL))
8ad15a0a
JW
2377 {
2378 if (where)
e0b9e5f9 2379 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2700d0e3 2380 " %s", formal->name, &actual->where, err);
f3e1097b 2381 return false;
8ad15a0a 2382 }
5ad6345e 2383
9b63f282 2384 if (formal->attr.function && !act_sym->attr.function)
03bd096b
JW
2385 {
2386 gfc_add_function (&act_sym->attr, act_sym->name,
2387 &act_sym->declared_at);
2388 if (act_sym->ts.type == BT_UNKNOWN
524af0d6 2389 && !gfc_set_default_type (act_sym, 1, act_sym->ns))
f3e1097b 2390 return false;
03bd096b
JW
2391 }
2392 else if (formal->attr.subroutine && !act_sym->attr.subroutine)
9b63f282
JW
2393 gfc_add_subroutine (&act_sym->attr, act_sym->name,
2394 &act_sym->declared_at);
2395
f3e1097b 2396 return true;
6de9cd9a
DN
2397 }
2398
5c0ba546 2399 ppc = gfc_get_proc_ptr_comp (actual);
228eb42a 2400 if (ppc && ppc->ts.interface)
5c0ba546
JW
2401 {
2402 if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
2403 err, sizeof(err), NULL, NULL))
2404 {
2405 if (where)
e0b9e5f9 2406 gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2700d0e3 2407 " %s", formal->name, &actual->where, err);
f3e1097b 2408 return false;
5c0ba546
JW
2409 }
2410 }
2411
fe4e525c
TB
2412 /* F2008, C1241. */
2413 if (formal->attr.pointer && formal->attr.contiguous
460263d0 2414 && !gfc_is_simply_contiguous (actual, true, false))
fe4e525c
TB
2415 {
2416 if (where)
c4100eae 2417 gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
62732c30 2418 "must be simply contiguous", formal->name, &actual->where);
f3e1097b 2419 return false;
fe4e525c
TB
2420 }
2421
fec5ce24
JW
2422 symbol_attribute actual_attr = gfc_expr_attr (actual);
2423 if (actual->ts.type == BT_CLASS && !actual_attr.class_ok)
f3e1097b 2424 return true;
fec5ce24 2425
90aeadcb 2426 if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
df161b69 2427 && actual->ts.type != BT_HOLLERITH
45a69325 2428 && formal->ts.type != BT_ASSUMED
e7ac6a7c 2429 && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
c49ea23d
PT
2430 && !gfc_compare_types (&formal->ts, &actual->ts)
2431 && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
8b704316 2432 && gfc_compare_derived_types (formal->ts.u.derived,
c49ea23d 2433 CLASS_DATA (actual)->ts.u.derived)))
5ad6345e 2434 {
d68e117b 2435 if (where)
e0b9e5f9
TK
2436 {
2437 if (formal->attr.artificial)
2438 {
2439 if (!flag_allow_argument_mismatch || !formal->error)
2440 gfc_error_opt (0, "Type mismatch between actual argument at %L "
2441 "and actual argument at %L (%s/%s).",
2442 &actual->where,
2443 &formal->declared_at,
f61e54e5
ME
2444 gfc_typename (actual),
2445 gfc_dummy_typename (&formal->ts));
e0b9e5f9
TK
2446
2447 formal->error = 1;
2448 }
2449 else
2450 gfc_error_opt (0, "Type mismatch in argument %qs at %L; passed %s "
f61e54e5
ME
2451 "to %s", formal->name, where, gfc_typename (actual),
2452 gfc_dummy_typename (&formal->ts));
e0b9e5f9 2453 }
f3e1097b 2454 return false;
5ad6345e 2455 }
f18075ff 2456
3d54e576
TB
2457 if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
2458 {
2459 if (where)
2460 gfc_error ("Assumed-type actual argument at %L requires that dummy "
c4100eae 2461 "argument %qs is of assumed type", &actual->where,
3d54e576 2462 formal->name);
f3e1097b 2463 return false;
3d54e576
TB
2464 }
2465
2364250e
SL
2466 /* TS29113 C407c; F2018 C711. */
2467 if (actual->ts.type == BT_ASSUMED
2468 && symbol_rank (formal) == -1
2469 && actual->rank != -1
2470 && !(actual->symtree->n.sym->as
2471 && actual->symtree->n.sym->as->type == AS_ASSUMED_SHAPE))
2472 {
2473 if (where)
2474 gfc_error ("Assumed-type actual argument at %L corresponding to "
2475 "assumed-rank dummy argument %qs must be "
2476 "assumed-shape or assumed-rank",
2477 &actual->where, formal->name);
2478 return false;
2479 }
2480
f18075ff 2481 /* F2008, 12.5.2.5; IR F08/0073. */
67b1d004
JW
2482 if (formal->ts.type == BT_CLASS && formal->attr.class_ok
2483 && actual->expr_type != EXPR_NULL
f18075ff 2484 && ((CLASS_DATA (formal)->attr.class_pointer
86eb9e2f 2485 && formal->attr.intent != INTENT_IN)
5ac13b8e
JW
2486 || CLASS_DATA (formal)->attr.allocatable))
2487 {
2488 if (actual->ts.type != BT_CLASS)
2489 {
2490 if (where)
c4100eae 2491 gfc_error ("Actual argument to %qs at %L must be polymorphic",
5ac13b8e 2492 formal->name, &actual->where);
f3e1097b 2493 return false;
5ac13b8e 2494 }
67b1d004 2495
a8267f8d
TB
2496 if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
2497 && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
2498 CLASS_DATA (formal)->ts.u.derived))
5ac13b8e
JW
2499 {
2500 if (where)
c4100eae 2501 gfc_error ("Actual argument to %qs at %L must have the same "
5ac13b8e 2502 "declared type", formal->name, &actual->where);
f3e1097b 2503 return false;
5ac13b8e
JW
2504 }
2505 }
6de9cd9a 2506
8b704316
PT
2507 /* F08: 12.5.2.5 Allocatable and pointer dummy variables. However, this
2508 is necessary also for F03, so retain error for both.
2509 NOTE: Other type/kind errors pre-empt this error. Since they are F03
2510 compatible, no attempt has been made to channel to this one. */
2511 if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
2512 && (CLASS_DATA (formal)->attr.allocatable
2513 ||CLASS_DATA (formal)->attr.class_pointer))
2514 {
2515 if (where)
c4100eae 2516 gfc_error ("Actual argument to %qs at %L must be unlimited "
8b704316
PT
2517 "polymorphic since the formal argument is a "
2518 "pointer or allocatable unlimited polymorphic "
2519 "entity [F2008: 12.5.2.5]", formal->name,
2520 &actual->where);
f3e1097b 2521 return false;
8b704316
PT
2522 }
2523
fc27115d
PT
2524 if (formal->ts.type == BT_CLASS && formal->attr.class_ok)
2525 codimension = CLASS_DATA (formal)->attr.codimension;
2526 else
2527 codimension = formal->attr.codimension;
2528
2529 if (codimension && !gfc_is_coarray (actual))
d3a9eea2 2530 {
394d3a2e 2531 if (where)
c4100eae 2532 gfc_error ("Actual argument to %qs at %L must be a coarray",
d3a9eea2 2533 formal->name, &actual->where);
f3e1097b 2534 return false;
394d3a2e 2535 }
d3a9eea2 2536
fc27115d 2537 if (codimension && formal->attr.allocatable)
394d3a2e
TB
2538 {
2539 gfc_ref *last = NULL;
a3935ffc 2540
d3a9eea2 2541 for (ref = actual->ref; ref; ref = ref->next)
394d3a2e
TB
2542 if (ref->type == REF_COMPONENT)
2543 last = ref;
d3a9eea2 2544
d3a9eea2 2545 /* F2008, 12.5.2.6. */
394d3a2e
TB
2546 if ((last && last->u.c.component->as->corank != formal->as->corank)
2547 || (!last
2548 && actual->symtree->n.sym->as->corank != formal->as->corank))
d3a9eea2
TB
2549 {
2550 if (where)
c4100eae 2551 gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
d3a9eea2
TB
2552 formal->name, &actual->where, formal->as->corank,
2553 last ? last->u.c.component->as->corank
2554 : actual->symtree->n.sym->as->corank);
f3e1097b 2555 return false;
d3a9eea2 2556 }
394d3a2e 2557 }
fe4e525c 2558
fc27115d 2559 if (codimension)
394d3a2e 2560 {
460263d0 2561 /* F2008, 12.5.2.8 + Corrig 2 (IR F08/0048). */
8179b067 2562 /* F2018, 12.5.2.8. */
fe4e525c
TB
2563 if (formal->attr.dimension
2564 && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
fec5ce24 2565 && actual_attr.dimension
460263d0 2566 && !gfc_is_simply_contiguous (actual, true, true))
fe4e525c
TB
2567 {
2568 if (where)
c4100eae 2569 gfc_error ("Actual argument to %qs at %L must be simply "
460263d0
TB
2570 "contiguous or an element of such an array",
2571 formal->name, &actual->where);
f3e1097b 2572 return false;
fe4e525c 2573 }
fea54935
TB
2574
2575 /* F2008, C1303 and C1304. */
2576 if (formal->attr.intent != INTENT_INOUT
2577 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2578 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2579 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2580 || formal->attr.lock_comp))
2581
2582 {
2583 if (where)
c4100eae 2584 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
fea54935
TB
2585 "which is LOCK_TYPE or has a LOCK_TYPE component",
2586 formal->name, &actual->where);
f3e1097b 2587 return false;
fea54935 2588 }
5df445a2
TB
2589
2590 /* TS18508, C702/C703. */
2591 if (formal->attr.intent != INTENT_INOUT
2592 && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2593 && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2594 && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2595 || formal->attr.event_comp))
2596
2597 {
2598 if (where)
2599 gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2600 "which is EVENT_TYPE or has a EVENT_TYPE component",
2601 formal->name, &actual->where);
f3e1097b 2602 return false;
5df445a2 2603 }
394d3a2e 2604 }
fe4e525c
TB
2605
2606 /* F2008, C1239/C1240. */
2607 if (actual->expr_type == EXPR_VARIABLE
2608 && (actual->symtree->n.sym->attr.asynchronous
2609 || actual->symtree->n.sym->attr.volatile_)
2610 && (formal->attr.asynchronous || formal->attr.volatile_)
460263d0
TB
2611 && actual->rank && formal->as
2612 && !gfc_is_simply_contiguous (actual, true, false)
f188272d
TB
2613 && ((formal->as->type != AS_ASSUMED_SHAPE
2614 && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
fe4e525c
TB
2615 || formal->attr.contiguous))
2616 {
2617 if (where)
c4100eae 2618 gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
f188272d
TB
2619 "assumed-rank array without CONTIGUOUS attribute - as actual"
2620 " argument at %L is not simply contiguous and both are "
2621 "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
f3e1097b 2622 return false;
d3a9eea2
TB
2623 }
2624
fc27115d 2625 if (formal->attr.allocatable && !codimension
fec5ce24 2626 && actual_attr.codimension)
427180d2
TB
2627 {
2628 if (formal->attr.intent == INTENT_OUT)
2629 {
2630 if (where)
2631 gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
c4100eae 2632 "INTENT(OUT) dummy argument %qs", &actual->where,
427180d2 2633 formal->name);
f3e1097b 2634 return false;
427180d2 2635 }
73e42eef 2636 else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
48749dbc
MLI
2637 gfc_warning (OPT_Wsurprising,
2638 "Passing coarray at %L to allocatable, noncoarray dummy "
2639 "argument %qs, which is invalid if the allocation status"
427180d2
TB
2640 " is modified", &actual->where, formal->name);
2641 }
2642
c62c6622
TB
2643 /* If the rank is the same or the formal argument has assumed-rank. */
2644 if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
f3e1097b 2645 return true;
6de9cd9a 2646
5ad6345e
TB
2647 rank_check = where != NULL && !is_elemental && formal->as
2648 && (formal->as->type == AS_ASSUMED_SHAPE
d8a8dab3
TB
2649 || formal->as->type == AS_DEFERRED)
2650 && actual->expr_type != EXPR_NULL;
6de9cd9a 2651
e7ac6a7c
TB
2652 /* Skip rank checks for NO_ARG_CHECK. */
2653 if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
f3e1097b 2654 return true;
e7ac6a7c 2655
d3a9eea2 2656 /* Scalar & coindexed, see: F2008, Section 12.5.2.4. */
d8a8dab3
TB
2657 if (rank_check || ranks_must_agree
2658 || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
5ad6345e 2659 || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
c49ea23d
PT
2660 || (actual->rank == 0
2661 && ((formal->ts.type == BT_CLASS
2662 && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
2663 || (formal->ts.type != BT_CLASS
2664 && formal->as->type == AS_ASSUMED_SHAPE))
08857b61 2665 && actual->expr_type != EXPR_NULL)
d3a9eea2 2666 || (actual->rank == 0 && formal->attr.dimension
7a40f2e7
SL
2667 && gfc_is_coindexed (actual))
2668 /* Assumed-rank actual argument; F2018 C838. */
2669 || actual->rank == -1)
5ad6345e 2670 {
a5baf71b 2671 if (where
4a4fc7fe
TK
2672 && (!formal->attr.artificial || (!formal->maybe_array
2673 && !maybe_dummy_array_arg (actual))))
e0b9e5f9
TK
2674 {
2675 locus *where_formal;
2676 if (formal->attr.artificial)
2677 where_formal = &formal->declared_at;
2678 else
2679 where_formal = NULL;
2680
2681 argument_rank_mismatch (formal->name, &actual->where,
2682 symbol_rank (formal), actual->rank,
2683 where_formal);
2684 }
f3e1097b 2685 return false;
5ad6345e
TB
2686 }
2687 else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
f3e1097b 2688 return true;
5ad6345e
TB
2689
2690 /* At this point, we are considering a scalar passed to an array. This
975b975b 2691 is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
5ad6345e 2692 - if the actual argument is (a substring of) an element of a
975b975b
TB
2693 non-assumed-shape/non-pointer/non-polymorphic array; or
2694 - (F2003) if the actual argument is of type character of default/c_char
59f6dea9
TB
2695 kind.
2696 - (F2018) if the dummy argument is type(*). */
975b975b
TB
2697
2698 is_pointer = actual->expr_type == EXPR_VARIABLE
2699 ? actual->symtree->n.sym->attr.pointer : false;
6de9cd9a
DN
2700
2701 for (ref = actual->ref; ref; ref = ref->next)
975b975b
TB
2702 {
2703 if (ref->type == REF_COMPONENT)
2704 is_pointer = ref->u.c.component->attr.pointer;
2705 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2706 && ref->u.ar.dimen > 0
8b704316 2707 && (!ref->next
975b975b
TB
2708 || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
2709 break;
2710 }
2711
2712 if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
2713 {
2714 if (where)
c4100eae 2715 gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
975b975b 2716 "at %L", formal->name, &actual->where);
f3e1097b 2717 return false;
975b975b
TB
2718 }
2719
2720 if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
2721 && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2722 {
2723 if (where)
4a4fc7fe
TK
2724 {
2725 if (formal->attr.artificial)
2726 gfc_error ("Element of assumed-shape or pointer array "
7260547d
TK
2727 "as actual argument at %L cannot correspond to "
2728 "actual argument at %L",
4a4fc7fe
TK
2729 &actual->where, &formal->declared_at);
2730 else
2731 gfc_error ("Element of assumed-shape or pointer "
2732 "array passed to array dummy argument %qs at %L",
2733 formal->name, &actual->where);
2734 }
f3e1097b 2735 return false;
975b975b 2736 }
6de9cd9a 2737
975b975b
TB
2738 if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
2739 && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
5ad6345e 2740 {
975b975b
TB
2741 if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
2742 {
2743 if (where)
2744 gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
2745 "CHARACTER actual argument with array dummy argument "
c4100eae 2746 "%qs at %L", formal->name, &actual->where);
f3e1097b 2747 return false;
975b975b
TB
2748 }
2749
5ad6345e
TB
2750 if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
2751 {
2752 gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
c4100eae 2753 "array dummy argument %qs at %L",
5ad6345e 2754 formal->name, &actual->where);
f3e1097b 2755 return false;
5ad6345e 2756 }
5ad6345e 2757 else
f3e1097b 2758 return ((gfc_option.allow_std & GFC_STD_F2003) != 0);
5ad6345e 2759 }
975b975b
TB
2760
2761 if (ref == NULL && actual->expr_type != EXPR_NULL)
5ad6345e 2762 {
59f6dea9
TB
2763 if (actual->rank == 0
2764 && formal->ts.type == BT_ASSUMED
2765 && formal->as
2766 && formal->as->type == AS_ASSUMED_SIZE)
2767 /* This is new in F2018, type(*) is new in TS29113, but gfortran does
2768 not differentiate. Thus, if type(*) exists, it is valid;
2769 otherwise, type(*) is already rejected. */
2770 return true;
a5baf71b 2771 if (where
4a4fc7fe
TK
2772 && (!formal->attr.artificial || (!formal->maybe_array
2773 && !maybe_dummy_array_arg (actual))))
e0b9e5f9
TK
2774 {
2775 locus *where_formal;
2776 if (formal->attr.artificial)
2777 where_formal = &formal->declared_at;
2778 else
2779 where_formal = NULL;
2780
2781 argument_rank_mismatch (formal->name, &actual->where,
2782 symbol_rank (formal), actual->rank,
2783 where_formal);
2784 }
f3e1097b 2785 return false;
5ad6345e
TB
2786 }
2787
f3e1097b 2788 return true;
6de9cd9a
DN
2789}
2790
2791
2d5b90b2
TB
2792/* Returns the storage size of a symbol (formal argument) or
2793 zero if it cannot be determined. */
2794
2795static unsigned long
2796get_sym_storage_size (gfc_symbol *sym)
2797{
2798 int i;
2799 unsigned long strlen, elements;
2800
2801 if (sym->ts.type == BT_CHARACTER)
2802 {
bc21d315 2803 if (sym->ts.u.cl && sym->ts.u.cl->length
600956c8
HA
2804 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2805 && sym->ts.u.cl->length->ts.type == BT_INTEGER)
bc21d315 2806 strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
2d5b90b2
TB
2807 else
2808 return 0;
2809 }
2810 else
8b704316 2811 strlen = 1;
2d5b90b2
TB
2812
2813 if (symbol_rank (sym) == 0)
2814 return strlen;
2815
2816 elements = 1;
2817 if (sym->as->type != AS_EXPLICIT)
2818 return 0;
2819 for (i = 0; i < sym->as->rank; i++)
2820 {
efb63364 2821 if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
600956c8
HA
2822 || sym->as->lower[i]->expr_type != EXPR_CONSTANT
2823 || sym->as->upper[i]->ts.type != BT_INTEGER
2824 || sym->as->lower[i]->ts.type != BT_INTEGER)
2d5b90b2
TB
2825 return 0;
2826
c13af44b
SK
2827 elements *= mpz_get_si (sym->as->upper[i]->value.integer)
2828 - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
2d5b90b2
TB
2829 }
2830
2831 return strlen*elements;
2832}
2833
2834
2835/* Returns the storage size of an expression (actual argument) or
2836 zero if it cannot be determined. For an array element, it returns
1207ac67 2837 the remaining size as the element sequence consists of all storage
2d5b90b2
TB
2838 units of the actual argument up to the end of the array. */
2839
2840static unsigned long
2841get_expr_storage_size (gfc_expr *e)
2842{
2843 int i;
2844 long int strlen, elements;
6da0839a 2845 long int substrlen = 0;
a0710c29 2846 bool is_str_storage = false;
2d5b90b2
TB
2847 gfc_ref *ref;
2848
2849 if (e == NULL)
2850 return 0;
8b704316 2851
2d5b90b2
TB
2852 if (e->ts.type == BT_CHARACTER)
2853 {
bc21d315
JW
2854 if (e->ts.u.cl && e->ts.u.cl->length
2855 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
2856 strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
2d5b90b2 2857 else if (e->expr_type == EXPR_CONSTANT
bc21d315 2858 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
2d5b90b2
TB
2859 strlen = e->value.character.length;
2860 else
2861 return 0;
2862 }
2863 else
2864 strlen = 1; /* Length per element. */
2865
2866 if (e->rank == 0 && !e->ref)
2867 return strlen;
2868
2869 elements = 1;
2870 if (!e->ref)
2871 {
2872 if (!e->shape)
2873 return 0;
2874 for (i = 0; i < e->rank; i++)
2875 elements *= mpz_get_si (e->shape[i]);
2876 return elements*strlen;
2877 }
2878
2879 for (ref = e->ref; ref; ref = ref->next)
2880 {
6da0839a
TB
2881 if (ref->type == REF_SUBSTRING && ref->u.ss.start
2882 && ref->u.ss.start->expr_type == EXPR_CONSTANT)
2883 {
a0710c29
TB
2884 if (is_str_storage)
2885 {
2886 /* The string length is the substring length.
2887 Set now to full string length. */
e323640f 2888 if (!ref->u.ss.length || !ref->u.ss.length->length
a0710c29
TB
2889 || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
2890 return 0;
2891
2892 strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
2893 }
2894 substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
6da0839a
TB
2895 continue;
2896 }
2897
efb63364 2898 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2d5b90b2
TB
2899 for (i = 0; i < ref->u.ar.dimen; i++)
2900 {
2901 long int start, end, stride;
2902 stride = 1;
37639728 2903
2d5b90b2
TB
2904 if (ref->u.ar.stride[i])
2905 {
2906 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT)
2907 stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
2908 else
2909 return 0;
2910 }
2911
2912 if (ref->u.ar.start[i])
2913 {
2914 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT)
2915 start = mpz_get_si (ref->u.ar.start[i]->value.integer);
2916 else
2917 return 0;
2918 }
37639728
TB
2919 else if (ref->u.ar.as->lower[i]
2920 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT)
2921 start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
2922 else
2923 return 0;
2d5b90b2
TB
2924
2925 if (ref->u.ar.end[i])
2926 {
2927 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT)
2928 end = mpz_get_si (ref->u.ar.end[i]->value.integer);
2929 else
2930 return 0;
2931 }
2932 else if (ref->u.ar.as->upper[i]
2933 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
2934 end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
2935 else
2936 return 0;
2937
2938 elements *= (end - start)/stride + 1L;
2939 }
c6423ef3 2940 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
2d5b90b2
TB
2941 for (i = 0; i < ref->u.ar.as->rank; i++)
2942 {
2943 if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
2944 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
edcc76d5
SK
2945 && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER
2946 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
2947 && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
da9ad923
TB
2948 elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2949 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2d5b90b2
TB
2950 + 1L;
2951 else
2952 return 0;
2953 }
6da0839a 2954 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
a0710c29
TB
2955 && e->expr_type == EXPR_VARIABLE)
2956 {
93302a24 2957 if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
a0710c29
TB
2958 || e->symtree->n.sym->attr.pointer)
2959 {
2960 elements = 1;
2961 continue;
2962 }
2963
2964 /* Determine the number of remaining elements in the element
2965 sequence for array element designators. */
2966 is_str_storage = true;
2967 for (i = ref->u.ar.dimen - 1; i >= 0; i--)
2968 {
2969 if (ref->u.ar.start[i] == NULL
2970 || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
2971 || ref->u.ar.as->upper[i] == NULL
2972 || ref->u.ar.as->lower[i] == NULL
2973 || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
2974 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT)
2975 return 0;
2976
2977 elements
2978 = elements
2979 * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2980 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2981 + 1L)
2982 - (mpz_get_si (ref->u.ar.start[i]->value.integer)
2983 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
2984 }
2985 }
3436db75
JW
2986 else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
2987 && ref->u.c.component->attr.proc_pointer
2988 && ref->u.c.component->attr.dimension)
2989 {
2990 /* Array-valued procedure-pointer components. */
2991 gfc_array_spec *as = ref->u.c.component->as;
2992 for (i = 0; i < as->rank; i++)
2993 {
2994 if (!as->upper[i] || !as->lower[i]
2995 || as->upper[i]->expr_type != EXPR_CONSTANT
2996 || as->lower[i]->expr_type != EXPR_CONSTANT)
2997 return 0;
2998
2999 elements = elements
3000 * (mpz_get_si (as->upper[i]->value.integer)
3001 - mpz_get_si (as->lower[i]->value.integer) + 1L);
3002 }
3003 }
2d5b90b2
TB
3004 }
3005
6da0839a 3006 if (substrlen)
a0710c29
TB
3007 return (is_str_storage) ? substrlen + (elements-1)*strlen
3008 : elements*strlen;
3009 else
3010 return elements*strlen;
2d5b90b2
TB
3011}
3012
3013
59be8071 3014/* Given an expression, check whether it is an array section
f3e1097b 3015 which has a vector subscript. */
59be8071 3016
f3e1097b 3017bool
03af1e4c 3018gfc_has_vector_subscript (gfc_expr *e)
59be8071
TB
3019{
3020 int i;
3021 gfc_ref *ref;
3022
3023 if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
f3e1097b 3024 return false;
59be8071
TB
3025
3026 for (ref = e->ref; ref; ref = ref->next)
3027 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3028 for (i = 0; i < ref->u.ar.dimen; i++)
3029 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
f3e1097b 3030 return true;
59be8071 3031
f3e1097b 3032 return false;
59be8071
TB
3033}
3034
3035
4294c093
JW
3036static bool
3037is_procptr_result (gfc_expr *expr)
3038{
3039 gfc_component *c = gfc_get_proc_ptr_comp (expr);
3040 if (c)
3041 return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
3042 else
3043 return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
3044 && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
3045}
3046
3047
bcc478b9
BRF
3048/* Recursively append candidate argument ARG to CANDIDATES. Store the
3049 number of total candidates in CANDIDATES_LEN. */
3050
3051static void
3052lookup_arg_fuzzy_find_candidates (gfc_formal_arglist *arg,
3053 char **&candidates,
3054 size_t &candidates_len)
3055{
3056 for (gfc_formal_arglist *p = arg; p && p->sym; p = p->next)
3057 vec_push (candidates, candidates_len, p->sym->name);
3058}
3059
3060
3061/* Lookup argument ARG fuzzily, taking names in ARGUMENTS into account. */
3062
3063static const char*
3064lookup_arg_fuzzy (const char *arg, gfc_formal_arglist *arguments)
3065{
3066 char **candidates = NULL;
3067 size_t candidates_len = 0;
3068 lookup_arg_fuzzy_find_candidates (arguments, candidates, candidates_len);
3069 return gfc_closest_fuzzy_match (arg, candidates);
3070}
3071
3072
5888512f
MM
3073static gfc_dummy_arg *
3074get_nonintrinsic_dummy_arg (gfc_formal_arglist *formal)
3075{
3076 gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
3077
3078 dummy_arg->intrinsicness = GFC_NON_INTRINSIC_DUMMY_ARG;
3079 dummy_arg->u.non_intrinsic = formal;
3080
3081 return dummy_arg;
3082}
3083
3084
6de9cd9a
DN
3085/* Given formal and actual argument lists, see if they are compatible.
3086 If they are compatible, the actual argument list is sorted to
3087 correspond with the formal list, and elements for missing optional
3088 arguments are inserted. If WHERE pointer is nonnull, then we issue
3089 errors when things don't match instead of just returning the status
3090 code. */
3091
e68a35ae
TK
3092bool
3093gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
3094 int ranks_must_agree, int is_elemental,
3095 bool in_statement_function, locus *where)
6de9cd9a 3096{
fab27f52 3097 gfc_actual_arglist **new_arg, *a, *actual;
6de9cd9a
DN
3098 gfc_formal_arglist *f;
3099 int i, n, na;
2d5b90b2 3100 unsigned long actual_size, formal_size;
c49ea23d 3101 bool full_array = false;
eb401400 3102 gfc_array_ref *actual_arr_ref;
7afb6108
SL
3103 gfc_array_spec *fas, *aas;
3104 bool pointer_dummy, pointer_arg, allocatable_arg;
6de9cd9a 3105
ee11be7f
SL
3106 bool ok = true;
3107
6de9cd9a
DN
3108 actual = *ap;
3109
3110 if (actual == NULL && formal == NULL)
f3e1097b 3111 return true;
6de9cd9a
DN
3112
3113 n = 0;
3114 for (f = formal; f; f = f->next)
3115 n++;
3116
1145e690 3117 new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
6de9cd9a
DN
3118
3119 for (i = 0; i < n; i++)
7b901ac4 3120 new_arg[i] = NULL;
6de9cd9a
DN
3121
3122 na = 0;
3123 f = formal;
3124 i = 0;
3125
3126 for (a = actual; a; a = a->next, f = f->next)
3127 {
3453b6aa
SK
3128 if (a->name != NULL && in_statement_function)
3129 {
3130 gfc_error ("Keyword argument %qs at %L is invalid in "
3131 "a statement function", a->name, &a->expr->where);
3132 return false;
3133 }
3134
7fcafa71
PT
3135 /* Look for keywords but ignore g77 extensions like %VAL. */
3136 if (a->name != NULL && a->name[0] != '%')
6de9cd9a
DN
3137 {
3138 i = 0;
3139 for (f = formal; f; f = f->next, i++)
3140 {
3141 if (f->sym == NULL)
3142 continue;
3143 if (strcmp (f->sym->name, a->name) == 0)
3144 break;
3145 }
3146
3147 if (f == NULL)
3148 {
3149 if (where)
bcc478b9
BRF
3150 {
3151 const char *guessed = lookup_arg_fuzzy (a->name, formal);
3152 if (guessed)
3153 gfc_error ("Keyword argument %qs at %L is not in "
3154 "the procedure; did you mean %qs?",
3155 a->name, &a->expr->where, guessed);
3156 else
3157 gfc_error ("Keyword argument %qs at %L is not in "
3158 "the procedure", a->name, &a->expr->where);
3159 }
f3e1097b 3160 return false;
6de9cd9a
DN
3161 }
3162
7b901ac4 3163 if (new_arg[i] != NULL)
6de9cd9a
DN
3164 {
3165 if (where)
c4100eae 3166 gfc_error ("Keyword argument %qs at %L is already associated "
b251af97
SK
3167 "with another actual argument", a->name,
3168 &a->expr->where);
f3e1097b 3169 return false;
6de9cd9a
DN
3170 }
3171 }
3172
3173 if (f == NULL)
3174 {
3175 if (where)
b251af97
SK
3176 gfc_error ("More actual than formal arguments in procedure "
3177 "call at %L", where);
f3e1097b 3178 return false;
6de9cd9a
DN
3179 }
3180
3181 if (f->sym == NULL && a->expr == NULL)
3182 goto match;
3183
3184 if (f->sym == NULL)
3185 {
866664a3
TK
3186 /* These errors have to be issued, otherwise an ICE can occur.
3187 See PR 78865. */
6de9cd9a 3188 if (where)
866664a3
TK
3189 gfc_error_now ("Missing alternate return specifier in subroutine "
3190 "call at %L", where);
f3e1097b 3191 return false;
6de9cd9a 3192 }
5888512f
MM
3193 else
3194 a->associated_dummy = get_nonintrinsic_dummy_arg (f);
6de9cd9a
DN
3195
3196 if (a->expr == NULL)
3197 {
fb078366
TK
3198 if (f->sym->attr.optional)
3199 continue;
3200 else
3201 {
3202 if (where)
3203 gfc_error_now ("Unexpected alternate return specifier in "
3204 "subroutine call at %L", where);
3205 return false;
3206 }
6de9cd9a 3207 }
08857b61 3208
8b704316
PT
3209 /* Make sure that intrinsic vtables exist for calls to unlimited
3210 polymorphic formal arguments. */
524af0d6 3211 if (UNLIMITED_POLY (f->sym)
8b704316 3212 && a->expr->ts.type != BT_DERIVED
15e5858f
TK
3213 && a->expr->ts.type != BT_CLASS
3214 && a->expr->ts.type != BT_ASSUMED)
7289d1c9 3215 gfc_find_vtab (&a->expr->ts);
8b704316 3216
99091b70
TB
3217 if (a->expr->expr_type == EXPR_NULL
3218 && ((f->sym->ts.type != BT_CLASS && !f->sym->attr.pointer
3219 && (f->sym->attr.allocatable || !f->sym->attr.optional
3220 || (gfc_option.allow_std & GFC_STD_F2008) == 0))
3221 || (f->sym->ts.type == BT_CLASS
3222 && !CLASS_DATA (f->sym)->attr.class_pointer
3223 && (CLASS_DATA (f->sym)->attr.allocatable
3224 || !f->sym->attr.optional
3225 || (gfc_option.allow_std & GFC_STD_F2008) == 0))))
08857b61 3226 {
99091b70
TB
3227 if (where
3228 && (!f->sym->attr.optional
3229 || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
3230 || (f->sym->ts.type == BT_CLASS
3231 && CLASS_DATA (f->sym)->attr.allocatable)))
c4100eae 3232 gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
08857b61
TB
3233 where, f->sym->name);
3234 else if (where)
3235 gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
c4100eae 3236 "dummy %qs", where, f->sym->name);
ee11be7f
SL
3237 ok = false;
3238 goto match;
08857b61 3239 }
8b704316 3240
5ad6345e
TB
3241 if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
3242 is_elemental, where))
ee11be7f
SL
3243 {
3244 ok = false;
3245 goto match;
3246 }
6de9cd9a 3247
5098e707 3248 /* TS 29113, 6.3p2; F2018 15.5.2.4. */
45a69325
TB
3249 if (f->sym->ts.type == BT_ASSUMED
3250 && (a->expr->ts.type == BT_DERIVED
3251 || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
3252 {
5098e707
SL
3253 gfc_symbol *derived = (a->expr->ts.type == BT_DERIVED
3254 ? a->expr->ts.u.derived
3255 : CLASS_DATA (a->expr)->ts.u.derived);
3256 gfc_namespace *f2k_derived = derived->f2k_derived;
3257 if (derived->attr.pdt_type
3258 || (f2k_derived
3259 && (f2k_derived->finalizers || f2k_derived->tb_sym_root)))
45a69325 3260 {
5098e707
SL
3261 gfc_error ("Actual argument at %L to assumed-type dummy "
3262 "has type parameters or is of "
45a69325
TB
3263 "derived type with type-bound or FINAL procedures",
3264 &a->expr->where);
ee11be7f
SL
3265 ok = false;
3266 goto match;
45a69325
TB
3267 }
3268 }
3269
a0710c29
TB
3270 /* Special case for character arguments. For allocatable, pointer
3271 and assumed-shape dummies, the string length needs to match
3272 exactly. */
2d5b90b2 3273 if (a->expr->ts.type == BT_CHARACTER
eb401400
AV
3274 && a->expr->ts.u.cl && a->expr->ts.u.cl->length
3275 && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
3276 && f->sym->ts.type == BT_CHARACTER && f->sym->ts.u.cl
3277 && f->sym->ts.u.cl->length
3278 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
3279 && (f->sym->attr.pointer || f->sym->attr.allocatable
3280 || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
3281 && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
3282 f->sym->ts.u.cl->length->value.integer) != 0))
3283 {
3284 if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
e0b9e5f9 3285 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
eb401400
AV
3286 "argument and pointer or allocatable dummy argument "
3287 "%qs at %L",
3288 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3289 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3290 f->sym->name, &a->expr->where);
3291 else if (where)
e0b9e5f9 3292 gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
eb401400
AV
3293 "argument and assumed-shape dummy argument %qs "
3294 "at %L",
3295 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3296 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3297 f->sym->name, &a->expr->where);
ee11be7f
SL
3298 ok = false;
3299 goto match;
eb401400 3300 }
a0324f7b 3301
8d51f26f 3302 if ((f->sym->attr.pointer || f->sym->attr.allocatable)
eb401400
AV
3303 && f->sym->ts.deferred != a->expr->ts.deferred
3304 && a->expr->ts.type == BT_CHARACTER)
8d51f26f
PT
3305 {
3306 if (where)
0c133211 3307 gfc_error ("Actual argument at %L to allocatable or "
c4100eae 3308 "pointer dummy argument %qs must have a deferred "
8d51f26f
PT
3309 "length type parameter if and only if the dummy has one",
3310 &a->expr->where, f->sym->name);
ee11be7f
SL
3311 ok = false;
3312 goto match;
8d51f26f
PT
3313 }
3314
c49ea23d
PT
3315 if (f->sym->ts.type == BT_CLASS)
3316 goto skip_size_check;
3317
37639728
TB
3318 actual_size = get_expr_storage_size (a->expr);
3319 formal_size = get_sym_storage_size (f->sym);
93302a24
JW
3320 if (actual_size != 0 && actual_size < formal_size
3321 && a->expr->ts.type != BT_PROCEDURE
3322 && f->sym->attr.flavor != FL_PROCEDURE)
2d5b90b2
TB
3323 {
3324 if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
a8b79cc9
HA
3325 {
3326 gfc_warning (0, "Character length of actual argument shorter "
3327 "than of dummy argument %qs (%lu/%lu) at %L",
3328 f->sym->name, actual_size, formal_size,
3329 &a->expr->where);
3330 goto skip_size_check;
3331 }
2d5b90b2 3332 else if (where)
37d92a7e
DH
3333 {
3334 /* Emit a warning for -std=legacy and an error otherwise. */
3335 if (gfc_option.warn_std == 0)
e0b9e5f9 3336 gfc_warning (0, "Actual argument contains too few "
37d92a7e
DH
3337 "elements for dummy argument %qs (%lu/%lu) "
3338 "at %L", f->sym->name, actual_size,
3339 formal_size, &a->expr->where);
3340 else
3341 gfc_error_now ("Actual argument contains too few "
3342 "elements for dummy argument %qs (%lu/%lu) "
3343 "at %L", f->sym->name, actual_size,
3344 formal_size, &a->expr->where);
3345 }
ee11be7f
SL
3346 ok = false;
3347 goto match;
2d5b90b2
TB
3348 }
3349
c49ea23d
PT
3350 skip_size_check:
3351
e9355cc3
JW
3352 /* Satisfy F03:12.4.1.3 by ensuring that a procedure pointer actual
3353 argument is provided for a procedure pointer formal argument. */
8fb74da4 3354 if (f->sym->attr.proc_pointer
a7c0b11d 3355 && !((a->expr->expr_type == EXPR_VARIABLE
4294c093
JW
3356 && (a->expr->symtree->n.sym->attr.proc_pointer
3357 || gfc_is_proc_ptr_comp (a->expr)))
a7c0b11d 3358 || (a->expr->expr_type == EXPR_FUNCTION
4294c093 3359 && is_procptr_result (a->expr))))
8fb74da4
JW
3360 {
3361 if (where)
c4100eae 3362 gfc_error ("Expected a procedure pointer for argument %qs at %L",
8fb74da4 3363 f->sym->name, &a->expr->where);
ee11be7f
SL
3364 ok = false;
3365 goto match;
8fb74da4
JW
3366 }
3367
e9355cc3 3368 /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
699fa7aa 3369 provided for a procedure formal argument. */
e9355cc3 3370 if (f->sym->attr.flavor == FL_PROCEDURE
4294c093
JW
3371 && !((a->expr->expr_type == EXPR_VARIABLE
3372 && (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
3373 || a->expr->symtree->n.sym->attr.proc_pointer
3374 || gfc_is_proc_ptr_comp (a->expr)))
3375 || (a->expr->expr_type == EXPR_FUNCTION
3376 && is_procptr_result (a->expr))))
699fa7aa 3377 {
9914f8cf 3378 if (where)
c4100eae 3379 gfc_error ("Expected a procedure for argument %qs at %L",
9914f8cf 3380 f->sym->name, &a->expr->where);
ee11be7f
SL
3381 ok = false;
3382 goto match;
699fa7aa
PT
3383 }
3384
7afb6108
SL
3385 /* Class array variables and expressions store array info in a
3386 different place from non-class objects; consolidate the logic
3387 to access it here instead of repeating it below. Note that
3388 pointer_arg and allocatable_arg are not fully general and are
3389 only used in a specific situation below with an assumed-rank
3390 argument. */
3391 if (f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym))
3392 {
3393 gfc_component *classdata = CLASS_DATA (f->sym);
3394 fas = classdata->as;
3395 pointer_dummy = classdata->attr.class_pointer;
3396 }
3397 else
3398 {
3399 fas = f->sym->as;
3400 pointer_dummy = f->sym->attr.pointer;
3401 }
3402
3403 if (a->expr->expr_type != EXPR_VARIABLE)
3404 {
3405 aas = NULL;
3406 pointer_arg = false;
3407 allocatable_arg = false;
3408 }
3409 else if (a->expr->ts.type == BT_CLASS
3410 && a->expr->symtree->n.sym
3411 && CLASS_DATA (a->expr->symtree->n.sym))
3412 {
3413 gfc_component *classdata = CLASS_DATA (a->expr->symtree->n.sym);
3414 aas = classdata->as;
3415 pointer_arg = classdata->attr.class_pointer;
3416 allocatable_arg = classdata->attr.allocatable;
3417 }
3418 else
3419 {
3420 aas = a->expr->symtree->n.sym->as;
3421 pointer_arg = a->expr->symtree->n.sym->attr.pointer;
3422 allocatable_arg = a->expr->symtree->n.sym->attr.allocatable;
3423 }
3424
3425 /* F2018:9.5.2(2) permits assumed-size whole array expressions as
3426 actual arguments only if the shape is not required; thus it
3427 cannot be passed to an assumed-shape array dummy.
3428 F2018:15.5.2.(2) permits passing a nonpointer actual to an
3429 intent(in) pointer dummy argument and this is accepted by
3430 the compare_pointer check below, but this also requires shape
3431 information.
3432 There's more discussion of this in PR94110. */
3433 if (fas
3434 && (fas->type == AS_ASSUMED_SHAPE
3435 || fas->type == AS_DEFERRED
3436 || (fas->type == AS_ASSUMED_RANK && pointer_dummy))
3437 && aas
3438 && aas->type == AS_ASSUMED_SIZE
bf9d2177
JJ
3439 && (a->expr->ref == NULL
3440 || (a->expr->ref->type == REF_ARRAY
3441 && a->expr->ref->u.ar.type == AR_FULL)))
3442 {
3443 if (where)
c4100eae 3444 gfc_error ("Actual argument for %qs cannot be an assumed-size"
bf9d2177 3445 " array at %L", f->sym->name, where);
ee11be7f
SL
3446 ok = false;
3447 goto match;
bf9d2177
JJ
3448 }
3449
7afb6108
SL
3450 /* Diagnose F2018 C839 (TS29113 C535c). Here the problem is
3451 passing an assumed-size array to an INTENT(OUT) assumed-rank
3452 dummy when it doesn't have the size information needed to run
3453 initializers and finalizers. */
3454 if (f->sym->attr.intent == INTENT_OUT
3455 && fas
3456 && fas->type == AS_ASSUMED_RANK
3457 && aas
3458 && ((aas->type == AS_ASSUMED_SIZE
3459 && (a->expr->ref == NULL
3460 || (a->expr->ref->type == REF_ARRAY
3461 && a->expr->ref->u.ar.type == AR_FULL)))
3462 || (aas->type == AS_ASSUMED_RANK
3463 && !pointer_arg
3464 && !allocatable_arg))
3465 && (a->expr->ts.type == BT_CLASS
3466 || (a->expr->ts.type == BT_DERIVED
3467 && (gfc_is_finalizable (a->expr->ts.u.derived, NULL)
3468 || gfc_has_ultimate_allocatable (a->expr)
3469 || gfc_has_default_initializer
3470 (a->expr->ts.u.derived)))))
3471 {
3472 if (where)
3473 gfc_error ("Actual argument to assumed-rank INTENT(OUT) "
3474 "dummy %qs at %L cannot be of unknown size",
3475 f->sym->name, where);
ee11be7f
SL
3476 ok = false;
3477 goto match;
7afb6108
SL
3478 }
3479
58e7732a 3480 if (a->expr->expr_type != EXPR_NULL)
6de9cd9a 3481 {
58e7732a
JRFS
3482 int cmp = compare_pointer (f->sym, a->expr);
3483 bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);
6de9cd9a 3484
58e7732a
JRFS
3485 if (pre2008 && cmp == 0)
3486 {
3487 if (where)
3488 gfc_error ("Actual argument for %qs at %L must be a pointer",
3489 f->sym->name, &a->expr->where);
3490 ok = false;
3491 goto match;
3492 }
3493
3494 if (pre2008 && cmp == 2)
3495 {
3496 if (where)
3497 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
3498 "pointer dummy %qs", &a->expr->where, f->sym->name);
3499 ok = false;
3500 goto match;
3501 }
3502
3503 if (!pre2008 && cmp == 0)
3504 {
3505 if (where)
3506 gfc_error ("Actual argument for %qs at %L must be a pointer "
3507 "or a valid target for the dummy pointer in a "
3508 "pointer assignment statement",
3509 f->sym->name, &a->expr->where);
3510 ok = false;
3511 goto match;
3512 }
7d54ef80 3513 }
8b704316 3514
7d54ef80 3515
d3a9eea2
TB
3516 /* Fortran 2008, C1242. */
3517 if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
3518 {
3519 if (where)
3520 gfc_error ("Coindexed actual argument at %L to pointer "
c4100eae 3521 "dummy %qs",
d3a9eea2 3522 &a->expr->where, f->sym->name);
ee11be7f
SL
3523 ok = false;
3524 goto match;
d3a9eea2
TB
3525 }
3526
3527 /* Fortran 2008, 12.5.2.5 (no constraint). */
3528 if (a->expr->expr_type == EXPR_VARIABLE
3529 && f->sym->attr.intent != INTENT_IN
3530 && f->sym->attr.allocatable
3531 && gfc_is_coindexed (a->expr))
3532 {
3533 if (where)
3534 gfc_error ("Coindexed actual argument at %L to allocatable "
c4100eae 3535 "dummy %qs requires INTENT(IN)",
d3a9eea2 3536 &a->expr->where, f->sym->name);
ee11be7f
SL
3537 ok = false;
3538 goto match;
d3a9eea2
TB
3539 }
3540
3541 /* Fortran 2008, C1237. */
3542 if (a->expr->expr_type == EXPR_VARIABLE
3543 && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
3544 && gfc_is_coindexed (a->expr)
3545 && (a->expr->symtree->n.sym->attr.volatile_
3546 || a->expr->symtree->n.sym->attr.asynchronous))
3547 {
3548 if (where)
3549 gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
c4100eae 3550 "%L requires that dummy %qs has neither "
d3a9eea2
TB
3551 "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
3552 f->sym->name);
ee11be7f
SL
3553 ok = false;
3554 goto match;
d3a9eea2
TB
3555 }
3556
3557 /* Fortran 2008, 12.5.2.4 (no constraint). */
3558 if (a->expr->expr_type == EXPR_VARIABLE
3559 && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
3560 && gfc_is_coindexed (a->expr)
3561 && gfc_has_ultimate_allocatable (a->expr))
3562 {
3563 if (where)
3564 gfc_error ("Coindexed actual argument at %L with allocatable "
c4100eae 3565 "ultimate component to dummy %qs requires either VALUE "
d3a9eea2 3566 "or INTENT(IN)", &a->expr->where, f->sym->name);
ee11be7f
SL
3567 ok = false;
3568 goto match;
d3a9eea2
TB
3569 }
3570
c49ea23d
PT
3571 if (f->sym->ts.type == BT_CLASS
3572 && CLASS_DATA (f->sym)->attr.allocatable
3573 && gfc_is_class_array_ref (a->expr, &full_array)
3574 && !full_array)
3575 {
3576 if (where)
c4100eae 3577 gfc_error ("Actual CLASS array argument for %qs must be a full "
c49ea23d 3578 "array at %L", f->sym->name, &a->expr->where);
ee11be7f
SL
3579 ok = false;
3580 goto match;
c49ea23d
PT
3581 }
3582
3583
aa08038d 3584 if (a->expr->expr_type != EXPR_NULL
f3e1097b 3585 && !compare_allocatable (f->sym, a->expr))
aa08038d
EE
3586 {
3587 if (where)
c4100eae 3588 gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
aa08038d 3589 f->sym->name, &a->expr->where);
ee11be7f
SL
3590 ok = false;
3591 goto match;
aa08038d
EE
3592 }
3593
a920e94a 3594 /* Check intent = OUT/INOUT for definable actual argument. */
f3883269
SK
3595 if (!in_statement_function
3596 && (f->sym->attr.intent == INTENT_OUT
3597 || f->sym->attr.intent == INTENT_INOUT))
a920e94a 3598 {
8c91ab34
DK
3599 const char* context = (where
3600 ? _("actual argument to INTENT = OUT/INOUT")
3601 : NULL);
a920e94a 3602
bcb4ad36
TB
3603 if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3604 && CLASS_DATA (f->sym)->attr.class_pointer)
3605 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
524af0d6 3606 && !gfc_check_vardef_context (a->expr, true, false, false, context))
ee11be7f
SL
3607 {
3608 ok = false;
3609 goto match;
3610 }
524af0d6 3611 if (!gfc_check_vardef_context (a->expr, false, false, false, context))
ee11be7f
SL
3612 {
3613 ok = false;
3614 goto match;
3615 }
ee7e677f
TB
3616 }
3617
59be8071
TB
3618 if ((f->sym->attr.intent == INTENT_OUT
3619 || f->sym->attr.intent == INTENT_INOUT
84efddb2
DF
3620 || f->sym->attr.volatile_
3621 || f->sym->attr.asynchronous)
03af1e4c 3622 && gfc_has_vector_subscript (a->expr))
59be8071
TB
3623 {
3624 if (where)
84efddb2
DF
3625 gfc_error ("Array-section actual argument with vector "
3626 "subscripts at %L is incompatible with INTENT(OUT), "
3627 "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
c4100eae 3628 "of the dummy argument %qs",
59be8071 3629 &a->expr->where, f->sym->name);
ee11be7f
SL
3630 ok = false;
3631 goto match;
59be8071
TB
3632 }
3633
9bce3c1c
TB
3634 /* C1232 (R1221) For an actual argument which is an array section or
3635 an assumed-shape array, the dummy argument shall be an assumed-
3636 shape array, if the dummy argument has the VOLATILE attribute. */
3637
3638 if (f->sym->attr.volatile_
271dd55c 3639 && a->expr->expr_type == EXPR_VARIABLE
9bce3c1c
TB
3640 && a->expr->symtree->n.sym->as
3641 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
7afb6108 3642 && !(fas && fas->type == AS_ASSUMED_SHAPE))
9bce3c1c
TB
3643 {
3644 if (where)
3645 gfc_error ("Assumed-shape actual argument at %L is "
3646 "incompatible with the non-assumed-shape "
c4100eae 3647 "dummy argument %qs due to VOLATILE attribute",
9bce3c1c 3648 &a->expr->where,f->sym->name);
ee11be7f
SL
3649 ok = false;
3650 goto match;
9bce3c1c
TB
3651 }
3652
eb401400
AV
3653 /* Find the last array_ref. */
3654 actual_arr_ref = NULL;
3655 if (a->expr->ref)
3656 actual_arr_ref = gfc_find_array_ref (a->expr, true);
3657
9bce3c1c 3658 if (f->sym->attr.volatile_
eb401400 3659 && actual_arr_ref && actual_arr_ref->type == AR_SECTION
7afb6108 3660 && !(fas && fas->type == AS_ASSUMED_SHAPE))
9bce3c1c
TB
3661 {
3662 if (where)
3663 gfc_error ("Array-section actual argument at %L is "
3664 "incompatible with the non-assumed-shape "
c4100eae 3665 "dummy argument %qs due to VOLATILE attribute",
eb401400 3666 &a->expr->where, f->sym->name);
ee11be7f
SL
3667 ok = false;
3668 goto match;
9bce3c1c
TB
3669 }
3670
3671 /* C1233 (R1221) For an actual argument which is a pointer array, the
3672 dummy argument shall be an assumed-shape or pointer array, if the
3673 dummy argument has the VOLATILE attribute. */
3674
3675 if (f->sym->attr.volatile_
271dd55c 3676 && a->expr->expr_type == EXPR_VARIABLE
9bce3c1c
TB
3677 && a->expr->symtree->n.sym->attr.pointer
3678 && a->expr->symtree->n.sym->as
7afb6108
SL
3679 && !(fas
3680 && (fas->type == AS_ASSUMED_SHAPE
9bce3c1c
TB
3681 || f->sym->attr.pointer)))
3682 {
3683 if (where)
3684 gfc_error ("Pointer-array actual argument at %L requires "
3685 "an assumed-shape or pointer-array dummy "
c4100eae 3686 "argument %qs due to VOLATILE attribute",
9bce3c1c 3687 &a->expr->where,f->sym->name);
ee11be7f
SL
3688 ok = false;
3689 goto match;
9bce3c1c
TB
3690 }
3691
6de9cd9a
DN
3692 match:
3693 if (a == actual)
3694 na = i;
3695
7b901ac4 3696 new_arg[i++] = a;
6de9cd9a
DN
3697 }
3698
ee11be7f
SL
3699 /* Give up now if we saw any bad argument. */
3700 if (!ok)
3701 return false;
3702
6de9cd9a
DN
3703 /* Make sure missing actual arguments are optional. */
3704 i = 0;
3705 for (f = formal; f; f = f->next, i++)
3706 {
7b901ac4 3707 if (new_arg[i] != NULL)
6de9cd9a 3708 continue;
3ab7b3de
BM
3709 if (f->sym == NULL)
3710 {
3711 if (where)
b251af97
SK
3712 gfc_error ("Missing alternate return spec in subroutine call "
3713 "at %L", where);
f3e1097b 3714 return false;
3ab7b3de 3715 }
eb92cd57
TB
3716 /* For CLASS, the optional attribute might be set at either location. */
3717 if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
3718 && !f->sym->attr.optional)
3719 || (in_statement_function
3720 && (f->sym->attr.optional
3721 || (f->sym->ts.type == BT_CLASS
3722 && CLASS_DATA (f->sym)->attr.optional))))
6de9cd9a
DN
3723 {
3724 if (where)
c4100eae 3725 gfc_error ("Missing actual argument for argument %qs at %L",
6de9cd9a 3726 f->sym->name, where);
f3e1097b 3727 return false;
6de9cd9a
DN
3728 }
3729 }
3730
a85e5696
SL
3731 /* We should have handled the cases where the formal arglist is null
3732 already. */
3733 gcc_assert (n > 0);
3734
6de9cd9a
DN
3735 /* The argument lists are compatible. We now relink a new actual
3736 argument list with null arguments in the right places. The head
3737 of the list remains the head. */
5888512f 3738 for (f = formal, i = 0; f; f = f->next, i++)
7b901ac4 3739 if (new_arg[i] == NULL)
5888512f
MM
3740 {
3741 new_arg[i] = gfc_get_actual_arglist ();
3742 new_arg[i]->associated_dummy = get_nonintrinsic_dummy_arg (f);
3743 }
6de9cd9a
DN
3744
3745 if (na != 0)
3746 {
fab27f52
MM
3747 std::swap (*new_arg[0], *actual);
3748 std::swap (new_arg[0], new_arg[na]);
6de9cd9a
DN
3749 }
3750
3751 for (i = 0; i < n - 1; i++)
7b901ac4 3752 new_arg[i]->next = new_arg[i + 1];
6de9cd9a 3753
7b901ac4 3754 new_arg[i]->next = NULL;
6de9cd9a
DN
3755
3756 if (*ap == NULL && n > 0)
7b901ac4 3757 *ap = new_arg[0];
6de9cd9a 3758
f3e1097b 3759 return true;
6de9cd9a
DN
3760}
3761
3762
3763typedef struct
3764{
3765 gfc_formal_arglist *f;
3766 gfc_actual_arglist *a;
3767}
3768argpair;
3769
3770/* qsort comparison function for argument pairs, with the following
3771 order:
3772 - p->a->expr == NULL
3773 - p->a->expr->expr_type != EXPR_VARIABLE
c5014982 3774 - by gfc_symbol pointer value (larger first). */
6de9cd9a
DN
3775
3776static int
3777pair_cmp (const void *p1, const void *p2)
3778{
3779 const gfc_actual_arglist *a1, *a2;
3780
3781 /* *p1 and *p2 are elements of the to-be-sorted array. */
3782 a1 = ((const argpair *) p1)->a;
3783 a2 = ((const argpair *) p2)->a;
3784 if (!a1->expr)
3785 {
3786 if (!a2->expr)
3787 return 0;
3788 return -1;
3789 }
3790 if (!a2->expr)
3791 return 1;
3792 if (a1->expr->expr_type != EXPR_VARIABLE)
3793 {
3794 if (a2->expr->expr_type != EXPR_VARIABLE)
3795 return 0;
3796 return -1;
3797 }
3798 if (a2->expr->expr_type != EXPR_VARIABLE)
3799 return 1;
c5014982
AM
3800 if (a1->expr->symtree->n.sym > a2->expr->symtree->n.sym)
3801 return -1;
6de9cd9a
DN
3802 return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
3803}
3804
3805
3806/* Given two expressions from some actual arguments, test whether they
3807 refer to the same expression. The analysis is conservative.
524af0d6 3808 Returning false will produce no warning. */
6de9cd9a 3809
524af0d6 3810static bool
b251af97 3811compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
6de9cd9a
DN
3812{
3813 const gfc_ref *r1, *r2;
3814
3815 if (!e1 || !e2
3816 || e1->expr_type != EXPR_VARIABLE
3817 || e2->expr_type != EXPR_VARIABLE
3818 || e1->symtree->n.sym != e2->symtree->n.sym)
524af0d6 3819 return false;
6de9cd9a 3820
e53b6e56 3821 /* TODO: improve comparison, see expr.cc:show_ref(). */
6de9cd9a
DN
3822 for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
3823 {
3824 if (r1->type != r2->type)
524af0d6 3825 return false;
6de9cd9a
DN
3826 switch (r1->type)
3827 {
3828 case REF_ARRAY:
3829 if (r1->u.ar.type != r2->u.ar.type)
524af0d6 3830 return false;
6de9cd9a
DN
3831 /* TODO: At the moment, consider only full arrays;
3832 we could do better. */
3833 if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
524af0d6 3834 return false;
6de9cd9a
DN
3835 break;
3836
3837 case REF_COMPONENT:
3838 if (r1->u.c.component != r2->u.c.component)
524af0d6 3839 return false;
6de9cd9a
DN
3840 break;
3841
3842 case REF_SUBSTRING:
524af0d6 3843 return false;
6de9cd9a 3844
f16be16d
SK
3845 case REF_INQUIRY:
3846 if (e1->symtree->n.sym->ts.type == BT_COMPLEX
3847 && e1->ts.type == BT_REAL && e2->ts.type == BT_REAL
3848 && r1->u.i != r2->u.i)
3849 return false;
3850 break;
3851
6de9cd9a
DN
3852 default:
3853 gfc_internal_error ("compare_actual_expr(): Bad component code");
3854 }
3855 }
3856 if (!r1 && !r2)
524af0d6
JB
3857 return true;
3858 return false;
6de9cd9a
DN
3859}
3860
b251af97 3861
6de9cd9a
DN
3862/* Given formal and actual argument lists that correspond to one
3863 another, check that identical actual arguments aren't not
3864 associated with some incompatible INTENTs. */
3865
524af0d6 3866static bool
b251af97 3867check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
6de9cd9a
DN
3868{
3869 sym_intent f1_intent, f2_intent;
3870 gfc_formal_arglist *f1;
3871 gfc_actual_arglist *a1;
3872 size_t n, i, j;
3873 argpair *p;
524af0d6 3874 bool t = true;
6de9cd9a
DN
3875
3876 n = 0;
3877 for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
3878 {
3879 if (f1 == NULL && a1 == NULL)
3880 break;
3881 if (f1 == NULL || a1 == NULL)
3882 gfc_internal_error ("check_some_aliasing(): List mismatch");
3883 n++;
3884 }
3885 if (n == 0)
3886 return t;
1145e690 3887 p = XALLOCAVEC (argpair, n);
6de9cd9a
DN
3888
3889 for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
3890 {
3891 p[i].f = f1;
3892 p[i].a = a1;
3893 }
3894
3895 qsort (p, n, sizeof (argpair), pair_cmp);
3896
3897 for (i = 0; i < n; i++)
3898 {
3899 if (!p[i].a->expr
3900 || p[i].a->expr->expr_type != EXPR_VARIABLE
3901 || p[i].a->expr->ts.type == BT_PROCEDURE)
3902 continue;
3903 f1_intent = p[i].f->sym->attr.intent;
3904 for (j = i + 1; j < n; j++)
3905 {
3906 /* Expected order after the sort. */
3907 if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
3908 gfc_internal_error ("check_some_aliasing(): corrupted data");
3909
3910 /* Are the expression the same? */
524af0d6 3911 if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
6de9cd9a
DN
3912 break;
3913 f2_intent = p[j].f->sym->attr.intent;
3914 if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
9f1930be
TB
3915 || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
3916 || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
6de9cd9a 3917 {
db30e21c 3918 gfc_warning (0, "Same actual argument associated with INTENT(%s) "
48749dbc 3919 "argument %qs and INTENT(%s) argument %qs at %L",
6de9cd9a
DN
3920 gfc_intent_string (f1_intent), p[i].f->sym->name,
3921 gfc_intent_string (f2_intent), p[j].f->sym->name,
3922 &p[i].a->expr->where);
524af0d6 3923 t = false;
6de9cd9a
DN
3924 }
3925 }
3926 }
3927
3928 return t;
3929}
3930
3931
3932/* Given formal and actual argument lists that correspond to one
3933 another, check that they are compatible in the sense that intents
3934 are not mismatched. */
3935
524af0d6 3936static bool
b251af97 3937check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
6de9cd9a 3938{
f17facac 3939 sym_intent f_intent;
6de9cd9a
DN
3940
3941 for (;; f = f->next, a = a->next)
3942 {
99c39534
TB
3943 gfc_expr *expr;
3944
6de9cd9a
DN
3945 if (f == NULL && a == NULL)
3946 break;
3947 if (f == NULL || a == NULL)
3948 gfc_internal_error ("check_intents(): List mismatch");
3949
99c39534
TB
3950 if (a->expr && a->expr->expr_type == EXPR_FUNCTION
3951 && a->expr->value.function.isym
3952 && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
3953 expr = a->expr->value.function.actual->expr;
3954 else
3955 expr = a->expr;
3956
3957 if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
6de9cd9a
DN
3958 continue;
3959
6de9cd9a
DN
3960 f_intent = f->sym->attr.intent;
3961
99c39534 3962 if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
6de9cd9a 3963 {
bcb4ad36
TB
3964 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3965 && CLASS_DATA (f->sym)->attr.class_pointer)
3966 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
6de9cd9a 3967 {
b251af97
SK
3968 gfc_error ("Procedure argument at %L is local to a PURE "
3969 "procedure and has the POINTER attribute",
99c39534 3970 &expr->where);
524af0d6 3971 return false;
6de9cd9a
DN
3972 }
3973 }
d3a9eea2
TB
3974
3975 /* Fortran 2008, C1283. */
99c39534 3976 if (gfc_pure (NULL) && gfc_is_coindexed (expr))
d3a9eea2
TB
3977 {
3978 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
3979 {
3980 gfc_error ("Coindexed actual argument at %L in PURE procedure "
3981 "is passed to an INTENT(%s) argument",
99c39534 3982 &expr->where, gfc_intent_string (f_intent));
524af0d6 3983 return false;
d3a9eea2
TB
3984 }
3985
bcb4ad36
TB
3986 if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3987 && CLASS_DATA (f->sym)->attr.class_pointer)
3988 || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
d3a9eea2
TB
3989 {
3990 gfc_error ("Coindexed actual argument at %L in PURE procedure "
3991 "is passed to a POINTER dummy argument",
99c39534 3992 &expr->where);
524af0d6 3993 return false;
d3a9eea2
TB
3994 }
3995 }
3996
3997 /* F2008, Section 12.5.2.4. */
99c39534
TB
3998 if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
3999 && gfc_is_coindexed (expr))
d3a9eea2
TB
4000 {
4001 gfc_error ("Coindexed polymorphic actual argument at %L is passed "
c4100eae 4002 "polymorphic dummy argument %qs",
99c39534 4003 &expr->where, f->sym->name);
524af0d6 4004 return false;
d3a9eea2 4005 }
6de9cd9a
DN
4006 }
4007
524af0d6 4008 return true;
6de9cd9a
DN
4009}
4010
4011
4012/* Check how a procedure is used against its interface. If all goes
4013 well, the actual argument list will also end up being properly
4014 sorted. */
4015
524af0d6 4016bool
b251af97 4017gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
6de9cd9a 4018{
f3883269 4019 gfc_actual_arglist *a;
4cbc9039 4020 gfc_formal_arglist *dummy_args;
4a4fc7fe 4021 bool implicit = false;
4cbc9039 4022
a9c5fe7e 4023 /* Warn about calls with an implicit interface. Special case
6bd2c800 4024 for calling a ISO_C_BINDING because c_loc and c_funloc
ca071303
FXC
4025 are pseudo-unknown. Additionally, warn about procedures not
4026 explicitly declared at all if requested. */
8b7a967e 4027 if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
ca071303 4028 {
b31f8023 4029 bool has_implicit_none_export = false;
4a4fc7fe 4030 implicit = true;
b31f8023
TB
4031 if (sym->attr.proc == PROC_UNKNOWN)
4032 for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
4033 if (ns->has_implicit_none_export)
4034 {
4035 has_implicit_none_export = true;
4036 break;
4037 }
4038 if (has_implicit_none_export)
8b7a967e 4039 {
bcc478b9
BRF
4040 const char *guessed
4041 = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
4042 if (guessed)
4043 gfc_error ("Procedure %qs called at %L is not explicitly declared"
4044 "; did you mean %qs?",
4045 sym->name, where, guessed);
4046 else
4047 gfc_error ("Procedure %qs called at %L is not explicitly declared",
4048 sym->name, where);
8b7a967e
TB
4049 return false;
4050 }
73e42eef 4051 if (warn_implicit_interface)
48749dbc
MLI
4052 gfc_warning (OPT_Wimplicit_interface,
4053 "Procedure %qs called with an implicit interface at %L",
ca071303 4054 sym->name, where);
73e42eef 4055 else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
48749dbc
MLI
4056 gfc_warning (OPT_Wimplicit_procedure,
4057 "Procedure %qs called at %L is not explicitly declared",
ca071303 4058 sym->name, where);
ffeebc4f 4059 gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
ca071303 4060 }
6de9cd9a 4061
e6895430 4062 if (sym->attr.if_source == IFSRC_UNKNOWN)
ac05557c 4063 {
86d7449c
TB
4064 if (sym->attr.pointer)
4065 {
c4100eae
MLI
4066 gfc_error ("The pointer object %qs at %L must have an explicit "
4067 "function interface or be declared as array",
4068 sym->name, where);
524af0d6 4069 return false;
86d7449c
TB
4070 }
4071
4072 if (sym->attr.allocatable && !sym->attr.external)
4073 {
c4100eae
MLI
4074 gfc_error ("The allocatable object %qs at %L must have an explicit "
4075 "function interface or be declared as array",
4076 sym->name, where);
524af0d6 4077 return false;
86d7449c
TB
4078 }
4079
4080 if (sym->attr.allocatable)
4081 {
c4100eae
MLI
4082 gfc_error ("Allocatable function %qs at %L must have an explicit "
4083 "function interface", sym->name, where);
524af0d6 4084 return false;
86d7449c
TB
4085 }
4086
ac05557c
DF
4087 for (a = *ap; a; a = a->next)
4088 {
fb078366
TK
4089 if (a->expr && a->expr->error)
4090 return false;
4091
4a4fc7fe
TK
4092 /* F2018, 15.4.2.2 Explicit interface is required for a
4093 polymorphic dummy argument, so there is no way to
4094 legally have a class appear in an argument with an
4095 implicit interface. */
4096
4097 if (implicit && a->expr && a->expr->ts.type == BT_CLASS)
4098 {
4099 gfc_error ("Explicit interface required for polymorphic "
4100 "argument at %L",&a->expr->where);
4101 a->expr->error = 1;
4102 break;
4103 }
4104
ac05557c
DF
4105 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4106 if (a->name != NULL && a->name[0] != '%')
4107 {
c4100eae
MLI
4108 gfc_error ("Keyword argument requires explicit interface "
4109 "for procedure %qs at %L", sym->name, &a->expr->where);
ac05557c
DF
4110 break;
4111 }
fea54935 4112
45a69325
TB
4113 /* TS 29113, 6.2. */
4114 if (a->expr && a->expr->ts.type == BT_ASSUMED
4115 && sym->intmod_sym_id != ISOCBINDING_LOC)
4116 {
4117 gfc_error ("Assumed-type argument %s at %L requires an explicit "
4118 "interface", a->expr->symtree->n.sym->name,
4119 &a->expr->where);
fb078366 4120 a->expr->error = 1;
45a69325
TB
4121 break;
4122 }
4123
fea54935
TB
4124 /* F2008, C1303 and C1304. */
4125 if (a->expr
4126 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
f477062c 4127 && a->expr->ts.u.derived
fea54935
TB
4128 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4129 && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
4130 || gfc_expr_attr (a->expr).lock_comp))
4131 {
c4100eae
MLI
4132 gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
4133 "component at %L requires an explicit interface for "
4134 "procedure %qs", &a->expr->where, sym->name);
fb078366 4135 a->expr->error = 1;
fea54935
TB
4136 break;
4137 }
ea8ad3e5 4138
5df445a2
TB
4139 if (a->expr
4140 && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
f477062c 4141 && a->expr->ts.u.derived
5df445a2
TB
4142 && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4143 && a->expr->ts.u.derived->intmod_sym_id
4144 == ISOFORTRAN_EVENT_TYPE)
4145 || gfc_expr_attr (a->expr).event_comp))
4146 {
4147 gfc_error ("Actual argument of EVENT_TYPE or with EVENT_TYPE "
4148 "component at %L requires an explicit interface for "
4149 "procedure %qs", &a->expr->where, sym->name);
fb078366 4150 a->expr->error = 1;
5df445a2
TB
4151 break;
4152 }
4153
ea8ad3e5
TB
4154 if (a->expr && a->expr->expr_type == EXPR_NULL
4155 && a->expr->ts.type == BT_UNKNOWN)
4156 {
fb078366
TK
4157 gfc_error ("MOLD argument to NULL required at %L",
4158 &a->expr->where);
4159 a->expr->error = 1;
524af0d6 4160 return false;
ea8ad3e5 4161 }
c62c6622
TB
4162
4163 /* TS 29113, C407b. */
4164 if (a->expr && a->expr->expr_type == EXPR_VARIABLE
4165 && symbol_rank (a->expr->symtree->n.sym) == -1)
4166 {
4167 gfc_error ("Assumed-rank argument requires an explicit interface "
4168 "at %L", &a->expr->where);
fb078366 4169 a->expr->error = 1;
524af0d6 4170 return false;
c62c6622 4171 }
ac05557c
DF
4172 }
4173
524af0d6 4174 return true;
ac05557c
DF
4175 }
4176
4cbc9039
JW
4177 dummy_args = gfc_sym_get_dummy_args (sym);
4178
f3883269
SK
4179 /* For a statement function, check that types and type parameters of actual
4180 arguments and dummy arguments match. */
e68a35ae
TK
4181 if (!gfc_compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
4182 sym->attr.proc == PROC_ST_FUNCTION, where))
524af0d6 4183 return false;
a5baf71b 4184
524af0d6
JB
4185 if (!check_intents (dummy_args, *ap))
4186 return false;
6de9cd9a 4187
73e42eef 4188 if (warn_aliasing)
4cbc9039 4189 check_some_aliasing (dummy_args, *ap);
f8552cd4 4190
524af0d6 4191 return true;
6de9cd9a
DN
4192}
4193
4194
7e196f89
JW
4195/* Check how a procedure pointer component is used against its interface.
4196 If all goes well, the actual argument list will also end up being properly
4197 sorted. Completely analogous to gfc_procedure_use. */
4198
4199void
4200gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
4201{
7e196f89 4202 /* Warn about calls with an implicit interface. Special case
6bd2c800 4203 for calling a ISO_C_BINDING because c_loc and c_funloc
7e196f89 4204 are pseudo-unknown. */
73e42eef 4205 if (warn_implicit_interface
7e196f89
JW
4206 && comp->attr.if_source == IFSRC_UNKNOWN
4207 && !comp->attr.is_iso_c)
48749dbc
MLI
4208 gfc_warning (OPT_Wimplicit_interface,
4209 "Procedure pointer component %qs called with an implicit "
7e196f89
JW
4210 "interface at %L", comp->name, where);
4211
4212 if (comp->attr.if_source == IFSRC_UNKNOWN)
4213 {
4214 gfc_actual_arglist *a;
4215 for (a = *ap; a; a = a->next)
4216 {
4217 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
4218 if (a->name != NULL && a->name[0] != '%')
4219 {
c4100eae
MLI
4220 gfc_error ("Keyword argument requires explicit interface "
4221 "for procedure pointer component %qs at %L",
4222 comp->name, &a->expr->where);
7e196f89
JW
4223 break;
4224 }
4225 }
4226
4227 return;
4228 }
4229
e68a35ae 4230 if (!gfc_compare_actual_formal (ap, comp->ts.interface->formal, 0,
f3883269 4231 comp->attr.elemental, false, where))
7e196f89
JW
4232 return;
4233
4cbc9039 4234 check_intents (comp->ts.interface->formal, *ap);
73e42eef 4235 if (warn_aliasing)
4cbc9039 4236 check_some_aliasing (comp->ts.interface->formal, *ap);
7e196f89
JW
4237}
4238
4239
f0ac18b7
DK
4240/* Try if an actual argument list matches the formal list of a symbol,
4241 respecting the symbol's attributes like ELEMENTAL. This is used for
4242 GENERIC resolution. */
4243
4244bool
4245gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
4246{
4cbc9039 4247 gfc_formal_arglist *dummy_args;
f0ac18b7
DK
4248 bool r;
4249
1d101216
JD
4250 if (sym->attr.flavor != FL_PROCEDURE)
4251 return false;
f0ac18b7 4252
4cbc9039
JW
4253 dummy_args = gfc_sym_get_dummy_args (sym);
4254
f0ac18b7 4255 r = !sym->attr.elemental;
e68a35ae 4256 if (gfc_compare_actual_formal (args, dummy_args, r, !r, false, NULL))
f0ac18b7 4257 {
4cbc9039 4258 check_intents (dummy_args, *args);
73e42eef 4259 if (warn_aliasing)
4cbc9039 4260 check_some_aliasing (dummy_args, *args);
f0ac18b7
DK
4261 return true;
4262 }
4263
4264 return false;
4265}
4266
4267
6de9cd9a
DN
4268/* Given an interface pointer and an actual argument list, search for
4269 a formal argument list that matches the actual. If found, returns
4270 a pointer to the symbol of the correct interface. Returns NULL if
4271 not found. */
4272
4273gfc_symbol *
b251af97
SK
4274gfc_search_interface (gfc_interface *intr, int sub_flag,
4275 gfc_actual_arglist **ap)
6de9cd9a 4276{
22a0a780 4277 gfc_symbol *elem_sym = NULL;
ea8ad3e5
TB
4278 gfc_symbol *null_sym = NULL;
4279 locus null_expr_loc;
4280 gfc_actual_arglist *a;
4281 bool has_null_arg = false;
4282
4283 for (a = *ap; a; a = a->next)
4284 if (a->expr && a->expr->expr_type == EXPR_NULL
4285 && a->expr->ts.type == BT_UNKNOWN)
4286 {
4287 has_null_arg = true;
4288 null_expr_loc = a->expr->where;
4289 break;
8b704316 4290 }
ea8ad3e5 4291
6de9cd9a
DN
4292 for (; intr; intr = intr->next)
4293 {
f6288c24 4294 if (gfc_fl_struct (intr->sym->attr.flavor))
c3f34952 4295 continue;
6de9cd9a
DN
4296 if (sub_flag && intr->sym->attr.function)
4297 continue;
4298 if (!sub_flag && intr->sym->attr.subroutine)
4299 continue;
4300
f0ac18b7 4301 if (gfc_arglist_matches_symbol (ap, intr->sym))
22a0a780 4302 {
ea8ad3e5
TB
4303 if (has_null_arg && null_sym)
4304 {
4305 gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
4306 "between specific functions %s and %s",
4307 &null_expr_loc, null_sym->name, intr->sym->name);
4308 return NULL;
4309 }
4310 else if (has_null_arg)
4311 {
4312 null_sym = intr->sym;
4313 continue;
4314 }
4315
22a0a780 4316 /* Satisfy 12.4.4.1 such that an elemental match has lower
8b704316 4317 weight than a non-elemental match. */
22a0a780
PT
4318 if (intr->sym->attr.elemental)
4319 {
4320 elem_sym = intr->sym;
4321 continue;
4322 }
4323 return intr->sym;
4324 }
6de9cd9a
DN
4325 }
4326
ea8ad3e5
TB
4327 if (null_sym)
4328 return null_sym;
4329
22a0a780 4330 return elem_sym ? elem_sym : NULL;
6de9cd9a
DN
4331}
4332
4333
4334/* Do a brute force recursive search for a symbol. */
4335
4336static gfc_symtree *
b251af97 4337find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
6de9cd9a
DN
4338{
4339 gfc_symtree * st;
4340
4341 if (root->n.sym == sym)
4342 return root;
4343
4344 st = NULL;
4345 if (root->left)
4346 st = find_symtree0 (root->left, sym);
4347 if (root->right && ! st)
4348 st = find_symtree0 (root->right, sym);
4349 return st;
4350}
4351
4352
4353/* Find a symtree for a symbol. */
4354
f6fad28e
DK
4355gfc_symtree *
4356gfc_find_sym_in_symtree (gfc_symbol *sym)
6de9cd9a
DN
4357{
4358 gfc_symtree *st;
4359 gfc_namespace *ns;
4360
4361 /* First try to find it by name. */
4362 gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
4363 if (st && st->n.sym == sym)
4364 return st;
4365
66e4ab31 4366 /* If it's been renamed, resort to a brute-force search. */
6de9cd9a
DN
4367 /* TODO: avoid having to do this search. If the symbol doesn't exist
4368 in the symtree for the current namespace, it should probably be added. */
4369 for (ns = gfc_current_ns; ns; ns = ns->parent)
4370 {
4371 st = find_symtree0 (ns->sym_root, sym);
4372 if (st)
b251af97 4373 return st;
6de9cd9a 4374 }
17d5d49f 4375 gfc_internal_error ("Unable to find symbol %qs", sym->name);
66e4ab31 4376 /* Not reached. */
6de9cd9a
DN
4377}
4378
4379
4a44a72d
DK
4380/* See if the arglist to an operator-call contains a derived-type argument
4381 with a matching type-bound operator. If so, return the matching specific
4382 procedure defined as operator-target as well as the base-object to use
974df0f8
PT
4383 (which is the found derived-type argument with operator). The generic
4384 name, if any, is transmitted to the final expression via 'gname'. */
4a44a72d
DK
4385
4386static gfc_typebound_proc*
4387matching_typebound_op (gfc_expr** tb_base,
4388 gfc_actual_arglist* args,
974df0f8
PT
4389 gfc_intrinsic_op op, const char* uop,
4390 const char ** gname)
4a44a72d
DK
4391{
4392 gfc_actual_arglist* base;
4393
4394 for (base = args; base; base = base->next)
4b7dd692 4395 if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
4a44a72d
DK
4396 {
4397 gfc_typebound_proc* tb;
4398 gfc_symbol* derived;
524af0d6 4399 bool result;
4a44a72d 4400
efd2e969
PT
4401 while (base->expr->expr_type == EXPR_OP
4402 && base->expr->value.op.op == INTRINSIC_PARENTHESES)
4403 base->expr = base->expr->value.op.op1;
4404
4b7dd692 4405 if (base->expr->ts.type == BT_CLASS)
528622fd 4406 {
fba5a793 4407 if (!base->expr->ts.u.derived || CLASS_DATA (base->expr) == NULL
0a59e583 4408 || !gfc_expr_attr (base->expr).class_ok)
528622fd
JW
4409 continue;
4410 derived = CLASS_DATA (base->expr)->ts.u.derived;
4411 }
4b7dd692
JW
4412 else
4413 derived = base->expr->ts.u.derived;
4a44a72d
DK
4414
4415 if (op == INTRINSIC_USER)
4416 {
4417 gfc_symtree* tb_uop;
4418
4419 gcc_assert (uop);
4420 tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
4421 false, NULL);
4422
4423 if (tb_uop)
4424 tb = tb_uop->n.tb;
4425 else
4426 tb = NULL;
4427 }
4428 else
4429 tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
4430 false, NULL);
4431
4432 /* This means we hit a PRIVATE operator which is use-associated and
4433 should thus not be seen. */
524af0d6 4434 if (!result)
4a44a72d
DK
4435 tb = NULL;
4436
4437 /* Look through the super-type hierarchy for a matching specific
4438 binding. */
4439 for (; tb; tb = tb->overridden)
4440 {
4441 gfc_tbp_generic* g;
4442
4443 gcc_assert (tb->is_generic);
4444 for (g = tb->u.generic; g; g = g->next)
4445 {
4446 gfc_symbol* target;
4447 gfc_actual_arglist* argcopy;
4448 bool matches;
4449
4450 gcc_assert (g->specific);
4451 if (g->specific->error)
4452 continue;
4453
4454 target = g->specific->u.specific->n.sym;
4455
4456 /* Check if this arglist matches the formal. */
4457 argcopy = gfc_copy_actual_arglist (args);
4458 matches = gfc_arglist_matches_symbol (&argcopy, target);
4459 gfc_free_actual_arglist (argcopy);
4460
4461 /* Return if we found a match. */
4462 if (matches)
4463 {
4464 *tb_base = base->expr;
974df0f8 4465 *gname = g->specific_st->name;
4a44a72d
DK
4466 return g->specific;
4467 }
4468 }
4469 }
4470 }
4471
4472 return NULL;
4473}
4474
4475
4476/* For the 'actual arglist' of an operator call and a specific typebound
4477 procedure that has been found the target of a type-bound operator, build the
4478 appropriate EXPR_COMPCALL and resolve it. We take this indirection over
4479 type-bound procedures rather than resolving type-bound operators 'directly'
4480 so that we can reuse the existing logic. */
4481
4482static void
4483build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
974df0f8
PT
4484 gfc_expr* base, gfc_typebound_proc* target,
4485 const char *gname)
4a44a72d
DK
4486{
4487 e->expr_type = EXPR_COMPCALL;
4488 e->value.compcall.tbp = target;
974df0f8 4489 e->value.compcall.name = gname ? gname : "$op";
4a44a72d
DK
4490 e->value.compcall.actual = actual;
4491 e->value.compcall.base_object = base;
4492 e->value.compcall.ignore_pass = 1;
4493 e->value.compcall.assign = 0;
94fae14b
PT
4494 if (e->ts.type == BT_UNKNOWN
4495 && target->function)
4496 {
4497 if (target->is_generic)
4498 e->ts = target->u.generic->specific->u.specific->n.sym->ts;
4499 else
4500 e->ts = target->u.specific->n.sym->ts;
4501 }
4a44a72d
DK
4502}
4503
4504
6de9cd9a
DN
4505/* This subroutine is called when an expression is being resolved.
4506 The expression node in question is either a user defined operator
1f2959f0 4507 or an intrinsic operator with arguments that aren't compatible
6de9cd9a
DN
4508 with the operator. This subroutine builds an actual argument list
4509 corresponding to the operands, then searches for a compatible
4510 interface. If one is found, the expression node is replaced with
eaee02a5
JW
4511 the appropriate function call. We use the 'match' enum to specify
4512 whether a replacement has been made or not, or if an error occurred. */
6de9cd9a 4513
eaee02a5
JW
4514match
4515gfc_extend_expr (gfc_expr *e)
6de9cd9a
DN
4516{
4517 gfc_actual_arglist *actual;
4518 gfc_symbol *sym;
4519 gfc_namespace *ns;
4520 gfc_user_op *uop;
4521 gfc_intrinsic_op i;
974df0f8 4522 const char *gname;
517d78be
JW
4523 gfc_typebound_proc* tbo;
4524 gfc_expr* tb_base;
6de9cd9a
DN
4525
4526 sym = NULL;
4527
4528 actual = gfc_get_actual_arglist ();
58b03ab2 4529 actual->expr = e->value.op.op1;
6de9cd9a 4530
974df0f8 4531 gname = NULL;
4a44a72d 4532
58b03ab2 4533 if (e->value.op.op2 != NULL)
6de9cd9a
DN
4534 {
4535 actual->next = gfc_get_actual_arglist ();
58b03ab2 4536 actual->next->expr = e->value.op.op2;
6de9cd9a
DN
4537 }
4538
e8d4f3fc 4539 i = fold_unary_intrinsic (e->value.op.op);
6de9cd9a 4540
517d78be
JW
4541 /* See if we find a matching type-bound operator. */
4542 if (i == INTRINSIC_USER)
4543 tbo = matching_typebound_op (&tb_base, actual,
4544 i, e->value.op.uop->name, &gname);
4545 else
4546 switch (i)
4547 {
4548#define CHECK_OS_COMPARISON(comp) \
4549 case INTRINSIC_##comp: \
4550 case INTRINSIC_##comp##_OS: \
4551 tbo = matching_typebound_op (&tb_base, actual, \
4552 INTRINSIC_##comp, NULL, &gname); \
4553 if (!tbo) \
4554 tbo = matching_typebound_op (&tb_base, actual, \
4555 INTRINSIC_##comp##_OS, NULL, &gname); \
4556 break;
4557 CHECK_OS_COMPARISON(EQ)
4558 CHECK_OS_COMPARISON(NE)
4559 CHECK_OS_COMPARISON(GT)
4560 CHECK_OS_COMPARISON(GE)
4561 CHECK_OS_COMPARISON(LT)
4562 CHECK_OS_COMPARISON(LE)
4563#undef CHECK_OS_COMPARISON
4564
4565 default:
4566 tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
4567 break;
4568 }
4569
4570 /* If there is a matching typebound-operator, replace the expression with
4571 a call to it and succeed. */
4572 if (tbo)
4573 {
4574 gcc_assert (tb_base);
4575 build_compcall_for_operator (e, actual, tb_base, tbo, gname);
4576
4577 if (!gfc_resolve_expr (e))
4578 return MATCH_ERROR;
4579 else
4580 return MATCH_YES;
4581 }
e73d3ca6 4582
6de9cd9a
DN
4583 if (i == INTRINSIC_USER)
4584 {
4585 for (ns = gfc_current_ns; ns; ns = ns->parent)
4586 {
58b03ab2 4587 uop = gfc_find_uop (e->value.op.uop->name, ns);
6de9cd9a
DN
4588 if (uop == NULL)
4589 continue;
4590
a1ee985f 4591 sym = gfc_search_interface (uop->op, 0, &actual);
6de9cd9a
DN
4592 if (sym != NULL)
4593 break;
4594 }
4595 }
4596 else
4597 {
4598 for (ns = gfc_current_ns; ns; ns = ns->parent)
4599 {
3bed9dd0
DF
4600 /* Due to the distinction between '==' and '.eq.' and friends, one has
4601 to check if either is defined. */
4602 switch (i)
4603 {
4a44a72d
DK
4604#define CHECK_OS_COMPARISON(comp) \
4605 case INTRINSIC_##comp: \
4606 case INTRINSIC_##comp##_OS: \
4607 sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
4608 if (!sym) \
4609 sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
4610 break;
4611 CHECK_OS_COMPARISON(EQ)
4612 CHECK_OS_COMPARISON(NE)
4613 CHECK_OS_COMPARISON(GT)
4614 CHECK_OS_COMPARISON(GE)
4615 CHECK_OS_COMPARISON(LT)
4616 CHECK_OS_COMPARISON(LE)
4617#undef CHECK_OS_COMPARISON
3bed9dd0
DF
4618
4619 default:
a1ee985f 4620 sym = gfc_search_interface (ns->op[i], 0, &actual);
3bed9dd0
DF
4621 }
4622
6de9cd9a
DN
4623 if (sym != NULL)
4624 break;
4625 }
4626 }
4627
4a44a72d
DK
4628 /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
4629 found rather than just taking the first one and not checking further. */
4630
6de9cd9a
DN
4631 if (sym == NULL)
4632 {
66e4ab31 4633 /* Don't use gfc_free_actual_arglist(). */
04695783 4634 free (actual->next);
cede9502 4635 free (actual);
eaee02a5 4636 return MATCH_NO;
6de9cd9a
DN
4637 }
4638
4639 /* Change the expression node to a function call. */
4640 e->expr_type = EXPR_FUNCTION;
f6fad28e 4641 e->symtree = gfc_find_sym_in_symtree (sym);
6de9cd9a 4642 e->value.function.actual = actual;
58b03ab2
TS
4643 e->value.function.esym = NULL;
4644 e->value.function.isym = NULL;
cf013e9f 4645 e->value.function.name = NULL;
a1ab6660 4646 e->user_operator = 1;
6de9cd9a 4647
524af0d6 4648 if (!gfc_resolve_expr (e))
eaee02a5 4649 return MATCH_ERROR;
6de9cd9a 4650
eaee02a5 4651 return MATCH_YES;
6de9cd9a
DN
4652}
4653
4654
4f7395ff
JW
4655/* Tries to replace an assignment code node with a subroutine call to the
4656 subroutine associated with the assignment operator. Return true if the node
4657 was replaced. On false, no error is generated. */
6de9cd9a 4658
524af0d6 4659bool
b251af97 4660gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
6de9cd9a
DN
4661{
4662 gfc_actual_arglist *actual;
4f7395ff
JW
4663 gfc_expr *lhs, *rhs, *tb_base;
4664 gfc_symbol *sym = NULL;
4665 const char *gname = NULL;
4666 gfc_typebound_proc* tbo;
6de9cd9a 4667
a513927a 4668 lhs = c->expr1;
6de9cd9a
DN
4669 rhs = c->expr2;
4670
8dc63166
SK
4671 /* Don't allow an intrinsic assignment with a BOZ rhs to be replaced. */
4672 if (c->op == EXEC_ASSIGN
4673 && c->expr1->expr_type == EXPR_VARIABLE
4674 && c->expr2->expr_type == EXPR_CONSTANT && c->expr2->ts.type == BT_BOZ)
4675 return false;
4676
6de9cd9a 4677 /* Don't allow an intrinsic assignment to be replaced. */
4b7dd692 4678 if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
e19bb186 4679 && (rhs->rank == 0 || rhs->rank == lhs->rank)
6de9cd9a 4680 && (lhs->ts.type == rhs->ts.type
b251af97 4681 || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
524af0d6 4682 return false;
6de9cd9a
DN
4683
4684 actual = gfc_get_actual_arglist ();
4685 actual->expr = lhs;
4686
4687 actual->next = gfc_get_actual_arglist ();
4688 actual->next->expr = rhs;
4689
4f7395ff
JW
4690 /* TODO: Ambiguity-check, see above for gfc_extend_expr. */
4691
4692 /* See if we find a matching type-bound assignment. */
4693 tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
4694 NULL, &gname);
4695
4696 if (tbo)
4697 {
4698 /* Success: Replace the expression with a type-bound call. */
4699 gcc_assert (tb_base);
4700 c->expr1 = gfc_get_expr ();
4701 build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
4702 c->expr1->value.compcall.assign = 1;
4703 c->expr1->where = c->loc;
4704 c->expr2 = NULL;
4705 c->op = EXEC_COMPCALL;
4706 return true;
4707 }
6de9cd9a 4708
4f7395ff 4709 /* See if we find an 'ordinary' (non-typebound) assignment procedure. */
6de9cd9a
DN
4710 for (; ns; ns = ns->parent)
4711 {
a1ee985f 4712 sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
6de9cd9a
DN
4713 if (sym != NULL)
4714 break;
4715 }
4716
4f7395ff 4717 if (sym)
6de9cd9a 4718 {
4f7395ff
JW
4719 /* Success: Replace the assignment with the call. */
4720 c->op = EXEC_ASSIGN_CALL;
4721 c->symtree = gfc_find_sym_in_symtree (sym);
4722 c->expr1 = NULL;
4723 c->expr2 = NULL;
4724 c->ext.actual = actual;
4725 return true;
6de9cd9a
DN
4726 }
4727
4f7395ff
JW
4728 /* Failure: No assignment procedure found. */
4729 free (actual->next);
4730 free (actual);
4731 return false;
6de9cd9a
DN
4732}
4733
4734
4735/* Make sure that the interface just parsed is not already present in
4736 the given interface list. Ambiguity isn't checked yet since module
4737 procedures can be present without interfaces. */
4738
524af0d6 4739bool
362aa474 4740gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
6de9cd9a
DN
4741{
4742 gfc_interface *ip;
4743
4744 for (ip = base; ip; ip = ip->next)
4745 {
7b901ac4 4746 if (ip->sym == new_sym)
6de9cd9a 4747 {
c4100eae 4748 gfc_error ("Entity %qs at %L is already present in the interface",
362aa474 4749 new_sym->name, &loc);
524af0d6 4750 return false;
6de9cd9a
DN
4751 }
4752 }
4753
524af0d6 4754 return true;
6de9cd9a
DN
4755}
4756
4757
4758/* Add a symbol to the current interface. */
4759
524af0d6 4760bool
7b901ac4 4761gfc_add_interface (gfc_symbol *new_sym)
6de9cd9a
DN
4762{
4763 gfc_interface **head, *intr;
4764 gfc_namespace *ns;
4765 gfc_symbol *sym;
4766
4767 switch (current_interface.type)
4768 {
4769 case INTERFACE_NAMELESS:
9e1d712c 4770 case INTERFACE_ABSTRACT:
524af0d6 4771 return true;
6de9cd9a
DN
4772
4773 case INTERFACE_INTRINSIC_OP:
4774 for (ns = current_interface.ns; ns; ns = ns->parent)
3bed9dd0
DF
4775 switch (current_interface.op)
4776 {
4777 case INTRINSIC_EQ:
4778 case INTRINSIC_EQ_OS:
e73d3ca6 4779 if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym,
524af0d6 4780 gfc_current_locus)
e73d3ca6 4781 || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS],
524af0d6
JB
4782 new_sym, gfc_current_locus))
4783 return false;
3bed9dd0
DF
4784 break;
4785
4786 case INTRINSIC_NE:
4787 case INTRINSIC_NE_OS:
e73d3ca6 4788 if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym,
524af0d6 4789 gfc_current_locus)
e73d3ca6 4790 || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS],
524af0d6
JB
4791 new_sym, gfc_current_locus))
4792 return false;
3bed9dd0
DF
4793 break;
4794
4795 case INTRINSIC_GT:
4796 case INTRINSIC_GT_OS:
e73d3ca6 4797 if (!gfc_check_new_interface (ns->op[INTRINSIC_GT],
524af0d6 4798 new_sym, gfc_current_locus)
e73d3ca6 4799 || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS],
524af0d6
JB
4800 new_sym, gfc_current_locus))
4801 return false;
3bed9dd0
DF
4802 break;
4803
4804 case INTRINSIC_GE:
4805 case INTRINSIC_GE_OS:
e73d3ca6 4806 if (!gfc_check_new_interface (ns->op[INTRINSIC_GE],
524af0d6 4807 new_sym, gfc_current_locus)
e73d3ca6 4808 || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS],
524af0d6
JB
4809 new_sym, gfc_current_locus))
4810 return false;
3bed9dd0
DF
4811 break;
4812
4813 case INTRINSIC_LT:
4814 case INTRINSIC_LT_OS:
e73d3ca6 4815 if (!gfc_check_new_interface (ns->op[INTRINSIC_LT],
524af0d6 4816 new_sym, gfc_current_locus)
e73d3ca6 4817 || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS],
524af0d6
JB
4818 new_sym, gfc_current_locus))
4819 return false;
3bed9dd0
DF
4820 break;
4821
4822 case INTRINSIC_LE:
4823 case INTRINSIC_LE_OS:
e73d3ca6 4824 if (!gfc_check_new_interface (ns->op[INTRINSIC_LE],
524af0d6 4825 new_sym, gfc_current_locus)
e73d3ca6 4826 || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS],
524af0d6
JB
4827 new_sym, gfc_current_locus))
4828 return false;
3bed9dd0
DF
4829 break;
4830
4831 default:
e73d3ca6 4832 if (!gfc_check_new_interface (ns->op[current_interface.op],
524af0d6
JB
4833 new_sym, gfc_current_locus))
4834 return false;
3bed9dd0 4835 }
6de9cd9a 4836
a1ee985f 4837 head = &current_interface.ns->op[current_interface.op];
6de9cd9a
DN
4838 break;
4839
4840 case INTERFACE_GENERIC:
e73d3ca6 4841 case INTERFACE_DTIO:
6de9cd9a
DN
4842 for (ns = current_interface.ns; ns; ns = ns->parent)
4843 {
4844 gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
4845 if (sym == NULL)
4846 continue;
4847
e73d3ca6 4848 if (!gfc_check_new_interface (sym->generic,
524af0d6
JB
4849 new_sym, gfc_current_locus))
4850 return false;
6de9cd9a
DN
4851 }
4852
4853 head = &current_interface.sym->generic;
4854 break;
4855
4856 case INTERFACE_USER_OP:
e73d3ca6 4857 if (!gfc_check_new_interface (current_interface.uop->op,
524af0d6
JB
4858 new_sym, gfc_current_locus))
4859 return false;
6de9cd9a 4860
a1ee985f 4861 head = &current_interface.uop->op;
6de9cd9a
DN
4862 break;
4863
4864 default:
4865 gfc_internal_error ("gfc_add_interface(): Bad interface type");
4866 }
4867
4868 intr = gfc_get_interface ();
7b901ac4 4869 intr->sym = new_sym;
63645982 4870 intr->where = gfc_current_locus;
6de9cd9a
DN
4871
4872 intr->next = *head;
4873 *head = intr;
4874
524af0d6 4875 return true;
6de9cd9a
DN
4876}
4877
4878
2b77e908
FXC
4879gfc_interface *
4880gfc_current_interface_head (void)
4881{
4882 switch (current_interface.type)
4883 {
4884 case INTERFACE_INTRINSIC_OP:
a1ee985f 4885 return current_interface.ns->op[current_interface.op];
2b77e908
FXC
4886
4887 case INTERFACE_GENERIC:
e73d3ca6 4888 case INTERFACE_DTIO:
2b77e908 4889 return current_interface.sym->generic;
2b77e908
FXC
4890
4891 case INTERFACE_USER_OP:
a1ee985f 4892 return current_interface.uop->op;
2b77e908
FXC
4893
4894 default:
4895 gcc_unreachable ();
4896 }
4897}
4898
4899
4900void
4901gfc_set_current_interface_head (gfc_interface *i)
4902{
4903 switch (current_interface.type)
4904 {
4905 case INTERFACE_INTRINSIC_OP:
a1ee985f 4906 current_interface.ns->op[current_interface.op] = i;
2b77e908
FXC
4907 break;
4908
4909 case INTERFACE_GENERIC:
e73d3ca6 4910 case INTERFACE_DTIO:
2b77e908
FXC
4911 current_interface.sym->generic = i;
4912 break;
4913
4914 case INTERFACE_USER_OP:
a1ee985f 4915 current_interface.uop->op = i;
2b77e908
FXC
4916 break;
4917
4918 default:
4919 gcc_unreachable ();
4920 }
4921}
4922
4923
6de9cd9a
DN
4924/* Gets rid of a formal argument list. We do not free symbols.
4925 Symbols are freed when a namespace is freed. */
4926
4927void
b251af97 4928gfc_free_formal_arglist (gfc_formal_arglist *p)
6de9cd9a
DN
4929{
4930 gfc_formal_arglist *q;
4931
4932 for (; p; p = q)
4933 {
4934 q = p->next;
cede9502 4935 free (p);
6de9cd9a
DN
4936 }
4937}
99fc1b90
JW
4938
4939
9795c594
JW
4940/* Check that it is ok for the type-bound procedure 'proc' to override the
4941 procedure 'old', cf. F08:4.5.7.3. */
99fc1b90 4942
524af0d6 4943bool
99fc1b90
JW
4944gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
4945{
4946 locus where;
edc802c7 4947 gfc_symbol *proc_target, *old_target;
99fc1b90 4948 unsigned proc_pass_arg, old_pass_arg, argpos;
9795c594
JW
4949 gfc_formal_arglist *proc_formal, *old_formal;
4950 bool check_type;
4951 char err[200];
99fc1b90
JW
4952
4953 /* This procedure should only be called for non-GENERIC proc. */
4954 gcc_assert (!proc->n.tb->is_generic);
4955
4956 /* If the overwritten procedure is GENERIC, this is an error. */
4957 if (old->n.tb->is_generic)
4958 {
1fe61adf 4959 gfc_error ("Cannot overwrite GENERIC %qs at %L",
99fc1b90 4960 old->name, &proc->n.tb->where);
524af0d6 4961 return false;
99fc1b90
JW
4962 }
4963
4964 where = proc->n.tb->where;
4965 proc_target = proc->n.tb->u.specific->n.sym;
4966 old_target = old->n.tb->u.specific->n.sym;
4967
4968 /* Check that overridden binding is not NON_OVERRIDABLE. */
4969 if (old->n.tb->non_overridable)
4970 {
c4100eae 4971 gfc_error ("%qs at %L overrides a procedure binding declared"
99fc1b90 4972 " NON_OVERRIDABLE", proc->name, &where);
524af0d6 4973 return false;
99fc1b90
JW
4974 }
4975
4976 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
4977 if (!old->n.tb->deferred && proc->n.tb->deferred)
4978 {
c4100eae 4979 gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
99fc1b90 4980 " non-DEFERRED binding", proc->name, &where);
524af0d6 4981 return false;
99fc1b90
JW
4982 }
4983
4984 /* If the overridden binding is PURE, the overriding must be, too. */
4985 if (old_target->attr.pure && !proc_target->attr.pure)
4986 {
c4100eae 4987 gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
99fc1b90 4988 proc->name, &where);
524af0d6 4989 return false;
99fc1b90
JW
4990 }
4991
4992 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
4993 is not, the overriding must not be either. */
4994 if (old_target->attr.elemental && !proc_target->attr.elemental)
4995 {
c4100eae 4996 gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
99fc1b90 4997 " ELEMENTAL", proc->name, &where);
524af0d6 4998 return false;
99fc1b90
JW
4999 }
5000 if (!old_target->attr.elemental && proc_target->attr.elemental)
5001 {
c4100eae 5002 gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
99fc1b90 5003 " be ELEMENTAL, either", proc->name, &where);
524af0d6 5004 return false;
99fc1b90
JW
5005 }
5006
5007 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
5008 SUBROUTINE. */
5009 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
5010 {
c4100eae 5011 gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
99fc1b90 5012 " SUBROUTINE", proc->name, &where);
524af0d6 5013 return false;
99fc1b90
JW
5014 }
5015
5016 /* If the overridden binding is a FUNCTION, the overriding must also be a
5017 FUNCTION and have the same characteristics. */
5018 if (old_target->attr.function)
5019 {
5020 if (!proc_target->attr.function)
5021 {
c4100eae 5022 gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
99fc1b90 5023 " FUNCTION", proc->name, &where);
524af0d6 5024 return false;
99fc1b90 5025 }
8b704316 5026
4668d6f9
PT
5027 if (!gfc_check_result_characteristics (proc_target, old_target,
5028 err, sizeof(err)))
2240d1cf 5029 {
edc802c7 5030 gfc_error ("Result mismatch for the overriding procedure "
c4100eae 5031 "%qs at %L: %s", proc->name, &where, err);
524af0d6 5032 return false;
2240d1cf 5033 }
99fc1b90
JW
5034 }
5035
5036 /* If the overridden binding is PUBLIC, the overriding one must not be
5037 PRIVATE. */
5038 if (old->n.tb->access == ACCESS_PUBLIC
5039 && proc->n.tb->access == ACCESS_PRIVATE)
5040 {
c4100eae 5041 gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
99fc1b90 5042 " PRIVATE", proc->name, &where);
524af0d6 5043 return false;
99fc1b90
JW
5044 }
5045
5046 /* Compare the formal argument lists of both procedures. This is also abused
5047 to find the position of the passed-object dummy arguments of both
5048 bindings as at least the overridden one might not yet be resolved and we
5049 need those positions in the check below. */
5050 proc_pass_arg = old_pass_arg = 0;
5051 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
5052 proc_pass_arg = 1;
5053 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
5054 old_pass_arg = 1;
5055 argpos = 1;
4cbc9039
JW
5056 proc_formal = gfc_sym_get_dummy_args (proc_target);
5057 old_formal = gfc_sym_get_dummy_args (old_target);
5058 for ( ; proc_formal && old_formal;
99fc1b90
JW
5059 proc_formal = proc_formal->next, old_formal = old_formal->next)
5060 {
5061 if (proc->n.tb->pass_arg
5062 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
5063 proc_pass_arg = argpos;
5064 if (old->n.tb->pass_arg
5065 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
5066 old_pass_arg = argpos;
5067
5068 /* Check that the names correspond. */
5069 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
5070 {
c4100eae 5071 gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
99fc1b90
JW
5072 " to match the corresponding argument of the overridden"
5073 " procedure", proc_formal->sym->name, proc->name, &where,
5074 old_formal->sym->name);
524af0d6 5075 return false;
99fc1b90
JW
5076 }
5077
9795c594 5078 check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
4668d6f9 5079 if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
524af0d6 5080 check_type, err, sizeof(err)))
99fc1b90 5081 {
e0b9e5f9 5082 gfc_error_opt (0, "Argument mismatch for the overriding procedure "
2700d0e3 5083 "%qs at %L: %s", proc->name, &where, err);
524af0d6 5084 return false;
99fc1b90
JW
5085 }
5086
5087 ++argpos;
5088 }
5089 if (proc_formal || old_formal)
5090 {
c4100eae 5091 gfc_error ("%qs at %L must have the same number of formal arguments as"
99fc1b90 5092 " the overridden procedure", proc->name, &where);
524af0d6 5093 return false;
99fc1b90
JW
5094 }
5095
5096 /* If the overridden binding is NOPASS, the overriding one must also be
5097 NOPASS. */
5098 if (old->n.tb->nopass && !proc->n.tb->nopass)
5099 {
c4100eae 5100 gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
99fc1b90 5101 " NOPASS", proc->name, &where);
524af0d6 5102 return false;
99fc1b90
JW
5103 }
5104
5105 /* If the overridden binding is PASS(x), the overriding one must also be
5106 PASS and the passed-object dummy arguments must correspond. */
5107 if (!old->n.tb->nopass)
5108 {
5109 if (proc->n.tb->nopass)
5110 {
c4100eae 5111 gfc_error ("%qs at %L overrides a binding with PASS and must also be"
99fc1b90 5112 " PASS", proc->name, &where);
524af0d6 5113 return false;
99fc1b90
JW
5114 }
5115
5116 if (proc_pass_arg != old_pass_arg)
5117 {
c4100eae 5118 gfc_error ("Passed-object dummy argument of %qs at %L must be at"
99fc1b90
JW
5119 " the same position as the passed-object dummy argument of"
5120 " the overridden procedure", proc->name, &where);
524af0d6 5121 return false;
99fc1b90
JW
5122 }
5123 }
5124
524af0d6 5125 return true;
99fc1b90 5126}
e73d3ca6
PT
5127
5128
5129/* The following three functions check that the formal arguments
5130 of user defined derived type IO procedures are compliant with
f3ad8745 5131 the requirements of the standard, see F03:9.5.3.7.2 (F08:9.6.4.8.3). */
e73d3ca6
PT
5132
5133static void
5134check_dtio_arg_TKR_intent (gfc_symbol *fsym, bool typebound, bt type,
5135 int kind, int rank, sym_intent intent)
5136{
5137 if (fsym->ts.type != type)
739d9339
PT
5138 {
5139 gfc_error ("DTIO dummy argument at %L must be of type %s",
5140 &fsym->declared_at, gfc_basic_typename (type));
5141 return;
5142 }
e73d3ca6
PT
5143
5144 if (fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED
5145 && fsym->ts.kind != kind)
5146 gfc_error ("DTIO dummy argument at %L must be of KIND = %d",
5147 &fsym->declared_at, kind);
5148
5149 if (!typebound
5150 && rank == 0
5151 && (((type == BT_CLASS) && CLASS_DATA (fsym)->attr.dimension)
5152 || ((type != BT_CLASS) && fsym->attr.dimension)))
b93a9a15 5153 gfc_error ("DTIO dummy argument at %L must be a scalar",
e73d3ca6
PT
5154 &fsym->declared_at);
5155 else if (rank == 1
5156 && (fsym->as == NULL || fsym->as->type != AS_ASSUMED_SHAPE))
5157 gfc_error ("DTIO dummy argument at %L must be an "
5158 "ASSUMED SHAPE ARRAY", &fsym->declared_at);
5159
f3ad8745
JW
5160 if (type == BT_CHARACTER && fsym->ts.u.cl->length != NULL)
5161 gfc_error ("DTIO character argument at %L must have assumed length",
5162 &fsym->declared_at);
5163
e73d3ca6 5164 if (fsym->attr.intent != intent)
77be9417 5165 gfc_error ("DTIO dummy argument at %L must have INTENT %s",
e73d3ca6
PT
5166 &fsym->declared_at, gfc_code2string (intents, (int)intent));
5167 return;
5168}
5169
5170
5171static void
5172check_dtio_interface1 (gfc_symbol *derived, gfc_symtree *tb_io_st,
5173 bool typebound, bool formatted, int code)
5174{
5175 gfc_symbol *dtio_sub, *generic_proc, *fsym;
5176 gfc_typebound_proc *tb_io_proc, *specific_proc;
5177 gfc_interface *intr;
5178 gfc_formal_arglist *formal;
5179 int arg_num;
5180
5181 bool read = ((dtio_codes)code == DTIO_RF)
5182 || ((dtio_codes)code == DTIO_RUF);
5183 bt type;
5184 sym_intent intent;
5185 int kind;
5186
5187 dtio_sub = NULL;
5188 if (typebound)
5189 {
5190 /* Typebound DTIO binding. */
5191 tb_io_proc = tb_io_st->n.tb;
739d9339
PT
5192 if (tb_io_proc == NULL)
5193 return;
5194
e73d3ca6 5195 gcc_assert (tb_io_proc->is_generic);
e73d3ca6
PT
5196
5197 specific_proc = tb_io_proc->u.generic->specific;
739d9339
PT
5198 if (specific_proc == NULL || specific_proc->is_generic)
5199 return;
e73d3ca6
PT
5200
5201 dtio_sub = specific_proc->u.specific->n.sym;
5202 }
5203 else
5204 {
5205 generic_proc = tb_io_st->n.sym;
739d9339
PT
5206 if (generic_proc == NULL || generic_proc->generic == NULL)
5207 return;
e73d3ca6
PT
5208
5209 for (intr = tb_io_st->n.sym->generic; intr; intr = intr->next)
5210 {
a8de3002 5211 if (intr->sym && intr->sym->formal && intr->sym->formal->sym
e73d3ca6
PT
5212 && ((intr->sym->formal->sym->ts.type == BT_CLASS
5213 && CLASS_DATA (intr->sym->formal->sym)->ts.u.derived
5214 == derived)
5215 || (intr->sym->formal->sym->ts.type == BT_DERIVED
5216 && intr->sym->formal->sym->ts.u.derived == derived)))
5217 {
5218 dtio_sub = intr->sym;
5219 break;
5220 }
a8de3002
PT
5221 else if (intr->sym && intr->sym->formal && !intr->sym->formal->sym)
5222 {
5223 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5224 "procedure", &intr->sym->declared_at);
5225 return;
5226 }
e73d3ca6
PT
5227 }
5228
5229 if (dtio_sub == NULL)
5230 return;
5231 }
5232
5233 gcc_assert (dtio_sub);
5234 if (!dtio_sub->attr.subroutine)
2f029c08 5235 gfc_error ("DTIO procedure %qs at %L must be a subroutine",
e73d3ca6
PT
5236 dtio_sub->name, &dtio_sub->declared_at);
5237
dbeaa7ab 5238 if (!dtio_sub->resolve_symbol_called)
3ab216a4
TB
5239 gfc_resolve_formal_arglist (dtio_sub);
5240
a8de3002
PT
5241 arg_num = 0;
5242 for (formal = dtio_sub->formal; formal; formal = formal->next)
5243 arg_num++;
5244
5245 if (arg_num < (formatted ? 6 : 4))
5246 {
2f029c08 5247 gfc_error ("Too few dummy arguments in DTIO procedure %qs at %L",
a8de3002
PT
5248 dtio_sub->name, &dtio_sub->declared_at);
5249 return;
5250 }
5251
5252 if (arg_num > (formatted ? 6 : 4))
5253 {
2f029c08 5254 gfc_error ("Too many dummy arguments in DTIO procedure %qs at %L",
a8de3002
PT
5255 dtio_sub->name, &dtio_sub->declared_at);
5256 return;
5257 }
5258
e73d3ca6
PT
5259 /* Now go through the formal arglist. */
5260 arg_num = 1;
5261 for (formal = dtio_sub->formal; formal; formal = formal->next, arg_num++)
5262 {
5263 if (!formatted && arg_num == 3)
5264 arg_num = 5;
5265 fsym = formal->sym;
a8de3002
PT
5266
5267 if (fsym == NULL)
5268 {
5269 gfc_error ("Alternate return at %L is not permitted in a DTIO "
5270 "procedure", &dtio_sub->declared_at);
5271 return;
5272 }
5273
e73d3ca6
PT
5274 switch (arg_num)
5275 {
5276 case(1): /* DTV */
5277 type = derived->attr.sequence || derived->attr.is_bind_c ?
5278 BT_DERIVED : BT_CLASS;
5279 kind = 0;
5280 intent = read ? INTENT_INOUT : INTENT_IN;
5281 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5282 0, intent);
5283 break;
5284
5285 case(2): /* UNIT */
5286 type = BT_INTEGER;
5287 kind = gfc_default_integer_kind;
5288 intent = INTENT_IN;
5289 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5290 0, intent);
5291 break;
5292 case(3): /* IOTYPE */
5293 type = BT_CHARACTER;
5294 kind = gfc_default_character_kind;
5295 intent = INTENT_IN;
5296 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5297 0, intent);
5298 break;
5299 case(4): /* VLIST */
5300 type = BT_INTEGER;
5301 kind = gfc_default_integer_kind;
5302 intent = INTENT_IN;
5303 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5304 1, intent);
5305 break;
5306 case(5): /* IOSTAT */
5307 type = BT_INTEGER;
5308 kind = gfc_default_integer_kind;
5309 intent = INTENT_OUT;
5310 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5311 0, intent);
5312 break;
5313 case(6): /* IOMSG */
5314 type = BT_CHARACTER;
5315 kind = gfc_default_character_kind;
5316 intent = INTENT_INOUT;
5317 check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5318 0, intent);
5319 break;
5320 default:
5321 gcc_unreachable ();
5322 }
5323 }
5324 derived->attr.has_dtio_procs = 1;
5325 return;
5326}
5327
5328void
5329gfc_check_dtio_interfaces (gfc_symbol *derived)
5330{
5331 gfc_symtree *tb_io_st;
5332 bool t = false;
5333 int code;
5334 bool formatted;
5335
5336 if (derived->attr.is_class == 1 || derived->attr.vtype == 1)
5337 return;
5338
5339 /* Check typebound DTIO bindings. */
5340 for (code = 0; code < 4; code++)
5341 {
5342 formatted = ((dtio_codes)code == DTIO_RF)
5343 || ((dtio_codes)code == DTIO_WF);
5344
5345 tb_io_st = gfc_find_typebound_proc (derived, &t,
5346 gfc_code2string (dtio_procs, code),
5347 true, &derived->declared_at);
5348 if (tb_io_st != NULL)
5349 check_dtio_interface1 (derived, tb_io_st, true, formatted, code);
5350 }
5351
5352 /* Check generic DTIO interfaces. */
5353 for (code = 0; code < 4; code++)
5354 {
5355 formatted = ((dtio_codes)code == DTIO_RF)
5356 || ((dtio_codes)code == DTIO_WF);
5357
5358 tb_io_st = gfc_find_symtree (derived->ns->sym_root,
5359 gfc_code2string (dtio_procs, code));
5360 if (tb_io_st != NULL)
5361 check_dtio_interface1 (derived, tb_io_st, false, formatted, code);
5362 }
5363}
5364
5365
e4e659b9
JW
5366gfc_symtree*
5367gfc_find_typebound_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
e73d3ca6
PT
5368{
5369 gfc_symtree *tb_io_st = NULL;
e73d3ca6
PT
5370 bool t = false;
5371
dbeaa7ab
ME
5372 if (!derived || !derived->resolve_symbol_called
5373 || derived->attr.flavor != FL_DERIVED)
9beb81ed
PT
5374 return NULL;
5375
e73d3ca6
PT
5376 /* Try to find a typebound DTIO binding. */
5377 if (formatted == true)
5378 {
5379 if (write == true)
5380 tb_io_st = gfc_find_typebound_proc (derived, &t,
5381 gfc_code2string (dtio_procs,
5382 DTIO_WF),
5383 true,
5384 &derived->declared_at);
5385 else
5386 tb_io_st = gfc_find_typebound_proc (derived, &t,
5387 gfc_code2string (dtio_procs,
5388 DTIO_RF),
5389 true,
5390 &derived->declared_at);
5391 }
5392 else
5393 {
5394 if (write == true)
5395 tb_io_st = gfc_find_typebound_proc (derived, &t,
5396 gfc_code2string (dtio_procs,
5397 DTIO_WUF),
5398 true,
5399 &derived->declared_at);
5400 else
5401 tb_io_st = gfc_find_typebound_proc (derived, &t,
5402 gfc_code2string (dtio_procs,
5403 DTIO_RUF),
5404 true,
5405 &derived->declared_at);
5406 }
e4e659b9
JW
5407 return tb_io_st;
5408}
5409
5410
5411gfc_symbol *
5412gfc_find_specific_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5413{
5414 gfc_symtree *tb_io_st = NULL;
5415 gfc_symbol *dtio_sub = NULL;
5416 gfc_symbol *extended;
5417 gfc_typebound_proc *tb_io_proc, *specific_proc;
5418
5419 tb_io_st = gfc_find_typebound_dtio_proc (derived, write, formatted);
e73d3ca6
PT
5420
5421 if (tb_io_st != NULL)
5422 {
096506bb
PT
5423 const char *genname;
5424 gfc_symtree *st;
5425
e73d3ca6
PT
5426 tb_io_proc = tb_io_st->n.tb;
5427 gcc_assert (tb_io_proc != NULL);
5428 gcc_assert (tb_io_proc->is_generic);
5429 gcc_assert (tb_io_proc->u.generic->next == NULL);
5430
5431 specific_proc = tb_io_proc->u.generic->specific;
5432 gcc_assert (!specific_proc->is_generic);
5433
096506bb
PT
5434 /* Go back and make sure that we have the right specific procedure.
5435 Here we most likely have a procedure from the parent type, which
5436 can be overridden in extensions. */
5437 genname = tb_io_proc->u.generic->specific_st->name;
5438 st = gfc_find_typebound_proc (derived, NULL, genname,
5439 true, &tb_io_proc->where);
5440 if (st)
5441 dtio_sub = st->n.tb->u.specific->n.sym;
5442 else
5443 dtio_sub = specific_proc->u.specific->n.sym;
e73d3ca6 5444
e4e659b9
JW
5445 goto finish;
5446 }
e73d3ca6
PT
5447
5448 /* If there is not a typebound binding, look for a generic
5449 DTIO interface. */
5450 for (extended = derived; extended;
5451 extended = gfc_get_derived_super_type (extended))
5452 {
e4e659b9
JW
5453 if (extended == NULL || extended->ns == NULL
5454 || extended->attr.flavor == FL_UNKNOWN)
a8de3002
PT
5455 return NULL;
5456
e73d3ca6
PT
5457 if (formatted == true)
5458 {
5459 if (write == true)
5460 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5461 gfc_code2string (dtio_procs,
5462 DTIO_WF));
5463 else
5464 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5465 gfc_code2string (dtio_procs,
5466 DTIO_RF));
5467 }
5468 else
5469 {
5470 if (write == true)
5471 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5472 gfc_code2string (dtio_procs,
5473 DTIO_WUF));
5474 else
5475 tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5476 gfc_code2string (dtio_procs,
5477 DTIO_RUF));
5478 }
5479
5480 if (tb_io_st != NULL
5481 && tb_io_st->n.sym
5482 && tb_io_st->n.sym->generic)
5483 {
40109581 5484 for (gfc_interface *intr = tb_io_st->n.sym->generic;
413e859c 5485 intr && intr->sym; intr = intr->next)
e73d3ca6 5486 {
413e859c 5487 if (intr->sym->formal)
e73d3ca6 5488 {
413e859c
JW
5489 gfc_symbol *fsym = intr->sym->formal->sym;
5490 if ((fsym->ts.type == BT_CLASS
5491 && CLASS_DATA (fsym)->ts.u.derived == extended)
5492 || (fsym->ts.type == BT_DERIVED
5493 && fsym->ts.u.derived == extended))
5494 {
5495 dtio_sub = intr->sym;
5496 break;
5497 }
e73d3ca6
PT
5498 }
5499 }
5500 }
5501 }
5502
5503finish:
72d91d6c
TB
5504 if (dtio_sub
5505 && dtio_sub->formal->sym->ts.type == BT_CLASS
5506 && derived != CLASS_DATA (dtio_sub->formal->sym)->ts.u.derived)
e73d3ca6
PT
5507 gfc_find_derived_vtab (derived);
5508
5509 return dtio_sub;
5510}
e68a35ae
TK
5511
5512/* Helper function - if we do not find an interface for a procedure,
5513 construct it from the actual arglist. Luckily, this can only
5514 happen for call by reference, so the information we actually need
5515 to provide (and which would be impossible to guess from the call
5516 itself) is not actually needed. */
5517
5518void
5519gfc_get_formal_from_actual_arglist (gfc_symbol *sym,
5520 gfc_actual_arglist *actual_args)
5521{
5522 gfc_actual_arglist *a;
5523 gfc_formal_arglist **f;
5524 gfc_symbol *s;
5525 char name[GFC_MAX_SYMBOL_LEN + 1];
5526 static int var_num;
5527
5528 f = &sym->formal;
5529 for (a = actual_args; a != NULL; a = a->next)
5530 {
5531 (*f) = gfc_get_formal_arglist ();
5532 if (a->expr)
5533 {
5534 snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
5535 gfc_get_symbol (name, gfc_current_ns, &s);
5536 if (a->expr->ts.type == BT_PROCEDURE)
5537 {
5538 s->attr.flavor = FL_PROCEDURE;
5539 }
5540 else
5541 {
5542 s->ts = a->expr->ts;
5543
5544 if (s->ts.type == BT_CHARACTER)
5545 s->ts.u.cl = gfc_get_charlen ();
5546
5547 s->ts.deferred = 0;
5548 s->ts.is_iso_c = 0;
5549 s->ts.is_c_interop = 0;
5550 s->attr.flavor = FL_VARIABLE;
e68a35ae
TK
5551 if (a->expr->rank > 0)
5552 {
5553 s->attr.dimension = 1;
5554 s->as = gfc_get_array_spec ();
5555 s->as->rank = 1;
5556 s->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5557 &a->expr->where, 1);
5558 s->as->upper[0] = NULL;
5559 s->as->type = AS_ASSUMED_SIZE;
5560 }
4a4fc7fe
TK
5561 else
5562 s->maybe_array = maybe_dummy_array_arg (a->expr);
e68a35ae
TK
5563 }
5564 s->attr.dummy = 1;
3b0e49a5 5565 s->attr.artificial = 1;
e0b9e5f9 5566 s->declared_at = a->expr->where;
e68a35ae
TK
5567 s->attr.intent = INTENT_UNKNOWN;
5568 (*f)->sym = s;
5569 }
5570 else /* If a->expr is NULL, this is an alternate rerturn. */
5571 (*f)->sym = NULL;
5572
5573 f = &((*f)->next);
5574 }
5575}
5d9d16db
MM
5576
5577
48a8c5be
MM
5578const char *
5579gfc_dummy_arg_get_name (gfc_dummy_arg & dummy_arg)
5580{
5581 switch (dummy_arg.intrinsicness)
5582 {
5583 case GFC_INTRINSIC_DUMMY_ARG:
5584 return dummy_arg.u.intrinsic->name;
5585
5586 case GFC_NON_INTRINSIC_DUMMY_ARG:
5587 return dummy_arg.u.non_intrinsic->sym->name;
5588
5589 default:
5590 gcc_unreachable ();
5591 }
5592}
5593
5594
5d9d16db
MM
5595const gfc_typespec &
5596gfc_dummy_arg_get_typespec (gfc_dummy_arg & dummy_arg)
5597{
5598 switch (dummy_arg.intrinsicness)
5599 {
5600 case GFC_INTRINSIC_DUMMY_ARG:
5601 return dummy_arg.u.intrinsic->ts;
5602
5603 case GFC_NON_INTRINSIC_DUMMY_ARG:
5604 return dummy_arg.u.non_intrinsic->sym->ts;
5605
5606 default:
5607 gcc_unreachable ();
5608 }
5609}
5610
5611
5612bool
5613gfc_dummy_arg_is_optional (gfc_dummy_arg & dummy_arg)
5614{
5615 switch (dummy_arg.intrinsicness)
5616 {
5617 case GFC_INTRINSIC_DUMMY_ARG:
5618 return dummy_arg.u.intrinsic->optional;
5619
5620 case GFC_NON_INTRINSIC_DUMMY_ARG:
5621 return dummy_arg.u.non_intrinsic->sym->attr.optional;
5622
5623 default:
5624 gcc_unreachable ();
5625 }
5626}