interface.c 117 KB
Newer Older
1
/* Deal with interfaces.
Jakub Jelinek committed
2
   Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 4
   Contributed by Andy Vaught

5
This file is part of GCC.
6

7 8
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 3, or (at your option) any later
10
version.
11

12 13 14 15
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
16 17

You should have received a copy of the GNU General Public License
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42


/* Deal with interfaces.  An explicit interface is represented as a
   singly linked list of formal argument structures attached to the
   relevant symbols.  For an implicit interface, the arguments don't
   point to symbols.  Explicit interfaces point to namespaces that
   contain the symbols within that interface.

   Implicit interfaces are linked together in a singly linked list
   along the next_if member of symbol nodes.  Since a particular
   symbol can only have a single explicit interface, the symbol cannot
   be part of multiple lists and a single next-member suffices.

   This is not the case for general classes, though.  An operator
   definition is independent of just about all other uses and has it's
   own head pointer.

   Nameless interfaces:
     Nameless interfaces create symbols with explicit interfaces within
     the current namespace.  They are otherwise unlinked.

   Generic interfaces:
     The generic name points to a linked list of symbols.  Each symbol
43
     has an explicit interface.  Each explicit interface has its own
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
     namespace containing the arguments.  Module procedures are symbols in
     which the interface is added later when the module procedure is parsed.

   User operators:
     User-defined operators are stored in a their own set of symtrees
     separate from regular symbols.  The symtrees point to gfc_user_op
     structures which in turn head up a list of relevant interfaces.

   Extended intrinsics and assignment:
     The head of these interface lists are stored in the containing namespace.

   Implicit interfaces:
     An implicit interface is represented as a singly linked list of
     formal argument list structures that don't point to any symbol
     nodes -- they just contain types.


   When a subprogram is defined, the program unit's name points to an
   interface as usual, but the link to the namespace is NULL and the
   formal argument list points to symbols within the same namespace as
   the program unit name.  */

#include "config.h"
67
#include "system.h"
68
#include "coretypes.h"
69
#include "flags.h"
70 71
#include "gfortran.h"
#include "match.h"
72
#include "arith.h"
73 74 75 76 77 78 79 80 81 82 83

/* The current_interface structure holds information about the
   interface currently being parsed.  This structure is saved and
   restored during recursive interfaces.  */

gfc_interface_info current_interface;


/* Free a singly linked list of gfc_interface structures.  */

void
84
gfc_free_interface (gfc_interface *intr)
85 86 87 88 89 90
{
  gfc_interface *next;

  for (; intr; intr = next)
    {
      next = intr->next;
91
      free (intr);
92 93 94 95 96 97 98 99
    }
}


/* Change the operators unary plus and minus into binary plus and
   minus respectively, leaving the rest unchanged.  */

static gfc_intrinsic_op
100
fold_unary_intrinsic (gfc_intrinsic_op op)
101
{
102
  switch (op)
103 104
    {
    case INTRINSIC_UPLUS:
105
      op = INTRINSIC_PLUS;
106 107
      break;
    case INTRINSIC_UMINUS:
108
      op = INTRINSIC_MINUS;
109 110 111 112 113
      break;
    default:
      break;
    }

114
  return op;
115 116 117 118
}


/* Match a generic specification.  Depending on which type of
119
   interface is found, the 'name' or 'op' pointers may be set.
120 121 122
   This subroutine doesn't return MATCH_NO.  */

match
123
gfc_match_generic_spec (interface_type *type,
124
			char *name,
125
			gfc_intrinsic_op *op)
126 127 128 129 130 131 132 133
{
  char buffer[GFC_MAX_SYMBOL_LEN + 1];
  match m;
  gfc_intrinsic_op i;

  if (gfc_match (" assignment ( = )") == MATCH_YES)
    {
      *type = INTERFACE_INTRINSIC_OP;
134
      *op = INTRINSIC_ASSIGN;
135 136 137 138 139 140
      return MATCH_YES;
    }

  if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
    {				/* Operator i/f */
      *type = INTERFACE_INTRINSIC_OP;
141
      *op = fold_unary_intrinsic (i);
142 143 144
      return MATCH_YES;
    }

145
  *op = INTRINSIC_NONE;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  if (gfc_match (" operator ( ") == MATCH_YES)
    {
      m = gfc_match_defined_op_name (buffer, 1);
      if (m == MATCH_NO)
	goto syntax;
      if (m != MATCH_YES)
	return MATCH_ERROR;

      m = gfc_match_char (')');
      if (m == MATCH_NO)
	goto syntax;
      if (m != MATCH_YES)
	return MATCH_ERROR;

      strcpy (name, buffer);
      *type = INTERFACE_USER_OP;
      return MATCH_YES;
    }

  if (gfc_match_name (buffer) == MATCH_YES)
    {
      strcpy (name, buffer);
      *type = INTERFACE_GENERIC;
      return MATCH_YES;
    }

  *type = INTERFACE_NAMELESS;
  return MATCH_YES;

syntax:
  gfc_error ("Syntax error in generic specification at %C");
  return MATCH_ERROR;
}


Tobias Burnus committed
181 182
/* Match one of the five F95 forms of an interface statement.  The
   matcher for the abstract interface follows.  */
183 184 185 186 187 188 189

match
gfc_match_interface (void)
{
  char name[GFC_MAX_SYMBOL_LEN + 1];
  interface_type type;
  gfc_symbol *sym;
190
  gfc_intrinsic_op op;
191 192 193 194
  match m;

  m = gfc_match_space ();

195
  if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
196 197 198 199 200
    return MATCH_ERROR;

  /* If we're not looking at the end of the statement now, or if this
     is not a nameless interface but we did not see a space, punt.  */
  if (gfc_match_eos () != MATCH_YES
201
      || (type != INTERFACE_NAMELESS && m != MATCH_YES))
202
    {
203 204
      gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
		 "at %C");
205 206 207 208 209 210 211 212 213 214 215
      return MATCH_ERROR;
    }

  current_interface.type = type;

  switch (type)
    {
    case INTERFACE_GENERIC:
      if (gfc_get_symbol (name, NULL, &sym))
	return MATCH_ERROR;

216
      if (!sym->attr.generic
217
	  && !gfc_add_generic (&sym->attr, sym->name, NULL))
218 219
	return MATCH_ERROR;

220 221
      if (sym->attr.dummy)
	{
222
	  gfc_error ("Dummy procedure %qs at %C cannot have a "
223 224 225 226
		     "generic interface", sym->name);
	  return MATCH_ERROR;
	}

227 228 229 230 231 232 233 234
      current_interface.sym = gfc_new_block = sym;
      break;

    case INTERFACE_USER_OP:
      current_interface.uop = gfc_get_uop (name);
      break;

    case INTERFACE_INTRINSIC_OP:
235
      current_interface.op = op;
236 237 238
      break;

    case INTERFACE_NAMELESS:
Tobias Burnus committed
239
    case INTERFACE_ABSTRACT:
240 241 242 243 244 245 246
      break;
    }

  return MATCH_YES;
}


Tobias Burnus committed
247 248 249 250 251 252 253 254

/* Match a F2003 abstract interface.  */

match
gfc_match_abstract_interface (void)
{
  match m;

255
  if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT INTERFACE at %C"))
Tobias Burnus committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    return MATCH_ERROR;

  m = gfc_match_eos ();

  if (m != MATCH_YES)
    {
      gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
      return MATCH_ERROR;
    }

  current_interface.type = INTERFACE_ABSTRACT;

  return m;
}


272 273 274 275 276 277 278 279
/* Match the different sort of generic-specs that can be present after
   the END INTERFACE itself.  */

match
gfc_match_end_interface (void)
{
  char name[GFC_MAX_SYMBOL_LEN + 1];
  interface_type type;
280
  gfc_intrinsic_op op;
281 282 283 284
  match m;

  m = gfc_match_space ();

285
  if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
286 287 288 289 290
    return MATCH_ERROR;

  /* If we're not looking at the end of the statement now, or if this
     is not a nameless interface but we did not see a space, punt.  */
  if (gfc_match_eos () != MATCH_YES
291
      || (type != INTERFACE_NAMELESS && m != MATCH_YES))
292
    {
293 294
      gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
		 "statement at %C");
295 296 297 298 299 300 301 302
      return MATCH_ERROR;
    }

  m = MATCH_YES;

  switch (current_interface.type)
    {
    case INTERFACE_NAMELESS:
Tobias Burnus committed
303 304
    case INTERFACE_ABSTRACT:
      if (type != INTERFACE_NAMELESS)
305 306 307 308 309 310 311 312
	{
	  gfc_error ("Expected a nameless interface at %C");
	  m = MATCH_ERROR;
	}

      break;

    case INTERFACE_INTRINSIC_OP:
313
      if (type != current_interface.type || op != current_interface.op)
314 315 316
	{

	  if (current_interface.op == INTRINSIC_ASSIGN)
317 318
	    {
	      m = MATCH_ERROR;
319
	      gfc_error ("Expected %<END INTERFACE ASSIGNMENT (=)%> at %C");
320
	    }
321
	  else
322
	    {
323
	      const char *s1, *s2;
324 325 326 327 328
	      s1 = gfc_op2string (current_interface.op);
	      s2 = gfc_op2string (op);

	      /* The following if-statements are used to enforce C1202
		 from F2003.  */
329 330
	      if ((strcmp(s1, "==") == 0 && strcmp (s2, ".eq.") == 0)
		  || (strcmp(s1, ".eq.") == 0 && strcmp (s2, "==") == 0))
331
		break;
332 333
	      if ((strcmp(s1, "/=") == 0 && strcmp (s2, ".ne.") == 0)
		  || (strcmp(s1, ".ne.") == 0 && strcmp (s2, "/=") == 0))
334
		break;
335 336
	      if ((strcmp(s1, "<=") == 0 && strcmp (s2, ".le.") == 0)
		  || (strcmp(s1, ".le.") == 0 && strcmp (s2, "<=") == 0))
337
		break;
338 339
	      if ((strcmp(s1, "<") == 0 && strcmp (s2, ".lt.") == 0)
		  || (strcmp(s1, ".lt.") == 0 && strcmp (s2, "<") == 0))
340
		break;
341 342
	      if ((strcmp(s1, ">=") == 0 && strcmp (s2, ".ge.") == 0)
		  || (strcmp(s1, ".ge.") == 0 && strcmp (s2, ">=") == 0))
343
		break;
344 345
	      if ((strcmp(s1, ">") == 0 && strcmp (s2, ".gt.") == 0)
		  || (strcmp(s1, ".gt.") == 0 && strcmp (s2, ">") == 0))
346 347 348
		break;

	      m = MATCH_ERROR;
349
	      gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> at %C, "
350 351
			 "but got %s", s1, s2);
	    }
352

353 354 355 356 357 358
	}

      break;

    case INTERFACE_USER_OP:
      /* Comparing the symbol node names is OK because only use-associated
359
	 symbols can be renamed.  */
360
      if (type != current_interface.type
361
	  || strcmp (current_interface.uop->name, name) != 0)
362
	{
363
	  gfc_error ("Expecting %<END INTERFACE OPERATOR (.%s.)%> at %C",
364
		     current_interface.uop->name);
365 366 367 368 369 370 371 372 373
	  m = MATCH_ERROR;
	}

      break;

    case INTERFACE_GENERIC:
      if (type != current_interface.type
	  || strcmp (current_interface.sym->name, name) != 0)
	{
374
	  gfc_error ("Expecting %<END INTERFACE %s%> at %C",
375 376 377 378 379 380 381 382 383 384 385
		     current_interface.sym->name);
	  m = MATCH_ERROR;
	}

      break;
    }

  return m;
}


386 387
/* Compare two derived types using the criteria in 4.4.2 of the standard,
   recursing through gfc_compare_types for the components.  */
388 389

