]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/interface.c
discourage unnecessary use of if before free
[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;
cede9502 90 free (intr);
6de9cd9a
DN
91 }
92}
93
94
95/* Change the operators unary plus and minus into binary plus and
96 minus respectively, leaving the rest unchanged. */
97
98static gfc_intrinsic_op
e8d4f3fc 99fold_unary_intrinsic (gfc_intrinsic_op op)
6de9cd9a 100{
a1ee985f 101 switch (op)
6de9cd9a
DN
102 {
103 case INTRINSIC_UPLUS:
a1ee985f 104 op = INTRINSIC_PLUS;
6de9cd9a
DN
105 break;
106 case INTRINSIC_UMINUS:
a1ee985f 107 op = INTRINSIC_MINUS;
6de9cd9a
DN
108 break;
109 default:
110 break;
111 }
112
a1ee985f 113 return op;
6de9cd9a
DN
114}
115
116
117/* Match a generic specification. Depending on which type of
a1ee985f 118 interface is found, the 'name' or 'op' pointers may be set.
6de9cd9a
DN
119 This subroutine doesn't return MATCH_NO. */
120
121match
b251af97 122gfc_match_generic_spec (interface_type *type,
6de9cd9a 123 char *name,
a1ee985f 124 gfc_intrinsic_op *op)
6de9cd9a
DN
125{
126 char buffer[GFC_MAX_SYMBOL_LEN + 1];
127 match m;
128 gfc_intrinsic_op i;
129
130 if (gfc_match (" assignment ( = )") == MATCH_YES)
131 {
132 *type = INTERFACE_INTRINSIC_OP;
a1ee985f 133 *op = INTRINSIC_ASSIGN;
6de9cd9a
DN
134 return MATCH_YES;
135 }
136
137 if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
138 { /* Operator i/f */
139 *type = INTERFACE_INTRINSIC_OP;
e8d4f3fc 140 *op = fold_unary_intrinsic (i);
6de9cd9a
DN
141 return MATCH_YES;
142 }
143
e8d4f3fc 144 *op = INTRINSIC_NONE;
6de9cd9a
DN
145 if (gfc_match (" operator ( ") == MATCH_YES)
146 {
147 m = gfc_match_defined_op_name (buffer, 1);
148 if (m == MATCH_NO)
149 goto syntax;
150 if (m != MATCH_YES)
151 return MATCH_ERROR;
152
153 m = gfc_match_char (')');
154 if (m == MATCH_NO)
155 goto syntax;
156 if (m != MATCH_YES)
157 return MATCH_ERROR;
158
159 strcpy (name, buffer);
160 *type = INTERFACE_USER_OP;
161 return MATCH_YES;
162 }
163
164 if (gfc_match_name (buffer) == MATCH_YES)
165 {
166 strcpy (name, buffer);
167 *type = INTERFACE_GENERIC;
168 return MATCH_YES;
169 }
170
171 *type = INTERFACE_NAMELESS;
172 return MATCH_YES;
173
174syntax:
175 gfc_error ("Syntax error in generic specification at %C");
176 return MATCH_ERROR;
177}
178
179
9e1d712c
TB
180/* Match one of the five F95 forms of an interface statement. The
181 matcher for the abstract interface follows. */
6de9cd9a
DN
182
183match
184gfc_match_interface (void)
185{
186 char name[GFC_MAX_SYMBOL_LEN + 1];
187 interface_type type;
188 gfc_symbol *sym;
a1ee985f 189 gfc_intrinsic_op op;
6de9cd9a
DN
190 match m;
191
192 m = gfc_match_space ();
193
a1ee985f 194 if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
6de9cd9a
DN
195 return MATCH_ERROR;
196
6de9cd9a
DN
197 /* If we're not looking at the end of the statement now, or if this
198 is not a nameless interface but we did not see a space, punt. */
199 if (gfc_match_eos () != MATCH_YES
b251af97 200 || (type != INTERFACE_NAMELESS && m != MATCH_YES))
6de9cd9a 201 {
b251af97
SK
202 gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
203 "at %C");
6de9cd9a
DN
204 return MATCH_ERROR;
205 }
206
207 current_interface.type = type;
208
209 switch (type)
210 {
211 case INTERFACE_GENERIC:
212 if (gfc_get_symbol (name, NULL, &sym))
213 return MATCH_ERROR;
214
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
cede9502 915 free (arg);
6de9cd9a
DN
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 1149 qlast->next = q->next;
cede9502 1150 free (q);
6de9cd9a
DN
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;
975b975b 1464 bool rank_check, is_pointer;
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
a3935ffc 1567 || !gfc_expr_attr (actual).codimension)
d3a9eea2
TB
1568 {
1569 if (where)
1570 gfc_error ("Actual argument to '%s' at %L must be a coarray",
1571 formal->name, &actual->where);
1572 return 0;
1573 }
1574
a3935ffc
TB
1575 if (gfc_is_coindexed (actual))
1576 {
1577 if (where)
1578 gfc_error ("Actual argument to '%s' at %L must be a coarray "
1579 "and not coindexed", formal->name, &actual->where);
1580 return 0;
1581 }
1582
d3a9eea2
TB
1583 for (ref = actual->ref; ref; ref = ref->next)
1584 {
d3a9eea2
TB
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
d3a9eea2
TB
1598 /* F2008, 12.5.2.6. */
1599 if (formal->attr.allocatable &&
1600 ((last && last->u.c.component->as->corank != formal->as->corank)
1601 || (!last
1602 && actual->symtree->n.sym->as->corank != formal->as->corank)))
1603 {
1604 if (where)
1605 gfc_error ("Corank mismatch in argument '%s' at %L (%d and %d)",
1606 formal->name, &actual->where, formal->as->corank,
1607 last ? last->u.c.component->as->corank
1608 : actual->symtree->n.sym->as->corank);
1609 return 0;
1610 }
fe4e525c
TB
1611
1612 /* F2008, 12.5.2.8. */
1613 if (formal->attr.dimension
1614 && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
1615 && !gfc_is_simply_contiguous (actual, true))
1616 {
1617 if (where)
1618 gfc_error ("Actual argument to '%s' at %L must be simply "
1619 "contiguous", formal->name, &actual->where);
1620 return 0;
1621 }
1622 }
1623
1624 /* F2008, C1239/C1240. */
1625 if (actual->expr_type == EXPR_VARIABLE
1626 && (actual->symtree->n.sym->attr.asynchronous
1627 || actual->symtree->n.sym->attr.volatile_)
1628 && (formal->attr.asynchronous || formal->attr.volatile_)
1629 && actual->rank && !gfc_is_simply_contiguous (actual, true)
1630 && ((formal->as->type != AS_ASSUMED_SHAPE && !formal->attr.pointer)
1631 || formal->attr.contiguous))
1632 {
1633 if (where)
1634 gfc_error ("Dummy argument '%s' has to be a pointer or assumed-shape "
1635 "array without CONTIGUOUS attribute - as actual argument at"
1636 " %L is not simply contiguous and both are ASYNCHRONOUS "
1637 "or VOLATILE", formal->name, &actual->where);
1638 return 0;
d3a9eea2
TB
1639 }
1640
6de9cd9a
DN
1641 if (symbol_rank (formal) == actual->rank)
1642 return 1;
1643
5ad6345e
TB
1644 rank_check = where != NULL && !is_elemental && formal->as
1645 && (formal->as->type == AS_ASSUMED_SHAPE
d8a8dab3
TB
1646 || formal->as->type == AS_DEFERRED)
1647 && actual->expr_type != EXPR_NULL;
6de9cd9a 1648
d3a9eea2 1649 /* Scalar & coindexed, see: F2008, Section 12.5.2.4. */
d8a8dab3
TB
1650 if (rank_check || ranks_must_agree
1651 || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
5ad6345e 1652 || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
08857b61
TB
1653 || (actual->rank == 0 && formal->as->type == AS_ASSUMED_SHAPE
1654 && actual->expr_type != EXPR_NULL)
d3a9eea2
TB
1655 || (actual->rank == 0 && formal->attr.dimension
1656 && gfc_is_coindexed (actual)))
5ad6345e
TB
1657 {
1658 if (where)
a516520c
PT
1659 argument_rank_mismatch (formal->name, &actual->where,
1660 symbol_rank (formal), actual->rank);
6de9cd9a 1661 return 0;
5ad6345e
TB
1662 }
1663 else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
1664 return 1;
1665
1666 /* At this point, we are considering a scalar passed to an array. This
975b975b 1667 is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
5ad6345e 1668 - if the actual argument is (a substring of) an element of a
975b975b
TB
1669 non-assumed-shape/non-pointer/non-polymorphic array; or
1670 - (F2003) if the actual argument is of type character of default/c_char
1671 kind. */
1672
1673 is_pointer = actual->expr_type == EXPR_VARIABLE
1674 ? actual->symtree->n.sym->attr.pointer : false;
6de9cd9a
DN
1675
1676 for (ref = actual->ref; ref; ref = ref->next)
975b975b
TB
1677 {
1678 if (ref->type == REF_COMPONENT)
1679 is_pointer = ref->u.c.component->attr.pointer;
1680 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
1681 && ref->u.ar.dimen > 0
1682 && (!ref->next
1683 || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
1684 break;
1685 }
1686
1687 if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
1688 {
1689 if (where)
1690 gfc_error ("Polymorphic scalar passed to array dummy argument '%s' "
1691 "at %L", formal->name, &actual->where);
1692 return 0;
1693 }
1694
1695 if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
1696 && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
1697 {
1698 if (where)
1699 gfc_error ("Element of assumed-shaped or pointer "
1700 "array passed to array dummy argument '%s' at %L",
1701 formal->name, &actual->where);
1702 return 0;
1703 }
6de9cd9a 1704
975b975b
TB
1705 if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
1706 && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
5ad6345e 1707 {
975b975b
TB
1708 if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
1709 {
1710 if (where)
1711 gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
1712 "CHARACTER actual argument with array dummy argument "
1713 "'%s' at %L", formal->name, &actual->where);
1714 return 0;
1715 }
1716
5ad6345e
TB
1717 if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
1718 {
1719 gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
1720 "array dummy argument '%s' at %L",
1721 formal->name, &actual->where);
1722 return 0;
1723 }
1724 else if ((gfc_option.allow_std & GFC_STD_F2003) == 0)
1725 return 0;
1726 else
1727 return 1;
1728 }
975b975b
TB
1729
1730 if (ref == NULL && actual->expr_type != EXPR_NULL)
5ad6345e
TB
1731 {
1732 if (where)
a516520c
PT
1733 argument_rank_mismatch (formal->name, &actual->where,
1734 symbol_rank (formal), actual->rank);
5ad6345e
TB
1735 return 0;
1736 }
1737
6de9cd9a
DN
1738 return 1;
1739}
1740
1741
2d5b90b2
TB
1742/* Returns the storage size of a symbol (formal argument) or
1743 zero if it cannot be determined. */
1744
1745static unsigned long
1746get_sym_storage_size (gfc_symbol *sym)
1747{
1748 int i;
1749 unsigned long strlen, elements;
1750
1751 if (sym->ts.type == BT_CHARACTER)
1752 {
bc21d315
JW
1753 if (sym->ts.u.cl && sym->ts.u.cl->length
1754 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1755 strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
2d5b90b2
TB
1756 else
1757 return 0;
1758 }
1759 else
1760 strlen = 1;
1761
1762 if (symbol_rank (sym) == 0)
1763 return strlen;
1764
1765 elements = 1;
1766 if (sym->as->type != AS_EXPLICIT)
1767 return 0;
1768 for (i = 0; i < sym->as->rank; i++)
1769 {
1770 if (!sym->as || sym->as->upper[i]->expr_type != EXPR_CONSTANT
1771 || sym->as->lower[i]->expr_type != EXPR_CONSTANT)
1772 return 0;
1773
c13af44b
SK
1774 elements *= mpz_get_si (sym->as->upper[i]->value.integer)
1775 - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
2d5b90b2
TB
1776 }
1777
1778 return strlen*elements;
1779}
1780
1781
1782/* Returns the storage size of an expression (actual argument) or
1783 zero if it cannot be determined. For an array element, it returns
1207ac67 1784 the remaining size as the element sequence consists of all storage
2d5b90b2
TB
1785 units of the actual argument up to the end of the array. */
1786
1787static unsigned long
1788get_expr_storage_size (gfc_expr *e)
1789{
1790 int i;
1791 long int strlen, elements;
6da0839a 1792 long int substrlen = 0;
a0710c29 1793 bool is_str_storage = false;
2d5b90b2
TB
1794 gfc_ref *ref;
1795
1796 if (e == NULL)
1797 return 0;
1798
1799 if (e->ts.type == BT_CHARACTER)
1800 {
bc21d315
JW
1801 if (e->ts.u.cl && e->ts.u.cl->length
1802 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1803 strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
2d5b90b2 1804 else if (e->expr_type == EXPR_CONSTANT
bc21d315 1805 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
2d5b90b2
TB
1806 strlen = e->value.character.length;
1807 else
1808 return 0;
1809 }
1810 else
1811 strlen = 1; /* Length per element. */
1812
1813 if (e->rank == 0 && !e->ref)
1814 return strlen;
1815
1816 elements = 1;
1817 if (!e->ref)
1818 {
1819 if (!e->shape)
1820 return 0;
1821 for (i = 0; i < e->rank; i++)
1822 elements *= mpz_get_si (e->shape[i]);
1823 return elements*strlen;
1824 }
1825
1826 for (ref = e->ref; ref; ref = ref->next)
1827 {
6da0839a
TB
1828 if (ref->type == REF_SUBSTRING && ref->u.ss.start
1829 && ref->u.ss.start->expr_type == EXPR_CONSTANT)
1830 {
a0710c29
TB
1831 if (is_str_storage)
1832 {
1833 /* The string length is the substring length.
1834 Set now to full string length. */
1835 if (ref->u.ss.length == NULL
1836 || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
1837 return 0;
1838
1839 strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
1840 }
1841 substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
6da0839a
TB
1842 continue;
1843 }
1844
2d5b90b2
TB
1845 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION
1846 && ref->u.ar.start && ref->u.ar.end && ref->u.ar.stride
1847 && ref->u.ar.as->upper)
1848 for (i = 0; i < ref->u.ar.dimen; i++)
1849 {
1850 long int start, end, stride;
1851 stride = 1;
37639728 1852
2d5b90b2
TB
1853 if (ref->u.ar.stride[i])
1854 {
1855 if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT)
1856 stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
1857 else
1858 return 0;
1859 }
1860
1861 if (ref->u.ar.start[i])
1862 {
1863 if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT)
1864 start = mpz_get_si (ref->u.ar.start[i]->value.integer);
1865 else
1866 return 0;
1867 }
37639728
TB
1868 else if (ref->u.ar.as->lower[i]
1869 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT)
1870 start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
1871 else
1872 return 0;
2d5b90b2
TB
1873
1874 if (ref->u.ar.end[i])
1875 {
1876 if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT)
1877 end = mpz_get_si (ref->u.ar.end[i]->value.integer);
1878 else
1879 return 0;
1880 }
1881 else if (ref->u.ar.as->upper[i]
1882 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
1883 end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
1884 else
1885 return 0;
1886
1887 elements *= (end - start)/stride + 1L;
1888 }
1889 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL
1890 && ref->u.ar.as->lower && ref->u.ar.as->upper)
1891 for (i = 0; i < ref->u.ar.as->rank; i++)
1892 {
1893 if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
1894 && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
1895 && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
da9ad923
TB
1896 elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
1897 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2d5b90b2
TB
1898 + 1L;
1899 else
1900 return 0;
1901 }
6da0839a 1902 else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
a0710c29
TB
1903 && e->expr_type == EXPR_VARIABLE)
1904 {
93302a24 1905 if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
a0710c29
TB
1906 || e->symtree->n.sym->attr.pointer)
1907 {
1908 elements = 1;
1909 continue;
1910 }
1911
1912 /* Determine the number of remaining elements in the element
1913 sequence for array element designators. */
1914 is_str_storage = true;
1915 for (i = ref->u.ar.dimen - 1; i >= 0; i--)
1916 {
1917 if (ref->u.ar.start[i] == NULL
1918 || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
1919 || ref->u.ar.as->upper[i] == NULL
1920 || ref->u.ar.as->lower[i] == NULL
1921 || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
1922 || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT)
1923 return 0;
1924
1925 elements
1926 = elements
1927 * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
1928 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
1929 + 1L)
1930 - (mpz_get_si (ref->u.ar.start[i]->value.integer)
1931 - mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
1932 }
1933 }
2d5b90b2
TB
1934 }
1935
6da0839a 1936 if (substrlen)
a0710c29
TB
1937 return (is_str_storage) ? substrlen + (elements-1)*strlen
1938 : elements*strlen;
1939 else
1940 return elements*strlen;
2d5b90b2
TB
1941}
1942
1943
59be8071
TB
1944/* Given an expression, check whether it is an array section
1945 which has a vector subscript. If it has, one is returned,
1946 otherwise zero. */
1947
03af1e4c
DK
1948int
1949gfc_has_vector_subscript (gfc_expr *e)
59be8071
TB
1950{
1951 int i;
1952 gfc_ref *ref;
1953
1954 if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
1955 return 0;
1956
1957 for (ref = e->ref; ref; ref = ref->next)
1958 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
1959 for (i = 0; i < ref->u.ar.dimen; i++)
1960 if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1961 return 1;
1962
1963 return 0;
1964}
1965
1966
6de9cd9a
DN
1967/* Given formal and actual argument lists, see if they are compatible.
1968 If they are compatible, the actual argument list is sorted to
1969 correspond with the formal list, and elements for missing optional
1970 arguments are inserted. If WHERE pointer is nonnull, then we issue
1971 errors when things don't match instead of just returning the status
1972 code. */
1973
f0ac18b7
DK
1974static int
1975compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
1976 int ranks_must_agree, int is_elemental, locus *where)
6de9cd9a 1977{
7b901ac4 1978 gfc_actual_arglist **new_arg, *a, *actual, temp;
6de9cd9a
DN
1979 gfc_formal_arglist *f;
1980 int i, n, na;
2d5b90b2 1981 unsigned long actual_size, formal_size;
6de9cd9a
DN
1982
1983 actual = *ap;
1984
1985 if (actual == NULL && formal == NULL)
1986 return 1;
1987
1988 n = 0;
1989 for (f = formal; f; f = f->next)
1990 n++;
1991
1145e690 1992 new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
6de9cd9a
DN
1993
1994 for (i = 0; i < n; i++)
7b901ac4 1995 new_arg[i] = NULL;
6de9cd9a
DN
1996
1997 na = 0;
1998 f = formal;
1999 i = 0;
2000
2001 for (a = actual; a; a = a->next, f = f->next)
2002 {
7fcafa71
PT
2003 /* Look for keywords but ignore g77 extensions like %VAL. */
2004 if (a->name != NULL && a->name[0] != '%')
6de9cd9a
DN
2005 {
2006 i = 0;
2007 for (f = formal; f; f = f->next, i++)
2008 {
2009 if (f->sym == NULL)
2010 continue;
2011 if (strcmp (f->sym->name, a->name) == 0)
2012 break;
2013 }
2014
2015 if (f == NULL)
2016 {
2017 if (where)
b251af97
SK
2018 gfc_error ("Keyword argument '%s' at %L is not in "
2019 "the procedure", a->name, &a->expr->where);
6de9cd9a
DN
2020 return 0;
2021 }
2022
7b901ac4 2023 if (new_arg[i] != NULL)
6de9cd9a
DN
2024 {
2025 if (where)
b251af97
SK
2026 gfc_error ("Keyword argument '%s' at %L is already associated "
2027 "with another actual argument", a->name,
2028 &a->expr->where);
6de9cd9a
DN
2029 return 0;
2030 }
2031 }
2032
2033 if (f == NULL)
2034 {
2035 if (where)
b251af97
SK
2036 gfc_error ("More actual than formal arguments in procedure "
2037 "call at %L", where);
6de9cd9a
DN
2038
2039 return 0;
2040 }
2041
2042 if (f->sym == NULL && a->expr == NULL)
2043 goto match;
2044
2045 if (f->sym == NULL)
2046 {
2047 if (where)
b251af97
SK
2048 gfc_error ("Missing alternate return spec in subroutine call "
2049 "at %L", where);
6de9cd9a
DN
2050 return 0;
2051 }
2052
2053 if (a->expr == NULL)
2054 {
2055 if (where)
b251af97
SK
2056 gfc_error ("Unexpected alternate return spec in subroutine "
2057 "call at %L", where);
6de9cd9a
DN
2058 return 0;
2059 }
08857b61
TB
2060
2061 if (a->expr->expr_type == EXPR_NULL && !f->sym->attr.pointer
2062 && (f->sym->attr.allocatable || !f->sym->attr.optional
2063 || (gfc_option.allow_std & GFC_STD_F2008) == 0))
2064 {
2065 if (where && (f->sym->attr.allocatable || !f->sym->attr.optional))
2066 gfc_error ("Unexpected NULL() intrinsic at %L to dummy '%s'",
2067 where, f->sym->name);
2068 else if (where)
2069 gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
2070 "dummy '%s'", where, f->sym->name);
2071
2072 return 0;
2073 }
5ad6345e
TB
2074
2075 if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
2076 is_elemental, where))
2077 return 0;
6de9cd9a 2078
a0710c29
TB
2079 /* Special case for character arguments. For allocatable, pointer
2080 and assumed-shape dummies, the string length needs to match
2081 exactly. */
2d5b90b2 2082 if (a->expr->ts.type == BT_CHARACTER
bc21d315
JW
2083 && a->expr->ts.u.cl && a->expr->ts.u.cl->length
2084 && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
2085 && f->sym->ts.u.cl && f->sym->ts.u.cl && f->sym->ts.u.cl->length
2086 && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
a0710c29
TB
2087 && (f->sym->attr.pointer || f->sym->attr.allocatable
2088 || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
bc21d315
JW
2089 && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
2090 f->sym->ts.u.cl->length->value.integer) != 0))
a0324f7b 2091 {
a0710c29
TB
2092 if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
2093 gfc_warning ("Character length mismatch (%ld/%ld) between actual "
2094 "argument and pointer or allocatable dummy argument "
2095 "'%s' at %L",
bc21d315
JW
2096 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
2097 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
a0710c29
TB
2098 f->sym->name, &a->expr->where);
2099 else if (where)
2100 gfc_warning ("Character length mismatch (%ld/%ld) between actual "
2101 "argument and assumed-shape dummy argument '%s' "
2102 "at %L",
bc21d315
JW
2103 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
2104 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
a0710c29
TB
2105 f->sym->name, &a->expr->where);
2106 return 0;
a0324f7b
TB
2107 }
2108
8d51f26f
PT
2109 if ((f->sym->attr.pointer || f->sym->attr.allocatable)
2110 && f->sym->ts.deferred != a->expr->ts.deferred
2111 && a->expr->ts.type == BT_CHARACTER)
2112 {
2113 if (where)
2114 gfc_error ("Actual argument argument at %L to allocatable or "
2115 "pointer dummy argument '%s' must have a deferred "
2116 "length type parameter if and only if the dummy has one",
2117 &a->expr->where, f->sym->name);
2118 return 0;
2119 }
2120
37639728
TB
2121 actual_size = get_expr_storage_size (a->expr);
2122 formal_size = get_sym_storage_size (f->sym);
93302a24
JW
2123 if (actual_size != 0 && actual_size < formal_size
2124 && a->expr->ts.type != BT_PROCEDURE
2125 && f->sym->attr.flavor != FL_PROCEDURE)
2d5b90b2
TB
2126 {
2127 if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
2128 gfc_warning ("Character length of actual argument shorter "
8d51f26f
PT
2129 "than of dummy argument '%s' (%lu/%lu) at %L",
2130 f->sym->name, actual_size, formal_size,
2131 &a->expr->where);
2d5b90b2
TB
2132 else if (where)
2133 gfc_warning ("Actual argument contains too few "
8d51f26f
PT
2134 "elements for dummy argument '%s' (%lu/%lu) at %L",
2135 f->sym->name, actual_size, formal_size,
2136 &a->expr->where);
2d5b90b2
TB
2137 return 0;
2138 }
2139
8fb74da4
JW
2140 /* Satisfy 12.4.1.3 by ensuring that a procedure pointer actual argument
2141 is provided for a procedure pointer formal argument. */
2142 if (f->sym->attr.proc_pointer
a7c0b11d
JW
2143 && !((a->expr->expr_type == EXPR_VARIABLE
2144 && a->expr->symtree->n.sym->attr.proc_pointer)
2145 || (a->expr->expr_type == EXPR_FUNCTION
2146 && a->expr->symtree->n.sym->result->attr.proc_pointer)
f64edc8b 2147 || gfc_is_proc_ptr_comp (a->expr, NULL)))
8fb74da4
JW
2148 {
2149 if (where)
2150 gfc_error ("Expected a procedure pointer for argument '%s' at %L",
2151 f->sym->name, &a->expr->where);
2152 return 0;
2153 }
2154
699fa7aa
PT
2155 /* Satisfy 12.4.1.2 by ensuring that a procedure actual argument is
2156 provided for a procedure formal argument. */
f64edc8b 2157 if (a->expr->ts.type != BT_PROCEDURE && !gfc_is_proc_ptr_comp (a->expr, NULL)
699fa7aa
PT
2158 && a->expr->expr_type == EXPR_VARIABLE
2159 && f->sym->attr.flavor == FL_PROCEDURE)
2160 {
9914f8cf
PT
2161 if (where)
2162 gfc_error ("Expected a procedure for argument '%s' at %L",
2163 f->sym->name, &a->expr->where);
2164 return 0;
699fa7aa
PT
2165 }
2166
b251af97
SK
2167 if (f->sym->attr.flavor == FL_PROCEDURE && f->sym->attr.pure
2168 && a->expr->ts.type == BT_PROCEDURE
2169 && !a->expr->symtree->n.sym->attr.pure)
d68bd5a8
PT
2170 {
2171 if (where)
2172 gfc_error ("Expected a PURE procedure for argument '%s' at %L",
2173 f->sym->name, &a->expr->where);
2174 return 0;
2175 }
2176
b251af97 2177 if (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE
bf9d2177
JJ
2178 && a->expr->expr_type == EXPR_VARIABLE
2179 && a->expr->symtree->n.sym->as
2180 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
2181 && (a->expr->ref == NULL
2182 || (a->expr->ref->type == REF_ARRAY
2183 && a->expr->ref->u.ar.type == AR_FULL)))
2184 {
2185 if (where)
2186 gfc_error ("Actual argument for '%s' cannot be an assumed-size"
2187 " array at %L", f->sym->name, where);
2188 return 0;
2189 }
2190
1600fe22
TS
2191 if (a->expr->expr_type != EXPR_NULL
2192 && compare_pointer (f->sym, a->expr) == 0)
6de9cd9a
DN
2193 {
2194 if (where)
2195 gfc_error ("Actual argument for '%s' must be a pointer at %L",
2196 f->sym->name, &a->expr->where);
2197 return 0;
2198 }
2199
7d54ef80
TB
2200 if (a->expr->expr_type != EXPR_NULL
2201 && (gfc_option.allow_std & GFC_STD_F2008) == 0
2202 && compare_pointer (f->sym, a->expr) == 2)
2203 {
2204 if (where)
2205 gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
2206 "pointer dummy '%s'", &a->expr->where,f->sym->name);
2207 return 0;
2208 }
2209
2210
d3a9eea2
TB
2211 /* Fortran 2008, C1242. */
2212 if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
2213 {
2214 if (where)
2215 gfc_error ("Coindexed actual argument at %L to pointer "
2216 "dummy '%s'",
2217 &a->expr->where, f->sym->name);
2218 return 0;
2219 }
2220
2221 /* Fortran 2008, 12.5.2.5 (no constraint). */
2222 if (a->expr->expr_type == EXPR_VARIABLE
2223 && f->sym->attr.intent != INTENT_IN
2224 && f->sym->attr.allocatable
2225 && gfc_is_coindexed (a->expr))
2226 {
2227 if (where)
2228 gfc_error ("Coindexed actual argument at %L to allocatable "
2229 "dummy '%s' requires INTENT(IN)",
2230 &a->expr->where, f->sym->name);
2231 return 0;
2232 }
2233
2234 /* Fortran 2008, C1237. */
2235 if (a->expr->expr_type == EXPR_VARIABLE
2236 && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
2237 && gfc_is_coindexed (a->expr)
2238 && (a->expr->symtree->n.sym->attr.volatile_
2239 || a->expr->symtree->n.sym->attr.asynchronous))
2240 {
2241 if (where)
2242 gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
2243 "at %L requires that dummy %s' has neither "
2244 "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
2245 f->sym->name);
2246 return 0;
2247 }
2248
2249 /* Fortran 2008, 12.5.2.4 (no constraint). */
2250 if (a->expr->expr_type == EXPR_VARIABLE
2251 && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
2252 && gfc_is_coindexed (a->expr)
2253 && gfc_has_ultimate_allocatable (a->expr))
2254 {
2255 if (where)
2256 gfc_error ("Coindexed actual argument at %L with allocatable "
2257 "ultimate component to dummy '%s' requires either VALUE "
2258 "or INTENT(IN)", &a->expr->where, f->sym->name);
2259 return 0;
2260 }
2261
aa08038d
EE
2262 if (a->expr->expr_type != EXPR_NULL
2263 && compare_allocatable (f->sym, a->expr) == 0)
2264 {
2265 if (where)
2266 gfc_error ("Actual argument for '%s' must be ALLOCATABLE at %L",
2267 f->sym->name, &a->expr->where);
2268 return 0;
2269 }
2270
a920e94a 2271 /* Check intent = OUT/INOUT for definable actual argument. */
8c91ab34
DK
2272 if ((f->sym->attr.intent == INTENT_OUT
2273 || f->sym->attr.intent == INTENT_INOUT))
a920e94a 2274 {
8c91ab34
DK
2275 const char* context = (where
2276 ? _("actual argument to INTENT = OUT/INOUT")
2277 : NULL);
a920e94a 2278
8c91ab34
DK
2279 if (f->sym->attr.pointer
2280 && gfc_check_vardef_context (a->expr, true, context)
2281 == FAILURE)
2282 return 0;
2283 if (gfc_check_vardef_context (a->expr, false, context)
2284 == FAILURE)
2285 return 0;
ee7e677f
TB
2286 }
2287
59be8071
TB
2288 if ((f->sym->attr.intent == INTENT_OUT
2289 || f->sym->attr.intent == INTENT_INOUT
84efddb2
DF
2290 || f->sym->attr.volatile_
2291 || f->sym->attr.asynchronous)
03af1e4c 2292 && gfc_has_vector_subscript (a->expr))
59be8071
TB
2293 {
2294 if (where)
84efddb2
DF
2295 gfc_error ("Array-section actual argument with vector "
2296 "subscripts at %L is incompatible with INTENT(OUT), "
2297 "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
2298 "of the dummy argument '%s'",
59be8071
TB
2299 &a->expr->where, f->sym->name);
2300 return 0;
2301 }
2302
9bce3c1c
TB
2303 /* C1232 (R1221) For an actual argument which is an array section or
2304 an assumed-shape array, the dummy argument shall be an assumed-
2305 shape array, if the dummy argument has the VOLATILE attribute. */
2306
2307 if (f->sym->attr.volatile_
2308 && a->expr->symtree->n.sym->as
2309 && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
2310 && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2311 {
2312 if (where)
2313 gfc_error ("Assumed-shape actual argument at %L is "
2314 "incompatible with the non-assumed-shape "
2315 "dummy argument '%s' due to VOLATILE attribute",
2316 &a->expr->where,f->sym->name);
2317 return 0;
2318 }
2319
2320 if (f->sym->attr.volatile_
2321 && a->expr->ref && a->expr->ref->u.ar.type == AR_SECTION
2322 && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2323 {
2324 if (where)
2325 gfc_error ("Array-section actual argument at %L is "
2326 "incompatible with the non-assumed-shape "
2327 "dummy argument '%s' due to VOLATILE attribute",
2328 &a->expr->where,f->sym->name);
2329 return 0;
2330 }
2331
2332 /* C1233 (R1221) For an actual argument which is a pointer array, the
2333 dummy argument shall be an assumed-shape or pointer array, if the
2334 dummy argument has the VOLATILE attribute. */
2335
2336 if (f->sym->attr.volatile_
2337 && a->expr->symtree->n.sym->attr.pointer
2338 && a->expr->symtree->n.sym->as
2339 && !(f->sym->as
2340 && (f->sym->as->type == AS_ASSUMED_SHAPE
2341 || f->sym->attr.pointer)))
2342 {
2343 if (where)
2344 gfc_error ("Pointer-array actual argument at %L requires "
2345 "an assumed-shape or pointer-array dummy "
2346 "argument '%s' due to VOLATILE attribute",
2347 &a->expr->where,f->sym->name);
2348 return 0;
2349 }
2350
6de9cd9a
DN
2351 match:
2352 if (a == actual)
2353 na = i;
2354
7b901ac4 2355 new_arg[i++] = a;
6de9cd9a
DN
2356 }
2357
2358 /* Make sure missing actual arguments are optional. */
2359 i = 0;
2360 for (f = formal; f; f = f->next, i++)
2361 {
7b901ac4 2362 if (new_arg[i] != NULL)
6de9cd9a 2363 continue;
3ab7b3de
BM
2364 if (f->sym == NULL)
2365 {
2366 if (where)
b251af97
SK
2367 gfc_error ("Missing alternate return spec in subroutine call "
2368 "at %L", where);
3ab7b3de
BM
2369 return 0;
2370 }
6de9cd9a
DN
2371 if (!f->sym->attr.optional)
2372 {
2373 if (where)
2374 gfc_error ("Missing actual argument for argument '%s' at %L",
2375 f->sym->name, where);
2376 return 0;
2377 }
2378 }
2379
2380 /* The argument lists are compatible. We now relink a new actual
2381 argument list with null arguments in the right places. The head
2382 of the list remains the head. */
2383 for (i = 0; i < n; i++)
7b901ac4
KG
2384 if (new_arg[i] == NULL)
2385 new_arg[i] = gfc_get_actual_arglist ();
6de9cd9a
DN
2386
2387 if (na != 0)
2388 {
7b901ac4
KG
2389 temp = *new_arg[0];
2390 *new_arg[0] = *actual;
6de9cd9a
DN
2391 *actual = temp;
2392
7b901ac4
KG
2393 a = new_arg[0];
2394 new_arg[0] = new_arg[na];
2395 new_arg[na] = a;
6de9cd9a
DN
2396 }
2397
2398 for (i = 0; i < n - 1; i++)
7b901ac4 2399 new_arg[i]->next = new_arg[i + 1];
6de9cd9a 2400
7b901ac4 2401 new_arg[i]->next = NULL;
6de9cd9a
DN
2402
2403 if (*ap == NULL && n > 0)
7b901ac4 2404 *ap = new_arg[0];
6de9cd9a 2405
1600fe22 2406 /* Note the types of omitted optional arguments. */
b5ca4fd2 2407 for (a = *ap, f = formal; a; a = a->next, f = f->next)
1600fe22
TS
2408 if (a->expr == NULL && a->label == NULL)
2409 a->missing_arg_type = f->sym->ts.type;
2410
6de9cd9a
DN
2411 return 1;
2412}
2413
2414
2415typedef struct
2416{
2417 gfc_formal_arglist *f;
2418 gfc_actual_arglist *a;
2419}
2420argpair;
2421
2422/* qsort comparison function for argument pairs, with the following
2423 order:
2424 - p->a->expr == NULL
2425 - p->a->expr->expr_type != EXPR_VARIABLE
f7b529fa 2426 - growing p->a->expr->symbol. */
6de9cd9a
DN
2427
2428static int
2429pair_cmp (const void *p1, const void *p2)
2430{
2431 const gfc_actual_arglist *a1, *a2;
2432
2433 /* *p1 and *p2 are elements of the to-be-sorted array. */
2434 a1 = ((const argpair *) p1)->a;
2435 a2 = ((const argpair *) p2)->a;
2436 if (!a1->expr)
2437 {
2438 if (!a2->expr)
2439 return 0;
2440 return -1;
2441 }
2442 if (!a2->expr)
2443 return 1;
2444 if (a1->expr->expr_type != EXPR_VARIABLE)
2445 {
2446 if (a2->expr->expr_type != EXPR_VARIABLE)
2447 return 0;
2448 return -1;
2449 }
2450 if (a2->expr->expr_type != EXPR_VARIABLE)
2451 return 1;
2452 return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
2453}
2454
2455
2456/* Given two expressions from some actual arguments, test whether they
2457 refer to the same expression. The analysis is conservative.
2458 Returning FAILURE will produce no warning. */
2459
17b1d2a0 2460static gfc_try
b251af97 2461compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
6de9cd9a
DN
2462{
2463 const gfc_ref *r1, *r2;
2464
2465 if (!e1 || !e2
2466 || e1->expr_type != EXPR_VARIABLE
2467 || e2->expr_type != EXPR_VARIABLE
2468 || e1->symtree->n.sym != e2->symtree->n.sym)
2469 return FAILURE;
2470
2471 /* TODO: improve comparison, see expr.c:show_ref(). */
2472 for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
2473 {
2474 if (r1->type != r2->type)
2475 return FAILURE;
2476 switch (r1->type)
2477 {
2478 case REF_ARRAY:
2479 if (r1->u.ar.type != r2->u.ar.type)
2480 return FAILURE;
2481 /* TODO: At the moment, consider only full arrays;
2482 we could do better. */
2483 if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
2484 return FAILURE;
2485 break;
2486
2487 case REF_COMPONENT:
2488 if (r1->u.c.component != r2->u.c.component)
2489 return FAILURE;
2490 break;
2491
2492 case REF_SUBSTRING:
2493 return FAILURE;
2494
2495 default:
2496 gfc_internal_error ("compare_actual_expr(): Bad component code");
2497 }
2498 }
2499 if (!r1 && !r2)
2500 return SUCCESS;
2501 return FAILURE;
2502}
2503
b251af97 2504
6de9cd9a
DN
2505/* Given formal and actual argument lists that correspond to one
2506 another, check that identical actual arguments aren't not
2507 associated with some incompatible INTENTs. */
2508
17b1d2a0 2509static gfc_try
b251af97 2510check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
6de9cd9a
DN
2511{
2512 sym_intent f1_intent, f2_intent;
2513 gfc_formal_arglist *f1;
2514 gfc_actual_arglist *a1;
2515 size_t n, i, j;
2516 argpair *p;
17b1d2a0 2517 gfc_try t = SUCCESS;
6de9cd9a
DN
2518
2519 n = 0;
2520 for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
2521 {
2522 if (f1 == NULL && a1 == NULL)
2523 break;
2524 if (f1 == NULL || a1 == NULL)
2525 gfc_internal_error ("check_some_aliasing(): List mismatch");
2526 n++;
2527 }
2528 if (n == 0)
2529 return t;
1145e690 2530 p = XALLOCAVEC (argpair, n);
6de9cd9a
DN
2531
2532 for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
2533 {
2534 p[i].f = f1;
2535 p[i].a = a1;
2536 }
2537
2538 qsort (p, n, sizeof (argpair), pair_cmp);
2539
2540 for (i = 0; i < n; i++)
2541 {
2542 if (!p[i].a->expr
2543 || p[i].a->expr->expr_type != EXPR_VARIABLE
2544 || p[i].a->expr->ts.type == BT_PROCEDURE)
2545 continue;
2546 f1_intent = p[i].f->sym->attr.intent;
2547 for (j = i + 1; j < n; j++)
2548 {
2549 /* Expected order after the sort. */
2550 if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
2551 gfc_internal_error ("check_some_aliasing(): corrupted data");
2552
2553 /* Are the expression the same? */
2554 if (compare_actual_expr (p[i].a->expr, p[j].a->expr) == FAILURE)
2555 break;
2556 f2_intent = p[j].f->sym->attr.intent;
2557 if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
2558 || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN))
2559 {
2560 gfc_warning ("Same actual argument associated with INTENT(%s) "
2561 "argument '%s' and INTENT(%s) argument '%s' at %L",
2562 gfc_intent_string (f1_intent), p[i].f->sym->name,
2563 gfc_intent_string (f2_intent), p[j].f->sym->name,
2564 &p[i].a->expr->where);
2565 t = FAILURE;
2566 }
2567 }
2568 }
2569
2570 return t;
2571}
2572
2573
f17facac 2574/* Given a symbol of a formal argument list and an expression,
86bf520d 2575 return nonzero if their intents are compatible, zero otherwise. */
f17facac
TB
2576
2577static int
b251af97 2578compare_parameter_intent (gfc_symbol *formal, gfc_expr *actual)
f17facac 2579{
b251af97 2580 if (actual->symtree->n.sym->attr.pointer && !formal->attr.pointer)
f17facac
TB
2581 return 1;
2582
2583 if (actual->symtree->n.sym->attr.intent != INTENT_IN)
2584 return 1;
2585
b251af97 2586 if (formal->attr.intent == INTENT_INOUT || formal->attr.intent == INTENT_OUT)
f17facac
TB
2587 return 0;
2588
2589 return 1;
2590}
2591
2592
6de9cd9a
DN
2593/* Given formal and actual argument lists that correspond to one
2594 another, check that they are compatible in the sense that intents
2595 are not mismatched. */
2596
17b1d2a0 2597static gfc_try
b251af97 2598check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
6de9cd9a 2599{
f17facac 2600 sym_intent f_intent;
6de9cd9a
DN
2601
2602 for (;; f = f->next, a = a->next)
2603 {
2604 if (f == NULL && a == NULL)
2605 break;
2606 if (f == NULL || a == NULL)
2607 gfc_internal_error ("check_intents(): List mismatch");
2608
2609 if (a->expr == NULL || a->expr->expr_type != EXPR_VARIABLE)
2610 continue;
2611
6de9cd9a
DN
2612 f_intent = f->sym->attr.intent;
2613
f17facac 2614 if (!compare_parameter_intent(f->sym, a->expr))
6de9cd9a 2615 {
6de9cd9a
DN
2616 gfc_error ("Procedure argument at %L is INTENT(IN) while interface "
2617 "specifies INTENT(%s)", &a->expr->where,
2618 gfc_intent_string (f_intent));
2619 return FAILURE;
2620 }
2621
2622 if (gfc_pure (NULL) && gfc_impure_variable (a->expr->symtree->n.sym))
2623 {
2624 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
2625 {
b251af97
SK
2626 gfc_error ("Procedure argument at %L is local to a PURE "
2627 "procedure and is passed to an INTENT(%s) argument",
2628 &a->expr->where, gfc_intent_string (f_intent));
6de9cd9a
DN
2629 return FAILURE;
2630 }
2631
c4e3543d 2632 if (f->sym->attr.pointer)
6de9cd9a 2633 {
b251af97
SK
2634 gfc_error ("Procedure argument at %L is local to a PURE "
2635 "procedure and has the POINTER attribute",
2636 &a->expr->where);
6de9cd9a
DN
2637 return FAILURE;
2638 }
2639 }
d3a9eea2
TB
2640
2641 /* Fortran 2008, C1283. */
2642 if (gfc_pure (NULL) && gfc_is_coindexed (a->expr))
2643 {
2644 if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
2645 {
2646 gfc_error ("Coindexed actual argument at %L in PURE procedure "
2647 "is passed to an INTENT(%s) argument",
2648 &a->expr->where, gfc_intent_string (f_intent));
2649 return FAILURE;
2650 }
2651
2652 if (f->sym->attr.pointer)
2653 {
2654 gfc_error ("Coindexed actual argument at %L in PURE procedure "
2655 "is passed to a POINTER dummy argument",
2656 &a->expr->where);
2657 return FAILURE;
2658 }
2659 }
2660
2661 /* F2008, Section 12.5.2.4. */
2662 if (a->expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
2663 && gfc_is_coindexed (a->expr))
2664 {
2665 gfc_error ("Coindexed polymorphic actual argument at %L is passed "
2666 "polymorphic dummy argument '%s'",
2667 &a->expr->where, f->sym->name);
2668 return FAILURE;
2669 }
6de9cd9a
DN
2670 }
2671
2672 return SUCCESS;
2673}
2674
2675
2676/* Check how a procedure is used against its interface. If all goes
2677 well, the actual argument list will also end up being properly
2678 sorted. */
2679
2680void
b251af97 2681gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
6de9cd9a 2682{
c4bbc105 2683
a9c5fe7e
TK
2684 /* Warn about calls with an implicit interface. Special case
2685 for calling a ISO_C_BINDING becase c_loc and c_funloc
ca071303
FXC
2686 are pseudo-unknown. Additionally, warn about procedures not
2687 explicitly declared at all if requested. */
2688 if (sym->attr.if_source == IFSRC_UNKNOWN && ! sym->attr.is_iso_c)
2689 {
2690 if (gfc_option.warn_implicit_interface)
2691 gfc_warning ("Procedure '%s' called with an implicit interface at %L",
2692 sym->name, where);
2693 else if (gfc_option.warn_implicit_procedure
2694 && sym->attr.proc == PROC_UNKNOWN)
2695 gfc_warning ("Procedure '%s' called at %L is not explicitly declared",
2696 sym->name, where);
2697 }
6de9cd9a 2698
e6895430 2699 if (sym->attr.if_source == IFSRC_UNKNOWN)
ac05557c
DF
2700 {
2701 gfc_actual_arglist *a;
86d7449c
TB
2702
2703 if (sym->attr.pointer)
2704 {
2705 gfc_error("The pointer object '%s' at %L must have an explicit "
2706 "function interface or be declared as array",
2707 sym->name, where);
2708 return;
2709 }
2710
2711 if (sym->attr.allocatable && !sym->attr.external)
2712 {
2713 gfc_error("The allocatable object '%s' at %L must have an explicit "
2714 "function interface or be declared as array",
2715 sym->name, where);
2716 return;
2717 }
2718
2719 if (sym->attr.allocatable)
2720 {
2721 gfc_error("Allocatable function '%s' at %L must have an explicit "
2722 "function interface", sym->name, where);
2723 return;
2724 }
2725
ac05557c
DF
2726 for (a = *ap; a; a = a->next)
2727 {
2728 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
2729 if (a->name != NULL && a->name[0] != '%')
2730 {
2731 gfc_error("Keyword argument requires explicit interface "
2732 "for procedure '%s' at %L", sym->name, &a->expr->where);
2733 break;
2734 }
2735 }
2736
2737 return;
2738 }
2739
f0ac18b7 2740 if (!compare_actual_formal (ap, sym->formal, 0, sym->attr.elemental, where))
6de9cd9a
DN
2741 return;
2742
2743 check_intents (sym->formal, *ap);
2744 if (gfc_option.warn_aliasing)
2745 check_some_aliasing (sym->formal, *ap);
2746}
2747
2748
7e196f89
JW
2749/* Check how a procedure pointer component is used against its interface.
2750 If all goes well, the actual argument list will also end up being properly
2751 sorted. Completely analogous to gfc_procedure_use. */
2752
2753void
2754gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
2755{
2756
2757 /* Warn about calls with an implicit interface. Special case
2758 for calling a ISO_C_BINDING becase c_loc and c_funloc
2759 are pseudo-unknown. */
2760 if (gfc_option.warn_implicit_interface
2761 && comp->attr.if_source == IFSRC_UNKNOWN
2762 && !comp->attr.is_iso_c)
2763 gfc_warning ("Procedure pointer component '%s' called with an implicit "
2764 "interface at %L", comp->name, where);
2765
2766 if (comp->attr.if_source == IFSRC_UNKNOWN)
2767 {
2768 gfc_actual_arglist *a;
2769 for (a = *ap; a; a = a->next)
2770 {
2771 /* Skip g77 keyword extensions like %VAL, %REF, %LOC. */
2772 if (a->name != NULL && a->name[0] != '%')
2773 {
2774 gfc_error("Keyword argument requires explicit interface "
2775 "for procedure pointer component '%s' at %L",
2776 comp->name, &a->expr->where);
2777 break;
2778 }
2779 }
2780
2781 return;
2782 }
2783
2784 if (!compare_actual_formal (ap, comp->formal, 0, comp->attr.elemental, where))
2785 return;
2786
2787 check_intents (comp->formal, *ap);
2788 if (gfc_option.warn_aliasing)
2789 check_some_aliasing (comp->formal, *ap);
2790}
2791
2792
f0ac18b7
DK
2793/* Try if an actual argument list matches the formal list of a symbol,
2794 respecting the symbol's attributes like ELEMENTAL. This is used for
2795 GENERIC resolution. */
2796
2797bool
2798gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
2799{
2800 bool r;
2801
2802 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
2803
2804 r = !sym->attr.elemental;
2805 if (compare_actual_formal (args, sym->formal, r, !r, NULL))
2806 {
2807 check_intents (sym->formal, *args);
2808 if (gfc_option.warn_aliasing)
2809 check_some_aliasing (sym->formal, *args);
2810 return true;
2811 }
2812
2813 return false;
2814}
2815
2816
6de9cd9a
DN
2817/* Given an interface pointer and an actual argument list, search for
2818 a formal argument list that matches the actual. If found, returns
2819 a pointer to the symbol of the correct interface. Returns NULL if
2820 not found. */
2821
2822gfc_symbol *
b251af97
SK
2823gfc_search_interface (gfc_interface *intr, int sub_flag,
2824 gfc_actual_arglist **ap)
6de9cd9a 2825{
22a0a780 2826 gfc_symbol *elem_sym = NULL;
6de9cd9a
DN
2827 for (; intr; intr = intr->next)
2828 {
2829 if (sub_flag && intr->sym->attr.function)
2830 continue;
2831 if (!sub_flag && intr->sym->attr.subroutine)
2832 continue;
2833
f0ac18b7 2834 if (gfc_arglist_matches_symbol (ap, intr->sym))
22a0a780
PT
2835 {
2836 /* Satisfy 12.4.4.1 such that an elemental match has lower
2837 weight than a non-elemental match. */
2838 if (intr->sym->attr.elemental)
2839 {
2840 elem_sym = intr->sym;
2841 continue;
2842 }
2843 return intr->sym;
2844 }
6de9cd9a
DN
2845 }
2846
22a0a780 2847 return elem_sym ? elem_sym : NULL;
6de9cd9a
DN
2848}
2849
2850
2851/* Do a brute force recursive search for a symbol. */
2852
2853static gfc_symtree *
b251af97 2854find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
6de9cd9a
DN
2855{
2856 gfc_symtree * st;
2857
2858 if (root->n.sym == sym)
2859 return root;
2860
2861 st = NULL;
2862 if (root->left)
2863 st = find_symtree0 (root->left, sym);
2864 if (root->right && ! st)
2865 st = find_symtree0 (root->right, sym);
2866 return st;
2867}
2868
2869
2870/* Find a symtree for a symbol. */
2871
f6fad28e
DK
2872gfc_symtree *
2873gfc_find_sym_in_symtree (gfc_symbol *sym)
6de9cd9a
DN
2874{
2875 gfc_symtree *st;
2876 gfc_namespace *ns;
2877
2878 /* First try to find it by name. */
2879 gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
2880 if (st && st->n.sym == sym)
2881 return st;
2882
66e4ab31 2883 /* If it's been renamed, resort to a brute-force search. */
6de9cd9a
DN
2884 /* TODO: avoid having to do this search. If the symbol doesn't exist
2885 in the symtree for the current namespace, it should probably be added. */
2886 for (ns = gfc_current_ns; ns; ns = ns->parent)
2887 {
2888 st = find_symtree0 (ns->sym_root, sym);
2889 if (st)
b251af97 2890 return st;
6de9cd9a
DN
2891 }
2892 gfc_internal_error ("Unable to find symbol %s", sym->name);
66e4ab31 2893 /* Not reached. */
6de9cd9a
DN
2894}
2895
2896
4a44a72d
DK
2897/* See if the arglist to an operator-call contains a derived-type argument
2898 with a matching type-bound operator. If so, return the matching specific
2899 procedure defined as operator-target as well as the base-object to use
974df0f8
PT
2900 (which is the found derived-type argument with operator). The generic
2901 name, if any, is transmitted to the final expression via 'gname'. */
4a44a72d
DK
2902
2903static gfc_typebound_proc*
2904matching_typebound_op (gfc_expr** tb_base,
2905 gfc_actual_arglist* args,
974df0f8
PT
2906 gfc_intrinsic_op op, const char* uop,
2907 const char ** gname)
4a44a72d
DK
2908{
2909 gfc_actual_arglist* base;
2910
2911 for (base = args; base; base = base->next)
4b7dd692 2912 if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
4a44a72d
DK
2913 {
2914 gfc_typebound_proc* tb;
2915 gfc_symbol* derived;
2916 gfc_try result;
2917
4b7dd692 2918 if (base->expr->ts.type == BT_CLASS)
528622fd
JW
2919 {
2920 if (!gfc_expr_attr (base->expr).class_ok)
2921 continue;
2922 derived = CLASS_DATA (base->expr)->ts.u.derived;
2923 }
4b7dd692
JW
2924 else
2925 derived = base->expr->ts.u.derived;
4a44a72d
DK
2926
2927 if (op == INTRINSIC_USER)
2928 {
2929 gfc_symtree* tb_uop;
2930
2931 gcc_assert (uop);
2932 tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
2933 false, NULL);
2934
2935 if (tb_uop)
2936 tb = tb_uop->n.tb;
2937 else
2938 tb = NULL;
2939 }
2940 else
2941 tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
2942 false, NULL);
2943
2944 /* This means we hit a PRIVATE operator which is use-associated and
2945 should thus not be seen. */
2946 if (result == FAILURE)
2947 tb = NULL;
2948
2949 /* Look through the super-type hierarchy for a matching specific
2950 binding. */
2951 for (; tb; tb = tb->overridden)
2952 {
2953 gfc_tbp_generic* g;
2954
2955 gcc_assert (tb->is_generic);
2956 for (g = tb->u.generic; g; g = g->next)
2957 {
2958 gfc_symbol* target;
2959 gfc_actual_arglist* argcopy;
2960 bool matches;
2961
2962 gcc_assert (g->specific);
2963 if (g->specific->error)
2964 continue;
2965
2966 target = g->specific->u.specific->n.sym;
2967
2968 /* Check if this arglist matches the formal. */
2969 argcopy = gfc_copy_actual_arglist (args);
2970 matches = gfc_arglist_matches_symbol (&argcopy, target);
2971 gfc_free_actual_arglist (argcopy);
2972
2973 /* Return if we found a match. */
2974 if (matches)
2975 {
2976 *tb_base = base->expr;
974df0f8 2977 *gname = g->specific_st->name;
4a44a72d
DK
2978 return g->specific;
2979 }
2980 }
2981 }
2982 }
2983
2984 return NULL;
2985}
2986
2987
2988/* For the 'actual arglist' of an operator call and a specific typebound
2989 procedure that has been found the target of a type-bound operator, build the
2990 appropriate EXPR_COMPCALL and resolve it. We take this indirection over
2991 type-bound procedures rather than resolving type-bound operators 'directly'
2992 so that we can reuse the existing logic. */
2993
2994static void
2995build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
974df0f8
PT
2996 gfc_expr* base, gfc_typebound_proc* target,
2997 const char *gname)
4a44a72d
DK
2998{
2999 e->expr_type = EXPR_COMPCALL;
3000 e->value.compcall.tbp = target;
974df0f8 3001 e->value.compcall.name = gname ? gname : "$op";
4a44a72d
DK
3002 e->value.compcall.actual = actual;
3003 e->value.compcall.base_object = base;
3004 e->value.compcall.ignore_pass = 1;
3005 e->value.compcall.assign = 0;
3006}
3007
3008
6de9cd9a
DN
3009/* This subroutine is called when an expression is being resolved.
3010 The expression node in question is either a user defined operator
1f2959f0 3011 or an intrinsic operator with arguments that aren't compatible
6de9cd9a
DN
3012 with the operator. This subroutine builds an actual argument list
3013 corresponding to the operands, then searches for a compatible
3014 interface. If one is found, the expression node is replaced with
4a44a72d
DK
3015 the appropriate function call.
3016 real_error is an additional output argument that specifies if FAILURE
3017 is because of some real error and not because no match was found. */
6de9cd9a 3018
17b1d2a0 3019gfc_try
4a44a72d 3020gfc_extend_expr (gfc_expr *e, bool *real_error)
6de9cd9a
DN
3021{
3022 gfc_actual_arglist *actual;
3023 gfc_symbol *sym;
3024 gfc_namespace *ns;
3025 gfc_user_op *uop;
3026 gfc_intrinsic_op i;
974df0f8 3027 const char *gname;
6de9cd9a
DN
3028
3029 sym = NULL;
3030
3031 actual = gfc_get_actual_arglist ();
58b03ab2 3032 actual->expr = e->value.op.op1;
6de9cd9a 3033
4a44a72d 3034 *real_error = false;
974df0f8 3035 gname = NULL;
4a44a72d 3036
58b03ab2 3037 if (e->value.op.op2 != NULL)
6de9cd9a
DN
3038 {
3039 actual->next = gfc_get_actual_arglist ();
58b03ab2 3040 actual->next->expr = e->value.op.op2;
6de9cd9a
DN
3041 }
3042
e8d4f3fc 3043 i = fold_unary_intrinsic (e->value.op.op);
6de9cd9a
DN
3044
3045 if (i == INTRINSIC_USER)
3046 {
3047 for (ns = gfc_current_ns; ns; ns = ns->parent)
3048 {
58b03ab2 3049 uop = gfc_find_uop (e->value.op.uop->name, ns);
6de9cd9a
DN
3050 if (uop == NULL)
3051 continue;
3052
a1ee985f 3053 sym = gfc_search_interface (uop->op, 0, &actual);
6de9cd9a
DN
3054 if (sym != NULL)
3055 break;
3056 }
3057 }
3058 else
3059 {
3060 for (ns = gfc_current_ns; ns; ns = ns->parent)
3061 {
3bed9dd0
DF
3062 /* Due to the distinction between '==' and '.eq.' and friends, one has
3063 to check if either is defined. */
3064 switch (i)
3065 {
4a44a72d
DK
3066#define CHECK_OS_COMPARISON(comp) \
3067 case INTRINSIC_##comp: \
3068 case INTRINSIC_##comp##_OS: \
3069 sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
3070 if (!sym) \
3071 sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
3072 break;
3073 CHECK_OS_COMPARISON(EQ)
3074 CHECK_OS_COMPARISON(NE)
3075 CHECK_OS_COMPARISON(GT)
3076 CHECK_OS_COMPARISON(GE)
3077 CHECK_OS_COMPARISON(LT)
3078 CHECK_OS_COMPARISON(LE)
3079#undef CHECK_OS_COMPARISON
3bed9dd0
DF
3080
3081 default:
a1ee985f 3082 sym = gfc_search_interface (ns->op[i], 0, &actual);
3bed9dd0
DF
3083 }
3084
6de9cd9a
DN
3085 if (sym != NULL)
3086 break;
3087 }
3088 }
3089
4a44a72d
DK
3090 /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
3091 found rather than just taking the first one and not checking further. */
3092
6de9cd9a
DN
3093 if (sym == NULL)
3094 {
4a44a72d
DK
3095 gfc_typebound_proc* tbo;
3096 gfc_expr* tb_base;
3097
3098 /* See if we find a matching type-bound operator. */
3099 if (i == INTRINSIC_USER)
3100 tbo = matching_typebound_op (&tb_base, actual,
974df0f8 3101 i, e->value.op.uop->name, &gname);
4a44a72d
DK
3102 else
3103 switch (i)
3104 {
3105#define CHECK_OS_COMPARISON(comp) \
3106 case INTRINSIC_##comp: \
3107 case INTRINSIC_##comp##_OS: \
3108 tbo = matching_typebound_op (&tb_base, actual, \
974df0f8 3109 INTRINSIC_##comp, NULL, &gname); \
4a44a72d
DK
3110 if (!tbo) \
3111 tbo = matching_typebound_op (&tb_base, actual, \
974df0f8 3112 INTRINSIC_##comp##_OS, NULL, &gname); \
4a44a72d
DK
3113 break;
3114 CHECK_OS_COMPARISON(EQ)
3115 CHECK_OS_COMPARISON(NE)
3116 CHECK_OS_COMPARISON(GT)
3117 CHECK_OS_COMPARISON(GE)
3118 CHECK_OS_COMPARISON(LT)
3119 CHECK_OS_COMPARISON(LE)
3120#undef CHECK_OS_COMPARISON
3121
3122 default:
974df0f8 3123 tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
4a44a72d
DK
3124 break;
3125 }
3126
3127 /* If there is a matching typebound-operator, replace the expression with
3128 a call to it and succeed. */
3129 if (tbo)
3130 {
3131 gfc_try result;
3132
3133 gcc_assert (tb_base);
974df0f8 3134 build_compcall_for_operator (e, actual, tb_base, tbo, gname);
4a44a72d
DK
3135
3136 result = gfc_resolve_expr (e);
3137 if (result == FAILURE)
3138 *real_error = true;
3139
3140 return result;
3141 }
3142
66e4ab31 3143 /* Don't use gfc_free_actual_arglist(). */
6de9cd9a 3144 if (actual->next != NULL)
cede9502
JM
3145 free (actual->next);
3146 free (actual);
6de9cd9a
DN
3147
3148 return FAILURE;
3149 }
3150
3151 /* Change the expression node to a function call. */
3152 e->expr_type = EXPR_FUNCTION;
f6fad28e 3153 e->symtree = gfc_find_sym_in_symtree (sym);
6de9cd9a 3154 e->value.function.actual = actual;
58b03ab2
TS
3155 e->value.function.esym = NULL;
3156 e->value.function.isym = NULL;
cf013e9f 3157 e->value.function.name = NULL;
a1ab6660 3158 e->user_operator = 1;
6de9cd9a 3159
4a44a72d 3160 if (gfc_resolve_expr (e) == FAILURE)
6de9cd9a 3161 {
4a44a72d 3162 *real_error = true;
6de9cd9a
DN
3163 return FAILURE;
3164 }
3165
6de9cd9a
DN
3166 return SUCCESS;
3167}
3168
3169
3170/* Tries to replace an assignment code node with a subroutine call to
3171 the subroutine associated with the assignment operator. Return
3172 SUCCESS if the node was replaced. On FAILURE, no error is
3173 generated. */
3174
17b1d2a0 3175gfc_try
b251af97 3176gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
6de9cd9a
DN
3177{
3178 gfc_actual_arglist *actual;
3179 gfc_expr *lhs, *rhs;
3180 gfc_symbol *sym;
974df0f8
PT
3181 const char *gname;
3182
3183 gname = NULL;
6de9cd9a 3184
a513927a 3185 lhs = c->expr1;
6de9cd9a
DN
3186 rhs = c->expr2;
3187
3188 /* Don't allow an intrinsic assignment to be replaced. */
4b7dd692 3189 if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
e19bb186 3190 && (rhs->rank == 0 || rhs->rank == lhs->rank)
6de9cd9a 3191 && (lhs->ts.type == rhs->ts.type
b251af97 3192 || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
6de9cd9a
DN
3193 return FAILURE;
3194
3195 actual = gfc_get_actual_arglist ();
3196 actual->expr = lhs;
3197
3198 actual->next = gfc_get_actual_arglist ();
3199 actual->next->expr = rhs;
3200
3201 sym = NULL;
3202
3203 for (; ns; ns = ns->parent)
3204 {
a1ee985f 3205 sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
6de9cd9a
DN
3206 if (sym != NULL)
3207 break;
3208 }
3209
4a44a72d
DK
3210 /* TODO: Ambiguity-check, see above for gfc_extend_expr. */
3211
6de9cd9a
DN
3212 if (sym == NULL)
3213 {
4a44a72d
DK
3214 gfc_typebound_proc* tbo;
3215 gfc_expr* tb_base;
3216
3217 /* See if we find a matching type-bound assignment. */
3218 tbo = matching_typebound_op (&tb_base, actual,
974df0f8 3219 INTRINSIC_ASSIGN, NULL, &gname);
4a44a72d
DK
3220
3221 /* If there is one, replace the expression with a call to it and
3222 succeed. */
3223 if (tbo)
3224 {
3225 gcc_assert (tb_base);
3226 c->expr1 = gfc_get_expr ();
974df0f8 3227 build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
4a44a72d
DK
3228 c->expr1->value.compcall.assign = 1;
3229 c->expr2 = NULL;
3230 c->op = EXEC_COMPCALL;
3231
3232 /* c is resolved from the caller, so no need to do it here. */
3233
3234 return SUCCESS;
3235 }
3236
cede9502
JM
3237 free (actual->next);
3238 free (actual);
6de9cd9a
DN
3239 return FAILURE;
3240 }
3241
3242 /* Replace the assignment with the call. */
476220e7 3243 c->op = EXEC_ASSIGN_CALL;
f6fad28e 3244 c->symtree = gfc_find_sym_in_symtree (sym);
a513927a 3245 c->expr1 = NULL;
6de9cd9a
DN
3246 c->expr2 = NULL;
3247 c->ext.actual = actual;
3248
6de9cd9a
DN
3249 return SUCCESS;
3250}
3251
3252
3253/* Make sure that the interface just parsed is not already present in
3254 the given interface list. Ambiguity isn't checked yet since module
3255 procedures can be present without interfaces. */
3256
17b1d2a0 3257static gfc_try
7b901ac4 3258check_new_interface (gfc_interface *base, gfc_symbol *new_sym)
6de9cd9a
DN
3259{
3260 gfc_interface *ip;
3261
3262 for (ip = base; ip; ip = ip->next)
3263 {
7b901ac4 3264 if (ip->sym == new_sym)
6de9cd9a
DN
3265 {
3266 gfc_error ("Entity '%s' at %C is already present in the interface",
7b901ac4 3267 new_sym->name);
6de9cd9a
DN
3268 return FAILURE;
3269 }
3270 }
3271
3272 return SUCCESS;
3273}
3274
3275
3276/* Add a symbol to the current interface. */
3277
17b1d2a0 3278gfc_try
7b901ac4 3279gfc_add_interface (gfc_symbol *new_sym)
6de9cd9a
DN
3280{
3281 gfc_interface **head, *intr;
3282 gfc_namespace *ns;
3283 gfc_symbol *sym;
3284
3285 switch (current_interface.type)
3286 {
3287 case INTERFACE_NAMELESS:
9e1d712c 3288 case INTERFACE_ABSTRACT:
6de9cd9a
DN
3289 return SUCCESS;
3290
3291 case INTERFACE_INTRINSIC_OP:
3292 for (ns = current_interface.ns; ns; ns = ns->parent)
3bed9dd0
DF
3293 switch (current_interface.op)
3294 {
3295 case INTRINSIC_EQ:
3296 case INTRINSIC_EQ_OS:
7b901ac4
KG
3297 if (check_new_interface (ns->op[INTRINSIC_EQ], new_sym) == FAILURE ||
3298 check_new_interface (ns->op[INTRINSIC_EQ_OS], new_sym) == FAILURE)
3bed9dd0
DF
3299 return FAILURE;
3300 break;
3301
3302 case INTRINSIC_NE:
3303 case INTRINSIC_NE_OS:
7b901ac4
KG
3304 if (check_new_interface (ns->op[INTRINSIC_NE], new_sym) == FAILURE ||
3305 check_new_interface (ns->op[INTRINSIC_NE_OS], new_sym) == FAILURE)
3bed9dd0
DF
3306 return FAILURE;
3307 break;
3308
3309 case INTRINSIC_GT:
3310 case INTRINSIC_GT_OS:
7b901ac4
KG
3311 if (check_new_interface (ns->op[INTRINSIC_GT], new_sym) == FAILURE ||
3312 check_new_interface (ns->op[INTRINSIC_GT_OS], new_sym) == FAILURE)
3bed9dd0
DF
3313 return FAILURE;
3314 break;
3315
3316 case INTRINSIC_GE:
3317 case INTRINSIC_GE_OS:
7b901ac4
KG
3318 if (check_new_interface (ns->op[INTRINSIC_GE], new_sym) == FAILURE ||
3319 check_new_interface (ns->op[INTRINSIC_GE_OS], new_sym) == FAILURE)
3bed9dd0
DF
3320 return FAILURE;
3321 break;
3322
3323 case INTRINSIC_LT:
3324 case INTRINSIC_LT_OS:
7b901ac4
KG
3325 if (check_new_interface (ns->op[INTRINSIC_LT], new_sym) == FAILURE ||
3326 check_new_interface (ns->op[INTRINSIC_LT_OS], new_sym) == FAILURE)
3bed9dd0
DF
3327 return FAILURE;
3328 break;
3329
3330 case INTRINSIC_LE:
3331 case INTRINSIC_LE_OS:
7b901ac4
KG
3332 if (check_new_interface (ns->op[INTRINSIC_LE], new_sym) == FAILURE ||
3333 check_new_interface (ns->op[INTRINSIC_LE_OS], new_sym) == FAILURE)
3bed9dd0
DF
3334 return FAILURE;
3335 break;
3336
3337 default:
7b901ac4 3338 if (check_new_interface (ns->op[current_interface.op], new_sym) == FAILURE)
3bed9dd0
DF
3339 return FAILURE;
3340 }
6de9cd9a 3341
a1ee985f 3342 head = &current_interface.ns->op[current_interface.op];
6de9cd9a
DN
3343 break;
3344
3345 case INTERFACE_GENERIC:
3346 for (ns = current_interface.ns; ns; ns = ns->parent)
3347 {
3348 gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
3349 if (sym == NULL)
3350 continue;
3351
7b901ac4 3352 if (check_new_interface (sym->generic, new_sym) == FAILURE)
6de9cd9a
DN
3353 return FAILURE;
3354 }
3355
3356 head = &current_interface.sym->generic;
3357 break;
3358
3359 case INTERFACE_USER_OP:
7b901ac4 3360 if (check_new_interface (current_interface.uop->op, new_sym)
b251af97 3361 == FAILURE)
6de9cd9a
DN
3362 return FAILURE;
3363
a1ee985f 3364 head = &current_interface.uop->op;
6de9cd9a
DN
3365 break;
3366
3367 default:
3368 gfc_internal_error ("gfc_add_interface(): Bad interface type");
3369 }
3370
3371 intr = gfc_get_interface ();
7b901ac4 3372 intr->sym = new_sym;
63645982 3373 intr->where = gfc_current_locus;
6de9cd9a
DN
3374
3375 intr->next = *head;
3376 *head = intr;
3377
3378 return SUCCESS;
3379}
3380
3381
2b77e908
FXC
3382gfc_interface *
3383gfc_current_interface_head (void)
3384{
3385 switch (current_interface.type)
3386 {
3387 case INTERFACE_INTRINSIC_OP:
a1ee985f 3388 return current_interface.ns->op[current_interface.op];
2b77e908
FXC
3389 break;
3390
3391 case INTERFACE_GENERIC:
3392 return current_interface.sym->generic;
3393 break;
3394
3395 case INTERFACE_USER_OP:
a1ee985f 3396 return current_interface.uop->op;
2b77e908
FXC
3397 break;
3398
3399 default:
3400 gcc_unreachable ();
3401 }
3402}
3403
3404
3405void
3406gfc_set_current_interface_head (gfc_interface *i)
3407{
3408 switch (current_interface.type)
3409 {
3410 case INTERFACE_INTRINSIC_OP:
a1ee985f 3411 current_interface.ns->op[current_interface.op] = i;
2b77e908
FXC
3412 break;
3413
3414 case INTERFACE_GENERIC:
3415 current_interface.sym->generic = i;
3416 break;
3417
3418 case INTERFACE_USER_OP:
a1ee985f 3419 current_interface.uop->op = i;
2b77e908
FXC
3420 break;
3421
3422 default:
3423 gcc_unreachable ();
3424 }
3425}
3426
3427
6de9cd9a
DN
3428/* Gets rid of a formal argument list. We do not free symbols.
3429 Symbols are freed when a namespace is freed. */
3430
3431void
b251af97 3432gfc_free_formal_arglist (gfc_formal_arglist *p)
6de9cd9a
DN
3433{
3434 gfc_formal_arglist *q;
3435
3436 for (; p; p = q)
3437 {
3438 q = p->next;
cede9502 3439 free (p);
6de9cd9a
DN
3440 }
3441}
This page took 2.593025 seconds and 5 git commands to generate.