xref: /netbsd-src/external/gpl3/gcc/dist/gcc/fortran/interface.cc (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 /* Deal with interfaces.
2    Copyright (C) 2000-2022 Free Software Foundation, Inc.
3    Contributed by Andy Vaught
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 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 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 
22 /* Deal with interfaces.  An explicit interface is represented as a
23    singly linked list of formal argument structures attached to the
24    relevant symbols.  For an implicit interface, the arguments don't
25    point to symbols.  Explicit interfaces point to namespaces that
26    contain the symbols within that interface.
27 
28    Implicit interfaces are linked together in a singly linked list
29    along the next_if member of symbol nodes.  Since a particular
30    symbol can only have a single explicit interface, the symbol cannot
31    be part of multiple lists and a single next-member suffices.
32 
33    This is not the case for general classes, though.  An operator
34    definition is independent of just about all other uses and has it's
35    own head pointer.
36 
37    Nameless interfaces:
38      Nameless interfaces create symbols with explicit interfaces within
39      the current namespace.  They are otherwise unlinked.
40 
41    Generic interfaces:
42      The generic name points to a linked list of symbols.  Each symbol
43      has an explicit interface.  Each explicit interface has its own
44      namespace containing the arguments.  Module procedures are symbols in
45      which the interface is added later when the module procedure is parsed.
46 
47    User operators:
48      User-defined operators are stored in a their own set of symtrees
49      separate from regular symbols.  The symtrees point to gfc_user_op
50      structures which in turn head up a list of relevant interfaces.
51 
52    Extended intrinsics and assignment:
53      The head of these interface lists are stored in the containing namespace.
54 
55    Implicit interfaces:
56      An implicit interface is represented as a singly linked list of
57      formal argument list structures that don't point to any symbol
58      nodes -- they just contain types.
59 
60 
61    When a subprogram is defined, the program unit's name points to an
62    interface as usual, but the link to the namespace is NULL and the
63    formal argument list points to symbols within the same namespace as
64    the program unit name.  */
65 
66 #include "config.h"
67 #include "system.h"
68 #include "coretypes.h"
69 #include "options.h"
70 #include "gfortran.h"
71 #include "match.h"
72 #include "arith.h"
73 
74 /* The current_interface structure holds information about the
75    interface currently being parsed.  This structure is saved and
76    restored during recursive interfaces.  */
77 
78 gfc_interface_info current_interface;
79 
80 
81 /* Free a singly linked list of gfc_interface structures.  */
82 
83 void
gfc_free_interface(gfc_interface * intr)84 gfc_free_interface (gfc_interface *intr)
85 {
86   gfc_interface *next;
87 
88   for (; intr; intr = next)
89     {
90       next = intr->next;
91       free (intr);
92     }
93 }
94 
95 
96 /* Change the operators unary plus and minus into binary plus and
97    minus respectively, leaving the rest unchanged.  */
98 
99 static gfc_intrinsic_op
fold_unary_intrinsic(gfc_intrinsic_op op)100 fold_unary_intrinsic (gfc_intrinsic_op op)
101 {
102   switch (op)
103     {
104     case INTRINSIC_UPLUS:
105       op = INTRINSIC_PLUS;
106       break;
107     case INTRINSIC_UMINUS:
108       op = INTRINSIC_MINUS;
109       break;
110     default:
111       break;
112     }
113 
114   return op;
115 }
116 
117 
118 /* Return the operator depending on the DTIO moded string.  Note that
119    these are not operators in the normal sense and so have been placed
120    beyond GFC_INTRINSIC_END in gfortran.h:enum gfc_intrinsic_op.  */
121 
122 static gfc_intrinsic_op
dtio_op(char * mode)123 dtio_op (char* mode)
124 {
125   if (strcmp (mode, "formatted") == 0)
126     return INTRINSIC_FORMATTED;
127   if (strcmp (mode, "unformatted") == 0)
128     return INTRINSIC_UNFORMATTED;
129   return INTRINSIC_NONE;
130 }
131 
132 
133 /* Match a generic specification.  Depending on which type of
134    interface is found, the 'name' or 'op' pointers may be set.
135    This subroutine doesn't return MATCH_NO.  */
136 
137 match
gfc_match_generic_spec(interface_type * type,char * name,gfc_intrinsic_op * op)138 gfc_match_generic_spec (interface_type *type,
139 			char *name,
140 			gfc_intrinsic_op *op)
141 {
142   char buffer[GFC_MAX_SYMBOL_LEN + 1];
143   match m;
144   gfc_intrinsic_op i;
145 
146   if (gfc_match (" assignment ( = )") == MATCH_YES)
147     {
148       *type = INTERFACE_INTRINSIC_OP;
149       *op = INTRINSIC_ASSIGN;
150       return MATCH_YES;
151     }
152 
153   if (gfc_match (" operator ( %o )", &i) == MATCH_YES)
154     {				/* Operator i/f */
155       *type = INTERFACE_INTRINSIC_OP;
156       *op = fold_unary_intrinsic (i);
157       return MATCH_YES;
158     }
159 
160   *op = INTRINSIC_NONE;
161   if (gfc_match (" operator ( ") == MATCH_YES)
162     {
163       m = gfc_match_defined_op_name (buffer, 1);
164       if (m == MATCH_NO)
165 	goto syntax;
166       if (m != MATCH_YES)
167 	return MATCH_ERROR;
168 
169       m = gfc_match_char (')');
170       if (m == MATCH_NO)
171 	goto syntax;
172       if (m != MATCH_YES)
173 	return MATCH_ERROR;
174 
175       strcpy (name, buffer);
176       *type = INTERFACE_USER_OP;
177       return MATCH_YES;
178     }
179 
180   if (gfc_match (" read ( %n )", buffer) == MATCH_YES)
181     {
182       *op = dtio_op (buffer);
183       if (*op == INTRINSIC_FORMATTED)
184 	{
185 	  strcpy (name, gfc_code2string (dtio_procs, DTIO_RF));
186 	  *type = INTERFACE_DTIO;
187 	}
188       if (*op == INTRINSIC_UNFORMATTED)
189 	{
190 	  strcpy (name, gfc_code2string (dtio_procs, DTIO_RUF));
191 	  *type = INTERFACE_DTIO;
192 	}
193       if (*op != INTRINSIC_NONE)
194 	return MATCH_YES;
195     }
196 
197   if (gfc_match (" write ( %n )", buffer) == MATCH_YES)
198     {
199       *op = dtio_op (buffer);
200       if (*op == INTRINSIC_FORMATTED)
201 	{
202 	  strcpy (name, gfc_code2string (dtio_procs, DTIO_WF));
203 	  *type = INTERFACE_DTIO;
204 	}
205       if (*op == INTRINSIC_UNFORMATTED)
206 	{
207 	  strcpy (name, gfc_code2string (dtio_procs, DTIO_WUF));
208 	  *type = INTERFACE_DTIO;
209 	}
210       if (*op != INTRINSIC_NONE)
211 	return MATCH_YES;
212     }
213 
214   if (gfc_match_name (buffer) == MATCH_YES)
215     {
216       strcpy (name, buffer);
217       *type = INTERFACE_GENERIC;
218       return MATCH_YES;
219     }
220 
221   *type = INTERFACE_NAMELESS;
222   return MATCH_YES;
223 
224 syntax:
225   gfc_error ("Syntax error in generic specification at %C");
226   return MATCH_ERROR;
227 }
228 
229 
230 /* Match one of the five F95 forms of an interface statement.  The
231    matcher for the abstract interface follows.  */
232 
233 match
gfc_match_interface(void)234 gfc_match_interface (void)
235 {
236   char name[GFC_MAX_SYMBOL_LEN + 1];
237   interface_type type;
238   gfc_symbol *sym;
239   gfc_intrinsic_op op;
240   match m;
241 
242   m = gfc_match_space ();
243 
244   if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
245     return MATCH_ERROR;
246 
247   /* If we're not looking at the end of the statement now, or if this
248      is not a nameless interface but we did not see a space, punt.  */
249   if (gfc_match_eos () != MATCH_YES
250       || (type != INTERFACE_NAMELESS && m != MATCH_YES))
251     {
252       gfc_error ("Syntax error: Trailing garbage in INTERFACE statement "
253 		 "at %C");
254       return MATCH_ERROR;
255     }
256 
257   current_interface.type = type;
258 
259   switch (type)
260     {
261     case INTERFACE_DTIO:
262     case INTERFACE_GENERIC:
263       if (gfc_get_symbol (name, NULL, &sym))
264 	return MATCH_ERROR;
265 
266       if (!sym->attr.generic
267 	  && !gfc_add_generic (&sym->attr, sym->name, NULL))
268 	return MATCH_ERROR;
269 
270       if (sym->attr.dummy)
271 	{
272 	  gfc_error ("Dummy procedure %qs at %C cannot have a "
273 		     "generic interface", sym->name);
274 	  return MATCH_ERROR;
275 	}
276 
277       current_interface.sym = gfc_new_block = sym;
278       break;
279 
280     case INTERFACE_USER_OP:
281       current_interface.uop = gfc_get_uop (name);
282       break;
283 
284     case INTERFACE_INTRINSIC_OP:
285       current_interface.op = op;
286       break;
287 
288     case INTERFACE_NAMELESS:
289     case INTERFACE_ABSTRACT:
290       break;
291     }
292 
293   return MATCH_YES;
294 }
295 
296 
297 
298 /* Match a F2003 abstract interface.  */
299 
300 match
gfc_match_abstract_interface(void)301 gfc_match_abstract_interface (void)
302 {
303   match m;
304 
305   if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT INTERFACE at %C"))
306     return MATCH_ERROR;
307 
308   m = gfc_match_eos ();
309 
310   if (m != MATCH_YES)
311     {
312       gfc_error ("Syntax error in ABSTRACT INTERFACE statement at %C");
313       return MATCH_ERROR;
314     }
315 
316   current_interface.type = INTERFACE_ABSTRACT;
317 
318   return m;
319 }
320 
321 
322 /* Match the different sort of generic-specs that can be present after
323    the END INTERFACE itself.  */
324 
325 match
gfc_match_end_interface(void)326 gfc_match_end_interface (void)
327 {
328   char name[GFC_MAX_SYMBOL_LEN + 1];
329   interface_type type;
330   gfc_intrinsic_op op;
331   match m;
332 
333   m = gfc_match_space ();
334 
335   if (gfc_match_generic_spec (&type, name, &op) == MATCH_ERROR)
336     return MATCH_ERROR;
337 
338   /* If we're not looking at the end of the statement now, or if this
339      is not a nameless interface but we did not see a space, punt.  */
340   if (gfc_match_eos () != MATCH_YES
341       || (type != INTERFACE_NAMELESS && m != MATCH_YES))
342     {
343       gfc_error ("Syntax error: Trailing garbage in END INTERFACE "
344 		 "statement at %C");
345       return MATCH_ERROR;
346     }
347 
348   m = MATCH_YES;
349 
350   switch (current_interface.type)
351     {
352     case INTERFACE_NAMELESS:
353     case INTERFACE_ABSTRACT:
354       if (type != INTERFACE_NAMELESS)
355 	{
356 	  gfc_error ("Expected a nameless interface at %C");
357 	  m = MATCH_ERROR;
358 	}
359 
360       break;
361 
362     case INTERFACE_INTRINSIC_OP:
363       if (type != current_interface.type || op != current_interface.op)
364 	{
365 
366 	  if (current_interface.op == INTRINSIC_ASSIGN)
367 	    {
368 	      m = MATCH_ERROR;
369 	      gfc_error ("Expected %<END INTERFACE ASSIGNMENT (=)%> at %C");
370 	    }
371 	  else
372 	    {
373 	      const char *s1, *s2;
374 	      s1 = gfc_op2string (current_interface.op);
375 	      s2 = gfc_op2string (op);
376 
377 	      /* The following if-statements are used to enforce C1202
378 		 from F2003.  */
379 	      if ((strcmp(s1, "==") == 0 && strcmp (s2, ".eq.") == 0)
380 		  || (strcmp(s1, ".eq.") == 0 && strcmp (s2, "==") == 0))
381 		break;
382 	      if ((strcmp(s1, "/=") == 0 && strcmp (s2, ".ne.") == 0)
383 		  || (strcmp(s1, ".ne.") == 0 && strcmp (s2, "/=") == 0))
384 		break;
385 	      if ((strcmp(s1, "<=") == 0 && strcmp (s2, ".le.") == 0)
386 		  || (strcmp(s1, ".le.") == 0 && strcmp (s2, "<=") == 0))
387 		break;
388 	      if ((strcmp(s1, "<") == 0 && strcmp (s2, ".lt.") == 0)
389 		  || (strcmp(s1, ".lt.") == 0 && strcmp (s2, "<") == 0))
390 		break;
391 	      if ((strcmp(s1, ">=") == 0 && strcmp (s2, ".ge.") == 0)
392 		  || (strcmp(s1, ".ge.") == 0 && strcmp (s2, ">=") == 0))
393 		break;
394 	      if ((strcmp(s1, ">") == 0 && strcmp (s2, ".gt.") == 0)
395 		  || (strcmp(s1, ".gt.") == 0 && strcmp (s2, ">") == 0))
396 		break;
397 
398 	      m = MATCH_ERROR;
399 	      if (strcmp(s2, "none") == 0)
400 		gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> "
401 			   "at %C", s1);
402 	      else
403 		gfc_error ("Expecting %<END INTERFACE OPERATOR (%s)%> at %C, "
404 			   "but got %qs", s1, s2);
405 	    }
406 
407 	}
408 
409       break;
410 
411     case INTERFACE_USER_OP:
412       /* Comparing the symbol node names is OK because only use-associated
413 	 symbols can be renamed.  */
414       if (type != current_interface.type
415 	  || strcmp (current_interface.uop->name, name) != 0)
416 	{
417 	  gfc_error ("Expecting %<END INTERFACE OPERATOR (.%s.)%> at %C",
418 		     current_interface.uop->name);
419 	  m = MATCH_ERROR;
420 	}
421 
422       break;
423 
424     case INTERFACE_DTIO:
425     case INTERFACE_GENERIC:
426       if (type != current_interface.type
427 	  || strcmp (current_interface.sym->name, name) != 0)
428 	{
429 	  gfc_error ("Expecting %<END INTERFACE %s%> at %C",
430 		     current_interface.sym->name);
431 	  m = MATCH_ERROR;
432 	}
433 
434       break;
435     }
436 
437   return m;
438 }
439 
440 
441 /* Return whether the component was defined anonymously.  */
442 
443 static bool
is_anonymous_component(gfc_component * cmp)444 is_anonymous_component (gfc_component *cmp)
445 {
446   /* Only UNION and MAP components are anonymous.  In the case of a MAP,
447      the derived type symbol is FL_STRUCT and the component name looks like mM*.
448      This is the only case in which the second character of a component name is
449      uppercase.  */
450   return cmp->ts.type == BT_UNION
451     || (cmp->ts.type == BT_DERIVED
452         && cmp->ts.u.derived->attr.flavor == FL_STRUCT
453         && cmp->name[0] && cmp->name[1] && ISUPPER (cmp->name[1]));
454 }
455 
456 
457 /* Return whether the derived type was defined anonymously.  */
458 
459 static bool
is_anonymous_dt(gfc_symbol * derived)460 is_anonymous_dt (gfc_symbol *derived)
461 {
462   /* UNION and MAP types are always anonymous. Otherwise, only nested STRUCTURE
463      types can be anonymous.  For anonymous MAP/STRUCTURE, we have FL_STRUCT
464      and the type name looks like XX*.  This is the only case in which the
465      second character of a type name is uppercase.  */
466   return derived->attr.flavor == FL_UNION
467     || (derived->attr.flavor == FL_STRUCT
468         && derived->name[0] && derived->name[1] && ISUPPER (derived->name[1]));
469 }
470 
471 
472 /* Compare components according to 4.4.2 of the Fortran standard.  */
473 
474 static bool
compare_components(gfc_component * cmp1,gfc_component * cmp2,gfc_symbol * derived1,gfc_symbol * derived2)475 compare_components (gfc_component *cmp1, gfc_component *cmp2,
476     gfc_symbol *derived1, gfc_symbol *derived2)
477 {
478   /* Compare names, but not for anonymous components such as UNION or MAP.  */
479   if (!is_anonymous_component (cmp1) && !is_anonymous_component (cmp2)
480       && strcmp (cmp1->name, cmp2->name) != 0)
481     return false;
482 
483   if (cmp1->attr.access != cmp2->attr.access)
484     return false;
485 
486   if (cmp1->attr.pointer != cmp2->attr.pointer)
487     return false;
488 
489   if (cmp1->attr.dimension != cmp2->attr.dimension)
490     return false;
491 
492   if (cmp1->attr.allocatable != cmp2->attr.allocatable)
493     return false;
494 
495   if (cmp1->attr.dimension && gfc_compare_array_spec (cmp1->as, cmp2->as) == 0)
496     return false;
497 
498   if (cmp1->ts.type == BT_CHARACTER && cmp2->ts.type == BT_CHARACTER)
499     {
500       gfc_charlen *l1 = cmp1->ts.u.cl;
501       gfc_charlen *l2 = cmp2->ts.u.cl;
502       if (l1 && l2 && l1->length && l2->length
503           && l1->length->expr_type == EXPR_CONSTANT
504           && l2->length->expr_type == EXPR_CONSTANT
505           && gfc_dep_compare_expr (l1->length, l2->length) != 0)
506         return false;
507     }
508 
509   /* Make sure that link lists do not put this function into an
510      endless recursive loop!  */
511   if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
512       && !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived)
513       && !gfc_compare_types (&cmp1->ts, &cmp2->ts))
514     return false;
515 
516   else if ( (cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
517         && !(cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
518     return false;
519 
520   else if (!(cmp1->ts.type == BT_DERIVED && derived1 == cmp1->ts.u.derived)
521         &&  (cmp2->ts.type == BT_DERIVED && derived2 == cmp2->ts.u.derived))
522     return false;
523 
524   return true;
525 }
526 
527 
528 /* Compare two union types by comparing the components of their maps.
529    Because unions and maps are anonymous their types get special internal
530    names; therefore the usual derived type comparison will fail on them.
531 
532    Returns nonzero if equal, as with gfc_compare_derived_types. Also as with
533    gfc_compare_derived_types, 'equal' is closer to meaning 'duplicate
534    definitions' than 'equivalent structure'. */
535 
536 static bool
compare_union_types(gfc_symbol * un1,gfc_symbol * un2)537 compare_union_types (gfc_symbol *un1, gfc_symbol *un2)
538 {
539   gfc_component *map1, *map2, *cmp1, *cmp2;
540   gfc_symbol *map1_t, *map2_t;
541 
542   if (un1->attr.flavor != FL_UNION || un2->attr.flavor != FL_UNION)
543     return false;
544 
545   if (un1->attr.zero_comp != un2->attr.zero_comp)
546     return false;
547 
548   if (un1->attr.zero_comp)
549     return true;
550 
551   map1 = un1->components;
552   map2 = un2->components;
553 
554   /* In terms of 'equality' here we are worried about types which are
555      declared the same in two places, not types that represent equivalent
556      structures. (This is common because of FORTRAN's weird scoping rules.)
557      Though two unions with their maps in different orders could be equivalent,
558      we will say they are not equal for the purposes of this test; therefore
559      we compare the maps sequentially. */
560   for (;;)
561     {
562       map1_t = map1->ts.u.derived;
563       map2_t = map2->ts.u.derived;
564 
565       cmp1 = map1_t->components;
566       cmp2 = map2_t->components;
567 
568       /* Protect against null components.  */
569       if (map1_t->attr.zero_comp != map2_t->attr.zero_comp)
570 	return false;
571 
572       if (map1_t->attr.zero_comp)
573 	return true;
574 
575       for (;;)
576 	{
577 	  /* No two fields will ever point to the same map type unless they are
578 	     the same component, because one map field is created with its type
579 	     declaration. Therefore don't worry about recursion here. */
580 	  /* TODO: worry about recursion into parent types of the unions? */
581 	  if (!compare_components (cmp1, cmp2, map1_t, map2_t))
582 	    return false;
583 
584 	  cmp1 = cmp1->next;
585 	  cmp2 = cmp2->next;
586 
587 	  if (cmp1 == NULL && cmp2 == NULL)
588 	    break;
589 	  if (cmp1 == NULL || cmp2 == NULL)
590 	    return false;
591 	}
592 
593       map1 = map1->next;
594       map2 = map2->next;
595 
596       if (map1 == NULL && map2 == NULL)
597 	break;
598       if (map1 == NULL || map2 == NULL)
599 	return false;
600     }
601 
602   return true;
603 }
604 
605 
606 
607 /* Compare two derived types using the criteria in 4.4.2 of the standard,
608    recursing through gfc_compare_types for the components.  */
609 
610 bool
gfc_compare_derived_types(gfc_symbol * derived1,gfc_symbol * derived2)611 gfc_compare_derived_types (gfc_symbol *derived1, gfc_symbol *derived2)
612 {
613   gfc_component *cmp1, *cmp2;
614 
615   if (derived1 == derived2)
616     return true;
617 
618   if (!derived1 || !derived2)
619     gfc_internal_error ("gfc_compare_derived_types: invalid derived type");
620 
621   if (derived1->attr.unlimited_polymorphic
622       && derived2->attr.unlimited_polymorphic)
623     return true;
624 
625   if (derived1->attr.unlimited_polymorphic
626       != derived2->attr.unlimited_polymorphic)
627     return false;
628 
629   /* Compare UNION types specially.  */
630   if (derived1->attr.flavor == FL_UNION || derived2->attr.flavor == FL_UNION)
631     return compare_union_types (derived1, derived2);
632 
633   /* Special case for comparing derived types across namespaces.  If the
634      true names and module names are the same and the module name is
635      nonnull, then they are equal.  */
636   if (strcmp (derived1->name, derived2->name) == 0
637       && derived1->module != NULL && derived2->module != NULL
638       && strcmp (derived1->module, derived2->module) == 0)
639     return true;
640 
641   /* Compare type via the rules of the standard.  Both types must have the
642      SEQUENCE or BIND(C) attribute to be equal.  We also compare types
643      recursively if they are class descriptors types or virtual tables types.
644      STRUCTUREs are special because they can be anonymous; therefore two
645      structures with different names may be equal.  */
646 
647   /* Compare names, but not for anonymous types such as UNION or MAP.  */
648   if (!is_anonymous_dt (derived1) && !is_anonymous_dt (derived2)
649       && strcmp (derived1->name, derived2->name) != 0)
650     return false;
651 
652   if (derived1->component_access == ACCESS_PRIVATE
653       || derived2->component_access == ACCESS_PRIVATE)
654     return false;
655 
656   if (!(derived1->attr.sequence && derived2->attr.sequence)
657       && !(derived1->attr.is_bind_c && derived2->attr.is_bind_c)
658       && !(derived1->attr.is_class && derived2->attr.is_class)
659       && !(derived1->attr.vtype && derived2->attr.vtype)
660       && !(derived1->attr.pdt_type && derived2->attr.pdt_type))
661     return false;
662 
663   /* Protect against null components.  */
664   if (derived1->attr.zero_comp != derived2->attr.zero_comp)
665     return false;
666 
667   if (derived1->attr.zero_comp)
668     return true;
669 
670   cmp1 = derived1->components;
671   cmp2 = derived2->components;
672 
673   /* Since subtypes of SEQUENCE types must be SEQUENCE types as well, a
674      simple test can speed things up.  Otherwise, lots of things have to
675      match.  */
676   for (;;)
677     {
678       if (!compare_components (cmp1, cmp2, derived1, derived2))
679         return false;
680 
681       cmp1 = cmp1->next;
682       cmp2 = cmp2->next;
683 
684       if (cmp1 == NULL && cmp2 == NULL)
685 	break;
686       if (cmp1 == NULL || cmp2 == NULL)
687 	return false;
688     }
689 
690   return true;
691 }
692 
693 
694 /* Compare two typespecs, recursively if necessary.  */
695 
696 bool
gfc_compare_types(gfc_typespec * ts1,gfc_typespec * ts2)697 gfc_compare_types (gfc_typespec *ts1, gfc_typespec *ts2)
698 {
699   /* See if one of the typespecs is a BT_VOID, which is what is being used
700      to allow the funcs like c_f_pointer to accept any pointer type.
701      TODO: Possibly should narrow this to just the one typespec coming in
702      that is for the formal arg, but oh well.  */
703   if (ts1->type == BT_VOID || ts2->type == BT_VOID)
704     return true;
705 
706   /* Special case for our C interop types.  FIXME: There should be a
707      better way of doing this.  When ISO C binding is cleared up,
708      this can probably be removed.  See PR 57048.  */
709 
710   if (((ts1->type == BT_INTEGER && ts2->type == BT_DERIVED)
711        || (ts1->type == BT_DERIVED && ts2->type == BT_INTEGER))
712       && ts1->u.derived && ts2->u.derived
713       && ts1->u.derived == ts2->u.derived)
714     return true;
715 
716   /* The _data component is not always present, therefore check for its
717      presence before assuming, that its derived->attr is available.
718      When the _data component is not present, then nevertheless the
719      unlimited_polymorphic flag may be set in the derived type's attr.  */
720   if (ts1->type == BT_CLASS && ts1->u.derived->components
721       && ((ts1->u.derived->attr.is_class
722 	   && ts1->u.derived->components->ts.u.derived->attr
723 						  .unlimited_polymorphic)
724 	  || ts1->u.derived->attr.unlimited_polymorphic))
725     return true;
726 
727   /* F2003: C717  */
728   if (ts2->type == BT_CLASS && ts1->type == BT_DERIVED
729       && ts2->u.derived->components
730       && ((ts2->u.derived->attr.is_class
731 	   && ts2->u.derived->components->ts.u.derived->attr
732 						  .unlimited_polymorphic)
733 	  || ts2->u.derived->attr.unlimited_polymorphic)
734       && (ts1->u.derived->attr.sequence || ts1->u.derived->attr.is_bind_c))
735     return true;
736 
737   if (ts1->type != ts2->type
738       && ((ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
739 	  || (ts2->type != BT_DERIVED && ts2->type != BT_CLASS)))
740     return false;
741 
742   if (ts1->type == BT_UNION)
743     return compare_union_types (ts1->u.derived, ts2->u.derived);
744 
745   if (ts1->type != BT_DERIVED && ts1->type != BT_CLASS)
746     return (ts1->kind == ts2->kind);
747 
748   /* Compare derived types.  */
749   return gfc_type_compatible (ts1, ts2);
750 }
751 
752 
753 static bool
compare_type(gfc_symbol * s1,gfc_symbol * s2)754 compare_type (gfc_symbol *s1, gfc_symbol *s2)
755 {
756   if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
757     return true;
758 
759   return gfc_compare_types (&s1->ts, &s2->ts) || s2->ts.type == BT_ASSUMED;
760 }
761 
762 
763 static bool
compare_type_characteristics(gfc_symbol * s1,gfc_symbol * s2)764 compare_type_characteristics (gfc_symbol *s1, gfc_symbol *s2)
765 {
766   /* TYPE and CLASS of the same declared type are type compatible,
767      but have different characteristics.  */
768   if ((s1->ts.type == BT_CLASS && s2->ts.type == BT_DERIVED)
769       || (s1->ts.type == BT_DERIVED && s2->ts.type == BT_CLASS))
770     return false;
771 
772   return compare_type (s1, s2);
773 }
774 
775 
776 static bool
compare_rank(gfc_symbol * s1,gfc_symbol * s2)777 compare_rank (gfc_symbol *s1, gfc_symbol *s2)
778 {
779   gfc_array_spec *as1, *as2;
780   int r1, r2;
781 
782   if (s2->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
783     return true;
784 
785   as1 = (s1->ts.type == BT_CLASS
786 	 && !s1->ts.u.derived->attr.unlimited_polymorphic)
787 	? CLASS_DATA (s1)->as : s1->as;
788   as2 = (s2->ts.type == BT_CLASS
789 	 && !s2->ts.u.derived->attr.unlimited_polymorphic)
790 	? CLASS_DATA (s2)->as : s2->as;
791 
792   r1 = as1 ? as1->rank : 0;
793   r2 = as2 ? as2->rank : 0;
794 
795   if (r1 != r2 && (!as2 || as2->type != AS_ASSUMED_RANK))
796     return false;  /* Ranks differ.  */
797 
798   return true;
799 }
800 
801 
802 /* Given two symbols that are formal arguments, compare their ranks
803    and types.  Returns true if they have the same rank and type,
804    false otherwise.  */
805 
806 static bool
compare_type_rank(gfc_symbol * s1,gfc_symbol * s2)807 compare_type_rank (gfc_symbol *s1, gfc_symbol *s2)
808 {
809   return compare_type (s1, s2) && compare_rank (s1, s2);
810 }
811 
812 
813 /* Given two symbols that are formal arguments, compare their types
814    and rank and their formal interfaces if they are both dummy
815    procedures.  Returns true if the same, false if different.  */
816 
817 static bool
compare_type_rank_if(gfc_symbol * s1,gfc_symbol * s2)818 compare_type_rank_if (gfc_symbol *s1, gfc_symbol *s2)
819 {
820   if (s1 == NULL || s2 == NULL)
821     return (s1 == s2);
822 
823   if (s1 == s2)
824     return true;
825 
826   if (s1->attr.flavor != FL_PROCEDURE && s2->attr.flavor != FL_PROCEDURE)
827     return compare_type_rank (s1, s2);
828 
829   if (s1->attr.flavor != FL_PROCEDURE || s2->attr.flavor != FL_PROCEDURE)
830     return false;
831 
832   /* At this point, both symbols are procedures.  It can happen that
833      external procedures are compared, where one is identified by usage
834      to be a function or subroutine but the other is not.  Check TKR
835      nonetheless for these cases.  */
836   if (s1->attr.function == 0 && s1->attr.subroutine == 0)
837     return s1->attr.external ? compare_type_rank (s1, s2) : false;
838 
839   if (s2->attr.function == 0 && s2->attr.subroutine == 0)
840     return s2->attr.external ? compare_type_rank (s1, s2) : false;
841 
842   /* Now the type of procedure has been identified.  */
843   if (s1->attr.function != s2->attr.function
844       || s1->attr.subroutine != s2->attr.subroutine)
845     return false;
846 
847   if (s1->attr.function && !compare_type_rank (s1, s2))
848     return false;
849 
850   /* Originally, gfortran recursed here to check the interfaces of passed
851      procedures.  This is explicitly not required by the standard.  */
852   return true;
853 }
854 
855 
856 /* Given a formal argument list and a keyword name, search the list
857    for that keyword.  Returns the correct symbol node if found, NULL
858    if not found.  */
859 
860 static gfc_symbol *
find_keyword_arg(const char * name,gfc_formal_arglist * f)861 find_keyword_arg (const char *name, gfc_formal_arglist *f)
862 {
863   for (; f; f = f->next)
864     if (strcmp (f->sym->name, name) == 0)
865       return f->sym;
866 
867   return NULL;
868 }
869 
870 
871 /******** Interface checking subroutines **********/
872 
873 
874 /* Given an operator interface and the operator, make sure that all
875    interfaces for that operator are legal.  */
876 
877 bool
gfc_check_operator_interface(gfc_symbol * sym,gfc_intrinsic_op op,locus opwhere)878 gfc_check_operator_interface (gfc_symbol *sym, gfc_intrinsic_op op,
879 			      locus opwhere)
880 {
881   gfc_formal_arglist *formal;
882   sym_intent i1, i2;
883   bt t1, t2;
884   int args, r1, r2, k1, k2;
885 
886   gcc_assert (sym);
887 
888   args = 0;
889   t1 = t2 = BT_UNKNOWN;
890   i1 = i2 = INTENT_UNKNOWN;
891   r1 = r2 = -1;
892   k1 = k2 = -1;
893 
894   for (formal = gfc_sym_get_dummy_args (sym); formal; formal = formal->next)
895     {
896       gfc_symbol *fsym = formal->sym;
897       if (fsym == NULL)
898 	{
899 	  gfc_error ("Alternate return cannot appear in operator "
900 		     "interface at %L", &sym->declared_at);
901 	  return false;
902 	}
903       if (args == 0)
904 	{
905 	  t1 = fsym->ts.type;
906 	  i1 = fsym->attr.intent;
907 	  r1 = (fsym->as != NULL) ? fsym->as->rank : 0;
908 	  k1 = fsym->ts.kind;
909 	}
910       if (args == 1)
911 	{
912 	  t2 = fsym->ts.type;
913 	  i2 = fsym->attr.intent;
914 	  r2 = (fsym->as != NULL) ? fsym->as->rank : 0;
915 	  k2 = fsym->ts.kind;
916 	}
917       args++;
918     }
919 
920   /* Only +, - and .not. can be unary operators.
921      .not. cannot be a binary operator.  */
922   if (args == 0 || args > 2 || (args == 1 && op != INTRINSIC_PLUS
923 				&& op != INTRINSIC_MINUS
924 				&& op != INTRINSIC_NOT)
925       || (args == 2 && op == INTRINSIC_NOT))
926     {
927       if (op == INTRINSIC_ASSIGN)
928 	gfc_error ("Assignment operator interface at %L must have "
929 		   "two arguments", &sym->declared_at);
930       else
931 	gfc_error ("Operator interface at %L has the wrong number of arguments",
932 		   &sym->declared_at);
933       return false;
934     }
935 
936   /* Check that intrinsics are mapped to functions, except
937      INTRINSIC_ASSIGN which should map to a subroutine.  */
938   if (op == INTRINSIC_ASSIGN)
939     {
940       gfc_formal_arglist *dummy_args;
941 
942       if (!sym->attr.subroutine)
943 	{
944 	  gfc_error ("Assignment operator interface at %L must be "
945 		     "a SUBROUTINE", &sym->declared_at);
946 	  return false;
947 	}
948 
949       /* Allowed are (per F2003, 12.3.2.1.2 Defined assignments):
950 	 - First argument an array with different rank than second,
951 	 - First argument is a scalar and second an array,
952 	 - Types and kinds do not conform, or
953 	 - First argument is of derived type.  */
954       dummy_args = gfc_sym_get_dummy_args (sym);
955       if (dummy_args->sym->ts.type != BT_DERIVED
956 	  && dummy_args->sym->ts.type != BT_CLASS
957 	  && (r2 == 0 || r1 == r2)
958 	  && (dummy_args->sym->ts.type == dummy_args->next->sym->ts.type
959 	      || (gfc_numeric_ts (&dummy_args->sym->ts)
960 		  && gfc_numeric_ts (&dummy_args->next->sym->ts))))
961 	{
962 	  gfc_error ("Assignment operator interface at %L must not redefine "
963 		     "an INTRINSIC type assignment", &sym->declared_at);
964 	  return false;
965 	}
966     }
967   else
968     {
969       if (!sym->attr.function)
970 	{
971 	  gfc_error ("Intrinsic operator interface at %L must be a FUNCTION",
972 		     &sym->declared_at);
973 	  return false;
974 	}
975     }
976 
977   /* Check intents on operator interfaces.  */
978   if (op == INTRINSIC_ASSIGN)
979     {
980       if (i1 != INTENT_OUT && i1 != INTENT_INOUT)
981 	{
982 	  gfc_error ("First argument of defined assignment at %L must be "
983 		     "INTENT(OUT) or INTENT(INOUT)", &sym->declared_at);
984 	  return false;
985 	}
986 
987       if (i2 != INTENT_IN)
988 	{
989 	  gfc_error ("Second argument of defined assignment at %L must be "
990 		     "INTENT(IN)", &sym->declared_at);
991 	  return false;
992 	}
993     }
994   else
995     {
996       if (i1 != INTENT_IN)
997 	{
998 	  gfc_error ("First argument of operator interface at %L must be "
999 		     "INTENT(IN)", &sym->declared_at);
1000 	  return false;
1001 	}
1002 
1003       if (args == 2 && i2 != INTENT_IN)
1004 	{
1005 	  gfc_error ("Second argument of operator interface at %L must be "
1006 		     "INTENT(IN)", &sym->declared_at);
1007 	  return false;
1008 	}
1009     }
1010 
1011   /* From now on, all we have to do is check that the operator definition
1012      doesn't conflict with an intrinsic operator. The rules for this
1013      game are defined in 7.1.2 and 7.1.3 of both F95 and F2003 standards,
1014      as well as 12.3.2.1.1 of Fortran 2003:
1015 
1016      "If the operator is an intrinsic-operator (R310), the number of
1017      function arguments shall be consistent with the intrinsic uses of
1018      that operator, and the types, kind type parameters, or ranks of the
1019      dummy arguments shall differ from those required for the intrinsic
1020      operation (7.1.2)."  */
1021 
1022 #define IS_NUMERIC_TYPE(t) \
1023   ((t) == BT_INTEGER || (t) == BT_REAL || (t) == BT_COMPLEX)
1024 
1025   /* Unary ops are easy, do them first.  */
1026   if (op == INTRINSIC_NOT)
1027     {
1028       if (t1 == BT_LOGICAL)
1029 	goto bad_repl;
1030       else
1031 	return true;
1032     }
1033 
1034   if (args == 1 && (op == INTRINSIC_PLUS || op == INTRINSIC_MINUS))
1035     {
1036       if (IS_NUMERIC_TYPE (t1))
1037 	goto bad_repl;
1038       else
1039 	return true;
1040     }
1041 
1042   /* Character intrinsic operators have same character kind, thus
1043      operator definitions with operands of different character kinds
1044      are always safe.  */
1045   if (t1 == BT_CHARACTER && t2 == BT_CHARACTER && k1 != k2)
1046     return true;
1047 
1048   /* Intrinsic operators always perform on arguments of same rank,
1049      so different ranks is also always safe.  (rank == 0) is an exception
1050      to that, because all intrinsic operators are elemental.  */
1051   if (r1 != r2 && r1 != 0 && r2 != 0)
1052     return true;
1053 
1054   switch (op)
1055   {
1056     case INTRINSIC_EQ:
1057     case INTRINSIC_EQ_OS:
1058     case INTRINSIC_NE:
1059     case INTRINSIC_NE_OS:
1060       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
1061 	goto bad_repl;
1062       /* Fall through.  */
1063 
1064     case INTRINSIC_PLUS:
1065     case INTRINSIC_MINUS:
1066     case INTRINSIC_TIMES:
1067     case INTRINSIC_DIVIDE:
1068     case INTRINSIC_POWER:
1069       if (IS_NUMERIC_TYPE (t1) && IS_NUMERIC_TYPE (t2))
1070 	goto bad_repl;
1071       break;
1072 
1073     case INTRINSIC_GT:
1074     case INTRINSIC_GT_OS:
1075     case INTRINSIC_GE:
1076     case INTRINSIC_GE_OS:
1077     case INTRINSIC_LT:
1078     case INTRINSIC_LT_OS:
1079     case INTRINSIC_LE:
1080     case INTRINSIC_LE_OS:
1081       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
1082 	goto bad_repl;
1083       if ((t1 == BT_INTEGER || t1 == BT_REAL)
1084 	  && (t2 == BT_INTEGER || t2 == BT_REAL))
1085 	goto bad_repl;
1086       break;
1087 
1088     case INTRINSIC_CONCAT:
1089       if (t1 == BT_CHARACTER && t2 == BT_CHARACTER)
1090 	goto bad_repl;
1091       break;
1092 
1093     case INTRINSIC_AND:
1094     case INTRINSIC_OR:
1095     case INTRINSIC_EQV:
1096     case INTRINSIC_NEQV:
1097       if (t1 == BT_LOGICAL && t2 == BT_LOGICAL)
1098 	goto bad_repl;
1099       break;
1100 
1101     default:
1102       break;
1103   }
1104 
1105   return true;
1106 
1107 #undef IS_NUMERIC_TYPE
1108 
1109 bad_repl:
1110   gfc_error ("Operator interface at %L conflicts with intrinsic interface",
1111 	     &opwhere);
1112   return false;
1113 }
1114 
1115 
1116 /* Given a pair of formal argument lists, we see if the two lists can
1117    be distinguished by counting the number of nonoptional arguments of
1118    a given type/rank in f1 and seeing if there are less then that
1119    number of those arguments in f2 (including optional arguments).
1120    Since this test is asymmetric, it has to be called twice to make it
1121    symmetric. Returns nonzero if the argument lists are incompatible
1122    by this test. This subroutine implements rule 1 of section F03:16.2.3.
1123    'p1' and 'p2' are the PASS arguments of both procedures (if applicable).  */
1124 
1125 static bool
count_types_test(gfc_formal_arglist * f1,gfc_formal_arglist * f2,const char * p1,const char * p2)1126 count_types_test (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
1127 		  const char *p1, const char *p2)
1128 {
1129   int ac1, ac2, i, j, k, n1;
1130   gfc_formal_arglist *f;
1131 
1132   typedef struct
1133   {
1134     int flag;
1135     gfc_symbol *sym;
1136   }
1137   arginfo;
1138 
1139   arginfo *arg;
1140 
1141   n1 = 0;
1142 
1143   for (f = f1; f; f = f->next)
1144     n1++;
1145 
1146   /* Build an array of integers that gives the same integer to
1147      arguments of the same type/rank.  */
1148   arg = XCNEWVEC (arginfo, n1);
1149 
1150   f = f1;
1151   for (i = 0; i < n1; i++, f = f->next)
1152     {
1153       arg[i].flag = -1;
1154       arg[i].sym = f->sym;
1155     }
1156 
1157   k = 0;
1158 
1159   for (i = 0; i < n1; i++)
1160     {
1161       if (arg[i].flag != -1)
1162 	continue;
1163 
1164       if (arg[i].sym && (arg[i].sym->attr.optional
1165 			 || (p1 && strcmp (arg[i].sym->name, p1) == 0)))
1166 	continue;		/* Skip OPTIONAL and PASS arguments.  */
1167 
1168       arg[i].flag = k;
1169 
1170       /* Find other non-optional, non-pass arguments of the same type/rank.  */
1171       for (j = i + 1; j < n1; j++)
1172 	if ((arg[j].sym == NULL
1173 	     || !(arg[j].sym->attr.optional
1174 		  || (p1 && strcmp (arg[j].sym->name, p1) == 0)))
1175 	    && (compare_type_rank_if (arg[i].sym, arg[j].sym)
1176 	        || compare_type_rank_if (arg[j].sym, arg[i].sym)))
1177 	  arg[j].flag = k;
1178 
1179       k++;
1180     }
1181 
1182   /* Now loop over each distinct type found in f1.  */
1183   k = 0;
1184   bool rc = false;
1185 
1186   for (i = 0; i < n1; i++)
1187     {
1188       if (arg[i].flag != k)
1189 	continue;
1190 
1191       ac1 = 1;
1192       for (j = i + 1; j < n1; j++)
1193 	if (arg[j].flag == k)
1194 	  ac1++;
1195 
1196       /* Count the number of non-pass arguments in f2 with that type,
1197 	 including those that are optional.  */
1198       ac2 = 0;
1199 
1200       for (f = f2; f; f = f->next)
1201 	if ((!p2 || strcmp (f->sym->name, p2) != 0)
1202 	    && (compare_type_rank_if (arg[i].sym, f->sym)
1203 		|| compare_type_rank_if (f->sym, arg[i].sym)))
1204 	  ac2++;
1205 
1206       if (ac1 > ac2)
1207 	{
1208 	  rc = true;
1209 	  break;
1210 	}
1211 
1212       k++;
1213     }
1214 
1215   free (arg);
1216 
1217   return rc;
1218 }
1219 
1220 
1221 /* Returns true if two dummy arguments are distinguishable due to their POINTER
1222    and ALLOCATABLE attributes according to F2018 section 15.4.3.4.5 (3).
1223    The function is asymmetric wrt to the arguments s1 and s2 and should always
1224    be called twice (with flipped arguments in the second call).  */
1225 
1226 static bool
compare_ptr_alloc(gfc_symbol * s1,gfc_symbol * s2)1227 compare_ptr_alloc(gfc_symbol *s1, gfc_symbol *s2)
1228 {
1229   /* Is s1 allocatable?  */
1230   const bool a1 = s1->ts.type == BT_CLASS ?
1231 		  CLASS_DATA(s1)->attr.allocatable : s1->attr.allocatable;
1232   /* Is s2 a pointer?  */
1233   const bool p2 = s2->ts.type == BT_CLASS ?
1234 		  CLASS_DATA(s2)->attr.class_pointer : s2->attr.pointer;
1235   return a1 && p2 && (s2->attr.intent != INTENT_IN);
1236 }
1237 
1238 
1239 /* Perform the correspondence test in rule (3) of F08:C1215.
1240    Returns zero if no argument is found that satisfies this rule,
1241    nonzero otherwise. 'p1' and 'p2' are the PASS arguments of both procedures
1242    (if applicable).
1243 
1244    This test is also not symmetric in f1 and f2 and must be called
1245    twice.  This test finds problems caused by sorting the actual
1246    argument list with keywords.  For example:
1247 
1248    INTERFACE FOO
1249      SUBROUTINE F1(A, B)
1250        INTEGER :: A ; REAL :: B
1251      END SUBROUTINE F1
1252 
1253      SUBROUTINE F2(B, A)
1254        INTEGER :: A ; REAL :: B
1255      END SUBROUTINE F1
1256    END INTERFACE FOO
1257 
1258    At this point, 'CALL FOO(A=1, B=1.0)' is ambiguous.  */
1259 
1260 static bool
generic_correspondence(gfc_formal_arglist * f1,gfc_formal_arglist * f2,const char * p1,const char * p2)1261 generic_correspondence (gfc_formal_arglist *f1, gfc_formal_arglist *f2,
1262 			const char *p1, const char *p2)
1263 {
1264   gfc_formal_arglist *f2_save, *g;
1265   gfc_symbol *sym;
1266 
1267   f2_save = f2;
1268 
1269   while (f1)
1270     {
1271       if (!f1->sym || f1->sym->attr.optional)
1272 	goto next;
1273 
1274       if (p1 && strcmp (f1->sym->name, p1) == 0)
1275 	f1 = f1->next;
1276       if (f2 && p2 && strcmp (f2->sym->name, p2) == 0)
1277 	f2 = f2->next;
1278 
1279       if (f2 != NULL && (compare_type_rank (f1->sym, f2->sym)
1280 			 || compare_type_rank (f2->sym, f1->sym))
1281 	  && !((gfc_option.allow_std & GFC_STD_F2008)
1282 	       && (compare_ptr_alloc(f1->sym, f2->sym)
1283 		   || compare_ptr_alloc(f2->sym, f1->sym))))
1284 	goto next;
1285 
1286       /* Now search for a disambiguating keyword argument starting at
1287 	 the current non-match.  */
1288       for (g = f1; g; g = g->next)
1289 	{
1290 	  if (g->sym->attr.optional || (p1 && strcmp (g->sym->name, p1) == 0))
1291 	    continue;
1292 
1293 	  sym = find_keyword_arg (g->sym->name, f2_save);
1294 	  if (sym == NULL || !compare_type_rank (g->sym, sym)
1295 	      || ((gfc_option.allow_std & GFC_STD_F2008)
1296 		  && (compare_ptr_alloc(sym, g->sym)
1297 		      || compare_ptr_alloc(g->sym, sym))))
1298 	    return true;
1299 	}
1300 
1301     next:
1302       if (f1 != NULL)
1303 	f1 = f1->next;
1304       if (f2 != NULL)
1305 	f2 = f2->next;
1306     }
1307 
1308   return false;
1309 }
1310 
1311 
1312 static int
symbol_rank(gfc_symbol * sym)1313 symbol_rank (gfc_symbol *sym)
1314 {
1315   gfc_array_spec *as = NULL;
1316 
1317   if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
1318     as = CLASS_DATA (sym)->as;
1319   else
1320     as = sym->as;
1321 
1322   return as ? as->rank : 0;
1323 }
1324 
1325 
1326 /* Check if the characteristics of two dummy arguments match,
1327    cf. F08:12.3.2.  */
1328 
1329 bool
gfc_check_dummy_characteristics(gfc_symbol * s1,gfc_symbol * s2,bool type_must_agree,char * errmsg,int err_len)1330 gfc_check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
1331 				 bool type_must_agree, char *errmsg,
1332 				 int err_len)
1333 {
1334   if (s1 == NULL || s2 == NULL)
1335     return s1 == s2 ? true : false;
1336 
1337   /* Check type and rank.  */
1338   if (type_must_agree)
1339     {
1340       if (!compare_type_characteristics (s1, s2)
1341 	  || !compare_type_characteristics (s2, s1))
1342 	{
1343 	  snprintf (errmsg, err_len, "Type mismatch in argument '%s' (%s/%s)",
1344 		    s1->name, gfc_dummy_typename (&s1->ts),
1345 		    gfc_dummy_typename (&s2->ts));
1346 	  return false;
1347 	}
1348       if (!compare_rank (s1, s2))
1349 	{
1350 	  snprintf (errmsg, err_len, "Rank mismatch in argument '%s' (%i/%i)",
1351 		    s1->name, symbol_rank (s1), symbol_rank (s2));
1352 	  return false;
1353 	}
1354     }
1355 
1356   /* Check INTENT.  */
1357   if (s1->attr.intent != s2->attr.intent && !s1->attr.artificial
1358       && !s2->attr.artificial)
1359     {
1360       snprintf (errmsg, err_len, "INTENT mismatch in argument '%s'",
1361 		s1->name);
1362       return false;
1363     }
1364 
1365   /* Check OPTIONAL attribute.  */
1366   if (s1->attr.optional != s2->attr.optional)
1367     {
1368       snprintf (errmsg, err_len, "OPTIONAL mismatch in argument '%s'",
1369 		s1->name);
1370       return false;
1371     }
1372 
1373   /* Check ALLOCATABLE attribute.  */
1374   if (s1->attr.allocatable != s2->attr.allocatable)
1375     {
1376       snprintf (errmsg, err_len, "ALLOCATABLE mismatch in argument '%s'",
1377 		s1->name);
1378       return false;
1379     }
1380 
1381   /* Check POINTER attribute.  */
1382   if (s1->attr.pointer != s2->attr.pointer)
1383     {
1384       snprintf (errmsg, err_len, "POINTER mismatch in argument '%s'",
1385 		s1->name);
1386       return false;
1387     }
1388 
1389   /* Check TARGET attribute.  */
1390   if (s1->attr.target != s2->attr.target)
1391     {
1392       snprintf (errmsg, err_len, "TARGET mismatch in argument '%s'",
1393 		s1->name);
1394       return false;
1395     }
1396 
1397   /* Check ASYNCHRONOUS attribute.  */
1398   if (s1->attr.asynchronous != s2->attr.asynchronous)
1399     {
1400       snprintf (errmsg, err_len, "ASYNCHRONOUS mismatch in argument '%s'",
1401 		s1->name);
1402       return false;
1403     }
1404 
1405   /* Check CONTIGUOUS attribute.  */
1406   if (s1->attr.contiguous != s2->attr.contiguous)
1407     {
1408       snprintf (errmsg, err_len, "CONTIGUOUS mismatch in argument '%s'",
1409 		s1->name);
1410       return false;
1411     }
1412 
1413   /* Check VALUE attribute.  */
1414   if (s1->attr.value != s2->attr.value)
1415     {
1416       snprintf (errmsg, err_len, "VALUE mismatch in argument '%s'",
1417 		s1->name);
1418       return false;
1419     }
1420 
1421   /* Check VOLATILE attribute.  */
1422   if (s1->attr.volatile_ != s2->attr.volatile_)
1423     {
1424       snprintf (errmsg, err_len, "VOLATILE mismatch in argument '%s'",
1425 		s1->name);
1426       return false;
1427     }
1428 
1429   /* Check interface of dummy procedures.  */
1430   if (s1->attr.flavor == FL_PROCEDURE)
1431     {
1432       char err[200];
1433       if (!gfc_compare_interfaces (s1, s2, s2->name, 0, 1, err, sizeof(err),
1434 				   NULL, NULL))
1435 	{
1436 	  snprintf (errmsg, err_len, "Interface mismatch in dummy procedure "
1437 		    "'%s': %s", s1->name, err);
1438 	  return false;
1439 	}
1440     }
1441 
1442   /* Check string length.  */
1443   if (s1->ts.type == BT_CHARACTER
1444       && s1->ts.u.cl && s1->ts.u.cl->length
1445       && s2->ts.u.cl && s2->ts.u.cl->length)
1446     {
1447       int compval = gfc_dep_compare_expr (s1->ts.u.cl->length,
1448 					  s2->ts.u.cl->length);
1449       switch (compval)
1450       {
1451 	case -1:
1452 	case  1:
1453 	case -3:
1454 	  snprintf (errmsg, err_len, "Character length mismatch "
1455 		    "in argument '%s'", s1->name);
1456 	  return false;
1457 
1458 	case -2:
1459 	  /* FIXME: Implement a warning for this case.
1460 	  gfc_warning (0, "Possible character length mismatch in argument %qs",
1461 		       s1->name);*/
1462 	  break;
1463 
1464 	case 0:
1465 	  break;
1466 
1467 	default:
1468 	  gfc_internal_error ("check_dummy_characteristics: Unexpected result "
1469 			      "%i of gfc_dep_compare_expr", compval);
1470 	  break;
1471       }
1472     }
1473 
1474   /* Check array shape.  */
1475   if (s1->as && s2->as)
1476     {
1477       int i, compval;
1478       gfc_expr *shape1, *shape2;
1479 
1480       /* Sometimes the ambiguity between deferred shape and assumed shape
1481 	 does not get resolved in module procedures, where the only explicit
1482 	 declaration of the dummy is in the interface.  */
1483       if (s1->ns->proc_name && s1->ns->proc_name->attr.module_procedure
1484 	  && s1->as->type == AS_ASSUMED_SHAPE
1485 	  && s2->as->type == AS_DEFERRED)
1486 	{
1487 	  s2->as->type = AS_ASSUMED_SHAPE;
1488 	  for (i = 0; i < s2->as->rank; i++)
1489 	    if (s1->as->lower[i] != NULL)
1490 	      s2->as->lower[i] = gfc_copy_expr (s1->as->lower[i]);
1491 	}
1492 
1493       if (s1->as->type != s2->as->type)
1494 	{
1495 	  snprintf (errmsg, err_len, "Shape mismatch in argument '%s'",
1496 		    s1->name);
1497 	  return false;
1498 	}
1499 
1500       if (s1->as->corank != s2->as->corank)
1501 	{
1502 	  snprintf (errmsg, err_len, "Corank mismatch in argument '%s' (%i/%i)",
1503 		    s1->name, s1->as->corank, s2->as->corank);
1504 	  return false;
1505 	}
1506 
1507       if (s1->as->type == AS_EXPLICIT)
1508 	for (i = 0; i < s1->as->rank + MAX (0, s1->as->corank-1); i++)
1509 	  {
1510 	    shape1 = gfc_subtract (gfc_copy_expr (s1->as->upper[i]),
1511 				  gfc_copy_expr (s1->as->lower[i]));
1512 	    shape2 = gfc_subtract (gfc_copy_expr (s2->as->upper[i]),
1513 				  gfc_copy_expr (s2->as->lower[i]));
1514 	    compval = gfc_dep_compare_expr (shape1, shape2);
1515 	    gfc_free_expr (shape1);
1516 	    gfc_free_expr (shape2);
1517 	    switch (compval)
1518 	    {
1519 	      case -1:
1520 	      case  1:
1521 	      case -3:
1522 		if (i < s1->as->rank)
1523 		  snprintf (errmsg, err_len, "Shape mismatch in dimension %i of"
1524 			    " argument '%s'", i + 1, s1->name);
1525 		else
1526 		  snprintf (errmsg, err_len, "Shape mismatch in codimension %i "
1527 			    "of argument '%s'", i - s1->as->rank + 1, s1->name);
1528 		return false;
1529 
1530 	      case -2:
1531 		/* FIXME: Implement a warning for this case.
1532 		gfc_warning (0, "Possible shape mismatch in argument %qs",
1533 			    s1->name);*/
1534 		break;
1535 
1536 	      case 0:
1537 		break;
1538 
1539 	      default:
1540 		gfc_internal_error ("check_dummy_characteristics: Unexpected "
1541 				    "result %i of gfc_dep_compare_expr",
1542 				    compval);
1543 		break;
1544 	    }
1545 	  }
1546     }
1547 
1548   return true;
1549 }
1550 
1551 
1552 /* Check if the characteristics of two function results match,
1553    cf. F08:12.3.3.  */
1554 
1555 bool
gfc_check_result_characteristics(gfc_symbol * s1,gfc_symbol * s2,char * errmsg,int err_len)1556 gfc_check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
1557 				  char *errmsg, int err_len)
1558 {
1559   gfc_symbol *r1, *r2;
1560 
1561   if (s1->ts.interface && s1->ts.interface->result)
1562     r1 = s1->ts.interface->result;
1563   else
1564     r1 = s1->result ? s1->result : s1;
1565 
1566   if (s2->ts.interface && s2->ts.interface->result)
1567     r2 = s2->ts.interface->result;
1568   else
1569     r2 = s2->result ? s2->result : s2;
1570 
1571   if (r1->ts.type == BT_UNKNOWN)
1572     return true;
1573 
1574   /* Check type and rank.  */
1575   if (!compare_type_characteristics (r1, r2))
1576     {
1577       snprintf (errmsg, err_len, "Type mismatch in function result (%s/%s)",
1578 		gfc_typename (&r1->ts), gfc_typename (&r2->ts));
1579       return false;
1580     }
1581   if (!compare_rank (r1, r2))
1582     {
1583       snprintf (errmsg, err_len, "Rank mismatch in function result (%i/%i)",
1584 		symbol_rank (r1), symbol_rank (r2));
1585       return false;
1586     }
1587 
1588   /* Check ALLOCATABLE attribute.  */
1589   if (r1->attr.allocatable != r2->attr.allocatable)
1590     {
1591       snprintf (errmsg, err_len, "ALLOCATABLE attribute mismatch in "
1592 		"function result");
1593       return false;
1594     }
1595 
1596   /* Check POINTER attribute.  */
1597   if (r1->attr.pointer != r2->attr.pointer)
1598     {
1599       snprintf (errmsg, err_len, "POINTER attribute mismatch in "
1600 		"function result");
1601       return false;
1602     }
1603 
1604   /* Check CONTIGUOUS attribute.  */
1605   if (r1->attr.contiguous != r2->attr.contiguous)
1606     {
1607       snprintf (errmsg, err_len, "CONTIGUOUS attribute mismatch in "
1608 		"function result");
1609       return false;
1610     }
1611 
1612   /* Check PROCEDURE POINTER attribute.  */
1613   if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
1614     {
1615       snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
1616 		"function result");
1617       return false;
1618     }
1619 
1620   /* Check string length.  */
1621   if (r1->ts.type == BT_CHARACTER && r1->ts.u.cl && r2->ts.u.cl)
1622     {
1623       if (r1->ts.deferred != r2->ts.deferred)
1624 	{
1625 	  snprintf (errmsg, err_len, "Character length mismatch "
1626 		    "in function result");
1627 	  return false;
1628 	}
1629 
1630       if (r1->ts.u.cl->length && r2->ts.u.cl->length)
1631 	{
1632 	  int compval = gfc_dep_compare_expr (r1->ts.u.cl->length,
1633 					      r2->ts.u.cl->length);
1634 	  switch (compval)
1635 	  {
1636 	    case -1:
1637 	    case  1:
1638 	    case -3:
1639 	      snprintf (errmsg, err_len, "Character length mismatch "
1640 			"in function result");
1641 	      return false;
1642 
1643 	    case -2:
1644 	      /* FIXME: Implement a warning for this case.
1645 	      snprintf (errmsg, err_len, "Possible character length mismatch "
1646 			"in function result");*/
1647 	      break;
1648 
1649 	    case 0:
1650 	      break;
1651 
1652 	    default:
1653 	      gfc_internal_error ("check_result_characteristics (1): Unexpected "
1654 				  "result %i of gfc_dep_compare_expr", compval);
1655 	      break;
1656 	  }
1657 	}
1658     }
1659 
1660   /* Check array shape.  */
1661   if (!r1->attr.allocatable && !r1->attr.pointer && r1->as && r2->as)
1662     {
1663       int i, compval;
1664       gfc_expr *shape1, *shape2;
1665 
1666       if (r1->as->type != r2->as->type)
1667 	{
1668 	  snprintf (errmsg, err_len, "Shape mismatch in function result");
1669 	  return false;
1670 	}
1671 
1672       if (r1->as->type == AS_EXPLICIT)
1673 	for (i = 0; i < r1->as->rank + r1->as->corank; i++)
1674 	  {
1675 	    shape1 = gfc_subtract (gfc_copy_expr (r1->as->upper[i]),
1676 				   gfc_copy_expr (r1->as->lower[i]));
1677 	    shape2 = gfc_subtract (gfc_copy_expr (r2->as->upper[i]),
1678 				   gfc_copy_expr (r2->as->lower[i]));
1679 	    compval = gfc_dep_compare_expr (shape1, shape2);
1680 	    gfc_free_expr (shape1);
1681 	    gfc_free_expr (shape2);
1682 	    switch (compval)
1683 	    {
1684 	      case -1:
1685 	      case  1:
1686 	      case -3:
1687 		snprintf (errmsg, err_len, "Shape mismatch in dimension %i of "
1688 			  "function result", i + 1);
1689 		return false;
1690 
1691 	      case -2:
1692 		/* FIXME: Implement a warning for this case.
1693 		gfc_warning (0, "Possible shape mismatch in return value");*/
1694 		break;
1695 
1696 	      case 0:
1697 		break;
1698 
1699 	      default:
1700 		gfc_internal_error ("check_result_characteristics (2): "
1701 				    "Unexpected result %i of "
1702 				    "gfc_dep_compare_expr", compval);
1703 		break;
1704 	    }
1705 	  }
1706     }
1707 
1708   return true;
1709 }
1710 
1711 
1712 /* 'Compare' two formal interfaces associated with a pair of symbols.
1713    We return true if there exists an actual argument list that
1714    would be ambiguous between the two interfaces, zero otherwise.
1715    'strict_flag' specifies whether all the characteristics are
1716    required to match, which is not the case for ambiguity checks.
1717    'p1' and 'p2' are the PASS arguments of both procedures (if applicable).  */
1718 
1719 bool
gfc_compare_interfaces(gfc_symbol * s1,gfc_symbol * s2,const char * name2,int generic_flag,int strict_flag,char * errmsg,int err_len,const char * p1,const char * p2,bool * bad_result_characteristics)1720 gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
1721 			int generic_flag, int strict_flag,
1722 			char *errmsg, int err_len,
1723 			const char *p1, const char *p2,
1724 			bool *bad_result_characteristics)
1725 {
1726   gfc_formal_arglist *f1, *f2;
1727 
1728   gcc_assert (name2 != NULL);
1729 
1730   if (bad_result_characteristics)
1731     *bad_result_characteristics = false;
1732 
1733   if (s1->attr.function && (s2->attr.subroutine
1734       || (!s2->attr.function && s2->ts.type == BT_UNKNOWN
1735 	  && gfc_get_default_type (name2, s2->ns)->type == BT_UNKNOWN)))
1736     {
1737       if (errmsg != NULL)
1738 	snprintf (errmsg, err_len, "'%s' is not a function", name2);
1739       return false;
1740     }
1741 
1742   if (s1->attr.subroutine && s2->attr.function)
1743     {
1744       if (errmsg != NULL)
1745 	snprintf (errmsg, err_len, "'%s' is not a subroutine", name2);
1746       return false;
1747     }
1748 
1749   if (s2->attr.subroutine && s1->attr.flavor == FL_VARIABLE)
1750     {
1751       if (errmsg != NULL)
1752 	snprintf (errmsg, err_len, "subroutine proc pointer '%s' passed "
1753 		  "to dummy variable '%s'", name2, s1->name);
1754       return false;
1755     }
1756 
1757   /* Do strict checks on all characteristics
1758      (for dummy procedures and procedure pointer assignments).  */
1759   if (!generic_flag && strict_flag)
1760     {
1761       if (s1->attr.function && s2->attr.function)
1762 	{
1763 	  /* If both are functions, check result characteristics.  */
1764 	  if (!gfc_check_result_characteristics (s1, s2, errmsg, err_len)
1765 	      || !gfc_check_result_characteristics (s2, s1, errmsg, err_len))
1766 	    {
1767 	      if (bad_result_characteristics)
1768 		*bad_result_characteristics = true;
1769 	      return false;
1770 	    }
1771 	}
1772 
1773       if (s1->attr.pure && !s2->attr.pure)
1774 	{
1775 	  snprintf (errmsg, err_len, "Mismatch in PURE attribute");
1776 	  return false;
1777 	}
1778       if (s1->attr.elemental && !s2->attr.elemental)
1779 	{
1780 	  snprintf (errmsg, err_len, "Mismatch in ELEMENTAL attribute");
1781 	  return false;
1782 	}
1783     }
1784 
1785   if (s1->attr.if_source == IFSRC_UNKNOWN
1786       || s2->attr.if_source == IFSRC_UNKNOWN)
1787     return true;
1788 
1789   f1 = gfc_sym_get_dummy_args (s1);
1790   f2 = gfc_sym_get_dummy_args (s2);
1791 
1792   /* Special case: No arguments.  */
1793   if (f1 == NULL && f2 == NULL)
1794     return true;
1795 
1796   if (generic_flag)
1797     {
1798       if (count_types_test (f1, f2, p1, p2)
1799 	  || count_types_test (f2, f1, p2, p1))
1800 	return false;
1801 
1802       /* Special case: alternate returns.  If both f1->sym and f2->sym are
1803 	 NULL, then the leading formal arguments are alternate returns.
1804 	 The previous conditional should catch argument lists with
1805 	 different number of argument.  */
1806       if (f1 && f1->sym == NULL && f2 && f2->sym == NULL)
1807 	return true;
1808 
1809       if (generic_correspondence (f1, f2, p1, p2)
1810 	  || generic_correspondence (f2, f1, p2, p1))
1811 	return false;
1812     }
1813   else
1814     /* Perform the abbreviated correspondence test for operators (the
1815        arguments cannot be optional and are always ordered correctly).
1816        This is also done when comparing interfaces for dummy procedures and in
1817        procedure pointer assignments.  */
1818 
1819     for (; f1 || f2; f1 = f1->next, f2 = f2->next)
1820       {
1821 	/* Check existence.  */
1822 	if (f1 == NULL || f2 == NULL)
1823 	  {
1824 	    if (errmsg != NULL)
1825 	      snprintf (errmsg, err_len, "'%s' has the wrong number of "
1826 			"arguments", name2);
1827 	    return false;
1828 	  }
1829 
1830 	if (strict_flag)
1831 	  {
1832 	    /* Check all characteristics.  */
1833 	    if (!gfc_check_dummy_characteristics (f1->sym, f2->sym, true,
1834 					      errmsg, err_len))
1835 	      return false;
1836 	  }
1837 	else
1838 	  {
1839 	    /* Operators: Only check type and rank of arguments.  */
1840 	    if (!compare_type (f2->sym, f1->sym))
1841 	      {
1842 		if (errmsg != NULL)
1843 		  snprintf (errmsg, err_len, "Type mismatch in argument '%s' "
1844 			    "(%s/%s)", f1->sym->name,
1845 			    gfc_typename (&f1->sym->ts),
1846 			    gfc_typename (&f2->sym->ts));
1847 		return false;
1848 	      }
1849 	    if (!compare_rank (f2->sym, f1->sym))
1850 	      {
1851 		if (errmsg != NULL)
1852 		  snprintf (errmsg, err_len, "Rank mismatch in argument "
1853 			    "'%s' (%i/%i)", f1->sym->name,
1854 			    symbol_rank (f1->sym), symbol_rank (f2->sym));
1855 		return false;
1856 	      }
1857 	    if ((gfc_option.allow_std & GFC_STD_F2008)
1858 		&& (compare_ptr_alloc(f1->sym, f2->sym)
1859 		    || compare_ptr_alloc(f2->sym, f1->sym)))
1860 	      {
1861     		if (errmsg != NULL)
1862 		  snprintf (errmsg, err_len, "Mismatching POINTER/ALLOCATABLE "
1863 			    "attribute in argument '%s' ", f1->sym->name);
1864 		return false;
1865 	      }
1866 	  }
1867       }
1868 
1869   return true;
1870 }
1871 
1872 
1873 /* Given a pointer to an interface pointer, remove duplicate
1874    interfaces and make sure that all symbols are either functions
1875    or subroutines, and all of the same kind.  Returns true if
1876    something goes wrong.  */
1877 
1878 static bool
check_interface0(gfc_interface * p,const char * interface_name)1879 check_interface0 (gfc_interface *p, const char *interface_name)
1880 {
1881   gfc_interface *psave, *q, *qlast;
1882 
1883   psave = p;
1884   for (; p; p = p->next)
1885     {
1886       /* Make sure all symbols in the interface have been defined as
1887 	 functions or subroutines.  */
1888       if (((!p->sym->attr.function && !p->sym->attr.subroutine)
1889 	   || !p->sym->attr.if_source)
1890 	  && !gfc_fl_struct (p->sym->attr.flavor))
1891 	{
1892 	  const char *guessed
1893 	    = gfc_lookup_function_fuzzy (p->sym->name, p->sym->ns->sym_root);
1894 
1895 	  if (p->sym->attr.external)
1896 	    if (guessed)
1897 	      gfc_error ("Procedure %qs in %s at %L has no explicit interface"
1898 			 "; did you mean %qs?",
1899 			 p->sym->name, interface_name, &p->sym->declared_at,
1900 			 guessed);
1901 	    else
1902 	      gfc_error ("Procedure %qs in %s at %L has no explicit interface",
1903 			 p->sym->name, interface_name, &p->sym->declared_at);
1904 	  else
1905 	    if (guessed)
1906 	      gfc_error ("Procedure %qs in %s at %L is neither function nor "
1907 			 "subroutine; did you mean %qs?", p->sym->name,
1908 			interface_name, &p->sym->declared_at, guessed);
1909 	    else
1910 	      gfc_error ("Procedure %qs in %s at %L is neither function nor "
1911 			 "subroutine", p->sym->name, interface_name,
1912 			&p->sym->declared_at);
1913 	  return true;
1914 	}
1915 
1916       /* Verify that procedures are either all SUBROUTINEs or all FUNCTIONs.  */
1917       if ((psave->sym->attr.function && !p->sym->attr.function
1918 	   && !gfc_fl_struct (p->sym->attr.flavor))
1919 	  || (psave->sym->attr.subroutine && !p->sym->attr.subroutine))
1920 	{
1921 	  if (!gfc_fl_struct (p->sym->attr.flavor))
1922 	    gfc_error ("In %s at %L procedures must be either all SUBROUTINEs"
1923 		       " or all FUNCTIONs", interface_name,
1924 		       &p->sym->declared_at);
1925 	  else if (p->sym->attr.flavor == FL_DERIVED)
1926 	    gfc_error ("In %s at %L procedures must be all FUNCTIONs as the "
1927 		       "generic name is also the name of a derived type",
1928 		       interface_name, &p->sym->declared_at);
1929 	  return true;
1930 	}
1931 
1932       /* F2003, C1207. F2008, C1207.  */
1933       if (p->sym->attr.proc == PROC_INTERNAL
1934 	  && !gfc_notify_std (GFC_STD_F2008, "Internal procedure "
1935 			      "%qs in %s at %L", p->sym->name,
1936 			      interface_name, &p->sym->declared_at))
1937 	return true;
1938     }
1939   p = psave;
1940 
1941   /* Remove duplicate interfaces in this interface list.  */
1942   for (; p; p = p->next)
1943     {
1944       qlast = p;
1945 
1946       for (q = p->next; q;)
1947 	{
1948 	  if (p->sym != q->sym)
1949 	    {
1950 	      qlast = q;
1951 	      q = q->next;
1952 	    }
1953 	  else
1954 	    {
1955 	      /* Duplicate interface.  */
1956 	      qlast->next = q->next;
1957 	      free (q);
1958 	      q = qlast->next;
1959 	    }
1960 	}
1961     }
1962 
1963   return false;
1964 }
1965 
1966 
1967 /* Check lists of interfaces to make sure that no two interfaces are
1968    ambiguous.  Duplicate interfaces (from the same symbol) are OK here.  */
1969 
1970 static bool
check_interface1(gfc_interface * p,gfc_interface * q0,int generic_flag,const char * interface_name,bool referenced)1971 check_interface1 (gfc_interface *p, gfc_interface *q0,
1972 		  int generic_flag, const char *interface_name,
1973 		  bool referenced)
1974 {
1975   gfc_interface *q;
1976   for (; p; p = p->next)
1977     for (q = q0; q; q = q->next)
1978       {
1979 	if (p->sym == q->sym)
1980 	  continue;		/* Duplicates OK here.  */
1981 
1982 	if (p->sym->name == q->sym->name && p->sym->module == q->sym->module)
1983 	  continue;
1984 
1985 	if (!gfc_fl_struct (p->sym->attr.flavor)
1986 	    && !gfc_fl_struct (q->sym->attr.flavor)
1987 	    && gfc_compare_interfaces (p->sym, q->sym, q->sym->name,
1988 				       generic_flag, 0, NULL, 0, NULL, NULL))
1989 	  {
1990 	    if (referenced)
1991 	      gfc_error ("Ambiguous interfaces in %s for %qs at %L "
1992 			 "and %qs at %L", interface_name,
1993 			 q->sym->name, &q->sym->declared_at,
1994 			 p->sym->name, &p->sym->declared_at);
1995 	    else if (!p->sym->attr.use_assoc && q->sym->attr.use_assoc)
1996 	      gfc_warning (0, "Ambiguous interfaces in %s for %qs at %L "
1997 			 "and %qs at %L", interface_name,
1998 			 q->sym->name, &q->sym->declared_at,
1999 			 p->sym->name, &p->sym->declared_at);
2000 	    else
2001 	      gfc_warning (0, "Although not referenced, %qs has ambiguous "
2002 			   "interfaces at %L", interface_name, &p->where);
2003 	    return true;
2004 	  }
2005       }
2006   return false;
2007 }
2008 
2009 
2010 /* Check the generic and operator interfaces of symbols to make sure
2011    that none of the interfaces conflict.  The check has to be done
2012    after all of the symbols are actually loaded.  */
2013 
2014 static void
check_sym_interfaces(gfc_symbol * sym)2015 check_sym_interfaces (gfc_symbol *sym)
2016 {
2017   /* Provide sufficient space to hold "generic interface 'symbol.symbol'".  */
2018   char interface_name[2*GFC_MAX_SYMBOL_LEN+2 + sizeof("generic interface ''")];
2019   gfc_interface *p;
2020 
2021   if (sym->ns != gfc_current_ns)
2022     return;
2023 
2024   if (sym->generic != NULL)
2025     {
2026       size_t len = strlen (sym->name) + sizeof("generic interface ''");
2027       gcc_assert (len < sizeof (interface_name));
2028       sprintf (interface_name, "generic interface '%s'", sym->name);
2029       if (check_interface0 (sym->generic, interface_name))
2030 	return;
2031 
2032       for (p = sym->generic; p; p = p->next)
2033 	{
2034 	  if (p->sym->attr.mod_proc
2035 	      && !p->sym->attr.module_procedure
2036 	      && (p->sym->attr.if_source != IFSRC_DECL
2037 		  || p->sym->attr.procedure))
2038 	    {
2039 	      gfc_error ("%qs at %L is not a module procedure",
2040 			 p->sym->name, &p->where);
2041 	      return;
2042 	    }
2043 	}
2044 
2045       /* Originally, this test was applied to host interfaces too;
2046 	 this is incorrect since host associated symbols, from any
2047 	 source, cannot be ambiguous with local symbols.  */
2048       check_interface1 (sym->generic, sym->generic, 1, interface_name,
2049 			sym->attr.referenced || !sym->attr.use_assoc);
2050     }
2051 }
2052 
2053 
2054 static void
check_uop_interfaces(gfc_user_op * uop)2055 check_uop_interfaces (gfc_user_op *uop)
2056 {
2057   char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("operator interface ''")];
2058   gfc_user_op *uop2;
2059   gfc_namespace *ns;
2060 
2061   sprintf (interface_name, "operator interface '%s'", uop->name);
2062   if (check_interface0 (uop->op, interface_name))
2063     return;
2064 
2065   for (ns = gfc_current_ns; ns; ns = ns->parent)
2066     {
2067       uop2 = gfc_find_uop (uop->name, ns);
2068       if (uop2 == NULL)
2069 	continue;
2070 
2071       check_interface1 (uop->op, uop2->op, 0,
2072 			interface_name, true);
2073     }
2074 }
2075 
2076 /* Given an intrinsic op, return an equivalent op if one exists,
2077    or INTRINSIC_NONE otherwise.  */
2078 
2079 gfc_intrinsic_op
gfc_equivalent_op(gfc_intrinsic_op op)2080 gfc_equivalent_op (gfc_intrinsic_op op)
2081 {
2082   switch(op)
2083     {
2084     case INTRINSIC_EQ:
2085       return INTRINSIC_EQ_OS;
2086 
2087     case INTRINSIC_EQ_OS:
2088       return INTRINSIC_EQ;
2089 
2090     case INTRINSIC_NE:
2091       return INTRINSIC_NE_OS;
2092 
2093     case INTRINSIC_NE_OS:
2094       return INTRINSIC_NE;
2095 
2096     case INTRINSIC_GT:
2097       return INTRINSIC_GT_OS;
2098 
2099     case INTRINSIC_GT_OS:
2100       return INTRINSIC_GT;
2101 
2102     case INTRINSIC_GE:
2103       return INTRINSIC_GE_OS;
2104 
2105     case INTRINSIC_GE_OS:
2106       return INTRINSIC_GE;
2107 
2108     case INTRINSIC_LT:
2109       return INTRINSIC_LT_OS;
2110 
2111     case INTRINSIC_LT_OS:
2112       return INTRINSIC_LT;
2113 
2114     case INTRINSIC_LE:
2115       return INTRINSIC_LE_OS;
2116 
2117     case INTRINSIC_LE_OS:
2118       return INTRINSIC_LE;
2119 
2120     default:
2121       return INTRINSIC_NONE;
2122     }
2123 }
2124 
2125 /* For the namespace, check generic, user operator and intrinsic
2126    operator interfaces for consistency and to remove duplicate
2127    interfaces.  We traverse the whole namespace, counting on the fact
2128    that most symbols will not have generic or operator interfaces.  */
2129 
2130 void
gfc_check_interfaces(gfc_namespace * ns)2131 gfc_check_interfaces (gfc_namespace *ns)
2132 {
2133   gfc_namespace *old_ns, *ns2;
2134   char interface_name[GFC_MAX_SYMBOL_LEN + sizeof("intrinsic '' operator")];
2135   int i;
2136 
2137   old_ns = gfc_current_ns;
2138   gfc_current_ns = ns;
2139 
2140   gfc_traverse_ns (ns, check_sym_interfaces);
2141 
2142   gfc_traverse_user_op (ns, check_uop_interfaces);
2143 
2144   for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2145     {
2146       if (i == INTRINSIC_USER)
2147 	continue;
2148 
2149       if (i == INTRINSIC_ASSIGN)
2150 	strcpy (interface_name, "intrinsic assignment operator");
2151       else
2152 	sprintf (interface_name, "intrinsic '%s' operator",
2153 		 gfc_op2string ((gfc_intrinsic_op) i));
2154 
2155       if (check_interface0 (ns->op[i], interface_name))
2156 	continue;
2157 
2158       if (ns->op[i])
2159 	gfc_check_operator_interface (ns->op[i]->sym, (gfc_intrinsic_op) i,
2160 				      ns->op[i]->where);
2161 
2162       for (ns2 = ns; ns2; ns2 = ns2->parent)
2163 	{
2164 	  gfc_intrinsic_op other_op;
2165 
2166 	  if (check_interface1 (ns->op[i], ns2->op[i], 0,
2167 				interface_name, true))
2168 	    goto done;
2169 
2170 	  /* i should be gfc_intrinsic_op, but has to be int with this cast
2171 	     here for stupid C++ compatibility rules.  */
2172 	  other_op = gfc_equivalent_op ((gfc_intrinsic_op) i);
2173 	  if (other_op != INTRINSIC_NONE
2174 	    &&  check_interface1 (ns->op[i], ns2->op[other_op],
2175 				  0, interface_name, true))
2176 	    goto done;
2177 	}
2178     }
2179 
2180 done:
2181   gfc_current_ns = old_ns;
2182 }
2183 
2184 
2185 /* Given a symbol of a formal argument list and an expression, if the
2186    formal argument is allocatable, check that the actual argument is
2187    allocatable. Returns true if compatible, zero if not compatible.  */
2188 
2189 static bool
compare_allocatable(gfc_symbol * formal,gfc_expr * actual)2190 compare_allocatable (gfc_symbol *formal, gfc_expr *actual)
2191 {
2192   if (formal->attr.allocatable
2193       || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)->attr.allocatable))
2194     {
2195       symbol_attribute attr = gfc_expr_attr (actual);
2196       if (actual->ts.type == BT_CLASS && !attr.class_ok)
2197 	return true;
2198       else if (!attr.allocatable)
2199 	return false;
2200     }
2201 
2202   return true;
2203 }
2204 
2205 
2206 /* Given a symbol of a formal argument list and an expression, if the
2207    formal argument is a pointer, see if the actual argument is a
2208    pointer. Returns nonzero if compatible, zero if not compatible.  */
2209 
2210 static int
compare_pointer(gfc_symbol * formal,gfc_expr * actual)2211 compare_pointer (gfc_symbol *formal, gfc_expr *actual)
2212 {
2213   symbol_attribute attr;
2214 
2215   if (formal->attr.pointer
2216       || (formal->ts.type == BT_CLASS && CLASS_DATA (formal)
2217 	  && CLASS_DATA (formal)->attr.class_pointer))
2218     {
2219       attr = gfc_expr_attr (actual);
2220 
2221       /* Fortran 2008 allows non-pointer actual arguments.  */
2222       if (!attr.pointer && attr.target && formal->attr.intent == INTENT_IN)
2223 	return 2;
2224 
2225       if (!attr.pointer)
2226 	return 0;
2227     }
2228 
2229   return 1;
2230 }
2231 
2232 
2233 /* Emit clear error messages for rank mismatch.  */
2234 
2235 static void
argument_rank_mismatch(const char * name,locus * where,int rank1,int rank2,locus * where_formal)2236 argument_rank_mismatch (const char *name, locus *where,
2237 			int rank1, int rank2, locus *where_formal)
2238 {
2239 
2240   /* TS 29113, C407b.  */
2241   if (where_formal == NULL)
2242     {
2243       if (rank2 == -1)
2244 	gfc_error ("The assumed-rank array at %L requires that the dummy "
2245 		   "argument %qs has assumed-rank", where, name);
2246       else if (rank1 == 0)
2247 	gfc_error_opt (0, "Rank mismatch in argument %qs "
2248 		       "at %L (scalar and rank-%d)", name, where, rank2);
2249       else if (rank2 == 0)
2250 	gfc_error_opt (0, "Rank mismatch in argument %qs "
2251 		       "at %L (rank-%d and scalar)", name, where, rank1);
2252       else
2253 	gfc_error_opt (0, "Rank mismatch in argument %qs "
2254 		       "at %L (rank-%d and rank-%d)", name, where, rank1,
2255 		       rank2);
2256     }
2257   else
2258     {
2259       if (rank2 == -1)
2260 	/* This is an assumed rank-actual passed to a function without
2261 	   an explicit interface, which is already diagnosed in
2262 	   gfc_procedure_use.  */
2263 	return;
2264       if (rank1 == 0)
2265 	gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2266 		       "and actual argument at %L (scalar and rank-%d)",
2267 		       where, where_formal, rank2);
2268       else if (rank2 == 0)
2269 	gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2270 		       "and actual argument at %L (rank-%d and scalar)",
2271 		       where, where_formal, rank1);
2272       else
2273 	gfc_error_opt (0, "Rank mismatch between actual argument at %L "
2274 		       "and actual argument at %L (rank-%d and rank-%d)", where,
2275 		       where_formal, rank1, rank2);
2276     }
2277 }
2278 
2279 
2280 /* Under certain conditions, a scalar actual argument can be passed
2281    to an array dummy argument - see F2018, 15.5.2.4, paragraph 14.
2282    This function returns true for these conditions so that an error
2283    or warning for this can be suppressed later.  Always return false
2284    for expressions with rank > 0.  */
2285 
2286 bool
maybe_dummy_array_arg(gfc_expr * e)2287 maybe_dummy_array_arg (gfc_expr *e)
2288 {
2289   gfc_symbol *s;
2290   gfc_ref *ref;
2291   bool array_pointer = false;
2292   bool assumed_shape = false;
2293   bool scalar_ref = true;
2294 
2295   if (e->rank > 0)
2296     return false;
2297 
2298   if (e->ts.type == BT_CHARACTER && e->ts.kind == 1)
2299     return true;
2300 
2301   /* If this comes from a constructor, it has been an array element
2302      originally.  */
2303 
2304   if (e->expr_type == EXPR_CONSTANT)
2305     return e->from_constructor;
2306 
2307   if (e->expr_type != EXPR_VARIABLE)
2308     return false;
2309 
2310   s = e->symtree->n.sym;
2311 
2312   if (s->attr.dimension)
2313     {
2314       scalar_ref = false;
2315       array_pointer = s->attr.pointer;
2316     }
2317 
2318   if (s->as && s->as->type == AS_ASSUMED_SHAPE)
2319     assumed_shape = true;
2320 
2321   for (ref=e->ref; ref; ref=ref->next)
2322     {
2323       if (ref->type == REF_COMPONENT)
2324 	{
2325 	  symbol_attribute *attr;
2326 	  attr = &ref->u.c.component->attr;
2327 	  if (attr->dimension)
2328 	    {
2329 	      array_pointer = attr->pointer;
2330 	      assumed_shape = false;
2331 	      scalar_ref = false;
2332 	    }
2333 	  else
2334 	    scalar_ref = true;
2335 	}
2336     }
2337 
2338   return !(scalar_ref || array_pointer || assumed_shape);
2339 }
2340 
2341 /* Given a symbol of a formal argument list and an expression, see if
2342    the two are compatible as arguments.  Returns true if
2343    compatible, false if not compatible.  */
2344 
2345 static bool
compare_parameter(gfc_symbol * formal,gfc_expr * actual,int ranks_must_agree,int is_elemental,locus * where)2346 compare_parameter (gfc_symbol *formal, gfc_expr *actual,
2347 		   int ranks_must_agree, int is_elemental, locus *where)
2348 {
2349   gfc_ref *ref;
2350   bool rank_check, is_pointer;
2351   char err[200];
2352   gfc_component *ppc;
2353   bool codimension = false;
2354 
2355   /* If the formal arg has type BT_VOID, it's to one of the iso_c_binding
2356      procs c_f_pointer or c_f_procpointer, and we need to accept most
2357      pointers the user could give us.  This should allow that.  */
2358   if (formal->ts.type == BT_VOID)
2359     return true;
2360 
2361   if (formal->ts.type == BT_DERIVED
2362       && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
2363       && actual->ts.type == BT_DERIVED
2364       && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
2365     {
2366       if (formal->ts.u.derived->intmod_sym_id
2367 	  != actual->ts.u.derived->intmod_sym_id)
2368 	return false;
2369 
2370       if (ranks_must_agree
2371 	  && symbol_rank (formal) != actual->rank
2372 	  && symbol_rank (formal) != -1)
2373 	{
2374 	  if (where)
2375 	    argument_rank_mismatch (formal->name, &actual->where,
2376 				    symbol_rank (formal), actual->rank,
2377 				    NULL);
2378 	  return false;
2379 	}
2380       return true;
2381     }
2382 
2383   if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
2384     /* Make sure the vtab symbol is present when
2385        the module variables are generated.  */
2386     gfc_find_derived_vtab (actual->ts.u.derived);
2387 
2388   if (actual->ts.type == BT_PROCEDURE)
2389     {
2390       gfc_symbol *act_sym = actual->symtree->n.sym;
2391 
2392       if (formal->attr.flavor != FL_PROCEDURE && !act_sym->ts.interface)
2393 	{
2394 	  if (where)
2395 	    gfc_error ("Invalid procedure argument at %L", &actual->where);
2396 	  return false;
2397 	}
2398       else if (act_sym->ts.interface
2399 	       && !gfc_compare_interfaces (formal, act_sym->ts.interface,
2400 					   act_sym->name, 0, 1, err,
2401 					   sizeof(err),NULL, NULL))
2402 	{
2403 	  if (where)
2404 	    gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2405 			   " %s", formal->name, &actual->where, err);
2406 	  return false;
2407 	}
2408 
2409       if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
2410 				   sizeof(err), NULL, NULL))
2411 	{
2412 	  if (where)
2413 	    gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2414 			   " %s", formal->name, &actual->where, err);
2415 	  return false;
2416 	}
2417 
2418       if (formal->attr.function && !act_sym->attr.function)
2419 	{
2420 	  gfc_add_function (&act_sym->attr, act_sym->name,
2421 	  &act_sym->declared_at);
2422 	  if (act_sym->ts.type == BT_UNKNOWN
2423 	      && !gfc_set_default_type (act_sym, 1, act_sym->ns))
2424 	    return false;
2425 	}
2426       else if (formal->attr.subroutine && !act_sym->attr.subroutine)
2427 	gfc_add_subroutine (&act_sym->attr, act_sym->name,
2428 			    &act_sym->declared_at);
2429 
2430       return true;
2431     }
2432 
2433   ppc = gfc_get_proc_ptr_comp (actual);
2434   if (ppc && ppc->ts.interface)
2435     {
2436       if (!gfc_compare_interfaces (formal, ppc->ts.interface, ppc->name, 0, 1,
2437 				   err, sizeof(err), NULL, NULL))
2438 	{
2439 	  if (where)
2440 	    gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2441 			   " %s", formal->name, &actual->where, err);
2442 	  return false;
2443 	}
2444     }
2445 
2446   /* F2008, C1241.  */
2447   if (formal->attr.pointer && formal->attr.contiguous
2448       && !gfc_is_simply_contiguous (actual, true, false))
2449     {
2450       if (where)
2451 	gfc_error ("Actual argument to contiguous pointer dummy %qs at %L "
2452 		   "must be simply contiguous", formal->name, &actual->where);
2453       return false;
2454     }
2455 
2456   symbol_attribute actual_attr = gfc_expr_attr (actual);
2457   if (actual->ts.type == BT_CLASS && !actual_attr.class_ok)
2458     return true;
2459 
2460   if ((actual->expr_type != EXPR_NULL || actual->ts.type != BT_UNKNOWN)
2461       && actual->ts.type != BT_HOLLERITH
2462       && formal->ts.type != BT_ASSUMED
2463       && !(formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2464       && !gfc_compare_types (&formal->ts, &actual->ts)
2465       && !(formal->ts.type == BT_DERIVED && actual->ts.type == BT_CLASS
2466 	   && gfc_compare_derived_types (formal->ts.u.derived,
2467 					 CLASS_DATA (actual)->ts.u.derived)))
2468     {
2469       if (where)
2470 	{
2471 	  if (formal->attr.artificial)
2472 	    {
2473 	      if (!flag_allow_argument_mismatch || !formal->error)
2474 		gfc_error_opt (0, "Type mismatch between actual argument at %L "
2475 			       "and actual argument at %L (%s/%s).",
2476 			       &actual->where,
2477 			       &formal->declared_at,
2478 			       gfc_typename (actual),
2479 			       gfc_dummy_typename (&formal->ts));
2480 
2481 	      formal->error = 1;
2482 	    }
2483 	  else
2484 	    gfc_error_opt (0, "Type mismatch in argument %qs at %L; passed %s "
2485 			   "to %s", formal->name, where, gfc_typename (actual),
2486 			   gfc_dummy_typename (&formal->ts));
2487 	}
2488       return false;
2489     }
2490 
2491   if (actual->ts.type == BT_ASSUMED && formal->ts.type != BT_ASSUMED)
2492     {
2493       if (where)
2494 	gfc_error ("Assumed-type actual argument at %L requires that dummy "
2495 		   "argument %qs is of assumed type", &actual->where,
2496 		   formal->name);
2497       return false;
2498     }
2499 
2500   /* TS29113 C407c; F2018 C711.  */
2501   if (actual->ts.type == BT_ASSUMED
2502       && symbol_rank (formal) == -1
2503       && actual->rank != -1
2504       && !(actual->symtree->n.sym->as
2505 	   && actual->symtree->n.sym->as->type == AS_ASSUMED_SHAPE))
2506     {
2507       if (where)
2508 	gfc_error ("Assumed-type actual argument at %L corresponding to "
2509 		   "assumed-rank dummy argument %qs must be "
2510 		   "assumed-shape or assumed-rank",
2511 		   &actual->where, formal->name);
2512       return false;
2513     }
2514 
2515   /* F2008, 12.5.2.5; IR F08/0073.  */
2516   if (formal->ts.type == BT_CLASS && formal->attr.class_ok
2517       && actual->expr_type != EXPR_NULL
2518       && ((CLASS_DATA (formal)->attr.class_pointer
2519 	   && formal->attr.intent != INTENT_IN)
2520           || CLASS_DATA (formal)->attr.allocatable))
2521     {
2522       if (actual->ts.type != BT_CLASS)
2523 	{
2524 	  if (where)
2525 	    gfc_error ("Actual argument to %qs at %L must be polymorphic",
2526 			formal->name, &actual->where);
2527 	  return false;
2528 	}
2529 
2530       if ((!UNLIMITED_POLY (formal) || !UNLIMITED_POLY(actual))
2531 	  && !gfc_compare_derived_types (CLASS_DATA (actual)->ts.u.derived,
2532 					 CLASS_DATA (formal)->ts.u.derived))
2533 	{
2534 	  if (where)
2535 	    gfc_error ("Actual argument to %qs at %L must have the same "
2536 		       "declared type", formal->name, &actual->where);
2537 	  return false;
2538 	}
2539     }
2540 
2541   /* F08: 12.5.2.5 Allocatable and pointer dummy variables.  However, this
2542      is necessary also for F03, so retain error for both.
2543      NOTE: Other type/kind errors pre-empt this error.  Since they are F03
2544      compatible, no attempt has been made to channel to this one.  */
2545   if (UNLIMITED_POLY (formal) && !UNLIMITED_POLY (actual)
2546       && (CLASS_DATA (formal)->attr.allocatable
2547 	  ||CLASS_DATA (formal)->attr.class_pointer))
2548     {
2549       if (where)
2550 	gfc_error ("Actual argument to %qs at %L must be unlimited "
2551 		   "polymorphic since the formal argument is a "
2552 		   "pointer or allocatable unlimited polymorphic "
2553 		   "entity [F2008: 12.5.2.5]", formal->name,
2554 		   &actual->where);
2555       return false;
2556     }
2557 
2558   if (formal->ts.type == BT_CLASS && formal->attr.class_ok)
2559     codimension = CLASS_DATA (formal)->attr.codimension;
2560   else
2561     codimension = formal->attr.codimension;
2562 
2563   if (codimension && !gfc_is_coarray (actual))
2564     {
2565       if (where)
2566 	gfc_error ("Actual argument to %qs at %L must be a coarray",
2567 		       formal->name, &actual->where);
2568       return false;
2569     }
2570 
2571   if (codimension && formal->attr.allocatable)
2572     {
2573       gfc_ref *last = NULL;
2574 
2575       for (ref = actual->ref; ref; ref = ref->next)
2576 	if (ref->type == REF_COMPONENT)
2577 	  last = ref;
2578 
2579       /* F2008, 12.5.2.6.  */
2580       if ((last && last->u.c.component->as->corank != formal->as->corank)
2581 	  || (!last
2582 	      && actual->symtree->n.sym->as->corank != formal->as->corank))
2583 	{
2584 	  if (where)
2585 	    gfc_error ("Corank mismatch in argument %qs at %L (%d and %d)",
2586 		   formal->name, &actual->where, formal->as->corank,
2587 		   last ? last->u.c.component->as->corank
2588 			: actual->symtree->n.sym->as->corank);
2589 	  return false;
2590 	}
2591     }
2592 
2593   if (codimension)
2594     {
2595       /* F2008, 12.5.2.8 + Corrig 2 (IR F08/0048).  */
2596       /* F2018, 12.5.2.8.  */
2597       if (formal->attr.dimension
2598 	  && (formal->attr.contiguous || formal->as->type != AS_ASSUMED_SHAPE)
2599 	  && actual_attr.dimension
2600 	  && !gfc_is_simply_contiguous (actual, true, true))
2601 	{
2602 	  if (where)
2603 	    gfc_error ("Actual argument to %qs at %L must be simply "
2604 		       "contiguous or an element of such an array",
2605 		       formal->name, &actual->where);
2606 	  return false;
2607 	}
2608 
2609       /* F2008, C1303 and C1304.  */
2610       if (formal->attr.intent != INTENT_INOUT
2611 	  && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2612 	       && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2613 	       && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
2614 	      || formal->attr.lock_comp))
2615 
2616     	{
2617 	  if (where)
2618 	    gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2619 		       "which is LOCK_TYPE or has a LOCK_TYPE component",
2620 		       formal->name, &actual->where);
2621 	  return false;
2622 	}
2623 
2624       /* TS18508, C702/C703.  */
2625       if (formal->attr.intent != INTENT_INOUT
2626 	  && (((formal->ts.type == BT_DERIVED || formal->ts.type == BT_CLASS)
2627 	       && formal->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
2628 	       && formal->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
2629 	      || formal->attr.event_comp))
2630 
2631     	{
2632 	  if (where)
2633 	    gfc_error ("Actual argument to non-INTENT(INOUT) dummy %qs at %L, "
2634 		       "which is EVENT_TYPE or has a EVENT_TYPE component",
2635 		       formal->name, &actual->where);
2636 	  return false;
2637 	}
2638     }
2639 
2640   /* F2008, C1239/C1240.  */
2641   if (actual->expr_type == EXPR_VARIABLE
2642       && (actual->symtree->n.sym->attr.asynchronous
2643          || actual->symtree->n.sym->attr.volatile_)
2644       &&  (formal->attr.asynchronous || formal->attr.volatile_)
2645       && actual->rank && formal->as
2646       && !gfc_is_simply_contiguous (actual, true, false)
2647       && ((formal->as->type != AS_ASSUMED_SHAPE
2648 	   && formal->as->type != AS_ASSUMED_RANK && !formal->attr.pointer)
2649 	  || formal->attr.contiguous))
2650     {
2651       if (where)
2652 	gfc_error ("Dummy argument %qs has to be a pointer, assumed-shape or "
2653 		   "assumed-rank array without CONTIGUOUS attribute - as actual"
2654 		   " argument at %L is not simply contiguous and both are "
2655 		   "ASYNCHRONOUS or VOLATILE", formal->name, &actual->where);
2656       return false;
2657     }
2658 
2659   if (formal->attr.allocatable && !codimension
2660       && actual_attr.codimension)
2661     {
2662       if (formal->attr.intent == INTENT_OUT)
2663 	{
2664 	  if (where)
2665 	    gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
2666 		       "INTENT(OUT) dummy argument %qs", &actual->where,
2667 		       formal->name);
2668 	  return false;
2669 	}
2670       else if (warn_surprising && where && formal->attr.intent != INTENT_IN)
2671 	gfc_warning (OPT_Wsurprising,
2672 		     "Passing coarray at %L to allocatable, noncoarray dummy "
2673 		     "argument %qs, which is invalid if the allocation status"
2674 		     " is modified",  &actual->where, formal->name);
2675     }
2676 
2677   /* If the rank is the same or the formal argument has assumed-rank.  */
2678   if (symbol_rank (formal) == actual->rank || symbol_rank (formal) == -1)
2679     return true;
2680 
2681   rank_check = where != NULL && !is_elemental && formal->as
2682 	       && (formal->as->type == AS_ASSUMED_SHAPE
2683 		   || formal->as->type == AS_DEFERRED)
2684 	       && actual->expr_type != EXPR_NULL;
2685 
2686   /* Skip rank checks for NO_ARG_CHECK.  */
2687   if (formal->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2688     return true;
2689 
2690   /* Scalar & coindexed, see: F2008, Section 12.5.2.4.  */
2691   if (rank_check || ranks_must_agree
2692       || (formal->attr.pointer && actual->expr_type != EXPR_NULL)
2693       || (actual->rank != 0 && !(is_elemental || formal->attr.dimension))
2694       || (actual->rank == 0
2695 	  && ((formal->ts.type == BT_CLASS
2696 	       && CLASS_DATA (formal)->as->type == AS_ASSUMED_SHAPE)
2697 	      || (formal->ts.type != BT_CLASS
2698 		   && formal->as->type == AS_ASSUMED_SHAPE))
2699 	  && actual->expr_type != EXPR_NULL)
2700       || (actual->rank == 0 && formal->attr.dimension
2701 	  && gfc_is_coindexed (actual))
2702       /* Assumed-rank actual argument; F2018 C838.  */
2703       || actual->rank == -1)
2704     {
2705       if (where
2706 	  && (!formal->attr.artificial || (!formal->maybe_array
2707 					   && !maybe_dummy_array_arg (actual))))
2708 	{
2709 	  locus *where_formal;
2710 	  if (formal->attr.artificial)
2711 	    where_formal = &formal->declared_at;
2712 	  else
2713 	    where_formal = NULL;
2714 
2715 	  argument_rank_mismatch (formal->name, &actual->where,
2716 				  symbol_rank (formal), actual->rank,
2717 				  where_formal);
2718 	}
2719       return false;
2720     }
2721   else if (actual->rank != 0 && (is_elemental || formal->attr.dimension))
2722     return true;
2723 
2724   /* At this point, we are considering a scalar passed to an array.   This
2725      is valid (cf. F95 12.4.1.1, F2003 12.4.1.2, and F2008 12.5.2.4),
2726      - if the actual argument is (a substring of) an element of a
2727        non-assumed-shape/non-pointer/non-polymorphic array; or
2728      - (F2003) if the actual argument is of type character of default/c_char
2729        kind.  */
2730 
2731   is_pointer = actual->expr_type == EXPR_VARIABLE
2732 	       ? actual->symtree->n.sym->attr.pointer : false;
2733 
2734   for (ref = actual->ref; ref; ref = ref->next)
2735     {
2736       if (ref->type == REF_COMPONENT)
2737 	is_pointer = ref->u.c.component->attr.pointer;
2738       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2739 	       && ref->u.ar.dimen > 0
2740 	       && (!ref->next
2741 		   || (ref->next->type == REF_SUBSTRING && !ref->next->next)))
2742         break;
2743     }
2744 
2745   if (actual->ts.type == BT_CLASS && actual->expr_type != EXPR_NULL)
2746     {
2747       if (where)
2748 	gfc_error ("Polymorphic scalar passed to array dummy argument %qs "
2749 		   "at %L", formal->name, &actual->where);
2750       return false;
2751     }
2752 
2753   if (actual->expr_type != EXPR_NULL && ref && actual->ts.type != BT_CHARACTER
2754       && (is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2755     {
2756       if (where)
2757 	{
2758 	  if (formal->attr.artificial)
2759 	    gfc_error ("Element of assumed-shape or pointer array "
2760 		       "as actual argument at %L cannot correspond to "
2761 		       "actual argument at %L",
2762 		       &actual->where, &formal->declared_at);
2763 	  else
2764 	    gfc_error ("Element of assumed-shape or pointer "
2765 		       "array passed to array dummy argument %qs at %L",
2766 		       formal->name, &actual->where);
2767 	}
2768       return false;
2769     }
2770 
2771   if (actual->ts.type == BT_CHARACTER && actual->expr_type != EXPR_NULL
2772       && (!ref || is_pointer || ref->u.ar.as->type == AS_ASSUMED_SHAPE))
2773     {
2774       if (formal->ts.kind != 1 && (gfc_option.allow_std & GFC_STD_GNU) == 0)
2775 	{
2776 	  if (where)
2777 	    gfc_error ("Extension: Scalar non-default-kind, non-C_CHAR-kind "
2778 		       "CHARACTER actual argument with array dummy argument "
2779 		       "%qs at %L", formal->name, &actual->where);
2780 	  return false;
2781 	}
2782 
2783       if (where && (gfc_option.allow_std & GFC_STD_F2003) == 0)
2784 	{
2785 	  gfc_error ("Fortran 2003: Scalar CHARACTER actual argument with "
2786 		     "array dummy argument %qs at %L",
2787 		     formal->name, &actual->where);
2788 	  return false;
2789 	}
2790       else
2791 	return ((gfc_option.allow_std & GFC_STD_F2003) != 0);
2792     }
2793 
2794   if (ref == NULL && actual->expr_type != EXPR_NULL)
2795     {
2796       if (where
2797 	  && (!formal->attr.artificial || (!formal->maybe_array
2798 					   && !maybe_dummy_array_arg (actual))))
2799 	{
2800 	  locus *where_formal;
2801 	  if (formal->attr.artificial)
2802 	    where_formal = &formal->declared_at;
2803 	  else
2804 	    where_formal = NULL;
2805 
2806 	  argument_rank_mismatch (formal->name, &actual->where,
2807 				  symbol_rank (formal), actual->rank,
2808 				  where_formal);
2809 	}
2810       return false;
2811     }
2812 
2813   return true;
2814 }
2815 
2816 
2817 /* Returns the storage size of a symbol (formal argument) or
2818    zero if it cannot be determined.  */
2819 
2820 static unsigned long
get_sym_storage_size(gfc_symbol * sym)2821 get_sym_storage_size (gfc_symbol *sym)
2822 {
2823   int i;
2824   unsigned long strlen, elements;
2825 
2826   if (sym->ts.type == BT_CHARACTER)
2827     {
2828       if (sym->ts.u.cl && sym->ts.u.cl->length
2829 	  && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
2830 	  && sym->ts.u.cl->length->ts.type == BT_INTEGER)
2831 	strlen = mpz_get_ui (sym->ts.u.cl->length->value.integer);
2832       else
2833 	return 0;
2834     }
2835   else
2836     strlen = 1;
2837 
2838   if (symbol_rank (sym) == 0)
2839     return strlen;
2840 
2841   elements = 1;
2842   if (sym->as->type != AS_EXPLICIT)
2843     return 0;
2844   for (i = 0; i < sym->as->rank; i++)
2845     {
2846       if (sym->as->upper[i]->expr_type != EXPR_CONSTANT
2847 	  || sym->as->lower[i]->expr_type != EXPR_CONSTANT
2848 	  || sym->as->upper[i]->ts.type != BT_INTEGER
2849 	  || sym->as->lower[i]->ts.type != BT_INTEGER)
2850 	return 0;
2851 
2852       elements *= mpz_get_si (sym->as->upper[i]->value.integer)
2853 		  - mpz_get_si (sym->as->lower[i]->value.integer) + 1L;
2854     }
2855 
2856   return strlen*elements;
2857 }
2858 
2859 
2860 /* Returns the storage size of an expression (actual argument) or
2861    zero if it cannot be determined. For an array element, it returns
2862    the remaining size as the element sequence consists of all storage
2863    units of the actual argument up to the end of the array.  */
2864 
2865 static unsigned long
get_expr_storage_size(gfc_expr * e)2866 get_expr_storage_size (gfc_expr *e)
2867 {
2868   int i;
2869   long int strlen, elements;
2870   long int substrlen = 0;
2871   bool is_str_storage = false;
2872   gfc_ref *ref;
2873 
2874   if (e == NULL)
2875     return 0;
2876 
2877   if (e->ts.type == BT_CHARACTER)
2878     {
2879       if (e->ts.u.cl && e->ts.u.cl->length
2880 	  && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
2881 	  && e->ts.u.cl->length->ts.type == BT_INTEGER)
2882 	strlen = mpz_get_si (e->ts.u.cl->length->value.integer);
2883       else if (e->expr_type == EXPR_CONSTANT
2884 	       && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
2885 	strlen = e->value.character.length;
2886       else
2887 	return 0;
2888     }
2889   else
2890     strlen = 1; /* Length per element.  */
2891 
2892   if (e->rank == 0 && !e->ref)
2893     return strlen;
2894 
2895   elements = 1;
2896   if (!e->ref)
2897     {
2898       if (!e->shape)
2899 	return 0;
2900       for (i = 0; i < e->rank; i++)
2901 	elements *= mpz_get_si (e->shape[i]);
2902       return elements*strlen;
2903     }
2904 
2905   for (ref = e->ref; ref; ref = ref->next)
2906     {
2907       if (ref->type == REF_SUBSTRING && ref->u.ss.start
2908 	  && ref->u.ss.start->expr_type == EXPR_CONSTANT)
2909 	{
2910 	  if (is_str_storage)
2911 	    {
2912 	      /* The string length is the substring length.
2913 		 Set now to full string length.  */
2914 	      if (!ref->u.ss.length || !ref->u.ss.length->length
2915 		  || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)
2916 		return 0;
2917 
2918 	      strlen = mpz_get_ui (ref->u.ss.length->length->value.integer);
2919 	    }
2920 	  substrlen = strlen - mpz_get_ui (ref->u.ss.start->value.integer) + 1;
2921 	  continue;
2922 	}
2923 
2924       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
2925 	for (i = 0; i < ref->u.ar.dimen; i++)
2926 	  {
2927 	    long int start, end, stride;
2928 	    stride = 1;
2929 
2930 	    if (ref->u.ar.stride[i])
2931 	      {
2932 		if (ref->u.ar.stride[i]->expr_type == EXPR_CONSTANT
2933 		    && ref->u.ar.stride[i]->ts.type == BT_INTEGER)
2934 		  stride = mpz_get_si (ref->u.ar.stride[i]->value.integer);
2935 		else
2936 		  return 0;
2937 	      }
2938 
2939 	    if (ref->u.ar.start[i])
2940 	      {
2941 		if (ref->u.ar.start[i]->expr_type == EXPR_CONSTANT
2942 		    && ref->u.ar.start[i]->ts.type == BT_INTEGER)
2943 		  start = mpz_get_si (ref->u.ar.start[i]->value.integer);
2944 		else
2945 		  return 0;
2946 	      }
2947 	    else if (ref->u.ar.as->lower[i]
2948 		     && ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
2949 		     && ref->u.ar.as->lower[i]->ts.type == BT_INTEGER)
2950 	      start = mpz_get_si (ref->u.ar.as->lower[i]->value.integer);
2951 	    else
2952 	      return 0;
2953 
2954 	    if (ref->u.ar.end[i])
2955 	      {
2956 		if (ref->u.ar.end[i]->expr_type == EXPR_CONSTANT
2957 		    && ref->u.ar.end[i]->ts.type == BT_INTEGER)
2958 		  end = mpz_get_si (ref->u.ar.end[i]->value.integer);
2959 		else
2960 		  return 0;
2961 	      }
2962 	    else if (ref->u.ar.as->upper[i]
2963 		     && ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
2964 		     && ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
2965 	      end = mpz_get_si (ref->u.ar.as->upper[i]->value.integer);
2966 	    else
2967 	      return 0;
2968 
2969 	    elements *= (end - start)/stride + 1L;
2970 	  }
2971       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_FULL)
2972 	for (i = 0; i < ref->u.ar.as->rank; i++)
2973 	  {
2974 	    if (ref->u.ar.as->lower[i] && ref->u.ar.as->upper[i]
2975 		&& ref->u.ar.as->lower[i]->expr_type == EXPR_CONSTANT
2976 		&& ref->u.ar.as->lower[i]->ts.type == BT_INTEGER
2977 		&& ref->u.ar.as->upper[i]->expr_type == EXPR_CONSTANT
2978 		&& ref->u.ar.as->upper[i]->ts.type == BT_INTEGER)
2979 	      elements *= mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
2980 			  - mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
2981 			  + 1L;
2982 	    else
2983 	      return 0;
2984 	  }
2985       else if (ref->type == REF_ARRAY && ref->u.ar.type == AR_ELEMENT
2986 	       && e->expr_type == EXPR_VARIABLE)
2987 	{
2988 	  if (ref->u.ar.as->type == AS_ASSUMED_SHAPE
2989 	      || e->symtree->n.sym->attr.pointer)
2990 	    {
2991 	      elements = 1;
2992 	      continue;
2993 	    }
2994 
2995 	  /* Determine the number of remaining elements in the element
2996 	     sequence for array element designators.  */
2997 	  is_str_storage = true;
2998 	  for (i = ref->u.ar.dimen - 1; i >= 0; i--)
2999 	    {
3000 	      if (ref->u.ar.start[i] == NULL
3001 		  || ref->u.ar.start[i]->expr_type != EXPR_CONSTANT
3002 		  || ref->u.ar.as->upper[i] == NULL
3003 		  || ref->u.ar.as->lower[i] == NULL
3004 		  || ref->u.ar.as->upper[i]->expr_type != EXPR_CONSTANT
3005 		  || ref->u.ar.as->lower[i]->expr_type != EXPR_CONSTANT
3006 		  || ref->u.ar.as->upper[i]->ts.type != BT_INTEGER
3007 		  || ref->u.ar.as->lower[i]->ts.type != BT_INTEGER)
3008 		return 0;
3009 
3010 	      elements
3011 		   = elements
3012 		     * (mpz_get_si (ref->u.ar.as->upper[i]->value.integer)
3013 			- mpz_get_si (ref->u.ar.as->lower[i]->value.integer)
3014 			+ 1L)
3015 		     - (mpz_get_si (ref->u.ar.start[i]->value.integer)
3016 			- mpz_get_si (ref->u.ar.as->lower[i]->value.integer));
3017 	    }
3018         }
3019       else if (ref->type == REF_COMPONENT && ref->u.c.component->attr.function
3020 	       && ref->u.c.component->attr.proc_pointer
3021 	       && ref->u.c.component->attr.dimension)
3022 	{
3023 	  /* Array-valued procedure-pointer components.  */
3024 	  gfc_array_spec *as = ref->u.c.component->as;
3025 	  for (i = 0; i < as->rank; i++)
3026 	    {
3027 	      if (!as->upper[i] || !as->lower[i]
3028 		  || as->upper[i]->expr_type != EXPR_CONSTANT
3029 		  || as->lower[i]->expr_type != EXPR_CONSTANT
3030 		  || as->upper[i]->ts.type != BT_INTEGER
3031 		  || as->lower[i]->ts.type != BT_INTEGER)
3032 		return 0;
3033 
3034 	      elements = elements
3035 			 * (mpz_get_si (as->upper[i]->value.integer)
3036 			    - mpz_get_si (as->lower[i]->value.integer) + 1L);
3037 	    }
3038 	}
3039     }
3040 
3041   if (substrlen)
3042     return (is_str_storage) ? substrlen + (elements-1)*strlen
3043 			    : elements*strlen;
3044   else
3045     return elements*strlen;
3046 }
3047 
3048 
3049 /* Given an expression, check whether it is an array section
3050    which has a vector subscript.  */
3051 
3052 bool
gfc_has_vector_subscript(gfc_expr * e)3053 gfc_has_vector_subscript (gfc_expr *e)
3054 {
3055   int i;
3056   gfc_ref *ref;
3057 
3058   if (e == NULL || e->rank == 0 || e->expr_type != EXPR_VARIABLE)
3059     return false;
3060 
3061   for (ref = e->ref; ref; ref = ref->next)
3062     if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
3063       for (i = 0; i < ref->u.ar.dimen; i++)
3064 	if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
3065 	  return true;
3066 
3067   return false;
3068 }
3069 
3070 
3071 static bool
is_procptr_result(gfc_expr * expr)3072 is_procptr_result (gfc_expr *expr)
3073 {
3074   gfc_component *c = gfc_get_proc_ptr_comp (expr);
3075   if (c)
3076     return (c->ts.interface && (c->ts.interface->attr.proc_pointer == 1));
3077   else
3078     return ((expr->symtree->n.sym->result != expr->symtree->n.sym)
3079 	    && (expr->symtree->n.sym->result->attr.proc_pointer == 1));
3080 }
3081 
3082 
3083 /* Recursively append candidate argument ARG to CANDIDATES.  Store the
3084    number of total candidates in CANDIDATES_LEN.  */
3085 
3086 static void
lookup_arg_fuzzy_find_candidates(gfc_formal_arglist * arg,char ** & candidates,size_t & candidates_len)3087 lookup_arg_fuzzy_find_candidates (gfc_formal_arglist *arg,
3088 				  char **&candidates,
3089 				  size_t &candidates_len)
3090 {
3091   for (gfc_formal_arglist *p = arg; p && p->sym; p = p->next)
3092     vec_push (candidates, candidates_len, p->sym->name);
3093 }
3094 
3095 
3096 /* Lookup argument ARG fuzzily, taking names in ARGUMENTS into account.  */
3097 
3098 static const char*
lookup_arg_fuzzy(const char * arg,gfc_formal_arglist * arguments)3099 lookup_arg_fuzzy (const char *arg, gfc_formal_arglist *arguments)
3100 {
3101   char **candidates = NULL;
3102   size_t candidates_len = 0;
3103   lookup_arg_fuzzy_find_candidates (arguments, candidates, candidates_len);
3104   return gfc_closest_fuzzy_match (arg, candidates);
3105 }
3106 
3107 
3108 static gfc_dummy_arg *
get_nonintrinsic_dummy_arg(gfc_formal_arglist * formal)3109 get_nonintrinsic_dummy_arg (gfc_formal_arglist *formal)
3110 {
3111   gfc_dummy_arg * const dummy_arg = gfc_get_dummy_arg ();
3112 
3113   dummy_arg->intrinsicness = GFC_NON_INTRINSIC_DUMMY_ARG;
3114   dummy_arg->u.non_intrinsic = formal;
3115 
3116   return dummy_arg;
3117 }
3118 
3119 
3120 /* Given formal and actual argument lists, see if they are compatible.
3121    If they are compatible, the actual argument list is sorted to
3122    correspond with the formal list, and elements for missing optional
3123    arguments are inserted. If WHERE pointer is nonnull, then we issue
3124    errors when things don't match instead of just returning the status
3125    code.  */
3126 
3127 bool
gfc_compare_actual_formal(gfc_actual_arglist ** ap,gfc_formal_arglist * formal,int ranks_must_agree,int is_elemental,bool in_statement_function,locus * where)3128 gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
3129 			   int ranks_must_agree, int is_elemental,
3130 			   bool in_statement_function, locus *where)
3131 {
3132   gfc_actual_arglist **new_arg, *a, *actual;
3133   gfc_formal_arglist *f;
3134   int i, n, na;
3135   unsigned long actual_size, formal_size;
3136   bool full_array = false;
3137   gfc_array_ref *actual_arr_ref;
3138   gfc_array_spec *fas, *aas;
3139   bool pointer_dummy, pointer_arg, allocatable_arg;
3140 
3141   bool ok = true;
3142 
3143   actual = *ap;
3144 
3145   if (actual == NULL && formal == NULL)
3146     return true;
3147 
3148   n = 0;
3149   for (f = formal; f; f = f->next)
3150     n++;
3151 
3152   new_arg = XALLOCAVEC (gfc_actual_arglist *, n);
3153 
3154   for (i = 0; i < n; i++)
3155     new_arg[i] = NULL;
3156 
3157   na = 0;
3158   f = formal;
3159   i = 0;
3160 
3161   for (a = actual; a; a = a->next, f = f->next)
3162     {
3163       if (a->name != NULL && in_statement_function)
3164 	{
3165 	  gfc_error ("Keyword argument %qs at %L is invalid in "
3166 		     "a statement function", a->name, &a->expr->where);
3167 	  return false;
3168 	}
3169 
3170       /* Look for keywords but ignore g77 extensions like %VAL.  */
3171       if (a->name != NULL && a->name[0] != '%')
3172 	{
3173 	  i = 0;
3174 	  for (f = formal; f; f = f->next, i++)
3175 	    {
3176 	      if (f->sym == NULL)
3177 		continue;
3178 	      if (strcmp (f->sym->name, a->name) == 0)
3179 		break;
3180 	    }
3181 
3182 	  if (f == NULL)
3183 	    {
3184 	      if (where)
3185 		{
3186 		  const char *guessed = lookup_arg_fuzzy (a->name, formal);
3187 		  if (guessed)
3188 		    gfc_error ("Keyword argument %qs at %L is not in "
3189 			       "the procedure; did you mean %qs?",
3190 			       a->name, &a->expr->where, guessed);
3191 		  else
3192 		    gfc_error ("Keyword argument %qs at %L is not in "
3193 			       "the procedure", a->name, &a->expr->where);
3194 		}
3195 	      return false;
3196 	    }
3197 
3198 	  if (new_arg[i] != NULL)
3199 	    {
3200 	      if (where)
3201 		gfc_error ("Keyword argument %qs at %L is already associated "
3202 			   "with another actual argument", a->name,
3203 			   &a->expr->where);
3204 	      return false;
3205 	    }
3206 	}
3207 
3208       if (f == NULL)
3209 	{
3210 	  if (where)
3211 	    gfc_error ("More actual than formal arguments in procedure "
3212 		       "call at %L", where);
3213 	  return false;
3214 	}
3215 
3216       if (f->sym == NULL && a->expr == NULL)
3217 	goto match;
3218 
3219       if (f->sym == NULL)
3220 	{
3221 	  /* These errors have to be issued, otherwise an ICE can occur.
3222 	     See PR 78865.  */
3223 	  if (where)
3224 	    gfc_error_now ("Missing alternate return specifier in subroutine "
3225 			   "call at %L", where);
3226 	  return false;
3227 	}
3228       else
3229 	a->associated_dummy = get_nonintrinsic_dummy_arg (f);
3230 
3231       if (a->expr == NULL)
3232 	{
3233 	  if (f->sym->attr.optional)
3234 	    continue;
3235 	  else
3236 	    {
3237 	      if (where)
3238 		gfc_error_now ("Unexpected alternate return specifier in "
3239 			       "subroutine call at %L", where);
3240 	      return false;
3241 	    }
3242 	}
3243 
3244       /* Make sure that intrinsic vtables exist for calls to unlimited
3245 	 polymorphic formal arguments.  */
3246       if (UNLIMITED_POLY (f->sym)
3247 	  && a->expr->ts.type != BT_DERIVED
3248 	  && a->expr->ts.type != BT_CLASS
3249 	  && a->expr->ts.type != BT_ASSUMED)
3250 	gfc_find_vtab (&a->expr->ts);
3251 
3252       if (a->expr->expr_type == EXPR_NULL
3253 	  && ((f->sym->ts.type != BT_CLASS && !f->sym->attr.pointer
3254 	       && (f->sym->attr.allocatable || !f->sym->attr.optional
3255 		   || (gfc_option.allow_std & GFC_STD_F2008) == 0))
3256 	      || (f->sym->ts.type == BT_CLASS
3257 		  && !CLASS_DATA (f->sym)->attr.class_pointer
3258 		  && (CLASS_DATA (f->sym)->attr.allocatable
3259 		      || !f->sym->attr.optional
3260 		      || (gfc_option.allow_std & GFC_STD_F2008) == 0))))
3261 	{
3262 	  if (where
3263 	      && (!f->sym->attr.optional
3264 		  || (f->sym->ts.type != BT_CLASS && f->sym->attr.allocatable)
3265 		  || (f->sym->ts.type == BT_CLASS
3266 			 && CLASS_DATA (f->sym)->attr.allocatable)))
3267 	    gfc_error ("Unexpected NULL() intrinsic at %L to dummy %qs",
3268 		       where, f->sym->name);
3269 	  else if (where)
3270 	    gfc_error ("Fortran 2008: Null pointer at %L to non-pointer "
3271 		       "dummy %qs", where, f->sym->name);
3272 	  ok = false;
3273 	  goto match;
3274 	}
3275 
3276       if (!compare_parameter (f->sym, a->expr, ranks_must_agree,
3277 			      is_elemental, where))
3278 	{
3279 	  ok = false;
3280 	  goto match;
3281 	}
3282 
3283       /* TS 29113, 6.3p2; F2018 15.5.2.4.  */
3284       if (f->sym->ts.type == BT_ASSUMED
3285 	  && (a->expr->ts.type == BT_DERIVED
3286 	      || (a->expr->ts.type == BT_CLASS && CLASS_DATA (a->expr))))
3287 	{
3288 	  gfc_symbol *derived = (a->expr->ts.type == BT_DERIVED
3289 				 ? a->expr->ts.u.derived
3290 				 : CLASS_DATA (a->expr)->ts.u.derived);
3291 	  gfc_namespace *f2k_derived = derived->f2k_derived;
3292 	  if (derived->attr.pdt_type
3293 	      || (f2k_derived
3294 		  && (f2k_derived->finalizers || f2k_derived->tb_sym_root)))
3295 	    {
3296 	      gfc_error ("Actual argument at %L to assumed-type dummy "
3297 			 "has type parameters or is of "
3298 			 "derived type with type-bound or FINAL procedures",
3299 			 &a->expr->where);
3300 	      ok = false;
3301 	      goto match;
3302 	    }
3303 	}
3304 
3305       /* Special case for character arguments.  For allocatable, pointer
3306 	 and assumed-shape dummies, the string length needs to match
3307 	 exactly.  */
3308       if (a->expr->ts.type == BT_CHARACTER
3309 	  && a->expr->ts.u.cl && a->expr->ts.u.cl->length
3310 	  && a->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
3311 	  && f->sym->ts.type == BT_CHARACTER && f->sym->ts.u.cl
3312 	  && f->sym->ts.u.cl->length
3313 	  && f->sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
3314 	  && (f->sym->attr.pointer || f->sym->attr.allocatable
3315 	      || (f->sym->as && f->sym->as->type == AS_ASSUMED_SHAPE))
3316 	  && (mpz_cmp (a->expr->ts.u.cl->length->value.integer,
3317 		       f->sym->ts.u.cl->length->value.integer) != 0))
3318 	{
3319 	  if (where && (f->sym->attr.pointer || f->sym->attr.allocatable))
3320 	    gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
3321 			 "argument and pointer or allocatable dummy argument "
3322 			 "%qs at %L",
3323 			 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3324 			 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3325 			 f->sym->name, &a->expr->where);
3326 	  else if (where)
3327 	    gfc_warning (0, "Character length mismatch (%ld/%ld) between actual "
3328 			 "argument and assumed-shape dummy argument %qs "
3329 			 "at %L",
3330 			 mpz_get_si (a->expr->ts.u.cl->length->value.integer),
3331 			 mpz_get_si (f->sym->ts.u.cl->length->value.integer),
3332 			 f->sym->name, &a->expr->where);
3333 	  ok = false;
3334 	  goto match;
3335 	}
3336 
3337       if ((f->sym->attr.pointer || f->sym->attr.allocatable)
3338 	  && f->sym->ts.deferred != a->expr->ts.deferred
3339 	  && a->expr->ts.type == BT_CHARACTER)
3340 	{
3341 	  if (where)
3342 	    gfc_error ("Actual argument at %L to allocatable or "
3343 		       "pointer dummy argument %qs must have a deferred "
3344 		       "length type parameter if and only if the dummy has one",
3345 		       &a->expr->where, f->sym->name);
3346 	  ok = false;
3347 	  goto match;
3348 	}
3349 
3350       if (f->sym->ts.type == BT_CLASS)
3351 	goto skip_size_check;
3352 
3353       actual_size = get_expr_storage_size (a->expr);
3354       formal_size = get_sym_storage_size (f->sym);
3355       if (actual_size != 0 && actual_size < formal_size
3356 	  && a->expr->ts.type != BT_PROCEDURE
3357 	  && f->sym->attr.flavor != FL_PROCEDURE)
3358 	{
3359 	  if (a->expr->ts.type == BT_CHARACTER && !f->sym->as && where)
3360 	    {
3361 	      gfc_warning (0, "Character length of actual argument shorter "
3362 			   "than of dummy argument %qs (%lu/%lu) at %L",
3363 			   f->sym->name, actual_size, formal_size,
3364 			   &a->expr->where);
3365 	      goto skip_size_check;
3366 	    }
3367           else if (where)
3368 	    {
3369 	      /* Emit a warning for -std=legacy and an error otherwise. */
3370 	      if (gfc_option.warn_std == 0)
3371 	        gfc_warning (0, "Actual argument contains too few "
3372 			     "elements for dummy argument %qs (%lu/%lu) "
3373 			     "at %L", f->sym->name, actual_size,
3374 			     formal_size, &a->expr->where);
3375 	      else
3376 	        gfc_error_now ("Actual argument contains too few "
3377 			       "elements for dummy argument %qs (%lu/%lu) "
3378 			       "at %L", f->sym->name, actual_size,
3379 			       formal_size, &a->expr->where);
3380 	    }
3381 	  ok = false;
3382 	  goto match;
3383 	}
3384 
3385      skip_size_check:
3386 
3387       /* Satisfy F03:12.4.1.3 by ensuring that a procedure pointer actual
3388          argument is provided for a procedure pointer formal argument.  */
3389       if (f->sym->attr.proc_pointer
3390 	  && !((a->expr->expr_type == EXPR_VARIABLE
3391 		&& (a->expr->symtree->n.sym->attr.proc_pointer
3392 		    || gfc_is_proc_ptr_comp (a->expr)))
3393 	       || (a->expr->expr_type == EXPR_FUNCTION
3394 		   && is_procptr_result (a->expr))))
3395 	{
3396 	  if (where)
3397 	    gfc_error ("Expected a procedure pointer for argument %qs at %L",
3398 		       f->sym->name, &a->expr->where);
3399 	  ok = false;
3400 	  goto match;
3401 	}
3402 
3403       /* Satisfy F03:12.4.1.3 by ensuring that a procedure actual argument is
3404 	 provided for a procedure formal argument.  */
3405       if (f->sym->attr.flavor == FL_PROCEDURE
3406 	  && !((a->expr->expr_type == EXPR_VARIABLE
3407 		&& (a->expr->symtree->n.sym->attr.flavor == FL_PROCEDURE
3408 		    || a->expr->symtree->n.sym->attr.proc_pointer
3409 		    || gfc_is_proc_ptr_comp (a->expr)))
3410 	       || (a->expr->expr_type == EXPR_FUNCTION
3411 		   && is_procptr_result (a->expr))))
3412 	{
3413 	  if (where)
3414 	    gfc_error ("Expected a procedure for argument %qs at %L",
3415 		       f->sym->name, &a->expr->where);
3416 	  ok = false;
3417 	  goto match;
3418 	}
3419 
3420       /* Class array variables and expressions store array info in a
3421 	 different place from non-class objects; consolidate the logic
3422 	 to access it here instead of repeating it below.  Note that
3423 	 pointer_arg and allocatable_arg are not fully general and are
3424 	 only used in a specific situation below with an assumed-rank
3425 	 argument.  */
3426       if (f->sym->ts.type == BT_CLASS && CLASS_DATA (f->sym))
3427 	{
3428 	  gfc_component *classdata = CLASS_DATA (f->sym);
3429 	  fas = classdata->as;
3430 	  pointer_dummy = classdata->attr.class_pointer;
3431 	}
3432       else
3433 	{
3434 	  fas = f->sym->as;
3435 	  pointer_dummy = f->sym->attr.pointer;
3436 	}
3437 
3438       if (a->expr->expr_type != EXPR_VARIABLE)
3439 	{
3440 	  aas = NULL;
3441 	  pointer_arg = false;
3442 	  allocatable_arg = false;
3443 	}
3444       else if (a->expr->ts.type == BT_CLASS
3445 	       && a->expr->symtree->n.sym
3446 	       && CLASS_DATA (a->expr->symtree->n.sym))
3447 	{
3448 	  gfc_component *classdata = CLASS_DATA (a->expr->symtree->n.sym);
3449 	  aas = classdata->as;
3450 	  pointer_arg = classdata->attr.class_pointer;
3451 	  allocatable_arg = classdata->attr.allocatable;
3452 	}
3453       else
3454 	{
3455 	  aas = a->expr->symtree->n.sym->as;
3456 	  pointer_arg = a->expr->symtree->n.sym->attr.pointer;
3457 	  allocatable_arg = a->expr->symtree->n.sym->attr.allocatable;
3458 	}
3459 
3460       /* F2018:9.5.2(2) permits assumed-size whole array expressions as
3461 	 actual arguments only if the shape is not required; thus it
3462 	 cannot be passed to an assumed-shape array dummy.
3463 	 F2018:15.5.2.(2) permits passing a nonpointer actual to an
3464 	 intent(in) pointer dummy argument and this is accepted by
3465 	 the compare_pointer check below, but this also requires shape
3466 	 information.
3467 	 There's more discussion of this in PR94110.  */
3468       if (fas
3469 	  && (fas->type == AS_ASSUMED_SHAPE
3470 	      || fas->type == AS_DEFERRED
3471 	      || (fas->type == AS_ASSUMED_RANK && pointer_dummy))
3472 	  && aas
3473 	  && aas->type == AS_ASSUMED_SIZE
3474 	  && (a->expr->ref == NULL
3475 	      || (a->expr->ref->type == REF_ARRAY
3476 		  && a->expr->ref->u.ar.type == AR_FULL)))
3477 	{
3478 	  if (where)
3479 	    gfc_error ("Actual argument for %qs cannot be an assumed-size"
3480 		       " array at %L", f->sym->name, where);
3481 	  ok = false;
3482 	  goto match;
3483 	}
3484 
3485       /* Diagnose F2018 C839 (TS29113 C535c).  Here the problem is
3486 	 passing an assumed-size array to an INTENT(OUT) assumed-rank
3487 	 dummy when it doesn't have the size information needed to run
3488 	 initializers and finalizers.  */
3489       if (f->sym->attr.intent == INTENT_OUT
3490 	  && fas
3491 	  && fas->type == AS_ASSUMED_RANK
3492 	  && aas
3493 	  && ((aas->type == AS_ASSUMED_SIZE
3494 	       && (a->expr->ref == NULL
3495 		   || (a->expr->ref->type == REF_ARRAY
3496 		       && a->expr->ref->u.ar.type == AR_FULL)))
3497 	      || (aas->type == AS_ASSUMED_RANK
3498 		  && !pointer_arg
3499 		  && !allocatable_arg))
3500 	  && (a->expr->ts.type == BT_CLASS
3501 	      || (a->expr->ts.type == BT_DERIVED
3502 		  && (gfc_is_finalizable (a->expr->ts.u.derived, NULL)
3503 		      || gfc_has_ultimate_allocatable (a->expr)
3504 		      || gfc_has_default_initializer
3505 			   (a->expr->ts.u.derived)))))
3506 	{
3507 	  if (where)
3508 	    gfc_error ("Actual argument to assumed-rank INTENT(OUT) "
3509 		       "dummy %qs at %L cannot be of unknown size",
3510 		       f->sym->name, where);
3511 	  ok = false;
3512 	  goto match;
3513 	}
3514 
3515       if (a->expr->expr_type != EXPR_NULL
3516 	  && compare_pointer (f->sym, a->expr) == 0)
3517 	{
3518 	  if (where)
3519 	    gfc_error ("Actual argument for %qs must be a pointer at %L",
3520 		       f->sym->name, &a->expr->where);
3521 	  ok = false;
3522 	  goto match;
3523 	}
3524 
3525       if (a->expr->expr_type != EXPR_NULL
3526 	  && (gfc_option.allow_std & GFC_STD_F2008) == 0
3527 	  && compare_pointer (f->sym, a->expr) == 2)
3528 	{
3529 	  if (where)
3530 	    gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
3531 		       "pointer dummy %qs", &a->expr->where,f->sym->name);
3532 	  ok = false;
3533 	  goto match;
3534 	}
3535 
3536 
3537       /* Fortran 2008, C1242.  */
3538       if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
3539 	{
3540 	  if (where)
3541 	    gfc_error ("Coindexed actual argument at %L to pointer "
3542 		       "dummy %qs",
3543 		       &a->expr->where, f->sym->name);
3544 	  ok = false;
3545 	  goto match;
3546 	}
3547 
3548       /* Fortran 2008, 12.5.2.5 (no constraint).  */
3549       if (a->expr->expr_type == EXPR_VARIABLE
3550 	  && f->sym->attr.intent != INTENT_IN
3551 	  && f->sym->attr.allocatable
3552 	  && gfc_is_coindexed (a->expr))
3553 	{
3554 	  if (where)
3555 	    gfc_error ("Coindexed actual argument at %L to allocatable "
3556 		       "dummy %qs requires INTENT(IN)",
3557 		       &a->expr->where, f->sym->name);
3558 	  ok = false;
3559 	  goto match;
3560 	}
3561 
3562       /* Fortran 2008, C1237.  */
3563       if (a->expr->expr_type == EXPR_VARIABLE
3564 	  && (f->sym->attr.asynchronous || f->sym->attr.volatile_)
3565 	  && gfc_is_coindexed (a->expr)
3566 	  && (a->expr->symtree->n.sym->attr.volatile_
3567 	      || a->expr->symtree->n.sym->attr.asynchronous))
3568 	{
3569 	  if (where)
3570 	    gfc_error ("Coindexed ASYNCHRONOUS or VOLATILE actual argument at "
3571 		       "%L requires that dummy %qs has neither "
3572 		       "ASYNCHRONOUS nor VOLATILE", &a->expr->where,
3573 		       f->sym->name);
3574 	  ok = false;
3575 	  goto match;
3576 	}
3577 
3578       /* Fortran 2008, 12.5.2.4 (no constraint).  */
3579       if (a->expr->expr_type == EXPR_VARIABLE
3580 	  && f->sym->attr.intent != INTENT_IN && !f->sym->attr.value
3581 	  && gfc_is_coindexed (a->expr)
3582 	  && gfc_has_ultimate_allocatable (a->expr))
3583 	{
3584 	  if (where)
3585 	    gfc_error ("Coindexed actual argument at %L with allocatable "
3586 		       "ultimate component to dummy %qs requires either VALUE "
3587 		       "or INTENT(IN)", &a->expr->where, f->sym->name);
3588 	  ok = false;
3589 	  goto match;
3590 	}
3591 
3592      if (f->sym->ts.type == BT_CLASS
3593 	   && CLASS_DATA (f->sym)->attr.allocatable
3594 	   && gfc_is_class_array_ref (a->expr, &full_array)
3595 	   && !full_array)
3596 	{
3597 	  if (where)
3598 	    gfc_error ("Actual CLASS array argument for %qs must be a full "
3599 		       "array at %L", f->sym->name, &a->expr->where);
3600 	  ok = false;
3601 	  goto match;
3602 	}
3603 
3604 
3605       if (a->expr->expr_type != EXPR_NULL
3606 	  && !compare_allocatable (f->sym, a->expr))
3607 	{
3608 	  if (where)
3609 	    gfc_error ("Actual argument for %qs must be ALLOCATABLE at %L",
3610 		       f->sym->name, &a->expr->where);
3611 	  ok = false;
3612 	  goto match;
3613 	}
3614 
3615       /* Check intent = OUT/INOUT for definable actual argument.  */
3616       if (!in_statement_function
3617 	  && (f->sym->attr.intent == INTENT_OUT
3618 	      || f->sym->attr.intent == INTENT_INOUT))
3619 	{
3620 	  const char* context = (where
3621 				 ? _("actual argument to INTENT = OUT/INOUT")
3622 				 : NULL);
3623 
3624 	  if (((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3625 		&& CLASS_DATA (f->sym)->attr.class_pointer)
3626 	       || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
3627 	      && !gfc_check_vardef_context (a->expr, true, false, false, context))
3628 	    {
3629 	      ok = false;
3630 	      goto match;
3631 	    }
3632 	  if (!gfc_check_vardef_context (a->expr, false, false, false, context))
3633 	    {
3634 	      ok = false;
3635 	      goto match;
3636 	    }
3637 	}
3638 
3639       if ((f->sym->attr.intent == INTENT_OUT
3640 	   || f->sym->attr.intent == INTENT_INOUT
3641 	   || f->sym->attr.volatile_
3642 	   || f->sym->attr.asynchronous)
3643 	  && gfc_has_vector_subscript (a->expr))
3644 	{
3645 	  if (where)
3646 	    gfc_error ("Array-section actual argument with vector "
3647 		       "subscripts at %L is incompatible with INTENT(OUT), "
3648 		       "INTENT(INOUT), VOLATILE or ASYNCHRONOUS attribute "
3649 		       "of the dummy argument %qs",
3650 		       &a->expr->where, f->sym->name);
3651 	  ok = false;
3652 	  goto match;
3653 	}
3654 
3655       /* C1232 (R1221) For an actual argument which is an array section or
3656 	 an assumed-shape array, the dummy argument shall be an assumed-
3657 	 shape array, if the dummy argument has the VOLATILE attribute.  */
3658 
3659       if (f->sym->attr.volatile_
3660 	  && a->expr->expr_type == EXPR_VARIABLE
3661 	  && a->expr->symtree->n.sym->as
3662 	  && a->expr->symtree->n.sym->as->type == AS_ASSUMED_SHAPE
3663 	  && !(fas && fas->type == AS_ASSUMED_SHAPE))
3664 	{
3665 	  if (where)
3666 	    gfc_error ("Assumed-shape actual argument at %L is "
3667 		       "incompatible with the non-assumed-shape "
3668 		       "dummy argument %qs due to VOLATILE attribute",
3669 		       &a->expr->where,f->sym->name);
3670 	  ok = false;
3671 	  goto match;
3672 	}
3673 
3674       /* Find the last array_ref.  */
3675       actual_arr_ref = NULL;
3676       if (a->expr->ref)
3677 	actual_arr_ref = gfc_find_array_ref (a->expr, true);
3678 
3679       if (f->sym->attr.volatile_
3680 	  && actual_arr_ref && actual_arr_ref->type == AR_SECTION
3681 	  && !(fas && fas->type == AS_ASSUMED_SHAPE))
3682 	{
3683 	  if (where)
3684 	    gfc_error ("Array-section actual argument at %L is "
3685 		       "incompatible with the non-assumed-shape "
3686 		       "dummy argument %qs due to VOLATILE attribute",
3687 		       &a->expr->where, f->sym->name);
3688 	  ok = false;
3689 	  goto match;
3690 	}
3691 
3692       /* C1233 (R1221) For an actual argument which is a pointer array, the
3693 	 dummy argument shall be an assumed-shape or pointer array, if the
3694 	 dummy argument has the VOLATILE attribute.  */
3695 
3696       if (f->sym->attr.volatile_
3697 	  && a->expr->expr_type == EXPR_VARIABLE
3698 	  && a->expr->symtree->n.sym->attr.pointer
3699 	  && a->expr->symtree->n.sym->as
3700 	  && !(fas
3701 	       && (fas->type == AS_ASSUMED_SHAPE
3702 		   || f->sym->attr.pointer)))
3703 	{
3704 	  if (where)
3705 	    gfc_error ("Pointer-array actual argument at %L requires "
3706 		       "an assumed-shape or pointer-array dummy "
3707 		       "argument %qs due to VOLATILE attribute",
3708 		       &a->expr->where,f->sym->name);
3709 	  ok = false;
3710 	  goto match;
3711 	}
3712 
3713     match:
3714       if (a == actual)
3715 	na = i;
3716 
3717       new_arg[i++] = a;
3718     }
3719 
3720   /* Give up now if we saw any bad argument.  */
3721   if (!ok)
3722     return false;
3723 
3724   /* Make sure missing actual arguments are optional.  */
3725   i = 0;
3726   for (f = formal; f; f = f->next, i++)
3727     {
3728       if (new_arg[i] != NULL)
3729 	continue;
3730       if (f->sym == NULL)
3731 	{
3732 	  if (where)
3733 	    gfc_error ("Missing alternate return spec in subroutine call "
3734 		       "at %L", where);
3735 	  return false;
3736 	}
3737       /* For CLASS, the optional attribute might be set at either location. */
3738       if (((f->sym->ts.type != BT_CLASS || !CLASS_DATA (f->sym)->attr.optional)
3739 	   && !f->sym->attr.optional)
3740 	  || (in_statement_function
3741 	      && (f->sym->attr.optional
3742 		  || (f->sym->ts.type == BT_CLASS
3743 		      && CLASS_DATA (f->sym)->attr.optional))))
3744 	{
3745 	  if (where)
3746 	    gfc_error ("Missing actual argument for argument %qs at %L",
3747 		       f->sym->name, where);
3748 	  return false;
3749 	}
3750     }
3751 
3752   /* We should have handled the cases where the formal arglist is null
3753      already.  */
3754   gcc_assert (n > 0);
3755 
3756   /* The argument lists are compatible.  We now relink a new actual
3757      argument list with null arguments in the right places.  The head
3758      of the list remains the head.  */
3759   for (f = formal, i = 0; f; f = f->next, i++)
3760     if (new_arg[i] == NULL)
3761       {
3762 	new_arg[i] = gfc_get_actual_arglist ();
3763 	new_arg[i]->associated_dummy = get_nonintrinsic_dummy_arg (f);
3764       }
3765 
3766   if (na != 0)
3767     {
3768       std::swap (*new_arg[0], *actual);
3769       std::swap (new_arg[0], new_arg[na]);
3770     }
3771 
3772   for (i = 0; i < n - 1; i++)
3773     new_arg[i]->next = new_arg[i + 1];
3774 
3775   new_arg[i]->next = NULL;
3776 
3777   if (*ap == NULL && n > 0)
3778     *ap = new_arg[0];
3779 
3780   return true;
3781 }
3782 
3783 
3784 typedef struct
3785 {
3786   gfc_formal_arglist *f;
3787   gfc_actual_arglist *a;
3788 }
3789 argpair;
3790 
3791 /* qsort comparison function for argument pairs, with the following
3792    order:
3793     - p->a->expr == NULL
3794     - p->a->expr->expr_type != EXPR_VARIABLE
3795     - by gfc_symbol pointer value (larger first).  */
3796 
3797 static int
pair_cmp(const void * p1,const void * p2)3798 pair_cmp (const void *p1, const void *p2)
3799 {
3800   const gfc_actual_arglist *a1, *a2;
3801 
3802   /* *p1 and *p2 are elements of the to-be-sorted array.  */
3803   a1 = ((const argpair *) p1)->a;
3804   a2 = ((const argpair *) p2)->a;
3805   if (!a1->expr)
3806     {
3807       if (!a2->expr)
3808 	return 0;
3809       return -1;
3810     }
3811   if (!a2->expr)
3812     return 1;
3813   if (a1->expr->expr_type != EXPR_VARIABLE)
3814     {
3815       if (a2->expr->expr_type != EXPR_VARIABLE)
3816 	return 0;
3817       return -1;
3818     }
3819   if (a2->expr->expr_type != EXPR_VARIABLE)
3820     return 1;
3821   if (a1->expr->symtree->n.sym > a2->expr->symtree->n.sym)
3822     return -1;
3823   return a1->expr->symtree->n.sym < a2->expr->symtree->n.sym;
3824 }
3825 
3826 
3827 /* Given two expressions from some actual arguments, test whether they
3828    refer to the same expression. The analysis is conservative.
3829    Returning false will produce no warning.  */
3830 
3831 static bool
compare_actual_expr(gfc_expr * e1,gfc_expr * e2)3832 compare_actual_expr (gfc_expr *e1, gfc_expr *e2)
3833 {
3834   const gfc_ref *r1, *r2;
3835 
3836   if (!e1 || !e2
3837       || e1->expr_type != EXPR_VARIABLE
3838       || e2->expr_type != EXPR_VARIABLE
3839       || e1->symtree->n.sym != e2->symtree->n.sym)
3840     return false;
3841 
3842   /* TODO: improve comparison, see expr.cc:show_ref().  */
3843   for (r1 = e1->ref, r2 = e2->ref; r1 && r2; r1 = r1->next, r2 = r2->next)
3844     {
3845       if (r1->type != r2->type)
3846 	return false;
3847       switch (r1->type)
3848 	{
3849 	case REF_ARRAY:
3850 	  if (r1->u.ar.type != r2->u.ar.type)
3851 	    return false;
3852 	  /* TODO: At the moment, consider only full arrays;
3853 	     we could do better.  */
3854 	  if (r1->u.ar.type != AR_FULL || r2->u.ar.type != AR_FULL)
3855 	    return false;
3856 	  break;
3857 
3858 	case REF_COMPONENT:
3859 	  if (r1->u.c.component != r2->u.c.component)
3860 	    return false;
3861 	  break;
3862 
3863 	case REF_SUBSTRING:
3864 	  return false;
3865 
3866 	case REF_INQUIRY:
3867 	  if (e1->symtree->n.sym->ts.type == BT_COMPLEX
3868 	      && e1->ts.type == BT_REAL && e2->ts.type == BT_REAL
3869 	      && r1->u.i != r2->u.i)
3870 	    return false;
3871 	  break;
3872 
3873 	default:
3874 	  gfc_internal_error ("compare_actual_expr(): Bad component code");
3875 	}
3876     }
3877   if (!r1 && !r2)
3878     return true;
3879   return false;
3880 }
3881 
3882 
3883 /* Given formal and actual argument lists that correspond to one
3884    another, check that identical actual arguments aren't not
3885    associated with some incompatible INTENTs.  */
3886 
3887 static bool
check_some_aliasing(gfc_formal_arglist * f,gfc_actual_arglist * a)3888 check_some_aliasing (gfc_formal_arglist *f, gfc_actual_arglist *a)
3889 {
3890   sym_intent f1_intent, f2_intent;
3891   gfc_formal_arglist *f1;
3892   gfc_actual_arglist *a1;
3893   size_t n, i, j;
3894   argpair *p;
3895   bool t = true;
3896 
3897   n = 0;
3898   for (f1 = f, a1 = a;; f1 = f1->next, a1 = a1->next)
3899     {
3900       if (f1 == NULL && a1 == NULL)
3901 	break;
3902       if (f1 == NULL || a1 == NULL)
3903 	gfc_internal_error ("check_some_aliasing(): List mismatch");
3904       n++;
3905     }
3906   if (n == 0)
3907     return t;
3908   p = XALLOCAVEC (argpair, n);
3909 
3910   for (i = 0, f1 = f, a1 = a; i < n; i++, f1 = f1->next, a1 = a1->next)
3911     {
3912       p[i].f = f1;
3913       p[i].a = a1;
3914     }
3915 
3916   qsort (p, n, sizeof (argpair), pair_cmp);
3917 
3918   for (i = 0; i < n; i++)
3919     {
3920       if (!p[i].a->expr
3921 	  || p[i].a->expr->expr_type != EXPR_VARIABLE
3922 	  || p[i].a->expr->ts.type == BT_PROCEDURE)
3923 	continue;
3924       f1_intent = p[i].f->sym->attr.intent;
3925       for (j = i + 1; j < n; j++)
3926 	{
3927 	  /* Expected order after the sort.  */
3928 	  if (!p[j].a->expr || p[j].a->expr->expr_type != EXPR_VARIABLE)
3929 	    gfc_internal_error ("check_some_aliasing(): corrupted data");
3930 
3931 	  /* Are the expression the same?  */
3932 	  if (!compare_actual_expr (p[i].a->expr, p[j].a->expr))
3933 	    break;
3934 	  f2_intent = p[j].f->sym->attr.intent;
3935 	  if ((f1_intent == INTENT_IN && f2_intent == INTENT_OUT)
3936 	      || (f1_intent == INTENT_OUT && f2_intent == INTENT_IN)
3937 	      || (f1_intent == INTENT_OUT && f2_intent == INTENT_OUT))
3938 	    {
3939 	      gfc_warning (0, "Same actual argument associated with INTENT(%s) "
3940 			   "argument %qs and INTENT(%s) argument %qs at %L",
3941 			   gfc_intent_string (f1_intent), p[i].f->sym->name,
3942 			   gfc_intent_string (f2_intent), p[j].f->sym->name,
3943 			   &p[i].a->expr->where);
3944 	      t = false;
3945 	    }
3946 	}
3947     }
3948 
3949   return t;
3950 }
3951 
3952 
3953 /* Given formal and actual argument lists that correspond to one
3954    another, check that they are compatible in the sense that intents
3955    are not mismatched.  */
3956 
3957 static bool
check_intents(gfc_formal_arglist * f,gfc_actual_arglist * a)3958 check_intents (gfc_formal_arglist *f, gfc_actual_arglist *a)
3959 {
3960   sym_intent f_intent;
3961 
3962   for (;; f = f->next, a = a->next)
3963     {
3964       gfc_expr *expr;
3965 
3966       if (f == NULL && a == NULL)
3967 	break;
3968       if (f == NULL || a == NULL)
3969 	gfc_internal_error ("check_intents(): List mismatch");
3970 
3971       if (a->expr && a->expr->expr_type == EXPR_FUNCTION
3972 	  && a->expr->value.function.isym
3973 	  && a->expr->value.function.isym->id == GFC_ISYM_CAF_GET)
3974 	expr = a->expr->value.function.actual->expr;
3975       else
3976 	expr = a->expr;
3977 
3978       if (expr == NULL || expr->expr_type != EXPR_VARIABLE)
3979 	continue;
3980 
3981       f_intent = f->sym->attr.intent;
3982 
3983       if (gfc_pure (NULL) && gfc_impure_variable (expr->symtree->n.sym))
3984 	{
3985 	  if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
3986 	       && CLASS_DATA (f->sym)->attr.class_pointer)
3987 	      || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
3988 	    {
3989 	      gfc_error ("Procedure argument at %L is local to a PURE "
3990 			 "procedure and has the POINTER attribute",
3991 			 &expr->where);
3992 	      return false;
3993 	    }
3994 	}
3995 
3996        /* Fortran 2008, C1283.  */
3997        if (gfc_pure (NULL) && gfc_is_coindexed (expr))
3998 	{
3999 	  if (f_intent == INTENT_INOUT || f_intent == INTENT_OUT)
4000 	    {
4001 	      gfc_error ("Coindexed actual argument at %L in PURE procedure "
4002 			 "is passed to an INTENT(%s) argument",
4003 			 &expr->where, gfc_intent_string (f_intent));
4004 	      return false;
4005 	    }
4006 
4007 	  if ((f->sym->ts.type == BT_CLASS && f->sym->attr.class_ok
4008                && CLASS_DATA (f->sym)->attr.class_pointer)
4009               || (f->sym->ts.type != BT_CLASS && f->sym->attr.pointer))
4010 	    {
4011 	      gfc_error ("Coindexed actual argument at %L in PURE procedure "
4012 			 "is passed to a POINTER dummy argument",
4013 			 &expr->where);
4014 	      return false;
4015 	    }
4016 	}
4017 
4018        /* F2008, Section 12.5.2.4.  */
4019        if (expr->ts.type == BT_CLASS && f->sym->ts.type == BT_CLASS
4020 	   && gfc_is_coindexed (expr))
4021 	 {
4022 	   gfc_error ("Coindexed polymorphic actual argument at %L is passed "
4023 		      "polymorphic dummy argument %qs",
4024 			 &expr->where, f->sym->name);
4025 	   return false;
4026 	 }
4027     }
4028 
4029   return true;
4030 }
4031 
4032 
4033 /* Check how a procedure is used against its interface.  If all goes
4034    well, the actual argument list will also end up being properly
4035    sorted.  */
4036 
4037 bool
gfc_procedure_use(gfc_symbol * sym,gfc_actual_arglist ** ap,locus * where)4038 gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
4039 {
4040   gfc_actual_arglist *a;
4041   gfc_formal_arglist *dummy_args;
4042   bool implicit = false;
4043 
4044   /* Warn about calls with an implicit interface.  Special case
4045      for calling a ISO_C_BINDING because c_loc and c_funloc
4046      are pseudo-unknown.  Additionally, warn about procedures not
4047      explicitly declared at all if requested.  */
4048   if (sym->attr.if_source == IFSRC_UNKNOWN && !sym->attr.is_iso_c)
4049     {
4050       bool has_implicit_none_export = false;
4051       implicit = true;
4052       if (sym->attr.proc == PROC_UNKNOWN)
4053 	for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
4054 	  if (ns->has_implicit_none_export)
4055 	    {
4056 	      has_implicit_none_export = true;
4057 	      break;
4058 	    }
4059       if (has_implicit_none_export)
4060 	{
4061 	  const char *guessed
4062 	    = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
4063 	  if (guessed)
4064 	    gfc_error ("Procedure %qs called at %L is not explicitly declared"
4065 		       "; did you mean %qs?",
4066 		       sym->name, where, guessed);
4067 	  else
4068 	    gfc_error ("Procedure %qs called at %L is not explicitly declared",
4069 		       sym->name, where);
4070 	  return false;
4071 	}
4072       if (warn_implicit_interface)
4073 	gfc_warning (OPT_Wimplicit_interface,
4074 		     "Procedure %qs called with an implicit interface at %L",
4075 		     sym->name, where);
4076       else if (warn_implicit_procedure && sym->attr.proc == PROC_UNKNOWN)
4077 	gfc_warning (OPT_Wimplicit_procedure,
4078 		     "Procedure %qs called at %L is not explicitly declared",
4079 		     sym->name, where);
4080       gfc_find_proc_namespace (sym->ns)->implicit_interface_calls = 1;
4081     }
4082 
4083   if (sym->attr.if_source == IFSRC_UNKNOWN)
4084     {
4085       if (sym->attr.pointer)
4086 	{
4087 	  gfc_error ("The pointer object %qs at %L must have an explicit "
4088 		     "function interface or be declared as array",
4089 		     sym->name, where);
4090 	  return false;
4091 	}
4092 
4093       if (sym->attr.allocatable && !sym->attr.external)
4094 	{
4095 	  gfc_error ("The allocatable object %qs at %L must have an explicit "
4096 		     "function interface or be declared as array",
4097 		     sym->name, where);
4098 	  return false;
4099 	}
4100 
4101       if (sym->attr.allocatable)
4102 	{
4103 	  gfc_error ("Allocatable function %qs at %L must have an explicit "
4104 		     "function interface", sym->name, where);
4105 	  return false;
4106 	}
4107 
4108       for (a = *ap; a; a = a->next)
4109 	{
4110 	  if (a->expr && a->expr->error)
4111 	    return false;
4112 
4113 	  /* F2018, 15.4.2.2 Explicit interface is required for a
4114 	     polymorphic dummy argument, so there is no way to
4115 	     legally have a class appear in an argument with an
4116 	     implicit interface.  */
4117 
4118 	  if (implicit && a->expr && a->expr->ts.type == BT_CLASS)
4119 	    {
4120 	      gfc_error ("Explicit interface required for polymorphic "
4121 			 "argument at %L",&a->expr->where);
4122 	      a->expr->error = 1;
4123 	      break;
4124 	    }
4125 
4126 	  /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
4127 	  if (a->name != NULL && a->name[0] != '%')
4128 	    {
4129 	      gfc_error ("Keyword argument requires explicit interface "
4130 			 "for procedure %qs at %L", sym->name, &a->expr->where);
4131 	      break;
4132 	    }
4133 
4134 	  /* TS 29113, 6.2.  */
4135 	  if (a->expr && a->expr->ts.type == BT_ASSUMED
4136 	      && sym->intmod_sym_id != ISOCBINDING_LOC)
4137 	    {
4138 	      gfc_error ("Assumed-type argument %s at %L requires an explicit "
4139 			 "interface", a->expr->symtree->n.sym->name,
4140 			 &a->expr->where);
4141 	      a->expr->error = 1;
4142 	      break;
4143 	    }
4144 
4145 	  /* F2008, C1303 and C1304.  */
4146 	  if (a->expr
4147 	      && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
4148 	      && a->expr->ts.u.derived
4149 	      && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4150 		   && a->expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
4151 		  || gfc_expr_attr (a->expr).lock_comp))
4152 	    {
4153 	      gfc_error ("Actual argument of LOCK_TYPE or with LOCK_TYPE "
4154 			 "component at %L requires an explicit interface for "
4155 			 "procedure %qs", &a->expr->where, sym->name);
4156 	      a->expr->error = 1;
4157 	      break;
4158 	    }
4159 
4160 	  if (a->expr
4161 	      && (a->expr->ts.type == BT_DERIVED || a->expr->ts.type == BT_CLASS)
4162 	      && a->expr->ts.u.derived
4163 	      && ((a->expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
4164 		   && a->expr->ts.u.derived->intmod_sym_id
4165 		      == ISOFORTRAN_EVENT_TYPE)
4166 		  || gfc_expr_attr (a->expr).event_comp))
4167 	    {
4168 	      gfc_error ("Actual argument of EVENT_TYPE or with EVENT_TYPE "
4169 			 "component at %L requires an explicit interface for "
4170 			 "procedure %qs", &a->expr->where, sym->name);
4171 	      a->expr->error = 1;
4172 	      break;
4173 	    }
4174 
4175 	  if (a->expr && a->expr->expr_type == EXPR_NULL
4176 	      && a->expr->ts.type == BT_UNKNOWN)
4177 	    {
4178 	      gfc_error ("MOLD argument to NULL required at %L",
4179 			 &a->expr->where);
4180 	      a->expr->error = 1;
4181 	      return false;
4182 	    }
4183 
4184 	  if (a->expr && a->expr->expr_type == EXPR_NULL)
4185 	    {
4186 	      gfc_error ("Passing intrinsic NULL as actual argument at %L "
4187 			 "requires an explicit interface", &a->expr->where);
4188 	      a->expr->error = 1;
4189 	      return false;
4190 	    }
4191 
4192 	  /* TS 29113, C407b.  */
4193 	  if (a->expr && a->expr->expr_type == EXPR_VARIABLE
4194 	      && symbol_rank (a->expr->symtree->n.sym) == -1)
4195 	    {
4196 	      gfc_error ("Assumed-rank argument requires an explicit interface "
4197 			 "at %L", &a->expr->where);
4198 	      a->expr->error = 1;
4199 	      return false;
4200 	    }
4201 	}
4202 
4203       return true;
4204     }
4205 
4206   dummy_args = gfc_sym_get_dummy_args (sym);
4207 
4208   /* For a statement function, check that types and type parameters of actual
4209      arguments and dummy arguments match.  */
4210   if (!gfc_compare_actual_formal (ap, dummy_args, 0, sym->attr.elemental,
4211 				  sym->attr.proc == PROC_ST_FUNCTION, where))
4212     return false;
4213 
4214   if (!check_intents (dummy_args, *ap))
4215     return false;
4216 
4217   if (warn_aliasing)
4218     check_some_aliasing (dummy_args, *ap);
4219 
4220   return true;
4221 }
4222 
4223 
4224 /* Check how a procedure pointer component is used against its interface.
4225    If all goes well, the actual argument list will also end up being properly
4226    sorted. Completely analogous to gfc_procedure_use.  */
4227 
4228 void
gfc_ppc_use(gfc_component * comp,gfc_actual_arglist ** ap,locus * where)4229 gfc_ppc_use (gfc_component *comp, gfc_actual_arglist **ap, locus *where)
4230 {
4231   /* Warn about calls with an implicit interface.  Special case
4232      for calling a ISO_C_BINDING because c_loc and c_funloc
4233      are pseudo-unknown.  */
4234   if (warn_implicit_interface
4235       && comp->attr.if_source == IFSRC_UNKNOWN
4236       && !comp->attr.is_iso_c)
4237     gfc_warning (OPT_Wimplicit_interface,
4238 		 "Procedure pointer component %qs called with an implicit "
4239 		 "interface at %L", comp->name, where);
4240 
4241   if (comp->attr.if_source == IFSRC_UNKNOWN)
4242     {
4243       gfc_actual_arglist *a;
4244       for (a = *ap; a; a = a->next)
4245 	{
4246 	  /* Skip g77 keyword extensions like %VAL, %REF, %LOC.  */
4247 	  if (a->name != NULL && a->name[0] != '%')
4248 	    {
4249 	      gfc_error ("Keyword argument requires explicit interface "
4250 			 "for procedure pointer component %qs at %L",
4251 			 comp->name, &a->expr->where);
4252 	      break;
4253 	    }
4254 	}
4255 
4256       return;
4257     }
4258 
4259   if (!gfc_compare_actual_formal (ap, comp->ts.interface->formal, 0,
4260 			      comp->attr.elemental, false, where))
4261     return;
4262 
4263   check_intents (comp->ts.interface->formal, *ap);
4264   if (warn_aliasing)
4265     check_some_aliasing (comp->ts.interface->formal, *ap);
4266 }
4267 
4268 
4269 /* Try if an actual argument list matches the formal list of a symbol,
4270    respecting the symbol's attributes like ELEMENTAL.  This is used for
4271    GENERIC resolution.  */
4272 
4273 bool
gfc_arglist_matches_symbol(gfc_actual_arglist ** args,gfc_symbol * sym)4274 gfc_arglist_matches_symbol (gfc_actual_arglist** args, gfc_symbol* sym)
4275 {
4276   gfc_formal_arglist *dummy_args;
4277   bool r;
4278 
4279   if (sym->attr.flavor != FL_PROCEDURE)
4280     return false;
4281 
4282   dummy_args = gfc_sym_get_dummy_args (sym);
4283 
4284   r = !sym->attr.elemental;
4285   if (gfc_compare_actual_formal (args, dummy_args, r, !r, false, NULL))
4286     {
4287       check_intents (dummy_args, *args);
4288       if (warn_aliasing)
4289 	check_some_aliasing (dummy_args, *args);
4290       return true;
4291     }
4292 
4293   return false;
4294 }
4295 
4296 
4297 /* Given an interface pointer and an actual argument list, search for
4298    a formal argument list that matches the actual.  If found, returns
4299    a pointer to the symbol of the correct interface.  Returns NULL if
4300    not found.  */
4301 
4302 gfc_symbol *
gfc_search_interface(gfc_interface * intr,int sub_flag,gfc_actual_arglist ** ap)4303 gfc_search_interface (gfc_interface *intr, int sub_flag,
4304 		      gfc_actual_arglist **ap)
4305 {
4306   gfc_symbol *elem_sym = NULL;
4307   gfc_symbol *null_sym = NULL;
4308   locus null_expr_loc;
4309   gfc_actual_arglist *a;
4310   bool has_null_arg = false;
4311 
4312   for (a = *ap; a; a = a->next)
4313     if (a->expr && a->expr->expr_type == EXPR_NULL
4314 	&& a->expr->ts.type == BT_UNKNOWN)
4315       {
4316 	has_null_arg = true;
4317 	null_expr_loc = a->expr->where;
4318 	break;
4319       }
4320 
4321   for (; intr; intr = intr->next)
4322     {
4323       if (gfc_fl_struct (intr->sym->attr.flavor))
4324 	continue;
4325       if (sub_flag && intr->sym->attr.function)
4326 	continue;
4327       if (!sub_flag && intr->sym->attr.subroutine)
4328 	continue;
4329 
4330       if (gfc_arglist_matches_symbol (ap, intr->sym))
4331 	{
4332 	  if (has_null_arg && null_sym)
4333 	    {
4334 	      gfc_error ("MOLD= required in NULL() argument at %L: Ambiguity "
4335 			 "between specific functions %s and %s",
4336 			 &null_expr_loc, null_sym->name, intr->sym->name);
4337 	      return NULL;
4338 	    }
4339 	  else if (has_null_arg)
4340 	    {
4341 	      null_sym = intr->sym;
4342 	      continue;
4343 	    }
4344 
4345 	  /* Satisfy 12.4.4.1 such that an elemental match has lower
4346 	     weight than a non-elemental match.  */
4347 	  if (intr->sym->attr.elemental)
4348 	    {
4349 	      elem_sym = intr->sym;
4350 	      continue;
4351 	    }
4352 	  return intr->sym;
4353 	}
4354     }
4355 
4356   if (null_sym)
4357     return null_sym;
4358 
4359   return elem_sym ? elem_sym : NULL;
4360 }
4361 
4362 
4363 /* Do a brute force recursive search for a symbol.  */
4364 
4365 static gfc_symtree *
find_symtree0(gfc_symtree * root,gfc_symbol * sym)4366 find_symtree0 (gfc_symtree *root, gfc_symbol *sym)
4367 {
4368   gfc_symtree * st;
4369 
4370   if (root->n.sym == sym)
4371     return root;
4372 
4373   st = NULL;
4374   if (root->left)
4375     st = find_symtree0 (root->left, sym);
4376   if (root->right && ! st)
4377     st = find_symtree0 (root->right, sym);
4378   return st;
4379 }
4380 
4381 
4382 /* Find a symtree for a symbol.  */
4383 
4384 gfc_symtree *
gfc_find_sym_in_symtree(gfc_symbol * sym)4385 gfc_find_sym_in_symtree (gfc_symbol *sym)
4386 {
4387   gfc_symtree *st;
4388   gfc_namespace *ns;
4389 
4390   /* First try to find it by name.  */
4391   gfc_find_sym_tree (sym->name, gfc_current_ns, 1, &st);
4392   if (st && st->n.sym == sym)
4393     return st;
4394 
4395   /* If it's been renamed, resort to a brute-force search.  */
4396   /* TODO: avoid having to do this search.  If the symbol doesn't exist
4397      in the symtree for the current namespace, it should probably be added.  */
4398   for (ns = gfc_current_ns; ns; ns = ns->parent)
4399     {
4400       st = find_symtree0 (ns->sym_root, sym);
4401       if (st)
4402 	return st;
4403     }
4404   gfc_internal_error ("Unable to find symbol %qs", sym->name);
4405   /* Not reached.  */
4406 }
4407 
4408 
4409 /* See if the arglist to an operator-call contains a derived-type argument
4410    with a matching type-bound operator.  If so, return the matching specific
4411    procedure defined as operator-target as well as the base-object to use
4412    (which is the found derived-type argument with operator).  The generic
4413    name, if any, is transmitted to the final expression via 'gname'.  */
4414 
4415 static gfc_typebound_proc*
matching_typebound_op(gfc_expr ** tb_base,gfc_actual_arglist * args,gfc_intrinsic_op op,const char * uop,const char ** gname)4416 matching_typebound_op (gfc_expr** tb_base,
4417 		       gfc_actual_arglist* args,
4418 		       gfc_intrinsic_op op, const char* uop,
4419 		       const char ** gname)
4420 {
4421   gfc_actual_arglist* base;
4422 
4423   for (base = args; base; base = base->next)
4424     if (base->expr->ts.type == BT_DERIVED || base->expr->ts.type == BT_CLASS)
4425       {
4426 	gfc_typebound_proc* tb;
4427 	gfc_symbol* derived;
4428 	bool result;
4429 
4430 	while (base->expr->expr_type == EXPR_OP
4431 	       && base->expr->value.op.op == INTRINSIC_PARENTHESES)
4432 	  base->expr = base->expr->value.op.op1;
4433 
4434 	if (base->expr->ts.type == BT_CLASS)
4435 	  {
4436 	    if (!base->expr->ts.u.derived || CLASS_DATA (base->expr) == NULL
4437 		|| !gfc_expr_attr (base->expr).class_ok)
4438 	      continue;
4439 	    derived = CLASS_DATA (base->expr)->ts.u.derived;
4440 	  }
4441 	else
4442 	  derived = base->expr->ts.u.derived;
4443 
4444 	if (op == INTRINSIC_USER)
4445 	  {
4446 	    gfc_symtree* tb_uop;
4447 
4448 	    gcc_assert (uop);
4449 	    tb_uop = gfc_find_typebound_user_op (derived, &result, uop,
4450 						 false, NULL);
4451 
4452 	    if (tb_uop)
4453 	      tb = tb_uop->n.tb;
4454 	    else
4455 	      tb = NULL;
4456 	  }
4457 	else
4458 	  tb = gfc_find_typebound_intrinsic_op (derived, &result, op,
4459 						false, NULL);
4460 
4461 	/* This means we hit a PRIVATE operator which is use-associated and
4462 	   should thus not be seen.  */
4463 	if (!result)
4464 	  tb = NULL;
4465 
4466 	/* Look through the super-type hierarchy for a matching specific
4467 	   binding.  */
4468 	for (; tb; tb = tb->overridden)
4469 	  {
4470 	    gfc_tbp_generic* g;
4471 
4472 	    gcc_assert (tb->is_generic);
4473 	    for (g = tb->u.generic; g; g = g->next)
4474 	      {
4475 		gfc_symbol* target;
4476 		gfc_actual_arglist* argcopy;
4477 		bool matches;
4478 
4479 		gcc_assert (g->specific);
4480 		if (g->specific->error)
4481 		  continue;
4482 
4483 		target = g->specific->u.specific->n.sym;
4484 
4485 		/* Check if this arglist matches the formal.  */
4486 		argcopy = gfc_copy_actual_arglist (args);
4487 		matches = gfc_arglist_matches_symbol (&argcopy, target);
4488 		gfc_free_actual_arglist (argcopy);
4489 
4490 		/* Return if we found a match.  */
4491 		if (matches)
4492 		  {
4493 		    *tb_base = base->expr;
4494 		    *gname = g->specific_st->name;
4495 		    return g->specific;
4496 		  }
4497 	      }
4498 	  }
4499       }
4500 
4501   return NULL;
4502 }
4503 
4504 
4505 /* For the 'actual arglist' of an operator call and a specific typebound
4506    procedure that has been found the target of a type-bound operator, build the
4507    appropriate EXPR_COMPCALL and resolve it.  We take this indirection over
4508    type-bound procedures rather than resolving type-bound operators 'directly'
4509    so that we can reuse the existing logic.  */
4510 
4511 static void
build_compcall_for_operator(gfc_expr * e,gfc_actual_arglist * actual,gfc_expr * base,gfc_typebound_proc * target,const char * gname)4512 build_compcall_for_operator (gfc_expr* e, gfc_actual_arglist* actual,
4513 			     gfc_expr* base, gfc_typebound_proc* target,
4514 			     const char *gname)
4515 {
4516   e->expr_type = EXPR_COMPCALL;
4517   e->value.compcall.tbp = target;
4518   e->value.compcall.name = gname ? gname : "$op";
4519   e->value.compcall.actual = actual;
4520   e->value.compcall.base_object = base;
4521   e->value.compcall.ignore_pass = 1;
4522   e->value.compcall.assign = 0;
4523   if (e->ts.type == BT_UNKNOWN
4524 	&& target->function)
4525     {
4526       if (target->is_generic)
4527 	e->ts = target->u.generic->specific->u.specific->n.sym->ts;
4528       else
4529 	e->ts = target->u.specific->n.sym->ts;
4530     }
4531 }
4532 
4533 
4534 /* This subroutine is called when an expression is being resolved.
4535    The expression node in question is either a user defined operator
4536    or an intrinsic operator with arguments that aren't compatible
4537    with the operator.  This subroutine builds an actual argument list
4538    corresponding to the operands, then searches for a compatible
4539    interface.  If one is found, the expression node is replaced with
4540    the appropriate function call. We use the 'match' enum to specify
4541    whether a replacement has been made or not, or if an error occurred.  */
4542 
4543 match
gfc_extend_expr(gfc_expr * e)4544 gfc_extend_expr (gfc_expr *e)
4545 {
4546   gfc_actual_arglist *actual;
4547   gfc_symbol *sym;
4548   gfc_namespace *ns;
4549   gfc_user_op *uop;
4550   gfc_intrinsic_op i;
4551   const char *gname;
4552   gfc_typebound_proc* tbo;
4553   gfc_expr* tb_base;
4554 
4555   sym = NULL;
4556 
4557   actual = gfc_get_actual_arglist ();
4558   actual->expr = e->value.op.op1;
4559 
4560   gname = NULL;
4561 
4562   if (e->value.op.op2 != NULL)
4563     {
4564       actual->next = gfc_get_actual_arglist ();
4565       actual->next->expr = e->value.op.op2;
4566     }
4567 
4568   i = fold_unary_intrinsic (e->value.op.op);
4569 
4570   /* See if we find a matching type-bound operator.  */
4571   if (i == INTRINSIC_USER)
4572     tbo = matching_typebound_op (&tb_base, actual,
4573 				  i, e->value.op.uop->name, &gname);
4574   else
4575     switch (i)
4576       {
4577 #define CHECK_OS_COMPARISON(comp) \
4578   case INTRINSIC_##comp: \
4579   case INTRINSIC_##comp##_OS: \
4580     tbo = matching_typebound_op (&tb_base, actual, \
4581 				 INTRINSIC_##comp, NULL, &gname); \
4582     if (!tbo) \
4583       tbo = matching_typebound_op (&tb_base, actual, \
4584 				   INTRINSIC_##comp##_OS, NULL, &gname); \
4585     break;
4586 	CHECK_OS_COMPARISON(EQ)
4587 	CHECK_OS_COMPARISON(NE)
4588 	CHECK_OS_COMPARISON(GT)
4589 	CHECK_OS_COMPARISON(GE)
4590 	CHECK_OS_COMPARISON(LT)
4591 	CHECK_OS_COMPARISON(LE)
4592 #undef CHECK_OS_COMPARISON
4593 
4594 	default:
4595 	  tbo = matching_typebound_op (&tb_base, actual, i, NULL, &gname);
4596 	  break;
4597       }
4598 
4599   /* If there is a matching typebound-operator, replace the expression with
4600       a call to it and succeed.  */
4601   if (tbo)
4602     {
4603       gcc_assert (tb_base);
4604       build_compcall_for_operator (e, actual, tb_base, tbo, gname);
4605 
4606       if (!gfc_resolve_expr (e))
4607 	return MATCH_ERROR;
4608       else
4609 	return MATCH_YES;
4610     }
4611 
4612   if (i == INTRINSIC_USER)
4613     {
4614       for (ns = gfc_current_ns; ns; ns = ns->parent)
4615 	{
4616 	  uop = gfc_find_uop (e->value.op.uop->name, ns);
4617 	  if (uop == NULL)
4618 	    continue;
4619 
4620 	  sym = gfc_search_interface (uop->op, 0, &actual);
4621 	  if (sym != NULL)
4622 	    break;
4623 	}
4624     }
4625   else
4626     {
4627       for (ns = gfc_current_ns; ns; ns = ns->parent)
4628 	{
4629 	  /* Due to the distinction between '==' and '.eq.' and friends, one has
4630 	     to check if either is defined.  */
4631 	  switch (i)
4632 	    {
4633 #define CHECK_OS_COMPARISON(comp) \
4634   case INTRINSIC_##comp: \
4635   case INTRINSIC_##comp##_OS: \
4636     sym = gfc_search_interface (ns->op[INTRINSIC_##comp], 0, &actual); \
4637     if (!sym) \
4638       sym = gfc_search_interface (ns->op[INTRINSIC_##comp##_OS], 0, &actual); \
4639     break;
4640 	      CHECK_OS_COMPARISON(EQ)
4641 	      CHECK_OS_COMPARISON(NE)
4642 	      CHECK_OS_COMPARISON(GT)
4643 	      CHECK_OS_COMPARISON(GE)
4644 	      CHECK_OS_COMPARISON(LT)
4645 	      CHECK_OS_COMPARISON(LE)
4646 #undef CHECK_OS_COMPARISON
4647 
4648 	      default:
4649 		sym = gfc_search_interface (ns->op[i], 0, &actual);
4650 	    }
4651 
4652 	  if (sym != NULL)
4653 	    break;
4654 	}
4655     }
4656 
4657   /* TODO: Do an ambiguity-check and error if multiple matching interfaces are
4658      found rather than just taking the first one and not checking further.  */
4659 
4660   if (sym == NULL)
4661     {
4662       /* Don't use gfc_free_actual_arglist().  */
4663       free (actual->next);
4664       free (actual);
4665       return MATCH_NO;
4666     }
4667 
4668   /* Change the expression node to a function call.  */
4669   e->expr_type = EXPR_FUNCTION;
4670   e->symtree = gfc_find_sym_in_symtree (sym);
4671   e->value.function.actual = actual;
4672   e->value.function.esym = NULL;
4673   e->value.function.isym = NULL;
4674   e->value.function.name = NULL;
4675   e->user_operator = 1;
4676 
4677   if (!gfc_resolve_expr (e))
4678     return MATCH_ERROR;
4679 
4680   return MATCH_YES;
4681 }
4682 
4683 
4684 /* Tries to replace an assignment code node with a subroutine call to the
4685    subroutine associated with the assignment operator. Return true if the node
4686    was replaced. On false, no error is generated.  */
4687 
4688 bool
gfc_extend_assign(gfc_code * c,gfc_namespace * ns)4689 gfc_extend_assign (gfc_code *c, gfc_namespace *ns)
4690 {
4691   gfc_actual_arglist *actual;
4692   gfc_expr *lhs, *rhs, *tb_base;
4693   gfc_symbol *sym = NULL;
4694   const char *gname = NULL;
4695   gfc_typebound_proc* tbo;
4696 
4697   lhs = c->expr1;
4698   rhs = c->expr2;
4699 
4700   /* Don't allow an intrinsic assignment with a BOZ rhs to be replaced.  */
4701   if (c->op == EXEC_ASSIGN
4702       && c->expr1->expr_type == EXPR_VARIABLE
4703       && c->expr2->expr_type == EXPR_CONSTANT && c->expr2->ts.type == BT_BOZ)
4704     return false;
4705 
4706   /* Don't allow an intrinsic assignment to be replaced.  */
4707   if (lhs->ts.type != BT_DERIVED && lhs->ts.type != BT_CLASS
4708       && (rhs->rank == 0 || rhs->rank == lhs->rank)
4709       && (lhs->ts.type == rhs->ts.type
4710 	  || (gfc_numeric_ts (&lhs->ts) && gfc_numeric_ts (&rhs->ts))))
4711     return false;
4712 
4713   actual = gfc_get_actual_arglist ();
4714   actual->expr = lhs;
4715 
4716   actual->next = gfc_get_actual_arglist ();
4717   actual->next->expr = rhs;
4718 
4719   /* TODO: Ambiguity-check, see above for gfc_extend_expr.  */
4720 
4721   /* See if we find a matching type-bound assignment.  */
4722   tbo = matching_typebound_op (&tb_base, actual, INTRINSIC_ASSIGN,
4723 			       NULL, &gname);
4724 
4725   if (tbo)
4726     {
4727       /* Success: Replace the expression with a type-bound call.  */
4728       gcc_assert (tb_base);
4729       c->expr1 = gfc_get_expr ();
4730       build_compcall_for_operator (c->expr1, actual, tb_base, tbo, gname);
4731       c->expr1->value.compcall.assign = 1;
4732       c->expr1->where = c->loc;
4733       c->expr2 = NULL;
4734       c->op = EXEC_COMPCALL;
4735       return true;
4736     }
4737 
4738   /* See if we find an 'ordinary' (non-typebound) assignment procedure.  */
4739   for (; ns; ns = ns->parent)
4740     {
4741       sym = gfc_search_interface (ns->op[INTRINSIC_ASSIGN], 1, &actual);
4742       if (sym != NULL)
4743 	break;
4744     }
4745 
4746   if (sym)
4747     {
4748       /* Success: Replace the assignment with the call.  */
4749       c->op = EXEC_ASSIGN_CALL;
4750       c->symtree = gfc_find_sym_in_symtree (sym);
4751       c->expr1 = NULL;
4752       c->expr2 = NULL;
4753       c->ext.actual = actual;
4754       return true;
4755     }
4756 
4757   /* Failure: No assignment procedure found.  */
4758   free (actual->next);
4759   free (actual);
4760   return false;
4761 }
4762 
4763 
4764 /* Make sure that the interface just parsed is not already present in
4765    the given interface list.  Ambiguity isn't checked yet since module
4766    procedures can be present without interfaces.  */
4767 
4768 bool
gfc_check_new_interface(gfc_interface * base,gfc_symbol * new_sym,locus loc)4769 gfc_check_new_interface (gfc_interface *base, gfc_symbol *new_sym, locus loc)
4770 {
4771   gfc_interface *ip;
4772 
4773   for (ip = base; ip; ip = ip->next)
4774     {
4775       if (ip->sym == new_sym)
4776 	{
4777 	  gfc_error ("Entity %qs at %L is already present in the interface",
4778 		     new_sym->name, &loc);
4779 	  return false;
4780 	}
4781     }
4782 
4783   return true;
4784 }
4785 
4786 
4787 /* Add a symbol to the current interface.  */
4788 
4789 bool
gfc_add_interface(gfc_symbol * new_sym)4790 gfc_add_interface (gfc_symbol *new_sym)
4791 {
4792   gfc_interface **head, *intr;
4793   gfc_namespace *ns;
4794   gfc_symbol *sym;
4795 
4796   switch (current_interface.type)
4797     {
4798     case INTERFACE_NAMELESS:
4799     case INTERFACE_ABSTRACT:
4800       return true;
4801 
4802     case INTERFACE_INTRINSIC_OP:
4803       for (ns = current_interface.ns; ns; ns = ns->parent)
4804 	switch (current_interface.op)
4805 	  {
4806 	    case INTRINSIC_EQ:
4807 	    case INTRINSIC_EQ_OS:
4808 	      if (!gfc_check_new_interface (ns->op[INTRINSIC_EQ], new_sym,
4809 					    gfc_current_locus)
4810 	          || !gfc_check_new_interface (ns->op[INTRINSIC_EQ_OS],
4811 					       new_sym, gfc_current_locus))
4812 		return false;
4813 	      break;
4814 
4815 	    case INTRINSIC_NE:
4816 	    case INTRINSIC_NE_OS:
4817 	      if (!gfc_check_new_interface (ns->op[INTRINSIC_NE], new_sym,
4818 					    gfc_current_locus)
4819 	          || !gfc_check_new_interface (ns->op[INTRINSIC_NE_OS],
4820 					       new_sym, gfc_current_locus))
4821 		return false;
4822 	      break;
4823 
4824 	    case INTRINSIC_GT:
4825 	    case INTRINSIC_GT_OS:
4826 	      if (!gfc_check_new_interface (ns->op[INTRINSIC_GT],
4827 					    new_sym, gfc_current_locus)
4828 	          || !gfc_check_new_interface (ns->op[INTRINSIC_GT_OS],
4829 					       new_sym, gfc_current_locus))
4830 		return false;
4831 	      break;
4832 
4833 	    case INTRINSIC_GE:
4834 	    case INTRINSIC_GE_OS:
4835 	      if (!gfc_check_new_interface (ns->op[INTRINSIC_GE],
4836 					    new_sym, gfc_current_locus)
4837 	          || !gfc_check_new_interface (ns->op[INTRINSIC_GE_OS],
4838 					       new_sym, gfc_current_locus))
4839 		return false;
4840 	      break;
4841 
4842 	    case INTRINSIC_LT:
4843 	    case INTRINSIC_LT_OS:
4844 	      if (!gfc_check_new_interface (ns->op[INTRINSIC_LT],
4845 					    new_sym, gfc_current_locus)
4846 	          || !gfc_check_new_interface (ns->op[INTRINSIC_LT_OS],
4847 					       new_sym, gfc_current_locus))
4848 		return false;
4849 	      break;
4850 
4851 	    case INTRINSIC_LE:
4852 	    case INTRINSIC_LE_OS:
4853 	      if (!gfc_check_new_interface (ns->op[INTRINSIC_LE],
4854 					    new_sym, gfc_current_locus)
4855 	          || !gfc_check_new_interface (ns->op[INTRINSIC_LE_OS],
4856 					       new_sym, gfc_current_locus))
4857 		return false;
4858 	      break;
4859 
4860 	    default:
4861 	      if (!gfc_check_new_interface (ns->op[current_interface.op],
4862 					    new_sym, gfc_current_locus))
4863 		return false;
4864 	  }
4865 
4866       head = &current_interface.ns->op[current_interface.op];
4867       break;
4868 
4869     case INTERFACE_GENERIC:
4870     case INTERFACE_DTIO:
4871       for (ns = current_interface.ns; ns; ns = ns->parent)
4872 	{
4873 	  gfc_find_symbol (current_interface.sym->name, ns, 0, &sym);
4874 	  if (sym == NULL)
4875 	    continue;
4876 
4877 	  if (!gfc_check_new_interface (sym->generic,
4878 					new_sym, gfc_current_locus))
4879 	    return false;
4880 	}
4881 
4882       head = &current_interface.sym->generic;
4883       break;
4884 
4885     case INTERFACE_USER_OP:
4886       if (!gfc_check_new_interface (current_interface.uop->op,
4887 				    new_sym, gfc_current_locus))
4888 	return false;
4889 
4890       head = &current_interface.uop->op;
4891       break;
4892 
4893     default:
4894       gfc_internal_error ("gfc_add_interface(): Bad interface type");
4895     }
4896 
4897   intr = gfc_get_interface ();
4898   intr->sym = new_sym;
4899   intr->where = gfc_current_locus;
4900 
4901   intr->next = *head;
4902   *head = intr;
4903 
4904   return true;
4905 }
4906 
4907 
4908 gfc_interface *
gfc_current_interface_head(void)4909 gfc_current_interface_head (void)
4910 {
4911   switch (current_interface.type)
4912     {
4913       case INTERFACE_INTRINSIC_OP:
4914 	return current_interface.ns->op[current_interface.op];
4915 
4916       case INTERFACE_GENERIC:
4917       case INTERFACE_DTIO:
4918 	return current_interface.sym->generic;
4919 
4920       case INTERFACE_USER_OP:
4921 	return current_interface.uop->op;
4922 
4923       default:
4924 	gcc_unreachable ();
4925     }
4926 }
4927 
4928 
4929 void
gfc_set_current_interface_head(gfc_interface * i)4930 gfc_set_current_interface_head (gfc_interface *i)
4931 {
4932   switch (current_interface.type)
4933     {
4934       case INTERFACE_INTRINSIC_OP:
4935 	current_interface.ns->op[current_interface.op] = i;
4936 	break;
4937 
4938       case INTERFACE_GENERIC:
4939       case INTERFACE_DTIO:
4940 	current_interface.sym->generic = i;
4941 	break;
4942 
4943       case INTERFACE_USER_OP:
4944 	current_interface.uop->op = i;
4945 	break;
4946 
4947       default:
4948 	gcc_unreachable ();
4949     }
4950 }
4951 
4952 
4953 /* Gets rid of a formal argument list.  We do not free symbols.
4954    Symbols are freed when a namespace is freed.  */
4955 
4956 void
gfc_free_formal_arglist(gfc_formal_arglist * p)4957 gfc_free_formal_arglist (gfc_formal_arglist *p)
4958 {
4959   gfc_formal_arglist *q;
4960 
4961   for (; p; p = q)
4962     {
4963       q = p->next;
4964       free (p);
4965     }
4966 }
4967 
4968 
4969 /* Check that it is ok for the type-bound procedure 'proc' to override the
4970    procedure 'old', cf. F08:4.5.7.3.  */
4971 
4972 bool
gfc_check_typebound_override(gfc_symtree * proc,gfc_symtree * old)4973 gfc_check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
4974 {
4975   locus where;
4976   gfc_symbol *proc_target, *old_target;
4977   unsigned proc_pass_arg, old_pass_arg, argpos;
4978   gfc_formal_arglist *proc_formal, *old_formal;
4979   bool check_type;
4980   char err[200];
4981 
4982   /* This procedure should only be called for non-GENERIC proc.  */
4983   gcc_assert (!proc->n.tb->is_generic);
4984 
4985   /* If the overwritten procedure is GENERIC, this is an error.  */
4986   if (old->n.tb->is_generic)
4987     {
4988       gfc_error ("Cannot overwrite GENERIC %qs at %L",
4989 		 old->name, &proc->n.tb->where);
4990       return false;
4991     }
4992 
4993   where = proc->n.tb->where;
4994   proc_target = proc->n.tb->u.specific->n.sym;
4995   old_target = old->n.tb->u.specific->n.sym;
4996 
4997   /* Check that overridden binding is not NON_OVERRIDABLE.  */
4998   if (old->n.tb->non_overridable)
4999     {
5000       gfc_error ("%qs at %L overrides a procedure binding declared"
5001 		 " NON_OVERRIDABLE", proc->name, &where);
5002       return false;
5003     }
5004 
5005   /* It's an error to override a non-DEFERRED procedure with a DEFERRED one.  */
5006   if (!old->n.tb->deferred && proc->n.tb->deferred)
5007     {
5008       gfc_error ("%qs at %L must not be DEFERRED as it overrides a"
5009 		 " non-DEFERRED binding", proc->name, &where);
5010       return false;
5011     }
5012 
5013   /* If the overridden binding is PURE, the overriding must be, too.  */
5014   if (old_target->attr.pure && !proc_target->attr.pure)
5015     {
5016       gfc_error ("%qs at %L overrides a PURE procedure and must also be PURE",
5017 		 proc->name, &where);
5018       return false;
5019     }
5020 
5021   /* If the overridden binding is ELEMENTAL, the overriding must be, too.  If it
5022      is not, the overriding must not be either.  */
5023   if (old_target->attr.elemental && !proc_target->attr.elemental)
5024     {
5025       gfc_error ("%qs at %L overrides an ELEMENTAL procedure and must also be"
5026 		 " ELEMENTAL", proc->name, &where);
5027       return false;
5028     }
5029   if (!old_target->attr.elemental && proc_target->attr.elemental)
5030     {
5031       gfc_error ("%qs at %L overrides a non-ELEMENTAL procedure and must not"
5032 		 " be ELEMENTAL, either", proc->name, &where);
5033       return false;
5034     }
5035 
5036   /* If the overridden binding is a SUBROUTINE, the overriding must also be a
5037      SUBROUTINE.  */
5038   if (old_target->attr.subroutine && !proc_target->attr.subroutine)
5039     {
5040       gfc_error ("%qs at %L overrides a SUBROUTINE and must also be a"
5041 		 " SUBROUTINE", proc->name, &where);
5042       return false;
5043     }
5044 
5045   /* If the overridden binding is a FUNCTION, the overriding must also be a
5046      FUNCTION and have the same characteristics.  */
5047   if (old_target->attr.function)
5048     {
5049       if (!proc_target->attr.function)
5050 	{
5051 	  gfc_error ("%qs at %L overrides a FUNCTION and must also be a"
5052 		     " FUNCTION", proc->name, &where);
5053 	  return false;
5054 	}
5055 
5056       if (!gfc_check_result_characteristics (proc_target, old_target,
5057 					     err, sizeof(err)))
5058 	{
5059 	  gfc_error ("Result mismatch for the overriding procedure "
5060 		     "%qs at %L: %s", proc->name, &where, err);
5061 	  return false;
5062 	}
5063     }
5064 
5065   /* If the overridden binding is PUBLIC, the overriding one must not be
5066      PRIVATE.  */
5067   if (old->n.tb->access == ACCESS_PUBLIC
5068       && proc->n.tb->access == ACCESS_PRIVATE)
5069     {
5070       gfc_error ("%qs at %L overrides a PUBLIC procedure and must not be"
5071 		 " PRIVATE", proc->name, &where);
5072       return false;
5073     }
5074 
5075   /* Compare the formal argument lists of both procedures.  This is also abused
5076      to find the position of the passed-object dummy arguments of both
5077      bindings as at least the overridden one might not yet be resolved and we
5078      need those positions in the check below.  */
5079   proc_pass_arg = old_pass_arg = 0;
5080   if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
5081     proc_pass_arg = 1;
5082   if (!old->n.tb->nopass && !old->n.tb->pass_arg)
5083     old_pass_arg = 1;
5084   argpos = 1;
5085   proc_formal = gfc_sym_get_dummy_args (proc_target);
5086   old_formal = gfc_sym_get_dummy_args (old_target);
5087   for ( ; proc_formal && old_formal;
5088        proc_formal = proc_formal->next, old_formal = old_formal->next)
5089     {
5090       if (proc->n.tb->pass_arg
5091 	  && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
5092 	proc_pass_arg = argpos;
5093       if (old->n.tb->pass_arg
5094 	  && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
5095 	old_pass_arg = argpos;
5096 
5097       /* Check that the names correspond.  */
5098       if (strcmp (proc_formal->sym->name, old_formal->sym->name))
5099 	{
5100 	  gfc_error ("Dummy argument %qs of %qs at %L should be named %qs as"
5101 		     " to match the corresponding argument of the overridden"
5102 		     " procedure", proc_formal->sym->name, proc->name, &where,
5103 		     old_formal->sym->name);
5104 	  return false;
5105 	}
5106 
5107       check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
5108       if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
5109 					check_type, err, sizeof(err)))
5110 	{
5111 	  gfc_error_opt (0, "Argument mismatch for the overriding procedure "
5112 			 "%qs at %L: %s", proc->name, &where, err);
5113 	  return false;
5114 	}
5115 
5116       ++argpos;
5117     }
5118   if (proc_formal || old_formal)
5119     {
5120       gfc_error ("%qs at %L must have the same number of formal arguments as"
5121 		 " the overridden procedure", proc->name, &where);
5122       return false;
5123     }
5124 
5125   /* If the overridden binding is NOPASS, the overriding one must also be
5126      NOPASS.  */
5127   if (old->n.tb->nopass && !proc->n.tb->nopass)
5128     {
5129       gfc_error ("%qs at %L overrides a NOPASS binding and must also be"
5130 		 " NOPASS", proc->name, &where);
5131       return false;
5132     }
5133 
5134   /* If the overridden binding is PASS(x), the overriding one must also be
5135      PASS and the passed-object dummy arguments must correspond.  */
5136   if (!old->n.tb->nopass)
5137     {
5138       if (proc->n.tb->nopass)
5139 	{
5140 	  gfc_error ("%qs at %L overrides a binding with PASS and must also be"
5141 		     " PASS", proc->name, &where);
5142 	  return false;
5143 	}
5144 
5145       if (proc_pass_arg != old_pass_arg)
5146 	{
5147 	  gfc_error ("Passed-object dummy argument of %qs at %L must be at"
5148 		     " the same position as the passed-object dummy argument of"
5149 		     " the overridden procedure", proc->name, &where);
5150 	  return false;
5151 	}
5152     }
5153 
5154   return true;
5155 }
5156 
5157 
5158 /* The following three functions check that the formal arguments
5159    of user defined derived type IO procedures are compliant with
5160    the requirements of the standard, see F03:9.5.3.7.2 (F08:9.6.4.8.3).  */
5161 
5162 static void
check_dtio_arg_TKR_intent(gfc_symbol * fsym,bool typebound,bt type,int kind,int rank,sym_intent intent)5163 check_dtio_arg_TKR_intent (gfc_symbol *fsym, bool typebound, bt type,
5164 			   int kind, int rank, sym_intent intent)
5165 {
5166   if (fsym->ts.type != type)
5167     {
5168       gfc_error ("DTIO dummy argument at %L must be of type %s",
5169 		 &fsym->declared_at, gfc_basic_typename (type));
5170       return;
5171     }
5172 
5173   if (fsym->ts.type != BT_CLASS && fsym->ts.type != BT_DERIVED
5174       && fsym->ts.kind != kind)
5175     gfc_error ("DTIO dummy argument at %L must be of KIND = %d",
5176 	       &fsym->declared_at, kind);
5177 
5178   if (!typebound
5179       && rank == 0
5180       && (((type == BT_CLASS) && CLASS_DATA (fsym)->attr.dimension)
5181 	  || ((type != BT_CLASS) && fsym->attr.dimension)))
5182     gfc_error ("DTIO dummy argument at %L must be a scalar",
5183 	       &fsym->declared_at);
5184   else if (rank == 1
5185 	   && (fsym->as == NULL || fsym->as->type != AS_ASSUMED_SHAPE))
5186     gfc_error ("DTIO dummy argument at %L must be an "
5187 	       "ASSUMED SHAPE ARRAY", &fsym->declared_at);
5188 
5189   if (type == BT_CHARACTER && fsym->ts.u.cl->length != NULL)
5190     gfc_error ("DTIO character argument at %L must have assumed length",
5191                &fsym->declared_at);
5192 
5193   if (fsym->attr.intent != intent)
5194     gfc_error ("DTIO dummy argument at %L must have INTENT %s",
5195 	       &fsym->declared_at, gfc_code2string (intents, (int)intent));
5196   return;
5197 }
5198 
5199 
5200 static void
check_dtio_interface1(gfc_symbol * derived,gfc_symtree * tb_io_st,bool typebound,bool formatted,int code)5201 check_dtio_interface1 (gfc_symbol *derived, gfc_symtree *tb_io_st,
5202 		       bool typebound, bool formatted, int code)
5203 {
5204   gfc_symbol *dtio_sub, *generic_proc, *fsym;
5205   gfc_typebound_proc *tb_io_proc, *specific_proc;
5206   gfc_interface *intr;
5207   gfc_formal_arglist *formal;
5208   int arg_num;
5209 
5210   bool read = ((dtio_codes)code == DTIO_RF)
5211 	       || ((dtio_codes)code == DTIO_RUF);
5212   bt type;
5213   sym_intent intent;
5214   int kind;
5215 
5216   dtio_sub = NULL;
5217   if (typebound)
5218     {
5219       /* Typebound DTIO binding.  */
5220       tb_io_proc = tb_io_st->n.tb;
5221       if (tb_io_proc == NULL)
5222 	return;
5223 
5224       gcc_assert (tb_io_proc->is_generic);
5225 
5226       specific_proc = tb_io_proc->u.generic->specific;
5227       if (specific_proc == NULL || specific_proc->is_generic)
5228 	return;
5229 
5230       dtio_sub = specific_proc->u.specific->n.sym;
5231     }
5232   else
5233     {
5234       generic_proc = tb_io_st->n.sym;
5235       if (generic_proc == NULL || generic_proc->generic == NULL)
5236 	return;
5237 
5238       for (intr = tb_io_st->n.sym->generic; intr; intr = intr->next)
5239 	{
5240 	  if (intr->sym && intr->sym->formal && intr->sym->formal->sym
5241 	      && ((intr->sym->formal->sym->ts.type == BT_CLASS
5242 	           && CLASS_DATA (intr->sym->formal->sym)->ts.u.derived
5243 							     == derived)
5244 		  || (intr->sym->formal->sym->ts.type == BT_DERIVED
5245 		      && intr->sym->formal->sym->ts.u.derived == derived)))
5246 	    {
5247 	      dtio_sub = intr->sym;
5248 	      break;
5249 	    }
5250 	  else if (intr->sym && intr->sym->formal && !intr->sym->formal->sym)
5251 	    {
5252 	      gfc_error ("Alternate return at %L is not permitted in a DTIO "
5253 			 "procedure", &intr->sym->declared_at);
5254 	      return;
5255 	    }
5256 	}
5257 
5258       if (dtio_sub == NULL)
5259 	return;
5260     }
5261 
5262   gcc_assert (dtio_sub);
5263   if (!dtio_sub->attr.subroutine)
5264     gfc_error ("DTIO procedure %qs at %L must be a subroutine",
5265 	       dtio_sub->name, &dtio_sub->declared_at);
5266 
5267   if (!dtio_sub->resolve_symbol_called)
5268     gfc_resolve_formal_arglist (dtio_sub);
5269 
5270   arg_num = 0;
5271   for (formal = dtio_sub->formal; formal; formal = formal->next)
5272     arg_num++;
5273 
5274   if (arg_num < (formatted ? 6 : 4))
5275     {
5276       gfc_error ("Too few dummy arguments in DTIO procedure %qs at %L",
5277 		 dtio_sub->name, &dtio_sub->declared_at);
5278       return;
5279     }
5280 
5281   if (arg_num > (formatted ? 6 : 4))
5282     {
5283       gfc_error ("Too many dummy arguments in DTIO procedure %qs at %L",
5284 		 dtio_sub->name, &dtio_sub->declared_at);
5285       return;
5286     }
5287 
5288   /* Now go through the formal arglist.  */
5289   arg_num = 1;
5290   for (formal = dtio_sub->formal; formal; formal = formal->next, arg_num++)
5291     {
5292       if (!formatted && arg_num == 3)
5293 	arg_num = 5;
5294       fsym = formal->sym;
5295 
5296       if (fsym == NULL)
5297 	{
5298 	  gfc_error ("Alternate return at %L is not permitted in a DTIO "
5299 		     "procedure", &dtio_sub->declared_at);
5300 	  return;
5301 	}
5302 
5303       switch (arg_num)
5304 	{
5305 	case(1):			/* DTV  */
5306 	  type = derived->attr.sequence || derived->attr.is_bind_c ?
5307 		 BT_DERIVED : BT_CLASS;
5308 	  kind = 0;
5309 	  intent = read ? INTENT_INOUT : INTENT_IN;
5310 	  check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5311 				     0, intent);
5312 	  break;
5313 
5314 	case(2):			/* UNIT  */
5315 	  type = BT_INTEGER;
5316 	  kind = gfc_default_integer_kind;
5317 	  intent = INTENT_IN;
5318 	  check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5319 				     0, intent);
5320 	  break;
5321 	case(3):			/* IOTYPE  */
5322 	  type = BT_CHARACTER;
5323 	  kind = gfc_default_character_kind;
5324 	  intent = INTENT_IN;
5325 	  check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5326 				     0, intent);
5327 	  break;
5328 	case(4):			/* VLIST  */
5329 	  type = BT_INTEGER;
5330 	  kind = gfc_default_integer_kind;
5331 	  intent = INTENT_IN;
5332 	  check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5333 				     1, intent);
5334 	  break;
5335 	case(5):			/* IOSTAT  */
5336 	  type = BT_INTEGER;
5337 	  kind = gfc_default_integer_kind;
5338 	  intent = INTENT_OUT;
5339 	  check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5340 				     0, intent);
5341 	  break;
5342 	case(6):			/* IOMSG  */
5343 	  type = BT_CHARACTER;
5344 	  kind = gfc_default_character_kind;
5345 	  intent = INTENT_INOUT;
5346 	  check_dtio_arg_TKR_intent (fsym, typebound, type, kind,
5347 				     0, intent);
5348 	  break;
5349 	default:
5350 	  gcc_unreachable ();
5351 	}
5352     }
5353   derived->attr.has_dtio_procs = 1;
5354   return;
5355 }
5356 
5357 void
gfc_check_dtio_interfaces(gfc_symbol * derived)5358 gfc_check_dtio_interfaces (gfc_symbol *derived)
5359 {
5360   gfc_symtree *tb_io_st;
5361   bool t = false;
5362   int code;
5363   bool formatted;
5364 
5365   if (derived->attr.is_class == 1 || derived->attr.vtype == 1)
5366     return;
5367 
5368   /* Check typebound DTIO bindings.  */
5369   for (code = 0; code < 4; code++)
5370     {
5371       formatted = ((dtio_codes)code == DTIO_RF)
5372 		   || ((dtio_codes)code == DTIO_WF);
5373 
5374       tb_io_st = gfc_find_typebound_proc (derived, &t,
5375 					  gfc_code2string (dtio_procs, code),
5376 					  true, &derived->declared_at);
5377       if (tb_io_st != NULL)
5378 	check_dtio_interface1 (derived, tb_io_st, true, formatted, code);
5379     }
5380 
5381   /* Check generic DTIO interfaces.  */
5382   for (code = 0; code < 4; code++)
5383     {
5384       formatted = ((dtio_codes)code == DTIO_RF)
5385 		   || ((dtio_codes)code == DTIO_WF);
5386 
5387       tb_io_st = gfc_find_symtree (derived->ns->sym_root,
5388 				   gfc_code2string (dtio_procs, code));
5389       if (tb_io_st != NULL)
5390 	check_dtio_interface1 (derived, tb_io_st, false, formatted, code);
5391     }
5392 }
5393 
5394 
5395 gfc_symtree*
gfc_find_typebound_dtio_proc(gfc_symbol * derived,bool write,bool formatted)5396 gfc_find_typebound_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5397 {
5398   gfc_symtree *tb_io_st = NULL;
5399   bool t = false;
5400 
5401   if (!derived || !derived->resolve_symbol_called
5402       || derived->attr.flavor != FL_DERIVED)
5403     return NULL;
5404 
5405   /* Try to find a typebound DTIO binding.  */
5406   if (formatted == true)
5407     {
5408       if (write == true)
5409         tb_io_st = gfc_find_typebound_proc (derived, &t,
5410 					    gfc_code2string (dtio_procs,
5411 							     DTIO_WF),
5412 					    true,
5413 					    &derived->declared_at);
5414       else
5415         tb_io_st = gfc_find_typebound_proc (derived, &t,
5416 					    gfc_code2string (dtio_procs,
5417 							     DTIO_RF),
5418 					    true,
5419 					    &derived->declared_at);
5420     }
5421   else
5422     {
5423       if (write == true)
5424         tb_io_st = gfc_find_typebound_proc (derived, &t,
5425 					    gfc_code2string (dtio_procs,
5426 							     DTIO_WUF),
5427 					    true,
5428 					    &derived->declared_at);
5429       else
5430         tb_io_st = gfc_find_typebound_proc (derived, &t,
5431 					    gfc_code2string (dtio_procs,
5432 							     DTIO_RUF),
5433 					    true,
5434 					    &derived->declared_at);
5435     }
5436   return tb_io_st;
5437 }
5438 
5439 
5440 gfc_symbol *
gfc_find_specific_dtio_proc(gfc_symbol * derived,bool write,bool formatted)5441 gfc_find_specific_dtio_proc (gfc_symbol *derived, bool write, bool formatted)
5442 {
5443   gfc_symtree *tb_io_st = NULL;
5444   gfc_symbol *dtio_sub = NULL;
5445   gfc_symbol *extended;
5446   gfc_typebound_proc *tb_io_proc, *specific_proc;
5447 
5448   tb_io_st = gfc_find_typebound_dtio_proc (derived, write, formatted);
5449 
5450   if (tb_io_st != NULL)
5451     {
5452       const char *genname;
5453       gfc_symtree *st;
5454 
5455       tb_io_proc = tb_io_st->n.tb;
5456       gcc_assert (tb_io_proc != NULL);
5457       gcc_assert (tb_io_proc->is_generic);
5458       gcc_assert (tb_io_proc->u.generic->next == NULL);
5459 
5460       specific_proc = tb_io_proc->u.generic->specific;
5461       gcc_assert (!specific_proc->is_generic);
5462 
5463       /* Go back and make sure that we have the right specific procedure.
5464 	 Here we most likely have a procedure from the parent type, which
5465 	 can be overridden in extensions.  */
5466       genname = tb_io_proc->u.generic->specific_st->name;
5467       st = gfc_find_typebound_proc (derived, NULL, genname,
5468 				    true, &tb_io_proc->where);
5469       if (st)
5470 	dtio_sub = st->n.tb->u.specific->n.sym;
5471       else
5472 	dtio_sub = specific_proc->u.specific->n.sym;
5473 
5474       goto finish;
5475     }
5476 
5477   /* If there is not a typebound binding, look for a generic
5478      DTIO interface.  */
5479   for (extended = derived; extended;
5480        extended = gfc_get_derived_super_type (extended))
5481     {
5482       if (extended == NULL || extended->ns == NULL
5483 	  || extended->attr.flavor == FL_UNKNOWN)
5484 	return NULL;
5485 
5486       if (formatted == true)
5487 	{
5488 	  if (write == true)
5489 	    tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5490 					 gfc_code2string (dtio_procs,
5491 							  DTIO_WF));
5492 	  else
5493 	    tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5494 					 gfc_code2string (dtio_procs,
5495 							  DTIO_RF));
5496 	}
5497       else
5498 	{
5499 	  if (write == true)
5500 	    tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5501 					 gfc_code2string (dtio_procs,
5502 							  DTIO_WUF));
5503 	  else
5504 	    tb_io_st = gfc_find_symtree (extended->ns->sym_root,
5505 					 gfc_code2string (dtio_procs,
5506 							  DTIO_RUF));
5507 	}
5508 
5509       if (tb_io_st != NULL
5510 	  && tb_io_st->n.sym
5511 	  && tb_io_st->n.sym->generic)
5512 	{
5513 	  for (gfc_interface *intr = tb_io_st->n.sym->generic;
5514 	       intr && intr->sym; intr = intr->next)
5515 	    {
5516 	      if (intr->sym->formal)
5517 		{
5518 		  gfc_symbol *fsym = intr->sym->formal->sym;
5519 		  if ((fsym->ts.type == BT_CLASS
5520 		      && CLASS_DATA (fsym)->ts.u.derived == extended)
5521 		      || (fsym->ts.type == BT_DERIVED
5522 			  && fsym->ts.u.derived == extended))
5523 		    {
5524 		      dtio_sub = intr->sym;
5525 		      break;
5526 		    }
5527 		}
5528 	    }
5529 	}
5530     }
5531 
5532 finish:
5533   if (dtio_sub
5534       && dtio_sub->formal->sym->ts.type == BT_CLASS
5535       && derived != CLASS_DATA (dtio_sub->formal->sym)->ts.u.derived)
5536     gfc_find_derived_vtab (derived);
5537 
5538   return dtio_sub;
5539 }
5540 
5541 /* Helper function - if we do not find an interface for a procedure,
5542    construct it from the actual arglist.  Luckily, this can only
5543    happen for call by reference, so the information we actually need
5544    to provide (and which would be impossible to guess from the call
5545    itself) is not actually needed.  */
5546 
5547 void
gfc_get_formal_from_actual_arglist(gfc_symbol * sym,gfc_actual_arglist * actual_args)5548 gfc_get_formal_from_actual_arglist (gfc_symbol *sym,
5549 				    gfc_actual_arglist *actual_args)
5550 {
5551   gfc_actual_arglist *a;
5552   gfc_formal_arglist **f;
5553   gfc_symbol *s;
5554   char name[GFC_MAX_SYMBOL_LEN + 1];
5555   static int var_num;
5556 
5557   f = &sym->formal;
5558   for (a = actual_args; a != NULL; a = a->next)
5559     {
5560       (*f) = gfc_get_formal_arglist ();
5561       if (a->expr)
5562 	{
5563 	  snprintf (name, GFC_MAX_SYMBOL_LEN, "_formal_%d", var_num ++);
5564 	  gfc_get_symbol (name, gfc_current_ns, &s);
5565 	  if (a->expr->ts.type == BT_PROCEDURE)
5566 	    {
5567 	      s->attr.flavor = FL_PROCEDURE;
5568 	    }
5569 	  else
5570 	    {
5571 	      s->ts = a->expr->ts;
5572 
5573 	      if (s->ts.type == BT_CHARACTER)
5574 		s->ts.u.cl = gfc_get_charlen ();
5575 
5576 	      s->ts.deferred = 0;
5577 	      s->ts.is_iso_c = 0;
5578 	      s->ts.is_c_interop = 0;
5579 	      s->attr.flavor = FL_VARIABLE;
5580 	      if (a->expr->rank > 0)
5581 		{
5582 		  s->attr.dimension = 1;
5583 		  s->as = gfc_get_array_spec ();
5584 		  s->as->rank = 1;
5585 		  s->as->lower[0] = gfc_get_int_expr (gfc_index_integer_kind,
5586 						      &a->expr->where, 1);
5587 		  s->as->upper[0] = NULL;
5588 		  s->as->type = AS_ASSUMED_SIZE;
5589 		}
5590 	      else
5591 		s->maybe_array = maybe_dummy_array_arg (a->expr);
5592 	    }
5593 	  s->attr.dummy = 1;
5594 	  s->attr.artificial = 1;
5595 	  s->declared_at = a->expr->where;
5596 	  s->attr.intent = INTENT_UNKNOWN;
5597 	  (*f)->sym = s;
5598 	}
5599       else  /* If a->expr is NULL, this is an alternate rerturn.  */
5600 	(*f)->sym = NULL;
5601 
5602       f = &((*f)->next);
5603     }
5604 }
5605 
5606 
5607 const char *
gfc_dummy_arg_get_name(gfc_dummy_arg & dummy_arg)5608 gfc_dummy_arg_get_name (gfc_dummy_arg & dummy_arg)
5609 {
5610   switch (dummy_arg.intrinsicness)
5611     {
5612     case GFC_INTRINSIC_DUMMY_ARG:
5613       return dummy_arg.u.intrinsic->name;
5614 
5615     case GFC_NON_INTRINSIC_DUMMY_ARG:
5616       return dummy_arg.u.non_intrinsic->sym->name;
5617 
5618     default:
5619       gcc_unreachable ();
5620     }
5621 }
5622 
5623 
5624 const gfc_typespec &
gfc_dummy_arg_get_typespec(gfc_dummy_arg & dummy_arg)5625 gfc_dummy_arg_get_typespec (gfc_dummy_arg & dummy_arg)
5626 {
5627   switch (dummy_arg.intrinsicness)
5628     {
5629     case GFC_INTRINSIC_DUMMY_ARG:
5630       return dummy_arg.u.intrinsic->ts;
5631 
5632     case GFC_NON_INTRINSIC_DUMMY_ARG:
5633       return dummy_arg.u.non_intrinsic->sym->ts;
5634 
5635     default:
5636       gcc_unreachable ();
5637     }
5638 }
5639 
5640 
5641 bool
gfc_dummy_arg_is_optional(gfc_dummy_arg & dummy_arg)5642 gfc_dummy_arg_is_optional (gfc_dummy_arg & dummy_arg)
5643 {
5644   switch (dummy_arg.intrinsicness)
5645     {
5646     case GFC_INTRINSIC_DUMMY_ARG:
5647       return dummy_arg.u.intrinsic->optional;
5648 
5649     case GFC_NON_INTRINSIC_DUMMY_ARG:
5650       return dummy_arg.u.non_intrinsic->sym->attr.optional;
5651 
5652     default:
5653       gcc_unreachable ();
5654     }
5655 }
5656