int
390
gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
391 392 393
{
  gfc_component *dt1, *dt2;

394 395 396
  if (derived1 == derived2)
    return 1;

397 398
  gcc_assert (derived1 && derived2);

399 400 401
  /* Special case for comparing derived types across namespaces.  If the
     true names and module names are the same and the module name is
     nonnull, then they are equal.  */
402
  if (strcmp (derived1->name, derived2->name) == 0
403 404
      && derived1->module != NULL && derived2->module != NULL
      && strcmp (derived1->module, derived2->module) == 0)
405 406 407
    return 1;

  /* Compare type via the rules of the standard.  Both types must have
408
     the SEQUENCE or BIND(C) attribute to be equal.  */
409

410
  if (strcmp (derived1->name, derived2->name))
411 412
    return 0;

413
  if (derived1->component_access == ACCESS_PRIVATE
414
      || derived2->component_access == ACCESS_PRIVATE)
415
    return 0;
416

417 418
  if (!(derived1->attr.sequence && derived2->attr.sequence)
      && !(derived1->attr.is_bind_c && derived2->attr.is_bind_c))
419 420
    return 0;

421 422 423
  dt1 = derived1->components;
  dt2 = derived2->components;

424 425 426 427 428 429 430 431
  /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
     simple test can speed things up.  Otherwise, lots of things have to
     match.  */
  for (;;)
    {
      if (strcmp (dt1->name, dt2->name) != 0)
	return 0;

432
      if (dt1->attr.access != dt2->attr.access)
433 434
	return 0;

435
      if (dt1->attr.pointer != dt2->attr.pointer)
436 437
	return 0;

438
      if (dt1->attr.dimension != dt2->attr.dimension)
439 440
	return 0;

441
     if (dt1->attr.allocatable != dt2->attr.allocatable)
Paul Thomas committed
442 443
	return 0;

444
      if (dt1->attr.dimension && gfc_compare_array_spec (dt1->as, dt2->as) == 0)
445 446
	return 0;

447
      /* Make sure that link lists do not put this function into an
448
	 endless recursive loop!  */
449
      if (!(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
450
	    && !(dt2->ts.type == BT_DERIVED && derived2 == dt2->ts.u.derived)
451 452 453
	    && gfc_compare_types (&dt1->ts, &dt2->ts) == 0)
	return 0;

454 455
      else if ((dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
		&& !(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived))
456 457
	return 0;

458 459
      else if (!(dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived)
		&& (dt1->ts.type == BT_DERIVED && derived1 == dt1->ts.u.derived))
460 461 462 463 464 465 466 467 468 469 470 471 472 473
	return 0;

      dt1 = dt1->next;
      dt2 = dt2->next;

      if (dt1 == NULL && dt2 == NULL)
	break;
      if (dt1 == NULL || dt2 == NULL)
	return 0;
    }

  return 1;
}

474

475 476 477
/* Compare two typespecs, recursively if necessary.  */

int
478
gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
479
{
480 481 482 483 484 485
  /* See if one of the typespecs is a BT_VOID, which is what is being used
     to allow the funcs like c_f_pointer to accept any pointer type.
     TODO: Possibly should narrow this to just the one typespec coming in
     that is for the formal arg, but oh well.  */
  if (ts1->type == BT_VOID || ts2->type == BT_VOID)
    return 1;
486

487 488 489 490 491 492 493 494 495
  /* The _data component is not always present, therefore check for its
     presence before assuming, that its derived->attr is available.
     When the _data component is not present, then nevertheless the
     unlimited_polymorphic flag may be set in the derived type's attr.  */
  if (ts1->type == BT_CLASS && ts1->u.derived->components
      && ((ts1->u.derived->attr.is_class
	   && ts1->u.derived->components->ts.u.derived->attr
						  .unlimited_polymorphic)
	  || ts1->u.derived->attr.unlimited_polymorphic))
496 497 498 499
    return 1;

  /* F2003: C717  */
  if (ts2->type == BT_CLASS && ts1->type == BT_DERIVED
500 501 502 503 504
      && ts2->u.derived->components
      && ((ts2->u.derived->attr.is_class
	   && ts2->u.derived->components->ts.u.derived->attr
						  .unlimited_polymorphic)
	  || ts2->u.derived->attr.unlimited_polymorphic)
505 506 507
      && (ts1->u.derived->attr.sequence || ts1->u.derived->attr.is_bind_c))
    return 1;

508 509 510
  if (ts1->type != ts2->type
      && ((ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
	  || (ts2->type != BT_DERIVED && ts2->type != BT_CLASS)))
511
    return 0;
512
  if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
513 514 515
    return (ts1->kind == ts2->kind);

  /* Compare derived types.  */
516
  if (gfc_type_compatible (ts1, ts2))
517 518
    return 1;

519
  return gfc_compare_derived_types (ts1->u.derived ,ts2->u.derived);
520 521
}

522

523 524 525 526 527 528
static int
compare_type (gfc_symbol *s1, gfc_symbol *s2)
{
  if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    return 1;

529 530 531 532 533 534
  /* TYPE and CLASS of the same declared type are type compatible,
     but have different characteristics.  */
  if ((s1->ts.type == BT_CLASS && s2->ts.type == BT_DERIVED)
      || (s1->ts.type == BT_DERIVED && s2->ts.type == BT_CLASS))
    return 0;

535 536 537
  return gfc_compare_types (&s1->ts, &s2->ts) || s2->ts.type == BT_ASSUMED;
}

538 539

static int
540
compare_rank (gfc_symbol *s1, gfc_symbol *s2)
541
{
542
  gfc_array_spec *as1, *as2;
543 544
  int r1, r2;

545
  if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
546 547
    return 1;

548 549 550 551 552
  as1 = (s1->ts.type == BT_CLASS) ? CLASS_DATA (s1)->as : s1->as;
  as2 = (s2->ts.type == BT_CLASS) ? CLASS_DATA (s2)->as : s2->as;

  r1 = as1 ? as1->rank : 0;
  r2 = as2 ? as2->rank : 0;
553

554
  if (r1 != r2 && (!as2 || as2->type != AS_ASSUMED_RANK))
555
    return 0;			/* Ranks differ.  */
556

557 558 559 560 561 562 563 564 565 566 567 568
  return 1;
}


/* Given two symbols that are formal arguments, compare their ranks
   and types.  Returns nonzero if they have the same rank and type,
   zero otherwise.  */

static int
compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
{
  return compare_type (s1, s2) && compare_rank (s1, s2);
569 570 571 572 573 574 575 576
}


/* Given two symbols that are formal arguments, compare their types
   and rank and their formal interfaces if they are both dummy
   procedures.  Returns nonzero if the same, zero if different.  */

static int
577
compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
578
{
579 580
  if (s1 == NULL || s2 == NULL)
    return s1 == s2 ? 1 : 0;
581

582 583 584
  if (s1 == s2)
    return 1;

585 586 587 588 589 590
  if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
    return compare_type_rank (s1, s2);

  if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
    return 0;

591 592 593 594 595 596 597 598 599
  /* At this point, both symbols are procedures.  It can happen that
     external procedures are compared, where one is identified by usage
     to be a function or subroutine but the other is not.  Check TKR
     nonetheless for these cases.  */
  if (s1->attr.function == 0 && s1->attr.subroutine == 0)
    return s1->attr.external == 1 ? compare_type_rank (s1, s2) : 0;

  if (s2->attr.function == 0 && s2->attr.subroutine == 0)
    return s2->attr.external == 1 ? compare_type_rank (s1, s2) : 0;
600

601
  /* Now the type of procedure has been identified.  */
602 603 604 605 606 607 608
  if (s1->attr.function != s2->attr.function
      || s1->attr.subroutine != s2->attr.subroutine)
    return 0;

  if (s1->attr.function && compare_type_rank (s1, s2) == 0)
    return 0;

609 610 611
  /* Originally, gfortran recursed here to check the interfaces of passed
     procedures.  This is explicitly not required by the standard.  */
  return 1;
612 613 614 615 616 617 618 619
}


/* Given a formal argument list and a keyword name, search the list
   for that keyword.  Returns the correct symbol node if found, NULL
   if not found.  */

static gfc_symbol *
620
find_keyword_arg (const char *name, gfc_formal_arglist *f)
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
  for (; f; f = f->next)
    if (strcmp (f->sym->name, name) == 0)
      return f->sym;

  return NULL;
}


/******** Interface checking subroutines **********/


/* Given an operator interface and the operator, make sure that all
   interfaces for that operator are legal.  */

636 637 638
bool
gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
			      locus opwhere)
639 640 641 642
{
  gfc_formal_arglist *formal;
  sym_intent i1, i2;
  bt t1, t2;
643
  int args, r1, r2, k1, k2;
644

645
  gcc_assert (sym);
646 647 648 649

  args = 0;
  t1 = t2 = BT_UNKNOWN;
  i1 = i2 = INTENT_UNKNOWN;
650 651
  r1 = r2 = -1;
  k1 = k2 = -1;
652

653
  for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
654
    {
655 656
      gfc_symbol *fsym = formal->sym;
      if (fsym == NULL)
657 658
	{
	  gfc_error ("Alternate return cannot appear in operator "
659 660
		     "interface at %L", &sym->declared_at);
	  return false;
661
	}
662 663
      if (args == 0)
	{
664 665 666 667
	  t1 = fsym->ts.type;
	  i1 = fsym->attr.intent;
	  r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
	  k1 = fsym->ts.kind;
668 669 670
	}
      if (args == 1)
	{
671 672 673 674
	  t2 = fsym->ts.type;
	  i2 = fsym->attr.intent;
	  r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
	  k2 = fsym->ts.kind;
675 676 677 678
	}
      args++;
    }

679 680
  /* Only +, - and .not. can be unary operators.
     .not. cannot be a binary operator.  */
681 682 683 684
  if (args == 0 || args > 2 || (args == 1 && op != INTRINSIC_PLUS
				&& op != INTRINSIC_MINUS
				&& op != INTRINSIC_NOT)
      || (args == 2 && op == INTRINSIC_NOT))
685
    {
686 687 688 689 690 691
      if (op == INTRINSIC_ASSIGN)
	gfc_error ("Assignment operator interface at %L must have "
		   "two arguments", &sym->declared_at);
      else
	gfc_error ("Operator interface at %L has the wrong number of arguments",
		   &sym->declared_at);
692
      return false;
693 694 695 696
    }

  /* Check that intrinsics are mapped to functions, except
     INTRINSIC_ASSIGN which should map to a subroutine.  */
697
  if (op == INTRINSIC_ASSIGN)
698
    {
699 700
      gfc_formal_arglist *dummy_args;

701 702
      if (!sym->attr.subroutine)
	{
703
	  gfc_error ("Assignment operator interface at %L must be "
704 705
		     "a SUBROUTINE", &sym->declared_at);
	  return false;
706
	}
707 708

      /* Allowed are (per F2003, 12.3.2.1.2 Defined assignments):
709
	 - First argument an array with different rank than second,
710 711
	 - First argument is a scalar and second an array,
	 - Types and kinds do not conform, or
712
	 - First argument is of derived type.  */
713 714 715
      dummy_args = gfc_sym_get_dummy_args (sym);
      if (dummy_args->sym->ts.type != BT_DERIVED
	  && dummy_args->sym->ts.type != BT_CLASS
716
	  && (r2 == 0 || r1 == r2)
717 718 719
	  && (dummy_args->sym->ts.type == dummy_args->next->sym->ts.type
	      || (gfc_numeric_ts (&dummy_args->sym->ts)
		  && gfc_numeric_ts (&dummy_args->next->sym->ts))))
720
	{
721
	  gfc_error ("Assignment operator interface at %L must not redefine "
722 723
		     "an INTRINSIC type assignment", &sym->declared_at);
	  return false;
724
	}
725 726 727 728 729 730
    }
  else
    {
      if (!sym->attr.function)
	{
	  gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
731 732
		     &sym->declared_at);
	  return false;
733 734 735
	}
    }

736
  /* Check intents on operator interfaces.  */
737
  if (op == INTRINSIC_ASSIGN)
738
    {
739
      if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
740 741 742 743 744
	{
	  gfc_error ("First argument of defined assignment at %L must be "
		     "INTENT(OUT) or INTENT(INOUT)", &sym->declared_at);
	  return false;
	}
745 746

      if (i2 != INTENT_IN)
747 748 749 750 751
	{
	  gfc_error ("Second argument of defined assignment at %L must be "
		     "INTENT(IN)", &sym->declared_at);
	  return false;
	}
752 753 754 755
    }
  else
    {
      if (i1 != INTENT_IN)
756 757 758 759 760
	{
	  gfc_error ("First argument of operator interface at %L must be "
		     "INTENT(IN)", &sym->declared_at);
	  return false;
	}
761 762

      if (args == 2 && i2 != INTENT_IN)
763 764 765 766 767
	{
	  gfc_error ("Second argument of operator interface at %L must be "
		     "INTENT(IN)", &sym->declared_at);
	  return false;
	}
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    }

  /* From now on, all we have to do is check that the operator definition
     doesn't conflict with an intrinsic operator. The rules for this
     game are defined in 7.1.2 and 7.1.3 of both F95 and F2003 standards,
     as well as 12.3.2.1.1 of Fortran 2003:

     "If the operator is an intrinsic-operator (R310), the number of
     function arguments shall be consistent with the intrinsic uses of
     that operator, and the types, kind type parameters, or ranks of the
     dummy arguments shall differ from those required for the intrinsic
     operation (7.1.2)."  */

#define IS_NUMERIC_TYPE(t) \
  ((t) == BT_INTEGER || (t) == BT_REAL || (t) == BT_COMPLEX)

  /* Unary ops are easy, do them first.  */
785
  if (op == INTRINSIC_NOT)
786 787
    {
      if (t1 == BT_LOGICAL)
788
	goto bad_repl;
789
      else
790
	return true;
791
    }
792

793
  if (args == 1 && (op == INTRINSIC_PLUS || op == INTRINSIC_MINUS))
794 795
    {
      if (IS_NUMERIC_TYPE (t1))
796
	goto bad_repl;
797
      else
798
	return true;
799
    }
800

801 802 803 804
  /* Character intrinsic operators have same character kind, thus
     operator definitions with operands of different character kinds
     are always safe.  */
  if (t1 == BT_CHARACTER && t2 == BT_CHARACTER && k1 != k2)
805
    return true;
806

807 808 809 810
  /* Intrinsic operators always perform on arguments of same rank,
     so different ranks is also always safe.  (rank == 0) is an exception
     to that, because all intrinsic operators are elemental.  */
  if (r1 != r2 && r1 != 0 && r2 != 0)
811
    return true;
812

813
  switch (op)
814
  {
815
    case INTRINSIC_EQ:
816
    case INTRINSIC_EQ_OS:
817
    case INTRINSIC_NE:
818
    case INTRINSIC_NE_OS:
819
      if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
820
	goto bad_repl;
821
      /* Fall through.  */
822

823 824 825 826 827 828 829
    case INTRINSIC_PLUS:
    case INTRINSIC_MINUS:
    case INTRINSIC_TIMES:
    case INTRINSIC_DIVIDE:
    case INTRINSIC_POWER:
      if (IS_NUMERIC_TYPE (t1) && IS_NUMERIC_TYPE (t2))
	goto bad_repl;
830 831 832
      break;

    case INTRINSIC_GT:
833
    case INTRINSIC_GT_OS:
834
    case INTRINSIC_GE:
835
    case INTRINSIC_GE_OS:
836
    case INTRINSIC_LT:
837
    case INTRINSIC_LT_OS:
838
    case INTRINSIC_LE:
839
    case INTRINSIC_LE_OS:
840 841
      if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
	goto bad_repl;
842 843 844
      if ((t1 == BT_INTEGER || t1 == BT_REAL)
	  && (t2 == BT_INTEGER || t2 == BT_REAL))
	goto bad_repl;
845
      break;
846

847 848 849
    case INTRINSIC_CONCAT:
      if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
	goto bad_repl;
850 851 852
      break;

    case INTRINSIC_AND:
853
    case INTRINSIC_OR:
854 855 856 857 858 859 860
    case INTRINSIC_EQV:
    case INTRINSIC_NEQV:
      if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
	goto bad_repl;
      break;

    default:
861 862
      break;
  }
863

864
  return true;
865

866 867
#undef IS_NUMERIC_TYPE

868 869
bad_repl:
  gfc_error ("Operator interface at %L conflicts with intrinsic interface",
870 871
	     &opwhere);
  return false;
872 873 874 875 876 877 878 879
}


/* Given a pair of formal argument lists, we see if the two lists can
   be distinguished by counting the number of nonoptional arguments of
   a given type/rank in f1 and seeing if there are less then that
   number of those arguments in f2 (including optional arguments).
   Since this test is asymmetric, it has to be called twice to make it
880 881 882
   symmetric. Returns nonzero if the argument lists are incompatible
   by this test. This subroutine implements rule 1 of section F03:16.2.3.
   'p1' and 'p2' are the PASS arguments of both procedures (if applicable).  */
883 884

static int
885 886
count_types_test (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
		  const char *p1, const char *p2)
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
{
  int rc, ac1, ac2, i, j, k, n1;
  gfc_formal_arglist *f;

  typedef struct
  {
    int flag;
    gfc_symbol *sym;
  }
  arginfo;

  arginfo *arg;

  n1 = 0;

  for (f = f1; f; f = f->next)
    n1++;

  /* Build an array of integers that gives the same integer to
     arguments of the same type/rank.  */
907
  arg = XCNEWVEC (arginfo, n1);
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

  f = f1;
  for (i = 0; i < n1; i++, f = f->next)
    {
      arg[i].flag = -1;
      arg[i].sym = f->sym;
    }

  k = 0;

  for (i = 0; i < n1; i++)
    {
      if (arg[i].flag != -1)
	continue;

923 924 925
      if (arg[i].sym && (arg[i].sym->attr.optional
			 || (p1 && strcmp (arg[i].sym->name, p1) == 0)))
	continue;		/* Skip OPTIONAL and PASS arguments.  */
926 927 928

      arg[i].flag = k;

929
      /* Find other non-optional, non-pass arguments of the same type/rank.  */
930
      for (j = i + 1; j < n1; j++)
931 932 933
	if ((arg[j].sym == NULL
	     || !(arg[j].sym->attr.optional
		  || (p1 && strcmp (arg[j].sym->name, p1) == 0)))
934 935
	    && (compare_type_rank_if (arg[i].sym, arg[j].sym)
	        || compare_type_rank_if (arg[j].sym, arg[i].sym)))
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	  arg[j].flag = k;

      k++;
    }

  /* Now loop over each distinct type found in f1.  */
  k = 0;
  rc = 0;

  for (i = 0; i < n1; i++)
    {
      if (arg[i].flag != k)
	continue;

      ac1 = 1;
      for (j = i + 1; j < n1; j++)
	if (arg[j].flag == k)
	  ac1++;

955 956
      /* Count the number of non-pass arguments in f2 with that type,
	 including those that are optional.  */
957 958 959
      ac2 = 0;

      for (f = f2; f; f = f->next)
960 961 962
	if ((!p2 || strcmp (f->sym->name, p2) != 0)
	    && (compare_type_rank_if (arg[i].sym, f->sym)
		|| compare_type_rank_if (f->sym, arg[i].sym)))
963 964 965 966 967 968 969 970 971 972 973
	  ac2++;

      if (ac1 > ac2)
	{
	  rc = 1;
	  break;
	}

      k++;
    }

974
  free (arg);
975 976 977 978 979

  return rc;
}


980 981 982
/* Perform the correspondence test in rule (3) of F08:C1215.
   Returns zero if no argument is found that satisfies this rule,
   nonzero otherwise. 'p1' and 'p2' are the PASS arguments of both procedures
983
   (if applicable).
984 985 986 987 988 989

   This test is also not symmetric in f1 and f2 and must be called
   twice.  This test finds problems caused by sorting the actual
   argument list with keywords.  For example:

   INTERFACE FOO
990 991 992
     SUBROUTINE F1(A, B)
       INTEGER :: A ; REAL :: B
     END SUBROUTINE F1
993

994 995 996
     SUBROUTINE F2(B, A)
       INTEGER :: A ; REAL :: B
     END SUBROUTINE F1
997 998 999 1000 1001
   END INTERFACE FOO

   At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous.  */

static int
1002 1003
generic_correspondence (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
			const char *p1, const char *p2)
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
{
  gfc_formal_arglist *f2_save, *g;
  gfc_symbol *sym;

  f2_save = f2;

  while (f1)
    {
      if (f1->sym->attr.optional)
	goto next;

1015 1016 1017 1018 1019
      if (p1 && strcmp (f1->sym->name, p1) == 0)
	f1 = f1->next;
      if (f2 && p2 && strcmp (f2->sym->name, p2) == 0)
	f2 = f2->next;

1020
      if (f2 != NULL && (compare_type_rank (f1->sym, f2->sym)
1021 1022 1023 1024
			 || compare_type_rank (f2->sym, f1->sym))
	  && !((gfc_option.allow_std & GFC_STD_F2008)
	       && ((f1->sym->attr.allocatable && f2->sym->attr.pointer)
		   || (f2->sym->attr.allocatable && f1->sym->attr.pointer))))
1025 1026 1027
	goto next;

      /* Now search for a disambiguating keyword argument starting at
1028
	 the current non-match.  */
1029 1030
      for (g = f1; g; g = g->next)
	{
1031
	  if (g->sym->attr.optional || (p1 && strcmp (g->sym->name, p1) == 0))
1032 1033 1034
	    continue;

	  sym = find_keyword_arg (g->sym->name, f2_save);
1035 1036 1037 1038
	  if (sym == NULL || !compare_type_rank (g->sym, sym)
	      || ((gfc_option.allow_std & GFC_STD_F2008)
		  && ((sym->attr.allocatable && g->sym->attr.pointer)
		      || (sym->attr.pointer && g->sym->attr.allocatable))))
1039 1040 1041 1042
	    return 1;
	}

    next:
1043 1044
      if (f1 != NULL)
	f1 = f1->next;
1045 1046 1047 1048 1049 1050 1051 1052
      if (f2 != NULL)
	f2 = f2->next;
    }

  return 0;
}


1053 1054 1055 1056 1057 1058 1059 1060 1061
static int
symbol_rank (gfc_symbol *sym)
{
  gfc_array_spec *as;
  as = (sym->ts.type == BT_CLASS) ? CLASS_DATA (sym)->as : sym->as;
  return as ? as->rank : 0;
}


1062 1063 1064
/* Check if the characteristics of two dummy arguments match,
   cf. F08:12.3.2.  */

1065
static bool
1066 1067 1068
check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
			     bool type_must_agree, char *errmsg, int err_len)
{
1069
  if (s1 == NULL || s2 == NULL)
1070
    return s1 == s2 ? true : false;
1071

1072
  /* Check type and rank.  */
1073
  if (type_must_agree)
1074
    {
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
      if (!compare_type (s1, s2) || !compare_type (s2, s1))
	{
	  snprintf (errmsg, err_len, "Type mismatch in argument '%s' (%s/%s)",
		    s1->name, gfc_typename (&s1->ts), gfc_typename (&s2->ts));
	  return false;
	}
      if (!compare_rank (s1, s2))
	{
	  snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
		    s1->name, symbol_rank (s1), symbol_rank (s2));
	  return false;
	}
1087 1088 1089 1090 1091 1092 1093
    }

  /* Check INTENT.  */
  if (s1->attr.intent != s2->attr.intent)
    {
      snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
		s1->name);
1094
      return false;
