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