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