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