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