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