2010-10-20 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / resolve.c
blob4280555eae4632c2390fa3b5da6a1303d386ecd0
1 /* Perform type resolution on the various structures.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "arith.h" /* For gfc_compare_expr(). */
29 #include "dependency.h"
30 #include "data.h"
31 #include "target-memory.h" /* for gfc_simplify_transfer */
32 #include "constructor.h"
34 /* Types used in equivalence statements. */
36 typedef enum seq_type
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
40 seq_type;
42 /* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
45 typedef struct code_stack
47 struct gfc_code *head, *current;
48 struct code_stack *prev;
50 /* This bitmap keeps track of the targets valid for a branch from
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
53 bitmap reachable_labels;
55 code_stack;
57 static code_stack *cs_base = NULL;
60 /* Nonzero if we're inside a FORALL block. */
62 static int forall_flag;
64 /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
66 static int omp_workshare_flag;
68 /* Nonzero if we are processing a formal arglist. The corresponding function
69 resets the flag each time that it is read. */
70 static int formal_arg_flag = 0;
72 /* True if we are resolving a specification expression. */
73 static int specification_expr = 0;
75 /* The id of the last entry seen. */
76 static int current_entry_id;
78 /* We use bitmaps to determine if a branch target is valid. */
79 static bitmap_obstack labels_obstack;
81 /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
82 static bool inquiry_argument = false;
84 int
85 gfc_is_formal_arg (void)
87 return formal_arg_flag;
90 /* Is the symbol host associated? */
91 static bool
92 is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
94 for (ns = ns->parent; ns; ns = ns->parent)
96 if (sym->ns == ns)
97 return true;
100 return false;
103 /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
104 an ABSTRACT derived-type. If where is not NULL, an error message with that
105 locus is printed, optionally using name. */
107 static gfc_try
108 resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
110 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
112 if (where)
114 if (name)
115 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
116 name, where, ts->u.derived->name);
117 else
118 gfc_error ("ABSTRACT type '%s' used at %L",
119 ts->u.derived->name, where);
122 return FAILURE;
125 return SUCCESS;
129 static void resolve_symbol (gfc_symbol *sym);
130 static gfc_try resolve_intrinsic (gfc_symbol *sym, locus *loc);
133 /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
135 static gfc_try
136 resolve_procedure_interface (gfc_symbol *sym)
138 if (sym->ts.interface == sym)
140 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
141 sym->name, &sym->declared_at);
142 return FAILURE;
144 if (sym->ts.interface->attr.procedure)
146 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared "
147 "in a later PROCEDURE statement", sym->ts.interface->name,
148 sym->name, &sym->declared_at);
149 return FAILURE;
152 /* Get the attributes from the interface (now resolved). */
153 if (sym->ts.interface->attr.if_source || sym->ts.interface->attr.intrinsic)
155 gfc_symbol *ifc = sym->ts.interface;
156 resolve_symbol (ifc);
158 if (ifc->attr.intrinsic)
159 resolve_intrinsic (ifc, &ifc->declared_at);
161 if (ifc->result)
162 sym->ts = ifc->result->ts;
163 else
164 sym->ts = ifc->ts;
165 sym->ts.interface = ifc;
166 sym->attr.function = ifc->attr.function;
167 sym->attr.subroutine = ifc->attr.subroutine;
168 gfc_copy_formal_args (sym, ifc);
170 sym->attr.allocatable = ifc->attr.allocatable;
171 sym->attr.pointer = ifc->attr.pointer;
172 sym->attr.pure = ifc->attr.pure;
173 sym->attr.elemental = ifc->attr.elemental;
174 sym->attr.dimension = ifc->attr.dimension;
175 sym->attr.contiguous = ifc->attr.contiguous;
176 sym->attr.recursive = ifc->attr.recursive;
177 sym->attr.always_explicit = ifc->attr.always_explicit;
178 sym->attr.ext_attr |= ifc->attr.ext_attr;
179 /* Copy array spec. */
180 sym->as = gfc_copy_array_spec (ifc->as);
181 if (sym->as)
183 int i;
184 for (i = 0; i < sym->as->rank; i++)
186 gfc_expr_replace_symbols (sym->as->lower[i], sym);
187 gfc_expr_replace_symbols (sym->as->upper[i], sym);
190 /* Copy char length. */
191 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
193 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
194 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
195 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
196 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
197 return FAILURE;
200 else if (sym->ts.interface->name[0] != '\0')
202 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
203 sym->ts.interface->name, sym->name, &sym->declared_at);
204 return FAILURE;
207 return SUCCESS;
211 /* Resolve types of formal argument lists. These have to be done early so that
212 the formal argument lists of module procedures can be copied to the
213 containing module before the individual procedures are resolved
214 individually. We also resolve argument lists of procedures in interface
215 blocks because they are self-contained scoping units.
217 Since a dummy argument cannot be a non-dummy procedure, the only
218 resort left for untyped names are the IMPLICIT types. */
220 static void
221 resolve_formal_arglist (gfc_symbol *proc)
223 gfc_formal_arglist *f;
224 gfc_symbol *sym;
225 int i;
227 if (proc->result != NULL)
228 sym = proc->result;
229 else
230 sym = proc;
232 if (gfc_elemental (proc)
233 || sym->attr.pointer || sym->attr.allocatable
234 || (sym->as && sym->as->rank > 0))
236 proc->attr.always_explicit = 1;
237 sym->attr.always_explicit = 1;
240 formal_arg_flag = 1;
242 for (f = proc->formal; f; f = f->next)
244 sym = f->sym;
246 if (sym == NULL)
248 /* Alternate return placeholder. */
249 if (gfc_elemental (proc))
250 gfc_error ("Alternate return specifier in elemental subroutine "
251 "'%s' at %L is not allowed", proc->name,
252 &proc->declared_at);
253 if (proc->attr.function)
254 gfc_error ("Alternate return specifier in function "
255 "'%s' at %L is not allowed", proc->name,
256 &proc->declared_at);
257 continue;
259 else if (sym->attr.procedure && sym->ts.interface
260 && sym->attr.if_source != IFSRC_DECL)
261 resolve_procedure_interface (sym);
263 if (sym->attr.if_source != IFSRC_UNKNOWN)
264 resolve_formal_arglist (sym);
266 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
268 if (gfc_pure (proc) && !gfc_pure (sym))
270 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
271 "also be PURE", sym->name, &sym->declared_at);
272 continue;
275 if (gfc_elemental (proc))
277 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
278 "procedure", &sym->declared_at);
279 continue;
282 if (sym->attr.function
283 && sym->ts.type == BT_UNKNOWN
284 && sym->attr.intrinsic)
286 gfc_intrinsic_sym *isym;
287 isym = gfc_find_function (sym->name);
288 if (isym == NULL || !isym->specific)
290 gfc_error ("Unable to find a specific INTRINSIC procedure "
291 "for the reference '%s' at %L", sym->name,
292 &sym->declared_at);
294 sym->ts = isym->ts;
297 continue;
300 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
301 && (!sym->attr.function || sym->result == sym))
302 gfc_set_default_type (sym, 1, sym->ns);
304 gfc_resolve_array_spec (sym->as, 0);
306 /* We can't tell if an array with dimension (:) is assumed or deferred
307 shape until we know if it has the pointer or allocatable attributes.
309 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
310 && !(sym->attr.pointer || sym->attr.allocatable))
312 sym->as->type = AS_ASSUMED_SHAPE;
313 for (i = 0; i < sym->as->rank; i++)
314 sym->as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind,
315 NULL, 1);
318 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
319 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
320 || sym->attr.optional)
322 proc->attr.always_explicit = 1;
323 if (proc->result)
324 proc->result->attr.always_explicit = 1;
327 /* If the flavor is unknown at this point, it has to be a variable.
328 A procedure specification would have already set the type. */
330 if (sym->attr.flavor == FL_UNKNOWN)
331 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
333 if (gfc_pure (proc) && !sym->attr.pointer
334 && sym->attr.flavor != FL_PROCEDURE)
336 if (proc->attr.function && sym->attr.intent != INTENT_IN)
337 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
338 "INTENT(IN)", sym->name, proc->name,
339 &sym->declared_at);
341 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
342 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
343 "have its INTENT specified", sym->name, proc->name,
344 &sym->declared_at);
347 if (gfc_elemental (proc))
349 /* F2008, C1289. */
350 if (sym->attr.codimension)
352 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
353 "procedure", sym->name, &sym->declared_at);
354 continue;
357 if (sym->as != NULL)
359 gfc_error ("Argument '%s' of elemental procedure at %L must "
360 "be scalar", sym->name, &sym->declared_at);
361 continue;
364 if (sym->attr.allocatable)
366 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
367 "have the ALLOCATABLE attribute", sym->name,
368 &sym->declared_at);
369 continue;
372 if (sym->attr.pointer)
374 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
375 "have the POINTER attribute", sym->name,
376 &sym->declared_at);
377 continue;
380 if (sym->attr.flavor == FL_PROCEDURE)
382 gfc_error ("Dummy procedure '%s' not allowed in elemental "
383 "procedure '%s' at %L", sym->name, proc->name,
384 &sym->declared_at);
385 continue;
388 if (sym->attr.intent == INTENT_UNKNOWN)
390 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
391 "have its INTENT specified", sym->name, proc->name,
392 &sym->declared_at);
393 continue;
397 /* Each dummy shall be specified to be scalar. */
398 if (proc->attr.proc == PROC_ST_FUNCTION)
400 if (sym->as != NULL)
402 gfc_error ("Argument '%s' of statement function at %L must "
403 "be scalar", sym->name, &sym->declared_at);
404 continue;
407 if (sym->ts.type == BT_CHARACTER)
409 gfc_charlen *cl = sym->ts.u.cl;
410 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
412 gfc_error ("Character-valued argument '%s' of statement "
413 "function at %L must have constant length",
414 sym->name, &sym->declared_at);
415 continue;
420 formal_arg_flag = 0;
424 /* Work function called when searching for symbols that have argument lists
425 associated with them. */
427 static void
428 find_arglists (gfc_symbol *sym)
430 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
431 return;
433 resolve_formal_arglist (sym);
437 /* Given a namespace, resolve all formal argument lists within the namespace.
440 static void
441 resolve_formal_arglists (gfc_namespace *ns)
443 if (ns == NULL)
444 return;
446 gfc_traverse_ns (ns, find_arglists);
450 static void
451 resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
453 gfc_try t;
455 /* If this namespace is not a function or an entry master function,
456 ignore it. */
457 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
458 || sym->attr.entry_master)
459 return;
461 /* Try to find out of what the return type is. */
462 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
464 t = gfc_set_default_type (sym->result, 0, ns);
466 if (t == FAILURE && !sym->result->attr.untyped)
468 if (sym->result == sym)
469 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
470 sym->name, &sym->declared_at);
471 else if (!sym->result->attr.proc_pointer)
472 gfc_error ("Result '%s' of contained function '%s' at %L has "
473 "no IMPLICIT type", sym->result->name, sym->name,
474 &sym->result->declared_at);
475 sym->result->attr.untyped = 1;
479 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
480 type, lists the only ways a character length value of * can be used:
481 dummy arguments of procedures, named constants, and function results
482 in external functions. Internal function results and results of module
483 procedures are not on this list, ergo, not permitted. */
485 if (sym->result->ts.type == BT_CHARACTER)
487 gfc_charlen *cl = sym->result->ts.u.cl;
488 if (!cl || !cl->length)
490 /* See if this is a module-procedure and adapt error message
491 accordingly. */
492 bool module_proc;
493 gcc_assert (ns->parent && ns->parent->proc_name);
494 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
496 gfc_error ("Character-valued %s '%s' at %L must not be"
497 " assumed length",
498 module_proc ? _("module procedure")
499 : _("internal function"),
500 sym->name, &sym->declared_at);
506 /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
507 introduce duplicates. */
509 static void
510 merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
512 gfc_formal_arglist *f, *new_arglist;
513 gfc_symbol *new_sym;
515 for (; new_args != NULL; new_args = new_args->next)
517 new_sym = new_args->sym;
518 /* See if this arg is already in the formal argument list. */
519 for (f = proc->formal; f; f = f->next)
521 if (new_sym == f->sym)
522 break;
525 if (f)
526 continue;
528 /* Add a new argument. Argument order is not important. */
529 new_arglist = gfc_get_formal_arglist ();
530 new_arglist->sym = new_sym;
531 new_arglist->next = proc->formal;
532 proc->formal = new_arglist;
537 /* Flag the arguments that are not present in all entries. */
539 static void
540 check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
542 gfc_formal_arglist *f, *head;
543 head = new_args;
545 for (f = proc->formal; f; f = f->next)
547 if (f->sym == NULL)
548 continue;
550 for (new_args = head; new_args; new_args = new_args->next)
552 if (new_args->sym == f->sym)
553 break;
556 if (new_args)
557 continue;
559 f->sym->attr.not_always_present = 1;
564 /* Resolve alternate entry points. If a symbol has multiple entry points we
565 create a new master symbol for the main routine, and turn the existing
566 symbol into an entry point. */
568 static void
569 resolve_entries (gfc_namespace *ns)
571 gfc_namespace *old_ns;
572 gfc_code *c;
573 gfc_symbol *proc;
574 gfc_entry_list *el;
575 char name[GFC_MAX_SYMBOL_LEN + 1];
576 static int master_count = 0;
578 if (ns->proc_name == NULL)
579 return;
581 /* No need to do anything if this procedure doesn't have alternate entry
582 points. */
583 if (!ns->entries)
584 return;
586 /* We may already have resolved alternate entry points. */
587 if (ns->proc_name->attr.entry_master)
588 return;
590 /* If this isn't a procedure something has gone horribly wrong. */
591 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
593 /* Remember the current namespace. */
594 old_ns = gfc_current_ns;
596 gfc_current_ns = ns;
598 /* Add the main entry point to the list of entry points. */
599 el = gfc_get_entry_list ();
600 el->sym = ns->proc_name;
601 el->id = 0;
602 el->next = ns->entries;
603 ns->entries = el;
604 ns->proc_name->attr.entry = 1;
606 /* If it is a module function, it needs to be in the right namespace
607 so that gfc_get_fake_result_decl can gather up the results. The
608 need for this arose in get_proc_name, where these beasts were
609 left in their own namespace, to keep prior references linked to
610 the entry declaration.*/
611 if (ns->proc_name->attr.function
612 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
613 el->sym->ns = ns;
615 /* Do the same for entries where the master is not a module
616 procedure. These are retained in the module namespace because
617 of the module procedure declaration. */
618 for (el = el->next; el; el = el->next)
619 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
620 && el->sym->attr.mod_proc)
621 el->sym->ns = ns;
622 el = ns->entries;
624 /* Add an entry statement for it. */
625 c = gfc_get_code ();
626 c->op = EXEC_ENTRY;
627 c->ext.entry = el;
628 c->next = ns->code;
629 ns->code = c;
631 /* Create a new symbol for the master function. */
632 /* Give the internal function a unique name (within this file).
633 Also include the function name so the user has some hope of figuring
634 out what is going on. */
635 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
636 master_count++, ns->proc_name->name);
637 gfc_get_ha_symbol (name, &proc);
638 gcc_assert (proc != NULL);
640 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
641 if (ns->proc_name->attr.subroutine)
642 gfc_add_subroutine (&proc->attr, proc->name, NULL);
643 else
645 gfc_symbol *sym;
646 gfc_typespec *ts, *fts;
647 gfc_array_spec *as, *fas;
648 gfc_add_function (&proc->attr, proc->name, NULL);
649 proc->result = proc;
650 fas = ns->entries->sym->as;
651 fas = fas ? fas : ns->entries->sym->result->as;
652 fts = &ns->entries->sym->result->ts;
653 if (fts->type == BT_UNKNOWN)
654 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
655 for (el = ns->entries->next; el; el = el->next)
657 ts = &el->sym->result->ts;
658 as = el->sym->as;
659 as = as ? as : el->sym->result->as;
660 if (ts->type == BT_UNKNOWN)
661 ts = gfc_get_default_type (el->sym->result->name, NULL);
663 if (! gfc_compare_types (ts, fts)
664 || (el->sym->result->attr.dimension
665 != ns->entries->sym->result->attr.dimension)
666 || (el->sym->result->attr.pointer
667 != ns->entries->sym->result->attr.pointer))
668 break;
669 else if (as && fas && ns->entries->sym->result != el->sym->result
670 && gfc_compare_array_spec (as, fas) == 0)
671 gfc_error ("Function %s at %L has entries with mismatched "
672 "array specifications", ns->entries->sym->name,
673 &ns->entries->sym->declared_at);
674 /* The characteristics need to match and thus both need to have
675 the same string length, i.e. both len=*, or both len=4.
676 Having both len=<variable> is also possible, but difficult to
677 check at compile time. */
678 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
679 && (((ts->u.cl->length && !fts->u.cl->length)
680 ||(!ts->u.cl->length && fts->u.cl->length))
681 || (ts->u.cl->length
682 && ts->u.cl->length->expr_type
683 != fts->u.cl->length->expr_type)
684 || (ts->u.cl->length
685 && ts->u.cl->length->expr_type == EXPR_CONSTANT
686 && mpz_cmp (ts->u.cl->length->value.integer,
687 fts->u.cl->length->value.integer) != 0)))
688 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
689 "entries returning variables of different "
690 "string lengths", ns->entries->sym->name,
691 &ns->entries->sym->declared_at);
694 if (el == NULL)
696 sym = ns->entries->sym->result;
697 /* All result types the same. */
698 proc->ts = *fts;
699 if (sym->attr.dimension)
700 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
701 if (sym->attr.pointer)
702 gfc_add_pointer (&proc->attr, NULL);
704 else
706 /* Otherwise the result will be passed through a union by
707 reference. */
708 proc->attr.mixed_entry_master = 1;
709 for (el = ns->entries; el; el = el->next)
711 sym = el->sym->result;
712 if (sym->attr.dimension)
714 if (el == ns->entries)
715 gfc_error ("FUNCTION result %s can't be an array in "
716 "FUNCTION %s at %L", sym->name,
717 ns->entries->sym->name, &sym->declared_at);
718 else
719 gfc_error ("ENTRY result %s can't be an array in "
720 "FUNCTION %s at %L", sym->name,
721 ns->entries->sym->name, &sym->declared_at);
723 else if (sym->attr.pointer)
725 if (el == ns->entries)
726 gfc_error ("FUNCTION result %s can't be a POINTER in "
727 "FUNCTION %s at %L", sym->name,
728 ns->entries->sym->name, &sym->declared_at);
729 else
730 gfc_error ("ENTRY result %s can't be a POINTER in "
731 "FUNCTION %s at %L", sym->name,
732 ns->entries->sym->name, &sym->declared_at);
734 else
736 ts = &sym->ts;
737 if (ts->type == BT_UNKNOWN)
738 ts = gfc_get_default_type (sym->name, NULL);
739 switch (ts->type)
741 case BT_INTEGER:
742 if (ts->kind == gfc_default_integer_kind)
743 sym = NULL;
744 break;
745 case BT_REAL:
746 if (ts->kind == gfc_default_real_kind
747 || ts->kind == gfc_default_double_kind)
748 sym = NULL;
749 break;
750 case BT_COMPLEX:
751 if (ts->kind == gfc_default_complex_kind)
752 sym = NULL;
753 break;
754 case BT_LOGICAL:
755 if (ts->kind == gfc_default_logical_kind)
756 sym = NULL;
757 break;
758 case BT_UNKNOWN:
759 /* We will issue error elsewhere. */
760 sym = NULL;
761 break;
762 default:
763 break;
765 if (sym)
767 if (el == ns->entries)
768 gfc_error ("FUNCTION result %s can't be of type %s "
769 "in FUNCTION %s at %L", sym->name,
770 gfc_typename (ts), ns->entries->sym->name,
771 &sym->declared_at);
772 else
773 gfc_error ("ENTRY result %s can't be of type %s "
774 "in FUNCTION %s at %L", sym->name,
775 gfc_typename (ts), ns->entries->sym->name,
776 &sym->declared_at);
782 proc->attr.access = ACCESS_PRIVATE;
783 proc->attr.entry_master = 1;
785 /* Merge all the entry point arguments. */
786 for (el = ns->entries; el; el = el->next)
787 merge_argument_lists (proc, el->sym->formal);
789 /* Check the master formal arguments for any that are not
790 present in all entry points. */
791 for (el = ns->entries; el; el = el->next)
792 check_argument_lists (proc, el->sym->formal);
794 /* Use the master function for the function body. */
795 ns->proc_name = proc;
797 /* Finalize the new symbols. */
798 gfc_commit_symbols ();
800 /* Restore the original namespace. */
801 gfc_current_ns = old_ns;
805 /* Resolve common variables. */
806 static void
807 resolve_common_vars (gfc_symbol *sym, bool named_common)
809 gfc_symbol *csym = sym;
811 for (; csym; csym = csym->common_next)
813 if (csym->value || csym->attr.data)
815 if (!csym->ns->is_block_data)
816 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
817 "but only in BLOCK DATA initialization is "
818 "allowed", csym->name, &csym->declared_at);
819 else if (!named_common)
820 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
821 "in a blank COMMON but initialization is only "
822 "allowed in named common blocks", csym->name,
823 &csym->declared_at);
826 if (csym->ts.type != BT_DERIVED)
827 continue;
829 if (!(csym->ts.u.derived->attr.sequence
830 || csym->ts.u.derived->attr.is_bind_c))
831 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
832 "has neither the SEQUENCE nor the BIND(C) "
833 "attribute", csym->name, &csym->declared_at);
834 if (csym->ts.u.derived->attr.alloc_comp)
835 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
836 "has an ultimate component that is "
837 "allocatable", csym->name, &csym->declared_at);
838 if (gfc_has_default_initializer (csym->ts.u.derived))
839 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
840 "may not have default initializer", csym->name,
841 &csym->declared_at);
843 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
844 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
848 /* Resolve common blocks. */
849 static void
850 resolve_common_blocks (gfc_symtree *common_root)
852 gfc_symbol *sym;
854 if (common_root == NULL)
855 return;
857 if (common_root->left)
858 resolve_common_blocks (common_root->left);
859 if (common_root->right)
860 resolve_common_blocks (common_root->right);
862 resolve_common_vars (common_root->n.common->head, true);
864 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
865 if (sym == NULL)
866 return;
868 if (sym->attr.flavor == FL_PARAMETER)
869 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
870 sym->name, &common_root->n.common->where, &sym->declared_at);
872 if (sym->attr.intrinsic)
873 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
874 sym->name, &common_root->n.common->where);
875 else if (sym->attr.result
876 || gfc_is_function_return_value (sym, gfc_current_ns))
877 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
878 "that is also a function result", sym->name,
879 &common_root->n.common->where);
880 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
881 && sym->attr.proc != PROC_ST_FUNCTION)
882 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
883 "that is also a global procedure", sym->name,
884 &common_root->n.common->where);
888 /* Resolve contained function types. Because contained functions can call one
889 another, they have to be worked out before any of the contained procedures
890 can be resolved.
892 The good news is that if a function doesn't already have a type, the only
893 way it can get one is through an IMPLICIT type or a RESULT variable, because
894 by definition contained functions are contained namespace they're contained
895 in, not in a sibling or parent namespace. */
897 static void
898 resolve_contained_functions (gfc_namespace *ns)
900 gfc_namespace *child;
901 gfc_entry_list *el;
903 resolve_formal_arglists (ns);
905 for (child = ns->contained; child; child = child->sibling)
907 /* Resolve alternate entry points first. */
908 resolve_entries (child);
910 /* Then check function return types. */
911 resolve_contained_fntype (child->proc_name, child);
912 for (el = child->entries; el; el = el->next)
913 resolve_contained_fntype (el->sym, child);
918 /* Resolve all of the elements of a structure constructor and make sure that
919 the types are correct. The 'init' flag indicates that the given
920 constructor is an initializer. */
922 static gfc_try
923 resolve_structure_cons (gfc_expr *expr, int init)
925 gfc_constructor *cons;
926 gfc_component *comp;
927 gfc_try t;
928 symbol_attribute a;
930 t = SUCCESS;
932 if (expr->ts.type == BT_DERIVED)
933 resolve_symbol (expr->ts.u.derived);
935 cons = gfc_constructor_first (expr->value.constructor);
936 /* A constructor may have references if it is the result of substituting a
937 parameter variable. In this case we just pull out the component we
938 want. */
939 if (expr->ref)
940 comp = expr->ref->u.c.sym->components;
941 else
942 comp = expr->ts.u.derived->components;
944 /* See if the user is trying to invoke a structure constructor for one of
945 the iso_c_binding derived types. */
946 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
947 && expr->ts.u.derived->ts.is_iso_c && cons
948 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
950 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
951 expr->ts.u.derived->name, &(expr->where));
952 return FAILURE;
955 /* Return if structure constructor is c_null_(fun)prt. */
956 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
957 && expr->ts.u.derived->ts.is_iso_c && cons
958 && cons->expr && cons->expr->expr_type == EXPR_NULL)
959 return SUCCESS;
961 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
963 int rank;
965 if (!cons->expr)
966 continue;
968 if (gfc_resolve_expr (cons->expr) == FAILURE)
970 t = FAILURE;
971 continue;
974 rank = comp->as ? comp->as->rank : 0;
975 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
976 && (comp->attr.allocatable || cons->expr->rank))
978 gfc_error ("The rank of the element in the derived type "
979 "constructor at %L does not match that of the "
980 "component (%d/%d)", &cons->expr->where,
981 cons->expr->rank, rank);
982 t = FAILURE;
985 /* If we don't have the right type, try to convert it. */
987 if (!comp->attr.proc_pointer &&
988 !gfc_compare_types (&cons->expr->ts, &comp->ts))
990 t = FAILURE;
991 if (strcmp (comp->name, "$extends") == 0)
993 /* Can afford to be brutal with the $extends initializer.
994 The derived type can get lost because it is PRIVATE
995 but it is not usage constrained by the standard. */
996 cons->expr->ts = comp->ts;
997 t = SUCCESS;
999 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1000 gfc_error ("The element in the derived type constructor at %L, "
1001 "for pointer component '%s', is %s but should be %s",
1002 &cons->expr->where, comp->name,
1003 gfc_basic_typename (cons->expr->ts.type),
1004 gfc_basic_typename (comp->ts.type));
1005 else
1006 t = gfc_convert_type (cons->expr, &comp->ts, 1);
1009 /* For strings, the length of the constructor should be the same as
1010 the one of the structure, ensure this if the lengths are known at
1011 compile time and when we are dealing with PARAMETER or structure
1012 constructors. */
1013 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1014 && comp->ts.u.cl->length
1015 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1016 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1017 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1018 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1019 comp->ts.u.cl->length->value.integer) != 0)
1021 if (cons->expr->expr_type == EXPR_VARIABLE
1022 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1024 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1025 to make use of the gfc_resolve_character_array_constructor
1026 machinery. The expression is later simplified away to
1027 an array of string literals. */
1028 gfc_expr *para = cons->expr;
1029 cons->expr = gfc_get_expr ();
1030 cons->expr->ts = para->ts;
1031 cons->expr->where = para->where;
1032 cons->expr->expr_type = EXPR_ARRAY;
1033 cons->expr->rank = para->rank;
1034 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1035 gfc_constructor_append_expr (&cons->expr->value.constructor,
1036 para, &cons->expr->where);
1038 if (cons->expr->expr_type == EXPR_ARRAY)
1040 gfc_constructor *p;
1041 p = gfc_constructor_first (cons->expr->value.constructor);
1042 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1044 gfc_charlen *cl, *cl2;
1046 cl2 = NULL;
1047 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1049 if (cl == cons->expr->ts.u.cl)
1050 break;
1051 cl2 = cl;
1054 gcc_assert (cl);
1056 if (cl2)
1057 cl2->next = cl->next;
1059 gfc_free_expr (cl->length);
1060 gfc_free (cl);
1063 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1064 cons->expr->ts.u.cl->length_from_typespec = true;
1065 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1066 gfc_resolve_character_array_constructor (cons->expr);
1070 if (cons->expr->expr_type == EXPR_NULL
1071 && !(comp->attr.pointer || comp->attr.allocatable
1072 || comp->attr.proc_pointer
1073 || (comp->ts.type == BT_CLASS
1074 && (CLASS_DATA (comp)->attr.class_pointer
1075 || CLASS_DATA (comp)->attr.allocatable))))
1077 t = FAILURE;
1078 gfc_error ("The NULL in the derived type constructor at %L is "
1079 "being applied to component '%s', which is neither "
1080 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1081 comp->name);
1084 if (!comp->attr.pointer || comp->attr.proc_pointer
1085 || cons->expr->expr_type == EXPR_NULL)
1086 continue;
1088 a = gfc_expr_attr (cons->expr);
1090 if (!a.pointer && !a.target)
1092 t = FAILURE;
1093 gfc_error ("The element in the derived type constructor at %L, "
1094 "for pointer component '%s' should be a POINTER or "
1095 "a TARGET", &cons->expr->where, comp->name);
1098 if (init)
1100 /* F08:C461. Additional checks for pointer initialization. */
1101 if (a.allocatable)
1103 t = FAILURE;
1104 gfc_error ("Pointer initialization target at %L "
1105 "must not be ALLOCATABLE ", &cons->expr->where);
1107 if (!a.save)
1109 t = FAILURE;
1110 gfc_error ("Pointer initialization target at %L "
1111 "must have the SAVE attribute", &cons->expr->where);
1115 /* F2003, C1272 (3). */
1116 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
1117 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1118 || gfc_is_coindexed (cons->expr)))
1120 t = FAILURE;
1121 gfc_error ("Invalid expression in the derived type constructor for "
1122 "pointer component '%s' at %L in PURE procedure",
1123 comp->name, &cons->expr->where);
1128 return t;
1132 /****************** Expression name resolution ******************/
1134 /* Returns 0 if a symbol was not declared with a type or
1135 attribute declaration statement, nonzero otherwise. */
1137 static int
1138 was_declared (gfc_symbol *sym)
1140 symbol_attribute a;
1142 a = sym->attr;
1144 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1145 return 1;
1147 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1148 || a.optional || a.pointer || a.save || a.target || a.volatile_
1149 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1150 || a.asynchronous || a.codimension)
1151 return 1;
1153 return 0;
1157 /* Determine if a symbol is generic or not. */
1159 static int
1160 generic_sym (gfc_symbol *sym)
1162 gfc_symbol *s;
1164 if (sym->attr.generic ||
1165 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1166 return 1;
1168 if (was_declared (sym) || sym->ns->parent == NULL)
1169 return 0;
1171 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1173 if (s != NULL)
1175 if (s == sym)
1176 return 0;
1177 else
1178 return generic_sym (s);
1181 return 0;
1185 /* Determine if a symbol is specific or not. */
1187 static int
1188 specific_sym (gfc_symbol *sym)
1190 gfc_symbol *s;
1192 if (sym->attr.if_source == IFSRC_IFBODY
1193 || sym->attr.proc == PROC_MODULE
1194 || sym->attr.proc == PROC_INTERNAL
1195 || sym->attr.proc == PROC_ST_FUNCTION
1196 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1197 || sym->attr.external)
1198 return 1;
1200 if (was_declared (sym) || sym->ns->parent == NULL)
1201 return 0;
1203 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1205 return (s == NULL) ? 0 : specific_sym (s);
1209 /* Figure out if the procedure is specific, generic or unknown. */
1211 typedef enum
1212 { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1213 proc_type;
1215 static proc_type
1216 procedure_kind (gfc_symbol *sym)
1218 if (generic_sym (sym))
1219 return PTYPE_GENERIC;
1221 if (specific_sym (sym))
1222 return PTYPE_SPECIFIC;
1224 return PTYPE_UNKNOWN;
1227 /* Check references to assumed size arrays. The flag need_full_assumed_size
1228 is nonzero when matching actual arguments. */
1230 static int need_full_assumed_size = 0;
1232 static bool
1233 check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1235 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1236 return false;
1238 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1239 What should it be? */
1240 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1241 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1242 && (e->ref->u.ar.type == AR_FULL))
1244 gfc_error ("The upper bound in the last dimension must "
1245 "appear in the reference to the assumed size "
1246 "array '%s' at %L", sym->name, &e->where);
1247 return true;
1249 return false;
1253 /* Look for bad assumed size array references in argument expressions
1254 of elemental and array valued intrinsic procedures. Since this is
1255 called from procedure resolution functions, it only recurses at
1256 operators. */
1258 static bool
1259 resolve_assumed_size_actual (gfc_expr *e)
1261 if (e == NULL)
1262 return false;
1264 switch (e->expr_type)
1266 case EXPR_VARIABLE:
1267 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1268 return true;
1269 break;
1271 case EXPR_OP:
1272 if (resolve_assumed_size_actual (e->value.op.op1)
1273 || resolve_assumed_size_actual (e->value.op.op2))
1274 return true;
1275 break;
1277 default:
1278 break;
1280 return false;
1284 /* Check a generic procedure, passed as an actual argument, to see if
1285 there is a matching specific name. If none, it is an error, and if
1286 more than one, the reference is ambiguous. */
1287 static int
1288 count_specific_procs (gfc_expr *e)
1290 int n;
1291 gfc_interface *p;
1292 gfc_symbol *sym;
1294 n = 0;
1295 sym = e->symtree->n.sym;
1297 for (p = sym->generic; p; p = p->next)
1298 if (strcmp (sym->name, p->sym->name) == 0)
1300 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1301 sym->name);
1302 n++;
1305 if (n > 1)
1306 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1307 &e->where);
1309 if (n == 0)
1310 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1311 "argument at %L", sym->name, &e->where);
1313 return n;
1317 /* See if a call to sym could possibly be a not allowed RECURSION because of
1318 a missing RECURIVE declaration. This means that either sym is the current
1319 context itself, or sym is the parent of a contained procedure calling its
1320 non-RECURSIVE containing procedure.
1321 This also works if sym is an ENTRY. */
1323 static bool
1324 is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1326 gfc_symbol* proc_sym;
1327 gfc_symbol* context_proc;
1328 gfc_namespace* real_context;
1330 if (sym->attr.flavor == FL_PROGRAM)
1331 return false;
1333 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1335 /* If we've got an ENTRY, find real procedure. */
1336 if (sym->attr.entry && sym->ns->entries)
1337 proc_sym = sym->ns->entries->sym;
1338 else
1339 proc_sym = sym;
1341 /* If sym is RECURSIVE, all is well of course. */
1342 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1343 return false;
1345 /* Find the context procedure's "real" symbol if it has entries.
1346 We look for a procedure symbol, so recurse on the parents if we don't
1347 find one (like in case of a BLOCK construct). */
1348 for (real_context = context; ; real_context = real_context->parent)
1350 /* We should find something, eventually! */
1351 gcc_assert (real_context);
1353 context_proc = (real_context->entries ? real_context->entries->sym
1354 : real_context->proc_name);
1356 /* In some special cases, there may not be a proc_name, like for this
1357 invalid code:
1358 real(bad_kind()) function foo () ...
1359 when checking the call to bad_kind ().
1360 In these cases, we simply return here and assume that the
1361 call is ok. */
1362 if (!context_proc)
1363 return false;
1365 if (context_proc->attr.flavor != FL_LABEL)
1366 break;
1369 /* A call from sym's body to itself is recursion, of course. */
1370 if (context_proc == proc_sym)
1371 return true;
1373 /* The same is true if context is a contained procedure and sym the
1374 containing one. */
1375 if (context_proc->attr.contained)
1377 gfc_symbol* parent_proc;
1379 gcc_assert (context->parent);
1380 parent_proc = (context->parent->entries ? context->parent->entries->sym
1381 : context->parent->proc_name);
1383 if (parent_proc == proc_sym)
1384 return true;
1387 return false;
1391 /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1392 its typespec and formal argument list. */
1394 static gfc_try
1395 resolve_intrinsic (gfc_symbol *sym, locus *loc)
1397 gfc_intrinsic_sym* isym = NULL;
1398 const char* symstd;
1400 if (sym->formal)
1401 return SUCCESS;
1403 /* We already know this one is an intrinsic, so we don't call
1404 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1405 gfc_find_subroutine directly to check whether it is a function or
1406 subroutine. */
1408 if (sym->intmod_sym_id)
1409 isym = gfc_intrinsic_function_by_id ((gfc_isym_id) sym->intmod_sym_id);
1410 else
1411 isym = gfc_find_function (sym->name);
1413 if (isym)
1415 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1416 && !sym->attr.implicit_type)
1417 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1418 " ignored", sym->name, &sym->declared_at);
1420 if (!sym->attr.function &&
1421 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1422 return FAILURE;
1424 sym->ts = isym->ts;
1426 else if ((isym = gfc_find_subroutine (sym->name)))
1428 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1430 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1431 " specifier", sym->name, &sym->declared_at);
1432 return FAILURE;
1435 if (!sym->attr.subroutine &&
1436 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1437 return FAILURE;
1439 else
1441 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1442 &sym->declared_at);
1443 return FAILURE;
1446 gfc_copy_formal_args_intr (sym, isym);
1448 /* Check it is actually available in the standard settings. */
1449 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1450 == FAILURE)
1452 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1453 " available in the current standard settings but %s. Use"
1454 " an appropriate -std=* option or enable -fall-intrinsics"
1455 " in order to use it.",
1456 sym->name, &sym->declared_at, symstd);
1457 return FAILURE;
1460 return SUCCESS;
1464 /* Resolve a procedure expression, like passing it to a called procedure or as
1465 RHS for a procedure pointer assignment. */
1467 static gfc_try
1468 resolve_procedure_expression (gfc_expr* expr)
1470 gfc_symbol* sym;
1472 if (expr->expr_type != EXPR_VARIABLE)
1473 return SUCCESS;
1474 gcc_assert (expr->symtree);
1476 sym = expr->symtree->n.sym;
1478 if (sym->attr.intrinsic)
1479 resolve_intrinsic (sym, &expr->where);
1481 if (sym->attr.flavor != FL_PROCEDURE
1482 || (sym->attr.function && sym->result == sym))
1483 return SUCCESS;
1485 /* A non-RECURSIVE procedure that is used as procedure expression within its
1486 own body is in danger of being called recursively. */
1487 if (is_illegal_recursion (sym, gfc_current_ns))
1488 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1489 " itself recursively. Declare it RECURSIVE or use"
1490 " -frecursive", sym->name, &expr->where);
1492 return SUCCESS;
1496 /* Resolve an actual argument list. Most of the time, this is just
1497 resolving the expressions in the list.
1498 The exception is that we sometimes have to decide whether arguments
1499 that look like procedure arguments are really simple variable
1500 references. */
1502 static gfc_try
1503 resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1504 bool no_formal_args)
1506 gfc_symbol *sym;
1507 gfc_symtree *parent_st;
1508 gfc_expr *e;
1509 int save_need_full_assumed_size;
1510 gfc_component *comp;
1512 for (; arg; arg = arg->next)
1514 e = arg->expr;
1515 if (e == NULL)
1517 /* Check the label is a valid branching target. */
1518 if (arg->label)
1520 if (arg->label->defined == ST_LABEL_UNKNOWN)
1522 gfc_error ("Label %d referenced at %L is never defined",
1523 arg->label->value, &arg->label->where);
1524 return FAILURE;
1527 continue;
1530 if (gfc_is_proc_ptr_comp (e, &comp))
1532 e->ts = comp->ts;
1533 if (e->expr_type == EXPR_PPC)
1535 if (comp->as != NULL)
1536 e->rank = comp->as->rank;
1537 e->expr_type = EXPR_FUNCTION;
1539 if (gfc_resolve_expr (e) == FAILURE)
1540 return FAILURE;
1541 goto argument_list;
1544 if (e->expr_type == EXPR_VARIABLE
1545 && e->symtree->n.sym->attr.generic
1546 && no_formal_args
1547 && count_specific_procs (e) != 1)
1548 return FAILURE;
1550 if (e->ts.type != BT_PROCEDURE)
1552 save_need_full_assumed_size = need_full_assumed_size;
1553 if (e->expr_type != EXPR_VARIABLE)
1554 need_full_assumed_size = 0;
1555 if (gfc_resolve_expr (e) != SUCCESS)
1556 return FAILURE;
1557 need_full_assumed_size = save_need_full_assumed_size;
1558 goto argument_list;
1561 /* See if the expression node should really be a variable reference. */
1563 sym = e->symtree->n.sym;
1565 if (sym->attr.flavor == FL_PROCEDURE
1566 || sym->attr.intrinsic
1567 || sym->attr.external)
1569 int actual_ok;
1571 /* If a procedure is not already determined to be something else
1572 check if it is intrinsic. */
1573 if (!sym->attr.intrinsic
1574 && !(sym->attr.external || sym->attr.use_assoc
1575 || sym->attr.if_source == IFSRC_IFBODY)
1576 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
1577 sym->attr.intrinsic = 1;
1579 if (sym->attr.proc == PROC_ST_FUNCTION)
1581 gfc_error ("Statement function '%s' at %L is not allowed as an "
1582 "actual argument", sym->name, &e->where);
1585 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1586 sym->attr.subroutine);
1587 if (sym->attr.intrinsic && actual_ok == 0)
1589 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1590 "actual argument", sym->name, &e->where);
1593 if (sym->attr.contained && !sym->attr.use_assoc
1594 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1596 if (gfc_notify_std (GFC_STD_F2008,
1597 "Fortran 2008: Internal procedure '%s' is"
1598 " used as actual argument at %L",
1599 sym->name, &e->where) == FAILURE)
1600 return FAILURE;
1603 if (sym->attr.elemental && !sym->attr.intrinsic)
1605 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
1606 "allowed as an actual argument at %L", sym->name,
1607 &e->where);
1610 /* Check if a generic interface has a specific procedure
1611 with the same name before emitting an error. */
1612 if (sym->attr.generic && count_specific_procs (e) != 1)
1613 return FAILURE;
1615 /* Just in case a specific was found for the expression. */
1616 sym = e->symtree->n.sym;
1618 /* If the symbol is the function that names the current (or
1619 parent) scope, then we really have a variable reference. */
1621 if (gfc_is_function_return_value (sym, sym->ns))
1622 goto got_variable;
1624 /* If all else fails, see if we have a specific intrinsic. */
1625 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
1627 gfc_intrinsic_sym *isym;
1629 isym = gfc_find_function (sym->name);
1630 if (isym == NULL || !isym->specific)
1632 gfc_error ("Unable to find a specific INTRINSIC procedure "
1633 "for the reference '%s' at %L", sym->name,
1634 &e->where);
1635 return FAILURE;
1637 sym->ts = isym->ts;
1638 sym->attr.intrinsic = 1;
1639 sym->attr.function = 1;
1642 if (gfc_resolve_expr (e) == FAILURE)
1643 return FAILURE;
1644 goto argument_list;
1647 /* See if the name is a module procedure in a parent unit. */
1649 if (was_declared (sym) || sym->ns->parent == NULL)
1650 goto got_variable;
1652 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1654 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1655 return FAILURE;
1658 if (parent_st == NULL)
1659 goto got_variable;
1661 sym = parent_st->n.sym;
1662 e->symtree = parent_st; /* Point to the right thing. */
1664 if (sym->attr.flavor == FL_PROCEDURE
1665 || sym->attr.intrinsic
1666 || sym->attr.external)
1668 if (gfc_resolve_expr (e) == FAILURE)
1669 return FAILURE;
1670 goto argument_list;
1673 got_variable:
1674 e->expr_type = EXPR_VARIABLE;
1675 e->ts = sym->ts;
1676 if (sym->as != NULL)
1678 e->rank = sym->as->rank;
1679 e->ref = gfc_get_ref ();
1680 e->ref->type = REF_ARRAY;
1681 e->ref->u.ar.type = AR_FULL;
1682 e->ref->u.ar.as = sym->as;
1685 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1686 primary.c (match_actual_arg). If above code determines that it
1687 is a variable instead, it needs to be resolved as it was not
1688 done at the beginning of this function. */
1689 save_need_full_assumed_size = need_full_assumed_size;
1690 if (e->expr_type != EXPR_VARIABLE)
1691 need_full_assumed_size = 0;
1692 if (gfc_resolve_expr (e) != SUCCESS)
1693 return FAILURE;
1694 need_full_assumed_size = save_need_full_assumed_size;
1696 argument_list:
1697 /* Check argument list functions %VAL, %LOC and %REF. There is
1698 nothing to do for %REF. */
1699 if (arg->name && arg->name[0] == '%')
1701 if (strncmp ("%VAL", arg->name, 4) == 0)
1703 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1705 gfc_error ("By-value argument at %L is not of numeric "
1706 "type", &e->where);
1707 return FAILURE;
1710 if (e->rank)
1712 gfc_error ("By-value argument at %L cannot be an array or "
1713 "an array section", &e->where);
1714 return FAILURE;
1717 /* Intrinsics are still PROC_UNKNOWN here. However,
1718 since same file external procedures are not resolvable
1719 in gfortran, it is a good deal easier to leave them to
1720 intrinsic.c. */
1721 if (ptype != PROC_UNKNOWN
1722 && ptype != PROC_DUMMY
1723 && ptype != PROC_EXTERNAL
1724 && ptype != PROC_MODULE)
1726 gfc_error ("By-value argument at %L is not allowed "
1727 "in this context", &e->where);
1728 return FAILURE;
1732 /* Statement functions have already been excluded above. */
1733 else if (strncmp ("%LOC", arg->name, 4) == 0
1734 && e->ts.type == BT_PROCEDURE)
1736 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1738 gfc_error ("Passing internal procedure at %L by location "
1739 "not allowed", &e->where);
1740 return FAILURE;
1745 /* Fortran 2008, C1237. */
1746 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
1747 && gfc_has_ultimate_pointer (e))
1749 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
1750 "component", &e->where);
1751 return FAILURE;
1755 return SUCCESS;
1759 /* Do the checks of the actual argument list that are specific to elemental
1760 procedures. If called with c == NULL, we have a function, otherwise if
1761 expr == NULL, we have a subroutine. */
1763 static gfc_try
1764 resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1766 gfc_actual_arglist *arg0;
1767 gfc_actual_arglist *arg;
1768 gfc_symbol *esym = NULL;
1769 gfc_intrinsic_sym *isym = NULL;
1770 gfc_expr *e = NULL;
1771 gfc_intrinsic_arg *iformal = NULL;
1772 gfc_formal_arglist *eformal = NULL;
1773 bool formal_optional = false;
1774 bool set_by_optional = false;
1775 int i;
1776 int rank = 0;
1778 /* Is this an elemental procedure? */
1779 if (expr && expr->value.function.actual != NULL)
1781 if (expr->value.function.esym != NULL
1782 && expr->value.function.esym->attr.elemental)
1784 arg0 = expr->value.function.actual;
1785 esym = expr->value.function.esym;
1787 else if (expr->value.function.isym != NULL
1788 && expr->value.function.isym->elemental)
1790 arg0 = expr->value.function.actual;
1791 isym = expr->value.function.isym;
1793 else
1794 return SUCCESS;
1796 else if (c && c->ext.actual != NULL)
1798 arg0 = c->ext.actual;
1800 if (c->resolved_sym)
1801 esym = c->resolved_sym;
1802 else
1803 esym = c->symtree->n.sym;
1804 gcc_assert (esym);
1806 if (!esym->attr.elemental)
1807 return SUCCESS;
1809 else
1810 return SUCCESS;
1812 /* The rank of an elemental is the rank of its array argument(s). */
1813 for (arg = arg0; arg; arg = arg->next)
1815 if (arg->expr != NULL && arg->expr->rank > 0)
1817 rank = arg->expr->rank;
1818 if (arg->expr->expr_type == EXPR_VARIABLE
1819 && arg->expr->symtree->n.sym->attr.optional)
1820 set_by_optional = true;
1822 /* Function specific; set the result rank and shape. */
1823 if (expr)
1825 expr->rank = rank;
1826 if (!expr->shape && arg->expr->shape)
1828 expr->shape = gfc_get_shape (rank);
1829 for (i = 0; i < rank; i++)
1830 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1833 break;
1837 /* If it is an array, it shall not be supplied as an actual argument
1838 to an elemental procedure unless an array of the same rank is supplied
1839 as an actual argument corresponding to a nonoptional dummy argument of
1840 that elemental procedure(12.4.1.5). */
1841 formal_optional = false;
1842 if (isym)
1843 iformal = isym->formal;
1844 else
1845 eformal = esym->formal;
1847 for (arg = arg0; arg; arg = arg->next)
1849 if (eformal)
1851 if (eformal->sym && eformal->sym->attr.optional)
1852 formal_optional = true;
1853 eformal = eformal->next;
1855 else if (isym && iformal)
1857 if (iformal->optional)
1858 formal_optional = true;
1859 iformal = iformal->next;
1861 else if (isym)
1862 formal_optional = true;
1864 if (pedantic && arg->expr != NULL
1865 && arg->expr->expr_type == EXPR_VARIABLE
1866 && arg->expr->symtree->n.sym->attr.optional
1867 && formal_optional
1868 && arg->expr->rank
1869 && (set_by_optional || arg->expr->rank != rank)
1870 && !(isym && isym->id == GFC_ISYM_CONVERSION))
1872 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1873 "MISSING, it cannot be the actual argument of an "
1874 "ELEMENTAL procedure unless there is a non-optional "
1875 "argument with the same rank (12.4.1.5)",
1876 arg->expr->symtree->n.sym->name, &arg->expr->where);
1877 return FAILURE;
1881 for (arg = arg0; arg; arg = arg->next)
1883 if (arg->expr == NULL || arg->expr->rank == 0)
1884 continue;
1886 /* Being elemental, the last upper bound of an assumed size array
1887 argument must be present. */
1888 if (resolve_assumed_size_actual (arg->expr))
1889 return FAILURE;
1891 /* Elemental procedure's array actual arguments must conform. */
1892 if (e != NULL)
1894 if (gfc_check_conformance (arg->expr, e,
1895 "elemental procedure") == FAILURE)
1896 return FAILURE;
1898 else
1899 e = arg->expr;
1902 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1903 is an array, the intent inout/out variable needs to be also an array. */
1904 if (rank > 0 && esym && expr == NULL)
1905 for (eformal = esym->formal, arg = arg0; arg && eformal;
1906 arg = arg->next, eformal = eformal->next)
1907 if ((eformal->sym->attr.intent == INTENT_OUT
1908 || eformal->sym->attr.intent == INTENT_INOUT)
1909 && arg->expr && arg->expr->rank == 0)
1911 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1912 "ELEMENTAL subroutine '%s' is a scalar, but another "
1913 "actual argument is an array", &arg->expr->where,
1914 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1915 : "INOUT", eformal->sym->name, esym->name);
1916 return FAILURE;
1918 return SUCCESS;
1922 /* This function does the checking of references to global procedures
1923 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1924 77 and 95 standards. It checks for a gsymbol for the name, making
1925 one if it does not already exist. If it already exists, then the
1926 reference being resolved must correspond to the type of gsymbol.
1927 Otherwise, the new symbol is equipped with the attributes of the
1928 reference. The corresponding code that is called in creating
1929 global entities is parse.c.
1931 In addition, for all but -std=legacy, the gsymbols are used to
1932 check the interfaces of external procedures from the same file.
1933 The namespace of the gsymbol is resolved and then, once this is
1934 done the interface is checked. */
1937 static bool
1938 not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1940 if (!gsym_ns->proc_name->attr.recursive)
1941 return true;
1943 if (sym->ns == gsym_ns)
1944 return false;
1946 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1947 return false;
1949 return true;
1952 static bool
1953 not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1955 if (gsym_ns->entries)
1957 gfc_entry_list *entry = gsym_ns->entries;
1959 for (; entry; entry = entry->next)
1961 if (strcmp (sym->name, entry->sym->name) == 0)
1963 if (strcmp (gsym_ns->proc_name->name,
1964 sym->ns->proc_name->name) == 0)
1965 return false;
1967 if (sym->ns->parent
1968 && strcmp (gsym_ns->proc_name->name,
1969 sym->ns->parent->proc_name->name) == 0)
1970 return false;
1974 return true;
1977 static void
1978 resolve_global_procedure (gfc_symbol *sym, locus *where,
1979 gfc_actual_arglist **actual, int sub)
1981 gfc_gsymbol * gsym;
1982 gfc_namespace *ns;
1983 enum gfc_symbol_type type;
1985 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1987 gsym = gfc_get_gsymbol (sym->name);
1989 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
1990 gfc_global_used (gsym, where);
1992 if (gfc_option.flag_whole_file
1993 && (sym->attr.if_source == IFSRC_UNKNOWN
1994 || sym->attr.if_source == IFSRC_IFBODY)
1995 && gsym->type != GSYM_UNKNOWN
1996 && gsym->ns
1997 && gsym->ns->resolved != -1
1998 && gsym->ns->proc_name
1999 && not_in_recursive (sym, gsym->ns)
2000 && not_entry_self_reference (sym, gsym->ns))
2002 gfc_symbol *def_sym;
2004 /* Resolve the gsymbol namespace if needed. */
2005 if (!gsym->ns->resolved)
2007 gfc_dt_list *old_dt_list;
2009 /* Stash away derived types so that the backend_decls do not
2010 get mixed up. */
2011 old_dt_list = gfc_derived_types;
2012 gfc_derived_types = NULL;
2014 gfc_resolve (gsym->ns);
2016 /* Store the new derived types with the global namespace. */
2017 if (gfc_derived_types)
2018 gsym->ns->derived_types = gfc_derived_types;
2020 /* Restore the derived types of this namespace. */
2021 gfc_derived_types = old_dt_list;
2024 /* Make sure that translation for the gsymbol occurs before
2025 the procedure currently being resolved. */
2026 ns = gfc_global_ns_list;
2027 for (; ns && ns != gsym->ns; ns = ns->sibling)
2029 if (ns->sibling == gsym->ns)
2031 ns->sibling = gsym->ns->sibling;
2032 gsym->ns->sibling = gfc_global_ns_list;
2033 gfc_global_ns_list = gsym->ns;
2034 break;
2038 def_sym = gsym->ns->proc_name;
2039 if (def_sym->attr.entry_master)
2041 gfc_entry_list *entry;
2042 for (entry = gsym->ns->entries; entry; entry = entry->next)
2043 if (strcmp (entry->sym->name, sym->name) == 0)
2045 def_sym = entry->sym;
2046 break;
2050 /* Differences in constant character lengths. */
2051 if (sym->attr.function && sym->ts.type == BT_CHARACTER)
2053 long int l1 = 0, l2 = 0;
2054 gfc_charlen *cl1 = sym->ts.u.cl;
2055 gfc_charlen *cl2 = def_sym->ts.u.cl;
2057 if (cl1 != NULL
2058 && cl1->length != NULL
2059 && cl1->length->expr_type == EXPR_CONSTANT)
2060 l1 = mpz_get_si (cl1->length->value.integer);
2062 if (cl2 != NULL
2063 && cl2->length != NULL
2064 && cl2->length->expr_type == EXPR_CONSTANT)
2065 l2 = mpz_get_si (cl2->length->value.integer);
2067 if (l1 && l2 && l1 != l2)
2068 gfc_error ("Character length mismatch in return type of "
2069 "function '%s' at %L (%ld/%ld)", sym->name,
2070 &sym->declared_at, l1, l2);
2073 /* Type mismatch of function return type and expected type. */
2074 if (sym->attr.function
2075 && !gfc_compare_types (&sym->ts, &def_sym->ts))
2076 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2077 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2078 gfc_typename (&def_sym->ts));
2080 if (def_sym->formal && sym->attr.if_source != IFSRC_IFBODY)
2082 gfc_formal_arglist *arg = def_sym->formal;
2083 for ( ; arg; arg = arg->next)
2084 if (!arg->sym)
2085 continue;
2086 /* F2003, 12.3.1.1 (2a); F2008, 12.4.2.2 (2a) */
2087 else if (arg->sym->attr.allocatable
2088 || arg->sym->attr.asynchronous
2089 || arg->sym->attr.optional
2090 || arg->sym->attr.pointer
2091 || arg->sym->attr.target
2092 || arg->sym->attr.value
2093 || arg->sym->attr.volatile_)
2095 gfc_error ("Dummy argument '%s' of procedure '%s' at %L "
2096 "has an attribute that requires an explicit "
2097 "interface for this procedure", arg->sym->name,
2098 sym->name, &sym->declared_at);
2099 break;
2101 /* F2003, 12.3.1.1 (2b); F2008, 12.4.2.2 (2b) */
2102 else if (arg->sym && arg->sym->as
2103 && arg->sym->as->type == AS_ASSUMED_SHAPE)
2105 gfc_error ("Procedure '%s' at %L with assumed-shape dummy "
2106 "argument '%s' must have an explicit interface",
2107 sym->name, &sym->declared_at, arg->sym->name);
2108 break;
2110 /* F2008, 12.4.2.2 (2c) */
2111 else if (arg->sym->attr.codimension)
2113 gfc_error ("Procedure '%s' at %L with coarray dummy argument "
2114 "'%s' must have an explicit interface",
2115 sym->name, &sym->declared_at, arg->sym->name);
2116 break;
2118 /* F2003, 12.3.1.1 (2c); F2008, 12.4.2.2 (2d) */
2119 else if (false) /* TODO: is a parametrized derived type */
2121 gfc_error ("Procedure '%s' at %L with parametrized derived "
2122 "type argument '%s' must have an explicit "
2123 "interface", sym->name, &sym->declared_at,
2124 arg->sym->name);
2125 break;
2127 /* F2003, 12.3.1.1 (2d); F2008, 12.4.2.2 (2e) */
2128 else if (arg->sym->ts.type == BT_CLASS)
2130 gfc_error ("Procedure '%s' at %L with polymorphic dummy "
2131 "argument '%s' must have an explicit interface",
2132 sym->name, &sym->declared_at, arg->sym->name);
2133 break;
2137 if (def_sym->attr.function)
2139 /* F2003, 12.3.1.1 (3a); F2008, 12.4.2.2 (3a) */
2140 if (def_sym->as && def_sym->as->rank
2141 && (!sym->as || sym->as->rank != def_sym->as->rank))
2142 gfc_error ("The reference to function '%s' at %L either needs an "
2143 "explicit INTERFACE or the rank is incorrect", sym->name,
2144 where);
2146 /* F2003, 12.3.1.1 (3b); F2008, 12.4.2.2 (3b) */
2147 if ((def_sym->result->attr.pointer
2148 || def_sym->result->attr.allocatable)
2149 && (sym->attr.if_source != IFSRC_IFBODY
2150 || def_sym->result->attr.pointer
2151 != sym->result->attr.pointer
2152 || def_sym->result->attr.allocatable
2153 != sym->result->attr.allocatable))
2154 gfc_error ("Function '%s' at %L with a POINTER or ALLOCATABLE "
2155 "result must have an explicit interface", sym->name,
2156 where);
2158 /* F2003, 12.3.1.1 (3c); F2008, 12.4.2.2 (3c) */
2159 if (sym->ts.type == BT_CHARACTER && sym->attr.if_source != IFSRC_IFBODY
2160 && def_sym->ts.u.cl->length != NULL)
2162 gfc_charlen *cl = sym->ts.u.cl;
2164 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
2165 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
2167 gfc_error ("Nonconstant character-length function '%s' at %L "
2168 "must have an explicit interface", sym->name,
2169 &sym->declared_at);
2174 /* F2003, 12.3.1.1 (4); F2008, 12.4.2.2 (4) */
2175 if (def_sym->attr.elemental && !sym->attr.elemental)
2177 gfc_error ("ELEMENTAL procedure '%s' at %L must have an explicit "
2178 "interface", sym->name, &sym->declared_at);
2181 /* F2003, 12.3.1.1 (5); F2008, 12.4.2.2 (5) */
2182 if (def_sym->attr.is_bind_c && !sym->attr.is_bind_c)
2184 gfc_error ("Procedure '%s' at %L with BIND(C) attribute must have "
2185 "an explicit interface", sym->name, &sym->declared_at);
2188 if (gfc_option.flag_whole_file == 1
2189 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2190 && !(gfc_option.warn_std & GFC_STD_GNU)))
2191 gfc_errors_to_warnings (1);
2193 if (sym->attr.if_source != IFSRC_IFBODY)
2194 gfc_procedure_use (def_sym, actual, where);
2196 gfc_errors_to_warnings (0);
2199 if (gsym->type == GSYM_UNKNOWN)
2201 gsym->type = type;
2202 gsym->where = *where;
2205 gsym->used = 1;
2209 /************* Function resolution *************/
2211 /* Resolve a function call known to be generic.
2212 Section 14.1.2.4.1. */
2214 static match
2215 resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2217 gfc_symbol *s;
2219 if (sym->attr.generic)
2221 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2222 if (s != NULL)
2224 expr->value.function.name = s->name;
2225 expr->value.function.esym = s;
2227 if (s->ts.type != BT_UNKNOWN)
2228 expr->ts = s->ts;
2229 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2230 expr->ts = s->result->ts;
2232 if (s->as != NULL)
2233 expr->rank = s->as->rank;
2234 else if (s->result != NULL && s->result->as != NULL)
2235 expr->rank = s->result->as->rank;
2237 gfc_set_sym_referenced (expr->value.function.esym);
2239 return MATCH_YES;
2242 /* TODO: Need to search for elemental references in generic
2243 interface. */
2246 if (sym->attr.intrinsic)
2247 return gfc_intrinsic_func_interface (expr, 0);
2249 return MATCH_NO;
2253 static gfc_try
2254 resolve_generic_f (gfc_expr *expr)
2256 gfc_symbol *sym;
2257 match m;
2259 sym = expr->symtree->n.sym;
2261 for (;;)
2263 m = resolve_generic_f0 (expr, sym);
2264 if (m == MATCH_YES)
2265 return SUCCESS;
2266 else if (m == MATCH_ERROR)
2267 return FAILURE;
2269 generic:
2270 if (sym->ns->parent == NULL)
2271 break;
2272 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2274 if (sym == NULL)
2275 break;
2276 if (!generic_sym (sym))
2277 goto generic;
2280 /* Last ditch attempt. See if the reference is to an intrinsic
2281 that possesses a matching interface. 14.1.2.4 */
2282 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
2284 gfc_error ("There is no specific function for the generic '%s' at %L",
2285 expr->symtree->n.sym->name, &expr->where);
2286 return FAILURE;
2289 m = gfc_intrinsic_func_interface (expr, 0);
2290 if (m == MATCH_YES)
2291 return SUCCESS;
2292 if (m == MATCH_NO)
2293 gfc_error ("Generic function '%s' at %L is not consistent with a "
2294 "specific intrinsic interface", expr->symtree->n.sym->name,
2295 &expr->where);
2297 return FAILURE;
2301 /* Resolve a function call known to be specific. */
2303 static match
2304 resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2306 match m;
2308 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2310 if (sym->attr.dummy)
2312 sym->attr.proc = PROC_DUMMY;
2313 goto found;
2316 sym->attr.proc = PROC_EXTERNAL;
2317 goto found;
2320 if (sym->attr.proc == PROC_MODULE
2321 || sym->attr.proc == PROC_ST_FUNCTION
2322 || sym->attr.proc == PROC_INTERNAL)
2323 goto found;
2325 if (sym->attr.intrinsic)
2327 m = gfc_intrinsic_func_interface (expr, 1);
2328 if (m == MATCH_YES)
2329 return MATCH_YES;
2330 if (m == MATCH_NO)
2331 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2332 "with an intrinsic", sym->name, &expr->where);
2334 return MATCH_ERROR;
2337 return MATCH_NO;
2339 found:
2340 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2342 if (sym->result)
2343 expr->ts = sym->result->ts;
2344 else
2345 expr->ts = sym->ts;
2346 expr->value.function.name = sym->name;
2347 expr->value.function.esym = sym;
2348 if (sym->as != NULL)
2349 expr->rank = sym->as->rank;
2351 return MATCH_YES;
2355 static gfc_try
2356 resolve_specific_f (gfc_expr *expr)
2358 gfc_symbol *sym;
2359 match m;
2361 sym = expr->symtree->n.sym;
2363 for (;;)
2365 m = resolve_specific_f0 (sym, expr);
2366 if (m == MATCH_YES)
2367 return SUCCESS;
2368 if (m == MATCH_ERROR)
2369 return FAILURE;
2371 if (sym->ns->parent == NULL)
2372 break;
2374 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2376 if (sym == NULL)
2377 break;
2380 gfc_error ("Unable to resolve the specific function '%s' at %L",
2381 expr->symtree->n.sym->name, &expr->where);
2383 return SUCCESS;
2387 /* Resolve a procedure call not known to be generic nor specific. */
2389 static gfc_try
2390 resolve_unknown_f (gfc_expr *expr)
2392 gfc_symbol *sym;
2393 gfc_typespec *ts;
2395 sym = expr->symtree->n.sym;
2397 if (sym->attr.dummy)
2399 sym->attr.proc = PROC_DUMMY;
2400 expr->value.function.name = sym->name;
2401 goto set_type;
2404 /* See if we have an intrinsic function reference. */
2406 if (gfc_is_intrinsic (sym, 0, expr->where))
2408 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2409 return SUCCESS;
2410 return FAILURE;
2413 /* The reference is to an external name. */
2415 sym->attr.proc = PROC_EXTERNAL;
2416 expr->value.function.name = sym->name;
2417 expr->value.function.esym = expr->symtree->n.sym;
2419 if (sym->as != NULL)
2420 expr->rank = sym->as->rank;
2422 /* Type of the expression is either the type of the symbol or the
2423 default type of the symbol. */
2425 set_type:
2426 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2428 if (sym->ts.type != BT_UNKNOWN)
2429 expr->ts = sym->ts;
2430 else
2432 ts = gfc_get_default_type (sym->name, sym->ns);
2434 if (ts->type == BT_UNKNOWN)
2436 gfc_error ("Function '%s' at %L has no IMPLICIT type",
2437 sym->name, &expr->where);
2438 return FAILURE;
2440 else
2441 expr->ts = *ts;
2444 return SUCCESS;
2448 /* Return true, if the symbol is an external procedure. */
2449 static bool
2450 is_external_proc (gfc_symbol *sym)
2452 if (!sym->attr.dummy && !sym->attr.contained
2453 && !(sym->attr.intrinsic
2454 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
2455 && sym->attr.proc != PROC_ST_FUNCTION
2456 && !sym->attr.proc_pointer
2457 && !sym->attr.use_assoc
2458 && sym->name)
2459 return true;
2461 return false;
2465 /* Figure out if a function reference is pure or not. Also set the name
2466 of the function for a potential error message. Return nonzero if the
2467 function is PURE, zero if not. */
2468 static int
2469 pure_stmt_function (gfc_expr *, gfc_symbol *);
2471 static int
2472 pure_function (gfc_expr *e, const char **name)
2474 int pure;
2476 *name = NULL;
2478 if (e->symtree != NULL
2479 && e->symtree->n.sym != NULL
2480 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2481 return pure_stmt_function (e, e->symtree->n.sym);
2483 if (e->value.function.esym)
2485 pure = gfc_pure (e->value.function.esym);
2486 *name = e->value.function.esym->name;
2488 else if (e->value.function.isym)
2490 pure = e->value.function.isym->pure
2491 || e->value.function.isym->elemental;
2492 *name = e->value.function.isym->name;
2494 else
2496 /* Implicit functions are not pure. */
2497 pure = 0;
2498 *name = e->value.function.name;
2501 return pure;
2505 static bool
2506 impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2507 int *f ATTRIBUTE_UNUSED)
2509 const char *name;
2511 /* Don't bother recursing into other statement functions
2512 since they will be checked individually for purity. */
2513 if (e->expr_type != EXPR_FUNCTION
2514 || !e->symtree
2515 || e->symtree->n.sym == sym
2516 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2517 return false;
2519 return pure_function (e, &name) ? false : true;
2523 static int
2524 pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2526 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2530 static gfc_try
2531 is_scalar_expr_ptr (gfc_expr *expr)
2533 gfc_try retval = SUCCESS;
2534 gfc_ref *ref;
2535 int start;
2536 int end;
2538 /* See if we have a gfc_ref, which means we have a substring, array
2539 reference, or a component. */
2540 if (expr->ref != NULL)
2542 ref = expr->ref;
2543 while (ref->next != NULL)
2544 ref = ref->next;
2546 switch (ref->type)
2548 case REF_SUBSTRING:
2549 if (ref->u.ss.length != NULL
2550 && ref->u.ss.length->length != NULL
2551 && ref->u.ss.start
2552 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2553 && ref->u.ss.end
2554 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2556 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2557 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2558 if (end - start + 1 != 1)
2559 retval = FAILURE;
2561 else
2562 retval = FAILURE;
2563 break;
2564 case REF_ARRAY:
2565 if (ref->u.ar.type == AR_ELEMENT)
2566 retval = SUCCESS;
2567 else if (ref->u.ar.type == AR_FULL)
2569 /* The user can give a full array if the array is of size 1. */
2570 if (ref->u.ar.as != NULL
2571 && ref->u.ar.as->rank == 1
2572 && ref->u.ar.as->type == AS_EXPLICIT
2573 && ref->u.ar.as->lower[0] != NULL
2574 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2575 && ref->u.ar.as->upper[0] != NULL
2576 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2578 /* If we have a character string, we need to check if
2579 its length is one. */
2580 if (expr->ts.type == BT_CHARACTER)
2582 if (expr->ts.u.cl == NULL
2583 || expr->ts.u.cl->length == NULL
2584 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
2585 != 0)
2586 retval = FAILURE;
2588 else
2590 /* We have constant lower and upper bounds. If the
2591 difference between is 1, it can be considered a
2592 scalar. */
2593 start = (int) mpz_get_si
2594 (ref->u.ar.as->lower[0]->value.integer);
2595 end = (int) mpz_get_si
2596 (ref->u.ar.as->upper[0]->value.integer);
2597 if (end - start + 1 != 1)
2598 retval = FAILURE;
2601 else
2602 retval = FAILURE;
2604 else
2605 retval = FAILURE;
2606 break;
2607 default:
2608 retval = SUCCESS;
2609 break;
2612 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2614 /* Character string. Make sure it's of length 1. */
2615 if (expr->ts.u.cl == NULL
2616 || expr->ts.u.cl->length == NULL
2617 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
2618 retval = FAILURE;
2620 else if (expr->rank != 0)
2621 retval = FAILURE;
2623 return retval;
2627 /* Match one of the iso_c_binding functions (c_associated or c_loc)
2628 and, in the case of c_associated, set the binding label based on
2629 the arguments. */
2631 static gfc_try
2632 gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2633 gfc_symbol **new_sym)
2635 char name[GFC_MAX_SYMBOL_LEN + 1];
2636 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2637 int optional_arg = 0;
2638 gfc_try retval = SUCCESS;
2639 gfc_symbol *args_sym;
2640 gfc_typespec *arg_ts;
2641 symbol_attribute arg_attr;
2643 if (args->expr->expr_type == EXPR_CONSTANT
2644 || args->expr->expr_type == EXPR_OP
2645 || args->expr->expr_type == EXPR_NULL)
2647 gfc_error ("Argument to '%s' at %L is not a variable",
2648 sym->name, &(args->expr->where));
2649 return FAILURE;
2652 args_sym = args->expr->symtree->n.sym;
2654 /* The typespec for the actual arg should be that stored in the expr
2655 and not necessarily that of the expr symbol (args_sym), because
2656 the actual expression could be a part-ref of the expr symbol. */
2657 arg_ts = &(args->expr->ts);
2658 arg_attr = gfc_expr_attr (args->expr);
2660 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2662 /* If the user gave two args then they are providing something for
2663 the optional arg (the second cptr). Therefore, set the name and
2664 binding label to the c_associated for two cptrs. Otherwise,
2665 set c_associated to expect one cptr. */
2666 if (args->next)
2668 /* two args. */
2669 sprintf (name, "%s_2", sym->name);
2670 sprintf (binding_label, "%s_2", sym->binding_label);
2671 optional_arg = 1;
2673 else
2675 /* one arg. */
2676 sprintf (name, "%s_1", sym->name);
2677 sprintf (binding_label, "%s_1", sym->binding_label);
2678 optional_arg = 0;
2681 /* Get a new symbol for the version of c_associated that
2682 will get called. */
2683 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2685 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2686 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2688 sprintf (name, "%s", sym->name);
2689 sprintf (binding_label, "%s", sym->binding_label);
2691 /* Error check the call. */
2692 if (args->next != NULL)
2694 gfc_error_now ("More actual than formal arguments in '%s' "
2695 "call at %L", name, &(args->expr->where));
2696 retval = FAILURE;
2698 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2700 /* Make sure we have either the target or pointer attribute. */
2701 if (!arg_attr.target && !arg_attr.pointer)
2703 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2704 "a TARGET or an associated pointer",
2705 args_sym->name,
2706 sym->name, &(args->expr->where));
2707 retval = FAILURE;
2710 /* See if we have interoperable type and type param. */
2711 if (verify_c_interop (arg_ts) == SUCCESS
2712 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
2714 if (args_sym->attr.target == 1)
2716 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2717 has the target attribute and is interoperable. */
2718 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2719 allocatable variable that has the TARGET attribute and
2720 is not an array of zero size. */
2721 if (args_sym->attr.allocatable == 1)
2723 if (args_sym->attr.dimension != 0
2724 && (args_sym->as && args_sym->as->rank == 0))
2726 gfc_error_now ("Allocatable variable '%s' used as a "
2727 "parameter to '%s' at %L must not be "
2728 "an array of zero size",
2729 args_sym->name, sym->name,
2730 &(args->expr->where));
2731 retval = FAILURE;
2734 else
2736 /* A non-allocatable target variable with C
2737 interoperable type and type parameters must be
2738 interoperable. */
2739 if (args_sym && args_sym->attr.dimension)
2741 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2743 gfc_error ("Assumed-shape array '%s' at %L "
2744 "cannot be an argument to the "
2745 "procedure '%s' because "
2746 "it is not C interoperable",
2747 args_sym->name,
2748 &(args->expr->where), sym->name);
2749 retval = FAILURE;
2751 else if (args_sym->as->type == AS_DEFERRED)
2753 gfc_error ("Deferred-shape array '%s' at %L "
2754 "cannot be an argument to the "
2755 "procedure '%s' because "
2756 "it is not C interoperable",
2757 args_sym->name,
2758 &(args->expr->where), sym->name);
2759 retval = FAILURE;
2763 /* Make sure it's not a character string. Arrays of
2764 any type should be ok if the variable is of a C
2765 interoperable type. */
2766 if (arg_ts->type == BT_CHARACTER)
2767 if (arg_ts->u.cl != NULL
2768 && (arg_ts->u.cl->length == NULL
2769 || arg_ts->u.cl->length->expr_type
2770 != EXPR_CONSTANT
2771 || mpz_cmp_si
2772 (arg_ts->u.cl->length->value.integer, 1)
2773 != 0)
2774 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2776 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2777 "at %L must have a length of 1",
2778 args_sym->name, sym->name,
2779 &(args->expr->where));
2780 retval = FAILURE;
2784 else if (arg_attr.pointer
2785 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2787 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2788 scalar pointer. */
2789 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2790 "associated scalar POINTER", args_sym->name,
2791 sym->name, &(args->expr->where));
2792 retval = FAILURE;
2795 else
2797 /* The parameter is not required to be C interoperable. If it
2798 is not C interoperable, it must be a nonpolymorphic scalar
2799 with no length type parameters. It still must have either
2800 the pointer or target attribute, and it can be
2801 allocatable (but must be allocated when c_loc is called). */
2802 if (args->expr->rank != 0
2803 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2805 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2806 "scalar", args_sym->name, sym->name,
2807 &(args->expr->where));
2808 retval = FAILURE;
2810 else if (arg_ts->type == BT_CHARACTER
2811 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2813 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2814 "%L must have a length of 1",
2815 args_sym->name, sym->name,
2816 &(args->expr->where));
2817 retval = FAILURE;
2819 else if (arg_ts->type == BT_CLASS)
2821 gfc_error_now ("Parameter '%s' to '%s' at %L must not be "
2822 "polymorphic", args_sym->name, sym->name,
2823 &(args->expr->where));
2824 retval = FAILURE;
2828 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2830 if (args_sym->attr.flavor != FL_PROCEDURE)
2832 /* TODO: Update this error message to allow for procedure
2833 pointers once they are implemented. */
2834 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2835 "procedure",
2836 args_sym->name, sym->name,
2837 &(args->expr->where));
2838 retval = FAILURE;
2840 else if (args_sym->attr.is_bind_c != 1)
2842 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2843 "BIND(C)",
2844 args_sym->name, sym->name,
2845 &(args->expr->where));
2846 retval = FAILURE;
2850 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2851 *new_sym = sym;
2853 else
2855 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2856 "iso_c_binding function: '%s'!\n", sym->name);
2859 return retval;
2863 /* Resolve a function call, which means resolving the arguments, then figuring
2864 out which entity the name refers to. */
2866 static gfc_try
2867 resolve_function (gfc_expr *expr)
2869 gfc_actual_arglist *arg;
2870 gfc_symbol *sym;
2871 const char *name;
2872 gfc_try t;
2873 int temp;
2874 procedure_type p = PROC_INTRINSIC;
2875 bool no_formal_args;
2877 sym = NULL;
2878 if (expr->symtree)
2879 sym = expr->symtree->n.sym;
2881 /* If this is a procedure pointer component, it has already been resolved. */
2882 if (gfc_is_proc_ptr_comp (expr, NULL))
2883 return SUCCESS;
2885 if (sym && sym->attr.intrinsic
2886 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2887 return FAILURE;
2889 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
2891 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
2892 return FAILURE;
2895 /* If this ia a deferred TBP with an abstract interface (which may
2896 of course be referenced), expr->value.function.esym will be set. */
2897 if (sym && sym->attr.abstract && !expr->value.function.esym)
2899 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2900 sym->name, &expr->where);
2901 return FAILURE;
2904 /* Switch off assumed size checking and do this again for certain kinds
2905 of procedure, once the procedure itself is resolved. */
2906 need_full_assumed_size++;
2908 if (expr->symtree && expr->symtree->n.sym)
2909 p = expr->symtree->n.sym->attr.proc;
2911 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2912 inquiry_argument = true;
2913 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2915 if (resolve_actual_arglist (expr->value.function.actual,
2916 p, no_formal_args) == FAILURE)
2918 inquiry_argument = false;
2919 return FAILURE;
2922 inquiry_argument = false;
2924 /* Need to setup the call to the correct c_associated, depending on
2925 the number of cptrs to user gives to compare. */
2926 if (sym && sym->attr.is_iso_c == 1)
2928 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2929 == FAILURE)
2930 return FAILURE;
2932 /* Get the symtree for the new symbol (resolved func).
2933 the old one will be freed later, when it's no longer used. */
2934 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2937 /* Resume assumed_size checking. */
2938 need_full_assumed_size--;
2940 /* If the procedure is external, check for usage. */
2941 if (sym && is_external_proc (sym))
2942 resolve_global_procedure (sym, &expr->where,
2943 &expr->value.function.actual, 0);
2945 if (sym && sym->ts.type == BT_CHARACTER
2946 && sym->ts.u.cl
2947 && sym->ts.u.cl->length == NULL
2948 && !sym->attr.dummy
2949 && expr->value.function.esym == NULL
2950 && !sym->attr.contained)
2952 /* Internal procedures are taken care of in resolve_contained_fntype. */
2953 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2954 "be used at %L since it is not a dummy argument",
2955 sym->name, &expr->where);
2956 return FAILURE;
2959 /* See if function is already resolved. */
2961 if (expr->value.function.name != NULL)
2963 if (expr->ts.type == BT_UNKNOWN)
2964 expr->ts = sym->ts;
2965 t = SUCCESS;
2967 else
2969 /* Apply the rules of section 14.1.2. */
2971 switch (procedure_kind (sym))
2973 case PTYPE_GENERIC:
2974 t = resolve_generic_f (expr);
2975 break;
2977 case PTYPE_SPECIFIC:
2978 t = resolve_specific_f (expr);
2979 break;
2981 case PTYPE_UNKNOWN:
2982 t = resolve_unknown_f (expr);
2983 break;
2985 default:
2986 gfc_internal_error ("resolve_function(): bad function type");
2990 /* If the expression is still a function (it might have simplified),
2991 then we check to see if we are calling an elemental function. */
2993 if (expr->expr_type != EXPR_FUNCTION)
2994 return t;
2996 temp = need_full_assumed_size;
2997 need_full_assumed_size = 0;
2999 if (resolve_elemental_actual (expr, NULL) == FAILURE)
3000 return FAILURE;
3002 if (omp_workshare_flag
3003 && expr->value.function.esym
3004 && ! gfc_elemental (expr->value.function.esym))
3006 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
3007 "in WORKSHARE construct", expr->value.function.esym->name,
3008 &expr->where);
3009 t = FAILURE;
3012 #define GENERIC_ID expr->value.function.isym->id
3013 else if (expr->value.function.actual != NULL
3014 && expr->value.function.isym != NULL
3015 && GENERIC_ID != GFC_ISYM_LBOUND
3016 && GENERIC_ID != GFC_ISYM_LEN
3017 && GENERIC_ID != GFC_ISYM_LOC
3018 && GENERIC_ID != GFC_ISYM_PRESENT)
3020 /* Array intrinsics must also have the last upper bound of an
3021 assumed size array argument. UBOUND and SIZE have to be
3022 excluded from the check if the second argument is anything
3023 than a constant. */
3025 for (arg = expr->value.function.actual; arg; arg = arg->next)
3027 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3028 && arg->next != NULL && arg->next->expr)
3030 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3031 break;
3033 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
3034 break;
3036 if ((int)mpz_get_si (arg->next->expr->value.integer)
3037 < arg->expr->rank)
3038 break;
3041 if (arg->expr != NULL
3042 && arg->expr->rank > 0
3043 && resolve_assumed_size_actual (arg->expr))
3044 return FAILURE;
3047 #undef GENERIC_ID
3049 need_full_assumed_size = temp;
3050 name = NULL;
3052 if (!pure_function (expr, &name) && name)
3054 if (forall_flag)
3056 gfc_error ("reference to non-PURE function '%s' at %L inside a "
3057 "FORALL %s", name, &expr->where,
3058 forall_flag == 2 ? "mask" : "block");
3059 t = FAILURE;
3061 else if (gfc_pure (NULL))
3063 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3064 "procedure within a PURE procedure", name, &expr->where);
3065 t = FAILURE;
3069 /* Functions without the RECURSIVE attribution are not allowed to
3070 * call themselves. */
3071 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3073 gfc_symbol *esym;
3074 esym = expr->value.function.esym;
3076 if (is_illegal_recursion (esym, gfc_current_ns))
3078 if (esym->attr.entry && esym->ns->entries)
3079 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3080 " function '%s' is not RECURSIVE",
3081 esym->name, &expr->where, esym->ns->entries->sym->name);
3082 else
3083 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3084 " is not RECURSIVE", esym->name, &expr->where);
3086 t = FAILURE;
3090 /* Character lengths of use associated functions may contains references to
3091 symbols not referenced from the current program unit otherwise. Make sure
3092 those symbols are marked as referenced. */
3094 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3095 && expr->value.function.esym->attr.use_assoc)
3097 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3100 /* Make sure that the expression has a typespec that works. */
3101 if (expr->ts.type == BT_UNKNOWN)
3103 if (expr->symtree->n.sym->result
3104 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3105 && !expr->symtree->n.sym->result->attr.proc_pointer)
3106 expr->ts = expr->symtree->n.sym->result->ts;
3109 return t;
3113 /************* Subroutine resolution *************/
3115 static void
3116 pure_subroutine (gfc_code *c, gfc_symbol *sym)
3118 if (gfc_pure (sym))
3119 return;
3121 if (forall_flag)
3122 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3123 sym->name, &c->loc);
3124 else if (gfc_pure (NULL))
3125 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3126 &c->loc);
3130 static match
3131 resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3133 gfc_symbol *s;
3135 if (sym->attr.generic)
3137 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3138 if (s != NULL)
3140 c->resolved_sym = s;
3141 pure_subroutine (c, s);
3142 return MATCH_YES;
3145 /* TODO: Need to search for elemental references in generic interface. */
3148 if (sym->attr.intrinsic)
3149 return gfc_intrinsic_sub_interface (c, 0);
3151 return MATCH_NO;
3155 static gfc_try
3156 resolve_generic_s (gfc_code *c)
3158 gfc_symbol *sym;
3159 match m;
3161 sym = c->symtree->n.sym;
3163 for (;;)
3165 m = resolve_generic_s0 (c, sym);
3166 if (m == MATCH_YES)
3167 return SUCCESS;
3168 else if (m == MATCH_ERROR)
3169 return FAILURE;
3171 generic:
3172 if (sym->ns->parent == NULL)
3173 break;
3174 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3176 if (sym == NULL)
3177 break;
3178 if (!generic_sym (sym))
3179 goto generic;
3182 /* Last ditch attempt. See if the reference is to an intrinsic
3183 that possesses a matching interface. 14.1.2.4 */
3184 sym = c->symtree->n.sym;
3186 if (!gfc_is_intrinsic (sym, 1, c->loc))
3188 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3189 sym->name, &c->loc);
3190 return FAILURE;
3193 m = gfc_intrinsic_sub_interface (c, 0);
3194 if (m == MATCH_YES)
3195 return SUCCESS;
3196 if (m == MATCH_NO)
3197 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3198 "intrinsic subroutine interface", sym->name, &c->loc);
3200 return FAILURE;
3204 /* Set the name and binding label of the subroutine symbol in the call
3205 expression represented by 'c' to include the type and kind of the
3206 second parameter. This function is for resolving the appropriate
3207 version of c_f_pointer() and c_f_procpointer(). For example, a
3208 call to c_f_pointer() for a default integer pointer could have a
3209 name of c_f_pointer_i4. If no second arg exists, which is an error
3210 for these two functions, it defaults to the generic symbol's name
3211 and binding label. */
3213 static void
3214 set_name_and_label (gfc_code *c, gfc_symbol *sym,
3215 char *name, char *binding_label)
3217 gfc_expr *arg = NULL;
3218 char type;
3219 int kind;
3221 /* The second arg of c_f_pointer and c_f_procpointer determines
3222 the type and kind for the procedure name. */
3223 arg = c->ext.actual->next->expr;
3225 if (arg != NULL)
3227 /* Set up the name to have the given symbol's name,
3228 plus the type and kind. */
3229 /* a derived type is marked with the type letter 'u' */
3230 if (arg->ts.type == BT_DERIVED)
3232 type = 'd';
3233 kind = 0; /* set the kind as 0 for now */
3235 else
3237 type = gfc_type_letter (arg->ts.type);
3238 kind = arg->ts.kind;
3241 if (arg->ts.type == BT_CHARACTER)
3242 /* Kind info for character strings not needed. */
3243 kind = 0;
3245 sprintf (name, "%s_%c%d", sym->name, type, kind);
3246 /* Set up the binding label as the given symbol's label plus
3247 the type and kind. */
3248 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
3250 else
3252 /* If the second arg is missing, set the name and label as
3253 was, cause it should at least be found, and the missing
3254 arg error will be caught by compare_parameters(). */
3255 sprintf (name, "%s", sym->name);
3256 sprintf (binding_label, "%s", sym->binding_label);
3259 return;
3263 /* Resolve a generic version of the iso_c_binding procedure given
3264 (sym) to the specific one based on the type and kind of the
3265 argument(s). Currently, this function resolves c_f_pointer() and
3266 c_f_procpointer based on the type and kind of the second argument
3267 (FPTR). Other iso_c_binding procedures aren't specially handled.
3268 Upon successfully exiting, c->resolved_sym will hold the resolved
3269 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
3270 otherwise. */
3272 match
3273 gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
3275 gfc_symbol *new_sym;
3276 /* this is fine, since we know the names won't use the max */
3277 char name[GFC_MAX_SYMBOL_LEN + 1];
3278 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
3279 /* default to success; will override if find error */
3280 match m = MATCH_YES;
3282 /* Make sure the actual arguments are in the necessary order (based on the
3283 formal args) before resolving. */
3284 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
3286 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3287 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3289 set_name_and_label (c, sym, name, binding_label);
3291 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3293 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
3295 /* Make sure we got a third arg if the second arg has non-zero
3296 rank. We must also check that the type and rank are
3297 correct since we short-circuit this check in
3298 gfc_procedure_use() (called above to sort actual args). */
3299 if (c->ext.actual->next->expr->rank != 0)
3301 if(c->ext.actual->next->next == NULL
3302 || c->ext.actual->next->next->expr == NULL)
3304 m = MATCH_ERROR;
3305 gfc_error ("Missing SHAPE parameter for call to %s "
3306 "at %L", sym->name, &(c->loc));
3308 else if (c->ext.actual->next->next->expr->ts.type
3309 != BT_INTEGER
3310 || c->ext.actual->next->next->expr->rank != 1)
3312 m = MATCH_ERROR;
3313 gfc_error ("SHAPE parameter for call to %s at %L must "
3314 "be a rank 1 INTEGER array", sym->name,
3315 &(c->loc));
3321 if (m != MATCH_ERROR)
3323 /* the 1 means to add the optional arg to formal list */
3324 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
3326 /* for error reporting, say it's declared where the original was */
3327 new_sym->declared_at = sym->declared_at;
3330 else
3332 /* no differences for c_loc or c_funloc */
3333 new_sym = sym;
3336 /* set the resolved symbol */
3337 if (m != MATCH_ERROR)
3338 c->resolved_sym = new_sym;
3339 else
3340 c->resolved_sym = sym;
3342 return m;
3346 /* Resolve a subroutine call known to be specific. */
3348 static match
3349 resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3351 match m;
3353 if(sym->attr.is_iso_c)
3355 m = gfc_iso_c_sub_interface (c,sym);
3356 return m;
3359 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3361 if (sym->attr.dummy)
3363 sym->attr.proc = PROC_DUMMY;
3364 goto found;
3367 sym->attr.proc = PROC_EXTERNAL;
3368 goto found;
3371 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3372 goto found;
3374 if (sym->attr.intrinsic)
3376 m = gfc_intrinsic_sub_interface (c, 1);
3377 if (m == MATCH_YES)
3378 return MATCH_YES;
3379 if (m == MATCH_NO)
3380 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3381 "with an intrinsic", sym->name, &c->loc);
3383 return MATCH_ERROR;
3386 return MATCH_NO;
3388 found:
3389 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3391 c->resolved_sym = sym;
3392 pure_subroutine (c, sym);
3394 return MATCH_YES;
3398 static gfc_try
3399 resolve_specific_s (gfc_code *c)
3401 gfc_symbol *sym;
3402 match m;
3404 sym = c->symtree->n.sym;
3406 for (;;)
3408 m = resolve_specific_s0 (c, sym);
3409 if (m == MATCH_YES)
3410 return SUCCESS;
3411 if (m == MATCH_ERROR)
3412 return FAILURE;
3414 if (sym->ns->parent == NULL)
3415 break;
3417 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3419 if (sym == NULL)
3420 break;
3423 sym = c->symtree->n.sym;
3424 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3425 sym->name, &c->loc);
3427 return FAILURE;
3431 /* Resolve a subroutine call not known to be generic nor specific. */
3433 static gfc_try
3434 resolve_unknown_s (gfc_code *c)
3436 gfc_symbol *sym;
3438 sym = c->symtree->n.sym;
3440 if (sym->attr.dummy)
3442 sym->attr.proc = PROC_DUMMY;
3443 goto found;
3446 /* See if we have an intrinsic function reference. */
3448 if (gfc_is_intrinsic (sym, 1, c->loc))
3450 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3451 return SUCCESS;
3452 return FAILURE;
3455 /* The reference is to an external name. */
3457 found:
3458 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3460 c->resolved_sym = sym;
3462 pure_subroutine (c, sym);
3464 return SUCCESS;
3468 /* Resolve a subroutine call. Although it was tempting to use the same code
3469 for functions, subroutines and functions are stored differently and this
3470 makes things awkward. */
3472 static gfc_try
3473 resolve_call (gfc_code *c)
3475 gfc_try t;
3476 procedure_type ptype = PROC_INTRINSIC;
3477 gfc_symbol *csym, *sym;
3478 bool no_formal_args;
3480 csym = c->symtree ? c->symtree->n.sym : NULL;
3482 if (csym && csym->ts.type != BT_UNKNOWN)
3484 gfc_error ("'%s' at %L has a type, which is not consistent with "
3485 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3486 return FAILURE;
3489 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3491 gfc_symtree *st;
3492 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3493 sym = st ? st->n.sym : NULL;
3494 if (sym && csym != sym
3495 && sym->ns == gfc_current_ns
3496 && sym->attr.flavor == FL_PROCEDURE
3497 && sym->attr.contained)
3499 sym->refs++;
3500 if (csym->attr.generic)
3501 c->symtree->n.sym = sym;
3502 else
3503 c->symtree = st;
3504 csym = c->symtree->n.sym;
3508 /* If this ia a deferred TBP with an abstract interface
3509 (which may of course be referenced), c->expr1 will be set. */
3510 if (csym && csym->attr.abstract && !c->expr1)
3512 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3513 csym->name, &c->loc);
3514 return FAILURE;
3517 /* Subroutines without the RECURSIVE attribution are not allowed to
3518 * call themselves. */
3519 if (csym && is_illegal_recursion (csym, gfc_current_ns))
3521 if (csym->attr.entry && csym->ns->entries)
3522 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3523 " subroutine '%s' is not RECURSIVE",
3524 csym->name, &c->loc, csym->ns->entries->sym->name);
3525 else
3526 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3527 " is not RECURSIVE", csym->name, &c->loc);
3529 t = FAILURE;
3532 /* Switch off assumed size checking and do this again for certain kinds
3533 of procedure, once the procedure itself is resolved. */
3534 need_full_assumed_size++;
3536 if (csym)
3537 ptype = csym->attr.proc;
3539 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3540 if (resolve_actual_arglist (c->ext.actual, ptype,
3541 no_formal_args) == FAILURE)
3542 return FAILURE;
3544 /* Resume assumed_size checking. */
3545 need_full_assumed_size--;
3547 /* If external, check for usage. */
3548 if (csym && is_external_proc (csym))
3549 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3551 t = SUCCESS;
3552 if (c->resolved_sym == NULL)
3554 c->resolved_isym = NULL;
3555 switch (procedure_kind (csym))
3557 case PTYPE_GENERIC:
3558 t = resolve_generic_s (c);
3559 break;
3561 case PTYPE_SPECIFIC:
3562 t = resolve_specific_s (c);
3563 break;
3565 case PTYPE_UNKNOWN:
3566 t = resolve_unknown_s (c);
3567 break;
3569 default:
3570 gfc_internal_error ("resolve_subroutine(): bad function type");
3574 /* Some checks of elemental subroutine actual arguments. */
3575 if (resolve_elemental_actual (NULL, c) == FAILURE)
3576 return FAILURE;
3578 return t;
3582 /* Compare the shapes of two arrays that have non-NULL shapes. If both
3583 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3584 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3585 if their shapes do not match. If either op1->shape or op2->shape is
3586 NULL, return SUCCESS. */
3588 static gfc_try
3589 compare_shapes (gfc_expr *op1, gfc_expr *op2)
3591 gfc_try t;
3592 int i;
3594 t = SUCCESS;
3596 if (op1->shape != NULL && op2->shape != NULL)
3598 for (i = 0; i < op1->rank; i++)
3600 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3602 gfc_error ("Shapes for operands at %L and %L are not conformable",
3603 &op1->where, &op2->where);
3604 t = FAILURE;
3605 break;
3610 return t;
3614 /* Resolve an operator expression node. This can involve replacing the
3615 operation with a user defined function call. */
3617 static gfc_try
3618 resolve_operator (gfc_expr *e)
3620 gfc_expr *op1, *op2;
3621 char msg[200];
3622 bool dual_locus_error;
3623 gfc_try t;
3625 /* Resolve all subnodes-- give them types. */
3627 switch (e->value.op.op)
3629 default:
3630 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
3631 return FAILURE;
3633 /* Fall through... */
3635 case INTRINSIC_NOT:
3636 case INTRINSIC_UPLUS:
3637 case INTRINSIC_UMINUS:
3638 case INTRINSIC_PARENTHESES:
3639 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
3640 return FAILURE;
3641 break;
3644 /* Typecheck the new node. */
3646 op1 = e->value.op.op1;
3647 op2 = e->value.op.op2;
3648 dual_locus_error = false;
3650 if ((op1 && op1->expr_type == EXPR_NULL)
3651 || (op2 && op2->expr_type == EXPR_NULL))
3653 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3654 goto bad_op;
3657 switch (e->value.op.op)
3659 case INTRINSIC_UPLUS:
3660 case INTRINSIC_UMINUS:
3661 if (op1->ts.type == BT_INTEGER
3662 || op1->ts.type == BT_REAL
3663 || op1->ts.type == BT_COMPLEX)
3665 e->ts = op1->ts;
3666 break;
3669 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
3670 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
3671 goto bad_op;
3673 case INTRINSIC_PLUS:
3674 case INTRINSIC_MINUS:
3675 case INTRINSIC_TIMES:
3676 case INTRINSIC_DIVIDE:
3677 case INTRINSIC_POWER:
3678 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3680 gfc_type_convert_binary (e, 1);
3681 break;
3684 sprintf (msg,
3685 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
3686 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3687 gfc_typename (&op2->ts));
3688 goto bad_op;
3690 case INTRINSIC_CONCAT:
3691 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3692 && op1->ts.kind == op2->ts.kind)
3694 e->ts.type = BT_CHARACTER;
3695 e->ts.kind = op1->ts.kind;
3696 break;
3699 sprintf (msg,
3700 _("Operands of string concatenation operator at %%L are %s/%s"),
3701 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3702 goto bad_op;
3704 case INTRINSIC_AND:
3705 case INTRINSIC_OR:
3706 case INTRINSIC_EQV:
3707 case INTRINSIC_NEQV:
3708 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3710 e->ts.type = BT_LOGICAL;
3711 e->ts.kind = gfc_kind_max (op1, op2);
3712 if (op1->ts.kind < e->ts.kind)
3713 gfc_convert_type (op1, &e->ts, 2);
3714 else if (op2->ts.kind < e->ts.kind)
3715 gfc_convert_type (op2, &e->ts, 2);
3716 break;
3719 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
3720 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3721 gfc_typename (&op2->ts));
3723 goto bad_op;
3725 case INTRINSIC_NOT:
3726 if (op1->ts.type == BT_LOGICAL)
3728 e->ts.type = BT_LOGICAL;
3729 e->ts.kind = op1->ts.kind;
3730 break;
3733 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
3734 gfc_typename (&op1->ts));
3735 goto bad_op;
3737 case INTRINSIC_GT:
3738 case INTRINSIC_GT_OS:
3739 case INTRINSIC_GE:
3740 case INTRINSIC_GE_OS:
3741 case INTRINSIC_LT:
3742 case INTRINSIC_LT_OS:
3743 case INTRINSIC_LE:
3744 case INTRINSIC_LE_OS:
3745 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3747 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
3748 goto bad_op;
3751 /* Fall through... */
3753 case INTRINSIC_EQ:
3754 case INTRINSIC_EQ_OS:
3755 case INTRINSIC_NE:
3756 case INTRINSIC_NE_OS:
3757 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3758 && op1->ts.kind == op2->ts.kind)
3760 e->ts.type = BT_LOGICAL;
3761 e->ts.kind = gfc_default_logical_kind;
3762 break;
3765 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3767 gfc_type_convert_binary (e, 1);
3769 e->ts.type = BT_LOGICAL;
3770 e->ts.kind = gfc_default_logical_kind;
3771 break;
3774 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3775 sprintf (msg,
3776 _("Logicals at %%L must be compared with %s instead of %s"),
3777 (e->value.op.op == INTRINSIC_EQ
3778 || e->value.op.op == INTRINSIC_EQ_OS)
3779 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
3780 else
3781 sprintf (msg,
3782 _("Operands of comparison operator '%s' at %%L are %s/%s"),
3783 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
3784 gfc_typename (&op2->ts));
3786 goto bad_op;
3788 case INTRINSIC_USER:
3789 if (e->value.op.uop->op == NULL)
3790 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3791 else if (op2 == NULL)
3792 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
3793 e->value.op.uop->name, gfc_typename (&op1->ts));
3794 else
3795 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3796 e->value.op.uop->name, gfc_typename (&op1->ts),
3797 gfc_typename (&op2->ts));
3799 goto bad_op;
3801 case INTRINSIC_PARENTHESES:
3802 e->ts = op1->ts;
3803 if (e->ts.type == BT_CHARACTER)
3804 e->ts.u.cl = op1->ts.u.cl;
3805 break;
3807 default:
3808 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3811 /* Deal with arrayness of an operand through an operator. */
3813 t = SUCCESS;
3815 switch (e->value.op.op)
3817 case INTRINSIC_PLUS:
3818 case INTRINSIC_MINUS:
3819 case INTRINSIC_TIMES:
3820 case INTRINSIC_DIVIDE:
3821 case INTRINSIC_POWER:
3822 case INTRINSIC_CONCAT:
3823 case INTRINSIC_AND:
3824 case INTRINSIC_OR:
3825 case INTRINSIC_EQV:
3826 case INTRINSIC_NEQV:
3827 case INTRINSIC_EQ:
3828 case INTRINSIC_EQ_OS:
3829 case INTRINSIC_NE:
3830 case INTRINSIC_NE_OS:
3831 case INTRINSIC_GT:
3832 case INTRINSIC_GT_OS:
3833 case INTRINSIC_GE:
3834 case INTRINSIC_GE_OS:
3835 case INTRINSIC_LT:
3836 case INTRINSIC_LT_OS:
3837 case INTRINSIC_LE:
3838 case INTRINSIC_LE_OS:
3840 if (op1->rank == 0 && op2->rank == 0)
3841 e->rank = 0;
3843 if (op1->rank == 0 && op2->rank != 0)
3845 e->rank = op2->rank;
3847 if (e->shape == NULL)
3848 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3851 if (op1->rank != 0 && op2->rank == 0)
3853 e->rank = op1->rank;
3855 if (e->shape == NULL)
3856 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3859 if (op1->rank != 0 && op2->rank != 0)
3861 if (op1->rank == op2->rank)
3863 e->rank = op1->rank;
3864 if (e->shape == NULL)
3866 t = compare_shapes (op1, op2);
3867 if (t == FAILURE)
3868 e->shape = NULL;
3869 else
3870 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3873 else
3875 /* Allow higher level expressions to work. */
3876 e->rank = 0;
3878 /* Try user-defined operators, and otherwise throw an error. */
3879 dual_locus_error = true;
3880 sprintf (msg,
3881 _("Inconsistent ranks for operator at %%L and %%L"));
3882 goto bad_op;
3886 break;
3888 case INTRINSIC_PARENTHESES:
3889 case INTRINSIC_NOT:
3890 case INTRINSIC_UPLUS:
3891 case INTRINSIC_UMINUS:
3892 /* Simply copy arrayness attribute */
3893 e->rank = op1->rank;
3895 if (e->shape == NULL)
3896 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3898 break;
3900 default:
3901 break;
3904 /* Attempt to simplify the expression. */
3905 if (t == SUCCESS)
3907 t = gfc_simplify_expr (e, 0);
3908 /* Some calls do not succeed in simplification and return FAILURE
3909 even though there is no error; e.g. variable references to
3910 PARAMETER arrays. */
3911 if (!gfc_is_constant_expr (e))
3912 t = SUCCESS;
3914 return t;
3916 bad_op:
3919 bool real_error;
3920 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3921 return SUCCESS;
3923 if (real_error)
3924 return FAILURE;
3927 if (dual_locus_error)
3928 gfc_error (msg, &op1->where, &op2->where);
3929 else
3930 gfc_error (msg, &e->where);
3932 return FAILURE;
3936 /************** Array resolution subroutines **************/
3938 typedef enum
3939 { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3940 comparison;
3942 /* Compare two integer expressions. */
3944 static comparison
3945 compare_bound (gfc_expr *a, gfc_expr *b)
3947 int i;
3949 if (a == NULL || a->expr_type != EXPR_CONSTANT
3950 || b == NULL || b->expr_type != EXPR_CONSTANT)
3951 return CMP_UNKNOWN;
3953 /* If either of the types isn't INTEGER, we must have
3954 raised an error earlier. */
3956 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
3957 return CMP_UNKNOWN;
3959 i = mpz_cmp (a->value.integer, b->value.integer);
3961 if (i < 0)
3962 return CMP_LT;
3963 if (i > 0)
3964 return CMP_GT;
3965 return CMP_EQ;
3969 /* Compare an integer expression with an integer. */
3971 static comparison
3972 compare_bound_int (gfc_expr *a, int b)
3974 int i;
3976 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3977 return CMP_UNKNOWN;
3979 if (a->ts.type != BT_INTEGER)
3980 gfc_internal_error ("compare_bound_int(): Bad expression");
3982 i = mpz_cmp_si (a->value.integer, b);
3984 if (i < 0)
3985 return CMP_LT;
3986 if (i > 0)
3987 return CMP_GT;
3988 return CMP_EQ;
3992 /* Compare an integer expression with a mpz_t. */
3994 static comparison
3995 compare_bound_mpz_t (gfc_expr *a, mpz_t b)
3997 int i;
3999 if (a == NULL || a->expr_type != EXPR_CONSTANT)
4000 return CMP_UNKNOWN;
4002 if (a->ts.type != BT_INTEGER)
4003 gfc_internal_error ("compare_bound_int(): Bad expression");
4005 i = mpz_cmp (a->value.integer, b);
4007 if (i < 0)
4008 return CMP_LT;
4009 if (i > 0)
4010 return CMP_GT;
4011 return CMP_EQ;
4015 /* Compute the last value of a sequence given by a triplet.
4016 Return 0 if it wasn't able to compute the last value, or if the
4017 sequence if empty, and 1 otherwise. */
4019 static int
4020 compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4021 gfc_expr *stride, mpz_t last)
4023 mpz_t rem;
4025 if (start == NULL || start->expr_type != EXPR_CONSTANT
4026 || end == NULL || end->expr_type != EXPR_CONSTANT
4027 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4028 return 0;
4030 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4031 || (stride != NULL && stride->ts.type != BT_INTEGER))
4032 return 0;
4034 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
4036 if (compare_bound (start, end) == CMP_GT)
4037 return 0;
4038 mpz_set (last, end->value.integer);
4039 return 1;
4042 if (compare_bound_int (stride, 0) == CMP_GT)
4044 /* Stride is positive */
4045 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4046 return 0;
4048 else
4050 /* Stride is negative */
4051 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4052 return 0;
4055 mpz_init (rem);
4056 mpz_sub (rem, end->value.integer, start->value.integer);
4057 mpz_tdiv_r (rem, rem, stride->value.integer);
4058 mpz_sub (last, end->value.integer, rem);
4059 mpz_clear (rem);
4061 return 1;
4065 /* Compare a single dimension of an array reference to the array
4066 specification. */
4068 static gfc_try
4069 check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4071 mpz_t last_value;
4073 if (ar->dimen_type[i] == DIMEN_STAR)
4075 gcc_assert (ar->stride[i] == NULL);
4076 /* This implies [*] as [*:] and [*:3] are not possible. */
4077 if (ar->start[i] == NULL)
4079 gcc_assert (ar->end[i] == NULL);
4080 return SUCCESS;
4084 /* Given start, end and stride values, calculate the minimum and
4085 maximum referenced indexes. */
4087 switch (ar->dimen_type[i])
4089 case DIMEN_VECTOR:
4090 break;
4092 case DIMEN_STAR:
4093 case DIMEN_ELEMENT:
4094 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
4096 if (i < as->rank)
4097 gfc_warning ("Array reference at %L is out of bounds "
4098 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4099 mpz_get_si (ar->start[i]->value.integer),
4100 mpz_get_si (as->lower[i]->value.integer), i+1);
4101 else
4102 gfc_warning ("Array reference at %L is out of bounds "
4103 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4104 mpz_get_si (ar->start[i]->value.integer),
4105 mpz_get_si (as->lower[i]->value.integer),
4106 i + 1 - as->rank);
4107 return SUCCESS;
4109 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
4111 if (i < as->rank)
4112 gfc_warning ("Array reference at %L is out of bounds "
4113 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4114 mpz_get_si (ar->start[i]->value.integer),
4115 mpz_get_si (as->upper[i]->value.integer), i+1);
4116 else
4117 gfc_warning ("Array reference at %L is out of bounds "
4118 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4119 mpz_get_si (ar->start[i]->value.integer),
4120 mpz_get_si (as->upper[i]->value.integer),
4121 i + 1 - as->rank);
4122 return SUCCESS;
4125 break;
4127 case DIMEN_RANGE:
4129 #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4130 #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4132 comparison comp_start_end = compare_bound (AR_START, AR_END);
4134 /* Check for zero stride, which is not allowed. */
4135 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4137 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
4138 return FAILURE;
4141 /* if start == len || (stride > 0 && start < len)
4142 || (stride < 0 && start > len),
4143 then the array section contains at least one element. In this
4144 case, there is an out-of-bounds access if
4145 (start < lower || start > upper). */
4146 if (compare_bound (AR_START, AR_END) == CMP_EQ
4147 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4148 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4149 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4150 && comp_start_end == CMP_GT))
4152 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4154 gfc_warning ("Lower array reference at %L is out of bounds "
4155 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4156 mpz_get_si (AR_START->value.integer),
4157 mpz_get_si (as->lower[i]->value.integer), i+1);
4158 return SUCCESS;
4160 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4162 gfc_warning ("Lower array reference at %L is out of bounds "
4163 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4164 mpz_get_si (AR_START->value.integer),
4165 mpz_get_si (as->upper[i]->value.integer), i+1);
4166 return SUCCESS;
4170 /* If we can compute the highest index of the array section,
4171 then it also has to be between lower and upper. */
4172 mpz_init (last_value);
4173 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4174 last_value))
4176 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4178 gfc_warning ("Upper array reference at %L is out of bounds "
4179 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4180 mpz_get_si (last_value),
4181 mpz_get_si (as->lower[i]->value.integer), i+1);
4182 mpz_clear (last_value);
4183 return SUCCESS;
4185 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
4187 gfc_warning ("Upper array reference at %L is out of bounds "
4188 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4189 mpz_get_si (last_value),
4190 mpz_get_si (as->upper[i]->value.integer), i+1);
4191 mpz_clear (last_value);
4192 return SUCCESS;
4195 mpz_clear (last_value);
4197 #undef AR_START
4198 #undef AR_END
4200 break;
4202 default:
4203 gfc_internal_error ("check_dimension(): Bad array reference");
4206 return SUCCESS;
4210 /* Compare an array reference with an array specification. */
4212 static gfc_try
4213 compare_spec_to_ref (gfc_array_ref *ar)
4215 gfc_array_spec *as;
4216 int i;
4218 as = ar->as;
4219 i = as->rank - 1;
4220 /* TODO: Full array sections are only allowed as actual parameters. */
4221 if (as->type == AS_ASSUMED_SIZE
4222 && (/*ar->type == AR_FULL
4223 ||*/ (ar->type == AR_SECTION
4224 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4226 gfc_error ("Rightmost upper bound of assumed size array section "
4227 "not specified at %L", &ar->where);
4228 return FAILURE;
4231 if (ar->type == AR_FULL)
4232 return SUCCESS;
4234 if (as->rank != ar->dimen)
4236 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4237 &ar->where, ar->dimen, as->rank);
4238 return FAILURE;
4241 /* ar->codimen == 0 is a local array. */
4242 if (as->corank != ar->codimen && ar->codimen != 0)
4244 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4245 &ar->where, ar->codimen, as->corank);
4246 return FAILURE;
4249 for (i = 0; i < as->rank; i++)
4250 if (check_dimension (i, ar, as) == FAILURE)
4251 return FAILURE;
4253 /* Local access has no coarray spec. */
4254 if (ar->codimen != 0)
4255 for (i = as->rank; i < as->rank + as->corank; i++)
4257 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate)
4259 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4260 i + 1 - as->rank, &ar->where);
4261 return FAILURE;
4263 if (check_dimension (i, ar, as) == FAILURE)
4264 return FAILURE;
4267 return SUCCESS;
4271 /* Resolve one part of an array index. */
4273 static gfc_try
4274 gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4275 int force_index_integer_kind)
4277 gfc_typespec ts;
4279 if (index == NULL)
4280 return SUCCESS;
4282 if (gfc_resolve_expr (index) == FAILURE)
4283 return FAILURE;
4285 if (check_scalar && index->rank != 0)
4287 gfc_error ("Array index at %L must be scalar", &index->where);
4288 return FAILURE;
4291 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4293 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4294 &index->where, gfc_basic_typename (index->ts.type));
4295 return FAILURE;
4298 if (index->ts.type == BT_REAL)
4299 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
4300 &index->where) == FAILURE)
4301 return FAILURE;
4303 if ((index->ts.kind != gfc_index_integer_kind
4304 && force_index_integer_kind)
4305 || index->ts.type != BT_INTEGER)
4307 gfc_clear_ts (&ts);
4308 ts.type = BT_INTEGER;
4309 ts.kind = gfc_index_integer_kind;
4311 gfc_convert_type_warn (index, &ts, 2, 0);
4314 return SUCCESS;
4317 /* Resolve one part of an array index. */
4319 gfc_try
4320 gfc_resolve_index (gfc_expr *index, int check_scalar)
4322 return gfc_resolve_index_1 (index, check_scalar, 1);
4325 /* Resolve a dim argument to an intrinsic function. */
4327 gfc_try
4328 gfc_resolve_dim_arg (gfc_expr *dim)
4330 if (dim == NULL)
4331 return SUCCESS;
4333 if (gfc_resolve_expr (dim) == FAILURE)
4334 return FAILURE;
4336 if (dim->rank != 0)
4338 gfc_error ("Argument dim at %L must be scalar", &dim->where);
4339 return FAILURE;
4343 if (dim->ts.type != BT_INTEGER)
4345 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
4346 return FAILURE;
4349 if (dim->ts.kind != gfc_index_integer_kind)
4351 gfc_typespec ts;
4353 gfc_clear_ts (&ts);
4354 ts.type = BT_INTEGER;
4355 ts.kind = gfc_index_integer_kind;
4357 gfc_convert_type_warn (dim, &ts, 2, 0);
4360 return SUCCESS;
4363 /* Given an expression that contains array references, update those array
4364 references to point to the right array specifications. While this is
4365 filled in during matching, this information is difficult to save and load
4366 in a module, so we take care of it here.
4368 The idea here is that the original array reference comes from the
4369 base symbol. We traverse the list of reference structures, setting
4370 the stored reference to references. Component references can
4371 provide an additional array specification. */
4373 static void
4374 find_array_spec (gfc_expr *e)
4376 gfc_array_spec *as;
4377 gfc_component *c;
4378 gfc_symbol *derived;
4379 gfc_ref *ref;
4381 if (e->symtree->n.sym->ts.type == BT_CLASS)
4382 as = CLASS_DATA (e->symtree->n.sym)->as;
4383 else
4384 as = e->symtree->n.sym->as;
4385 derived = NULL;
4387 for (ref = e->ref; ref; ref = ref->next)
4388 switch (ref->type)
4390 case REF_ARRAY:
4391 if (as == NULL)
4392 gfc_internal_error ("find_array_spec(): Missing spec");
4394 ref->u.ar.as = as;
4395 as = NULL;
4396 break;
4398 case REF_COMPONENT:
4399 if (derived == NULL)
4400 derived = e->symtree->n.sym->ts.u.derived;
4402 if (derived->attr.is_class)
4403 derived = derived->components->ts.u.derived;
4405 c = derived->components;
4407 for (; c; c = c->next)
4408 if (c == ref->u.c.component)
4410 /* Track the sequence of component references. */
4411 if (c->ts.type == BT_DERIVED)
4412 derived = c->ts.u.derived;
4413 break;
4416 if (c == NULL)
4417 gfc_internal_error ("find_array_spec(): Component not found");
4419 if (c->attr.dimension)
4421 if (as != NULL)
4422 gfc_internal_error ("find_array_spec(): unused as(1)");
4423 as = c->as;
4426 break;
4428 case REF_SUBSTRING:
4429 break;
4432 if (as != NULL)
4433 gfc_internal_error ("find_array_spec(): unused as(2)");
4437 /* Resolve an array reference. */
4439 static gfc_try
4440 resolve_array_ref (gfc_array_ref *ar)
4442 int i, check_scalar;
4443 gfc_expr *e;
4445 for (i = 0; i < ar->dimen + ar->codimen; i++)
4447 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4449 /* Do not force gfc_index_integer_kind for the start. We can
4450 do fine with any integer kind. This avoids temporary arrays
4451 created for indexing with a vector. */
4452 if (gfc_resolve_index_1 (ar->start[i], check_scalar, 0) == FAILURE)
4453 return FAILURE;
4454 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4455 return FAILURE;
4456 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4457 return FAILURE;
4459 e = ar->start[i];
4461 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
4462 switch (e->rank)
4464 case 0:
4465 ar->dimen_type[i] = DIMEN_ELEMENT;
4466 break;
4468 case 1:
4469 ar->dimen_type[i] = DIMEN_VECTOR;
4470 if (e->expr_type == EXPR_VARIABLE
4471 && e->symtree->n.sym->ts.type == BT_DERIVED)
4472 ar->start[i] = gfc_get_parentheses (e);
4473 break;
4475 default:
4476 gfc_error ("Array index at %L is an array of rank %d",
4477 &ar->c_where[i], e->rank);
4478 return FAILURE;
4481 /* Fill in the upper bound, which may be lower than the
4482 specified one for something like a(2:10:5), which is
4483 identical to a(2:7:5). Only relevant for strides not equal
4484 to one. */
4485 if (ar->dimen_type[i] == DIMEN_RANGE
4486 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
4487 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0)
4489 mpz_t size, end;
4491 if (gfc_ref_dimen_size (ar, i, &size, &end) == SUCCESS)
4493 if (ar->end[i] == NULL)
4495 ar->end[i] =
4496 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4497 &ar->where);
4498 mpz_set (ar->end[i]->value.integer, end);
4500 else if (ar->end[i]->ts.type == BT_INTEGER
4501 && ar->end[i]->expr_type == EXPR_CONSTANT)
4503 mpz_set (ar->end[i]->value.integer, end);
4505 else
4506 gcc_unreachable ();
4508 mpz_clear (size);
4509 mpz_clear (end);
4514 if (ar->type == AR_FULL && ar->as->rank == 0)
4515 ar->type = AR_ELEMENT;
4517 /* If the reference type is unknown, figure out what kind it is. */
4519 if (ar->type == AR_UNKNOWN)
4521 ar->type = AR_ELEMENT;
4522 for (i = 0; i < ar->dimen; i++)
4523 if (ar->dimen_type[i] == DIMEN_RANGE
4524 || ar->dimen_type[i] == DIMEN_VECTOR)
4526 ar->type = AR_SECTION;
4527 break;
4531 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
4532 return FAILURE;
4534 return SUCCESS;
4538 static gfc_try
4539 resolve_substring (gfc_ref *ref)
4541 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4543 if (ref->u.ss.start != NULL)
4545 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4546 return FAILURE;
4548 if (ref->u.ss.start->ts.type != BT_INTEGER)
4550 gfc_error ("Substring start index at %L must be of type INTEGER",
4551 &ref->u.ss.start->where);
4552 return FAILURE;
4555 if (ref->u.ss.start->rank != 0)
4557 gfc_error ("Substring start index at %L must be scalar",
4558 &ref->u.ss.start->where);
4559 return FAILURE;
4562 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4563 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4564 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4566 gfc_error ("Substring start index at %L is less than one",
4567 &ref->u.ss.start->where);
4568 return FAILURE;
4572 if (ref->u.ss.end != NULL)
4574 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4575 return FAILURE;
4577 if (ref->u.ss.end->ts.type != BT_INTEGER)
4579 gfc_error ("Substring end index at %L must be of type INTEGER",
4580 &ref->u.ss.end->where);
4581 return FAILURE;
4584 if (ref->u.ss.end->rank != 0)
4586 gfc_error ("Substring end index at %L must be scalar",
4587 &ref->u.ss.end->where);
4588 return FAILURE;
4591 if (ref->u.ss.length != NULL
4592 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4593 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4594 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4596 gfc_error ("Substring end index at %L exceeds the string length",
4597 &ref->u.ss.start->where);
4598 return FAILURE;
4601 if (compare_bound_mpz_t (ref->u.ss.end,
4602 gfc_integer_kinds[k].huge) == CMP_GT
4603 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4604 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4606 gfc_error ("Substring end index at %L is too large",
4607 &ref->u.ss.end->where);
4608 return FAILURE;
4612 return SUCCESS;
4616 /* This function supplies missing substring charlens. */
4618 void
4619 gfc_resolve_substring_charlen (gfc_expr *e)
4621 gfc_ref *char_ref;
4622 gfc_expr *start, *end;
4624 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4625 if (char_ref->type == REF_SUBSTRING)
4626 break;
4628 if (!char_ref)
4629 return;
4631 gcc_assert (char_ref->next == NULL);
4633 if (e->ts.u.cl)
4635 if (e->ts.u.cl->length)
4636 gfc_free_expr (e->ts.u.cl->length);
4637 else if (e->expr_type == EXPR_VARIABLE
4638 && e->symtree->n.sym->attr.dummy)
4639 return;
4642 e->ts.type = BT_CHARACTER;
4643 e->ts.kind = gfc_default_character_kind;
4645 if (!e->ts.u.cl)
4646 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4648 if (char_ref->u.ss.start)
4649 start = gfc_copy_expr (char_ref->u.ss.start);
4650 else
4651 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
4653 if (char_ref->u.ss.end)
4654 end = gfc_copy_expr (char_ref->u.ss.end);
4655 else if (e->expr_type == EXPR_VARIABLE)
4656 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
4657 else
4658 end = NULL;
4660 if (!start || !end)
4661 return;
4663 /* Length = (end - start +1). */
4664 e->ts.u.cl->length = gfc_subtract (end, start);
4665 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4666 gfc_get_int_expr (gfc_default_integer_kind,
4667 NULL, 1));
4669 e->ts.u.cl->length->ts.type = BT_INTEGER;
4670 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4672 /* Make sure that the length is simplified. */
4673 gfc_simplify_expr (e->ts.u.cl->length, 1);
4674 gfc_resolve_expr (e->ts.u.cl->length);
4678 /* Resolve subtype references. */
4680 static gfc_try
4681 resolve_ref (gfc_expr *expr)
4683 int current_part_dimension, n_components, seen_part_dimension;
4684 gfc_ref *ref;
4686 for (ref = expr->ref; ref; ref = ref->next)
4687 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4689 find_array_spec (expr);
4690 break;
4693 for (ref = expr->ref; ref; ref = ref->next)
4694 switch (ref->type)
4696 case REF_ARRAY:
4697 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4698 return FAILURE;
4699 break;
4701 case REF_COMPONENT:
4702 break;
4704 case REF_SUBSTRING:
4705 resolve_substring (ref);
4706 break;
4709 /* Check constraints on part references. */
4711 current_part_dimension = 0;
4712 seen_part_dimension = 0;
4713 n_components = 0;
4715 for (ref = expr->ref; ref; ref = ref->next)
4717 switch (ref->type)
4719 case REF_ARRAY:
4720 switch (ref->u.ar.type)
4722 case AR_FULL:
4723 /* Coarray scalar. */
4724 if (ref->u.ar.as->rank == 0)
4726 current_part_dimension = 0;
4727 break;
4729 /* Fall through. */
4730 case AR_SECTION:
4731 current_part_dimension = 1;
4732 break;
4734 case AR_ELEMENT:
4735 current_part_dimension = 0;
4736 break;
4738 case AR_UNKNOWN:
4739 gfc_internal_error ("resolve_ref(): Bad array reference");
4742 break;
4744 case REF_COMPONENT:
4745 if (current_part_dimension || seen_part_dimension)
4747 /* F03:C614. */
4748 if (ref->u.c.component->attr.pointer
4749 || ref->u.c.component->attr.proc_pointer)
4751 gfc_error ("Component to the right of a part reference "
4752 "with nonzero rank must not have the POINTER "
4753 "attribute at %L", &expr->where);
4754 return FAILURE;
4756 else if (ref->u.c.component->attr.allocatable)
4758 gfc_error ("Component to the right of a part reference "
4759 "with nonzero rank must not have the ALLOCATABLE "
4760 "attribute at %L", &expr->where);
4761 return FAILURE;
4765 n_components++;
4766 break;
4768 case REF_SUBSTRING:
4769 break;
4772 if (((ref->type == REF_COMPONENT && n_components > 1)
4773 || ref->next == NULL)
4774 && current_part_dimension
4775 && seen_part_dimension)
4777 gfc_error ("Two or more part references with nonzero rank must "
4778 "not be specified at %L", &expr->where);
4779 return FAILURE;
4782 if (ref->type == REF_COMPONENT)
4784 if (current_part_dimension)
4785 seen_part_dimension = 1;
4787 /* reset to make sure */
4788 current_part_dimension = 0;
4792 return SUCCESS;
4796 /* Given an expression, determine its shape. This is easier than it sounds.
4797 Leaves the shape array NULL if it is not possible to determine the shape. */
4799 static void
4800 expression_shape (gfc_expr *e)
4802 mpz_t array[GFC_MAX_DIMENSIONS];
4803 int i;
4805 if (e->rank == 0 || e->shape != NULL)
4806 return;
4808 for (i = 0; i < e->rank; i++)
4809 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4810 goto fail;
4812 e->shape = gfc_get_shape (e->rank);
4814 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4816 return;
4818 fail:
4819 for (i--; i >= 0; i--)
4820 mpz_clear (array[i]);
4824 /* Given a variable expression node, compute the rank of the expression by
4825 examining the base symbol and any reference structures it may have. */
4827 static void
4828 expression_rank (gfc_expr *e)
4830 gfc_ref *ref;
4831 int i, rank;
4833 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4834 could lead to serious confusion... */
4835 gcc_assert (e->expr_type != EXPR_COMPCALL);
4837 if (e->ref == NULL)
4839 if (e->expr_type == EXPR_ARRAY)
4840 goto done;
4841 /* Constructors can have a rank different from one via RESHAPE(). */
4843 if (e->symtree == NULL)
4845 e->rank = 0;
4846 goto done;
4849 e->rank = (e->symtree->n.sym->as == NULL)
4850 ? 0 : e->symtree->n.sym->as->rank;
4851 goto done;
4854 rank = 0;
4856 for (ref = e->ref; ref; ref = ref->next)
4858 if (ref->type != REF_ARRAY)
4859 continue;
4861 if (ref->u.ar.type == AR_FULL)
4863 rank = ref->u.ar.as->rank;
4864 break;
4867 if (ref->u.ar.type == AR_SECTION)
4869 /* Figure out the rank of the section. */
4870 if (rank != 0)
4871 gfc_internal_error ("expression_rank(): Two array specs");
4873 for (i = 0; i < ref->u.ar.dimen; i++)
4874 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4875 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4876 rank++;
4878 break;
4882 e->rank = rank;
4884 done:
4885 expression_shape (e);
4889 /* Resolve a variable expression. */
4891 static gfc_try
4892 resolve_variable (gfc_expr *e)
4894 gfc_symbol *sym;
4895 gfc_try t;
4897 t = SUCCESS;
4899 if (e->symtree == NULL)
4900 return FAILURE;
4901 sym = e->symtree->n.sym;
4903 /* If this is an associate-name, it may be parsed with an array reference
4904 in error even though the target is scalar. Fail directly in this case. */
4905 if (sym->assoc && !sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
4906 return FAILURE;
4908 /* On the other hand, the parser may not have known this is an array;
4909 in this case, we have to add a FULL reference. */
4910 if (sym->assoc && sym->attr.dimension && !e->ref)
4912 e->ref = gfc_get_ref ();
4913 e->ref->type = REF_ARRAY;
4914 e->ref->u.ar.type = AR_FULL;
4915 e->ref->u.ar.dimen = 0;
4918 if (e->ref && resolve_ref (e) == FAILURE)
4919 return FAILURE;
4921 if (sym->attr.flavor == FL_PROCEDURE
4922 && (!sym->attr.function
4923 || (sym->attr.function && sym->result
4924 && sym->result->attr.proc_pointer
4925 && !sym->result->attr.function)))
4927 e->ts.type = BT_PROCEDURE;
4928 goto resolve_procedure;
4931 if (sym->ts.type != BT_UNKNOWN)
4932 gfc_variable_attr (e, &e->ts);
4933 else
4935 /* Must be a simple variable reference. */
4936 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
4937 return FAILURE;
4938 e->ts = sym->ts;
4941 if (check_assumed_size_reference (sym, e))
4942 return FAILURE;
4944 /* Deal with forward references to entries during resolve_code, to
4945 satisfy, at least partially, 12.5.2.5. */
4946 if (gfc_current_ns->entries
4947 && current_entry_id == sym->entry_id
4948 && cs_base
4949 && cs_base->current
4950 && cs_base->current->op != EXEC_ENTRY)
4952 gfc_entry_list *entry;
4953 gfc_formal_arglist *formal;
4954 int n;
4955 bool seen;
4957 /* If the symbol is a dummy... */
4958 if (sym->attr.dummy && sym->ns == gfc_current_ns)
4960 entry = gfc_current_ns->entries;
4961 seen = false;
4963 /* ...test if the symbol is a parameter of previous entries. */
4964 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4965 for (formal = entry->sym->formal; formal; formal = formal->next)
4967 if (formal->sym && sym->name == formal->sym->name)
4968 seen = true;
4971 /* If it has not been seen as a dummy, this is an error. */
4972 if (!seen)
4974 if (specification_expr)
4975 gfc_error ("Variable '%s', used in a specification expression"
4976 ", is referenced at %L before the ENTRY statement "
4977 "in which it is a parameter",
4978 sym->name, &cs_base->current->loc);
4979 else
4980 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4981 "statement in which it is a parameter",
4982 sym->name, &cs_base->current->loc);
4983 t = FAILURE;
4987 /* Now do the same check on the specification expressions. */
4988 specification_expr = 1;
4989 if (sym->ts.type == BT_CHARACTER
4990 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
4991 t = FAILURE;
4993 if (sym->as)
4994 for (n = 0; n < sym->as->rank; n++)
4996 specification_expr = 1;
4997 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4998 t = FAILURE;
4999 specification_expr = 1;
5000 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
5001 t = FAILURE;
5003 specification_expr = 0;
5005 if (t == SUCCESS)
5006 /* Update the symbol's entry level. */
5007 sym->entry_id = current_entry_id + 1;
5010 /* If a symbol has been host_associated mark it. This is used latter,
5011 to identify if aliasing is possible via host association. */
5012 if (sym->attr.flavor == FL_VARIABLE
5013 && gfc_current_ns->parent
5014 && (gfc_current_ns->parent == sym->ns
5015 || (gfc_current_ns->parent->parent
5016 && gfc_current_ns->parent->parent == sym->ns)))
5017 sym->attr.host_assoc = 1;
5019 resolve_procedure:
5020 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
5021 t = FAILURE;
5023 /* F2008, C617 and C1229. */
5024 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5025 && gfc_is_coindexed (e))
5027 gfc_ref *ref, *ref2 = NULL;
5029 if (e->ts.type == BT_CLASS)
5031 gfc_error ("Polymorphic subobject of coindexed object at %L",
5032 &e->where);
5033 t = FAILURE;
5036 for (ref = e->ref; ref; ref = ref->next)
5038 if (ref->type == REF_COMPONENT)
5039 ref2 = ref;
5040 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5041 break;
5044 for ( ; ref; ref = ref->next)
5045 if (ref->type == REF_COMPONENT)
5046 break;
5048 /* Expression itself is coindexed object. */
5049 if (ref == NULL)
5051 gfc_component *c;
5052 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5053 for ( ; c; c = c->next)
5054 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5056 gfc_error ("Coindexed object with polymorphic allocatable "
5057 "subcomponent at %L", &e->where);
5058 t = FAILURE;
5059 break;
5064 return t;
5068 /* Checks to see that the correct symbol has been host associated.
5069 The only situation where this arises is that in which a twice
5070 contained function is parsed after the host association is made.
5071 Therefore, on detecting this, change the symbol in the expression
5072 and convert the array reference into an actual arglist if the old
5073 symbol is a variable. */
5074 static bool
5075 check_host_association (gfc_expr *e)
5077 gfc_symbol *sym, *old_sym;
5078 gfc_symtree *st;
5079 int n;
5080 gfc_ref *ref;
5081 gfc_actual_arglist *arg, *tail = NULL;
5082 bool retval = e->expr_type == EXPR_FUNCTION;
5084 /* If the expression is the result of substitution in
5085 interface.c(gfc_extend_expr) because there is no way in
5086 which the host association can be wrong. */
5087 if (e->symtree == NULL
5088 || e->symtree->n.sym == NULL
5089 || e->user_operator)
5090 return retval;
5092 old_sym = e->symtree->n.sym;
5094 if (gfc_current_ns->parent
5095 && old_sym->ns != gfc_current_ns)
5097 /* Use the 'USE' name so that renamed module symbols are
5098 correctly handled. */
5099 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5101 if (sym && old_sym != sym
5102 && sym->ts.type == old_sym->ts.type
5103 && sym->attr.flavor == FL_PROCEDURE
5104 && sym->attr.contained)
5106 /* Clear the shape, since it might not be valid. */
5107 if (e->shape != NULL)
5109 for (n = 0; n < e->rank; n++)
5110 mpz_clear (e->shape[n]);
5112 gfc_free (e->shape);
5115 /* Give the expression the right symtree! */
5116 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5117 gcc_assert (st != NULL);
5119 if (old_sym->attr.flavor == FL_PROCEDURE
5120 || e->expr_type == EXPR_FUNCTION)
5122 /* Original was function so point to the new symbol, since
5123 the actual argument list is already attached to the
5124 expression. */
5125 e->value.function.esym = NULL;
5126 e->symtree = st;
5128 else
5130 /* Original was variable so convert array references into
5131 an actual arglist. This does not need any checking now
5132 since gfc_resolve_function will take care of it. */
5133 e->value.function.actual = NULL;
5134 e->expr_type = EXPR_FUNCTION;
5135 e->symtree = st;
5137 /* Ambiguity will not arise if the array reference is not
5138 the last reference. */
5139 for (ref = e->ref; ref; ref = ref->next)
5140 if (ref->type == REF_ARRAY && ref->next == NULL)
5141 break;
5143 gcc_assert (ref->type == REF_ARRAY);
5145 /* Grab the start expressions from the array ref and
5146 copy them into actual arguments. */
5147 for (n = 0; n < ref->u.ar.dimen; n++)
5149 arg = gfc_get_actual_arglist ();
5150 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5151 if (e->value.function.actual == NULL)
5152 tail = e->value.function.actual = arg;
5153 else
5155 tail->next = arg;
5156 tail = arg;
5160 /* Dump the reference list and set the rank. */
5161 gfc_free_ref_list (e->ref);
5162 e->ref = NULL;
5163 e->rank = sym->as ? sym->as->rank : 0;
5166 gfc_resolve_expr (e);
5167 sym->refs++;
5170 /* This might have changed! */
5171 return e->expr_type == EXPR_FUNCTION;
5175 static void
5176 gfc_resolve_character_operator (gfc_expr *e)
5178 gfc_expr *op1 = e->value.op.op1;
5179 gfc_expr *op2 = e->value.op.op2;
5180 gfc_expr *e1 = NULL;
5181 gfc_expr *e2 = NULL;
5183 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
5185 if (op1->ts.u.cl && op1->ts.u.cl->length)
5186 e1 = gfc_copy_expr (op1->ts.u.cl->length);
5187 else if (op1->expr_type == EXPR_CONSTANT)
5188 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5189 op1->value.character.length);
5191 if (op2->ts.u.cl && op2->ts.u.cl->length)
5192 e2 = gfc_copy_expr (op2->ts.u.cl->length);
5193 else if (op2->expr_type == EXPR_CONSTANT)
5194 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5195 op2->value.character.length);
5197 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5199 if (!e1 || !e2)
5200 return;
5202 e->ts.u.cl->length = gfc_add (e1, e2);
5203 e->ts.u.cl->length->ts.type = BT_INTEGER;
5204 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5205 gfc_simplify_expr (e->ts.u.cl->length, 0);
5206 gfc_resolve_expr (e->ts.u.cl->length);
5208 return;
5212 /* Ensure that an character expression has a charlen and, if possible, a
5213 length expression. */
5215 static void
5216 fixup_charlen (gfc_expr *e)
5218 /* The cases fall through so that changes in expression type and the need
5219 for multiple fixes are picked up. In all circumstances, a charlen should
5220 be available for the middle end to hang a backend_decl on. */
5221 switch (e->expr_type)
5223 case EXPR_OP:
5224 gfc_resolve_character_operator (e);
5226 case EXPR_ARRAY:
5227 if (e->expr_type == EXPR_ARRAY)
5228 gfc_resolve_character_array_constructor (e);
5230 case EXPR_SUBSTRING:
5231 if (!e->ts.u.cl && e->ref)
5232 gfc_resolve_substring_charlen (e);
5234 default:
5235 if (!e->ts.u.cl)
5236 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5238 break;
5243 /* Update an actual argument to include the passed-object for type-bound
5244 procedures at the right position. */
5246 static gfc_actual_arglist*
5247 update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5248 const char *name)
5250 gcc_assert (argpos > 0);
5252 if (argpos == 1)
5254 gfc_actual_arglist* result;
5256 result = gfc_get_actual_arglist ();
5257 result->expr = po;
5258 result->next = lst;
5259 if (name)
5260 result->name = name;
5262 return result;
5265 if (lst)
5266 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5267 else
5268 lst = update_arglist_pass (NULL, po, argpos - 1, name);
5269 return lst;
5273 /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
5275 static gfc_expr*
5276 extract_compcall_passed_object (gfc_expr* e)
5278 gfc_expr* po;
5280 gcc_assert (e->expr_type == EXPR_COMPCALL);
5282 if (e->value.compcall.base_object)
5283 po = gfc_copy_expr (e->value.compcall.base_object);
5284 else
5286 po = gfc_get_expr ();
5287 po->expr_type = EXPR_VARIABLE;
5288 po->symtree = e->symtree;
5289 po->ref = gfc_copy_ref (e->ref);
5290 po->where = e->where;
5293 if (gfc_resolve_expr (po) == FAILURE)
5294 return NULL;
5296 return po;
5300 /* Update the arglist of an EXPR_COMPCALL expression to include the
5301 passed-object. */
5303 static gfc_try
5304 update_compcall_arglist (gfc_expr* e)
5306 gfc_expr* po;
5307 gfc_typebound_proc* tbp;
5309 tbp = e->value.compcall.tbp;
5311 if (tbp->error)
5312 return FAILURE;
5314 po = extract_compcall_passed_object (e);
5315 if (!po)
5316 return FAILURE;
5318 if (tbp->nopass || e->value.compcall.ignore_pass)
5320 gfc_free_expr (po);
5321 return SUCCESS;
5324 gcc_assert (tbp->pass_arg_num > 0);
5325 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5326 tbp->pass_arg_num,
5327 tbp->pass_arg);
5329 return SUCCESS;
5333 /* Extract the passed object from a PPC call (a copy of it). */
5335 static gfc_expr*
5336 extract_ppc_passed_object (gfc_expr *e)
5338 gfc_expr *po;
5339 gfc_ref **ref;
5341 po = gfc_get_expr ();
5342 po->expr_type = EXPR_VARIABLE;
5343 po->symtree = e->symtree;
5344 po->ref = gfc_copy_ref (e->ref);
5345 po->where = e->where;
5347 /* Remove PPC reference. */
5348 ref = &po->ref;
5349 while ((*ref)->next)
5350 ref = &(*ref)->next;
5351 gfc_free_ref_list (*ref);
5352 *ref = NULL;
5354 if (gfc_resolve_expr (po) == FAILURE)
5355 return NULL;
5357 return po;
5361 /* Update the actual arglist of a procedure pointer component to include the
5362 passed-object. */
5364 static gfc_try
5365 update_ppc_arglist (gfc_expr* e)
5367 gfc_expr* po;
5368 gfc_component *ppc;
5369 gfc_typebound_proc* tb;
5371 if (!gfc_is_proc_ptr_comp (e, &ppc))
5372 return FAILURE;
5374 tb = ppc->tb;
5376 if (tb->error)
5377 return FAILURE;
5378 else if (tb->nopass)
5379 return SUCCESS;
5381 po = extract_ppc_passed_object (e);
5382 if (!po)
5383 return FAILURE;
5385 if (po->rank > 0)
5387 gfc_error ("Passed-object at %L must be scalar", &e->where);
5388 return FAILURE;
5391 gcc_assert (tb->pass_arg_num > 0);
5392 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5393 tb->pass_arg_num,
5394 tb->pass_arg);
5396 return SUCCESS;
5400 /* Check that the object a TBP is called on is valid, i.e. it must not be
5401 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5403 static gfc_try
5404 check_typebound_baseobject (gfc_expr* e)
5406 gfc_expr* base;
5407 gfc_try return_value = FAILURE;
5409 base = extract_compcall_passed_object (e);
5410 if (!base)
5411 return FAILURE;
5413 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
5415 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
5417 gfc_error ("Base object for type-bound procedure call at %L is of"
5418 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
5419 goto cleanup;
5422 /* If the procedure called is NOPASS, the base object must be scalar. */
5423 if (e->value.compcall.tbp->nopass && base->rank > 0)
5425 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5426 " be scalar", &e->where);
5427 goto cleanup;
5430 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
5431 if (base->rank > 0)
5433 gfc_error ("Non-scalar base object at %L currently not implemented",
5434 &e->where);
5435 goto cleanup;
5438 return_value = SUCCESS;
5440 cleanup:
5441 gfc_free_expr (base);
5442 return return_value;
5446 /* Resolve a call to a type-bound procedure, either function or subroutine,
5447 statically from the data in an EXPR_COMPCALL expression. The adapted
5448 arglist and the target-procedure symtree are returned. */
5450 static gfc_try
5451 resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5452 gfc_actual_arglist** actual)
5454 gcc_assert (e->expr_type == EXPR_COMPCALL);
5455 gcc_assert (!e->value.compcall.tbp->is_generic);
5457 /* Update the actual arglist for PASS. */
5458 if (update_compcall_arglist (e) == FAILURE)
5459 return FAILURE;
5461 *actual = e->value.compcall.actual;
5462 *target = e->value.compcall.tbp->u.specific;
5464 gfc_free_ref_list (e->ref);
5465 e->ref = NULL;
5466 e->value.compcall.actual = NULL;
5468 return SUCCESS;
5472 /* Get the ultimate declared type from an expression. In addition,
5473 return the last class/derived type reference and the copy of the
5474 reference list. */
5475 static gfc_symbol*
5476 get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5477 gfc_expr *e)
5479 gfc_symbol *declared;
5480 gfc_ref *ref;
5482 declared = NULL;
5483 if (class_ref)
5484 *class_ref = NULL;
5485 if (new_ref)
5486 *new_ref = gfc_copy_ref (e->ref);
5488 for (ref = e->ref; ref; ref = ref->next)
5490 if (ref->type != REF_COMPONENT)
5491 continue;
5493 if (ref->u.c.component->ts.type == BT_CLASS
5494 || ref->u.c.component->ts.type == BT_DERIVED)
5496 declared = ref->u.c.component->ts.u.derived;
5497 if (class_ref)
5498 *class_ref = ref;
5502 if (declared == NULL)
5503 declared = e->symtree->n.sym->ts.u.derived;
5505 return declared;
5509 /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5510 which of the specific bindings (if any) matches the arglist and transform
5511 the expression into a call of that binding. */
5513 static gfc_try
5514 resolve_typebound_generic_call (gfc_expr* e, const char **name)
5516 gfc_typebound_proc* genproc;
5517 const char* genname;
5518 gfc_symtree *st;
5519 gfc_symbol *derived;
5521 gcc_assert (e->expr_type == EXPR_COMPCALL);
5522 genname = e->value.compcall.name;
5523 genproc = e->value.compcall.tbp;
5525 if (!genproc->is_generic)
5526 return SUCCESS;
5528 /* Try the bindings on this type and in the inheritance hierarchy. */
5529 for (; genproc; genproc = genproc->overridden)
5531 gfc_tbp_generic* g;
5533 gcc_assert (genproc->is_generic);
5534 for (g = genproc->u.generic; g; g = g->next)
5536 gfc_symbol* target;
5537 gfc_actual_arglist* args;
5538 bool matches;
5540 gcc_assert (g->specific);
5542 if (g->specific->error)
5543 continue;
5545 target = g->specific->u.specific->n.sym;
5547 /* Get the right arglist by handling PASS/NOPASS. */
5548 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5549 if (!g->specific->nopass)
5551 gfc_expr* po;
5552 po = extract_compcall_passed_object (e);
5553 if (!po)
5554 return FAILURE;
5556 gcc_assert (g->specific->pass_arg_num > 0);
5557 gcc_assert (!g->specific->error);
5558 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5559 g->specific->pass_arg);
5561 resolve_actual_arglist (args, target->attr.proc,
5562 is_external_proc (target) && !target->formal);
5564 /* Check if this arglist matches the formal. */
5565 matches = gfc_arglist_matches_symbol (&args, target);
5567 /* Clean up and break out of the loop if we've found it. */
5568 gfc_free_actual_arglist (args);
5569 if (matches)
5571 e->value.compcall.tbp = g->specific;
5572 genname = g->specific_st->name;
5573 /* Pass along the name for CLASS methods, where the vtab
5574 procedure pointer component has to be referenced. */
5575 if (name)
5576 *name = genname;
5577 goto success;
5582 /* Nothing matching found! */
5583 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5584 " '%s' at %L", genname, &e->where);
5585 return FAILURE;
5587 success:
5588 /* Make sure that we have the right specific instance for the name. */
5589 derived = get_declared_from_expr (NULL, NULL, e);
5591 st = gfc_find_typebound_proc (derived, NULL, genname, false, &e->where);
5592 if (st)
5593 e->value.compcall.tbp = st->n.tb;
5595 return SUCCESS;
5599 /* Resolve a call to a type-bound subroutine. */
5601 static gfc_try
5602 resolve_typebound_call (gfc_code* c, const char **name)
5604 gfc_actual_arglist* newactual;
5605 gfc_symtree* target;
5607 /* Check that's really a SUBROUTINE. */
5608 if (!c->expr1->value.compcall.tbp->subroutine)
5610 gfc_error ("'%s' at %L should be a SUBROUTINE",
5611 c->expr1->value.compcall.name, &c->loc);
5612 return FAILURE;
5615 if (check_typebound_baseobject (c->expr1) == FAILURE)
5616 return FAILURE;
5618 /* Pass along the name for CLASS methods, where the vtab
5619 procedure pointer component has to be referenced. */
5620 if (name)
5621 *name = c->expr1->value.compcall.name;
5623 if (resolve_typebound_generic_call (c->expr1, name) == FAILURE)
5624 return FAILURE;
5626 /* Transform into an ordinary EXEC_CALL for now. */
5628 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
5629 return FAILURE;
5631 c->ext.actual = newactual;
5632 c->symtree = target;
5633 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
5635 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
5637 gfc_free_expr (c->expr1);
5638 c->expr1 = gfc_get_expr ();
5639 c->expr1->expr_type = EXPR_FUNCTION;
5640 c->expr1->symtree = target;
5641 c->expr1->where = c->loc;
5643 return resolve_call (c);
5647 /* Resolve a component-call expression. */
5648 static gfc_try
5649 resolve_compcall (gfc_expr* e, const char **name)
5651 gfc_actual_arglist* newactual;
5652 gfc_symtree* target;
5654 /* Check that's really a FUNCTION. */
5655 if (!e->value.compcall.tbp->function)
5657 gfc_error ("'%s' at %L should be a FUNCTION",
5658 e->value.compcall.name, &e->where);
5659 return FAILURE;
5662 /* These must not be assign-calls! */
5663 gcc_assert (!e->value.compcall.assign);
5665 if (check_typebound_baseobject (e) == FAILURE)
5666 return FAILURE;
5668 /* Pass along the name for CLASS methods, where the vtab
5669 procedure pointer component has to be referenced. */
5670 if (name)
5671 *name = e->value.compcall.name;
5673 if (resolve_typebound_generic_call (e, name) == FAILURE)
5674 return FAILURE;
5675 gcc_assert (!e->value.compcall.tbp->is_generic);
5677 /* Take the rank from the function's symbol. */
5678 if (e->value.compcall.tbp->u.specific->n.sym->as)
5679 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
5681 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
5682 arglist to the TBP's binding target. */
5684 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5685 return FAILURE;
5687 e->value.function.actual = newactual;
5688 e->value.function.name = NULL;
5689 e->value.function.esym = target->n.sym;
5690 e->value.function.isym = NULL;
5691 e->symtree = target;
5692 e->ts = target->n.sym->ts;
5693 e->expr_type = EXPR_FUNCTION;
5695 /* Resolution is not necessary if this is a class subroutine; this
5696 function only has to identify the specific proc. Resolution of
5697 the call will be done next in resolve_typebound_call. */
5698 return gfc_resolve_expr (e);
5703 /* Resolve a typebound function, or 'method'. First separate all
5704 the non-CLASS references by calling resolve_compcall directly. */
5706 static gfc_try
5707 resolve_typebound_function (gfc_expr* e)
5709 gfc_symbol *declared;
5710 gfc_component *c;
5711 gfc_ref *new_ref;
5712 gfc_ref *class_ref;
5713 gfc_symtree *st;
5714 const char *name;
5715 gfc_typespec ts;
5716 gfc_expr *expr;
5718 st = e->symtree;
5720 /* Deal with typebound operators for CLASS objects. */
5721 expr = e->value.compcall.base_object;
5722 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
5724 /* Since the typebound operators are generic, we have to ensure
5725 that any delays in resolution are corrected and that the vtab
5726 is present. */
5727 ts = expr->ts;
5728 declared = ts.u.derived;
5729 c = gfc_find_component (declared, "$vptr", true, true);
5730 if (c->ts.u.derived == NULL)
5731 c->ts.u.derived = gfc_find_derived_vtab (declared);
5733 if (resolve_compcall (e, &name) == FAILURE)
5734 return FAILURE;
5736 /* Use the generic name if it is there. */
5737 name = name ? name : e->value.function.esym->name;
5738 e->symtree = expr->symtree;
5739 e->ref = gfc_copy_ref (expr->ref);
5740 gfc_add_component_ref (e, "$vptr");
5741 gfc_add_component_ref (e, name);
5742 e->value.function.esym = NULL;
5743 return SUCCESS;
5746 if (st == NULL)
5747 return resolve_compcall (e, NULL);
5749 if (resolve_ref (e) == FAILURE)
5750 return FAILURE;
5752 /* Get the CLASS declared type. */
5753 declared = get_declared_from_expr (&class_ref, &new_ref, e);
5755 /* Weed out cases of the ultimate component being a derived type. */
5756 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5757 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5759 gfc_free_ref_list (new_ref);
5760 return resolve_compcall (e, NULL);
5763 c = gfc_find_component (declared, "$data", true, true);
5764 declared = c->ts.u.derived;
5766 /* Treat the call as if it is a typebound procedure, in order to roll
5767 out the correct name for the specific function. */
5768 if (resolve_compcall (e, &name) == FAILURE)
5769 return FAILURE;
5770 ts = e->ts;
5772 /* Then convert the expression to a procedure pointer component call. */
5773 e->value.function.esym = NULL;
5774 e->symtree = st;
5776 if (new_ref)
5777 e->ref = new_ref;
5779 /* '$vptr' points to the vtab, which contains the procedure pointers. */
5780 gfc_add_component_ref (e, "$vptr");
5781 gfc_add_component_ref (e, name);
5783 /* Recover the typespec for the expression. This is really only
5784 necessary for generic procedures, where the additional call
5785 to gfc_add_component_ref seems to throw the collection of the
5786 correct typespec. */
5787 e->ts = ts;
5788 return SUCCESS;
5791 /* Resolve a typebound subroutine, or 'method'. First separate all
5792 the non-CLASS references by calling resolve_typebound_call
5793 directly. */
5795 static gfc_try
5796 resolve_typebound_subroutine (gfc_code *code)
5798 gfc_symbol *declared;
5799 gfc_component *c;
5800 gfc_ref *new_ref;
5801 gfc_ref *class_ref;
5802 gfc_symtree *st;
5803 const char *name;
5804 gfc_typespec ts;
5805 gfc_expr *expr;
5807 st = code->expr1->symtree;
5809 /* Deal with typebound operators for CLASS objects. */
5810 expr = code->expr1->value.compcall.base_object;
5811 if (expr && expr->symtree->n.sym->ts.type == BT_CLASS
5812 && code->expr1->value.compcall.name)
5814 /* Since the typebound operators are generic, we have to ensure
5815 that any delays in resolution are corrected and that the vtab
5816 is present. */
5817 ts = expr->symtree->n.sym->ts;
5818 declared = ts.u.derived;
5819 c = gfc_find_component (declared, "$vptr", true, true);
5820 if (c->ts.u.derived == NULL)
5821 c->ts.u.derived = gfc_find_derived_vtab (declared);
5823 if (resolve_typebound_call (code, &name) == FAILURE)
5824 return FAILURE;
5826 /* Use the generic name if it is there. */
5827 name = name ? name : code->expr1->value.function.esym->name;
5828 code->expr1->symtree = expr->symtree;
5829 expr->symtree->n.sym->ts.u.derived = declared;
5830 gfc_add_component_ref (code->expr1, "$vptr");
5831 gfc_add_component_ref (code->expr1, name);
5832 code->expr1->value.function.esym = NULL;
5833 return SUCCESS;
5836 if (st == NULL)
5837 return resolve_typebound_call (code, NULL);
5839 if (resolve_ref (code->expr1) == FAILURE)
5840 return FAILURE;
5842 /* Get the CLASS declared type. */
5843 get_declared_from_expr (&class_ref, &new_ref, code->expr1);
5845 /* Weed out cases of the ultimate component being a derived type. */
5846 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5847 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
5849 gfc_free_ref_list (new_ref);
5850 return resolve_typebound_call (code, NULL);
5853 if (resolve_typebound_call (code, &name) == FAILURE)
5854 return FAILURE;
5855 ts = code->expr1->ts;
5857 /* Then convert the expression to a procedure pointer component call. */
5858 code->expr1->value.function.esym = NULL;
5859 code->expr1->symtree = st;
5861 if (new_ref)
5862 code->expr1->ref = new_ref;
5864 /* '$vptr' points to the vtab, which contains the procedure pointers. */
5865 gfc_add_component_ref (code->expr1, "$vptr");
5866 gfc_add_component_ref (code->expr1, name);
5868 /* Recover the typespec for the expression. This is really only
5869 necessary for generic procedures, where the additional call
5870 to gfc_add_component_ref seems to throw the collection of the
5871 correct typespec. */
5872 code->expr1->ts = ts;
5873 return SUCCESS;
5877 /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5879 static gfc_try
5880 resolve_ppc_call (gfc_code* c)
5882 gfc_component *comp;
5883 bool b;
5885 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5886 gcc_assert (b);
5888 c->resolved_sym = c->expr1->symtree->n.sym;
5889 c->expr1->expr_type = EXPR_VARIABLE;
5891 if (!comp->attr.subroutine)
5892 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
5894 if (resolve_ref (c->expr1) == FAILURE)
5895 return FAILURE;
5897 if (update_ppc_arglist (c->expr1) == FAILURE)
5898 return FAILURE;
5900 c->ext.actual = c->expr1->value.compcall.actual;
5902 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5903 comp->formal == NULL) == FAILURE)
5904 return FAILURE;
5906 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
5908 return SUCCESS;
5912 /* Resolve a Function Call to a Procedure Pointer Component (Function). */
5914 static gfc_try
5915 resolve_expr_ppc (gfc_expr* e)
5917 gfc_component *comp;
5918 bool b;
5920 b = gfc_is_proc_ptr_comp (e, &comp);
5921 gcc_assert (b);
5923 /* Convert to EXPR_FUNCTION. */
5924 e->expr_type = EXPR_FUNCTION;
5925 e->value.function.isym = NULL;
5926 e->value.function.actual = e->value.compcall.actual;
5927 e->ts = comp->ts;
5928 if (comp->as != NULL)
5929 e->rank = comp->as->rank;
5931 if (!comp->attr.function)
5932 gfc_add_function (&comp->attr, comp->name, &e->where);
5934 if (resolve_ref (e) == FAILURE)
5935 return FAILURE;
5937 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5938 comp->formal == NULL) == FAILURE)
5939 return FAILURE;
5941 if (update_ppc_arglist (e) == FAILURE)
5942 return FAILURE;
5944 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
5946 return SUCCESS;
5950 static bool
5951 gfc_is_expandable_expr (gfc_expr *e)
5953 gfc_constructor *con;
5955 if (e->expr_type == EXPR_ARRAY)
5957 /* Traverse the constructor looking for variables that are flavor
5958 parameter. Parameters must be expanded since they are fully used at
5959 compile time. */
5960 con = gfc_constructor_first (e->value.constructor);
5961 for (; con; con = gfc_constructor_next (con))
5963 if (con->expr->expr_type == EXPR_VARIABLE
5964 && con->expr->symtree
5965 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5966 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5967 return true;
5968 if (con->expr->expr_type == EXPR_ARRAY
5969 && gfc_is_expandable_expr (con->expr))
5970 return true;
5974 return false;
5977 /* Resolve an expression. That is, make sure that types of operands agree
5978 with their operators, intrinsic operators are converted to function calls
5979 for overloaded types and unresolved function references are resolved. */
5981 gfc_try
5982 gfc_resolve_expr (gfc_expr *e)
5984 gfc_try t;
5985 bool inquiry_save;
5987 if (e == NULL)
5988 return SUCCESS;
5990 /* inquiry_argument only applies to variables. */
5991 inquiry_save = inquiry_argument;
5992 if (e->expr_type != EXPR_VARIABLE)
5993 inquiry_argument = false;
5995 switch (e->expr_type)
5997 case EXPR_OP:
5998 t = resolve_operator (e);
5999 break;
6001 case EXPR_FUNCTION:
6002 case EXPR_VARIABLE:
6004 if (check_host_association (e))
6005 t = resolve_function (e);
6006 else
6008 t = resolve_variable (e);
6009 if (t == SUCCESS)
6010 expression_rank (e);
6013 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
6014 && e->ref->type != REF_SUBSTRING)
6015 gfc_resolve_substring_charlen (e);
6017 break;
6019 case EXPR_COMPCALL:
6020 t = resolve_typebound_function (e);
6021 break;
6023 case EXPR_SUBSTRING:
6024 t = resolve_ref (e);
6025 break;
6027 case EXPR_CONSTANT:
6028 case EXPR_NULL:
6029 t = SUCCESS;
6030 break;
6032 case EXPR_PPC:
6033 t = resolve_expr_ppc (e);
6034 break;
6036 case EXPR_ARRAY:
6037 t = FAILURE;
6038 if (resolve_ref (e) == FAILURE)
6039 break;
6041 t = gfc_resolve_array_constructor (e);
6042 /* Also try to expand a constructor. */
6043 if (t == SUCCESS)
6045 expression_rank (e);
6046 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
6047 gfc_expand_constructor (e, false);
6050 /* This provides the opportunity for the length of constructors with
6051 character valued function elements to propagate the string length
6052 to the expression. */
6053 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
6055 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
6056 here rather then add a duplicate test for it above. */
6057 gfc_expand_constructor (e, false);
6058 t = gfc_resolve_character_array_constructor (e);
6061 break;
6063 case EXPR_STRUCTURE:
6064 t = resolve_ref (e);
6065 if (t == FAILURE)
6066 break;
6068 t = resolve_structure_cons (e, 0);
6069 if (t == FAILURE)
6070 break;
6072 t = gfc_simplify_expr (e, 0);
6073 break;
6075 default:
6076 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6079 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
6080 fixup_charlen (e);
6082 inquiry_argument = inquiry_save;
6084 return t;
6088 /* Resolve an expression from an iterator. They must be scalar and have
6089 INTEGER or (optionally) REAL type. */
6091 static gfc_try
6092 gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6093 const char *name_msgid)
6095 if (gfc_resolve_expr (expr) == FAILURE)
6096 return FAILURE;
6098 if (expr->rank != 0)
6100 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6101 return FAILURE;
6104 if (expr->ts.type != BT_INTEGER)
6106 if (expr->ts.type == BT_REAL)
6108 if (real_ok)
6109 return gfc_notify_std (GFC_STD_F95_DEL,
6110 "Deleted feature: %s at %L must be integer",
6111 _(name_msgid), &expr->where);
6112 else
6114 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6115 &expr->where);
6116 return FAILURE;
6119 else
6121 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
6122 return FAILURE;
6125 return SUCCESS;
6129 /* Resolve the expressions in an iterator structure. If REAL_OK is
6130 false allow only INTEGER type iterators, otherwise allow REAL types. */
6132 gfc_try
6133 gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
6135 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
6136 == FAILURE)
6137 return FAILURE;
6139 if (gfc_check_vardef_context (iter->var, false, _("iterator variable"))
6140 == FAILURE)
6141 return FAILURE;
6143 if (gfc_resolve_iterator_expr (iter->start, real_ok,
6144 "Start expression in DO loop") == FAILURE)
6145 return FAILURE;
6147 if (gfc_resolve_iterator_expr (iter->end, real_ok,
6148 "End expression in DO loop") == FAILURE)
6149 return FAILURE;
6151 if (gfc_resolve_iterator_expr (iter->step, real_ok,
6152 "Step expression in DO loop") == FAILURE)
6153 return FAILURE;
6155 if (iter->step->expr_type == EXPR_CONSTANT)
6157 if ((iter->step->ts.type == BT_INTEGER
6158 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6159 || (iter->step->ts.type == BT_REAL
6160 && mpfr_sgn (iter->step->value.real) == 0))
6162 gfc_error ("Step expression in DO loop at %L cannot be zero",
6163 &iter->step->where);
6164 return FAILURE;
6168 /* Convert start, end, and step to the same type as var. */
6169 if (iter->start->ts.kind != iter->var->ts.kind
6170 || iter->start->ts.type != iter->var->ts.type)
6171 gfc_convert_type (iter->start, &iter->var->ts, 2);
6173 if (iter->end->ts.kind != iter->var->ts.kind
6174 || iter->end->ts.type != iter->var->ts.type)
6175 gfc_convert_type (iter->end, &iter->var->ts, 2);
6177 if (iter->step->ts.kind != iter->var->ts.kind
6178 || iter->step->ts.type != iter->var->ts.type)
6179 gfc_convert_type (iter->step, &iter->var->ts, 2);
6181 if (iter->start->expr_type == EXPR_CONSTANT
6182 && iter->end->expr_type == EXPR_CONSTANT
6183 && iter->step->expr_type == EXPR_CONSTANT)
6185 int sgn, cmp;
6186 if (iter->start->ts.type == BT_INTEGER)
6188 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6189 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6191 else
6193 sgn = mpfr_sgn (iter->step->value.real);
6194 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6196 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
6197 gfc_warning ("DO loop at %L will be executed zero times",
6198 &iter->step->where);
6201 return SUCCESS;
6205 /* Traversal function for find_forall_index. f == 2 signals that
6206 that variable itself is not to be checked - only the references. */
6208 static bool
6209 forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
6211 if (expr->expr_type != EXPR_VARIABLE)
6212 return false;
6214 /* A scalar assignment */
6215 if (!expr->ref || *f == 1)
6217 if (expr->symtree->n.sym == sym)
6218 return true;
6219 else
6220 return false;
6223 if (*f == 2)
6224 *f = 1;
6225 return false;
6229 /* Check whether the FORALL index appears in the expression or not.
6230 Returns SUCCESS if SYM is found in EXPR. */
6232 gfc_try
6233 find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6235 if (gfc_traverse_expr (expr, sym, forall_index, f))
6236 return SUCCESS;
6237 else
6238 return FAILURE;
6242 /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6243 to be a scalar INTEGER variable. The subscripts and stride are scalar
6244 INTEGERs, and if stride is a constant it must be nonzero.
6245 Furthermore "A subscript or stride in a forall-triplet-spec shall
6246 not contain a reference to any index-name in the
6247 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6249 static void
6250 resolve_forall_iterators (gfc_forall_iterator *it)
6252 gfc_forall_iterator *iter, *iter2;
6254 for (iter = it; iter; iter = iter->next)
6256 if (gfc_resolve_expr (iter->var) == SUCCESS
6257 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6258 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6259 &iter->var->where);
6261 if (gfc_resolve_expr (iter->start) == SUCCESS
6262 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6263 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6264 &iter->start->where);
6265 if (iter->var->ts.kind != iter->start->ts.kind)
6266 gfc_convert_type (iter->start, &iter->var->ts, 2);
6268 if (gfc_resolve_expr (iter->end) == SUCCESS
6269 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6270 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6271 &iter->end->where);
6272 if (iter->var->ts.kind != iter->end->ts.kind)
6273 gfc_convert_type (iter->end, &iter->var->ts, 2);
6275 if (gfc_resolve_expr (iter->stride) == SUCCESS)
6277 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6278 gfc_error ("FORALL stride expression at %L must be a scalar %s",
6279 &iter->stride->where, "INTEGER");
6281 if (iter->stride->expr_type == EXPR_CONSTANT
6282 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
6283 gfc_error ("FORALL stride expression at %L cannot be zero",
6284 &iter->stride->where);
6286 if (iter->var->ts.kind != iter->stride->ts.kind)
6287 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6290 for (iter = it; iter; iter = iter->next)
6291 for (iter2 = iter; iter2; iter2 = iter2->next)
6293 if (find_forall_index (iter2->start,
6294 iter->var->symtree->n.sym, 0) == SUCCESS
6295 || find_forall_index (iter2->end,
6296 iter->var->symtree->n.sym, 0) == SUCCESS
6297 || find_forall_index (iter2->stride,
6298 iter->var->symtree->n.sym, 0) == SUCCESS)
6299 gfc_error ("FORALL index '%s' may not appear in triplet "
6300 "specification at %L", iter->var->symtree->name,
6301 &iter2->start->where);
6306 /* Given a pointer to a symbol that is a derived type, see if it's
6307 inaccessible, i.e. if it's defined in another module and the components are
6308 PRIVATE. The search is recursive if necessary. Returns zero if no
6309 inaccessible components are found, nonzero otherwise. */
6311 static int
6312 derived_inaccessible (gfc_symbol *sym)
6314 gfc_component *c;
6316 if (sym->attr.use_assoc && sym->attr.private_comp)
6317 return 1;
6319 for (c = sym->components; c; c = c->next)
6321 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
6322 return 1;
6325 return 0;
6329 /* Resolve the argument of a deallocate expression. The expression must be
6330 a pointer or a full array. */
6332 static gfc_try
6333 resolve_deallocate_expr (gfc_expr *e)
6335 symbol_attribute attr;
6336 int allocatable, pointer;
6337 gfc_ref *ref;
6338 gfc_symbol *sym;
6339 gfc_component *c;
6341 if (gfc_resolve_expr (e) == FAILURE)
6342 return FAILURE;
6344 if (e->expr_type != EXPR_VARIABLE)
6345 goto bad;
6347 sym = e->symtree->n.sym;
6349 if (sym->ts.type == BT_CLASS)
6351 allocatable = CLASS_DATA (sym)->attr.allocatable;
6352 pointer = CLASS_DATA (sym)->attr.class_pointer;
6354 else
6356 allocatable = sym->attr.allocatable;
6357 pointer = sym->attr.pointer;
6359 for (ref = e->ref; ref; ref = ref->next)
6361 switch (ref->type)
6363 case REF_ARRAY:
6364 if (ref->u.ar.type != AR_FULL)
6365 allocatable = 0;
6366 break;
6368 case REF_COMPONENT:
6369 c = ref->u.c.component;
6370 if (c->ts.type == BT_CLASS)
6372 allocatable = CLASS_DATA (c)->attr.allocatable;
6373 pointer = CLASS_DATA (c)->attr.class_pointer;
6375 else
6377 allocatable = c->attr.allocatable;
6378 pointer = c->attr.pointer;
6380 break;
6382 case REF_SUBSTRING:
6383 allocatable = 0;
6384 break;
6388 attr = gfc_expr_attr (e);
6390 if (allocatable == 0 && attr.pointer == 0)
6392 bad:
6393 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6394 &e->where);
6395 return FAILURE;
6398 if (pointer
6399 && gfc_check_vardef_context (e, true, _("DEALLOCATE object")) == FAILURE)
6400 return FAILURE;
6401 if (gfc_check_vardef_context (e, false, _("DEALLOCATE object")) == FAILURE)
6402 return FAILURE;
6404 if (e->ts.type == BT_CLASS)
6406 /* Only deallocate the DATA component. */
6407 gfc_add_component_ref (e, "$data");
6410 return SUCCESS;
6414 /* Returns true if the expression e contains a reference to the symbol sym. */
6415 static bool
6416 sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6418 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6419 return true;
6421 return false;
6424 bool
6425 gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
6427 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
6431 /* Given the expression node e for an allocatable/pointer of derived type to be
6432 allocated, get the expression node to be initialized afterwards (needed for
6433 derived types with default initializers, and derived types with allocatable
6434 components that need nullification.) */
6436 gfc_expr *
6437 gfc_expr_to_initialize (gfc_expr *e)
6439 gfc_expr *result;
6440 gfc_ref *ref;
6441 int i;
6443 result = gfc_copy_expr (e);
6445 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6446 for (ref = result->ref; ref; ref = ref->next)
6447 if (ref->type == REF_ARRAY && ref->next == NULL)
6449 ref->u.ar.type = AR_FULL;
6451 for (i = 0; i < ref->u.ar.dimen; i++)
6452 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
6454 result->rank = ref->u.ar.dimen;
6455 break;
6458 return result;
6462 /* If the last ref of an expression is an array ref, return a copy of the
6463 expression with that one removed. Otherwise, a copy of the original
6464 expression. This is used for allocate-expressions and pointer assignment
6465 LHS, where there may be an array specification that needs to be stripped
6466 off when using gfc_check_vardef_context. */
6468 static gfc_expr*
6469 remove_last_array_ref (gfc_expr* e)
6471 gfc_expr* e2;
6472 gfc_ref** r;
6474 e2 = gfc_copy_expr (e);
6475 for (r = &e2->ref; *r; r = &(*r)->next)
6476 if ((*r)->type == REF_ARRAY && !(*r)->next)
6478 gfc_free_ref_list (*r);
6479 *r = NULL;
6480 break;
6483 return e2;
6487 /* Used in resolve_allocate_expr to check that a allocation-object and
6488 a source-expr are conformable. This does not catch all possible
6489 cases; in particular a runtime checking is needed. */
6491 static gfc_try
6492 conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6494 gfc_ref *tail;
6495 for (tail = e2->ref; tail && tail->next; tail = tail->next);
6497 /* First compare rank. */
6498 if (tail && e1->rank != tail->u.ar.as->rank)
6500 gfc_error ("Source-expr at %L must be scalar or have the "
6501 "same rank as the allocate-object at %L",
6502 &e1->where, &e2->where);
6503 return FAILURE;
6506 if (e1->shape)
6508 int i;
6509 mpz_t s;
6511 mpz_init (s);
6513 for (i = 0; i < e1->rank; i++)
6515 if (tail->u.ar.end[i])
6517 mpz_set (s, tail->u.ar.end[i]->value.integer);
6518 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
6519 mpz_add_ui (s, s, 1);
6521 else
6523 mpz_set (s, tail->u.ar.start[i]->value.integer);
6526 if (mpz_cmp (e1->shape[i], s) != 0)
6528 gfc_error ("Source-expr at %L and allocate-object at %L must "
6529 "have the same shape", &e1->where, &e2->where);
6530 mpz_clear (s);
6531 return FAILURE;
6535 mpz_clear (s);
6538 return SUCCESS;
6542 /* Resolve the expression in an ALLOCATE statement, doing the additional
6543 checks to see whether the expression is OK or not. The expression must
6544 have a trailing array reference that gives the size of the array. */
6546 static gfc_try
6547 resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6549 int i, pointer, allocatable, dimension, is_abstract;
6550 int codimension;
6551 symbol_attribute attr;
6552 gfc_ref *ref, *ref2;
6553 gfc_expr *e2;
6554 gfc_array_ref *ar;
6555 gfc_symbol *sym = NULL;
6556 gfc_alloc *a;
6557 gfc_component *c;
6558 gfc_try t;
6560 /* Mark the ultimost array component as being in allocate to allow DIMEN_STAR
6561 checking of coarrays. */
6562 for (ref = e->ref; ref; ref = ref->next)
6563 if (ref->next == NULL)
6564 break;
6566 if (ref && ref->type == REF_ARRAY)
6567 ref->u.ar.in_allocate = true;
6569 if (gfc_resolve_expr (e) == FAILURE)
6570 goto failure;
6572 /* Make sure the expression is allocatable or a pointer. If it is
6573 pointer, the next-to-last reference must be a pointer. */
6575 ref2 = NULL;
6576 if (e->symtree)
6577 sym = e->symtree->n.sym;
6579 /* Check whether ultimate component is abstract and CLASS. */
6580 is_abstract = 0;
6582 if (e->expr_type != EXPR_VARIABLE)
6584 allocatable = 0;
6585 attr = gfc_expr_attr (e);
6586 pointer = attr.pointer;
6587 dimension = attr.dimension;
6588 codimension = attr.codimension;
6590 else
6592 if (sym->ts.type == BT_CLASS)
6594 allocatable = CLASS_DATA (sym)->attr.allocatable;
6595 pointer = CLASS_DATA (sym)->attr.class_pointer;
6596 dimension = CLASS_DATA (sym)->attr.dimension;
6597 codimension = CLASS_DATA (sym)->attr.codimension;
6598 is_abstract = CLASS_DATA (sym)->attr.abstract;
6600 else
6602 allocatable = sym->attr.allocatable;
6603 pointer = sym->attr.pointer;
6604 dimension = sym->attr.dimension;
6605 codimension = sym->attr.codimension;
6608 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
6610 switch (ref->type)
6612 case REF_ARRAY:
6613 if (ref->next != NULL)
6614 pointer = 0;
6615 break;
6617 case REF_COMPONENT:
6618 /* F2008, C644. */
6619 if (gfc_is_coindexed (e))
6621 gfc_error ("Coindexed allocatable object at %L",
6622 &e->where);
6623 goto failure;
6626 c = ref->u.c.component;
6627 if (c->ts.type == BT_CLASS)
6629 allocatable = CLASS_DATA (c)->attr.allocatable;
6630 pointer = CLASS_DATA (c)->attr.class_pointer;
6631 dimension = CLASS_DATA (c)->attr.dimension;
6632 codimension = CLASS_DATA (c)->attr.codimension;
6633 is_abstract = CLASS_DATA (c)->attr.abstract;
6635 else
6637 allocatable = c->attr.allocatable;
6638 pointer = c->attr.pointer;
6639 dimension = c->attr.dimension;
6640 codimension = c->attr.codimension;
6641 is_abstract = c->attr.abstract;
6643 break;
6645 case REF_SUBSTRING:
6646 allocatable = 0;
6647 pointer = 0;
6648 break;
6653 if (allocatable == 0 && pointer == 0)
6655 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6656 &e->where);
6657 goto failure;
6660 /* Some checks for the SOURCE tag. */
6661 if (code->expr3)
6663 /* Check F03:C631. */
6664 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6666 gfc_error ("Type of entity at %L is type incompatible with "
6667 "source-expr at %L", &e->where, &code->expr3->where);
6668 goto failure;
6671 /* Check F03:C632 and restriction following Note 6.18. */
6672 if (code->expr3->rank > 0
6673 && conformable_arrays (code->expr3, e) == FAILURE)
6674 goto failure;
6676 /* Check F03:C633. */
6677 if (code->expr3->ts.kind != e->ts.kind)
6679 gfc_error ("The allocate-object at %L and the source-expr at %L "
6680 "shall have the same kind type parameter",
6681 &e->where, &code->expr3->where);
6682 goto failure;
6686 /* Check F08:C629. */
6687 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6688 && !code->expr3)
6690 gcc_assert (e->ts.type == BT_CLASS);
6691 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6692 "type-spec or source-expr", sym->name, &e->where);
6693 goto failure;
6696 /* In the variable definition context checks, gfc_expr_attr is used
6697 on the expression. This is fooled by the array specification
6698 present in e, thus we have to eliminate that one temporarily. */
6699 e2 = remove_last_array_ref (e);
6700 t = SUCCESS;
6701 if (t == SUCCESS && pointer)
6702 t = gfc_check_vardef_context (e2, true, _("ALLOCATE object"));
6703 if (t == SUCCESS)
6704 t = gfc_check_vardef_context (e2, false, _("ALLOCATE object"));
6705 gfc_free_expr (e2);
6706 if (t == FAILURE)
6707 goto failure;
6709 if (!code->expr3)
6711 /* Set up default initializer if needed. */
6712 gfc_typespec ts;
6713 gfc_expr *init_e;
6715 if (code->ext.alloc.ts.type == BT_DERIVED)
6716 ts = code->ext.alloc.ts;
6717 else
6718 ts = e->ts;
6720 if (ts.type == BT_CLASS)
6721 ts = ts.u.derived->components->ts;
6723 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
6725 gfc_code *init_st = gfc_get_code ();
6726 init_st->loc = code->loc;
6727 init_st->op = EXEC_INIT_ASSIGN;
6728 init_st->expr1 = gfc_expr_to_initialize (e);
6729 init_st->expr2 = init_e;
6730 init_st->next = code->next;
6731 code->next = init_st;
6734 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6736 /* Default initialization via MOLD (non-polymorphic). */
6737 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6738 gfc_resolve_expr (rhs);
6739 gfc_free_expr (code->expr3);
6740 code->expr3 = rhs;
6743 if (e->ts.type == BT_CLASS)
6745 /* Make sure the vtab symbol is present when
6746 the module variables are generated. */
6747 gfc_typespec ts = e->ts;
6748 if (code->expr3)
6749 ts = code->expr3->ts;
6750 else if (code->ext.alloc.ts.type == BT_DERIVED)
6751 ts = code->ext.alloc.ts;
6752 gfc_find_derived_vtab (ts.u.derived);
6755 if (pointer || (dimension == 0 && codimension == 0))
6756 goto success;
6758 /* Make sure the last reference node is an array specifiction. */
6760 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
6761 || (dimension && ref2->u.ar.dimen == 0))
6763 gfc_error ("Array specification required in ALLOCATE statement "
6764 "at %L", &e->where);
6765 goto failure;
6768 /* Make sure that the array section reference makes sense in the
6769 context of an ALLOCATE specification. */
6771 ar = &ref2->u.ar;
6773 if (codimension && ar->codimen == 0)
6775 gfc_error ("Coarray specification required in ALLOCATE statement "
6776 "at %L", &e->where);
6777 goto failure;
6780 for (i = 0; i < ar->dimen; i++)
6782 if (ref2->u.ar.type == AR_ELEMENT)
6783 goto check_symbols;
6785 switch (ar->dimen_type[i])
6787 case DIMEN_ELEMENT:
6788 break;
6790 case DIMEN_RANGE:
6791 if (ar->start[i] != NULL
6792 && ar->end[i] != NULL
6793 && ar->stride[i] == NULL)
6794 break;
6796 /* Fall Through... */
6798 case DIMEN_UNKNOWN:
6799 case DIMEN_VECTOR:
6800 case DIMEN_STAR:
6801 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6802 &e->where);
6803 goto failure;
6806 check_symbols:
6807 for (a = code->ext.alloc.list; a; a = a->next)
6809 sym = a->expr->symtree->n.sym;
6811 /* TODO - check derived type components. */
6812 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
6813 continue;
6815 if ((ar->start[i] != NULL
6816 && gfc_find_sym_in_expr (sym, ar->start[i]))
6817 || (ar->end[i] != NULL
6818 && gfc_find_sym_in_expr (sym, ar->end[i])))
6820 gfc_error ("'%s' must not appear in the array specification at "
6821 "%L in the same ALLOCATE statement where it is "
6822 "itself allocated", sym->name, &ar->where);
6823 goto failure;
6828 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
6830 if (ar->dimen_type[i] == DIMEN_ELEMENT
6831 || ar->dimen_type[i] == DIMEN_RANGE)
6833 if (i == (ar->dimen + ar->codimen - 1))
6835 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
6836 "statement at %L", &e->where);
6837 goto failure;
6839 break;
6842 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
6843 && ar->stride[i] == NULL)
6844 break;
6846 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
6847 &e->where);
6848 goto failure;
6851 if (codimension && ar->as->rank == 0)
6853 gfc_error ("Sorry, allocatable scalar coarrays are not yet supported "
6854 "at %L", &e->where);
6855 goto failure;
6858 success:
6859 return SUCCESS;
6861 failure:
6862 return FAILURE;
6865 static void
6866 resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6868 gfc_expr *stat, *errmsg, *pe, *qe;
6869 gfc_alloc *a, *p, *q;
6871 stat = code->expr1;
6872 errmsg = code->expr2;
6874 /* Check the stat variable. */
6875 if (stat)
6877 gfc_check_vardef_context (stat, false, _("STAT variable"));
6879 if ((stat->ts.type != BT_INTEGER
6880 && !(stat->ref && (stat->ref->type == REF_ARRAY
6881 || stat->ref->type == REF_COMPONENT)))
6882 || stat->rank > 0)
6883 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6884 "variable", &stat->where);
6886 for (p = code->ext.alloc.list; p; p = p->next)
6887 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6889 gfc_ref *ref1, *ref2;
6890 bool found = true;
6892 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
6893 ref1 = ref1->next, ref2 = ref2->next)
6895 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6896 continue;
6897 if (ref1->u.c.component->name != ref2->u.c.component->name)
6899 found = false;
6900 break;
6904 if (found)
6906 gfc_error ("Stat-variable at %L shall not be %sd within "
6907 "the same %s statement", &stat->where, fcn, fcn);
6908 break;
6913 /* Check the errmsg variable. */
6914 if (errmsg)
6916 if (!stat)
6917 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6918 &errmsg->where);
6920 gfc_check_vardef_context (errmsg, false, _("ERRMSG variable"));
6922 if ((errmsg->ts.type != BT_CHARACTER
6923 && !(errmsg->ref
6924 && (errmsg->ref->type == REF_ARRAY
6925 || errmsg->ref->type == REF_COMPONENT)))
6926 || errmsg->rank > 0 )
6927 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6928 "variable", &errmsg->where);
6930 for (p = code->ext.alloc.list; p; p = p->next)
6931 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6933 gfc_ref *ref1, *ref2;
6934 bool found = true;
6936 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
6937 ref1 = ref1->next, ref2 = ref2->next)
6939 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
6940 continue;
6941 if (ref1->u.c.component->name != ref2->u.c.component->name)
6943 found = false;
6944 break;
6948 if (found)
6950 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6951 "the same %s statement", &errmsg->where, fcn, fcn);
6952 break;
6957 /* Check that an allocate-object appears only once in the statement.
6958 FIXME: Checking derived types is disabled. */
6959 for (p = code->ext.alloc.list; p; p = p->next)
6961 pe = p->expr;
6962 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6963 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6965 for (q = p->next; q; q = q->next)
6967 qe = q->expr;
6968 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6969 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6970 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6971 gfc_error ("Allocate-object at %L also appears at %L",
6972 &pe->where, &qe->where);
6977 if (strcmp (fcn, "ALLOCATE") == 0)
6979 for (a = code->ext.alloc.list; a; a = a->next)
6980 resolve_allocate_expr (a->expr, code);
6982 else
6984 for (a = code->ext.alloc.list; a; a = a->next)
6985 resolve_deallocate_expr (a->expr);
6990 /************ SELECT CASE resolution subroutines ************/
6992 /* Callback function for our mergesort variant. Determines interval
6993 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
6994 op1 > op2. Assumes we're not dealing with the default case.
6995 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6996 There are nine situations to check. */
6998 static int
6999 compare_cases (const gfc_case *op1, const gfc_case *op2)
7001 int retval;
7003 if (op1->low == NULL) /* op1 = (:L) */
7005 /* op2 = (:N), so overlap. */
7006 retval = 0;
7007 /* op2 = (M:) or (M:N), L < M */
7008 if (op2->low != NULL
7009 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7010 retval = -1;
7012 else if (op1->high == NULL) /* op1 = (K:) */
7014 /* op2 = (M:), so overlap. */
7015 retval = 0;
7016 /* op2 = (:N) or (M:N), K > N */
7017 if (op2->high != NULL
7018 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7019 retval = 1;
7021 else /* op1 = (K:L) */
7023 if (op2->low == NULL) /* op2 = (:N), K > N */
7024 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7025 ? 1 : 0;
7026 else if (op2->high == NULL) /* op2 = (M:), L < M */
7027 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7028 ? -1 : 0;
7029 else /* op2 = (M:N) */
7031 retval = 0;
7032 /* L < M */
7033 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7034 retval = -1;
7035 /* K > N */
7036 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7037 retval = 1;
7041 return retval;
7045 /* Merge-sort a double linked case list, detecting overlap in the
7046 process. LIST is the head of the double linked case list before it
7047 is sorted. Returns the head of the sorted list if we don't see any
7048 overlap, or NULL otherwise. */
7050 static gfc_case *
7051 check_case_overlap (gfc_case *list)
7053 gfc_case *p, *q, *e, *tail;
7054 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7056 /* If the passed list was empty, return immediately. */
7057 if (!list)
7058 return NULL;
7060 overlap_seen = 0;
7061 insize = 1;
7063 /* Loop unconditionally. The only exit from this loop is a return
7064 statement, when we've finished sorting the case list. */
7065 for (;;)
7067 p = list;
7068 list = NULL;
7069 tail = NULL;
7071 /* Count the number of merges we do in this pass. */
7072 nmerges = 0;
7074 /* Loop while there exists a merge to be done. */
7075 while (p)
7077 int i;
7079 /* Count this merge. */
7080 nmerges++;
7082 /* Cut the list in two pieces by stepping INSIZE places
7083 forward in the list, starting from P. */
7084 psize = 0;
7085 q = p;
7086 for (i = 0; i < insize; i++)
7088 psize++;
7089 q = q->right;
7090 if (!q)
7091 break;
7093 qsize = insize;
7095 /* Now we have two lists. Merge them! */
7096 while (psize > 0 || (qsize > 0 && q != NULL))
7098 /* See from which the next case to merge comes from. */
7099 if (psize == 0)
7101 /* P is empty so the next case must come from Q. */
7102 e = q;
7103 q = q->right;
7104 qsize--;
7106 else if (qsize == 0 || q == NULL)
7108 /* Q is empty. */
7109 e = p;
7110 p = p->right;
7111 psize--;
7113 else
7115 cmp = compare_cases (p, q);
7116 if (cmp < 0)
7118 /* The whole case range for P is less than the
7119 one for Q. */
7120 e = p;
7121 p = p->right;
7122 psize--;
7124 else if (cmp > 0)
7126 /* The whole case range for Q is greater than
7127 the case range for P. */
7128 e = q;
7129 q = q->right;
7130 qsize--;
7132 else
7134 /* The cases overlap, or they are the same
7135 element in the list. Either way, we must
7136 issue an error and get the next case from P. */
7137 /* FIXME: Sort P and Q by line number. */
7138 gfc_error ("CASE label at %L overlaps with CASE "
7139 "label at %L", &p->where, &q->where);
7140 overlap_seen = 1;
7141 e = p;
7142 p = p->right;
7143 psize--;
7147 /* Add the next element to the merged list. */
7148 if (tail)
7149 tail->right = e;
7150 else
7151 list = e;
7152 e->left = tail;
7153 tail = e;
7156 /* P has now stepped INSIZE places along, and so has Q. So
7157 they're the same. */
7158 p = q;
7160 tail->right = NULL;
7162 /* If we have done only one merge or none at all, we've
7163 finished sorting the cases. */
7164 if (nmerges <= 1)
7166 if (!overlap_seen)
7167 return list;
7168 else
7169 return NULL;
7172 /* Otherwise repeat, merging lists twice the size. */
7173 insize *= 2;
7178 /* Check to see if an expression is suitable for use in a CASE statement.
7179 Makes sure that all case expressions are scalar constants of the same
7180 type. Return FAILURE if anything is wrong. */
7182 static gfc_try
7183 validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
7185 if (e == NULL) return SUCCESS;
7187 if (e->ts.type != case_expr->ts.type)
7189 gfc_error ("Expression in CASE statement at %L must be of type %s",
7190 &e->where, gfc_basic_typename (case_expr->ts.type));
7191 return FAILURE;
7194 /* C805 (R808) For a given case-construct, each case-value shall be of
7195 the same type as case-expr. For character type, length differences
7196 are allowed, but the kind type parameters shall be the same. */
7198 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
7200 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7201 &e->where, case_expr->ts.kind);
7202 return FAILURE;
7205 /* Convert the case value kind to that of case expression kind,
7206 if needed */
7208 if (e->ts.kind != case_expr->ts.kind)
7209 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7211 if (e->rank != 0)
7213 gfc_error ("Expression in CASE statement at %L must be scalar",
7214 &e->where);
7215 return FAILURE;
7218 return SUCCESS;
7222 /* Given a completely parsed select statement, we:
7224 - Validate all expressions and code within the SELECT.
7225 - Make sure that the selection expression is not of the wrong type.
7226 - Make sure that no case ranges overlap.
7227 - Eliminate unreachable cases and unreachable code resulting from
7228 removing case labels.
7230 The standard does allow unreachable cases, e.g. CASE (5:3). But
7231 they are a hassle for code generation, and to prevent that, we just
7232 cut them out here. This is not necessary for overlapping cases
7233 because they are illegal and we never even try to generate code.
7235 We have the additional caveat that a SELECT construct could have
7236 been a computed GOTO in the source code. Fortunately we can fairly
7237 easily work around that here: The case_expr for a "real" SELECT CASE
7238 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7239 we have to do is make sure that the case_expr is a scalar integer
7240 expression. */
7242 static void
7243 resolve_select (gfc_code *code)
7245 gfc_code *body;
7246 gfc_expr *case_expr;
7247 gfc_case *cp, *default_case, *tail, *head;
7248 int seen_unreachable;
7249 int seen_logical;
7250 int ncases;
7251 bt type;
7252 gfc_try t;
7254 if (code->expr1 == NULL)
7256 /* This was actually a computed GOTO statement. */
7257 case_expr = code->expr2;
7258 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
7259 gfc_error ("Selection expression in computed GOTO statement "
7260 "at %L must be a scalar integer expression",
7261 &case_expr->where);
7263 /* Further checking is not necessary because this SELECT was built
7264 by the compiler, so it should always be OK. Just move the
7265 case_expr from expr2 to expr so that we can handle computed
7266 GOTOs as normal SELECTs from here on. */
7267 code->expr1 = code->expr2;
7268 code->expr2 = NULL;
7269 return;
7272 case_expr = code->expr1;
7274 type = case_expr->ts.type;
7275 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7277 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7278 &case_expr->where, gfc_typename (&case_expr->ts));
7280 /* Punt. Going on here just produce more garbage error messages. */
7281 return;
7284 if (case_expr->rank != 0)
7286 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7287 "expression", &case_expr->where);
7289 /* Punt. */
7290 return;
7294 /* Raise a warning if an INTEGER case value exceeds the range of
7295 the case-expr. Later, all expressions will be promoted to the
7296 largest kind of all case-labels. */
7298 if (type == BT_INTEGER)
7299 for (body = code->block; body; body = body->block)
7300 for (cp = body->ext.case_list; cp; cp = cp->next)
7302 if (cp->low
7303 && gfc_check_integer_range (cp->low->value.integer,
7304 case_expr->ts.kind) != ARITH_OK)
7305 gfc_warning ("Expression in CASE statement at %L is "
7306 "not in the range of %s", &cp->low->where,
7307 gfc_typename (&case_expr->ts));
7309 if (cp->high
7310 && cp->low != cp->high
7311 && gfc_check_integer_range (cp->high->value.integer,
7312 case_expr->ts.kind) != ARITH_OK)
7313 gfc_warning ("Expression in CASE statement at %L is "
7314 "not in the range of %s", &cp->high->where,
7315 gfc_typename (&case_expr->ts));
7318 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7319 of the SELECT CASE expression and its CASE values. Walk the lists
7320 of case values, and if we find a mismatch, promote case_expr to
7321 the appropriate kind. */
7323 if (type == BT_LOGICAL || type == BT_INTEGER)
7325 for (body = code->block; body; body = body->block)
7327 /* Walk the case label list. */
7328 for (cp = body->ext.case_list; cp; cp = cp->next)
7330 /* Intercept the DEFAULT case. It does not have a kind. */
7331 if (cp->low == NULL && cp->high == NULL)
7332 continue;
7334 /* Unreachable case ranges are discarded, so ignore. */
7335 if (cp->low != NULL && cp->high != NULL
7336 && cp->low != cp->high
7337 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7338 continue;
7340 if (cp->low != NULL
7341 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7342 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7344 if (cp->high != NULL
7345 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
7346 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
7351 /* Assume there is no DEFAULT case. */
7352 default_case = NULL;
7353 head = tail = NULL;
7354 ncases = 0;
7355 seen_logical = 0;
7357 for (body = code->block; body; body = body->block)
7359 /* Assume the CASE list is OK, and all CASE labels can be matched. */
7360 t = SUCCESS;
7361 seen_unreachable = 0;
7363 /* Walk the case label list, making sure that all case labels
7364 are legal. */
7365 for (cp = body->ext.case_list; cp; cp = cp->next)
7367 /* Count the number of cases in the whole construct. */
7368 ncases++;
7370 /* Intercept the DEFAULT case. */
7371 if (cp->low == NULL && cp->high == NULL)
7373 if (default_case != NULL)
7375 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7376 "by a second DEFAULT CASE at %L",
7377 &default_case->where, &cp->where);
7378 t = FAILURE;
7379 break;
7381 else
7383 default_case = cp;
7384 continue;
7388 /* Deal with single value cases and case ranges. Errors are
7389 issued from the validation function. */
7390 if (validate_case_label_expr (cp->low, case_expr) != SUCCESS
7391 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
7393 t = FAILURE;
7394 break;
7397 if (type == BT_LOGICAL
7398 && ((cp->low == NULL || cp->high == NULL)
7399 || cp->low != cp->high))
7401 gfc_error ("Logical range in CASE statement at %L is not "
7402 "allowed", &cp->low->where);
7403 t = FAILURE;
7404 break;
7407 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7409 int value;
7410 value = cp->low->value.logical == 0 ? 2 : 1;
7411 if (value & seen_logical)
7413 gfc_error ("Constant logical value in CASE statement "
7414 "is repeated at %L",
7415 &cp->low->where);
7416 t = FAILURE;
7417 break;
7419 seen_logical |= value;
7422 if (cp->low != NULL && cp->high != NULL
7423 && cp->low != cp->high
7424 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
7426 if (gfc_option.warn_surprising)
7427 gfc_warning ("Range specification at %L can never "
7428 "be matched", &cp->where);
7430 cp->unreachable = 1;
7431 seen_unreachable = 1;
7433 else
7435 /* If the case range can be matched, it can also overlap with
7436 other cases. To make sure it does not, we put it in a
7437 double linked list here. We sort that with a merge sort
7438 later on to detect any overlapping cases. */
7439 if (!head)
7441 head = tail = cp;
7442 head->right = head->left = NULL;
7444 else
7446 tail->right = cp;
7447 tail->right->left = tail;
7448 tail = tail->right;
7449 tail->right = NULL;
7454 /* It there was a failure in the previous case label, give up
7455 for this case label list. Continue with the next block. */
7456 if (t == FAILURE)
7457 continue;
7459 /* See if any case labels that are unreachable have been seen.
7460 If so, we eliminate them. This is a bit of a kludge because
7461 the case lists for a single case statement (label) is a
7462 single forward linked lists. */
7463 if (seen_unreachable)
7465 /* Advance until the first case in the list is reachable. */
7466 while (body->ext.case_list != NULL
7467 && body->ext.case_list->unreachable)
7469 gfc_case *n = body->ext.case_list;
7470 body->ext.case_list = body->ext.case_list->next;
7471 n->next = NULL;
7472 gfc_free_case_list (n);
7475 /* Strip all other unreachable cases. */
7476 if (body->ext.case_list)
7478 for (cp = body->ext.case_list; cp->next; cp = cp->next)
7480 if (cp->next->unreachable)
7482 gfc_case *n = cp->next;
7483 cp->next = cp->next->next;
7484 n->next = NULL;
7485 gfc_free_case_list (n);
7492 /* See if there were overlapping cases. If the check returns NULL,
7493 there was overlap. In that case we don't do anything. If head
7494 is non-NULL, we prepend the DEFAULT case. The sorted list can
7495 then used during code generation for SELECT CASE constructs with
7496 a case expression of a CHARACTER type. */
7497 if (head)
7499 head = check_case_overlap (head);
7501 /* Prepend the default_case if it is there. */
7502 if (head != NULL && default_case)
7504 default_case->left = NULL;
7505 default_case->right = head;
7506 head->left = default_case;
7510 /* Eliminate dead blocks that may be the result if we've seen
7511 unreachable case labels for a block. */
7512 for (body = code; body && body->block; body = body->block)
7514 if (body->block->ext.case_list == NULL)
7516 /* Cut the unreachable block from the code chain. */
7517 gfc_code *c = body->block;
7518 body->block = c->block;
7520 /* Kill the dead block, but not the blocks below it. */
7521 c->block = NULL;
7522 gfc_free_statements (c);
7526 /* More than two cases is legal but insane for logical selects.
7527 Issue a warning for it. */
7528 if (gfc_option.warn_surprising && type == BT_LOGICAL
7529 && ncases > 2)
7530 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7531 &code->loc);
7535 /* Check if a derived type is extensible. */
7537 bool
7538 gfc_type_is_extensible (gfc_symbol *sym)
7540 return !(sym->attr.is_bind_c || sym->attr.sequence);
7544 /* Resolve an associate name: Resolve target and ensure the type-spec is
7545 correct as well as possibly the array-spec. */
7547 static void
7548 resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7550 gfc_expr* target;
7552 gcc_assert (sym->assoc);
7553 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7555 /* If this is for SELECT TYPE, the target may not yet be set. In that
7556 case, return. Resolution will be called later manually again when
7557 this is done. */
7558 target = sym->assoc->target;
7559 if (!target)
7560 return;
7561 gcc_assert (!sym->assoc->dangling);
7563 if (resolve_target && gfc_resolve_expr (target) != SUCCESS)
7564 return;
7566 /* For variable targets, we get some attributes from the target. */
7567 if (target->expr_type == EXPR_VARIABLE)
7569 gfc_symbol* tsym;
7571 gcc_assert (target->symtree);
7572 tsym = target->symtree->n.sym;
7574 sym->attr.asynchronous = tsym->attr.asynchronous;
7575 sym->attr.volatile_ = tsym->attr.volatile_;
7577 sym->attr.target = (tsym->attr.target || tsym->attr.pointer);
7580 /* Get type if this was not already set. Note that it can be
7581 some other type than the target in case this is a SELECT TYPE
7582 selector! So we must not update when the type is already there. */
7583 if (sym->ts.type == BT_UNKNOWN)
7584 sym->ts = target->ts;
7585 gcc_assert (sym->ts.type != BT_UNKNOWN);
7587 /* See if this is a valid association-to-variable. */
7588 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7589 && !gfc_has_vector_subscript (target));
7591 /* Finally resolve if this is an array or not. */
7592 if (sym->attr.dimension && target->rank == 0)
7594 gfc_error ("Associate-name '%s' at %L is used as array",
7595 sym->name, &sym->declared_at);
7596 sym->attr.dimension = 0;
7597 return;
7599 if (target->rank > 0)
7600 sym->attr.dimension = 1;
7602 if (sym->attr.dimension)
7604 sym->as = gfc_get_array_spec ();
7605 sym->as->rank = target->rank;
7606 sym->as->type = AS_DEFERRED;
7608 /* Target must not be coindexed, thus the associate-variable
7609 has no corank. */
7610 sym->as->corank = 0;
7615 /* Resolve a SELECT TYPE statement. */
7617 static void
7618 resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
7620 gfc_symbol *selector_type;
7621 gfc_code *body, *new_st, *if_st, *tail;
7622 gfc_code *class_is = NULL, *default_case = NULL;
7623 gfc_case *c;
7624 gfc_symtree *st;
7625 char name[GFC_MAX_SYMBOL_LEN];
7626 gfc_namespace *ns;
7627 int error = 0;
7629 ns = code->ext.block.ns;
7630 gfc_resolve (ns);
7632 /* Check for F03:C813. */
7633 if (code->expr1->ts.type != BT_CLASS
7634 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7636 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7637 "at %L", &code->loc);
7638 return;
7641 if (code->expr2)
7643 if (code->expr1->symtree->n.sym->attr.untyped)
7644 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7645 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
7647 else
7648 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7650 /* Loop over TYPE IS / CLASS IS cases. */
7651 for (body = code->block; body; body = body->block)
7653 c = body->ext.case_list;
7655 /* Check F03:C815. */
7656 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7657 && !gfc_type_is_extensible (c->ts.u.derived))
7659 gfc_error ("Derived type '%s' at %L must be extensible",
7660 c->ts.u.derived->name, &c->where);
7661 error++;
7662 continue;
7665 /* Check F03:C816. */
7666 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7667 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
7669 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7670 c->ts.u.derived->name, &c->where, selector_type->name);
7671 error++;
7672 continue;
7675 /* Intercept the DEFAULT case. */
7676 if (c->ts.type == BT_UNKNOWN)
7678 /* Check F03:C818. */
7679 if (default_case)
7681 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7682 "by a second DEFAULT CASE at %L",
7683 &default_case->ext.case_list->where, &c->where);
7684 error++;
7685 continue;
7688 default_case = body;
7692 if (error > 0)
7693 return;
7695 /* Transform SELECT TYPE statement to BLOCK and associate selector to
7696 target if present. If there are any EXIT statements referring to the
7697 SELECT TYPE construct, this is no problem because the gfc_code
7698 reference stays the same and EXIT is equally possible from the BLOCK
7699 it is changed to. */
7700 code->op = EXEC_BLOCK;
7701 if (code->expr2)
7703 gfc_association_list* assoc;
7705 assoc = gfc_get_association_list ();
7706 assoc->st = code->expr1->symtree;
7707 assoc->target = gfc_copy_expr (code->expr2);
7708 /* assoc->variable will be set by resolve_assoc_var. */
7710 code->ext.block.assoc = assoc;
7711 code->expr1->symtree->n.sym->assoc = assoc;
7713 resolve_assoc_var (code->expr1->symtree->n.sym, false);
7715 else
7716 code->ext.block.assoc = NULL;
7718 /* Add EXEC_SELECT to switch on type. */
7719 new_st = gfc_get_code ();
7720 new_st->op = code->op;
7721 new_st->expr1 = code->expr1;
7722 new_st->expr2 = code->expr2;
7723 new_st->block = code->block;
7724 code->expr1 = code->expr2 = NULL;
7725 code->block = NULL;
7726 if (!ns->code)
7727 ns->code = new_st;
7728 else
7729 ns->code->next = new_st;
7730 code = new_st;
7731 code->op = EXEC_SELECT;
7732 gfc_add_component_ref (code->expr1, "$vptr");
7733 gfc_add_component_ref (code->expr1, "$hash");
7735 /* Loop over TYPE IS / CLASS IS cases. */
7736 for (body = code->block; body; body = body->block)
7738 c = body->ext.case_list;
7740 if (c->ts.type == BT_DERIVED)
7741 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
7742 c->ts.u.derived->hash_value);
7744 else if (c->ts.type == BT_UNKNOWN)
7745 continue;
7747 /* Associate temporary to selector. This should only be done
7748 when this case is actually true, so build a new ASSOCIATE
7749 that does precisely this here (instead of using the
7750 'global' one). */
7752 if (c->ts.type == BT_CLASS)
7753 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7754 else
7755 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
7756 st = gfc_find_symtree (ns->sym_root, name);
7757 gcc_assert (st->n.sym->assoc);
7758 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
7759 if (c->ts.type == BT_DERIVED)
7760 gfc_add_component_ref (st->n.sym->assoc->target, "$data");
7762 new_st = gfc_get_code ();
7763 new_st->op = EXEC_BLOCK;
7764 new_st->ext.block.ns = gfc_build_block_ns (ns);
7765 new_st->ext.block.ns->code = body->next;
7766 body->next = new_st;
7768 /* Chain in the new list only if it is marked as dangling. Otherwise
7769 there is a CASE label overlap and this is already used. Just ignore,
7770 the error is diagonsed elsewhere. */
7771 if (st->n.sym->assoc->dangling)
7773 new_st->ext.block.assoc = st->n.sym->assoc;
7774 st->n.sym->assoc->dangling = 0;
7777 resolve_assoc_var (st->n.sym, false);
7780 /* Take out CLASS IS cases for separate treatment. */
7781 body = code;
7782 while (body && body->block)
7784 if (body->block->ext.case_list->ts.type == BT_CLASS)
7786 /* Add to class_is list. */
7787 if (class_is == NULL)
7789 class_is = body->block;
7790 tail = class_is;
7792 else
7794 for (tail = class_is; tail->block; tail = tail->block) ;
7795 tail->block = body->block;
7796 tail = tail->block;
7798 /* Remove from EXEC_SELECT list. */
7799 body->block = body->block->block;
7800 tail->block = NULL;
7802 else
7803 body = body->block;
7806 if (class_is)
7808 gfc_symbol *vtab;
7810 if (!default_case)
7812 /* Add a default case to hold the CLASS IS cases. */
7813 for (tail = code; tail->block; tail = tail->block) ;
7814 tail->block = gfc_get_code ();
7815 tail = tail->block;
7816 tail->op = EXEC_SELECT_TYPE;
7817 tail->ext.case_list = gfc_get_case ();
7818 tail->ext.case_list->ts.type = BT_UNKNOWN;
7819 tail->next = NULL;
7820 default_case = tail;
7823 /* More than one CLASS IS block? */
7824 if (class_is->block)
7826 gfc_code **c1,*c2;
7827 bool swapped;
7828 /* Sort CLASS IS blocks by extension level. */
7831 swapped = false;
7832 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7834 c2 = (*c1)->block;
7835 /* F03:C817 (check for doubles). */
7836 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7837 == c2->ext.case_list->ts.u.derived->hash_value)
7839 gfc_error ("Double CLASS IS block in SELECT TYPE "
7840 "statement at %L", &c2->ext.case_list->where);
7841 return;
7843 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7844 < c2->ext.case_list->ts.u.derived->attr.extension)
7846 /* Swap. */
7847 (*c1)->block = c2->block;
7848 c2->block = *c1;
7849 *c1 = c2;
7850 swapped = true;
7854 while (swapped);
7857 /* Generate IF chain. */
7858 if_st = gfc_get_code ();
7859 if_st->op = EXEC_IF;
7860 new_st = if_st;
7861 for (body = class_is; body; body = body->block)
7863 new_st->block = gfc_get_code ();
7864 new_st = new_st->block;
7865 new_st->op = EXEC_IF;
7866 /* Set up IF condition: Call _gfortran_is_extension_of. */
7867 new_st->expr1 = gfc_get_expr ();
7868 new_st->expr1->expr_type = EXPR_FUNCTION;
7869 new_st->expr1->ts.type = BT_LOGICAL;
7870 new_st->expr1->ts.kind = 4;
7871 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7872 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7873 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7874 /* Set up arguments. */
7875 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7876 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7877 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7878 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7879 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7880 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7881 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7882 new_st->next = body->next;
7884 if (default_case->next)
7886 new_st->block = gfc_get_code ();
7887 new_st = new_st->block;
7888 new_st->op = EXEC_IF;
7889 new_st->next = default_case->next;
7892 /* Replace CLASS DEFAULT code by the IF chain. */
7893 default_case->next = if_st;
7896 /* Resolve the internal code. This can not be done earlier because
7897 it requires that the sym->assoc of selectors is set already. */
7898 gfc_current_ns = ns;
7899 gfc_resolve_blocks (code->block, gfc_current_ns);
7900 gfc_current_ns = old_ns;
7902 resolve_select (code);
7906 /* Resolve a transfer statement. This is making sure that:
7907 -- a derived type being transferred has only non-pointer components
7908 -- a derived type being transferred doesn't have private components, unless
7909 it's being transferred from the module where the type was defined
7910 -- we're not trying to transfer a whole assumed size array. */
7912 static void
7913 resolve_transfer (gfc_code *code)
7915 gfc_typespec *ts;
7916 gfc_symbol *sym;
7917 gfc_ref *ref;
7918 gfc_expr *exp;
7920 exp = code->expr1;
7922 while (exp != NULL && exp->expr_type == EXPR_OP
7923 && exp->value.op.op == INTRINSIC_PARENTHESES)
7924 exp = exp->value.op.op1;
7926 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
7927 && exp->expr_type != EXPR_FUNCTION))
7928 return;
7930 /* If we are reading, the variable will be changed. Note that
7931 code->ext.dt may be NULL if the TRANSFER is related to
7932 an INQUIRE statement -- but in this case, we are not reading, either. */
7933 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
7934 && gfc_check_vardef_context (exp, false, _("item in READ")) == FAILURE)
7935 return;
7937 sym = exp->symtree->n.sym;
7938 ts = &sym->ts;
7940 /* Go to actual component transferred. */
7941 for (ref = exp->ref; ref; ref = ref->next)
7942 if (ref->type == REF_COMPONENT)
7943 ts = &ref->u.c.component->ts;
7945 if (ts->type == BT_DERIVED)
7947 /* Check that transferred derived type doesn't contain POINTER
7948 components. */
7949 if (ts->u.derived->attr.pointer_comp)
7951 gfc_error ("Data transfer element at %L cannot have "
7952 "POINTER components", &code->loc);
7953 return;
7956 if (ts->u.derived->attr.alloc_comp)
7958 gfc_error ("Data transfer element at %L cannot have "
7959 "ALLOCATABLE components", &code->loc);
7960 return;
7963 if (derived_inaccessible (ts->u.derived))
7965 gfc_error ("Data transfer element at %L cannot have "
7966 "PRIVATE components",&code->loc);
7967 return;
7971 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7972 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7974 gfc_error ("Data transfer element at %L cannot be a full reference to "
7975 "an assumed-size array", &code->loc);
7976 return;
7981 /*********** Toplevel code resolution subroutines ***********/
7983 /* Find the set of labels that are reachable from this block. We also
7984 record the last statement in each block. */
7986 static void
7987 find_reachable_labels (gfc_code *block)
7989 gfc_code *c;
7991 if (!block)
7992 return;
7994 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7996 /* Collect labels in this block. We don't keep those corresponding
7997 to END {IF|SELECT}, these are checked in resolve_branch by going
7998 up through the code_stack. */
7999 for (c = block; c; c = c->next)
8001 if (c->here && c->op != EXEC_END_BLOCK)
8002 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
8005 /* Merge with labels from parent block. */
8006 if (cs_base->prev)
8008 gcc_assert (cs_base->prev->reachable_labels);
8009 bitmap_ior_into (cs_base->reachable_labels,
8010 cs_base->prev->reachable_labels);
8015 static void
8016 resolve_sync (gfc_code *code)
8018 /* Check imageset. The * case matches expr1 == NULL. */
8019 if (code->expr1)
8021 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8022 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8023 "INTEGER expression", &code->expr1->where);
8024 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8025 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8026 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8027 &code->expr1->where);
8028 else if (code->expr1->expr_type == EXPR_ARRAY
8029 && gfc_simplify_expr (code->expr1, 0) == SUCCESS)
8031 gfc_constructor *cons;
8032 cons = gfc_constructor_first (code->expr1->value.constructor);
8033 for (; cons; cons = gfc_constructor_next (cons))
8034 if (cons->expr->expr_type == EXPR_CONSTANT
8035 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8036 gfc_error ("Imageset argument at %L must between 1 and "
8037 "num_images()", &cons->expr->where);
8041 /* Check STAT. */
8042 if (code->expr2
8043 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8044 || code->expr2->expr_type != EXPR_VARIABLE))
8045 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8046 &code->expr2->where);
8048 /* Check ERRMSG. */
8049 if (code->expr3
8050 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8051 || code->expr3->expr_type != EXPR_VARIABLE))
8052 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8053 &code->expr3->where);
8057 /* Given a branch to a label, see if the branch is conforming.
8058 The code node describes where the branch is located. */
8060 static void
8061 resolve_branch (gfc_st_label *label, gfc_code *code)
8063 code_stack *stack;
8065 if (label == NULL)
8066 return;
8068 /* Step one: is this a valid branching target? */
8070 if (label->defined == ST_LABEL_UNKNOWN)
8072 gfc_error ("Label %d referenced at %L is never defined", label->value,
8073 &label->where);
8074 return;
8077 if (label->defined != ST_LABEL_TARGET)
8079 gfc_error ("Statement at %L is not a valid branch target statement "
8080 "for the branch statement at %L", &label->where, &code->loc);
8081 return;
8084 /* Step two: make sure this branch is not a branch to itself ;-) */
8086 if (code->here == label)
8088 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
8089 return;
8092 /* Step three: See if the label is in the same block as the
8093 branching statement. The hard work has been done by setting up
8094 the bitmap reachable_labels. */
8096 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
8098 /* Check now whether there is a CRITICAL construct; if so, check
8099 whether the label is still visible outside of the CRITICAL block,
8100 which is invalid. */
8101 for (stack = cs_base; stack; stack = stack->prev)
8102 if (stack->current->op == EXEC_CRITICAL
8103 && bitmap_bit_p (stack->reachable_labels, label->value))
8104 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8105 " at %L", &code->loc, &label->where);
8107 return;
8110 /* Step four: If we haven't found the label in the bitmap, it may
8111 still be the label of the END of the enclosing block, in which
8112 case we find it by going up the code_stack. */
8114 for (stack = cs_base; stack; stack = stack->prev)
8116 if (stack->current->next && stack->current->next->here == label)
8117 break;
8118 if (stack->current->op == EXEC_CRITICAL)
8120 /* Note: A label at END CRITICAL does not leave the CRITICAL
8121 construct as END CRITICAL is still part of it. */
8122 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8123 " at %L", &code->loc, &label->where);
8124 return;
8128 if (stack)
8130 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
8131 return;
8134 /* The label is not in an enclosing block, so illegal. This was
8135 allowed in Fortran 66, so we allow it as extension. No
8136 further checks are necessary in this case. */
8137 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8138 "as the GOTO statement at %L", &label->where,
8139 &code->loc);
8140 return;
8144 /* Check whether EXPR1 has the same shape as EXPR2. */
8146 static gfc_try
8147 resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8149 mpz_t shape[GFC_MAX_DIMENSIONS];
8150 mpz_t shape2[GFC_MAX_DIMENSIONS];
8151 gfc_try result = FAILURE;
8152 int i;
8154 /* Compare the rank. */
8155 if (expr1->rank != expr2->rank)
8156 return result;
8158 /* Compare the size of each dimension. */
8159 for (i=0; i<expr1->rank; i++)
8161 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
8162 goto ignore;
8164 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
8165 goto ignore;
8167 if (mpz_cmp (shape[i], shape2[i]))
8168 goto over;
8171 /* When either of the two expression is an assumed size array, we
8172 ignore the comparison of dimension sizes. */
8173 ignore:
8174 result = SUCCESS;
8176 over:
8177 for (i--; i >= 0; i--)
8179 mpz_clear (shape[i]);
8180 mpz_clear (shape2[i]);
8182 return result;
8186 /* Check whether a WHERE assignment target or a WHERE mask expression
8187 has the same shape as the outmost WHERE mask expression. */
8189 static void
8190 resolve_where (gfc_code *code, gfc_expr *mask)
8192 gfc_code *cblock;
8193 gfc_code *cnext;
8194 gfc_expr *e = NULL;
8196 cblock = code->block;
8198 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8199 In case of nested WHERE, only the outmost one is stored. */
8200 if (mask == NULL) /* outmost WHERE */
8201 e = cblock->expr1;
8202 else /* inner WHERE */
8203 e = mask;
8205 while (cblock)
8207 if (cblock->expr1)
8209 /* Check if the mask-expr has a consistent shape with the
8210 outmost WHERE mask-expr. */
8211 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
8212 gfc_error ("WHERE mask at %L has inconsistent shape",
8213 &cblock->expr1->where);
8216 /* the assignment statement of a WHERE statement, or the first
8217 statement in where-body-construct of a WHERE construct */
8218 cnext = cblock->next;
8219 while (cnext)
8221 switch (cnext->op)
8223 /* WHERE assignment statement */
8224 case EXEC_ASSIGN:
8226 /* Check shape consistent for WHERE assignment target. */
8227 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
8228 gfc_error ("WHERE assignment target at %L has "
8229 "inconsistent shape", &cnext->expr1->where);
8230 break;
8233 case EXEC_ASSIGN_CALL:
8234 resolve_call (cnext);
8235 if (!cnext->resolved_sym->attr.elemental)
8236 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8237 &cnext->ext.actual->expr->where);
8238 break;
8240 /* WHERE or WHERE construct is part of a where-body-construct */
8241 case EXEC_WHERE:
8242 resolve_where (cnext, e);
8243 break;
8245 default:
8246 gfc_error ("Unsupported statement inside WHERE at %L",
8247 &cnext->loc);
8249 /* the next statement within the same where-body-construct */
8250 cnext = cnext->next;
8252 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8253 cblock = cblock->block;
8258 /* Resolve assignment in FORALL construct.
8259 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8260 FORALL index variables. */
8262 static void
8263 gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8265 int n;
8267 for (n = 0; n < nvar; n++)
8269 gfc_symbol *forall_index;
8271 forall_index = var_expr[n]->symtree->n.sym;
8273 /* Check whether the assignment target is one of the FORALL index
8274 variable. */
8275 if ((code->expr1->expr_type == EXPR_VARIABLE)
8276 && (code->expr1->symtree->n.sym == forall_index))
8277 gfc_error ("Assignment to a FORALL index variable at %L",
8278 &code->expr1->where);
8279 else
8281 /* If one of the FORALL index variables doesn't appear in the
8282 assignment variable, then there could be a many-to-one
8283 assignment. Emit a warning rather than an error because the
8284 mask could be resolving this problem. */
8285 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
8286 gfc_warning ("The FORALL with index '%s' is not used on the "
8287 "left side of the assignment at %L and so might "
8288 "cause multiple assignment to this object",
8289 var_expr[n]->symtree->name, &code->expr1->where);
8295 /* Resolve WHERE statement in FORALL construct. */
8297 static void
8298 gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8299 gfc_expr **var_expr)
8301 gfc_code *cblock;
8302 gfc_code *cnext;
8304 cblock = code->block;
8305 while (cblock)
8307 /* the assignment statement of a WHERE statement, or the first
8308 statement in where-body-construct of a WHERE construct */
8309 cnext = cblock->next;
8310 while (cnext)
8312 switch (cnext->op)
8314 /* WHERE assignment statement */
8315 case EXEC_ASSIGN:
8316 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8317 break;
8319 /* WHERE operator assignment statement */
8320 case EXEC_ASSIGN_CALL:
8321 resolve_call (cnext);
8322 if (!cnext->resolved_sym->attr.elemental)
8323 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
8324 &cnext->ext.actual->expr->where);
8325 break;
8327 /* WHERE or WHERE construct is part of a where-body-construct */
8328 case EXEC_WHERE:
8329 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8330 break;
8332 default:
8333 gfc_error ("Unsupported statement inside WHERE at %L",
8334 &cnext->loc);
8336 /* the next statement within the same where-body-construct */
8337 cnext = cnext->next;
8339 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8340 cblock = cblock->block;
8345 /* Traverse the FORALL body to check whether the following errors exist:
8346 1. For assignment, check if a many-to-one assignment happens.
8347 2. For WHERE statement, check the WHERE body to see if there is any
8348 many-to-one assignment. */
8350 static void
8351 gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8353 gfc_code *c;
8355 c = code->block->next;
8356 while (c)
8358 switch (c->op)
8360 case EXEC_ASSIGN:
8361 case EXEC_POINTER_ASSIGN:
8362 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8363 break;
8365 case EXEC_ASSIGN_CALL:
8366 resolve_call (c);
8367 break;
8369 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8370 there is no need to handle it here. */
8371 case EXEC_FORALL:
8372 break;
8373 case EXEC_WHERE:
8374 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8375 break;
8376 default:
8377 break;
8379 /* The next statement in the FORALL body. */
8380 c = c->next;
8385 /* Counts the number of iterators needed inside a forall construct, including
8386 nested forall constructs. This is used to allocate the needed memory
8387 in gfc_resolve_forall. */
8389 static int
8390 gfc_count_forall_iterators (gfc_code *code)
8392 int max_iters, sub_iters, current_iters;
8393 gfc_forall_iterator *fa;
8395 gcc_assert(code->op == EXEC_FORALL);
8396 max_iters = 0;
8397 current_iters = 0;
8399 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8400 current_iters ++;
8402 code = code->block->next;
8404 while (code)
8406 if (code->op == EXEC_FORALL)
8408 sub_iters = gfc_count_forall_iterators (code);
8409 if (sub_iters > max_iters)
8410 max_iters = sub_iters;
8412 code = code->next;
8415 return current_iters + max_iters;
8419 /* Given a FORALL construct, first resolve the FORALL iterator, then call
8420 gfc_resolve_forall_body to resolve the FORALL body. */
8422 static void
8423 gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8425 static gfc_expr **var_expr;
8426 static int total_var = 0;
8427 static int nvar = 0;
8428 int old_nvar, tmp;
8429 gfc_forall_iterator *fa;
8430 int i;
8432 old_nvar = nvar;
8434 /* Start to resolve a FORALL construct */
8435 if (forall_save == 0)
8437 /* Count the total number of FORALL index in the nested FORALL
8438 construct in order to allocate the VAR_EXPR with proper size. */
8439 total_var = gfc_count_forall_iterators (code);
8441 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
8442 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
8445 /* The information about FORALL iterator, including FORALL index start, end
8446 and stride. The FORALL index can not appear in start, end or stride. */
8447 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8449 /* Check if any outer FORALL index name is the same as the current
8450 one. */
8451 for (i = 0; i < nvar; i++)
8453 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8455 gfc_error ("An outer FORALL construct already has an index "
8456 "with this name %L", &fa->var->where);
8460 /* Record the current FORALL index. */
8461 var_expr[nvar] = gfc_copy_expr (fa->var);
8463 nvar++;
8465 /* No memory leak. */
8466 gcc_assert (nvar <= total_var);
8469 /* Resolve the FORALL body. */
8470 gfc_resolve_forall_body (code, nvar, var_expr);
8472 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
8473 gfc_resolve_blocks (code->block, ns);
8475 tmp = nvar;
8476 nvar = old_nvar;
8477 /* Free only the VAR_EXPRs allocated in this frame. */
8478 for (i = nvar; i < tmp; i++)
8479 gfc_free_expr (var_expr[i]);
8481 if (nvar == 0)
8483 /* We are in the outermost FORALL construct. */
8484 gcc_assert (forall_save == 0);
8486 /* VAR_EXPR is not needed any more. */
8487 gfc_free (var_expr);
8488 total_var = 0;
8493 /* Resolve a BLOCK construct statement. */
8495 static void
8496 resolve_block_construct (gfc_code* code)
8498 /* Resolve the BLOCK's namespace. */
8499 gfc_resolve (code->ext.block.ns);
8501 /* For an ASSOCIATE block, the associations (and their targets) are already
8502 resolved during resolve_symbol. */
8506 /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
8507 DO code nodes. */
8509 static void resolve_code (gfc_code *, gfc_namespace *);
8511 void
8512 gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
8514 gfc_try t;
8516 for (; b; b = b->block)
8518 t = gfc_resolve_expr (b->expr1);
8519 if (gfc_resolve_expr (b->expr2) == FAILURE)
8520 t = FAILURE;
8522 switch (b->op)
8524 case EXEC_IF:
8525 if (t == SUCCESS && b->expr1 != NULL
8526 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
8527 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8528 &b->expr1->where);
8529 break;
8531 case EXEC_WHERE:
8532 if (t == SUCCESS
8533 && b->expr1 != NULL
8534 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
8535 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
8536 &b->expr1->where);
8537 break;
8539 case EXEC_GOTO:
8540 resolve_branch (b->label1, b);
8541 break;
8543 case EXEC_BLOCK:
8544 resolve_block_construct (b);
8545 break;
8547 case EXEC_SELECT:
8548 case EXEC_SELECT_TYPE:
8549 case EXEC_FORALL:
8550 case EXEC_DO:
8551 case EXEC_DO_WHILE:
8552 case EXEC_CRITICAL:
8553 case EXEC_READ:
8554 case EXEC_WRITE:
8555 case EXEC_IOLENGTH:
8556 case EXEC_WAIT:
8557 break;
8559 case EXEC_OMP_ATOMIC:
8560 case EXEC_OMP_CRITICAL:
8561 case EXEC_OMP_DO:
8562 case EXEC_OMP_MASTER:
8563 case EXEC_OMP_ORDERED:
8564 case EXEC_OMP_PARALLEL:
8565 case EXEC_OMP_PARALLEL_DO:
8566 case EXEC_OMP_PARALLEL_SECTIONS:
8567 case EXEC_OMP_PARALLEL_WORKSHARE:
8568 case EXEC_OMP_SECTIONS:
8569 case EXEC_OMP_SINGLE:
8570 case EXEC_OMP_TASK:
8571 case EXEC_OMP_TASKWAIT:
8572 case EXEC_OMP_WORKSHARE:
8573 break;
8575 default:
8576 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
8579 resolve_code (b->next, ns);
8584 /* Does everything to resolve an ordinary assignment. Returns true
8585 if this is an interface assignment. */
8586 static bool
8587 resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
8589 bool rval = false;
8590 gfc_expr *lhs;
8591 gfc_expr *rhs;
8592 int llen = 0;
8593 int rlen = 0;
8594 int n;
8595 gfc_ref *ref;
8597 if (gfc_extend_assign (code, ns) == SUCCESS)
8599 gfc_expr** rhsptr;
8601 if (code->op == EXEC_ASSIGN_CALL)
8603 lhs = code->ext.actual->expr;
8604 rhsptr = &code->ext.actual->next->expr;
8606 else
8608 gfc_actual_arglist* args;
8609 gfc_typebound_proc* tbp;
8611 gcc_assert (code->op == EXEC_COMPCALL);
8613 args = code->expr1->value.compcall.actual;
8614 lhs = args->expr;
8615 rhsptr = &args->next->expr;
8617 tbp = code->expr1->value.compcall.tbp;
8618 gcc_assert (!tbp->is_generic);
8621 /* Make a temporary rhs when there is a default initializer
8622 and rhs is the same symbol as the lhs. */
8623 if ((*rhsptr)->expr_type == EXPR_VARIABLE
8624 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
8625 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
8626 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
8627 *rhsptr = gfc_get_parentheses (*rhsptr);
8629 return true;
8632 lhs = code->expr1;
8633 rhs = code->expr2;
8635 if (rhs->is_boz
8636 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
8637 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
8638 &code->loc) == FAILURE)
8639 return false;
8641 /* Handle the case of a BOZ literal on the RHS. */
8642 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
8644 int rc;
8645 if (gfc_option.warn_surprising)
8646 gfc_warning ("BOZ literal at %L is bitwise transferred "
8647 "non-integer symbol '%s'", &code->loc,
8648 lhs->symtree->n.sym->name);
8650 if (!gfc_convert_boz (rhs, &lhs->ts))
8651 return false;
8652 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
8654 if (rc == ARITH_UNDERFLOW)
8655 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
8656 ". This check can be disabled with the option "
8657 "-fno-range-check", &rhs->where);
8658 else if (rc == ARITH_OVERFLOW)
8659 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
8660 ". This check can be disabled with the option "
8661 "-fno-range-check", &rhs->where);
8662 else if (rc == ARITH_NAN)
8663 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
8664 ". This check can be disabled with the option "
8665 "-fno-range-check", &rhs->where);
8666 return false;
8670 if (lhs->ts.type == BT_CHARACTER
8671 && gfc_option.warn_character_truncation)
8673 if (lhs->ts.u.cl != NULL
8674 && lhs->ts.u.cl->length != NULL
8675 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8676 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
8678 if (rhs->expr_type == EXPR_CONSTANT)
8679 rlen = rhs->value.character.length;
8681 else if (rhs->ts.u.cl != NULL
8682 && rhs->ts.u.cl->length != NULL
8683 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8684 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
8686 if (rlen && llen && rlen > llen)
8687 gfc_warning_now ("CHARACTER expression will be truncated "
8688 "in assignment (%d/%d) at %L",
8689 llen, rlen, &code->loc);
8692 /* Ensure that a vector index expression for the lvalue is evaluated
8693 to a temporary if the lvalue symbol is referenced in it. */
8694 if (lhs->rank)
8696 for (ref = lhs->ref; ref; ref= ref->next)
8697 if (ref->type == REF_ARRAY)
8699 for (n = 0; n < ref->u.ar.dimen; n++)
8700 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
8701 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
8702 ref->u.ar.start[n]))
8703 ref->u.ar.start[n]
8704 = gfc_get_parentheses (ref->u.ar.start[n]);
8708 if (gfc_pure (NULL))
8710 if (lhs->ts.type == BT_DERIVED
8711 && lhs->expr_type == EXPR_VARIABLE
8712 && lhs->ts.u.derived->attr.pointer_comp
8713 && rhs->expr_type == EXPR_VARIABLE
8714 && (gfc_impure_variable (rhs->symtree->n.sym)
8715 || gfc_is_coindexed (rhs)))
8717 /* F2008, C1283. */
8718 if (gfc_is_coindexed (rhs))
8719 gfc_error ("Coindexed expression at %L is assigned to "
8720 "a derived type variable with a POINTER "
8721 "component in a PURE procedure",
8722 &rhs->where);
8723 else
8724 gfc_error ("The impure variable at %L is assigned to "
8725 "a derived type variable with a POINTER "
8726 "component in a PURE procedure (12.6)",
8727 &rhs->where);
8728 return rval;
8731 /* Fortran 2008, C1283. */
8732 if (gfc_is_coindexed (lhs))
8734 gfc_error ("Assignment to coindexed variable at %L in a PURE "
8735 "procedure", &rhs->where);
8736 return rval;
8740 /* F03:7.4.1.2. */
8741 /* FIXME: Valid in Fortran 2008, unless the LHS is both polymorphic
8742 and coindexed; cf. F2008, 7.2.1.2 and PR 43366. */
8743 if (lhs->ts.type == BT_CLASS)
8745 gfc_error ("Variable must not be polymorphic in assignment at %L",
8746 &lhs->where);
8747 return false;
8750 /* F2008, Section 7.2.1.2. */
8751 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
8753 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
8754 "component in assignment at %L", &lhs->where);
8755 return false;
8758 gfc_check_assign (lhs, rhs, 1);
8759 return false;
8763 /* Given a block of code, recursively resolve everything pointed to by this
8764 code block. */
8766 static void
8767 resolve_code (gfc_code *code, gfc_namespace *ns)
8769 int omp_workshare_save;
8770 int forall_save;
8771 code_stack frame;
8772 gfc_try t;
8774 frame.prev = cs_base;
8775 frame.head = code;
8776 cs_base = &frame;
8778 find_reachable_labels (code);
8780 for (; code; code = code->next)
8782 frame.current = code;
8783 forall_save = forall_flag;
8785 if (code->op == EXEC_FORALL)
8787 forall_flag = 1;
8788 gfc_resolve_forall (code, ns, forall_save);
8789 forall_flag = 2;
8791 else if (code->block)
8793 omp_workshare_save = -1;
8794 switch (code->op)
8796 case EXEC_OMP_PARALLEL_WORKSHARE:
8797 omp_workshare_save = omp_workshare_flag;
8798 omp_workshare_flag = 1;
8799 gfc_resolve_omp_parallel_blocks (code, ns);
8800 break;
8801 case EXEC_OMP_PARALLEL:
8802 case EXEC_OMP_PARALLEL_DO:
8803 case EXEC_OMP_PARALLEL_SECTIONS:
8804 case EXEC_OMP_TASK:
8805 omp_workshare_save = omp_workshare_flag;
8806 omp_workshare_flag = 0;
8807 gfc_resolve_omp_parallel_blocks (code, ns);
8808 break;
8809 case EXEC_OMP_DO:
8810 gfc_resolve_omp_do_blocks (code, ns);
8811 break;
8812 case EXEC_SELECT_TYPE:
8813 /* Blocks are handled in resolve_select_type because we have
8814 to transform the SELECT TYPE into ASSOCIATE first. */
8815 break;
8816 case EXEC_OMP_WORKSHARE:
8817 omp_workshare_save = omp_workshare_flag;
8818 omp_workshare_flag = 1;
8819 /* FALLTHROUGH */
8820 default:
8821 gfc_resolve_blocks (code->block, ns);
8822 break;
8825 if (omp_workshare_save != -1)
8826 omp_workshare_flag = omp_workshare_save;
8829 t = SUCCESS;
8830 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
8831 t = gfc_resolve_expr (code->expr1);
8832 forall_flag = forall_save;
8834 if (gfc_resolve_expr (code->expr2) == FAILURE)
8835 t = FAILURE;
8837 if (code->op == EXEC_ALLOCATE
8838 && gfc_resolve_expr (code->expr3) == FAILURE)
8839 t = FAILURE;
8841 switch (code->op)
8843 case EXEC_NOP:
8844 case EXEC_END_BLOCK:
8845 case EXEC_CYCLE:
8846 case EXEC_PAUSE:
8847 case EXEC_STOP:
8848 case EXEC_ERROR_STOP:
8849 case EXEC_EXIT:
8850 case EXEC_CONTINUE:
8851 case EXEC_DT_END:
8852 case EXEC_ASSIGN_CALL:
8853 case EXEC_CRITICAL:
8854 break;
8856 case EXEC_SYNC_ALL:
8857 case EXEC_SYNC_IMAGES:
8858 case EXEC_SYNC_MEMORY:
8859 resolve_sync (code);
8860 break;
8862 case EXEC_ENTRY:
8863 /* Keep track of which entry we are up to. */
8864 current_entry_id = code->ext.entry->id;
8865 break;
8867 case EXEC_WHERE:
8868 resolve_where (code, NULL);
8869 break;
8871 case EXEC_GOTO:
8872 if (code->expr1 != NULL)
8874 if (code->expr1->ts.type != BT_INTEGER)
8875 gfc_error ("ASSIGNED GOTO statement at %L requires an "
8876 "INTEGER variable", &code->expr1->where);
8877 else if (code->expr1->symtree->n.sym->attr.assign != 1)
8878 gfc_error ("Variable '%s' has not been assigned a target "
8879 "label at %L", code->expr1->symtree->n.sym->name,
8880 &code->expr1->where);
8882 else
8883 resolve_branch (code->label1, code);
8884 break;
8886 case EXEC_RETURN:
8887 if (code->expr1 != NULL
8888 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
8889 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
8890 "INTEGER return specifier", &code->expr1->where);
8891 break;
8893 case EXEC_INIT_ASSIGN:
8894 case EXEC_END_PROCEDURE:
8895 break;
8897 case EXEC_ASSIGN:
8898 if (t == FAILURE)
8899 break;
8901 if (gfc_check_vardef_context (code->expr1, false, _("assignment"))
8902 == FAILURE)
8903 break;
8905 if (resolve_ordinary_assign (code, ns))
8907 if (code->op == EXEC_COMPCALL)
8908 goto compcall;
8909 else
8910 goto call;
8912 break;
8914 case EXEC_LABEL_ASSIGN:
8915 if (code->label1->defined == ST_LABEL_UNKNOWN)
8916 gfc_error ("Label %d referenced at %L is never defined",
8917 code->label1->value, &code->label1->where);
8918 if (t == SUCCESS
8919 && (code->expr1->expr_type != EXPR_VARIABLE
8920 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8921 || code->expr1->symtree->n.sym->ts.kind
8922 != gfc_default_integer_kind
8923 || code->expr1->symtree->n.sym->as != NULL))
8924 gfc_error ("ASSIGN statement at %L requires a scalar "
8925 "default INTEGER variable", &code->expr1->where);
8926 break;
8928 case EXEC_POINTER_ASSIGN:
8930 gfc_expr* e;
8932 if (t == FAILURE)
8933 break;
8935 /* This is both a variable definition and pointer assignment
8936 context, so check both of them. For rank remapping, a final
8937 array ref may be present on the LHS and fool gfc_expr_attr
8938 used in gfc_check_vardef_context. Remove it. */
8939 e = remove_last_array_ref (code->expr1);
8940 t = gfc_check_vardef_context (e, true, _("pointer assignment"));
8941 if (t == SUCCESS)
8942 t = gfc_check_vardef_context (e, false, _("pointer assignment"));
8943 gfc_free_expr (e);
8944 if (t == FAILURE)
8945 break;
8947 gfc_check_pointer_assign (code->expr1, code->expr2);
8948 break;
8951 case EXEC_ARITHMETIC_IF:
8952 if (t == SUCCESS
8953 && code->expr1->ts.type != BT_INTEGER
8954 && code->expr1->ts.type != BT_REAL)
8955 gfc_error ("Arithmetic IF statement at %L requires a numeric "
8956 "expression", &code->expr1->where);
8958 resolve_branch (code->label1, code);
8959 resolve_branch (code->label2, code);
8960 resolve_branch (code->label3, code);
8961 break;
8963 case EXEC_IF:
8964 if (t == SUCCESS && code->expr1 != NULL
8965 && (code->expr1->ts.type != BT_LOGICAL
8966 || code->expr1->rank != 0))
8967 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
8968 &code->expr1->where);
8969 break;
8971 case EXEC_CALL:
8972 call:
8973 resolve_call (code);
8974 break;
8976 case EXEC_COMPCALL:
8977 compcall:
8978 resolve_typebound_subroutine (code);
8979 break;
8981 case EXEC_CALL_PPC:
8982 resolve_ppc_call (code);
8983 break;
8985 case EXEC_SELECT:
8986 /* Select is complicated. Also, a SELECT construct could be
8987 a transformed computed GOTO. */
8988 resolve_select (code);
8989 break;
8991 case EXEC_SELECT_TYPE:
8992 resolve_select_type (code, ns);
8993 break;
8995 case EXEC_BLOCK:
8996 resolve_block_construct (code);
8997 break;
8999 case EXEC_DO:
9000 if (code->ext.iterator != NULL)
9002 gfc_iterator *iter = code->ext.iterator;
9003 if (gfc_resolve_iterator (iter, true) != FAILURE)
9004 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9006 break;
9008 case EXEC_DO_WHILE:
9009 if (code->expr1 == NULL)
9010 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
9011 if (t == SUCCESS
9012 && (code->expr1->rank != 0
9013 || code->expr1->ts.type != BT_LOGICAL))
9014 gfc_error ("Exit condition of DO WHILE loop at %L must be "
9015 "a scalar LOGICAL expression", &code->expr1->where);
9016 break;
9018 case EXEC_ALLOCATE:
9019 if (t == SUCCESS)
9020 resolve_allocate_deallocate (code, "ALLOCATE");
9022 break;
9024 case EXEC_DEALLOCATE:
9025 if (t == SUCCESS)
9026 resolve_allocate_deallocate (code, "DEALLOCATE");
9028 break;
9030 case EXEC_OPEN:
9031 if (gfc_resolve_open (code->ext.open) == FAILURE)
9032 break;
9034 resolve_branch (code->ext.open->err, code);
9035 break;
9037 case EXEC_CLOSE:
9038 if (gfc_resolve_close (code->ext.close) == FAILURE)
9039 break;
9041 resolve_branch (code->ext.close->err, code);
9042 break;
9044 case EXEC_BACKSPACE:
9045 case EXEC_ENDFILE:
9046 case EXEC_REWIND:
9047 case EXEC_FLUSH:
9048 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
9049 break;
9051 resolve_branch (code->ext.filepos->err, code);
9052 break;
9054 case EXEC_INQUIRE:
9055 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9056 break;
9058 resolve_branch (code->ext.inquire->err, code);
9059 break;
9061 case EXEC_IOLENGTH:
9062 gcc_assert (code->ext.inquire != NULL);
9063 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
9064 break;
9066 resolve_branch (code->ext.inquire->err, code);
9067 break;
9069 case EXEC_WAIT:
9070 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
9071 break;
9073 resolve_branch (code->ext.wait->err, code);
9074 resolve_branch (code->ext.wait->end, code);
9075 resolve_branch (code->ext.wait->eor, code);
9076 break;
9078 case EXEC_READ:
9079 case EXEC_WRITE:
9080 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
9081 break;
9083 resolve_branch (code->ext.dt->err, code);
9084 resolve_branch (code->ext.dt->end, code);
9085 resolve_branch (code->ext.dt->eor, code);
9086 break;
9088 case EXEC_TRANSFER:
9089 resolve_transfer (code);
9090 break;
9092 case EXEC_FORALL:
9093 resolve_forall_iterators (code->ext.forall_iterator);
9095 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
9096 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
9097 "expression", &code->expr1->where);
9098 break;
9100 case EXEC_OMP_ATOMIC:
9101 case EXEC_OMP_BARRIER:
9102 case EXEC_OMP_CRITICAL:
9103 case EXEC_OMP_FLUSH:
9104 case EXEC_OMP_DO:
9105 case EXEC_OMP_MASTER:
9106 case EXEC_OMP_ORDERED:
9107 case EXEC_OMP_SECTIONS:
9108 case EXEC_OMP_SINGLE:
9109 case EXEC_OMP_TASKWAIT:
9110 case EXEC_OMP_WORKSHARE:
9111 gfc_resolve_omp_directive (code, ns);
9112 break;
9114 case EXEC_OMP_PARALLEL:
9115 case EXEC_OMP_PARALLEL_DO:
9116 case EXEC_OMP_PARALLEL_SECTIONS:
9117 case EXEC_OMP_PARALLEL_WORKSHARE:
9118 case EXEC_OMP_TASK:
9119 omp_workshare_save = omp_workshare_flag;
9120 omp_workshare_flag = 0;
9121 gfc_resolve_omp_directive (code, ns);
9122 omp_workshare_flag = omp_workshare_save;
9123 break;
9125 default:
9126 gfc_internal_error ("resolve_code(): Bad statement code");
9130 cs_base = frame.prev;
9134 /* Resolve initial values and make sure they are compatible with
9135 the variable. */
9137 static void
9138 resolve_values (gfc_symbol *sym)
9140 gfc_try t;
9142 if (sym->value == NULL)
9143 return;
9145 if (sym->value->expr_type == EXPR_STRUCTURE)
9146 t= resolve_structure_cons (sym->value, 1);
9147 else
9148 t = gfc_resolve_expr (sym->value);
9150 if (t == FAILURE)
9151 return;
9153 gfc_check_assign_symbol (sym, sym->value);
9157 /* Verify the binding labels for common blocks that are BIND(C). The label
9158 for a BIND(C) common block must be identical in all scoping units in which
9159 the common block is declared. Further, the binding label can not collide
9160 with any other global entity in the program. */
9162 static void
9163 resolve_bind_c_comms (gfc_symtree *comm_block_tree)
9165 if (comm_block_tree->n.common->is_bind_c == 1)
9167 gfc_gsymbol *binding_label_gsym;
9168 gfc_gsymbol *comm_name_gsym;
9170 /* See if a global symbol exists by the common block's name. It may
9171 be NULL if the common block is use-associated. */
9172 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
9173 comm_block_tree->n.common->name);
9174 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
9175 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
9176 "with the global entity '%s' at %L",
9177 comm_block_tree->n.common->binding_label,
9178 comm_block_tree->n.common->name,
9179 &(comm_block_tree->n.common->where),
9180 comm_name_gsym->name, &(comm_name_gsym->where));
9181 else if (comm_name_gsym != NULL
9182 && strcmp (comm_name_gsym->name,
9183 comm_block_tree->n.common->name) == 0)
9185 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
9186 as expected. */
9187 if (comm_name_gsym->binding_label == NULL)
9188 /* No binding label for common block stored yet; save this one. */
9189 comm_name_gsym->binding_label =
9190 comm_block_tree->n.common->binding_label;
9191 else
9192 if (strcmp (comm_name_gsym->binding_label,
9193 comm_block_tree->n.common->binding_label) != 0)
9195 /* Common block names match but binding labels do not. */
9196 gfc_error ("Binding label '%s' for common block '%s' at %L "
9197 "does not match the binding label '%s' for common "
9198 "block '%s' at %L",
9199 comm_block_tree->n.common->binding_label,
9200 comm_block_tree->n.common->name,
9201 &(comm_block_tree->n.common->where),
9202 comm_name_gsym->binding_label,
9203 comm_name_gsym->name,
9204 &(comm_name_gsym->where));
9205 return;
9209 /* There is no binding label (NAME="") so we have nothing further to
9210 check and nothing to add as a global symbol for the label. */
9211 if (comm_block_tree->n.common->binding_label[0] == '\0' )
9212 return;
9214 binding_label_gsym =
9215 gfc_find_gsymbol (gfc_gsym_root,
9216 comm_block_tree->n.common->binding_label);
9217 if (binding_label_gsym == NULL)
9219 /* Need to make a global symbol for the binding label to prevent
9220 it from colliding with another. */
9221 binding_label_gsym =
9222 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
9223 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
9224 binding_label_gsym->type = GSYM_COMMON;
9226 else
9228 /* If comm_name_gsym is NULL, the name common block is use
9229 associated and the name could be colliding. */
9230 if (binding_label_gsym->type != GSYM_COMMON)
9231 gfc_error ("Binding label '%s' for common block '%s' at %L "
9232 "collides with the global entity '%s' at %L",
9233 comm_block_tree->n.common->binding_label,
9234 comm_block_tree->n.common->name,
9235 &(comm_block_tree->n.common->where),
9236 binding_label_gsym->name,
9237 &(binding_label_gsym->where));
9238 else if (comm_name_gsym != NULL
9239 && (strcmp (binding_label_gsym->name,
9240 comm_name_gsym->binding_label) != 0)
9241 && (strcmp (binding_label_gsym->sym_name,
9242 comm_name_gsym->name) != 0))
9243 gfc_error ("Binding label '%s' for common block '%s' at %L "
9244 "collides with global entity '%s' at %L",
9245 binding_label_gsym->name, binding_label_gsym->sym_name,
9246 &(comm_block_tree->n.common->where),
9247 comm_name_gsym->name, &(comm_name_gsym->where));
9251 return;
9255 /* Verify any BIND(C) derived types in the namespace so we can report errors
9256 for them once, rather than for each variable declared of that type. */
9258 static void
9259 resolve_bind_c_derived_types (gfc_symbol *derived_sym)
9261 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
9262 && derived_sym->attr.is_bind_c == 1)
9263 verify_bind_c_derived_type (derived_sym);
9265 return;
9269 /* Verify that any binding labels used in a given namespace do not collide
9270 with the names or binding labels of any global symbols. */
9272 static void
9273 gfc_verify_binding_labels (gfc_symbol *sym)
9275 int has_error = 0;
9277 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
9278 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
9280 gfc_gsymbol *bind_c_sym;
9282 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
9283 if (bind_c_sym != NULL
9284 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
9286 if (sym->attr.if_source == IFSRC_DECL
9287 && (bind_c_sym->type != GSYM_SUBROUTINE
9288 && bind_c_sym->type != GSYM_FUNCTION)
9289 && ((sym->attr.contained == 1
9290 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
9291 || (sym->attr.use_assoc == 1
9292 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
9294 /* Make sure global procedures don't collide with anything. */
9295 gfc_error ("Binding label '%s' at %L collides with the global "
9296 "entity '%s' at %L", sym->binding_label,
9297 &(sym->declared_at), bind_c_sym->name,
9298 &(bind_c_sym->where));
9299 has_error = 1;
9301 else if (sym->attr.contained == 0
9302 && (sym->attr.if_source == IFSRC_IFBODY
9303 && sym->attr.flavor == FL_PROCEDURE)
9304 && (bind_c_sym->sym_name != NULL
9305 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
9307 /* Make sure procedures in interface bodies don't collide. */
9308 gfc_error ("Binding label '%s' in interface body at %L collides "
9309 "with the global entity '%s' at %L",
9310 sym->binding_label,
9311 &(sym->declared_at), bind_c_sym->name,
9312 &(bind_c_sym->where));
9313 has_error = 1;
9315 else if (sym->attr.contained == 0
9316 && sym->attr.if_source == IFSRC_UNKNOWN)
9317 if ((sym->attr.use_assoc && bind_c_sym->mod_name
9318 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
9319 || sym->attr.use_assoc == 0)
9321 gfc_error ("Binding label '%s' at %L collides with global "
9322 "entity '%s' at %L", sym->binding_label,
9323 &(sym->declared_at), bind_c_sym->name,
9324 &(bind_c_sym->where));
9325 has_error = 1;
9328 if (has_error != 0)
9329 /* Clear the binding label to prevent checking multiple times. */
9330 sym->binding_label[0] = '\0';
9332 else if (bind_c_sym == NULL)
9334 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
9335 bind_c_sym->where = sym->declared_at;
9336 bind_c_sym->sym_name = sym->name;
9338 if (sym->attr.use_assoc == 1)
9339 bind_c_sym->mod_name = sym->module;
9340 else
9341 if (sym->ns->proc_name != NULL)
9342 bind_c_sym->mod_name = sym->ns->proc_name->name;
9344 if (sym->attr.contained == 0)
9346 if (sym->attr.subroutine)
9347 bind_c_sym->type = GSYM_SUBROUTINE;
9348 else if (sym->attr.function)
9349 bind_c_sym->type = GSYM_FUNCTION;
9353 return;
9357 /* Resolve an index expression. */
9359 static gfc_try
9360 resolve_index_expr (gfc_expr *e)
9362 if (gfc_resolve_expr (e) == FAILURE)
9363 return FAILURE;
9365 if (gfc_simplify_expr (e, 0) == FAILURE)
9366 return FAILURE;
9368 if (gfc_specification_expr (e) == FAILURE)
9369 return FAILURE;
9371 return SUCCESS;
9374 /* Resolve a charlen structure. */
9376 static gfc_try
9377 resolve_charlen (gfc_charlen *cl)
9379 int i, k;
9381 if (cl->resolved)
9382 return SUCCESS;
9384 cl->resolved = 1;
9386 specification_expr = 1;
9388 if (resolve_index_expr (cl->length) == FAILURE)
9390 specification_expr = 0;
9391 return FAILURE;
9394 /* "If the character length parameter value evaluates to a negative
9395 value, the length of character entities declared is zero." */
9396 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
9398 if (gfc_option.warn_surprising)
9399 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
9400 " the length has been set to zero",
9401 &cl->length->where, i);
9402 gfc_replace_expr (cl->length,
9403 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
9406 /* Check that the character length is not too large. */
9407 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
9408 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
9409 && cl->length->ts.type == BT_INTEGER
9410 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
9412 gfc_error ("String length at %L is too large", &cl->length->where);
9413 return FAILURE;
9416 return SUCCESS;
9420 /* Test for non-constant shape arrays. */
9422 static bool
9423 is_non_constant_shape_array (gfc_symbol *sym)
9425 gfc_expr *e;
9426 int i;
9427 bool not_constant;
9429 not_constant = false;
9430 if (sym->as != NULL)
9432 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
9433 has not been simplified; parameter array references. Do the
9434 simplification now. */
9435 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
9437 e = sym->as->lower[i];
9438 if (e && (resolve_index_expr (e) == FAILURE
9439 || !gfc_is_constant_expr (e)))
9440 not_constant = true;
9441 e = sym->as->upper[i];
9442 if (e && (resolve_index_expr (e) == FAILURE
9443 || !gfc_is_constant_expr (e)))
9444 not_constant = true;
9447 return not_constant;
9450 /* Given a symbol and an initialization expression, add code to initialize
9451 the symbol to the function entry. */
9452 static void
9453 build_init_assign (gfc_symbol *sym, gfc_expr *init)
9455 gfc_expr *lval;
9456 gfc_code *init_st;
9457 gfc_namespace *ns = sym->ns;
9459 /* Search for the function namespace if this is a contained
9460 function without an explicit result. */
9461 if (sym->attr.function && sym == sym->result
9462 && sym->name != sym->ns->proc_name->name)
9464 ns = ns->contained;
9465 for (;ns; ns = ns->sibling)
9466 if (strcmp (ns->proc_name->name, sym->name) == 0)
9467 break;
9470 if (ns == NULL)
9472 gfc_free_expr (init);
9473 return;
9476 /* Build an l-value expression for the result. */
9477 lval = gfc_lval_expr_from_sym (sym);
9479 /* Add the code at scope entry. */
9480 init_st = gfc_get_code ();
9481 init_st->next = ns->code;
9482 ns->code = init_st;
9484 /* Assign the default initializer to the l-value. */
9485 init_st->loc = sym->declared_at;
9486 init_st->op = EXEC_INIT_ASSIGN;
9487 init_st->expr1 = lval;
9488 init_st->expr2 = init;
9491 /* Assign the default initializer to a derived type variable or result. */
9493 static void
9494 apply_default_init (gfc_symbol *sym)
9496 gfc_expr *init = NULL;
9498 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9499 return;
9501 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
9502 init = gfc_default_initializer (&sym->ts);
9504 if (init == NULL && sym->ts.type != BT_CLASS)
9505 return;
9507 build_init_assign (sym, init);
9508 sym->attr.referenced = 1;
9511 /* Build an initializer for a local integer, real, complex, logical, or
9512 character variable, based on the command line flags finit-local-zero,
9513 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
9514 null if the symbol should not have a default initialization. */
9515 static gfc_expr *
9516 build_default_init_expr (gfc_symbol *sym)
9518 int char_len;
9519 gfc_expr *init_expr;
9520 int i;
9522 /* These symbols should never have a default initialization. */
9523 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
9524 || sym->attr.external
9525 || sym->attr.dummy
9526 || sym->attr.pointer
9527 || sym->attr.in_equivalence
9528 || sym->attr.in_common
9529 || sym->attr.data
9530 || sym->module
9531 || sym->attr.cray_pointee
9532 || sym->attr.cray_pointer)
9533 return NULL;
9535 /* Now we'll try to build an initializer expression. */
9536 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
9537 &sym->declared_at);
9539 /* We will only initialize integers, reals, complex, logicals, and
9540 characters, and only if the corresponding command-line flags
9541 were set. Otherwise, we free init_expr and return null. */
9542 switch (sym->ts.type)
9544 case BT_INTEGER:
9545 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
9546 mpz_set_si (init_expr->value.integer,
9547 gfc_option.flag_init_integer_value);
9548 else
9550 gfc_free_expr (init_expr);
9551 init_expr = NULL;
9553 break;
9555 case BT_REAL:
9556 switch (gfc_option.flag_init_real)
9558 case GFC_INIT_REAL_SNAN:
9559 init_expr->is_snan = 1;
9560 /* Fall through. */
9561 case GFC_INIT_REAL_NAN:
9562 mpfr_set_nan (init_expr->value.real);
9563 break;
9565 case GFC_INIT_REAL_INF:
9566 mpfr_set_inf (init_expr->value.real, 1);
9567 break;
9569 case GFC_INIT_REAL_NEG_INF:
9570 mpfr_set_inf (init_expr->value.real, -1);
9571 break;
9573 case GFC_INIT_REAL_ZERO:
9574 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
9575 break;
9577 default:
9578 gfc_free_expr (init_expr);
9579 init_expr = NULL;
9580 break;
9582 break;
9584 case BT_COMPLEX:
9585 switch (gfc_option.flag_init_real)
9587 case GFC_INIT_REAL_SNAN:
9588 init_expr->is_snan = 1;
9589 /* Fall through. */
9590 case GFC_INIT_REAL_NAN:
9591 mpfr_set_nan (mpc_realref (init_expr->value.complex));
9592 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
9593 break;
9595 case GFC_INIT_REAL_INF:
9596 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
9597 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
9598 break;
9600 case GFC_INIT_REAL_NEG_INF:
9601 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
9602 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
9603 break;
9605 case GFC_INIT_REAL_ZERO:
9606 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
9607 break;
9609 default:
9610 gfc_free_expr (init_expr);
9611 init_expr = NULL;
9612 break;
9614 break;
9616 case BT_LOGICAL:
9617 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
9618 init_expr->value.logical = 0;
9619 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
9620 init_expr->value.logical = 1;
9621 else
9623 gfc_free_expr (init_expr);
9624 init_expr = NULL;
9626 break;
9628 case BT_CHARACTER:
9629 /* For characters, the length must be constant in order to
9630 create a default initializer. */
9631 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
9632 && sym->ts.u.cl->length
9633 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9635 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
9636 init_expr->value.character.length = char_len;
9637 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
9638 for (i = 0; i < char_len; i++)
9639 init_expr->value.character.string[i]
9640 = (unsigned char) gfc_option.flag_init_character_value;
9642 else
9644 gfc_free_expr (init_expr);
9645 init_expr = NULL;
9647 break;
9649 default:
9650 gfc_free_expr (init_expr);
9651 init_expr = NULL;
9653 return init_expr;
9656 /* Add an initialization expression to a local variable. */
9657 static void
9658 apply_default_init_local (gfc_symbol *sym)
9660 gfc_expr *init = NULL;
9662 /* The symbol should be a variable or a function return value. */
9663 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
9664 || (sym->attr.function && sym->result != sym))
9665 return;
9667 /* Try to build the initializer expression. If we can't initialize
9668 this symbol, then init will be NULL. */
9669 init = build_default_init_expr (sym);
9670 if (init == NULL)
9671 return;
9673 /* For saved variables, we don't want to add an initializer at
9674 function entry, so we just add a static initializer. */
9675 if (sym->attr.save || sym->ns->save_all
9676 || gfc_option.flag_max_stack_var_size == 0)
9678 /* Don't clobber an existing initializer! */
9679 gcc_assert (sym->value == NULL);
9680 sym->value = init;
9681 return;
9684 build_init_assign (sym, init);
9687 /* Resolution of common features of flavors variable and procedure. */
9689 static gfc_try
9690 resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
9692 /* Constraints on deferred shape variable. */
9693 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
9695 if (sym->attr.allocatable)
9697 if (sym->attr.dimension)
9699 gfc_error ("Allocatable array '%s' at %L must have "
9700 "a deferred shape", sym->name, &sym->declared_at);
9701 return FAILURE;
9703 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
9704 "may not be ALLOCATABLE", sym->name,
9705 &sym->declared_at) == FAILURE)
9706 return FAILURE;
9709 if (sym->attr.pointer && sym->attr.dimension)
9711 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
9712 sym->name, &sym->declared_at);
9713 return FAILURE;
9716 else
9718 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
9719 && !sym->attr.dummy && sym->ts.type != BT_CLASS && !sym->assoc)
9721 gfc_error ("Array '%s' at %L cannot have a deferred shape",
9722 sym->name, &sym->declared_at);
9723 return FAILURE;
9727 /* Constraints on polymorphic variables. */
9728 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
9730 /* F03:C502. */
9731 if (sym->attr.class_ok
9732 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
9734 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
9735 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
9736 &sym->declared_at);
9737 return FAILURE;
9740 /* F03:C509. */
9741 /* Assume that use associated symbols were checked in the module ns.
9742 Class-variables that are associate-names are also something special
9743 and excepted from the test. */
9744 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
9746 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
9747 "or pointer", sym->name, &sym->declared_at);
9748 return FAILURE;
9752 return SUCCESS;
9756 /* Additional checks for symbols with flavor variable and derived
9757 type. To be called from resolve_fl_variable. */
9759 static gfc_try
9760 resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
9762 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
9764 /* Check to see if a derived type is blocked from being host
9765 associated by the presence of another class I symbol in the same
9766 namespace. 14.6.1.3 of the standard and the discussion on
9767 comp.lang.fortran. */
9768 if (sym->ns != sym->ts.u.derived->ns
9769 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
9771 gfc_symbol *s;
9772 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
9773 if (s && s->attr.flavor != FL_DERIVED)
9775 gfc_error ("The type '%s' cannot be host associated at %L "
9776 "because it is blocked by an incompatible object "
9777 "of the same name declared at %L",
9778 sym->ts.u.derived->name, &sym->declared_at,
9779 &s->declared_at);
9780 return FAILURE;
9784 /* 4th constraint in section 11.3: "If an object of a type for which
9785 component-initialization is specified (R429) appears in the
9786 specification-part of a module and does not have the ALLOCATABLE
9787 or POINTER attribute, the object shall have the SAVE attribute."
9789 The check for initializers is performed with
9790 gfc_has_default_initializer because gfc_default_initializer generates
9791 a hidden default for allocatable components. */
9792 if (!(sym->value || no_init_flag) && sym->ns->proc_name
9793 && sym->ns->proc_name->attr.flavor == FL_MODULE
9794 && !sym->ns->save_all && !sym->attr.save
9795 && !sym->attr.pointer && !sym->attr.allocatable
9796 && gfc_has_default_initializer (sym->ts.u.derived)
9797 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: Implied SAVE for "
9798 "module variable '%s' at %L, needed due to "
9799 "the default initialization", sym->name,
9800 &sym->declared_at) == FAILURE)
9801 return FAILURE;
9803 /* Assign default initializer. */
9804 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9805 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
9807 sym->value = gfc_default_initializer (&sym->ts);
9810 return SUCCESS;
9814 /* Resolve symbols with flavor variable. */
9816 static gfc_try
9817 resolve_fl_variable (gfc_symbol *sym, int mp_flag)
9819 int no_init_flag, automatic_flag;
9820 gfc_expr *e;
9821 const char *auto_save_msg;
9823 auto_save_msg = "Automatic object '%s' at %L cannot have the "
9824 "SAVE attribute";
9826 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9827 return FAILURE;
9829 /* Set this flag to check that variables are parameters of all entries.
9830 This check is effected by the call to gfc_resolve_expr through
9831 is_non_constant_shape_array. */
9832 specification_expr = 1;
9834 if (sym->ns->proc_name
9835 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9836 || sym->ns->proc_name->attr.is_main_program)
9837 && !sym->attr.use_assoc
9838 && !sym->attr.allocatable
9839 && !sym->attr.pointer
9840 && is_non_constant_shape_array (sym))
9842 /* The shape of a main program or module array needs to be
9843 constant. */
9844 gfc_error ("The module or main program array '%s' at %L must "
9845 "have constant shape", sym->name, &sym->declared_at);
9846 specification_expr = 0;
9847 return FAILURE;
9850 if (sym->ts.type == BT_CHARACTER)
9852 /* Make sure that character string variables with assumed length are
9853 dummy arguments. */
9854 e = sym->ts.u.cl->length;
9855 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
9857 gfc_error ("Entity with assumed character length at %L must be a "
9858 "dummy argument or a PARAMETER", &sym->declared_at);
9859 return FAILURE;
9862 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
9864 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9865 return FAILURE;
9868 if (!gfc_is_constant_expr (e)
9869 && !(e->expr_type == EXPR_VARIABLE
9870 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9871 && sym->ns->proc_name
9872 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9873 || sym->ns->proc_name->attr.is_main_program)
9874 && !sym->attr.use_assoc)
9876 gfc_error ("'%s' at %L must have constant character length "
9877 "in this context", sym->name, &sym->declared_at);
9878 return FAILURE;
9882 if (sym->value == NULL && sym->attr.referenced)
9883 apply_default_init_local (sym); /* Try to apply a default initialization. */
9885 /* Determine if the symbol may not have an initializer. */
9886 no_init_flag = automatic_flag = 0;
9887 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9888 || sym->attr.intrinsic || sym->attr.result)
9889 no_init_flag = 1;
9890 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
9891 && is_non_constant_shape_array (sym))
9893 no_init_flag = automatic_flag = 1;
9895 /* Also, they must not have the SAVE attribute.
9896 SAVE_IMPLICIT is checked below. */
9897 if (sym->attr.save == SAVE_EXPLICIT)
9899 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9900 return FAILURE;
9904 /* Ensure that any initializer is simplified. */
9905 if (sym->value)
9906 gfc_simplify_expr (sym->value, 1);
9908 /* Reject illegal initializers. */
9909 if (!sym->mark && sym->value)
9911 if (sym->attr.allocatable)
9912 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9913 sym->name, &sym->declared_at);
9914 else if (sym->attr.external)
9915 gfc_error ("External '%s' at %L cannot have an initializer",
9916 sym->name, &sym->declared_at);
9917 else if (sym->attr.dummy
9918 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
9919 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9920 sym->name, &sym->declared_at);
9921 else if (sym->attr.intrinsic)
9922 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9923 sym->name, &sym->declared_at);
9924 else if (sym->attr.result)
9925 gfc_error ("Function result '%s' at %L cannot have an initializer",
9926 sym->name, &sym->declared_at);
9927 else if (automatic_flag)
9928 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9929 sym->name, &sym->declared_at);
9930 else
9931 goto no_init_error;
9932 return FAILURE;
9935 no_init_error:
9936 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9937 return resolve_fl_variable_derived (sym, no_init_flag);
9939 return SUCCESS;
9943 /* Resolve a procedure. */
9945 static gfc_try
9946 resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9948 gfc_formal_arglist *arg;
9950 if (sym->attr.function
9951 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
9952 return FAILURE;
9954 if (sym->ts.type == BT_CHARACTER)
9956 gfc_charlen *cl = sym->ts.u.cl;
9958 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9959 && resolve_charlen (cl) == FAILURE)
9960 return FAILURE;
9962 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9963 && sym->attr.proc == PROC_ST_FUNCTION)
9965 gfc_error ("Character-valued statement function '%s' at %L must "
9966 "have constant length", sym->name, &sym->declared_at);
9967 return FAILURE;
9971 /* Ensure that derived type for are not of a private type. Internal
9972 module procedures are excluded by 2.2.3.3 - i.e., they are not
9973 externally accessible and can access all the objects accessible in
9974 the host. */
9975 if (!(sym->ns->parent
9976 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9977 && gfc_check_access(sym->attr.access, sym->ns->default_access))
9979 gfc_interface *iface;
9981 for (arg = sym->formal; arg; arg = arg->next)
9983 if (arg->sym
9984 && arg->sym->ts.type == BT_DERIVED
9985 && !arg->sym->ts.u.derived->attr.use_assoc
9986 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9987 arg->sym->ts.u.derived->ns->default_access)
9988 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9989 "PRIVATE type and cannot be a dummy argument"
9990 " of '%s', which is PUBLIC at %L",
9991 arg->sym->name, sym->name, &sym->declared_at)
9992 == FAILURE)
9994 /* Stop this message from recurring. */
9995 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
9996 return FAILURE;
10000 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10001 PRIVATE to the containing module. */
10002 for (iface = sym->generic; iface; iface = iface->next)
10004 for (arg = iface->sym->formal; arg; arg = arg->next)
10006 if (arg->sym
10007 && arg->sym->ts.type == BT_DERIVED
10008 && !arg->sym->ts.u.derived->attr.use_assoc
10009 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
10010 arg->sym->ts.u.derived->ns->default_access)
10011 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10012 "'%s' in PUBLIC interface '%s' at %L "
10013 "takes dummy arguments of '%s' which is "
10014 "PRIVATE", iface->sym->name, sym->name,
10015 &iface->sym->declared_at,
10016 gfc_typename (&arg->sym->ts)) == FAILURE)
10018 /* Stop this message from recurring. */
10019 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10020 return FAILURE;
10025 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10026 PRIVATE to the containing module. */
10027 for (iface = sym->generic; iface; iface = iface->next)
10029 for (arg = iface->sym->formal; arg; arg = arg->next)
10031 if (arg->sym
10032 && arg->sym->ts.type == BT_DERIVED
10033 && !arg->sym->ts.u.derived->attr.use_assoc
10034 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
10035 arg->sym->ts.u.derived->ns->default_access)
10036 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
10037 "'%s' in PUBLIC interface '%s' at %L "
10038 "takes dummy arguments of '%s' which is "
10039 "PRIVATE", iface->sym->name, sym->name,
10040 &iface->sym->declared_at,
10041 gfc_typename (&arg->sym->ts)) == FAILURE)
10043 /* Stop this message from recurring. */
10044 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
10045 return FAILURE;
10051 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
10052 && !sym->attr.proc_pointer)
10054 gfc_error ("Function '%s' at %L cannot have an initializer",
10055 sym->name, &sym->declared_at);
10056 return FAILURE;
10059 /* An external symbol may not have an initializer because it is taken to be
10060 a procedure. Exception: Procedure Pointers. */
10061 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
10063 gfc_error ("External object '%s' at %L may not have an initializer",
10064 sym->name, &sym->declared_at);
10065 return FAILURE;
10068 /* An elemental function is required to return a scalar 12.7.1 */
10069 if (sym->attr.elemental && sym->attr.function && sym->as)
10071 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
10072 "result", sym->name, &sym->declared_at);
10073 /* Reset so that the error only occurs once. */
10074 sym->attr.elemental = 0;
10075 return FAILURE;
10078 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
10079 char-len-param shall not be array-valued, pointer-valued, recursive
10080 or pure. ....snip... A character value of * may only be used in the
10081 following ways: (i) Dummy arg of procedure - dummy associates with
10082 actual length; (ii) To declare a named constant; or (iii) External
10083 function - but length must be declared in calling scoping unit. */
10084 if (sym->attr.function
10085 && sym->ts.type == BT_CHARACTER
10086 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
10088 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
10089 || (sym->attr.recursive) || (sym->attr.pure))
10091 if (sym->as && sym->as->rank)
10092 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10093 "array-valued", sym->name, &sym->declared_at);
10095 if (sym->attr.pointer)
10096 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10097 "pointer-valued", sym->name, &sym->declared_at);
10099 if (sym->attr.pure)
10100 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10101 "pure", sym->name, &sym->declared_at);
10103 if (sym->attr.recursive)
10104 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
10105 "recursive", sym->name, &sym->declared_at);
10107 return FAILURE;
10110 /* Appendix B.2 of the standard. Contained functions give an
10111 error anyway. Fixed-form is likely to be F77/legacy. */
10112 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
10113 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
10114 "CHARACTER(*) function '%s' at %L",
10115 sym->name, &sym->declared_at);
10118 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
10120 gfc_formal_arglist *curr_arg;
10121 int has_non_interop_arg = 0;
10123 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10124 sym->common_block) == FAILURE)
10126 /* Clear these to prevent looking at them again if there was an
10127 error. */
10128 sym->attr.is_bind_c = 0;
10129 sym->attr.is_c_interop = 0;
10130 sym->ts.is_c_interop = 0;
10132 else
10134 /* So far, no errors have been found. */
10135 sym->attr.is_c_interop = 1;
10136 sym->ts.is_c_interop = 1;
10139 curr_arg = sym->formal;
10140 while (curr_arg != NULL)
10142 /* Skip implicitly typed dummy args here. */
10143 if (curr_arg->sym->attr.implicit_type == 0)
10144 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
10145 /* If something is found to fail, record the fact so we
10146 can mark the symbol for the procedure as not being
10147 BIND(C) to try and prevent multiple errors being
10148 reported. */
10149 has_non_interop_arg = 1;
10151 curr_arg = curr_arg->next;
10154 /* See if any of the arguments were not interoperable and if so, clear
10155 the procedure symbol to prevent duplicate error messages. */
10156 if (has_non_interop_arg != 0)
10158 sym->attr.is_c_interop = 0;
10159 sym->ts.is_c_interop = 0;
10160 sym->attr.is_bind_c = 0;
10164 if (!sym->attr.proc_pointer)
10166 if (sym->attr.save == SAVE_EXPLICIT)
10168 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
10169 "in '%s' at %L", sym->name, &sym->declared_at);
10170 return FAILURE;
10172 if (sym->attr.intent)
10174 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
10175 "in '%s' at %L", sym->name, &sym->declared_at);
10176 return FAILURE;
10178 if (sym->attr.subroutine && sym->attr.result)
10180 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
10181 "in '%s' at %L", sym->name, &sym->declared_at);
10182 return FAILURE;
10184 if (sym->attr.external && sym->attr.function
10185 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
10186 || sym->attr.contained))
10188 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
10189 "in '%s' at %L", sym->name, &sym->declared_at);
10190 return FAILURE;
10192 if (strcmp ("ppr@", sym->name) == 0)
10194 gfc_error ("Procedure pointer result '%s' at %L "
10195 "is missing the pointer attribute",
10196 sym->ns->proc_name->name, &sym->declared_at);
10197 return FAILURE;
10201 return SUCCESS;
10205 /* Resolve a list of finalizer procedures. That is, after they have hopefully
10206 been defined and we now know their defined arguments, check that they fulfill
10207 the requirements of the standard for procedures used as finalizers. */
10209 static gfc_try
10210 gfc_resolve_finalizers (gfc_symbol* derived)
10212 gfc_finalizer* list;
10213 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
10214 gfc_try result = SUCCESS;
10215 bool seen_scalar = false;
10217 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
10218 return SUCCESS;
10220 /* Walk over the list of finalizer-procedures, check them, and if any one
10221 does not fit in with the standard's definition, print an error and remove
10222 it from the list. */
10223 prev_link = &derived->f2k_derived->finalizers;
10224 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
10226 gfc_symbol* arg;
10227 gfc_finalizer* i;
10228 int my_rank;
10230 /* Skip this finalizer if we already resolved it. */
10231 if (list->proc_tree)
10233 prev_link = &(list->next);
10234 continue;
10237 /* Check this exists and is a SUBROUTINE. */
10238 if (!list->proc_sym->attr.subroutine)
10240 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
10241 list->proc_sym->name, &list->where);
10242 goto error;
10245 /* We should have exactly one argument. */
10246 if (!list->proc_sym->formal || list->proc_sym->formal->next)
10248 gfc_error ("FINAL procedure at %L must have exactly one argument",
10249 &list->where);
10250 goto error;
10252 arg = list->proc_sym->formal->sym;
10254 /* This argument must be of our type. */
10255 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
10257 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
10258 &arg->declared_at, derived->name);
10259 goto error;
10262 /* It must neither be a pointer nor allocatable nor optional. */
10263 if (arg->attr.pointer)
10265 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
10266 &arg->declared_at);
10267 goto error;
10269 if (arg->attr.allocatable)
10271 gfc_error ("Argument of FINAL procedure at %L must not be"
10272 " ALLOCATABLE", &arg->declared_at);
10273 goto error;
10275 if (arg->attr.optional)
10277 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
10278 &arg->declared_at);
10279 goto error;
10282 /* It must not be INTENT(OUT). */
10283 if (arg->attr.intent == INTENT_OUT)
10285 gfc_error ("Argument of FINAL procedure at %L must not be"
10286 " INTENT(OUT)", &arg->declared_at);
10287 goto error;
10290 /* Warn if the procedure is non-scalar and not assumed shape. */
10291 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
10292 && arg->as->type != AS_ASSUMED_SHAPE)
10293 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
10294 " shape argument", &arg->declared_at);
10296 /* Check that it does not match in kind and rank with a FINAL procedure
10297 defined earlier. To really loop over the *earlier* declarations,
10298 we need to walk the tail of the list as new ones were pushed at the
10299 front. */
10300 /* TODO: Handle kind parameters once they are implemented. */
10301 my_rank = (arg->as ? arg->as->rank : 0);
10302 for (i = list->next; i; i = i->next)
10304 /* Argument list might be empty; that is an error signalled earlier,
10305 but we nevertheless continued resolving. */
10306 if (i->proc_sym->formal)
10308 gfc_symbol* i_arg = i->proc_sym->formal->sym;
10309 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
10310 if (i_rank == my_rank)
10312 gfc_error ("FINAL procedure '%s' declared at %L has the same"
10313 " rank (%d) as '%s'",
10314 list->proc_sym->name, &list->where, my_rank,
10315 i->proc_sym->name);
10316 goto error;
10321 /* Is this the/a scalar finalizer procedure? */
10322 if (!arg->as || arg->as->rank == 0)
10323 seen_scalar = true;
10325 /* Find the symtree for this procedure. */
10326 gcc_assert (!list->proc_tree);
10327 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
10329 prev_link = &list->next;
10330 continue;
10332 /* Remove wrong nodes immediately from the list so we don't risk any
10333 troubles in the future when they might fail later expectations. */
10334 error:
10335 result = FAILURE;
10336 i = list;
10337 *prev_link = list->next;
10338 gfc_free_finalizer (i);
10341 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
10342 were nodes in the list, must have been for arrays. It is surely a good
10343 idea to have a scalar version there if there's something to finalize. */
10344 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
10345 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
10346 " defined at %L, suggest also scalar one",
10347 derived->name, &derived->declared_at);
10349 /* TODO: Remove this error when finalization is finished. */
10350 gfc_error ("Finalization at %L is not yet implemented",
10351 &derived->declared_at);
10353 return result;
10357 /* Check that it is ok for the typebound procedure proc to override the
10358 procedure old. */
10360 static gfc_try
10361 check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
10363 locus where;
10364 const gfc_symbol* proc_target;
10365 const gfc_symbol* old_target;
10366 unsigned proc_pass_arg, old_pass_arg, argpos;
10367 gfc_formal_arglist* proc_formal;
10368 gfc_formal_arglist* old_formal;
10370 /* This procedure should only be called for non-GENERIC proc. */
10371 gcc_assert (!proc->n.tb->is_generic);
10373 /* If the overwritten procedure is GENERIC, this is an error. */
10374 if (old->n.tb->is_generic)
10376 gfc_error ("Can't overwrite GENERIC '%s' at %L",
10377 old->name, &proc->n.tb->where);
10378 return FAILURE;
10381 where = proc->n.tb->where;
10382 proc_target = proc->n.tb->u.specific->n.sym;
10383 old_target = old->n.tb->u.specific->n.sym;
10385 /* Check that overridden binding is not NON_OVERRIDABLE. */
10386 if (old->n.tb->non_overridable)
10388 gfc_error ("'%s' at %L overrides a procedure binding declared"
10389 " NON_OVERRIDABLE", proc->name, &where);
10390 return FAILURE;
10393 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
10394 if (!old->n.tb->deferred && proc->n.tb->deferred)
10396 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
10397 " non-DEFERRED binding", proc->name, &where);
10398 return FAILURE;
10401 /* If the overridden binding is PURE, the overriding must be, too. */
10402 if (old_target->attr.pure && !proc_target->attr.pure)
10404 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
10405 proc->name, &where);
10406 return FAILURE;
10409 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
10410 is not, the overriding must not be either. */
10411 if (old_target->attr.elemental && !proc_target->attr.elemental)
10413 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
10414 " ELEMENTAL", proc->name, &where);
10415 return FAILURE;
10417 if (!old_target->attr.elemental && proc_target->attr.elemental)
10419 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
10420 " be ELEMENTAL, either", proc->name, &where);
10421 return FAILURE;
10424 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
10425 SUBROUTINE. */
10426 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
10428 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
10429 " SUBROUTINE", proc->name, &where);
10430 return FAILURE;
10433 /* If the overridden binding is a FUNCTION, the overriding must also be a
10434 FUNCTION and have the same characteristics. */
10435 if (old_target->attr.function)
10437 if (!proc_target->attr.function)
10439 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
10440 " FUNCTION", proc->name, &where);
10441 return FAILURE;
10444 /* FIXME: Do more comprehensive checking (including, for instance, the
10445 rank and array-shape). */
10446 gcc_assert (proc_target->result && old_target->result);
10447 if (!gfc_compare_types (&proc_target->result->ts,
10448 &old_target->result->ts))
10450 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
10451 " matching result types", proc->name, &where);
10452 return FAILURE;
10456 /* If the overridden binding is PUBLIC, the overriding one must not be
10457 PRIVATE. */
10458 if (old->n.tb->access == ACCESS_PUBLIC
10459 && proc->n.tb->access == ACCESS_PRIVATE)
10461 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
10462 " PRIVATE", proc->name, &where);
10463 return FAILURE;
10466 /* Compare the formal argument lists of both procedures. This is also abused
10467 to find the position of the passed-object dummy arguments of both
10468 bindings as at least the overridden one might not yet be resolved and we
10469 need those positions in the check below. */
10470 proc_pass_arg = old_pass_arg = 0;
10471 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
10472 proc_pass_arg = 1;
10473 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
10474 old_pass_arg = 1;
10475 argpos = 1;
10476 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
10477 proc_formal && old_formal;
10478 proc_formal = proc_formal->next, old_formal = old_formal->next)
10480 if (proc->n.tb->pass_arg
10481 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
10482 proc_pass_arg = argpos;
10483 if (old->n.tb->pass_arg
10484 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
10485 old_pass_arg = argpos;
10487 /* Check that the names correspond. */
10488 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
10490 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
10491 " to match the corresponding argument of the overridden"
10492 " procedure", proc_formal->sym->name, proc->name, &where,
10493 old_formal->sym->name);
10494 return FAILURE;
10497 /* Check that the types correspond if neither is the passed-object
10498 argument. */
10499 /* FIXME: Do more comprehensive testing here. */
10500 if (proc_pass_arg != argpos && old_pass_arg != argpos
10501 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
10503 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
10504 "in respect to the overridden procedure",
10505 proc_formal->sym->name, proc->name, &where);
10506 return FAILURE;
10509 ++argpos;
10511 if (proc_formal || old_formal)
10513 gfc_error ("'%s' at %L must have the same number of formal arguments as"
10514 " the overridden procedure", proc->name, &where);
10515 return FAILURE;
10518 /* If the overridden binding is NOPASS, the overriding one must also be
10519 NOPASS. */
10520 if (old->n.tb->nopass && !proc->n.tb->nopass)
10522 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
10523 " NOPASS", proc->name, &where);
10524 return FAILURE;
10527 /* If the overridden binding is PASS(x), the overriding one must also be
10528 PASS and the passed-object dummy arguments must correspond. */
10529 if (!old->n.tb->nopass)
10531 if (proc->n.tb->nopass)
10533 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
10534 " PASS", proc->name, &where);
10535 return FAILURE;
10538 if (proc_pass_arg != old_pass_arg)
10540 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
10541 " the same position as the passed-object dummy argument of"
10542 " the overridden procedure", proc->name, &where);
10543 return FAILURE;
10547 return SUCCESS;
10551 /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
10553 static gfc_try
10554 check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
10555 const char* generic_name, locus where)
10557 gfc_symbol* sym1;
10558 gfc_symbol* sym2;
10560 gcc_assert (t1->specific && t2->specific);
10561 gcc_assert (!t1->specific->is_generic);
10562 gcc_assert (!t2->specific->is_generic);
10564 sym1 = t1->specific->u.specific->n.sym;
10565 sym2 = t2->specific->u.specific->n.sym;
10567 if (sym1 == sym2)
10568 return SUCCESS;
10570 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
10571 if (sym1->attr.subroutine != sym2->attr.subroutine
10572 || sym1->attr.function != sym2->attr.function)
10574 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
10575 " GENERIC '%s' at %L",
10576 sym1->name, sym2->name, generic_name, &where);
10577 return FAILURE;
10580 /* Compare the interfaces. */
10581 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
10583 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
10584 sym1->name, sym2->name, generic_name, &where);
10585 return FAILURE;
10588 return SUCCESS;
10592 /* Worker function for resolving a generic procedure binding; this is used to
10593 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
10595 The difference between those cases is finding possible inherited bindings
10596 that are overridden, as one has to look for them in tb_sym_root,
10597 tb_uop_root or tb_op, respectively. Thus the caller must already find
10598 the super-type and set p->overridden correctly. */
10600 static gfc_try
10601 resolve_tb_generic_targets (gfc_symbol* super_type,
10602 gfc_typebound_proc* p, const char* name)
10604 gfc_tbp_generic* target;
10605 gfc_symtree* first_target;
10606 gfc_symtree* inherited;
10608 gcc_assert (p && p->is_generic);
10610 /* Try to find the specific bindings for the symtrees in our target-list. */
10611 gcc_assert (p->u.generic);
10612 for (target = p->u.generic; target; target = target->next)
10613 if (!target->specific)
10615 gfc_typebound_proc* overridden_tbp;
10616 gfc_tbp_generic* g;
10617 const char* target_name;
10619 target_name = target->specific_st->name;
10621 /* Defined for this type directly. */
10622 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
10624 target->specific = target->specific_st->n.tb;
10625 goto specific_found;
10628 /* Look for an inherited specific binding. */
10629 if (super_type)
10631 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
10632 true, NULL);
10634 if (inherited)
10636 gcc_assert (inherited->n.tb);
10637 target->specific = inherited->n.tb;
10638 goto specific_found;
10642 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
10643 " at %L", target_name, name, &p->where);
10644 return FAILURE;
10646 /* Once we've found the specific binding, check it is not ambiguous with
10647 other specifics already found or inherited for the same GENERIC. */
10648 specific_found:
10649 gcc_assert (target->specific);
10651 /* This must really be a specific binding! */
10652 if (target->specific->is_generic)
10654 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
10655 " '%s' is GENERIC, too", name, &p->where, target_name);
10656 return FAILURE;
10659 /* Check those already resolved on this type directly. */
10660 for (g = p->u.generic; g; g = g->next)
10661 if (g != target && g->specific
10662 && check_generic_tbp_ambiguity (target, g, name, p->where)
10663 == FAILURE)
10664 return FAILURE;
10666 /* Check for ambiguity with inherited specific targets. */
10667 for (overridden_tbp = p->overridden; overridden_tbp;
10668 overridden_tbp = overridden_tbp->overridden)
10669 if (overridden_tbp->is_generic)
10671 for (g = overridden_tbp->u.generic; g; g = g->next)
10673 gcc_assert (g->specific);
10674 if (check_generic_tbp_ambiguity (target, g,
10675 name, p->where) == FAILURE)
10676 return FAILURE;
10681 /* If we attempt to "overwrite" a specific binding, this is an error. */
10682 if (p->overridden && !p->overridden->is_generic)
10684 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
10685 " the same name", name, &p->where);
10686 return FAILURE;
10689 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
10690 all must have the same attributes here. */
10691 first_target = p->u.generic->specific->u.specific;
10692 gcc_assert (first_target);
10693 p->subroutine = first_target->n.sym->attr.subroutine;
10694 p->function = first_target->n.sym->attr.function;
10696 return SUCCESS;
10700 /* Resolve a GENERIC procedure binding for a derived type. */
10702 static gfc_try
10703 resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
10705 gfc_symbol* super_type;
10707 /* Find the overridden binding if any. */
10708 st->n.tb->overridden = NULL;
10709 super_type = gfc_get_derived_super_type (derived);
10710 if (super_type)
10712 gfc_symtree* overridden;
10713 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
10714 true, NULL);
10716 if (overridden && overridden->n.tb)
10717 st->n.tb->overridden = overridden->n.tb;
10720 /* Resolve using worker function. */
10721 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
10725 /* Retrieve the target-procedure of an operator binding and do some checks in
10726 common for intrinsic and user-defined type-bound operators. */
10728 static gfc_symbol*
10729 get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
10731 gfc_symbol* target_proc;
10733 gcc_assert (target->specific && !target->specific->is_generic);
10734 target_proc = target->specific->u.specific->n.sym;
10735 gcc_assert (target_proc);
10737 /* All operator bindings must have a passed-object dummy argument. */
10738 if (target->specific->nopass)
10740 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
10741 return NULL;
10744 return target_proc;
10748 /* Resolve a type-bound intrinsic operator. */
10750 static gfc_try
10751 resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
10752 gfc_typebound_proc* p)
10754 gfc_symbol* super_type;
10755 gfc_tbp_generic* target;
10757 /* If there's already an error here, do nothing (but don't fail again). */
10758 if (p->error)
10759 return SUCCESS;
10761 /* Operators should always be GENERIC bindings. */
10762 gcc_assert (p->is_generic);
10764 /* Look for an overridden binding. */
10765 super_type = gfc_get_derived_super_type (derived);
10766 if (super_type && super_type->f2k_derived)
10767 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
10768 op, true, NULL);
10769 else
10770 p->overridden = NULL;
10772 /* Resolve general GENERIC properties using worker function. */
10773 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
10774 goto error;
10776 /* Check the targets to be procedures of correct interface. */
10777 for (target = p->u.generic; target; target = target->next)
10779 gfc_symbol* target_proc;
10781 target_proc = get_checked_tb_operator_target (target, p->where);
10782 if (!target_proc)
10783 goto error;
10785 if (!gfc_check_operator_interface (target_proc, op, p->where))
10786 goto error;
10789 return SUCCESS;
10791 error:
10792 p->error = 1;
10793 return FAILURE;
10797 /* Resolve a type-bound user operator (tree-walker callback). */
10799 static gfc_symbol* resolve_bindings_derived;
10800 static gfc_try resolve_bindings_result;
10802 static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
10804 static void
10805 resolve_typebound_user_op (gfc_symtree* stree)
10807 gfc_symbol* super_type;
10808 gfc_tbp_generic* target;
10810 gcc_assert (stree && stree->n.tb);
10812 if (stree->n.tb->error)
10813 return;
10815 /* Operators should always be GENERIC bindings. */
10816 gcc_assert (stree->n.tb->is_generic);
10818 /* Find overridden procedure, if any. */
10819 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10820 if (super_type && super_type->f2k_derived)
10822 gfc_symtree* overridden;
10823 overridden = gfc_find_typebound_user_op (super_type, NULL,
10824 stree->name, true, NULL);
10826 if (overridden && overridden->n.tb)
10827 stree->n.tb->overridden = overridden->n.tb;
10829 else
10830 stree->n.tb->overridden = NULL;
10832 /* Resolve basically using worker function. */
10833 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
10834 == FAILURE)
10835 goto error;
10837 /* Check the targets to be functions of correct interface. */
10838 for (target = stree->n.tb->u.generic; target; target = target->next)
10840 gfc_symbol* target_proc;
10842 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
10843 if (!target_proc)
10844 goto error;
10846 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
10847 goto error;
10850 return;
10852 error:
10853 resolve_bindings_result = FAILURE;
10854 stree->n.tb->error = 1;
10858 /* Resolve the type-bound procedures for a derived type. */
10860 static void
10861 resolve_typebound_procedure (gfc_symtree* stree)
10863 gfc_symbol* proc;
10864 locus where;
10865 gfc_symbol* me_arg;
10866 gfc_symbol* super_type;
10867 gfc_component* comp;
10869 gcc_assert (stree);
10871 /* Undefined specific symbol from GENERIC target definition. */
10872 if (!stree->n.tb)
10873 return;
10875 if (stree->n.tb->error)
10876 return;
10878 /* If this is a GENERIC binding, use that routine. */
10879 if (stree->n.tb->is_generic)
10881 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10882 == FAILURE)
10883 goto error;
10884 return;
10887 /* Get the target-procedure to check it. */
10888 gcc_assert (!stree->n.tb->is_generic);
10889 gcc_assert (stree->n.tb->u.specific);
10890 proc = stree->n.tb->u.specific->n.sym;
10891 where = stree->n.tb->where;
10893 /* Default access should already be resolved from the parser. */
10894 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
10896 /* It should be a module procedure or an external procedure with explicit
10897 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
10898 if ((!proc->attr.subroutine && !proc->attr.function)
10899 || (proc->attr.proc != PROC_MODULE
10900 && proc->attr.if_source != IFSRC_IFBODY)
10901 || (proc->attr.abstract && !stree->n.tb->deferred))
10903 gfc_error ("'%s' must be a module procedure or an external procedure with"
10904 " an explicit interface at %L", proc->name, &where);
10905 goto error;
10907 stree->n.tb->subroutine = proc->attr.subroutine;
10908 stree->n.tb->function = proc->attr.function;
10910 /* Find the super-type of the current derived type. We could do this once and
10911 store in a global if speed is needed, but as long as not I believe this is
10912 more readable and clearer. */
10913 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10915 /* If PASS, resolve and check arguments if not already resolved / loaded
10916 from a .mod file. */
10917 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
10919 if (stree->n.tb->pass_arg)
10921 gfc_formal_arglist* i;
10923 /* If an explicit passing argument name is given, walk the arg-list
10924 and look for it. */
10926 me_arg = NULL;
10927 stree->n.tb->pass_arg_num = 1;
10928 for (i = proc->formal; i; i = i->next)
10930 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
10932 me_arg = i->sym;
10933 break;
10935 ++stree->n.tb->pass_arg_num;
10938 if (!me_arg)
10940 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10941 " argument '%s'",
10942 proc->name, stree->n.tb->pass_arg, &where,
10943 stree->n.tb->pass_arg);
10944 goto error;
10947 else
10949 /* Otherwise, take the first one; there should in fact be at least
10950 one. */
10951 stree->n.tb->pass_arg_num = 1;
10952 if (!proc->formal)
10954 gfc_error ("Procedure '%s' with PASS at %L must have at"
10955 " least one argument", proc->name, &where);
10956 goto error;
10958 me_arg = proc->formal->sym;
10961 /* Now check that the argument-type matches and the passed-object
10962 dummy argument is generally fine. */
10964 gcc_assert (me_arg);
10966 if (me_arg->ts.type != BT_CLASS)
10968 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10969 " at %L", proc->name, &where);
10970 goto error;
10973 if (CLASS_DATA (me_arg)->ts.u.derived
10974 != resolve_bindings_derived)
10976 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10977 " the derived-type '%s'", me_arg->name, proc->name,
10978 me_arg->name, &where, resolve_bindings_derived->name);
10979 goto error;
10982 gcc_assert (me_arg->ts.type == BT_CLASS);
10983 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank > 0)
10985 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10986 " scalar", proc->name, &where);
10987 goto error;
10989 if (CLASS_DATA (me_arg)->attr.allocatable)
10991 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10992 " be ALLOCATABLE", proc->name, &where);
10993 goto error;
10995 if (CLASS_DATA (me_arg)->attr.class_pointer)
10997 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10998 " be POINTER", proc->name, &where);
10999 goto error;
11003 /* If we are extending some type, check that we don't override a procedure
11004 flagged NON_OVERRIDABLE. */
11005 stree->n.tb->overridden = NULL;
11006 if (super_type)
11008 gfc_symtree* overridden;
11009 overridden = gfc_find_typebound_proc (super_type, NULL,
11010 stree->name, true, NULL);
11012 if (overridden && overridden->n.tb)
11013 stree->n.tb->overridden = overridden->n.tb;
11015 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
11016 goto error;
11019 /* See if there's a name collision with a component directly in this type. */
11020 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11021 if (!strcmp (comp->name, stree->name))
11023 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11024 " '%s'",
11025 stree->name, &where, resolve_bindings_derived->name);
11026 goto error;
11029 /* Try to find a name collision with an inherited component. */
11030 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11032 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11033 " component of '%s'",
11034 stree->name, &where, resolve_bindings_derived->name);
11035 goto error;
11038 stree->n.tb->error = 0;
11039 return;
11041 error:
11042 resolve_bindings_result = FAILURE;
11043 stree->n.tb->error = 1;
11047 static gfc_try
11048 resolve_typebound_procedures (gfc_symbol* derived)
11050 int op;
11052 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
11053 return SUCCESS;
11055 resolve_bindings_derived = derived;
11056 resolve_bindings_result = SUCCESS;
11058 /* Make sure the vtab has been generated. */
11059 gfc_find_derived_vtab (derived);
11061 if (derived->f2k_derived->tb_sym_root)
11062 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11063 &resolve_typebound_procedure);
11065 if (derived->f2k_derived->tb_uop_root)
11066 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11067 &resolve_typebound_user_op);
11069 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11071 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
11072 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
11073 p) == FAILURE)
11074 resolve_bindings_result = FAILURE;
11077 return resolve_bindings_result;
11081 /* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11082 to give all identical derived types the same backend_decl. */
11083 static void
11084 add_dt_to_dt_list (gfc_symbol *derived)
11086 gfc_dt_list *dt_list;
11088 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11089 if (derived == dt_list->derived)
11090 return;
11092 dt_list = gfc_get_dt_list ();
11093 dt_list->next = gfc_derived_types;
11094 dt_list->derived = derived;
11095 gfc_derived_types = dt_list;
11099 /* Ensure that a derived-type is really not abstract, meaning that every
11100 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11102 static gfc_try
11103 ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11105 if (!st)
11106 return SUCCESS;
11108 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
11109 return FAILURE;
11110 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
11111 return FAILURE;
11113 if (st->n.tb && st->n.tb->deferred)
11115 gfc_symtree* overriding;
11116 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
11117 if (!overriding)
11118 return FAILURE;
11119 gcc_assert (overriding->n.tb);
11120 if (overriding->n.tb->deferred)
11122 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11123 " '%s' is DEFERRED and not overridden",
11124 sub->name, &sub->declared_at, st->name);
11125 return FAILURE;
11129 return SUCCESS;
11132 static gfc_try
11133 ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11135 /* The algorithm used here is to recursively travel up the ancestry of sub
11136 and for each ancestor-type, check all bindings. If any of them is
11137 DEFERRED, look it up starting from sub and see if the found (overriding)
11138 binding is not DEFERRED.
11139 This is not the most efficient way to do this, but it should be ok and is
11140 clearer than something sophisticated. */
11142 gcc_assert (ancestor && !sub->attr.abstract);
11144 if (!ancestor->attr.abstract)
11145 return SUCCESS;
11147 /* Walk bindings of this ancestor. */
11148 if (ancestor->f2k_derived)
11150 gfc_try t;
11151 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
11152 if (t == FAILURE)
11153 return FAILURE;
11156 /* Find next ancestor type and recurse on it. */
11157 ancestor = gfc_get_derived_super_type (ancestor);
11158 if (ancestor)
11159 return ensure_not_abstract (sub, ancestor);
11161 return SUCCESS;
11165 /* Resolve the components of a derived type. */
11167 static gfc_try
11168 resolve_fl_derived (gfc_symbol *sym)
11170 gfc_symbol* super_type;
11171 gfc_component *c;
11173 super_type = gfc_get_derived_super_type (sym);
11175 if (sym->attr.is_class && sym->ts.u.derived == NULL)
11177 /* Fix up incomplete CLASS symbols. */
11178 gfc_component *data = gfc_find_component (sym, "$data", true, true);
11179 gfc_component *vptr = gfc_find_component (sym, "$vptr", true, true);
11180 if (vptr->ts.u.derived == NULL)
11182 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
11183 gcc_assert (vtab);
11184 vptr->ts.u.derived = vtab->ts.u.derived;
11188 /* F2008, C432. */
11189 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
11191 gfc_error ("As extending type '%s' at %L has a coarray component, "
11192 "parent type '%s' shall also have one", sym->name,
11193 &sym->declared_at, super_type->name);
11194 return FAILURE;
11197 /* Ensure the extended type gets resolved before we do. */
11198 if (super_type && resolve_fl_derived (super_type) == FAILURE)
11199 return FAILURE;
11201 /* An ABSTRACT type must be extensible. */
11202 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
11204 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
11205 sym->name, &sym->declared_at);
11206 return FAILURE;
11209 for (c = sym->components; c != NULL; c = c->next)
11211 /* F2008, C442. */
11212 if (c->attr.codimension /* FIXME: c->as check due to PR 43412. */
11213 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
11215 gfc_error ("Coarray component '%s' at %L must be allocatable with "
11216 "deferred shape", c->name, &c->loc);
11217 return FAILURE;
11220 /* F2008, C443. */
11221 if (c->attr.codimension && c->ts.type == BT_DERIVED
11222 && c->ts.u.derived->ts.is_iso_c)
11224 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
11225 "shall not be a coarray", c->name, &c->loc);
11226 return FAILURE;
11229 /* F2008, C444. */
11230 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
11231 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
11232 || c->attr.allocatable))
11234 gfc_error ("Component '%s' at %L with coarray component "
11235 "shall be a nonpointer, nonallocatable scalar",
11236 c->name, &c->loc);
11237 return FAILURE;
11240 /* F2008, C448. */
11241 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
11243 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
11244 "is not an array pointer", c->name, &c->loc);
11245 return FAILURE;
11248 if (c->attr.proc_pointer && c->ts.interface)
11250 if (c->ts.interface->attr.procedure && !sym->attr.vtype)
11251 gfc_error ("Interface '%s', used by procedure pointer component "
11252 "'%s' at %L, is declared in a later PROCEDURE statement",
11253 c->ts.interface->name, c->name, &c->loc);
11255 /* Get the attributes from the interface (now resolved). */
11256 if (c->ts.interface->attr.if_source
11257 || c->ts.interface->attr.intrinsic)
11259 gfc_symbol *ifc = c->ts.interface;
11261 if (ifc->formal && !ifc->formal_ns)
11262 resolve_symbol (ifc);
11264 if (ifc->attr.intrinsic)
11265 resolve_intrinsic (ifc, &ifc->declared_at);
11267 if (ifc->result)
11269 c->ts = ifc->result->ts;
11270 c->attr.allocatable = ifc->result->attr.allocatable;
11271 c->attr.pointer = ifc->result->attr.pointer;
11272 c->attr.dimension = ifc->result->attr.dimension;
11273 c->as = gfc_copy_array_spec (ifc->result->as);
11275 else
11277 c->ts = ifc->ts;
11278 c->attr.allocatable = ifc->attr.allocatable;
11279 c->attr.pointer = ifc->attr.pointer;
11280 c->attr.dimension = ifc->attr.dimension;
11281 c->as = gfc_copy_array_spec (ifc->as);
11283 c->ts.interface = ifc;
11284 c->attr.function = ifc->attr.function;
11285 c->attr.subroutine = ifc->attr.subroutine;
11286 gfc_copy_formal_args_ppc (c, ifc);
11288 c->attr.pure = ifc->attr.pure;
11289 c->attr.elemental = ifc->attr.elemental;
11290 c->attr.recursive = ifc->attr.recursive;
11291 c->attr.always_explicit = ifc->attr.always_explicit;
11292 c->attr.ext_attr |= ifc->attr.ext_attr;
11293 /* Replace symbols in array spec. */
11294 if (c->as)
11296 int i;
11297 for (i = 0; i < c->as->rank; i++)
11299 gfc_expr_replace_comp (c->as->lower[i], c);
11300 gfc_expr_replace_comp (c->as->upper[i], c);
11303 /* Copy char length. */
11304 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
11306 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
11307 gfc_expr_replace_comp (cl->length, c);
11308 if (cl->length && !cl->resolved
11309 && gfc_resolve_expr (cl->length) == FAILURE)
11310 return FAILURE;
11311 c->ts.u.cl = cl;
11314 else if (!sym->attr.vtype && c->ts.interface->name[0] != '\0')
11316 gfc_error ("Interface '%s' of procedure pointer component "
11317 "'%s' at %L must be explicit", c->ts.interface->name,
11318 c->name, &c->loc);
11319 return FAILURE;
11322 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
11324 /* Since PPCs are not implicitly typed, a PPC without an explicit
11325 interface must be a subroutine. */
11326 gfc_add_subroutine (&c->attr, c->name, &c->loc);
11329 /* Procedure pointer components: Check PASS arg. */
11330 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
11331 && !sym->attr.vtype)
11333 gfc_symbol* me_arg;
11335 if (c->tb->pass_arg)
11337 gfc_formal_arglist* i;
11339 /* If an explicit passing argument name is given, walk the arg-list
11340 and look for it. */
11342 me_arg = NULL;
11343 c->tb->pass_arg_num = 1;
11344 for (i = c->formal; i; i = i->next)
11346 if (!strcmp (i->sym->name, c->tb->pass_arg))
11348 me_arg = i->sym;
11349 break;
11351 c->tb->pass_arg_num++;
11354 if (!me_arg)
11356 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
11357 "at %L has no argument '%s'", c->name,
11358 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
11359 c->tb->error = 1;
11360 return FAILURE;
11363 else
11365 /* Otherwise, take the first one; there should in fact be at least
11366 one. */
11367 c->tb->pass_arg_num = 1;
11368 if (!c->formal)
11370 gfc_error ("Procedure pointer component '%s' with PASS at %L "
11371 "must have at least one argument",
11372 c->name, &c->loc);
11373 c->tb->error = 1;
11374 return FAILURE;
11376 me_arg = c->formal->sym;
11379 /* Now check that the argument-type matches. */
11380 gcc_assert (me_arg);
11381 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
11382 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
11383 || (me_arg->ts.type == BT_CLASS
11384 && CLASS_DATA (me_arg)->ts.u.derived != sym))
11386 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11387 " the derived type '%s'", me_arg->name, c->name,
11388 me_arg->name, &c->loc, sym->name);
11389 c->tb->error = 1;
11390 return FAILURE;
11393 /* Check for C453. */
11394 if (me_arg->attr.dimension)
11396 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11397 "must be scalar", me_arg->name, c->name, me_arg->name,
11398 &c->loc);
11399 c->tb->error = 1;
11400 return FAILURE;
11403 if (me_arg->attr.pointer)
11405 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11406 "may not have the POINTER attribute", me_arg->name,
11407 c->name, me_arg->name, &c->loc);
11408 c->tb->error = 1;
11409 return FAILURE;
11412 if (me_arg->attr.allocatable)
11414 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
11415 "may not be ALLOCATABLE", me_arg->name, c->name,
11416 me_arg->name, &c->loc);
11417 c->tb->error = 1;
11418 return FAILURE;
11421 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
11422 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11423 " at %L", c->name, &c->loc);
11427 /* Check type-spec if this is not the parent-type component. */
11428 if ((!sym->attr.extension || c != sym->components) && !sym->attr.vtype
11429 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
11430 return FAILURE;
11432 /* If this type is an extension, set the accessibility of the parent
11433 component. */
11434 if (super_type && c == sym->components
11435 && strcmp (super_type->name, c->name) == 0)
11436 c->attr.access = super_type->attr.access;
11438 /* If this type is an extension, see if this component has the same name
11439 as an inherited type-bound procedure. */
11440 if (super_type && !sym->attr.is_class
11441 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
11443 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
11444 " inherited type-bound procedure",
11445 c->name, sym->name, &c->loc);
11446 return FAILURE;
11449 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
11451 if (c->ts.u.cl->length == NULL
11452 || (resolve_charlen (c->ts.u.cl) == FAILURE)
11453 || !gfc_is_constant_expr (c->ts.u.cl->length))
11455 gfc_error ("Character length of component '%s' needs to "
11456 "be a constant specification expression at %L",
11457 c->name,
11458 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
11459 return FAILURE;
11463 if (c->ts.type == BT_DERIVED
11464 && sym->component_access != ACCESS_PRIVATE
11465 && gfc_check_access (sym->attr.access, sym->ns->default_access)
11466 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
11467 && !c->ts.u.derived->attr.use_assoc
11468 && !gfc_check_access (c->ts.u.derived->attr.access,
11469 c->ts.u.derived->ns->default_access)
11470 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
11471 "is a PRIVATE type and cannot be a component of "
11472 "'%s', which is PUBLIC at %L", c->name,
11473 sym->name, &sym->declared_at) == FAILURE)
11474 return FAILURE;
11476 if (sym->attr.sequence)
11478 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
11480 gfc_error ("Component %s of SEQUENCE type declared at %L does "
11481 "not have the SEQUENCE attribute",
11482 c->ts.u.derived->name, &sym->declared_at);
11483 return FAILURE;
11487 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
11488 && c->attr.pointer && c->ts.u.derived->components == NULL
11489 && !c->ts.u.derived->attr.zero_comp)
11491 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11492 "that has not been declared", c->name, sym->name,
11493 &c->loc);
11494 return FAILURE;
11497 if (c->ts.type == BT_CLASS && CLASS_DATA (c)->attr.class_pointer
11498 && CLASS_DATA (c)->ts.u.derived->components == NULL
11499 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp)
11501 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
11502 "that has not been declared", c->name, sym->name,
11503 &c->loc);
11504 return FAILURE;
11507 /* C437. */
11508 if (c->ts.type == BT_CLASS
11509 && !(CLASS_DATA (c)->attr.class_pointer
11510 || CLASS_DATA (c)->attr.allocatable))
11512 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
11513 "or pointer", c->name, &c->loc);
11514 return FAILURE;
11517 /* Ensure that all the derived type components are put on the
11518 derived type list; even in formal namespaces, where derived type
11519 pointer components might not have been declared. */
11520 if (c->ts.type == BT_DERIVED
11521 && c->ts.u.derived
11522 && c->ts.u.derived->components
11523 && c->attr.pointer
11524 && sym != c->ts.u.derived)
11525 add_dt_to_dt_list (c->ts.u.derived);
11527 if (gfc_resolve_array_spec (c->as, !(c->attr.pointer
11528 || c->attr.proc_pointer
11529 || c->attr.allocatable)) == FAILURE)
11530 return FAILURE;
11533 /* Resolve the type-bound procedures. */
11534 if (resolve_typebound_procedures (sym) == FAILURE)
11535 return FAILURE;
11537 /* Resolve the finalizer procedures. */
11538 if (gfc_resolve_finalizers (sym) == FAILURE)
11539 return FAILURE;
11541 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
11542 all DEFERRED bindings are overridden. */
11543 if (super_type && super_type->attr.abstract && !sym->attr.abstract
11544 && !sym->attr.is_class
11545 && ensure_not_abstract (sym, super_type) == FAILURE)
11546 return FAILURE;
11548 /* Add derived type to the derived type list. */
11549 add_dt_to_dt_list (sym);
11551 return SUCCESS;
11555 static gfc_try
11556 resolve_fl_namelist (gfc_symbol *sym)
11558 gfc_namelist *nl;
11559 gfc_symbol *nlsym;
11561 for (nl = sym->namelist; nl; nl = nl->next)
11563 /* Reject namelist arrays of assumed shape. */
11564 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
11565 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
11566 "must not have assumed shape in namelist "
11567 "'%s' at %L", nl->sym->name, sym->name,
11568 &sym->declared_at) == FAILURE)
11569 return FAILURE;
11571 /* Reject namelist arrays that are not constant shape. */
11572 if (is_non_constant_shape_array (nl->sym))
11574 gfc_error ("NAMELIST array object '%s' must have constant "
11575 "shape in namelist '%s' at %L", nl->sym->name,
11576 sym->name, &sym->declared_at);
11577 return FAILURE;
11580 /* Namelist objects cannot have allocatable or pointer components. */
11581 if (nl->sym->ts.type != BT_DERIVED)
11582 continue;
11584 if (nl->sym->ts.u.derived->attr.alloc_comp)
11586 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11587 "have ALLOCATABLE components",
11588 nl->sym->name, sym->name, &sym->declared_at);
11589 return FAILURE;
11592 if (nl->sym->ts.u.derived->attr.pointer_comp)
11594 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
11595 "have POINTER components",
11596 nl->sym->name, sym->name, &sym->declared_at);
11597 return FAILURE;
11601 /* Reject PRIVATE objects in a PUBLIC namelist. */
11602 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
11604 for (nl = sym->namelist; nl; nl = nl->next)
11606 if (!nl->sym->attr.use_assoc
11607 && !is_sym_host_assoc (nl->sym, sym->ns)
11608 && !gfc_check_access(nl->sym->attr.access,
11609 nl->sym->ns->default_access))
11611 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
11612 "cannot be member of PUBLIC namelist '%s' at %L",
11613 nl->sym->name, sym->name, &sym->declared_at);
11614 return FAILURE;
11617 /* Types with private components that came here by USE-association. */
11618 if (nl->sym->ts.type == BT_DERIVED
11619 && derived_inaccessible (nl->sym->ts.u.derived))
11621 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
11622 "components and cannot be member of namelist '%s' at %L",
11623 nl->sym->name, sym->name, &sym->declared_at);
11624 return FAILURE;
11627 /* Types with private components that are defined in the same module. */
11628 if (nl->sym->ts.type == BT_DERIVED
11629 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
11630 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
11631 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
11632 nl->sym->ns->default_access))
11634 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
11635 "cannot be a member of PUBLIC namelist '%s' at %L",
11636 nl->sym->name, sym->name, &sym->declared_at);
11637 return FAILURE;
11643 /* 14.1.2 A module or internal procedure represent local entities
11644 of the same type as a namelist member and so are not allowed. */
11645 for (nl = sym->namelist; nl; nl = nl->next)
11647 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
11648 continue;
11650 if (nl->sym->attr.function && nl->sym == nl->sym->result)
11651 if ((nl->sym == sym->ns->proc_name)
11653 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
11654 continue;
11656 nlsym = NULL;
11657 if (nl->sym && nl->sym->name)
11658 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
11659 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
11661 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
11662 "attribute in '%s' at %L", nlsym->name,
11663 &sym->declared_at);
11664 return FAILURE;
11668 return SUCCESS;
11672 static gfc_try
11673 resolve_fl_parameter (gfc_symbol *sym)
11675 /* A parameter array's shape needs to be constant. */
11676 if (sym->as != NULL
11677 && (sym->as->type == AS_DEFERRED
11678 || is_non_constant_shape_array (sym)))
11680 gfc_error ("Parameter array '%s' at %L cannot be automatic "
11681 "or of deferred shape", sym->name, &sym->declared_at);
11682 return FAILURE;
11685 /* Make sure a parameter that has been implicitly typed still
11686 matches the implicit type, since PARAMETER statements can precede
11687 IMPLICIT statements. */
11688 if (sym->attr.implicit_type
11689 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
11690 sym->ns)))
11692 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
11693 "later IMPLICIT type", sym->name, &sym->declared_at);
11694 return FAILURE;
11697 /* Make sure the types of derived parameters are consistent. This
11698 type checking is deferred until resolution because the type may
11699 refer to a derived type from the host. */
11700 if (sym->ts.type == BT_DERIVED
11701 && !gfc_compare_types (&sym->ts, &sym->value->ts))
11703 gfc_error ("Incompatible derived type in PARAMETER at %L",
11704 &sym->value->where);
11705 return FAILURE;
11707 return SUCCESS;
11711 /* Do anything necessary to resolve a symbol. Right now, we just
11712 assume that an otherwise unknown symbol is a variable. This sort
11713 of thing commonly happens for symbols in module. */
11715 static void
11716 resolve_symbol (gfc_symbol *sym)
11718 int check_constant, mp_flag;
11719 gfc_symtree *symtree;
11720 gfc_symtree *this_symtree;
11721 gfc_namespace *ns;
11722 gfc_component *c;
11724 /* Avoid double resolution of function result symbols. */
11725 if ((sym->result || sym->attr.result) && !sym->attr.dummy
11726 && (sym->ns != gfc_current_ns))
11727 return;
11729 if (sym->attr.flavor == FL_UNKNOWN)
11732 /* If we find that a flavorless symbol is an interface in one of the
11733 parent namespaces, find its symtree in this namespace, free the
11734 symbol and set the symtree to point to the interface symbol. */
11735 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
11737 symtree = gfc_find_symtree (ns->sym_root, sym->name);
11738 if (symtree && symtree->n.sym->generic)
11740 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11741 sym->name);
11742 gfc_release_symbol (sym);
11743 symtree->n.sym->refs++;
11744 this_symtree->n.sym = symtree->n.sym;
11745 return;
11749 /* Otherwise give it a flavor according to such attributes as
11750 it has. */
11751 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
11752 sym->attr.flavor = FL_VARIABLE;
11753 else
11755 sym->attr.flavor = FL_PROCEDURE;
11756 if (sym->attr.dimension)
11757 sym->attr.function = 1;
11761 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
11762 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
11764 if (sym->attr.procedure && sym->ts.interface
11765 && sym->attr.if_source != IFSRC_DECL
11766 && resolve_procedure_interface (sym) == FAILURE)
11767 return;
11769 if (sym->attr.is_protected && !sym->attr.proc_pointer
11770 && (sym->attr.procedure || sym->attr.external))
11772 if (sym->attr.external)
11773 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
11774 "at %L", &sym->declared_at);
11775 else
11776 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
11777 "at %L", &sym->declared_at);
11779 return;
11783 /* F2008, C530. */
11784 if (sym->attr.contiguous
11785 && (!sym->attr.dimension || (sym->as->type != AS_ASSUMED_SHAPE
11786 && !sym->attr.pointer)))
11788 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
11789 "array pointer or an assumed-shape array", sym->name,
11790 &sym->declared_at);
11791 return;
11794 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
11795 return;
11797 /* Symbols that are module procedures with results (functions) have
11798 the types and array specification copied for type checking in
11799 procedures that call them, as well as for saving to a module
11800 file. These symbols can't stand the scrutiny that their results
11801 can. */
11802 mp_flag = (sym->result != NULL && sym->result != sym);
11804 /* Make sure that the intrinsic is consistent with its internal
11805 representation. This needs to be done before assigning a default
11806 type to avoid spurious warnings. */
11807 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
11808 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
11809 return;
11811 /* Resolve associate names. */
11812 if (sym->assoc)
11813 resolve_assoc_var (sym, true);
11815 /* Assign default type to symbols that need one and don't have one. */
11816 if (sym->ts.type == BT_UNKNOWN)
11818 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
11819 gfc_set_default_type (sym, 1, NULL);
11821 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
11822 && !sym->attr.function && !sym->attr.subroutine
11823 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
11824 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
11826 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
11828 /* The specific case of an external procedure should emit an error
11829 in the case that there is no implicit type. */
11830 if (!mp_flag)
11831 gfc_set_default_type (sym, sym->attr.external, NULL);
11832 else
11834 /* Result may be in another namespace. */
11835 resolve_symbol (sym->result);
11837 if (!sym->result->attr.proc_pointer)
11839 sym->ts = sym->result->ts;
11840 sym->as = gfc_copy_array_spec (sym->result->as);
11841 sym->attr.dimension = sym->result->attr.dimension;
11842 sym->attr.pointer = sym->result->attr.pointer;
11843 sym->attr.allocatable = sym->result->attr.allocatable;
11844 sym->attr.contiguous = sym->result->attr.contiguous;
11850 /* Assumed size arrays and assumed shape arrays must be dummy
11851 arguments. Array-spec's of implied-shape should have been resolved to
11852 AS_EXPLICIT already. */
11854 if (sym->as)
11856 gcc_assert (sym->as->type != AS_IMPLIED_SHAPE);
11857 if (((sym->as->type == AS_ASSUMED_SIZE && !sym->as->cp_was_assumed)
11858 || sym->as->type == AS_ASSUMED_SHAPE)
11859 && sym->attr.dummy == 0)
11861 if (sym->as->type == AS_ASSUMED_SIZE)
11862 gfc_error ("Assumed size array at %L must be a dummy argument",
11863 &sym->declared_at);
11864 else
11865 gfc_error ("Assumed shape array at %L must be a dummy argument",
11866 &sym->declared_at);
11867 return;
11871 /* Make sure symbols with known intent or optional are really dummy
11872 variable. Because of ENTRY statement, this has to be deferred
11873 until resolution time. */
11875 if (!sym->attr.dummy
11876 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
11878 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
11879 return;
11882 if (sym->attr.value && !sym->attr.dummy)
11884 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
11885 "it is not a dummy argument", sym->name, &sym->declared_at);
11886 return;
11889 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
11891 gfc_charlen *cl = sym->ts.u.cl;
11892 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11894 gfc_error ("Character dummy variable '%s' at %L with VALUE "
11895 "attribute must have constant length",
11896 sym->name, &sym->declared_at);
11897 return;
11900 if (sym->ts.is_c_interop
11901 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
11903 gfc_error ("C interoperable character dummy variable '%s' at %L "
11904 "with VALUE attribute must have length one",
11905 sym->name, &sym->declared_at);
11906 return;
11910 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11911 do this for something that was implicitly typed because that is handled
11912 in gfc_set_default_type. Handle dummy arguments and procedure
11913 definitions separately. Also, anything that is use associated is not
11914 handled here but instead is handled in the module it is declared in.
11915 Finally, derived type definitions are allowed to be BIND(C) since that
11916 only implies that they're interoperable, and they are checked fully for
11917 interoperability when a variable is declared of that type. */
11918 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11919 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11920 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11922 gfc_try t = SUCCESS;
11924 /* First, make sure the variable is declared at the
11925 module-level scope (J3/04-007, Section 15.3). */
11926 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11927 sym->attr.in_common == 0)
11929 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11930 "is neither a COMMON block nor declared at the "
11931 "module level scope", sym->name, &(sym->declared_at));
11932 t = FAILURE;
11934 else if (sym->common_head != NULL)
11936 t = verify_com_block_vars_c_interop (sym->common_head);
11938 else
11940 /* If type() declaration, we need to verify that the components
11941 of the given type are all C interoperable, etc. */
11942 if (sym->ts.type == BT_DERIVED &&
11943 sym->ts.u.derived->attr.is_c_interop != 1)
11945 /* Make sure the user marked the derived type as BIND(C). If
11946 not, call the verify routine. This could print an error
11947 for the derived type more than once if multiple variables
11948 of that type are declared. */
11949 if (sym->ts.u.derived->attr.is_bind_c != 1)
11950 verify_bind_c_derived_type (sym->ts.u.derived);
11951 t = FAILURE;
11954 /* Verify the variable itself as C interoperable if it
11955 is BIND(C). It is not possible for this to succeed if
11956 the verify_bind_c_derived_type failed, so don't have to handle
11957 any error returned by verify_bind_c_derived_type. */
11958 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11959 sym->common_block);
11962 if (t == FAILURE)
11964 /* clear the is_bind_c flag to prevent reporting errors more than
11965 once if something failed. */
11966 sym->attr.is_bind_c = 0;
11967 return;
11971 /* If a derived type symbol has reached this point, without its
11972 type being declared, we have an error. Notice that most
11973 conditions that produce undefined derived types have already
11974 been dealt with. However, the likes of:
11975 implicit type(t) (t) ..... call foo (t) will get us here if
11976 the type is not declared in the scope of the implicit
11977 statement. Change the type to BT_UNKNOWN, both because it is so
11978 and to prevent an ICE. */
11979 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11980 && !sym->ts.u.derived->attr.zero_comp)
11982 gfc_error ("The derived type '%s' at %L is of type '%s', "
11983 "which has not been defined", sym->name,
11984 &sym->declared_at, sym->ts.u.derived->name);
11985 sym->ts.type = BT_UNKNOWN;
11986 return;
11989 /* Make sure that the derived type has been resolved and that the
11990 derived type is visible in the symbol's namespace, if it is a
11991 module function and is not PRIVATE. */
11992 if (sym->ts.type == BT_DERIVED
11993 && sym->ts.u.derived->attr.use_assoc
11994 && sym->ns->proc_name
11995 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11997 gfc_symbol *ds;
11999 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
12000 return;
12002 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
12003 if (!ds && sym->attr.function
12004 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12006 symtree = gfc_new_symtree (&sym->ns->sym_root,
12007 sym->ts.u.derived->name);
12008 symtree->n.sym = sym->ts.u.derived;
12009 sym->ts.u.derived->refs++;
12013 /* Unless the derived-type declaration is use associated, Fortran 95
12014 does not allow public entries of private derived types.
12015 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
12016 161 in 95-006r3. */
12017 if (sym->ts.type == BT_DERIVED
12018 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
12019 && !sym->ts.u.derived->attr.use_assoc
12020 && gfc_check_access (sym->attr.access, sym->ns->default_access)
12021 && !gfc_check_access (sym->ts.u.derived->attr.access,
12022 sym->ts.u.derived->ns->default_access)
12023 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
12024 "of PRIVATE derived type '%s'",
12025 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
12026 : "variable", sym->name, &sym->declared_at,
12027 sym->ts.u.derived->name) == FAILURE)
12028 return;
12030 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
12031 default initialization is defined (5.1.2.4.4). */
12032 if (sym->ts.type == BT_DERIVED
12033 && sym->attr.dummy
12034 && sym->attr.intent == INTENT_OUT
12035 && sym->as
12036 && sym->as->type == AS_ASSUMED_SIZE)
12038 for (c = sym->ts.u.derived->components; c; c = c->next)
12040 if (c->initializer)
12042 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
12043 "ASSUMED SIZE and so cannot have a default initializer",
12044 sym->name, &sym->declared_at);
12045 return;
12050 /* F2008, C526. */
12051 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12052 || sym->attr.codimension)
12053 && sym->attr.result)
12054 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
12055 "a coarray component", sym->name, &sym->declared_at);
12057 /* F2008, C524. */
12058 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
12059 && sym->ts.u.derived->ts.is_iso_c)
12060 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12061 "shall not be a coarray", sym->name, &sym->declared_at);
12063 /* F2008, C525. */
12064 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp
12065 && (sym->attr.codimension || sym->attr.pointer || sym->attr.dimension
12066 || sym->attr.allocatable))
12067 gfc_error ("Variable '%s' at %L with coarray component "
12068 "shall be a nonpointer, nonallocatable scalar",
12069 sym->name, &sym->declared_at);
12071 /* F2008, C526. The function-result case was handled above. */
12072 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12073 || sym->attr.codimension)
12074 && !(sym->attr.allocatable || sym->attr.dummy || sym->attr.save
12075 || sym->ns->proc_name->attr.flavor == FL_MODULE
12076 || sym->ns->proc_name->attr.is_main_program
12077 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
12078 gfc_error ("Variable '%s' at %L is a coarray or has a coarray "
12079 "component and is not ALLOCATABLE, SAVE nor a "
12080 "dummy argument", sym->name, &sym->declared_at);
12081 /* F2008, C528. */ /* FIXME: sym->as check due to PR 43412. */
12082 else if (sym->attr.codimension && !sym->attr.allocatable
12083 && sym->as && sym->as->cotype == AS_DEFERRED)
12084 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
12085 "deferred shape", sym->name, &sym->declared_at);
12086 else if (sym->attr.codimension && sym->attr.allocatable
12087 && (sym->as->type != AS_DEFERRED || sym->as->cotype != AS_DEFERRED))
12088 gfc_error ("Allocatable coarray variable '%s' at %L must have "
12089 "deferred shape", sym->name, &sym->declared_at);
12092 /* F2008, C541. */
12093 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
12094 || (sym->attr.codimension && sym->attr.allocatable))
12095 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
12096 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
12097 "allocatable coarray or have coarray components",
12098 sym->name, &sym->declared_at);
12100 if (sym->attr.codimension && sym->attr.dummy
12101 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
12102 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
12103 "procedure '%s'", sym->name, &sym->declared_at,
12104 sym->ns->proc_name->name);
12106 switch (sym->attr.flavor)
12108 case FL_VARIABLE:
12109 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
12110 return;
12111 break;
12113 case FL_PROCEDURE:
12114 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
12115 return;
12116 break;
12118 case FL_NAMELIST:
12119 if (resolve_fl_namelist (sym) == FAILURE)
12120 return;
12121 break;
12123 case FL_PARAMETER:
12124 if (resolve_fl_parameter (sym) == FAILURE)
12125 return;
12126 break;
12128 default:
12129 break;
12132 /* Resolve array specifier. Check as well some constraints
12133 on COMMON blocks. */
12135 check_constant = sym->attr.in_common && !sym->attr.pointer;
12137 /* Set the formal_arg_flag so that check_conflict will not throw
12138 an error for host associated variables in the specification
12139 expression for an array_valued function. */
12140 if (sym->attr.function && sym->as)
12141 formal_arg_flag = 1;
12143 gfc_resolve_array_spec (sym->as, check_constant);
12145 formal_arg_flag = 0;
12147 /* Resolve formal namespaces. */
12148 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
12149 && !sym->attr.contained && !sym->attr.intrinsic)
12150 gfc_resolve (sym->formal_ns);
12152 /* Make sure the formal namespace is present. */
12153 if (sym->formal && !sym->formal_ns)
12155 gfc_formal_arglist *formal = sym->formal;
12156 while (formal && !formal->sym)
12157 formal = formal->next;
12159 if (formal)
12161 sym->formal_ns = formal->sym->ns;
12162 sym->formal_ns->refs++;
12166 /* Check threadprivate restrictions. */
12167 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
12168 && (!sym->attr.in_common
12169 && sym->module == NULL
12170 && (sym->ns->proc_name == NULL
12171 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
12172 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
12174 /* If we have come this far we can apply default-initializers, as
12175 described in 14.7.5, to those variables that have not already
12176 been assigned one. */
12177 if (sym->ts.type == BT_DERIVED
12178 && sym->ns == gfc_current_ns
12179 && !sym->value
12180 && !sym->attr.allocatable
12181 && !sym->attr.alloc_comp)
12183 symbol_attribute *a = &sym->attr;
12185 if ((!a->save && !a->dummy && !a->pointer
12186 && !a->in_common && !a->use_assoc
12187 && (a->referenced || a->result)
12188 && !(a->function && sym != sym->result))
12189 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
12190 apply_default_init (sym);
12193 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
12194 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
12195 && !CLASS_DATA (sym)->attr.class_pointer
12196 && !CLASS_DATA (sym)->attr.allocatable)
12197 apply_default_init (sym);
12199 /* If this symbol has a type-spec, check it. */
12200 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
12201 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
12202 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
12203 == FAILURE)
12204 return;
12208 /************* Resolve DATA statements *************/
12210 static struct
12212 gfc_data_value *vnode;
12213 mpz_t left;
12215 values;
12218 /* Advance the values structure to point to the next value in the data list. */
12220 static gfc_try
12221 next_data_value (void)
12223 while (mpz_cmp_ui (values.left, 0) == 0)
12226 if (values.vnode->next == NULL)
12227 return FAILURE;
12229 values.vnode = values.vnode->next;
12230 mpz_set (values.left, values.vnode->repeat);
12233 return SUCCESS;
12237 static gfc_try
12238 check_data_variable (gfc_data_variable *var, locus *where)
12240 gfc_expr *e;
12241 mpz_t size;
12242 mpz_t offset;
12243 gfc_try t;
12244 ar_type mark = AR_UNKNOWN;
12245 int i;
12246 mpz_t section_index[GFC_MAX_DIMENSIONS];
12247 gfc_ref *ref;
12248 gfc_array_ref *ar;
12249 gfc_symbol *sym;
12250 int has_pointer;
12252 if (gfc_resolve_expr (var->expr) == FAILURE)
12253 return FAILURE;
12255 ar = NULL;
12256 mpz_init_set_si (offset, 0);
12257 e = var->expr;
12259 if (e->expr_type != EXPR_VARIABLE)
12260 gfc_internal_error ("check_data_variable(): Bad expression");
12262 sym = e->symtree->n.sym;
12264 if (sym->ns->is_block_data && !sym->attr.in_common)
12266 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
12267 sym->name, &sym->declared_at);
12270 if (e->ref == NULL && sym->as)
12272 gfc_error ("DATA array '%s' at %L must be specified in a previous"
12273 " declaration", sym->name, where);
12274 return FAILURE;
12277 has_pointer = sym->attr.pointer;
12279 for (ref = e->ref; ref; ref = ref->next)
12281 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
12282 has_pointer = 1;
12284 if (ref->type == REF_ARRAY && ref->u.ar.codimen)
12286 gfc_error ("DATA element '%s' at %L cannot have a coindex",
12287 sym->name, where);
12288 return FAILURE;
12291 if (has_pointer
12292 && ref->type == REF_ARRAY
12293 && ref->u.ar.type != AR_FULL)
12295 gfc_error ("DATA element '%s' at %L is a pointer and so must "
12296 "be a full array", sym->name, where);
12297 return FAILURE;
12301 if (e->rank == 0 || has_pointer)
12303 mpz_init_set_ui (size, 1);
12304 ref = NULL;
12306 else
12308 ref = e->ref;
12310 /* Find the array section reference. */
12311 for (ref = e->ref; ref; ref = ref->next)
12313 if (ref->type != REF_ARRAY)
12314 continue;
12315 if (ref->u.ar.type == AR_ELEMENT)
12316 continue;
12317 break;
12319 gcc_assert (ref);
12321 /* Set marks according to the reference pattern. */
12322 switch (ref->u.ar.type)
12324 case AR_FULL:
12325 mark = AR_FULL;
12326 break;
12328 case AR_SECTION:
12329 ar = &ref->u.ar;
12330 /* Get the start position of array section. */
12331 gfc_get_section_index (ar, section_index, &offset);
12332 mark = AR_SECTION;
12333 break;
12335 default:
12336 gcc_unreachable ();
12339 if (gfc_array_size (e, &size) == FAILURE)
12341 gfc_error ("Nonconstant array section at %L in DATA statement",
12342 &e->where);
12343 mpz_clear (offset);
12344 return FAILURE;
12348 t = SUCCESS;
12350 while (mpz_cmp_ui (size, 0) > 0)
12352 if (next_data_value () == FAILURE)
12354 gfc_error ("DATA statement at %L has more variables than values",
12355 where);
12356 t = FAILURE;
12357 break;
12360 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
12361 if (t == FAILURE)
12362 break;
12364 /* If we have more than one element left in the repeat count,
12365 and we have more than one element left in the target variable,
12366 then create a range assignment. */
12367 /* FIXME: Only done for full arrays for now, since array sections
12368 seem tricky. */
12369 if (mark == AR_FULL && ref && ref->next == NULL
12370 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
12372 mpz_t range;
12374 if (mpz_cmp (size, values.left) >= 0)
12376 mpz_init_set (range, values.left);
12377 mpz_sub (size, size, values.left);
12378 mpz_set_ui (values.left, 0);
12380 else
12382 mpz_init_set (range, size);
12383 mpz_sub (values.left, values.left, size);
12384 mpz_set_ui (size, 0);
12387 t = gfc_assign_data_value_range (var->expr, values.vnode->expr,
12388 offset, range);
12390 mpz_add (offset, offset, range);
12391 mpz_clear (range);
12393 if (t == FAILURE)
12394 break;
12397 /* Assign initial value to symbol. */
12398 else
12400 mpz_sub_ui (values.left, values.left, 1);
12401 mpz_sub_ui (size, size, 1);
12403 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
12404 if (t == FAILURE)
12405 break;
12407 if (mark == AR_FULL)
12408 mpz_add_ui (offset, offset, 1);
12410 /* Modify the array section indexes and recalculate the offset
12411 for next element. */
12412 else if (mark == AR_SECTION)
12413 gfc_advance_section (section_index, ar, &offset);
12417 if (mark == AR_SECTION)
12419 for (i = 0; i < ar->dimen; i++)
12420 mpz_clear (section_index[i]);
12423 mpz_clear (size);
12424 mpz_clear (offset);
12426 return t;
12430 static gfc_try traverse_data_var (gfc_data_variable *, locus *);
12432 /* Iterate over a list of elements in a DATA statement. */
12434 static gfc_try
12435 traverse_data_list (gfc_data_variable *var, locus *where)
12437 mpz_t trip;
12438 iterator_stack frame;
12439 gfc_expr *e, *start, *end, *step;
12440 gfc_try retval = SUCCESS;
12442 mpz_init (frame.value);
12443 mpz_init (trip);
12445 start = gfc_copy_expr (var->iter.start);
12446 end = gfc_copy_expr (var->iter.end);
12447 step = gfc_copy_expr (var->iter.step);
12449 if (gfc_simplify_expr (start, 1) == FAILURE
12450 || start->expr_type != EXPR_CONSTANT)
12452 gfc_error ("start of implied-do loop at %L could not be "
12453 "simplified to a constant value", &start->where);
12454 retval = FAILURE;
12455 goto cleanup;
12457 if (gfc_simplify_expr (end, 1) == FAILURE
12458 || end->expr_type != EXPR_CONSTANT)
12460 gfc_error ("end of implied-do loop at %L could not be "
12461 "simplified to a constant value", &start->where);
12462 retval = FAILURE;
12463 goto cleanup;
12465 if (gfc_simplify_expr (step, 1) == FAILURE
12466 || step->expr_type != EXPR_CONSTANT)
12468 gfc_error ("step of implied-do loop at %L could not be "
12469 "simplified to a constant value", &start->where);
12470 retval = FAILURE;
12471 goto cleanup;
12474 mpz_set (trip, end->value.integer);
12475 mpz_sub (trip, trip, start->value.integer);
12476 mpz_add (trip, trip, step->value.integer);
12478 mpz_div (trip, trip, step->value.integer);
12480 mpz_set (frame.value, start->value.integer);
12482 frame.prev = iter_stack;
12483 frame.variable = var->iter.var->symtree;
12484 iter_stack = &frame;
12486 while (mpz_cmp_ui (trip, 0) > 0)
12488 if (traverse_data_var (var->list, where) == FAILURE)
12490 retval = FAILURE;
12491 goto cleanup;
12494 e = gfc_copy_expr (var->expr);
12495 if (gfc_simplify_expr (e, 1) == FAILURE)
12497 gfc_free_expr (e);
12498 retval = FAILURE;
12499 goto cleanup;
12502 mpz_add (frame.value, frame.value, step->value.integer);
12504 mpz_sub_ui (trip, trip, 1);
12507 cleanup:
12508 mpz_clear (frame.value);
12509 mpz_clear (trip);
12511 gfc_free_expr (start);
12512 gfc_free_expr (end);
12513 gfc_free_expr (step);
12515 iter_stack = frame.prev;
12516 return retval;
12520 /* Type resolve variables in the variable list of a DATA statement. */
12522 static gfc_try
12523 traverse_data_var (gfc_data_variable *var, locus *where)
12525 gfc_try t;
12527 for (; var; var = var->next)
12529 if (var->expr == NULL)
12530 t = traverse_data_list (var, where);
12531 else
12532 t = check_data_variable (var, where);
12534 if (t == FAILURE)
12535 return FAILURE;
12538 return SUCCESS;
12542 /* Resolve the expressions and iterators associated with a data statement.
12543 This is separate from the assignment checking because data lists should
12544 only be resolved once. */
12546 static gfc_try
12547 resolve_data_variables (gfc_data_variable *d)
12549 for (; d; d = d->next)
12551 if (d->list == NULL)
12553 if (gfc_resolve_expr (d->expr) == FAILURE)
12554 return FAILURE;
12556 else
12558 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
12559 return FAILURE;
12561 if (resolve_data_variables (d->list) == FAILURE)
12562 return FAILURE;
12566 return SUCCESS;
12570 /* Resolve a single DATA statement. We implement this by storing a pointer to
12571 the value list into static variables, and then recursively traversing the
12572 variables list, expanding iterators and such. */
12574 static void
12575 resolve_data (gfc_data *d)
12578 if (resolve_data_variables (d->var) == FAILURE)
12579 return;
12581 values.vnode = d->value;
12582 if (d->value == NULL)
12583 mpz_set_ui (values.left, 0);
12584 else
12585 mpz_set (values.left, d->value->repeat);
12587 if (traverse_data_var (d->var, &d->where) == FAILURE)
12588 return;
12590 /* At this point, we better not have any values left. */
12592 if (next_data_value () == SUCCESS)
12593 gfc_error ("DATA statement at %L has more values than variables",
12594 &d->where);
12598 /* 12.6 Constraint: In a pure subprogram any variable which is in common or
12599 accessed by host or use association, is a dummy argument to a pure function,
12600 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
12601 is storage associated with any such variable, shall not be used in the
12602 following contexts: (clients of this function). */
12604 /* Determines if a variable is not 'pure', i.e., not assignable within a pure
12605 procedure. Returns zero if assignment is OK, nonzero if there is a
12606 problem. */
12608 gfc_impure_variable (gfc_symbol *sym)
12610 gfc_symbol *proc;
12611 gfc_namespace *ns;
12613 if (sym->attr.use_assoc || sym->attr.in_common)
12614 return 1;
12616 /* Check if the symbol's ns is inside the pure procedure. */
12617 for (ns = gfc_current_ns; ns; ns = ns->parent)
12619 if (ns == sym->ns)
12620 break;
12621 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
12622 return 1;
12625 proc = sym->ns->proc_name;
12626 if (sym->attr.dummy && gfc_pure (proc)
12627 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
12629 proc->attr.function))
12630 return 1;
12632 /* TODO: Sort out what can be storage associated, if anything, and include
12633 it here. In principle equivalences should be scanned but it does not
12634 seem to be possible to storage associate an impure variable this way. */
12635 return 0;
12639 /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
12640 current namespace is inside a pure procedure. */
12643 gfc_pure (gfc_symbol *sym)
12645 symbol_attribute attr;
12646 gfc_namespace *ns;
12648 if (sym == NULL)
12650 /* Check if the current namespace or one of its parents
12651 belongs to a pure procedure. */
12652 for (ns = gfc_current_ns; ns; ns = ns->parent)
12654 sym = ns->proc_name;
12655 if (sym == NULL)
12656 return 0;
12657 attr = sym->attr;
12658 if (attr.flavor == FL_PROCEDURE && attr.pure)
12659 return 1;
12661 return 0;
12664 attr = sym->attr;
12666 return attr.flavor == FL_PROCEDURE && attr.pure;
12670 /* Test whether the current procedure is elemental or not. */
12673 gfc_elemental (gfc_symbol *sym)
12675 symbol_attribute attr;
12677 if (sym == NULL)
12678 sym = gfc_current_ns->proc_name;
12679 if (sym == NULL)
12680 return 0;
12681 attr = sym->attr;
12683 return attr.flavor == FL_PROCEDURE && attr.elemental;
12687 /* Warn about unused labels. */
12689 static void
12690 warn_unused_fortran_label (gfc_st_label *label)
12692 if (label == NULL)
12693 return;
12695 warn_unused_fortran_label (label->left);
12697 if (label->defined == ST_LABEL_UNKNOWN)
12698 return;
12700 switch (label->referenced)
12702 case ST_LABEL_UNKNOWN:
12703 gfc_warning ("Label %d at %L defined but not used", label->value,
12704 &label->where);
12705 break;
12707 case ST_LABEL_BAD_TARGET:
12708 gfc_warning ("Label %d at %L defined but cannot be used",
12709 label->value, &label->where);
12710 break;
12712 default:
12713 break;
12716 warn_unused_fortran_label (label->right);
12720 /* Returns the sequence type of a symbol or sequence. */
12722 static seq_type
12723 sequence_type (gfc_typespec ts)
12725 seq_type result;
12726 gfc_component *c;
12728 switch (ts.type)
12730 case BT_DERIVED:
12732 if (ts.u.derived->components == NULL)
12733 return SEQ_NONDEFAULT;
12735 result = sequence_type (ts.u.derived->components->ts);
12736 for (c = ts.u.derived->components->next; c; c = c->next)
12737 if (sequence_type (c->ts) != result)
12738 return SEQ_MIXED;
12740 return result;
12742 case BT_CHARACTER:
12743 if (ts.kind != gfc_default_character_kind)
12744 return SEQ_NONDEFAULT;
12746 return SEQ_CHARACTER;
12748 case BT_INTEGER:
12749 if (ts.kind != gfc_default_integer_kind)
12750 return SEQ_NONDEFAULT;
12752 return SEQ_NUMERIC;
12754 case BT_REAL:
12755 if (!(ts.kind == gfc_default_real_kind
12756 || ts.kind == gfc_default_double_kind))
12757 return SEQ_NONDEFAULT;
12759 return SEQ_NUMERIC;
12761 case BT_COMPLEX:
12762 if (ts.kind != gfc_default_complex_kind)
12763 return SEQ_NONDEFAULT;
12765 return SEQ_NUMERIC;
12767 case BT_LOGICAL:
12768 if (ts.kind != gfc_default_logical_kind)
12769 return SEQ_NONDEFAULT;
12771 return SEQ_NUMERIC;
12773 default:
12774 return SEQ_NONDEFAULT;
12779 /* Resolve derived type EQUIVALENCE object. */
12781 static gfc_try
12782 resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
12784 gfc_component *c = derived->components;
12786 if (!derived)
12787 return SUCCESS;
12789 /* Shall not be an object of nonsequence derived type. */
12790 if (!derived->attr.sequence)
12792 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
12793 "attribute to be an EQUIVALENCE object", sym->name,
12794 &e->where);
12795 return FAILURE;
12798 /* Shall not have allocatable components. */
12799 if (derived->attr.alloc_comp)
12801 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
12802 "components to be an EQUIVALENCE object",sym->name,
12803 &e->where);
12804 return FAILURE;
12807 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
12809 gfc_error ("Derived type variable '%s' at %L with default "
12810 "initialization cannot be in EQUIVALENCE with a variable "
12811 "in COMMON", sym->name, &e->where);
12812 return FAILURE;
12815 for (; c ; c = c->next)
12817 if (c->ts.type == BT_DERIVED
12818 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
12819 return FAILURE;
12821 /* Shall not be an object of sequence derived type containing a pointer
12822 in the structure. */
12823 if (c->attr.pointer)
12825 gfc_error ("Derived type variable '%s' at %L with pointer "
12826 "component(s) cannot be an EQUIVALENCE object",
12827 sym->name, &e->where);
12828 return FAILURE;
12831 return SUCCESS;
12835 /* Resolve equivalence object.
12836 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
12837 an allocatable array, an object of nonsequence derived type, an object of
12838 sequence derived type containing a pointer at any level of component
12839 selection, an automatic object, a function name, an entry name, a result
12840 name, a named constant, a structure component, or a subobject of any of
12841 the preceding objects. A substring shall not have length zero. A
12842 derived type shall not have components with default initialization nor
12843 shall two objects of an equivalence group be initialized.
12844 Either all or none of the objects shall have an protected attribute.
12845 The simple constraints are done in symbol.c(check_conflict) and the rest
12846 are implemented here. */
12848 static void
12849 resolve_equivalence (gfc_equiv *eq)
12851 gfc_symbol *sym;
12852 gfc_symbol *first_sym;
12853 gfc_expr *e;
12854 gfc_ref *r;
12855 locus *last_where = NULL;
12856 seq_type eq_type, last_eq_type;
12857 gfc_typespec *last_ts;
12858 int object, cnt_protected;
12859 const char *msg;
12861 last_ts = &eq->expr->symtree->n.sym->ts;
12863 first_sym = eq->expr->symtree->n.sym;
12865 cnt_protected = 0;
12867 for (object = 1; eq; eq = eq->eq, object++)
12869 e = eq->expr;
12871 e->ts = e->symtree->n.sym->ts;
12872 /* match_varspec might not know yet if it is seeing
12873 array reference or substring reference, as it doesn't
12874 know the types. */
12875 if (e->ref && e->ref->type == REF_ARRAY)
12877 gfc_ref *ref = e->ref;
12878 sym = e->symtree->n.sym;
12880 if (sym->attr.dimension)
12882 ref->u.ar.as = sym->as;
12883 ref = ref->next;
12886 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
12887 if (e->ts.type == BT_CHARACTER
12888 && ref
12889 && ref->type == REF_ARRAY
12890 && ref->u.ar.dimen == 1
12891 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
12892 && ref->u.ar.stride[0] == NULL)
12894 gfc_expr *start = ref->u.ar.start[0];
12895 gfc_expr *end = ref->u.ar.end[0];
12896 void *mem = NULL;
12898 /* Optimize away the (:) reference. */
12899 if (start == NULL && end == NULL)
12901 if (e->ref == ref)
12902 e->ref = ref->next;
12903 else
12904 e->ref->next = ref->next;
12905 mem = ref;
12907 else
12909 ref->type = REF_SUBSTRING;
12910 if (start == NULL)
12911 start = gfc_get_int_expr (gfc_default_integer_kind,
12912 NULL, 1);
12913 ref->u.ss.start = start;
12914 if (end == NULL && e->ts.u.cl)
12915 end = gfc_copy_expr (e->ts.u.cl->length);
12916 ref->u.ss.end = end;
12917 ref->u.ss.length = e->ts.u.cl;
12918 e->ts.u.cl = NULL;
12920 ref = ref->next;
12921 gfc_free (mem);
12924 /* Any further ref is an error. */
12925 if (ref)
12927 gcc_assert (ref->type == REF_ARRAY);
12928 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
12929 &ref->u.ar.where);
12930 continue;
12934 if (gfc_resolve_expr (e) == FAILURE)
12935 continue;
12937 sym = e->symtree->n.sym;
12939 if (sym->attr.is_protected)
12940 cnt_protected++;
12941 if (cnt_protected > 0 && cnt_protected != object)
12943 gfc_error ("Either all or none of the objects in the "
12944 "EQUIVALENCE set at %L shall have the "
12945 "PROTECTED attribute",
12946 &e->where);
12947 break;
12950 /* Shall not equivalence common block variables in a PURE procedure. */
12951 if (sym->ns->proc_name
12952 && sym->ns->proc_name->attr.pure
12953 && sym->attr.in_common)
12955 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
12956 "object in the pure procedure '%s'",
12957 sym->name, &e->where, sym->ns->proc_name->name);
12958 break;
12961 /* Shall not be a named constant. */
12962 if (e->expr_type == EXPR_CONSTANT)
12964 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
12965 "object", sym->name, &e->where);
12966 continue;
12969 if (e->ts.type == BT_DERIVED
12970 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
12971 continue;
12973 /* Check that the types correspond correctly:
12974 Note 5.28:
12975 A numeric sequence structure may be equivalenced to another sequence
12976 structure, an object of default integer type, default real type, double
12977 precision real type, default logical type such that components of the
12978 structure ultimately only become associated to objects of the same
12979 kind. A character sequence structure may be equivalenced to an object
12980 of default character kind or another character sequence structure.
12981 Other objects may be equivalenced only to objects of the same type and
12982 kind parameters. */
12984 /* Identical types are unconditionally OK. */
12985 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
12986 goto identical_types;
12988 last_eq_type = sequence_type (*last_ts);
12989 eq_type = sequence_type (sym->ts);
12991 /* Since the pair of objects is not of the same type, mixed or
12992 non-default sequences can be rejected. */
12994 msg = "Sequence %s with mixed components in EQUIVALENCE "
12995 "statement at %L with different type objects";
12996 if ((object ==2
12997 && last_eq_type == SEQ_MIXED
12998 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
12999 == FAILURE)
13000 || (eq_type == SEQ_MIXED
13001 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13002 &e->where) == FAILURE))
13003 continue;
13005 msg = "Non-default type object or sequence %s in EQUIVALENCE "
13006 "statement at %L with objects of different type";
13007 if ((object ==2
13008 && last_eq_type == SEQ_NONDEFAULT
13009 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
13010 last_where) == FAILURE)
13011 || (eq_type == SEQ_NONDEFAULT
13012 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13013 &e->where) == FAILURE))
13014 continue;
13016 msg ="Non-CHARACTER object '%s' in default CHARACTER "
13017 "EQUIVALENCE statement at %L";
13018 if (last_eq_type == SEQ_CHARACTER
13019 && eq_type != SEQ_CHARACTER
13020 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13021 &e->where) == FAILURE)
13022 continue;
13024 msg ="Non-NUMERIC object '%s' in default NUMERIC "
13025 "EQUIVALENCE statement at %L";
13026 if (last_eq_type == SEQ_NUMERIC
13027 && eq_type != SEQ_NUMERIC
13028 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
13029 &e->where) == FAILURE)
13030 continue;
13032 identical_types:
13033 last_ts =&sym->ts;
13034 last_where = &e->where;
13036 if (!e->ref)
13037 continue;
13039 /* Shall not be an automatic array. */
13040 if (e->ref->type == REF_ARRAY
13041 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
13043 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
13044 "an EQUIVALENCE object", sym->name, &e->where);
13045 continue;
13048 r = e->ref;
13049 while (r)
13051 /* Shall not be a structure component. */
13052 if (r->type == REF_COMPONENT)
13054 gfc_error ("Structure component '%s' at %L cannot be an "
13055 "EQUIVALENCE object",
13056 r->u.c.component->name, &e->where);
13057 break;
13060 /* A substring shall not have length zero. */
13061 if (r->type == REF_SUBSTRING)
13063 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
13065 gfc_error ("Substring at %L has length zero",
13066 &r->u.ss.start->where);
13067 break;
13070 r = r->next;
13076 /* Resolve function and ENTRY types, issue diagnostics if needed. */
13078 static void
13079 resolve_fntype (gfc_namespace *ns)
13081 gfc_entry_list *el;
13082 gfc_symbol *sym;
13084 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
13085 return;
13087 /* If there are any entries, ns->proc_name is the entry master
13088 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
13089 if (ns->entries)
13090 sym = ns->entries->sym;
13091 else
13092 sym = ns->proc_name;
13093 if (sym->result == sym
13094 && sym->ts.type == BT_UNKNOWN
13095 && gfc_set_default_type (sym, 0, NULL) == FAILURE
13096 && !sym->attr.untyped)
13098 gfc_error ("Function '%s' at %L has no IMPLICIT type",
13099 sym->name, &sym->declared_at);
13100 sym->attr.untyped = 1;
13103 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
13104 && !sym->attr.contained
13105 && !gfc_check_access (sym->ts.u.derived->attr.access,
13106 sym->ts.u.derived->ns->default_access)
13107 && gfc_check_access (sym->attr.access, sym->ns->default_access))
13109 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
13110 "%L of PRIVATE type '%s'", sym->name,
13111 &sym->declared_at, sym->ts.u.derived->name);
13114 if (ns->entries)
13115 for (el = ns->entries->next; el; el = el->next)
13117 if (el->sym->result == el->sym
13118 && el->sym->ts.type == BT_UNKNOWN
13119 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
13120 && !el->sym->attr.untyped)
13122 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
13123 el->sym->name, &el->sym->declared_at);
13124 el->sym->attr.untyped = 1;
13130 /* 12.3.2.1.1 Defined operators. */
13132 static gfc_try
13133 check_uop_procedure (gfc_symbol *sym, locus where)
13135 gfc_formal_arglist *formal;
13137 if (!sym->attr.function)
13139 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
13140 sym->name, &where);
13141 return FAILURE;
13144 if (sym->ts.type == BT_CHARACTER
13145 && !(sym->ts.u.cl && sym->ts.u.cl->length)
13146 && !(sym->result && sym->result->ts.u.cl
13147 && sym->result->ts.u.cl->length))
13149 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
13150 "character length", sym->name, &where);
13151 return FAILURE;
13154 formal = sym->formal;
13155 if (!formal || !formal->sym)
13157 gfc_error ("User operator procedure '%s' at %L must have at least "
13158 "one argument", sym->name, &where);
13159 return FAILURE;
13162 if (formal->sym->attr.intent != INTENT_IN)
13164 gfc_error ("First argument of operator interface at %L must be "
13165 "INTENT(IN)", &where);
13166 return FAILURE;
13169 if (formal->sym->attr.optional)
13171 gfc_error ("First argument of operator interface at %L cannot be "
13172 "optional", &where);
13173 return FAILURE;
13176 formal = formal->next;
13177 if (!formal || !formal->sym)
13178 return SUCCESS;
13180 if (formal->sym->attr.intent != INTENT_IN)
13182 gfc_error ("Second argument of operator interface at %L must be "
13183 "INTENT(IN)", &where);
13184 return FAILURE;
13187 if (formal->sym->attr.optional)
13189 gfc_error ("Second argument of operator interface at %L cannot be "
13190 "optional", &where);
13191 return FAILURE;
13194 if (formal->next)
13196 gfc_error ("Operator interface at %L must have, at most, two "
13197 "arguments", &where);
13198 return FAILURE;
13201 return SUCCESS;
13204 static void
13205 gfc_resolve_uops (gfc_symtree *symtree)
13207 gfc_interface *itr;
13209 if (symtree == NULL)
13210 return;
13212 gfc_resolve_uops (symtree->left);
13213 gfc_resolve_uops (symtree->right);
13215 for (itr = symtree->n.uop->op; itr; itr = itr->next)
13216 check_uop_procedure (itr->sym, itr->sym->declared_at);
13220 /* Examine all of the expressions associated with a program unit,
13221 assign types to all intermediate expressions, make sure that all
13222 assignments are to compatible types and figure out which names
13223 refer to which functions or subroutines. It doesn't check code
13224 block, which is handled by resolve_code. */
13226 static void
13227 resolve_types (gfc_namespace *ns)
13229 gfc_namespace *n;
13230 gfc_charlen *cl;
13231 gfc_data *d;
13232 gfc_equiv *eq;
13233 gfc_namespace* old_ns = gfc_current_ns;
13235 /* Check that all IMPLICIT types are ok. */
13236 if (!ns->seen_implicit_none)
13238 unsigned letter;
13239 for (letter = 0; letter != GFC_LETTERS; ++letter)
13240 if (ns->set_flag[letter]
13241 && resolve_typespec_used (&ns->default_type[letter],
13242 &ns->implicit_loc[letter],
13243 NULL) == FAILURE)
13244 return;
13247 gfc_current_ns = ns;
13249 resolve_entries (ns);
13251 resolve_common_vars (ns->blank_common.head, false);
13252 resolve_common_blocks (ns->common_root);
13254 resolve_contained_functions (ns);
13256 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
13258 for (cl = ns->cl_list; cl; cl = cl->next)
13259 resolve_charlen (cl);
13261 gfc_traverse_ns (ns, resolve_symbol);
13263 resolve_fntype (ns);
13265 for (n = ns->contained; n; n = n->sibling)
13267 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
13268 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
13269 "also be PURE", n->proc_name->name,
13270 &n->proc_name->declared_at);
13272 resolve_types (n);
13275 forall_flag = 0;
13276 gfc_check_interfaces (ns);
13278 gfc_traverse_ns (ns, resolve_values);
13280 if (ns->save_all)
13281 gfc_save_all (ns);
13283 iter_stack = NULL;
13284 for (d = ns->data; d; d = d->next)
13285 resolve_data (d);
13287 iter_stack = NULL;
13288 gfc_traverse_ns (ns, gfc_formalize_init_value);
13290 gfc_traverse_ns (ns, gfc_verify_binding_labels);
13292 if (ns->common_root != NULL)
13293 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
13295 for (eq = ns->equiv; eq; eq = eq->next)
13296 resolve_equivalence (eq);
13298 /* Warn about unused labels. */
13299 if (warn_unused_label)
13300 warn_unused_fortran_label (ns->st_labels);
13302 gfc_resolve_uops (ns->uop_root);
13304 gfc_current_ns = old_ns;
13308 /* Call resolve_code recursively. */
13310 static void
13311 resolve_codes (gfc_namespace *ns)
13313 gfc_namespace *n;
13314 bitmap_obstack old_obstack;
13316 for (n = ns->contained; n; n = n->sibling)
13317 resolve_codes (n);
13319 gfc_current_ns = ns;
13321 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
13322 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
13323 cs_base = NULL;
13325 /* Set to an out of range value. */
13326 current_entry_id = -1;
13328 old_obstack = labels_obstack;
13329 bitmap_obstack_initialize (&labels_obstack);
13331 resolve_code (ns->code, ns);
13333 bitmap_obstack_release (&labels_obstack);
13334 labels_obstack = old_obstack;
13338 /* This function is called after a complete program unit has been compiled.
13339 Its purpose is to examine all of the expressions associated with a program
13340 unit, assign types to all intermediate expressions, make sure that all
13341 assignments are to compatible types and figure out which names refer to
13342 which functions or subroutines. */
13344 void
13345 gfc_resolve (gfc_namespace *ns)
13347 gfc_namespace *old_ns;
13348 code_stack *old_cs_base;
13350 if (ns->resolved)
13351 return;
13353 ns->resolved = -1;
13354 old_ns = gfc_current_ns;
13355 old_cs_base = cs_base;
13357 resolve_types (ns);
13358 resolve_codes (ns);
13360 gfc_current_ns = old_ns;
13361 cs_base = old_cs_base;
13362 ns->resolved = 1;
13364 gfc_run_passes (ns);