1095 1096 1097 1098 1099 1100 1101
    }

  /* Check OPTIONAL attribute.  */
  if (s1->attr.optional != s2->attr.optional)
    {
      snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
		s1->name);
1102
      return false;
1103 1104 1105 1106 1107 1108 1109
    }

  /* Check ALLOCATABLE attribute.  */
  if (s1->attr.allocatable != s2->attr.allocatable)
    {
      snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
		s1->name);
1110
      return false;
1111 1112 1113 1114 1115 1116 1117
    }

  /* Check POINTER attribute.  */
  if (s1->attr.pointer != s2->attr.pointer)
    {
      snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
		s1->name);
1118
      return false;
1119 1120 1121 1122 1123 1124 1125
    }

  /* Check TARGET attribute.  */
  if (s1->attr.target != s2->attr.target)
    {
      snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
		s1->name);
1126
      return false;
1127 1128
    }

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  /* Check ASYNCHRONOUS attribute.  */
  if (s1->attr.asynchronous != s2->attr.asynchronous)
    {
      snprintf (errmsg, err_len, "ASYNCHRONOUS mismatch in argument '%s'",
		s1->name);
      return false;
    }

  /* Check CONTIGUOUS attribute.  */
  if (s1->attr.contiguous != s2->attr.contiguous)
    {
      snprintf (errmsg, err_len, "CONTIGUOUS mismatch in argument '%s'",
		s1->name);
      return false;
    }

  /* Check VALUE attribute.  */
  if (s1->attr.value != s2->attr.value)
    {
      snprintf (errmsg, err_len, "VALUE mismatch in argument '%s'",
		s1->name);
      return false;
    }

  /* Check VOLATILE attribute.  */
  if (s1->attr.volatile_ != s2->attr.volatile_)
    {
      snprintf (errmsg, err_len, "VOLATILE mismatch in argument '%s'",
		s1->name);
      return false;
    }
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169
  /* Check interface of dummy procedures.  */
  if (s1->attr.flavor == FL_PROCEDURE)
    {
      char err[200];
      if (!gfc_compare_interfaces (s1, s2, s2->name, 0, 1, err, sizeof(err),
				   NULL, NULL))
	{
	  snprintf (errmsg, err_len, "Interface mismatch in dummy procedure "
		    "'%s': %s", s1->name, err);
1170
	  return false;
1171 1172 1173
	}
    }

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
  /* Check string length.  */
  if (s1->ts.type == BT_CHARACTER
      && s1->ts.u.cl && s1->ts.u.cl->length
      && s2->ts.u.cl && s2->ts.u.cl->length)
    {
      int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
					  s2->ts.u.cl->length);
      switch (compval)
      {
	case -1:
	case  1:
	case -3:
	  snprintf (errmsg, err_len, "Character length mismatch "
		    "in argument '%s'", s1->name);
1188
	  return false;
1189 1190 1191

	case -2:
	  /* FIXME: Implement a warning for this case.
1192
	  gfc_warning (0, "Possible character length mismatch in argument %qs",
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
		       s1->name);*/
	  break;

	case 0:
	  break;

	default:
	  gfc_internal_error ("check_dummy_characteristics: Unexpected result "
			      "%i of gfc_dep_compare_expr", compval);
	  break;
      }
    }

  /* Check array shape.  */
  if (s1->as && s2->as)
    {
1209 1210 1211
      int i, compval;
      gfc_expr *shape1, *shape2;

1212 1213 1214 1215
      if (s1->as->type != s2->as->type)
	{
	  snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
		    s1->name);
1216
	  return false;
1217
	}
1218

1219 1220 1221 1222 1223 1224 1225
      if (s1->as->corank != s2->as->corank)
	{
	  snprintf (errmsg, err_len, "Corank mismatch in argument '%s' (%i/%i)",
		    s1->name, s1->as->corank, s2->as->corank);
	  return false;
	}

1226
      if (s1->as->type == AS_EXPLICIT)
1227
	for (i = 0; i < s1->as->rank + MAX (0, s1->as->corank-1); i++)
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	  {
	    shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
				  gfc_copy_expr (s1->as->lower[i]));
	    shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
				  gfc_copy_expr (s2->as->lower[i]));
	    compval = gfc_dep_compare_expr (shape1, shape2);
	    gfc_free_expr (shape1);
	    gfc_free_expr (shape2);
	    switch (compval)
	    {
	      case -1:
	      case  1:
	      case -3:
1241 1242 1243 1244 1245 1246
		if (i < s1->as->rank)
		  snprintf (errmsg, err_len, "Shape mismatch in dimension %i of"
			    " argument '%s'", i + 1, s1->name);
		else
		  snprintf (errmsg, err_len, "Shape mismatch in codimension %i "
			    "of argument '%s'", i - s1->as->rank + 1, s1->name);
1247
		return false;
1248 1249 1250

	      case -2:
		/* FIXME: Implement a warning for this case.
1251
		gfc_warning (0, "Possible shape mismatch in argument %qs",
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
			    s1->name);*/
		break;

	      case 0:
		break;

	      default:
		gfc_internal_error ("check_dummy_characteristics: Unexpected "
				    "result %i of gfc_dep_compare_expr",
				    compval);
		break;
	    }
	  }
1265
    }
1266

1267
  return true;
1268 1269 1270
}


1271 1272 1273
/* Check if the characteristics of two function results match,
   cf. F08:12.3.3.  */

1274
static bool
1275 1276 1277 1278 1279
check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
			      char *errmsg, int err_len)
{
  gfc_symbol *r1, *r2;

1280 1281 1282 1283 1284 1285 1286 1287 1288
  if (s1->ts.interface && s1->ts.interface->result)
    r1 = s1->ts.interface->result;
  else
    r1 = s1->result ? s1->result : s1;

  if (s2->ts.interface && s2->ts.interface->result)
    r2 = s2->ts.interface->result;
  else
    r2 = s2->result ? s2->result : s2;
1289 1290

  if (r1->ts.type == BT_UNKNOWN)
1291
    return true;
1292 1293

  /* Check type and rank.  */
1294
  if (!compare_type (r1, r2))
1295
    {
1296 1297 1298 1299 1300 1301 1302 1303
      snprintf (errmsg, err_len, "Type mismatch in function result (%s/%s)",
		gfc_typename (&r1->ts), gfc_typename (&r2->ts));
      return false;
    }
  if (!compare_rank (r1, r2))
    {
      snprintf (errmsg, err_len, "Rank mismatch in function result (%i/%i)",
		symbol_rank (r1), symbol_rank (r2));
1304
      return false;
1305 1306 1307 1308 1309 1310 1311
    }

  /* Check ALLOCATABLE attribute.  */
  if (r1->attr.allocatable != r2->attr.allocatable)
    {
      snprintf (errmsg, err_len, "ALLOCATABLE attribute mismatch in "
		"function result");
1312
      return false;
1313 1314 1315 1316 1317 1318 1319
    }

  /* Check POINTER attribute.  */
  if (r1->attr.pointer != r2->attr.pointer)
    {
      snprintf (errmsg, err_len, "POINTER attribute mismatch in "
		"function result");
1320
      return false;
1321 1322 1323 1324 1325 1326 1327
    }

  /* Check CONTIGUOUS attribute.  */
  if (r1->attr.contiguous != r2->attr.contiguous)
    {
      snprintf (errmsg, err_len, "CONTIGUOUS attribute mismatch in "
		"function result");
1328
      return false;
1329 1330 1331 1332 1333 1334 1335
    }

  /* Check PROCEDURE POINTER attribute.  */
  if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
    {
      snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
		"function result");
1336
      return false;
1337 1338 1339 1340 1341 1342 1343 1344 1345
    }

  /* Check string length.  */
  if (r1->ts.type == BT_CHARACTER && r1->ts.u.cl && r2->ts.u.cl)
    {
      if (r1->ts.deferred != r2->ts.deferred)
	{
	  snprintf (errmsg, err_len, "Character length mismatch "
		    "in function result");
1346
	  return false;
1347 1348
	}

1349
      if (r1->ts.u.cl->length && r2->ts.u.cl->length)
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
	{
	  int compval = gfc_dep_compare_expr (r1->ts.u.cl->length,
					      r2->ts.u.cl->length);
	  switch (compval)
	  {
	    case -1:
	    case  1:
	    case -3:
	      snprintf (errmsg, err_len, "Character length mismatch "
			"in function result");
1360
	      return false;
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387

	    case -2:
	      /* FIXME: Implement a warning for this case.
	      snprintf (errmsg, err_len, "Possible character length mismatch "
			"in function result");*/
	      break;

	    case 0:
	      break;

	    default:
	      gfc_internal_error ("check_result_characteristics (1): Unexpected "
				  "result %i of gfc_dep_compare_expr", compval);
	      break;
	  }
	}
    }

  /* Check array shape.  */
  if (!r1->attr.allocatable && !r1->attr.pointer && r1->as && r2->as)
    {
      int i, compval;
      gfc_expr *shape1, *shape2;

      if (r1->as->type != r2->as->type)
	{
	  snprintf (errmsg, err_len, "Shape mismatch in function result");
1388
	  return false;
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
	}

      if (r1->as->type == AS_EXPLICIT)
	for (i = 0; i < r1->as->rank + r1->as->corank; i++)
	  {
	    shape1 = gfc_subtract (gfc_copy_expr (r1->as->upper[i]),
				   gfc_copy_expr (r1->as->lower[i]));
	    shape2 = gfc_subtract (gfc_copy_expr (r2->as->upper[i]),
				   gfc_copy_expr (r2->as->lower[i]));
	    compval = gfc_dep_compare_expr (shape1, shape2);
	    gfc_free_expr (shape1);
	    gfc_free_expr (shape2);
	    switch (compval)
	    {
	      case -1:
	      case  1:
	      case -3:
		snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
			  "function result", i + 1);
1408
		return false;
1409 1410 1411

	      case -2:
		/* FIXME: Implement a warning for this case.
1412
		gfc_warning (0, "Possible shape mismatch in return value");*/
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
		break;

	      case 0:
		break;

	      default:
		gfc_internal_error ("check_result_characteristics (2): "
				    "Unexpected result %i of "
				    "gfc_dep_compare_expr", compval);
		break;
	    }
	  }
    }

1427
  return true;
1428 1429 1430
}


1431 1432
/* 'Compare' two formal interfaces associated with a pair of symbols.
   We return nonzero if there exists an actual argument list that
1433
   would be ambiguous between the two interfaces, zero otherwise.
1434
   'strict_flag' specifies whether all the characteristics are
1435 1436
   required to match, which is not the case for ambiguity checks.
   'p1' and 'p2' are the PASS arguments of both procedures (if applicable).  */
1437

1438
int
1439
gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
1440
			int generic_flag, int strict_flag,
1441 1442
			char *errmsg, int err_len,
			const char *p1, const char *p2)
1443 1444 1445
{
  gfc_formal_arglist *f1, *f2;

1446 1447
  gcc_assert (name2 != NULL);

1448 1449
  if (s1->attr.function && (s2->attr.subroutine
      || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
1450
	  && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
1451 1452
    {
      if (errmsg != NULL)
1453
	snprintf (errmsg, err_len, "'%s' is not a function", name2);
1454 1455 1456 1457 1458 1459
      return 0;
    }

  if (s1->attr.subroutine && s2->attr.function)
    {
      if (errmsg != NULL)
1460
	snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
1461 1462
      return 0;
    }
1463

1464 1465 1466
  /* Do strict checks on all characteristics
     (for dummy procedures and procedure pointer assignments).  */
  if (!generic_flag && strict_flag)
1467
    {
1468
      if (s1->attr.function && s2->attr.function)
1469
	{
1470
	  /* If both are functions, check result characteristics.  */
1471 1472
	  if (!check_result_characteristics (s1, s2, errmsg, err_len)
	      || !check_result_characteristics (s2, s1, errmsg, err_len))
1473
	    return 0;
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	}

      if (s1->attr.pure && !s2->attr.pure)
	{
	  snprintf (errmsg, err_len, "Mismatch in PURE attribute");
	  return 0;
	}
      if (s1->attr.elemental && !s2->attr.elemental)
	{
	  snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
1484 1485
	  return 0;
	}
1486
    }
1487

1488 1489
  if (s1->attr.if_source == IFSRC_UNKNOWN
      || s2->attr.if_source == IFSRC_UNKNOWN)
1490 1491
    return 1;

1492 1493
  f1 = gfc_sym_get_dummy_args (s1);
  f2 = gfc_sym_get_dummy_args (s2);
1494

1495
  if (f1 == NULL && f2 == NULL)
1496
    return 1;			/* Special case: No arguments.  */
1497

1498
  if (generic_flag)
1499
    {
1500 1501
      if (count_types_test (f1, f2, p1, p2)
	  || count_types_test (f2, f1, p2, p1))
1502
	return 0;
1503 1504
      if (generic_correspondence (f1, f2, p1, p2)
	  || generic_correspondence (f2, f1, p2, p1))
1505 1506
	return 0;
    }
1507
  else
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    /* Perform the abbreviated correspondence test for operators (the
       arguments cannot be optional and are always ordered correctly).
       This is also done when comparing interfaces for dummy procedures and in
       procedure pointer assignments.  */

    for (;;)
      {
	/* Check existence.  */
	if (f1 == NULL && f2 == NULL)
	  break;
	if (f1 == NULL || f2 == NULL)
	  {
	    if (errmsg != NULL)
	      snprintf (errmsg, err_len, "'%s' has the wrong number of "
1522
			"arguments", name2);
1523 1524 1525
	    return 0;
	  }

1526 1527 1528
	if (UNLIMITED_POLY (f1->sym))
	  goto next;

1529
	if (strict_flag)
1530
	  {
1531
	    /* Check all characteristics.  */
1532 1533
	    if (!check_dummy_characteristics (f1->sym, f2->sym, true, 
					      errmsg, err_len))
1534 1535
	      return 0;
	  }
1536
	else
1537 1538
	  {
	    /* Only check type and rank.  */
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
	    if (!compare_type (f2->sym, f1->sym))
	      {
		if (errmsg != NULL)
		  snprintf (errmsg, err_len, "Type mismatch in argument '%s' "
			    "(%s/%s)", f1->sym->name,
			    gfc_typename (&f1->sym->ts),
			    gfc_typename (&f2->sym->ts));
		return 0;
	      }
	    if (!compare_rank (f2->sym, f1->sym))
	      {
		if (errmsg != NULL)
		  snprintf (errmsg, err_len, "Rank mismatch in argument '%s' "
			    "(%i/%i)", f1->sym->name, symbol_rank (f1->sym),
			    symbol_rank (f2->sym));
		return 0;
	      }
1556
	  }
1557
next:
1558 1559 1560 1561
	f1 = f1->next;
	f2 = f2->next;
      }

1562 1563 1564 1565
  return 1;
}


1566
/* Given a pointer to an interface pointer, remove duplicate
1567 1568 1569
   interfaces and make sure that all symbols are either functions
   or subroutines, and all of the same kind.  Returns nonzero if
   something goes wrong.  */
1570 1571

static int
1572
check_interface0 (gfc_interface *p, const char *interface_name)
1573 1574 1575 1576 1577
{
  gfc_interface *psave, *q, *qlast;

  psave = p;
  for (; p; p = p->next)
1578 1579 1580
    {
      /* Make sure all symbols in the interface have been defined as
	 functions or subroutines.  */
1581 1582 1583
      if (((!p->sym->attr.function && !p->sym->attr.subroutine)
	   || !p->sym->attr.if_source)
	  && p->sym->attr.flavor != FL_DERIVED)
1584 1585
	{
	  if (p->sym->attr.external)
1586
	    gfc_error ("Procedure %qs in %s at %L has no explicit interface",
1587 1588
		       p->sym->name, interface_name, &p->sym->declared_at);
	  else
1589
	    gfc_error ("Procedure %qs in %s at %L is neither function nor "
1590 1591 1592 1593 1594 1595
		       "subroutine", p->sym->name, interface_name,
		      &p->sym->declared_at);
	  return 1;
	}

      /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs.  */
1596 1597
      if ((psave->sym->attr.function && !p->sym->attr.function
	   && p->sym->attr.flavor != FL_DERIVED)
1598 1599
	  || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
	{
1600 1601 1602 1603 1604 1605 1606 1607
	  if (p->sym->attr.flavor != FL_DERIVED)
	    gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
		       " or all FUNCTIONs", interface_name,
		       &p->sym->declared_at);
	  else
	    gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
		       "generic name is also the name of a derived type",
		       interface_name, &p->sym->declared_at);
1608 1609
	  return 1;
	}
1610

1611
      /* F2003, C1207. F2008, C1207.  */
1612
      if (p->sym->attr.proc == PROC_INTERNAL
1613
	  && !gfc_notify_std (GFC_STD_F2008, "Internal procedure "
1614
			      "%qs in %s at %L", p->sym->name,
1615
			      interface_name, &p->sym->declared_at))
1616
	return 1;
1617
    }
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
  p = psave;

  /* Remove duplicate interfaces in this interface list.  */
  for (; p; p = p->next)
    {
      qlast = p;

      for (q = p->next; q;)
	{
	  if (p->sym != q->sym)
	    {
	      qlast = q;
	      q = q->next;
	    }
	  else
	    {
1634
	      /* Duplicate interface.  */
1635
	      qlast->next = q->next;
1636
	      free (q);
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
	      q = qlast->next;
	    }
	}
    }

  return 0;
}


/* Check lists of interfaces to make sure that no two interfaces are
1647
   ambiguous.  Duplicate interfaces (from the same symbol) are OK here.  */
1648 1649

static int
1650
check_interface1 (gfc_interface *p, gfc_interface *q0,
1651
		  int generic_flag, const char *interface_name,
1652
		  bool referenced)
1653
{
1654
  gfc_interface *q;
1655
  for (; p; p = p->next)
1656
    for (q = q0; q; q = q->next)
1657 1658
      {
	if (p->sym == q->sym)
1659
	  continue;		/* Duplicates OK here.  */
1660

1661
	if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
1662 1663
	  continue;

1664 1665 1666
	if (p->sym->attr.flavor != FL_DERIVED
	    && q->sym->attr.flavor != FL_DERIVED
	    && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
1667
				       generic_flag, 0, NULL, 0, NULL, NULL))
1668
	  {
1669
	    if (referenced)
1670
	      gfc_error ("Ambiguous interfaces %qs and %qs in %s at %L",
1671 1672 1673
			 p->sym->name, q->sym->name, interface_name,
			 &p->where);
	    else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
1674
	      gfc_warning (0, "Ambiguous interfaces %qs and %qs in %s at %L",
1675 1676
			   p->sym->name, q->sym->name, interface_name,
			   &p->where);
1677
	    else
1678
	      gfc_warning (0, "Although not referenced, %qs has ambiguous "
1679
			   "interfaces at %L", interface_name, &p->where);
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	    return 1;
	  }
      }
  return 0;
}


