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