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