/* Check the generic and operator interfaces of symbols to make sure
   that none of the interfaces conflict.  The check has to be done
   after all of the symbols are actually loaded.  */

static void
1692
check_sym_interfaces (gfc_symbol *sym)
1693 1694
{
  char interface_name[100];
1695
  gfc_interface *p;
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

  if (sym->ns != gfc_current_ns)
    return;

  if (sym->generic != NULL)
    {
      sprintf (interface_name, "generic interface '%s'", sym->name);
      if (check_interface0 (sym->generic, interface_name))
	return;

1706 1707
      for (p = sym->generic; p; p = p->next)
	{
1708 1709 1710
	  if (p->sym->attr.mod_proc
	      && (p->sym->attr.if_source != IFSRC_DECL
		  || p->sym->attr.procedure))
1711
	    {
1712
	      gfc_error ("%qs at %L is not a module procedure",
1713
			 p->sym->name, &p->where);
1714 1715 1716 1717
	      return;
	    }
	}

1718
      /* Originally, this test was applied to host interfaces too;
1719 1720
	 this is incorrect since host associated symbols, from any
	 source, cannot be ambiguous with local symbols.  */
1721 1722
      check_interface1 (sym->generic, sym->generic, 1, interface_name,
			sym->attr.referenced || !sym->attr.use_assoc);
1723 1724 1725 1726 1727
    }
}


static void
1728
check_uop_interfaces (gfc_user_op *uop)
1729 1730 1731 1732 1733 1734
{
  char interface_name[100];
  gfc_user_op *uop2;
  gfc_namespace *ns;

  sprintf (interface_name, "operator interface '%s'", uop->name);
1735
  if (check_interface0 (uop->op, interface_name))
1736 1737 1738 1739 1740 1741 1742 1743
    return;

  for (ns = gfc_current_ns; ns; ns = ns->parent)
    {
      uop2 = gfc_find_uop (uop->name, ns);
      if (uop2 == NULL)
	continue;

1744
      check_interface1 (uop->op, uop2->op, 0,
1745
			interface_name, true);
1746 1747 1748
    }
}

1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
/* Given an intrinsic op, return an equivalent op if one exists,
   or INTRINSIC_NONE otherwise.  */

gfc_intrinsic_op
gfc_equivalent_op (gfc_intrinsic_op op)
{
  switch(op)
    {
    case INTRINSIC_EQ:
      return INTRINSIC_EQ_OS;

    case INTRINSIC_EQ_OS:
      return INTRINSIC_EQ;

    case INTRINSIC_NE:
      return INTRINSIC_NE_OS;

    case INTRINSIC_NE_OS:
      return INTRINSIC_NE;

    case INTRINSIC_GT:
      return INTRINSIC_GT_OS;

    case INTRINSIC_GT_OS:
      return INTRINSIC_GT;

    case INTRINSIC_GE:
      return INTRINSIC_GE_OS;

    case INTRINSIC_GE_OS:
      return INTRINSIC_GE;

    case INTRINSIC_LT:
      return INTRINSIC_LT_OS;

    case INTRINSIC_LT_OS:
      return INTRINSIC_LT;

    case INTRINSIC_LE:
      return INTRINSIC_LE_OS;

    case INTRINSIC_LE_OS:
      return INTRINSIC_LE;

    default:
      return INTRINSIC_NONE;
    }
}
1797 1798 1799 1800 1801 1802 1803

/* For the namespace, check generic, user operator and intrinsic
   operator interfaces for consistency and to remove duplicate
   interfaces.  We traverse the whole namespace, counting on the fact
   that most symbols will not have generic or operator interfaces.  */

void
1804
gfc_check_interfaces (gfc_namespace *ns)
1805 1806 1807
{
  gfc_namespace *old_ns, *ns2;
  char interface_name[100];
1808
  int i;
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

  old_ns = gfc_current_ns;
  gfc_current_ns = ns;

  gfc_traverse_ns (ns, check_sym_interfaces);

  gfc_traverse_user_op (ns, check_uop_interfaces);

  for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
    {
      if (i == INTRINSIC_USER)
	continue;

      if (i == INTRINSIC_ASSIGN)
	strcpy (interface_name, "intrinsic assignment operator");
      else
	sprintf (interface_name, "intrinsic '%s' operator",
1826
		 gfc_op2string ((gfc_intrinsic_op) i));
1827

1828
      if (check_interface0 (ns->op[i], interface_name))
1829 1830
	continue;

1831 1832 1833
      if (ns->op[i])
	gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
				      ns->op[i]->where);
1834

1835 1836
      for (ns2 = ns; ns2; ns2 = ns2->parent)
	{
1837
	  gfc_intrinsic_op other_op;
1838

1839
	  if (check_interface1 (ns->op[i], ns2->op[i], 0,
1840 1841 1842
				interface_name, true))
	    goto done;

1843 1844 1845 1846 1847 1848 1849
	  /* i should be gfc_intrinsic_op, but has to be int with this cast
	     here for stupid C++ compatibility rules.  */
	  other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
	  if (other_op != INTRINSIC_NONE
	    &&  check_interface1 (ns->op[i], ns2->op[other_op],
				  0, interface_name, true))
	    goto done;
1850
	}
1851 1852
    }

1853
done:
1854 1855 1856 1857 1858
  gfc_current_ns = old_ns;
}


/* Given a symbol of a formal argument list and an expression, if the
1859 1860 1861 1862
   formal argument is allocatable, check that the actual argument is
   allocatable. Returns nonzero if compatible, zero if not compatible.  */

static int
1863
compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
1864 1865 1866
{
  symbol_attribute attr;

1867 1868
  if (formal->attr.allocatable
      || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
    {
      attr = gfc_expr_attr (actual);
      if (!attr.allocatable)
	return 0;
    }

  return 1;
}


/* Given a symbol of a formal argument list and an expression, if the
1880 1881 1882 1883
   formal argument is a pointer, see if the actual argument is a
   pointer. Returns nonzero if compatible, zero if not compatible.  */

static int
1884
compare_pointer (gfc_symbol *formal, gfc_expr *actual)
1885 1886 1887
{
  symbol_attribute attr;

1888 1889 1890
  if (formal->attr.pointer
      || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)
	  && CLASS_DATA (formal)->attr.class_pointer))
1891 1892
    {
      attr = gfc_expr_attr (actual);
1893 1894 1895 1896 1897

      /* Fortran 2008 allows non-pointer actual arguments.  */
      if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
	return 2;

1898 1899 1900 1901 1902 1903 1904 1905
      if (!attr.pointer)
	return 0;
    }

  return 1;
}


1906 1907 1908 1909 1910 1911
/* Emit clear error messages for rank mismatch.  */

static void
argument_rank_mismatch (const char *name, locus *where,
			int rank1, int rank2)
{
1912 1913 1914 1915 1916

  /* TS 29113, C407b.  */
  if (rank2 == -1)
    {
      gfc_error ("The assumed-rank array at %L requires that the dummy argument"
1917
		 " %qs has assumed-rank", where, name);
1918 1919
    }
  else if (rank1 == 0)
1920
    {
1921
      gfc_error ("Rank mismatch in argument %qs at %L "
1922 1923 1924 1925
		 "(scalar and rank-%d)", name, where, rank2);
    }
  else if (rank2 == 0)
    {
1926
      gfc_error ("Rank mismatch in argument %qs at %L "
1927 1928 1929
		 "(rank-%d and scalar)", name, where, rank1);
    }
  else
1930
    {
1931
      gfc_error ("Rank mismatch in argument %qs at %L "
1932 1933 1934 1935 1936
		 "(rank-%d and rank-%d)", name, where, rank1, rank2);
    }
}


1937 1938 1939 1940 1941
/* Given a symbol of a formal argument list and an expression, see if
   the two are compatible as arguments.  Returns nonzero if
   compatible, zero if not compatible.  */

static int
1942
compare_parameter (gfc_symbol *formal, gfc_expr *actual,
1943
		   int ranks_must_agree, int is_elemental, locus *where)
1944 1945
{
  gfc_ref *ref;
1946
  bool rank_check, is_pointer;
1947 1948
  char err[200];
  gfc_component *ppc;
1949

1950 1951 1952 1953 1954 1955 1956
  /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
     procs c_f_pointer or c_f_procpointer, and we need to accept most
     pointers the user could give us.  This should allow that.  */
  if (formal->ts.type == BT_VOID)
    return 1;

  if (formal->ts.type == BT_DERIVED
1957
      && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
1958
      && actual->ts.type == BT_DERIVED
1959
      && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
1960 1961
    return 1;

1962
  if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
1963 1964
    /* Make sure the vtab symbol is present when
       the module variables are generated.  */
1965
    gfc_find_derived_vtab (actual->ts.u.derived);
1966

1967 1968
  if (actual->ts.type == BT_PROCEDURE)
    {
1969
      gfc_symbol *act_sym = actual->symtree->n.sym;
1970

1971 1972 1973 1974 1975 1976
      if (formal->attr.flavor != FL_PROCEDURE)
	{
	  if (where)
	    gfc_error ("Invalid procedure argument at %L", &actual->where);
	  return 0;
	}
1977

1978
      if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
1979
				   sizeof(err), NULL, NULL))
1980 1981
	{
	  if (where)
1982
	    gfc_error ("Interface mismatch in dummy procedure %qs at %L: %s",
1983 1984 1985
		       formal->name, &actual->where, err);
	  return 0;
	}
1986

1987
      if (formal->attr.function && !act_sym->attr.function)
1988 1989 1990 1991
	{
	  gfc_add_function (&act_sym->attr, act_sym->name,
	  &act_sym->declared_at);
	  if (act_sym->ts.type == BT_UNKNOWN
1992
	      && !gfc_set_default_type (act_sym, 1, act_sym->ns))
1993 1994 1995
	    return 0;
	}
      else if (formal->attr.subroutine && !act_sym->attr.subroutine)
1996 1997 1998
	gfc_add_subroutine (&act_sym->attr, act_sym->name,
			    &act_sym->declared_at);

1999
      return 1;
2000 2001
    }

2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
  ppc = gfc_get_proc_ptr_comp (actual);
  if (ppc)
    {
      if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
				   err, sizeof(err), NULL, NULL))
	{
	  if (where)
	    gfc_error ("Interface mismatch in dummy procedure %qs at %L: %s",
		       formal->name, &actual->where, err);
	  return 0;
	}
    }

2015 2016 2017 2018 2019
  /* F2008, C1241.  */
  if (formal->attr.pointer && formal->attr.contiguous
      && !gfc_is_simply_contiguous (actual, true))
    {
      if (where)
2020
	gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
2021
		   "must be simply contiguous", formal->name, &actual->where);
2022 2023 2024
      return 0;
    }

2025
  if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
2026
      && actual->ts.type != BT_HOLLERITH
2027
      && formal->ts.type != BT_ASSUMED
2028
      && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2029 2030
      && !gfc_compare_types (&formal->ts, &actual->ts)
      && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
2031
	   && gfc_compare_derived_types (formal->ts.u.derived,
2032
					 CLASS_DATA (actual)->ts.u.derived)))
2033
    {
2034
      if (where)
2035
	gfc_error ("Type mismatch in argument %qs at %L; passed %s to %s",
2036 2037
		   formal->name, &actual->where, gfc_typename (&actual->ts),
		   gfc_typename (&formal->ts));
2038 2039
      return 0;
    }
2040

2041 2042 2043 2044
  if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
    {
      if (where)
	gfc_error ("Assumed-type actual argument at %L requires that dummy "
2045
		   "argument %qs is of assumed type", &actual->where,
2046 2047 2048 2049
		   formal->name);
      return 0;
    }

2050
  /* F2008, 12.5.2.5; IR F08/0073.  */
2051 2052
  if (formal->ts.type == BT_CLASS && formal->attr.class_ok
      && actual->expr_type != EXPR_NULL
2053
      && ((CLASS_DATA (formal)->attr.class_pointer
2054
	   && formal->attr.intent != INTENT_IN)
2055 2056 2057 2058 2059
          || CLASS_DATA (formal)->attr.allocatable))
    {
      if (actual->ts.type != BT_CLASS)
	{
	  if (where)
2060
	    gfc_error ("Actual argument to %qs at %L must be polymorphic",
2061 2062 2063
			formal->name, &actual->where);
	  return 0;
	}
2064 2065 2066 2067

      if (!gfc_expr_attr (actual).class_ok)
	return 0;

2068 2069 2070
      if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
	  && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
					 CLASS_DATA (formal)->ts.u.derived))
2071 2072
	{
	  if (where)
2073
	    gfc_error ("Actual argument to %qs at %L must have the same "
2074 2075 2076 2077
		       "declared type", formal->name, &actual->where);
	  return 0;
	}
    }
2078

2079 2080 2081 2082 2083 2084 2085 2086 2087
  /* F08: 12.5.2.5 Allocatable and pointer dummy variables.  However, this
     is necessary also for F03, so retain error for both.
     NOTE: Other type/kind errors pre-empt this error.  Since they are F03
     compatible, no attempt has been made to channel to this one.  */
  if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
      && (CLASS_DATA (formal)->attr.allocatable
	  ||CLASS_DATA (formal)->attr.class_pointer))
    {
      if (where)
2088
	gfc_error ("Actual argument to %qs at %L must be unlimited "
2089 2090 2091 2092 2093 2094 2095
		   "polymorphic since the formal argument is a "
		   "pointer or allocatable unlimited polymorphic "
		   "entity [F2008: 12.5.2.5]", formal->name,
		   &actual->where);
      return 0;
    }

2096
  if (formal->attr.codimension && !gfc_is_coarray (actual))
2097
    {
2098
      if (where)
2099
	gfc_error ("Actual argument to %qs at %L must be a coarray",
2100
		       formal->name, &actual->where);
2101 2102
      return 0;
    }
2103

2104 2105 2106
  if (formal->attr.codimension && formal->attr.allocatable)
    {
      gfc_ref *last = NULL;
2107

2108
      for (ref = actual->ref; ref; ref = ref->next)
2109 2110
	if (ref->type == REF_COMPONENT)
	  last = ref;
2111 2112

      /* F2008, 12.5.2.6.  */
2113 2114 2115
      if ((last && last->u.c.component->as->corank != formal->as->corank)
	  || (!last
	      && actual->symtree->n.sym->as->corank != formal->as->corank))
2116 2117
	{
	  if (where)
2118
	    gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
2119 2120 2121 2122 2123
		   formal->name, &actual->where, formal->as->corank,
		   last ? last->u.c.component->as->corank
			: actual->symtree->n.sym->as->corank);
	  return 0;
	}
2124
    }
2125

2126 2127
  if (formal->attr.codimension)
    {
2128 2129 2130
      /* F2008, 12.5.2.8.  */
      if (formal->attr.dimension
	  && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
2131
	  && gfc_expr_attr (actual).dimension
2132 2133 2134
	  && !gfc_is_simply_contiguous (actual, true))
	{
	  if (where)
2135
	    gfc_error ("Actual argument to %qs at %L must be simply "
2136 2137 2138
		       "contiguous", formal->name, &actual->where);
	  return 0;
	}
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148

      /* F2008, C1303 and C1304.  */
      if (formal->attr.intent != INTENT_INOUT
	  && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
	       && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
	       && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
	      || formal->attr.lock_comp))

    	{
	  if (where)
2149
	    gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2150 2151 2152 2153
		       "which is LOCK_TYPE or has a LOCK_TYPE component",
		       formal->name, &actual->where);
	  return 0;
	}
2154
    }
2155 2156 2157 2158 2159 2160

  /* F2008, C1239/C1240.  */
  if (actual->expr_type == EXPR_VARIABLE
      && (actual->symtree->n.sym->attr.asynchronous
         || actual->symtree->n.sym->attr.volatile_)
      &&  (formal->attr.asynchronous || formal->attr.volatile_)
2161
      && actual->rank && formal->as && !gfc_is_simply_contiguous (actual, true)
2162 2163
      && ((formal->as->type != AS_ASSUMED_SHAPE
	   && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
2164 2165 2166
	  || formal->attr.contiguous))
    {
      if (where)
2167
	gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
2168 2169 2170
		   "assumed-rank array without CONTIGUOUS attribute - as actual"
		   " argument at %L is not simply contiguous and both are "
		   "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
2171
      return 0;
2172 2173
    }

2174 2175 2176 2177 2178 2179 2180
  if (formal->attr.allocatable && !formal->attr.codimension
      && gfc_expr_attr (actual).codimension)
    {
      if (formal->attr.intent == INTENT_OUT)
	{
	  if (where)
	    gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
2181
		       "INTENT(OUT) dummy argument %qs", &actual->where,
2182 2183 2184
		       formal->name);
	    return 0;
	}
2185
      else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
2186 2187 2188
	gfc_warning (OPT_Wsurprising,
		     "Passing coarray at %L to allocatable, noncoarray dummy "
		     "argument %qs, which is invalid if the allocation status"
2189 2190 2191
		     " is modified",  &actual->where, formal->name);
    }

