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