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