2192 2193
  /* If the rank is the same or the formal argument has assumed-rank.  */
  if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
2194 2195
    return 1;

2196 2197
  rank_check = where != NULL && !is_elemental && formal->as
	       && (formal->as->type == AS_ASSUMED_SHAPE
2198 2199
		   || formal->as->type == AS_DEFERRED)
	       && actual->expr_type != EXPR_NULL;
2200

2201 2202 2203 2204
  /* Skip rank checks for NO_ARG_CHECK.  */
  if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
    return 1;

2205
  /* Scalar & coindexed, see: F2008, Section 12.5.2.4.  */
2206 2207
  if (rank_check || ranks_must_agree
      || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
2208
      || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
2209 2210 2211 2212 2213
      || (actual->rank == 0
	  && ((formal->ts.type == BT_CLASS
	       && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
	      || (formal->ts.type != BT_CLASS
		   && formal->as->type == AS_ASSUMED_SHAPE))
2214
	  && actual->expr_type != EXPR_NULL)
2215 2216
      || (actual->rank == 0 && formal->attr.dimension
	  && gfc_is_coindexed (actual)))
2217 2218
    {
      if (where)
2219 2220
	argument_rank_mismatch (formal->name, &actual->where,
				symbol_rank (formal), actual->rank);
2221
      return 0;
2222 2223 2224 2225 2226
    }
  else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
    return 1;

  /* At this point, we are considering a scalar passed to an array.   This
2227
     is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
2228
     - if the actual argument is (a substring of) an element of a
2229 2230 2231 2232 2233 2234
       non-assumed-shape/non-pointer/non-polymorphic array; or
     - (F2003) if the actual argument is of type character of default/c_char
       kind.  */

  is_pointer = actual->expr_type == EXPR_VARIABLE
	       ? actual->symtree->n.sym->attr.pointer : false;
2235 2236

  for (ref = actual->ref; ref; ref = ref->next)
2237 2238 2239 2240 2241
    {
      if (ref->type == REF_COMPONENT)
	is_pointer = ref->u.c.component->attr.pointer;
      else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
	       && ref->u.ar.dimen > 0
2242
	       && (!ref->next
2243 2244 2245 2246 2247 2248 2249
		   || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
        break;
    }

  if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
    {
      if (where)
2250
	gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
2251 2252 2253 2254 2255 2256 2257 2258 2259
		   "at %L", formal->name, &actual->where);
      return 0;
    }

  if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
      && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
    {
      if (where)
	gfc_error ("Element of assumed-shaped or pointer "
2260
		   "array passed to array dummy argument %qs at %L",
2261 2262 2263
		   formal->name, &actual->where);
      return 0;
    }
2264

2265 2266
  if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
      && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2267
    {
2268 2269 2270 2271 2272
      if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
	{
	  if (where)
	    gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
		       "CHARACTER actual argument with array dummy argument "
2273
		       "%qs at %L", formal->name, &actual->where);
2274 2275 2276
	  return 0;
	}

2277 2278 2279
      if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
	{
	  gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
2280
		     "array dummy argument %qs at %L",
2281 2282 2283 2284 2285 2286 2287 2288
		     formal->name, &actual->where);
	  return 0;
	}
      else if ((gfc_option.allow_std & GFC_STD_F2003) == 0)
	return 0;
      else
	return 1;
    }
2289 2290

  if (ref == NULL && actual->expr_type != EXPR_NULL)
2291 2292
    {
      if (where)
2293 2294
	argument_rank_mismatch (formal->name, &actual->where,
				symbol_rank (formal), actual->rank);
2295 2296 2297
      return 0;
    }

2298 2299 2300 2301
  return 1;
}


2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
/* Returns the storage size of a symbol (formal argument) or
   zero if it cannot be determined.  */

static unsigned long
get_sym_storage_size (gfc_symbol *sym)
{
  int i;
  unsigned long strlen, elements;

  if (sym->ts.type == BT_CHARACTER)
    {
2313 2314 2315
      if (sym->ts.u.cl && sym->ts.u.cl->length
          && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
	strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
2316 2317 2318 2319
      else
	return 0;
    }
  else
2320
    strlen = 1;
2321 2322 2323 2324 2325 2326 2327 2328 2329

  if (symbol_rank (sym) == 0)
    return strlen;

  elements = 1;
  if (sym->as->type != AS_EXPLICIT)
    return 0;
  for (i = 0; i < sym->as->rank; i++)
    {
2330
      if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
2331 2332 2333
	  || sym->as->lower[i]->expr_type != EXPR_CONSTANT)
	return 0;

2334 2335
      elements *= mpz_get_si (sym->as->upper[i]->value.integer)
		  - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
2336 2337 2338 2339 2340 2341 2342 2343
    }

  return strlen*elements;
}


/* Returns the storage size of an expression (actual argument) or
   zero if it cannot be determined. For an array element, it returns
2344
   the remaining size as the element sequence consists of all storage
2345 2346 2347 2348 2349 2350 2351
   units of the actual argument up to the end of the array.  */

static unsigned long
get_expr_storage_size (gfc_expr *e)
{
  int i;
  long int strlen, elements;
2352
  long int substrlen = 0;
2353
  bool is_str_storage = false;
2354 2355 2356 2357
  gfc_ref *ref;

  if (e == NULL)
    return 0;
2358

2359 2360
  if (e->ts.type == BT_CHARACTER)
    {
2361 2362 2363
      if (e->ts.u.cl && e->ts.u.cl->length
          && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
	strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
2364
      else if (e->expr_type == EXPR_CONSTANT
2365
	       && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
	strlen = e->value.character.length;
      else
	return 0;
    }
  else
    strlen = 1; /* Length per element.  */

  if (e->rank == 0 && !e->ref)
    return strlen;

  elements = 1;
  if (!e->ref)
    {
      if (!e->shape)
	return 0;
      for (i = 0; i < e->rank; i++)
	elements *= mpz_get_si (e->shape[i]);
      return elements*strlen;
    }

  for (ref = e->ref; ref; ref = ref->next)
    {
2388 2389 2390
      if (ref->type == REF_SUBSTRING && ref->u.ss.start
	  && ref->u.ss.start->expr_type == EXPR_CONSTANT)
	{
2391 2392 2393 2394
	  if (is_str_storage)
	    {
	      /* The string length is the substring length.
		 Set now to full string length.  */
2395
	      if (!ref->u.ss.length || !ref->u.ss.length->length
2396 2397 2398 2399 2400 2401
		  || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
		return 0;

	      strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
	    }
	  substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
2402 2403 2404
	  continue;
	}

2405
      if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2406 2407 2408 2409
	for (i = 0; i < ref->u.ar.dimen; i++)
	  {
	    long int start, end, stride;
	    stride = 1;
2410

2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
	    if (ref->u.ar.stride[i])
	      {
		if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT)
		  stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
		else
		  return 0;
	      }

	    if (ref->u.ar.start[i])
	      {
		if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT)
		  start = mpz_get_si (ref->u.ar.start[i]->value.integer);
		else
		  return 0;
	      }
2426 2427 2428 2429 2430
	    else if (ref->u.ar.as->lower[i]
		     && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT)
	      start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
	    else
	      return 0;
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446

	    if (ref->u.ar.end[i])
	      {
		if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT)
		  end = mpz_get_si (ref->u.ar.end[i]->value.integer);
		else
		  return 0;
	      }
	    else if (ref->u.ar.as->upper[i]
		     && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
	      end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
	    else
	      return 0;

	    elements *= (end - start)/stride + 1L;
	  }
2447
      else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
2448 2449 2450 2451 2452
	for (i = 0; i < ref->u.ar.as->rank; i++)
	  {
	    if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
		&& ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
		&& ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT)
2453 2454
	      elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
			  - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2455 2456 2457 2458
			  + 1L;
	    else
	      return 0;
	  }
2459
      else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2460 2461
	       && e->expr_type == EXPR_VARIABLE)
	{
2462
	  if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
	      || e->symtree->n.sym->attr.pointer)
	    {
	      elements = 1;
	      continue;
	    }

	  /* Determine the number of remaining elements in the element
	     sequence for array element designators.  */
	  is_str_storage = true;
	  for (i = ref->u.ar.dimen - 1; i >= 0; i--)
	    {
	      if (ref->u.ar.start[i] == NULL
		  || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
		  || ref->u.ar.as->upper[i] == NULL
		  || ref->u.ar.as->lower[i] == NULL
		  || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
		  || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT)
		return 0;

	      elements
		   = elements
		     * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
			- mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
			+ 1L)
		     - (mpz_get_si (ref->u.ar.start[i]->value.integer)
			- mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
	    }
        }
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
      else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
	       && ref->u.c.component->attr.proc_pointer
	       && ref->u.c.component->attr.dimension)
	{
	  /* Array-valued procedure-pointer components.  */
	  gfc_array_spec *as = ref->u.c.component->as;
	  for (i = 0; i < as->rank; i++)
	    {
	      if (!as->upper[i] || !as->lower[i]
		  || as->upper[i]->expr_type != EXPR_CONSTANT
		  || as->lower[i]->expr_type != EXPR_CONSTANT)
		return 0;

	      elements = elements
			 * (mpz_get_si (as->upper[i]->value.integer)
			    - mpz_get_si (as->lower[i]->value.integer) + 1L);
	    }
	}
2509 2510
    }

2511
  if (substrlen)
2512 2513 2514 2515
    return (is_str_storage) ? substrlen + (elements-1)*strlen
			    : elements*strlen;
  else
    return elements*strlen;
2516 2517 2518
}


2519 2520 2521 2522
/* Given an expression, check whether it is an array section
   which has a vector subscript. If it has, one is returned,
   otherwise zero.  */

2523 2524
int
gfc_has_vector_subscript (gfc_expr *e)
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
{
  int i;
  gfc_ref *ref;

  if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
    return 0;

  for (ref = e->ref; ref; ref = ref->next)
    if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
      for (i = 0; i < ref->u.ar.dimen; i++)
	if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
	  return 1;

  return 0;
}


2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
static bool
is_procptr_result (gfc_expr *expr)
{
  gfc_component *c = gfc_get_proc_ptr_comp (expr);
  if (c)
    return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
  else
    return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
	    && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
}


2554 2555 2556 2557 2558 2559 2560
/* Given formal and actual argument lists, see if they are compatible.
   If they are compatible, the actual argument list is sorted to
   correspond with the formal list, and elements for missing optional
   arguments are inserted. If WHERE pointer is nonnull, then we issue
   errors when things don't match instead of just returning the status
   code.  */

2561 2562 2563
static int
compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
	 	       int ranks_must_agree, int is_elemental, locus *where)
2564
{
2565
  gfc_actual_arglist **new_arg, *a, *actual, temp;
2566 2567
  gfc_formal_arglist *f;
  int i, n, na;
2568
  unsigned long actual_size, formal_size;
2569
  bool full_array = false;
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579

  actual = *ap;

  if (actual == NULL && formal == NULL)
    return 1;

  n = 0;
  for (f = formal; f; f = f->next)
    n++;

2580
  new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
2581 2582

  for (i = 0; i < n; i++)
2583
    new_arg[i] = NULL;
2584 2585 2586 2587 2588 2589 2590

  na = 0;
  f = formal;
  i = 0;

  for (a = actual; a; a = a->next, f = f->next)
    {
2591 2592
      /* Look for keywords but ignore g77 extensions like %VAL.  */
      if (a->name != NULL && a->name[0] != '%')
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
	{
	  i = 0;
	  for (f = formal; f; f = f->next, i++)
	    {
	      if (f->sym == NULL)
		continue;
	      if (strcmp (f->sym->name, a->name) == 0)
		break;
	    }

	  if (f == NULL)
	    {
	      if (where)
2606
		gfc_error ("Keyword argument %qs at %L is not in "
2607
			   "the procedure", a->name, &a->expr->where);
2608 2609 2610
	      return 0;
	    }

2611
	  if (new_arg[i] != NULL)
2612 2613
	    {
	      if (where)
2614
		gfc_error ("Keyword argument %qs at %L is already associated "
2615 2616
			   "with another actual argument", a->name,
			   &a->expr->where);
2617 2618 2619 2620 2621 2622 2623
	      return 0;
	    }
	}

      if (f == NULL)
	{
	  if (where)
2624 2625
	    gfc_error ("More actual than formal arguments in procedure "
		       "call at %L", where);
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635

	  return 0;
	}

      if (f->sym == NULL && a->expr == NULL)
	goto match;

      if (f->sym == NULL)
	{
	  if (where)
2636 2637
	    gfc_error ("Missing alternate return spec in subroutine call "
		       "at %L", where);
2638 2639 2640 2641 2642 2643
	  return 0;
	}

      if (a->expr == NULL)
	{
	  if (where)
2644 2645
	    gfc_error ("Unexpected alternate return spec in subroutine "
		       "call at %L", where);
2646 2647
	  return 0;
	}
2648

2649 2650
      /* Make sure that intrinsic vtables exist for calls to unlimited
	 polymorphic formal arguments.  */
2651
      if (UNLIMITED_POLY (f->sym)
2652 2653
	  && a->expr->ts.type != BT_DERIVED
	  && a->expr->ts.type != BT_CLASS)
2654
	gfc_find_vtab (&a->expr->ts);
2655

2656 2657 2658 2659 2660 2661 2662 2663 2664
      if (a->expr->expr_type == EXPR_NULL
	  && ((f->sym->ts.type != BT_CLASS && !f->sym->attr.pointer
	       && (f->sym->attr.allocatable || !f->sym->attr.optional
		   || (gfc_option.allow_std & GFC_STD_F2008) == 0))
	      || (f->sym->ts.type == BT_CLASS
		  && !CLASS_DATA (f->sym)->attr.class_pointer
		  && (CLASS_DATA (f->sym)->attr.allocatable
		      || !f->sym->attr.optional
		      || (gfc_option.allow_std & GFC_STD_F2008) == 0))))
2665
	{
2666 2667 2668 2669 2670
	  if (where
	      && (!f->sym->attr.optional
		  || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
		  || (f->sym->ts.type == BT_CLASS
			 && CLASS_DATA (f->sym)->attr.allocatable)))
2671
	    gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
2672 2673 2674
		       where, f->sym->name);
	  else if (where)
	    gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
2675
		       "dummy %qs", where, f->sym->name);
2676 2677 2678

	  return 0;
	}
2679

2680 2681 2682
      if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
			      is_elemental, where))
	return 0;
2683

