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