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