2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
      /* TS 29113, 6.3p2.  */
      if (f->sym->ts.type == BT_ASSUMED
	  && (a->expr->ts.type == BT_DERIVED
	      || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
	{
	  gfc_namespace *f2k_derived;

	  f2k_derived = a->expr->ts.type == BT_DERIVED
			? a->expr->ts.u.derived->f2k_derived
			: CLASS_DATA (a->expr)->ts.u.derived->f2k_derived;

	  if (f2k_derived
	      && (f2k_derived->finalizers || f2k_derived->tb_sym_root))
	    {
	      gfc_error ("Actual argument at %L to assumed-type dummy is of "
			 "derived type with type-bound or FINAL procedures",
			 &a->expr->where);
2701
	      return false;
2702 2703 2704
	    }
	}

2705 2706 2707
      /* Special case for character arguments.  For allocatable, pointer
	 and assumed-shape dummies, the string length needs to match
	 exactly.  */
2708
      if (a->expr->ts.type == BT_CHARACTER
2709 2710 2711 2712
	   && a->expr->ts.u.cl && a->expr->ts.u.cl->length
	   && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
	   && f->sym->ts.u.cl && f->sym->ts.u.cl && f->sym->ts.u.cl->length
	   && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2713 2714
	   && (f->sym->attr.pointer || f->sym->attr.allocatable
	       || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
2715 2716
	   && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
			f->sym->ts.u.cl->length->value.integer) != 0))
2717
	 {
2718
	   if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
2719 2720
	     gfc_warning (0,
			  "Character length mismatch (%ld/%ld) between actual "
2721
			  "argument and pointer or allocatable dummy argument "
2722
			  "%qs at %L",
2723 2724
			  mpz_get_si (a->expr->ts.u.cl->length->value.integer),
			  mpz_get_si (f->sym->ts.u.cl->length->value.integer),
2725 2726
			  f->sym->name, &a->expr->where);
	   else if (where)
2727 2728
	     gfc_warning (0,
			  "Character length mismatch (%ld/%ld) between actual "
2729
			  "argument and assumed-shape dummy argument %qs "
2730
			  "at %L",
2731 2732
			  mpz_get_si (a->expr->ts.u.cl->length->value.integer),
			  mpz_get_si (f->sym->ts.u.cl->length->value.integer),
2733 2734
			  f->sym->name, &a->expr->where);
	   return 0;
2735 2736
	 }

2737 2738 2739 2740 2741
      if ((f->sym->attr.pointer || f->sym->attr.allocatable)
	    && f->sym->ts.deferred != a->expr->ts.deferred
	    && a->expr->ts.type == BT_CHARACTER)
	{
	  if (where)
2742
	    gfc_error ("Actual argument at %L to allocatable or "
2743
		       "pointer dummy argument %qs must have a deferred "
2744 2745 2746 2747 2748
		       "length type parameter if and only if the dummy has one",
		       &a->expr->where, f->sym->name);
	  return 0;
	}

2749 2750 2751
      if (f->sym->ts.type == BT_CLASS)
	goto skip_size_check;

2752 2753
      actual_size = get_expr_storage_size (a->expr);
      formal_size = get_sym_storage_size (f->sym);
2754 2755 2756
      if (actual_size != 0 && actual_size < formal_size
	  && a->expr->ts.type != BT_PROCEDURE
	  && f->sym->attr.flavor != FL_PROCEDURE)
2757 2758
	{
	  if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
2759
	    gfc_warning (0, "Character length of actual argument shorter "
2760
			 "than of dummy argument %qs (%lu/%lu) at %L",
2761 2762
			 f->sym->name, actual_size, formal_size,
			 &a->expr->where);
2763
          else if (where)
2764
	    gfc_warning (0, "Actual argument contains too few "
2765
			 "elements for dummy argument %qs (%lu/%lu) at %L",
2766 2767
			 f->sym->name, actual_size, formal_size,
			 &a->expr->where);
2768 2769 2770
	  return  0;
	}

2771 2772
     skip_size_check:

2773 2774
      /* Satisfy F03:12.4.1.3 by ensuring that a procedure pointer actual
         argument is provided for a procedure pointer formal argument.  */
2775
      if (f->sym->attr.proc_pointer
2776
	  && !((a->expr->expr_type == EXPR_VARIABLE
2777 2778
		&& (a->expr->symtree->n.sym->attr.proc_pointer
		    || gfc_is_proc_ptr_comp (a->expr)))
2779
	       || (a->expr->expr_type == EXPR_FUNCTION
2780
		   && is_procptr_result (a->expr))))
2781 2782
	{
	  if (where)
2783
	    gfc_error ("Expected a procedure pointer for argument %qs at %L",
2784 2785 2786 2787
		       f->sym->name, &a->expr->where);
	  return 0;
	}

2788
      /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
2789
	 provided for a procedure formal argument.  */
2790
      if (f->sym->attr.flavor == FL_PROCEDURE
2791 2792 2793 2794 2795 2796
	  && !((a->expr->expr_type == EXPR_VARIABLE
		&& (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
		    || a->expr->symtree->n.sym->attr.proc_pointer
		    || gfc_is_proc_ptr_comp (a->expr)))
	       || (a->expr->expr_type == EXPR_FUNCTION
		   && is_procptr_result (a->expr))))
2797
	{
2798
	  if (where)
2799
	    gfc_error ("Expected a procedure for argument %qs at %L",
2800 2801
		       f->sym->name, &a->expr->where);
	  return 0;
2802 2803
	}

2804
      if (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE
2805 2806 2807 2808 2809 2810 2811 2812
	  && a->expr->expr_type == EXPR_VARIABLE
	  && a->expr->symtree->n.sym->as
	  && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SIZE
	  && (a->expr->ref == NULL
	      || (a->expr->ref->type == REF_ARRAY
		  && a->expr->ref->u.ar.type == AR_FULL)))
	{
	  if (where)
2813
	    gfc_error ("Actual argument for %qs cannot be an assumed-size"
2814 2815 2816 2817
		       " array at %L", f->sym->name, where);
	  return 0;
	}

2818 2819
      if (a->expr->expr_type != EXPR_NULL
	  && compare_pointer (f->sym, a->expr) == 0)
2820 2821
	{
	  if (where)
2822
	    gfc_error ("Actual argument for %qs must be a pointer at %L",
2823 2824 2825 2826
		       f->sym->name, &a->expr->where);
	  return 0;
	}

2827 2828 2829 2830 2831 2832
      if (a->expr->expr_type != EXPR_NULL
	  && (gfc_option.allow_std & GFC_STD_F2008) == 0
	  && compare_pointer (f->sym, a->expr) == 2)
	{
	  if (where)
	    gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
2833
		       "pointer dummy %qs", &a->expr->where,f->sym->name);
2834 2835
	  return 0;
	}
2836

2837

2838 2839 2840 2841 2842
      /* Fortran 2008, C1242.  */
      if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
	{
	  if (where)
	    gfc_error ("Coindexed actual argument at %L to pointer "
2843
		       "dummy %qs",
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
		       &a->expr->where, f->sym->name);
	  return 0;
	}

      /* Fortran 2008, 12.5.2.5 (no constraint).  */
      if (a->expr->expr_type == EXPR_VARIABLE
	  && f->sym->attr.intent != INTENT_IN
	  && f->sym->attr.allocatable
	  && gfc_is_coindexed (a->expr))
	{
	  if (where)
	    gfc_error ("Coindexed actual argument at %L to allocatable "
2856
		       "dummy %qs requires INTENT(IN)",
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
		       &a->expr->where, f->sym->name);
	  return 0;
	}

      /* Fortran 2008, C1237.  */
      if (a->expr->expr_type == EXPR_VARIABLE
	  && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
	  && gfc_is_coindexed (a->expr)
	  && (a->expr->symtree->n.sym->attr.volatile_
	      || a->expr->symtree->n.sym->attr.asynchronous))
	{
	  if (where)
	    gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
2870
		       "%L requires that dummy %qs has neither "
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
		       "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
		       f->sym->name);
	  return 0;
	}

      /* Fortran 2008, 12.5.2.4 (no constraint).  */
      if (a->expr->expr_type == EXPR_VARIABLE
	  && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
	  && gfc_is_coindexed (a->expr)
	  && gfc_has_ultimate_allocatable (a->expr))
	{
	  if (where)
	    gfc_error ("Coindexed actual argument at %L with allocatable "
2884
		       "ultimate component to dummy %qs requires either VALUE "
2885 2886 2887 2888
		       "or INTENT(IN)", &a->expr->where, f->sym->name);
	  return 0;
	}

2889 2890 2891 2892 2893 2894
     if (f->sym->ts.type == BT_CLASS
	   && CLASS_DATA (f->sym)->attr.allocatable
	   && gfc_is_class_array_ref (a->expr, &full_array)
	   && !full_array)
	{
	  if (where)
2895
	    gfc_error ("Actual CLASS array argument for %qs must be a full "
2896 2897 2898 2899 2900
		       "array at %L", f->sym->name, &a->expr->where);
	  return 0;
	}


2901 2902 2903 2904
      if (a->expr->expr_type != EXPR_NULL
	  && compare_allocatable (f->sym, a->expr) == 0)
	{
	  if (where)
2905
	    gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
2906 2907 2908 2909
		       f->sym->name, &a->expr->where);
	  return 0;
	}

2910
      /* Check intent = OUT/INOUT for definable actual argument.  */
2911 2912
      if ((f->sym->attr.intent == INTENT_OUT
	  || f->sym->attr.intent == INTENT_INOUT))
2913
	{
2914 2915 2916
	  const char* context = (where
				 ? _("actual argument to INTENT = OUT/INOUT")
				 : NULL);
2917

2918 2919 2920
	  if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
		&& CLASS_DATA (f->sym)->attr.class_pointer)
	       || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
2921
	      && !gfc_check_vardef_context (a->expr, true, false, false, context))
2922
	    return 0;
2923
	  if (!gfc_check_vardef_context (a->expr, false, false, false, context))
2924
	    return 0;
2925 2926
	}

2927 2928
      if ((f->sym->attr.intent == INTENT_OUT
	   || f->sym->attr.intent == INTENT_INOUT
2929 2930
	   || f->sym->attr.volatile_
	   || f->sym->attr.asynchronous)
2931
	  && gfc_has_vector_subscript (a->expr))
2932 2933
	{
	  if (where)
2934 2935 2936
	    gfc_error ("Array-section actual argument with vector "
		       "subscripts at %L is incompatible with INTENT(OUT), "
		       "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
2937
		       "of the dummy argument %qs",
2938 2939 2940 2941
		       &a->expr->where, f->sym->name);
	  return 0;
	}

2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953
      /* C1232 (R1221) For an actual argument which is an array section or
	 an assumed-shape array, the dummy argument shall be an assumed-
	 shape array, if the dummy argument has the VOLATILE attribute.  */

      if (f->sym->attr.volatile_
	  && a->expr->symtree->n.sym->as
	  && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
	  && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
	{
	  if (where)
	    gfc_error ("Assumed-shape actual argument at %L is "
		       "incompatible with the non-assumed-shape "
2954
		       "dummy argument %qs due to VOLATILE attribute",
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
		       &a->expr->where,f->sym->name);
	  return 0;
	}

      if (f->sym->attr.volatile_
	  && a->expr->ref && a->expr->ref->u.ar.type == AR_SECTION
	  && !(f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
	{
	  if (where)
	    gfc_error ("Array-section actual argument at %L is "
		       "incompatible with the non-assumed-shape "
2966
		       "dummy argument %qs due to VOLATILE attribute",
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
		       &a->expr->where,f->sym->name);
	  return 0;
	}

      /* C1233 (R1221) For an actual argument which is a pointer array, the
	 dummy argument shall be an assumed-shape or pointer array, if the
	 dummy argument has the VOLATILE attribute.  */

      if (f->sym->attr.volatile_
	  && a->expr->symtree->n.sym->attr.pointer
	  && a->expr->symtree->n.sym->as
	  && !(f->sym->as
	       && (f->sym->as->type == AS_ASSUMED_SHAPE
		   || f->sym->attr.pointer)))
	{
	  if (where)
	    gfc_error ("Pointer-array actual argument at %L requires "
		       "an assumed-shape or pointer-array dummy "
2985
		       "argument %qs due to VOLATILE attribute",
2986 2987 2988 2989
		       &a->expr->where,f->sym->name);
	  return 0;
	}

2990 2991 2992 2993
    match:
      if (a == actual)
	na = i;

2994
      new_arg[i++] = a;
2995 2996 2997 2998 2999 3000
    }

  /* Make sure missing actual arguments are optional.  */
  i = 0;
  for (f = formal; f; f = f->next, i++)
    {
3001
      if (new_arg[i] != NULL)
3002
	continue;
3003 3004 3005
      if (f->sym == NULL)
	{
	  if (where)
3006 3007
	    gfc_error ("Missing alternate return spec in subroutine call "
		       "at %L", where);
3008 3009
	  return 0;
	}
3010 3011 3012
      if (!f->sym->attr.optional)
	{
	  if (where)
3013
	    gfc_error ("Missing actual argument for argument %qs at %L",
3014 3015 3016 3017 3018 3019 3020 3021 3022
		       f->sym->name, where);
	  return 0;
	}
    }

  /* The argument lists are compatible.  We now relink a new actual
     argument list with null arguments in the right places.  The head
     of the list remains the head.  */
  for (i = 0; i < n; i++)
3023 3024
    if (new_arg[i] == NULL)
      new_arg[i] = gfc_get_actual_arglist ();
3025 3026 3027

  if (na != 0)
    {
3028 3029
      temp = *new_arg[0];
      *new_arg[0] = *actual;
3030 3031
      *actual = temp;

3032 3033 3034
      a = new_arg[0];
      new_arg[0] = new_arg[na];
      new_arg[na] = a;
3035 3036 3037
    }

  for (i = 0; i < n - 1; i++)
3038
    new_arg[i]->next = new_arg[i + 1];
3039

3040
  new_arg[i]->next = NULL;
3041 3042

  if (*ap == NULL && n > 0)
3043
    *ap = new_arg[0];
3044

3045
  /* Note the types of omitted optional arguments.  */
3046
  for (a = *ap, f = formal; a; a = a->next, f = f->next)
3047 3048 3049
    if (a->expr == NULL && a->label == NULL)
      a->missing_arg_type = f->sym->ts.type;

3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
  return 1;
}


typedef struct
{
  gfc_formal_arglist *f;
  gfc_actual_arglist *a;
}
argpair;

/* qsort comparison function for argument pairs, with the following
   order:
    - p->a->expr == NULL
    - p->a->expr->expr_type != EXPR_VARIABLE
3065
    - growing p->a->expr->symbol.  */
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096

static int
pair_cmp (const void *p1, const void *p2)
{
  const gfc_actual_arglist *a1, *a2;

  /* *p1 and *p2 are elements of the to-be-sorted array.  */
  a1 = ((const argpair *) p1)->a;
  a2 = ((const argpair *) p2)->a;
  if (!a1->expr)
    {
      if (!a2->expr)
	return 0;
      return -1;
    }
  if (!a2->expr)
    return 1;
  if (a1->expr->expr_type != EXPR_VARIABLE)
    {
      if (a2->expr->expr_type != EXPR_VARIABLE)
	return 0;
      return -1;
    }
  if (a2->expr->expr_type != EXPR_VARIABLE)
    return 1;
  return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
}


/* Given two expressions from some actual arguments, test whether they
   refer to the same expression. The analysis is conservative.
3097
   Returning false will produce no warning.  */
3098

3099
static bool
3100
compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
3101 3102 3103 3104 3105 3106 3107
{
  const gfc_ref *r1, *r2;

  if (!e1 || !e2
      || e1->expr_type != EXPR_VARIABLE
      || e2->expr_type != EXPR_VARIABLE
      || e1->symtree->n.sym != e2->symtree->n.sym)
3108
    return false;
3109 3110 3111 3112 3113

  /* TODO: improve comparison, see expr.c:show_ref().  */
  for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
    {
      if (r1->type != r2->type)
3114
	return false;
3115 3116 3117 3118
      switch (r1->type)
	{
	case REF_ARRAY:
	  if (r1->u.ar.type != r2->u.ar.type)
3119
	    return false;
3120 3121 3122
	  /* TODO: At the moment, consider only full arrays;
	     we could do better.  */
	  if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
3123
	    return false;
3124 3125 3126 3127
	  break;

	case REF_COMPONENT:
	  if (r1->u.c.component != r2->u.c.component)
3128
	    return false;
3129 3130 3131
	  break;

	case REF_SUBSTRING:
3132
	  return false;
3133 3134 3135 3136 3137 3138

	default:
	  gfc_internal_error ("compare_actual_expr(): Bad component code");
	}
    }
  if (!r1 && !r2)
3139 3140
    return true;
  return false;
3141 3142
}

3143

3144 3145 3146 3147
/* Given formal and actual argument lists that correspond to one
   another, check that identical actual arguments aren't not
   associated with some incompatible INTENTs.  */

3148
static bool
3149
check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
3150 3151 3152 3153 3154 3155
{
  sym_intent f1_intent, f2_intent;
  gfc_formal_arglist *f1;
  gfc_actual_arglist *a1;
  size_t n, i, j;
  argpair *p;
3156
  bool t = true;
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168

  n = 0;
  for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
    {
      if (f1 == NULL && a1 == NULL)
	break;
      if (f1 == NULL || a1 == NULL)
	gfc_internal_error ("check_some_aliasing(): List mismatch");
      n++;
    }
  if (n == 0)
    return t;
3169
  p = XALLOCAVEC (argpair, n);
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192

  for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
    {
      p[i].f = f1;
      p[i].a = a1;
    }

  qsort (p, n, sizeof (argpair), pair_cmp);

  for (i = 0; i < n; i++)
    {
      if (!p[i].a->expr
	  || p[i].a->expr->expr_type != EXPR_VARIABLE
	  || p[i].a->expr->ts.type == BT_PROCEDURE)
	continue;
      f1_intent = p[i].f->sym->attr.intent;
      for (j = i + 1; j < n; j++)
	{
	  /* Expected order after the sort.  */
	  if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
	    gfc_internal_error ("check_some_aliasing(): corrupted data");

	  /* Are the expression the same?  */
3193
	  if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
3194 3195 3196
	    break;
	  f2_intent = p[j].f->sym->attr.intent;
	  if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
3197 3198
	      || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
	      || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
3199
	    {
3200
	      gfc_warning (0, "Same actual argument associated with INTENT(%s) "
3201
			   "argument %qs and INTENT(%s) argument %qs at %L",
3202 3203 3204
			   gfc_intent_string (f1_intent), p[i].f->sym->name,
			   gfc_intent_string (f2_intent), p[j].f->sym->name,
			   &p[i].a->expr->where);
3205
	      t = false;
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217
	    }
	}
    }

  return t;
}


/* Given formal and actual argument lists that correspond to one
   another, check that they are compatible in the sense that intents
   are not mismatched.  */

3218
static bool
3219
check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
3220
{
3221
  sym_intent f_intent;
3222 3223 3224

  for (;; f = f->next, a = a->next)
    {
3225 3226
      gfc_expr *expr;

3227 3228 3229 3230 3231
      if (f == NULL && a == NULL)
	break;
      if (f == NULL || a == NULL)
	gfc_internal_error ("check_intents(): List mismatch");

3232 3233 3234 3235 3236 3237 3238 3239
      if (a->expr && a->expr->expr_type == EXPR_FUNCTION
	  && a->expr->value.function.isym
	  && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
	expr = a->expr->value.function.actual->expr;
      else
	expr = a->expr;

      if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
3240 3241 3242 3243
	continue;

      f_intent = f->sym->attr.intent;

3244
      if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
3245
	{
3246 3247 3248
	  if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
	       && CLASS_DATA (f->sym)->attr.class_pointer)
	      || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
3249
	    {
3250 3251
	      gfc_error ("Procedure argument at %L is local to a PURE "
			 "procedure and has the POINTER attribute",
3252
			 &expr->where);
3253
	      return false;
3254 3255
	    }
	}
3256 3257

       /* Fortran 2008, C1283.  */
3258
       if (gfc_pure (NULL) && gfc_is_coindexed (expr))
3259 3260 3261 3262 3263
	{
	  if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
	    {
	      gfc_error ("Coindexed actual argument at %L in PURE procedure "
			 "is passed to an INTENT(%s) argument",
3264
			 &expr->where, gfc_intent_string (f_intent));
3265
	      return false;
3266 3267
	    }

3268 3269 3270
	  if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
               && CLASS_DATA (f->sym)->attr.class_pointer)
              || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
3271 3272 3273
	    {
	      gfc_error ("Coindexed actual argument at %L in PURE procedure "
			 "is passed to a POINTER dummy argument",
3274
			 &expr->where);
3275
	      return false;
3276 3277 3278 3279
	    }
	}

       /* F2008, Section 12.5.2.4.  */
3280 3281
       if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
	   && gfc_is_coindexed (expr))
3282 3283
	 {
	   gfc_error ("Coindexed polymorphic actual argument at %L is passed "
3284
		      "polymorphic dummy argument %qs",
3285
			 &expr->where, f->sym->name);
3286
	   return false;
3287
	 }
3288 3289
    }

