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