3290
  return true;
3291 3292 3293 3294 3295 3296 3297
}


/* Check how a procedure is used against its interface.  If all goes
   well, the actual argument list will also end up being properly
   sorted.  */

3298
bool
3299
gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
3300
{
3301 3302
  gfc_formal_arglist *dummy_args;

3303
  /* Warn about calls with an implicit interface.  Special case
3304
     for calling a ISO_C_BINDING because c_loc and c_funloc
3305 3306
     are pseudo-unknown.  Additionally, warn about procedures not
     explicitly declared at all if requested.  */
3307
  if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
3308
    {
3309 3310
      if (sym->ns->has_implicit_none_export && sym->attr.proc == PROC_UNKNOWN)
	{
3311
	  gfc_error ("Procedure %qs called at %L is not explicitly declared",
3312 3313 3314
		     sym->name, where);
	  return false;
	}
3315
      if (warn_implicit_interface)
3316 3317
	gfc_warning (OPT_Wimplicit_interface,
		     "Procedure %qs called with an implicit interface at %L",
3318
		     sym->name, where);
3319
      else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
3320 3321
	gfc_warning (OPT_Wimplicit_procedure,
		     "Procedure %qs called at %L is not explicitly declared",
3322 3323
		     sym->name, where);
    }
3324

3325
  if (sym->attr.if_source == IFSRC_UNKNOWN)
3326 3327
    {
      gfc_actual_arglist *a;
3328 3329 3330

      if (sym->attr.pointer)
	{
3331 3332 3333
	  gfc_error ("The pointer object %qs at %L must have an explicit "
		     "function interface or be declared as array",
		     sym->name, where);
3334
	  return false;
3335 3336 3337 3338
	}

      if (sym->attr.allocatable && !sym->attr.external)
	{
3339 3340 3341
	  gfc_error ("The allocatable object %qs at %L must have an explicit "
		     "function interface or be declared as array",
		     sym->name, where);
3342
	  return false;
3343 3344 3345 3346
	}

      if (sym->attr.allocatable)
	{
3347 3348
	  gfc_error ("Allocatable function %qs at %L must have an explicit "
		     "function interface", sym->name, where);
3349
	  return false;
3350 3351
	}

3352 3353 3354 3355 3356
      for (a = *ap; a; a = a->next)
	{
	  /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
	  if (a->name != NULL && a->name[0] != '%')
	    {
3357 3358
	      gfc_error ("Keyword argument requires explicit interface "
			 "for procedure %qs at %L", sym->name, &a->expr->where);
3359 3360
	      break;
	    }
3361

3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
	  /* TS 29113, 6.2.  */
	  if (a->expr && a->expr->ts.type == BT_ASSUMED
	      && sym->intmod_sym_id != ISOCBINDING_LOC)
	    {
	      gfc_error ("Assumed-type argument %s at %L requires an explicit "
			 "interface", a->expr->symtree->n.sym->name,
			 &a->expr->where);
	      break;
	    }

3372 3373 3374 3375 3376 3377 3378
	  /* F2008, C1303 and C1304.  */
	  if (a->expr
	      && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
	      && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
		   && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
		  || gfc_expr_attr (a->expr).lock_comp))
	    {
3379 3380 3381
	      gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
			 "component at %L requires an explicit interface for "
			 "procedure %qs", &a->expr->where, sym->name);
3382 3383
	      break;
	    }
3384 3385 3386 3387 3388

	  if (a->expr && a->expr->expr_type == EXPR_NULL
	      && a->expr->ts.type == BT_UNKNOWN)
	    {
	      gfc_error ("MOLD argument to NULL required at %L", &a->expr->where);
3389
	      return false;
3390
	    }
3391 3392 3393 3394 3395 3396 3397

	  /* TS 29113, C407b.  */
	  if (a->expr && a->expr->expr_type == EXPR_VARIABLE
	      && symbol_rank (a->expr->symtree->n.sym) == -1)
	    {
	      gfc_error ("Assumed-rank argument requires an explicit interface "
			 "at %L", &a->expr->where);
3398
	      return false;
3399
	    }
3400 3401
	}

3402
      return true;
3403 3404
    }

3405 3406 3407
  dummy_args = gfc_sym_get_dummy_args (sym);

  if (!compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental, where))
3408
    return false;
3409

3410 3411
  if (!check_intents (dummy_args, *ap))
    return false;
3412

3413
  if (warn_aliasing)
3414
    check_some_aliasing (dummy_args, *ap);
3415

3416
  return true;
3417 3418 3419
}


3420 3421 3422 3423 3424 3425 3426 3427
/* Check how a procedure pointer component is used against its interface.
   If all goes well, the actual argument list will also end up being properly
   sorted. Completely analogous to gfc_procedure_use.  */

void
gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
{
  /* Warn about calls with an implicit interface.  Special case
3428
     for calling a ISO_C_BINDING because c_loc and c_funloc
3429
     are pseudo-unknown.  */
3430
  if (warn_implicit_interface
3431 3432
      && comp->attr.if_source == IFSRC_UNKNOWN
      && !comp->attr.is_iso_c)
3433 3434
    gfc_warning (OPT_Wimplicit_interface,
		 "Procedure pointer component %qs called with an implicit "
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
		 "interface at %L", comp->name, where);

  if (comp->attr.if_source == IFSRC_UNKNOWN)
    {
      gfc_actual_arglist *a;
      for (a = *ap; a; a = a->next)
	{
	  /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
	  if (a->name != NULL && a->name[0] != '%')
	    {
3445 3446 3447
	      gfc_error ("Keyword argument requires explicit interface "
			 "for procedure pointer component %qs at %L",
			 comp->name, &a->expr->where);
3448 3449 3450 3451 3452 3453 3454
	      break;
	    }
	}

      return;
    }

3455 3456
  if (!compare_actual_formal (ap, comp->ts.interface->formal, 0,
			      comp->attr.elemental, where))
3457 3458
    return;

3459
  check_intents (comp->ts.interface->formal, *ap);
3460
  if (warn_aliasing)
3461
    check_some_aliasing (comp->ts.interface->formal, *ap);
3462 3463 3464
}


3465 3466 3467 3468 3469 3470 3471
/* Try if an actual argument list matches the formal list of a symbol,
   respecting the symbol's attributes like ELEMENTAL.  This is used for
   GENERIC resolution.  */

bool
gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
{
3472
  gfc_formal_arglist *dummy_args;
3473 3474 3475 3476
  bool r;

  gcc_assert (sym->attr.flavor == FL_PROCEDURE);

3477 3478
  dummy_args = gfc_sym_get_dummy_args (sym);

3479
  r = !sym->attr.elemental;
3480
  if (compare_actual_formal (args, dummy_args, r, !r, NULL))
3481
    {
3482
      check_intents (dummy_args, *args);
3483
      if (warn_aliasing)
3484
	check_some_aliasing (dummy_args, *args);
3485 3486 3487 3488 3489 3490 3491
      return true;
    }

  return false;
}


3492 3493 3494 3495 3496 3497
/* Given an interface pointer and an actual argument list, search for
   a formal argument list that matches the actual.  If found, returns
   a pointer to the symbol of the correct interface.  Returns NULL if
   not found.  */

gfc_symbol *
3498 3499
gfc_search_interface (gfc_interface *intr, int sub_flag,
		      gfc_actual_arglist **ap)
3500
{
3501
  gfc_symbol *elem_sym = NULL;
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
  gfc_symbol *null_sym = NULL;
  locus null_expr_loc;
  gfc_actual_arglist *a;
  bool has_null_arg = false;

  for (a = *ap; a; a = a->next)
    if (a->expr && a->expr->expr_type == EXPR_NULL
	&& a->expr->ts.type == BT_UNKNOWN)
      {
	has_null_arg = true;
	null_expr_loc = a->expr->where;
	break;
3514
      }
3515

3516 3517
  for (; intr; intr = intr->next)
    {
3518 3519
      if (intr->sym->attr.flavor == FL_DERIVED)
	continue;
3520 3521 3522 3523 3524
      if (sub_flag && intr->sym->attr.function)
	continue;
      if (!sub_flag && intr->sym->attr.subroutine)
	continue;

3525
      if (gfc_arglist_matches_symbol (ap, intr->sym))
3526
	{
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
	  if (has_null_arg && null_sym)
	    {
	      gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
			 "between specific functions %s and %s",
			 &null_expr_loc, null_sym->name, intr->sym->name);
	      return NULL;
	    }
	  else if (has_null_arg)
	    {
	      null_sym = intr->sym;
	      continue;
	    }

3540
	  /* Satisfy 12.4.4.1 such that an elemental match has lower
3541
	     weight than a non-elemental match.  */
3542 3543 3544 3545 3546 3547 3548
	  if (intr->sym->attr.elemental)
	    {
	      elem_sym = intr->sym;
	      continue;
	    }
	  return intr->sym;
	}
3549 3550
    }

3551 3552 3553
  if (null_sym)
    return null_sym;

3554
  return elem_sym ? elem_sym : NULL;
3555 3556 3557 3558 3559 3560
}


/* Do a brute force recursive search for a symbol.  */

static gfc_symtree *
3561
find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
{
  gfc_symtree * st;

  if (root->n.sym == sym)
    return root;

  st = NULL;
  if (root->left)
    st = find_symtree0 (root->left, sym);
  if (root->right && ! st)
    st = find_symtree0 (root->right, sym);
  return st;
}


/* Find a symtree for a symbol.  */

3579 3580
gfc_symtree *
gfc_find_sym_in_symtree (gfc_symbol *sym)
3581 3582 3583 3584 3585 3586 3587 3588 3589
{
  gfc_symtree *st;
  gfc_namespace *ns;

  /* First try to find it by name.  */
  gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
  if (st && st->n.sym == sym)
    return st;

3590
  /* If it's been renamed, resort to a brute-force search.  */
3591 3592 3593 3594 3595 3596
  /* TODO: avoid having to do this search.  If the symbol doesn't exist
     in the symtree for the current namespace, it should probably be added.  */
  for (ns = gfc_current_ns; ns; ns = ns->parent)
    {
      st = find_symtree0 (ns->sym_root, sym);
      if (st)
3597
	return st;
3598
    }
3599
  gfc_internal_error ("Unable to find symbol %qs", sym->name);
3600
  /* Not reached.  */
3601 3602 3603
}


3604 3605 3606
/* See if the arglist to an operator-call contains a derived-type argument
   with a matching type-bound operator.  If so, return the matching specific
   procedure defined as operator-target as well as the base-object to use
3607 3608
   (which is the found derived-type argument with operator).  The generic
   name, if any, is transmitted to the final expression via 'gname'.  */
3609 3610 3611 3612

static gfc_typebound_proc*
matching_typebound_op (gfc_expr** tb_base,
		       gfc_actual_arglist* args,
3613 3614
		       gfc_intrinsic_op op, const char* uop,
		       const char ** gname)
3615 3616 3617 3618
{
  gfc_actual_arglist* base;

  for (base = args; base; base = base->next)
3619
    if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
3620 3621 3622
      {
	gfc_typebound_proc* tb;
	gfc_symbol* derived;
3623
	bool result;
3624

3625 3626 3627 3628
	while (base->expr->expr_type == EXPR_OP
	       && base->expr->value.op.op == INTRINSIC_PARENTHESES)
	  base->expr = base->expr->value.op.op1;

3629
	if (base->expr->ts.type == BT_CLASS)
3630
	  {
3631 3632
	    if (CLASS_DATA (base->expr) == NULL
		|| !gfc_expr_attr (base->expr).class_ok)
3633 3634 3635
	      continue;
	    derived = CLASS_DATA (base->expr)->ts.u.derived;
	  }
3636 3637
	else
	  derived = base->expr->ts.u.derived;
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657

	if (op == INTRINSIC_USER)
	  {
	    gfc_symtree* tb_uop;

	    gcc_assert (uop);
	    tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
						 false, NULL);

	    if (tb_uop)
	      tb = tb_uop->n.tb;
	    else
	      tb = NULL;
	  }
	else
	  tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
						false, NULL);

	/* This means we hit a PRIVATE operator which is use-associated and
	   should thus not be seen.  */
3658
	if (!result)
3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688
	  tb = NULL;

	/* Look through the super-type hierarchy for a matching specific
	   binding.  */
	for (; tb; tb = tb->overridden)
	  {
	    gfc_tbp_generic* g;

	    gcc_assert (tb->is_generic);
	    for (g = tb->u.generic; g; g = g->next)
	      {
		gfc_symbol* target;
		gfc_actual_arglist* argcopy;
		bool matches;

		gcc_assert (g->specific);
		if (g->specific->error)
		  continue;

		target = g->specific->u.specific->n.sym;

		/* Check if this arglist matches the formal.  */
		argcopy = gfc_copy_actual_arglist (args);
		matches = gfc_arglist_matches_symbol (&argcopy, target);
		gfc_free_actual_arglist (argcopy);

		/* Return if we found a match.  */
		if (matches)
		  {
		    *tb_base = base->expr;
3689
		    *gname = g->specific_st->name;
3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707
		    return g->specific;
		  }
	      }
	  }
      }

  return NULL;
}


/* For the 'actual arglist' of an operator call and a specific typebound
   procedure that has been found the target of a type-bound operator, build the
   appropriate EXPR_COMPCALL and resolve it.  We take this indirection over
   type-bound procedures rather than resolving type-bound operators 'directly'
   so that we can reuse the existing logic.  */

static void
build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
3708 3709
			     gfc_expr* base, gfc_typebound_proc* target,
			     const char *gname)
3710 3711 3712
{
  e->expr_type = EXPR_COMPCALL;
  e->value.compcall.tbp = target;
3713
  e->value.compcall.name = gname ? gname : "$op";
3714 3715 3716 3717
  e->value.compcall.actual = actual;
  e->value.compcall.base_object = base;
  e->value.compcall.ignore_pass = 1;
  e->value.compcall.assign = 0;
3718 3719 3720 3721 3722 3723 3724 3725
  if (e->ts.type == BT_UNKNOWN
	&& target->function)
    {
      if (target->is_generic)
	e->ts = target->u.generic->specific->u.specific->n.sym->ts;
      else
	e->ts = target->u.specific->n.sym->ts;
    }
3726 3727 3728
}


3729 3730
/* This subroutine is called when an expression is being resolved.
   The expression node in question is either a user defined operator
3731
   or an intrinsic operator with arguments that aren't compatible
3732 3733 3734
   with the operator.  This subroutine builds an actual argument list
   corresponding to the operands, then searches for a compatible
   interface.  If one is found, the expression node is replaced with
3735 3736
   the appropriate function call. We use the 'match' enum to specify
   whether a replacement has been made or not, or if an error occurred.  */
3737

3738 3739
match
gfc_extend_expr (gfc_expr *e)
3740 3741 3742 3743 3744 3745
{
  gfc_actual_arglist *actual;
  gfc_symbol *sym;
  gfc_namespace *ns;
  gfc_user_op *uop;
  gfc_intrinsic_op i;
3746
  const char *gname;
3747 3748
  gfc_typebound_proc* tbo;
  gfc_expr* tb_base;
3749 3750 3751 3752

  sym = NULL;

  actual = gfc_get_actual_arglist ();
3753
  actual->expr = e->value.op.op1;
3754

3755
  gname = NULL;
3756

3757
  if (e->value.op.op2 != NULL)
3758 3759
    {
      actual->next = gfc_get_actual_arglist ();
3760
      actual->next->expr = e->value.op.op2;
3761 3762
    }

3763
  i = fold_unary_intrinsic (e->value.op.op);
3764

3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
  /* See if we find a matching type-bound operator.  */
  if (i == INTRINSIC_USER)
    tbo = matching_typebound_op (&tb_base, actual,
				  i, e->value.op.uop->name, &gname);
  else
    switch (i)
      {
#define CHECK_OS_COMPARISON(comp) \
  case INTRINSIC_##comp: \
  case INTRINSIC_##comp##_OS: \
    tbo = matching_typebound_op (&tb_base, actual, \
				 INTRINSIC_##comp, NULL, &gname); \
    if (!tbo) \
      tbo = matching_typebound_op (&tb_base, actual, \
				   INTRINSIC_##comp##_OS, NULL, &gname); \
    break;
	CHECK_OS_COMPARISON(EQ)
	CHECK_OS_COMPARISON(NE)
	CHECK_OS_COMPARISON(GT)
	CHECK_OS_COMPARISON(GE)
	CHECK_OS_COMPARISON(LT)
	CHECK_OS_COMPARISON(LE)
#undef CHECK_OS_COMPARISON

	default:
	  tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
	  break;
      }

  /* If there is a matching typebound-operator, replace the expression with
      a call to it and succeed.  */
  if (tbo)
    {
      gcc_assert (tb_base);
      build_compcall_for_operator (e, actual, tb_base, tbo, gname);

      if (!gfc_resolve_expr (e))
	return MATCH_ERROR;
      else
	return MATCH_YES;
    }
 
3807 3808 3809 3810
  if (i == INTRINSIC_USER)
    {
      for (ns = gfc_current_ns; ns; ns = ns->parent)
	{
3811
	  uop = gfc_find_uop (e->value.op.uop->name, ns);
3812 3813 3814
	  if (uop == NULL)
	    continue;

3815
	  sym = gfc_search_interface (uop->op, 0, &actual);
3816 3817 3818 3819 3820 3821 3822 3823
	  if (sym != NULL)
	    break;
	}
    }
  else
    {
      for (ns = gfc_current_ns; ns; ns = ns->parent)
	{
3824 3825 3826 3827
	  /* Due to the distinction between '==' and '.eq.' and friends, one has
	     to check if either is defined.  */
	  switch (i)
	    {
3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
#define CHECK_OS_COMPARISON(comp) \
  case INTRINSIC_##comp: \
  case INTRINSIC_##comp##_OS: \
    sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
    if (!sym) \
      sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
    break;
	      CHECK_OS_COMPARISON(EQ)
	      CHECK_OS_COMPARISON(NE)
	      CHECK_OS_COMPARISON(GT)
	      CHECK_OS_COMPARISON(GE)
	      CHECK_OS_COMPARISON(LT)
	      CHECK_OS_COMPARISON(LE)
#undef CHECK_OS_COMPARISON
3842 3843

	      default:
3844
		sym = gfc_search_interface (ns->op[i], 0, &actual);
3845 3846
	    }

3847 3848 3849 3850 3851
	  if (sym != NULL)
	    break;
	}
    }

3852 3853 3854
  /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
     found rather than just taking the first one and not checking further.  */

3855 3856
  if (sym == NULL)
    {
3857
      /* Don't use gfc_free_actual_arglist().  */
3858
      free (actual->next);
3859
      free (actual);
3860
      return MATCH_NO;
3861 3862 3863 3864
    }

  /* Change the expression node to a function call.  */
  e->expr_type = EXPR_FUNCTION;
3865
  e->symtree = gfc_find_sym_in_symtree (sym);
3866
  e->value.function.actual = actual;
3867 3868
  e->value.function.esym = NULL;
  e->value.function.isym = NULL;
3869
  e->value.function.name = NULL;
3870
  e->user_operator = 1;
3871

3872
  if (!gfc_resolve_expr (e))
3873
    return MATCH_ERROR;
3874

3875
  return MATCH_YES;
3876 3877 3878
}


3879 3880 3881
/* Tries to replace an assignment code node with a subroutine call to the
   subroutine associated with the assignment operator. Return true if the node
   was replaced. On false, no error is generated.  */
3882

3883
bool
3884
gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
3885 3886
{
  gfc_actual_arglist *actual;
3887 3888 3889 3890
  gfc_expr *lhs, *rhs, *tb_base;
  gfc_symbol *sym = NULL;
  const char *gname = NULL;
  gfc_typebound_proc* tbo;
3891

3892
  lhs = c->expr1;
3893 3894 3895
  rhs = c->expr2;

  /* Don't allow an intrinsic assignment to be replaced.  */
3896
  if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
3897
      && (rhs->rank == 0 || rhs->rank == lhs->rank)
3898
      && (lhs->ts.type == rhs->ts.type
3899
	  || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
3900
    return false;
3901 3902 3903 3904 3905 3906 3907

  actual = gfc_get_actual_arglist ();
  actual->expr = lhs;

  actual->next = gfc_get_actual_arglist ();
  actual->next->expr = rhs;

3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
  /* TODO: Ambiguity-check, see above for gfc_extend_expr.  */

  /* See if we find a matching type-bound assignment.  */
  tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
			       NULL, &gname);

  if (tbo)
    {
      /* Success: Replace the expression with a type-bound call.  */
      gcc_assert (tb_base);
      c->expr1 = gfc_get_expr ();
      build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
      c->expr1->value.compcall.assign = 1;
      c->expr1->where = c->loc;
      c->expr2 = NULL;
      c->op = EXEC_COMPCALL;
      return true;
    }
3926

3927
  /* See if we find an 'ordinary' (non-typebound) assignment procedure.  */
3928 3929
  for (; ns; ns = ns->parent)
    {
3930
      sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
3931 3932 3933 3934
      if (sym != NULL)
	break;
    }

3935
  if (sym)
3936
    {
3937 3938 3939 3940 3941 3942 3943
      /* Success: Replace the assignment with the call.  */
      c->op = EXEC_ASSIGN_CALL;
      c->symtree = gfc_find_sym_in_symtree (sym);
      c->expr1 = NULL;
      c->expr2 = NULL;
      c->ext.actual = actual;
      return true;
3944 3945
    }

3946 3947 3948 3949
  /* Failure: No assignment procedure found.  */
  free (actual->next);
  free (actual);
  return false;
3950 3951 3952 3953 3954 3955 3956
}


/* Make sure that the interface just parsed is not already present in
   the given interface list.  Ambiguity isn't checked yet since module
   procedures can be present without interfaces.  */

3957
bool
3958
gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
3959 3960 3961 3962 3963
{
  gfc_interface *ip;

  for (ip = base; ip; ip = ip->next)
    {
3964
      if (ip->sym == new_sym)
3965
	{
3966
	  gfc_error ("Entity %qs at %L is already present in the interface",
3967
		     new_sym->name, &loc);
3968
	  return false;
3969 3970 3971
	}
    }

3972
  return true;
3973 3974 3975 3976 3977
}


/* Add a symbol to the current interface.  */

3978
bool
3979
gfc_add_interface (gfc_symbol *new_sym)
3980 3981 3982 3983 3984 3985 3986 3987
{
  gfc_interface **head, *intr;
  gfc_namespace *ns;
  gfc_symbol *sym;

  switch (current_interface.type)
    {
    case INTERFACE_NAMELESS:
Tobias Burnus committed
3988
    case INTERFACE_ABSTRACT:
3989
      return true;
3990 3991 3992

    case INTERFACE_INTRINSIC_OP:
      for (ns = current_interface.ns; ns; ns = ns->parent)
3993 3994 3995 3996
	switch (current_interface.op)
	  {
	    case INTRINSIC_EQ:
	    case INTRINSIC_EQ_OS:
3997 3998 3999 4000 4001
	      if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym, 
					    gfc_current_locus)
	          || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS], 
					       new_sym, gfc_current_locus))
		return false;
4002 4003 4004 4005
	      break;

	    case INTRINSIC_NE:
	    case INTRINSIC_NE_OS:
4006 4007 4008 4009 4010
	      if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym, 
					    gfc_current_locus)
	          || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS], 
					       new_sym, gfc_current_locus))
		return false;
4011 4012 4013 4014
	      break;

	    case INTRINSIC_GT:
	    case INTRINSIC_GT_OS:
4015 4016 4017 4018 4019
	      if (!gfc_check_new_interface (ns->op[INTRINSIC_GT], 
					    new_sym, gfc_current_locus)
	          || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS], 
					       new_sym, gfc_current_locus))
		return false;
4020 4021 4022 4023
	      break;

	    case INTRINSIC_GE:
	    case INTRINSIC_GE_OS:
4024 4025 4026 4027 4028
	      if (!gfc_check_new_interface (ns->op[INTRINSIC_GE], 
					    new_sym, gfc_current_locus)
	          || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS], 
					       new_sym, gfc_current_locus))
		return false;
4029 4030 4031 4032
	      break;

	    case INTRINSIC_LT:
	    case INTRINSIC_LT_OS:
4033 4034 4035 4036 4037
	      if (!gfc_check_new_interface (ns->op[INTRINSIC_LT], 
					    new_sym, gfc_current_locus)
	          || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS], 
					       new_sym, gfc_current_locus))
		return false;
4038 4039 4040 4041
	      break;

	    case INTRINSIC_LE:
	    case INTRINSIC_LE_OS:
4042 4043 4044 4045 4046
	      if (!gfc_check_new_interface (ns->op[INTRINSIC_LE], 
					    new_sym, gfc_current_locus)
	          || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS], 
					       new_sym, gfc_current_locus))
		return false;
4047 4048 4049
	      break;

	    default:
4050 4051 4052
	      if (!gfc_check_new_interface (ns->op[current_interface.op], 
					    new_sym, gfc_current_locus))
		return false;
4053
	  }
4054

4055
      head = &current_interface.ns->op[current_interface.op];
4056 4057 4058 4059 4060 4061 4062 4063 4064
      break;

    case INTERFACE_GENERIC:
      for (ns = current_interface.ns; ns; ns = ns->parent)
	{
	  gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
	  if (sym == NULL)
	    continue;

4065 4066 4067
	  if (!gfc_check_new_interface (sym->generic, 
					new_sym, gfc_current_locus))
	    return false;
4068 4069 4070 4071 4072 4073
	}

      head = &current_interface.sym->generic;
      break;

    case INTERFACE_USER_OP:
4074 4075 4076
      if (!gfc_check_new_interface (current_interface.uop->op, 
				    new_sym, gfc_current_locus))
	return false;
4077

4078
      head = &current_interface.uop->op;
4079 4080 4081 4082 4083 4084 4085
      break;

    default:
      gfc_internal_error ("gfc_add_interface(): Bad interface type");
    }

  intr = gfc_get_interface ();
4086
  intr->sym = new_sym;
4087
  intr->where = gfc_current_locus;
4088 4089 4090 4091

  intr->next = *head;
  *head = intr;

4092
  return true;
4093 4094 4095
}


4096 4097 4098 4099 4100 4101
gfc_interface *
gfc_current_interface_head (void)
{
  switch (current_interface.type)
    {
      case INTERFACE_INTRINSIC_OP:
4102
	return current_interface.ns->op[current_interface.op];
4103 4104 4105 4106 4107 4108 4109
	break;

      case INTERFACE_GENERIC:
	return current_interface.sym->generic;
	break;

      case INTERFACE_USER_OP:
4110
	return current_interface.uop->op;
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124
	break;

      default:
	gcc_unreachable ();
    }
}


void
gfc_set_current_interface_head (gfc_interface *i)
{
  switch (current_interface.type)
    {
      case INTERFACE_INTRINSIC_OP:
4125
	current_interface.ns->op[current_interface.op] = i;
4126 4127 4128 4129 4130 4131 4132
	break;

      case INTERFACE_GENERIC:
	current_interface.sym->generic = i;
	break;

      case INTERFACE_USER_OP:
4133
	current_interface.uop->op = i;
4134 4135 4136 4137 4138 4139 4140 4141
	break;

      default:
	gcc_unreachable ();
    }
}


4142 4143 4144 4145
/* Gets rid of a formal argument list.  We do not free symbols.
   Symbols are freed when a namespace is freed.  */

void
4146
gfc_free_formal_arglist (gfc_formal_arglist *p)
4147 4148 4149 4150 4151 4152
{
  gfc_formal_arglist *q;

  for (; p; p = q)
    {
      q = p->next;
4153
      free (p);
4154 4155
    }
}
4156 4157


4158 4159
/* Check that it is ok for the type-bound procedure 'proc' to override the
   procedure 'old', cf. F08:4.5.7.3.  */
4160

4161
bool
4162 4163 4164
gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
{
  locus where;
4165
  gfc_symbol *proc_target, *old_target;
4166
  unsigned proc_pass_arg, old_pass_arg, argpos;
4167 4168 4169
  gfc_formal_arglist *proc_formal, *old_formal;
  bool check_type;
  char err[200];
4170 4171 4172 4173 4174 4175 4176

  /* This procedure should only be called for non-GENERIC proc.  */
  gcc_assert (!proc->n.tb->is_generic);

  /* If the overwritten procedure is GENERIC, this is an error.  */
  if (old->n.tb->is_generic)
    {
4177
      gfc_error ("Can't overwrite GENERIC %qs at %L",
4178
		 old->name, &proc->n.tb->where);
4179
      return false;
4180 4181 4182 4183 4184 4185 4186 4187 4188
    }

  where = proc->n.tb->where;
  proc_target = proc->n.tb->u.specific->n.sym;
  old_target = old->n.tb->u.specific->n.sym;

  /* Check that overridden binding is not NON_OVERRIDABLE.  */
  if (old->n.tb->non_overridable)
    {
4189
      gfc_error ("%qs at %L overrides a procedure binding declared"
4190
		 " NON_OVERRIDABLE", proc->name, &where);
4191
      return false;
4192 4193 4194 4195 4196
    }

  /* It's an error to override a non-DEFERRED procedure with a DEFERRED one.  */
  if (!old->n.tb->deferred && proc->n.tb->deferred)
    {
4197
      gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
4198
		 " non-DEFERRED binding", proc->name, &where);
4199
      return false;
4200 4201 4202 4203 4204
    }

  /* If the overridden binding is PURE, the overriding must be, too.  */
  if (old_target->attr.pure && !proc_target->attr.pure)
    {
4205
      gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
4206
		 proc->name, &where);
4207
      return false;
4208 4209 4210 4211 4212 4213
    }

  /* If the overridden binding is ELEMENTAL, the overriding must be, too.  If it
     is not, the overriding must not be either.  */
  if (old_target->attr.elemental && !proc_target->attr.elemental)
    {
4214
      gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
4215
		 " ELEMENTAL", proc->name, &where);
4216
      return false;
4217 4218 4219
    }
  if (!old_target->attr.elemental && proc_target->attr.elemental)
    {
4220
      gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
4221
		 " be ELEMENTAL, either", proc->name, &where);
4222
      return false;
4223 4224 4225 4226 4227 4228
    }

  /* If the overridden binding is a SUBROUTINE, the overriding must also be a
     SUBROUTINE.  */
  if (old_target->attr.subroutine && !proc_target->attr.subroutine)
    {
4229
      gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
4230
		 " SUBROUTINE", proc->name, &where);
4231
      return false;
4232 4233 4234 4235 4236 4237 4238 4239
    }

  /* If the overridden binding is a FUNCTION, the overriding must also be a
     FUNCTION and have the same characteristics.  */
  if (old_target->attr.function)
    {
      if (!proc_target->attr.function)
	{
4240
	  gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
4241
		     " FUNCTION", proc->name, &where);
4242
	  return false;
4243
	}
4244

4245 4246
      if (!check_result_characteristics (proc_target, old_target, err, 
					 sizeof(err)))
4247
	{
4248
	  gfc_error ("Result mismatch for the overriding procedure "
4249
		     "%qs at %L: %s", proc->name, &where, err);
4250
	  return false;
4251
	}
4252 4253 4254 4255 4256 4257 4258
    }

  /* If the overridden binding is PUBLIC, the overriding one must not be
     PRIVATE.  */
  if (old->n.tb->access == ACCESS_PUBLIC
      && proc->n.tb->access == ACCESS_PRIVATE)
    {
4259
      gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
4260
		 " PRIVATE", proc->name, &where);
4261
      return false;
4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273
    }

  /* Compare the formal argument lists of both procedures.  This is also abused
     to find the position of the passed-object dummy arguments of both
     bindings as at least the overridden one might not yet be resolved and we
     need those positions in the check below.  */
  proc_pass_arg = old_pass_arg = 0;
  if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
    proc_pass_arg = 1;
  if (!old->n.tb->nopass && !old->n.tb->pass_arg)
    old_pass_arg = 1;
  argpos = 1;
4274 4275 4276
  proc_formal = gfc_sym_get_dummy_args (proc_target);
  old_formal = gfc_sym_get_dummy_args (old_target);
  for ( ; proc_formal && old_formal;
4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288
       proc_formal = proc_formal->next, old_formal = old_formal->next)
    {
      if (proc->n.tb->pass_arg
	  && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
	proc_pass_arg = argpos;
      if (old->n.tb->pass_arg
	  && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
	old_pass_arg = argpos;

      /* Check that the names correspond.  */
      if (strcmp (proc_formal->sym->name, old_formal->sym->name))
	{
4289
	  gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
4290 4291 4292
		     " to match the corresponding argument of the overridden"
		     " procedure", proc_formal->sym->name, proc->name, &where,
		     old_formal->sym->name);
4293
	  return false;
4294 4295
	}

4296
      check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
4297 4298
      if (!check_dummy_characteristics (proc_formal->sym, old_formal->sym, 
					check_type, err, sizeof(err)))
4299
	{
4300
	  gfc_error ("Argument mismatch for the overriding procedure "
4301
		     "%qs at %L: %s", proc->name, &where, err);
4302
	  return false;
4303 4304 4305 4306 4307 4308
	}

      ++argpos;
    }
  if (proc_formal || old_formal)
    {
4309
      gfc_error ("%qs at %L must have the same number of formal arguments as"
4310
		 " the overridden procedure", proc->name, &where);
4311
      return false;
4312 4313 4314 4315 4316 4317
    }

  /* If the overridden binding is NOPASS, the overriding one must also be
     NOPASS.  */
  if (old->n.tb->nopass && !proc->n.tb->nopass)
    {
4318
      gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
4319
		 " NOPASS", proc->name, &where);
4320
      return false;
4321 4322 4323 4324 4325 4326 4327 4328
    }

  /* If the overridden binding is PASS(x), the overriding one must also be
     PASS and the passed-object dummy arguments must correspond.  */
  if (!old->n.tb->nopass)
    {
      if (proc->n.tb->nopass)
	{
4329
	  gfc_error ("%qs at %L overrides a binding with PASS and must also be"
4330
		     " PASS", proc->name, &where);
4331
	  return false;
4332 4333 4334 4335
	}

      if (proc_pass_arg != old_pass_arg)
	{
4336
	  gfc_error ("Passed-object dummy argument of %qs at %L must be at"
4337 4338
		     " the same position as the passed-object dummy argument of"
		     " the overridden procedure", proc->name, &where);
4339
	  return false;
4340 4341 4342
	}
    }

4343
  return true;
4344
}